-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPixelateGrid.h
185 lines (156 loc) · 4.37 KB
/
CPixelateGrid.h
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
#pragma once
class CPixelateGrid
{
CPixelateGrid() {};
~CPixelateGrid() {};
static bool OffsetFilter(Bitmap &b, Point **offset)
{
Rect bitmapRc(0,0,(INT)b.GetWidth(),(INT)b.GetHeight());
Bitmap *bSrc = b.Clone(bitmapRc, PixelFormat32bppRGB);
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData;
b.LockBits(&bitmapRc, ImageLockMode::ImageLockModeWrite, PixelFormat32bppRGB, &bmData);
BitmapData bmSrc;
bSrc->LockBits(&bitmapRc, ImageLockMode::ImageLockModeRead, PixelFormat32bppRGB, &bmSrc);
int scanline = bmSrc.Stride;
byte * p = (byte *)(void *)bmData.Scan0;
byte * pSrc = (byte *)(void *)bmSrc.Scan0;
#define bits 4
int nOffset = bmData.Stride - b.GetWidth() * bits;
int nWidth = b.GetWidth();
int nHeight = b.GetHeight();
int xOffset, yOffset;
for (int y = 0; y < nHeight; ++y)
{
for (int x = 0; x < nWidth; ++x)
{
xOffset = offset[x][y].X;
yOffset = offset[x][y].Y;
if (y + yOffset >= 0 && y + yOffset < nHeight && x + xOffset >= 0 && x + xOffset < nWidth)
{
p[0] = pSrc[((y + yOffset) * scanline) + ((x + xOffset) * bits)];
p[1] = pSrc[((y + yOffset) * scanline) + ((x + xOffset) * bits) + 1];
p[2] = pSrc[((y + yOffset) * scanline) + ((x + xOffset) * bits) + 2];
p[3] = pSrc[((y + yOffset) * scanline) + ((x + xOffset) * bits) + 3];
}
p += bits;
}
p += nOffset;
}
b.UnlockBits(&bmData);
bSrc->UnlockBits(&bmSrc);
delete bSrc;
return true;
}
static COLORREF CalcAvarageRectColor(Bitmap &bitmap, RECT &rc)
{
BitmapData bmSrc;
int iWid = (INT)bitmap.GetWidth(), iHei = (INT)bitmap.GetHeight();
Rect bitmapRc(0, 0, iWid, iHei);
bitmap.LockBits(&bitmapRc, ImageLockMode::ImageLockModeRead, PixelFormat32bppRGB, &bmSrc);
LPBYTE pLine =(LPBYTE)bmSrc.Scan0 + rc.top*bmSrc.Stride;
if (rc.right > iWid) rc.right = iWid;
if (rc.bottom > iHei) rc.bottom = iHei;
int nWid = rc.right - rc.left;
int nHei = rc.bottom - rc.top;
int r = 0, g = 0, b = 0;
for (int y = 0; y < nHei; y++)
{
LPBYTE p = pLine + rc.left * 4;
for (int x = 0; x < nWid; x++)
{
r += *p++;
g += *p++;
b += *p++;
p++;//skip alpha
}
pLine += bmSrc.Stride;
}
int nPixels = (nWid*nHei);
r /= nPixels;
g /= nPixels;
b /= nPixels;
bitmap.UnlockBits(&bmSrc);
return RGBA(r, g, b,255);
}
static bool OffsetFilter(Bitmap &b, short pixel, COLORREF **offset)
{
int nWidth = b.GetWidth();
int nHeight = b.GetHeight();
Rect bitmapRc(0, 0, nWidth, nHeight);
BitmapData bmData;
b.LockBits(&bitmapRc, ImageLockMode::ImageLockModeWrite, PixelFormat32bppRGB, &bmData);
COLORREF *p = (COLORREF *)bmData.Scan0;
for (int y = 0; y < nHeight; ++y)
{
for (int x = 0; x < nWidth; ++x)
{
p[0] = offset[x / pixel][y / pixel];
++p ;
}
p += 2*bmData.Stride/sizeof(COLORREF);
}
b.UnlockBits(&bmData);
return true;
}
public:
static bool Pixelate(Bitmap &b, short pixel, bool bGrid)
{
int nWidth = b.GetWidth();
int nHeight = b.GetHeight();
Point** pt = new Point*[nWidth];
for (int i = 0; i<nWidth; i++)
pt[i] = new Point[nHeight];
int newX, newY;
for (int x = 0; x < nWidth; ++x)
for (int y = 0; y < nHeight; ++y)
{
newX = pixel - x % pixel;
if (bGrid && newX == pixel)
pt[x][y].X = -x;
else if (x + newX > 0 && x + newX < nWidth)
pt[x][y].X = newX;
else
pt[x][y].X = 0;
newY = pixel - y % pixel;
if (bGrid && newY == pixel)
pt[x][y].Y = -y;
else if (y + newY > 0 && y + newY < nHeight)
pt[x][y].Y = newY;
else
pt[x][y].Y = 0;
}
OffsetFilter(b, pt);
for (int i = 0; i<nWidth; i++)
{
delete[nHeight] pt[i];
}
delete[nWidth]pt;
return true;
}
static bool Pixelate(Bitmap &b, short pixel)
{
int nWidth = b.GetWidth()/ pixel;
if ((b.GetWidth() % pixel) > 0)
++nWidth;
int nHeight = b.GetHeight() / pixel;
if ((b.GetHeight() % pixel) > 0)
++nHeight;
COLORREF** colorList = new COLORREF*[nWidth];
for (int i = 0; i < nWidth; i++)
colorList[i] = new COLORREF[nHeight];
for (int x = 0; x < nWidth; ++x)
for (int y = 0; y < nHeight; ++y)
{
RECT rc = { x*pixel,y*pixel, (x + 1)*pixel, (y + 1)*pixel };
colorList[x][y] = CalcAvarageRectColor(b, rc);
}
OffsetFilter(b,pixel, colorList);
for (int i = 0; i < nWidth; i++)
{
delete[nHeight] colorList[i];
}
delete[nWidth]colorList;
return true;
}
};