SkiaEncoder.cs 26 KB

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