mshtml: Added IHTMLWindow2::focus implementation.
[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     for (i = 0; i < 128; i++)
550     {
551         pbmi->bmiHeader.biBitCount = i;
552         pbmi->bmiHeader.biCompression = BI_RGB;
553         hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
554         if (i == 1 || i == 4 || i == 8 || i == 16 || i == 24 || i == 32)
555             ok(hdib != NULL, "CreateDIBSection bpp %u\n", i);
556         else
557             ok(hdib == NULL, "CreateDIBSection bpp %u succeeded\n", i);
558         if (hdib) DeleteObject( hdib );
559     }
560
561     pbmi->bmiHeader.biBitCount = 16;
562     pbmi->bmiHeader.biCompression = BI_BITFIELDS;
563     ((PDWORD)pbmi->bmiColors)[0] = 0xf800;
564     ((PDWORD)pbmi->bmiColors)[1] = 0x07e0;
565     ((PDWORD)pbmi->bmiColors)[2] = 0x001f;
566     SetLastError(0xdeadbeef);
567     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
568     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
569
570     /* test the DIB memory */
571     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
572         "VirtualQuery failed\n");
573     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
574     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
575     ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
576     ok(info.RegionSize == 0x19000, "0x%lx != 0x19000\n", info.RegionSize);
577     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
578     ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
579     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
580
581     test_dib_info(hdib, bits, &pbmi->bmiHeader);
582     DeleteObject(hdib);
583
584     memset(pbmi, 0, sizeof(bmibuf));
585     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
586     pbmi->bmiHeader.biHeight = 16;
587     pbmi->bmiHeader.biWidth = 16;
588     pbmi->bmiHeader.biBitCount = 1;
589     pbmi->bmiHeader.biPlanes = 1;
590     pbmi->bmiHeader.biCompression = BI_RGB;
591     pbmi->bmiColors[0].rgbRed = 0xff;
592     pbmi->bmiColors[0].rgbGreen = 0;
593     pbmi->bmiColors[0].rgbBlue = 0;
594     pbmi->bmiColors[1].rgbRed = 0;
595     pbmi->bmiColors[1].rgbGreen = 0;
596     pbmi->bmiColors[1].rgbBlue = 0xff;
597
598     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
599     ok(hdib != NULL, "CreateDIBSection failed\n");
600     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
601     ok(dibsec.dsBmih.biClrUsed == 2,
602         "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
603
604     /* Test if the old BITMAPCOREINFO structure is supported */    
605         
606     pbci->bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
607     pbci->bmciHeader.bcBitCount = 0;
608
609     if (!is_win9x) {
610         ret = GetDIBits(hdc, hdib, 0, 16, NULL, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
611         ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
612         ok((pbci->bmciHeader.bcWidth == 16) && (pbci->bmciHeader.bcHeight == 16)
613             && (pbci->bmciHeader.bcBitCount == 1) && (pbci->bmciHeader.bcPlanes == 1),
614         "GetDIBits did't fill in the BITMAPCOREHEADER structure properly\n");
615
616         ret = GetDIBits(hdc, hdib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
617         ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
618         ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
619             (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
620             (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
621             "The color table has not been translated to the old BITMAPCOREINFO format\n");
622
623         hcoredib = CreateDIBSection(hdc, (BITMAPINFO*) pbci, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
624         ok(hcoredib != NULL, "CreateDIBSection failed with a BITMAPCOREINFO\n");
625
626         ZeroMemory(pbci->bmciColors, 256 * sizeof(RGBTRIPLE));
627         ret = GetDIBits(hdc, hcoredib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
628         ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
629         ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
630             (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
631             (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
632             "The color table has not been translated to the old BITMAPCOREINFO format\n");
633
634         DeleteObject(hcoredib);
635     }
636
637     hdcmem = CreateCompatibleDC(hdc);
638     oldbm = SelectObject(hdcmem, hdib);
639
640     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
641     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
642     ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
643        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
644        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
645        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
646
647     c0 = RGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen, pbmi->bmiColors[0].rgbBlue);
648     c1 = RGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen, pbmi->bmiColors[1].rgbBlue);
649
650     test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
651     test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
652     test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
653     test_color(hdcmem, PALETTEINDEX(0), c0, 1, 1);
654     test_color(hdcmem, PALETTEINDEX(1), c0, 1, 1);
655     test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
656     test_color(hdcmem, PALETTERGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen,
657         pbmi->bmiColors[0].rgbBlue), c0, 1, 1);
658     test_color(hdcmem, PALETTERGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen,
659         pbmi->bmiColors[1].rgbBlue), c1, 1, 1);
660     test_color(hdcmem, PALETTERGB(0, 0, 0), c0, 1, 1);
661     test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
662     test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c1, 1, 1);
663
664     SelectObject(hdcmem, oldbm);
665     DeleteObject(hdib);
666
667     pbmi->bmiColors[0].rgbRed = 0xff;
668     pbmi->bmiColors[0].rgbGreen = 0xff;
669     pbmi->bmiColors[0].rgbBlue = 0xff;
670     pbmi->bmiColors[1].rgbRed = 0;
671     pbmi->bmiColors[1].rgbGreen = 0;
672     pbmi->bmiColors[1].rgbBlue = 0;
673
674     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
675     ok(hdib != NULL, "CreateDIBSection failed\n");
676
677     test_dib_info(hdib, bits, &pbmi->bmiHeader);
678
679     oldbm = SelectObject(hdcmem, hdib);
680
681     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
682     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
683     ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
684        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
685        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
686        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
687
688     SelectObject(hdcmem, oldbm);
689     test_dib_info(hdib, bits, &pbmi->bmiHeader);
690     DeleteObject(hdib);
691
692     pbmi->bmiHeader.biBitCount = 4;
693     for (i = 0; i < 16; i++) {
694         pbmi->bmiColors[i].rgbRed = i;
695         pbmi->bmiColors[i].rgbGreen = 16-i;
696         pbmi->bmiColors[i].rgbBlue = 0;
697     }
698     hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
699     ok(hdib != NULL, "CreateDIBSection failed\n");
700     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
701     ok(dibsec.dsBmih.biClrUsed == 16,
702        "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 16);
703     test_dib_info(hdib, bits, &pbmi->bmiHeader);
704     DeleteObject(hdib);
705
706     pbmi->bmiHeader.biBitCount = 8;
707
708     for (i = 0; i < 128; i++) {
709         pbmi->bmiColors[i].rgbRed = 255 - i * 2;
710         pbmi->bmiColors[i].rgbGreen = i * 2;
711         pbmi->bmiColors[i].rgbBlue = 0;
712         pbmi->bmiColors[255 - i].rgbRed = 0;
713         pbmi->bmiColors[255 - i].rgbGreen = i * 2;
714         pbmi->bmiColors[255 - i].rgbBlue = 255 - i * 2;
715     }
716     hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
717     ok(hdib != NULL, "CreateDIBSection failed\n");
718     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
719     ok(dibsec.dsBmih.biClrUsed == 256,
720         "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
721
722     oldbm = SelectObject(hdcmem, hdib);
723
724     for (i = 0; i < 256; i++) {
725         test_color(hdcmem, DIBINDEX(i), 
726             RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
727         test_color(hdcmem, PALETTERGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 
728             RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
729     }
730
731     SelectObject(hdcmem, oldbm);
732     test_dib_info(hdib, bits, &pbmi->bmiHeader);
733     DeleteObject(hdib);
734
735     pbmi->bmiHeader.biBitCount = 1;
736
737     /* Now create a palette and a palette indexed dib section */
738     memset(plogpal, 0, sizeof(logpalbuf));
739     plogpal->palVersion = 0x300;
740     plogpal->palNumEntries = 2;
741     plogpal->palPalEntry[0].peRed = 0xff;
742     plogpal->palPalEntry[0].peBlue = 0xff;
743     plogpal->palPalEntry[1].peGreen = 0xff;
744
745     index = (WORD*)pbmi->bmiColors;
746     *index++ = 0;
747     *index = 1;
748     hpal = CreatePalette(plogpal);
749     ok(hpal != NULL, "CreatePalette failed\n");
750     oldpal = SelectPalette(hdc, hpal, TRUE);
751     hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
752     ok(hdib != NULL, "CreateDIBSection failed\n");
753     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
754     ok(dibsec.dsBmih.biClrUsed == 2 ||
755        broken(dibsec.dsBmih.biClrUsed == 0), /* win9x */
756         "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
757
758     /* The colour table has already been grabbed from the dc, so we select back the
759        old palette */
760
761     SelectPalette(hdc, oldpal, TRUE);
762     oldbm = SelectObject(hdcmem, hdib);
763     oldpal = SelectPalette(hdcmem, hpal, TRUE);
764
765     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
766     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
767     ok(rgb[0].rgbRed == 0xff && rgb[0].rgbBlue == 0xff && rgb[0].rgbGreen == 0 &&
768        rgb[1].rgbRed == 0    && rgb[1].rgbBlue == 0    && rgb[1].rgbGreen == 0xff,
769        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
770        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
771        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
772
773     c0 = RGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen, plogpal->palPalEntry[0].peBlue);
774     c1 = RGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen, plogpal->palPalEntry[1].peBlue);
775
776     test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
777     test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
778     test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
779     test_color(hdcmem, PALETTEINDEX(0), c0, 0, 1);
780     test_color(hdcmem, PALETTEINDEX(1), c1, 0, 1);
781     test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
782     test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen,
783         plogpal->palPalEntry[0].peBlue), c0, 1, 1);
784     test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen,
785         plogpal->palPalEntry[1].peBlue), c1, 1, 1);
786     test_color(hdcmem, PALETTERGB(0, 0, 0), c1, 1, 1);
787     test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
788     test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c0, 1, 1);
789     test_color(hdcmem, PALETTERGB(0, 1, 0), c1, 1, 1);
790     test_color(hdcmem, PALETTERGB(0x3f, 0, 0x3f), c1, 1, 1);
791     test_color(hdcmem, PALETTERGB(0x40, 0, 0x40), c0, 1, 1);
792
793     /* Bottom and 2nd row from top green, everything else magenta */
794     bits[0] = bits[1] = 0xff;
795     bits[13 * 4] = bits[13*4 + 1] = 0xff;
796
797     test_dib_info(hdib, bits, &pbmi->bmiHeader);
798
799     pbmi->bmiHeader.biBitCount = 32;
800
801     hdib2 = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&bits32, NULL, 0);
802     ok(hdib2 != NULL, "CreateDIBSection failed\n");
803     hdcmem2 = CreateCompatibleDC(hdc);
804     oldbm2 = SelectObject(hdcmem2, hdib2);
805
806     BitBlt(hdcmem2, 0, 0, 16,16, hdcmem, 0, 0, SRCCOPY);
807
808     ok(bits32[0] == 0xff00, "lower left pixel is %08x\n", bits32[0]);
809     ok(bits32[17] == 0xff00ff ||
810        broken(bits32[17] == 0x00ff00), /* Intermittent on Win9x/ME */
811        "bottom but one, left pixel is %08x\n", bits32[17]);
812
813     SelectObject(hdcmem2, oldbm2);
814     test_dib_info(hdib2, bits32, &pbmi->bmiHeader);
815     DeleteObject(hdib2);
816
817     SelectObject(hdcmem, oldbm);
818     SelectObject(hdcmem, oldpal);
819     DeleteObject(hdib);
820     DeleteObject(hpal);
821
822
823     pbmi->bmiHeader.biBitCount = 8;
824
825     memset(plogpal, 0, sizeof(logpalbuf));
826     plogpal->palVersion = 0x300;
827     plogpal->palNumEntries = 256;
828
829     for (i = 0; i < 128; i++) {
830         plogpal->palPalEntry[i].peRed = 255 - i * 2;
831         plogpal->palPalEntry[i].peBlue = i * 2;
832         plogpal->palPalEntry[i].peGreen = 0;
833         plogpal->palPalEntry[255 - i].peRed = 0;
834         plogpal->palPalEntry[255 - i].peGreen = i * 2;
835         plogpal->palPalEntry[255 - i].peBlue = 255 - i * 2;
836     }
837
838     index = (WORD*)pbmi->bmiColors;
839     for (i = 0; i < 256; i++) {
840         *index++ = i;
841     }
842
843     hpal = CreatePalette(plogpal);
844     ok(hpal != NULL, "CreatePalette failed\n");
845     oldpal = SelectPalette(hdc, hpal, TRUE);
846     hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
847     ok(hdib != NULL, "CreateDIBSection failed\n");
848     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
849     ok(dibsec.dsBmih.biClrUsed == 256 ||
850        broken(dibsec.dsBmih.biClrUsed == 0), /* win9x */
851         "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
852
853     test_dib_info(hdib, bits, &pbmi->bmiHeader);
854
855     SelectPalette(hdc, oldpal, TRUE);
856     oldbm = SelectObject(hdcmem, hdib);
857     oldpal = SelectPalette(hdcmem, hpal, TRUE);
858
859     ret = GetDIBColorTable(hdcmem, 0, 256, rgb);
860     ok(ret == 256, "GetDIBColorTable returned %d\n", ret);
861     for (i = 0; i < 256; i++) {
862         ok(rgb[i].rgbRed == plogpal->palPalEntry[i].peRed && 
863             rgb[i].rgbBlue == plogpal->palPalEntry[i].peBlue && 
864             rgb[i].rgbGreen == plogpal->palPalEntry[i].peGreen, 
865             "GetDIBColorTable returns table %d: r%02x g%02x b%02x res%02x\n",
866             i, rgb[i].rgbRed, rgb[i].rgbGreen, rgb[i].rgbBlue, rgb[i].rgbReserved);
867     }
868
869     for (i = 0; i < 256; i++) {
870         test_color(hdcmem, DIBINDEX(i), 
871             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
872         test_color(hdcmem, PALETTEINDEX(i), 
873             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
874         test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 
875             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
876     }
877
878     SelectPalette(hdcmem, oldpal, TRUE);
879     SelectObject(hdcmem, oldbm);
880     DeleteObject(hdib);
881     DeleteObject(hpal);
882
883     DeleteDC(hdcmem);
884     DeleteDC(hdcmem2);
885     ReleaseDC(0, hdc);
886 }
887
888 static void test_mono_dibsection(void)
889 {
890     HDC hdc, memdc;
891     HBITMAP old_bm, mono_ds;
892     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
893     BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
894     BYTE bits[10 * 4];
895     BYTE *ds_bits;
896     int num;
897
898     hdc = GetDC(0);
899
900     memdc = CreateCompatibleDC(hdc);
901
902     memset(pbmi, 0, sizeof(bmibuf));
903     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
904     pbmi->bmiHeader.biHeight = 10;
905     pbmi->bmiHeader.biWidth = 10;
906     pbmi->bmiHeader.biBitCount = 1;
907     pbmi->bmiHeader.biPlanes = 1;
908     pbmi->bmiHeader.biCompression = BI_RGB;
909     pbmi->bmiColors[0].rgbRed = 0xff;
910     pbmi->bmiColors[0].rgbGreen = 0xff;
911     pbmi->bmiColors[0].rgbBlue = 0xff;
912     pbmi->bmiColors[1].rgbRed = 0x0;
913     pbmi->bmiColors[1].rgbGreen = 0x0;
914     pbmi->bmiColors[1].rgbBlue = 0x0;
915
916     /*
917      * First dib section is 'inverted' ie color[0] is white, color[1] is black
918      */
919
920     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
921     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
922     old_bm = SelectObject(memdc, mono_ds);
923
924     /* black border, white interior */
925     Rectangle(memdc, 0, 0, 10, 10);
926     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
927     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
928
929     /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
930
931     memset(bits, 0, sizeof(bits));
932     bits[0] = 0xaa;
933
934     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
935     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
936
937     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
938
939     pbmi->bmiColors[0].rgbRed = 0x0;
940     pbmi->bmiColors[0].rgbGreen = 0x0;
941     pbmi->bmiColors[0].rgbBlue = 0x0;
942     pbmi->bmiColors[1].rgbRed = 0xff;
943     pbmi->bmiColors[1].rgbGreen = 0xff;
944     pbmi->bmiColors[1].rgbBlue = 0xff;
945
946     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
947     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
948
949     SelectObject(memdc, old_bm);
950     DeleteObject(mono_ds);
951
952     /*
953      * Next dib section is 'normal' ie color[0] is black, color[1] is white
954      */
955
956     pbmi->bmiColors[0].rgbRed = 0x0;
957     pbmi->bmiColors[0].rgbGreen = 0x0;
958     pbmi->bmiColors[0].rgbBlue = 0x0;
959     pbmi->bmiColors[1].rgbRed = 0xff;
960     pbmi->bmiColors[1].rgbGreen = 0xff;
961     pbmi->bmiColors[1].rgbBlue = 0xff;
962
963     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
964     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
965     old_bm = SelectObject(memdc, mono_ds);
966
967     /* black border, white interior */
968     Rectangle(memdc, 0, 0, 10, 10);
969     ok(ds_bits[0] == 0x00, "out_bits %02x\n", ds_bits[0]);
970     ok(ds_bits[4] == 0x7f, "out_bits %02x\n", ds_bits[4]);
971
972     /* SetDIBitsToDevice with a normal bmi -> normal dib section */
973
974     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
975     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
976
977     /* SetDIBitsToDevice with a inverted bmi -> normal dib section */
978
979     pbmi->bmiColors[0].rgbRed = 0xff;
980     pbmi->bmiColors[0].rgbGreen = 0xff;
981     pbmi->bmiColors[0].rgbBlue = 0xff;
982     pbmi->bmiColors[1].rgbRed = 0x0;
983     pbmi->bmiColors[1].rgbGreen = 0x0;
984     pbmi->bmiColors[1].rgbBlue = 0x0;
985
986     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
987     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
988
989     /*
990      * Take that 'normal' dibsection and change its colour table to an 'inverted' one
991      */
992
993     pbmi->bmiColors[0].rgbRed = 0xff;
994     pbmi->bmiColors[0].rgbGreen = 0xff;
995     pbmi->bmiColors[0].rgbBlue = 0xff;
996     pbmi->bmiColors[1].rgbRed = 0x0;
997     pbmi->bmiColors[1].rgbGreen = 0x0;
998     pbmi->bmiColors[1].rgbBlue = 0x0;
999     num = SetDIBColorTable(memdc, 0, 2, pbmi->bmiColors);
1000     ok(num == 2, "num = %d\n", num);
1001
1002     /* black border, white interior */
1003     Rectangle(memdc, 0, 0, 10, 10);
1004 todo_wine {
1005     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
1006     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
1007  }
1008     /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
1009
1010     memset(bits, 0, sizeof(bits));
1011     bits[0] = 0xaa;
1012
1013     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1014     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
1015
1016     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
1017
1018     pbmi->bmiColors[0].rgbRed = 0x0;
1019     pbmi->bmiColors[0].rgbGreen = 0x0;
1020     pbmi->bmiColors[0].rgbBlue = 0x0;
1021     pbmi->bmiColors[1].rgbRed = 0xff;
1022     pbmi->bmiColors[1].rgbGreen = 0xff;
1023     pbmi->bmiColors[1].rgbBlue = 0xff;
1024
1025     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1026     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1027
1028     SelectObject(memdc, old_bm);
1029     DeleteObject(mono_ds);
1030
1031     /*
1032      * Now a dib section with a strange colour map just for fun.  This behaves just like an inverted one.
1033      */
1034  
1035     pbmi->bmiColors[0].rgbRed = 0xff;
1036     pbmi->bmiColors[0].rgbGreen = 0x0;
1037     pbmi->bmiColors[0].rgbBlue = 0x0;
1038     pbmi->bmiColors[1].rgbRed = 0xfe;
1039     pbmi->bmiColors[1].rgbGreen = 0x0;
1040     pbmi->bmiColors[1].rgbBlue = 0x0;
1041
1042     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
1043     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
1044     old_bm = SelectObject(memdc, mono_ds);
1045
1046     /* black border, white interior */
1047     Rectangle(memdc, 0, 0, 10, 10);
1048     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
1049     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
1050
1051     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
1052
1053     pbmi->bmiColors[0].rgbRed = 0x0;
1054     pbmi->bmiColors[0].rgbGreen = 0x0;
1055     pbmi->bmiColors[0].rgbBlue = 0x0;
1056     pbmi->bmiColors[1].rgbRed = 0xff;
1057     pbmi->bmiColors[1].rgbGreen = 0xff;
1058     pbmi->bmiColors[1].rgbBlue = 0xff;
1059
1060     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1061     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1062
1063     /* SetDIBitsToDevice with a inverted bmi -> inverted dib section */
1064
1065     pbmi->bmiColors[0].rgbRed = 0xff;
1066     pbmi->bmiColors[0].rgbGreen = 0xff;
1067     pbmi->bmiColors[0].rgbBlue = 0xff;
1068     pbmi->bmiColors[1].rgbRed = 0x0;
1069     pbmi->bmiColors[1].rgbGreen = 0x0;
1070     pbmi->bmiColors[1].rgbBlue = 0x0;
1071
1072     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1073     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
1074
1075     SelectObject(memdc, old_bm);
1076     DeleteObject(mono_ds);
1077
1078     DeleteDC(memdc);
1079     ReleaseDC(0, hdc);
1080 }
1081
1082 static void test_bitmap(void)
1083 {
1084     char buf[256], buf_cmp[256];
1085     HBITMAP hbmp, hbmp_old;
1086     HDC hdc;
1087     BITMAP bm;
1088     BITMAP bma[2];
1089     INT ret;
1090
1091     hdc = CreateCompatibleDC(0);
1092     assert(hdc != 0);
1093
1094     SetLastError(0xdeadbeef);
1095     hbmp = CreateBitmap(0x7ffffff, 1, 1, 1, NULL);
1096     if (!hbmp)
1097     {
1098         ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1099            GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1100            "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1101     }
1102     else
1103         DeleteObject(hbmp);
1104
1105     SetLastError(0xdeadbeef);
1106     hbmp = CreateBitmap(0x7ffffff, 9, 1, 1, NULL);
1107     if (!hbmp)
1108     {
1109         ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1110            GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1111            "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1112     }
1113     else
1114         DeleteObject(hbmp);
1115
1116     SetLastError(0xdeadbeef);
1117     hbmp = CreateBitmap(0x7ffffff + 1, 1, 1, 1, NULL);
1118     ok(!hbmp || broken(hbmp != NULL /* Win9x */), "CreateBitmap should fail\n");
1119     if (!hbmp)
1120         ok(GetLastError() == ERROR_INVALID_PARAMETER,
1121            "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1122     else
1123         DeleteObject(hbmp);
1124
1125     hbmp = CreateBitmap(15, 15, 1, 1, NULL);
1126     assert(hbmp != NULL);
1127
1128     ret = GetObject(hbmp, sizeof(bm), &bm);
1129     ok(ret == sizeof(bm), "wrong size %d\n", ret);
1130
1131     ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1132     ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1133     ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1134     ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1135     ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1136     ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1137     ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1138
1139     assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
1140     assert(sizeof(buf) == sizeof(buf_cmp));
1141
1142     ret = GetBitmapBits(hbmp, 0, NULL);
1143     ok(ret == bm.bmWidthBytes * bm.bmHeight || broken(ret == 0 /* Win9x */),
1144         "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1145
1146     memset(buf_cmp, 0xAA, sizeof(buf_cmp));
1147     memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
1148
1149     memset(buf, 0xAA, sizeof(buf));
1150     ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1151     ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1152     ok(!memcmp(buf, buf_cmp, sizeof(buf)) ||
1153        broken(memcmp(buf, buf_cmp, sizeof(buf))), /* win9x doesn't init the bitmap bits */
1154        "buffers do not match\n");
1155
1156     hbmp_old = SelectObject(hdc, hbmp);
1157
1158     ret = GetObject(hbmp, sizeof(bm), &bm);
1159     ok(ret == sizeof(bm), "wrong size %d\n", ret);
1160
1161     ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1162     ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1163     ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1164     ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1165     ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1166     ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1167     ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1168
1169     memset(buf, 0xAA, sizeof(buf));
1170     ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1171     ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1172     ok(!memcmp(buf, buf_cmp, sizeof(buf)) ||
1173        broken(memcmp(buf, buf_cmp, sizeof(buf))), /* win9x doesn't init the bitmap bits */
1174        "buffers do not match\n");
1175
1176     hbmp_old = SelectObject(hdc, hbmp_old);
1177     ok(hbmp_old == hbmp, "wrong old bitmap %p\n", hbmp_old);
1178
1179     /* test various buffer sizes for GetObject */
1180     ret = GetObject(hbmp, sizeof(*bma) * 2, bma);
1181     ok(ret == sizeof(*bma) || broken(ret == sizeof(*bma) * 2 /* Win9x */), "wrong size %d\n", ret);
1182
1183     ret = GetObject(hbmp, sizeof(bm) / 2, &bm);
1184     ok(ret == 0 || broken(ret == sizeof(bm) / 2 /* Win9x */), "%d != 0\n", ret);
1185
1186     ret = GetObject(hbmp, 0, &bm);
1187     ok(ret == 0, "%d != 0\n", ret);
1188
1189     ret = GetObject(hbmp, 1, &bm);
1190     ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
1191
1192     DeleteObject(hbmp);
1193     DeleteDC(hdc);
1194 }
1195
1196 static void test_bmBits(void)
1197 {
1198     BYTE bits[4];
1199     HBITMAP hbmp;
1200     BITMAP bmp;
1201
1202     memset(bits, 0, sizeof(bits));
1203     hbmp = CreateBitmap(2, 2, 1, 4, bits);
1204     ok(hbmp != NULL, "CreateBitmap failed\n");
1205
1206     memset(&bmp, 0xFF, sizeof(bmp));
1207     ok(GetObject(hbmp, sizeof(bmp), &bmp) == sizeof(bmp),
1208        "GetObject failed or returned a wrong structure size\n");
1209     ok(!bmp.bmBits, "bmBits must be NULL for device-dependent bitmaps\n");
1210
1211     DeleteObject(hbmp);
1212 }
1213
1214 static void test_GetDIBits_selected_DIB(UINT bpp)
1215 {
1216     HBITMAP dib;
1217     BITMAPINFO * info;
1218     BITMAPINFO * info2;
1219     void * bits;
1220     void * bits2;
1221     UINT dib_size, dib32_size;
1222     DWORD pixel;
1223     HDC dib_dc, dc;
1224     HBITMAP old_bmp;
1225     BOOL equalContents;
1226     UINT i;
1227     int res;
1228
1229     /* Create a DIB section with a color table */
1230
1231     info  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1232     info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1233     assert(info);
1234     assert(info2);
1235
1236     info->bmiHeader.biSize = sizeof(info->bmiHeader);
1237
1238     /* Choose width and height such that the row length (in bytes)
1239        is a multiple of 4 (makes things easier) */
1240     info->bmiHeader.biWidth = 32;
1241     info->bmiHeader.biHeight = 32;
1242     info->bmiHeader.biPlanes = 1;
1243     info->bmiHeader.biBitCount = bpp;
1244     info->bmiHeader.biCompression = BI_RGB;
1245
1246     for (i=0; i < (1u << bpp); i++)
1247     {
1248         BYTE c = i * (1 << (8 - bpp));
1249         info->bmiColors[i].rgbRed = c;
1250         info->bmiColors[i].rgbGreen = c;
1251         info->bmiColors[i].rgbBlue = c;
1252         info->bmiColors[i].rgbReserved = 0;
1253     }
1254
1255     dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
1256     assert(dib);
1257     dib_size = bpp * (info->bmiHeader.biWidth * info->bmiHeader.biHeight) / 8;
1258     dib32_size = 32 * (info->bmiHeader.biWidth * info->bmiHeader.biHeight) / 8;
1259
1260     /* Set the bits of the DIB section */
1261     for (i=0; i < dib_size; i++)
1262     {
1263         ((BYTE *)bits)[i] = i % 256;
1264     }
1265
1266     /* Select the DIB into a DC */
1267     dib_dc = CreateCompatibleDC(NULL);
1268     old_bmp = SelectObject(dib_dc, dib);
1269     dc = CreateCompatibleDC(NULL);
1270     bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dib32_size);
1271     assert(bits2);
1272
1273     /* Copy the DIB attributes but not the color table */
1274     memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1275
1276     res = GetDIBits(dc, dib, 0, info->bmiHeader.biHeight, bits2, info2, DIB_RGB_COLORS);
1277     ok(res, "GetDIBits failed\n");
1278
1279     /* Compare the color table and the bits */
1280     equalContents = TRUE;
1281     for (i=0; i < (1u << bpp); i++)
1282     {
1283         if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1284             || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1285             || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1286             || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1287         {
1288             equalContents = FALSE;
1289             break;
1290         }
1291     }
1292     ok(equalContents, "GetDIBits with DIB selected in DC: Invalid DIB color table\n");
1293
1294     equalContents = TRUE;
1295     for (i=0; i < dib_size / sizeof(DWORD); i++)
1296     {
1297         if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1298         {
1299             equalContents = FALSE;
1300             break;
1301         }
1302     }
1303     ok(equalContents, "GetDIBits with %d bpp DIB selected in DC: Invalid DIB bits\n",bpp);
1304
1305     /* Map into a 32bit-DIB */
1306     info2->bmiHeader.biBitCount = 32;
1307     res = GetDIBits(dc, dib, 0, info->bmiHeader.biHeight, bits2, info2, DIB_RGB_COLORS);
1308     ok(res, "GetDIBits failed\n");
1309
1310     /* Check if last pixel was set */
1311     pixel = ((DWORD *)bits2)[info->bmiHeader.biWidth * info->bmiHeader.biHeight - 1];
1312     ok(pixel != 0, "Pixel: 0x%08x\n", pixel);
1313
1314     HeapFree(GetProcessHeap(), 0, bits2);
1315     DeleteDC(dc);
1316
1317     SelectObject(dib_dc, old_bmp);
1318     DeleteDC(dib_dc);
1319     DeleteObject(dib);
1320
1321     HeapFree(GetProcessHeap(), 0, info2);
1322     HeapFree(GetProcessHeap(), 0, info);
1323 }
1324
1325 static void test_GetDIBits_selected_DDB(BOOL monochrome)
1326 {
1327     HBITMAP ddb;
1328     BITMAPINFO * info;
1329     BITMAPINFO * info2;
1330     void * bits;
1331     void * bits2;
1332     HDC ddb_dc, dc;
1333     HBITMAP old_bmp;
1334     BOOL equalContents;
1335     UINT width, height;
1336     UINT bpp;
1337     UINT i, j;
1338     int res;
1339
1340     width = height = 16;
1341
1342     /* Create a DDB (device-dependent bitmap) */
1343     if (monochrome)
1344     {
1345         bpp = 1;
1346         ddb = CreateBitmap(width, height, 1, 1, NULL);
1347     }
1348     else
1349     {
1350         HDC screen_dc = GetDC(NULL);
1351         bpp = GetDeviceCaps(screen_dc, BITSPIXEL) * GetDeviceCaps(screen_dc, PLANES);
1352         ddb = CreateCompatibleBitmap(screen_dc, width, height);
1353         ReleaseDC(NULL, screen_dc);
1354     }
1355
1356     /* Set the pixels */
1357     ddb_dc = CreateCompatibleDC(NULL);
1358     old_bmp = SelectObject(ddb_dc, ddb);
1359     for (i = 0; i < width; i++)
1360     {
1361         for (j=0; j < height; j++)
1362         {
1363             BYTE c = (i * width + j) % 256;
1364             SetPixelV(ddb_dc, i, j, RGB(c, c, c));
1365         }
1366     }
1367     SelectObject(ddb_dc, old_bmp);
1368
1369     info  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1370     info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1371     assert(info);
1372     assert(info2);
1373
1374     info->bmiHeader.biSize = sizeof(info->bmiHeader);
1375     info->bmiHeader.biWidth = width;
1376     info->bmiHeader.biHeight = height;
1377     info->bmiHeader.biPlanes = 1;
1378     info->bmiHeader.biBitCount = bpp;
1379     info->bmiHeader.biCompression = BI_RGB;
1380
1381     dc = CreateCompatibleDC(NULL);
1382
1383     /* Fill in biSizeImage */
1384     GetDIBits(dc, ddb, 0, height, NULL, info, DIB_RGB_COLORS);
1385     ok(info->bmiHeader.biSizeImage != 0, "GetDIBits failed to get the DIB attributes\n");
1386
1387     bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1388     bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1389     assert(bits);
1390     assert(bits2);
1391
1392     /* Get the bits */
1393     res = GetDIBits(dc, ddb, 0, height, bits, info, DIB_RGB_COLORS);
1394     ok(res, "GetDIBits failed\n");
1395
1396     /* Copy the DIB attributes but not the color table */
1397     memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1398
1399     /* Select the DDB into another DC */
1400     old_bmp = SelectObject(ddb_dc, ddb);
1401
1402     /* Get the bits */
1403     res = GetDIBits(dc, ddb, 0, height, bits2, info2, DIB_RGB_COLORS);
1404     ok(res, "GetDIBits failed\n");
1405
1406     /* Compare the color table and the bits */
1407     if (bpp <= 8)
1408     {
1409         equalContents = TRUE;
1410         for (i=0; i < (1u << bpp); i++)
1411         {
1412             if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1413                 || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1414                 || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1415                 || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1416             {
1417                 equalContents = FALSE;
1418                 break;
1419             }
1420         }
1421         ok(equalContents, "GetDIBits with DDB selected in DC: Got a different color table\n");
1422     }
1423
1424     equalContents = TRUE;
1425     for (i=0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++)
1426     {
1427         if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1428         {
1429             equalContents = FALSE;
1430         }
1431     }
1432     ok(equalContents, "GetDIBits with DDB selected in DC: Got different DIB bits\n");
1433
1434     /* Test the palette */
1435     equalContents = TRUE;
1436     if (info2->bmiHeader.biBitCount <= 8)
1437     {
1438         WORD *colors = (WORD*)info2->bmiColors;
1439
1440         /* Get the palette indices */
1441         res = GetDIBits(dc, ddb, 0, 0, NULL, info2, DIB_PAL_COLORS);
1442         if (res == 0 && GetLastError() == ERROR_INVALID_PARAMETER) /* Win9x */
1443             res = GetDIBits(dc, ddb, 0, height, NULL, info2, DIB_PAL_COLORS);
1444         ok(res, "GetDIBits failed\n");
1445
1446         for (i=0;i < 1 << info->bmiHeader.biSizeImage; i++)
1447         {
1448             if (colors[i] != i)
1449             {
1450                 equalContents = FALSE;
1451                 break;
1452             }
1453         }
1454     }
1455
1456     ok(equalContents, "GetDIBits with DDB selected in DC: non 1:1 palette indices\n");
1457
1458     HeapFree(GetProcessHeap(), 0, bits2);
1459     HeapFree(GetProcessHeap(), 0, bits);
1460     DeleteDC(dc);
1461
1462     SelectObject(ddb_dc, old_bmp);
1463     DeleteDC(ddb_dc);
1464     DeleteObject(ddb);
1465
1466     HeapFree(GetProcessHeap(), 0, info2);
1467     HeapFree(GetProcessHeap(), 0, info);
1468 }
1469
1470 static void test_GetDIBits(void)
1471 {
1472     /* 2-bytes aligned 1-bit bitmap data: 16x16 */
1473     static const BYTE bmp_bits_1[16 * 2] =
1474     {
1475         0xff,0xff, 0,0, 0xff,0xff, 0,0,
1476         0xff,0xff, 0,0, 0xff,0xff, 0,0,
1477         0xff,0xff, 0,0, 0xff,0xff, 0,0,
1478         0xff,0xff, 0,0, 0xff,0xff, 0,0
1479     };
1480     /* 4-bytes aligned 1-bit DIB data: 16x16 */
1481     static const BYTE dib_bits_1[16 * 4] =
1482     {
1483         0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1484         0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1485         0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1486         0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0
1487     };
1488     static const BYTE dib_bits_1_9x[16 * 4] =
1489     {
1490         0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa,
1491         0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa,
1492         0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa,
1493         0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa
1494     };
1495     /* 2-bytes aligned 24-bit bitmap data: 16x16 */
1496     static const BYTE bmp_bits_24[16 * 16*3] =
1497     {
1498         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1499         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1500         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1501         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1502         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1503         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1504         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1505         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1506         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1507         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1508         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1509         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1510         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1511         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1512         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1513         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1514         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1515         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1516         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1517         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1518         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1519         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1520         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1521         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1522         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1523         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1524         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1525         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1526         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1527         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1530     };
1531     /* 4-bytes aligned 24-bit DIB data: 16x16 */
1532     static const BYTE dib_bits_24[16 * 16*3] =
1533     {
1534         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
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         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1543         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1544         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1545         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1546         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1547         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1548         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1549         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1550         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1551         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1552         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1553         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1554         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1555         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1556         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1557         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1558         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1559         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1560         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1561         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1562         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1563         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1564         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1565         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
1566     };
1567     HBITMAP hbmp;
1568     BITMAP bm;
1569     HDC hdc;
1570     int i, bytes, lines;
1571     BYTE buf[1024];
1572     char bi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
1573     BITMAPINFO *bi = (BITMAPINFO *)bi_buf;
1574
1575     hdc = GetDC(0);
1576
1577     /* 1-bit source bitmap data */
1578     hbmp = CreateBitmap(16, 16, 1, 1, bmp_bits_1);
1579     ok(hbmp != 0, "CreateBitmap failed\n");
1580
1581     memset(&bm, 0xAA, sizeof(bm));
1582     bytes = GetObject(hbmp, sizeof(bm), &bm);
1583     ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1584     ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
1585     ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1586     ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1587     ok(bm.bmWidthBytes == 2, "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1588     ok(bm.bmPlanes == 1, "wrong bmPlanes %u\n", bm.bmPlanes);
1589     ok(bm.bmBitsPixel == 1, "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1590     ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1591
1592     bytes = GetBitmapBits(hbmp, 0, NULL);
1593     ok(bytes == sizeof(bmp_bits_1) || broken(bytes == 0 /* Win9x */), "expected 16*2 got %d bytes\n", bytes);
1594     bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1595     ok(bytes == sizeof(bmp_bits_1), "expected 16*2 got %d bytes\n", bytes);
1596     ok(!memcmp(buf, bmp_bits_1, sizeof(bmp_bits_1)), "bitmap bits don't match\n");
1597
1598     /* retrieve 1-bit DIB data */
1599     memset(bi, 0, sizeof(*bi));
1600     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1601     bi->bmiHeader.biWidth = bm.bmWidth;
1602     bi->bmiHeader.biHeight = bm.bmHeight;
1603     bi->bmiHeader.biPlanes = 1;
1604     bi->bmiHeader.biBitCount = 1;
1605     bi->bmiHeader.biCompression = BI_RGB;
1606     bi->bmiHeader.biSizeImage = 0;
1607     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1608     SetLastError(0xdeadbeef);
1609     lines = GetDIBits(0, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1610     ok(lines == 0, "GetDIBits copied %d lines with hdc = 0\n", lines);
1611     ok(GetLastError() == ERROR_INVALID_PARAMETER ||
1612        broken(GetLastError() == 0xdeadbeef), /* winnt */
1613        "wrong error %u\n", GetLastError());
1614     ok(bi->bmiHeader.biSizeImage == 0, "expected 0, got %u\n", bi->bmiHeader.biSizeImage);
1615
1616     memset(buf, 0xAA, sizeof(buf));
1617     SetLastError(0xdeadbeef);
1618     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1619     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1620        lines, bm.bmHeight, GetLastError());
1621     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1) ||
1622        broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1623        "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1624
1625     /* the color table consists of black and white */
1626     ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1627        bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1628        "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1629        bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1630        bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1631     ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1632        bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1633        "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1634        bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1635        bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1636     for (i = 2; i < 256; i++)
1637     {
1638         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1639            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1640            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1641            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1642            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1643     }
1644
1645     /* returned bits are DWORD aligned and upside down */
1646     ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)) ||
1647        broken(!memcmp(buf, dib_bits_1_9x, sizeof(dib_bits_1_9x))), /* Win9x, WinME */
1648        "DIB bits don't match\n");
1649
1650     /* Test the palette indices */
1651     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1652     SetLastError(0xdeadbeef);
1653     lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1654     if (lines == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
1655         win_skip("Win9x/WinMe doesn't handle 0 for the number of scan lines\n");
1656     else
1657     {
1658         ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1659         ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1660     }
1661     for (i = 2; i < 256; i++)
1662         ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[1]);
1663
1664     /* retrieve 24-bit DIB data */
1665     memset(bi, 0, sizeof(*bi));
1666     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1667     bi->bmiHeader.biWidth = bm.bmWidth;
1668     bi->bmiHeader.biHeight = bm.bmHeight;
1669     bi->bmiHeader.biPlanes = 1;
1670     bi->bmiHeader.biBitCount = 24;
1671     bi->bmiHeader.biCompression = BI_RGB;
1672     bi->bmiHeader.biSizeImage = 0;
1673     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1674     memset(buf, 0xAA, sizeof(buf));
1675     SetLastError(0xdeadbeef);
1676     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1677     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1678        lines, bm.bmHeight, GetLastError());
1679     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24) ||
1680        broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1681        "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1682
1683     /* the color table doesn't exist for 24-bit images */
1684     for (i = 0; i < 256; i++)
1685     {
1686         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1687            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1688            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1689            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1690            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1691     }
1692
1693     /* returned bits are DWORD aligned and upside down */
1694     ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1695     DeleteObject(hbmp);
1696
1697     /* 24-bit source bitmap data */
1698     hbmp = CreateCompatibleBitmap(hdc, 16, 16);
1699     ok(hbmp != 0, "CreateBitmap failed\n");
1700     SetLastError(0xdeadbeef);
1701     bi->bmiHeader.biHeight = -bm.bmHeight; /* indicate bottom-up data */
1702     lines = SetDIBits(hdc, hbmp, 0, bm.bmHeight, bmp_bits_24, bi, DIB_RGB_COLORS);
1703     ok(lines == bm.bmHeight, "SetDIBits copied %d lines of %d, error %u\n",
1704        lines, bm.bmHeight, GetLastError());
1705
1706     memset(&bm, 0xAA, sizeof(bm));
1707     bytes = GetObject(hbmp, sizeof(bm), &bm);
1708     ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1709     ok(bm.bmType == 0 ||
1710        broken(bm.bmType == 21072), /* win9x */
1711        "wrong bmType %d\n", bm.bmType);
1712     ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1713     ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1714     ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1715     ok(bm.bmPlanes == GetDeviceCaps(hdc, PLANES), "wrong bmPlanes %u\n", bm.bmPlanes);
1716     ok(bm.bmBitsPixel == GetDeviceCaps(hdc, BITSPIXEL), "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1717     ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1718
1719     bytes = GetBitmapBits(hbmp, 0, NULL);
1720     ok(bytes == bm.bmWidthBytes * bm.bmHeight ||
1721        broken(bytes == 0), /* win9x */
1722        "expected %d got %d bytes\n", bm.bmWidthBytes * bm.bmHeight, bytes);
1723     bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1724     ok(bytes == bm.bmWidthBytes * bm.bmHeight, "expected %d got %d bytes\n",
1725        bm.bmWidthBytes * bm.bmHeight, bytes);
1726
1727     /* retrieve 1-bit DIB data */
1728     memset(bi, 0, sizeof(*bi));
1729     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1730     bi->bmiHeader.biWidth = bm.bmWidth;
1731     bi->bmiHeader.biHeight = bm.bmHeight;
1732     bi->bmiHeader.biPlanes = 1;
1733     bi->bmiHeader.biBitCount = 1;
1734     bi->bmiHeader.biCompression = BI_RGB;
1735     bi->bmiHeader.biSizeImage = 0;
1736     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1737     memset(buf, 0xAA, sizeof(buf));
1738     SetLastError(0xdeadbeef);
1739     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1740     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1741        lines, bm.bmHeight, GetLastError());
1742     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1) ||
1743        broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1744        "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1745
1746     /* the color table consists of black and white */
1747     ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1748        bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1749        "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1750        bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1751        bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1752     ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1753        bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1754        "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1755        bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1756        bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1757     for (i = 2; i < 256; i++)
1758     {
1759         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1760            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1761            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1762            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1763            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1764     }
1765
1766     /* returned bits are DWORD aligned and upside down */
1767 todo_wine
1768     ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)) ||
1769        broken(!memcmp(buf, dib_bits_1_9x, sizeof(dib_bits_1_9x))), /* Win9x, WinME */
1770        "DIB bits don't match\n");
1771
1772     /* Test the palette indices */
1773     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1774     SetLastError(0xdeadbeef);
1775     lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1776     if (lines == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
1777         win_skip("Win9x/WinMe doesn't handle 0 for the number of scan lines\n");
1778     else
1779     {
1780         ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1781         ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1782     }
1783     for (i = 2; i < 256; i++)
1784         ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[i]);
1785
1786     /* retrieve 24-bit DIB data */
1787     memset(bi, 0, sizeof(*bi));
1788     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1789     bi->bmiHeader.biWidth = bm.bmWidth;
1790     bi->bmiHeader.biHeight = bm.bmHeight;
1791     bi->bmiHeader.biPlanes = 1;
1792     bi->bmiHeader.biBitCount = 24;
1793     bi->bmiHeader.biCompression = BI_RGB;
1794     bi->bmiHeader.biSizeImage = 0;
1795     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1796     memset(buf, 0xAA, sizeof(buf));
1797     SetLastError(0xdeadbeef);
1798     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1799     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1800        lines, bm.bmHeight, GetLastError());
1801     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24) ||
1802        broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1803        "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1804
1805     /* the color table doesn't exist for 24-bit images */
1806     for (i = 0; i < 256; i++)
1807     {
1808         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1809            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1810            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1811            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1812            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1813     }
1814
1815     /* returned bits are DWORD aligned and upside down */
1816     ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1817     DeleteObject(hbmp);
1818
1819     ReleaseDC(0, hdc);
1820 }
1821
1822 static void test_GetDIBits_BI_BITFIELDS(void)
1823 {
1824     /* Try a screen resolution detection technique
1825      * from the September 1999 issue of Windows Developer's Journal
1826      * which seems to be in widespread use.
1827      * http://www.lesher.ws/highcolor.html
1828      * http://www.lesher.ws/vidfmt.c
1829      * It hinges on being able to retrieve the bitmaps
1830      * for the three primary colors in non-paletted 16 bit mode.
1831      */
1832     char dibinfo_buf[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
1833     DWORD bits[32];
1834     LPBITMAPINFO dibinfo = (LPBITMAPINFO) dibinfo_buf;
1835     DWORD *bitmasks = (DWORD *)dibinfo->bmiColors;
1836     HDC hdc;
1837     HBITMAP hbm;
1838     int ret;
1839     void *ptr;
1840
1841     memset(dibinfo, 0, sizeof(dibinfo_buf));
1842     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1843
1844     hdc = GetDC(NULL);
1845     ok(hdc != NULL, "GetDC failed?\n");
1846     hbm = CreateCompatibleBitmap(hdc, 1, 1);
1847     ok(hbm != NULL, "CreateCompatibleBitmap failed?\n");
1848
1849     /* Call GetDIBits to fill in bmiHeader.  */
1850     ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1851     ok(ret == 1, "GetDIBits failed\n");
1852     if (dibinfo->bmiHeader.biBitCount > 8)
1853     {
1854         ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS,
1855             "compression is %u\n", dibinfo->bmiHeader.biCompression );
1856
1857         ok( !bitmasks[0], "red mask is set\n" );
1858         ok( !bitmasks[1], "green mask is set\n" );
1859         ok( !bitmasks[2], "blue mask is set\n" );
1860
1861         /* test with NULL bits pointer and correct bpp */
1862         dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1863         ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1864         ok(ret == 1, "GetDIBits failed\n");
1865
1866         ok( bitmasks[0] != 0, "red mask is not set\n" );
1867         ok( bitmasks[1] != 0, "green mask is not set\n" );
1868         ok( bitmasks[2] != 0, "blue mask is not set\n" );
1869         ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1870
1871         /* test with valid bits pointer */
1872         memset(dibinfo, 0, sizeof(dibinfo_buf));
1873         dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1874         ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1875         ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1876         dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1877         ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1878         ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1879
1880         ok( bitmasks[0] != 0, "red mask is not set\n" );
1881         ok( bitmasks[1] != 0, "green mask is not set\n" );
1882         ok( bitmasks[2] != 0, "blue mask is not set\n" );
1883         ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef ||
1884             broken(dibinfo->bmiHeader.biSizeImage == 0xdeadbeef), /* win9x */
1885             "size image not set\n" );
1886
1887         /* now with bits and 0 lines */
1888         memset(dibinfo, 0, sizeof(dibinfo_buf));
1889         dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1890         dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1891         SetLastError(0xdeadbeef);
1892         ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1893         if (ret == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
1894             win_skip("Win9x/WinMe doesn't handle 0 for the number of scan lines\n");
1895         else
1896         {
1897             ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1898
1899             ok( !bitmasks[0], "red mask is set\n" );
1900             ok( !bitmasks[1], "green mask is set\n" );
1901             ok( !bitmasks[2], "blue mask is set\n" );
1902             ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1903
1904             memset(bitmasks, 0, 3*sizeof(DWORD));
1905             dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1906             ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1907             ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1908
1909             ok( bitmasks[0] != 0, "red mask is not set\n" );
1910             ok( bitmasks[1] != 0, "green mask is not set\n" );
1911             ok( bitmasks[2] != 0, "blue mask is not set\n" );
1912             ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1913         }
1914     }
1915     else skip("not in 16 bpp BI_BITFIELDS mode, skipping that test\n");
1916
1917     DeleteObject(hbm);
1918
1919     /* same thing now with a 32-bpp DIB section */
1920
1921     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1922     dibinfo->bmiHeader.biWidth = 1;
1923     dibinfo->bmiHeader.biHeight = 1;
1924     dibinfo->bmiHeader.biPlanes = 1;
1925     dibinfo->bmiHeader.biBitCount = 32;
1926     dibinfo->bmiHeader.biCompression = BI_RGB;
1927     dibinfo->bmiHeader.biSizeImage = 0;
1928     dibinfo->bmiHeader.biXPelsPerMeter = 0;
1929     dibinfo->bmiHeader.biYPelsPerMeter = 0;
1930     dibinfo->bmiHeader.biClrUsed = 0;
1931     dibinfo->bmiHeader.biClrImportant = 0;
1932     bitmasks[0] = 0x0000ff;
1933     bitmasks[1] = 0x00ff00;
1934     bitmasks[2] = 0xff0000;
1935     hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
1936     ok( hbm != 0, "failed to create bitmap\n" );
1937
1938     memset(dibinfo, 0, sizeof(dibinfo_buf));
1939     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1940     ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
1941     ok(ret == 1, "GetDIBits failed\n");
1942     ok( dibinfo->bmiHeader.biBitCount == 32, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
1943
1944     ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS,
1945         "compression is %u\n", dibinfo->bmiHeader.biCompression );
1946     ok( !bitmasks[0], "red mask is set\n" );
1947     ok( !bitmasks[1], "green mask is set\n" );
1948     ok( !bitmasks[2], "blue mask is set\n" );
1949
1950     dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1951     ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1952     ok(ret == 1, "GetDIBits failed\n");
1953     ok( dibinfo->bmiHeader.biBitCount == 32, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
1954     ok( bitmasks[0] == 0xff0000, "wrong red mask %08x\n", bitmasks[0] );
1955     ok( bitmasks[1] == 0x00ff00, "wrong green mask %08x\n", bitmasks[1] );
1956     ok( bitmasks[2] == 0x0000ff, "wrong blue mask %08x\n", bitmasks[2] );
1957     ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef ||
1958         broken(dibinfo->bmiHeader.biSizeImage == 0xdeadbeef), /* win9x */
1959         "size image not set\n" );
1960
1961     DeleteObject(hbm);
1962
1963     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1964     dibinfo->bmiHeader.biWidth = 1;
1965     dibinfo->bmiHeader.biHeight = 1;
1966     dibinfo->bmiHeader.biPlanes = 1;
1967     dibinfo->bmiHeader.biBitCount = 32;
1968     dibinfo->bmiHeader.biCompression = BI_BITFIELDS;
1969     dibinfo->bmiHeader.biSizeImage = 0;
1970     dibinfo->bmiHeader.biXPelsPerMeter = 0;
1971     dibinfo->bmiHeader.biYPelsPerMeter = 0;
1972     dibinfo->bmiHeader.biClrUsed = 0;
1973     dibinfo->bmiHeader.biClrImportant = 0;
1974     bitmasks[0] = 0x0000ff;
1975     bitmasks[1] = 0x00ff00;
1976     bitmasks[2] = 0xff0000;
1977     hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
1978     ok( hbm != 0 || broken(!hbm), /* win9x */  "failed to create bitmap\n" );
1979
1980     if (hbm)
1981     {
1982         memset(dibinfo, 0, sizeof(dibinfo_buf));
1983         dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1984         ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
1985         ok(ret == 1, "GetDIBits failed\n");
1986
1987         ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS,
1988             "compression is %u\n", dibinfo->bmiHeader.biCompression );
1989         ok( !bitmasks[0], "red mask is set\n" );
1990         ok( !bitmasks[1], "green mask is set\n" );
1991         ok( !bitmasks[2], "blue mask is set\n" );
1992
1993         dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1994         ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1995         ok(ret == 1, "GetDIBits failed\n");
1996         ok( bitmasks[0] == 0x0000ff, "wrong red mask %08x\n", bitmasks[0] );
1997         ok( bitmasks[1] == 0x00ff00, "wrong green mask %08x\n", bitmasks[1] );
1998         ok( bitmasks[2] == 0xff0000, "wrong blue mask %08x\n", bitmasks[2] );
1999         ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
2000
2001         DeleteObject(hbm);
2002     }
2003
2004     /* 24-bpp DIB sections don't have bitfields */
2005
2006     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2007     dibinfo->bmiHeader.biWidth = 1;
2008     dibinfo->bmiHeader.biHeight = 1;
2009     dibinfo->bmiHeader.biPlanes = 1;
2010     dibinfo->bmiHeader.biBitCount = 24;
2011     dibinfo->bmiHeader.biCompression = BI_BITFIELDS;
2012     dibinfo->bmiHeader.biSizeImage = 0;
2013     dibinfo->bmiHeader.biXPelsPerMeter = 0;
2014     dibinfo->bmiHeader.biYPelsPerMeter = 0;
2015     dibinfo->bmiHeader.biClrUsed = 0;
2016     dibinfo->bmiHeader.biClrImportant = 0;
2017     hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
2018     ok( hbm == 0, "creating 24-bpp BI_BITFIELDS dibsection should fail\n" );
2019     dibinfo->bmiHeader.biCompression = BI_RGB;
2020     hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
2021     ok( hbm != 0, "failed to create bitmap\n" );
2022
2023     memset(dibinfo, 0, sizeof(dibinfo_buf));
2024     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2025     ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
2026     ok(ret == 1, "GetDIBits failed\n");
2027     ok( dibinfo->bmiHeader.biBitCount == 24, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
2028
2029     ok( dibinfo->bmiHeader.biCompression == BI_RGB,
2030         "compression is %u\n", dibinfo->bmiHeader.biCompression );
2031     ok( !bitmasks[0], "red mask is set\n" );
2032     ok( !bitmasks[1], "green mask is set\n" );
2033     ok( !bitmasks[2], "blue mask is set\n" );
2034
2035     dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
2036     ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
2037     ok(ret == 1, "GetDIBits failed\n");
2038     ok( dibinfo->bmiHeader.biBitCount == 24, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
2039     ok( !bitmasks[0], "red mask is set\n" );
2040     ok( !bitmasks[1], "green mask is set\n" );
2041     ok( !bitmasks[2], "blue mask is set\n" );
2042     ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef ||
2043         broken(dibinfo->bmiHeader.biSizeImage == 0xdeadbeef), /* win9x */
2044         "size image not set\n" );
2045
2046     DeleteObject(hbm);
2047     ReleaseDC(NULL, hdc);
2048 }
2049
2050 static void test_select_object(void)
2051 {
2052     HDC hdc;
2053     HBITMAP hbm, hbm_old;
2054     INT planes, bpp, i;
2055     DWORD depths[] = {8, 15, 16, 24, 32};
2056     BITMAP bm;
2057     DWORD bytes;
2058
2059     hdc = GetDC(0);
2060     ok(hdc != 0, "GetDC(0) failed\n");
2061     hbm = CreateCompatibleBitmap(hdc, 10, 10);
2062     ok(hbm != 0, "CreateCompatibleBitmap failed\n");
2063
2064     hbm_old = SelectObject(hdc, hbm);
2065     ok(hbm_old == 0, "SelectObject should fail\n");
2066
2067     DeleteObject(hbm);
2068     ReleaseDC(0, hdc);
2069
2070     hdc = CreateCompatibleDC(0);
2071     ok(hdc != 0, "GetDC(0) failed\n");
2072     hbm = CreateCompatibleBitmap(hdc, 10, 10);
2073     ok(hbm != 0, "CreateCompatibleBitmap failed\n");
2074
2075     hbm_old = SelectObject(hdc, hbm);
2076     ok(hbm_old != 0, "SelectObject failed\n");
2077     hbm_old = SelectObject(hdc, hbm_old);
2078     ok(hbm_old == hbm, "SelectObject failed\n");
2079
2080     DeleteObject(hbm);
2081
2082     /* test an 1-bpp bitmap */
2083     planes = GetDeviceCaps(hdc, PLANES);
2084     bpp = 1;
2085
2086     hbm = CreateBitmap(10, 10, planes, bpp, NULL);
2087     ok(hbm != 0, "CreateBitmap failed\n");
2088
2089     hbm_old = SelectObject(hdc, hbm);
2090     ok(hbm_old != 0, "SelectObject failed\n");
2091     hbm_old = SelectObject(hdc, hbm_old);
2092     ok(hbm_old == hbm, "SelectObject failed\n");
2093
2094     DeleteObject(hbm);
2095
2096     for(i = 0; i < sizeof(depths)/sizeof(depths[0]); i++) {
2097         /* test a color bitmap to dc bpp matching */
2098         planes = GetDeviceCaps(hdc, PLANES);
2099         bpp = GetDeviceCaps(hdc, BITSPIXEL);
2100
2101         hbm = CreateBitmap(10, 10, planes, depths[i], NULL);
2102         ok(hbm != 0, "CreateBitmap failed\n");
2103
2104         hbm_old = SelectObject(hdc, hbm);
2105         if(depths[i] == bpp ||
2106           (bpp == 16 && depths[i] == 15)        /* 16 and 15 bpp are compatible */
2107           ) {
2108             ok(hbm_old != 0, "SelectObject failed, BITSPIXEL: %d, created depth: %d\n", bpp, depths[i]);
2109             SelectObject(hdc, hbm_old);
2110         } else {
2111             ok(hbm_old == 0, "SelectObject should fail. BITSPIXELS: %d, created depth: %d\n", bpp, depths[i]);
2112         }
2113
2114         memset(&bm, 0xAA, sizeof(bm));
2115         bytes = GetObject(hbm, sizeof(bm), &bm);
2116         ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
2117         ok(bm.bmType == 0 ||
2118            broken(bm.bmType == 21072), /* win9x */
2119            "wrong bmType %d\n", bm.bmType);
2120         ok(bm.bmWidth == 10, "wrong bmWidth %d\n", bm.bmWidth);
2121         ok(bm.bmHeight == 10, "wrong bmHeight %d\n", bm.bmHeight);
2122         ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
2123         ok(bm.bmPlanes == planes, "wrong bmPlanes %u\n", bm.bmPlanes);
2124         if(depths[i] == 15) {
2125             ok(bm.bmBitsPixel == 16 ||
2126                broken(bm.bmBitsPixel == 15), /* Win9x/WinME */
2127                "wrong bmBitsPixel %d(15 bpp special)\n", bm.bmBitsPixel);
2128         } else {
2129             ok(bm.bmBitsPixel == depths[i], "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
2130         }
2131         ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
2132
2133         DeleteObject(hbm);
2134     }
2135
2136     DeleteDC(hdc);
2137 }
2138
2139 static void test_mono_1x1_bmp_dbg(HBITMAP hbmp, int line)
2140 {
2141     INT ret;
2142     BITMAP bm;
2143
2144     ret = GetObjectType(hbmp);
2145     ok_(__FILE__, line)(ret == OBJ_BITMAP, "the object %p is not bitmap\n", hbmp);
2146
2147     ret = GetObject(hbmp, 0, 0);
2148     ok_(__FILE__, line)(ret == sizeof(BITMAP) /* XP */ ||
2149                         ret == sizeof(DIBSECTION) /* Win9x */, "object size %d\n", ret);
2150
2151     memset(&bm, 0xDA, sizeof(bm));
2152     SetLastError(0xdeadbeef);
2153     ret = GetObject(hbmp, sizeof(bm), &bm);
2154     if (!ret) /* XP, only for curObj2 */ return;
2155     ok_(__FILE__, line)(ret == sizeof(BITMAP) ||
2156                         ret == sizeof(DIBSECTION) /* Win9x, only for curObj2 */,
2157                         "GetObject returned %d, error %u\n", ret, GetLastError());
2158     ok_(__FILE__, line)(bm.bmType == 0, "wrong bmType, expected 0 got %d\n", bm.bmType);
2159     ok_(__FILE__, line)(bm.bmWidth == 1, "wrong bmWidth, expected 1 got %d\n", bm.bmWidth);
2160     ok_(__FILE__, line)(bm.bmHeight == 1, "wrong bmHeight, expected 1 got %d\n", bm.bmHeight);
2161     ok_(__FILE__, line)(bm.bmWidthBytes == 2, "wrong bmWidthBytes, expected 2 got %d\n", bm.bmWidthBytes);
2162     ok_(__FILE__, line)(bm.bmPlanes == 1, "wrong bmPlanes, expected 1 got %u\n", bm.bmPlanes);
2163     ok_(__FILE__, line)(bm.bmBitsPixel == 1, "wrong bmBitsPixel, expected 1 got %d\n", bm.bmBitsPixel);
2164     ok_(__FILE__, line)(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
2165 }
2166
2167 #define test_mono_1x1_bmp(a) test_mono_1x1_bmp_dbg((a), __LINE__)
2168
2169 static void test_CreateBitmap(void)
2170 {
2171     BITMAP bmp;
2172     HDC screenDC = GetDC(0);
2173     HDC hdc = CreateCompatibleDC(screenDC);
2174     UINT i, expect = 0;
2175
2176     /* all of these are the stock monochrome bitmap */
2177     HBITMAP bm = CreateCompatibleBitmap(hdc, 0, 0);
2178     HBITMAP bm1 = CreateCompatibleBitmap(screenDC, 0, 0);
2179     HBITMAP bm4 = CreateBitmap(0, 1, 0, 0, 0);
2180     HBITMAP bm5 = CreateDiscardableBitmap(hdc, 0, 0);
2181     HBITMAP curObj1 = GetCurrentObject(hdc, OBJ_BITMAP);
2182     HBITMAP curObj2 = GetCurrentObject(screenDC, OBJ_BITMAP);
2183
2184     /* these 2 are not the stock monochrome bitmap */
2185     HBITMAP bm2 = CreateCompatibleBitmap(hdc, 1, 1);
2186     HBITMAP bm3 = CreateBitmap(1, 1, 1, 1, 0);
2187
2188     HBITMAP old1 = SelectObject(hdc, bm2);
2189     HBITMAP old2 = SelectObject(screenDC, bm3);
2190     SelectObject(hdc, old1);
2191     SelectObject(screenDC, old2);
2192
2193     ok(bm == bm1 && bm == bm4 && bm == bm5 && bm == curObj1 && bm == old1,
2194        "0: %p, 1: %p, 4: %p, 5: %p, curObj1 %p, old1 %p\n",
2195        bm, bm1, bm4, bm5, curObj1, old1);
2196     ok(bm != bm2 && bm != bm3, "0: %p, 2: %p, 3: %p\n", bm, bm2, bm3);
2197 todo_wine
2198     ok(bm != curObj2 || /* WinXP */
2199        broken(bm == curObj2) /* Win9x */,
2200        "0: %p, curObj2 %p\n", bm, curObj2);
2201     ok(old2 == 0, "old2 %p\n", old2);
2202
2203     test_mono_1x1_bmp(bm);
2204     test_mono_1x1_bmp(bm1);
2205     test_mono_1x1_bmp(bm2);
2206     test_mono_1x1_bmp(bm3);
2207     test_mono_1x1_bmp(bm4);
2208     test_mono_1x1_bmp(bm5);
2209     test_mono_1x1_bmp(old1);
2210     test_mono_1x1_bmp(curObj1);
2211
2212     DeleteObject(bm);
2213     DeleteObject(bm1);
2214     DeleteObject(bm2);
2215     DeleteObject(bm3);
2216     DeleteObject(bm4);
2217     DeleteObject(bm5);
2218
2219     DeleteDC(hdc);
2220     ReleaseDC(0, screenDC);
2221
2222     /* show that Windows ignores the provided bm.bmWidthBytes */
2223     bmp.bmType = 0;
2224     bmp.bmWidth = 1;
2225     bmp.bmHeight = 1;
2226     bmp.bmWidthBytes = 28;
2227     bmp.bmPlanes = 1;
2228     bmp.bmBitsPixel = 1;
2229     bmp.bmBits = NULL;
2230     bm = CreateBitmapIndirect(&bmp);
2231     ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2232     test_mono_1x1_bmp(bm);
2233     DeleteObject(bm);
2234
2235     /* Test how the bmBitsPixel field is treated */
2236     for(i = 1; i <= 33; i++) {
2237         bmp.bmType = 0;
2238         bmp.bmWidth = 1;
2239         bmp.bmHeight = 1;
2240         bmp.bmWidthBytes = 28;
2241         bmp.bmPlanes = 1;
2242         bmp.bmBitsPixel = i;
2243         bmp.bmBits = NULL;
2244         SetLastError(0xdeadbeef);
2245         bm = CreateBitmapIndirect(&bmp);
2246         if(i > 32) {
2247             DWORD error = GetLastError();
2248             ok(bm == 0 ||
2249                broken(bm != 0), /* Win9x and WinMe */
2250                "CreateBitmapIndirect for %d bpp succeeded\n", i);
2251             ok(error == ERROR_INVALID_PARAMETER ||
2252                broken(error == 0xdeadbeef), /* Win9x and WinME */
2253                "Got error %d, expected ERROR_INVALID_PARAMETER\n", error);
2254             DeleteObject(bm);
2255             continue;
2256         }
2257         ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2258         GetObject(bm, sizeof(bmp), &bmp);
2259         if(i == 1) {
2260             expect = 1;
2261         } else if(i <= 4) {
2262             expect = 4;
2263         } else if(i <= 8) {
2264             expect = 8;
2265         } else if(i <= 16) {
2266             expect = 16;
2267         } else if(i <= 24) {
2268             expect = 24;
2269         } else if(i <= 32) {
2270             expect = 32;
2271         }
2272         ok(bmp.bmBitsPixel == expect ||
2273            broken(bmp.bmBitsPixel == i), /* Win9x and WinMe */
2274            "CreateBitmapIndirect for a %d bpp bitmap created a %d bpp bitmap, expected %d\n",
2275            i, bmp.bmBitsPixel, expect);
2276         DeleteObject(bm);
2277     }
2278 }
2279
2280 static void test_bitmapinfoheadersize(void)
2281 {
2282     HBITMAP hdib;
2283     BITMAPINFO bmi;
2284     BITMAPCOREINFO bci;
2285     HDC hdc = GetDC(0);
2286
2287     memset(&bmi, 0, sizeof(BITMAPINFO));
2288     bmi.bmiHeader.biHeight = 100;
2289     bmi.bmiHeader.biWidth = 512;
2290     bmi.bmiHeader.biBitCount = 24;
2291     bmi.bmiHeader.biPlanes = 1;
2292
2293     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER) - 1;
2294
2295     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2296     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2297
2298     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2299
2300     SetLastError(0xdeadbeef);
2301     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2302     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2303     DeleteObject(hdib);
2304
2305     bmi.bmiHeader.biSize++;
2306
2307     SetLastError(0xdeadbeef);
2308     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2309     ok(hdib != NULL ||
2310        broken(!hdib), /* Win98, WinMe */
2311        "CreateDIBSection error %d\n", GetLastError());
2312     DeleteObject(hdib);
2313
2314     bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
2315
2316     SetLastError(0xdeadbeef);
2317     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2318     ok(hdib != NULL ||
2319        broken(!hdib), /* Win98, WinMe */
2320        "CreateDIBSection error %d\n", GetLastError());
2321     DeleteObject(hdib);
2322
2323     bmi.bmiHeader.biSize++;
2324
2325     SetLastError(0xdeadbeef);
2326     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2327     ok(hdib != NULL ||
2328        broken(!hdib), /* Win98, WinMe */
2329        "CreateDIBSection error %d\n", GetLastError());
2330     DeleteObject(hdib);
2331
2332     bmi.bmiHeader.biSize = sizeof(BITMAPV4HEADER);
2333
2334     SetLastError(0xdeadbeef);
2335     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2336     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2337     DeleteObject(hdib);
2338
2339     bmi.bmiHeader.biSize = sizeof(BITMAPV5HEADER);
2340
2341     SetLastError(0xdeadbeef);
2342     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2343     ok(hdib != NULL ||
2344        broken(!hdib), /* Win95 */
2345        "CreateDIBSection error %d\n", GetLastError());
2346     DeleteObject(hdib);
2347
2348     memset(&bci, 0, sizeof(BITMAPCOREINFO));
2349     bci.bmciHeader.bcHeight = 100;
2350     bci.bmciHeader.bcWidth = 512;
2351     bci.bmciHeader.bcBitCount = 24;
2352     bci.bmciHeader.bcPlanes = 1;
2353
2354     bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER) - 1;
2355
2356     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2357     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2358
2359     bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
2360
2361     SetLastError(0xdeadbeef);
2362     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2363     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2364     DeleteObject(hdib);
2365
2366     bci.bmciHeader.bcSize++;
2367
2368     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2369     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2370
2371     bci.bmciHeader.bcSize = sizeof(BITMAPCOREINFO);
2372
2373     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2374     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2375
2376     ReleaseDC(0, hdc);
2377 }
2378
2379 static void test_get16dibits(void)
2380 {
2381     BYTE bits[4 * (16 / sizeof(BYTE))];
2382     HBITMAP hbmp;
2383     HDC screen_dc = GetDC(NULL);
2384     int ret;
2385     BITMAPINFO * info;
2386     int info_len = sizeof(BITMAPINFOHEADER) + 1024;
2387     BYTE *p;
2388     int overwritten_bytes = 0;
2389
2390     memset(bits, 0, sizeof(bits));
2391     hbmp = CreateBitmap(2, 2, 1, 16, bits);
2392     ok(hbmp != NULL, "CreateBitmap failed\n");
2393
2394     info  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info_len);
2395     assert(info);
2396
2397     memset(info, '!', info_len);
2398     memset(info, 0, sizeof(info->bmiHeader));
2399
2400     info->bmiHeader.biSize = sizeof(info->bmiHeader);
2401     info->bmiHeader.biWidth = 2;
2402     info->bmiHeader.biHeight = 2;
2403     info->bmiHeader.biPlanes = 1;
2404     info->bmiHeader.biCompression = BI_RGB;
2405
2406     ret = GetDIBits(screen_dc, hbmp, 0, 0, NULL, info, 0);
2407     ok(ret != 0 ||
2408        broken(ret == 0), /* win9x */
2409        "GetDIBits failed got %d\n", ret);
2410
2411     for (p = ((BYTE *) info) + sizeof(info->bmiHeader); (p - ((BYTE *) info)) < info_len; p++)
2412         if (*p != '!')
2413             overwritten_bytes++;
2414     ok(overwritten_bytes == 0, "GetDIBits wrote past the buffer given\n");
2415
2416     HeapFree(GetProcessHeap(), 0, info);
2417     DeleteObject(hbmp);
2418     ReleaseDC(NULL, screen_dc);
2419 }
2420
2421 static BOOL compare_buffers_no_alpha(UINT32 *a, UINT32 *b, int length)
2422 {
2423     int i;
2424     for(i = 0; i < length; i++)
2425         if((a[i] & 0x00FFFFFF) != (b[i] & 0x00FFFFFF))
2426             return FALSE;
2427     return TRUE;
2428 }
2429
2430 static void check_BitBlt_pixel(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2431                                DWORD dwRop, UINT32 expected, int line)
2432 {
2433     *srcBuffer = 0xFEDCBA98;
2434     *dstBuffer = 0x89ABCDEF;
2435     Rectangle(hdcSrc, 0, 0, 1, 1);  /* A null operation to ensure dibs are coerced to X11 */
2436     BitBlt(hdcDst, 0, 0, 1, 1, hdcSrc, 0, 0, dwRop);
2437     ok(expected == *dstBuffer,
2438         "BitBlt with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2439         dwRop, expected, *dstBuffer, line);
2440 }
2441
2442 static void test_BitBlt(void)
2443 {
2444     HBITMAP bmpDst, bmpSrc;
2445     HBITMAP oldDst, oldSrc;
2446     HDC hdcScreen, hdcDst, hdcSrc;
2447     UINT32 *dstBuffer, *srcBuffer;
2448     HBRUSH hBrush, hOldBrush;
2449     BITMAPINFO bitmapInfo;
2450
2451     memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2452     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2453     bitmapInfo.bmiHeader.biWidth = 1;
2454     bitmapInfo.bmiHeader.biHeight = 1;
2455     bitmapInfo.bmiHeader.biPlanes = 1;
2456     bitmapInfo.bmiHeader.biBitCount = 32;
2457     bitmapInfo.bmiHeader.biCompression = BI_RGB;
2458     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
2459
2460     hdcScreen = CreateCompatibleDC(0);
2461     hdcDst = CreateCompatibleDC(hdcScreen);
2462     hdcSrc = CreateCompatibleDC(hdcDst);
2463
2464     /* Setup the destination dib section */
2465     bmpDst = CreateDIBSection(hdcScreen, &bitmapInfo, DIB_RGB_COLORS, (void**)&dstBuffer,
2466         NULL, 0);
2467     oldDst = SelectObject(hdcDst, bmpDst);
2468
2469     hBrush = CreateSolidBrush(0x012345678);
2470     hOldBrush = SelectObject(hdcDst, hBrush);
2471
2472     /* Setup the source dib section */
2473     bmpSrc = CreateDIBSection(hdcScreen, &bitmapInfo, DIB_RGB_COLORS, (void**)&srcBuffer,
2474         NULL, 0);
2475     oldSrc = SelectObject(hdcSrc, bmpSrc);
2476
2477     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2478     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2479     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2480     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2481     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2482     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2483     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2484     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2485     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2486     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2487     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2488     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2489     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2490     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2491     check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2492
2493     /* Tidy up */
2494     SelectObject(hdcSrc, oldSrc);
2495     DeleteObject(bmpSrc);
2496     DeleteDC(hdcSrc);
2497
2498     SelectObject(hdcDst, hOldBrush);
2499     DeleteObject(hBrush);
2500     SelectObject(hdcDst, oldDst);
2501     DeleteObject(bmpDst);
2502     DeleteDC(hdcDst);
2503
2504
2505     DeleteDC(hdcScreen);
2506 }
2507
2508 static void check_StretchBlt_pixel(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2509                                    DWORD dwRop, UINT32 expected, int line)
2510 {
2511     *srcBuffer = 0xFEDCBA98;
2512     *dstBuffer = 0x89ABCDEF;
2513     StretchBlt(hdcDst, 0, 0, 2, 1, hdcSrc, 0, 0, 1, 1, dwRop);
2514     ok(expected == *dstBuffer,
2515         "StretchBlt with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2516         dwRop, expected, *dstBuffer, line);
2517 }
2518
2519 static void check_StretchBlt_stretch(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2520                                      int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,
2521                                      int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
2522                                      UINT32 expected[4], UINT32 legacy_expected[4], int line)
2523 {
2524     memset(dstBuffer, 0, 16);
2525     StretchBlt(hdcDst, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
2526                hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
2527     ok(memcmp(dstBuffer, expected, 16) == 0 ||
2528         broken(compare_buffers_no_alpha(dstBuffer, legacy_expected, 4)),
2529         "StretchBlt expected { %08X, %08X, %08X, %08X } got { %08X, %08X, %08X, %08X } "
2530         "stretching { %d, %d, %d, %d } to { %d, %d, %d, %d } from line %d\n",
2531         expected[0], expected[1], expected[2], expected[3],
2532         dstBuffer[0], dstBuffer[1], dstBuffer[2], dstBuffer[3],
2533         nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2534         nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, line);
2535 }
2536
2537 static void test_StretchBlt(void)
2538 {
2539     HBITMAP bmpDst, bmpSrc;
2540     HBITMAP oldDst, oldSrc;
2541     HDC hdcScreen, hdcDst, hdcSrc;
2542     UINT32 *dstBuffer, *srcBuffer;
2543     HBRUSH hBrush, hOldBrush;
2544     BITMAPINFO biDst, biSrc;
2545     UINT32 expected[4], legacy_expected[4];
2546
2547     memset(&biDst, 0, sizeof(BITMAPINFO));
2548     biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2549     biDst.bmiHeader.biWidth = 2;
2550     biDst.bmiHeader.biHeight = -2;
2551     biDst.bmiHeader.biPlanes = 1;
2552     biDst.bmiHeader.biBitCount = 32;
2553     biDst.bmiHeader.biCompression = BI_RGB;
2554     memcpy(&biSrc, &biDst, sizeof(BITMAPINFO));
2555
2556     hdcScreen = CreateCompatibleDC(0);
2557     hdcDst = CreateCompatibleDC(hdcScreen);
2558     hdcSrc = CreateCompatibleDC(hdcDst);
2559
2560     /* Pixel Tests */
2561     bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2562         NULL, 0);
2563     oldDst = SelectObject(hdcDst, bmpDst);
2564
2565     bmpSrc = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&srcBuffer,
2566         NULL, 0);
2567     oldSrc = SelectObject(hdcSrc, bmpSrc);
2568
2569     hBrush = CreateSolidBrush(0x012345678);
2570     hOldBrush = SelectObject(hdcDst, hBrush);
2571
2572     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2573     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2574     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2575     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2576     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2577     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2578     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2579     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2580     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2581     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2582     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2583     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2584     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2585     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2586     check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2587
2588     SelectObject(hdcDst, hOldBrush);
2589     DeleteObject(hBrush);
2590
2591     /* Top-down to top-down tests */
2592     srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2593     srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2594
2595     expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2596     expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2597     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2598                              0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2599
2600     expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2601     expected[2] = 0x00000000, expected[3] = 0x00000000;
2602     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2603                              0, 0, 1, 1, 0, 0, 1, 1, expected, expected, __LINE__);
2604
2605     expected[0] = 0xCAFED00D, expected[1] = 0xCAFED00D;
2606     expected[2] = 0xCAFED00D, expected[3] = 0xCAFED00D;
2607     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2608                              0, 0, 2, 2, 0, 0, 1, 1, expected, expected, __LINE__);
2609
2610     expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2611     expected[2] = 0x00000000, expected[3] = 0x00000000;
2612     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2613                              0, 0, 1, 1, 0, 0, 2, 2, expected, expected, __LINE__);
2614
2615     expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2616     expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2617     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2618                              0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2619
2620     expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2621     expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2622     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2623                              1, 1, -2, -2, 0, 0, 2, 2, expected, expected, __LINE__);
2624
2625     /* This result seems broken. One might expect the following result:
2626      * 0xCAFED00D 0xFEEDFACE
2627      * 0xFEDCBA98 0x76543210
2628      */
2629     expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2630     expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2631     legacy_expected[0] = 0xCAFED00D, legacy_expected[1] = 0x00000000;
2632     legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2633     todo_wine check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2634                                        1, 1, -2, -2, 1, 1, -2, -2, expected,
2635                                        legacy_expected, __LINE__);
2636
2637     expected[0] = 0x00000000, expected[1] = 0x00000000;
2638     expected[2] = 0x00000000, expected[3] = 0xCAFED00D;
2639     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2640                              1, 1, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2641
2642     SelectObject(hdcDst, oldDst);
2643     DeleteObject(bmpDst);
2644
2645     /* Top-down to bottom-up tests */
2646     biDst.bmiHeader.biHeight = 2;
2647     bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2648         NULL, 0);
2649     oldDst = SelectObject(hdcDst, bmpDst);
2650
2651     expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2652     expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2653     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2654                              0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2655
2656     expected[0] = 0xFEEDFACE, expected[1] = 0xCAFED00D;
2657     expected[2] = 0x76543210, expected[3] = 0xFEDCBA98;
2658     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2659                              0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2660
2661     SelectObject(hdcSrc, oldSrc);
2662     DeleteObject(bmpSrc);
2663
2664     /* Bottom-up to bottom-up tests */
2665     biSrc.bmiHeader.biHeight = 2;
2666     bmpSrc = CreateDIBSection(hdcScreen, &biSrc, DIB_RGB_COLORS, (void**)&srcBuffer,
2667         NULL, 0);
2668     srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2669     srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2670     oldSrc = SelectObject(hdcSrc, bmpSrc);
2671
2672     expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2673     expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2674     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2675                              0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2676
2677     expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2678     expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2679     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2680                              0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2681
2682     SelectObject(hdcDst, oldDst);
2683     DeleteObject(bmpDst);
2684
2685     /* Bottom-up to top-down tests */
2686     biDst.bmiHeader.biHeight = -2;
2687     bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2688         NULL, 0);
2689     oldDst = SelectObject(hdcDst, bmpDst);
2690
2691     expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2692     expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2693     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2694                              0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2695
2696     expected[0] = 0xFEEDFACE, expected[1] = 0xCAFED00D;
2697     expected[2] = 0x76543210, expected[3] = 0xFEDCBA98;
2698     check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2699                              0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2700
2701     /* Tidy up */
2702     SelectObject(hdcSrc, oldSrc);
2703     DeleteObject(bmpSrc);
2704     DeleteDC(hdcSrc);
2705
2706     SelectObject(hdcDst, oldDst);
2707     DeleteObject(bmpDst);
2708     DeleteDC(hdcDst);
2709
2710     DeleteDC(hdcScreen);
2711 }
2712
2713 static void check_StretchDIBits_pixel(HDC hdcDst, UINT32 *dstBuffer, UINT32 *srcBuffer,
2714                                       DWORD dwRop, UINT32 expected, int line)
2715 {
2716     const UINT32 buffer[2] = { 0xFEDCBA98, 0 };
2717     BITMAPINFO bitmapInfo;
2718
2719     memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2720     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2721     bitmapInfo.bmiHeader.biWidth = 2;
2722     bitmapInfo.bmiHeader.biHeight = 1;
2723     bitmapInfo.bmiHeader.biPlanes = 1;
2724     bitmapInfo.bmiHeader.biBitCount = 32;
2725     bitmapInfo.bmiHeader.biCompression = BI_RGB;
2726     bitmapInfo.bmiHeader.biSizeImage = sizeof(buffer);
2727
2728     *dstBuffer = 0x89ABCDEF;
2729
2730     StretchDIBits(hdcDst, 0, 0, 2, 1, 0, 0, 1, 1, &buffer, &bitmapInfo, DIB_RGB_COLORS, dwRop);
2731     ok(expected == *dstBuffer,
2732         "StretchDIBits with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2733         dwRop, expected, *dstBuffer, line);
2734 }
2735
2736 static void check_StretchDIBits_stretch(HDC hdcDst, UINT32 *dstBuffer, UINT32 *srcBuffer,
2737                                         int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,
2738                                         int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
2739                                         UINT32 expected[4], UINT32 legacy_expected[4], int line)
2740 {
2741     BITMAPINFO bitmapInfo;
2742
2743     memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2744     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2745     bitmapInfo.bmiHeader.biWidth = 2;
2746     bitmapInfo.bmiHeader.biHeight = -2;
2747     bitmapInfo.bmiHeader.biPlanes = 1;
2748     bitmapInfo.bmiHeader.biBitCount = 32;
2749     bitmapInfo.bmiHeader.biCompression = BI_RGB;
2750
2751     memset(dstBuffer, 0, 16);
2752     StretchDIBits(hdcDst, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
2753                   nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2754                   srcBuffer, &bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
2755     ok(memcmp(dstBuffer, expected, 16) == 0 ||                              /* Win2k/XP */
2756         broken(compare_buffers_no_alpha(dstBuffer, legacy_expected, 4)) ||  /* Win9X/ME */
2757         broken(nWidthSrc < 0 || nHeightSrc < 0),                            /* Win9X/ME */
2758         "StretchDIBits expected { %08X, %08X, %08X, %08X } got { %08X, %08X, %08X, %08X } "
2759         "stretching { %d, %d, %d, %d } to { %d, %d, %d, %d } from line %d\n",
2760         expected[0], expected[1], expected[2], expected[3],
2761         dstBuffer[0], dstBuffer[1], dstBuffer[2], dstBuffer[3],
2762         nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2763         nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, line);
2764 }
2765
2766 static void test_StretchDIBits(void)
2767 {
2768     HBITMAP bmpDst;
2769     HBITMAP oldDst;
2770     HDC hdcScreen, hdcDst;
2771     UINT32 *dstBuffer, srcBuffer[4];
2772     HBRUSH hBrush, hOldBrush;
2773     BITMAPINFO biDst;
2774     UINT32 expected[4], legacy_expected[4];
2775
2776     memset(&biDst, 0, sizeof(BITMAPINFO));
2777     biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2778     biDst.bmiHeader.biWidth = 2;
2779     biDst.bmiHeader.biHeight = -2;
2780     biDst.bmiHeader.biPlanes = 1;
2781     biDst.bmiHeader.biBitCount = 32;
2782     biDst.bmiHeader.biCompression = BI_RGB;
2783
2784     hdcScreen = CreateCompatibleDC(0);
2785     hdcDst = CreateCompatibleDC(hdcScreen);
2786
2787     /* Pixel Tests */
2788     bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2789         NULL, 0);
2790     oldDst = SelectObject(hdcDst, bmpDst);
2791
2792     hBrush = CreateSolidBrush(0x012345678);
2793     hOldBrush = SelectObject(hdcDst, hBrush);
2794
2795     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2796     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2797     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2798     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2799     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2800     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2801     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2802     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2803     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2804     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2805     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2806     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2807     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2808     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2809     check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2810
2811     SelectObject(hdcDst, hOldBrush);
2812     DeleteObject(hBrush);
2813
2814     /* Top-down destination tests */
2815     srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2816     srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2817
2818     expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2819     expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2820     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2821                                 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2822
2823     expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2824     expected[2] = 0x00000000, expected[3] = 0x00000000;
2825     legacy_expected[0] = 0xFEDCBA98, legacy_expected[1] = 0x00000000;
2826     legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2827     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2828                                 0, 0, 1, 1, 0, 0, 1, 1, expected, legacy_expected, __LINE__);
2829
2830     expected[0] = 0xFEDCBA98, expected[1] = 0xFEDCBA98;
2831     expected[2] = 0xFEDCBA98, expected[3] = 0xFEDCBA98;
2832     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2833                                 0, 0, 2, 2, 0, 0, 1, 1, expected, expected, __LINE__);
2834
2835     expected[0] = 0x42441000, expected[1] = 0x00000000;
2836     expected[2] = 0x00000000, expected[3] = 0x00000000;
2837     legacy_expected[0] = 0x00543210, legacy_expected[1] = 0x00000000;
2838     legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2839     todo_wine check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2840                                 0, 0, 1, 1, 0, 0, 2, 2, expected, legacy_expected, __LINE__);
2841
2842     expected[0] = 0x00000000, expected[1] = 0x00000000;
2843     expected[2] = 0x00000000, expected[3] = 0x00000000;
2844     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2845                                 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2846
2847     expected[0] = 0x00000000, expected[1] = 0x00000000;
2848     expected[2] = 0x00000000, expected[3] = 0x00000000;
2849     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2850                                 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2851
2852     expected[0] = 0x00000000, expected[1] = 0x00000000;
2853     expected[2] = 0x00000000, expected[3] = 0x00000000;
2854     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2855                                 1, 1, -2, -2, 1, 1, -2, -2, expected, expected, __LINE__);
2856
2857     expected[0] = 0x00000000, expected[1] = 0x00000000;
2858     expected[2] = 0x00000000, expected[3] = 0xCAFED00D;
2859     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2860                                 1, 1, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2861
2862     SelectObject(hdcDst, oldDst);
2863     DeleteObject(bmpDst);
2864
2865     /* Bottom up destination tests */
2866     biDst.bmiHeader.biHeight = 2;
2867     bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2868         NULL, 0);
2869     oldDst = SelectObject(hdcDst, bmpDst);
2870
2871     expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2872     expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2873     check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2874                                 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2875
2876     /* Tidy up */
2877     SelectObject(hdcDst, oldDst);
2878     DeleteObject(bmpDst);
2879     DeleteDC(hdcDst);
2880
2881     DeleteDC(hdcScreen);
2882 }
2883
2884 static void test_GdiAlphaBlend(void)
2885 {
2886     /* test out-of-bound parameters for GdiAlphaBlend */
2887     HDC hdcNull;
2888
2889     HDC hdcDst;
2890     HBITMAP bmpDst;
2891     HBITMAP oldDst;
2892
2893     BITMAPINFO bmi;
2894     HDC hdcSrc;
2895     HBITMAP bmpSrc;
2896     HBITMAP oldSrc;
2897     LPVOID bits;
2898
2899     BLENDFUNCTION blend;
2900
2901     if (!pGdiAlphaBlend)
2902     {
2903         win_skip("GdiAlphaBlend() is not implemented\n");
2904         return;
2905     }
2906
2907     hdcNull = GetDC(NULL);
2908     hdcDst = CreateCompatibleDC(hdcNull);
2909     bmpDst = CreateCompatibleBitmap(hdcNull, 100, 100);
2910     hdcSrc = CreateCompatibleDC(hdcNull);
2911
2912     memset(&bmi, 0, sizeof(bmi));  /* as of Wine 0.9.44 we require the src to be a DIB section */
2913     bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
2914     bmi.bmiHeader.biHeight = 20;
2915     bmi.bmiHeader.biWidth = 20;
2916     bmi.bmiHeader.biBitCount = 32;
2917     bmi.bmiHeader.biPlanes = 1;
2918     bmi.bmiHeader.biCompression = BI_RGB;
2919     bmpSrc = CreateDIBSection(hdcDst, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
2920     ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2921
2922     oldDst = SelectObject(hdcDst, bmpDst);
2923     oldSrc = SelectObject(hdcSrc, bmpSrc);
2924
2925     blend.BlendOp = AC_SRC_OVER;
2926     blend.BlendFlags = 0;
2927     blend.SourceConstantAlpha = 128;
2928     blend.AlphaFormat = 0;
2929
2930     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, 0, 20, 20, blend), TRUE, BOOL, "%d");
2931     SetLastError(0xdeadbeef);
2932     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), FALSE, BOOL, "%d");
2933     expect_eq(GetLastError(), ERROR_INVALID_PARAMETER, int, "%d");
2934     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), FALSE, BOOL, "%d");
2935     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 15, 0, 10, 10, blend), FALSE, BOOL, "%d");
2936     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2937     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2938
2939     SetWindowOrgEx(hdcSrc, -10, -10, NULL);
2940     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), TRUE, BOOL, "%d");
2941     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), TRUE, BOOL, "%d");
2942     SetMapMode(hdcSrc, MM_ANISOTROPIC);
2943     ScaleWindowExtEx(hdcSrc, 10, 1, 10, 1, NULL);
2944     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 30, 30, blend), TRUE, BOOL, "%d");
2945     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 30, 30, blend), TRUE, BOOL, "%d");
2946
2947     SetLastError(0xdeadbeef);
2948     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, NULL, 0, 0, 20, 20, blend), FALSE, BOOL, "%d");
2949     expect_eq(GetLastError(), 0xdeadbeef, int, "%d");
2950
2951     SelectObject(hdcDst, oldDst);
2952     SelectObject(hdcSrc, oldSrc);
2953     DeleteObject(bmpSrc);
2954     DeleteObject(bmpDst);
2955     DeleteDC(hdcDst);
2956     DeleteDC(hdcSrc);
2957
2958     ReleaseDC(NULL, hdcNull);
2959
2960 }
2961
2962 static void test_clipping(void)
2963 {
2964     HBITMAP bmpDst;
2965     HBITMAP bmpSrc;
2966     HRGN hRgn;
2967     LPVOID bits;
2968     BOOL result;
2969
2970     HDC hdcDst = CreateCompatibleDC( NULL );
2971     HDC hdcSrc = CreateCompatibleDC( NULL );
2972
2973     BITMAPINFO bmpinfo={{0}};
2974     bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2975     bmpinfo.bmiHeader.biWidth = 100;
2976     bmpinfo.bmiHeader.biHeight = 100;
2977     bmpinfo.bmiHeader.biPlanes = 1;
2978     bmpinfo.bmiHeader.biBitCount = GetDeviceCaps( hdcDst, BITSPIXEL );
2979     bmpinfo.bmiHeader.biCompression = BI_RGB;
2980
2981     bmpDst = CreateDIBSection( hdcDst, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2982     ok(bmpDst != NULL, "Couldn't create destination bitmap\n");
2983     SelectObject( hdcDst, bmpDst );
2984
2985     bmpSrc = CreateDIBSection( hdcSrc, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2986     ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2987     SelectObject( hdcSrc, bmpSrc );
2988
2989     result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 100, 100, SRCCOPY );
2990     ok(result, "BitBlt failed\n");
2991
2992     hRgn = CreateRectRgn( 0,0,0,0 );
2993     SelectClipRgn( hdcDst, hRgn );
2994
2995     result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 0, 0, SRCCOPY );
2996     ok(result, "BitBlt failed\n");
2997
2998     DeleteObject( bmpDst );
2999     DeleteObject( bmpSrc );
3000     DeleteObject( hRgn );
3001     DeleteDC( hdcDst );
3002     DeleteDC( hdcSrc );
3003 }
3004
3005 static void test_32bit_bitmap_blt(void)
3006 {
3007     BITMAPINFO biDst;
3008     HBITMAP bmpSrc, bmpDst;
3009     HBITMAP oldSrc, oldDst;
3010     HDC hdcSrc, hdcDst, hdcScreen;
3011     UINT32 *dstBuffer;
3012     DWORD colorSrc = 0x11223344;
3013
3014     memset(&biDst, 0, sizeof(BITMAPINFO));
3015     biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3016     biDst.bmiHeader.biWidth = 2;
3017     biDst.bmiHeader.biHeight = -2;
3018     biDst.bmiHeader.biPlanes = 1;
3019     biDst.bmiHeader.biBitCount = 32;
3020     biDst.bmiHeader.biCompression = BI_RGB;
3021
3022     hdcScreen = CreateCompatibleDC(0);
3023     if(GetDeviceCaps(hdcScreen, BITSPIXEL) != 32)
3024     {
3025         DeleteDC(hdcScreen);
3026         trace("Skipping 32-bit DDB test\n");
3027         return;
3028     }
3029
3030     hdcSrc = CreateCompatibleDC(hdcScreen);
3031     bmpSrc = CreateBitmap(1, 1, 1, 32, &colorSrc);
3032     oldSrc = SelectObject(hdcSrc, bmpSrc);
3033
3034     hdcDst = CreateCompatibleDC(hdcScreen);
3035     bmpDst = CreateDIBSection(hdcDst, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer, NULL, 0);
3036     oldDst = SelectObject(hdcDst, bmpDst);
3037
3038     StretchBlt(hdcDst, 0, 0, 1, 1, hdcSrc, 0, 0, 1, 1, SRCCOPY);
3039     ok(dstBuffer[0] == colorSrc, "Expected color=%x, received color=%x\n", colorSrc, dstBuffer[0]);
3040
3041     /* Tidy up */
3042     SelectObject(hdcDst, oldDst);
3043     DeleteObject(bmpDst);
3044     DeleteDC(hdcDst);
3045
3046     SelectObject(hdcSrc, oldSrc);
3047     DeleteObject(bmpSrc);
3048     DeleteDC(hdcSrc);
3049
3050     DeleteDC(hdcScreen);
3051 }
3052
3053 START_TEST(bitmap)
3054 {
3055     HMODULE hdll;
3056     is_win9x = GetWindowLongPtrW(GetDesktopWindow(), GWLP_WNDPROC) == 0;
3057
3058     hdll = GetModuleHandle("gdi32.dll");
3059     pGdiAlphaBlend = (void*)GetProcAddress(hdll, "GdiAlphaBlend");
3060
3061     test_createdibitmap();
3062     test_dibsections();
3063     test_mono_dibsection();
3064     test_bitmap();
3065     test_bmBits();
3066     test_GetDIBits_selected_DIB(1);
3067     test_GetDIBits_selected_DIB(4);
3068     test_GetDIBits_selected_DIB(8);
3069     test_GetDIBits_selected_DDB(TRUE);
3070     test_GetDIBits_selected_DDB(FALSE);
3071     test_GetDIBits();
3072     test_GetDIBits_BI_BITFIELDS();
3073     test_select_object();
3074     test_CreateBitmap();
3075     test_BitBlt();
3076     test_StretchBlt();
3077     test_StretchDIBits();
3078     test_GdiAlphaBlend();
3079     test_32bit_bitmap_blt();
3080     test_bitmapinfoheadersize();
3081     test_get16dibits();
3082     test_clipping();
3083 }