-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReadonlyImage.cs
217 lines (192 loc) · 6.1 KB
/
ReadonlyImage.cs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Holocure_Auto_Fishing_Bot
{
internal readonly struct ARGBColor
{
public byte A { get; }
public byte R { get; }
public byte G { get; }
public byte B { get; }
public ARGBColor(int alpha, int red, int green, int blue)
{
A = (byte)alpha;
R = (byte)red;
G = (byte)green;
B = (byte)blue;
}
public float ColorDiff(ARGBColor other)
{
int dR = Math.Abs(R - other.R);
int dG = Math.Abs(G - other.G);
int dB = Math.Abs(B - other.B);
// Scale between 0 and 1
return (float)(dR + dG + dB) / (3 * 255);
}
}
internal sealed class ReadonlyImage
{
private readonly ARGBColor[] _pixels;
private int _opaqueCount = -1;
#region Properties
public int Width { get; }
public int Height { get; }
public int OpaqueCount
{
get
{
if (_opaqueCount == -1)
{
InitOpaqueCount();
}
return _opaqueCount;
}
}
public ARGBColor this[int x, int y]
{
get => _pixels[y * Width + x];
set => _pixels[y * Width + x] = value;
}
#endregion
public ReadonlyImage(int width, int height)
{
Width = width;
Height = height;
_pixels = new ARGBColor[Width * Height];
}
public ReadonlyImage(Bitmap source)
: this(source.Width, source.Height)
{
BitmapData data = source.LockBits(
new Rectangle(0, 0, source.Width, source.Height),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb
);
unsafe
{
byte* ptrCurrentRow = (byte*)data.Scan0;
for (int y = 0; y < data.Height; y++, ptrCurrentRow += data.Stride)
{
for (int x = 0; x < data.Width; x++)
{
this[x, y] = new ARGBColor(
ptrCurrentRow[x * 4 + 3],
ptrCurrentRow[x * 4 + 2],
ptrCurrentRow[x * 4 + 1],
ptrCurrentRow[x * 4 + 0]
);
}
}
}
source.UnlockBits(data);
source.Dispose();
}
public ReadonlyImage(string filename)
: this(new Bitmap(filename)) { }
private void InitOpaqueCount()
{
_opaqueCount = 0;
foreach (ARGBColor color in _pixels)
{
if (color.A == 255)
{
_opaqueCount++;
}
}
}
public ReadonlyImage Crop(int left, int top, int width, int height)
{
ReadonlyImage cropped = new ReadonlyImage(width, height);
for (int i = Math.Max(-left, 0); i < Math.Min(width, Width - left); i++)
{
for (int j = Math.Max(-top, 0); j < Math.Min(height, Height - top); j++)
{
cropped[i, j] = this[left + i, top + j];
}
}
return cropped;
}
public ReadonlyImage ShrinkBy(int factor)
{
ReadonlyImage shrunk = new ReadonlyImage(Width / factor, Height / factor);
int xOffset = Width % factor;
int yOffset = Height % factor;
for (int i = 0; i < shrunk.Width; i++)
{
for (int j = 0; j < shrunk.Height; j++)
{
shrunk[i, j] = this[i * factor + xOffset, j * factor + yOffset];
}
}
return shrunk;
}
public bool CroppedEquals(
ReadonlyImage other,
int left = 0,
int top = 0,
float threshold = 0.108f
)
{
threshold *= other.OpaqueCount;
float sum = 0;
for (int i = 0; i < other.Width; i++)
{
for (int j = 0; j < other.Height; j++)
{
if (other[i, j].A == 0)
{
continue;
}
sum += this[left + i, top + j].ColorDiff(other[i, j]);
if (sum > threshold)
{
return false;
}
}
}
return true;
}
public (int x, int y) Find(ReadonlyImage other)
{
for (int x = 0; x <= Width - other.Width; x++)
{
for (int y = 0; y <= Height - other.Height; y++)
{
if (CroppedEquals(other, x, y))
{
return (x, y);
}
}
}
return (-1, -1);
}
public void Save(string path)
{
Bitmap bmp = new Bitmap(Width, Height);
BitmapData data = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.WriteOnly,
PixelFormat.Format32bppArgb
);
unsafe
{
byte* ptrCurrentRow = (byte*)data.Scan0;
for (int y = 0; y < data.Height; y++, ptrCurrentRow += data.Stride)
{
for (int x = 0; x < data.Width; x++)
{
ARGBColor color = this[x, y];
ptrCurrentRow[x * 4 + 3] = color.A;
ptrCurrentRow[x * 4 + 2] = color.R;
ptrCurrentRow[x * 4 + 1] = color.G;
ptrCurrentRow[x * 4] = color.B;
}
}
}
bmp.UnlockBits(data);
bmp.Save(path);
bmp.Dispose();
}
}
}