ImageMagickEncoder.cs 7.1 KB

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