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