e0aca1b279c51dd8bf8ec68cdd2a9b3c40ea4083
[reactos.git] / rostests / apitests / gdi32 / CreateDIBitmap.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for CreateDIBitmap
5 * PROGRAMMERS: Timo Kreuzer
6 */
7
8 #include <apitest.h>
9
10 #include <wingdi.h>
11 #include <winuser.h>
12
13 #include "init.h"
14
15 #define CBM_CREATDIB 2
16
17 BOOL
18 GetExpected(
19 DWORD *pdwError,
20 HDC hdc,
21 const BITMAPINFOHEADER *lpbmih,
22 DWORD fdwInit,
23 const VOID *lpbInit,
24 const BITMAPINFO *lpbmi,
25 UINT fuUsage)
26 {
27 if (fuUsage > 2)
28 {
29 *pdwError = ERROR_INVALID_PARAMETER;
30 return FALSE;
31 }
32
33 if (fuUsage != DIB_RGB_COLORS)
34 {
35 if (hdc == (HDC)-1)
36 {
37 return FALSE;
38 }
39 }
40
41 if (fdwInit & CBM_INIT)
42 {
43 if (!lpbmih)
44 {
45 if (!lpbInit || (lpbInit == (PVOID)0xC0000000)) return FALSE;
46 }
47 else
48 {
49 if (lpbInit)
50 {
51 if (lpbInit == (PVOID)0xC0000000) return FALSE;
52 if (!lpbmi || (lpbmi == (PVOID)0xC0000000)) return FALSE;
53 if (lpbmi->bmiHeader.biSize == 0) return FALSE;
54 if (fuUsage == 2) return FALSE;
55 }
56 }
57 }
58
59
60 if (fdwInit & CBM_CREATDIB)
61 {
62 if (fuUsage == 2) return FALSE;
63 if ((fuUsage == DIB_PAL_COLORS) && !hdc)
64 {
65 return FALSE;
66 }
67
68 if ((fdwInit & CBM_INIT))
69 {
70 if (!lpbInit || (lpbInit == (PVOID)0xC0000000)) return FALSE;
71 }
72
73 if ((!lpbmi) || (lpbmi == (PVOID)0xc0000000) || (lpbmi->bmiHeader.biSize == 0))
74 {
75 return FALSE;
76 }
77
78 return TRUE;
79 }
80
81 if ((lpbmih == NULL) ||
82 (lpbmih == (PVOID)0xC0000000) ||
83 (lpbmih->biSize == 0))
84 {
85 return FALSE;
86 }
87
88 if (hdc == (HDC)-1)
89 {
90 *pdwError = ERROR_INVALID_PARAMETER;
91 return FALSE;
92 }
93
94
95 if (lpbmi == (PVOID)0xc0000000) return FALSE;
96
97
98 return TRUE;
99 }
100
101
102 void
103 Test_CreateDIBitmap_Params(void)
104 {
105 HBITMAP hbmp;
106 HDC hdc;
107 BITMAPINFO bmi =
108 {{sizeof(BITMAPINFOHEADER), 4, 4, 1, 8, BI_RGB, 0, 1, 1, 1, 0}, {{0,0,0,0}}};
109 BITMAPINFO bmiBroken =
110 {{0, -2, -4, 55, 42, 21, 0, 1, 1, 1, 0}, {{0,0,0,0}}};
111 BYTE ajBits[10];
112
113 hdc = CreateCompatibleDC(0);
114 ok(hdc != 0, "failed\n");
115
116 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, ajBits, &bmi, DIB_PAL_COLORS);
117 ok(hbmp != 0, "\n");
118
119 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, NULL, &bmi, DIB_PAL_COLORS);
120 ok(hbmp != 0, "\n");
121
122 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, NULL, NULL, DIB_PAL_COLORS);
123 ok(hbmp != 0, "\n");
124
125 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, 0, (PVOID)0xc0000000, &bmi, DIB_PAL_COLORS);
126 ok(hbmp != 0, "\n");
127
128 hbmp = CreateDIBitmap(NULL, &bmi.bmiHeader, CBM_INIT, NULL, &bmi, DIB_PAL_COLORS);
129 ok(hbmp != 0, "\n");
130
131 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, NULL, &bmiBroken, DIB_PAL_COLORS);
132 ok(hbmp != 0, "\n");
133
134 hbmp = CreateDIBitmap(NULL, NULL, 2, NULL, &bmi, 0);
135 ok(hbmp != 0, "\n");
136
137 SetLastError(0xbadbad00);
138 hbmp = CreateDIBitmap(hdc, NULL, CBM_INIT, ajBits, &bmi, DIB_PAL_COLORS);
139 ok(hbmp == 0, "\n");
140 ok_err(ERROR_INVALID_PARAMETER);
141
142 SetLastError(0xbadbad00);
143 hbmp = CreateDIBitmap(hdc, NULL, CBM_INIT, NULL, &bmiBroken, DIB_PAL_COLORS);
144 ok(hbmp == 0, "\n");
145 ok_err(ERROR_INVALID_PARAMETER);
146
147 SetLastError(0xbadbad00);
148 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, ajBits, &bmi, 2);
149 ok(hbmp == 0, "\n");
150 ok_err(ERROR_INVALID_PARAMETER);
151
152 SetLastError(0xbadbad00);
153 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, ajBits, NULL, DIB_PAL_COLORS);
154 ok(hbmp == 0, "\n");
155 ok_err(0xbadbad00);
156
157 SetLastError(0xbadbad00);
158 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, (PVOID)0xc0000000, &bmi, DIB_PAL_COLORS);
159 ok(hbmp == 0, "\n");
160 ok_err(0xbadbad00);
161
162 SetLastError(0xbadbad00);
163 _SEH2_TRY
164 {
165 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, 0, ajBits, (PVOID)0xc0000000, DIB_PAL_COLORS);
166 }
167 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
168 {
169 hbmp = (HBITMAP)-1;
170 }
171 _SEH2_END;
172 ok(hbmp == (HBITMAP)-1, "\n");
173 ok_err(0xbadbad00);
174
175 SetLastError(0xbadbad00);
176 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, NULL, NULL, 5);
177 ok(hbmp == 0, "\n");
178 ok_err(ERROR_INVALID_PARAMETER);
179
180 SetLastError(0xbadbad00);
181 hbmp = CreateDIBitmap((HDC)-1, &bmi.bmiHeader, CBM_INIT, NULL, &bmi, DIB_PAL_COLORS);
182 ok(hbmp == 0, "\n");
183 ok_err(0xbadbad00);
184
185 SetLastError(0xbadbad00);
186 hbmp = CreateDIBitmap(hdc, &bmiBroken.bmiHeader, CBM_INIT, NULL, &bmi, DIB_PAL_COLORS);
187 ok(hbmp == 0, "\n");
188 ok_err(ERROR_INVALID_PARAMETER);
189
190 if (1)
191 {
192 ULONG i1, i2, i3, i4, i5, i6;
193 HDC ahdc[3] = {0, hdc, (HDC)-1};
194 PBITMAPINFOHEADER apbih[4] = {NULL, &bmi.bmiHeader, &bmiBroken.bmiHeader, (PVOID)0xC0000000};
195 ULONG afInitf[12] = {0, 1, 2, 3, CBM_INIT, 4, 5, 6, 7, 8, 0x10, 0x20};
196 PVOID apvBits[3] = {NULL, ajBits, (PVOID)0xc0000000};
197 PBITMAPINFO apbmi[4] = {NULL, &bmi, &bmiBroken, (PVOID)0xC0000000};
198 ULONG aiUsage[5] = {0, 1, 2, 3, 23};
199 DWORD dwExpError;
200 BOOL bExpSuccess;
201
202 for (i1 = 0; i1 < 3; i1++)
203 {
204 for (i2 = 0; i2 < 4; i2++)
205 {
206 for (i3 = 0; i3 < 8; i3++)
207 {
208 for (i4 = 0; i4 < 3; i4++)
209 {
210 for (i5 = 0; i5 < 4; i5++)
211 {
212 for (i6 = 0; i6 < 5; i6++)
213 {
214 SetLastError(0xbadbad00);
215 dwExpError = 0xbadbad00;
216
217 bExpSuccess = GetExpected(&dwExpError, ahdc[i1], apbih[i2], afInitf[i3], apvBits[i4], apbmi[i5], aiUsage[i6]);
218
219 _SEH2_TRY
220 {
221 hbmp = CreateDIBitmap(ahdc[i1], apbih[i2], afInitf[i3], apvBits[i4], apbmi[i5], aiUsage[i6]);
222 }
223 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
224 {
225 hbmp = (HBITMAP)0;
226 }
227 _SEH2_END;
228
229 if (bExpSuccess)
230 {
231 ok(hbmp != 0, "Expected success for (%ld,%ld,%ld,%ld,%ld,%ld) CreateDIBitmap(%p, %p, 0x%lx, %p, %p, %ld)\n",
232 i1, i2, i3, i4, i5, i6,
233 ahdc[i1], apbih[i2], afInitf[i3], apvBits[i4], apbmi[i5], aiUsage[i6]);
234 }
235 else
236 {
237 ok(hbmp == 0, "Expected failure for (%ld,%ld,%ld,%ld,%ld,%ld) CreateDIBitmap(%p, %p, 0x%lx, %p, %p, %ld)\n",
238 i1, i2, i3, i4, i5, i6,
239 ahdc[i1], apbih[i2], afInitf[i3], apvBits[i4], apbmi[i5], aiUsage[i6]);
240 }
241
242 // ok(GetLastError() == dwExpError, "Expected error %ld got %ld for (%ld,%ld,%ld,%ld,%ld,%ld) CreateDIBitmap(%p, %p, 0x%lx, %p, %p, %ld)\n",
243 // dwExpError, GetLastError(), i1, i2, i3, i4, i5, i6,
244 // ahdc[i1], apbih[i2], afInitf[i3], apvBits[i4], apbmi[i5], aiUsage[i6]);
245 }
246 }
247 }
248 }
249 }
250 }
251 }
252
253
254 }
255
256 void
257 Test_CreateDIBitmap_DIB_PAL_COLORS(void)
258 {
259 struct
260 {
261 BITMAPINFOHEADER bmiHeader;
262 WORD bmiColors[8];
263 } bmibuffer;
264 BITMAPINFO *pbmi = (PVOID)&bmibuffer;
265 HBITMAP hbmp;
266 ULONG bits[16] = {0};
267 HDC hdc;
268 HPALETTE hpalOld;
269 USHORT i;
270
271 hdc = CreateCompatibleDC(0);
272 ok(hdc != 0, "failed\n");
273
274 /* Select a palette */
275 hpalOld = SelectPalette(hdc, ghpal, FALSE);
276 ok(hpalOld != NULL, "error=%ld\n", GetLastError());
277
278 /* Initialize a BITMAPINFO */
279 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
280 pbmi->bmiHeader.biWidth = 2;
281 pbmi->bmiHeader.biHeight = -2;
282 pbmi->bmiHeader.biPlanes = 1;
283 pbmi->bmiHeader.biBitCount = 8;
284 pbmi->bmiHeader.biCompression = BI_RGB;
285 pbmi->bmiHeader.biSizeImage = 0;
286 pbmi->bmiHeader.biXPelsPerMeter = 1;
287 pbmi->bmiHeader.biYPelsPerMeter = 1;
288 pbmi->bmiHeader.biClrUsed = 8;
289 pbmi->bmiHeader.biClrImportant = 0;
290
291 for( i = 0; i < 8; i++ )
292 {
293 bmibuffer.bmiColors[i] = i;
294 }
295
296 /* Create the bitmap */
297 hbmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT, bits, pbmi, DIB_PAL_COLORS);
298 ok(hbmp != 0, "failed\n");
299
300 SelectObject(hdc, hbmp);
301
302
303 }
304
305 void
306 Test_CreateDIBitmap1(void)
307 {
308 BITMAPINFO bmi;
309 HBITMAP hbmp;
310 BITMAP bitmap;
311 ULONG bits[128] = {0};
312 BYTE rlebits[] = {2, 0, 0, 0, 2, 1, 0, 1};
313 HDC hdc;
314 int ret;
315
316 hdc = GetDC(0);
317
318 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
319 bmi.bmiHeader.biWidth = 2;
320 bmi.bmiHeader.biHeight = 2;
321 bmi.bmiHeader.biPlanes = 1;
322 bmi.bmiHeader.biBitCount = 16;
323 bmi.bmiHeader.biCompression = BI_RGB;
324 bmi.bmiHeader.biSizeImage = 0;
325 bmi.bmiHeader.biXPelsPerMeter = 1;
326 bmi.bmiHeader.biYPelsPerMeter = 1;
327 bmi.bmiHeader.biClrUsed = 0;
328 bmi.bmiHeader.biClrImportant = 0;
329
330 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, bits, &bmi, DIB_RGB_COLORS);
331 ok(hbmp != 0, "failed\n");
332
333 ret = GetObject(hbmp, sizeof(bitmap), &bitmap);
334 ok(ret != 0, "failed\n");
335 ok(bitmap.bmType == 0, "\n");
336 ok(bitmap.bmWidth == 2, "\n");
337 ok(bitmap.bmHeight == 2, "\n");
338 ok(bitmap.bmWidthBytes == 8, "bmWidthBytes = %ld\n", bitmap.bmWidthBytes);
339 ok(bitmap.bmPlanes == 1, "\n");
340 ok(bitmap.bmBitsPixel == GetDeviceCaps(hdc, BITSPIXEL), "\n");
341 ok(bitmap.bmBits == 0, "\n");
342
343 SetLastError(0);
344 bmi.bmiHeader.biCompression = BI_RLE8;
345 bmi.bmiHeader.biBitCount = 8;
346 bmi.bmiHeader.biSizeImage = 8;
347 bmi.bmiHeader.biClrUsed = 1;
348 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_INIT, rlebits, &bmi, DIB_PAL_COLORS);
349 ok(hbmp != 0, "failed\n");
350 ok(GetLastError() == 0, "GetLastError() == %ld\n", GetLastError());
351
352 ret = GetObject(hbmp, sizeof(bitmap), &bitmap);
353 ok(ret != 0, "failed\n");
354 ok(bitmap.bmType == 0, "\n");
355 ok(bitmap.bmWidth == 2, "\n");
356 ok(bitmap.bmHeight == 2, "\n");
357 ok(bitmap.bmWidthBytes == 8, "bmWidthBytes = %ld\n", bitmap.bmWidthBytes);
358 ok(bitmap.bmPlanes == 1, "\n");
359 ok(bitmap.bmBitsPixel == GetDeviceCaps(hdc, BITSPIXEL), "\n");
360 ok(bitmap.bmBits == 0, "\n");
361
362
363 }
364
365 void Test_CreateDIBitmap_RLE8()
366 {
367 struct
368 {
369 BITMAPINFOHEADER bmiHeader;
370 WORD wColors[4];
371 BYTE ajBuffer[20];
372 } PackedDIB =
373 {
374 {sizeof(BITMAPINFOHEADER), 4, 4, 1, 8, BI_RLE8, 20, 1, 1, 4, 0},
375 {0, 1, 2, 7},
376 {4,0, 0,2,0,1,0,2,3,1, 2,1, 2,2, 1,3,1,0,1,2, },
377 };
378 HDC hdc;
379 HBITMAP hbmp;
380
381 hdc = CreateCompatibleDC(0);
382
383 SetLastError(0xbadbad00);
384 hbmp = CreateDIBitmap(hdc, &PackedDIB.bmiHeader, CBM_INIT, &PackedDIB.ajBuffer, (PVOID)&PackedDIB, DIB_PAL_COLORS);
385 ok(hbmp != 0, "CreateDIBitmap failed.\n");
386 ok_err(0xbadbad00);
387 DeleteObject(hbmp);
388
389 PackedDIB.bmiHeader.biSizeImage = 2;
390 hbmp = CreateDIBitmap(hdc, &PackedDIB.bmiHeader, CBM_INIT, &PackedDIB.ajBuffer, (PVOID)&PackedDIB, DIB_PAL_COLORS);
391 ok(hbmp != 0, "CreateDIBitmap failed.\n");
392 ok_err(0xbadbad00);
393 DeleteObject(hbmp);
394
395 PackedDIB.bmiHeader.biSizeImage = 1;
396 hbmp = CreateDIBitmap(hdc, &PackedDIB.bmiHeader, CBM_INIT, &PackedDIB.ajBuffer, (PVOID)&PackedDIB, DIB_PAL_COLORS);
397 ok(hbmp != 0, "CreateDIBitmap failed.\n");
398 ok_err(0xbadbad00);
399 DeleteObject(hbmp);
400
401 PackedDIB.bmiHeader.biSizeImage = 0;
402 hbmp = CreateDIBitmap(hdc, &PackedDIB.bmiHeader, CBM_INIT, &PackedDIB.ajBuffer, (PVOID)&PackedDIB, DIB_PAL_COLORS);
403 ok(hbmp == 0, "CreateDIBitmap succeeded, expeted failure\n");
404 ok_err(0xbadbad00);
405
406 /* Test a line that is too long */
407 PackedDIB.bmiHeader.biSizeImage = 20;
408 PackedDIB.ajBuffer[0] = 17;
409 hbmp = CreateDIBitmap(hdc, &PackedDIB.bmiHeader, CBM_INIT, &PackedDIB.ajBuffer, (PVOID)&PackedDIB, DIB_PAL_COLORS);
410 ok(hbmp != 0, "CreateDIBitmap failed\n");
411 ok_err(0xbadbad00);
412 DeleteObject(hbmp);
413
414
415 }
416
417 void
418 Test_CreateDIBitmap_CBM_CREATDIB(void)
419 {
420 HBITMAP hbmp, hbmpOld;
421 HDC hdc;
422 BITMAPINFO bmi =
423 {{sizeof(BITMAPINFOHEADER), 4, 4, 1, 8, BI_RGB, 0, 1, 1, 1, 0}, {{0,0,0,0}}};
424 BYTE ajBits[10];
425 BITMAP bitmap;
426
427 hdc = CreateCompatibleDC(0);
428 ok(hdc != 0, "failed\n");
429
430 hbmp = CreateDIBitmap(hdc, &bmi.bmiHeader, CBM_CREATDIB, ajBits, &bmi, DIB_PAL_COLORS);
431 ok(hbmp != 0, "CreateDIBitmap failed.\n");
432
433 ok_long(GetObject(hbmp, sizeof(DIBSECTION), &bitmap), sizeof(BITMAP));
434 ok_int(bitmap.bmType, 0);
435 ok_int(bitmap.bmWidth, 4);
436 ok_int(bitmap.bmHeight, 4);
437 ok_int(bitmap.bmWidthBytes, 4);
438 ok_int(bitmap.bmPlanes, 1);
439 ok_int(bitmap.bmBitsPixel, 8);
440 ok_ptr(bitmap.bmBits, 0);
441
442 hbmpOld = SelectObject(hdc, hbmp);
443 ok(hbmpOld != NULL, "Couldn't select the bitmap.\n");
444 SelectObject(hdc, hbmpOld);
445 DeleteObject(hbmp);
446 DeleteDC(hdc);
447 }
448
449 START_TEST(CreateDIBitmap)
450 {
451 InitStuff();
452 Test_CreateDIBitmap_Params();
453 Test_CreateDIBitmap1();
454 Test_CreateDIBitmap_DIB_PAL_COLORS();
455 Test_CreateDIBitmap_RLE8();
456 Test_CreateDIBitmap_CBM_CREATDIB();
457 }
458