04b3ba6996223557c9ee7290caad42b846513afa
[reactos.git] / rostests / apitests / w32knapi / ntgdi / NtGdiCreateDIBSection.c
1
2 /*
3 HBITMAP
4 APIENTRY
5 NtGdiCreateDIBSection(
6 IN HDC hDC,
7 IN OPTIONAL HANDLE hSection,
8 IN DWORD dwOffset,
9 IN LPBITMAPINFO pbmi,
10 IN DWORD iUsage,
11 IN UINT cjHeader,
12 IN FLONG fl,
13 IN ULONG_PTR dwColorSpace,
14 OUT PVOID *ppvBits)
15 */
16
17 ULONG
18 GetBitmapSize(BITMAPINFOHEADER *pbih)
19 {
20 ULONG WidthBits, WidthBytes;
21
22 WidthBits = pbih->biWidth * pbih->biBitCount * pbih->biPlanes;
23 WidthBytes = ((WidthBits + 31) & ~ 31) >> 3;
24
25 return pbih->biHeight * WidthBytes;
26 }
27
28
29 INT
30 Test_NtGdiCreateDIBSection(PTESTINFO pti)
31 {
32 HBITMAP hbmp;
33 HDC hDC;
34 ULONG cjHeader;
35 PVOID pvBits = NULL;
36 ULONG cEntries;
37 DIBSECTION dibsection;
38
39 struct
40 {
41 BITMAPINFOHEADER bmiHeader;
42 RGBQUAD bmiColors[100];
43 } bmi;
44 PBITMAPINFO pbmi = (PBITMAPINFO)&bmi;
45 PBITMAPINFOHEADER pbih = (PBITMAPINFOHEADER)&bmi.bmiHeader;
46 PBITMAPV4HEADER pbV4h = (PBITMAPV4HEADER)&bmi.bmiHeader;
47 PBITMAPV5HEADER pbV5h = (PBITMAPV5HEADER)&bmi.bmiHeader;
48
49 HANDLE hSection;
50 NTSTATUS Status;
51 LARGE_INTEGER MaximumSize;
52
53 hDC = GetDC(0);
54 pbih->biSize = sizeof(BITMAPINFOHEADER);
55 pbih->biWidth = 2;
56 pbih->biHeight = 2;
57 pbih->biPlanes = 1;
58 pbih->biBitCount = 1;
59 pbih->biCompression = BI_RGB;
60 pbih->biSizeImage = 0;
61 pbih->biXPelsPerMeter = 100;
62 pbih->biYPelsPerMeter = 100;
63 pbih->biClrUsed = 2;
64 pbih->biClrImportant = 2;
65
66 cEntries = 0;
67
68 /** iUsage = 0 (DIB_RGB_COLORS) ***********************************************/
69
70 cjHeader = bmi.bmiHeader.biSize + cEntries * 4 + 8;
71
72 /* Test something simple */
73 SetLastError(0);
74 pvBits = 0;
75 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
76 TEST(pvBits != NULL);
77 TEST(hbmp != 0);
78 // TEST(GetLastError() == 0);
79 TEST(GetObject(hbmp, sizeof(DIBSECTION), &dibsection) == sizeof(DIBSECTION));
80 TEST(dibsection.dsBitfields[0] == 0);
81 TEST(dibsection.dsBitfields[1] == 0);
82 TEST(dibsection.dsBitfields[2] == 0);
83 TEST(dibsection.dshSection == 0);
84 TEST(dibsection.dsOffset == 0);
85 if (hbmp) DeleteObject(hbmp);
86
87
88 /* Test a 0 HDC */
89 SetLastError(0);
90 pvBits = 0;
91 hbmp = NtGdiCreateDIBSection(0, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
92 TEST(pvBits != NULL);
93 TEST(hbmp != 0);
94 TEST(GetLastError() == ERROR_NOT_ENOUGH_MEMORY);
95 if (hbmp) DeleteObject(hbmp);
96
97 /* Test a wrong HDC */
98 SetLastError(0);
99 pvBits = 0;
100 hbmp = NtGdiCreateDIBSection((HDC)0xdeadbeef, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
101 TEST(pvBits != 0);
102 TEST(hbmp != 0);
103 TEST(GetLastError() == 8);
104 if (hbmp) DeleteObject(hbmp);
105
106 /* Test pbmi = NULL */
107 SetLastError(0);
108 pvBits = (PVOID)-1;
109 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, NULL, 0, cjHeader, 0, 0, &pvBits);
110 TEST(pvBits == (PVOID)-1);
111 TEST(hbmp == 0);
112 TEST(GetLastError() == 0);
113 if (hbmp) DeleteObject(hbmp);
114
115 /* Test invalid pbmi */
116 SetLastError(0);
117 pvBits = (PVOID)-1;
118 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, (PVOID)0x80001234, 0, cjHeader, 0, 0, &pvBits);
119 TEST(pvBits == (PVOID)-1);
120 TEST(hbmp == 0);
121 TEST(GetLastError() == ERROR_INVALID_PARAMETER);
122 if (hbmp) DeleteObject(hbmp);
123
124 /* Test invalid pbmi */
125 SetLastError(0);
126 pvBits = (PVOID)-1;
127 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, (PVOID)1, 0, cjHeader, 0, 0, &pvBits);
128 TEST(pvBits == (PVOID)-1);
129 TEST(hbmp == 0);
130 TEST(GetLastError() == ERROR_INVALID_PARAMETER);
131 if (hbmp) DeleteObject(hbmp);
132
133 /* Test ppvBits = NULL */
134 SetLastError(0);
135 hbmp = NtGdiCreateDIBSection(0, NULL, 0, pbmi, 0, cjHeader, 0, 0, NULL);
136 TEST(hbmp == 0);
137 TEST(GetLastError() == ERROR_INVALID_PARAMETER);
138 if (hbmp) DeleteObject(hbmp);
139
140 /* Test ppvBits = NULL and pbmi == 0*/
141 SetLastError(0);
142 hbmp = NtGdiCreateDIBSection(0, NULL, 0, NULL, 0, cjHeader, 0, 0, NULL);
143 TEST(hbmp == 0);
144 TEST(GetLastError() == 0);
145 if (hbmp) DeleteObject(hbmp);
146
147 /* Test ppvBits = NULL and wrong cjHeader */
148 SetLastError(0);
149 hbmp = NtGdiCreateDIBSection(0, NULL, 0, pbmi, 0, cjHeader+4, 0, 0, NULL);
150 TEST(hbmp == 0);
151 TEST(GetLastError() == 0);
152 if (hbmp) DeleteObject(hbmp);
153
154 /* Test ppvBits = NULL and cjHeader = 0 */
155 SetLastError(0);
156 hbmp = NtGdiCreateDIBSection(0, NULL, 0, pbmi, 0, 0, 0, 0, NULL);
157 TEST(hbmp == 0);
158 TEST(GetLastError() == 0);
159 if (hbmp) DeleteObject(hbmp);
160
161 /* Test wrong cjHeader */
162 SetLastError(0);
163 pvBits = (PVOID)-1;
164 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader+4, 0, 0, &pvBits);
165 pvBits = (PVOID)-1;
166 TEST(hbmp == 0);
167 TEST(GetLastError() == 0);
168 if (hbmp) DeleteObject(hbmp);
169
170 /* Test different bitcount */
171 pbih->biBitCount = 4;
172 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
173 TEST(hbmp != 0);
174 if (hbmp) DeleteObject(hbmp);
175
176 pbih->biBitCount = 8;
177 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
178 TEST(hbmp != 0);
179 if (hbmp) DeleteObject(hbmp);
180
181 cjHeader = pbih->biSize;
182 pbih->biBitCount = 16;
183 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
184 TEST(hbmp != 0);
185 if (hbmp) DeleteObject(hbmp);
186
187 pbih->biBitCount = 24;
188 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
189 TEST(hbmp != 0);
190 if (hbmp) DeleteObject(hbmp);
191
192 pbih->biBitCount = 32;
193 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
194 TEST(hbmp != 0);
195 if (hbmp) DeleteObject(hbmp);
196
197 /* Test BI_BITFIELDS */
198 cEntries = 3;
199 cjHeader = pbih->biSize + cEntries * sizeof(DWORD);
200 pbih->biBitCount = 16;
201 pbih->biCompression = BI_BITFIELDS;
202 ((DWORD*)pbmi->bmiColors)[0] = 0x0007;
203 ((DWORD*)pbmi->bmiColors)[1] = 0x0038;
204 ((DWORD*)pbmi->bmiColors)[2] = 0x01C0;
205 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
206 TEST(hbmp != 0);
207 TEST(GetObject(hbmp, sizeof(DIBSECTION), &dibsection) == sizeof(DIBSECTION));
208 TEST(dibsection.dsBm.bmType == 0);
209 TEST(dibsection.dsBm.bmWidth == 2);
210 TEST(dibsection.dsBm.bmHeight == 2);
211 TEST(dibsection.dsBm.bmWidthBytes == 4);
212 TEST(dibsection.dsBm.bmPlanes == 1);
213 TEST(dibsection.dsBm.bmBitsPixel == 16);
214 TEST(dibsection.dsBm.bmBits == pvBits);
215 TEST(dibsection.dsBmih.biSize == sizeof(BITMAPINFOHEADER));
216 TEST(dibsection.dsBmih.biWidth == 2);
217 TEST(dibsection.dsBmih.biHeight == 2);
218 TEST(dibsection.dsBmih.biPlanes == 1);
219 TEST(dibsection.dsBmih.biBitCount == 16);
220 TEST(dibsection.dsBmih.biCompression == BI_BITFIELDS);
221 TEST(dibsection.dsBmih.biSizeImage == 8);
222 TEST(dibsection.dsBmih.biXPelsPerMeter == 0);
223 TEST(dibsection.dsBmih.biYPelsPerMeter == 0);
224 TEST(dibsection.dsBmih.biClrUsed == 0);
225 TEST(dibsection.dsBmih.biClrImportant == 0);
226 TEST(dibsection.dsBitfields[0] == 0x0007);
227 TEST(dibsection.dsBitfields[1] == 0x0038);
228 TEST(dibsection.dsBitfields[2] == 0x01C0);
229 TEST(dibsection.dshSection == 0);
230 TEST(dibsection.dsOffset == 0);
231
232 printf("dib with bitfileds: %p\n", hbmp);
233 //system("PAUSE");
234
235 if (hbmp) DeleteObject(hbmp);
236
237
238 /* Test BI_BITFIELDS */
239 SetLastError(0);
240 pvBits = 0;
241
242 pbih->biSize = sizeof(BITMAPINFOHEADER);
243 pbih->biWidth = 2;
244 pbih->biHeight = 2;
245 pbih->biPlanes = 1;
246 pbih->biBitCount = 4;
247 pbih->biCompression = BI_RGB;
248 pbih->biSizeImage = 0;
249 pbih->biXPelsPerMeter = 100;
250 pbih->biYPelsPerMeter = 100;
251 pbih->biClrUsed = 0;
252 pbih->biClrImportant = 0;
253 ((DWORD*)pbmi->bmiColors)[0] = 0xF800;
254 ((DWORD*)pbmi->bmiColors)[1] = 0x00ff00;
255 ((DWORD*)pbmi->bmiColors)[2] = 0x0000ff;
256 cEntries = 0;
257 cjHeader = bmi.bmiHeader.biSize + cEntries * 4 + 20;
258
259
260 /** iUsage = 1 (DIB_PAL_COLORS) ***********************************************/
261
262 pbmi->bmiHeader.biClrUsed = 2;
263 pbmi->bmiHeader.biClrImportant = 2;
264
265 cEntries = 2;
266 cjHeader = bmi.bmiHeader.biSize + cEntries * 4 + 8;
267
268 /* Test iUsage = 1 */
269 SetLastError(0);
270 pvBits = (PVOID)-1;
271 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 1, cjHeader, 0, 0, &pvBits);
272 TEST(pvBits == (PVOID)-1);
273 TEST(hbmp == 0);
274 TEST(GetLastError() == 0);
275 if (hbmp) DeleteObject(hbmp);
276
277
278
279 /** iUsage = 2 (???) **********************************************************/
280
281 /* Test iUsage = 2 */
282 SetLastError(0);
283 pvBits = (PVOID)-1;
284 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 2, cjHeader, 0, 0, &pvBits);
285 TEST(pvBits == (PVOID)-1);
286 TEST(hbmp == 0);
287 TEST(GetLastError() == 0);
288 if (hbmp) DeleteObject(hbmp);
289
290
291 /** wrong iUsage **************************************************************/
292
293 cEntries = 0;
294 cjHeader = bmi.bmiHeader.biSize + cEntries * 4 + 8;
295
296 /* Test iUsage = 3 */
297 SetLastError(0);
298 pvBits = (PVOID)-1;
299 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 3, cjHeader, 0, 0, &pvBits);
300 TEST(pvBits == (PVOID)-1);
301 TEST(hbmp == 0);
302 TEST(GetLastError() == ERROR_INVALID_PARAMETER);
303 if (hbmp) DeleteObject(hbmp);
304
305 /* Test iUsage = 3 */
306 SetLastError(0);
307 pvBits = (PVOID)-1;
308 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 3, cjHeader+4, 0, 0, &pvBits);
309 TEST(pvBits == (PVOID)-1);
310 TEST(hbmp == 0);
311 TEST(GetLastError() == 0);
312 if (hbmp) DeleteObject(hbmp);
313
314 /* Test wrong iUsage */
315 SetLastError(0);
316 pvBits = (PVOID)-1;
317 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, -55, cjHeader, 0, 0, &pvBits);
318 TEST(pvBits == (PVOID)-1);
319 TEST(hbmp == 0);
320 TEST(GetLastError() == ERROR_INVALID_PARAMETER);
321 if (hbmp) DeleteObject(hbmp);
322
323 /* Test wrong iUsage and wrong cjHeader */
324 SetLastError(0);
325 pvBits = (PVOID)-1;
326 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, -55, cjHeader+4, 0, 0, &pvBits);
327 TEST(pvBits == (PVOID)-1);
328 TEST(hbmp == 0);
329 TEST(GetLastError() == 0);
330 if (hbmp) DeleteObject(hbmp);
331
332 /* increased header size */
333 pbih->biSize = sizeof(BITMAPINFOHEADER) + 4;
334 cjHeader = pbih->biSize + cEntries * 4 + 8;
335 SetLastError(0);
336 pvBits = 0;
337 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
338 TEST(pvBits != NULL);
339 TEST(hbmp != 0);
340 TEST(GetLastError() == 8);
341 if (hbmp) DeleteObject(hbmp);
342
343 /* increased header size */
344 pbih->biSize = sizeof(BITMAPINFOHEADER) + 2;
345 cjHeader = pbih->biSize + cEntries * 4 + 8;
346 SetLastError(0);
347 pvBits = (PVOID)-1;
348 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
349 TEST(pvBits == (PVOID)-1);
350 TEST(hbmp == 0);
351 TEST(GetLastError() == 0);
352 if (hbmp) DeleteObject(hbmp);
353
354 /* decreased header size */
355 pbih->biSize = sizeof(BITMAPINFOHEADER) - 4;
356 cjHeader = pbih->biSize + cEntries * 4 + 8;
357 SetLastError(0);
358 pvBits = (PVOID)-1;
359 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
360 TEST(pvBits == (PVOID)-1);
361 TEST(hbmp == 0);
362 TEST(GetLastError() == 0);
363 if (hbmp) DeleteObject(hbmp);
364
365
366 /** BITMAPV4HEADER ************************************************************/
367
368 pbV4h->bV4Size = sizeof(BITMAPV4HEADER);
369 pbV4h->bV4Width = 2;
370 pbV4h->bV4Height = 3;
371 pbV4h->bV4Planes = 1;
372 pbV4h->bV4BitCount = 1;
373 pbV4h->bV4V4Compression = BI_RGB;
374 pbV4h->bV4SizeImage = 0;
375 pbV4h->bV4XPelsPerMeter = 100;
376 pbV4h->bV4YPelsPerMeter = 100;
377 pbV4h->bV4ClrUsed = 0;
378 pbV4h->bV4ClrImportant = 0;
379 pbV4h->bV4RedMask = 0;
380 pbV4h->bV4GreenMask = 0;
381 pbV4h->bV4BlueMask = 0;
382 pbV4h->bV4AlphaMask = 0;
383 pbV4h->bV4CSType = 0;
384 memset(&pbV4h->bV4Endpoints, sizeof(CIEXYZTRIPLE), 0);
385 pbV4h->bV4GammaRed = 0;
386 pbV4h->bV4GammaGreen = 0;
387 pbV4h->bV4GammaBlue = 0;
388
389 cEntries = 0;
390 cjHeader = bmi.bmiHeader.biSize + cEntries * 4 + 8;
391
392 /* Test something simple */
393 SetLastError(0);
394 pvBits = 0;
395 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
396 TEST(pvBits != NULL);
397 TEST(hbmp != 0);
398 TEST(GetLastError() == 8);
399 if (hbmp) DeleteObject(hbmp);
400
401
402 /** BITMAPV5HEADER ************************************************************/
403
404 pbV5h->bV5Size = sizeof(BITMAPV5HEADER);
405 pbV5h->bV5Width = 2;
406 pbV5h->bV5Height = 3;
407 pbV5h->bV5Planes = 1;
408 pbV5h->bV5BitCount = 1;
409 pbV5h->bV5Compression = BI_RGB;
410 pbV5h->bV5SizeImage = 0;
411 pbV5h->bV5XPelsPerMeter = 100;
412 pbV5h->bV5YPelsPerMeter = 100;
413 pbV5h->bV5ClrUsed = 2;
414 pbV5h->bV5ClrImportant = 2;
415 pbV5h->bV5RedMask = 0;
416 pbV5h->bV5GreenMask = 0;
417 pbV5h->bV5BlueMask = 0;
418 pbV5h->bV5AlphaMask = 0;
419 pbV5h->bV5CSType = 0;
420 memset(&pbV5h->bV5Endpoints, 0, sizeof(CIEXYZTRIPLE));
421 pbV5h->bV5GammaRed = 0;
422 pbV5h->bV5GammaGreen = 0;
423 pbV5h->bV5GammaBlue = 0;
424 pbV5h->bV5Intent = 0;
425 pbV5h->bV5ProfileData = 0;
426 pbV5h->bV5ProfileSize = 0;
427 pbV5h->bV5Reserved = 0;
428
429 cEntries = 0;
430 cjHeader = pbV5h->bV5Size + cEntries * 4 + 8;
431
432 /* Test something simple */
433 SetLastError(0);
434 pvBits = 0;
435 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
436 TEST(pvBits != NULL);
437 TEST(hbmp != 0);
438 TEST(GetLastError() == 8);
439 if (hbmp) DeleteObject(hbmp);
440
441 /* increased header size */
442 pbV5h->bV5Size = sizeof(BITMAPV5HEADER) + 64;
443 cjHeader = pbV5h->bV5Size + cEntries * 4 + 8;
444 SetLastError(0);
445 pvBits = 0;
446 hbmp = NtGdiCreateDIBSection(hDC, NULL, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
447 TEST(pvBits != NULL);
448 TEST(hbmp != 0);
449 TEST(GetLastError() == 8);
450 if (hbmp) DeleteObject(hbmp);
451
452 /* Test section */
453 MaximumSize.QuadPart = 4096;
454 Status = ZwCreateSection(&hSection,
455 SECTION_ALL_ACCESS,
456 NULL,
457 &MaximumSize,
458 PAGE_READWRITE,
459 SEC_COMMIT,
460 NULL);
461 ASSERT(NT_SUCCESS(Status));
462
463 SetLastError(0);
464 pvBits = 0;
465 hbmp = NtGdiCreateDIBSection(hDC, hSection, 0, pbmi, 0, cjHeader, 0, 0, &pvBits);
466 TEST(pvBits != NULL);
467 TEST(hbmp != 0);
468 // TEST(GetLastError() == 0);
469 printf("hbmp = %p, pvBits = %p, hSection = %p\n", hbmp, pvBits, hSection);
470 //system("PAUSE");
471 if (hbmp) DeleteObject(hbmp);
472
473
474 return APISTATUS_NORMAL;
475 }