reduce image processing

This commit is contained in:
Luke Pulverenti
2015-11-09 13:18:37 -05:00
parent 7d7f9d7654
commit 81bb469fe1
14 changed files with 139 additions and 443 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -134,7 +134,7 @@ 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);

View File

@@ -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.HasDefaultOptionsWithoutSize(originalImagePath) && !isSizeChanged && options.Enhancers.Count == 0)
if (options.HasDefaultOptions(originalImagePath))
{
// Just spit out the original file if the new size equals the old
return originalImagePath;
// Just spit out the original file if all the options are default
return new Tuple<string, string>(originalImagePath, MimeTypes.GetMimeType(originalImagePath));
}
ImageSize? originalImageSize;
try
{
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 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,7 +257,7 @@ namespace Emby.Drawing
imageProcessingLockTaken = true;
_imageEncoder.EncodeImage(originalImagePath, cacheFilePath, newWidth, newHeight, quality, options);
_imageEncoder.EncodeImage(originalImagePath, cacheFilePath, newWidth, newHeight, quality, options, outputFormat);
}
}
finally
@@ -263,27 +270,48 @@ namespace Emby.Drawing
semaphore.Release();
}
return cacheFilePath;
return new Tuple<string, string>(cacheFilePath, GetMimeType(outputFormat, cacheFilePath));
}
private Tuple<ImageSize, bool> GetNewImageSize(string originalImagePath, DateTime dateModified, ImageProcessingOptions options)
private string GetMimeType(ImageFormat format, string path)
{
try
if (format == ImageFormat.Bmp)
{
var originalImageSize = GetImageSize(originalImagePath, dateModified, true);
// Determine the output size based on incoming parameters
var newSize = DrawingUtils.Resize(originalImageSize, options.Width, options.Height, options.MaxWidth, options.MaxHeight);
return new Tuple<ImageSize, bool>(newSize, !newSize.Equals(originalImageSize));
return MimeTypes.GetMimeType("i.bmp");
}
catch
if (format == ImageFormat.Gif)
{
return new Tuple<ImageSize, bool>(GetSizeEstimage(options), true);
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)
{
return MimeTypes.GetMimeType("i.webp");
}
return MimeTypes.GetMimeType(path);
}
private ImageSize GetSizeEstimage(ImageProcessingOptions options)
private ImageSize GetNewImageSize(ImageProcessingOptions options, ImageSize? originalImageSize)
{
if (originalImageSize.HasValue)
{
// 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 GetSizeEstimate(ImageProcessingOptions options)
{
if (options.Width.HasValue && options.Height.HasValue)
{

View File

@@ -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();
}