SkiaHelper.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections.Generic;
  2. using SkiaSharp;
  3. namespace Jellyfin.Drawing.Skia
  4. {
  5. /// <summary>
  6. /// Class containing helper methods for working with SkiaSharp.
  7. /// </summary>
  8. public static class SkiaHelper
  9. {
  10. /// <summary>
  11. /// Ensures the result is a success
  12. /// by throwing an exception when that's not the case.
  13. /// </summary>
  14. /// <param name="result">The result returned by Skia.</param>
  15. public static void EnsureSuccess(SKCodecResult result)
  16. {
  17. if (result != SKCodecResult.Success)
  18. {
  19. throw new SkiaCodecException(result);
  20. }
  21. }
  22. /// <summary>
  23. /// Gets the next valid image as a bitmap.
  24. /// </summary>
  25. /// <param name="skiaEncoder">The current skia encoder.</param>
  26. /// <param name="paths">The list of image paths.</param>
  27. /// <param name="currentIndex">The current checked indes.</param>
  28. /// <param name="newIndex">The new index.</param>
  29. /// <returns>A valid bitmap, or null if no bitmap exists after <c>currentIndex</c>.</returns>
  30. public static SKBitmap? GetNextValidImage(SkiaEncoder skiaEncoder, IReadOnlyList<string> paths, int currentIndex, out int newIndex)
  31. {
  32. var imagesTested = new Dictionary<int, int>();
  33. SKBitmap? bitmap = null;
  34. while (imagesTested.Count < paths.Count)
  35. {
  36. if (currentIndex >= paths.Count)
  37. {
  38. currentIndex = 0;
  39. }
  40. bitmap = skiaEncoder.Decode(paths[currentIndex], false, null, out _);
  41. imagesTested[currentIndex] = 0;
  42. currentIndex++;
  43. if (bitmap != null)
  44. {
  45. break;
  46. }
  47. }
  48. newIndex = currentIndex;
  49. return bitmap;
  50. }
  51. }
  52. }