ImageMagickEncoder.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System.Linq;
  2. using ImageMagickSharp;
  3. using MediaBrowser.Common.Configuration;
  4. using MediaBrowser.Controller.Drawing;
  5. using MediaBrowser.Model.Drawing;
  6. using MediaBrowser.Model.Logging;
  7. using System;
  8. using System.IO;
  9. namespace Emby.Drawing.ImageMagick
  10. {
  11. public class ImageMagickEncoder : IImageEncoder
  12. {
  13. private readonly ILogger _logger;
  14. private readonly IApplicationPaths _appPaths;
  15. public ImageMagickEncoder(ILogger logger, IApplicationPaths appPaths)
  16. {
  17. _logger = logger;
  18. _appPaths = appPaths;
  19. LogImageMagickVersion();
  20. }
  21. public string[] SupportedInputFormats
  22. {
  23. get
  24. {
  25. // Some common file name extensions for RAW picture files include: .cr2, .crw, .dng, .nef, .orf, .rw2, .pef, .arw, .sr2, .srf, and .tif.
  26. return new[]
  27. {
  28. "tiff",
  29. "jpeg",
  30. "jpg",
  31. "png",
  32. "aiff",
  33. "cr2",
  34. "crw",
  35. "dng",
  36. "nef",
  37. "orf",
  38. "pef",
  39. "arw",
  40. "webp",
  41. "gif",
  42. "bmp"
  43. };
  44. }
  45. }
  46. public ImageFormat[] SupportedOutputFormats
  47. {
  48. get
  49. {
  50. if (_webpAvailable)
  51. {
  52. return new[] { ImageFormat.Webp, ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png };
  53. }
  54. return new[] { ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png };
  55. }
  56. }
  57. private void LogImageMagickVersion()
  58. {
  59. _logger.Info("ImageMagick version: " + Wand.VersionString);
  60. TestWebp();
  61. Wand.SetMagickThreadCount(1);
  62. }
  63. private bool _webpAvailable = true;
  64. private void TestWebp()
  65. {
  66. try
  67. {
  68. var tmpPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".webp");
  69. Directory.CreateDirectory(Path.GetDirectoryName(tmpPath));
  70. using (var wand = new MagickWand(1, 1, new PixelWand("none", 1)))
  71. {
  72. wand.SaveImage(tmpPath);
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. _logger.ErrorException("Error loading webp: ", ex);
  78. _webpAvailable = false;
  79. }
  80. }
  81. public void CropWhiteSpace(string inputPath, string outputPath)
  82. {
  83. CheckDisposed();
  84. using (var wand = new MagickWand(inputPath))
  85. {
  86. wand.CurrentImage.TrimImage(10);
  87. wand.SaveImage(outputPath);
  88. }
  89. }
  90. public ImageSize GetImageSize(string path)
  91. {
  92. CheckDisposed();
  93. using (var wand = new MagickWand())
  94. {
  95. wand.PingImage(path);
  96. var img = wand.CurrentImage;
  97. return new ImageSize
  98. {
  99. Width = img.Width,
  100. Height = img.Height
  101. };
  102. }
  103. }
  104. private bool HasTransparency(string path)
  105. {
  106. var ext = Path.GetExtension(path);
  107. return string.Equals(ext, ".png", StringComparison.OrdinalIgnoreCase) ||
  108. string.Equals(ext, ".webp", StringComparison.OrdinalIgnoreCase);
  109. }
  110. public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options)
  111. {
  112. if (string.IsNullOrWhiteSpace(options.BackgroundColor) || !HasTransparency(inputPath))
  113. {
  114. using (var originalImage = new MagickWand(inputPath))
  115. {
  116. originalImage.CurrentImage.ResizeImage(width, height);
  117. DrawIndicator(originalImage, width, height, options);
  118. originalImage.CurrentImage.CompressionQuality = quality;
  119. originalImage.SaveImage(outputPath);
  120. }
  121. }
  122. else
  123. {
  124. using (var wand = new MagickWand(width, height, options.BackgroundColor))
  125. {
  126. using (var originalImage = new MagickWand(inputPath))
  127. {
  128. originalImage.CurrentImage.ResizeImage(width, height);
  129. wand.CurrentImage.CompositeImage(originalImage, CompositeOperator.OverCompositeOp, 0, 0);
  130. DrawIndicator(wand, width, height, options);
  131. wand.CurrentImage.CompressionQuality = quality;
  132. wand.SaveImage(outputPath);
  133. }
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Draws the indicator.
  139. /// </summary>
  140. /// <param name="wand">The wand.</param>
  141. /// <param name="imageWidth">Width of the image.</param>
  142. /// <param name="imageHeight">Height of the image.</param>
  143. /// <param name="options">The options.</param>
  144. private void DrawIndicator(MagickWand wand, int imageWidth, int imageHeight, ImageProcessingOptions options)
  145. {
  146. if (!options.AddPlayedIndicator && !options.UnplayedCount.HasValue && options.PercentPlayed.Equals(0))
  147. {
  148. return;
  149. }
  150. try
  151. {
  152. if (options.AddPlayedIndicator)
  153. {
  154. var currentImageSize = new ImageSize(imageWidth, imageHeight);
  155. new PlayedIndicatorDrawer(_appPaths).DrawPlayedIndicator(wand, currentImageSize);
  156. }
  157. else if (options.UnplayedCount.HasValue)
  158. {
  159. var currentImageSize = new ImageSize(imageWidth, imageHeight);
  160. new UnplayedCountIndicator(_appPaths).DrawUnplayedCountIndicator(wand, currentImageSize, options.UnplayedCount.Value);
  161. }
  162. if (options.PercentPlayed > 0)
  163. {
  164. new PercentPlayedDrawer().Process(wand, options.PercentPlayed);
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. _logger.ErrorException("Error drawing indicator overlay", ex);
  170. }
  171. }
  172. public void CreateImageCollage(ImageCollageOptions options)
  173. {
  174. double ratio = options.Width;
  175. ratio /= options.Height;
  176. if (ratio >= 1.4)
  177. {
  178. new StripCollageBuilder(_appPaths).BuildThumbCollage(options.InputPaths.ToList(), options.OutputPath, options.Width, options.Height, options.Text);
  179. }
  180. else if (ratio >= .9)
  181. {
  182. new StripCollageBuilder(_appPaths).BuildSquareCollage(options.InputPaths.ToList(), options.OutputPath, options.Width, options.Height, options.Text);
  183. }
  184. else
  185. {
  186. new StripCollageBuilder(_appPaths).BuildPosterCollage(options.InputPaths.ToList(), options.OutputPath, options.Width, options.Height, options.Text);
  187. }
  188. }
  189. public string Name
  190. {
  191. get { return "ImageMagick"; }
  192. }
  193. private bool _disposed;
  194. public void Dispose()
  195. {
  196. _disposed = true;
  197. Wand.CloseEnvironment();
  198. }
  199. private void CheckDisposed()
  200. {
  201. if (_disposed)
  202. {
  203. throw new ObjectDisposedException(GetType().Name);
  204. }
  205. }
  206. }
  207. }