GDIImageEncoder.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Drawing;
  3. using MediaBrowser.Model.Drawing;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using ImageFormat = MediaBrowser.Model.Drawing.ImageFormat;
  11. namespace Emby.Drawing.GDI
  12. {
  13. public class GDIImageEncoder : IImageEncoder
  14. {
  15. private readonly IFileSystem _fileSystem;
  16. private readonly ILogger _logger;
  17. public GDIImageEncoder(IFileSystem fileSystem, ILogger logger)
  18. {
  19. _fileSystem = fileSystem;
  20. _logger = logger;
  21. }
  22. public string[] SupportedInputFormats
  23. {
  24. get
  25. {
  26. return new[]
  27. {
  28. "png",
  29. "jpeg",
  30. "jpg",
  31. "gif",
  32. "bmp"
  33. };
  34. }
  35. }
  36. public ImageFormat[] SupportedOutputFormats
  37. {
  38. get
  39. {
  40. return new[] { ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png };
  41. }
  42. }
  43. public ImageSize GetImageSize(string path)
  44. {
  45. using (var image = Image.FromFile(path))
  46. {
  47. return new ImageSize
  48. {
  49. Width = image.Width,
  50. Height = image.Height
  51. };
  52. }
  53. }
  54. public void CropWhiteSpace(string inputPath, string outputPath)
  55. {
  56. using (var image = (Bitmap)Image.FromFile(inputPath))
  57. {
  58. using (var croppedImage = image.CropWhitespace())
  59. {
  60. Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
  61. using (var outputStream = _fileSystem.GetFileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
  62. {
  63. croppedImage.Save(System.Drawing.Imaging.ImageFormat.Png, outputStream, 100);
  64. }
  65. }
  66. }
  67. }
  68. public void EncodeImage(string inputPath, string cacheFilePath, int width, int height, int quality, ImageProcessingOptions options)
  69. {
  70. var hasPostProcessing = !string.IsNullOrEmpty(options.BackgroundColor) || options.UnplayedCount.HasValue || options.AddPlayedIndicator || options.PercentPlayed > 0;
  71. using (var originalImage = Image.FromFile(inputPath))
  72. {
  73. var newWidth = Convert.ToInt32(width);
  74. var newHeight = Convert.ToInt32(height);
  75. var selectedOutputFormat = options.OutputFormat;
  76. // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
  77. // Also, Webp only supports Format32bppArgb and Format32bppRgb
  78. var pixelFormat = selectedOutputFormat == ImageFormat.Webp
  79. ? PixelFormat.Format32bppArgb
  80. : PixelFormat.Format32bppPArgb;
  81. using (var thumbnail = new Bitmap(newWidth, newHeight, pixelFormat))
  82. {
  83. // Mono throw an exeception if assign 0 to SetResolution
  84. if (originalImage.HorizontalResolution > 0 && originalImage.VerticalResolution > 0)
  85. {
  86. // Preserve the original resolution
  87. thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
  88. }
  89. using (var thumbnailGraph = Graphics.FromImage(thumbnail))
  90. {
  91. thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
  92. thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
  93. thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
  94. thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
  95. thumbnailGraph.CompositingMode = !hasPostProcessing ?
  96. CompositingMode.SourceCopy :
  97. CompositingMode.SourceOver;
  98. SetBackgroundColor(thumbnailGraph, options);
  99. thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);
  100. DrawIndicator(thumbnailGraph, newWidth, newHeight, options);
  101. var outputFormat = GetOutputFormat(originalImage, selectedOutputFormat);
  102. Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
  103. // Save to the cache location
  104. using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
  105. {
  106. // Save to the memory stream
  107. thumbnail.Save(outputFormat, cacheFileStream, quality);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// Sets the color of the background.
  115. /// </summary>
  116. /// <param name="graphics">The graphics.</param>
  117. /// <param name="options">The options.</param>
  118. private void SetBackgroundColor(Graphics graphics, ImageProcessingOptions options)
  119. {
  120. var color = options.BackgroundColor;
  121. if (!string.IsNullOrEmpty(color))
  122. {
  123. Color drawingColor;
  124. try
  125. {
  126. drawingColor = ColorTranslator.FromHtml(color);
  127. }
  128. catch
  129. {
  130. drawingColor = ColorTranslator.FromHtml("#" + color);
  131. }
  132. graphics.Clear(drawingColor);
  133. }
  134. }
  135. /// <summary>
  136. /// Draws the indicator.
  137. /// </summary>
  138. /// <param name="graphics">The graphics.</param>
  139. /// <param name="imageWidth">Width of the image.</param>
  140. /// <param name="imageHeight">Height of the image.</param>
  141. /// <param name="options">The options.</param>
  142. private void DrawIndicator(Graphics graphics, int imageWidth, int imageHeight, ImageProcessingOptions options)
  143. {
  144. if (!options.AddPlayedIndicator && !options.UnplayedCount.HasValue && options.PercentPlayed.Equals(0))
  145. {
  146. return;
  147. }
  148. try
  149. {
  150. if (options.AddPlayedIndicator)
  151. {
  152. var currentImageSize = new Size(imageWidth, imageHeight);
  153. new PlayedIndicatorDrawer().DrawPlayedIndicator(graphics, currentImageSize);
  154. }
  155. else if (options.UnplayedCount.HasValue)
  156. {
  157. var currentImageSize = new Size(imageWidth, imageHeight);
  158. new UnplayedCountIndicator().DrawUnplayedCountIndicator(graphics, currentImageSize, options.UnplayedCount.Value);
  159. }
  160. if (options.PercentPlayed > 0)
  161. {
  162. var currentImageSize = new Size(imageWidth, imageHeight);
  163. new PercentPlayedDrawer().Process(graphics, currentImageSize, options.PercentPlayed);
  164. }
  165. }
  166. catch (Exception ex)
  167. {
  168. _logger.ErrorException("Error drawing indicator overlay", ex);
  169. }
  170. }
  171. /// <summary>
  172. /// Gets the output format.
  173. /// </summary>
  174. /// <param name="image">The image.</param>
  175. /// <param name="outputFormat">The output format.</param>
  176. /// <returns>ImageFormat.</returns>
  177. private System.Drawing.Imaging.ImageFormat GetOutputFormat(Image image, ImageFormat outputFormat)
  178. {
  179. switch (outputFormat)
  180. {
  181. case ImageFormat.Bmp:
  182. return System.Drawing.Imaging.ImageFormat.Bmp;
  183. case ImageFormat.Gif:
  184. return System.Drawing.Imaging.ImageFormat.Gif;
  185. case ImageFormat.Jpg:
  186. return System.Drawing.Imaging.ImageFormat.Jpeg;
  187. case ImageFormat.Png:
  188. return System.Drawing.Imaging.ImageFormat.Png;
  189. default:
  190. return image.RawFormat;
  191. }
  192. }
  193. public void CreateImageCollage(ImageCollageOptions options)
  194. {
  195. }
  196. public void Dispose()
  197. {
  198. }
  199. }
  200. }