diff options
Diffstat (limited to 'Emby.Drawing')
| -rw-r--r-- | Emby.Drawing/Emby.Drawing.csproj | 2 | ||||
| -rw-r--r-- | Emby.Drawing/GDI/GDIImageEncoder.cs | 4 | ||||
| -rw-r--r-- | Emby.Drawing/IImageEncoder.cs | 3 | ||||
| -rw-r--r-- | Emby.Drawing/ImageMagick/ImageMagickEncoder.cs | 28 | ||||
| -rw-r--r-- | Emby.Drawing/ImageProcessor.cs | 96 | ||||
| -rw-r--r-- | Emby.Drawing/NullImageEncoder.cs | 2 | ||||
| -rw-r--r-- | Emby.Drawing/packages.config | 2 |
7 files changed, 96 insertions, 41 deletions
diff --git a/Emby.Drawing/Emby.Drawing.csproj b/Emby.Drawing/Emby.Drawing.csproj index e9911a12d9..6cda8b5d05 100644 --- a/Emby.Drawing/Emby.Drawing.csproj +++ b/Emby.Drawing/Emby.Drawing.csproj @@ -37,7 +37,7 @@ </Reference> <Reference Include="ImageMagickSharp, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\ImageMagickSharp.1.0.0.16\lib\net45\ImageMagickSharp.dll</HintPath> + <HintPath>..\packages\ImageMagickSharp.1.0.0.17\lib\net45\ImageMagickSharp.dll</HintPath> </Reference> <Reference Include="Patterns.Logging"> <HintPath>..\packages\Patterns.Logging.1.0.0.2\lib\portable-net45+sl4+wp71+win8+wpa81\Patterns.Logging.dll</HintPath> diff --git a/Emby.Drawing/GDI/GDIImageEncoder.cs b/Emby.Drawing/GDI/GDIImageEncoder.cs index 5306137411..11c9eb8e91 100644 --- a/Emby.Drawing/GDI/GDIImageEncoder.cs +++ b/Emby.Drawing/GDI/GDIImageEncoder.cs @@ -89,7 +89,7 @@ namespace Emby.Drawing.GDI } } - public void EncodeImage(string inputPath, string cacheFilePath, int width, int height, int quality, ImageProcessingOptions options) + public void EncodeImage(string inputPath, string cacheFilePath, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat) { var hasPostProcessing = !string.IsNullOrEmpty(options.BackgroundColor) || options.UnplayedCount.HasValue || options.AddPlayedIndicator || options.PercentPlayed > 0; @@ -98,8 +98,6 @@ namespace Emby.Drawing.GDI var newWidth = Convert.ToInt32(width); var newHeight = Convert.ToInt32(height); - var selectedOutputFormat = options.OutputFormat; - // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here // Also, Webp only supports Format32bppArgb and Format32bppRgb var pixelFormat = selectedOutputFormat == ImageFormat.Webp diff --git a/Emby.Drawing/IImageEncoder.cs b/Emby.Drawing/IImageEncoder.cs index 4469075a6d..bfd382bb7c 100644 --- a/Emby.Drawing/IImageEncoder.cs +++ b/Emby.Drawing/IImageEncoder.cs @@ -31,7 +31,8 @@ namespace Emby.Drawing /// <param name="height">The height.</param> /// <param name="quality">The quality.</param> /// <param name="options">The options.</param> - void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options); + /// <param name="outputFormat">The output format.</param> + void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options, ImageFormat outputFormat); /// <summary> /// Creates the image collage. diff --git a/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs b/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs index ed0760ee33..d18ee1caab 100644 --- a/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs +++ b/Emby.Drawing/ImageMagick/ImageMagickEncoder.cs @@ -9,6 +9,7 @@ using System; using System.IO; using System.Linq; using CommonIO; +using MediaBrowser.Controller.Configuration; namespace Emby.Drawing.ImageMagick { @@ -18,13 +19,15 @@ namespace Emby.Drawing.ImageMagick private readonly IApplicationPaths _appPaths; private readonly IHttpClient _httpClient; private readonly IFileSystem _fileSystem; + private readonly IServerConfigurationManager _config; - public ImageMagickEncoder(ILogger logger, IApplicationPaths appPaths, IHttpClient httpClient, IFileSystem fileSystem) + public ImageMagickEncoder(ILogger logger, IApplicationPaths appPaths, IHttpClient httpClient, IFileSystem fileSystem, IServerConfigurationManager config) { _logger = logger; _appPaths = appPaths; _httpClient = httpClient; _fileSystem = fileSystem; + _config = config; LogVersion(); } @@ -131,17 +134,21 @@ namespace Emby.Drawing.ImageMagick string.Equals(ext, ".webp", StringComparison.OrdinalIgnoreCase); } - public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options) + public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat) { + // Even if the caller specified 100, don't use it because it takes forever + quality = Math.Min(quality, 99); + if (string.IsNullOrWhiteSpace(options.BackgroundColor) || !HasTransparency(inputPath)) { using (var originalImage = new MagickWand(inputPath)) { - originalImage.CurrentImage.ResizeImage(width, height); + ScaleImage(originalImage, width, height); DrawIndicator(originalImage, width, height, options); originalImage.CurrentImage.CompressionQuality = quality; + originalImage.CurrentImage.StripImage(); originalImage.SaveImage(outputPath); } @@ -152,12 +159,13 @@ namespace Emby.Drawing.ImageMagick { using (var originalImage = new MagickWand(inputPath)) { - originalImage.CurrentImage.ResizeImage(width, height); + ScaleImage(originalImage, width, height); wand.CurrentImage.CompositeImage(originalImage, CompositeOperator.OverCompositeOp, 0, 0); DrawIndicator(wand, width, height, options); wand.CurrentImage.CompressionQuality = quality; + wand.CurrentImage.StripImage(); wand.SaveImage(outputPath); } @@ -166,6 +174,18 @@ namespace Emby.Drawing.ImageMagick SaveDelay(); } + private void ScaleImage(MagickWand wand, int width, int height) + { + if (_config.Configuration.EnableHighQualityImageScaling) + { + wand.CurrentImage.ResizeImage(width, height); + } + else + { + wand.CurrentImage.ScaleImage(width, height); + } + } + /// <summary> /// Draws the indicator. /// </summary> diff --git a/Emby.Drawing/ImageProcessor.cs b/Emby.Drawing/ImageProcessor.cs index ec5d66cba4..e1b92bbffc 100644 --- a/Emby.Drawing/ImageProcessor.cs +++ b/Emby.Drawing/ImageProcessor.cs @@ -19,6 +19,7 @@ using System.Threading.Tasks; using CommonIO; using Emby.Drawing.Common; using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Net; namespace Emby.Drawing { @@ -152,7 +153,7 @@ namespace Emby.Drawing { var file = await ProcessImage(options).ConfigureAwait(false); - using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, true)) + using (var fileStream = _fileSystem.GetFileStream(file.Item1, FileMode.Open, FileAccess.Read, FileShare.Read, true)) { await fileStream.CopyToAsync(toStream).ConfigureAwait(false); } @@ -163,7 +164,7 @@ namespace Emby.Drawing return _imageEncoder.SupportedOutputFormats; } - public async Task<string> ProcessImage(ImageProcessingOptions options) + public async Task<Tuple<string, string>> ProcessImage(ImageProcessingOptions options) { if (options == null) { @@ -181,13 +182,7 @@ namespace Emby.Drawing if (!_imageEncoder.SupportsImageEncoding) { - return originalImagePath; - } - - if (options.HasDefaultOptions(originalImagePath) && options.Enhancers.Count == 0 && !options.CropWhiteSpace) - { - // Just spit out the original file if all the options are default - return originalImagePath; + return new Tuple<string, string>(originalImagePath, MimeTypes.GetMimeType(originalImagePath)); } var dateModified = originalImage.DateModified; @@ -214,19 +209,31 @@ namespace Emby.Drawing dateModified = tuple.Item2; } - var newSizeInfo = GetNewImageSize(originalImagePath, dateModified, options); - var newSize = newSizeInfo.Item1; - var isSizeChanged = newSizeInfo.Item2; + if (options.HasDefaultOptions(originalImagePath)) + { + // Just spit out the original file if all the options are default + return new Tuple<string, string>(originalImagePath, MimeTypes.GetMimeType(originalImagePath)); + } - if (options.HasDefaultOptionsWithoutSize(originalImagePath) && !isSizeChanged && options.Enhancers.Count == 0) + ImageSize? originalImageSize; + try { - // Just spit out the original file if the new size equals the old - return originalImagePath; + originalImageSize = GetImageSize(originalImagePath, dateModified, true); + if (options.HasDefaultOptions(originalImagePath, originalImageSize.Value)) + { + // Just spit out the original file if all the options are default + return new Tuple<string, string>(originalImagePath, MimeTypes.GetMimeType(originalImagePath)); + } + } + catch + { + originalImageSize = null; } - var quality = options.Quality ?? 90; + var newSize = GetNewImageSize(options, originalImageSize); + var quality = options.Quality; - var outputFormat = GetOutputFormat(options.OutputFormat); + var outputFormat = GetOutputFormat(options.SupportedOutputFormats[0]); var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, outputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.BackgroundColor); var semaphore = GetLock(cacheFilePath); @@ -250,8 +257,18 @@ namespace Emby.Drawing imageProcessingLockTaken = true; - _imageEncoder.EncodeImage(originalImagePath, cacheFilePath, newWidth, newHeight, quality, options); + _imageEncoder.EncodeImage(originalImagePath, cacheFilePath, newWidth, newHeight, quality, options, outputFormat); } + + return new Tuple<string, string>(cacheFilePath, GetMimeType(outputFormat, cacheFilePath)); + } + catch (Exception ex) + { + // If it fails for whatever reason, return the original image + _logger.ErrorException("Error encoding image", ex); + + // Just spit out the original file if all the options are default + return new Tuple<string, string>(originalImagePath, MimeTypes.GetMimeType(originalImagePath)); } finally { @@ -262,28 +279,47 @@ namespace Emby.Drawing semaphore.Release(); } - - return cacheFilePath; } - private Tuple<ImageSize, bool> GetNewImageSize(string originalImagePath, DateTime dateModified, ImageProcessingOptions options) + private string GetMimeType(ImageFormat format, string path) { - try + if (format == ImageFormat.Bmp) + { + return MimeTypes.GetMimeType("i.bmp"); + } + if (format == ImageFormat.Gif) + { + return MimeTypes.GetMimeType("i.gif"); + } + if (format == ImageFormat.Jpg) + { + return MimeTypes.GetMimeType("i.jpg"); + } + if (format == ImageFormat.Png) + { + return MimeTypes.GetMimeType("i.png"); + } + if (format == ImageFormat.Webp) { - var originalImageSize = GetImageSize(originalImagePath, dateModified, true); + return MimeTypes.GetMimeType("i.webp"); + } - // Determine the output size based on incoming parameters - var newSize = DrawingUtils.Resize(originalImageSize, options.Width, options.Height, options.MaxWidth, options.MaxHeight); + return MimeTypes.GetMimeType(path); + } - return new Tuple<ImageSize, bool>(newSize, !newSize.Equals(originalImageSize)); - } - catch + private ImageSize GetNewImageSize(ImageProcessingOptions options, ImageSize? originalImageSize) + { + if (originalImageSize.HasValue) { - return new Tuple<ImageSize, bool>(GetSizeEstimage(options), true); + // Determine the output size based on incoming parameters + var newSize = DrawingUtils.Resize(originalImageSize.Value, options.Width, options.Height, options.MaxWidth, options.MaxHeight); + + return newSize; } + return GetSizeEstimate(options); } - private ImageSize GetSizeEstimage(ImageProcessingOptions options) + private ImageSize GetSizeEstimate(ImageProcessingOptions options) { if (options.Width.HasValue && options.Height.HasValue) { diff --git a/Emby.Drawing/NullImageEncoder.cs b/Emby.Drawing/NullImageEncoder.cs index 30ea363290..1638a675ac 100644 --- a/Emby.Drawing/NullImageEncoder.cs +++ b/Emby.Drawing/NullImageEncoder.cs @@ -32,7 +32,7 @@ namespace Emby.Drawing throw new NotImplementedException(); } - public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options) + public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat) { throw new NotImplementedException(); } diff --git a/Emby.Drawing/packages.config b/Emby.Drawing/packages.config index 0fcdc278e3..3b8dbcab7c 100644 --- a/Emby.Drawing/packages.config +++ b/Emby.Drawing/packages.config @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <packages> <package id="CommonIO" version="1.0.0.5" targetFramework="net45" /> - <package id="ImageMagickSharp" version="1.0.0.16" targetFramework="net45" /> + <package id="ImageMagickSharp" version="1.0.0.17" targetFramework="net45" /> <package id="Patterns.Logging" version="1.0.0.2" targetFramework="net45" /> </packages>
\ No newline at end of file |
