SkiaEncoder.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Drawing;
  4. using MediaBrowser.Model.Drawing;
  5. using MediaBrowser.Model.IO;
  6. using MediaBrowser.Model.Logging;
  7. using SkiaSharp;
  8. using System;
  9. using System.Reflection;
  10. using System.Threading.Tasks;
  11. namespace Emby.Drawing.Skia
  12. {
  13. public class SkiaEncoder : IImageEncoder
  14. {
  15. private readonly ILogger _logger;
  16. private readonly IApplicationPaths _appPaths;
  17. private readonly Func<IHttpClient> _httpClientFactory;
  18. private readonly IFileSystem _fileSystem;
  19. public SkiaEncoder(ILogger logger, IApplicationPaths appPaths, Func<IHttpClient> httpClientFactory, IFileSystem fileSystem)
  20. {
  21. _logger = logger;
  22. _appPaths = appPaths;
  23. _httpClientFactory = httpClientFactory;
  24. _fileSystem = fileSystem;
  25. LogVersion();
  26. }
  27. public string[] SupportedInputFormats
  28. {
  29. get
  30. {
  31. // Some common file name extensions for RAW picture files include: .cr2, .crw, .dng, .nef, .orf, .rw2, .pef, .arw, .sr2, .srf, and .tif.
  32. return new[]
  33. {
  34. "jpeg",
  35. "jpg",
  36. "png",
  37. "dng",
  38. "webp",
  39. "gif",
  40. "bmp",
  41. "ico",
  42. "astc",
  43. "ktx",
  44. "pkm",
  45. "wbmp"
  46. };
  47. }
  48. }
  49. public ImageFormat[] SupportedOutputFormats
  50. {
  51. get
  52. {
  53. return new[] { ImageFormat.Webp, ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png, ImageFormat.Bmp };
  54. }
  55. }
  56. private void LogVersion()
  57. {
  58. _logger.Info("SkiaSharp version: " + GetVersion());
  59. }
  60. public static string GetVersion()
  61. {
  62. return typeof(SKCanvas).GetTypeInfo().Assembly.GetName().Version.ToString();
  63. }
  64. public static SKEncodedImageFormat GetImageFormat(ImageFormat selectedFormat)
  65. {
  66. switch (selectedFormat)
  67. {
  68. case ImageFormat.Bmp:
  69. return SKEncodedImageFormat.Bmp;
  70. case ImageFormat.Jpg:
  71. return SKEncodedImageFormat.Jpeg;
  72. case ImageFormat.Gif:
  73. return SKEncodedImageFormat.Gif;
  74. case ImageFormat.Webp:
  75. return SKEncodedImageFormat.Webp;
  76. case ImageFormat.Png:
  77. default:
  78. return SKEncodedImageFormat.Png;
  79. }
  80. }
  81. public void CropWhiteSpace(string inputPath, string outputPath)
  82. {
  83. CheckDisposed();
  84. using (var bitmap = SKBitmap.Decode(inputPath))
  85. {
  86. // @todo
  87. }
  88. _fileSystem.CopyFile(inputPath, outputPath, true);
  89. }
  90. public ImageSize GetImageSize(string path)
  91. {
  92. CheckDisposed();
  93. using (var s = new SKFileStream(path))
  94. {
  95. using (var codec = SKCodec.Create(s))
  96. {
  97. var info = codec.Info;
  98. return new ImageSize
  99. {
  100. Width = info.Width,
  101. Height = info.Height
  102. };
  103. }
  104. }
  105. }
  106. public void EncodeImage(string inputPath, string outputPath, bool autoOrient, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
  107. {
  108. if (string.IsNullOrWhiteSpace(inputPath))
  109. {
  110. throw new ArgumentNullException("inputPath");
  111. }
  112. if (string.IsNullOrWhiteSpace(inputPath))
  113. {
  114. throw new ArgumentNullException("outputPath");
  115. }
  116. using (var bitmap = SKBitmap.Decode(inputPath))
  117. {
  118. using (var resizedBitmap = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType))
  119. {
  120. // scale image
  121. bitmap.Resize(resizedBitmap, SKBitmapResizeMethod.Lanczos3);
  122. // create bitmap to use for canvas drawing
  123. using (var saveBitmap = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType))
  124. {
  125. // create canvas used to draw into bitmap
  126. using (var canvas = new SKCanvas(saveBitmap))
  127. {
  128. // set background color if present
  129. if (!string.IsNullOrWhiteSpace(options.BackgroundColor))
  130. {
  131. canvas.Clear(SKColor.Parse(options.BackgroundColor));
  132. }
  133. // Add blur if option is present
  134. if (options.Blur > 0)
  135. {
  136. using (var paint = new SKPaint())
  137. {
  138. // create image from resized bitmap to apply blur
  139. using (var filter = SKImageFilter.CreateBlur(5, 5))
  140. {
  141. paint.ImageFilter = filter;
  142. canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height), paint);
  143. }
  144. }
  145. }
  146. else
  147. {
  148. // draw resized bitmap onto canvas
  149. canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height));
  150. }
  151. // If foreground layer present then draw
  152. if (!string.IsNullOrWhiteSpace(options.ForegroundLayer))
  153. {
  154. Double opacity;
  155. if (!Double.TryParse(options.ForegroundLayer, out opacity)) opacity = .4;
  156. var foregroundColor = String.Format("#{0:X2}000000", (Byte)((1-opacity) * 0xFF));
  157. canvas.DrawColor(SKColor.Parse(foregroundColor), SKBlendMode.SrcOver);
  158. }
  159. DrawIndicator(canvas, width, height, options);
  160. using (var outputStream = new SKFileWStream(outputPath))
  161. {
  162. saveBitmap.Encode(outputStream, GetImageFormat(selectedOutputFormat), quality);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. public void CreateImageCollage(ImageCollageOptions options)
  170. {
  171. double ratio = options.Width;
  172. ratio /= options.Height;
  173. if (ratio >= 1.4)
  174. {
  175. new StripCollageBuilder(_appPaths, _fileSystem).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
  176. }
  177. else if (ratio >= .9)
  178. {
  179. new StripCollageBuilder(_appPaths, _fileSystem).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
  180. }
  181. else
  182. {
  183. new StripCollageBuilder(_appPaths, _fileSystem).BuildPosterCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
  184. }
  185. }
  186. private void DrawIndicator(SKCanvas canvas, int imageWidth, int imageHeight, ImageProcessingOptions options)
  187. {
  188. if (!options.AddPlayedIndicator && !options.UnplayedCount.HasValue && options.PercentPlayed.Equals(0))
  189. {
  190. return;
  191. }
  192. try
  193. {
  194. var currentImageSize = new ImageSize(imageWidth, imageHeight);
  195. if (options.AddPlayedIndicator)
  196. {
  197. var task = new PlayedIndicatorDrawer(_appPaths, _httpClientFactory(), _fileSystem).DrawPlayedIndicator(canvas, currentImageSize);
  198. Task.WaitAll(task);
  199. }
  200. else if (options.UnplayedCount.HasValue)
  201. {
  202. new UnplayedCountIndicator(_appPaths, _httpClientFactory(), _fileSystem).DrawUnplayedCountIndicator(canvas, currentImageSize, options.UnplayedCount.Value);
  203. }
  204. if (options.PercentPlayed > 0)
  205. {
  206. new PercentPlayedDrawer().Process(canvas, currentImageSize, options.PercentPlayed);
  207. }
  208. }
  209. catch (Exception ex)
  210. {
  211. _logger.ErrorException("Error drawing indicator overlay", ex);
  212. }
  213. }
  214. public string Name
  215. {
  216. get { return "Skia"; }
  217. }
  218. private bool _disposed;
  219. public void Dispose()
  220. {
  221. _disposed = true;
  222. }
  223. private void CheckDisposed()
  224. {
  225. if (_disposed)
  226. {
  227. throw new ObjectDisposedException(GetType().Name);
  228. }
  229. }
  230. public bool SupportsImageCollageCreation
  231. {
  232. get { return true; }
  233. }
  234. public bool SupportsImageEncoding
  235. {
  236. get { return true; }
  237. }
  238. }
  239. }