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