1 /*
2 * Unit test suite for bitmaps
3 *
4 * Copyright 2004 Huw Davies
5 * Copyright 2006 Dmitry Timoshkov
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <stdarg.h>
23 #include <assert.h>
24 #include <string.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "mmsystem.h"
32
33 #include "wine/test.h"
34
35 static BOOL (WINAPI *pGdiAlphaBlend)(HDC,int,int,int,int,HDC,int,int,int,int,BLENDFUNCTION);
36
37 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
38
39 static BOOL is_win9x;
40
41 static INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
42 {
43 switch(bpp)
44 {
45 case 1:
46 return 2 * ((bmWidth+15) >> 4);
47
48 case 24:
49 bmWidth *= 3; /* fall through */
50 case 8:
51 return bmWidth + (bmWidth & 1);
52
53 case 32:
54 return bmWidth * 4;
55
56 case 16:
57 case 15:
58 return bmWidth * 2;
59
60 case 4:
61 return 2 * ((bmWidth+3) >> 2);
62
63 default:
64 trace("Unknown depth %d, please report.\n", bpp );
65 assert(0);
66 }
67 return -1;
68 }
69
70 static void test_bitmap_info(HBITMAP hbm, INT expected_depth, const BITMAPINFOHEADER *bmih)
71 {
72 BITMAP bm;
73 BITMAP bma[2];
74 INT ret, width_bytes;
75 char buf[512], buf_cmp[512];
76 DWORD gle;
77
78 ret = GetObject(hbm, sizeof(bm), &bm);
79 ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
80
81 ok(bm.bmType == 0 || broken(bm.bmType == 21072 /* Win9x */), "wrong bm.bmType %d\n", bm.bmType);
82 ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
83 ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
84 width_bytes = BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
85 ok(bm.bmWidthBytes == width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, width_bytes);
86 ok(bm.bmPlanes == bmih->biPlanes, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
87 ok(bm.bmBitsPixel == expected_depth, "wrong bm.bmBitsPixel %d != %d\n", bm.bmBitsPixel, expected_depth);
88 ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
89
90 assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
91 assert(sizeof(buf) == sizeof(buf_cmp));
92
93 SetLastError(0xdeadbeef);
94 ret = GetBitmapBits(hbm, 0, NULL);
95 gle=GetLastError();
96 ok(ret == bm.bmWidthBytes * bm.bmHeight || (ret == 0 && gle == ERROR_INVALID_PARAMETER /* Win9x */), "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
97
98 memset(buf_cmp, 0xAA, sizeof(buf_cmp));
99 memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
100
101 memset(buf, 0xAA, sizeof(buf));
102 ret = GetBitmapBits(hbm, sizeof(buf), buf);
103 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
104 ok(!memcmp(buf, buf_cmp, sizeof(buf)), "buffers do not match\n");
105
106 /* test various buffer sizes for GetObject */
107 ret = GetObject(hbm, sizeof(*bma) * 2, bma);
108 ok(ret == sizeof(*bma) || broken(ret == sizeof(*bma) * 2 /* Win9x */), "wrong size %d\n", ret);
109
110 ret = GetObject(hbm, sizeof(bm) / 2, &bm);
111 ok(ret == 0 || broken(ret == sizeof(bm) / 2 /* Win9x */), "%d != 0\n", ret);
112
113 ret = GetObject(hbm, 0, &bm);
114 ok(ret == 0, "%d != 0\n", ret);
115
116 ret = GetObject(hbm, 1, &bm);
117 ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
118
119 /* Don't trust Win9x not to try to write to NULL */
120 if (ret == 0)
121 {
122 ret = GetObject(hbm, 0, NULL);
123 ok(ret == sizeof(bm), "wrong size %d\n", ret);
124 }
125 }
126
127 static void test_createdibitmap(void)
128 {
129 HDC hdc, hdcmem;
130 BITMAPINFOHEADER bmih;
131 BITMAPINFO bm;
132 HBITMAP hbm, hbm_colour, hbm_old;
133 INT screen_depth;
134 DWORD pixel;
135
136 hdc = GetDC(0);
137 screen_depth = GetDeviceCaps(hdc, BITSPIXEL);
138 memset(&bmih, 0, sizeof(bmih));
139 bmih.biSize = sizeof(bmih);
140 bmih.biWidth = 10;
141 bmih.biHeight = 10;
142 bmih.biPlanes = 1;
143 bmih.biBitCount = 32;
144 bmih.biCompression = BI_RGB;
145
146 /* First create an un-initialised bitmap. The depth of the bitmap
147 should match that of the hdc and not that supplied in bmih.
148 */
149
150 /* First try 32 bits */
151 hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
152 ok(hbm != NULL, "CreateDIBitmap failed\n");
153 test_bitmap_info(hbm, screen_depth, &bmih);
154 DeleteObject(hbm);
155
156 /* Then 16 */
157 bmih.biBitCount = 16;
158 hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
159 ok(hbm != NULL, "CreateDIBitmap failed\n");
160 test_bitmap_info(hbm, screen_depth, &bmih);
161 DeleteObject(hbm);
162
163 /* Then 1 */
164 bmih.biBitCount = 1;
165 hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
166 ok(hbm != NULL, "CreateDIBitmap failed\n");
167 test_bitmap_info(hbm, screen_depth, &bmih);
168 DeleteObject(hbm);
169
170 /* Now with a monochrome dc we expect a monochrome bitmap */
171 hdcmem = CreateCompatibleDC(hdc);
172
173 /* First try 32 bits */
174 bmih.biBitCount = 32;
175 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
176 ok(hbm != NULL, "CreateDIBitmap failed\n");
177 test_bitmap_info(hbm, 1, &bmih);
178 DeleteObject(hbm);
179
180 /* Then 16 */
181 bmih.biBitCount = 16;
182 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
183 ok(hbm != NULL, "CreateDIBitmap failed\n");
184 test_bitmap_info(hbm, 1, &bmih);
185 DeleteObject(hbm);
186
187 /* Then 1 */
188 bmih.biBitCount = 1;
189 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
190 ok(hbm != NULL, "CreateDIBitmap failed\n");
191 test_bitmap_info(hbm, 1, &bmih);
192 DeleteObject(hbm);
193
194 /* Now select a polychrome bitmap into the dc and we expect
195 screen_depth bitmaps again */
196 hbm_colour = CreateCompatibleBitmap(hdc, bmih.biWidth, bmih.biHeight);
197 test_bitmap_info(hbm_colour, screen_depth, &bmih);
198 hbm_old = SelectObject(hdcmem, hbm_colour);
199
200 /* First try 32 bits */
201 bmih.biBitCount = 32;
202 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
203 ok(hbm != NULL, "CreateDIBitmap failed\n");
204 test_bitmap_info(hbm, screen_depth, &bmih);
205 DeleteObject(hbm);
206
207 /* Then 16 */
208 bmih.biBitCount = 16;
209 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
210 ok(hbm != NULL, "CreateDIBitmap failed\n");
211 test_bitmap_info(hbm, screen_depth, &bmih);
212 DeleteObject(hbm);
213
214 /* Then 1 */
215 bmih.biBitCount = 1;
216 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
217 ok(hbm != NULL, "CreateDIBitmap failed\n");
218 test_bitmap_info(hbm, screen_depth, &bmih);
219 DeleteObject(hbm);
220
221 SelectObject(hdcmem, hbm_old);
222 DeleteObject(hbm_colour);
223 DeleteDC(hdcmem);
224
225 /* If hdc == 0 then we get a 1 bpp bitmap */
226 if (!is_win9x) {
227 bmih.biBitCount = 32;
228 hbm = CreateDIBitmap(0, &bmih, 0, NULL, NULL, 0);
229 ok(hbm != NULL, "CreateDIBitmap failed\n");
230 test_bitmap_info(hbm, 1, &bmih);
231 DeleteObject(hbm);
232 }
233
234 /* Test how formats are converted */
235 pixel = 0xffffffff;
236 bmih.biBitCount = 1;
237 bmih.biWidth = 1;
238 bmih.biHeight = 1;
239
240 memset(&bm, 0, sizeof(bm));
241 bm.bmiHeader.biSize = sizeof(bm.bmiHeader);
242 bm.bmiHeader.biWidth = 1;
243 bm.bmiHeader.biHeight = 1;
244 bm.bmiHeader.biPlanes = 1;
245 bm.bmiHeader.biBitCount= 24;
246 bm.bmiHeader.biCompression= BI_RGB;
247 bm.bmiHeader.biSizeImage = 0;
248 hbm = CreateDIBitmap(hdc, &bmih, CBM_INIT, &pixel, &bm, DIB_RGB_COLORS);
249 ok(hbm != NULL, "CreateDIBitmap failed\n");
250
251 pixel = 0xdeadbeef;
252 bm.bmiHeader.biBitCount= 32;
253 GetDIBits(hdc, hbm, 0, 1, &pixel, &bm, DIB_RGB_COLORS);
254 ok(pixel == 0x00ffffff, "Reading a 32 bit pixel from a DDB returned %08x\n", pixel);
255 DeleteObject(hbm);
256
257 ReleaseDC(0, hdc);
258 }
259
260 static INT DIB_GetWidthBytes( int width, int bpp )
261 {
262 int words;
263
264 switch (bpp)
265 {
266 case 1: words = (width + 31) / 32; break;
267 case 4: words = (width + 7) / 8; break;
268 case 8: words = (width + 3) / 4; break;
269 case 15:
270 case 16: words = (width + 1) / 2; break;
271 case 24: words = (width * 3 + 3)/4; break;
272 case 32: words = width; break;
273
274 default:
275 words=0;
276 trace("Unknown depth %d, please report.\n", bpp );
277 assert(0);
278 break;
279 }
280 return 4 * words;
281 }
282
283 static void test_dib_info(HBITMAP hbm, const void *bits, const BITMAPINFOHEADER *bmih)
284 {
285 BITMAP bm;
286 BITMAP bma[2];
287 DIBSECTION ds;
288 DIBSECTION dsa[2];
289 INT ret, bm_width_bytes, dib_width_bytes;
290 BYTE *buf;
291
292 ret = GetObject(hbm, sizeof(bm), &bm);
293 ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
294
295 ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
296 ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
297 ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
298 dib_width_bytes = DIB_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
299 bm_width_bytes = BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
300 if (bm.bmWidthBytes != dib_width_bytes) /* Win2k bug */
301 ok(bm.bmWidthBytes == bm_width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, bm_width_bytes);
302 else
303 ok(bm.bmWidthBytes == dib_width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, dib_width_bytes);
304 ok(bm.bmPlanes == bmih->biPlanes, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
305 ok(bm.bmBitsPixel == bmih->biBitCount, "bm.bmBitsPixel %d != %d\n", bm.bmBitsPixel, bmih->biBitCount);
306 ok(bm.bmBits == bits, "wrong bm.bmBits %p != %p\n", bm.bmBits, bits);
307
308 buf = HeapAlloc(GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight + 4096);
309
310 /* GetBitmapBits returns not 32-bit aligned data */
311 ret = GetBitmapBits(hbm, 0, NULL);
312 ok(ret == bm_width_bytes * bm.bmHeight, "%d != %d\n", ret, bm_width_bytes * bm.bmHeight);
313
314 memset(buf, 0xAA, bm.bmWidthBytes * bm.bmHeight + 4096);
315 ret = GetBitmapBits(hbm, bm.bmWidthBytes * bm.bmHeight + 4096, buf);
316 ok(ret == bm_width_bytes * bm.bmHeight, "%d != %d\n", ret, bm_width_bytes * bm.bmHeight);
317
318 HeapFree(GetProcessHeap(), 0, buf);
319
320 /* test various buffer sizes for GetObject */
321 memset(&ds, 0xAA, sizeof(ds));
322 ret = GetObject(hbm, sizeof(*bma) * 2, bma);
323 ok(ret == sizeof(*bma) || broken(ret == sizeof(*bma) * 2 /* Win9x */), "wrong size %d\n", ret);
324 ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
325 ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
326 ok(bm.bmBits == bits, "wrong bm.bmBits %p != %p\n", bm.bmBits, bits);
327
328 ret = GetObject(hbm, sizeof(bm) / 2, &bm);
329 ok(ret == 0 || broken(ret == sizeof(bm) / 2 /* Win9x */), "%d != 0\n", ret);
330
331 ret = GetObject(hbm, 0, &bm);
332 ok(ret == 0, "%d != 0\n", ret);
333
334 ret = GetObject(hbm, 1, &bm);
335 ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
336
337 /* test various buffer sizes for GetObject */
338 ret = GetObject(hbm, 0, NULL);
339 ok(ret == sizeof(bm), "wrong size %d\n", ret);
340
341 ret = GetObject(hbm, sizeof(*dsa) * 2, dsa);
342 ok(ret == sizeof(*dsa) || broken(ret == sizeof(*dsa) * 2 /* Win9x */), "wrong size %d\n", ret);
343
344 memset(&ds, 0xAA, sizeof(ds));
345 ret = GetObject(hbm, sizeof(ds), &ds);
346 ok(ret == sizeof(ds), "wrong size %d\n", ret);
347
348 ok(ds.dsBm.bmBits == bits, "wrong bm.bmBits %p != %p\n", ds.dsBm.bmBits, bits);
349 if (ds.dsBm.bmWidthBytes != bm_width_bytes) /* Win2k bug */
350 ok(ds.dsBmih.biSizeImage == ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight, "%u != %u\n",
351 ds.dsBmih.biSizeImage, ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight);
352 ok(bmih->biSizeImage == 0, "%u != 0\n", bmih->biSizeImage);
353 ds.dsBmih.biSizeImage = 0;
354
355 ok(ds.dsBmih.biSize == bmih->biSize, "%u != %u\n", ds.dsBmih.biSize, bmih->biSize);
356 ok(ds.dsBmih.biWidth == bmih->biWidth, "%u != %u\n", ds.dsBmih.biWidth, bmih->biWidth);
357 ok(ds.dsBmih.biHeight == bmih->biHeight, "%u != %u\n", ds.dsBmih.biHeight, bmih->biHeight);
358 ok(ds.dsBmih.biPlanes == bmih->biPlanes, "%u != %u\n", ds.dsBmih.biPlanes, bmih->biPlanes);
359 ok(ds.dsBmih.biBitCount == bmih->biBitCount, "%u != %u\n", ds.dsBmih.biBitCount, bmih->biBitCount);
360 ok(ds.dsBmih.biCompression == bmih->biCompression, "%u != %u\n", ds.dsBmih.biCompression, bmih->biCompression);
361 ok(ds.dsBmih.biSizeImage == bmih->biSizeImage, "%u != %u\n", ds.dsBmih.biSizeImage, bmih->biSizeImage);
362 ok(ds.dsBmih.biXPelsPerMeter == bmih->biXPelsPerMeter, "%u != %u\n", ds.dsBmih.biXPelsPerMeter, bmih->biXPelsPerMeter);
363 ok(ds.dsBmih.biYPelsPerMeter == bmih->biYPelsPerMeter, "%u != %u\n", ds.dsBmih.biYPelsPerMeter, bmih->biYPelsPerMeter);
364
365 memset(&ds, 0xAA, sizeof(ds));
366 ret = GetObject(hbm, sizeof(ds) - 4, &ds);
367 ok(ret == sizeof(ds.dsBm), "wrong size %d\n", ret);
368 ok(ds.dsBm.bmWidth == bmih->biWidth, "%u != %u\n", ds.dsBmih.biWidth, bmih->biWidth);
369 ok(ds.dsBm.bmHeight == bmih->biHeight, "%u != %u\n", ds.dsBmih.biHeight, bmih->biHeight);
370 ok(ds.dsBm.bmBits == bits, "%p != %p\n", ds.dsBm.bmBits, bits);
371
372 ret = GetObject(hbm, 0, &ds);
373 ok(ret == 0, "%d != 0\n", ret);
374
375 ret = GetObject(hbm, 1, &ds);
376 ok(ret == 0, "%d != 0\n", ret);
377 }
378
379 #define test_color_todo(got, exp, txt, todo) \
380 if (!todo && got != exp && screen_depth < 24) { \
381 todo_wine ok(0, #txt " failed at %d-bit screen depth: got 0x%06x expected 0x%06x - skipping DIB tests\n", \
382 screen_depth, (UINT)got, (UINT)exp); \
383 return; \
384 } else if (todo) todo_wine { ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp); } \
385 else ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp) \
386
387 #define test_color(hdc, color, exp, todo_setp, todo_getp) \
388 { \
389 COLORREF c; \
390 c = SetPixel(hdc, 0, 0, color); \
391 if (!is_win9x) { test_color_todo(c, exp, SetPixel, todo_setp); } \
392 c = GetPixel(hdc, 0, 0); \
393 test_color_todo(c, exp, GetPixel, todo_getp); \
394 }
395
396 static void test_dibsections(void)
397 {
398 HDC hdc, hdcmem, hdcmem2;
399 HBITMAP hdib, oldbm, hdib2, oldbm2;
400 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
401 char bcibuf[sizeof(BITMAPCOREINFO) + 256 * sizeof(RGBTRIPLE)];
402 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
403 BITMAPCOREINFO *pbci = (BITMAPCOREINFO *)bcibuf;
404 HBITMAP hcoredib;
405 char coreBits[256];
406 BYTE *bits;
407 RGBQUAD rgb[256];
408 int ret;
409 char logpalbuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
410 LOGPALETTE *plogpal = (LOGPALETTE*)logpalbuf;
411 WORD *index;
412 DWORD *bits32;
413 HPALETTE hpal, oldpal;
414 DIBSECTION dibsec;
415 COLORREF c0, c1;
416 int i;
417 int screen_depth;
418 MEMORY_BASIC_INFORMATION info;
419
420 hdc = GetDC(0);
421 screen_depth = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
422
423 memset(pbmi, 0, sizeof(bmibuf));
424 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
425 pbmi->bmiHeader.biHeight = 100;
426 pbmi->bmiHeader.biWidth = 512;
427 pbmi->bmiHeader.biBitCount = 24;
428 pbmi->bmiHeader.biPlanes = 1;
429 pbmi->bmiHeader.biCompression = BI_RGB;
430
431 SetLastError(0xdeadbeef);
432
433 /* invalid pointer for BITMAPINFO
434 (*bits should be NULL on error) */
435 bits = (BYTE*)0xdeadbeef;
436 hdib = CreateDIBSection(hdc, NULL, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
437 ok(hdib == NULL && bits == NULL, "CreateDIBSection failed for invalid parameter: bmi == 0x0\n");
438
439 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
440 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
441 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
442 ok(dibsec.dsBm.bmBits == bits, "dibsec.dsBits %p != bits %p\n", dibsec.dsBm.bmBits, bits);
443
444 /* test the DIB memory */
445 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
446 "VirtualQuery failed\n");
447 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
448 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
449 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
450 ok(info.RegionSize == 0x26000, "0x%lx != 0x26000\n", info.RegionSize);
451 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
452 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
453 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
454
455 test_dib_info(hdib, bits, &pbmi->bmiHeader);
456 DeleteObject(hdib);
457
458 pbmi->bmiHeader.biBitCount = 8;
459 pbmi->bmiHeader.biCompression = BI_RLE8;
460 SetLastError(0xdeadbeef);
461 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
462 ok(hdib == NULL, "CreateDIBSection should fail when asked to create a compressed DIB section\n");
463 ok(GetLastError() == 0xdeadbeef, "wrong error %d\n", GetLastError());
464
465 pbmi->bmiHeader.biBitCount = 16;
466 pbmi->bmiHeader.biCompression = BI_BITFIELDS;
467 ((PDWORD)pbmi->bmiColors)[0] = 0xf800;
468 ((PDWORD)pbmi->bmiColors)[1] = 0x07e0;
469 ((PDWORD)pbmi->bmiColors)[2] = 0x001f;
470 SetLastError(0xdeadbeef);
471 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
472 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
473
474 /* test the DIB memory */
475 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
476 "VirtualQuery failed\n");
477 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
478 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
479 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
480 ok(info.RegionSize == 0x19000, "0x%lx != 0x19000\n", info.RegionSize);
481 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
482 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
483 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
484
485 test_dib_info(hdib, bits, &pbmi->bmiHeader);
486 DeleteObject(hdib);
487
488 memset(pbmi, 0, sizeof(bmibuf));
489 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
490 pbmi->bmiHeader.biHeight = 16;
491 pbmi->bmiHeader.biWidth = 16;
492 pbmi->bmiHeader.biBitCount = 1;
493 pbmi->bmiHeader.biPlanes = 1;
494 pbmi->bmiHeader.biCompression = BI_RGB;
495 pbmi->bmiColors[0].rgbRed = 0xff;
496 pbmi->bmiColors[0].rgbGreen = 0;
497 pbmi->bmiColors[0].rgbBlue = 0;
498 pbmi->bmiColors[1].rgbRed = 0;
499 pbmi->bmiColors[1].rgbGreen = 0;
500 pbmi->bmiColors[1].rgbBlue = 0xff;
501
502 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
503 ok(hdib != NULL, "CreateDIBSection failed\n");
504 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
505 ok(dibsec.dsBmih.biClrUsed == 2,
506 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
507
508 /* Test if the old BITMAPCOREINFO structure is supported */
509
510 pbci->bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
511 pbci->bmciHeader.bcBitCount = 0;
512
513 if (!is_win9x) {
514 ret = GetDIBits(hdc, hdib, 0, 16, NULL, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
515 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
516 ok((pbci->bmciHeader.bcWidth == 16) && (pbci->bmciHeader.bcHeight == 16)
517 && (pbci->bmciHeader.bcBitCount == 1) && (pbci->bmciHeader.bcPlanes == 1),
518 "GetDIBits did't fill in the BITMAPCOREHEADER structure properly\n");
519
520 ret = GetDIBits(hdc, hdib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
521 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
522 ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
523 (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
524 (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
525 "The color table has not been translated to the old BITMAPCOREINFO format\n");
526
527 hcoredib = CreateDIBSection(hdc, (BITMAPINFO*) pbci, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
528 ok(hcoredib != NULL, "CreateDIBSection failed with a BITMAPCOREINFO\n");
529
530 ZeroMemory(pbci->bmciColors, 256 * sizeof(RGBTRIPLE));
531 ret = GetDIBits(hdc, hcoredib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
532 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
533 ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
534 (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
535 (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
536 "The color table has not been translated to the old BITMAPCOREINFO format\n");
537
538 DeleteObject(hcoredib);
539 }
540
541 hdcmem = CreateCompatibleDC(hdc);
542 oldbm = SelectObject(hdcmem, hdib);
543
544 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
545 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
546 ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
547 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
548 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
549 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
550
551 c0 = RGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen, pbmi->bmiColors[0].rgbBlue);
552 c1 = RGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen, pbmi->bmiColors[1].rgbBlue);
553
554 test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
555 test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
556 test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
557 test_color(hdcmem, PALETTEINDEX(0), c0, 1, 1);
558 test_color(hdcmem, PALETTEINDEX(1), c0, 1, 1);
559 test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
560 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen,
561 pbmi->bmiColors[0].rgbBlue), c0, 1, 1);
562 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen,
563 pbmi->bmiColors[1].rgbBlue), c1, 1, 1);
564 test_color(hdcmem, PALETTERGB(0, 0, 0), c0, 1, 1);
565 test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
566 test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c1, 1, 1);
567
568 SelectObject(hdcmem, oldbm);
569 DeleteObject(hdib);
570
571 pbmi->bmiColors[0].rgbRed = 0xff;
572 pbmi->bmiColors[0].rgbGreen = 0xff;
573 pbmi->bmiColors[0].rgbBlue = 0xff;
574 pbmi->bmiColors[1].rgbRed = 0;
575 pbmi->bmiColors[1].rgbGreen = 0;
576 pbmi->bmiColors[1].rgbBlue = 0;
577
578 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
579 ok(hdib != NULL, "CreateDIBSection failed\n");
580
581 test_dib_info(hdib, bits, &pbmi->bmiHeader);
582
583 oldbm = SelectObject(hdcmem, hdib);
584
585 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
586 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
587 ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
588 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
589 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
590 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
591
592 SelectObject(hdcmem, oldbm);
593 test_dib_info(hdib, bits, &pbmi->bmiHeader);
594 DeleteObject(hdib);
595
596 pbmi->bmiHeader.biBitCount = 4;
597 for (i = 0; i < 16; i++) {
598 pbmi->bmiColors[i].rgbRed = i;
599 pbmi->bmiColors[i].rgbGreen = 16-i;
600 pbmi->bmiColors[i].rgbBlue = 0;
601 }
602 hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
603 ok(hdib != NULL, "CreateDIBSection failed\n");
604 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
605 ok(dibsec.dsBmih.biClrUsed == 16,
606 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 16);
607 test_dib_info(hdib, bits, &pbmi->bmiHeader);
608 DeleteObject(hdib);
609
610 pbmi->bmiHeader.biBitCount = 8;
611
612 for (i = 0; i < 128; i++) {
613 pbmi->bmiColors[i].rgbRed = 255 - i * 2;
614 pbmi->bmiColors[i].rgbGreen = i * 2;
615 pbmi->bmiColors[i].rgbBlue = 0;
616 pbmi->bmiColors[255 - i].rgbRed = 0;
617 pbmi->bmiColors[255 - i].rgbGreen = i * 2;
618 pbmi->bmiColors[255 - i].rgbBlue = 255 - i * 2;
619 }
620 hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
621 ok(hdib != NULL, "CreateDIBSection failed\n");
622 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
623 ok(dibsec.dsBmih.biClrUsed == 256,
624 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
625
626 oldbm = SelectObject(hdcmem, hdib);
627
628 for (i = 0; i < 256; i++) {
629 test_color(hdcmem, DIBINDEX(i),
630 RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
631 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue),
632 RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
633 }
634
635 SelectObject(hdcmem, oldbm);
636 test_dib_info(hdib, bits, &pbmi->bmiHeader);
637 DeleteObject(hdib);
638
639 pbmi->bmiHeader.biBitCount = 1;
640
641 /* Now create a palette and a palette indexed dib section */
642 memset(plogpal, 0, sizeof(logpalbuf));
643 plogpal->palVersion = 0x300;
644 plogpal->palNumEntries = 2;
645 plogpal->palPalEntry[0].peRed = 0xff;
646 plogpal->palPalEntry[0].peBlue = 0xff;
647 plogpal->palPalEntry[1].peGreen = 0xff;
648
649 index = (WORD*)pbmi->bmiColors;
650 *index++ = 0;
651 *index = 1;
652 hpal = CreatePalette(plogpal);
653 ok(hpal != NULL, "CreatePalette failed\n");
654 oldpal = SelectPalette(hdc, hpal, TRUE);
655 hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
656 ok(hdib != NULL, "CreateDIBSection failed\n");
657 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
658 ok(dibsec.dsBmih.biClrUsed == 2,
659 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
660
661 /* The colour table has already been grabbed from the dc, so we select back the
662 old palette */
663
664 SelectPalette(hdc, oldpal, TRUE);
665 oldbm = SelectObject(hdcmem, hdib);
666 oldpal = SelectPalette(hdcmem, hpal, TRUE);
667
668 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
669 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
670 ok(rgb[0].rgbRed == 0xff && rgb[0].rgbBlue == 0xff && rgb[0].rgbGreen == 0 &&
671 rgb[1].rgbRed == 0 && rgb[1].rgbBlue == 0 && rgb[1].rgbGreen == 0xff,
672 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
673 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
674 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
675
676 c0 = RGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen, plogpal->palPalEntry[0].peBlue);
677 c1 = RGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen, plogpal->palPalEntry[1].peBlue);
678
679 test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
680 test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
681 test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
682 test_color(hdcmem, PALETTEINDEX(0), c0, 0, 1);
683 test_color(hdcmem, PALETTEINDEX(1), c1, 0, 1);
684 test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
685 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen,
686 plogpal->palPalEntry[0].peBlue), c0, 1, 1);
687 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen,
688 plogpal->palPalEntry[1].peBlue), c1, 1, 1);
689 test_color(hdcmem, PALETTERGB(0, 0, 0), c1, 1, 1);
690 test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
691 test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c0, 1, 1);
692 test_color(hdcmem, PALETTERGB(0, 1, 0), c1, 1, 1);
693 test_color(hdcmem, PALETTERGB(0x3f, 0, 0x3f), c1, 1, 1);
694 test_color(hdcmem, PALETTERGB(0x40, 0, 0x40), c0, 1, 1);
695
696 /* Bottom and 2nd row from top green, everything else magenta */
697 bits[0] = bits[1] = 0xff;
698 bits[13 * 4] = bits[13*4 + 1] = 0xff;
699
700 test_dib_info(hdib, bits, &pbmi->bmiHeader);
701
702 pbmi->bmiHeader.biBitCount = 32;
703
704 hdib2 = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&bits32, NULL, 0);
705 ok(hdib2 != NULL, "CreateDIBSection failed\n");
706 hdcmem2 = CreateCompatibleDC(hdc);
707 oldbm2 = SelectObject(hdcmem2, hdib2);
708
709 BitBlt(hdcmem2, 0, 0, 16,16, hdcmem, 0, 0, SRCCOPY);
710
711 ok(bits32[0] == 0xff00, "lower left pixel is %08x\n", bits32[0]);
712 ok(bits32[17] == 0xff00ff, "bottom but one, left pixel is %08x\n", bits32[17]);
713
714 SelectObject(hdcmem2, oldbm2);
715 test_dib_info(hdib2, bits32, &pbmi->bmiHeader);
716 DeleteObject(hdib2);
717
718 SelectObject(hdcmem, oldbm);
719 SelectObject(hdcmem, oldpal);
720 DeleteObject(hdib);
721 DeleteObject(hpal);
722
723
724 pbmi->bmiHeader.biBitCount = 8;
725
726 memset(plogpal, 0, sizeof(logpalbuf));
727 plogpal->palVersion = 0x300;
728 plogpal->palNumEntries = 256;
729
730 for (i = 0; i < 128; i++) {
731 plogpal->palPalEntry[i].peRed = 255 - i * 2;
732 plogpal->palPalEntry[i].peBlue = i * 2;
733 plogpal->palPalEntry[i].peGreen = 0;
734 plogpal->palPalEntry[255 - i].peRed = 0;
735 plogpal->palPalEntry[255 - i].peGreen = i * 2;
736 plogpal->palPalEntry[255 - i].peBlue = 255 - i * 2;
737 }
738
739 index = (WORD*)pbmi->bmiColors;
740 for (i = 0; i < 256; i++) {
741 *index++ = i;
742 }
743
744 hpal = CreatePalette(plogpal);
745 ok(hpal != NULL, "CreatePalette failed\n");
746 oldpal = SelectPalette(hdc, hpal, TRUE);
747 hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
748 ok(hdib != NULL, "CreateDIBSection failed\n");
749 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
750 ok(dibsec.dsBmih.biClrUsed == 256,
751 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
752
753 test_dib_info(hdib, bits, &pbmi->bmiHeader);
754
755 SelectPalette(hdc, oldpal, TRUE);
756 oldbm = SelectObject(hdcmem, hdib);
757 oldpal = SelectPalette(hdcmem, hpal, TRUE);
758
759 ret = GetDIBColorTable(hdcmem, 0, 256, rgb);
760 ok(ret == 256, "GetDIBColorTable returned %d\n", ret);
761 for (i = 0; i < 256; i++) {
762 ok(rgb[i].rgbRed == plogpal->palPalEntry[i].peRed &&
763 rgb[i].rgbBlue == plogpal->palPalEntry[i].peBlue &&
764 rgb[i].rgbGreen == plogpal->palPalEntry[i].peGreen,
765 "GetDIBColorTable returns table %d: r%02x g%02x b%02x res%02x\n",
766 i, rgb[i].rgbRed, rgb[i].rgbGreen, rgb[i].rgbBlue, rgb[i].rgbReserved);
767 }
768
769 for (i = 0; i < 256; i++) {
770 test_color(hdcmem, DIBINDEX(i),
771 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
772 test_color(hdcmem, PALETTEINDEX(i),
773 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
774 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue),
775 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
776 }
777
778 SelectPalette(hdcmem, oldpal, TRUE);
779 SelectObject(hdcmem, oldbm);
780 DeleteObject(hdib);
781 DeleteObject(hpal);
782
783
784 DeleteDC(hdcmem);
785 ReleaseDC(0, hdc);
786 }
787
788 static void test_mono_dibsection(void)
789 {
790 HDC hdc, memdc;
791 HBITMAP old_bm, mono_ds;