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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
#include "./imageLoader.h"
#include "./common.h"
#include "../api.h"
#include <api/service/waservicefactory.h>
#include <shlwapi.h>
template <class api_T>
void ImageLoader_ServiceBuild(api_T *&api_t, GUID factoryGUID_t)
{
if (WASABI_API_SVC)
{
waServiceFactory *factory = WASABI_API_SVC->service_getServiceByGuid(factoryGUID_t);
if (factory)
api_t = (api_T *)factory->getInterface();
}
}
typedef BOOL (CALLBACK *IMAGEDATAPROCESSOR)(const void * /*data*/, size_t /*dataSize*/, ULONG_PTR /*param*/);
typedef struct __LOADDATAPARAM
{
BOOL premultiply;
INT cx;
INT cy;
void *pixels;
} LOADDATAPARAM;
typedef struct __GETDIMENSIONPARAM
{
INT cx;
INT cy;
} GETDIMENSIONPARAM;
static BOOL CALLBACK ImageLoader_LoadDataCallback(const void *data, size_t size, ULONG_PTR param)
{
LOADDATAPARAM *loadParam = (LOADDATAPARAM*)param;
if (NULL == loadParam) return FALSE;
if (NULL == WASABI_API_PNGLOADER)
{
ImageLoader_ServiceBuild(WASABI_API_PNGLOADER, pngLoaderGUID);
if (NULL == WASABI_API_PNGLOADER)
return FALSE;
}
loadParam->pixels = (FALSE == loadParam->premultiply) ?
WASABI_API_PNGLOADER->loadImageData(data, (INT)size, &loadParam->cx, &loadParam->cy) :
WASABI_API_PNGLOADER->loadImage(data, (INT)size, &loadParam->cx, &loadParam->cy);
return (NULL != loadParam->pixels);
}
static BOOL CALLBACK ImageLoader_GetDimensionsCallback(const void *data, size_t size, ULONG_PTR param)
{
GETDIMENSIONPARAM *dimensionParam = (GETDIMENSIONPARAM*)param;
if (NULL == dimensionParam) return FALSE;
if (NULL == WASABI_API_PNGLOADER)
{
ImageLoader_ServiceBuild(WASABI_API_PNGLOADER, pngLoaderGUID);
if (NULL == WASABI_API_PNGLOADER)
return FALSE;
}
return WASABI_API_PNGLOADER->getDimensions(data, (INT)size, &dimensionParam->cx, &dimensionParam->cy);
}
static BOOL ImageLoader_ProcessResource(HINSTANCE hInstance, LPCWSTR pszName, LPCWSTR pszType, IMAGEDATAPROCESSOR processor, ULONG_PTR param)
{
HRSRC res = FindResourceW(hInstance, pszName, pszType);
if (NULL == res) return FALSE;
BOOL fSucceeded = FALSE;
HANDLE handle = LoadResource(hInstance, res);
if (NULL != handle)
{
UINT resourceSize = SizeofResource(hInstance, res);
if (0 != resourceSize)
{
void *resourceData = LockResource(handle);
if (NULL != resourceData)
fSucceeded = processor(resourceData, resourceSize, param);
}
FreeResource(handle);
}
return fSucceeded;
}
static HRESULT ImageLoader_ParseResProtocol(LPWSTR pszAddress, LPCWSTR defaultType, HINSTANCE *module, LPCWSTR *resourceName, LPCWSTR *resourceType)
{
if (NULL == module || NULL == resourceName || NULL == resourceType)
return E_POINTER;
if (NULL == pszAddress || L'\0' == *pszAddress)
return E_INVALIDARG;
INT cchAddress = lstrlenW(pszAddress);
const WCHAR szPrefix[] = L"res://";
INT cchPrefix = ARRAYSIZE(szPrefix) - 1;
if (cchAddress <= cchPrefix)
return S_FALSE;
if (CSTR_EQUAL != CompareStringW(CSTR_INVARIANT, NORM_IGNORECASE, pszAddress, cchPrefix, szPrefix, cchPrefix))
return S_FALSE;
pszAddress += cchPrefix;
cchAddress -= cchPrefix;
LPWSTR resType = NULL;
LPWSTR resName = NULL;
LPWSTR p = pszAddress + cchAddress;
while (p != pszAddress && L'/' != *p) p--;
if (p != pszAddress && p < (pszAddress + cchAddress))
{
resName = p + 1;
*p = L'\0';
p--;
}
if (NULL == resName || L'\0' == *resName)
return E_FAIL;
while (p != pszAddress && L'/' != *p) p--;
if (p != pszAddress && p < resName)
{
resType = p + 1;
if (L'\0' == *resType)
{
resType = NULL;
}
else
{
resType = p + 1;
*p = L'\0';
p--;
}
}
HINSTANCE hModule;
hModule = LoadLibraryExW(pszAddress, NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
if (NULL == hModule)
{
UINT errorCode = GetLastError();
if (NULL != resType)
{
*(resType - 1) = L'/';
resType = NULL;
hModule = LoadLibraryExW(pszAddress, NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
if (NULL == hModule) errorCode = GetLastError();
}
if (ERROR_SUCCESS != errorCode)
return HRESULT_FROM_WIN32(errorCode);
}
if (NULL == resType)
resType = (LPWSTR)defaultType;
if (NULL != resType && FALSE == IS_INTRESOURCE(resType) && L'#' == *resType)
{
INT typeId;
if (FALSE != StrToIntExW(resType + 1, STIF_DEFAULT, &typeId))
resType = MAKEINTRESOURCEW(typeId);
}
if (NULL != resName && FALSE == IS_INTRESOURCE(resName) && L'#' == *resName)
{
INT nameId;
if (FALSE != StrToIntExW(resName + 1, STIF_DEFAULT, &nameId))
resName = MAKEINTRESOURCEW(nameId);
}
*module = hModule;
*resourceName = resName;
*resourceType = resType;
return S_OK;
}
static BOOL ImageLoader_ProcessFile(LPCWSTR pszPath, IMAGEDATAPROCESSOR processor, ULONG_PTR param)
{
HINSTANCE resModule;
LPCWSTR resName, resType;
BOOL fSucceeded = FALSE;
LPWSTR name = LoginBox_CopyString(pszPath);
HRESULT hr = ImageLoader_ParseResProtocol(name, RT_RCDATA, &resModule, &resName, &resType);
if (S_OK == hr)
{
fSucceeded = ImageLoader_ProcessResource(resModule, resName, resType, processor, param);
LoginBox_FreeString(name);
return fSucceeded;
}
LoginBox_FreeString(name);
if (FAILED(hr))
return FALSE;
HANDLE hFile = CreateFileW(pszPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (INVALID_HANDLE_VALUE == hFile)
return FALSE;
UINT resourceSize = GetFileSize(hFile, NULL);
if (INVALID_FILE_SIZE != resourceSize)
{
void *resourceData = malloc(resourceSize);
if (NULL != resourceData)
{
DWORD readed = 0;
if (0 != ReadFile(hFile, resourceData, resourceSize, &readed, NULL) || resourceSize != readed)
fSucceeded = processor(resourceData, resourceSize, param);
free(resourceData);
}
}
CloseHandle(hFile);
return fSucceeded;
}
void *ImageLoader_LoadData(HINSTANCE hInstance, LPCWSTR pszName, BOOL fPremultiply, INT *widthOut, INT *heightOut)
{
BOOL fSucceeded;
LOADDATAPARAM param;
param.premultiply = fPremultiply;
if (NULL == hInstance && !IS_INTRESOURCE(pszName))
fSucceeded = ImageLoader_ProcessFile(pszName, ImageLoader_LoadDataCallback, (ULONG_PTR)¶m);
else
fSucceeded = ImageLoader_ProcessResource(hInstance, pszName, RT_RCDATA, ImageLoader_LoadDataCallback, (ULONG_PTR)¶m);
if (FALSE == fSucceeded)
{
if (NULL != widthOut) *widthOut = 0;
if (NULL != heightOut) *heightOut = 0;
return NULL;
}
if (NULL != widthOut) *widthOut = param.cx;
if (NULL != heightOut) *heightOut = param.cy;
return param.pixels;
}
void ImageLoader_FreeData(void *data)
{
if (NULL == data)
return;
if (NULL == WASABI_API_MEMMNGR)
{
ImageLoader_ServiceBuild(WASABI_API_MEMMNGR, memMgrApiServiceGuid);
if (NULL == WASABI_API_MEMMNGR) return;
}
WASABI_API_MEMMNGR->sysFree(data);
}
HBITMAP ImageLoader_LoadBitmapEx(HINSTANCE hInstance, LPCWSTR pszName, BOOL fPremultiply, BITMAPINFOHEADER *headerInfo, void **dataOut)
{
INT imageCX, imageCY;
void *data = ImageLoader_LoadData(hInstance, pszName, fPremultiply, &imageCX, &imageCY);
if (NULL == data) return NULL;
ZeroMemory(headerInfo, sizeof(BITMAPINFOHEADER));
headerInfo->biSize = sizeof(BITMAPINFOHEADER);
headerInfo->biCompression = BI_RGB;
headerInfo->biBitCount = 32;
headerInfo->biPlanes = 1;
headerInfo->biWidth = imageCX;
headerInfo->biHeight = -imageCY;
void *pixelData;
HBITMAP bitmap = CreateDIBSection(NULL, (LPBITMAPINFO)headerInfo, DIB_RGB_COLORS, &pixelData, NULL, 0);
if (NULL != bitmap)
{
if (NULL != dataOut) *dataOut = pixelData;
CopyMemory(pixelData, data, headerInfo->biWidth * abs(headerInfo->biHeight) * sizeof(DWORD));
}
else
{
if (NULL != dataOut) *dataOut = NULL;
}
ImageLoader_FreeData(data);
return bitmap;
}
HBITMAP ImageLoader_LoadBitmap(HINSTANCE hInstance, LPCWSTR pszName, BOOL fPremultiply, INT *widthOut, INT *heightOut)
{
BITMAPINFOHEADER header;
HBITMAP bitmap = ImageLoader_LoadBitmapEx(hInstance, pszName, fPremultiply, &header, NULL);
if (NULL != bitmap)
{
if (NULL != widthOut) *widthOut = header.biWidth;
if (NULL != heightOut) *heightOut = header.biHeight;
}
else
{
if (NULL != widthOut) *widthOut = 0;
if (NULL != heightOut) *heightOut = 0;
}
return bitmap;
}
BOOL ImageLoader_GetDimensions(HINSTANCE hInstance, LPCWSTR pszName, INT *widthOut, INT *heightOut)
{
BOOL fSucceeded;
GETDIMENSIONPARAM param;
if (NULL == hInstance && !IS_INTRESOURCE(pszName))
fSucceeded = ImageLoader_ProcessFile(pszName, ImageLoader_GetDimensionsCallback, (ULONG_PTR)¶m);
else
fSucceeded = ImageLoader_ProcessResource(hInstance, pszName, RT_RCDATA, ImageLoader_GetDimensionsCallback, (ULONG_PTR)¶m);
if (FALSE == fSucceeded)
{
if (NULL != widthOut) *widthOut = 0;
if (NULL != heightOut) *heightOut = 0;
return FALSE;
}
if (NULL != widthOut) *widthOut = param.cx;
if (NULL != heightOut) *heightOut = param.cy;
return TRUE;
}
|