ImageMagickEncoder.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. Wand.SetMagickThreadCount(1);
  61. }
  62. private bool _webpAvailable = true;
  63. private void TestWebp()
  64. {
  65. try
  66. {
  67. var tmpPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".webp");
  68. Directory.CreateDirectory(Path.GetDirectoryName(tmpPath));
  69. using (var wand = new MagickWand(1, 1, new PixelWand("none", 1)))
  70. {
  71. wand.SaveImage(tmpPath);
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. _logger.ErrorException("Error loading webp: ", ex);
  77. _webpAvailable = false;
  78. }
  79. }
  80. public void CropWhiteSpace(string inputPath, string outputPath)
  81. {
  82. CheckDisposed();
  83. using (var wand = new MagickWand(inputPath))
  84. {
  85. wand.CurrentImage.TrimImage(10);
  86. wand.SaveImage(outputPath);
  87. }
  88. }
  89. public ImageSize GetImageSize(string path)
  90. {
  91. CheckDisposed();
  92. using (var wand = new MagickWand())
  93. {
  94. wand.PingImage(path);
  95. var img = wand.CurrentImage;
  96. return new ImageSize
  97. {
  98. Width = img.Width,
  99. Height = img.Height
  100. };
  101. }
  102. }
  103. public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options)
  104. {
  105. if (string.IsNullOrWhiteSpace(options.BackgroundColor))
  106. {
  107. using (var originalImage = new MagickWand(inputPath))
  108. {
  109. originalImage.CurrentImage.ResizeImage(width, height);
  110. DrawIndicator(originalImage, width, height, options);
  111. originalImage.CurrentImage.CompressionQuality = quality;
  112. originalImage.SaveImage(outputPath);
  113. }
  114. }
  115. else
  116. {
  117. using (var wand = new MagickWand(width, height, options.BackgroundColor))
  118. {
  119. using (var originalImage = new MagickWand(inputPath))
  120. {
  121. originalImage.CurrentImage.ResizeImage(width, height);
  122. wand.CurrentImage.CompositeImage(originalImage, CompositeOperator.OverCompositeOp, 0, 0);
  123. DrawIndicator(wand, width, height, options);
  124. wand.CurrentImage.CompressionQuality = quality;
  125. wand.SaveImage(outputPath);
  126. }
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Draws the indicator.
  132. /// </summary>
  133. /// <param name="wand">The wand.</param>
  134. /// <param name="imageWidth">Width of the image.</param>
  135. /// <param name="imageHeight">Height of the image.</param>
  136. /// <param name="options">The options.</param>
  137. private void DrawIndicator(MagickWand wand, int imageWidth, int imageHeight, ImageProcessingOptions options)
  138. {
  139. if (!options.AddPlayedIndicator && !options.UnplayedCount.HasValue && options.PercentPlayed.Equals(0))
  140. {
  141. return;
  142. }
  143. try
  144. {
  145. if (options.AddPlayedIndicator)
  146. {
  147. var currentImageSize = new ImageSize(imageWidth, imageHeight);
  148. new PlayedIndicatorDrawer(_appPaths).DrawPlayedIndicator(wand, currentImageSize);
  149. }
  150. else if (options.UnplayedCount.HasValue)
  151. {
  152. var currentImageSize = new ImageSize(imageWidth, imageHeight);
  153. new UnplayedCountIndicator(_appPaths).DrawUnplayedCountIndicator(wand, currentImageSize, options.UnplayedCount.Value);
  154. }
  155. if (options.PercentPlayed > 0)
  156. {
  157. new PercentPlayedDrawer().Process(wand, options.PercentPlayed);
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. _logger.ErrorException("Error drawing indicator overlay", ex);
  163. }
  164. }
  165. public void CreateImageCollage(ImageCollageOptions options)
  166. {
  167. double ratio = options.Width;
  168. ratio /= options.Height;
  169. if (ratio >= 1.4)
  170. {
  171. new StripCollageBuilder(_appPaths).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, options.Height, options.Text);
  172. }
  173. else if (ratio >= .9)
  174. {
  175. new StripCollageBuilder(_appPaths).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height, options.Text);
  176. }
  177. else
  178. {
  179. new StripCollageBuilder(_appPaths).BuildPosterCollage(options.InputPaths, options.OutputPath, options.Width, options.Height, options.Text);
  180. }
  181. }
  182. public string Name
  183. {
  184. get { return "ImageMagick"; }
  185. }
  186. private bool _disposed;
  187. public void Dispose()
  188. {
  189. _disposed = true;
  190. Wand.CloseEnvironment();
  191. }
  192. private void CheckDisposed()
  193. {
  194. if (_disposed)
  195. {
  196. throw new ObjectDisposedException(GetType().Name);
  197. }
  198. }
  199. }
  200. }