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

290 lines
9.3 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;
2015-06-19 16:54:39 +00:00
using System.Linq;
2015-10-04 04:23:11 +00:00
using CommonIO;
2015-11-12 19:26:02 +00:00
using MediaBrowser.Controller.Configuration;
2015-04-08 14:38:02 +00:00
namespace Emby.Drawing.ImageMagick
{
public class ImageMagickEncoder : IImageEncoder
{
private readonly ILogger _logger;
private readonly IApplicationPaths _appPaths;
2015-10-25 17:13:30 +00:00
private readonly IHttpClient _httpClient;
private readonly IFileSystem _fileSystem;
2015-11-12 19:26:02 +00:00
private readonly IServerConfigurationManager _config;
2015-04-08 14:38:02 +00:00
2015-11-12 19:26:02 +00:00
public ImageMagickEncoder(ILogger logger, IApplicationPaths appPaths, IHttpClient httpClient, IFileSystem fileSystem, IServerConfigurationManager config)
2015-04-08 14:38:02 +00:00
{
_logger = logger;
_appPaths = appPaths;
2015-06-19 16:54:39 +00:00
_httpClient = httpClient;
2015-10-25 17:13:30 +00:00
_fileSystem = fileSystem;
2015-11-12 19:26:02 +00:00
_config = config;
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[]
{
"tiff",
"jpeg",
"jpg",
"png",
"aiff",
"cr2",
"crw",
"dng",
"nef",
"orf",
"pef",
"arw",
"webp",
"gif",
"bmp"
};
}
}
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
{
_logger.Info("ImageMagick version: " + Wand.VersionString);
TestWebp();
2015-04-24 02:15:29 +00:00
Wand.SetMagickThreadCount(1);
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");
2015-10-25 17:13:30 +00:00
_fileSystem.CreateDirectory(Path.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:26:02 +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 void CropWhiteSpace(string inputPath, string outputPath)
{
CheckDisposed();
using (var wand = new MagickWand(inputPath))
{
wand.CurrentImage.TrimImage(10);
wand.SaveImage(outputPath);
}
2015-10-25 17:13:30 +00:00
SaveDelay();
2015-04-08 14:38:02 +00:00
}
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
}
2015-11-12 19:26:02 +00:00
public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
2015-04-08 14:38:02 +00:00
{
2015-11-12 19:26:02 +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))
{
2015-11-12 19:26:02 +00:00
ScaleImage(originalImage, width, height);
2015-04-08 14:38:02 +00:00
DrawIndicator(originalImage, width, height, options);
originalImage.CurrentImage.CompressionQuality = quality;
2015-11-12 19:26:02 +00:00
//originalImage.CurrentImage.StripImage();
2015-04-08 14:38:02 +00:00
originalImage.SaveImage(outputPath);
}
}
else
{
using (var wand = new MagickWand(width, height, options.BackgroundColor))
{
using (var originalImage = new MagickWand(inputPath))
{
2015-11-12 19:26:02 +00:00
ScaleImage(originalImage, width, height);
2015-04-08 14:38:02 +00:00
wand.CurrentImage.CompositeImage(originalImage, CompositeOperator.OverCompositeOp, 0, 0);
DrawIndicator(wand, width, height, options);
wand.CurrentImage.CompressionQuality = quality;
2015-11-12 19:26:02 +00:00
//wand.CurrentImage.StripImage();
2015-04-08 14:38:02 +00:00
wand.SaveImage(outputPath);
}
}
}
2015-10-25 17:13:30 +00:00
SaveDelay();
2015-04-08 14:38:02 +00:00
}
2015-11-12 19:26:02 +00:00
private void ScaleImage(MagickWand wand, int width, int height)
{
wand.CurrentImage.ResizeImage(width, height);
//if (_config.Configuration.EnableHighQualityImageScaling)
//{
// wand.CurrentImage.ResizeImage(width, height);
//}
//else
//{
// wand.CurrentImage.ScaleImage(width, height);
//}
}
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);
2015-10-25 17:13:30 +00:00
var task = new PlayedIndicatorDrawer(_appPaths, _httpClient, _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)
{
2015-09-13 23:07:54 +00:00
new StripCollageBuilder(_appPaths, _fileSystem).BuildThumbCollage(options.InputPaths.ToList(), options.OutputPath, options.Width, options.Height, options.Text);
2015-04-08 14:38:02 +00:00
}
else if (ratio >= .9)
{
2015-09-13 23:07:54 +00:00
new StripCollageBuilder(_appPaths, _fileSystem).BuildSquareCollage(options.InputPaths.ToList(), options.OutputPath, options.Width, options.Height, options.Text);
2015-04-08 14:38:02 +00:00
}
else
{
2015-09-13 23:07:54 +00:00
new StripCollageBuilder(_appPaths, _fileSystem).BuildPosterCollage(options.InputPaths.ToList(), options.OutputPath, options.Width, options.Height, options.Text);
2015-04-08 14:38:02 +00:00
}
2015-10-25 17:13:30 +00:00
SaveDelay();
}
private void SaveDelay()
{
// For some reason the images are not always getting released right away
2015-11-12 19:26:02 +00:00
//var task = Task.Delay(300);
//Task.WaitAll(task);
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();
}
private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
}
2015-10-26 05:29:32 +00:00
public bool SupportsImageCollageCreation
{
get { return true; }
}
public bool SupportsImageEncoding
{
get { return true; }
}
2015-04-08 14:38:02 +00:00
}
}