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