ImageMagickEncoder.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. public void EncodeImage(string inputPath, string outputPath, int width, int height, int quality, ImageProcessingOptions options)
  105. {
  106. if (string.IsNullOrWhiteSpace(options.BackgroundColor))
  107. {
  108. using (var originalImage = new MagickWand(inputPath))
  109. {
  110. originalImage.CurrentImage.ResizeImage(width, height);
  111. DrawIndicator(originalImage, width, height, options);
  112. originalImage.CurrentImage.CompressionQuality = quality;
  113. originalImage.SaveImage(outputPath);
  114. }
  115. }
  116. else
  117. {
  118. using (var wand = new MagickWand(width, height, options.BackgroundColor))
  119. {
  120. using (var originalImage = new MagickWand(inputPath))
  121. {
  122. originalImage.CurrentImage.ResizeImage(width, height);
  123. wand.CurrentImage.CompositeImage(originalImage, CompositeOperator.OverCompositeOp, 0, 0);
  124. DrawIndicator(wand, width, height, options);
  125. wand.CurrentImage.CompressionQuality = quality;
  126. wand.SaveImage(outputPath);
  127. }
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// Draws the indicator.
  133. /// </summary>
  134. /// <param name="wand">The wand.</param>
  135. /// <param name="imageWidth">Width of the image.</param>
  136. /// <param name="imageHeight">Height of the image.</param>
  137. /// <param name="options">The options.</param>
  138. private void DrawIndicator(MagickWand wand, int imageWidth, int imageHeight, ImageProcessingOptions options)
  139. {
  140. if (!options.AddPlayedIndicator && !options.UnplayedCount.HasValue && options.PercentPlayed.Equals(0))
  141. {
  142. return;
  143. }
  144. try
  145. {
  146. if (options.AddPlayedIndicator)
  147. {
  148. var currentImageSize = new ImageSize(imageWidth, imageHeight);
  149. new PlayedIndicatorDrawer(_appPaths).DrawPlayedIndicator(wand, currentImageSize);
  150. }
  151. else if (options.UnplayedCount.HasValue)
  152. {
  153. var currentImageSize = new ImageSize(imageWidth, imageHeight);
  154. new UnplayedCountIndicator(_appPaths).DrawUnplayedCountIndicator(wand, currentImageSize, options.UnplayedCount.Value);
  155. }
  156. if (options.PercentPlayed > 0)
  157. {
  158. new PercentPlayedDrawer().Process(wand, options.PercentPlayed);
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. _logger.ErrorException("Error drawing indicator overlay", ex);
  164. }
  165. }
  166. public void CreateImageCollage(ImageCollageOptions options)
  167. {
  168. double ratio = options.Width;
  169. ratio /= options.Height;
  170. if (ratio >= 1.4)
  171. {
  172. new StripCollageBuilder(_appPaths).BuildThumbCollage(options.InputPaths.ToList(), options.OutputPath, options.Width, options.Height, options.Text);
  173. }
  174. else if (ratio >= .9)
  175. {
  176. new StripCollageBuilder(_appPaths).BuildSquareCollage(options.InputPaths.ToList(), options.OutputPath, options.Width, options.Height, options.Text);
  177. }
  178. else
  179. {
  180. new StripCollageBuilder(_appPaths).BuildPosterCollage(options.InputPaths.ToList(), options.OutputPath, options.Width, options.Height, options.Text);
  181. }
  182. }
  183. public string Name
  184. {
  185. get { return "ImageMagick"; }
  186. }
  187. private bool _disposed;
  188. public void Dispose()
  189. {
  190. _disposed = true;
  191. Wand.CloseEnvironment();
  192. }
  193. private void CheckDisposed()
  194. {
  195. if (_disposed)
  196. {
  197. throw new ObjectDisposedException(GetType().Name);
  198. }
  199. }
  200. }
  201. }