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