From bd6c2d2a22c099e35fd69816e28b5c46a83176b1 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Tue, 18 Sep 2012 18:32:37 -0400 Subject: A few more image improvements --- .../Drawing/BaseImageProcessor.cs | 55 ++++++++++++-------- MediaBrowser.Controller/Drawing/ImageProcessor.cs | 59 +++++++++++++++------- 2 files changed, 76 insertions(+), 38 deletions(-) (limited to 'MediaBrowser.Controller') diff --git a/MediaBrowser.Controller/Drawing/BaseImageProcessor.cs b/MediaBrowser.Controller/Drawing/BaseImageProcessor.cs index ebd0e22c86..8fc6564e7e 100644 --- a/MediaBrowser.Controller/Drawing/BaseImageProcessor.cs +++ b/MediaBrowser.Controller/Drawing/BaseImageProcessor.cs @@ -1,5 +1,6 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Model.Entities; +using System; using System.ComponentModel.Composition; using System.Drawing; using System.Drawing.Drawing2D; @@ -15,16 +16,7 @@ namespace MediaBrowser.Controller.Drawing public abstract class BaseImageProcessor { /// - /// Processes the primary image for a BaseEntity (Person, Studio, User, etc) - /// - /// The original Image, before re-sizing - /// The bitmap holding the original image, after re-sizing - /// The graphics surface on which the output is drawn - /// The entity that owns the image - public abstract void ProcessImage(Image originalImage, Bitmap bitmap, Graphics graphics, BaseEntity entity); - - /// - /// Processes an image for a BaseItem + /// Processes an image for a BaseEntity /// /// The original Image, before re-sizing /// The bitmap holding the original image, after re-sizing @@ -32,10 +24,11 @@ namespace MediaBrowser.Controller.Drawing /// The entity that owns the image /// The image type /// The image index (currently only used with backdrops) - public abstract void ProcessImage(Image originalImage, Bitmap bitmap, Graphics graphics, BaseItem entity, ImageType imageType, int imageIndex); + public abstract void ProcessImage(Image originalImage, Bitmap bitmap, Graphics graphics, BaseEntity entity, ImageType imageType, int imageIndex); /// /// If true, the image output format will be forced to png, resulting in an output size that will generally run larger than jpg + /// If false, the original image format is preserved. /// public virtual bool RequiresTransparency { @@ -44,6 +37,18 @@ namespace MediaBrowser.Controller.Drawing return false; } } + + /// + /// Determines if the image processor is configured to process the specified entity, image type and image index + /// This will aid http response caching so that we don't invalidate image caches when we don't have to + /// + public abstract bool IsConfiguredToProcess(BaseEntity entity, ImageType imageType, int imageIndex); + + /// + /// This is used for caching purposes, since a configuration change needs to invalidate a user's image cache + /// If the image processor is hosted within a plugin then this should be the plugin ConfigurationDateLastModified + /// + public abstract DateTime ProcessingConfigurationDateLastModifiedUtc { get; } } /// @@ -52,34 +57,44 @@ namespace MediaBrowser.Controller.Drawing //[Export(typeof(BaseImageProcessor))] public class MyRoundedCornerImageProcessor : BaseImageProcessor { - public override void ProcessImage(Image originalImage, Bitmap bitmap, Graphics g, BaseEntity entity) + public override void ProcessImage(Image originalImage, Bitmap bitmap, Graphics graphics, BaseEntity entity, ImageType imageType, int imageIndex) { var CornerRadius = 20; - g.Clear(Color.Transparent); - + graphics.Clear(Color.Transparent); + using (GraphicsPath gp = new GraphicsPath()) { gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90); gp.AddArc(0 + bitmap.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90); gp.AddArc(0 + bitmap.Width - CornerRadius, 0 + bitmap.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90); gp.AddArc(0, 0 + bitmap.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90); - - g.SetClip(gp); - g.DrawImage(originalImage, 0, 0, bitmap.Width, bitmap.Height); + + graphics.SetClip(gp); + graphics.DrawImage(originalImage, 0, 0, bitmap.Width, bitmap.Height); } } - public override void ProcessImage(Image originalImage, Bitmap bitmap, Graphics graphics, BaseItem entity, ImageType imageType, int imageIndex) + public override bool RequiresTransparency { + get + { + return true; + } } - public override bool RequiresTransparency + public override DateTime ProcessingConfigurationDateLastModifiedUtc { get { - return true; + // This will result in a situation where images are never cached, but again, this is a prototype + return DateTime.UtcNow; } } + + public override bool IsConfiguredToProcess(BaseEntity entity, ImageType imageType, int imageIndex) + { + return true; + } } } diff --git a/MediaBrowser.Controller/Drawing/ImageProcessor.cs b/MediaBrowser.Controller/Drawing/ImageProcessor.cs index f9b6366c65..7ee4ef7348 100644 --- a/MediaBrowser.Controller/Drawing/ImageProcessor.cs +++ b/MediaBrowser.Controller/Drawing/ImageProcessor.cs @@ -14,19 +14,18 @@ namespace MediaBrowser.Controller.Drawing /// /// Processes an image by resizing to target dimensions /// - /// The stream containing the source image + /// The entity that owns the image + /// The image type + /// The image index (currently only used with backdrops) /// The stream to save the new image to /// Use if a fixed width is required. Aspect ratio will be preserved. /// Use if a fixed height is required. Aspect ratio will be preserved. /// Use if a max width is required. Aspect ratio will be preserved. /// Use if a max height is required. Aspect ratio will be preserved. /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. - /// The entity that owns the image - /// The image type - /// The image index (currently only used with backdrops) - public static void ProcessImage(Stream sourceImageStream, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality, BaseEntity entity, ImageType imageType, int imageIndex) + public static void ProcessImage(BaseEntity entity, ImageType imageType, int imageIndex, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality) { - Image originalImage = Image.FromStream(sourceImageStream); + Image originalImage = Image.FromFile(GetImagePath(entity, imageType, imageIndex)); // Determine the output size based on incoming parameters Size newSize = DrawingUtils.Resize(originalImage.Size, width, height, maxWidth, maxHeight); @@ -79,9 +78,42 @@ namespace MediaBrowser.Controller.Drawing originalImage.Dispose(); } + public static string GetImagePath(BaseEntity entity, ImageType imageType, int imageIndex) + { + var item = entity as BaseItem; + + if (item != null) + { + if (imageType == ImageType.Logo) + { + return item.LogoImagePath; + } + if (imageType == ImageType.Backdrop) + { + return item.BackdropImagePaths.ElementAt(imageIndex); + } + if (imageType == ImageType.Banner) + { + return item.BannerImagePath; + } + if (imageType == ImageType.Art) + { + return item.ArtImagePath; + } + if (imageType == ImageType.Thumbnail) + { + return item.ThumbnailImagePath; + } + } + + return entity.PrimaryImagePath; + } + + /// /// Executes additional image processors that are registered with the Kernel /// + /// The original Image, before re-sizing /// The bitmap holding the original image, after re-sizing /// The graphics surface on which the output is drawn /// The entity that owns the image @@ -89,20 +121,11 @@ namespace MediaBrowser.Controller.Drawing /// The image index (currently only used with backdrops) private static void ExecuteAdditionalImageProcessors(Image originalImage, Bitmap bitmap, Graphics graphics, BaseEntity entity, ImageType imageType, int imageIndex) { - var baseItem = entity as BaseItem; - - if (baseItem != null) - { - foreach (var processor in Kernel.Instance.ImageProcessors) - { - processor.ProcessImage(originalImage, bitmap, graphics, baseItem, imageType, imageIndex); - } - } - else + foreach (var processor in Kernel.Instance.ImageProcessors) { - foreach (var processor in Kernel.Instance.ImageProcessors) + if (processor.IsConfiguredToProcess(entity, imageType, imageIndex)) { - processor.ProcessImage(originalImage, bitmap, graphics, entity); + processor.ProcessImage(originalImage, bitmap, graphics, entity, imageType, imageIndex); } } } -- cgit v1.2.3