SkiaEncoder.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Blurhash.Core;
  9. using MediaBrowser.Common.Configuration;
  10. using MediaBrowser.Controller.Drawing;
  11. using MediaBrowser.Controller.Extensions;
  12. using MediaBrowser.Model.Drawing;
  13. using Microsoft.Extensions.Logging;
  14. using SkiaSharp;
  15. using static Jellyfin.Drawing.Skia.SkiaHelper;
  16. namespace Jellyfin.Drawing.Skia
  17. {
  18. /// <summary>
  19. /// Image encoder that uses <see cref="SkiaSharp"/> to manipulate images.
  20. /// </summary>
  21. public class SkiaEncoder : CoreEncoder, IImageEncoder
  22. {
  23. private static readonly HashSet<string> _transparentImageTypes
  24. = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".png", ".gif", ".webp" };
  25. private readonly ILogger _logger;
  26. private readonly IApplicationPaths _appPaths;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="SkiaEncoder"/> class.
  29. /// </summary>
  30. /// <param name="logger">The application logger.</param>
  31. /// <param name="appPaths">The application paths.</param>
  32. public SkiaEncoder(
  33. ILogger<SkiaEncoder> logger,
  34. IApplicationPaths appPaths)
  35. {
  36. _logger = logger;
  37. _appPaths = appPaths;
  38. }
  39. /// <inheritdoc/>
  40. public string Name => "Skia";
  41. /// <inheritdoc/>
  42. public bool SupportsImageCollageCreation => true;
  43. /// <inheritdoc/>
  44. public bool SupportsImageEncoding => true;
  45. /// <inheritdoc/>
  46. public IReadOnlyCollection<string> SupportedInputFormats =>
  47. new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  48. {
  49. "jpeg",
  50. "jpg",
  51. "png",
  52. "dng",
  53. "webp",
  54. "gif",
  55. "bmp",
  56. "ico",
  57. "astc",
  58. "ktx",
  59. "pkm",
  60. "wbmp",
  61. // TODO
  62. // Are all of these supported? https://github.com/google/skia/blob/master/infra/bots/recipes/test.py#L454
  63. // working on windows at least
  64. "cr2",
  65. "nef",
  66. "arw"
  67. };
  68. /// <inheritdoc/>
  69. public IReadOnlyCollection<ImageFormat> SupportedOutputFormats
  70. => new HashSet<ImageFormat>() { ImageFormat.Webp, ImageFormat.Jpg, ImageFormat.Png };
  71. /// <summary>
  72. /// Check if the native lib is available.
  73. /// </summary>
  74. /// <returns>True if the native lib is available, otherwise false.</returns>
  75. public static bool IsNativeLibAvailable()
  76. {
  77. try
  78. {
  79. // test an operation that requires the native library
  80. SKPMColor.PreMultiply(SKColors.Black);
  81. return true;
  82. }
  83. catch (Exception)
  84. {
  85. return false;
  86. }
  87. }
  88. private static bool IsTransparent(SKColor color)
  89. => (color.Red == 255 && color.Green == 255 && color.Blue == 255) || color.Alpha == 0;
  90. /// <summary>
  91. /// Convert a <see cref="ImageFormat"/> to a <see cref="SKEncodedImageFormat"/>.
  92. /// </summary>
  93. /// <param name="selectedFormat">The format to convert.</param>
  94. /// <returns>The converted format.</returns>
  95. public static SKEncodedImageFormat GetImageFormat(ImageFormat selectedFormat)
  96. {
  97. switch (selectedFormat)
  98. {
  99. case ImageFormat.Bmp:
  100. return SKEncodedImageFormat.Bmp;
  101. case ImageFormat.Jpg:
  102. return SKEncodedImageFormat.Jpeg;
  103. case ImageFormat.Gif:
  104. return SKEncodedImageFormat.Gif;
  105. case ImageFormat.Webp:
  106. return SKEncodedImageFormat.Webp;
  107. default:
  108. return SKEncodedImageFormat.Png;
  109. }
  110. }
  111. private static bool IsTransparentRow(SKBitmap bmp, int row)
  112. {
  113. for (var i = 0; i < bmp.Width; ++i)
  114. {
  115. if (!IsTransparent(bmp.GetPixel(i, row)))
  116. {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. private static bool IsTransparentColumn(SKBitmap bmp, int col)
  123. {
  124. for (var i = 0; i < bmp.Height; ++i)
  125. {
  126. if (!IsTransparent(bmp.GetPixel(col, i)))
  127. {
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. private SKBitmap CropWhiteSpace(SKBitmap bitmap)
  134. {
  135. var topmost = 0;
  136. for (int row = 0; row < bitmap.Height; ++row)
  137. {
  138. if (IsTransparentRow(bitmap, row))
  139. {
  140. topmost = row + 1;
  141. }
  142. else
  143. {
  144. break;
  145. }
  146. }
  147. int bottommost = bitmap.Height;
  148. for (int row = bitmap.Height - 1; row >= 0; --row)
  149. {
  150. if (IsTransparentRow(bitmap, row))
  151. {
  152. bottommost = row;
  153. }
  154. else
  155. {
  156. break;
  157. }
  158. }
  159. int leftmost = 0, rightmost = bitmap.Width;
  160. for (int col = 0; col < bitmap.Width; ++col)
  161. {
  162. if (IsTransparentColumn(bitmap, col))
  163. {
  164. leftmost = col + 1;
  165. }
  166. else
  167. {
  168. break;
  169. }
  170. }
  171. for (int col = bitmap.Width - 1; col >= 0; --col)
  172. {
  173. if (IsTransparentColumn(bitmap, col))
  174. {
  175. rightmost = col;
  176. }
  177. else
  178. {
  179. break;
  180. }
  181. }
  182. var newRect = SKRectI.Create(leftmost, topmost, rightmost - leftmost, bottommost - topmost);
  183. using (var image = SKImage.FromBitmap(bitmap))
  184. using (var subset = image.Subset(newRect))
  185. {
  186. return SKBitmap.FromImage(subset);
  187. }
  188. }
  189. /// <inheritdoc />
  190. /// <exception cref="ArgumentNullException">The path is null.</exception>
  191. /// <exception cref="FileNotFoundException">The path is not valid.</exception>
  192. /// <exception cref="SkiaCodecException">The file at the specified path could not be used to generate a codec.</exception>
  193. public ImageDimensions GetImageSize(string path)
  194. {
  195. if (!File.Exists(path))
  196. {
  197. throw new FileNotFoundException("File not found", path);
  198. }
  199. using (var codec = SKCodec.Create(path, out SKCodecResult result))
  200. {
  201. EnsureSuccess(result);
  202. var info = codec.Info;
  203. return new ImageDimensions(info.Width, info.Height);
  204. }
  205. }
  206. /// <inheritdoc />
  207. /// <exception cref="ArgumentNullException">The path is null.</exception>
  208. /// <exception cref="FileNotFoundException">The path is not valid.</exception>
  209. /// <exception cref="SkiaCodecException">The file at the specified path could not be used to generate a codec.</exception>
  210. [SuppressMessage("Microsoft.Performance", "CA1814:PreferJaggedArraysOverMultidimensional")]
  211. public string GetImageHash(string path)
  212. {
  213. if (path == null)
  214. {
  215. throw new ArgumentNullException(nameof(path));
  216. }
  217. if (!File.Exists(path))
  218. {
  219. throw new FileNotFoundException("File not found", path);
  220. }
  221. using (var bitmap = GetBitmap(path, false, false, null))
  222. {
  223. if (bitmap == null)
  224. {
  225. throw new ArgumentOutOfRangeException($"Skia unable to read image {path}");
  226. }
  227. var width = bitmap.Width;
  228. var height = bitmap.Height;
  229. var pixels = new Pixel[width, height];
  230. Parallel.ForEach(Enumerable.Range(0, height), y =>
  231. {
  232. for (var x = 0; x < width; x++)
  233. {
  234. var color = bitmap.GetPixel(x, y);
  235. pixels[x, y].Red = MathUtils.SRgbToLinear(color.Red);
  236. pixels[x, y].Green = MathUtils.SRgbToLinear(color.Green);
  237. pixels[x, y].Blue = MathUtils.SRgbToLinear(color.Blue);
  238. }
  239. });
  240. return CoreEncode(pixels, 4, 4);
  241. }
  242. }
  243. private static bool HasDiacritics(string text)
  244. => !string.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);
  245. private bool RequiresSpecialCharacterHack(string path)
  246. {
  247. for (int i = 0; i < path.Length; i++)
  248. {
  249. if (char.GetUnicodeCategory(path[i]) == UnicodeCategory.OtherLetter)
  250. {
  251. return true;
  252. }
  253. }
  254. if (HasDiacritics(path))
  255. {
  256. return true;
  257. }
  258. return false;
  259. }
  260. private string NormalizePath(string path)
  261. {
  262. if (!RequiresSpecialCharacterHack(path))
  263. {
  264. return path;
  265. }
  266. var tempPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + Path.GetExtension(path) ?? string.Empty);
  267. Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
  268. File.Copy(path, tempPath, true);
  269. return tempPath;
  270. }
  271. private static SKEncodedOrigin GetSKEncodedOrigin(ImageOrientation? orientation)
  272. {
  273. if (!orientation.HasValue)
  274. {
  275. return SKEncodedOrigin.TopLeft;
  276. }
  277. switch (orientation.Value)
  278. {
  279. case ImageOrientation.TopRight:
  280. return SKEncodedOrigin.TopRight;
  281. case ImageOrientation.RightTop:
  282. return SKEncodedOrigin.RightTop;
  283. case ImageOrientation.RightBottom:
  284. return SKEncodedOrigin.RightBottom;
  285. case ImageOrientation.LeftTop:
  286. return SKEncodedOrigin.LeftTop;
  287. case ImageOrientation.LeftBottom:
  288. return SKEncodedOrigin.LeftBottom;
  289. case ImageOrientation.BottomRight:
  290. return SKEncodedOrigin.BottomRight;
  291. case ImageOrientation.BottomLeft:
  292. return SKEncodedOrigin.BottomLeft;
  293. default:
  294. return SKEncodedOrigin.TopLeft;
  295. }
  296. }
  297. /// <summary>
  298. /// Decode an image.
  299. /// </summary>
  300. /// <param name="path">The filepath of the image to decode.</param>
  301. /// <param name="forceCleanBitmap">Whether to force clean the bitmap.</param>
  302. /// <param name="orientation">The orientation of the image.</param>
  303. /// <param name="origin">The detected origin of the image.</param>
  304. /// <returns>The resulting bitmap of the image.</returns>
  305. internal SKBitmap? Decode(string path, bool forceCleanBitmap, ImageOrientation? orientation, out SKEncodedOrigin origin)
  306. {
  307. if (!File.Exists(path))
  308. {
  309. throw new FileNotFoundException("File not found", path);
  310. }
  311. var requiresTransparencyHack = _transparentImageTypes.Contains(Path.GetExtension(path));
  312. if (requiresTransparencyHack || forceCleanBitmap)
  313. {
  314. using (var codec = SKCodec.Create(NormalizePath(path)))
  315. {
  316. if (codec == null)
  317. {
  318. origin = GetSKEncodedOrigin(orientation);
  319. return null;
  320. }
  321. // create the bitmap
  322. var bitmap = new SKBitmap(codec.Info.Width, codec.Info.Height, !requiresTransparencyHack);
  323. // decode
  324. _ = codec.GetPixels(bitmap.Info, bitmap.GetPixels());
  325. origin = codec.EncodedOrigin;
  326. return bitmap;
  327. }
  328. }
  329. var resultBitmap = SKBitmap.Decode(NormalizePath(path));
  330. if (resultBitmap == null)
  331. {
  332. return Decode(path, true, orientation, out origin);
  333. }
  334. // If we have to resize these they often end up distorted
  335. if (resultBitmap.ColorType == SKColorType.Gray8)
  336. {
  337. using (resultBitmap)
  338. {
  339. return Decode(path, true, orientation, out origin);
  340. }
  341. }
  342. origin = SKEncodedOrigin.TopLeft;
  343. return resultBitmap;
  344. }
  345. private SKBitmap? GetBitmap(string path, bool cropWhitespace, bool forceAnalyzeBitmap, ImageOrientation? orientation, out SKEncodedOrigin origin)
  346. {
  347. if (cropWhitespace)
  348. {
  349. using (var bitmap = Decode(path, forceAnalyzeBitmap, orientation, out origin))
  350. {
  351. if (bitmap == null)
  352. {
  353. return null;
  354. }
  355. return CropWhiteSpace(bitmap);
  356. }
  357. }
  358. return Decode(path, forceAnalyzeBitmap, orientation, out origin);
  359. }
  360. private SKBitmap? GetBitmap(string path, bool cropWhitespace, bool autoOrient, ImageOrientation? orientation)
  361. {
  362. if (autoOrient)
  363. {
  364. var bitmap = GetBitmap(path, cropWhitespace, true, orientation, out var origin);
  365. if (bitmap != null && origin != SKEncodedOrigin.TopLeft)
  366. {
  367. using (bitmap)
  368. {
  369. return OrientImage(bitmap, origin);
  370. }
  371. }
  372. return bitmap;
  373. }
  374. return GetBitmap(path, cropWhitespace, false, orientation, out _);
  375. }
  376. private SKBitmap OrientImage(SKBitmap bitmap, SKEncodedOrigin origin)
  377. {
  378. switch (origin)
  379. {
  380. case SKEncodedOrigin.TopRight:
  381. {
  382. var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
  383. using (var surface = new SKCanvas(rotated))
  384. {
  385. surface.Translate(rotated.Width, 0);
  386. surface.Scale(-1, 1);
  387. surface.DrawBitmap(bitmap, 0, 0);
  388. }
  389. return rotated;
  390. }
  391. case SKEncodedOrigin.BottomRight:
  392. {
  393. var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
  394. using (var surface = new SKCanvas(rotated))
  395. {
  396. float px = (float)bitmap.Width / 2;
  397. float py = (float)bitmap.Height / 2;
  398. surface.RotateDegrees(180, px, py);
  399. surface.DrawBitmap(bitmap, 0, 0);
  400. }
  401. return rotated;
  402. }
  403. case SKEncodedOrigin.BottomLeft:
  404. {
  405. var rotated = new SKBitmap(bitmap.Width, bitmap.Height);
  406. using (var surface = new SKCanvas(rotated))
  407. {
  408. float px = (float)bitmap.Width / 2;
  409. float py = (float)bitmap.Height / 2;
  410. surface.Translate(rotated.Width, 0);
  411. surface.Scale(-1, 1);
  412. surface.RotateDegrees(180, px, py);
  413. surface.DrawBitmap(bitmap, 0, 0);
  414. }
  415. return rotated;
  416. }
  417. case SKEncodedOrigin.LeftTop:
  418. {
  419. // TODO: Remove dual canvases, had trouble with flipping
  420. using (var rotated = new SKBitmap(bitmap.Height, bitmap.Width))
  421. {
  422. using (var surface = new SKCanvas(rotated))
  423. {
  424. surface.Translate(rotated.Width, 0);
  425. surface.RotateDegrees(90);
  426. surface.DrawBitmap(bitmap, 0, 0);
  427. }
  428. var flippedBitmap = new SKBitmap(rotated.Width, rotated.Height);
  429. using (var flippedCanvas = new SKCanvas(flippedBitmap))
  430. {
  431. flippedCanvas.Translate(flippedBitmap.Width, 0);
  432. flippedCanvas.Scale(-1, 1);
  433. flippedCanvas.DrawBitmap(rotated, 0, 0);
  434. }
  435. return flippedBitmap;
  436. }
  437. }
  438. case SKEncodedOrigin.RightTop:
  439. {
  440. var rotated = new SKBitmap(bitmap.Height, bitmap.Width);
  441. using (var surface = new SKCanvas(rotated))
  442. {
  443. surface.Translate(rotated.Width, 0);
  444. surface.RotateDegrees(90);
  445. surface.DrawBitmap(bitmap, 0, 0);
  446. }
  447. return rotated;
  448. }
  449. case SKEncodedOrigin.RightBottom:
  450. {
  451. // TODO: Remove dual canvases, had trouble with flipping
  452. using (var rotated = new SKBitmap(bitmap.Height, bitmap.Width))
  453. {
  454. using (var surface = new SKCanvas(rotated))
  455. {
  456. surface.Translate(0, rotated.Height);
  457. surface.RotateDegrees(270);
  458. surface.DrawBitmap(bitmap, 0, 0);
  459. }
  460. var flippedBitmap = new SKBitmap(rotated.Width, rotated.Height);
  461. using (var flippedCanvas = new SKCanvas(flippedBitmap))
  462. {
  463. flippedCanvas.Translate(flippedBitmap.Width, 0);
  464. flippedCanvas.Scale(-1, 1);
  465. flippedCanvas.DrawBitmap(rotated, 0, 0);
  466. }
  467. return flippedBitmap;
  468. }
  469. }
  470. case SKEncodedOrigin.LeftBottom:
  471. {
  472. var rotated = new SKBitmap(bitmap.Height, bitmap.Width);
  473. using (var surface = new SKCanvas(rotated))
  474. {
  475. surface.Translate(0, rotated.Height);
  476. surface.RotateDegrees(270);
  477. surface.DrawBitmap(bitmap, 0, 0);
  478. }
  479. return rotated;
  480. }
  481. default: return bitmap;
  482. }
  483. }
  484. /// <inheritdoc/>
  485. public string EncodeImage(string inputPath, DateTime dateModified, string outputPath, bool autoOrient, ImageOrientation? orientation, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
  486. {
  487. if (inputPath.Length == 0)
  488. {
  489. throw new ArgumentException("String can't be empty.", nameof(inputPath));
  490. }
  491. if (outputPath.Length == 0)
  492. {
  493. throw new ArgumentException("String can't be empty.", nameof(outputPath));
  494. }
  495. var skiaOutputFormat = GetImageFormat(selectedOutputFormat);
  496. var hasBackgroundColor = !string.IsNullOrWhiteSpace(options.BackgroundColor);
  497. var hasForegroundColor = !string.IsNullOrWhiteSpace(options.ForegroundLayer);
  498. var blur = options.Blur ?? 0;
  499. var hasIndicator = options.AddPlayedIndicator || options.UnplayedCount.HasValue || !options.PercentPlayed.Equals(0);
  500. using (var bitmap = GetBitmap(inputPath, options.CropWhiteSpace, autoOrient, orientation))
  501. {
  502. if (bitmap == null)
  503. {
  504. throw new InvalidDataException($"Skia unable to read image {inputPath}");
  505. }
  506. var originalImageSize = new ImageDimensions(bitmap.Width, bitmap.Height);
  507. if (!options.CropWhiteSpace
  508. && options.HasDefaultOptions(inputPath, originalImageSize)
  509. && !autoOrient)
  510. {
  511. // Just spit out the original file if all the options are default
  512. return inputPath;
  513. }
  514. var newImageSize = ImageHelper.GetNewImageSize(options, originalImageSize);
  515. var width = newImageSize.Width;
  516. var height = newImageSize.Height;
  517. using (var resizedBitmap = new SKBitmap(width, height, bitmap.ColorType, bitmap.AlphaType))
  518. {
  519. // scale image
  520. bitmap.ScalePixels(resizedBitmap, SKFilterQuality.High);
  521. // If all we're doing is resizing then we can stop now
  522. if (!hasBackgroundColor && !hasForegroundColor && blur == 0 && !hasIndicator)
  523. {
  524. Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
  525. using (var outputStream = new SKFileWStream(outputPath))
  526. using (var pixmap = new SKPixmap(new SKImageInfo(width, height), resizedBitmap.GetPixels()))
  527. {
  528. pixmap.Encode(outputStream, skiaOutputFormat, quality);
  529. return outputPath;
  530. }
  531. }
  532. // create bitmap to use for canvas drawing used to draw into bitmap
  533. using (var saveBitmap = new SKBitmap(width, height)) // , bitmap.ColorType, bitmap.AlphaType))
  534. using (var canvas = new SKCanvas(saveBitmap))
  535. {
  536. // set background color if present
  537. if (hasBackgroundColor)
  538. {
  539. canvas.Clear(SKColor.Parse(options.BackgroundColor));
  540. }
  541. // Add blur if option is present
  542. if (blur > 0)
  543. {
  544. // create image from resized bitmap to apply blur
  545. using (var paint = new SKPaint())
  546. using (var filter = SKImageFilter.CreateBlur(blur, blur))
  547. {
  548. paint.ImageFilter = filter;
  549. canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height), paint);
  550. }
  551. }
  552. else
  553. {
  554. // draw resized bitmap onto canvas
  555. canvas.DrawBitmap(resizedBitmap, SKRect.Create(width, height));
  556. }
  557. // If foreground layer present then draw
  558. if (hasForegroundColor)
  559. {
  560. if (!double.TryParse(options.ForegroundLayer, out double opacity))
  561. {
  562. opacity = .4;
  563. }
  564. canvas.DrawColor(new SKColor(0, 0, 0, (byte)((1 - opacity) * 0xFF)), SKBlendMode.SrcOver);
  565. }
  566. if (hasIndicator)
  567. {
  568. DrawIndicator(canvas, width, height, options);
  569. }
  570. Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
  571. using (var outputStream = new SKFileWStream(outputPath))
  572. {
  573. using (var pixmap = new SKPixmap(new SKImageInfo(width, height), saveBitmap.GetPixels()))
  574. {
  575. pixmap.Encode(outputStream, skiaOutputFormat, quality);
  576. }
  577. }
  578. }
  579. }
  580. }
  581. return outputPath;
  582. }
  583. /// <inheritdoc/>
  584. public void CreateImageCollage(ImageCollageOptions options)
  585. {
  586. double ratio = (double)options.Width / options.Height;
  587. if (ratio >= 1.4)
  588. {
  589. new StripCollageBuilder(this).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
  590. }
  591. else if (ratio >= .9)
  592. {
  593. new StripCollageBuilder(this).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
  594. }
  595. else
  596. {
  597. // TODO: Create Poster collage capability
  598. new StripCollageBuilder(this).BuildSquareCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
  599. }
  600. }
  601. private void DrawIndicator(SKCanvas canvas, int imageWidth, int imageHeight, ImageProcessingOptions options)
  602. {
  603. try
  604. {
  605. var currentImageSize = new ImageDimensions(imageWidth, imageHeight);
  606. if (options.AddPlayedIndicator)
  607. {
  608. PlayedIndicatorDrawer.DrawPlayedIndicator(canvas, currentImageSize);
  609. }
  610. else if (options.UnplayedCount.HasValue)
  611. {
  612. UnplayedCountIndicator.DrawUnplayedCountIndicator(canvas, currentImageSize, options.UnplayedCount.Value);
  613. }
  614. if (options.PercentPlayed > 0)
  615. {
  616. PercentPlayedDrawer.Process(canvas, currentImageSize, options.PercentPlayed);
  617. }
  618. }
  619. catch (Exception ex)
  620. {
  621. _logger.LogError(ex, "Error drawing indicator overlay");
  622. }
  623. }
  624. }
  625. }