2012-09-18 21:33:57 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2012-09-17 01:12:39 +02:00
|
|
|
|
using System;
|
2012-07-13 05:50:50 +02:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.IO;
|
2012-09-18 21:33:57 +02:00
|
|
|
|
using System.Linq;
|
2012-07-13 05:50:50 +02:00
|
|
|
|
|
2012-09-18 21:33:57 +02:00
|
|
|
|
namespace MediaBrowser.Controller.Drawing
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
public static class ImageProcessor
|
|
|
|
|
{
|
2012-08-23 20:35:44 +02:00
|
|
|
|
/// <summary>
|
2012-09-18 21:33:57 +02:00
|
|
|
|
/// Processes an image by resizing to target dimensions
|
2012-08-23 20:35:44 +02:00
|
|
|
|
/// </summary>
|
2012-09-19 00:32:37 +02:00
|
|
|
|
/// <param name="entity">The entity that owns the image</param>
|
|
|
|
|
/// <param name="imageType">The image type</param>
|
|
|
|
|
/// <param name="imageIndex">The image index (currently only used with backdrops)</param>
|
2012-09-18 21:33:57 +02:00
|
|
|
|
/// <param name="toStream">The stream to save the new image to</param>
|
2012-08-23 20:35:44 +02:00
|
|
|
|
/// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
|
2012-09-19 00:32:37 +02:00
|
|
|
|
public static void ProcessImage(BaseEntity entity, ImageType imageType, int imageIndex, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-09-19 00:32:37 +02:00
|
|
|
|
Image originalImage = Image.FromFile(GetImagePath(entity, imageType, imageIndex));
|
2012-07-13 05:50:50 +02:00
|
|
|
|
|
2012-09-18 21:33:57 +02:00
|
|
|
|
// Determine the output size based on incoming parameters
|
2012-08-16 04:42:37 +02:00
|
|
|
|
Size newSize = DrawingUtils.Resize(originalImage.Size, width, height, maxWidth, maxHeight);
|
2012-07-13 05:50:50 +02:00
|
|
|
|
|
2012-09-06 18:00:36 +02:00
|
|
|
|
Bitmap thumbnail;
|
|
|
|
|
|
|
|
|
|
// Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
|
|
|
|
|
if (originalImage.PixelFormat.HasFlag(PixelFormat.Indexed))
|
|
|
|
|
{
|
|
|
|
|
thumbnail = new Bitmap(originalImage, newSize.Width, newSize.Height);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
thumbnail = new Bitmap(newSize.Width, newSize.Height, originalImage.PixelFormat);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 23:39:42 +02:00
|
|
|
|
thumbnail.MakeTransparent();
|
|
|
|
|
|
2012-09-18 21:33:57 +02:00
|
|
|
|
// Preserve the original resolution
|
2012-07-13 05:50:50 +02:00
|
|
|
|
thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
|
|
|
|
|
|
|
|
|
|
Graphics thumbnailGraph = Graphics.FromImage(thumbnail);
|
2012-09-06 18:00:36 +02:00
|
|
|
|
|
2012-07-13 05:50:50 +02:00
|
|
|
|
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
|
|
|
|
|
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
|
|
|
|
|
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
|
|
|
thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
|
|
|
|
thumbnailGraph.CompositingMode = CompositingMode.SourceOver;
|
|
|
|
|
|
2012-08-16 04:42:37 +02:00
|
|
|
|
thumbnailGraph.DrawImage(originalImage, 0, 0, newSize.Width, newSize.Height);
|
2012-07-13 05:50:50 +02:00
|
|
|
|
|
2012-09-18 23:39:42 +02:00
|
|
|
|
ImageFormat outputFormat = originalImage.RawFormat;
|
|
|
|
|
|
2012-09-18 21:33:57 +02:00
|
|
|
|
// Run Kernel image processors
|
|
|
|
|
if (Kernel.Instance.ImageProcessors.Any())
|
|
|
|
|
{
|
2012-09-18 23:39:42 +02:00
|
|
|
|
ExecuteAdditionalImageProcessors(originalImage, thumbnail, thumbnailGraph, entity, imageType, imageIndex);
|
|
|
|
|
|
|
|
|
|
if (Kernel.Instance.ImageProcessors.Any(i => i.RequiresTransparency))
|
|
|
|
|
{
|
|
|
|
|
outputFormat = ImageFormat.Png;
|
|
|
|
|
}
|
2012-09-18 21:33:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write to the output stream
|
2012-09-18 23:39:42 +02:00
|
|
|
|
SaveImage(outputFormat, thumbnail, toStream, quality);
|
2012-07-13 05:50:50 +02:00
|
|
|
|
|
|
|
|
|
thumbnailGraph.Dispose();
|
|
|
|
|
thumbnail.Dispose();
|
|
|
|
|
originalImage.Dispose();
|
|
|
|
|
}
|
2012-09-06 18:00:36 +02:00
|
|
|
|
|
2012-09-19 00:32:37 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-09-18 21:33:57 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes additional image processors that are registered with the Kernel
|
|
|
|
|
/// </summary>
|
2012-09-19 00:32:37 +02:00
|
|
|
|
/// <param name="originalImage">The original Image, before re-sizing</param>
|
2012-09-18 21:33:57 +02:00
|
|
|
|
/// <param name="bitmap">The bitmap holding the original image, after re-sizing</param>
|
|
|
|
|
/// <param name="graphics">The graphics surface on which the output is drawn</param>
|
|
|
|
|
/// <param name="entity">The entity that owns the image</param>
|
|
|
|
|
/// <param name="imageType">The image type</param>
|
|
|
|
|
/// <param name="imageIndex">The image index (currently only used with backdrops)</param>
|
2012-09-18 23:39:42 +02:00
|
|
|
|
private static void ExecuteAdditionalImageProcessors(Image originalImage, Bitmap bitmap, Graphics graphics, BaseEntity entity, ImageType imageType, int imageIndex)
|
2012-09-18 21:33:57 +02:00
|
|
|
|
{
|
2012-09-19 00:32:37 +02:00
|
|
|
|
foreach (var processor in Kernel.Instance.ImageProcessors)
|
2012-09-18 21:33:57 +02:00
|
|
|
|
{
|
2012-09-19 00:32:37 +02:00
|
|
|
|
if (processor.IsConfiguredToProcess(entity, imageType, imageIndex))
|
2012-09-18 21:33:57 +02:00
|
|
|
|
{
|
2012-09-19 00:32:37 +02:00
|
|
|
|
processor.ProcessImage(originalImage, bitmap, graphics, entity, imageType, imageIndex);
|
2012-09-18 21:33:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 23:39:42 +02:00
|
|
|
|
public static void SaveImage(ImageFormat outputFormat, Image newImage, Stream toStream, int? quality)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
// Use special save methods for jpeg and png that will result in a much higher quality image
|
|
|
|
|
// All other formats use the generic Image.Save
|
2012-09-18 23:39:42 +02:00
|
|
|
|
if (ImageFormat.Jpeg.Equals(outputFormat))
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
SaveJpeg(newImage, toStream, quality);
|
|
|
|
|
}
|
2012-09-18 23:39:42 +02:00
|
|
|
|
else if (ImageFormat.Png.Equals(outputFormat))
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-08-16 04:42:37 +02:00
|
|
|
|
newImage.Save(toStream, ImageFormat.Png);
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-09-18 23:39:42 +02:00
|
|
|
|
newImage.Save(toStream, outputFormat);
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 21:33:57 +02:00
|
|
|
|
public static void SaveJpeg(Image image, Stream target, int? quality)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
if (!quality.HasValue)
|
|
|
|
|
{
|
|
|
|
|
quality = 90;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 21:37:14 +02:00
|
|
|
|
using (var encoderParameters = new EncoderParameters(1))
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality.Value);
|
2012-09-18 21:33:57 +02:00
|
|
|
|
image.Save(target, GetImageCodecInfo("image/jpeg"), encoderParameters);
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 21:33:57 +02:00
|
|
|
|
public static ImageCodecInfo GetImageCodecInfo(string mimeType)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < info.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
ImageCodecInfo ici = info[i];
|
|
|
|
|
if (ici.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return ici;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return info[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|