gdi32: Add more broken behaviour on nt4 sp3.
[wine/multimedia.git] / dlls / gdi32 / tests / bitmap.c
blobca53fc76f7c7f35374ac5734336d69e1b64c8164
1 /*
2 * Unit test suite for bitmaps
4 * Copyright 2004 Huw Davies
5 * Copyright 2006 Dmitry Timoshkov
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.
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.
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
22 #include <stdarg.h>
23 #include <assert.h>
24 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "mmsystem.h"
33 #include "wine/test.h"
35 static BOOL (WINAPI *pGdiAlphaBlend)(HDC,int,int,int,int,HDC,int,int,int,int,BLENDFUNCTION);
37 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
39 static INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
41 switch(bpp)
43 case 1:
44 return 2 * ((bmWidth+15) >> 4);
46 case 24:
47 bmWidth *= 3; /* fall through */
48 case 8:
49 return bmWidth + (bmWidth & 1);
51 case 32:
52 return bmWidth * 4;
54 case 16:
55 case 15:
56 return bmWidth * 2;
58 case 4:
59 return 2 * ((bmWidth+3) >> 2);
61 default:
62 trace("Unknown depth %d, please report.\n", bpp );
63 assert(0);
65 return -1;
68 static void test_bitmap_info(HBITMAP hbm, INT expected_depth, const BITMAPINFOHEADER *bmih)
70 BITMAP bm;
71 BITMAP bma[2];
72 INT ret, width_bytes;
73 BYTE buf[512], buf_cmp[512];
75 ret = GetObject(hbm, sizeof(bm), &bm);
76 ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
78 ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
79 ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
80 ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
81 width_bytes = BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
82 ok(bm.bmWidthBytes == width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, width_bytes);
83 ok(bm.bmPlanes == bmih->biPlanes, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
84 ok(bm.bmBitsPixel == expected_depth, "wrong bm.bmBitsPixel %d != %d\n", bm.bmBitsPixel, expected_depth);
85 ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
87 assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
88 assert(sizeof(buf) == sizeof(buf_cmp));
90 SetLastError(0xdeadbeef);
91 ret = GetBitmapBits(hbm, 0, NULL);
92 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
94 memset(buf_cmp, 0xAA, sizeof(buf_cmp));
95 memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
97 memset(buf, 0xAA, sizeof(buf));
98 ret = GetBitmapBits(hbm, sizeof(buf), buf);
99 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
100 ok(!memcmp(buf, buf_cmp, sizeof(buf)),
101 "buffers do not match, depth %d\n", bmih->biBitCount);
103 /* test various buffer sizes for GetObject */
104 ret = GetObject(hbm, sizeof(*bma) * 2, bma);
105 ok(ret == sizeof(*bma), "wrong size %d\n", ret);
107 ret = GetObject(hbm, sizeof(bm) / 2, &bm);
108 ok(ret == 0, "%d != 0\n", ret);
110 ret = GetObject(hbm, 0, &bm);
111 ok(ret == 0, "%d != 0\n", ret);
113 ret = GetObject(hbm, 1, &bm);
114 ok(ret == 0, "%d != 0\n", ret);
116 ret = GetObject(hbm, 0, NULL);
117 ok(ret == sizeof(bm), "wrong size %d\n", ret);
120 static void test_createdibitmap(void)
122 HDC hdc, hdcmem;
123 BITMAPINFOHEADER bmih;
124 BITMAPINFO bm;
125 HBITMAP hbm, hbm_colour, hbm_old;
126 INT screen_depth;
127 DWORD pixel;
129 hdc = GetDC(0);
130 screen_depth = GetDeviceCaps(hdc, BITSPIXEL);
131 memset(&bmih, 0, sizeof(bmih));
132 bmih.biSize = sizeof(bmih);
133 bmih.biWidth = 10;
134 bmih.biHeight = 10;
135 bmih.biPlanes = 1;
136 bmih.biBitCount = 32;
137 bmih.biCompression = BI_RGB;
139 hbm = CreateDIBitmap(hdc, NULL, CBM_INIT, NULL, NULL, 0);
140 ok(hbm == NULL, "CreateDIBitmap should fail\n");
141 hbm = CreateDIBitmap(hdc, NULL, 0, NULL, NULL, 0);
142 ok(hbm == NULL, "CreateDIBitmap should fail\n");
144 /* First create an un-initialised bitmap. The depth of the bitmap
145 should match that of the hdc and not that supplied in bmih.
148 /* First try 32 bits */
149 hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
150 ok(hbm != NULL, "CreateDIBitmap failed\n");
151 test_bitmap_info(hbm, screen_depth, &bmih);
152 DeleteObject(hbm);
154 /* Then 16 */
155 bmih.biBitCount = 16;
156 hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
157 ok(hbm != NULL, "CreateDIBitmap failed\n");
158 test_bitmap_info(hbm, screen_depth, &bmih);
159 DeleteObject(hbm);
161 /* Then 1 */
162 bmih.biBitCount = 1;
163 hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
164 ok(hbm != NULL, "CreateDIBitmap failed\n");
165 test_bitmap_info(hbm, screen_depth, &bmih);
166 DeleteObject(hbm);
168 /* Now with a monochrome dc we expect a monochrome bitmap */
169 hdcmem = CreateCompatibleDC(hdc);
171 /* First try 32 bits */
172 bmih.biBitCount = 32;
173 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
174 ok(hbm != NULL, "CreateDIBitmap failed\n");
175 test_bitmap_info(hbm, 1, &bmih);
176 DeleteObject(hbm);
178 /* Then 16 */
179 bmih.biBitCount = 16;
180 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
181 ok(hbm != NULL, "CreateDIBitmap failed\n");
182 test_bitmap_info(hbm, 1, &bmih);
183 DeleteObject(hbm);
185 /* Then 1 */
186 bmih.biBitCount = 1;
187 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
188 ok(hbm != NULL, "CreateDIBitmap failed\n");
189 test_bitmap_info(hbm, 1, &bmih);
190 DeleteObject(hbm);
192 /* Now select a polychrome bitmap into the dc and we expect
193 screen_depth bitmaps again */
194 hbm_colour = CreateCompatibleBitmap(hdc, bmih.biWidth, bmih.biHeight);
195 test_bitmap_info(hbm_colour, screen_depth, &bmih);
196 hbm_old = SelectObject(hdcmem, hbm_colour);
198 /* First try 32 bits */
199 bmih.biBitCount = 32;
200 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
201 ok(hbm != NULL, "CreateDIBitmap failed\n");
202 test_bitmap_info(hbm, screen_depth, &bmih);
203 DeleteObject(hbm);
205 /* Then 16 */
206 bmih.biBitCount = 16;
207 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
208 ok(hbm != NULL, "CreateDIBitmap failed\n");
209 test_bitmap_info(hbm, screen_depth, &bmih);
210 DeleteObject(hbm);
212 /* Then 1 */
213 bmih.biBitCount = 1;
214 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
215 ok(hbm != NULL, "CreateDIBitmap failed\n");
216 test_bitmap_info(hbm, screen_depth, &bmih);
217 DeleteObject(hbm);
219 SelectObject(hdcmem, hbm_old);
220 DeleteObject(hbm_colour);
221 DeleteDC(hdcmem);
223 bmih.biBitCount = 32;
224 hbm = CreateDIBitmap(0, &bmih, 0, NULL, NULL, 0);
225 ok(hbm != NULL, "CreateDIBitmap failed\n");
226 test_bitmap_info(hbm, 1, &bmih);
227 DeleteObject(hbm);
229 /* Test how formats are converted */
230 pixel = 0xffffffff;
231 bmih.biBitCount = 1;
232 bmih.biWidth = 1;
233 bmih.biHeight = 1;
235 memset(&bm, 0, sizeof(bm));
236 bm.bmiHeader.biSize = sizeof(bm.bmiHeader);
237 bm.bmiHeader.biWidth = 1;
238 bm.bmiHeader.biHeight = 1;
239 bm.bmiHeader.biPlanes = 1;
240 bm.bmiHeader.biBitCount= 24;
241 bm.bmiHeader.biCompression= BI_RGB;
242 bm.bmiHeader.biSizeImage = 0;
243 hbm = CreateDIBitmap(hdc, &bmih, CBM_INIT, &pixel, &bm, DIB_RGB_COLORS);
244 ok(hbm != NULL, "CreateDIBitmap failed\n");
246 pixel = 0xdeadbeef;
247 bm.bmiHeader.biBitCount= 32;
248 GetDIBits(hdc, hbm, 0, 1, &pixel, &bm, DIB_RGB_COLORS);
249 ok(pixel == 0x00ffffff, "Reading a 32 bit pixel from a DDB returned %08x\n", pixel);
250 DeleteObject(hbm);
252 ReleaseDC(0, hdc);
255 static INT DIB_GetWidthBytes( int width, int bpp )
257 int words;
259 switch (bpp)
261 case 1: words = (width + 31) / 32; break;
262 case 4: words = (width + 7) / 8; break;
263 case 8: words = (width + 3) / 4; break;
264 case 15:
265 case 16: words = (width + 1) / 2; break;
266 case 24: words = (width * 3 + 3)/4; break;
267 case 32: words = width; break;
269 default:
270 words=0;
271 trace("Unknown depth %d, please report.\n", bpp );
272 assert(0);
273 break;
275 return 4 * words;
278 static void test_dib_info(HBITMAP hbm, const void *bits, const BITMAPINFOHEADER *bmih)
280 BITMAP bm;
281 BITMAP bma[2];
282 DIBSECTION ds;
283 DIBSECTION dsa[2];
284 INT ret, bm_width_bytes, dib_width_bytes;
285 BYTE *buf;
287 ret = GetObject(hbm, sizeof(bm), &bm);
288 ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
290 ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
291 ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
292 ok(bm.bmHeight == abs(bmih->biHeight), "wrong bm.bmHeight %d\n", bm.bmHeight);
293 dib_width_bytes = DIB_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
294 bm_width_bytes = BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
295 if (bm.bmWidthBytes != dib_width_bytes) /* Win2k bug */
296 ok(bm.bmWidthBytes == bm_width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, bm_width_bytes);
297 else
298 ok(bm.bmWidthBytes == dib_width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, dib_width_bytes);
299 ok(bm.bmPlanes == bmih->biPlanes, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
300 ok(bm.bmBitsPixel == bmih->biBitCount, "bm.bmBitsPixel %d != %d\n", bm.bmBitsPixel, bmih->biBitCount);
301 ok(bm.bmBits == bits, "wrong bm.bmBits %p != %p\n", bm.bmBits, bits);
303 buf = HeapAlloc(GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight + 4096);
305 /* GetBitmapBits returns not 32-bit aligned data */
306 SetLastError(0xdeadbeef);
307 ret = GetBitmapBits(hbm, 0, NULL);
308 ok(ret == bm_width_bytes * bm.bmHeight,
309 "%d != %d\n", ret, bm_width_bytes * bm.bmHeight);
311 memset(buf, 0xAA, bm.bmWidthBytes * bm.bmHeight + 4096);
312 ret = GetBitmapBits(hbm, bm.bmWidthBytes * bm.bmHeight + 4096, buf);
313 ok(ret == bm_width_bytes * bm.bmHeight, "%d != %d\n", ret, bm_width_bytes * bm.bmHeight);
315 HeapFree(GetProcessHeap(), 0, buf);
317 /* test various buffer sizes for GetObject */
318 memset(&ds, 0xAA, sizeof(ds));
319 ret = GetObject(hbm, sizeof(*bma) * 2, bma);
320 ok(ret == sizeof(*bma), "wrong size %d\n", ret);
321 ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
322 ok(bm.bmHeight == abs(bmih->biHeight), "wrong bm.bmHeight %d\n", bm.bmHeight);
323 ok(bm.bmBits == bits, "wrong bm.bmBits %p != %p\n", bm.bmBits, bits);
325 ret = GetObject(hbm, sizeof(bm) / 2, &bm);
326 ok(ret == 0, "%d != 0\n", ret);
328 ret = GetObject(hbm, 0, &bm);
329 ok(ret == 0, "%d != 0\n", ret);
331 ret = GetObject(hbm, 1, &bm);
332 ok(ret == 0, "%d != 0\n", ret);
334 /* test various buffer sizes for GetObject */
335 ret = GetObject(hbm, 0, NULL);
336 ok(ret == sizeof(bm), "wrong size %d\n", ret);
338 ret = GetObject(hbm, sizeof(*dsa) * 2, dsa);
339 ok(ret == sizeof(*dsa), "wrong size %d\n", ret);
341 memset(&ds, 0xAA, sizeof(ds));
342 ret = GetObject(hbm, sizeof(ds), &ds);
343 ok(ret == sizeof(ds), "wrong size %d\n", ret);
345 ok(ds.dsBm.bmBits == bits, "wrong bm.bmBits %p != %p\n", ds.dsBm.bmBits, bits);
346 if (ds.dsBm.bmWidthBytes != bm_width_bytes) /* Win2k bug */
347 ok(ds.dsBmih.biSizeImage == ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight, "%u != %u\n",
348 ds.dsBmih.biSizeImage, ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight);
349 ok(bmih->biSizeImage == 0, "%u != 0\n", bmih->biSizeImage);
350 ds.dsBmih.biSizeImage = 0;
352 ok(ds.dsBmih.biSize == bmih->biSize, "%u != %u\n", ds.dsBmih.biSize, bmih->biSize);
353 ok(ds.dsBmih.biWidth == bmih->biWidth, "%d != %d\n", ds.dsBmih.biWidth, bmih->biWidth);
354 ok(ds.dsBmih.biHeight == abs(bmih->biHeight), "%d != %d\n", ds.dsBmih.biHeight, abs(bmih->biHeight));
355 ok(ds.dsBmih.biPlanes == bmih->biPlanes, "%u != %u\n", ds.dsBmih.biPlanes, bmih->biPlanes);
356 ok(ds.dsBmih.biBitCount == bmih->biBitCount, "%u != %u\n", ds.dsBmih.biBitCount, bmih->biBitCount);
357 ok(ds.dsBmih.biCompression == bmih->biCompression ||
358 ((bmih->biBitCount == 32) && broken(ds.dsBmih.biCompression == BI_BITFIELDS)), /* nt4 sp1 and 2 */
359 "%u != %u\n", ds.dsBmih.biCompression, bmih->biCompression);
360 ok(ds.dsBmih.biSizeImage == bmih->biSizeImage, "%u != %u\n", ds.dsBmih.biSizeImage, bmih->biSizeImage);
361 ok(ds.dsBmih.biXPelsPerMeter == bmih->biXPelsPerMeter, "%d != %d\n", ds.dsBmih.biXPelsPerMeter, bmih->biXPelsPerMeter);
362 ok(ds.dsBmih.biYPelsPerMeter == bmih->biYPelsPerMeter, "%d != %d\n", ds.dsBmih.biYPelsPerMeter, bmih->biYPelsPerMeter);
364 memset(&ds, 0xAA, sizeof(ds));
365 ret = GetObject(hbm, sizeof(ds) - 4, &ds);
366 ok(ret == sizeof(ds.dsBm), "wrong size %d\n", ret);
367 ok(ds.dsBm.bmWidth == bmih->biWidth, "%d != %d\n", ds.dsBmih.biWidth, bmih->biWidth);
368 ok(ds.dsBm.bmHeight == abs(bmih->biHeight), "%d != %d\n", ds.dsBmih.biHeight, abs(bmih->biHeight));
369 ok(ds.dsBm.bmBits == bits, "%p != %p\n", ds.dsBm.bmBits, bits);
371 ret = GetObject(hbm, 0, &ds);
372 ok(ret == 0, "%d != 0\n", ret);
374 ret = GetObject(hbm, 1, &ds);
375 ok(ret == 0, "%d != 0\n", ret);
378 #define test_color_todo(got, exp, txt, todo) \
379 if (!todo && got != exp && screen_depth < 24) { \
380 todo_wine ok(0, #txt " failed at %d-bit screen depth: got 0x%06x expected 0x%06x - skipping DIB tests\n", \
381 screen_depth, (UINT)got, (UINT)exp); \
382 return; \
383 } else if (todo) todo_wine { ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp); } \
384 else ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp) \
386 #define test_color(hdc, color, exp, todo_setp, todo_getp) \
388 COLORREF c; \
389 c = SetPixel(hdc, 0, 0, color); \
390 test_color_todo(c, exp, SetPixel, todo_setp); \
391 c = GetPixel(hdc, 0, 0); \
392 test_color_todo(c, exp, GetPixel, todo_getp); \
395 static void test_dib_bits_access( HBITMAP hdib, void *bits )
397 MEMORY_BASIC_INFORMATION info;
398 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
399 DWORD data[256];
400 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
401 HDC hdc;
402 char filename[MAX_PATH];
403 HANDLE file;
404 DWORD written;
405 INT ret;
407 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
408 "VirtualQuery failed\n");
409 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
410 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
411 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
412 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
413 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
414 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
416 memset( pbmi, 0, sizeof(bmibuf) );
417 memset( data, 0xcc, sizeof(data) );
418 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
419 pbmi->bmiHeader.biHeight = 16;
420 pbmi->bmiHeader.biWidth = 16;
421 pbmi->bmiHeader.biBitCount = 32;
422 pbmi->bmiHeader.biPlanes = 1;
423 pbmi->bmiHeader.biCompression = BI_RGB;
425 hdc = GetDC(0);
427 ret = SetDIBits( hdc, hdib, 0, 16, data, pbmi, DIB_RGB_COLORS );
428 ok(ret == 16, "SetDIBits failed: expected 16 got %d\n", ret);
430 ReleaseDC(0, hdc);
432 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
433 "VirtualQuery failed\n");
434 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
435 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
436 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
437 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
438 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
439 /* it has been protected now */
440 todo_wine ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
442 /* try writing protected bits to a file */
444 GetTempFileNameA( ".", "dib", 0, filename );
445 file = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
446 CREATE_ALWAYS, 0, 0 );
447 ok( file != INVALID_HANDLE_VALUE, "failed to open %s error %u\n", filename, GetLastError() );
448 ret = WriteFile( file, bits, 8192, &written, NULL );
449 ok( ret, "WriteFile failed error %u\n", GetLastError() );
450 if (ret) ok( written == 8192, "only wrote %u bytes\n", written );
451 CloseHandle( file );
452 DeleteFileA( filename );
455 static void test_dibsections(void)
457 HDC hdc, hdcmem, hdcmem2;
458 HBITMAP hdib, oldbm, hdib2, oldbm2;
459 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
460 char bcibuf[sizeof(BITMAPCOREINFO) + 256 * sizeof(RGBTRIPLE)];
461 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
462 BITMAPCOREINFO *pbci = (BITMAPCOREINFO *)bcibuf;
463 HBITMAP hcoredib;
464 char coreBits[256];
465 BYTE *bits;
466 RGBQUAD rgb[256];
467 int ret;
468 char logpalbuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
469 LOGPALETTE *plogpal = (LOGPALETTE*)logpalbuf;
470 WORD *index;
471 DWORD *bits32;
472 HPALETTE hpal, oldpal;
473 DIBSECTION dibsec;
474 COLORREF c0, c1;
475 int i;
476 int screen_depth;
477 MEMORY_BASIC_INFORMATION info;
479 hdc = GetDC(0);
480 screen_depth = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
482 memset(pbmi, 0, sizeof(bmibuf));
483 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
484 pbmi->bmiHeader.biHeight = 100;
485 pbmi->bmiHeader.biWidth = 512;
486 pbmi->bmiHeader.biBitCount = 24;
487 pbmi->bmiHeader.biPlanes = 1;
488 pbmi->bmiHeader.biCompression = BI_RGB;
490 SetLastError(0xdeadbeef);
492 /* invalid pointer for BITMAPINFO
493 (*bits should be NULL on error) */
494 bits = (BYTE*)0xdeadbeef;
495 hdib = CreateDIBSection(hdc, NULL, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
496 ok(hdib == NULL && bits == NULL, "CreateDIBSection failed for invalid parameter: bmi == 0x0\n");
498 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
499 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
500 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
501 ok(dibsec.dsBm.bmBits == bits, "dibsec.dsBits %p != bits %p\n", dibsec.dsBm.bmBits, bits);
503 /* test the DIB memory */
504 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
505 "VirtualQuery failed\n");
506 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
507 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
508 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
509 ok(info.RegionSize == 0x26000, "0x%lx != 0x26000\n", info.RegionSize);
510 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
511 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
512 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
514 test_dib_bits_access( hdib, bits );
516 test_dib_info(hdib, bits, &pbmi->bmiHeader);
517 DeleteObject(hdib);
519 /* Test a top-down DIB. */
520 pbmi->bmiHeader.biHeight = -100;
521 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
522 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
523 test_dib_info(hdib, bits, &pbmi->bmiHeader);
524 DeleteObject(hdib);
526 pbmi->bmiHeader.biHeight = 100;
527 pbmi->bmiHeader.biBitCount = 8;
528 pbmi->bmiHeader.biCompression = BI_RLE8;
529 SetLastError(0xdeadbeef);
530 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
531 ok(hdib == NULL, "CreateDIBSection should fail when asked to create a compressed DIB section\n");
532 ok(GetLastError() == 0xdeadbeef, "wrong error %d\n", GetLastError());
534 for (i = 0; i < 128; i++)
536 pbmi->bmiHeader.biBitCount = i;
537 pbmi->bmiHeader.biCompression = BI_RGB;
538 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
539 if (i == 1 || i == 4 || i == 8 || i == 16 || i == 24 || i == 32)
540 ok(hdib != NULL, "CreateDIBSection bpp %u\n", i);
541 else
542 ok(hdib == NULL, "CreateDIBSection bpp %u succeeded\n", i);
543 if (hdib) DeleteObject( hdib );
546 pbmi->bmiHeader.biBitCount = 16;
547 pbmi->bmiHeader.biCompression = BI_BITFIELDS;
548 ((PDWORD)pbmi->bmiColors)[0] = 0xf800;
549 ((PDWORD)pbmi->bmiColors)[1] = 0x07e0;
550 ((PDWORD)pbmi->bmiColors)[2] = 0x001f;
551 SetLastError(0xdeadbeef);
552 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
553 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
555 /* test the DIB memory */
556 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
557 "VirtualQuery failed\n");
558 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
559 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
560 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
561 ok(info.RegionSize == 0x19000, "0x%lx != 0x19000\n", info.RegionSize);
562 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
563 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
564 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
566 test_dib_info(hdib, bits, &pbmi->bmiHeader);
567 DeleteObject(hdib);
569 memset(pbmi, 0, sizeof(bmibuf));
570 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
571 pbmi->bmiHeader.biHeight = 16;
572 pbmi->bmiHeader.biWidth = 16;
573 pbmi->bmiHeader.biBitCount = 1;
574 pbmi->bmiHeader.biPlanes = 1;
575 pbmi->bmiHeader.biCompression = BI_RGB;
576 pbmi->bmiColors[0].rgbRed = 0xff;
577 pbmi->bmiColors[0].rgbGreen = 0;
578 pbmi->bmiColors[0].rgbBlue = 0;
579 pbmi->bmiColors[1].rgbRed = 0;
580 pbmi->bmiColors[1].rgbGreen = 0;
581 pbmi->bmiColors[1].rgbBlue = 0xff;
583 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
584 ok(hdib != NULL, "CreateDIBSection failed\n");
585 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
586 ok(dibsec.dsBmih.biClrUsed == 2,
587 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
589 /* Test if the old BITMAPCOREINFO structure is supported */
591 pbci->bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
592 pbci->bmciHeader.bcBitCount = 0;
594 ret = GetDIBits(hdc, hdib, 0, 16, NULL, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
595 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
596 ok((pbci->bmciHeader.bcWidth == 16) && (pbci->bmciHeader.bcHeight == 16)
597 && (pbci->bmciHeader.bcBitCount == 1) && (pbci->bmciHeader.bcPlanes == 1),
598 "GetDIBits did't fill in the BITMAPCOREHEADER structure properly\n");
600 ret = GetDIBits(hdc, hdib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
601 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
602 ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
603 (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
604 (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
605 "The color table has not been translated to the old BITMAPCOREINFO format\n");
607 hcoredib = CreateDIBSection(hdc, (BITMAPINFO*) pbci, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
608 ok(hcoredib != NULL, "CreateDIBSection failed with a BITMAPCOREINFO\n");
610 ZeroMemory(pbci->bmciColors, 256 * sizeof(RGBTRIPLE));
611 ret = GetDIBits(hdc, hcoredib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
612 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
613 ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
614 (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
615 (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
616 "The color table has not been translated to the old BITMAPCOREINFO format\n");
618 DeleteObject(hcoredib);
620 hdcmem = CreateCompatibleDC(hdc);
621 oldbm = SelectObject(hdcmem, hdib);
623 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
624 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
625 ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
626 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
627 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
628 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
630 c0 = RGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen, pbmi->bmiColors[0].rgbBlue);
631 c1 = RGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen, pbmi->bmiColors[1].rgbBlue);
633 test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
634 test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
635 test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
636 test_color(hdcmem, PALETTEINDEX(0), c0, 1, 1);
637 test_color(hdcmem, PALETTEINDEX(1), c0, 1, 1);
638 test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
639 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen,
640 pbmi->bmiColors[0].rgbBlue), c0, 1, 1);
641 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen,
642 pbmi->bmiColors[1].rgbBlue), c1, 1, 1);
643 test_color(hdcmem, PALETTERGB(0, 0, 0), c0, 1, 1);
644 test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
645 test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c1, 1, 1);
647 SelectObject(hdcmem, oldbm);
648 DeleteObject(hdib);
650 pbmi->bmiColors[0].rgbRed = 0xff;
651 pbmi->bmiColors[0].rgbGreen = 0xff;
652 pbmi->bmiColors[0].rgbBlue = 0xff;
653 pbmi->bmiColors[1].rgbRed = 0;
654 pbmi->bmiColors[1].rgbGreen = 0;
655 pbmi->bmiColors[1].rgbBlue = 0;
657 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
658 ok(hdib != NULL, "CreateDIBSection failed\n");
660 test_dib_info(hdib, bits, &pbmi->bmiHeader);
662 oldbm = SelectObject(hdcmem, hdib);
664 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
665 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
666 ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
667 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
668 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
669 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
671 SelectObject(hdcmem, oldbm);
672 test_dib_info(hdib, bits, &pbmi->bmiHeader);
673 DeleteObject(hdib);
675 pbmi->bmiHeader.biBitCount = 4;
676 for (i = 0; i < 16; i++) {
677 pbmi->bmiColors[i].rgbRed = i;
678 pbmi->bmiColors[i].rgbGreen = 16-i;
679 pbmi->bmiColors[i].rgbBlue = 0;
681 hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
682 ok(hdib != NULL, "CreateDIBSection failed\n");
683 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
684 ok(dibsec.dsBmih.biClrUsed == 16,
685 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 16);
686 test_dib_info(hdib, bits, &pbmi->bmiHeader);
687 DeleteObject(hdib);
689 pbmi->bmiHeader.biBitCount = 8;
691 for (i = 0; i < 128; i++) {
692 pbmi->bmiColors[i].rgbRed = 255 - i * 2;
693 pbmi->bmiColors[i].rgbGreen = i * 2;
694 pbmi->bmiColors[i].rgbBlue = 0;
695 pbmi->bmiColors[255 - i].rgbRed = 0;
696 pbmi->bmiColors[255 - i].rgbGreen = i * 2;
697 pbmi->bmiColors[255 - i].rgbBlue = 255 - i * 2;
699 hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
700 ok(hdib != NULL, "CreateDIBSection failed\n");
701 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
702 ok(dibsec.dsBmih.biClrUsed == 256,
703 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
705 oldbm = SelectObject(hdcmem, hdib);
707 for (i = 0; i < 256; i++) {
708 test_color(hdcmem, DIBINDEX(i),
709 RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
710 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue),
711 RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
714 SelectObject(hdcmem, oldbm);
715 test_dib_info(hdib, bits, &pbmi->bmiHeader);
716 DeleteObject(hdib);
718 pbmi->bmiHeader.biBitCount = 1;
720 /* Now create a palette and a palette indexed dib section */
721 memset(plogpal, 0, sizeof(logpalbuf));
722 plogpal->palVersion = 0x300;
723 plogpal->palNumEntries = 2;
724 plogpal->palPalEntry[0].peRed = 0xff;
725 plogpal->palPalEntry[0].peBlue = 0xff;
726 plogpal->palPalEntry[1].peGreen = 0xff;
728 index = (WORD*)pbmi->bmiColors;
729 *index++ = 0;
730 *index = 1;
731 hpal = CreatePalette(plogpal);
732 ok(hpal != NULL, "CreatePalette failed\n");
733 oldpal = SelectPalette(hdc, hpal, TRUE);
734 hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
735 ok(hdib != NULL, "CreateDIBSection failed\n");
736 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
737 ok(dibsec.dsBmih.biClrUsed == 2, "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
739 /* The colour table has already been grabbed from the dc, so we select back the
740 old palette */
742 SelectPalette(hdc, oldpal, TRUE);
743 oldbm = SelectObject(hdcmem, hdib);
744 oldpal = SelectPalette(hdcmem, hpal, TRUE);
746 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
747 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
748 ok(rgb[0].rgbRed == 0xff && rgb[0].rgbBlue == 0xff && rgb[0].rgbGreen == 0 &&
749 rgb[1].rgbRed == 0 && rgb[1].rgbBlue == 0 && rgb[1].rgbGreen == 0xff,
750 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
751 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
752 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
754 c0 = RGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen, plogpal->palPalEntry[0].peBlue);
755 c1 = RGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen, plogpal->palPalEntry[1].peBlue);
757 test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
758 test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
759 test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
760 test_color(hdcmem, PALETTEINDEX(0), c0, 0, 1);
761 test_color(hdcmem, PALETTEINDEX(1), c1, 0, 1);
762 test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
763 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen,
764 plogpal->palPalEntry[0].peBlue), c0, 1, 1);
765 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen,
766 plogpal->palPalEntry[1].peBlue), c1, 1, 1);
767 test_color(hdcmem, PALETTERGB(0, 0, 0), c1, 1, 1);
768 test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
769 test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c0, 1, 1);
770 test_color(hdcmem, PALETTERGB(0, 1, 0), c1, 1, 1);
771 test_color(hdcmem, PALETTERGB(0x3f, 0, 0x3f), c1, 1, 1);
772 test_color(hdcmem, PALETTERGB(0x40, 0, 0x40), c0, 1, 1);
774 /* Bottom and 2nd row from top green, everything else magenta */
775 bits[0] = bits[1] = 0xff;
776 bits[13 * 4] = bits[13*4 + 1] = 0xff;
778 test_dib_info(hdib, bits, &pbmi->bmiHeader);
780 pbmi->bmiHeader.biBitCount = 32;
782 hdib2 = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&bits32, NULL, 0);
783 ok(hdib2 != NULL, "CreateDIBSection failed\n");
784 hdcmem2 = CreateCompatibleDC(hdc);
785 oldbm2 = SelectObject(hdcmem2, hdib2);
787 BitBlt(hdcmem2, 0, 0, 16,16, hdcmem, 0, 0, SRCCOPY);
789 ok(bits32[0] == 0xff00, "lower left pixel is %08x\n", bits32[0]);
790 ok(bits32[17] == 0xff00ff, "bottom but one, left pixel is %08x\n", bits32[17]);
792 SelectObject(hdcmem2, oldbm2);
793 test_dib_info(hdib2, bits32, &pbmi->bmiHeader);
794 DeleteObject(hdib2);
796 SelectObject(hdcmem, oldbm);
797 SelectPalette(hdcmem, oldpal, TRUE);
798 DeleteObject(hdib);
799 DeleteObject(hpal);
802 pbmi->bmiHeader.biBitCount = 8;
804 memset(plogpal, 0, sizeof(logpalbuf));
805 plogpal->palVersion = 0x300;
806 plogpal->palNumEntries = 256;
808 for (i = 0; i < 128; i++) {
809 plogpal->palPalEntry[i].peRed = 255 - i * 2;
810 plogpal->palPalEntry[i].peBlue = i * 2;
811 plogpal->palPalEntry[i].peGreen = 0;
812 plogpal->palPalEntry[255 - i].peRed = 0;
813 plogpal->palPalEntry[255 - i].peGreen = i * 2;
814 plogpal->palPalEntry[255 - i].peBlue = 255 - i * 2;
817 index = (WORD*)pbmi->bmiColors;
818 for (i = 0; i < 256; i++) {
819 *index++ = i;
822 hpal = CreatePalette(plogpal);
823 ok(hpal != NULL, "CreatePalette failed\n");
824 oldpal = SelectPalette(hdc, hpal, TRUE);
825 hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
826 ok(hdib != NULL, "CreateDIBSection failed\n");
827 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
828 ok(dibsec.dsBmih.biClrUsed == 256, "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
830 test_dib_info(hdib, bits, &pbmi->bmiHeader);
832 SelectPalette(hdc, oldpal, TRUE);
833 oldbm = SelectObject(hdcmem, hdib);
834 oldpal = SelectPalette(hdcmem, hpal, TRUE);
836 ret = GetDIBColorTable(hdcmem, 0, 256, rgb);
837 ok(ret == 256, "GetDIBColorTable returned %d\n", ret);
838 for (i = 0; i < 256; i++) {
839 ok(rgb[i].rgbRed == plogpal->palPalEntry[i].peRed &&
840 rgb[i].rgbBlue == plogpal->palPalEntry[i].peBlue &&
841 rgb[i].rgbGreen == plogpal->palPalEntry[i].peGreen,
842 "GetDIBColorTable returns table %d: r%02x g%02x b%02x res%02x\n",
843 i, rgb[i].rgbRed, rgb[i].rgbGreen, rgb[i].rgbBlue, rgb[i].rgbReserved);
846 for (i = 0; i < 256; i++) {
847 test_color(hdcmem, DIBINDEX(i),
848 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
849 test_color(hdcmem, PALETTEINDEX(i),
850 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
851 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue),
852 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
855 SelectPalette(hdcmem, oldpal, TRUE);
856 SelectObject(hdcmem, oldbm);
857 DeleteObject(hdib);
858 DeleteObject(hpal);
860 DeleteDC(hdcmem);
861 DeleteDC(hdcmem2);
862 ReleaseDC(0, hdc);
865 static void test_mono_dibsection(void)
867 HDC hdc, memdc;
868 HBITMAP old_bm, mono_ds;
869 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
870 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
871 BYTE bits[10 * 4];
872 BYTE *ds_bits;
873 int num;
875 hdc = GetDC(0);
877 memdc = CreateCompatibleDC(hdc);
879 memset(pbmi, 0, sizeof(bmibuf));
880 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
881 pbmi->bmiHeader.biHeight = 10;
882 pbmi->bmiHeader.biWidth = 10;
883 pbmi->bmiHeader.biBitCount = 1;
884 pbmi->bmiHeader.biPlanes = 1;
885 pbmi->bmiHeader.biCompression = BI_RGB;
886 pbmi->bmiColors[0].rgbRed = 0xff;
887 pbmi->bmiColors[0].rgbGreen = 0xff;
888 pbmi->bmiColors[0].rgbBlue = 0xff;
889 pbmi->bmiColors[1].rgbRed = 0x0;
890 pbmi->bmiColors[1].rgbGreen = 0x0;
891 pbmi->bmiColors[1].rgbBlue = 0x0;
894 * First dib section is 'inverted' ie color[0] is white, color[1] is black
897 mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
898 ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
899 old_bm = SelectObject(memdc, mono_ds);
901 /* black border, white interior */
902 Rectangle(memdc, 0, 0, 10, 10);
903 ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
904 ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
906 /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
908 memset(bits, 0, sizeof(bits));
909 bits[0] = 0xaa;
911 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
912 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
914 /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
916 pbmi->bmiColors[0].rgbRed = 0x0;
917 pbmi->bmiColors[0].rgbGreen = 0x0;
918 pbmi->bmiColors[0].rgbBlue = 0x0;
919 pbmi->bmiColors[1].rgbRed = 0xff;
920 pbmi->bmiColors[1].rgbGreen = 0xff;
921 pbmi->bmiColors[1].rgbBlue = 0xff;
923 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
924 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
926 SelectObject(memdc, old_bm);
927 DeleteObject(mono_ds);
930 * Next dib section is 'normal' ie color[0] is black, color[1] is white
933 pbmi->bmiColors[0].rgbRed = 0x0;
934 pbmi->bmiColors[0].rgbGreen = 0x0;
935 pbmi->bmiColors[0].rgbBlue = 0x0;
936 pbmi->bmiColors[1].rgbRed = 0xff;
937 pbmi->bmiColors[1].rgbGreen = 0xff;
938 pbmi->bmiColors[1].rgbBlue = 0xff;
940 mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
941 ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
942 old_bm = SelectObject(memdc, mono_ds);
944 /* black border, white interior */
945 Rectangle(memdc, 0, 0, 10, 10);
946 ok(ds_bits[0] == 0x00, "out_bits %02x\n", ds_bits[0]);
947 ok(ds_bits[4] == 0x7f, "out_bits %02x\n", ds_bits[4]);
949 /* SetDIBitsToDevice with a normal bmi -> normal dib section */
951 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
952 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
954 /* SetDIBitsToDevice with a inverted bmi -> normal dib section */
956 pbmi->bmiColors[0].rgbRed = 0xff;
957 pbmi->bmiColors[0].rgbGreen = 0xff;
958 pbmi->bmiColors[0].rgbBlue = 0xff;
959 pbmi->bmiColors[1].rgbRed = 0x0;
960 pbmi->bmiColors[1].rgbGreen = 0x0;
961 pbmi->bmiColors[1].rgbBlue = 0x0;
963 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
964 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
967 * Take that 'normal' dibsection and change its colour table to an 'inverted' one
970 pbmi->bmiColors[0].rgbRed = 0xff;
971 pbmi->bmiColors[0].rgbGreen = 0xff;
972 pbmi->bmiColors[0].rgbBlue = 0xff;
973 pbmi->bmiColors[1].rgbRed = 0x0;
974 pbmi->bmiColors[1].rgbGreen = 0x0;
975 pbmi->bmiColors[1].rgbBlue = 0x0;
976 num = SetDIBColorTable(memdc, 0, 2, pbmi->bmiColors);
977 ok(num == 2, "num = %d\n", num);
979 /* black border, white interior */
980 Rectangle(memdc, 0, 0, 10, 10);
981 ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
982 ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
984 /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
986 memset(bits, 0, sizeof(bits));
987 bits[0] = 0xaa;
989 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
990 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
992 /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
994 pbmi->bmiColors[0].rgbRed = 0x0;
995 pbmi->bmiColors[0].rgbGreen = 0x0;
996 pbmi->bmiColors[0].rgbBlue = 0x0;
997 pbmi->bmiColors[1].rgbRed = 0xff;
998 pbmi->bmiColors[1].rgbGreen = 0xff;
999 pbmi->bmiColors[1].rgbBlue = 0xff;
1001 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1002 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1004 SelectObject(memdc, old_bm);
1005 DeleteObject(mono_ds);
1008 * Now a dib section with a strange colour map just for fun. This behaves just like an inverted one.
1011 pbmi->bmiColors[0].rgbRed = 0xff;
1012 pbmi->bmiColors[0].rgbGreen = 0x0;
1013 pbmi->bmiColors[0].rgbBlue = 0x0;
1014 pbmi->bmiColors[1].rgbRed = 0xfe;
1015 pbmi->bmiColors[1].rgbGreen = 0x0;
1016 pbmi->bmiColors[1].rgbBlue = 0x0;
1018 mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
1019 ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
1020 old_bm = SelectObject(memdc, mono_ds);
1022 /* black border, white interior */
1023 Rectangle(memdc, 0, 0, 10, 10);
1024 ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
1025 ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
1027 /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
1029 pbmi->bmiColors[0].rgbRed = 0x0;
1030 pbmi->bmiColors[0].rgbGreen = 0x0;
1031 pbmi->bmiColors[0].rgbBlue = 0x0;
1032 pbmi->bmiColors[1].rgbRed = 0xff;
1033 pbmi->bmiColors[1].rgbGreen = 0xff;
1034 pbmi->bmiColors[1].rgbBlue = 0xff;
1036 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1037 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1039 /* SetDIBitsToDevice with a inverted bmi -> inverted dib section */
1041 pbmi->bmiColors[0].rgbRed = 0xff;
1042 pbmi->bmiColors[0].rgbGreen = 0xff;
1043 pbmi->bmiColors[0].rgbBlue = 0xff;
1044 pbmi->bmiColors[1].rgbRed = 0x0;
1045 pbmi->bmiColors[1].rgbGreen = 0x0;
1046 pbmi->bmiColors[1].rgbBlue = 0x0;
1048 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1049 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
1051 SelectObject(memdc, old_bm);
1052 DeleteObject(mono_ds);
1054 DeleteDC(memdc);
1055 ReleaseDC(0, hdc);
1058 static void test_bitmap(void)
1060 char buf[256], buf_cmp[256];
1061 HBITMAP hbmp, hbmp_old;
1062 HDC hdc;
1063 BITMAP bm;
1064 BITMAP bma[2];
1065 INT ret;
1067 hdc = CreateCompatibleDC(0);
1068 assert(hdc != 0);
1070 SetLastError(0xdeadbeef);
1071 hbmp = CreateBitmap(0x7ffffff, 1, 1, 1, NULL);
1072 if (!hbmp)
1074 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1075 GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1076 "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1078 else
1079 DeleteObject(hbmp);
1081 SetLastError(0xdeadbeef);
1082 hbmp = CreateBitmap(0x7ffffff, 9, 1, 1, NULL);
1083 if (!hbmp)
1085 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1086 GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1087 "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1089 else
1090 DeleteObject(hbmp);
1092 SetLastError(0xdeadbeef);
1093 hbmp = CreateBitmap(0x7ffffff + 1, 1, 1, 1, NULL);
1094 ok(!hbmp, "CreateBitmap should fail\n");
1095 if (!hbmp)
1096 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1097 "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1098 else
1099 DeleteObject(hbmp);
1101 hbmp = CreateBitmap(15, 15, 1, 1, NULL);
1102 assert(hbmp != NULL);
1104 ret = GetObject(hbmp, sizeof(bm), &bm);
1105 ok(ret == sizeof(bm), "wrong size %d\n", ret);
1107 ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1108 ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1109 ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1110 ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1111 ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1112 ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1113 ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1115 assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
1116 assert(sizeof(buf) == sizeof(buf_cmp));
1118 ret = GetBitmapBits(hbmp, 0, NULL);
1119 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1121 memset(buf_cmp, 0xAA, sizeof(buf_cmp));
1122 memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
1124 memset(buf, 0xAA, sizeof(buf));
1125 ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1126 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1127 ok(!memcmp(buf, buf_cmp, sizeof(buf)), "buffers do not match\n");
1129 hbmp_old = SelectObject(hdc, hbmp);
1131 ret = GetObject(hbmp, sizeof(bm), &bm);
1132 ok(ret == sizeof(bm), "wrong size %d\n", ret);
1134 ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1135 ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1136 ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1137 ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1138 ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1139 ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1140 ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1142 memset(buf, 0xAA, sizeof(buf));
1143 ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1144 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1145 ok(!memcmp(buf, buf_cmp, sizeof(buf)), "buffers do not match\n");
1147 hbmp_old = SelectObject(hdc, hbmp_old);
1148 ok(hbmp_old == hbmp, "wrong old bitmap %p\n", hbmp_old);
1150 /* test various buffer sizes for GetObject */
1151 ret = GetObject(hbmp, sizeof(*bma) * 2, bma);
1152 ok(ret == sizeof(*bma), "wrong size %d\n", ret);
1154 ret = GetObject(hbmp, sizeof(bm) / 2, &bm);
1155 ok(ret == 0, "%d != 0\n", ret);
1157 ret = GetObject(hbmp, 0, &bm);
1158 ok(ret == 0, "%d != 0\n", ret);
1160 ret = GetObject(hbmp, 1, &bm);
1161 ok(ret == 0, "%d != 0\n", ret);
1163 DeleteObject(hbmp);
1164 DeleteDC(hdc);
1167 static void test_bmBits(void)
1169 BYTE bits[4];
1170 HBITMAP hbmp;
1171 BITMAP bmp;
1173 memset(bits, 0, sizeof(bits));
1174 hbmp = CreateBitmap(2, 2, 1, 4, bits);
1175 ok(hbmp != NULL, "CreateBitmap failed\n");
1177 memset(&bmp, 0xFF, sizeof(bmp));
1178 ok(GetObject(hbmp, sizeof(bmp), &bmp) == sizeof(bmp),
1179 "GetObject failed or returned a wrong structure size\n");
1180 ok(!bmp.bmBits, "bmBits must be NULL for device-dependent bitmaps\n");
1182 DeleteObject(hbmp);
1185 static void test_GetDIBits_selected_DIB(UINT bpp)
1187 HBITMAP dib;
1188 BITMAPINFO * info;
1189 BITMAPINFO * info2;
1190 void * bits;
1191 void * bits2;
1192 UINT dib_size, dib32_size;
1193 DWORD pixel;
1194 HDC dib_dc, dc;
1195 HBITMAP old_bmp;
1196 BOOL equalContents;
1197 UINT i;
1198 int res;
1200 /* Create a DIB section with a color table */
1202 info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1203 info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1204 assert(info);
1205 assert(info2);
1207 info->bmiHeader.biSize = sizeof(info->bmiHeader);
1209 /* Choose width and height such that the row length (in bytes)
1210 is a multiple of 4 (makes things easier) */
1211 info->bmiHeader.biWidth = 32;
1212 info->bmiHeader.biHeight = 32;
1213 info->bmiHeader.biPlanes = 1;
1214 info->bmiHeader.biBitCount = bpp;
1215 info->bmiHeader.biCompression = BI_RGB;
1217 for (i=0; i < (1u << bpp); i++)
1219 BYTE c = i * (1 << (8 - bpp));
1220 info->bmiColors[i].rgbRed = c;
1221 info->bmiColors[i].rgbGreen = c;
1222 info->bmiColors[i].rgbBlue = c;
1223 info->bmiColors[i].rgbReserved = 0;
1226 dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
1227 assert(dib);
1228 dib_size = bpp * (info->bmiHeader.biWidth * info->bmiHeader.biHeight) / 8;
1229 dib32_size = 32 * (info->bmiHeader.biWidth * info->bmiHeader.biHeight) / 8;
1231 /* Set the bits of the DIB section */
1232 for (i=0; i < dib_size; i++)
1234 ((BYTE *)bits)[i] = i % 256;
1237 /* Select the DIB into a DC */
1238 dib_dc = CreateCompatibleDC(NULL);
1239 old_bmp = SelectObject(dib_dc, dib);
1240 dc = CreateCompatibleDC(NULL);
1241 bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dib32_size);
1242 assert(bits2);
1244 /* Copy the DIB attributes but not the color table */
1245 memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1247 res = GetDIBits(dc, dib, 0, info->bmiHeader.biHeight, bits2, info2, DIB_RGB_COLORS);
1248 ok(res, "GetDIBits failed\n");
1250 /* Compare the color table and the bits */
1251 equalContents = TRUE;
1252 for (i=0; i < (1u << bpp); i++)
1254 if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1255 || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1256 || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1257 || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1259 equalContents = FALSE;
1260 break;
1263 ok(equalContents, "GetDIBits with DIB selected in DC: Invalid DIB color table\n");
1265 equalContents = TRUE;
1266 for (i=0; i < dib_size / sizeof(DWORD); i++)
1268 if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1270 equalContents = FALSE;
1271 break;
1274 ok(equalContents, "GetDIBits with %d bpp DIB selected in DC: Invalid DIB bits\n",bpp);
1276 /* Map into a 32bit-DIB */
1277 info2->bmiHeader.biBitCount = 32;
1278 res = GetDIBits(dc, dib, 0, info->bmiHeader.biHeight, bits2, info2, DIB_RGB_COLORS);
1279 ok(res, "GetDIBits failed\n");
1281 /* Check if last pixel was set */
1282 pixel = ((DWORD *)bits2)[info->bmiHeader.biWidth * info->bmiHeader.biHeight - 1];
1283 ok(pixel != 0, "Pixel: 0x%08x\n", pixel);
1285 HeapFree(GetProcessHeap(), 0, bits2);
1286 DeleteDC(dc);
1288 SelectObject(dib_dc, old_bmp);
1289 DeleteDC(dib_dc);
1290 DeleteObject(dib);
1292 HeapFree(GetProcessHeap(), 0, info2);
1293 HeapFree(GetProcessHeap(), 0, info);
1296 static void test_GetDIBits_selected_DDB(BOOL monochrome)
1298 HBITMAP ddb;
1299 BITMAPINFO * info;
1300 BITMAPINFO * info2;
1301 void * bits;
1302 void * bits2;
1303 HDC ddb_dc, dc;
1304 HBITMAP old_bmp;
1305 BOOL equalContents;
1306 UINT width, height;
1307 UINT bpp;
1308 UINT i, j;
1309 int res;
1311 width = height = 16;
1313 /* Create a DDB (device-dependent bitmap) */
1314 if (monochrome)
1316 bpp = 1;
1317 ddb = CreateBitmap(width, height, 1, 1, NULL);
1319 else
1321 HDC screen_dc = GetDC(NULL);
1322 bpp = GetDeviceCaps(screen_dc, BITSPIXEL) * GetDeviceCaps(screen_dc, PLANES);
1323 ddb = CreateCompatibleBitmap(screen_dc, width, height);
1324 ReleaseDC(NULL, screen_dc);
1327 /* Set the pixels */
1328 ddb_dc = CreateCompatibleDC(NULL);
1329 old_bmp = SelectObject(ddb_dc, ddb);
1330 for (i = 0; i < width; i++)
1332 for (j=0; j < height; j++)
1334 BYTE c = (i * width + j) % 256;
1335 SetPixelV(ddb_dc, i, j, RGB(c, c, c));
1338 SelectObject(ddb_dc, old_bmp);
1340 info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1341 info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1342 assert(info);
1343 assert(info2);
1345 info->bmiHeader.biSize = sizeof(info->bmiHeader);
1346 info->bmiHeader.biWidth = width;
1347 info->bmiHeader.biHeight = height;
1348 info->bmiHeader.biPlanes = 1;
1349 info->bmiHeader.biBitCount = bpp;
1350 info->bmiHeader.biCompression = BI_RGB;
1352 dc = CreateCompatibleDC(NULL);
1354 /* Fill in biSizeImage */
1355 GetDIBits(dc, ddb, 0, height, NULL, info, DIB_RGB_COLORS);
1356 ok(info->bmiHeader.biSizeImage != 0, "GetDIBits failed to get the DIB attributes\n");
1358 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1359 bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1360 assert(bits);
1361 assert(bits2);
1363 /* Get the bits */
1364 res = GetDIBits(dc, ddb, 0, height, bits, info, DIB_RGB_COLORS);
1365 ok(res, "GetDIBits failed\n");
1367 /* Copy the DIB attributes but not the color table */
1368 memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1370 /* Select the DDB into another DC */
1371 old_bmp = SelectObject(ddb_dc, ddb);
1373 /* Get the bits */
1374 res = GetDIBits(dc, ddb, 0, height, bits2, info2, DIB_RGB_COLORS);
1375 ok(res, "GetDIBits failed\n");
1377 /* Compare the color table and the bits */
1378 if (bpp <= 8)
1380 equalContents = TRUE;
1381 for (i=0; i < (1u << bpp); i++)
1383 if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1384 || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1385 || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1386 || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1388 equalContents = FALSE;
1389 break;
1392 ok(equalContents, "GetDIBits with DDB selected in DC: Got a different color table\n");
1395 equalContents = TRUE;
1396 for (i=0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++)
1398 if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1400 equalContents = FALSE;
1403 ok(equalContents, "GetDIBits with DDB selected in DC: Got different DIB bits\n");
1405 /* Test the palette */
1406 equalContents = TRUE;
1407 if (info2->bmiHeader.biBitCount <= 8)
1409 WORD *colors = (WORD*)info2->bmiColors;
1411 /* Get the palette indices */
1412 res = GetDIBits(dc, ddb, 0, 0, NULL, info2, DIB_PAL_COLORS);
1413 ok(res, "GetDIBits failed\n");
1415 for (i=0;i < 1 << info->bmiHeader.biSizeImage; i++)
1417 if (colors[i] != i)
1419 equalContents = FALSE;
1420 break;
1425 ok(equalContents, "GetDIBits with DDB selected in DC: non 1:1 palette indices\n");
1427 HeapFree(GetProcessHeap(), 0, bits2);
1428 HeapFree(GetProcessHeap(), 0, bits);
1429 DeleteDC(dc);
1431 SelectObject(ddb_dc, old_bmp);
1432 DeleteDC(ddb_dc);
1433 DeleteObject(ddb);
1435 HeapFree(GetProcessHeap(), 0, info2);
1436 HeapFree(GetProcessHeap(), 0, info);
1439 static void test_GetDIBits(void)
1441 /* 2-bytes aligned 1-bit bitmap data: 16x16 */
1442 static const BYTE bmp_bits_1[16 * 2] =
1444 0xff,0xff, 0,0, 0xff,0xff, 0,0,
1445 0xff,0xff, 0,0, 0xff,0xff, 0,0,
1446 0xff,0xff, 0,0, 0xff,0xff, 0,0,
1447 0xff,0xff, 0,0, 0xff,0xff, 0,0
1449 /* 4-bytes aligned 1-bit DIB data: 16x16 */
1450 static const BYTE dib_bits_1[16 * 4] =
1452 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1453 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1454 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1455 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0
1457 /* 2-bytes aligned 24-bit bitmap data: 16x16 */
1458 static const BYTE bmp_bits_24[16 * 16*3] =
1460 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1461 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1462 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1463 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1464 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1465 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1466 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1467 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1468 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1469 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1470 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1471 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1472 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1473 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1474 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1475 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1476 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1477 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1478 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1479 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1480 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1481 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1482 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1483 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1484 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1485 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1486 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1487 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1488 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1489 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1490 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1491 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1493 /* 4-bytes aligned 24-bit DIB data: 16x16 */
1494 static const BYTE dib_bits_24[16 * 16*3] =
1496 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1497 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1498 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1499 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1500 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1501 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1502 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1503 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1504 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1505 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1506 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1507 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1508 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1509 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1510 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1511 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1512 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1513 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1514 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1515 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1516 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1517 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1518 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1519 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1520 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1521 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1522 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1523 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1524 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1525 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1526 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1527 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
1529 HBITMAP hbmp;
1530 BITMAP bm;
1531 HDC hdc;
1532 int i, bytes, lines;
1533 BYTE buf[1024];
1534 char bi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
1535 BITMAPINFO *bi = (BITMAPINFO *)bi_buf;
1537 hdc = GetDC(0);
1539 /* 1-bit source bitmap data */
1540 hbmp = CreateBitmap(16, 16, 1, 1, bmp_bits_1);
1541 ok(hbmp != 0, "CreateBitmap failed\n");
1543 memset(&bm, 0xAA, sizeof(bm));
1544 bytes = GetObject(hbmp, sizeof(bm), &bm);
1545 ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1546 ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
1547 ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1548 ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1549 ok(bm.bmWidthBytes == 2, "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1550 ok(bm.bmPlanes == 1, "wrong bmPlanes %u\n", bm.bmPlanes);
1551 ok(bm.bmBitsPixel == 1, "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1552 ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1554 bytes = GetBitmapBits(hbmp, 0, NULL);
1555 ok(bytes == sizeof(bmp_bits_1), "expected 16*2 got %d bytes\n", bytes);
1556 bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1557 ok(bytes == sizeof(bmp_bits_1), "expected 16*2 got %d bytes\n", bytes);
1558 ok(!memcmp(buf, bmp_bits_1, sizeof(bmp_bits_1)), "bitmap bits don't match\n");
1560 /* retrieve 1-bit DIB data */
1561 memset(bi, 0, sizeof(*bi));
1562 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1563 bi->bmiHeader.biWidth = bm.bmWidth;
1564 bi->bmiHeader.biHeight = bm.bmHeight;
1565 bi->bmiHeader.biPlanes = 1;
1566 bi->bmiHeader.biBitCount = 1;
1567 bi->bmiHeader.biCompression = BI_RGB;
1568 bi->bmiHeader.biSizeImage = 0;
1569 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1570 SetLastError(0xdeadbeef);
1571 lines = GetDIBits(0, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1572 ok(lines == 0, "GetDIBits copied %d lines with hdc = 0\n", lines);
1573 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
1574 broken(GetLastError() == 0xdeadbeef), /* winnt */
1575 "wrong error %u\n", GetLastError());
1576 ok(bi->bmiHeader.biSizeImage == 0, "expected 0, got %u\n", bi->bmiHeader.biSizeImage);
1578 memset(buf, 0xAA, sizeof(buf));
1579 SetLastError(0xdeadbeef);
1580 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1581 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1582 lines, bm.bmHeight, GetLastError());
1583 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1), "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1585 /* the color table consists of black and white */
1586 ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1587 bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1588 "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1589 bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1590 bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1591 ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1592 bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1593 "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1594 bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1595 bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1596 for (i = 2; i < 256; i++)
1598 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1599 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1600 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1601 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1602 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1605 /* returned bits are DWORD aligned and upside down */
1606 ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)), "DIB bits don't match\n");
1608 /* Test the palette indices */
1609 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1610 SetLastError(0xdeadbeef);
1611 lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1612 ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1613 ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1614 for (i = 2; i < 256; i++)
1615 ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[1]);
1617 /* retrieve 24-bit DIB data */
1618 memset(bi, 0, sizeof(*bi));
1619 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1620 bi->bmiHeader.biWidth = bm.bmWidth;
1621 bi->bmiHeader.biHeight = bm.bmHeight;
1622 bi->bmiHeader.biPlanes = 1;
1623 bi->bmiHeader.biBitCount = 24;
1624 bi->bmiHeader.biCompression = BI_RGB;
1625 bi->bmiHeader.biSizeImage = 0;
1626 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1627 memset(buf, 0xAA, sizeof(buf));
1628 SetLastError(0xdeadbeef);
1629 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1630 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1631 lines, bm.bmHeight, GetLastError());
1632 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24), "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1634 /* the color table doesn't exist for 24-bit images */
1635 for (i = 0; i < 256; i++)
1637 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1638 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1639 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1640 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1641 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1644 /* returned bits are DWORD aligned and upside down */
1645 ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1646 DeleteObject(hbmp);
1648 /* 24-bit source bitmap data */
1649 hbmp = CreateCompatibleBitmap(hdc, 16, 16);
1650 ok(hbmp != 0, "CreateBitmap failed\n");
1651 SetLastError(0xdeadbeef);
1652 bi->bmiHeader.biHeight = -bm.bmHeight; /* indicate bottom-up data */
1653 lines = SetDIBits(hdc, hbmp, 0, bm.bmHeight, bmp_bits_24, bi, DIB_RGB_COLORS);
1654 ok(lines == bm.bmHeight, "SetDIBits copied %d lines of %d, error %u\n",
1655 lines, bm.bmHeight, GetLastError());
1657 memset(&bm, 0xAA, sizeof(bm));
1658 bytes = GetObject(hbmp, sizeof(bm), &bm);
1659 ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1660 ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
1661 ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1662 ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1663 ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1664 ok(bm.bmPlanes == GetDeviceCaps(hdc, PLANES), "wrong bmPlanes %u\n", bm.bmPlanes);
1665 ok(bm.bmBitsPixel == GetDeviceCaps(hdc, BITSPIXEL), "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1666 ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1668 bytes = GetBitmapBits(hbmp, 0, NULL);
1669 ok(bytes == bm.bmWidthBytes * bm.bmHeight, "expected %d got %d bytes\n", bm.bmWidthBytes * bm.bmHeight, bytes);
1670 bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1671 ok(bytes == bm.bmWidthBytes * bm.bmHeight, "expected %d got %d bytes\n",
1672 bm.bmWidthBytes * bm.bmHeight, bytes);
1674 /* retrieve 1-bit DIB data */
1675 memset(bi, 0, sizeof(*bi));
1676 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1677 bi->bmiHeader.biWidth = bm.bmWidth;
1678 bi->bmiHeader.biHeight = bm.bmHeight;
1679 bi->bmiHeader.biPlanes = 1;
1680 bi->bmiHeader.biBitCount = 1;
1681 bi->bmiHeader.biCompression = BI_RGB;
1682 bi->bmiHeader.biSizeImage = 0;
1683 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1684 memset(buf, 0xAA, sizeof(buf));
1685 SetLastError(0xdeadbeef);
1686 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1687 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1688 lines, bm.bmHeight, GetLastError());
1689 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1), "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1691 /* the color table consists of black and white */
1692 ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1693 bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1694 "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1695 bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1696 bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1697 ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1698 bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1699 "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1700 bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1701 bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1702 for (i = 2; i < 256; i++)
1704 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1705 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1706 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1707 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1708 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1711 /* returned bits are DWORD aligned and upside down */
1712 todo_wine
1713 ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)), "DIB bits don't match\n");
1715 /* Test the palette indices */
1716 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1717 SetLastError(0xdeadbeef);
1718 lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1719 ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1720 ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1721 for (i = 2; i < 256; i++)
1722 ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[i]);
1724 /* retrieve 24-bit DIB data */
1725 memset(bi, 0, sizeof(*bi));
1726 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1727 bi->bmiHeader.biWidth = bm.bmWidth;
1728 bi->bmiHeader.biHeight = bm.bmHeight;
1729 bi->bmiHeader.biPlanes = 1;
1730 bi->bmiHeader.biBitCount = 24;
1731 bi->bmiHeader.biCompression = BI_RGB;
1732 bi->bmiHeader.biSizeImage = 0;
1733 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1734 memset(buf, 0xAA, sizeof(buf));
1735 SetLastError(0xdeadbeef);
1736 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1737 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1738 lines, bm.bmHeight, GetLastError());
1739 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24), "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1741 /* the color table doesn't exist for 24-bit images */
1742 for (i = 0; i < 256; i++)
1744 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1745 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1746 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1747 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1748 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1751 /* returned bits are DWORD aligned and upside down */
1752 ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1753 DeleteObject(hbmp);
1755 ReleaseDC(0, hdc);
1758 static void test_GetDIBits_BI_BITFIELDS(void)
1760 /* Try a screen resolution detection technique
1761 * from the September 1999 issue of Windows Developer's Journal
1762 * which seems to be in widespread use.
1763 * http://www.lesher.ws/highcolor.html
1764 * http://www.lesher.ws/vidfmt.c
1765 * It hinges on being able to retrieve the bitmaps
1766 * for the three primary colors in non-paletted 16 bit mode.
1768 char dibinfo_buf[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
1769 DWORD bits[32];
1770 LPBITMAPINFO dibinfo = (LPBITMAPINFO) dibinfo_buf;
1771 DWORD *bitmasks = (DWORD *)dibinfo->bmiColors;
1772 HDC hdc;
1773 HBITMAP hbm;
1774 int ret;
1775 void *ptr;
1777 memset(dibinfo, 0, sizeof(dibinfo_buf));
1778 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1780 hdc = GetDC(NULL);
1781 ok(hdc != NULL, "GetDC failed?\n");
1782 hbm = CreateCompatibleBitmap(hdc, 1, 1);
1783 ok(hbm != NULL, "CreateCompatibleBitmap failed?\n");
1785 /* Call GetDIBits to fill in bmiHeader. */
1786 ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1787 ok(ret == 1, "GetDIBits failed\n");
1788 if (dibinfo->bmiHeader.biBitCount > 8)
1790 ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS ||
1791 broken( dibinfo->bmiHeader.biCompression == BI_RGB ), /* nt4 sp3 */
1792 "compression is %u (%d bpp)\n", dibinfo->bmiHeader.biCompression, dibinfo->bmiHeader.biBitCount );
1794 if (dibinfo->bmiHeader.biCompression == BI_BITFIELDS)
1796 ok( !bitmasks[0], "red mask is set\n" );
1797 ok( !bitmasks[1], "green mask is set\n" );
1798 ok( !bitmasks[2], "blue mask is set\n" );
1800 /* test with NULL bits pointer and correct bpp */
1801 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1802 ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1803 ok(ret == 1, "GetDIBits failed\n");
1805 ok( bitmasks[0] != 0, "red mask is not set\n" );
1806 ok( bitmasks[1] != 0, "green mask is not set\n" );
1807 ok( bitmasks[2] != 0, "blue mask is not set\n" );
1808 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1810 /* test with valid bits pointer */
1811 memset(dibinfo, 0, sizeof(dibinfo_buf));
1812 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1813 ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1814 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1815 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1816 ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1817 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1819 ok( bitmasks[0] != 0, "red mask is not set\n" );
1820 ok( bitmasks[1] != 0, "green mask is not set\n" );
1821 ok( bitmasks[2] != 0, "blue mask is not set\n" );
1822 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1824 /* now with bits and 0 lines */
1825 memset(dibinfo, 0, sizeof(dibinfo_buf));
1826 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1827 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1828 SetLastError(0xdeadbeef);
1829 ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1830 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1832 ok( !bitmasks[0], "red mask is set\n" );
1833 ok( !bitmasks[1], "green mask is set\n" );
1834 ok( !bitmasks[2], "blue mask is set\n" );
1835 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1837 memset(bitmasks, 0, 3*sizeof(DWORD));
1838 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1839 ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1840 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1842 ok( bitmasks[0] != 0, "red mask is not set\n" );
1843 ok( bitmasks[1] != 0, "green mask is not set\n" );
1844 ok( bitmasks[2] != 0, "blue mask is not set\n" );
1845 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1848 else skip("bitmap in colortable mode, skipping BI_BITFIELDS tests\n");
1850 DeleteObject(hbm);
1852 /* same thing now with a 32-bpp DIB section */
1854 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1855 dibinfo->bmiHeader.biWidth = 1;
1856 dibinfo->bmiHeader.biHeight = 1;
1857 dibinfo->bmiHeader.biPlanes = 1;
1858 dibinfo->bmiHeader.biBitCount = 32;
1859 dibinfo->bmiHeader.biCompression = BI_RGB;
1860 dibinfo->bmiHeader.biSizeImage = 0;
1861 dibinfo->bmiHeader.biXPelsPerMeter = 0;
1862 dibinfo->bmiHeader.biYPelsPerMeter = 0;
1863 dibinfo->bmiHeader.biClrUsed = 0;
1864 dibinfo->bmiHeader.biClrImportant = 0;
1865 bitmasks[0] = 0x0000ff;
1866 bitmasks[1] = 0x00ff00;
1867 bitmasks[2] = 0xff0000;
1868 hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
1869 ok( hbm != 0, "failed to create bitmap\n" );
1871 memset(dibinfo, 0, sizeof(dibinfo_buf));
1872 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1873 ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
1874 ok(ret == 1, "GetDIBits failed\n");
1875 ok( dibinfo->bmiHeader.biBitCount == 32, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
1877 ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS ||
1878 broken( dibinfo->bmiHeader.biCompression == BI_RGB ), /* nt4 sp3 */
1879 "compression is %u\n", dibinfo->bmiHeader.biCompression );
1880 ok( !bitmasks[0], "red mask is set\n" );
1881 ok( !bitmasks[1], "green mask is set\n" );
1882 ok( !bitmasks[2], "blue mask is set\n" );
1884 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1885 ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1886 ok(ret == 1, "GetDIBits failed\n");
1887 ok( dibinfo->bmiHeader.biBitCount == 32, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
1888 ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS ||
1889 broken( dibinfo->bmiHeader.biCompression == BI_RGB ), /* nt4 sp3 */
1890 "compression is %u\n", dibinfo->bmiHeader.biCompression );
1891 if (dibinfo->bmiHeader.biCompression == BI_BITFIELDS)
1893 ok( bitmasks[0] == 0xff0000, "wrong red mask %08x\n", bitmasks[0] );
1894 ok( bitmasks[1] == 0x00ff00, "wrong green mask %08x\n", bitmasks[1] );
1895 ok( bitmasks[2] == 0x0000ff, "wrong blue mask %08x\n", bitmasks[2] );
1897 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1899 DeleteObject(hbm);
1901 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1902 dibinfo->bmiHeader.biWidth = 1;
1903 dibinfo->bmiHeader.biHeight = 1;
1904 dibinfo->bmiHeader.biPlanes = 1;
1905 dibinfo->bmiHeader.biBitCount = 32;
1906 dibinfo->bmiHeader.biCompression = BI_BITFIELDS;
1907 dibinfo->bmiHeader.biSizeImage = 0;
1908 dibinfo->bmiHeader.biXPelsPerMeter = 0;
1909 dibinfo->bmiHeader.biYPelsPerMeter = 0;
1910 dibinfo->bmiHeader.biClrUsed = 0;
1911 dibinfo->bmiHeader.biClrImportant = 0;
1912 bitmasks[0] = 0x0000ff;
1913 bitmasks[1] = 0x00ff00;
1914 bitmasks[2] = 0xff0000;
1915 hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
1916 ok( hbm != 0, "failed to create bitmap\n" );
1918 if (hbm)
1920 memset(dibinfo, 0, sizeof(dibinfo_buf));
1921 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1922 ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
1923 ok(ret == 1, "GetDIBits failed\n");
1925 ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS,
1926 "compression is %u\n", dibinfo->bmiHeader.biCompression );
1927 ok( !bitmasks[0], "red mask is set\n" );
1928 ok( !bitmasks[1], "green mask is set\n" );
1929 ok( !bitmasks[2], "blue mask is set\n" );
1931 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1932 ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1933 ok(ret == 1, "GetDIBits failed\n");
1934 ok( bitmasks[0] == 0x0000ff, "wrong red mask %08x\n", bitmasks[0] );
1935 ok( bitmasks[1] == 0x00ff00, "wrong green mask %08x\n", bitmasks[1] );
1936 ok( bitmasks[2] == 0xff0000, "wrong blue mask %08x\n", bitmasks[2] );
1937 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1939 DeleteObject(hbm);
1942 /* 24-bpp DIB sections don't have bitfields */
1944 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1945 dibinfo->bmiHeader.biWidth = 1;
1946 dibinfo->bmiHeader.biHeight = 1;
1947 dibinfo->bmiHeader.biPlanes = 1;
1948 dibinfo->bmiHeader.biBitCount = 24;
1949 dibinfo->bmiHeader.biCompression = BI_BITFIELDS;
1950 dibinfo->bmiHeader.biSizeImage = 0;
1951 dibinfo->bmiHeader.biXPelsPerMeter = 0;
1952 dibinfo->bmiHeader.biYPelsPerMeter = 0;
1953 dibinfo->bmiHeader.biClrUsed = 0;
1954 dibinfo->bmiHeader.biClrImportant = 0;
1955 hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
1956 ok( hbm == 0, "creating 24-bpp BI_BITFIELDS dibsection should fail\n" );
1957 dibinfo->bmiHeader.biCompression = BI_RGB;
1958 hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
1959 ok( hbm != 0, "failed to create bitmap\n" );
1961 memset(dibinfo, 0, sizeof(dibinfo_buf));
1962 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1963 ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
1964 ok(ret == 1, "GetDIBits failed\n");
1965 ok( dibinfo->bmiHeader.biBitCount == 24, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
1967 ok( dibinfo->bmiHeader.biCompression == BI_RGB,
1968 "compression is %u\n", dibinfo->bmiHeader.biCompression );
1969 ok( !bitmasks[0], "red mask is set\n" );
1970 ok( !bitmasks[1], "green mask is set\n" );
1971 ok( !bitmasks[2], "blue mask is set\n" );
1973 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1974 ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1975 ok(ret == 1, "GetDIBits failed\n");
1976 ok( dibinfo->bmiHeader.biBitCount == 24, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
1977 ok( !bitmasks[0], "red mask is set\n" );
1978 ok( !bitmasks[1], "green mask is set\n" );
1979 ok( !bitmasks[2], "blue mask is set\n" );
1980 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1982 DeleteObject(hbm);
1983 ReleaseDC(NULL, hdc);
1986 static void test_select_object(void)
1988 HDC hdc;
1989 HBITMAP hbm, hbm_old;
1990 INT planes, bpp, i;
1991 DWORD depths[] = {8, 15, 16, 24, 32};
1992 BITMAP bm;
1993 DWORD bytes;
1995 hdc = GetDC(0);
1996 ok(hdc != 0, "GetDC(0) failed\n");
1997 hbm = CreateCompatibleBitmap(hdc, 10, 10);
1998 ok(hbm != 0, "CreateCompatibleBitmap failed\n");
2000 hbm_old = SelectObject(hdc, hbm);
2001 ok(hbm_old == 0, "SelectObject should fail\n");
2003 DeleteObject(hbm);
2004 ReleaseDC(0, hdc);
2006 hdc = CreateCompatibleDC(0);
2007 ok(hdc != 0, "GetDC(0) failed\n");
2008 hbm = CreateCompatibleBitmap(hdc, 10, 10);
2009 ok(hbm != 0, "CreateCompatibleBitmap failed\n");
2011 hbm_old = SelectObject(hdc, hbm);
2012 ok(hbm_old != 0, "SelectObject failed\n");
2013 hbm_old = SelectObject(hdc, hbm_old);
2014 ok(hbm_old == hbm, "SelectObject failed\n");
2016 DeleteObject(hbm);
2018 /* test an 1-bpp bitmap */
2019 planes = GetDeviceCaps(hdc, PLANES);
2020 bpp = 1;
2022 hbm = CreateBitmap(10, 10, planes, bpp, NULL);
2023 ok(hbm != 0, "CreateBitmap failed\n");
2025 hbm_old = SelectObject(hdc, hbm);
2026 ok(hbm_old != 0, "SelectObject failed\n");
2027 hbm_old = SelectObject(hdc, hbm_old);
2028 ok(hbm_old == hbm, "SelectObject failed\n");
2030 DeleteObject(hbm);
2032 for(i = 0; i < sizeof(depths)/sizeof(depths[0]); i++) {
2033 /* test a color bitmap to dc bpp matching */
2034 planes = GetDeviceCaps(hdc, PLANES);
2035 bpp = GetDeviceCaps(hdc, BITSPIXEL);
2037 hbm = CreateBitmap(10, 10, planes, depths[i], NULL);
2038 ok(hbm != 0, "CreateBitmap failed\n");
2040 hbm_old = SelectObject(hdc, hbm);
2041 if(depths[i] == bpp ||
2042 (bpp == 16 && depths[i] == 15) /* 16 and 15 bpp are compatible */
2044 ok(hbm_old != 0, "SelectObject failed, BITSPIXEL: %d, created depth: %d\n", bpp, depths[i]);
2045 SelectObject(hdc, hbm_old);
2046 } else {
2047 ok(hbm_old == 0, "SelectObject should fail. BITSPIXELS: %d, created depth: %d\n", bpp, depths[i]);
2050 memset(&bm, 0xAA, sizeof(bm));
2051 bytes = GetObject(hbm, sizeof(bm), &bm);
2052 ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
2053 ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
2054 ok(bm.bmWidth == 10, "wrong bmWidth %d\n", bm.bmWidth);
2055 ok(bm.bmHeight == 10, "wrong bmHeight %d\n", bm.bmHeight);
2056 ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
2057 ok(bm.bmPlanes == planes, "wrong bmPlanes %u\n", bm.bmPlanes);
2058 if(depths[i] == 15) {
2059 ok(bm.bmBitsPixel == 16, "wrong bmBitsPixel %d(15 bpp special)\n", bm.bmBitsPixel);
2060 } else {
2061 ok(bm.bmBitsPixel == depths[i], "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
2063 ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
2065 DeleteObject(hbm);
2068 DeleteDC(hdc);
2071 static void test_mono_1x1_bmp_dbg(HBITMAP hbmp, int line)
2073 INT ret;
2074 BITMAP bm;
2076 ret = GetObjectType(hbmp);
2077 ok_(__FILE__, line)(ret == OBJ_BITMAP, "the object %p is not bitmap\n", hbmp);
2079 ret = GetObject(hbmp, 0, 0);
2080 ok_(__FILE__, line)(ret == sizeof(BITMAP), "object size %d\n", ret);
2082 memset(&bm, 0xDA, sizeof(bm));
2083 SetLastError(0xdeadbeef);
2084 ret = GetObject(hbmp, sizeof(bm), &bm);
2085 if (!ret) /* XP, only for curObj2 */ return;
2086 ok_(__FILE__, line)(ret == sizeof(BITMAP), "GetObject returned %d, error %u\n", ret, GetLastError());
2087 ok_(__FILE__, line)(bm.bmType == 0, "wrong bmType, expected 0 got %d\n", bm.bmType);
2088 ok_(__FILE__, line)(bm.bmWidth == 1, "wrong bmWidth, expected 1 got %d\n", bm.bmWidth);
2089 ok_(__FILE__, line)(bm.bmHeight == 1, "wrong bmHeight, expected 1 got %d\n", bm.bmHeight);
2090 ok_(__FILE__, line)(bm.bmWidthBytes == 2, "wrong bmWidthBytes, expected 2 got %d\n", bm.bmWidthBytes);
2091 ok_(__FILE__, line)(bm.bmPlanes == 1, "wrong bmPlanes, expected 1 got %u\n", bm.bmPlanes);
2092 ok_(__FILE__, line)(bm.bmBitsPixel == 1, "wrong bmBitsPixel, expected 1 got %d\n", bm.bmBitsPixel);
2093 ok_(__FILE__, line)(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
2096 #define test_mono_1x1_bmp(a) test_mono_1x1_bmp_dbg((a), __LINE__)
2098 static void test_CreateBitmap(void)
2100 BITMAP bmp;
2101 HDC screenDC = GetDC(0);
2102 HDC hdc = CreateCompatibleDC(screenDC);
2103 UINT i, expect = 0;
2105 /* all of these are the stock monochrome bitmap */
2106 HBITMAP bm = CreateCompatibleBitmap(hdc, 0, 0);
2107 HBITMAP bm1 = CreateCompatibleBitmap(screenDC, 0, 0);
2108 HBITMAP bm4 = CreateBitmap(0, 1, 0, 0, 0);
2109 HBITMAP bm5 = CreateDiscardableBitmap(hdc, 0, 0);
2110 HBITMAP curObj1 = GetCurrentObject(hdc, OBJ_BITMAP);
2111 HBITMAP curObj2 = GetCurrentObject(screenDC, OBJ_BITMAP);
2113 /* these 2 are not the stock monochrome bitmap */
2114 HBITMAP bm2 = CreateCompatibleBitmap(hdc, 1, 1);
2115 HBITMAP bm3 = CreateBitmap(1, 1, 1, 1, 0);
2117 HBITMAP old1 = SelectObject(hdc, bm2);
2118 HBITMAP old2 = SelectObject(screenDC, bm3);
2119 SelectObject(hdc, old1);
2120 SelectObject(screenDC, old2);
2122 ok(bm == bm1 && bm == bm4 && bm == bm5 && bm == curObj1 && bm == old1,
2123 "0: %p, 1: %p, 4: %p, 5: %p, curObj1 %p, old1 %p\n",
2124 bm, bm1, bm4, bm5, curObj1, old1);
2125 ok(bm != bm2 && bm != bm3, "0: %p, 2: %p, 3: %p\n", bm, bm2, bm3);
2126 todo_wine
2127 ok(bm != curObj2, "0: %p, curObj2 %p\n", bm, curObj2);
2128 ok(old2 == 0, "old2 %p\n", old2);
2130 test_mono_1x1_bmp(bm);
2131 test_mono_1x1_bmp(bm1);
2132 test_mono_1x1_bmp(bm2);
2133 test_mono_1x1_bmp(bm3);
2134 test_mono_1x1_bmp(bm4);
2135 test_mono_1x1_bmp(bm5);
2136 test_mono_1x1_bmp(old1);
2137 test_mono_1x1_bmp(curObj1);
2139 DeleteObject(bm);
2140 DeleteObject(bm1);
2141 DeleteObject(bm2);
2142 DeleteObject(bm3);
2143 DeleteObject(bm4);
2144 DeleteObject(bm5);
2146 DeleteDC(hdc);
2147 ReleaseDC(0, screenDC);
2149 /* show that Windows ignores the provided bm.bmWidthBytes */
2150 bmp.bmType = 0;
2151 bmp.bmWidth = 1;
2152 bmp.bmHeight = 1;
2153 bmp.bmWidthBytes = 28;
2154 bmp.bmPlanes = 1;
2155 bmp.bmBitsPixel = 1;
2156 bmp.bmBits = NULL;
2157 bm = CreateBitmapIndirect(&bmp);
2158 ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2159 test_mono_1x1_bmp(bm);
2160 DeleteObject(bm);
2162 /* Test how the bmBitsPixel field is treated */
2163 for(i = 1; i <= 33; i++) {
2164 bmp.bmType = 0;
2165 bmp.bmWidth = 1;
2166 bmp.bmHeight = 1;
2167 bmp.bmWidthBytes = 28;
2168 bmp.bmPlanes = 1;
2169 bmp.bmBitsPixel = i;
2170 bmp.bmBits = NULL;
2171 SetLastError(0xdeadbeef);
2172 bm = CreateBitmapIndirect(&bmp);
2173 if(i > 32) {
2174 DWORD error = GetLastError();
2175 ok(bm == 0, "CreateBitmapIndirect for %d bpp succeeded\n", i);
2176 ok(error == ERROR_INVALID_PARAMETER, "Got error %d, expected ERROR_INVALID_PARAMETER\n", error);
2177 DeleteObject(bm);
2178 continue;
2180 ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2181 GetObject(bm, sizeof(bmp), &bmp);
2182 if(i == 1) {
2183 expect = 1;
2184 } else if(i <= 4) {
2185 expect = 4;
2186 } else if(i <= 8) {
2187 expect = 8;
2188 } else if(i <= 16) {
2189 expect = 16;
2190 } else if(i <= 24) {
2191 expect = 24;
2192 } else if(i <= 32) {
2193 expect = 32;
2195 ok(bmp.bmBitsPixel == expect, "CreateBitmapIndirect for a %d bpp bitmap created a %d bpp bitmap, expected %d\n",
2196 i, bmp.bmBitsPixel, expect);
2197 DeleteObject(bm);
2201 static void test_bitmapinfoheadersize(void)
2203 HBITMAP hdib;
2204 BITMAPINFO bmi;
2205 BITMAPCOREINFO bci;
2206 HDC hdc = GetDC(0);
2208 memset(&bmi, 0, sizeof(BITMAPINFO));
2209 bmi.bmiHeader.biHeight = 100;
2210 bmi.bmiHeader.biWidth = 512;
2211 bmi.bmiHeader.biBitCount = 24;
2212 bmi.bmiHeader.biPlanes = 1;
2214 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER) - 1;
2216 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2217 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2219 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2221 SetLastError(0xdeadbeef);
2222 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2223 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2224 DeleteObject(hdib);
2226 bmi.bmiHeader.biSize++;
2228 SetLastError(0xdeadbeef);
2229 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2230 ok(hdib != NULL ||
2231 broken(!hdib), /* Win98, WinMe */
2232 "CreateDIBSection error %d\n", GetLastError());
2233 DeleteObject(hdib);
2235 bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
2237 SetLastError(0xdeadbeef);
2238 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2239 ok(hdib != NULL ||
2240 broken(!hdib), /* Win98, WinMe */
2241 "CreateDIBSection error %d\n", GetLastError());
2242 DeleteObject(hdib);
2244 bmi.bmiHeader.biSize++;
2246 SetLastError(0xdeadbeef);
2247 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2248 ok(hdib != NULL ||
2249 broken(!hdib), /* Win98, WinMe */
2250 "CreateDIBSection error %d\n", GetLastError());
2251 DeleteObject(hdib);
2253 bmi.bmiHeader.biSize = sizeof(BITMAPV4HEADER);
2255 SetLastError(0xdeadbeef);
2256 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2257 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2258 DeleteObject(hdib);
2260 bmi.bmiHeader.biSize = sizeof(BITMAPV5HEADER);
2262 SetLastError(0xdeadbeef);
2263 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2264 ok(hdib != NULL ||
2265 broken(!hdib), /* Win95 */
2266 "CreateDIBSection error %d\n", GetLastError());
2267 DeleteObject(hdib);
2269 memset(&bci, 0, sizeof(BITMAPCOREINFO));
2270 bci.bmciHeader.bcHeight = 100;
2271 bci.bmciHeader.bcWidth = 512;
2272 bci.bmciHeader.bcBitCount = 24;
2273 bci.bmciHeader.bcPlanes = 1;
2275 bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER) - 1;
2277 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2278 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2280 bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
2282 SetLastError(0xdeadbeef);
2283 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2284 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2285 DeleteObject(hdib);
2287 bci.bmciHeader.bcSize++;
2289 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2290 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2292 bci.bmciHeader.bcSize = sizeof(BITMAPCOREINFO);
2294 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2295 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2297 ReleaseDC(0, hdc);
2300 static void test_get16dibits(void)
2302 BYTE bits[4 * (16 / sizeof(BYTE))];
2303 HBITMAP hbmp;
2304 HDC screen_dc = GetDC(NULL);
2305 int ret;
2306 BITMAPINFO * info;
2307 int info_len = sizeof(BITMAPINFOHEADER) + 1024;
2308 BYTE *p;
2309 int overwritten_bytes = 0;
2311 memset(bits, 0, sizeof(bits));
2312 hbmp = CreateBitmap(2, 2, 1, 16, bits);
2313 ok(hbmp != NULL, "CreateBitmap failed\n");
2315 info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info_len);
2316 assert(info);
2318 memset(info, '!', info_len);
2319 memset(info, 0, sizeof(info->bmiHeader));
2321 info->bmiHeader.biSize = sizeof(info->bmiHeader);
2322 info->bmiHeader.biWidth = 2;
2323 info->bmiHeader.biHeight = 2;
2324 info->bmiHeader.biPlanes = 1;
2325 info->bmiHeader.biCompression = BI_RGB;
2327 ret = GetDIBits(screen_dc, hbmp, 0, 0, NULL, info, 0);
2328 ok(ret != 0, "GetDIBits failed got %d\n", ret);
2330 for (p = ((BYTE *) info) + sizeof(info->bmiHeader); (p - ((BYTE *) info)) < info_len; p++)
2331 if (*p != '!')
2332 overwritten_bytes++;
2333 ok(overwritten_bytes == 0, "GetDIBits wrote past the buffer given\n");
2335 HeapFree(GetProcessHeap(), 0, info);
2336 DeleteObject(hbmp);
2337 ReleaseDC(NULL, screen_dc);
2340 static BOOL compare_buffers_no_alpha(UINT32 *a, UINT32 *b, int length)
2342 int i;
2343 for(i = 0; i < length; i++)
2344 if((a[i] & 0x00FFFFFF) != (b[i] & 0x00FFFFFF))
2345 return FALSE;
2346 return TRUE;
2349 static void check_BitBlt_pixel(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2350 DWORD dwRop, UINT32 expected, int line)
2352 *srcBuffer = 0xFEDCBA98;
2353 *dstBuffer = 0x89ABCDEF;
2354 Rectangle(hdcSrc, 0, 0, 1, 1); /* A null operation to ensure dibs are coerced to X11 */
2355 BitBlt(hdcDst, 0, 0, 1, 1, hdcSrc, 0, 0, dwRop);
2356 ok(expected == *dstBuffer,
2357 "BitBlt with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2358 dwRop, expected, *dstBuffer, line);
2361 static void test_BitBlt(void)
2363 HBITMAP bmpDst, bmpSrc;
2364 HBITMAP oldDst, oldSrc;
2365 HDC hdcScreen, hdcDst, hdcSrc;
2366 UINT32 *dstBuffer, *srcBuffer;
2367 HBRUSH hBrush, hOldBrush;
2368 BITMAPINFO bitmapInfo;
2370 memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2371 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2372 bitmapInfo.bmiHeader.biWidth = 1;
2373 bitmapInfo.bmiHeader.biHeight = 1;
2374 bitmapInfo.bmiHeader.biPlanes = 1;
2375 bitmapInfo.bmiHeader.biBitCount = 32;
2376 bitmapInfo.bmiHeader.biCompression = BI_RGB;
2377 bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
2379 hdcScreen = CreateCompatibleDC(0);
2380 hdcDst = CreateCompatibleDC(hdcScreen);
2381 hdcSrc = CreateCompatibleDC(hdcDst);
2383 /* Setup the destination dib section */
2384 bmpDst = CreateDIBSection(hdcScreen, &bitmapInfo, DIB_RGB_COLORS, (void**)&dstBuffer,
2385 NULL, 0);
2386 oldDst = SelectObject(hdcDst, bmpDst);
2388 hBrush = CreateSolidBrush(0x012345678);
2389 hOldBrush = SelectObject(hdcDst, hBrush);
2391 /* Setup the source dib section */
2392 bmpSrc = CreateDIBSection(hdcScreen, &bitmapInfo, DIB_RGB_COLORS, (void**)&srcBuffer,
2393 NULL, 0);
2394 oldSrc = SelectObject(hdcSrc, bmpSrc);
2396 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2397 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2398 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2399 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2400 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2401 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2402 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2403 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2404 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2405 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2406 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2407 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2408 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2409 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2410 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2412 /* Tidy up */
2413 SelectObject(hdcSrc, oldSrc);
2414 DeleteObject(bmpSrc);
2415 DeleteDC(hdcSrc);
2417 SelectObject(hdcDst, hOldBrush);
2418 DeleteObject(hBrush);
2419 SelectObject(hdcDst, oldDst);
2420 DeleteObject(bmpDst);
2421 DeleteDC(hdcDst);
2424 DeleteDC(hdcScreen);
2427 static void check_StretchBlt_pixel(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2428 DWORD dwRop, UINT32 expected, int line)
2430 *srcBuffer = 0xFEDCBA98;
2431 *dstBuffer = 0x89ABCDEF;
2432 StretchBlt(hdcDst, 0, 0, 2, 1, hdcSrc, 0, 0, 1, 1, dwRop);
2433 ok(expected == *dstBuffer,
2434 "StretchBlt with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2435 dwRop, expected, *dstBuffer, line);
2438 static void check_StretchBlt_stretch(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2439 int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,
2440 int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
2441 UINT32 expected[4], UINT32 legacy_expected[4], int line)
2443 memset(dstBuffer, 0, 16);
2444 StretchBlt(hdcDst, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
2445 hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
2446 ok(memcmp(dstBuffer, expected, 16) == 0 ||
2447 broken(compare_buffers_no_alpha(dstBuffer, legacy_expected, 4)),
2448 "StretchBlt expected { %08X, %08X, %08X, %08X } got { %08X, %08X, %08X, %08X } "
2449 "stretching { %d, %d, %d, %d } to { %d, %d, %d, %d } from line %d\n",
2450 expected[0], expected[1], expected[2], expected[3],
2451 dstBuffer[0], dstBuffer[1], dstBuffer[2], dstBuffer[3],
2452 nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2453 nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, line);
2456 static void test_StretchBlt(void)
2458 HBITMAP bmpDst, bmpSrc;
2459 HBITMAP oldDst, oldSrc;
2460 HDC hdcScreen, hdcDst, hdcSrc;
2461 UINT32 *dstBuffer, *srcBuffer;
2462 HBRUSH hBrush, hOldBrush;
2463 BITMAPINFO biDst, biSrc;
2464 UINT32 expected[4], legacy_expected[4];
2466 memset(&biDst, 0, sizeof(BITMAPINFO));
2467 biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2468 biDst.bmiHeader.biWidth = 2;
2469 biDst.bmiHeader.biHeight = -2;
2470 biDst.bmiHeader.biPlanes = 1;
2471 biDst.bmiHeader.biBitCount = 32;
2472 biDst.bmiHeader.biCompression = BI_RGB;
2473 memcpy(&biSrc, &biDst, sizeof(BITMAPINFO));
2475 hdcScreen = CreateCompatibleDC(0);
2476 hdcDst = CreateCompatibleDC(hdcScreen);
2477 hdcSrc = CreateCompatibleDC(hdcDst);
2479 /* Pixel Tests */
2480 bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2481 NULL, 0);
2482 oldDst = SelectObject(hdcDst, bmpDst);
2484 bmpSrc = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&srcBuffer,
2485 NULL, 0);
2486 oldSrc = SelectObject(hdcSrc, bmpSrc);
2488 hBrush = CreateSolidBrush(0x012345678);
2489 hOldBrush = SelectObject(hdcDst, hBrush);
2491 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2492 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2493 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2494 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2495 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2496 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2497 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2498 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2499 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2500 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2501 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2502 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2503 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2504 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2505 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2507 SelectObject(hdcDst, hOldBrush);
2508 DeleteObject(hBrush);
2510 /* Top-down to top-down tests */
2511 srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2512 srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2514 expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2515 expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2516 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2517 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2519 expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2520 expected[2] = 0x00000000, expected[3] = 0x00000000;
2521 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2522 0, 0, 1, 1, 0, 0, 1, 1, expected, expected, __LINE__);
2524 expected[0] = 0xCAFED00D, expected[1] = 0xCAFED00D;
2525 expected[2] = 0xCAFED00D, expected[3] = 0xCAFED00D;
2526 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2527 0, 0, 2, 2, 0, 0, 1, 1, expected, expected, __LINE__);
2529 expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2530 expected[2] = 0x00000000, expected[3] = 0x00000000;
2531 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2532 0, 0, 1, 1, 0, 0, 2, 2, expected, expected, __LINE__);
2534 expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2535 expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2536 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2537 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2539 expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2540 expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2541 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2542 1, 1, -2, -2, 0, 0, 2, 2, expected, expected, __LINE__);
2544 /* This result seems broken. One might expect the following result:
2545 * 0xCAFED00D 0xFEEDFACE
2546 * 0xFEDCBA98 0x76543210
2548 expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2549 expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2550 legacy_expected[0] = 0xCAFED00D, legacy_expected[1] = 0x00000000;
2551 legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2552 todo_wine check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2553 1, 1, -2, -2, 1, 1, -2, -2, expected,
2554 legacy_expected, __LINE__);
2556 expected[0] = 0x00000000, expected[1] = 0x00000000;
2557 expected[2] = 0x00000000, expected[3] = 0xCAFED00D;
2558 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2559 1, 1, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2561 SelectObject(hdcDst, oldDst);
2562 DeleteObject(bmpDst);
2564 /* Top-down to bottom-up tests */
2565 biDst.bmiHeader.biHeight = 2;
2566 bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2567 NULL, 0);
2568 oldDst = SelectObject(hdcDst, bmpDst);
2570 expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2571 expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2572 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2573 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2575 expected[0] = 0xFEEDFACE, expected[1] = 0xCAFED00D;
2576 expected[2] = 0x76543210, expected[3] = 0xFEDCBA98;
2577 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2578 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2580 SelectObject(hdcSrc, oldSrc);
2581 DeleteObject(bmpSrc);
2583 /* Bottom-up to bottom-up tests */
2584 biSrc.bmiHeader.biHeight = 2;
2585 bmpSrc = CreateDIBSection(hdcScreen, &biSrc, DIB_RGB_COLORS, (void**)&srcBuffer,
2586 NULL, 0);
2587 srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2588 srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2589 oldSrc = SelectObject(hdcSrc, bmpSrc);
2591 expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2592 expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2593 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2594 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2596 expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2597 expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2598 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2599 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2601 SelectObject(hdcDst, oldDst);
2602 DeleteObject(bmpDst);
2604 /* Bottom-up to top-down tests */
2605 biDst.bmiHeader.biHeight = -2;
2606 bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2607 NULL, 0);
2608 oldDst = SelectObject(hdcDst, bmpDst);
2610 expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2611 expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2612 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2613 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2615 expected[0] = 0xFEEDFACE, expected[1] = 0xCAFED00D;
2616 expected[2] = 0x76543210, expected[3] = 0xFEDCBA98;
2617 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2618 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2620 /* Tidy up */
2621 SelectObject(hdcSrc, oldSrc);
2622 DeleteObject(bmpSrc);
2623 DeleteDC(hdcSrc);
2625 SelectObject(hdcDst, oldDst);
2626 DeleteObject(bmpDst);
2627 DeleteDC(hdcDst);
2629 DeleteDC(hdcScreen);
2632 static void check_StretchDIBits_pixel(HDC hdcDst, UINT32 *dstBuffer, UINT32 *srcBuffer,
2633 DWORD dwRop, UINT32 expected, int line)
2635 const UINT32 buffer[2] = { 0xFEDCBA98, 0 };
2636 BITMAPINFO bitmapInfo;
2638 memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2639 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2640 bitmapInfo.bmiHeader.biWidth = 2;
2641 bitmapInfo.bmiHeader.biHeight = 1;
2642 bitmapInfo.bmiHeader.biPlanes = 1;
2643 bitmapInfo.bmiHeader.biBitCount = 32;
2644 bitmapInfo.bmiHeader.biCompression = BI_RGB;
2645 bitmapInfo.bmiHeader.biSizeImage = sizeof(buffer);
2647 *dstBuffer = 0x89ABCDEF;
2649 StretchDIBits(hdcDst, 0, 0, 2, 1, 0, 0, 1, 1, &buffer, &bitmapInfo, DIB_RGB_COLORS, dwRop);
2650 ok(expected == *dstBuffer,
2651 "StretchDIBits with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2652 dwRop, expected, *dstBuffer, line);
2655 static void check_StretchDIBits_stretch(HDC hdcDst, UINT32 *dstBuffer, UINT32 *srcBuffer,
2656 int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,
2657 int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
2658 UINT32 expected[4], UINT32 legacy_expected[4], int line)
2660 BITMAPINFO bitmapInfo;
2662 memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2663 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2664 bitmapInfo.bmiHeader.biWidth = 2;
2665 bitmapInfo.bmiHeader.biHeight = -2;
2666 bitmapInfo.bmiHeader.biPlanes = 1;
2667 bitmapInfo.bmiHeader.biBitCount = 32;
2668 bitmapInfo.bmiHeader.biCompression = BI_RGB;
2670 memset(dstBuffer, 0, 16);
2671 StretchDIBits(hdcDst, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
2672 nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2673 srcBuffer, &bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
2674 ok(memcmp(dstBuffer, expected, 16) == 0,
2675 "StretchDIBits expected { %08X, %08X, %08X, %08X } got { %08X, %08X, %08X, %08X } "
2676 "stretching { %d, %d, %d, %d } to { %d, %d, %d, %d } from line %d\n",
2677 expected[0], expected[1], expected[2], expected[3],
2678 dstBuffer[0], dstBuffer[1], dstBuffer[2], dstBuffer[3],
2679 nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2680 nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, line);
2683 static void test_StretchDIBits(void)
2685 HBITMAP bmpDst;
2686 HBITMAP oldDst;
2687 HDC hdcScreen, hdcDst;
2688 UINT32 *dstBuffer, srcBuffer[4];
2689 HBRUSH hBrush, hOldBrush;
2690 BITMAPINFO biDst;
2691 UINT32 expected[4], legacy_expected[4];
2693 memset(&biDst, 0, sizeof(BITMAPINFO));
2694 biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2695 biDst.bmiHeader.biWidth = 2;
2696 biDst.bmiHeader.biHeight = -2;
2697 biDst.bmiHeader.biPlanes = 1;
2698 biDst.bmiHeader.biBitCount = 32;
2699 biDst.bmiHeader.biCompression = BI_RGB;
2701 hdcScreen = CreateCompatibleDC(0);
2702 hdcDst = CreateCompatibleDC(hdcScreen);
2704 /* Pixel Tests */
2705 bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2706 NULL, 0);
2707 oldDst = SelectObject(hdcDst, bmpDst);
2709 hBrush = CreateSolidBrush(0x012345678);
2710 hOldBrush = SelectObject(hdcDst, hBrush);
2712 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2713 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2714 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2715 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2716 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2717 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2718 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2719 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2720 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2721 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2722 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2723 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2724 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2725 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2726 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2728 SelectObject(hdcDst, hOldBrush);
2729 DeleteObject(hBrush);
2731 /* Top-down destination tests */
2732 srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2733 srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2735 expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2736 expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2737 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2738 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2740 expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2741 expected[2] = 0x00000000, expected[3] = 0x00000000;
2742 legacy_expected[0] = 0xFEDCBA98, legacy_expected[1] = 0x00000000;
2743 legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2744 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2745 0, 0, 1, 1, 0, 0, 1, 1, expected, legacy_expected, __LINE__);
2747 expected[0] = 0xFEDCBA98, expected[1] = 0xFEDCBA98;
2748 expected[2] = 0xFEDCBA98, expected[3] = 0xFEDCBA98;
2749 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2750 0, 0, 2, 2, 0, 0, 1, 1, expected, expected, __LINE__);
2752 expected[0] = 0x42441000, expected[1] = 0x00000000;
2753 expected[2] = 0x00000000, expected[3] = 0x00000000;
2754 legacy_expected[0] = 0x00543210, legacy_expected[1] = 0x00000000;
2755 legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2756 todo_wine check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2757 0, 0, 1, 1, 0, 0, 2, 2, expected, legacy_expected, __LINE__);
2759 expected[0] = 0x00000000, expected[1] = 0x00000000;
2760 expected[2] = 0x00000000, expected[3] = 0x00000000;
2761 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2762 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2764 expected[0] = 0x00000000, expected[1] = 0x00000000;
2765 expected[2] = 0x00000000, expected[3] = 0x00000000;
2766 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2767 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2769 expected[0] = 0x00000000, expected[1] = 0x00000000;
2770 expected[2] = 0x00000000, expected[3] = 0x00000000;
2771 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2772 1, 1, -2, -2, 1, 1, -2, -2, expected, expected, __LINE__);
2774 expected[0] = 0x00000000, expected[1] = 0x00000000;
2775 expected[2] = 0x00000000, expected[3] = 0xCAFED00D;
2776 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2777 1, 1, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2779 SelectObject(hdcDst, oldDst);
2780 DeleteObject(bmpDst);
2782 /* Bottom up destination tests */
2783 biDst.bmiHeader.biHeight = 2;
2784 bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2785 NULL, 0);
2786 oldDst = SelectObject(hdcDst, bmpDst);
2788 expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2789 expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2790 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2791 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2793 /* Tidy up */
2794 SelectObject(hdcDst, oldDst);
2795 DeleteObject(bmpDst);
2796 DeleteDC(hdcDst);
2798 DeleteDC(hdcScreen);
2801 static void test_GdiAlphaBlend(void)
2803 /* test out-of-bound parameters for GdiAlphaBlend */
2804 HDC hdcNull;
2806 HDC hdcDst;
2807 HBITMAP bmpDst;
2808 HBITMAP oldDst;
2810 BITMAPINFO bmi;
2811 HDC hdcSrc;
2812 HBITMAP bmpSrc;
2813 HBITMAP oldSrc;
2814 LPVOID bits;
2816 BLENDFUNCTION blend;
2818 if (!pGdiAlphaBlend)
2820 win_skip("GdiAlphaBlend() is not implemented\n");
2821 return;
2824 hdcNull = GetDC(NULL);
2825 hdcDst = CreateCompatibleDC(hdcNull);
2826 bmpDst = CreateCompatibleBitmap(hdcNull, 100, 100);
2827 hdcSrc = CreateCompatibleDC(hdcNull);
2829 memset(&bmi, 0, sizeof(bmi)); /* as of Wine 0.9.44 we require the src to be a DIB section */
2830 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
2831 bmi.bmiHeader.biHeight = 20;
2832 bmi.bmiHeader.biWidth = 20;
2833 bmi.bmiHeader.biBitCount = 32;
2834 bmi.bmiHeader.biPlanes = 1;
2835 bmi.bmiHeader.biCompression = BI_RGB;
2836 bmpSrc = CreateDIBSection(hdcDst, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
2837 ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2839 oldDst = SelectObject(hdcDst, bmpDst);
2840 oldSrc = SelectObject(hdcSrc, bmpSrc);
2842 blend.BlendOp = AC_SRC_OVER;
2843 blend.BlendFlags = 0;
2844 blend.SourceConstantAlpha = 128;
2845 blend.AlphaFormat = 0;
2847 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, 0, 20, 20, blend), TRUE, BOOL, "%d");
2848 SetLastError(0xdeadbeef);
2849 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), FALSE, BOOL, "%d");
2850 expect_eq(GetLastError(), ERROR_INVALID_PARAMETER, int, "%d");
2851 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), FALSE, BOOL, "%d");
2852 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 15, 0, 10, 10, blend), FALSE, BOOL, "%d");
2853 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2854 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2856 SetWindowOrgEx(hdcSrc, -10, -10, NULL);
2857 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), TRUE, BOOL, "%d");
2858 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), TRUE, BOOL, "%d");
2859 SetMapMode(hdcSrc, MM_ANISOTROPIC);
2860 ScaleWindowExtEx(hdcSrc, 10, 1, 10, 1, NULL);
2861 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 30, 30, blend), TRUE, BOOL, "%d");
2862 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 30, 30, blend), TRUE, BOOL, "%d");
2864 SetLastError(0xdeadbeef);
2865 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, NULL, 0, 0, 20, 20, blend), FALSE, BOOL, "%d");
2866 expect_eq(GetLastError(), 0xdeadbeef, int, "%d");
2868 SelectObject(hdcDst, oldDst);
2869 SelectObject(hdcSrc, oldSrc);
2870 DeleteObject(bmpSrc);
2871 DeleteObject(bmpDst);
2872 DeleteDC(hdcDst);
2873 DeleteDC(hdcSrc);
2875 ReleaseDC(NULL, hdcNull);
2879 static void test_clipping(void)
2881 HBITMAP bmpDst;
2882 HBITMAP bmpSrc;
2883 HRGN hRgn;
2884 LPVOID bits;
2885 BOOL result;
2887 HDC hdcDst = CreateCompatibleDC( NULL );
2888 HDC hdcSrc = CreateCompatibleDC( NULL );
2890 BITMAPINFO bmpinfo={{0}};
2891 bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2892 bmpinfo.bmiHeader.biWidth = 100;
2893 bmpinfo.bmiHeader.biHeight = 100;
2894 bmpinfo.bmiHeader.biPlanes = 1;
2895 bmpinfo.bmiHeader.biBitCount = GetDeviceCaps( hdcDst, BITSPIXEL );
2896 bmpinfo.bmiHeader.biCompression = BI_RGB;
2898 bmpDst = CreateDIBSection( hdcDst, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2899 ok(bmpDst != NULL, "Couldn't create destination bitmap\n");
2900 SelectObject( hdcDst, bmpDst );
2902 bmpSrc = CreateDIBSection( hdcSrc, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2903 ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2904 SelectObject( hdcSrc, bmpSrc );
2906 result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 100, 100, SRCCOPY );
2907 ok(result, "BitBlt failed\n");
2909 hRgn = CreateRectRgn( 0,0,0,0 );
2910 SelectClipRgn( hdcDst, hRgn );
2912 result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 0, 0, SRCCOPY );
2913 ok(result, "BitBlt failed\n");
2915 DeleteObject( bmpDst );
2916 DeleteObject( bmpSrc );
2917 DeleteObject( hRgn );
2918 DeleteDC( hdcDst );
2919 DeleteDC( hdcSrc );
2922 static void test_32bit_bitmap_blt(void)
2924 BITMAPINFO biDst;
2925 HBITMAP bmpSrc, bmpDst;
2926 HBITMAP oldSrc, oldDst;
2927 HDC hdcSrc, hdcDst, hdcScreen;
2928 UINT32 *dstBuffer;
2929 DWORD colorSrc = 0x11223344;
2931 memset(&biDst, 0, sizeof(BITMAPINFO));
2932 biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2933 biDst.bmiHeader.biWidth = 2;
2934 biDst.bmiHeader.biHeight = -2;
2935 biDst.bmiHeader.biPlanes = 1;
2936 biDst.bmiHeader.biBitCount = 32;
2937 biDst.bmiHeader.biCompression = BI_RGB;
2939 hdcScreen = CreateCompatibleDC(0);
2940 if(GetDeviceCaps(hdcScreen, BITSPIXEL) != 32)
2942 DeleteDC(hdcScreen);
2943 trace("Skipping 32-bit DDB test\n");
2944 return;
2947 hdcSrc = CreateCompatibleDC(hdcScreen);
2948 bmpSrc = CreateBitmap(1, 1, 1, 32, &colorSrc);
2949 oldSrc = SelectObject(hdcSrc, bmpSrc);
2951 hdcDst = CreateCompatibleDC(hdcScreen);
2952 bmpDst = CreateDIBSection(hdcDst, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer, NULL, 0);
2953 oldDst = SelectObject(hdcDst, bmpDst);
2955 StretchBlt(hdcDst, 0, 0, 1, 1, hdcSrc, 0, 0, 1, 1, SRCCOPY);
2956 ok(dstBuffer[0] == colorSrc, "Expected color=%x, received color=%x\n", colorSrc, dstBuffer[0]);
2958 /* Tidy up */
2959 SelectObject(hdcDst, oldDst);
2960 DeleteObject(bmpDst);
2961 DeleteDC(hdcDst);
2963 SelectObject(hdcSrc, oldSrc);
2964 DeleteObject(bmpSrc);
2965 DeleteDC(hdcSrc);
2967 DeleteDC(hdcScreen);
2971 * Used by test_GetDIBits_top_down to create the bitmap to test against.
2973 static void setup_picture(char *picture, int bpp)
2975 int i;
2977 switch(bpp)
2979 case 16:
2980 case 32:
2981 /*Set the first byte in each pixel to the index of that pixel.*/
2982 for (i = 0; i < 4; i++)
2983 picture[i * (bpp / 8)] = i;
2984 break;
2985 case 24:
2986 picture[0] = 0;
2987 picture[3] = 1;
2988 /*Each scanline in a bitmap must be a multiple of 4 bytes long.*/
2989 picture[8] = 2;
2990 picture[11] = 3;
2991 break;
2995 static void test_GetDIBits_top_down(int bpp)
2997 BITMAPINFO bi;
2998 HBITMAP bmptb, bmpbt;
2999 HDC hdc;
3000 int pictureOut[4];
3001 int *picture;
3002 int statusCode;
3004 bi.bmiHeader.biSize=sizeof(bi.bmiHeader);
3005 bi.bmiHeader.biWidth=2;
3006 bi.bmiHeader.biHeight=2;
3007 bi.bmiHeader.biPlanes=1;
3008 bi.bmiHeader.biBitCount=bpp;
3009 bi.bmiHeader.biCompression=BI_RGB;
3011 /*Get the device context for the screen.*/
3012 hdc = GetDC(NULL);
3013 ok(hdc != NULL, "Could not get a handle to a device context.\n");
3015 /*Create the bottom to top image (image's bottom scan line is at the top in memory).*/
3016 bmpbt = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void**)&picture, NULL, 0);
3017 ok(bmpbt != NULL, "Could not create a DIB section.\n");
3018 /*Now that we have a pointer to the pixels, we write to them.*/
3019 setup_picture((char*)picture, bpp);
3020 /*Create the top to bottom image (images' bottom scan line is at the bottom in memory).*/
3021 bi.bmiHeader.biHeight=-2; /*We specify that we want a top to bottom image by specifying a negative height.*/
3022 bmptb = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void**)&picture, NULL, 0);
3023 ok(bmptb != NULL, "Could not create a DIB section.\n");
3024 /*Write to this top to bottom bitmap.*/
3025 setup_picture((char*)picture, bpp);
3027 bi.bmiHeader.biWidth = 1;
3029 bi.bmiHeader.biHeight = 2;
3030 statusCode = GetDIBits(hdc, bmpbt, 0, 1, pictureOut, &bi, DIB_RGB_COLORS);
3031 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3032 /*Check the first byte of the pixel.*/
3033 ok((char)pictureOut[0] == 0, "Bottom-up -> bottom-up: first pixel should be 0 but was %d.\n", (char)pictureOut[0]);
3034 statusCode = GetDIBits(hdc, bmptb, 0, 1, pictureOut, &bi, DIB_RGB_COLORS);
3035 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3036 ok((char)pictureOut[0] == 2, "Top-down -> bottom-up: first pixel should be 2 but was %d.\n", (char)pictureOut[0]);
3037 /*Check second scanline.*/
3038 statusCode = GetDIBits(hdc, bmptb, 1, 1, pictureOut, &bi, DIB_RGB_COLORS);
3039 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3040 ok((char)pictureOut[0] == 0, "Top-down -> bottom-up: first pixel should be 0 but was %d.\n", (char)pictureOut[0]);
3041 statusCode = GetDIBits(hdc, bmpbt, 1, 1, pictureOut, &bi, DIB_RGB_COLORS);
3042 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3043 ok((char)pictureOut[0] == 2, "Bottom-up -> bottom-up: first pixel should be 2 but was %d.\n", (char)pictureOut[0]);
3044 /*Check both scanlines.*/
3045 statusCode = GetDIBits(hdc, bmptb, 0, 2, pictureOut, &bi, DIB_RGB_COLORS);
3046 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3047 ok((char)pictureOut[0] == 2, "Top-down -> bottom-up: first scanline should be 2 but was %d.\n", (char)pictureOut[0]);
3048 ok((char)pictureOut[1] == 0, "Top-down -> bottom-up: second scanline should be 0 but was %d.\n", (char)pictureOut[0]);
3049 statusCode = GetDIBits(hdc, bmpbt, 0, 2, pictureOut, &bi, DIB_RGB_COLORS);
3050 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3051 ok((char)pictureOut[0] == 0, "Bottom up -> bottom-up: first scanline should be 0 but was %d.\n", (char)pictureOut[0]);
3052 ok((char)pictureOut[1] == 2, "Bottom up -> bottom-up: second scanline should be 2 but was %d.\n", (char)pictureOut[0]);
3054 /*Make destination bitmap top-down.*/
3055 bi.bmiHeader.biHeight = -2;
3056 statusCode = GetDIBits(hdc, bmpbt, 0, 1, pictureOut, &bi, DIB_RGB_COLORS);
3057 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3058 ok((char)pictureOut[0] == 0, "Bottom-up -> top-down: first pixel should be 0 but was %d.\n", (char)pictureOut[0]);
3059 statusCode = GetDIBits(hdc, bmptb, 0, 1, pictureOut, &bi, DIB_RGB_COLORS);
3060 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3061 ok((char)pictureOut[0] == 2, "Top-down -> top-down: first pixel should be 2 but was %d.\n", (char)pictureOut[0]);
3062 /*Check second scanline.*/
3063 statusCode = GetDIBits(hdc, bmptb, 1, 1, pictureOut, &bi, DIB_RGB_COLORS);
3064 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3065 ok((char)pictureOut[0] == 0, "Top-down -> bottom-up: first pixel should be 0 but was %d.\n", (char)pictureOut[0]);
3066 statusCode = GetDIBits(hdc, bmpbt, 1, 1, pictureOut, &bi, DIB_RGB_COLORS);
3067 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3068 ok((char)pictureOut[0] == 2, "Top-down -> bottom-up: first pixel should be 2 but was %d.\n", (char)pictureOut[0]);
3069 /*Check both scanlines.*/
3070 statusCode = GetDIBits(hdc, bmptb, 0, 2, pictureOut, &bi, DIB_RGB_COLORS);
3071 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3072 ok((char)pictureOut[0] == 0, "Top-down -> top-down: first scanline should be 0 but was %d.\n", (char)pictureOut[0]);
3073 ok((char)pictureOut[1] == 2, "Top-down -> top-down: second scanline should be 2 but was %d.\n", (char)pictureOut[0]);
3074 statusCode = GetDIBits(hdc, bmpbt, 0, 2, pictureOut, &bi, DIB_RGB_COLORS);
3075 ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3076 ok((char)pictureOut[0] == 2, "Bottom up -> top-down: first scanline should be 2 but was %d.\n", (char)pictureOut[0]);
3077 ok((char)pictureOut[1] == 0, "Bottom up -> top-down: second scanline should be 0 but was %d.\n", (char)pictureOut[0]);
3079 DeleteObject(bmpbt);
3080 DeleteObject(bmptb);
3083 START_TEST(bitmap)
3085 HMODULE hdll;
3087 hdll = GetModuleHandle("gdi32.dll");
3088 pGdiAlphaBlend = (void*)GetProcAddress(hdll, "GdiAlphaBlend");
3090 test_createdibitmap();
3091 test_dibsections();
3092 test_mono_dibsection();
3093 test_bitmap();
3094 test_bmBits();
3095 test_GetDIBits_selected_DIB(1);
3096 test_GetDIBits_selected_DIB(4);
3097 test_GetDIBits_selected_DIB(8);
3098 test_GetDIBits_selected_DDB(TRUE);
3099 test_GetDIBits_selected_DDB(FALSE);
3100 test_GetDIBits();
3101 test_GetDIBits_BI_BITFIELDS();
3102 test_select_object();
3103 test_CreateBitmap();
3104 test_BitBlt();
3105 test_StretchBlt();
3106 test_StretchDIBits();
3107 test_GdiAlphaBlend();
3108 test_32bit_bitmap_blt();
3109 test_bitmapinfoheadersize();
3110 test_get16dibits();
3111 test_clipping();
3112 test_GetDIBits_top_down(16);
3113 test_GetDIBits_top_down(24);
3114 test_GetDIBits_top_down(32);