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
|
#include ".image.h"
MLImage::MLImage(void)
{
loader = NULL;
loaderDelete = TRUE;
ResetData();
}
MLImage::MLImage(IMGLOADFUNC loader, BOOL deleteDone)
{
ResetData();
SetLoader(loader, deleteDone, FALSE);
}
MLImage::MLImage(int width, int height)
{
loader = NULL;
ResetData();
Init(width,height);
}
MLImage::~MLImage(void)
{
ResetData();
}
INT_PTR MLImage::SetLoader(IMGLOADFUNC loader, BOOL deleteDone, BOOL forceLoad)
{
this->loader = loader;
this->loaderDelete = deleteDone;
if (loader && forceLoad) Load();
return (loader != NULL) ? (INT_PTR) this : FALSE;
}
BOOL MLImage::Load(void)
{
ResetData();
if (!loader) return FALSE;
HBITMAP hbmpLoaded = loader((INT_PTR)this);
if(hbmpLoaded == NULL) return FALSE;
BITMAP bi;
if (GetObject(hbmpLoaded, sizeof(bi), &bi))
{
hbmp = ConvertTo32BppDIB(hbmpLoaded, bi.bmWidth, bi.bmHeight, &info, &data);
}
if (loaderDelete) DeleteObject(hbmpLoaded);
return (hbmp != NULL);
}
void MLImage::ResetData(void)
{
if (hbmp) DeleteObject(hbmp);
hbmp = NULL;
SecureZeroMemory(&info, sizeof(BITMAPINFO));
data = NULL;
}
BOOL MLImage::Draw(HDC hdcDest, int destX, int destY, int destWidth, int destHeight, int sourceX, int sourceY)
{
if (!hbmp) return FALSE;
int realheight = abs(info.bmiHeader.biHeight);
int rsX = min(sourceX, info.bmiHeader.biWidth);
int rsY = min(sourceY, info.bmiHeader.biWidth);
int height = min(destHeight, realheight - rsY);
BOOL bResult = SetDIBitsToDevice( hdcDest, destX, destY,
min(destWidth, info.bmiHeader.biWidth - rsX), height,
rsX, realheight - height - rsY,
0, height,
data, &info, DIB_RGB_COLORS);
return bResult;
}
BOOL MLImage::Draw(HDC hdcDest, int destX, int destY)
{
return (!hbmp) ? FALSE : SetDIBitsToDevice( hdcDest, destX, destY,
info.bmiHeader.biWidth, abs(info.bmiHeader.biHeight),
0, 0,
0, abs(info.bmiHeader.biHeight),
data, &info, DIB_RGB_COLORS);
}
int MLImage::GetWidth(void) const
{
return (hbmp) ? info.bmiHeader.biWidth : 0;
}
int MLImage::GetHeight(void) const
{
return (hbmp) ? abs(info.bmiHeader.biHeight) : 0;
}
void* MLImage::GetData(void) const
{
return data;
}
HBITMAP MLImage::ConvertTo32BppDIB(HBITMAP bmpHandle, int bmpWidth, int bmpHeight, LPBITMAPINFO bmpInfo, LPVOID *bmpData)
{
HBITMAP hbmpNew = NULL;
HDC hdc = GetWindowDC(NULL);
HDC hdcTmp = CreateCompatibleDC(hdc);
HBITMAP hbmpTmp = CreateCompatibleBitmap(hdc, bmpWidth, bmpHeight);
HBITMAP hbmpOld = (HBITMAP) SelectObject(hdcTmp, hbmpTmp);
// render original bitmap to the temp dc
HDC hdcBmp = CreateCompatibleDC(hdc);
SelectObject(hdcBmp, bmpHandle);
BitBlt(hdcTmp, 0, 0, bmpWidth, bmpHeight, hdcBmp, 0,0, SRCCOPY);
SelectObject(hdcBmp, NULL);
DeleteDC(hdcBmp);
// Create a 32 bit bitmap
BITMAPINFO bih;
// create DIB Section
bih.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bih.bmiHeader.biWidth = bmpWidth;
bih.bmiHeader.biHeight = 0 - bmpHeight;
bih.bmiHeader.biPlanes = 1;
bih.bmiHeader.biBitCount = 32;
bih.bmiHeader.biCompression = BI_RGB;
bih.bmiHeader.biSizeImage = 0;
bih.bmiHeader.biXPelsPerMeter = 0;
bih.bmiHeader.biYPelsPerMeter = 0;
bih.bmiHeader.biClrUsed = 0;
bih.bmiHeader.biClrImportant = 0;
// Create a DC which will be used to get DIB, then create DIBsection
hbmpNew = CreateDIBSection(hdc, (const BITMAPINFO*) &bih, DIB_RGB_COLORS, bmpData, NULL, 0);
DWORD* line = (DWORD*)(*bmpData);
// Copy the bits into our 32 bit dib..
for(int i=0; i<bmpHeight; i++)
{
for(int j=0; j<bmpWidth; j++)
{
line[(i*bmpWidth) + j] = FIXCOLORREF(GetPixel(hdcTmp, j, i));
}
}
SelectObject(hdcTmp, hbmpOld);
ReleaseDC(NULL, hdc);
DeleteDC(hdcTmp);
memcpy(bmpInfo, &bih, sizeof(BITMAPINFO));
return hbmpNew;
}
MLImage* MLImage::Copy(MLImage* destination, const MLImage* original)
{
if (!destination) return NULL;
destination->ResetData();
destination->loader = original->loader;
destination->loaderDelete = original->loaderDelete;
destination->info = original->info;
HDC hdc = GetWindowDC(NULL);
destination->hbmp = CreateDIBSection(hdc, (const BITMAPINFO*) &destination->info, DIB_RGB_COLORS, &destination->data, NULL, 0);
CopyMemory(destination->data, original->data, 4*destination->GetHeight() * destination->GetWidth());
ReleaseDC(NULL, hdc);
return destination;
}
MLImage* MLImage::Init(int width, int height)
{
ResetData();
loader = NULL;
loaderDelete = TRUE;
// create DIB Section
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = width;
info.bmiHeader.biHeight = 0 - height;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biSizeImage = 0;
info.bmiHeader.biXPelsPerMeter = 0;
info.bmiHeader.biYPelsPerMeter = 0;
info.bmiHeader.biClrUsed = 0;
info.bmiHeader.biClrImportant = 0;
HDC hdc = GetWindowDC(NULL);
hbmp = CreateDIBSection(hdc, (const BITMAPINFO*) &info, DIB_RGB_COLORS, &data, NULL, 0);
ReleaseDC(NULL, hdc);
return this;
}
MLImage* MLImage::Init(int width, int height, COLORREF color)
{
Init(width, height);
int rColor = FIXCOLORREF(color);
DWORD *line = (DWORD*)(data);
DWORD *end = line + GetHeight() * GetWidth();
for(;line != end; line++) *line = rColor;
return this;
}
|