Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FastBitmap.cs « NesTiler - NesTiler.git/NesTiler.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e45a899e589a577abb646472bcc571e1ec6a7b2b (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace com.clusterrr.Famicom.NesTiler
{
    public class FastBitmap
    {
        public int Width { get; }
        public int Height { get; }

        private readonly int verticalOffset;
        private readonly SKColor[] pixels;
        private static Dictionary<string, (SKColor[] pixels, int w, int h)> imagesCache = new();

        private FastBitmap(SKColor[] pixels, int originalWidth, int originalHeight, int verticalOffset = 0, int height = -1)
        {
            Width = originalWidth;
            Height = height <= 0 ? originalHeight - verticalOffset : height;
            this.verticalOffset = verticalOffset;
            this.pixels = pixels;
        }

        public static FastBitmap? Decode(string filename, int verticalOffset = 0, int height = -1)
        {
            if (imagesCache.TryGetValue(filename, out (SKColor[] pixels, int w, int h) cachedImage))
            {
                return new FastBitmap(cachedImage.pixels, cachedImage.w, cachedImage.h, verticalOffset, height);
            }
            try
            {
                using (var image = SKBitmap.Decode(filename))
                {
                    if (image == null) return null;
                    var pixels = image.Pixels;
                    imagesCache[filename] = (pixels, image.Width, image.Height);
                    return new FastBitmap(pixels, image.Width, image.Height, verticalOffset, height);
                }
            }
            finally
            {
                GC.Collect();
            }
        }

        public SKColor GetPixelColor(int x, int y, SKColor? defaultColor = null)
        {
            var index = ((y + verticalOffset) * Width) + x;
            if (index >= pixels.Length)
            {
                if (defaultColor.HasValue) return defaultColor.Value;
                throw new IndexOutOfRangeException($"Pixel {x}x{y} is out of range");
            }
            return pixels[index];
        }

        public void SetPixelColor(int x, int y, SKColor color)
        {
            pixels[((y + verticalOffset) * Width) + x] = color;
        }

        public byte[] Encode(SKEncodedImageFormat format, int v)
        {
            using var skImage = new SKBitmap(Width, Height);
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    var color = GetPixelColor(x, y);
                    skImage.SetPixel(x, y, color);
                }
            }
            return skImage.Encode(format, v).ToArray();
        }
    }
}