aboutsummaryrefslogtreecommitdiff
path: root/src/Jellyfin.Drawing.Skia/SkiaHelper.cs
blob: 87446236cd37d2c213ccfa8458d85cf3c7ef9db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Collections.Generic;
using System.IO;
using SkiaSharp;

namespace Jellyfin.Drawing.Skia;

/// <summary>
/// Class containing helper methods for working with SkiaSharp.
/// </summary>
public static class SkiaHelper
{
    /// <summary>
    /// Gets the next valid image as a bitmap.
    /// </summary>
    /// <param name="skiaEncoder">The current skia encoder.</param>
    /// <param name="paths">The list of image paths.</param>
    /// <param name="currentIndex">The current checked index.</param>
    /// <param name="newIndex">The new index.</param>
    /// <returns>A valid bitmap, or null if no bitmap exists after <c>currentIndex</c>.</returns>
    public static SKBitmap? GetNextValidImage(SkiaEncoder skiaEncoder, IReadOnlyList<string> paths, int currentIndex, out int newIndex)
    {
        var imagesTested = new Dictionary<int, int>();

        while (imagesTested.Count < paths.Count)
        {
            if (currentIndex >= paths.Count)
            {
                currentIndex = 0;
            }

            var imagePath = paths[currentIndex];
            imagesTested[currentIndex] = 0;
            currentIndex++;

            if (!Path.Exists(imagePath))
            {
                continue;
            }

            SKBitmap? bitmap = skiaEncoder.Decode(imagePath, false, null, out _);

            if (bitmap is not null)
            {
                newIndex = currentIndex;
                return bitmap;
            }
        }

        newIndex = currentIndex;
        return null;
    }
}