jellyfin-server/Emby.Drawing.ImageMagick/ImageMagickEncoder.cs

354 lines
12 KiB
C#
Raw Normal View History

2015-06-19 16:54:39 +00:00
using System.Threading.Tasks;
2015-05-11 16:32:15 +00:00
using ImageMagickSharp;
2015-04-08 14:38:02 +00:00
using MediaBrowser.Common.Configuration;
2015-06-19 16:54:39 +00:00
using MediaBrowser.Common.Net;
2015-04-08 14:38:02 +00:00
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Logging;
using System;
using System.IO;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
2017-06-21 14:51:11 +00:00
using MediaBrowser.Model.System;
2015-04-08 14:38:02 +00:00
namespace Emby.Drawing.ImageMagick
{
2017-09-05 19:49:02 +00:00
public class ImageMagickEncoder : IImageEncoder, IDisposable
2015-04-08 14:38:02 +00:00
{
private readonly ILogger _logger;
private readonly IApplicationPaths _appPaths;
2016-11-11 19:55:12 +00:00
private readonly Func<IHttpClient> _httpClientFactory;
2015-10-25 17:13:30 +00:00
private readonly IFileSystem _fileSystem;
2017-06-21 14:51:11 +00:00
private readonly IEnvironmentInfo _environment;
2015-04-08 14:38:02 +00:00
2017-06-21 14:51:11 +00:00
public ImageMagickEncoder(ILogger logger, IApplicationPaths appPaths, Func<IHttpClient> httpClientFactory, IFileSystem fileSystem, IEnvironmentInfo environment)
2015-04-08 14:38:02 +00:00
{
_logger = logger;
_appPaths = appPaths;
2016-11-11 19:55:12 +00:00
_httpClientFactory = httpClientFactory;
2015-10-25 17:13:30 +00:00
_fileSystem = fileSystem;
2017-06-21 14:51:11 +00:00
_environment = environment;
2015-04-08 14:38:02 +00:00
2015-10-26 05:29:32 +00:00
LogVersion();
2015-04-08 14:38:02 +00:00
}
public string[] SupportedInputFormats
{
get
{
// Some common file name extensions for RAW picture files include: .cr2, .crw, .dng, .nef, .orf, .rw2, .pef, .arw, .sr2, .srf, and .tif.
return new[]
{
2017-09-05 19:49:02 +00:00
"tiff",
"tif",
2015-04-08 14:38:02 +00:00
"jpeg",
"jpg",
"png",
"aiff",
"cr2",
"crw",
"dng",
2017-03-05 15:38:36 +00:00
// Remove until supported
//"nef",
2015-04-08 14:38:02 +00:00
"orf",
"pef",
"arw",
"webp",
"gif",
"bmp",
"erf",
"raf",
2016-10-05 15:20:11 +00:00
"rw2",
"nrw"
2015-04-08 14:38:02 +00:00
};
}
}
public ImageFormat[] SupportedOutputFormats
{
get
{
if (_webpAvailable)
{
return new[] { ImageFormat.Webp, ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png };
}
return new[] { ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png };
}
}
2015-10-26 05:29:32 +00:00
private void LogVersion()
2015-04-08 14:38:02 +00:00
{
2016-01-04 19:40:59 +00:00
_logger.Info("ImageMagick version: " + GetVersion());
2015-04-08 14:38:02 +00:00
TestWebp();
2015-04-24 02:15:29 +00:00
Wand.SetMagickThreadCount(1);
2015-04-08 14:38:02 +00:00
}
2016-01-04 19:40:59 +00:00
public static string GetVersion()
{
return Wand.VersionString;
}
2015-04-08 14:38:02 +00:00
private bool _webpAvailable = true;
private void TestWebp()
{
try
{
var tmpPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".webp");
2017-05-04 18:14:45 +00:00
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(tmpPath));
2015-04-08 14:38:02 +00:00
using (var wand = new MagickWand(1, 1, new PixelWand("none", 1)))
{
wand.SaveImage(tmpPath);
}
}
2015-11-12 19:27:14 +00:00
catch
2015-04-08 14:38:02 +00:00
{
2015-10-26 05:29:32 +00:00
//_logger.ErrorException("Error loading webp: ", ex);
2015-04-08 14:38:02 +00:00
_webpAvailable = false;
}
}
public ImageSize GetImageSize(string path)
{
CheckDisposed();
using (var wand = new MagickWand())
{
wand.PingImage(path);
var img = wand.CurrentImage;
return new ImageSize
{
Width = img.Width,
Height = img.Height
};
}
}
2015-05-15 15:46:20 +00:00
private bool HasTransparency(string path)
{
var ext = Path.GetExtension(path);
2015-05-15 16:32:50 +00:00
return string.Equals(ext, ".png", StringComparison.OrdinalIgnoreCase) ||
string.Equals(ext, ".webp", StringComparison.OrdinalIgnoreCase);
2015-05-15 15:46:20 +00:00
}
2017-06-09 19:24:31 +00:00
public string EncodeImage(string inputPath, DateTime dateModified, string outputPath, bool autoOrient, ImageOrientation? orientation, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
2015-04-08 14:38:02 +00:00
{
2015-10-28 19:40:38 +00:00
// Even if the caller specified 100, don't use it because it takes forever
quality = Math.Min(quality, 99);
2015-05-15 16:32:50 +00:00
if (string.IsNullOrWhiteSpace(options.BackgroundColor) || !HasTransparency(inputPath))
2015-04-08 14:38:02 +00:00
{
using (var originalImage = new MagickWand(inputPath))
{
if (options.CropWhiteSpace)
{
originalImage.CurrentImage.TrimImage(10);
}
2017-05-17 18:18:18 +00:00
var originalImageSize = new ImageSize(originalImage.CurrentImage.Width, originalImage.CurrentImage.Height);
if (!options.CropWhiteSpace && options.HasDefaultOptions(inputPath, originalImageSize))
2017-05-15 02:27:58 +00:00
{
2017-05-17 18:18:18 +00:00
// Just spit out the original file if all the options are default
return inputPath;
2017-05-15 02:27:58 +00:00
}
var newImageSize = ImageHelper.GetNewImageSize(options, originalImageSize);
var width = Convert.ToInt32(Math.Round(newImageSize.Width));
var height = Convert.ToInt32(Math.Round(newImageSize.Height));
2016-12-03 07:58:48 +00:00
ScaleImage(originalImage, width, height, options.Blur ?? 0);
2015-04-08 14:38:02 +00:00
2016-01-21 16:53:03 +00:00
if (autoOrient)
2016-01-21 08:23:02 +00:00
{
2016-01-21 16:53:03 +00:00
AutoOrientImage(originalImage);
2016-01-21 08:23:02 +00:00
}
2016-02-23 19:48:58 +00:00
AddForegroundLayer(originalImage, options);
2015-04-08 14:38:02 +00:00
DrawIndicator(originalImage, width, height, options);
originalImage.CurrentImage.CompressionQuality = quality;
2015-11-12 19:31:25 +00:00
originalImage.CurrentImage.StripImage();
2015-04-08 14:38:02 +00:00
originalImage.SaveImage(outputPath);
}
}
else
{
2017-05-15 02:27:58 +00:00
using (var originalImage = new MagickWand(inputPath))
2015-04-08 14:38:02 +00:00
{
2017-05-17 18:18:18 +00:00
var originalImageSize = new ImageSize(originalImage.CurrentImage.Width, originalImage.CurrentImage.Height);
2017-05-15 02:27:58 +00:00
var newImageSize = ImageHelper.GetNewImageSize(options, originalImageSize);
var width = Convert.ToInt32(Math.Round(newImageSize.Width));
var height = Convert.ToInt32(Math.Round(newImageSize.Height));
using (var wand = new MagickWand(width, height, options.BackgroundColor))
2015-04-08 14:38:02 +00:00
{
2016-12-03 07:58:48 +00:00
ScaleImage(originalImage, width, height, options.Blur ?? 0);
2015-04-08 14:38:02 +00:00
2016-01-21 16:53:03 +00:00
if (autoOrient)
2016-01-21 08:23:02 +00:00
{
2016-01-21 16:53:03 +00:00
AutoOrientImage(originalImage);
2016-01-21 08:23:02 +00:00
}
2015-04-08 14:38:02 +00:00
wand.CurrentImage.CompositeImage(originalImage, CompositeOperator.OverCompositeOp, 0, 0);
2016-02-23 19:48:58 +00:00
AddForegroundLayer(wand, options);
2015-04-08 14:38:02 +00:00
DrawIndicator(wand, width, height, options);
wand.CurrentImage.CompressionQuality = quality;
2015-11-12 19:31:25 +00:00
wand.CurrentImage.StripImage();
2015-04-08 14:38:02 +00:00
wand.SaveImage(outputPath);
}
}
}
2017-05-17 18:18:18 +00:00
return outputPath;
2015-04-08 14:38:02 +00:00
}
2016-02-23 19:48:58 +00:00
private void AddForegroundLayer(MagickWand wand, ImageProcessingOptions options)
{
if (string.IsNullOrWhiteSpace(options.ForegroundLayer))
{
return;
}
2016-02-26 15:10:43 +00:00
Double opacity;
if (!Double.TryParse(options.ForegroundLayer, out opacity)) opacity = .4;
using (var pixel = new PixelWand("#000", opacity))
using (var overlay = new MagickWand(wand.CurrentImage.Width, wand.CurrentImage.Height, pixel))
{
wand.CurrentImage.CompositeImage(overlay, CompositeOperator.OverCompositeOp, 0, 0);
}
2016-02-23 19:48:58 +00:00
}
2016-01-21 16:53:03 +00:00
private void AutoOrientImage(MagickWand wand)
{
wand.CurrentImage.AutoOrientImage();
}
2016-01-21 08:23:02 +00:00
public static void RotateImage(MagickWand wand, float angle)
{
using (var pixelWand = new PixelWand("none", 1))
{
wand.CurrentImage.RotateImage(pixelWand, angle);
}
}
2016-12-03 07:58:48 +00:00
private void ScaleImage(MagickWand wand, int width, int height, int blur)
2015-10-28 04:06:13 +00:00
{
2016-12-03 07:58:48 +00:00
var useResize = blur > 1;
2016-01-12 19:30:14 +00:00
2016-12-03 07:58:48 +00:00
if (useResize)
2015-11-12 19:31:25 +00:00
{
2016-12-03 07:58:48 +00:00
wand.CurrentImage.ResizeImage(width, height, FilterTypes.GaussianFilter, blur);
2015-11-12 19:31:25 +00:00
}
else
{
wand.CurrentImage.ScaleImage(width, height);
}
2015-10-28 04:06:13 +00:00
}
2015-04-08 14:38:02 +00:00
/// <summary>
/// Draws the indicator.
/// </summary>
/// <param name="wand">The wand.</param>
/// <param name="imageWidth">Width of the image.</param>
/// <param name="imageHeight">Height of the image.</param>
/// <param name="options">The options.</param>
private void DrawIndicator(MagickWand wand, int imageWidth, int imageHeight, ImageProcessingOptions options)
{
if (!options.AddPlayedIndicator && !options.UnplayedCount.HasValue && options.PercentPlayed.Equals(0))
{
return;
}
try
{
if (options.AddPlayedIndicator)
{
var currentImageSize = new ImageSize(imageWidth, imageHeight);
2016-11-11 19:55:12 +00:00
var task = new PlayedIndicatorDrawer(_appPaths, _httpClientFactory(), _fileSystem).DrawPlayedIndicator(wand, currentImageSize);
2015-06-19 16:54:39 +00:00
Task.WaitAll(task);
2015-04-08 14:38:02 +00:00
}
else if (options.UnplayedCount.HasValue)
{
var currentImageSize = new ImageSize(imageWidth, imageHeight);
2015-09-13 23:07:54 +00:00
new UnplayedCountIndicator(_appPaths, _fileSystem).DrawUnplayedCountIndicator(wand, currentImageSize, options.UnplayedCount.Value);
2015-04-08 14:38:02 +00:00
}
if (options.PercentPlayed > 0)
{
new PercentPlayedDrawer().Process(wand, options.PercentPlayed);
}
}
catch (Exception ex)
{
_logger.ErrorException("Error drawing indicator overlay", ex);
}
}
public void CreateImageCollage(ImageCollageOptions options)
{
double ratio = options.Width;
ratio /= options.Height;
if (ratio >= 1.4)
{
2017-08-24 19:52:19 +00:00
new StripCollageBuilder(_appPaths, _fileSystem).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
2015-04-08 14:38:02 +00:00
}
else if (ratio >= .9)
{
2017-08-24 19:52:19 +00:00
new StripCollageBuilder(_appPaths, _fileSystem).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
2015-04-08 14:38:02 +00:00
}
else
{
2017-08-24 19:52:19 +00:00
new StripCollageBuilder(_appPaths, _fileSystem).BuildPosterCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
2015-04-08 14:38:02 +00:00
}
}
2015-04-09 05:20:23 +00:00
public string Name
{
get { return "ImageMagick"; }
}
2015-04-08 14:38:02 +00:00
private bool _disposed;
public void Dispose()
{
_disposed = true;
Wand.CloseEnvironment();
2017-09-05 19:49:02 +00:00
GC.SuppressFinalize(this);
2015-04-08 14:38:02 +00:00
}
private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
}
2015-10-26 05:29:32 +00:00
public bool SupportsImageCollageCreation
{
2017-06-21 14:51:11 +00:00
get
{
return true;
}
2015-10-26 05:29:32 +00:00
}
public bool SupportsImageEncoding
{
get { return true; }
}
2015-04-08 14:38:02 +00:00
}
}