From c233f2190c8d71508b5de40c18ad21d245a10de4 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 19 Sep 2013 13:45:48 -0400 Subject: [PATCH] fixes #518 - Add api param for watched indicator on images --- .../DefaultTheme/DefaultThemeService.cs | 1 + MediaBrowser.Api/Images/ImageRequest.cs | 4 +++ MediaBrowser.Api/Images/ImageService.cs | 24 +++++++++++++- MediaBrowser.Api/Images/ImageWriter.cs | 3 +- .../Drawing/ImageProcessingOptions.cs | 8 +++++ .../Drawing/ImageProcessor.cs | 32 +++++++++++++----- .../Drawing/WatchedIndicatorDrawer.cs | 33 +++++++++++++++++++ ...MediaBrowser.Server.Implementations.csproj | 1 + 8 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 MediaBrowser.Server.Implementations/Drawing/WatchedIndicatorDrawer.cs diff --git a/MediaBrowser.Api/DefaultTheme/DefaultThemeService.cs b/MediaBrowser.Api/DefaultTheme/DefaultThemeService.cs index e682921765..736710c546 100644 --- a/MediaBrowser.Api/DefaultTheme/DefaultThemeService.cs +++ b/MediaBrowser.Api/DefaultTheme/DefaultThemeService.cs @@ -175,6 +175,7 @@ namespace MediaBrowser.Api.DefaultTheme var dtos = FilterItemsForBackdropDisplay(seriesWithBackdrops) .OrderBy(i => Guid.NewGuid()) .Take(50) + .AsParallel() .Select(i => _dtoService.GetBaseItemDto(i, fields, user)); view.SpotlightItems = dtos.ToArray(); diff --git a/MediaBrowser.Api/Images/ImageRequest.cs b/MediaBrowser.Api/Images/ImageRequest.cs index 719b0de5ea..1302b50002 100644 --- a/MediaBrowser.Api/Images/ImageRequest.cs +++ b/MediaBrowser.Api/Images/ImageRequest.cs @@ -55,12 +55,16 @@ namespace MediaBrowser.Api.Images [ApiMember(Name = "Format", Description = "Determines the output foramt of the image - original,gif,jpg,png", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")] public ImageOutputFormat Format { get; set; } + + [ApiMember(Name = "Indicator", Description = "Determines what overlay to render, if any. none, watched.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")] + public ImageOverlay Indicator { get; set; } public ImageRequest() { EnableImageEnhancers = true; Format = ImageOutputFormat.Original; + Indicator = ImageOverlay.None; } } diff --git a/MediaBrowser.Api/Images/ImageService.cs b/MediaBrowser.Api/Images/ImageService.cs index 9f1c235ad3..0a76175fd0 100644 --- a/MediaBrowser.Api/Images/ImageService.cs +++ b/MediaBrowser.Api/Images/ImageService.cs @@ -747,7 +747,7 @@ namespace MediaBrowser.Api.Images throw new ResourceNotFoundException(string.Format("File not found: {0}", imagePath)); } - var contentType = MimeTypes.GetMimeType(imagePath); + var contentType = GetMimeType(request.Format, imagePath); var cacheGuid = _imageProcessor.GetImageCacheTag(item, request.Type, imagePath, originalFileImageDateModified, supportedImageEnhancers); @@ -774,6 +774,28 @@ namespace MediaBrowser.Api.Images }, contentType); } + private string GetMimeType(ImageOutputFormat format, string path) + { + if (format == ImageOutputFormat.Bmp) + { + return MimeTypes.GetMimeType("i.bmp"); + } + if (format == ImageOutputFormat.Gif) + { + return MimeTypes.GetMimeType("i.gif"); + } + if (format == ImageOutputFormat.Jpg) + { + return MimeTypes.GetMimeType("i.jpg"); + } + if (format == ImageOutputFormat.Png) + { + return MimeTypes.GetMimeType("i.png"); + } + + return MimeTypes.GetMimeType(path); + } + /// /// Gets the image path. /// diff --git a/MediaBrowser.Api/Images/ImageWriter.cs b/MediaBrowser.Api/Images/ImageWriter.cs index 03266fc996..be79878aa5 100644 --- a/MediaBrowser.Api/Images/ImageWriter.cs +++ b/MediaBrowser.Api/Images/ImageWriter.cs @@ -89,7 +89,8 @@ namespace MediaBrowser.Api.Images OriginalImagePath = OriginalImagePath, Quality = Request.Quality, Width = Request.Width, - OutputFormat = Request.Format + OutputFormat = Request.Format, + Indicator = Request.Indicator }; return ImageProcessor.ProcessImage(options, responseStream); diff --git a/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs b/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs index 7a56015adb..b7f092fb85 100644 --- a/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs +++ b/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs @@ -33,6 +33,8 @@ namespace MediaBrowser.Controller.Drawing public List Enhancers { get; set; } public ImageOutputFormat OutputFormat { get; set; } + + public ImageOverlay Indicator { get; set; } } public enum ImageOutputFormat @@ -43,4 +45,10 @@ namespace MediaBrowser.Controller.Drawing Jpg, Png } + + public enum ImageOverlay + { + None, + Watched + } } diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs index 07a944e403..ff532b9dd9 100644 --- a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs +++ b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs @@ -109,7 +109,7 @@ namespace MediaBrowser.Server.Implementations.Drawing var quality = options.Quality ?? 90; - var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat); + var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat, options.Indicator); try { @@ -175,6 +175,8 @@ namespace MediaBrowser.Server.Implementations.Drawing thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight); + DrawIndicator(thumbnailGraph, newWidth, newHeight, options.Indicator); + var outputFormat = GetOutputFormat(originalImage, options.OutputFormat); using (var outputMemoryStream = new MemoryStream()) @@ -204,6 +206,20 @@ namespace MediaBrowser.Server.Implementations.Drawing } } + private WatchedIndicatorDrawer _watchedDrawer; + + private void DrawIndicator(Graphics graphics, int imageWidth, int imageHeight, ImageOverlay indicator) + { + if (indicator == ImageOverlay.Watched) + { + _watchedDrawer = _watchedDrawer ?? (_watchedDrawer = new WatchedIndicatorDrawer()); + + var currentImageSize = new Size(imageWidth, imageHeight); + + _watchedDrawer.Process(graphics, currentImageSize); + } + } + /// /// Gets the output format. /// @@ -322,12 +338,7 @@ namespace MediaBrowser.Server.Implementations.Drawing /// /// Gets the cache file path based on a set of parameters /// - /// The path to the original image file - /// The size to output the image in - /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. - /// The last modified date of the image - /// System.String. - private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageOutputFormat format) + private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageOutputFormat format, ImageOverlay overlay) { var filename = originalPath; @@ -344,6 +355,11 @@ namespace MediaBrowser.Server.Implementations.Drawing filename += "format=" + format; } + if (overlay != ImageOverlay.None) + { + filename += "overlay=" + overlay; + } + return GetCachePath(_resizedImageCachePath, filename, Path.GetExtension(originalPath)); } @@ -506,7 +522,7 @@ namespace MediaBrowser.Server.Implementations.Drawing return string.Join("|", cacheKeys.ToArray()).GetMD5(); } - private async Task> GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item, + private async Task> GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item, ImageType imageType, int imageIndex, List enhancers) { diff --git a/MediaBrowser.Server.Implementations/Drawing/WatchedIndicatorDrawer.cs b/MediaBrowser.Server.Implementations/Drawing/WatchedIndicatorDrawer.cs new file mode 100644 index 0000000000..921494dba6 --- /dev/null +++ b/MediaBrowser.Server.Implementations/Drawing/WatchedIndicatorDrawer.cs @@ -0,0 +1,33 @@ +using System.Drawing; + +namespace MediaBrowser.Server.Implementations.Drawing +{ + public class WatchedIndicatorDrawer + { + private const int IndicatorHeight = 50; + private const int FontSize = 50; + + public void Process(Graphics graphics, Size imageSize) + { + var x = imageSize.Width - IndicatorHeight; + + using (var backdroundBrush = new SolidBrush(Color.FromArgb(225, 204, 51, 51))) + { + graphics.FillRectangle(backdroundBrush, x, 0, IndicatorHeight, IndicatorHeight); + + const string text = "a"; + + x = imageSize.Width - 55; + + using (var font = new Font("Webdings", FontSize, FontStyle.Regular, GraphicsUnit.Pixel)) + { + using (var fontBrush = new SolidBrush(Color.White)) + { + graphics.DrawString(text, font, fontBrush, x, -2); + } + } + } + + } + } +} diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index ff9ff4735c..80fee7d97a 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -114,6 +114,7 @@ +