SkiaHelper.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. /// Gets the next valid image as a bitmap.
  12. /// </summary>
  13. /// <param name="skiaEncoder">The current skia encoder.</param>
  14. /// <param name="paths">The list of image paths.</param>
  15. /// <param name="currentIndex">The current checked index.</param>
  16. /// <param name="newIndex">The new index.</param>
  17. /// <returns>A valid bitmap, or null if no bitmap exists after <c>currentIndex</c>.</returns>
  18. public static SKBitmap? GetNextValidImage(SkiaEncoder skiaEncoder, IReadOnlyList<string> paths, int currentIndex, out int newIndex)
  19. {
  20. var imagesTested = new Dictionary<int, int>();
  21. SKBitmap? bitmap = null;
  22. while (imagesTested.Count < paths.Count)
  23. {
  24. if (currentIndex >= paths.Count)
  25. {
  26. currentIndex = 0;
  27. }
  28. bitmap = skiaEncoder.Decode(paths[currentIndex], false, null, out _);
  29. imagesTested[currentIndex] = 0;
  30. currentIndex++;
  31. if (bitmap != null)
  32. {
  33. break;
  34. }
  35. }
  36. newIndex = currentIndex;
  37. return bitmap;
  38. }
  39. }
  40. }