crypt32: Add additional path for Solaris 11 Express.
[wine] / dlls / gdi32 / tests / bitmap.c
1 /*
2  * Unit test suite for bitmaps
3  *
4  * Copyright 2004 Huw Davies
5  * Copyright 2006 Dmitry Timoshkov
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23 #include <assert.h>
24 #include <string.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "mmsystem.h"
32
33 #include "wine/test.h"
34
35 static BOOL (WINAPI *pGdiAlphaBlend)(HDC,int,int,int,int,HDC,int,int,int,int,BLENDFUNCTION);
36 static DWORD (WINAPI *pSetLayout)(HDC hdc, DWORD layout);
37
38 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
39
40 static INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
41 {
42     switch(bpp)
43     {
44     case 1:
45         return 2 * ((bmWidth+15) >> 4);
46
47     case 24:
48         bmWidth *= 3; /* fall through */
49     case 8:
50         return bmWidth + (bmWidth & 1);
51
52     case 32:
53         return bmWidth * 4;
54
55     case 16:
56     case 15:
57         return bmWidth * 2;
58
59     case 4:
60         return 2 * ((bmWidth+3) >> 2);
61
62     default:
63         trace("Unknown depth %d, please report.\n", bpp );
64         assert(0);
65     }
66     return -1;
67 }
68
69 static void test_bitmap_info(HBITMAP hbm, INT expected_depth, const BITMAPINFOHEADER *bmih)
70 {
71     BITMAP bm;
72     BITMAP bma[2];
73     INT ret, width_bytes;
74     BYTE buf[512], buf_cmp[512];
75
76     ret = GetObject(hbm, sizeof(bm), &bm);
77     ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
78
79     ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
80     ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
81     ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
82     width_bytes = BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
83     ok(bm.bmWidthBytes == width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, width_bytes);
84     ok(bm.bmPlanes == bmih->biPlanes, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
85     ok(bm.bmBitsPixel == expected_depth, "wrong bm.bmBitsPixel %d != %d\n", bm.bmBitsPixel, expected_depth);
86     ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
87
88     assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
89     assert(sizeof(buf) == sizeof(buf_cmp));
90
91     SetLastError(0xdeadbeef);
92     ret = GetBitmapBits(hbm, 0, NULL);
93     ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
94
95     memset(buf_cmp, 0xAA, sizeof(buf_cmp));
96     memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
97
98     memset(buf, 0xAA, sizeof(buf));
99     ret = GetBitmapBits(hbm, sizeof(buf), buf);
100     ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
101     ok(!memcmp(buf, buf_cmp, sizeof(buf)),
102         "buffers do not match, depth %d\n", bmih->biBitCount);
103
104     /* test various buffer sizes for GetObject */
105     ret = GetObject(hbm, sizeof(*bma) * 2, bma);
106     ok(ret == sizeof(*bma), "wrong size %d\n", ret);
107
108     ret = GetObject(hbm, sizeof(bm) / 2, &bm);
109     ok(ret == 0, "%d != 0\n", ret);
110
111     ret = GetObject(hbm, 0, &bm);
112     ok(ret == 0, "%d != 0\n", ret);
113
114     ret = GetObject(hbm, 1, &bm);
115     ok(ret == 0, "%d != 0\n", ret);
116
117     ret = GetObject(hbm, 0, NULL);
118     ok(ret == sizeof(bm), "wrong size %d\n", ret);
119 }
120
121 static void test_createdibitmap(void)
122 {
123     HDC hdc, hdcmem;
124     BITMAPINFOHEADER bmih;
125     BITMAPINFO bm;
126     HBITMAP hbm, hbm_colour, hbm_old;
127     INT screen_depth;
128     DWORD pixel;
129
130     hdc = GetDC(0);
131     screen_depth = GetDeviceCaps(hdc, BITSPIXEL);
132     memset(&bmih, 0, sizeof(bmih));
133     bmih.biSize = sizeof(bmih);
134     bmih.biWidth = 10;
135     bmih.biHeight = 10;
136     bmih.biPlanes = 1;
137     bmih.biBitCount = 32;
138     bmih.biCompression = BI_RGB;
139
140     hbm = CreateDIBitmap(hdc, NULL, CBM_INIT, NULL, NULL, 0);
141     ok(hbm == NULL, "CreateDIBitmap should fail\n");
142     hbm = CreateDIBitmap(hdc, NULL, 0, NULL, NULL, 0);
143     ok(hbm == NULL, "CreateDIBitmap should fail\n");
144
145     /* First create an un-initialised bitmap.  The depth of the bitmap
146        should match that of the hdc and not that supplied in bmih.
147     */
148
149     /* First try 32 bits */
150     hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
151     ok(hbm != NULL, "CreateDIBitmap failed\n");
152     test_bitmap_info(hbm, screen_depth, &bmih);
153     DeleteObject(hbm);
154     
155     /* Then 16 */
156     bmih.biBitCount = 16;
157     hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
158     ok(hbm != NULL, "CreateDIBitmap failed\n");
159     test_bitmap_info(hbm, screen_depth, &bmih);
160     DeleteObject(hbm);
161
162     /* Then 1 */
163     bmih.biBitCount = 1;
164     hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
165     ok(hbm != NULL, "CreateDIBitmap failed\n");
166     test_bitmap_info(hbm, screen_depth, &bmih);
167     DeleteObject(hbm);
168
169     /* Now with a monochrome dc we expect a monochrome bitmap */
170     hdcmem = CreateCompatibleDC(hdc);
171
172     /* First try 32 bits */
173     bmih.biBitCount = 32;
174     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
175     ok(hbm != NULL, "CreateDIBitmap failed\n");
176     test_bitmap_info(hbm, 1, &bmih);
177     DeleteObject(hbm);
178     
179     /* Then 16 */
180     bmih.biBitCount = 16;
181     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
182     ok(hbm != NULL, "CreateDIBitmap failed\n");
183     test_bitmap_info(hbm, 1, &bmih);
184     DeleteObject(hbm);
185     
186     /* Then 1 */
187     bmih.biBitCount = 1;
188     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
189     ok(hbm != NULL, "CreateDIBitmap failed\n");
190     test_bitmap_info(hbm, 1, &bmih);
191     DeleteObject(hbm);
192
193     /* Now select a polychrome bitmap into the dc and we expect
194        screen_depth bitmaps again */
195     hbm_colour = CreateCompatibleBitmap(hdc, bmih.biWidth, bmih.biHeight);
196     test_bitmap_info(hbm_colour, screen_depth, &bmih);
197     hbm_old = SelectObject(hdcmem, hbm_colour);
198
199     /* First try 32 bits */
200     bmih.biBitCount = 32;
201     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
202     ok(hbm != NULL, "CreateDIBitmap failed\n");
203     test_bitmap_info(hbm, screen_depth, &bmih);
204     DeleteObject(hbm);
205     
206     /* Then 16 */
207     bmih.biBitCount = 16;
208     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
209     ok(hbm != NULL, "CreateDIBitmap failed\n");
210     test_bitmap_info(hbm, screen_depth, &bmih);
211     DeleteObject(hbm);
212     
213     /* Then 1 */
214     bmih.biBitCount = 1;
215     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
216     ok(hbm != NULL, "CreateDIBitmap failed\n");
217     test_bitmap_info(hbm, screen_depth, &bmih);
218     DeleteObject(hbm);
219
220     SelectObject(hdcmem, hbm_old);
221     DeleteObject(hbm_colour);
222     DeleteDC(hdcmem);
223
224     bmih.biBitCount = 32;
225     hbm = CreateDIBitmap(0, &bmih, 0, NULL, NULL, 0);
226     ok(hbm != NULL, "CreateDIBitmap failed\n");
227     test_bitmap_info(hbm, 1, &bmih);
228     DeleteObject(hbm);
229
230     /* Test how formats are converted */
231     pixel = 0xffffffff;
232     bmih.biBitCount = 1;
233     bmih.biWidth = 1;
234     bmih.biHeight = 1;
235
236     memset(&bm, 0, sizeof(bm));
237     bm.bmiHeader.biSize = sizeof(bm.bmiHeader);
238     bm.bmiHeader.biWidth = 1;
239     bm.bmiHeader.biHeight = 1;
240     bm.bmiHeader.biPlanes = 1;
241     bm.bmiHeader.biBitCount= 24;
242     bm.bmiHeader.biCompression= BI_RGB;
243     bm.bmiHeader.biSizeImage = 0;
244     hbm = CreateDIBitmap(hdc, &bmih, CBM_INIT, &pixel, &bm, DIB_RGB_COLORS);
245     ok(hbm != NULL, "CreateDIBitmap failed\n");
246
247     pixel = 0xdeadbeef;
248     bm.bmiHeader.biBitCount= 32;
249     GetDIBits(hdc, hbm, 0, 1, &pixel, &bm, DIB_RGB_COLORS);
250     ok(pixel == 0x00ffffff, "Reading a 32 bit pixel from a DDB returned %08x\n", pixel);
251     DeleteObject(hbm);
252
253     ReleaseDC(0, hdc);
254 }
255
256 static INT DIB_GetWidthBytes( int width, int bpp )
257 {
258     int words;
259
260     switch (bpp)
261     {
262         case 1:  words = (width + 31) / 32; break;
263         case 4:  words = (width + 7) / 8; break;
264         case 8:  words = (width + 3) / 4; break;
265         case 15:
266         case 16: words = (width + 1) / 2; break;
267         case 24: words = (width * 3 + 3)/4; break;
268         case 32: words = width; break;
269
270         default:
271             words=0;
272             trace("Unknown depth %d, please report.\n", bpp );
273             assert(0);
274             break;
275     }
276     return 4 * words;
277 }
278
279 static void test_dib_info(HBITMAP hbm, const void *bits, const BITMAPINFOHEADER *bmih)
280 {
281     BITMAP bm;
282     BITMAP bma[2];
283     DIBSECTION ds;
284     DIBSECTION dsa[2];
285     INT ret, bm_width_bytes, dib_width_bytes;
286     BYTE *buf;
287
288     ret = GetObject(hbm, sizeof(bm), &bm);
289     ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
290
291     ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
292     ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
293     ok(bm.bmHeight == abs(bmih->biHeight), "wrong bm.bmHeight %d\n", bm.bmHeight);
294     dib_width_bytes = DIB_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
295     bm_width_bytes = BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
296     if (bm.bmWidthBytes != dib_width_bytes) /* Win2k bug */
297         ok(bm.bmWidthBytes == bm_width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, bm_width_bytes);
298     else
299         ok(bm.bmWidthBytes == dib_width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, dib_width_bytes);
300     ok(bm.bmPlanes == bmih->biPlanes, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
301     ok(bm.bmBitsPixel == bmih->biBitCount, "bm.bmBitsPixel %d != %d\n", bm.bmBitsPixel, bmih->biBitCount);
302     ok(bm.bmBits == bits, "wrong bm.bmBits %p != %p\n", bm.bmBits, bits);
303
304     buf = HeapAlloc(GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight + 4096);
305
306     /* GetBitmapBits returns not 32-bit aligned data */
307     SetLastError(0xdeadbeef);
308     ret = GetBitmapBits(hbm, 0, NULL);
309     ok(ret == bm_width_bytes * bm.bmHeight,
310         "%d != %d\n", ret, bm_width_bytes * bm.bmHeight);
311
312     memset(buf, 0xAA, bm.bmWidthBytes * bm.bmHeight + 4096);
313     ret = GetBitmapBits(hbm, bm.bmWidthBytes * bm.bmHeight + 4096, buf);
314     ok(ret == bm_width_bytes * bm.bmHeight, "%d != %d\n", ret, bm_width_bytes * bm.bmHeight);
315
316     HeapFree(GetProcessHeap(), 0, buf);
317
318     /* test various buffer sizes for GetObject */
319     memset(&ds, 0xAA, sizeof(ds));
320     ret = GetObject(hbm, sizeof(*bma) * 2, bma);
321     ok(ret == sizeof(*bma), "wrong size %d\n", ret);
322     ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
323     ok(bm.bmHeight == abs(bmih->biHeight), "wrong bm.bmHeight %d\n", bm.bmHeight);
324     ok(bm.bmBits == bits, "wrong bm.bmBits %p != %p\n", bm.bmBits, bits);
325
326     ret = GetObject(hbm, sizeof(bm) / 2, &bm);
327     ok(ret == 0, "%d != 0\n", ret);
328
329     ret = GetObject(hbm, 0, &bm);
330     ok(ret == 0, "%d != 0\n", ret);
331
332     ret = GetObject(hbm, 1, &bm);
333     ok(ret == 0, "%d != 0\n", ret);
334
335     /* test various buffer sizes for GetObject */
336     ret = GetObject(hbm, 0, NULL);
337     ok(ret == sizeof(bm), "wrong size %d\n", ret);
338
339     ret = GetObject(hbm, sizeof(*dsa) * 2, dsa);
340     ok(ret == sizeof(*dsa), "wrong size %d\n", ret);
341
342     memset(&ds, 0xAA, sizeof(ds));
343     ret = GetObject(hbm, sizeof(ds), &ds);
344     ok(ret == sizeof(ds), "wrong size %d\n", ret);
345
346     ok(ds.dsBm.bmBits == bits, "wrong bm.bmBits %p != %p\n", ds.dsBm.bmBits, bits);
347     if (ds.dsBm.bmWidthBytes != bm_width_bytes) /* Win2k bug */
348         ok(ds.dsBmih.biSizeImage == ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight, "%u != %u\n",
349            ds.dsBmih.biSizeImage, ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight);
350     ok(bmih->biSizeImage == 0, "%u != 0\n", bmih->biSizeImage);
351     ds.dsBmih.biSizeImage = 0;
352
353     ok(ds.dsBmih.biSize == bmih->biSize, "%u != %u\n", ds.dsBmih.biSize, bmih->biSize);
354     ok(ds.dsBmih.biWidth == bmih->biWidth, "%d != %d\n", ds.dsBmih.biWidth, bmih->biWidth);
355     ok(ds.dsBmih.biHeight == abs(bmih->biHeight), "%d != %d\n", ds.dsBmih.biHeight, abs(bmih->biHeight));
356     ok(ds.dsBmih.biPlanes == bmih->biPlanes, "%u != %u\n", ds.dsBmih.biPlanes, bmih->biPlanes);
357     ok(ds.dsBmih.biBitCount == bmih->biBitCount, "%u != %u\n", ds.dsBmih.biBitCount, bmih->biBitCount);
358     ok(ds.dsBmih.biCompression == bmih->biCompression ||
359        ((bmih->biBitCount == 32) && broken(ds.dsBmih.biCompression == BI_BITFIELDS)), /* nt4 sp1 and 2 */
360        "%u != %u\n", ds.dsBmih.biCompression, bmih->biCompression);
361     ok(ds.dsBmih.biSizeImage == bmih->biSizeImage, "%u != %u\n", ds.dsBmih.biSizeImage, bmih->biSizeImage);
362     ok(ds.dsBmih.biXPelsPerMeter == bmih->biXPelsPerMeter, "%d != %d\n", ds.dsBmih.biXPelsPerMeter, bmih->biXPelsPerMeter);
363     ok(ds.dsBmih.biYPelsPerMeter == bmih->biYPelsPerMeter, "%d != %d\n", ds.dsBmih.biYPelsPerMeter, bmih->biYPelsPerMeter);
364
365     memset(&ds, 0xAA, sizeof(ds));
366     ret = GetObject(hbm, sizeof(ds) - 4, &ds);
367     ok(ret == sizeof(ds.dsBm), "wrong size %d\n", ret);
368     ok(ds.dsBm.bmWidth == bmih->biWidth, "%d != %d\n", ds.dsBmih.biWidth, bmih->biWidth);
369     ok(ds.dsBm.bmHeight == abs(bmih->biHeight), "%d != %d\n", ds.dsBmih.biHeight, abs(bmih->biHeight));
370     ok(ds.dsBm.bmBits == bits, "%p != %p\n", ds.dsBm.bmBits, bits);
371
372     ret = GetObject(hbm, 0, &ds);
373     ok(ret == 0, "%d != 0\n", ret);
374
375     ret = GetObject(hbm, 1, &ds);
376     ok(ret == 0, "%d != 0\n", ret);
377 }
378
379 #define test_color_todo(got, exp, txt, todo) \
380     if (!todo && got != exp && screen_depth < 24) { \
381       todo_wine ok(0, #txt " failed at %d-bit screen depth: got 0x%06x expected 0x%06x - skipping DIB tests\n", \
382                    screen_depth, (UINT)got, (UINT)exp); \
383       return; \
384     } else if (todo) todo_wine { ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp); } \
385     else ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp) \
386
387 #define test_color(hdc, color, exp, todo_setp, todo_getp) \
388 { \
389     COLORREF c; \
390     c = SetPixel(hdc, 0, 0, color); \
391     test_color_todo(c, exp, SetPixel, todo_setp); \
392     c = GetPixel(hdc, 0, 0); \
393     test_color_todo(c, exp, GetPixel, todo_getp); \
394 }
395
396 static void test_dib_bits_access( HBITMAP hdib, void *bits )
397 {
398     MEMORY_BASIC_INFORMATION info;
399     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
400     DWORD data[256];
401     BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
402     HDC hdc;
403     char filename[MAX_PATH];
404     HANDLE file;
405     DWORD written;
406     INT ret;
407
408     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
409         "VirtualQuery failed\n");
410     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
411     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
412     ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
413     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
414     ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
415     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
416
417     memset( pbmi, 0, sizeof(bmibuf) );
418     memset( data, 0xcc, sizeof(data) );
419     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
420     pbmi->bmiHeader.biHeight = 16;
421     pbmi->bmiHeader.biWidth = 16;
422     pbmi->bmiHeader.biBitCount = 32;
423     pbmi->bmiHeader.biPlanes = 1;
424     pbmi->bmiHeader.biCompression = BI_RGB;
425
426     hdc = GetDC(0);
427
428     ret = SetDIBits( hdc, hdib, 0, 16, data, pbmi, DIB_RGB_COLORS );
429     ok(ret == 16, "SetDIBits failed: expected 16 got %d\n", ret);
430
431     ReleaseDC(0, hdc);
432
433     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
434         "VirtualQuery failed\n");
435     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
436     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
437     ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
438     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
439     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
440     /* it has been protected now */
441     todo_wine ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
442
443     /* try writing protected bits to a file */
444
445     GetTempFileNameA( ".", "dib", 0, filename );
446     file = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
447                         CREATE_ALWAYS, 0, 0 );
448     ok( file != INVALID_HANDLE_VALUE, "failed to open %s error %u\n", filename, GetLastError() );
449     ret = WriteFile( file, bits, 8192, &written, NULL );
450     ok( ret, "WriteFile failed error %u\n", GetLastError() );
451     if (ret) ok( written == 8192, "only wrote %u bytes\n", written );
452     CloseHandle( file );
453     DeleteFileA( filename );
454 }
455
456 static void test_dibsections(void)
457 {
458     HDC hdc, hdcmem, hdcmem2;
459     HBITMAP hdib, oldbm, hdib2, oldbm2;
460     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
461     char bcibuf[sizeof(BITMAPCOREINFO) + 256 * sizeof(RGBTRIPLE)];
462     BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
463     BITMAPCOREINFO *pbci = (BITMAPCOREINFO *)bcibuf;
464     HBITMAP hcoredib;
465     char coreBits[256];
466     BYTE *bits;
467     RGBQUAD rgb[256];
468     int ret;
469     char logpalbuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
470     LOGPALETTE *plogpal = (LOGPALETTE*)logpalbuf;
471     WORD *index;
472     DWORD *bits32;
473     HPALETTE hpal, oldpal;
474     DIBSECTION dibsec;
475     COLORREF c0, c1;
476     int i;
477     int screen_depth;
478     MEMORY_BASIC_INFORMATION info;
479
480     hdc = GetDC(0);
481     screen_depth = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
482
483     memset(pbmi, 0, sizeof(bmibuf));
484     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
485     pbmi->bmiHeader.biHeight = 100;
486     pbmi->bmiHeader.biWidth = 512;
487     pbmi->bmiHeader.biBitCount = 24;
488     pbmi->bmiHeader.biPlanes = 1;
489     pbmi->bmiHeader.biCompression = BI_RGB;
490
491     SetLastError(0xdeadbeef);
492
493     /* invalid pointer for BITMAPINFO
494        (*bits should be NULL on error) */
495     bits = (BYTE*)0xdeadbeef;
496     hdib = CreateDIBSection(hdc, NULL, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
497     ok(hdib == NULL && bits == NULL, "CreateDIBSection failed for invalid parameter: bmi == 0x0\n");
498
499     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
500     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
501     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
502     ok(dibsec.dsBm.bmBits == bits, "dibsec.dsBits %p != bits %p\n", dibsec.dsBm.bmBits, bits);
503
504     /* test the DIB memory */
505     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
506         "VirtualQuery failed\n");
507     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
508     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
509     ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
510     ok(info.RegionSize == 0x26000, "0x%lx != 0x26000\n", info.RegionSize);
511     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
512     ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
513     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
514
515     test_dib_bits_access( hdib, bits );
516
517     test_dib_info(hdib, bits, &pbmi->bmiHeader);
518     DeleteObject(hdib);
519
520     /* Test a top-down DIB. */
521     pbmi->bmiHeader.biHeight = -100;
522     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
523     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
524     test_dib_info(hdib, bits, &pbmi->bmiHeader);
525     DeleteObject(hdib);
526
527     pbmi->bmiHeader.biHeight = 100;
528     pbmi->bmiHeader.biBitCount = 8;
529     pbmi->bmiHeader.biCompression = BI_RLE8;
530     SetLastError(0xdeadbeef);
531     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
532     ok(hdib == NULL, "CreateDIBSection should fail when asked to create a compressed DIB section\n");
533     ok(GetLastError() == 0xdeadbeef, "wrong error %d\n", GetLastError());
534
535     for (i = 0; i < 128; i++)
536     {
537         pbmi->bmiHeader.biBitCount = i;
538         pbmi->bmiHeader.biCompression = BI_RGB;
539         hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
540         if (i == 1 || i == 4 || i == 8 || i == 16 || i == 24 || i == 32)
541             ok(hdib != NULL, "CreateDIBSection bpp %u\n", i);
542         else
543             ok(hdib == NULL, "CreateDIBSection bpp %u succeeded\n", i);
544         if (hdib) DeleteObject( hdib );
545     }
546
547     pbmi->bmiHeader.biBitCount = 16;
548     pbmi->bmiHeader.biCompression = BI_BITFIELDS;
549     ((PDWORD)pbmi->bmiColors)[0] = 0xf800;
550     ((PDWORD)pbmi->bmiColors)[1] = 0x07e0;
551     ((PDWORD)pbmi->bmiColors)[2] = 0x001f;
552     SetLastError(0xdeadbeef);
553     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
554     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
555
556     /* test the DIB memory */
557     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
558         "VirtualQuery failed\n");
559     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
560     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
561     ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
562     ok(info.RegionSize == 0x19000, "0x%lx != 0x19000\n", info.RegionSize);
563     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
564     ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
565     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
566
567     test_dib_info(hdib, bits, &pbmi->bmiHeader);
568     DeleteObject(hdib);
569
570     memset(pbmi, 0, sizeof(bmibuf));
571     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
572     pbmi->bmiHeader.biHeight = 16;
573     pbmi->bmiHeader.biWidth = 16;
574     pbmi->bmiHeader.biBitCount = 1;
575     pbmi->bmiHeader.biPlanes = 1;
576     pbmi->bmiHeader.biCompression = BI_RGB;
577     pbmi->bmiColors[0].rgbRed = 0xff;
578     pbmi->bmiColors[0].rgbGreen = 0;
579     pbmi->bmiColors[0].rgbBlue = 0;
580     pbmi->bmiColors[1].rgbRed = 0;
581     pbmi->bmiColors[1].rgbGreen = 0;
582     pbmi->bmiColors[1].rgbBlue = 0xff;
583
584     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
585     ok(hdib != NULL, "CreateDIBSection failed\n");
586     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
587     ok(dibsec.dsBmih.biClrUsed == 2,
588         "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
589
590     /* Test if the old BITMAPCOREINFO structure is supported */    
591         
592     pbci->bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
593     pbci->bmciHeader.bcBitCount = 0;
594
595     ret = GetDIBits(hdc, hdib, 0, 16, NULL, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
596     ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
597     ok((pbci->bmciHeader.bcWidth == 16) && (pbci->bmciHeader.bcHeight == 16)
598         && (pbci->bmciHeader.bcBitCount == 1) && (pbci->bmciHeader.bcPlanes == 1),
599     "GetDIBits did't fill in the BITMAPCOREHEADER structure properly\n");
600
601     ret = GetDIBits(hdc, hdib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
602     ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
603     ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
604         (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
605         (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
606         "The color table has not been translated to the old BITMAPCOREINFO format\n");
607
608     hcoredib = CreateDIBSection(hdc, (BITMAPINFO*) pbci, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
609     ok(hcoredib != NULL, "CreateDIBSection failed with a BITMAPCOREINFO\n");
610
611     ZeroMemory(pbci->bmciColors, 256 * sizeof(RGBTRIPLE));
612     ret = GetDIBits(hdc, hcoredib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
613     ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
614     ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
615         (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
616         (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
617         "The color table has not been translated to the old BITMAPCOREINFO format\n");
618
619     DeleteObject(hcoredib);
620
621     hdcmem = CreateCompatibleDC(hdc);
622     oldbm = SelectObject(hdcmem, hdib);
623
624     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
625     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
626     ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
627        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
628        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
629        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
630
631     c0 = RGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen, pbmi->bmiColors[0].rgbBlue);
632     c1 = RGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen, pbmi->bmiColors[1].rgbBlue);
633
634     test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
635     test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
636     test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
637     test_color(hdcmem, PALETTEINDEX(0), c0, 1, 1);
638     test_color(hdcmem, PALETTEINDEX(1), c0, 1, 1);
639     test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
640     test_color(hdcmem, PALETTERGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen,
641         pbmi->bmiColors[0].rgbBlue), c0, 1, 1);
642     test_color(hdcmem, PALETTERGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen,
643         pbmi->bmiColors[1].rgbBlue), c1, 1, 1);
644     test_color(hdcmem, PALETTERGB(0, 0, 0), c0, 1, 1);
645     test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
646     test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c1, 1, 1);
647
648     SelectObject(hdcmem, oldbm);
649     DeleteObject(hdib);
650
651     pbmi->bmiColors[0].rgbRed = 0xff;
652     pbmi->bmiColors[0].rgbGreen = 0xff;
653     pbmi->bmiColors[0].rgbBlue = 0xff;
654     pbmi->bmiColors[1].rgbRed = 0;
655     pbmi->bmiColors[1].rgbGreen = 0;
656     pbmi->bmiColors[1].rgbBlue = 0;
657
658     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
659     ok(hdib != NULL, "CreateDIBSection failed\n");
660
661     test_dib_info(hdib, bits, &pbmi->bmiHeader);
662
663     oldbm = SelectObject(hdcmem, hdib);
664
665     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
666     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
667     ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
668        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
669        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
670        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
671
672     SelectObject(hdcmem, oldbm);
673     test_dib_info(hdib, bits, &pbmi->bmiHeader);
674     DeleteObject(hdib);
675
676     pbmi->bmiHeader.biBitCount = 4;
677     for (i = 0; i < 16; i++) {
678         pbmi->bmiColors[i].rgbRed = i;
679         pbmi->bmiColors[i].rgbGreen = 16-i;
680         pbmi->bmiColors[i].rgbBlue = 0;
681     }
682     hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
683     ok(hdib != NULL, "CreateDIBSection failed\n");
684     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
685     ok(dibsec.dsBmih.biClrUsed == 16,
686        "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 16);
687     test_dib_info(hdib, bits, &pbmi->bmiHeader);
688     DeleteObject(hdib);
689
690     pbmi->bmiHeader.biBitCount = 8;
691
692     for (i = 0; i < 128; i++) {
693         pbmi->bmiColors[i].rgbRed = 255 - i * 2;
694         pbmi->bmiColors[i].rgbGreen = i * 2;
695         pbmi->bmiColors[i].rgbBlue = 0;
696         pbmi->bmiColors[255 - i].rgbRed = 0;
697         pbmi->bmiColors[255 - i].rgbGreen = i * 2;
698         pbmi->bmiColors[255 - i].rgbBlue = 255 - i * 2;
699     }
700     hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
701     ok(hdib != NULL, "CreateDIBSection failed\n");
702     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
703     ok(dibsec.dsBmih.biClrUsed == 256,
704         "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
705
706     oldbm = SelectObject(hdcmem, hdib);
707
708     for (i = 0; i < 256; i++) {
709         test_color(hdcmem, DIBINDEX(i), 
710             RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
711         test_color(hdcmem, PALETTERGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 
712             RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
713     }
714
715     SelectObject(hdcmem, oldbm);
716     test_dib_info(hdib, bits, &pbmi->bmiHeader);
717     DeleteObject(hdib);
718
719     pbmi->bmiHeader.biBitCount = 1;
720
721     /* Now create a palette and a palette indexed dib section */
722     memset(plogpal, 0, sizeof(logpalbuf));
723     plogpal->palVersion = 0x300;
724     plogpal->palNumEntries = 2;
725     plogpal->palPalEntry[0].peRed = 0xff;
726     plogpal->palPalEntry[0].peBlue = 0xff;
727     plogpal->palPalEntry[1].peGreen = 0xff;
728
729     index = (WORD*)pbmi->bmiColors;
730     *index++ = 0;
731     *index = 1;
732     hpal = CreatePalette(plogpal);
733     ok(hpal != NULL, "CreatePalette failed\n");
734     oldpal = SelectPalette(hdc, hpal, TRUE);
735     hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
736     ok(hdib != NULL, "CreateDIBSection failed\n");
737     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
738     ok(dibsec.dsBmih.biClrUsed == 2, "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
739
740     /* The colour table has already been grabbed from the dc, so we select back the
741        old palette */
742
743     SelectPalette(hdc, oldpal, TRUE);
744     oldbm = SelectObject(hdcmem, hdib);
745     oldpal = SelectPalette(hdcmem, hpal, TRUE);
746
747     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
748     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
749     ok(rgb[0].rgbRed == 0xff && rgb[0].rgbBlue == 0xff && rgb[0].rgbGreen == 0 &&
750        rgb[1].rgbRed == 0    && rgb[1].rgbBlue == 0    && rgb[1].rgbGreen == 0xff,
751        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
752        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
753        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
754
755     c0 = RGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen, plogpal->palPalEntry[0].peBlue);
756     c1 = RGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen, plogpal->palPalEntry[1].peBlue);
757
758     test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
759     test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
760     test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
761     test_color(hdcmem, PALETTEINDEX(0), c0, 0, 1);
762     test_color(hdcmem, PALETTEINDEX(1), c1, 0, 1);
763     test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
764     test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen,
765         plogpal->palPalEntry[0].peBlue), c0, 1, 1);
766     test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen,
767         plogpal->palPalEntry[1].peBlue), c1, 1, 1);
768     test_color(hdcmem, PALETTERGB(0, 0, 0), c1, 1, 1);
769     test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
770     test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c0, 1, 1);
771     test_color(hdcmem, PALETTERGB(0, 1, 0), c1, 1, 1);
772     test_color(hdcmem, PALETTERGB(0x3f, 0, 0x3f), c1, 1, 1);
773     test_color(hdcmem, PALETTERGB(0x40, 0, 0x40), c0, 1, 1);
774
775     /* Bottom and 2nd row from top green, everything else magenta */
776     bits[0] = bits[1] = 0xff;
777     bits[13 * 4] = bits[13*4 + 1] = 0xff;
778
779     test_dib_info(hdib, bits, &pbmi->bmiHeader);
780
781     pbmi->bmiHeader.biBitCount = 32;
782
783     hdib2 = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&bits32, NULL, 0);
784     ok(hdib2 != NULL, "CreateDIBSection failed\n");
785     hdcmem2 = CreateCompatibleDC(hdc);
786     oldbm2 = SelectObject(hdcmem2, hdib2);
787
788     BitBlt(hdcmem2, 0, 0, 16,16, hdcmem, 0, 0, SRCCOPY);
789
790     ok(bits32[0] == 0xff00, "lower left pixel is %08x\n", bits32[0]);
791     ok(bits32[17] == 0xff00ff, "bottom but one, left pixel is %08x\n", bits32[17]);
792
793     SelectObject(hdcmem2, oldbm2);
794     test_dib_info(hdib2, bits32, &pbmi->bmiHeader);
795     DeleteObject(hdib2);
796
797     SelectObject(hdcmem, oldbm);
798     SelectPalette(hdcmem, oldpal, TRUE);
799     DeleteObject(hdib);
800     DeleteObject(hpal);
801
802
803     pbmi->bmiHeader.biBitCount = 8;
804
805     memset(plogpal, 0, sizeof(logpalbuf));
806     plogpal->palVersion = 0x300;
807     plogpal->palNumEntries = 256;
808
809     for (i = 0; i < 128; i++) {
810         plogpal->palPalEntry[i].peRed = 255 - i * 2;
811         plogpal->palPalEntry[i].peBlue = i * 2;
812         plogpal->palPalEntry[i].peGreen = 0;
813         plogpal->palPalEntry[255 - i].peRed = 0;
814         plogpal->palPalEntry[255 - i].peGreen = i * 2;
815         plogpal->palPalEntry[255 - i].peBlue = 255 - i * 2;
816     }
817
818     index = (WORD*)pbmi->bmiColors;
819     for (i = 0; i < 256; i++) {
820         *index++ = i;
821     }
822
823     hpal = CreatePalette(plogpal);
824     ok(hpal != NULL, "CreatePalette failed\n");
825     oldpal = SelectPalette(hdc, hpal, TRUE);
826     hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
827     ok(hdib != NULL, "CreateDIBSection failed\n");
828     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
829     ok(dibsec.dsBmih.biClrUsed == 256, "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
830
831     test_dib_info(hdib, bits, &pbmi->bmiHeader);
832
833     SelectPalette(hdc, oldpal, TRUE);
834     oldbm = SelectObject(hdcmem, hdib);
835     oldpal = SelectPalette(hdcmem, hpal, TRUE);
836
837     ret = GetDIBColorTable(hdcmem, 0, 256, rgb);
838     ok(ret == 256, "GetDIBColorTable returned %d\n", ret);
839     for (i = 0; i < 256; i++) {
840         ok(rgb[i].rgbRed == plogpal->palPalEntry[i].peRed && 
841             rgb[i].rgbBlue == plogpal->palPalEntry[i].peBlue && 
842             rgb[i].rgbGreen == plogpal->palPalEntry[i].peGreen, 
843             "GetDIBColorTable returns table %d: r%02x g%02x b%02x res%02x\n",
844             i, rgb[i].rgbRed, rgb[i].rgbGreen, rgb[i].rgbBlue, rgb[i].rgbReserved);
845     }
846
847     for (i = 0; i < 256; i++) {
848         test_color(hdcmem, DIBINDEX(i), 
849             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
850         test_color(hdcmem, PALETTEINDEX(i), 
851             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
852         test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 
853             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
854     }
855
856     SelectPalette(hdcmem, oldpal, TRUE);
857     SelectObject(hdcmem, oldbm);
858     DeleteObject(hdib);
859     DeleteObject(hpal);
860
861     DeleteDC(hdcmem);
862     DeleteDC(hdcmem2);
863     ReleaseDC(0, hdc);
864 }
865
866 static void test_mono_dibsection(void)
867 {
868     HDC hdc, memdc;
869     HBITMAP old_bm, mono_ds;
870     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
871     BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
872     BYTE bits[10 * 4];
873     BYTE *ds_bits;
874     int num;
875
876     hdc = GetDC(0);
877
878     memdc = CreateCompatibleDC(hdc);
879
880     memset(pbmi, 0, sizeof(bmibuf));
881     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
882     pbmi->bmiHeader.biHeight = 10;
883     pbmi->bmiHeader.biWidth = 10;
884     pbmi->bmiHeader.biBitCount = 1;
885     pbmi->bmiHeader.biPlanes = 1;
886     pbmi->bmiHeader.biCompression = BI_RGB;
887     pbmi->bmiColors[0].rgbRed = 0xff;
888     pbmi->bmiColors[0].rgbGreen = 0xff;
889     pbmi->bmiColors[0].rgbBlue = 0xff;
890     pbmi->bmiColors[1].rgbRed = 0x0;
891     pbmi->bmiColors[1].rgbGreen = 0x0;
892     pbmi->bmiColors[1].rgbBlue = 0x0;
893
894     /*
895      * First dib section is 'inverted' ie color[0] is white, color[1] is black
896      */
897
898     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
899     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
900     old_bm = SelectObject(memdc, mono_ds);
901
902     /* black border, white interior */
903     Rectangle(memdc, 0, 0, 10, 10);
904     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
905     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
906
907     /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
908
909     memset(bits, 0, sizeof(bits));
910     bits[0] = 0xaa;
911
912     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
913     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
914
915     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
916
917     pbmi->bmiColors[0].rgbRed = 0x0;
918     pbmi->bmiColors[0].rgbGreen = 0x0;
919     pbmi->bmiColors[0].rgbBlue = 0x0;
920     pbmi->bmiColors[1].rgbRed = 0xff;
921     pbmi->bmiColors[1].rgbGreen = 0xff;
922     pbmi->bmiColors[1].rgbBlue = 0xff;
923
924     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
925     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
926
927     SelectObject(memdc, old_bm);
928     DeleteObject(mono_ds);
929
930     /*
931      * Next dib section is 'normal' ie color[0] is black, color[1] is white
932      */
933
934     pbmi->bmiColors[0].rgbRed = 0x0;
935     pbmi->bmiColors[0].rgbGreen = 0x0;
936     pbmi->bmiColors[0].rgbBlue = 0x0;
937     pbmi->bmiColors[1].rgbRed = 0xff;
938     pbmi->bmiColors[1].rgbGreen = 0xff;
939     pbmi->bmiColors[1].rgbBlue = 0xff;
940
941     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
942     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
943     old_bm = SelectObject(memdc, mono_ds);
944
945     /* black border, white interior */
946     Rectangle(memdc, 0, 0, 10, 10);
947     ok(ds_bits[0] == 0x00, "out_bits %02x\n", ds_bits[0]);
948     ok(ds_bits[4] == 0x7f, "out_bits %02x\n", ds_bits[4]);
949
950     /* SetDIBitsToDevice with a normal bmi -> normal dib section */
951
952     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
953     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
954
955     /* SetDIBitsToDevice with a inverted bmi -> normal dib section */
956
957     pbmi->bmiColors[0].rgbRed = 0xff;
958     pbmi->bmiColors[0].rgbGreen = 0xff;
959     pbmi->bmiColors[0].rgbBlue = 0xff;
960     pbmi->bmiColors[1].rgbRed = 0x0;
961     pbmi->bmiColors[1].rgbGreen = 0x0;
962     pbmi->bmiColors[1].rgbBlue = 0x0;
963
964     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
965     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
966
967     /*
968      * Take that 'normal' dibsection and change its colour table to an 'inverted' one
969      */
970
971     pbmi->bmiColors[0].rgbRed = 0xff;
972     pbmi->bmiColors[0].rgbGreen = 0xff;
973     pbmi->bmiColors[0].rgbBlue = 0xff;
974     pbmi->bmiColors[1].rgbRed = 0x0;
975     pbmi->bmiColors[1].rgbGreen = 0x0;
976     pbmi->bmiColors[1].rgbBlue = 0x0;
977     num = SetDIBColorTable(memdc, 0, 2, pbmi->bmiColors);
978     ok(num == 2, "num = %d\n", num);
979
980     /* black border, white interior */
981     Rectangle(memdc, 0, 0, 10, 10);
982     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
983     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
984
985     /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
986
987     memset(bits, 0, sizeof(bits));
988     bits[0] = 0xaa;
989
990     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
991     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
992
993     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
994
995     pbmi->bmiColors[0].rgbRed = 0x0;
996     pbmi->bmiColors[0].rgbGreen = 0x0;
997     pbmi->bmiColors[0].rgbBlue = 0x0;
998     pbmi->bmiColors[1].rgbRed = 0xff;
999     pbmi->bmiColors[1].rgbGreen = 0xff;
1000     pbmi->bmiColors[1].rgbBlue = 0xff;
1001
1002     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1003     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1004
1005     SelectObject(memdc, old_bm);
1006     DeleteObject(mono_ds);
1007
1008     /*
1009      * Now a dib section with a strange colour map just for fun.  This behaves just like an inverted one.
1010      */
1011  
1012     pbmi->bmiColors[0].rgbRed = 0xff;
1013     pbmi->bmiColors[0].rgbGreen = 0x0;
1014     pbmi->bmiColors[0].rgbBlue = 0x0;
1015     pbmi->bmiColors[1].rgbRed = 0xfe;
1016     pbmi->bmiColors[1].rgbGreen = 0x0;
1017     pbmi->bmiColors[1].rgbBlue = 0x0;
1018
1019     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
1020     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
1021     old_bm = SelectObject(memdc, mono_ds);
1022
1023     /* black border, white interior */
1024     Rectangle(memdc, 0, 0, 10, 10);
1025     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
1026     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
1027
1028     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
1029
1030     pbmi->bmiColors[0].rgbRed = 0x0;
1031     pbmi->bmiColors[0].rgbGreen = 0x0;
1032     pbmi->bmiColors[0].rgbBlue = 0x0;
1033     pbmi->bmiColors[1].rgbRed = 0xff;
1034     pbmi->bmiColors[1].rgbGreen = 0xff;
1035     pbmi->bmiColors[1].rgbBlue = 0xff;
1036
1037     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1038     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1039
1040     /* SetDIBitsToDevice with a inverted bmi -> inverted dib section */
1041
1042     pbmi->bmiColors[0].rgbRed = 0xff;
1043     pbmi->bmiColors[0].rgbGreen = 0xff;
1044     pbmi->bmiColors[0].rgbBlue = 0xff;
1045     pbmi->bmiColors[1].rgbRed = 0x0;
1046     pbmi->bmiColors[1].rgbGreen = 0x0;
1047     pbmi->bmiColors[1].rgbBlue = 0x0;
1048
1049     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1050     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
1051
1052     SelectObject(memdc, old_bm);
1053     DeleteObject(mono_ds);
1054
1055     DeleteDC(memdc);
1056     ReleaseDC(0, hdc);
1057 }
1058
1059 static void test_bitmap(void)
1060 {
1061     char buf[256], buf_cmp[256];
1062     HBITMAP hbmp, hbmp_old;
1063     HDC hdc;
1064     BITMAP bm;
1065     BITMAP bma[2];
1066     INT ret;
1067
1068     hdc = CreateCompatibleDC(0);
1069     assert(hdc != 0);
1070
1071     SetLastError(0xdeadbeef);
1072     hbmp = CreateBitmap(0x7ffffff, 1, 1, 1, NULL);
1073     if (!hbmp)
1074     {
1075         ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1076            GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1077            "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1078     }
1079     else
1080         DeleteObject(hbmp);
1081
1082     SetLastError(0xdeadbeef);
1083     hbmp = CreateBitmap(0x7ffffff, 9, 1, 1, NULL);
1084     if (!hbmp)
1085     {
1086         ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1087            GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1088            "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1089     }
1090     else
1091         DeleteObject(hbmp);
1092
1093     SetLastError(0xdeadbeef);
1094     hbmp = CreateBitmap(0x7ffffff + 1, 1, 1, 1, NULL);
1095     ok(!hbmp, "CreateBitmap should fail\n");
1096     if (!hbmp)
1097         ok(GetLastError() == ERROR_INVALID_PARAMETER,
1098            "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1099     else
1100         DeleteObject(hbmp);
1101
1102     hbmp = CreateBitmap(15, 15, 1, 1, NULL);
1103     assert(hbmp != NULL);
1104
1105     ret = GetObject(hbmp, sizeof(bm), &bm);
1106     ok(ret == sizeof(bm), "wrong size %d\n", ret);
1107
1108     ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1109     ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1110     ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1111     ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1112     ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1113     ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1114     ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1115
1116     assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
1117     assert(sizeof(buf) == sizeof(buf_cmp));
1118
1119     ret = GetBitmapBits(hbmp, 0, NULL);
1120     ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1121
1122     memset(buf_cmp, 0xAA, sizeof(buf_cmp));
1123     memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
1124
1125     memset(buf, 0xAA, sizeof(buf));
1126     ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1127     ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1128     ok(!memcmp(buf, buf_cmp, sizeof(buf)), "buffers do not match\n");
1129
1130     hbmp_old = SelectObject(hdc, hbmp);
1131
1132     ret = GetObject(hbmp, sizeof(bm), &bm);
1133     ok(ret == sizeof(bm), "wrong size %d\n", ret);
1134
1135     ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1136     ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1137     ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1138     ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1139     ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1140     ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1141     ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1142
1143     memset(buf, 0xAA, sizeof(buf));
1144     ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1145     ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1146     ok(!memcmp(buf, buf_cmp, sizeof(buf)), "buffers do not match\n");
1147
1148     hbmp_old = SelectObject(hdc, hbmp_old);
1149     ok(hbmp_old == hbmp, "wrong old bitmap %p\n", hbmp_old);
1150
1151     /* test various buffer sizes for GetObject */
1152     ret = GetObject(hbmp, sizeof(*bma) * 2, bma);
1153     ok(ret == sizeof(*bma), "wrong size %d\n", ret);
1154
1155     ret = GetObject(hbmp, sizeof(bm) / 2, &bm);
1156     ok(ret == 0, "%d != 0\n", ret);
1157
1158     ret = GetObject(hbmp, 0, &bm);
1159     ok(ret == 0, "%d != 0\n", ret);
1160
1161     ret = GetObject(hbmp, 1, &bm);
1162     ok(ret == 0, "%d != 0\n", ret);
1163
1164     DeleteObject(hbmp);
1165     DeleteDC(hdc);
1166 }
1167
1168 static void test_bmBits(void)
1169 {
1170     BYTE bits[4];
1171     HBITMAP hbmp;
1172     BITMAP bmp;
1173
1174     memset(bits, 0, sizeof(bits));
1175     hbmp = CreateBitmap(2, 2, 1, 4, bits);
1176     ok(hbmp != NULL, "CreateBitmap failed\n");
1177
1178     memset(&bmp, 0xFF, sizeof(bmp));
1179     ok(GetObject(hbmp, sizeof(bmp), &bmp) == sizeof(bmp),
1180        "GetObject failed or returned a wrong structure size\n");
1181     ok(!bmp.bmBits, "bmBits must be NULL for device-dependent bitmaps\n");
1182
1183     DeleteObject(hbmp);
1184 }
1185
1186 static void test_GetDIBits_selected_DIB(UINT bpp)
1187 {
1188     HBITMAP dib;
1189     BITMAPINFO * info;
1190     BITMAPINFO * info2;
1191     void * bits;
1192     void * bits2;
1193     UINT dib_size, dib32_size;
1194     DWORD pixel;
1195     HDC dib_dc, dc;
1196     HBITMAP old_bmp;
1197     BOOL equalContents;
1198     UINT i;
1199     int res;
1200
1201     /* Create a DIB section with a color table */
1202
1203     info  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1204     info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1205     assert(info);
1206     assert(info2);
1207
1208     info->bmiHeader.biSize = sizeof(info->bmiHeader);
1209
1210     /* Choose width and height such that the row length (in bytes)
1211        is a multiple of 4 (makes things easier) */
1212     info->bmiHeader.biWidth = 32;
1213     info->bmiHeader.biHeight = 32;
1214     info->bmiHeader.biPlanes = 1;
1215     info->bmiHeader.biBitCount = bpp;
1216     info->bmiHeader.biCompression = BI_RGB;
1217
1218     for (i=0; i < (1u << bpp); i++)
1219     {
1220         BYTE c = i * (1 << (8 - bpp));
1221         info->bmiColors[i].rgbRed = c;
1222         info->bmiColors[i].rgbGreen = c;
1223         info->bmiColors[i].rgbBlue = c;
1224         info->bmiColors[i].rgbReserved = 0;
1225     }
1226
1227     dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
1228     assert(dib);
1229     dib_size = bpp * (info->bmiHeader.biWidth * info->bmiHeader.biHeight) / 8;
1230     dib32_size = 32 * (info->bmiHeader.biWidth * info->bmiHeader.biHeight) / 8;
1231
1232     /* Set the bits of the DIB section */
1233     for (i=0; i < dib_size; i++)
1234     {
1235         ((BYTE *)bits)[i] = i % 256;
1236     }
1237
1238     /* Select the DIB into a DC */
1239     dib_dc = CreateCompatibleDC(NULL);
1240     old_bmp = SelectObject(dib_dc, dib);
1241     dc = CreateCompatibleDC(NULL);
1242     bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dib32_size);
1243     assert(bits2);
1244
1245     /* Copy the DIB attributes but not the color table */
1246     memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1247
1248     res = GetDIBits(dc, dib, 0, info->bmiHeader.biHeight, bits2, info2, DIB_RGB_COLORS);
1249     ok(res, "GetDIBits failed\n");
1250
1251     /* Compare the color table and the bits */
1252     equalContents = TRUE;
1253     for (i=0; i < (1u << bpp); i++)
1254     {
1255         if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1256             || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1257             || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1258             || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1259         {
1260             equalContents = FALSE;
1261             break;
1262         }
1263     }
1264     ok(equalContents, "GetDIBits with DIB selected in DC: Invalid DIB color table\n");
1265
1266     equalContents = TRUE;
1267     for (i=0; i < dib_size / sizeof(DWORD); i++)
1268     {
1269         if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1270         {
1271             equalContents = FALSE;
1272             break;
1273         }
1274     }
1275     ok(equalContents, "GetDIBits with %d bpp DIB selected in DC: Invalid DIB bits\n",bpp);
1276
1277     /* Map into a 32bit-DIB */
1278     info2->bmiHeader.biBitCount = 32;
1279     res = GetDIBits(dc, dib, 0, info->bmiHeader.biHeight, bits2, info2, DIB_RGB_COLORS);
1280     ok(res, "GetDIBits failed\n");
1281
1282     /* Check if last pixel was set */
1283     pixel = ((DWORD *)bits2)[info->bmiHeader.biWidth * info->bmiHeader.biHeight - 1];
1284     ok(pixel != 0, "Pixel: 0x%08x\n", pixel);
1285
1286     HeapFree(GetProcessHeap(), 0, bits2);
1287     DeleteDC(dc);
1288
1289     SelectObject(dib_dc, old_bmp);
1290     DeleteDC(dib_dc);
1291     DeleteObject(dib);
1292
1293     HeapFree(GetProcessHeap(), 0, info2);
1294     HeapFree(GetProcessHeap(), 0, info);
1295 }
1296
1297 static void test_GetDIBits_selected_DDB(BOOL monochrome)
1298 {
1299     HBITMAP ddb;
1300     BITMAPINFO * info;
1301     BITMAPINFO * info2;
1302     void * bits;
1303     void * bits2;
1304     HDC ddb_dc, dc;
1305     HBITMAP old_bmp;
1306     BOOL equalContents;
1307     UINT width, height;
1308     UINT bpp;
1309     UINT i, j;
1310     int res;
1311
1312     width = height = 16;
1313
1314     /* Create a DDB (device-dependent bitmap) */
1315     if (monochrome)
1316     {
1317         bpp = 1;
1318         ddb = CreateBitmap(width, height, 1, 1, NULL);
1319     }
1320     else
1321     {
1322         HDC screen_dc = GetDC(NULL);
1323         bpp = GetDeviceCaps(screen_dc, BITSPIXEL) * GetDeviceCaps(screen_dc, PLANES);
1324         ddb = CreateCompatibleBitmap(screen_dc, width, height);
1325         ReleaseDC(NULL, screen_dc);
1326     }
1327
1328     /* Set the pixels */
1329     ddb_dc = CreateCompatibleDC(NULL);
1330     old_bmp = SelectObject(ddb_dc, ddb);
1331     for (i = 0; i < width; i++)
1332     {
1333         for (j=0; j < height; j++)
1334         {
1335             BYTE c = (i * width + j) % 256;
1336             SetPixelV(ddb_dc, i, j, RGB(c, c, c));
1337         }
1338     }
1339     SelectObject(ddb_dc, old_bmp);
1340
1341     info  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1342     info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1343     assert(info);
1344     assert(info2);
1345
1346     info->bmiHeader.biSize = sizeof(info->bmiHeader);
1347     info->bmiHeader.biWidth = width;
1348     info->bmiHeader.biHeight = height;
1349     info->bmiHeader.biPlanes = 1;
1350     info->bmiHeader.biBitCount = bpp;
1351     info->bmiHeader.biCompression = BI_RGB;
1352
1353     dc = CreateCompatibleDC(NULL);
1354
1355     /* Fill in biSizeImage */
1356     GetDIBits(dc, ddb, 0, height, NULL, info, DIB_RGB_COLORS);
1357     ok(info->bmiHeader.biSizeImage != 0, "GetDIBits failed to get the DIB attributes\n");
1358
1359     bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1360     bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1361     assert(bits);
1362     assert(bits2);
1363
1364     /* Get the bits */
1365     res = GetDIBits(dc, ddb, 0, height, bits, info, DIB_RGB_COLORS);
1366     ok(res, "GetDIBits failed\n");
1367
1368     /* Copy the DIB attributes but not the color table */
1369     memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1370
1371     /* Select the DDB into another DC */
1372     old_bmp = SelectObject(ddb_dc, ddb);
1373
1374     /* Get the bits */
1375     res = GetDIBits(dc, ddb, 0, height, bits2, info2, DIB_RGB_COLORS);
1376     ok(res, "GetDIBits failed\n");
1377
1378     /* Compare the color table and the bits */
1379     if (bpp <= 8)
1380     {
1381         equalContents = TRUE;
1382         for (i=0; i < (1u << bpp); i++)
1383         {
1384             if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1385                 || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1386                 || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1387                 || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1388             {
1389                 equalContents = FALSE;
1390                 break;
1391             }
1392         }
1393         ok(equalContents, "GetDIBits with DDB selected in DC: Got a different color table\n");
1394     }
1395
1396     equalContents = TRUE;
1397     for (i=0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++)
1398     {
1399         if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1400         {
1401             equalContents = FALSE;
1402         }
1403     }
1404     ok(equalContents, "GetDIBits with DDB selected in DC: Got different DIB bits\n");
1405
1406     /* Test the palette */
1407     equalContents = TRUE;
1408     if (info2->bmiHeader.biBitCount <= 8)
1409     {
1410         WORD *colors = (WORD*)info2->bmiColors;
1411
1412         /* Get the palette indices */
1413         res = GetDIBits(dc, ddb, 0, 0, NULL, info2, DIB_PAL_COLORS);
1414         ok(res, "GetDIBits failed\n");
1415
1416         for (i=0;i < 1 << info->bmiHeader.biSizeImage; i++)
1417         {
1418             if (colors[i] != i)
1419             {
1420                 equalContents = FALSE;
1421                 break;
1422             }
1423         }
1424     }
1425
1426     ok(equalContents, "GetDIBits with DDB selected in DC: non 1:1 palette indices\n");
1427
1428     HeapFree(GetProcessHeap(), 0, bits2);
1429     HeapFree(GetProcessHeap(), 0, bits);
1430     DeleteDC(dc);
1431
1432     SelectObject(ddb_dc, old_bmp);
1433     DeleteDC(ddb_dc);
1434     DeleteObject(ddb);
1435
1436     HeapFree(GetProcessHeap(), 0, info2);
1437     HeapFree(GetProcessHeap(), 0, info);
1438 }
1439
1440 static void test_GetDIBits(void)
1441 {
1442     /* 2-bytes aligned 1-bit bitmap data: 16x16 */
1443     static const BYTE bmp_bits_1[16 * 2] =
1444     {
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,
1448         0xff,0xff, 0,0, 0xff,0xff, 0,0
1449     };
1450     /* 4-bytes aligned 1-bit DIB data: 16x16 */
1451     static const BYTE dib_bits_1[16 * 4] =
1452     {
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,
1456         0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0
1457     };
1458     /* 2-bytes aligned 24-bit bitmap data: 16x16 */
1459     static const BYTE bmp_bits_24[16 * 16*3] =
1460     {
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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,
1492         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     };
1494     /* 4-bytes aligned 24-bit DIB data: 16x16 */
1495     static const BYTE dib_bits_24[16 * 16*3] =
1496     {
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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,
1528         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     };
1530     HBITMAP hbmp;
1531     BITMAP bm;
1532     HDC hdc;
1533     int i, bytes, lines;
1534     BYTE buf[1024];
1535     char bi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
1536     BITMAPINFO *bi = (BITMAPINFO *)bi_buf;
1537     PALETTEENTRY pal_ents[20];
1538
1539     hdc = GetDC(0);
1540
1541     /* 1-bit source bitmap data */
1542     hbmp = CreateBitmap(16, 16, 1, 1, bmp_bits_1);
1543     ok(hbmp != 0, "CreateBitmap failed\n");
1544
1545     memset(&bm, 0xAA, sizeof(bm));
1546     bytes = GetObject(hbmp, sizeof(bm), &bm);
1547     ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1548     ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
1549     ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1550     ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1551     ok(bm.bmWidthBytes == 2, "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1552     ok(bm.bmPlanes == 1, "wrong bmPlanes %u\n", bm.bmPlanes);
1553     ok(bm.bmBitsPixel == 1, "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1554     ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1555
1556     bytes = GetBitmapBits(hbmp, 0, NULL);
1557     ok(bytes == sizeof(bmp_bits_1), "expected 16*2 got %d bytes\n", bytes);
1558     bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1559     ok(bytes == sizeof(bmp_bits_1), "expected 16*2 got %d bytes\n", bytes);
1560     ok(!memcmp(buf, bmp_bits_1, sizeof(bmp_bits_1)), "bitmap bits don't match\n");
1561
1562     /* retrieve 1-bit DIB data */
1563     memset(bi, 0, sizeof(*bi));
1564     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1565     bi->bmiHeader.biWidth = bm.bmWidth;
1566     bi->bmiHeader.biHeight = bm.bmHeight;
1567     bi->bmiHeader.biPlanes = 1;
1568     bi->bmiHeader.biBitCount = 1;
1569     bi->bmiHeader.biCompression = BI_RGB;
1570     bi->bmiHeader.biSizeImage = 0;
1571     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1572     SetLastError(0xdeadbeef);
1573     lines = GetDIBits(0, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1574     ok(lines == 0, "GetDIBits copied %d lines with hdc = 0\n", lines);
1575     ok(GetLastError() == ERROR_INVALID_PARAMETER ||
1576        broken(GetLastError() == 0xdeadbeef), /* winnt */
1577        "wrong error %u\n", GetLastError());
1578     ok(bi->bmiHeader.biSizeImage == 0, "expected 0, got %u\n", bi->bmiHeader.biSizeImage);
1579
1580     memset(buf, 0xAA, sizeof(buf));
1581     SetLastError(0xdeadbeef);
1582     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1583     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1584        lines, bm.bmHeight, GetLastError());
1585     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1), "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1586
1587     /* the color table consists of black and white */
1588     ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1589        bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1590        "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1591        bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1592        bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1593     ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1594        bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1595        "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1596        bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1597        bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1598     for (i = 2; i < 256; i++)
1599     {
1600         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1601            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1602            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1603            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1604            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1605     }
1606
1607     /* returned bits are DWORD aligned and upside down */
1608     ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)), "DIB bits don't match\n");
1609
1610     /* Test the palette indices */
1611     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1612     SetLastError(0xdeadbeef);
1613     lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1614     ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1615     ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1616     for (i = 2; i < 256; i++)
1617         ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[1]);
1618
1619     /* retrieve 24-bit DIB data */
1620     memset(bi, 0, sizeof(*bi));
1621     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1622     bi->bmiHeader.biWidth = bm.bmWidth;
1623     bi->bmiHeader.biHeight = bm.bmHeight;
1624     bi->bmiHeader.biPlanes = 1;
1625     bi->bmiHeader.biBitCount = 24;
1626     bi->bmiHeader.biCompression = BI_RGB;
1627     bi->bmiHeader.biSizeImage = 0;
1628     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1629     memset(buf, 0xAA, sizeof(buf));
1630     SetLastError(0xdeadbeef);
1631     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1632     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1633        lines, bm.bmHeight, GetLastError());
1634     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24), "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1635
1636     /* the color table doesn't exist for 24-bit images */
1637     for (i = 0; i < 256; i++)
1638     {
1639         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1640            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1641            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1642            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1643            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1644     }
1645
1646     /* returned bits are DWORD aligned and upside down */
1647     ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1648     DeleteObject(hbmp);
1649
1650     /* 24-bit source bitmap data */
1651     hbmp = CreateCompatibleBitmap(hdc, 16, 16);
1652     ok(hbmp != 0, "CreateBitmap failed\n");
1653     SetLastError(0xdeadbeef);
1654     bi->bmiHeader.biHeight = -bm.bmHeight; /* indicate bottom-up data */
1655     lines = SetDIBits(hdc, hbmp, 0, bm.bmHeight, bmp_bits_24, bi, DIB_RGB_COLORS);
1656     ok(lines == bm.bmHeight, "SetDIBits copied %d lines of %d, error %u\n",
1657        lines, bm.bmHeight, GetLastError());
1658
1659     memset(&bm, 0xAA, sizeof(bm));
1660     bytes = GetObject(hbmp, sizeof(bm), &bm);
1661     ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1662     ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
1663     ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1664     ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1665     ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1666     ok(bm.bmPlanes == GetDeviceCaps(hdc, PLANES), "wrong bmPlanes %u\n", bm.bmPlanes);
1667     ok(bm.bmBitsPixel == GetDeviceCaps(hdc, BITSPIXEL), "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1668     ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1669
1670     bytes = GetBitmapBits(hbmp, 0, NULL);
1671     ok(bytes == bm.bmWidthBytes * bm.bmHeight, "expected %d got %d bytes\n", bm.bmWidthBytes * bm.bmHeight, bytes);
1672     bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1673     ok(bytes == bm.bmWidthBytes * bm.bmHeight, "expected %d got %d bytes\n",
1674        bm.bmWidthBytes * bm.bmHeight, bytes);
1675
1676     /* retrieve 1-bit DIB data */
1677     memset(bi, 0, sizeof(*bi));
1678     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1679     bi->bmiHeader.biWidth = bm.bmWidth;
1680     bi->bmiHeader.biHeight = bm.bmHeight;
1681     bi->bmiHeader.biPlanes = 1;
1682     bi->bmiHeader.biBitCount = 1;
1683     bi->bmiHeader.biCompression = BI_RGB;
1684     bi->bmiHeader.biSizeImage = 0;
1685     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1686     memset(buf, 0xAA, sizeof(buf));
1687     SetLastError(0xdeadbeef);
1688     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1689     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1690        lines, bm.bmHeight, GetLastError());
1691     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1), "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1692
1693     /* the color table consists of black and white */
1694     ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1695        bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1696        "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1697        bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1698        bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1699     ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1700        bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1701        "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1702        bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1703        bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1704     for (i = 2; i < 256; i++)
1705     {
1706         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1707            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1708            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1709            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1710            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1711     }
1712
1713     /* returned bits are DWORD aligned and upside down */
1714 todo_wine
1715     ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)), "DIB bits don't match\n");
1716
1717     /* Test the palette indices */
1718     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1719     SetLastError(0xdeadbeef);
1720     lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1721     ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1722     ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1723     for (i = 2; i < 256; i++)
1724         ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[i]);
1725
1726     /* retrieve 4-bit DIB data */
1727     memset(bi, 0, sizeof(*bi));
1728     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1729     bi->bmiHeader.biWidth = bm.bmWidth;
1730     bi->bmiHeader.biHeight = bm.bmHeight;
1731     bi->bmiHeader.biPlanes = 1;
1732     bi->bmiHeader.biBitCount = 4;
1733     bi->bmiHeader.biCompression = BI_RGB;
1734     bi->bmiHeader.biSizeImage = 0;
1735     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1736     memset(buf, 0xAA, sizeof(buf));
1737     SetLastError(0xdeadbeef);
1738     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1739     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1740        lines, bm.bmHeight, GetLastError());
1741
1742     GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, 20, pal_ents );
1743
1744     for (i = 0; i < 16; i++)
1745     {
1746         RGBQUAD expect;
1747         int entry = i < 8 ? i : i + 4;
1748
1749         if(entry == 7) entry = 12;
1750         else if(entry == 12) entry = 7;
1751
1752         expect.rgbRed   = pal_ents[entry].peRed;
1753         expect.rgbGreen = pal_ents[entry].peGreen;
1754         expect.rgbBlue  = pal_ents[entry].peBlue;
1755         expect.rgbReserved = 0;
1756
1757         ok(!memcmp(bi->bmiColors + i, &expect, sizeof(expect)),
1758            "expected bmiColors[%d] %x %x %x %x - got %x %x %x %x\n", i,
1759            expect.rgbRed, expect.rgbGreen, expect.rgbBlue, expect.rgbReserved,
1760            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1761            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1762     }
1763
1764     /* retrieve 8-bit DIB data */
1765     memset(bi, 0, sizeof(*bi));
1766     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1767     bi->bmiHeader.biWidth = bm.bmWidth;
1768     bi->bmiHeader.biHeight = bm.bmHeight;
1769     bi->bmiHeader.biPlanes = 1;
1770     bi->bmiHeader.biBitCount = 8;
1771     bi->bmiHeader.biCompression = BI_RGB;
1772     bi->bmiHeader.biSizeImage = 0;
1773     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1774     memset(buf, 0xAA, sizeof(buf));
1775     SetLastError(0xdeadbeef);
1776     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1777     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1778        lines, bm.bmHeight, GetLastError());
1779
1780     GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, 20, pal_ents );
1781
1782     for (i = 0; i < 256; i++)
1783     {
1784         RGBQUAD expect;
1785
1786         if (i < 10 || i >= 246)
1787         {
1788             int entry = i < 10 ? i : i - 236;
1789             expect.rgbRed   = pal_ents[entry].peRed;
1790             expect.rgbGreen = pal_ents[entry].peGreen;
1791             expect.rgbBlue  = pal_ents[entry].peBlue;
1792         }
1793         else
1794         {
1795             expect.rgbRed   = (i & 0x07) << 5;
1796             expect.rgbGreen = (i & 0x38) << 2;
1797             expect.rgbBlue  =  i & 0xc0;
1798         }
1799         expect.rgbReserved = 0;
1800
1801         ok(!memcmp(bi->bmiColors + i, &expect, sizeof(expect)),
1802            "expected bmiColors[%d] %x %x %x %x - got %x %x %x %x\n", i,
1803            expect.rgbRed, expect.rgbGreen, expect.rgbBlue, expect.rgbReserved,
1804            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1805            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1806     }
1807
1808     /* retrieve 24-bit DIB data */
1809     memset(bi, 0, sizeof(*bi));
1810     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1811     bi->bmiHeader.biWidth = bm.bmWidth;
1812     bi->bmiHeader.biHeight = bm.bmHeight;
1813     bi->bmiHeader.biPlanes = 1;
1814     bi->bmiHeader.biBitCount = 24;
1815     bi->bmiHeader.biCompression = BI_RGB;
1816     bi->bmiHeader.biSizeImage = 0;
1817     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1818     memset(buf, 0xAA, sizeof(buf));
1819     SetLastError(0xdeadbeef);
1820     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1821     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1822        lines, bm.bmHeight, GetLastError());
1823     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24), "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1824
1825     /* the color table doesn't exist for 24-bit images */
1826     for (i = 0; i < 256; i++)
1827     {
1828         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1829            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1830            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1831            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1832            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1833     }
1834
1835     /* returned bits are DWORD aligned and upside down */
1836     ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1837     DeleteObject(hbmp);
1838
1839     ReleaseDC(0, hdc);
1840 }
1841
1842 static void test_GetDIBits_BI_BITFIELDS(void)
1843 {
1844     /* Try a screen resolution detection technique
1845      * from the September 1999 issue of Windows Developer's Journal
1846      * which seems to be in widespread use.
1847      * http://www.lesher.ws/highcolor.html
1848      * http://www.lesher.ws/vidfmt.c
1849      * It hinges on being able to retrieve the bitmaps
1850      * for the three primary colors in non-paletted 16 bit mode.
1851      */
1852     char dibinfo_buf[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
1853     DWORD bits[32];
1854     LPBITMAPINFO dibinfo = (LPBITMAPINFO) dibinfo_buf;
1855     DWORD *bitmasks = (DWORD *)dibinfo->bmiColors;
1856     HDC hdc;
1857     HBITMAP hbm;
1858     int ret;
1859     void *ptr;
1860
1861     memset(dibinfo, 0, sizeof(dibinfo_buf));
1862     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1863
1864     hdc = GetDC(NULL);
1865     ok(hdc != NULL, "GetDC failed?\n");
1866     hbm = CreateCompatibleBitmap(hdc, 1, 1);
1867     ok(hbm != NULL, "CreateCompatibleBitmap failed?\n");
1868
1869     /* Call GetDIBits to fill in bmiHeader.  */
1870     ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1871     ok(ret == 1, "GetDIBits failed\n");
1872     if (dibinfo->bmiHeader.biBitCount > 8)
1873     {
1874         ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS ||
1875             broken( dibinfo->bmiHeader.biCompression == BI_RGB ), /* nt4 sp3 */
1876             "compression is %u (%d bpp)\n", dibinfo->bmiHeader.biCompression, dibinfo->bmiHeader.biBitCount );
1877
1878         if (dibinfo->bmiHeader.biCompression == BI_BITFIELDS)
1879         {
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" );
1883
1884             /* test with NULL bits pointer and correct bpp */
1885             dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1886             ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1887             ok(ret == 1, "GetDIBits failed\n");
1888
1889             ok( bitmasks[0] != 0, "red mask is not set\n" );
1890             ok( bitmasks[1] != 0, "green mask is not set\n" );
1891             ok( bitmasks[2] != 0, "blue mask is not set\n" );
1892             ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1893
1894             /* test with valid bits pointer */
1895             memset(dibinfo, 0, sizeof(dibinfo_buf));
1896             dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1897             ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1898             ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1899             dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1900             ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1901             ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1902
1903             ok( bitmasks[0] != 0, "red mask is not set\n" );
1904             ok( bitmasks[1] != 0, "green mask is not set\n" );
1905             ok( bitmasks[2] != 0, "blue mask is not set\n" );
1906             ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1907
1908             /* now with bits and 0 lines */
1909             memset(dibinfo, 0, sizeof(dibinfo_buf));
1910             dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1911             dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1912             SetLastError(0xdeadbeef);
1913             ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1914             ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1915
1916             ok( !bitmasks[0], "red mask is set\n" );
1917             ok( !bitmasks[1], "green mask is set\n" );
1918             ok( !bitmasks[2], "blue mask is set\n" );
1919             ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1920
1921             memset(bitmasks, 0, 3*sizeof(DWORD));
1922             dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1923             ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1924             ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1925
1926             ok( bitmasks[0] != 0, "red mask is not set\n" );
1927             ok( bitmasks[1] != 0, "green mask is not set\n" );
1928             ok( bitmasks[2] != 0, "blue mask is not set\n" );
1929             ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1930         }
1931     }
1932     else skip("bitmap in colortable mode, skipping BI_BITFIELDS tests\n");
1933
1934     DeleteObject(hbm);
1935
1936     /* same thing now with a 32-bpp DIB section */
1937
1938     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1939     dibinfo->bmiHeader.biWidth = 1;
1940     dibinfo->bmiHeader.biHeight = 1;
1941     dibinfo->bmiHeader.biPlanes = 1;
1942     dibinfo->bmiHeader.biBitCount = 32;
1943     dibinfo->bmiHeader.biCompression = BI_RGB;
1944     dibinfo->bmiHeader.biSizeImage = 0;
1945     dibinfo->bmiHeader.biXPelsPerMeter = 0;
1946     dibinfo->bmiHeader.biYPelsPerMeter = 0;
1947     dibinfo->bmiHeader.biClrUsed = 0;
1948     dibinfo->bmiHeader.biClrImportant = 0;
1949     bitmasks[0] = 0x0000ff;
1950     bitmasks[1] = 0x00ff00;
1951     bitmasks[2] = 0xff0000;
1952     hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
1953     ok( hbm != 0, "failed to create bitmap\n" );
1954
1955     memset(dibinfo, 0, sizeof(dibinfo_buf));
1956     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1957     ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
1958     ok(ret == 1, "GetDIBits failed\n");
1959     ok( dibinfo->bmiHeader.biBitCount == 32, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
1960
1961     ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS ||
1962         broken( dibinfo->bmiHeader.biCompression == BI_RGB ), /* nt4 sp3 */
1963         "compression is %u\n", dibinfo->bmiHeader.biCompression );
1964     ok( !bitmasks[0], "red mask is set\n" );
1965     ok( !bitmasks[1], "green mask is set\n" );
1966     ok( !bitmasks[2], "blue mask is set\n" );
1967
1968     dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1969     ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1970     ok(ret == 1, "GetDIBits failed\n");
1971     ok( dibinfo->bmiHeader.biBitCount == 32, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
1972     ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS ||
1973         broken( dibinfo->bmiHeader.biCompression == BI_RGB ), /* nt4 sp3 */
1974         "compression is %u\n", dibinfo->bmiHeader.biCompression );
1975     if (dibinfo->bmiHeader.biCompression == BI_BITFIELDS)
1976     {
1977         ok( bitmasks[0] == 0xff0000, "wrong red mask %08x\n", bitmasks[0] );
1978         ok( bitmasks[1] == 0x00ff00, "wrong green mask %08x\n", bitmasks[1] );
1979         ok( bitmasks[2] == 0x0000ff, "wrong blue mask %08x\n", bitmasks[2] );
1980     }
1981     ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1982
1983     DeleteObject(hbm);
1984
1985     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1986     dibinfo->bmiHeader.biWidth = 1;
1987     dibinfo->bmiHeader.biHeight = 1;
1988     dibinfo->bmiHeader.biPlanes = 1;
1989     dibinfo->bmiHeader.biBitCount = 32;
1990     dibinfo->bmiHeader.biCompression = BI_BITFIELDS;
1991     dibinfo->bmiHeader.biSizeImage = 0;
1992     dibinfo->bmiHeader.biXPelsPerMeter = 0;
1993     dibinfo->bmiHeader.biYPelsPerMeter = 0;
1994     dibinfo->bmiHeader.biClrUsed = 0;
1995     dibinfo->bmiHeader.biClrImportant = 0;
1996     bitmasks[0] = 0x0000ff;
1997     bitmasks[1] = 0x00ff00;
1998     bitmasks[2] = 0xff0000;
1999     hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
2000     ok( hbm != 0, "failed to create bitmap\n" );
2001
2002     if (hbm)
2003     {
2004         memset(dibinfo, 0, sizeof(dibinfo_buf));
2005         dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2006         ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
2007         ok(ret == 1, "GetDIBits failed\n");
2008
2009         ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS,
2010             "compression is %u\n", dibinfo->bmiHeader.biCompression );
2011         ok( !bitmasks[0], "red mask is set\n" );
2012         ok( !bitmasks[1], "green mask is set\n" );
2013         ok( !bitmasks[2], "blue mask is set\n" );
2014
2015         dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
2016         ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
2017         ok(ret == 1, "GetDIBits failed\n");
2018         ok( bitmasks[0] == 0x0000ff, "wrong red mask %08x\n", bitmasks[0] );
2019         ok( bitmasks[1] == 0x00ff00, "wrong green mask %08x\n", bitmasks[1] );
2020         ok( bitmasks[2] == 0xff0000, "wrong blue mask %08x\n", bitmasks[2] );
2021         ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
2022
2023         DeleteObject(hbm);
2024     }
2025
2026     /* 24-bpp DIB sections don't have bitfields */
2027
2028     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2029     dibinfo->bmiHeader.biWidth = 1;
2030     dibinfo->bmiHeader.biHeight = 1;
2031     dibinfo->bmiHeader.biPlanes = 1;
2032     dibinfo->bmiHeader.biBitCount = 24;
2033     dibinfo->bmiHeader.biCompression = BI_BITFIELDS;
2034     dibinfo->bmiHeader.biSizeImage = 0;
2035     dibinfo->bmiHeader.biXPelsPerMeter = 0;
2036     dibinfo->bmiHeader.biYPelsPerMeter = 0;
2037     dibinfo->bmiHeader.biClrUsed = 0;
2038     dibinfo->bmiHeader.biClrImportant = 0;
2039     hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
2040     ok( hbm == 0, "creating 24-bpp BI_BITFIELDS dibsection should fail\n" );
2041     dibinfo->bmiHeader.biCompression = BI_RGB;
2042     hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
2043     ok( hbm != 0, "failed to create bitmap\n" );
2044
2045     memset(dibinfo, 0, sizeof(dibinfo_buf));
2046     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2047     ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
2048     ok(ret == 1, "GetDIBits failed\n");
2049     ok( dibinfo->bmiHeader.biBitCount == 24, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
2050
2051     ok( dibinfo->bmiHeader.biCompression == BI_RGB,
2052         "compression is %u\n", dibinfo->bmiHeader.biCompression );
2053     ok( !bitmasks[0], "red mask is set\n" );
2054     ok( !bitmasks[1], "green mask is set\n" );
2055     ok( !bitmasks[2], "blue mask is set\n" );
2056
2057     dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
2058     ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
2059     ok(ret == 1, "GetDIBits failed\n");
2060     ok( dibinfo->bmiHeader.biBitCount == 24, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
2061     ok( !bitmasks[0], "red mask is set\n" );
2062     ok( !bitmasks[1], "green mask is set\n" );
2063     ok( !bitmasks[2], "blue mask is set\n" );
2064     ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
2065
2066     DeleteObject(hbm);
2067     ReleaseDC(NULL, hdc);
2068 }
2069
2070 static void test_select_object(void)
2071 {
2072     HDC hdc;
2073     HBITMAP hbm, hbm_old;
2074     INT planes, bpp, i;
2075     DWORD depths[] = {8, 15, 16, 24, 32};
2076     BITMAP bm;
2077     DWORD bytes;
2078
2079     hdc = GetDC(0);
2080     ok(hdc != 0, "GetDC(0) failed\n");
2081     hbm = CreateCompatibleBitmap(hdc, 10, 10);
2082     ok(hbm != 0, "CreateCompatibleBitmap failed\n");
2083
2084     hbm_old = SelectObject(hdc, hbm);
2085     ok(hbm_old == 0, "SelectObject should fail\n");
2086
2087     DeleteObject(hbm);
2088     ReleaseDC(0, hdc);
2089
2090     hdc = CreateCompatibleDC(0);
2091     ok(hdc != 0, "GetDC(0) failed\n");
2092     hbm = CreateCompatibleBitmap(hdc, 10, 10);
2093     ok(hbm != 0, "CreateCompatibleBitmap failed\n");
2094
2095     hbm_old = SelectObject(hdc, hbm);
2096     ok(hbm_old != 0, "SelectObject failed\n");
2097     hbm_old = SelectObject(hdc, hbm_old);
2098     ok(hbm_old == hbm, "SelectObject failed\n");
2099
2100     DeleteObject(hbm);
2101
2102     /* test an 1-bpp bitmap */
2103     planes = GetDeviceCaps(hdc, PLANES);
2104     bpp = 1;
2105
2106     hbm = CreateBitmap(10, 10, planes, bpp, NULL);
2107     ok(hbm != 0, "CreateBitmap failed\n");
2108
2109     hbm_old = SelectObject(hdc, hbm);
2110     ok(hbm_old != 0, "SelectObject failed\n");
2111     hbm_old = SelectObject(hdc, hbm_old);
2112     ok(hbm_old == hbm, "SelectObject failed\n");
2113
2114     DeleteObject(hbm);
2115
2116     for(i = 0; i < sizeof(depths)/sizeof(depths[0]); i++) {
2117         /* test a color bitmap to dc bpp matching */
2118         planes = GetDeviceCaps(hdc, PLANES);
2119         bpp = GetDeviceCaps(hdc, BITSPIXEL);
2120
2121         hbm = CreateBitmap(10, 10, planes, depths[i], NULL);
2122         ok(hbm != 0, "CreateBitmap failed\n");
2123
2124         hbm_old = SelectObject(hdc, hbm);
2125         if(depths[i] == bpp ||
2126           (bpp == 16 && depths[i] == 15)        /* 16 and 15 bpp are compatible */
2127           ) {
2128             ok(hbm_old != 0, "SelectObject failed, BITSPIXEL: %d, created depth: %d\n", bpp, depths[i]);
2129             SelectObject(hdc, hbm_old);
2130         } else {
2131             ok(hbm_old == 0, "SelectObject should fail. BITSPIXELS: %d, created depth: %d\n", bpp, depths[i]);
2132         }
2133
2134         memset(&bm, 0xAA, sizeof(bm));
2135         bytes = GetObject(hbm, sizeof(bm), &bm);
2136         ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
2137         ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
2138         ok(bm.bmWidth == 10, "wrong bmWidth %d\n", bm.bmWidth);
2139         ok(bm.bmHeight == 10, "wrong bmHeight %d\n", bm.bmHeight);
2140         ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
2141         ok(bm.bmPlanes == planes, "wrong bmPlanes %u\n", bm.bmPlanes);
2142         if(depths[i] == 15) {
2143             ok(bm.bmBitsPixel == 16, "wrong bmBitsPixel %d(15 bpp special)\n", bm.bmBitsPixel);
2144         } else {
2145             ok(bm.bmBitsPixel == depths[i], "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
2146         }
2147         ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
2148
2149         DeleteObject(hbm);
2150     }
2151
2152     DeleteDC(hdc);
2153 }
2154
2155 static void test_mono_1x1_bmp_dbg(HBITMAP hbmp, int line)
2156 {
2157     INT ret;
2158     BITMAP bm;
2159
2160     ret = GetObjectType(hbmp);
2161     ok_(__FILE__, line)(ret == OBJ_BITMAP, "the object %p is not bitmap\n", hbmp);
2162
2163     ret = GetObject(hbmp, 0, 0);
2164     ok_(__FILE__, line)(ret == sizeof(BITMAP), "object size %d\n", ret);
2165
2166     memset(&bm, 0xDA, sizeof(bm));
2167     SetLastError(0xdeadbeef);
2168     ret = GetObject(hbmp, sizeof(bm), &bm);
2169     if (!ret) /* XP, only for curObj2 */ return;
2170     ok_(__FILE__, line)(ret == sizeof(BITMAP), "GetObject returned %d, error %u\n", ret, GetLastError());
2171     ok_(__FILE__, line)(bm.bmType == 0, "wrong bmType, expected 0 got %d\n", bm.bmType);
2172     ok_(__FILE__, line)(bm.bmWidth == 1, "wrong bmWidth, expected 1 got %d\n", bm.bmWidth);
2173     ok_(__FILE__, line)(bm.bmHeight == 1, "wrong bmHeight, expected 1 got %d\n", bm.bmHeight);
2174     ok_(__FILE__, line)(bm.bmWidthBytes == 2, "wrong bmWidthBytes, expected 2 got %d\n", bm.bmWidthBytes);
2175     ok_(__FILE__, line)(bm.bmPlanes == 1, "wrong bmPlanes, expected 1 got %u\n", bm.bmPlanes);
2176     ok_(__FILE__, line)(bm.bmBitsPixel == 1, "wrong bmBitsPixel, expected 1 got %d\n", bm.bmBitsPixel);
2177     ok_(__FILE__, line)(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
2178 }
2179
2180 #define test_mono_1x1_bmp(a) test_mono_1x1_bmp_dbg((a), __LINE__)
2181
2182 static void test_CreateBitmap(void)
2183 {
2184     BITMAP bmp;
2185     HDC screenDC = GetDC(0);
2186     HDC hdc = CreateCompatibleDC(screenDC);
2187     UINT i, expect = 0;
2188
2189     /* all of these are the stock monochrome bitmap */
2190     HBITMAP bm = CreateCompatibleBitmap(hdc, 0, 0);
2191     HBITMAP bm1 = CreateCompatibleBitmap(screenDC, 0, 0);
2192     HBITMAP bm4 = CreateBitmap(0, 1, 0, 0, 0);
2193     HBITMAP bm5 = CreateDiscardableBitmap(hdc, 0, 0);
2194     HBITMAP curObj1 = GetCurrentObject(hdc, OBJ_BITMAP);
2195     HBITMAP curObj2 = GetCurrentObject(screenDC, OBJ_BITMAP);
2196
2197     /* these 2 are not the stock monochrome bitmap */
2198     HBITMAP bm2 = CreateCompatibleBitmap(hdc, 1, 1);
2199     HBITMAP bm3 = CreateBitmap(1, 1, 1, 1, 0);
2200
2201     HBITMAP old1 = SelectObject(hdc, bm2);
2202     HBITMAP old2 = SelectObject(screenDC, bm3);
2203     SelectObject(hdc, old1);
2204     SelectObject(screenDC, old2);
2205
2206     ok(bm == bm1 && bm == bm4 && bm == bm5 && bm == curObj1 && bm == old1,
2207        "0: %p, 1: %p, 4: %p, 5: %p, curObj1 %p, old1 %p\n",
2208        bm, bm1, bm4, bm5, curObj1, old1);
2209     ok(bm != bm2 && bm != bm3, "0: %p, 2: %p, 3: %p\n", bm, bm2, bm3);
2210 todo_wine
2211     ok(bm != curObj2, "0: %p, curObj2 %p\n", bm, curObj2);
2212     ok(old2 == 0, "old2 %p\n", old2);
2213
2214     test_mono_1x1_bmp(bm);
2215     test_mono_1x1_bmp(bm1);
2216     test_mono_1x1_bmp(bm2);
2217     test_mono_1x1_bmp(bm3);
2218     test_mono_1x1_bmp(bm4);
2219     test_mono_1x1_bmp(bm5);
2220     test_mono_1x1_bmp(old1);
2221     test_mono_1x1_bmp(curObj1);
2222
2223     DeleteObject(bm);
2224     DeleteObject(bm1);
2225     DeleteObject(bm2);
2226     DeleteObject(bm3);
2227     DeleteObject(bm4);
2228     DeleteObject(bm5);
2229
2230     DeleteDC(hdc);
2231     ReleaseDC(0, screenDC);
2232
2233     /* show that Windows ignores the provided bm.bmWidthBytes */
2234     bmp.bmType = 0;
2235     bmp.bmWidth = 1;
2236     bmp.bmHeight = 1;
2237     bmp.bmWidthBytes = 28;
2238     bmp.bmPlanes = 1;
2239     bmp.bmBitsPixel = 1;
2240     bmp.bmBits = NULL;
2241     bm = CreateBitmapIndirect(&bmp);
2242     ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2243     test_mono_1x1_bmp(bm);
2244     DeleteObject(bm);
2245
2246     /* Test how the bmBitsPixel field is treated */
2247     for(i = 1; i <= 33; i++) {
2248         bmp.bmType = 0;
2249         bmp.bmWidth = 1;
2250         bmp.bmHeight = 1;
2251         bmp.bmWidthBytes = 28;
2252         bmp.bmPlanes = 1;
2253         bmp.bmBitsPixel = i;
2254         bmp.bmBits = NULL;
2255         SetLastError(0xdeadbeef);
2256         bm = CreateBitmapIndirect(&bmp);
2257         if(i > 32) {
2258             DWORD error = GetLastError();
2259             ok(bm == 0, "CreateBitmapIndirect for %d bpp succeeded\n", i);
2260             ok(error == ERROR_INVALID_PARAMETER, "Got error %d, expected ERROR_INVALID_PARAMETER\n", error);
2261             DeleteObject(bm);
2262             continue;
2263         }
2264         ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2265         GetObject(bm, sizeof(bmp), &bmp);
2266         if(i == 1) {
2267             expect = 1;
2268         } else if(i <= 4) {
2269             expect = 4;
2270         } else if(i <= 8) {
2271             expect = 8;
2272         } else if(i <= 16) {
2273             expect = 16;
2274         } else if(i <= 24) {
2275             expect = 24;
2276         } else if(i <= 32) {
2277             expect = 32;
2278         }
2279         ok(bmp.bmBitsPixel == expect, "CreateBitmapIndirect for a %d bpp bitmap created a %d bpp bitmap, expected %d\n",
2280            i, bmp.bmBitsPixel, expect);
2281         DeleteObject(bm);
2282     }
2283 }
2284
2285 static void test_bitmapinfoheadersize(void)
2286 {
2287     HBITMAP hdib;
2288     BITMAPINFO bmi;
2289     BITMAPCOREINFO bci;
2290     HDC hdc = GetDC(0);
2291
2292     memset(&bmi, 0, sizeof(BITMAPINFO));
2293     bmi.bmiHeader.biHeight = 100;
2294     bmi.bmiHeader.biWidth = 512;
2295     bmi.bmiHeader.biBitCount = 24;
2296     bmi.bmiHeader.biPlanes = 1;
2297
2298     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER) - 1;
2299
2300     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2301     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2302
2303     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2304
2305     SetLastError(0xdeadbeef);
2306     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2307     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2308     DeleteObject(hdib);
2309
2310     bmi.bmiHeader.biSize++;
2311
2312     SetLastError(0xdeadbeef);
2313     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2314     ok(hdib != NULL ||
2315        broken(!hdib), /* Win98, WinMe */
2316        "CreateDIBSection error %d\n", GetLastError());
2317     DeleteObject(hdib);
2318
2319     bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
2320
2321     SetLastError(0xdeadbeef);
2322     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2323     ok(hdib != NULL ||
2324        broken(!hdib), /* Win98, WinMe */
2325        "CreateDIBSection error %d\n", GetLastError());
2326     DeleteObject(hdib);
2327
2328     bmi.bmiHeader.biSize++;
2329
2330     SetLastError(0xdeadbeef);
2331     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2332     ok(hdib != NULL ||
2333        broken(!hdib), /* Win98, WinMe */
2334        "CreateDIBSection error %d\n", GetLastError());
2335     DeleteObject(hdib);
2336
2337     bmi.bmiHeader.biSize = sizeof(BITMAPV4HEADER);
2338
2339     SetLastError(0xdeadbeef);
2340     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2341     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2342     DeleteObject(hdib);
2343
2344     bmi.bmiHeader.biSize = sizeof(BITMAPV5HEADER);
2345
2346     SetLastError(0xdeadbeef);
2347     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2348     ok(hdib != NULL ||
2349        broken(!hdib), /* Win95 */
2350        "CreateDIBSection error %d\n", GetLastError());
2351     DeleteObject(hdib);
2352
2353     memset(&bci, 0, sizeof(BITMAPCOREINFO));
2354     bci.bmciHeader.bcHeight = 100;
2355     bci.bmciHeader.bcWidth = 512;
2356     bci.bmciHeader.bcBitCount = 24;
2357     bci.bmciHeader.bcPlanes = 1;
2358
2359     bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER) - 1;
2360
2361     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2362     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2363
2364     bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
2365
2366     SetLastError(0xdeadbeef);
2367     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2368     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2369     DeleteObject(hdib);
2370
2371     bci.bmciHeader.bcSize++;
2372
2373     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2374     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2375
2376     bci.bmciHeader.bcSize = sizeof(BITMAPCOREINFO);
2377
2378     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2379     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2380
2381     ReleaseDC(0, hdc);
2382 }
2383
2384 static void test_get16dibits(void)
2385 {
2386     BYTE bits[4 * (16 / sizeof(BYTE))];
2387     HBITMAP hbmp;
2388     HDC screen_dc = GetDC(NULL);
2389     int ret;
2390     BITMAPINFO * info;
2391     int info_len = sizeof(BITMAPINFOHEADER) + 1024;
2392     BYTE *p;
2393     int overwritten_bytes = 0;
2394
2395     memset(bits, 0, sizeof(bits));
2396     hbmp = CreateBitmap(2, 2, 1, 16, bits);
2397     ok(hbmp != NULL, "CreateBitmap failed\n");
2398
2399     info  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info_len);
2400     assert(info);
2401
2402     memset(info, '!', info_len);
2403     memset(info, 0, sizeof(info->bmiHeader));
2404
2405     info->bmiHeader.biSize = sizeof(info->bmiHeader);
2406     info->bmiHeader.biWidth = 2;
2407     info->bmiHeader.biHeight = 2;
2408     info->bmiHeader.biPlanes = 1;
2409     info->bmiHeader.biCompression = BI_RGB;
2410
2411     ret = GetDIBits(screen_dc, hbmp, 0, 0, NULL, info, 0);
2412     ok(ret != 0, "GetDIBits failed got %d\n", ret);
2413
2414     for (p = ((BYTE *) info) + sizeof(info->bmiHeader); (p - ((BYTE *) info)) < info_len; p++)
2415         if (*p != '!')
2416             overwritten_bytes++;
2417     ok(overwritten_bytes == 0, "GetDIBits wrote past the buffer given\n");
2418
2419     HeapFree(GetProcessHeap(), 0, info);
2420     DeleteObject(hbmp);
2421     ReleaseDC(NULL, screen_dc);
2422 }
2423
2424 static BOOL compare_buffers_no_alpha(UINT32 *a, UINT32 *b, int length)
2425 {
2426     int i;
2427     for(i = 0; i < length; i++)
2428         if((a[i] & 0x00FFFFFF) != (b[i] & 0x00FFFFFF))
2429             return FALSE;
2430     return TRUE;
2431 }
2432
2433 static void check_BitBlt_pixel(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2434                                DWORD dwRop, UINT32 expected, int line)
2435 {
2436     *srcBuffer = 0xFEDCBA98;
2437     *dstBuffer = 0x89ABCDEF;
2438     Rectangle(hdcSrc, 0, 0, 1, 1);  /* A null operation to ensure dibs are coerced to X11 */
2439     BitBlt(hdcDst, 0, 0, 1, 1, hdcSrc, 0, 0, dwRop);
2440     ok(expected == *dstBuffer,
2441         "BitBlt with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2442         dwRop, expected, *dstBuffer, line);
2443 }
2444
2445 static void test_BitBlt(void)
2446 {
2447     HBITMAP bmpDst, bmpSrc;
2448     HBITMAP oldDst, oldSrc;
2449     HDC hdcScreen, hdcDst, hdcSrc;
2450     UINT32 *dstBuffer, *srcBuffer;
2451     HBRUSH hBrush, hOldBrush;
2452     BITMAPINFO bitmapInfo;
2453
2454     memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2455     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2456     bitmapInfo.bmiHeader.biWidth = 1;
2457     bitmapInfo.bmiHeader.biHeight = 1;
2458     bitmapInfo.bmiHeader.biPlanes = 1;
2459     bitmapInfo.bmiHeader.biBitCount = 32;
2460     bitmapInfo.bmiHeader.biCompression = BI_RGB;
2461     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
2462
2463     hdcScreen = CreateCompatibleDC(0);
2464     hdcDst = CreateCompatibleDC(hdcScreen);
2465     hdcSrc = CreateCompatibleDC(hdcDst);
2466
2467     /* Setup the destination dib section */
2468     bmpDst = CreateDIBSection(hdcScreen, &bitmapInfo, DIB_RGB_COLORS, (void**)&dstBuffer,
2469         NULL, 0);
2470     oldDst = SelectObject(hdcDst, bmpDst);
2471
2472     hBrush = CreateSolidBrush(0x012345678);
2473     hOldBrush = SelectObject(hdcDst, hBrush);
2474
2475     /* Setup the source dib section */
2476     bmpSrc = CreateDIBSection(hdcScreen, &bitmapInfo, DIB_RGB_COLORS, (void**)&srcBuffer,
2477         NULL, 0);
2478     oldSrc = SelectObject(hdcSrc, bmpSrc);
2479
2480     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2481     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2482     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2483     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2484     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2485     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2486     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2487     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2488     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2489     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2490     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2491     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2492     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2493     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2494     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2495
2496     /* Tidy up */
2497     SelectObject(hdcSrc, oldSrc);
2498     DeleteObject(bmpSrc);
2499     DeleteDC(hdcSrc);
2500
2501     SelectObject(hdcDst, hOldBrush);
2502     DeleteObject(hBrush);
2503     SelectObject(hdcDst, oldDst);
2504     DeleteObject(bmpDst);
2505     DeleteDC(hdcDst);
2506
2507
2508     DeleteDC(hdcScreen);
2509 }
2510
2511 static void check_StretchBlt_pixel(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2512                                    DWORD dwRop, UINT32 expected, int line)
2513 {
2514     *srcBuffer = 0xFEDCBA98;
2515     *dstBuffer = 0x89ABCDEF;
2516     StretchBlt(hdcDst, 0, 0, 2, 1, hdcSrc, 0, 0, 1, 1, dwRop);
2517     ok(expected == *dstBuffer,
2518         "StretchBlt with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2519         dwRop, expected, *dstBuffer, line);
2520 }
2521
2522 static void check_StretchBlt_stretch(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2523                                      int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,
2524                                      int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
2525                                      UINT32 expected[4], UINT32 legacy_expected[4], int line)
2526 {
2527     memset(dstBuffer, 0, 16);
2528     StretchBlt(hdcDst, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
2529                hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
2530     ok(memcmp(dstBuffer, expected, 16) == 0 ||
2531         broken(compare_buffers_no_alpha(dstBuffer, legacy_expected, 4)),
2532         "StretchBlt expected { %08X, %08X, %08X, %08X } got { %08X, %08X, %08X, %08X } "
2533         "stretching { %d, %d, %d, %d } to { %d, %d, %d, %d } from line %d\n",
2534         expected[0], expected[1], expected[2], expected[3],
2535         dstBuffer[0], dstBuffer[1], dstBuffer[2], dstBuffer[3],
2536         nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2537         nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, line);
2538 }
2539
2540 static void test_StretchBlt(void)
2541 {
2542     HBITMAP bmpDst, bmpSrc;
2543     HBITMAP oldDst, oldSrc;
2544     HDC hdcScreen, hdcDst, hdcSrc;
2545     UINT32 *dstBuffer, *srcBuffer;
2546     HBRUSH hBrush, hOldBrush;
2547     BITMAPINFO biDst, biSrc;
2548     UINT32 expected[4], legacy_expected[4];
2549
2550     memset(&biDst, 0, sizeof(BITMAPINFO));
2551     biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2552     biDst.bmiHeader.biWidth = 2;
2553     biDst.bmiHeader.biHeight = -2;
2554     biDst.bmiHeader.biPlanes = 1;
2555     biDst.bmiHeader.biBitCount = 32;
2556     biDst.bmiHeader.biCompression = BI_RGB;
2557     memcpy(&biSrc, &biDst, sizeof(BITMAPINFO));
2558
2559     hdcScreen = CreateCompatibleDC(0);
2560     hdcDst = CreateCompatibleDC(hdcScreen);
2561     hdcSrc = CreateCompatibleDC(hdcDst);
2562
2563     /* Pixel Tests */
2564     bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2565         NULL, 0);
2566     oldDst = SelectObject(hdcDst, bmpDst);
2567
2568     bmpSrc = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&srcBuffer,
2569         NULL, 0);
2570     oldSrc = SelectObject(hdcSrc, bmpSrc);
2571
2572     hBrush = CreateSolidBrush(0x012345678);
2573     hOldBrush = SelectObject(hdcDst, hBrush);
2574
2575     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2576     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2577     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2578     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2579     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2580     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2581     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2582     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2583     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2584     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2585     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2586     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2587     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2588     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2589     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2590
2591     SelectObject(hdcDst, hOldBrush);
2592     DeleteObject(hBrush);
2593
2594     /* Top-down to top-down tests */
2595     srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2596     srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2597
2598     expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2599     expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2600     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2601                              0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2602
2603     expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2604     expected[2] = 0x00000000, expected[3] = 0x00000000;
2605     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2606                              0, 0, 1, 1, 0, 0, 1, 1, expected, expected, __LINE__);
2607
2608     expected[0] = 0xCAFED00D, expected[1] = 0xCAFED00D;
2609     expected[2] = 0xCAFED00D, expected[3] = 0xCAFED00D;
2610     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2611                              0, 0, 2, 2, 0, 0, 1, 1, expected, expected, __LINE__);
2612
2613     expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2614     expected[2] = 0x00000000, expected[3] = 0x00000000;
2615     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2616                              0, 0, 1, 1, 0, 0, 2, 2, expected, expected, __LINE__);
2617
2618     expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2619     expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2620     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2621                              0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2622
2623     expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2624     expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2625     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2626                              1, 1, -2, -2, 0, 0, 2, 2, expected, expected, __LINE__);
2627
2628     /* This result seems broken. One might expect the following result:
2629      * 0xCAFED00D 0xFEEDFACE
2630      * 0xFEDCBA98 0x76543210
2631      */
2632     expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2633     expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2634     legacy_expected[0] = 0xCAFED00D, legacy_expected[1] = 0x00000000;
2635     legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2636     todo_wine check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2637                                        1, 1, -2, -2, 1, 1, -2, -2, expected,
2638                                        legacy_expected, __LINE__);
2639
2640     expected[0] = 0x00000000, expected[1] = 0x00000000;
2641     expected[2] = 0x00000000, expected[3] = 0xCAFED00D;
2642     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2643                              1, 1, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2644
2645     SelectObject(hdcDst, oldDst);
2646     DeleteObject(bmpDst);
2647
2648     /* Top-down to bottom-up tests */
2649     biDst.bmiHeader.biHeight = 2;
2650     bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2651         NULL, 0);
2652     oldDst = SelectObject(hdcDst, bmpDst);
2653
2654     expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2655     expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2656     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2657                              0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2658
2659     expected[0] = 0xFEEDFACE, expected[1] = 0xCAFED00D;
2660     expected[2] = 0x76543210, expected[3] = 0xFEDCBA98;
2661     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2662                              0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2663
2664     SelectObject(hdcSrc, oldSrc);
2665     DeleteObject(bmpSrc);
2666
2667     /* Bottom-up to bottom-up tests */
2668     biSrc.bmiHeader.biHeight = 2;
2669     bmpSrc = CreateDIBSection(hdcScreen, &biSrc, DIB_RGB_COLORS, (void**)&srcBuffer,
2670         NULL, 0);
2671     srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2672     srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2673     oldSrc = SelectObject(hdcSrc, bmpSrc);
2674
2675     expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2676     expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2677     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2678                              0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2679
2680     expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2681     expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2682     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2683                              0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2684
2685     SelectObject(hdcDst, oldDst);
2686     DeleteObject(bmpDst);
2687
2688     /* Bottom-up to top-down tests */
2689     biDst.bmiHeader.biHeight = -2;
2690     bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2691         NULL, 0);
2692     oldDst = SelectObject(hdcDst, bmpDst);
2693
2694     expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2695     expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2696     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2697                              0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2698
2699     expected[0] = 0xFEEDFACE, expected[1] = 0xCAFED00D;
2700     expected[2] = 0x76543210, expected[3] = 0xFEDCBA98;
2701     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2702                              0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2703
2704     /* Tidy up */
2705     SelectObject(hdcSrc, oldSrc);
2706     DeleteObject(bmpSrc);
2707     DeleteDC(hdcSrc);
2708
2709     SelectObject(hdcDst, oldDst);
2710     DeleteObject(bmpDst);
2711     DeleteDC(hdcDst);
2712
2713     DeleteDC(hdcScreen);
2714 }
2715
2716 static void check_StretchDIBits_pixel(HDC hdcDst, UINT32 *dstBuffer, UINT32 *srcBuffer,
2717                                       DWORD dwRop, UINT32 expected, int line)
2718 {
2719     const UINT32 buffer[2] = { 0xFEDCBA98, 0 };
2720     BITMAPINFO bitmapInfo;
2721
2722     memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2723     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2724     bitmapInfo.bmiHeader.biWidth = 2;
2725     bitmapInfo.bmiHeader.biHeight = 1;
2726     bitmapInfo.bmiHeader.biPlanes = 1;
2727     bitmapInfo.bmiHeader.biBitCount = 32;
2728     bitmapInfo.bmiHeader.biCompression = BI_RGB;
2729     bitmapInfo.bmiHeader.biSizeImage = sizeof(buffer);
2730
2731     *dstBuffer = 0x89ABCDEF;
2732
2733     StretchDIBits(hdcDst, 0, 0, 2, 1, 0, 0, 1, 1, &buffer, &bitmapInfo, DIB_RGB_COLORS, dwRop);
2734     ok(expected == *dstBuffer,
2735         "StretchDIBits with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2736         dwRop, expected, *dstBuffer, line);
2737 }
2738
2739 static void check_StretchDIBits_stretch(HDC hdcDst, UINT32 *dstBuffer, UINT32 *srcBuffer,
2740                                         int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,
2741                                         int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
2742                                         UINT32 expected[4], UINT32 legacy_expected[4], int line)
2743 {
2744     BITMAPINFO bitmapInfo;
2745
2746     memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2747     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2748     bitmapInfo.bmiHeader.biWidth = 2;
2749     bitmapInfo.bmiHeader.biHeight = -2;
2750     bitmapInfo.bmiHeader.biPlanes = 1;
2751     bitmapInfo.bmiHeader.biBitCount = 32;
2752     bitmapInfo.bmiHeader.biCompression = BI_RGB;
2753
2754     memset(dstBuffer, 0, 16);
2755     StretchDIBits(hdcDst, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
2756                   nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2757                   srcBuffer, &bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
2758     ok(memcmp(dstBuffer, expected, 16) == 0,
2759         "StretchDIBits expected { %08X, %08X, %08X, %08X } got { %08X, %08X, %08X, %08X } "
2760         "stretching { %d, %d, %d, %d } to { %d, %d, %d, %d } from line %d\n",
2761         expected[0], expected[1], expected[2], expected[3],
2762         dstBuffer[0], dstBuffer[1], dstBuffer[2], dstBuffer[3],
2763         nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2764         nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, line);
2765 }
2766
2767 static void test_StretchDIBits(void)
2768 {
2769     HBITMAP bmpDst;
2770     HBITMAP oldDst;
2771     HDC hdcScreen, hdcDst;
2772     UINT32 *dstBuffer, srcBuffer[4];
2773     HBRUSH hBrush, hOldBrush;
2774     BITMAPINFO biDst;
2775     UINT32 expected[4], legacy_expected[4];
2776
2777     memset(&biDst, 0, sizeof(BITMAPINFO));
2778     biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2779     biDst.bmiHeader.biWidth = 2;
2780     biDst.bmiHeader.biHeight = -2;
2781     biDst.bmiHeader.biPlanes = 1;
2782     biDst.bmiHeader.biBitCount = 32;
2783     biDst.bmiHeader.biCompression = BI_RGB;
2784
2785     hdcScreen = CreateCompatibleDC(0);
2786     hdcDst = CreateCompatibleDC(hdcScreen);
2787
2788     /* Pixel Tests */
2789     bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2790         NULL, 0);
2791     oldDst = SelectObject(hdcDst, bmpDst);
2792
2793     hBrush = CreateSolidBrush(0x012345678);
2794     hOldBrush = SelectObject(hdcDst, hBrush);
2795
2796     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2797     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2798     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2799     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2800     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2801     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2802     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2803     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2804     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2805     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2806     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2807     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2808     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2809     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2810     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2811
2812     SelectObject(hdcDst, hOldBrush);
2813     DeleteObject(hBrush);
2814
2815     /* Top-down destination tests */
2816     srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2817     srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2818
2819     expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2820     expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2821     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2822                                 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2823
2824     expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2825     expected[2] = 0x00000000, expected[3] = 0x00000000;
2826     legacy_expected[0] = 0xFEDCBA98, legacy_expected[1] = 0x00000000;
2827     legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2828     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2829                                 0, 0, 1, 1, 0, 0, 1, 1, expected, legacy_expected, __LINE__);
2830
2831     expected[0] = 0xFEDCBA98, expected[1] = 0xFEDCBA98;
2832     expected[2] = 0xFEDCBA98, expected[3] = 0xFEDCBA98;
2833     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2834                                 0, 0, 2, 2, 0, 0, 1, 1, expected, expected, __LINE__);
2835
2836     expected[0] = 0x42441000, expected[1] = 0x00000000;
2837     expected[2] = 0x00000000, expected[3] = 0x00000000;
2838     legacy_expected[0] = 0x00543210, legacy_expected[1] = 0x00000000;
2839     legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2840     todo_wine check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2841                                 0, 0, 1, 1, 0, 0, 2, 2, expected, legacy_expected, __LINE__);
2842
2843     expected[0] = 0x00000000, expected[1] = 0x00000000;
2844     expected[2] = 0x00000000, expected[3] = 0x00000000;
2845     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2846                                 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2847
2848     expected[0] = 0x00000000, expected[1] = 0x00000000;
2849     expected[2] = 0x00000000, expected[3] = 0x00000000;
2850     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2851                                 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2852
2853     expected[0] = 0x00000000, expected[1] = 0x00000000;
2854     expected[2] = 0x00000000, expected[3] = 0x00000000;
2855     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2856                                 1, 1, -2, -2, 1, 1, -2, -2, expected, expected, __LINE__);
2857
2858     expected[0] = 0x00000000, expected[1] = 0x00000000;
2859     expected[2] = 0x00000000, expected[3] = 0xCAFED00D;
2860     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2861                                 1, 1, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2862
2863     SelectObject(hdcDst, oldDst);
2864     DeleteObject(bmpDst);
2865
2866     /* Bottom up destination tests */
2867     biDst.bmiHeader.biHeight = 2;
2868     bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2869         NULL, 0);
2870     oldDst = SelectObject(hdcDst, bmpDst);
2871
2872     expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2873     expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2874     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2875                                 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2876
2877     /* Tidy up */
2878     SelectObject(hdcDst, oldDst);
2879     DeleteObject(bmpDst);
2880     DeleteDC(hdcDst);
2881
2882     DeleteDC(hdcScreen);
2883 }
2884
2885 static void test_GdiAlphaBlend(void)
2886 {
2887     /* test out-of-bound parameters for GdiAlphaBlend */
2888     HDC hdcNull;
2889
2890     HDC hdcDst;
2891     HBITMAP bmpDst;
2892     HBITMAP oldDst;
2893
2894     BITMAPINFO bmi;
2895     HDC hdcSrc;
2896     HBITMAP bmpSrc;
2897     HBITMAP oldSrc;
2898     LPVOID bits;
2899
2900     BLENDFUNCTION blend;
2901
2902     if (!pGdiAlphaBlend)
2903     {
2904         win_skip("GdiAlphaBlend() is not implemented\n");
2905         return;
2906     }
2907
2908     hdcNull = GetDC(NULL);
2909     hdcDst = CreateCompatibleDC(hdcNull);
2910     bmpDst = CreateCompatibleBitmap(hdcNull, 100, 100);
2911     hdcSrc = CreateCompatibleDC(hdcNull);
2912
2913     memset(&bmi, 0, sizeof(bmi));  /* as of Wine 0.9.44 we require the src to be a DIB section */
2914     bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
2915     bmi.bmiHeader.biHeight = 20;
2916     bmi.bmiHeader.biWidth = 20;
2917     bmi.bmiHeader.biBitCount = 32;
2918     bmi.bmiHeader.biPlanes = 1;
2919     bmi.bmiHeader.biCompression = BI_RGB;
2920     bmpSrc = CreateDIBSection(hdcDst, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
2921     ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2922
2923     oldDst = SelectObject(hdcDst, bmpDst);
2924     oldSrc = SelectObject(hdcSrc, bmpSrc);
2925
2926     blend.BlendOp = AC_SRC_OVER;
2927     blend.BlendFlags = 0;
2928     blend.SourceConstantAlpha = 128;
2929     blend.AlphaFormat = 0;
2930
2931     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, 0, 20, 20, blend), TRUE, BOOL, "%d");
2932     SetLastError(0xdeadbeef);
2933     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), FALSE, BOOL, "%d");
2934     expect_eq(GetLastError(), ERROR_INVALID_PARAMETER, int, "%d");
2935     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), FALSE, BOOL, "%d");
2936     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 15, 0, 10, 10, blend), FALSE, BOOL, "%d");
2937     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2938     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2939
2940     SetWindowOrgEx(hdcSrc, -10, -10, NULL);
2941     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), TRUE, BOOL, "%d");
2942     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), TRUE, BOOL, "%d");
2943     SetMapMode(hdcSrc, MM_ANISOTROPIC);
2944     ScaleWindowExtEx(hdcSrc, 10, 1, 10, 1, NULL);
2945     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 30, 30, blend), TRUE, BOOL, "%d");
2946     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 30, 30, blend), TRUE, BOOL, "%d");
2947
2948     SetLastError(0xdeadbeef);
2949     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, NULL, 0, 0, 20, 20, blend), FALSE, BOOL, "%d");
2950     expect_eq(GetLastError(), 0xdeadbeef, int, "%d");
2951
2952     SelectObject(hdcDst, oldDst);
2953     SelectObject(hdcSrc, oldSrc);
2954     DeleteObject(bmpSrc);
2955     DeleteObject(bmpDst);
2956     DeleteDC(hdcDst);
2957     DeleteDC(hdcSrc);
2958
2959     ReleaseDC(NULL, hdcNull);
2960
2961 }
2962
2963 static void test_clipping(void)
2964 {
2965     HBITMAP bmpDst;
2966     HBITMAP bmpSrc;
2967     HRGN hRgn;
2968     LPVOID bits;
2969     BOOL result;
2970
2971     HDC hdcDst = CreateCompatibleDC( NULL );
2972     HDC hdcSrc = CreateCompatibleDC( NULL );
2973
2974     BITMAPINFO bmpinfo={{0}};
2975     bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2976     bmpinfo.bmiHeader.biWidth = 100;
2977     bmpinfo.bmiHeader.biHeight = 100;
2978     bmpinfo.bmiHeader.biPlanes = 1;
2979     bmpinfo.bmiHeader.biBitCount = GetDeviceCaps( hdcDst, BITSPIXEL );
2980     bmpinfo.bmiHeader.biCompression = BI_RGB;
2981
2982     bmpDst = CreateDIBSection( hdcDst, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2983     ok(bmpDst != NULL, "Couldn't create destination bitmap\n");
2984     SelectObject( hdcDst, bmpDst );
2985
2986     bmpSrc = CreateDIBSection( hdcSrc, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2987     ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2988     SelectObject( hdcSrc, bmpSrc );
2989
2990     result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 100, 100, SRCCOPY );
2991     ok(result, "BitBlt failed\n");
2992
2993     hRgn = CreateRectRgn( 0,0,0,0 );
2994     SelectClipRgn( hdcDst, hRgn );
2995
2996     result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 0, 0, SRCCOPY );
2997     ok(result, "BitBlt failed\n");
2998
2999     DeleteObject( bmpDst );
3000     DeleteObject( bmpSrc );
3001     DeleteObject( hRgn );
3002     DeleteDC( hdcDst );
3003     DeleteDC( hdcSrc );
3004 }
3005
3006 static void test_32bit_bitmap_blt(void)
3007 {
3008     BITMAPINFO biDst;
3009     HBITMAP bmpSrc, bmpDst;
3010     HBITMAP oldSrc, oldDst;
3011     HDC hdcSrc, hdcDst, hdcScreen;
3012     UINT32 *dstBuffer;
3013     DWORD colorSrc = 0x11223344;
3014
3015     memset(&biDst, 0, sizeof(BITMAPINFO));
3016     biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3017     biDst.bmiHeader.biWidth = 2;
3018     biDst.bmiHeader.biHeight = -2;
3019     biDst.bmiHeader.biPlanes = 1;
3020     biDst.bmiHeader.biBitCount = 32;
3021     biDst.bmiHeader.biCompression = BI_RGB;
3022
3023     hdcScreen = CreateCompatibleDC(0);
3024     if(GetDeviceCaps(hdcScreen, BITSPIXEL) != 32)
3025     {
3026         DeleteDC(hdcScreen);
3027         trace("Skipping 32-bit DDB test\n");
3028         return;
3029     }
3030
3031     hdcSrc = CreateCompatibleDC(hdcScreen);
3032     bmpSrc = CreateBitmap(1, 1, 1, 32, &colorSrc);
3033     oldSrc = SelectObject(hdcSrc, bmpSrc);
3034
3035     hdcDst = CreateCompatibleDC(hdcScreen);
3036     bmpDst = CreateDIBSection(hdcDst, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer, NULL, 0);
3037     oldDst = SelectObject(hdcDst, bmpDst);
3038
3039     StretchBlt(hdcDst, 0, 0, 1, 1, hdcSrc, 0, 0, 1, 1, SRCCOPY);
3040     ok(dstBuffer[0] == colorSrc, "Expected color=%x, received color=%x\n", colorSrc, dstBuffer[0]);
3041
3042     /* Tidy up */
3043     SelectObject(hdcDst, oldDst);
3044     DeleteObject(bmpDst);
3045     DeleteDC(hdcDst);
3046
3047     SelectObject(hdcSrc, oldSrc);
3048     DeleteObject(bmpSrc);
3049     DeleteDC(hdcSrc);
3050
3051     DeleteDC(hdcScreen);
3052 }
3053
3054 /*
3055  * Used by test_GetDIBits_top_down to create the bitmap to test against.
3056  */
3057 static void setup_picture(char *picture, int bpp)
3058 {
3059     int i;
3060
3061     switch(bpp)
3062     {
3063         case 16:
3064         case 32:
3065             /*Set the first byte in each pixel to the index of that pixel.*/
3066             for (i = 0; i < 4; i++)
3067                 picture[i * (bpp / 8)] = i;
3068             break;
3069         case 24:
3070             picture[0] = 0;
3071             picture[3] = 1;
3072             /*Each scanline in a bitmap must be a multiple of 4 bytes long.*/
3073             picture[8] = 2;
3074             picture[11] = 3;
3075             break;
3076     }
3077 }
3078
3079 static void test_GetDIBits_top_down(int bpp)
3080 {
3081     BITMAPINFO bi;
3082     HBITMAP bmptb, bmpbt;
3083     HDC hdc;
3084     int pictureOut[4];
3085     int *picture;
3086     int statusCode;
3087
3088     bi.bmiHeader.biSize=sizeof(bi.bmiHeader);
3089     bi.bmiHeader.biWidth=2;
3090     bi.bmiHeader.biHeight=2;
3091     bi.bmiHeader.biPlanes=1;
3092     bi.bmiHeader.biBitCount=bpp;
3093     bi.bmiHeader.biCompression=BI_RGB;
3094
3095     /*Get the device context for the screen.*/
3096     hdc = GetDC(NULL);
3097     ok(hdc != NULL, "Could not get a handle to a device context.\n");
3098
3099     /*Create the bottom to top image (image's bottom scan line is at the top in memory).*/
3100     bmpbt = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void**)&picture, NULL, 0);
3101     ok(bmpbt != NULL, "Could not create a DIB section.\n");
3102     /*Now that we have a pointer to the pixels, we write to them.*/
3103     setup_picture((char*)picture, bpp);
3104     /*Create the top to bottom image (images' bottom scan line is at the bottom in memory).*/
3105     bi.bmiHeader.biHeight=-2; /*We specify that we want a top to bottom image by specifying a negative height.*/
3106     bmptb = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void**)&picture, NULL, 0);
3107     ok(bmptb != NULL, "Could not create a DIB section.\n");
3108     /*Write to this top to bottom bitmap.*/
3109     setup_picture((char*)picture, bpp);
3110
3111     bi.bmiHeader.biWidth = 1;
3112
3113     bi.bmiHeader.biHeight = 2;
3114     statusCode = GetDIBits(hdc, bmpbt, 0, 1, pictureOut, &bi, DIB_RGB_COLORS);
3115     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3116     /*Check the first byte of the pixel.*/
3117     ok((char)pictureOut[0] == 0, "Bottom-up -> bottom-up: first pixel should be 0 but was %d.\n", (char)pictureOut[0]);
3118     statusCode = GetDIBits(hdc, bmptb, 0, 1, pictureOut, &bi, DIB_RGB_COLORS);
3119     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3120     ok((char)pictureOut[0] == 2, "Top-down -> bottom-up: first pixel should be 2 but was %d.\n", (char)pictureOut[0]);
3121     /*Check second scanline.*/
3122     statusCode = GetDIBits(hdc, bmptb, 1, 1, pictureOut, &bi, DIB_RGB_COLORS);
3123     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3124     ok((char)pictureOut[0] == 0, "Top-down -> bottom-up: first pixel should be 0 but was %d.\n", (char)pictureOut[0]);
3125     statusCode = GetDIBits(hdc, bmpbt, 1, 1, pictureOut, &bi, DIB_RGB_COLORS);
3126     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3127     ok((char)pictureOut[0] == 2, "Bottom-up -> bottom-up: first pixel should be 2 but was %d.\n", (char)pictureOut[0]);
3128     /*Check both scanlines.*/
3129     statusCode = GetDIBits(hdc, bmptb, 0, 2, pictureOut, &bi, DIB_RGB_COLORS);
3130     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3131     ok((char)pictureOut[0] == 2, "Top-down -> bottom-up: first scanline should be 2 but was %d.\n", (char)pictureOut[0]);
3132     ok((char)pictureOut[1] == 0, "Top-down -> bottom-up: second scanline should be 0 but was %d.\n", (char)pictureOut[0]);
3133     statusCode = GetDIBits(hdc, bmpbt, 0, 2, pictureOut, &bi, DIB_RGB_COLORS);
3134     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3135     ok((char)pictureOut[0] == 0, "Bottom up -> bottom-up: first scanline should be 0 but was %d.\n", (char)pictureOut[0]);
3136     ok((char)pictureOut[1] == 2, "Bottom up -> bottom-up: second scanline should be 2 but was %d.\n", (char)pictureOut[0]);
3137
3138     /*Make destination bitmap top-down.*/
3139     bi.bmiHeader.biHeight = -2;
3140     statusCode = GetDIBits(hdc, bmpbt, 0, 1, pictureOut, &bi, DIB_RGB_COLORS);
3141     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3142     ok((char)pictureOut[0] == 0, "Bottom-up -> top-down: first pixel should be 0 but was %d.\n", (char)pictureOut[0]);
3143     statusCode = GetDIBits(hdc, bmptb, 0, 1, pictureOut, &bi, DIB_RGB_COLORS);
3144     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3145     ok((char)pictureOut[0] == 2, "Top-down -> top-down: first pixel should be 2 but was %d.\n", (char)pictureOut[0]);
3146     /*Check second scanline.*/
3147     statusCode = GetDIBits(hdc, bmptb, 1, 1, pictureOut, &bi, DIB_RGB_COLORS);
3148     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3149     ok((char)pictureOut[0] == 0, "Top-down -> bottom-up: first pixel should be 0 but was %d.\n", (char)pictureOut[0]);
3150     statusCode = GetDIBits(hdc, bmpbt, 1, 1, pictureOut, &bi, DIB_RGB_COLORS);
3151     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3152     ok((char)pictureOut[0] == 2, "Top-down -> bottom-up: first pixel should be 2 but was %d.\n", (char)pictureOut[0]);
3153     /*Check both scanlines.*/
3154     statusCode = GetDIBits(hdc, bmptb, 0, 2, pictureOut, &bi, DIB_RGB_COLORS);
3155     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3156     ok((char)pictureOut[0] == 0, "Top-down -> top-down: first scanline should be 0 but was %d.\n", (char)pictureOut[0]);
3157     ok((char)pictureOut[1] == 2, "Top-down -> top-down: second scanline should be 2 but was %d.\n", (char)pictureOut[0]);
3158     statusCode = GetDIBits(hdc, bmpbt, 0, 2, pictureOut, &bi, DIB_RGB_COLORS);
3159     ok(statusCode, "Failed to call GetDIBits. Status code: %d.\n", statusCode);
3160     ok((char)pictureOut[0] == 2, "Bottom up -> top-down: first scanline should be 2 but was %d.\n", (char)pictureOut[0]);
3161     ok((char)pictureOut[1] == 0, "Bottom up -> top-down: second scanline should be 0 but was %d.\n", (char)pictureOut[0]);
3162
3163     DeleteObject(bmpbt);
3164     DeleteObject(bmptb);
3165 }
3166
3167 static void test_GetSetDIBits_rtl(void)
3168 {
3169     HDC hdc, hdc_mem;
3170     HBITMAP bitmap, orig_bitmap;
3171     BITMAPINFO info;
3172     int ret;
3173     DWORD bits_1[8 * 8], bits_2[8 * 8];
3174
3175     if(!pSetLayout)
3176     {
3177         win_skip("Don't have SetLayout\n");
3178         return;
3179     }
3180
3181     hdc = GetDC( NULL );
3182     hdc_mem = CreateCompatibleDC( hdc );
3183     pSetLayout( hdc_mem, LAYOUT_LTR );
3184
3185     bitmap = CreateCompatibleBitmap( hdc, 8, 8 );
3186     orig_bitmap = SelectObject( hdc_mem, bitmap );
3187     SetPixel( hdc_mem, 0, 0, RGB(0xff, 0, 0) );
3188     SelectObject( hdc_mem, orig_bitmap );
3189
3190     info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3191     info.bmiHeader.biWidth = 8;
3192     info.bmiHeader.biHeight = 8;
3193     info.bmiHeader.biPlanes = 1;
3194     info.bmiHeader.biBitCount = 32;
3195     info.bmiHeader.biCompression = BI_RGB;
3196
3197     /* First show that GetDIBits ignores the layout mode. */
3198
3199     ret = GetDIBits( hdc_mem, bitmap, 0, 8, bits_1, &info, DIB_RGB_COLORS );
3200     ok(ret == 8, "got %d\n", ret);
3201     ok(bits_1[56] == 0xff0000, "got %08x\n", bits_1[56]); /* check we have a red pixel */
3202
3203     pSetLayout( hdc_mem, LAYOUT_RTL );
3204
3205     ret = GetDIBits( hdc_mem, bitmap, 0, 8, bits_2, &info, DIB_RGB_COLORS );
3206     ok(ret == 8, "got %d\n", ret);
3207
3208     ok(!memcmp( bits_1, bits_2, sizeof(bits_1) ), "bits differ\n");
3209
3210     /* Now to show that SetDIBits also ignores the mode, we perform a SetDIBits
3211        followed by a GetDIBits and show that the bits remain unchanged. */
3212
3213     pSetLayout( hdc_mem, LAYOUT_LTR );
3214
3215     ret = SetDIBits( hdc_mem, bitmap, 0, 8, bits_1, &info, DIB_RGB_COLORS );
3216     ok(ret == 8, "got %d\n", ret);
3217     ret = GetDIBits( hdc_mem, bitmap, 0, 8, bits_2, &info, DIB_RGB_COLORS );
3218     ok(ret == 8, "got %d\n", ret);
3219     ok(!memcmp( bits_1, bits_2, sizeof(bits_1) ), "bits differ\n");
3220
3221     pSetLayout( hdc_mem, LAYOUT_RTL );
3222
3223     ret = SetDIBits( hdc_mem, bitmap, 0, 8, bits_1, &info, DIB_RGB_COLORS );
3224     ok(ret == 8, "got %d\n", ret);
3225     ret = GetDIBits( hdc_mem, bitmap, 0, 8, bits_2, &info, DIB_RGB_COLORS );
3226     ok(ret == 8, "got %d\n", ret);
3227     ok(!memcmp( bits_1, bits_2, sizeof(bits_1) ), "bits differ\n");
3228
3229     DeleteObject( bitmap );
3230     DeleteDC( hdc_mem );
3231     ReleaseDC( NULL, hdc );
3232 }
3233
3234 START_TEST(bitmap)
3235 {
3236     HMODULE hdll;
3237
3238     hdll = GetModuleHandle("gdi32.dll");
3239     pGdiAlphaBlend = (void*)GetProcAddress(hdll, "GdiAlphaBlend");
3240     pSetLayout     = (void*)GetProcAddress(hdll, "SetLayout");
3241
3242     test_createdibitmap();
3243     test_dibsections();
3244     test_mono_dibsection();
3245     test_bitmap();
3246     test_bmBits();
3247     test_GetDIBits_selected_DIB(1);
3248     test_GetDIBits_selected_DIB(4);
3249     test_GetDIBits_selected_DIB(8);
3250     test_GetDIBits_selected_DDB(TRUE);
3251     test_GetDIBits_selected_DDB(FALSE);
3252     test_GetDIBits();
3253     test_GetDIBits_BI_BITFIELDS();
3254     test_select_object();
3255     test_CreateBitmap();
3256     test_BitBlt();
3257     test_StretchBlt();
3258     test_StretchDIBits();
3259     test_GdiAlphaBlend();
3260     test_32bit_bitmap_blt();
3261     test_bitmapinfoheadersize();
3262     test_get16dibits();
3263     test_clipping();
3264     test_GetDIBits_top_down(16);
3265     test_GetDIBits_top_down(24);
3266     test_GetDIBits_top_down(32);
3267     test_GetSetDIBits_rtl();
3268 }