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
|
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.ScreenCapture
{
public class ImageComparer
{
unsafe public Bitmap CreateDifferenceBitmap(Bitmap previousImage, Bitmap currentImage, Color matchColor)
{
if (previousImage == null | currentImage == null)
throw new InvalidOperationException("Cannot compare image. They are the same instance");
if (previousImage.Height != currentImage.Height || previousImage.Width != currentImage.Width)
throw new InvalidOperationException("Cannot compare image of different size.");
Bitmap diffImage = currentImage.Clone() as Bitmap;
int height = previousImage.Height;
int width = previousImage.Width;
BitmapData data1 = previousImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData data2 = currentImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData diffData = diffImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
byte* data1Ptr = (byte*)data1.Scan0;
byte* data2Ptr = (byte*)data2.Scan0;
byte* diffPtr = (byte*)diffData.Scan0;
byte[] swapColor = new byte[4];
swapColor[0] = matchColor.B;
swapColor[1] = matchColor.G;
swapColor[2] = matchColor.R;
swapColor[3] = matchColor.A;
int rowPadding = data1.Stride - (previousImage.Width * 4);
// iterate over height (rows)
for (int i = 0; i < height; i++)
{
// iterate over width (columns)
for (int j = 0; j < width; j++)
{
int same = 0;
byte[] tmp = new byte[4];
// compare pixels and copy new values into temporary array
for (int x = 0; x < 4; x++)
{
tmp[x] = data2Ptr[0];
if (data1Ptr[0] == data2Ptr[0])
{
same++;
}
data1Ptr++; // advance image1 ptr
data2Ptr++; // advance image2 ptr
}
// swap color or add new values
for (int x = 0; x < 4; x++)
{
diffPtr[0] = (same == 4) ? swapColor[x] : tmp[x];
diffPtr++; // advance diff image ptr
}
}
// at the end of each column, skip extra padding
if (rowPadding > 0)
{
data1Ptr += rowPadding;
data2Ptr += rowPadding;
diffPtr += rowPadding;
}
}
previousImage.UnlockBits(data1);
currentImage.UnlockBits(data2);
diffImage.UnlockBits(diffData);
return diffImage;
}
unsafe public VectorDifferenceImage CreateDifferenceVector(Bitmap previousImage, Bitmap currentImage)
{
VectorDifferenceImage vector = new VectorDifferenceImage(previousImage.Width, previousImage.Height);
if (previousImage == null | currentImage == null)
throw new InvalidOperationException("Cannot compare image. They are the same instance");
if (previousImage.Height != currentImage.Height || previousImage.Width != currentImage.Width)
throw new InvalidOperationException("Cannot compare image of different size.");
int height = previousImage.Height;
int width = previousImage.Width;
BitmapData data1 = previousImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData data2 = currentImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte* data1Ptr = (byte*)data1.Scan0;
byte* data2Ptr = (byte*)data2.Scan0;
int rowPadding = data1.Stride - (previousImage.Width * 4);
// iterate over height (rows)
for (int i = 0; i < height; i++)
{
// iterate over width (columns)
for (int j = 0; j < width; j++)
{
int same = 0;
byte[] tmp = new byte[4];
// compare pixels and copy new values into temporary array
for (int x = 0; x < 4; x++)
{
tmp[x] = data2Ptr[0];
if (data1Ptr[0] == data2Ptr[0])
{
same++;
}
data1Ptr++; // advance image1 ptr
data2Ptr++; // advance image2 ptr
}
if (same != 4)
{
vector.Pixels.Add(new VectorImagePixel()
{
X = (ushort)j,
Y = (ushort)i,
B = tmp[0],
G = tmp[1],
R = tmp[2],
});
}
}
// at the end of each column, skip extra padding
if (rowPadding > 0)
{
data1Ptr += rowPadding;
data2Ptr += rowPadding;
}
}
previousImage.UnlockBits(data1);
currentImage.UnlockBits(data2);
return vector;
}
}
}
|