user32/tests: Fix some window test failures on various Windows platforms.
[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     char buf[512], buf_cmp[512];
76     DWORD gle;
77
78     ret = GetObject(hbm, sizeof(bm), &bm);
79     ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
80
81     ok(bm.bmType == 0 || broken(bm.bmType == 21072 /* Win9x */), "wrong bm.bmType %d\n", bm.bmType);
82     ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
83     ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
84     width_bytes = BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
85     ok(bm.bmWidthBytes == width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, width_bytes);
86     ok(bm.bmPlanes == bmih->biPlanes, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
87     ok(bm.bmBitsPixel == expected_depth, "wrong bm.bmBitsPixel %d != %d\n", bm.bmBitsPixel, expected_depth);
88     ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
89
90     assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
91     assert(sizeof(buf) == sizeof(buf_cmp));
92
93     SetLastError(0xdeadbeef);
94     ret = GetBitmapBits(hbm, 0, NULL);
95     gle=GetLastError();
96     ok(ret == bm.bmWidthBytes * bm.bmHeight || (ret == 0 && gle == ERROR_INVALID_PARAMETER /* Win9x */), "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
97
98     memset(buf_cmp, 0xAA, sizeof(buf_cmp));
99     memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
100
101     memset(buf, 0xAA, sizeof(buf));
102     ret = GetBitmapBits(hbm, sizeof(buf), buf);
103     ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
104     ok(!memcmp(buf, buf_cmp, sizeof(buf)), "buffers do not match\n");
105
106     /* test various buffer sizes for GetObject */
107     ret = GetObject(hbm, sizeof(*bma) * 2, bma);
108     ok(ret == sizeof(*bma) || broken(ret == sizeof(*bma) * 2 /* Win9x */), "wrong size %d\n", ret);
109
110     ret = GetObject(hbm, sizeof(bm) / 2, &bm);
111     ok(ret == 0 || broken(ret == sizeof(bm) / 2 /* Win9x */), "%d != 0\n", ret);
112
113     ret = GetObject(hbm, 0, &bm);
114     ok(ret == 0, "%d != 0\n", ret);
115
116     ret = GetObject(hbm, 1, &bm);
117     ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
118
119     /* Don't trust Win9x not to try to write to NULL */
120     if (ret == 0)
121     {
122         ret = GetObject(hbm, 0, NULL);
123         ok(ret == sizeof(bm), "wrong size %d\n", ret);
124     }
125 }
126
127 static void test_createdibitmap(void)
128 {
129     HDC hdc, hdcmem;
130     BITMAPINFOHEADER bmih;
131     BITMAPINFO bm;
132     HBITMAP hbm, hbm_colour, hbm_old;
133     INT screen_depth;
134     DWORD pixel;
135
136     hdc = GetDC(0);
137     screen_depth = GetDeviceCaps(hdc, BITSPIXEL);
138     memset(&bmih, 0, sizeof(bmih));
139     bmih.biSize = sizeof(bmih);
140     bmih.biWidth = 10;
141     bmih.biHeight = 10;
142     bmih.biPlanes = 1;
143     bmih.biBitCount = 32;
144     bmih.biCompression = BI_RGB;
145  
146     /* First create an un-initialised bitmap.  The depth of the bitmap
147        should match that of the hdc and not that supplied in bmih.
148     */
149
150     /* First try 32 bits */
151     hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
152     ok(hbm != NULL, "CreateDIBitmap failed\n");
153     test_bitmap_info(hbm, screen_depth, &bmih);
154     DeleteObject(hbm);
155     
156     /* Then 16 */
157     bmih.biBitCount = 16;
158     hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
159     ok(hbm != NULL, "CreateDIBitmap failed\n");
160     test_bitmap_info(hbm, screen_depth, &bmih);
161     DeleteObject(hbm);
162
163     /* Then 1 */
164     bmih.biBitCount = 1;
165     hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
166     ok(hbm != NULL, "CreateDIBitmap failed\n");
167     test_bitmap_info(hbm, screen_depth, &bmih);
168     DeleteObject(hbm);
169
170     /* Now with a monochrome dc we expect a monochrome bitmap */
171     hdcmem = CreateCompatibleDC(hdc);
172
173     /* First try 32 bits */
174     bmih.biBitCount = 32;
175     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
176     ok(hbm != NULL, "CreateDIBitmap failed\n");
177     test_bitmap_info(hbm, 1, &bmih);
178     DeleteObject(hbm);
179     
180     /* Then 16 */
181     bmih.biBitCount = 16;
182     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
183     ok(hbm != NULL, "CreateDIBitmap failed\n");
184     test_bitmap_info(hbm, 1, &bmih);
185     DeleteObject(hbm);
186     
187     /* Then 1 */
188     bmih.biBitCount = 1;
189     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
190     ok(hbm != NULL, "CreateDIBitmap failed\n");
191     test_bitmap_info(hbm, 1, &bmih);
192     DeleteObject(hbm);
193
194     /* Now select a polychrome bitmap into the dc and we expect
195        screen_depth bitmaps again */
196     hbm_colour = CreateCompatibleBitmap(hdc, bmih.biWidth, bmih.biHeight);
197     test_bitmap_info(hbm_colour, screen_depth, &bmih);
198     hbm_old = SelectObject(hdcmem, hbm_colour);
199
200     /* First try 32 bits */
201     bmih.biBitCount = 32;
202     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
203     ok(hbm != NULL, "CreateDIBitmap failed\n");
204     test_bitmap_info(hbm, screen_depth, &bmih);
205     DeleteObject(hbm);
206     
207     /* Then 16 */
208     bmih.biBitCount = 16;
209     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
210     ok(hbm != NULL, "CreateDIBitmap failed\n");
211     test_bitmap_info(hbm, screen_depth, &bmih);
212     DeleteObject(hbm);
213     
214     /* Then 1 */
215     bmih.biBitCount = 1;
216     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
217     ok(hbm != NULL, "CreateDIBitmap failed\n");
218     test_bitmap_info(hbm, screen_depth, &bmih);
219     DeleteObject(hbm);
220
221     SelectObject(hdcmem, hbm_old);
222     DeleteObject(hbm_colour);
223     DeleteDC(hdcmem);
224
225     /* If hdc == 0 then we get a 1 bpp bitmap */
226     if (!is_win9x) {
227         bmih.biBitCount = 32;
228         hbm = CreateDIBitmap(0, &bmih, 0, NULL, NULL, 0);
229         ok(hbm != NULL, "CreateDIBitmap failed\n");
230         test_bitmap_info(hbm, 1, &bmih);
231         DeleteObject(hbm);
232     }
233
234     /* Test how formats are converted */
235     pixel = 0xffffffff;
236     bmih.biBitCount = 1;
237     bmih.biWidth = 1;
238     bmih.biHeight = 1;
239
240     memset(&bm, 0, sizeof(bm));
241     bm.bmiHeader.biSize = sizeof(bm.bmiHeader);
242     bm.bmiHeader.biWidth = 1;
243     bm.bmiHeader.biHeight = 1;
244     bm.bmiHeader.biPlanes = 1;
245     bm.bmiHeader.biBitCount= 24;
246     bm.bmiHeader.biCompression= BI_RGB;
247     bm.bmiHeader.biSizeImage = 0;
248     hbm = CreateDIBitmap(hdc, &bmih, CBM_INIT, &pixel, &bm, DIB_RGB_COLORS);
249     ok(hbm != NULL, "CreateDIBitmap failed\n");
250
251     pixel = 0xdeadbeef;
252     bm.bmiHeader.biBitCount= 32;
253     GetDIBits(hdc, hbm, 0, 1, &pixel, &bm, DIB_RGB_COLORS);
254     ok(pixel == 0x00ffffff, "Reading a 32 bit pixel from a DDB returned %08x\n", pixel);
255     DeleteObject(hbm);
256
257     ReleaseDC(0, hdc);
258 }
259
260 static INT DIB_GetWidthBytes( int width, int bpp )
261 {
262     int words;
263
264     switch (bpp)
265     {
266         case 1:  words = (width + 31) / 32; break;
267         case 4:  words = (width + 7) / 8; break;
268         case 8:  words = (width + 3) / 4; break;
269         case 15:
270         case 16: words = (width + 1) / 2; break;
271         case 24: words = (width * 3 + 3)/4; break;
272         case 32: words = width; break;
273
274         default:
275             words=0;
276             trace("Unknown depth %d, please report.\n", bpp );
277             assert(0);
278             break;
279     }
280     return 4 * words;
281 }
282
283 static void test_dib_info(HBITMAP hbm, const void *bits, const BITMAPINFOHEADER *bmih)
284 {
285     BITMAP bm;
286     BITMAP bma[2];
287     DIBSECTION ds;
288     DIBSECTION dsa[2];
289     INT ret, bm_width_bytes, dib_width_bytes;
290     BYTE *buf;
291
292     ret = GetObject(hbm, sizeof(bm), &bm);
293     ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
294
295     ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
296     ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
297     ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
298     dib_width_bytes = DIB_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
299     bm_width_bytes = BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
300     if (bm.bmWidthBytes != dib_width_bytes) /* Win2k bug */
301         ok(bm.bmWidthBytes == bm_width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, bm_width_bytes);
302     else
303         ok(bm.bmWidthBytes == dib_width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, dib_width_bytes);
304     ok(bm.bmPlanes == bmih->biPlanes, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
305     ok(bm.bmBitsPixel == bmih->biBitCount, "bm.bmBitsPixel %d != %d\n", bm.bmBitsPixel, bmih->biBitCount);
306     ok(bm.bmBits == bits, "wrong bm.bmBits %p != %p\n", bm.bmBits, bits);
307
308     buf = HeapAlloc(GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight + 4096);
309
310     /* GetBitmapBits returns not 32-bit aligned data */
311     SetLastError(0xdeadbeef);
312     ret = GetBitmapBits(hbm, 0, NULL);
313     ok(ret == bm_width_bytes * bm.bmHeight ||
314         broken(ret == 0 && GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
315         "%d != %d\n", ret, bm_width_bytes * bm.bmHeight);
316
317     memset(buf, 0xAA, bm.bmWidthBytes * bm.bmHeight + 4096);
318     ret = GetBitmapBits(hbm, bm.bmWidthBytes * bm.bmHeight + 4096, buf);
319     ok(ret == bm_width_bytes * bm.bmHeight, "%d != %d\n", ret, bm_width_bytes * bm.bmHeight);
320
321     HeapFree(GetProcessHeap(), 0, buf);
322
323     /* test various buffer sizes for GetObject */
324     memset(&ds, 0xAA, sizeof(ds));
325     ret = GetObject(hbm, sizeof(*bma) * 2, bma);
326     ok(ret == sizeof(*bma) || broken(ret == sizeof(*bma) * 2 /* Win9x */), "wrong size %d\n", ret);
327     ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
328     ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
329     ok(bm.bmBits == bits, "wrong bm.bmBits %p != %p\n", bm.bmBits, bits);
330
331     ret = GetObject(hbm, sizeof(bm) / 2, &bm);
332     ok(ret == 0 || broken(ret == sizeof(bm) / 2 /* Win9x */), "%d != 0\n", ret);
333
334     ret = GetObject(hbm, 0, &bm);
335     ok(ret == 0, "%d != 0\n", ret);
336
337     ret = GetObject(hbm, 1, &bm);
338     ok(ret == 0 || broken(ret ==  1 /* Win9x */), "%d != 0\n", ret);
339
340     /* test various buffer sizes for GetObject */
341     ret = GetObject(hbm, 0, NULL);
342     ok(ret == sizeof(bm) || broken(ret == sizeof(DIBSECTION) /* Win9x */), "wrong size %d\n", ret);
343
344     ret = GetObject(hbm, sizeof(*dsa) * 2, dsa);
345     ok(ret == sizeof(*dsa) || broken(ret == sizeof(*dsa) * 2 /* Win9x */), "wrong size %d\n", ret);
346
347     memset(&ds, 0xAA, sizeof(ds));
348     ret = GetObject(hbm, sizeof(ds), &ds);
349     ok(ret == sizeof(ds), "wrong size %d\n", ret);
350
351     ok(ds.dsBm.bmBits == bits, "wrong bm.bmBits %p != %p\n", ds.dsBm.bmBits, bits);
352     if (ds.dsBm.bmWidthBytes != bm_width_bytes) /* Win2k bug */
353         ok(ds.dsBmih.biSizeImage == ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight, "%u != %u\n",
354            ds.dsBmih.biSizeImage, ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight);
355     ok(bmih->biSizeImage == 0, "%u != 0\n", bmih->biSizeImage);
356     ds.dsBmih.biSizeImage = 0;
357
358     ok(ds.dsBmih.biSize == bmih->biSize, "%u != %u\n", ds.dsBmih.biSize, bmih->biSize);
359     ok(ds.dsBmih.biWidth == bmih->biWidth, "%u != %u\n", ds.dsBmih.biWidth, bmih->biWidth);
360     ok(ds.dsBmih.biHeight == bmih->biHeight, "%u != %u\n", ds.dsBmih.biHeight, bmih->biHeight);
361     ok(ds.dsBmih.biPlanes == bmih->biPlanes, "%u != %u\n", ds.dsBmih.biPlanes, bmih->biPlanes);
362     ok(ds.dsBmih.biBitCount == bmih->biBitCount, "%u != %u\n", ds.dsBmih.biBitCount, bmih->biBitCount);
363     ok(ds.dsBmih.biCompression == bmih->biCompression, "%u != %u\n", ds.dsBmih.biCompression, bmih->biCompression);
364     ok(ds.dsBmih.biSizeImage == bmih->biSizeImage, "%u != %u\n", ds.dsBmih.biSizeImage, bmih->biSizeImage);
365     ok(ds.dsBmih.biXPelsPerMeter == bmih->biXPelsPerMeter, "%u != %u\n", ds.dsBmih.biXPelsPerMeter, bmih->biXPelsPerMeter);
366     ok(ds.dsBmih.biYPelsPerMeter == bmih->biYPelsPerMeter, "%u != %u\n", ds.dsBmih.biYPelsPerMeter, bmih->biYPelsPerMeter);
367
368     memset(&ds, 0xAA, sizeof(ds));
369     ret = GetObject(hbm, sizeof(ds) - 4, &ds);
370     ok(ret == sizeof(ds.dsBm) || broken(ret == (sizeof(ds) - 4) /* Win9x */), "wrong size %d\n", ret);
371     ok(ds.dsBm.bmWidth == bmih->biWidth, "%u != %u\n", ds.dsBmih.biWidth, bmih->biWidth);
372     ok(ds.dsBm.bmHeight == bmih->biHeight, "%u != %u\n", ds.dsBmih.biHeight, bmih->biHeight);
373     ok(ds.dsBm.bmBits == bits, "%p != %p\n", ds.dsBm.bmBits, bits);
374
375     ret = GetObject(hbm, 0, &ds);
376     ok(ret == 0, "%d != 0\n", ret);
377
378     ret = GetObject(hbm, 1, &ds);
379     ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
380 }
381
382 #define test_color_todo(got, exp, txt, todo) \
383     if (!todo && got != exp && screen_depth < 24) { \
384       todo_wine ok(0, #txt " failed at %d-bit screen depth: got 0x%06x expected 0x%06x - skipping DIB tests\n", \
385                    screen_depth, (UINT)got, (UINT)exp); \
386       return; \
387     } else if (todo) todo_wine { ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp); } \
388     else ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp) \
389
390 #define test_color(hdc, color, exp, todo_setp, todo_getp) \
391 { \
392     COLORREF c; \
393     c = SetPixel(hdc, 0, 0, color); \
394     if (!is_win9x) { test_color_todo(c, exp, SetPixel, todo_setp); } \
395     c = GetPixel(hdc, 0, 0); \
396     test_color_todo(c, exp, GetPixel, todo_getp); \
397 }
398
399 static void test_dib_bits_access( HBITMAP hdib, void *bits )
400 {
401     MEMORY_BASIC_INFORMATION info;
402     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
403     DWORD data[256];
404     BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
405     HDC hdc = GetDC(0);
406     char filename[MAX_PATH];
407     HANDLE file;
408     DWORD written;
409     INT ret;
410
411     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
412         "VirtualQuery failed\n");
413     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
414     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
415     ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
416     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
417     ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
418     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
419
420     memset( pbmi, 0, sizeof(bmibuf) );
421     memset( data, 0xcc, sizeof(data) );
422     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
423     pbmi->bmiHeader.biHeight = 16;
424     pbmi->bmiHeader.biWidth = 16;
425     pbmi->bmiHeader.biBitCount = 32;
426     pbmi->bmiHeader.biPlanes = 1;
427     pbmi->bmiHeader.biCompression = BI_RGB;
428
429     ret = SetDIBits( hdc, hdib, 0, 16, data, pbmi, DIB_RGB_COLORS );
430     ok( ret == 16, "SetDIBits failed\n" );
431
432     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
433         "VirtualQuery failed\n");
434     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
435     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
436     ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
437     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
438     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
439     /* it has been protected now */
440     todo_wine ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
441
442     /* try writing protected bits to a file */
443
444     GetTempFileNameA( ".", "dib", 0, filename );
445     file = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
446                         CREATE_ALWAYS, 0, 0 );
447     ok( file != INVALID_HANDLE_VALUE, "failed to open %s error %u\n", filename, GetLastError() );
448     ret = WriteFile( file, bits, 8192, &written, NULL );
449     ok( ret, "WriteFile failed error %u\n", GetLastError() );
450     if (ret) ok( written == 8192, "only wrote %u bytes\n", written );
451     CloseHandle( file );
452     DeleteFileA( filename );
453 }
454
455 static void test_dibsections(void)
456 {
457     HDC hdc, hdcmem, hdcmem2;
458     HBITMAP hdib, oldbm, hdib2, oldbm2;
459     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
460     char bcibuf[sizeof(BITMAPCOREINFO) + 256 * sizeof(RGBTRIPLE)];
461     BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
462     BITMAPCOREINFO *pbci = (BITMAPCOREINFO *)bcibuf;
463     HBITMAP hcoredib;
464     char coreBits[256];
465     BYTE *bits;
466     RGBQUAD rgb[256];
467     int ret;
468     char logpalbuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
469     LOGPALETTE *plogpal = (LOGPALETTE*)logpalbuf;
470     WORD *index;
471     DWORD *bits32;
472     HPALETTE hpal, oldpal;
473     DIBSECTION dibsec;
474     COLORREF c0, c1;
475     int i;
476     int screen_depth;
477     MEMORY_BASIC_INFORMATION info;
478
479     hdc = GetDC(0);
480     screen_depth = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
481
482     memset(pbmi, 0, sizeof(bmibuf));
483     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
484     pbmi->bmiHeader.biHeight = 100;
485     pbmi->bmiHeader.biWidth = 512;
486     pbmi->bmiHeader.biBitCount = 24;
487     pbmi->bmiHeader.biPlanes = 1;
488     pbmi->bmiHeader.biCompression = BI_RGB;
489
490     SetLastError(0xdeadbeef);
491
492     /* invalid pointer for BITMAPINFO
493        (*bits should be NULL on error) */
494     bits = (BYTE*)0xdeadbeef;
495     hdib = CreateDIBSection(hdc, NULL, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
496     ok(hdib == NULL && bits == NULL, "CreateDIBSection failed for invalid parameter: bmi == 0x0\n");
497
498     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
499     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
500     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
501     ok(dibsec.dsBm.bmBits == bits, "dibsec.dsBits %p != bits %p\n", dibsec.dsBm.bmBits, bits);
502
503     /* test the DIB memory */
504     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
505         "VirtualQuery failed\n");
506     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
507     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
508     ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
509     ok(info.RegionSize == 0x26000, "0x%lx != 0x26000\n", info.RegionSize);
510     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
511     ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
512     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
513
514     test_dib_bits_access( hdib, bits );
515
516     test_dib_info(hdib, bits, &pbmi->bmiHeader);
517     DeleteObject(hdib);
518
519     pbmi->bmiHeader.biBitCount = 8;
520     pbmi->bmiHeader.biCompression = BI_RLE8;
521     SetLastError(0xdeadbeef);
522     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
523     ok(hdib == NULL, "CreateDIBSection should fail when asked to create a compressed DIB section\n");
524     ok(GetLastError() == 0xdeadbeef, "wrong error %d\n", GetLastError());
525
526     pbmi->bmiHeader.biBitCount = 16;
527     pbmi->bmiHeader.biCompression = BI_BITFIELDS;
528     ((PDWORD)pbmi->bmiColors)[0] = 0xf800;
529     ((PDWORD)pbmi->bmiColors)[1] = 0x07e0;
530     ((PDWORD)pbmi->bmiColors)[2] = 0x001f;
531     SetLastError(0xdeadbeef);
532     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
533     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
534
535     /* test the DIB memory */
536     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
537         "VirtualQuery failed\n");
538     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
539     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
540     ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
541     ok(info.RegionSize == 0x19000, "0x%lx != 0x19000\n", info.RegionSize);
542     ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
543     ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
544     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
545
546     test_dib_info(hdib, bits, &pbmi->bmiHeader);
547     DeleteObject(hdib);
548
549     memset(pbmi, 0, sizeof(bmibuf));
550     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
551     pbmi->bmiHeader.biHeight = 16;
552     pbmi->bmiHeader.biWidth = 16;
553     pbmi->bmiHeader.biBitCount = 1;
554     pbmi->bmiHeader.biPlanes = 1;
555     pbmi->bmiHeader.biCompression = BI_RGB;
556     pbmi->bmiColors[0].rgbRed = 0xff;
557     pbmi->bmiColors[0].rgbGreen = 0;
558     pbmi->bmiColors[0].rgbBlue = 0;
559     pbmi->bmiColors[1].rgbRed = 0;
560     pbmi->bmiColors[1].rgbGreen = 0;
561     pbmi->bmiColors[1].rgbBlue = 0xff;
562
563     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
564     ok(hdib != NULL, "CreateDIBSection failed\n");
565     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
566     ok(dibsec.dsBmih.biClrUsed == 2,
567         "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
568
569     /* Test if the old BITMAPCOREINFO structure is supported */    
570         
571     pbci->bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
572     pbci->bmciHeader.bcBitCount = 0;
573
574     if (!is_win9x) {
575         ret = GetDIBits(hdc, hdib, 0, 16, NULL, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
576         ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
577         ok((pbci->bmciHeader.bcWidth == 16) && (pbci->bmciHeader.bcHeight == 16)
578             && (pbci->bmciHeader.bcBitCount == 1) && (pbci->bmciHeader.bcPlanes == 1),
579         "GetDIBits did't fill in the BITMAPCOREHEADER structure properly\n");
580
581         ret = GetDIBits(hdc, hdib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
582         ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
583         ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
584             (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
585             (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
586             "The color table has not been translated to the old BITMAPCOREINFO format\n");
587
588         hcoredib = CreateDIBSection(hdc, (BITMAPINFO*) pbci, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
589         ok(hcoredib != NULL, "CreateDIBSection failed with a BITMAPCOREINFO\n");
590
591         ZeroMemory(pbci->bmciColors, 256 * sizeof(RGBTRIPLE));
592         ret = GetDIBits(hdc, hcoredib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
593         ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
594         ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
595             (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
596             (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
597             "The color table has not been translated to the old BITMAPCOREINFO format\n");
598
599         DeleteObject(hcoredib);
600     }
601
602     hdcmem = CreateCompatibleDC(hdc);
603     oldbm = SelectObject(hdcmem, hdib);
604
605     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
606     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
607     ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
608        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
609        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
610        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
611
612     c0 = RGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen, pbmi->bmiColors[0].rgbBlue);
613     c1 = RGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen, pbmi->bmiColors[1].rgbBlue);
614
615     test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
616     test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
617     test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
618     test_color(hdcmem, PALETTEINDEX(0), c0, 1, 1);
619     test_color(hdcmem, PALETTEINDEX(1), c0, 1, 1);
620     test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
621     test_color(hdcmem, PALETTERGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen,
622         pbmi->bmiColors[0].rgbBlue), c0, 1, 1);
623     test_color(hdcmem, PALETTERGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen,
624         pbmi->bmiColors[1].rgbBlue), c1, 1, 1);
625     test_color(hdcmem, PALETTERGB(0, 0, 0), c0, 1, 1);
626     test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
627     test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c1, 1, 1);
628
629     SelectObject(hdcmem, oldbm);
630     DeleteObject(hdib);
631
632     pbmi->bmiColors[0].rgbRed = 0xff;
633     pbmi->bmiColors[0].rgbGreen = 0xff;
634     pbmi->bmiColors[0].rgbBlue = 0xff;
635     pbmi->bmiColors[1].rgbRed = 0;
636     pbmi->bmiColors[1].rgbGreen = 0;
637     pbmi->bmiColors[1].rgbBlue = 0;
638
639     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
640     ok(hdib != NULL, "CreateDIBSection failed\n");
641
642     test_dib_info(hdib, bits, &pbmi->bmiHeader);
643
644     oldbm = SelectObject(hdcmem, hdib);
645
646     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
647     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
648     ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
649        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
650        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
651        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
652
653     SelectObject(hdcmem, oldbm);
654     test_dib_info(hdib, bits, &pbmi->bmiHeader);
655     DeleteObject(hdib);
656
657     pbmi->bmiHeader.biBitCount = 4;
658     for (i = 0; i < 16; i++) {
659         pbmi->bmiColors[i].rgbRed = i;
660         pbmi->bmiColors[i].rgbGreen = 16-i;
661         pbmi->bmiColors[i].rgbBlue = 0;
662     }
663     hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
664     ok(hdib != NULL, "CreateDIBSection failed\n");
665     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
666     ok(dibsec.dsBmih.biClrUsed == 16,
667        "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 16);
668     test_dib_info(hdib, bits, &pbmi->bmiHeader);
669     DeleteObject(hdib);
670
671     pbmi->bmiHeader.biBitCount = 8;
672
673     for (i = 0; i < 128; i++) {
674         pbmi->bmiColors[i].rgbRed = 255 - i * 2;
675         pbmi->bmiColors[i].rgbGreen = i * 2;
676         pbmi->bmiColors[i].rgbBlue = 0;
677         pbmi->bmiColors[255 - i].rgbRed = 0;
678         pbmi->bmiColors[255 - i].rgbGreen = i * 2;
679         pbmi->bmiColors[255 - i].rgbBlue = 255 - i * 2;
680     }
681     hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
682     ok(hdib != NULL, "CreateDIBSection failed\n");
683     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
684     ok(dibsec.dsBmih.biClrUsed == 256,
685         "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
686
687     oldbm = SelectObject(hdcmem, hdib);
688
689     for (i = 0; i < 256; i++) {
690         test_color(hdcmem, DIBINDEX(i), 
691             RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
692         test_color(hdcmem, PALETTERGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 
693             RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
694     }
695
696     SelectObject(hdcmem, oldbm);
697     test_dib_info(hdib, bits, &pbmi->bmiHeader);
698     DeleteObject(hdib);
699
700     pbmi->bmiHeader.biBitCount = 1;
701
702     /* Now create a palette and a palette indexed dib section */
703     memset(plogpal, 0, sizeof(logpalbuf));
704     plogpal->palVersion = 0x300;
705     plogpal->palNumEntries = 2;
706     plogpal->palPalEntry[0].peRed = 0xff;
707     plogpal->palPalEntry[0].peBlue = 0xff;
708     plogpal->palPalEntry[1].peGreen = 0xff;
709
710     index = (WORD*)pbmi->bmiColors;
711     *index++ = 0;
712     *index = 1;
713     hpal = CreatePalette(plogpal);
714     ok(hpal != NULL, "CreatePalette failed\n");
715     oldpal = SelectPalette(hdc, hpal, TRUE);
716     hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_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 == 2,
720         "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
721
722     /* The colour table has already been grabbed from the dc, so we select back the
723        old palette */
724
725     SelectPalette(hdc, oldpal, TRUE);
726     oldbm = SelectObject(hdcmem, hdib);
727     oldpal = SelectPalette(hdcmem, hpal, TRUE);
728
729     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
730     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
731     ok(rgb[0].rgbRed == 0xff && rgb[0].rgbBlue == 0xff && rgb[0].rgbGreen == 0 &&
732        rgb[1].rgbRed == 0    && rgb[1].rgbBlue == 0    && rgb[1].rgbGreen == 0xff,
733        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
734        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
735        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
736
737     c0 = RGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen, plogpal->palPalEntry[0].peBlue);
738     c1 = RGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen, plogpal->palPalEntry[1].peBlue);
739
740     test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
741     test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
742     test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
743     test_color(hdcmem, PALETTEINDEX(0), c0, 0, 1);
744     test_color(hdcmem, PALETTEINDEX(1), c1, 0, 1);
745     test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
746     test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen,
747         plogpal->palPalEntry[0].peBlue), c0, 1, 1);
748     test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen,
749         plogpal->palPalEntry[1].peBlue), c1, 1, 1);
750     test_color(hdcmem, PALETTERGB(0, 0, 0), c1, 1, 1);
751     test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
752     test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c0, 1, 1);
753     test_color(hdcmem, PALETTERGB(0, 1, 0), c1, 1, 1);
754     test_color(hdcmem, PALETTERGB(0x3f, 0, 0x3f), c1, 1, 1);
755     test_color(hdcmem, PALETTERGB(0x40, 0, 0x40), c0, 1, 1);
756
757     /* Bottom and 2nd row from top green, everything else magenta */
758     bits[0] = bits[1] = 0xff;
759     bits[13 * 4] = bits[13*4 + 1] = 0xff;
760
761     test_dib_info(hdib, bits, &pbmi->bmiHeader);
762
763     pbmi->bmiHeader.biBitCount = 32;
764
765     hdib2 = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&bits32, NULL, 0);
766     ok(hdib2 != NULL, "CreateDIBSection failed\n");
767     hdcmem2 = CreateCompatibleDC(hdc);
768     oldbm2 = SelectObject(hdcmem2, hdib2);
769
770     BitBlt(hdcmem2, 0, 0, 16,16, hdcmem, 0, 0, SRCCOPY);
771
772     ok(bits32[0] == 0xff00, "lower left pixel is %08x\n", bits32[0]);
773     ok(bits32[17] == 0xff00ff, "bottom but one, left pixel is %08x\n", bits32[17]);
774
775     SelectObject(hdcmem2, oldbm2);
776     test_dib_info(hdib2, bits32, &pbmi->bmiHeader);
777     DeleteObject(hdib2);
778
779     SelectObject(hdcmem, oldbm);
780     SelectObject(hdcmem, oldpal);
781     DeleteObject(hdib);
782     DeleteObject(hpal);
783
784
785     pbmi->bmiHeader.biBitCount = 8;
786
787     memset(plogpal, 0, sizeof(logpalbuf));
788     plogpal->palVersion = 0x300;
789     plogpal->palNumEntries = 256;
790
791     for (i = 0; i < 128; i++) {
792         plogpal->palPalEntry[i].peRed = 255 - i * 2;
793         plogpal->palPalEntry[i].peBlue = i * 2;
794         plogpal->palPalEntry[i].peGreen = 0;
795         plogpal->palPalEntry[255 - i].peRed = 0;
796         plogpal->palPalEntry[255 - i].peGreen = i * 2;
797         plogpal->palPalEntry[255 - i].peBlue = 255 - i * 2;
798     }
799
800     index = (WORD*)pbmi->bmiColors;
801     for (i = 0; i < 256; i++) {
802         *index++ = i;
803     }
804
805     hpal = CreatePalette(plogpal);
806     ok(hpal != NULL, "CreatePalette failed\n");
807     oldpal = SelectPalette(hdc, hpal, TRUE);
808     hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
809     ok(hdib != NULL, "CreateDIBSection failed\n");
810     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
811     ok(dibsec.dsBmih.biClrUsed == 256,
812         "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
813
814     test_dib_info(hdib, bits, &pbmi->bmiHeader);
815
816     SelectPalette(hdc, oldpal, TRUE);
817     oldbm = SelectObject(hdcmem, hdib);
818     oldpal = SelectPalette(hdcmem, hpal, TRUE);
819
820     ret = GetDIBColorTable(hdcmem, 0, 256, rgb);
821     ok(ret == 256, "GetDIBColorTable returned %d\n", ret);
822     for (i = 0; i < 256; i++) {
823         ok(rgb[i].rgbRed == plogpal->palPalEntry[i].peRed && 
824             rgb[i].rgbBlue == plogpal->palPalEntry[i].peBlue && 
825             rgb[i].rgbGreen == plogpal->palPalEntry[i].peGreen, 
826             "GetDIBColorTable returns table %d: r%02x g%02x b%02x res%02x\n",
827             i, rgb[i].rgbRed, rgb[i].rgbGreen, rgb[i].rgbBlue, rgb[i].rgbReserved);
828     }
829
830     for (i = 0; i < 256; i++) {
831         test_color(hdcmem, DIBINDEX(i), 
832             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
833         test_color(hdcmem, PALETTEINDEX(i), 
834             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
835         test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 
836             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
837     }
838
839     SelectPalette(hdcmem, oldpal, TRUE);
840     SelectObject(hdcmem, oldbm);
841     DeleteObject(hdib);
842     DeleteObject(hpal);
843
844
845     DeleteDC(hdcmem);
846     ReleaseDC(0, hdc);
847 }
848
849 static void test_mono_dibsection(void)
850 {
851     HDC hdc, memdc;
852     HBITMAP old_bm, mono_ds;
853     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
854     BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
855     BYTE bits[10 * 4];
856     BYTE *ds_bits;
857     int num;
858
859     hdc = GetDC(0);
860
861     memdc = CreateCompatibleDC(hdc);
862
863     memset(pbmi, 0, sizeof(bmibuf));
864     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
865     pbmi->bmiHeader.biHeight = 10;
866     pbmi->bmiHeader.biWidth = 10;
867     pbmi->bmiHeader.biBitCount = 1;
868     pbmi->bmiHeader.biPlanes = 1;
869     pbmi->bmiHeader.biCompression = BI_RGB;
870     pbmi->bmiColors[0].rgbRed = 0xff;
871     pbmi->bmiColors[0].rgbGreen = 0xff;
872     pbmi->bmiColors[0].rgbBlue = 0xff;
873     pbmi->bmiColors[1].rgbRed = 0x0;
874     pbmi->bmiColors[1].rgbGreen = 0x0;
875     pbmi->bmiColors[1].rgbBlue = 0x0;
876
877     /*
878      * First dib section is 'inverted' ie color[0] is white, color[1] is black
879      */
880
881     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
882     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
883     old_bm = SelectObject(memdc, mono_ds);
884
885     /* black border, white interior */
886     Rectangle(memdc, 0, 0, 10, 10);
887     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
888     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
889
890     /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
891
892     memset(bits, 0, sizeof(bits));
893     bits[0] = 0xaa;
894
895     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
896     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
897
898     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
899
900     pbmi->bmiColors[0].rgbRed = 0x0;
901     pbmi->bmiColors[0].rgbGreen = 0x0;
902     pbmi->bmiColors[0].rgbBlue = 0x0;
903     pbmi->bmiColors[1].rgbRed = 0xff;
904     pbmi->bmiColors[1].rgbGreen = 0xff;
905     pbmi->bmiColors[1].rgbBlue = 0xff;
906
907     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
908     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
909
910     SelectObject(memdc, old_bm);
911     DeleteObject(mono_ds);
912
913     /*
914      * Next dib section is 'normal' ie color[0] is black, color[1] is white
915      */
916
917     pbmi->bmiColors[0].rgbRed = 0x0;
918     pbmi->bmiColors[0].rgbGreen = 0x0;
919     pbmi->bmiColors[0].rgbBlue = 0x0;
920     pbmi->bmiColors[1].rgbRed = 0xff;
921     pbmi->bmiColors[1].rgbGreen = 0xff;
922     pbmi->bmiColors[1].rgbBlue = 0xff;
923
924     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
925     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
926     old_bm = SelectObject(memdc, mono_ds);
927
928     /* black border, white interior */
929     Rectangle(memdc, 0, 0, 10, 10);
930     ok(ds_bits[0] == 0x00, "out_bits %02x\n", ds_bits[0]);
931     ok(ds_bits[4] == 0x7f, "out_bits %02x\n", ds_bits[4]);
932
933     /* SetDIBitsToDevice with a normal bmi -> normal dib section */
934
935     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
936     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
937
938     /* SetDIBitsToDevice with a inverted bmi -> normal dib section */
939
940     pbmi->bmiColors[0].rgbRed = 0xff;
941     pbmi->bmiColors[0].rgbGreen = 0xff;
942     pbmi->bmiColors[0].rgbBlue = 0xff;
943     pbmi->bmiColors[1].rgbRed = 0x0;
944     pbmi->bmiColors[1].rgbGreen = 0x0;
945     pbmi->bmiColors[1].rgbBlue = 0x0;
946
947     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
948     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
949
950     /*
951      * Take that 'normal' dibsection and change its colour table to an 'inverted' one
952      */
953
954     pbmi->bmiColors[0].rgbRed = 0xff;
955     pbmi->bmiColors[0].rgbGreen = 0xff;
956     pbmi->bmiColors[0].rgbBlue = 0xff;
957     pbmi->bmiColors[1].rgbRed = 0x0;
958     pbmi->bmiColors[1].rgbGreen = 0x0;
959     pbmi->bmiColors[1].rgbBlue = 0x0;
960     num = SetDIBColorTable(memdc, 0, 2, pbmi->bmiColors);
961     ok(num == 2, "num = %d\n", num);
962
963     /* black border, white interior */
964     Rectangle(memdc, 0, 0, 10, 10);
965 todo_wine {
966     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
967     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
968  }
969     /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
970
971     memset(bits, 0, sizeof(bits));
972     bits[0] = 0xaa;
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 normal bmi -> inverted dib section */
978
979     pbmi->bmiColors[0].rgbRed = 0x0;
980     pbmi->bmiColors[0].rgbGreen = 0x0;
981     pbmi->bmiColors[0].rgbBlue = 0x0;
982     pbmi->bmiColors[1].rgbRed = 0xff;
983     pbmi->bmiColors[1].rgbGreen = 0xff;
984     pbmi->bmiColors[1].rgbBlue = 0xff;
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     SelectObject(memdc, old_bm);
990     DeleteObject(mono_ds);
991
992     /*
993      * Now a dib section with a strange colour map just for fun.  This behaves just like an inverted one.
994      */
995  
996     pbmi->bmiColors[0].rgbRed = 0xff;
997     pbmi->bmiColors[0].rgbGreen = 0x0;
998     pbmi->bmiColors[0].rgbBlue = 0x0;
999     pbmi->bmiColors[1].rgbRed = 0xfe;
1000     pbmi->bmiColors[1].rgbGreen = 0x0;
1001     pbmi->bmiColors[1].rgbBlue = 0x0;
1002
1003     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
1004     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
1005     old_bm = SelectObject(memdc, mono_ds);
1006
1007     /* black border, white interior */
1008     Rectangle(memdc, 0, 0, 10, 10);
1009     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
1010     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
1011
1012     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
1013
1014     pbmi->bmiColors[0].rgbRed = 0x0;
1015     pbmi->bmiColors[0].rgbGreen = 0x0;
1016     pbmi->bmiColors[0].rgbBlue = 0x0;
1017     pbmi->bmiColors[1].rgbRed = 0xff;
1018     pbmi->bmiColors[1].rgbGreen = 0xff;
1019     pbmi->bmiColors[1].rgbBlue = 0xff;
1020
1021     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1022     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1023
1024     /* SetDIBitsToDevice with a inverted bmi -> inverted dib section */
1025
1026     pbmi->bmiColors[0].rgbRed = 0xff;
1027     pbmi->bmiColors[0].rgbGreen = 0xff;
1028     pbmi->bmiColors[0].rgbBlue = 0xff;
1029     pbmi->bmiColors[1].rgbRed = 0x0;
1030     pbmi->bmiColors[1].rgbGreen = 0x0;
1031     pbmi->bmiColors[1].rgbBlue = 0x0;
1032
1033     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1034     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
1035
1036     SelectObject(memdc, old_bm);
1037     DeleteObject(mono_ds);
1038
1039     DeleteDC(memdc);
1040     ReleaseDC(0, hdc);
1041 }
1042
1043 static void test_bitmap(void)
1044 {
1045     char buf[256], buf_cmp[256];
1046     HBITMAP hbmp, hbmp_old;
1047     HDC hdc;
1048     BITMAP bm;
1049     BITMAP bma[2];
1050     INT ret;
1051
1052     hdc = CreateCompatibleDC(0);
1053     assert(hdc != 0);
1054
1055     SetLastError(0xdeadbeef);
1056     hbmp = CreateBitmap(0x7ffffff, 1, 1, 1, NULL);
1057     if (!hbmp)
1058     {
1059         ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1060            GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1061            "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1062     }
1063     else
1064         DeleteObject(hbmp);
1065
1066     SetLastError(0xdeadbeef);
1067     hbmp = CreateBitmap(0x7ffffff, 9, 1, 1, NULL);
1068     if (!hbmp)
1069     {
1070         ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1071            GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1072            "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1073     }
1074     else
1075         DeleteObject(hbmp);
1076
1077     SetLastError(0xdeadbeef);
1078     hbmp = CreateBitmap(0x7ffffff + 1, 1, 1, 1, NULL);
1079     ok(!hbmp || broken(hbmp != NULL /* Win9x */), "CreateBitmap should fail\n");
1080     if (!hbmp)
1081         ok(GetLastError() == ERROR_INVALID_PARAMETER,
1082            "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1083     else
1084         DeleteObject(hbmp);
1085
1086     hbmp = CreateBitmap(15, 15, 1, 1, NULL);
1087     assert(hbmp != NULL);
1088
1089     ret = GetObject(hbmp, sizeof(bm), &bm);
1090     ok(ret == sizeof(bm), "wrong size %d\n", ret);
1091
1092     ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1093     ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1094     ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1095     ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1096     ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1097     ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1098     ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1099
1100     assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
1101     assert(sizeof(buf) == sizeof(buf_cmp));
1102
1103     ret = GetBitmapBits(hbmp, 0, NULL);
1104     ok(ret == bm.bmWidthBytes * bm.bmHeight || broken(ret == 0 /* Win9x */),
1105         "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1106
1107     memset(buf_cmp, 0xAA, sizeof(buf_cmp));
1108     memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
1109
1110     memset(buf, 0xAA, sizeof(buf));
1111     ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1112     ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1113     ok(!memcmp(buf, buf_cmp, sizeof(buf)), "buffers do not match\n");
1114
1115     hbmp_old = SelectObject(hdc, hbmp);
1116
1117     ret = GetObject(hbmp, sizeof(bm), &bm);
1118     ok(ret == sizeof(bm), "wrong size %d\n", ret);
1119
1120     ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1121     ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1122     ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1123     ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1124     ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1125     ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1126     ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1127
1128     memset(buf, 0xAA, sizeof(buf));
1129     ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1130     ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1131     ok(!memcmp(buf, buf_cmp, sizeof(buf)), "buffers do not match\n");
1132
1133     hbmp_old = SelectObject(hdc, hbmp_old);
1134     ok(hbmp_old == hbmp, "wrong old bitmap %p\n", hbmp_old);
1135
1136     /* test various buffer sizes for GetObject */
1137     ret = GetObject(hbmp, sizeof(*bma) * 2, bma);
1138     ok(ret == sizeof(*bma) || broken(ret == sizeof(*bma) * 2 /* Win9x */), "wrong size %d\n", ret);
1139
1140     ret = GetObject(hbmp, sizeof(bm) / 2, &bm);
1141     ok(ret == 0 || broken(ret == sizeof(bm) / 2 /* Win9x */), "%d != 0\n", ret);
1142
1143     ret = GetObject(hbmp, 0, &bm);
1144     ok(ret == 0, "%d != 0\n", ret);
1145
1146     ret = GetObject(hbmp, 1, &bm);
1147     ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
1148
1149     DeleteObject(hbmp);
1150     DeleteDC(hdc);
1151 }
1152
1153 static void test_bmBits(void)
1154 {
1155     BYTE bits[4];
1156     HBITMAP hbmp;
1157     BITMAP bmp;
1158
1159     memset(bits, 0, sizeof(bits));
1160     hbmp = CreateBitmap(2, 2, 1, 4, bits);
1161     ok(hbmp != NULL, "CreateBitmap failed\n");
1162
1163     memset(&bmp, 0xFF, sizeof(bmp));
1164     ok(GetObject(hbmp, sizeof(bmp), &bmp) == sizeof(bmp),
1165        "GetObject failed or returned a wrong structure size\n");
1166     ok(!bmp.bmBits, "bmBits must be NULL for device-dependent bitmaps\n");
1167
1168     DeleteObject(hbmp);
1169 }
1170
1171 static void test_GetDIBits_selected_DIB(UINT bpp)
1172 {
1173     HBITMAP dib;
1174     BITMAPINFO * info;
1175     BITMAPINFO * info2;
1176     void * bits;
1177     void * bits2;
1178     UINT dib_size;
1179     HDC dib_dc, dc;
1180     HBITMAP old_bmp;
1181     BOOL equalContents;
1182     UINT i;
1183     int res;
1184
1185     /* Create a DIB section with a color table */
1186
1187     info  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1188     info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1189     assert(info);
1190     assert(info2);
1191
1192     info->bmiHeader.biSize = sizeof(info->bmiHeader);
1193
1194     /* Choose width and height such that the row length (in bytes)
1195        is a multiple of 4 (makes things easier) */
1196     info->bmiHeader.biWidth = 32;
1197     info->bmiHeader.biHeight = 32;
1198     info->bmiHeader.biPlanes = 1;
1199     info->bmiHeader.biBitCount = bpp;
1200     info->bmiHeader.biCompression = BI_RGB;
1201
1202     for (i=0; i < (1u << bpp); i++)
1203     {
1204         BYTE c = i * (1 << (8 - bpp));
1205         info->bmiColors[i].rgbRed = c;
1206         info->bmiColors[i].rgbGreen = c;
1207         info->bmiColors[i].rgbBlue = c;
1208         info->bmiColors[i].rgbReserved = 0;
1209     }
1210
1211     dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
1212     assert(dib);
1213     dib_size = bpp * (info->bmiHeader.biWidth * info->bmiHeader.biHeight) / 8;
1214
1215     /* Set the bits of the DIB section */
1216     for (i=0; i < dib_size; i++)
1217     {
1218         ((BYTE *)bits)[i] = i % 256;
1219     }
1220
1221     /* Select the DIB into a DC */
1222     dib_dc = CreateCompatibleDC(NULL);
1223     old_bmp = SelectObject(dib_dc, dib);
1224     dc = CreateCompatibleDC(NULL);
1225     bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dib_size);
1226     assert(bits2);
1227
1228     /* Copy the DIB attributes but not the color table */
1229     memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1230
1231     res = GetDIBits(dc, dib, 0, info->bmiHeader.biHeight, bits2, info2, DIB_RGB_COLORS);
1232     ok(res, "GetDIBits failed\n");
1233
1234     /* Compare the color table and the bits */
1235     equalContents = TRUE;
1236     for (i=0; i < (1u << bpp); i++)
1237     {
1238         if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1239             || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1240             || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1241             || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1242         {
1243             equalContents = FALSE;
1244             break;
1245         }
1246     }
1247     ok(equalContents, "GetDIBits with DIB selected in DC: Invalid DIB color table\n");
1248
1249     equalContents = TRUE;
1250     for (i=0; i < dib_size / sizeof(DWORD); i++)
1251     {
1252         if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1253         {
1254             equalContents = FALSE;
1255             break;
1256         }
1257     }
1258     ok(equalContents, "GetDIBits with %d bpp DIB selected in DC: Invalid DIB bits\n",bpp);
1259
1260     HeapFree(GetProcessHeap(), 0, bits2);
1261     DeleteDC(dc);
1262
1263     SelectObject(dib_dc, old_bmp);
1264     DeleteDC(dib_dc);
1265     DeleteObject(dib);
1266
1267     HeapFree(GetProcessHeap(), 0, info2);
1268     HeapFree(GetProcessHeap(), 0, info);
1269 }
1270
1271 static void test_GetDIBits_selected_DDB(BOOL monochrome)
1272 {
1273     HBITMAP ddb;
1274     BITMAPINFO * info;
1275     BITMAPINFO * info2;
1276     void * bits;
1277     void * bits2;
1278     HDC ddb_dc, dc;
1279     HBITMAP old_bmp;
1280     BOOL equalContents;
1281     UINT width, height;
1282     UINT bpp;
1283     UINT i, j;
1284     int res;
1285
1286     width = height = 16;
1287
1288     /* Create a DDB (device-dependent bitmap) */
1289     if (monochrome)
1290     {
1291         bpp = 1;
1292         ddb = CreateBitmap(width, height, 1, 1, NULL);
1293     }
1294     else
1295     {
1296         HDC screen_dc = GetDC(NULL);
1297         bpp = GetDeviceCaps(screen_dc, BITSPIXEL) * GetDeviceCaps(screen_dc, PLANES);
1298         ddb = CreateCompatibleBitmap(screen_dc, width, height);
1299         ReleaseDC(NULL, screen_dc);
1300     }
1301
1302     /* Set the pixels */
1303     ddb_dc = CreateCompatibleDC(NULL);
1304     old_bmp = SelectObject(ddb_dc, ddb);
1305     for (i = 0; i < width; i++)
1306     {
1307         for (j=0; j < height; j++)
1308         {
1309             BYTE c = (i * width + j) % 256;
1310             SetPixelV(ddb_dc, i, j, RGB(c, c, c));
1311         }
1312     }
1313     SelectObject(ddb_dc, old_bmp);
1314
1315     info  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1316     info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1317     assert(info);
1318     assert(info2);
1319
1320     info->bmiHeader.biSize = sizeof(info->bmiHeader);
1321     info->bmiHeader.biWidth = width;
1322     info->bmiHeader.biHeight = height;
1323     info->bmiHeader.biPlanes = 1;
1324     info->bmiHeader.biBitCount = bpp;
1325     info->bmiHeader.biCompression = BI_RGB;
1326
1327     dc = CreateCompatibleDC(NULL);
1328
1329     /* Fill in biSizeImage */
1330     GetDIBits(dc, ddb, 0, height, NULL, info, DIB_RGB_COLORS);
1331     ok(info->bmiHeader.biSizeImage != 0, "GetDIBits failed to get the DIB attributes\n");
1332
1333     bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1334     bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1335     assert(bits);
1336     assert(bits2);
1337
1338     /* Get the bits */
1339     res = GetDIBits(dc, ddb, 0, height, bits, info, DIB_RGB_COLORS);
1340     ok(res, "GetDIBits failed\n");
1341
1342     /* Copy the DIB attributes but not the color table */
1343     memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1344
1345     /* Select the DDB into another DC */
1346     old_bmp = SelectObject(ddb_dc, ddb);
1347
1348     /* Get the bits */
1349     res = GetDIBits(dc, ddb, 0, height, bits2, info2, DIB_RGB_COLORS);
1350     ok(res, "GetDIBits failed\n");
1351
1352     /* Compare the color table and the bits */
1353     if (bpp <= 8)
1354     {
1355         equalContents = TRUE;
1356         for (i=0; i < (1u << bpp); i++)
1357         {
1358             if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1359                 || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1360                 || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1361                 || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1362             {
1363                 equalContents = FALSE;
1364                 break;
1365             }
1366         }
1367         ok(equalContents, "GetDIBits with DDB selected in DC: Got a different color table\n");
1368     }
1369
1370     equalContents = TRUE;
1371     for (i=0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++)
1372     {
1373         if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1374         {
1375             equalContents = FALSE;
1376         }
1377     }
1378     ok(equalContents, "GetDIBits with DDB selected in DC: Got different DIB bits\n");
1379
1380     /* Test the palette */
1381     equalContents = TRUE;
1382     if (info2->bmiHeader.biBitCount <= 8)
1383     {
1384         WORD *colors = (WORD*)info2->bmiColors;
1385
1386         /* Get the palette indices */
1387         res = GetDIBits(dc, ddb, 0, 0, NULL, info2, DIB_PAL_COLORS);
1388         if (res == 0 && GetLastError() == ERROR_INVALID_PARAMETER) /* Win9x */
1389             res = GetDIBits(dc, ddb, 0, height, NULL, info2, DIB_PAL_COLORS);
1390         ok(res, "GetDIBits failed\n");
1391
1392         for (i=0;i < 1 << info->bmiHeader.biSizeImage; i++)
1393         {
1394             if (colors[i] != i)
1395             {
1396                 equalContents = FALSE;
1397                 break;
1398             }
1399         }
1400     }
1401
1402     ok(equalContents, "GetDIBits with DDB selected in DC: non 1:1 palette indices\n");
1403
1404     HeapFree(GetProcessHeap(), 0, bits2);
1405     HeapFree(GetProcessHeap(), 0, bits);
1406     DeleteDC(dc);
1407
1408     SelectObject(ddb_dc, old_bmp);
1409     DeleteDC(ddb_dc);
1410     DeleteObject(ddb);
1411
1412     HeapFree(GetProcessHeap(), 0, info2);
1413     HeapFree(GetProcessHeap(), 0, info);
1414 }
1415
1416 static void test_GetDIBits(void)
1417 {
1418     /* 2-bytes aligned 1-bit bitmap data: 16x16 */
1419     static const BYTE bmp_bits_1[16 * 2] =
1420     {
1421         0xff,0xff, 0,0, 0xff,0xff, 0,0,
1422         0xff,0xff, 0,0, 0xff,0xff, 0,0,
1423         0xff,0xff, 0,0, 0xff,0xff, 0,0,
1424         0xff,0xff, 0,0, 0xff,0xff, 0,0
1425     };
1426     /* 4-bytes aligned 1-bit DIB data: 16x16 */
1427     static const BYTE dib_bits_1[16 * 4] =
1428     {
1429         0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1430         0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1431         0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1432         0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0
1433     };
1434     /* 2-bytes aligned 24-bit bitmap data: 16x16 */
1435     static const BYTE bmp_bits_24[16 * 16*3] =
1436     {
1437         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1438         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1439         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1440         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1441         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1442         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1443         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1444         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1445         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1446         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1447         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1448         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1449         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1450         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1451         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1452         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1453         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1454         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1455         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1456         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1457         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1458         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1459         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1460         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1461         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1462         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1463         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1464         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1465         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1466         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1467         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1468         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1469     };
1470     /* 4-bytes aligned 24-bit DIB data: 16x16 */
1471     static const BYTE dib_bits_24[16 * 16*3] =
1472     {
1473         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1474         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1475         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1476         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1477         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1478         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1479         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1480         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1481         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1482         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1483         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1484         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1485         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1486         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1487         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1488         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1489         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1490         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1491         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1492         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1493         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1494         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1495         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1496         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1497         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1498         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1499         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1500         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1501         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1502         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1503         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1504         0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
1505     };
1506     HBITMAP hbmp;
1507     BITMAP bm;
1508     HDC hdc;
1509     int i, bytes, lines;
1510     BYTE buf[1024];
1511     char bi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
1512     BITMAPINFO *bi = (BITMAPINFO *)bi_buf;
1513
1514     hdc = GetDC(0);
1515
1516     /* 1-bit source bitmap data */
1517     hbmp = CreateBitmap(16, 16, 1, 1, bmp_bits_1);
1518     ok(hbmp != 0, "CreateBitmap failed\n");
1519
1520     memset(&bm, 0xAA, sizeof(bm));
1521     bytes = GetObject(hbmp, sizeof(bm), &bm);
1522     ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1523     ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
1524     ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1525     ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1526     ok(bm.bmWidthBytes == 2, "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1527     ok(bm.bmPlanes == 1, "wrong bmPlanes %u\n", bm.bmPlanes);
1528     ok(bm.bmBitsPixel == 1, "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1529     ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1530
1531     bytes = GetBitmapBits(hbmp, 0, NULL);
1532     ok(bytes == sizeof(bmp_bits_1) || broken(bytes == 0 /* Win9x */), "expected 16*2 got %d bytes\n", bytes);
1533     bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1534     ok(bytes == sizeof(bmp_bits_1), "expected 16*2 got %d bytes\n", bytes);
1535     ok(!memcmp(buf, bmp_bits_1, sizeof(bmp_bits_1)), "bitmap bits don't match\n");
1536
1537     /* retrieve 1-bit DIB data */
1538     memset(bi, 0, sizeof(*bi));
1539     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1540     bi->bmiHeader.biWidth = bm.bmWidth;
1541     bi->bmiHeader.biHeight = bm.bmHeight;
1542     bi->bmiHeader.biPlanes = 1;
1543     bi->bmiHeader.biBitCount = 1;
1544     bi->bmiHeader.biCompression = BI_RGB;
1545     bi->bmiHeader.biSizeImage = 0;
1546     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1547     SetLastError(0xdeadbeef);
1548     lines = GetDIBits(0, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1549     ok(lines == 0, "GetDIBits copied %d lines with hdc = 0\n", lines);
1550     ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
1551     ok(bi->bmiHeader.biSizeImage == 0, "expected 0, got %u\n", bi->bmiHeader.biSizeImage);
1552
1553     memset(buf, 0xAA, sizeof(buf));
1554     SetLastError(0xdeadbeef);
1555     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1556     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1557        lines, bm.bmHeight, GetLastError());
1558     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1), "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1559
1560     /* the color table consists of black and white */
1561     ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1562        bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1563        "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1564        bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1565        bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1566     ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1567        bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1568        "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1569        bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1570        bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1571     for (i = 2; i < 256; i++)
1572     {
1573         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1574            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1575            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1576            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1577            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1578     }
1579
1580     /* returned bits are DWORD aligned and upside down */
1581 todo_wine
1582     ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)), "DIB bits don't match\n");
1583
1584     /* Test the palette indices */
1585     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1586     SetLastError(0xdeadbeef);
1587     lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1588
1589     ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1590     ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1591     for (i = 2; i < 256; i++)
1592         ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[1]);
1593
1594     /* retrieve 24-bit DIB data */
1595     memset(bi, 0, sizeof(*bi));
1596     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1597     bi->bmiHeader.biWidth = bm.bmWidth;
1598     bi->bmiHeader.biHeight = bm.bmHeight;
1599     bi->bmiHeader.biPlanes = 1;
1600     bi->bmiHeader.biBitCount = 24;
1601     bi->bmiHeader.biCompression = BI_RGB;
1602     bi->bmiHeader.biSizeImage = 0;
1603     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1604     memset(buf, 0xAA, sizeof(buf));
1605     SetLastError(0xdeadbeef);
1606     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1607     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1608        lines, bm.bmHeight, GetLastError());
1609     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24), "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1610
1611     /* the color table doesn't exist for 24-bit images */
1612     for (i = 0; i < 256; i++)
1613     {
1614         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1615            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1616            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1617            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1618            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1619     }
1620
1621     /* returned bits are DWORD aligned and upside down */
1622     ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1623     DeleteObject(hbmp);
1624
1625     /* 24-bit source bitmap data */
1626     hbmp = CreateCompatibleBitmap(hdc, 16, 16);
1627     ok(hbmp != 0, "CreateBitmap failed\n");
1628     SetLastError(0xdeadbeef);
1629     bi->bmiHeader.biHeight = -bm.bmHeight; /* indicate bottom-up data */
1630     lines = SetDIBits(hdc, hbmp, 0, bm.bmHeight, bmp_bits_24, bi, DIB_RGB_COLORS);
1631     ok(lines == bm.bmHeight, "SetDIBits copied %d lines of %d, error %u\n",
1632        lines, bm.bmHeight, GetLastError());
1633
1634     memset(&bm, 0xAA, sizeof(bm));
1635     bytes = GetObject(hbmp, sizeof(bm), &bm);
1636     ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1637     ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
1638     ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1639     ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1640     ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1641     ok(bm.bmPlanes == GetDeviceCaps(hdc, PLANES), "wrong bmPlanes %u\n", bm.bmPlanes);
1642     ok(bm.bmBitsPixel == GetDeviceCaps(hdc, BITSPIXEL), "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1643     ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1644
1645     bytes = GetBitmapBits(hbmp, 0, NULL);
1646     ok(bytes == bm.bmWidthBytes * bm.bmHeight, "expected %d got %d bytes\n",
1647        bm.bmWidthBytes * bm.bmHeight, bytes);
1648     bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1649     ok(bytes == bm.bmWidthBytes * bm.bmHeight, "expected %d got %d bytes\n",
1650        bm.bmWidthBytes * bm.bmHeight, bytes);
1651
1652     /* retrieve 1-bit DIB data */
1653     memset(bi, 0, sizeof(*bi));
1654     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1655     bi->bmiHeader.biWidth = bm.bmWidth;
1656     bi->bmiHeader.biHeight = bm.bmHeight;
1657     bi->bmiHeader.biPlanes = 1;
1658     bi->bmiHeader.biBitCount = 1;
1659     bi->bmiHeader.biCompression = BI_RGB;
1660     bi->bmiHeader.biSizeImage = 0;
1661     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1662     memset(buf, 0xAA, sizeof(buf));
1663     SetLastError(0xdeadbeef);
1664     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1665     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1666        lines, bm.bmHeight, GetLastError());
1667     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1), "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1668
1669     /* the color table consists of black and white */
1670     ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1671        bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1672        "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1673        bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1674        bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1675     ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1676        bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1677        "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1678        bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1679        bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1680     for (i = 2; i < 256; i++)
1681     {
1682         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1683            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1684            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1685            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1686            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1687     }
1688
1689     /* returned bits are DWORD aligned and upside down */
1690 todo_wine
1691     ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)), "DIB bits don't match\n");
1692
1693     /* Test the palette indices */
1694     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1695     SetLastError(0xdeadbeef);
1696     lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1697
1698     ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1699     ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1700     for (i = 2; i < 256; i++)
1701         ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[i]);
1702
1703     /* retrieve 24-bit DIB data */
1704     memset(bi, 0, sizeof(*bi));
1705     bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1706     bi->bmiHeader.biWidth = bm.bmWidth;
1707     bi->bmiHeader.biHeight = bm.bmHeight;
1708     bi->bmiHeader.biPlanes = 1;
1709     bi->bmiHeader.biBitCount = 24;
1710     bi->bmiHeader.biCompression = BI_RGB;
1711     bi->bmiHeader.biSizeImage = 0;
1712     memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1713     memset(buf, 0xAA, sizeof(buf));
1714     SetLastError(0xdeadbeef);
1715     lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1716     ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1717        lines, bm.bmHeight, GetLastError());
1718     ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24), "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1719
1720     /* the color table doesn't exist for 24-bit images */
1721     for (i = 0; i < 256; i++)
1722     {
1723         ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1724            bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1725            "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1726            bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1727            bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1728     }
1729
1730     /* returned bits are DWORD aligned and upside down */
1731     ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1732     DeleteObject(hbmp);
1733
1734     ReleaseDC(0, hdc);
1735 }
1736
1737 static void test_GetDIBits_BI_BITFIELDS(void)
1738 {
1739     /* Try a screen resolution detection technique
1740      * from the September 1999 issue of Windows Developer's Journal
1741      * which seems to be in widespread use.
1742      * http://www.lesher.ws/highcolor.html
1743      * http://www.lesher.ws/vidfmt.c
1744      * It hinges on being able to retrieve the bitmaps
1745      * for the three primary colors in non-paletted 16 bit mode.
1746      */
1747     char dibinfo_buf[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
1748     DWORD bits[32];
1749     LPBITMAPINFO dibinfo = (LPBITMAPINFO) dibinfo_buf;
1750     HDC hdc;
1751     HBITMAP hbm;
1752     int ret;
1753
1754     memset(dibinfo, 0, sizeof(dibinfo_buf));
1755     dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1756
1757     hdc = GetDC(NULL);
1758     ok(hdc != NULL, "GetDC failed?\n");
1759     hbm = CreateCompatibleBitmap(hdc, 1, 1);
1760     ok(hbm != NULL, "CreateCompatibleBitmap failed?\n");
1761
1762     /* Call GetDIBits to fill in bmiHeader.  */
1763     ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1764     ok(ret == 1, "GetDIBits failed\n");
1765     if (dibinfo->bmiHeader.biBitCount > 8)
1766     {
1767         DWORD *bitmasks = (DWORD *)dibinfo->bmiColors;
1768
1769         ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS,
1770             "compression is %u\n", dibinfo->bmiHeader.biCompression );
1771
1772         ok( !bitmasks[0], "red mask is set\n" );
1773         ok( !bitmasks[1], "green mask is set\n" );
1774         ok( !bitmasks[2], "blue mask is set\n" );
1775
1776         /* test with NULL bits pointer and correct bpp */
1777         dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1778         ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1779         ok(ret == 1, "GetDIBits failed\n");
1780
1781         ok( bitmasks[0] != 0, "red mask is not set\n" );
1782         ok( bitmasks[1] != 0, "green mask is not set\n" );
1783         ok( bitmasks[2] != 0, "blue mask is not set\n" );
1784         ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1785
1786         /* test with valid bits pointer */
1787         memset(dibinfo, 0, sizeof(dibinfo_buf));
1788         dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1789         ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1790         ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1791         dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1792         ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1793         ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1794
1795         ok( bitmasks[0] != 0, "red mask is not set\n" );
1796         ok( bitmasks[1] != 0, "green mask is not set\n" );
1797         ok( bitmasks[2] != 0, "blue mask is not set\n" );
1798         ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1799
1800         /* now with bits and 0 lines */
1801         memset(dibinfo, 0, sizeof(dibinfo_buf));
1802         dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1803         dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1804         SetLastError(0xdeadbeef);
1805         ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1806         if (ret == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
1807             win_skip("Win9x/WinMe doesn't handle 0 for the number of scan lines\n");
1808         else
1809         {
1810             ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1811
1812             ok( !bitmasks[0], "red mask is set\n" );
1813             ok( !bitmasks[1], "green mask is set\n" );
1814             ok( !bitmasks[2], "blue mask is set\n" );
1815             ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1816
1817             memset(bitmasks, 0, 3*sizeof(DWORD));
1818             dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1819             ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1820             ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1821
1822             ok( bitmasks[0] != 0, "red mask is not set\n" );
1823             ok( bitmasks[1] != 0, "green mask is not set\n" );
1824             ok( bitmasks[2] != 0, "blue mask is not set\n" );
1825             ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1826         }
1827     }
1828     else skip("not in 16 bpp BI_BITFIELDS mode, skipping that test\n");
1829
1830     DeleteObject(hbm);
1831     ReleaseDC(NULL, hdc);
1832 }
1833
1834 static void test_select_object(void)
1835 {
1836     HDC hdc;
1837     HBITMAP hbm, hbm_old;
1838     INT planes, bpp, i;
1839     DWORD depths[] = {8, 15, 16, 24, 32};
1840     BITMAP bm;
1841     DWORD bytes;
1842
1843     hdc = GetDC(0);
1844     ok(hdc != 0, "GetDC(0) failed\n");
1845     hbm = CreateCompatibleBitmap(hdc, 10, 10);
1846     ok(hbm != 0, "CreateCompatibleBitmap failed\n");
1847
1848     hbm_old = SelectObject(hdc, hbm);
1849     ok(hbm_old == 0, "SelectObject should fail\n");
1850
1851     DeleteObject(hbm);
1852     ReleaseDC(0, hdc);
1853
1854     hdc = CreateCompatibleDC(0);
1855     ok(hdc != 0, "GetDC(0) failed\n");
1856     hbm = CreateCompatibleBitmap(hdc, 10, 10);
1857     ok(hbm != 0, "CreateCompatibleBitmap failed\n");
1858
1859     hbm_old = SelectObject(hdc, hbm);
1860     ok(hbm_old != 0, "SelectObject failed\n");
1861     hbm_old = SelectObject(hdc, hbm_old);
1862     ok(hbm_old == hbm, "SelectObject failed\n");
1863
1864     DeleteObject(hbm);
1865
1866     /* test an 1-bpp bitmap */
1867     planes = GetDeviceCaps(hdc, PLANES);
1868     bpp = 1;
1869
1870     hbm = CreateBitmap(10, 10, planes, bpp, NULL);
1871     ok(hbm != 0, "CreateBitmap failed\n");
1872
1873     hbm_old = SelectObject(hdc, hbm);
1874     ok(hbm_old != 0, "SelectObject failed\n");
1875     hbm_old = SelectObject(hdc, hbm_old);
1876     ok(hbm_old == hbm, "SelectObject failed\n");
1877
1878     DeleteObject(hbm);
1879
1880     for(i = 0; i < sizeof(depths)/sizeof(depths[0]); i++) {
1881         /* test a color bitmap to dc bpp matching */
1882         planes = GetDeviceCaps(hdc, PLANES);
1883         bpp = GetDeviceCaps(hdc, BITSPIXEL);
1884
1885         hbm = CreateBitmap(10, 10, planes, depths[i], NULL);
1886         ok(hbm != 0, "CreateBitmap failed\n");
1887
1888         hbm_old = SelectObject(hdc, hbm);
1889         if(depths[i] == bpp ||
1890           (bpp == 16 && depths[i] == 15)        /* 16 and 15 bpp are compatible */
1891           ) {
1892             ok(hbm_old != 0, "SelectObject failed, BITSPIXEL: %d, created depth: %d\n", bpp, depths[i]);
1893             SelectObject(hdc, hbm_old);
1894         } else {
1895             ok(hbm_old == 0, "SelectObject should fail. BITSPIXELS: %d, created depth: %d\n", bpp, depths[i]);
1896         }
1897
1898         memset(&bm, 0xAA, sizeof(bm));
1899         bytes = GetObject(hbm, sizeof(bm), &bm);
1900         ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1901         ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
1902         ok(bm.bmWidth == 10, "wrong bmWidth %d\n", bm.bmWidth);
1903         ok(bm.bmHeight == 10, "wrong bmHeight %d\n", bm.bmHeight);
1904         ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1905         ok(bm.bmPlanes == planes, "wrong bmPlanes %u\n", bm.bmPlanes);
1906         if(depths[i] == 15) {
1907             ok(bm.bmBitsPixel == 16, "wrong bmBitsPixel %d(15 bpp special)\n", bm.bmBitsPixel);
1908         } else {
1909             ok(bm.bmBitsPixel == depths[i], "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1910         }
1911         ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1912
1913         DeleteObject(hbm);
1914     }
1915
1916     DeleteDC(hdc);
1917 }
1918
1919 static void test_mono_1x1_bmp_dbg(HBITMAP hbmp, int line)
1920 {
1921     INT ret;
1922     BITMAP bm;
1923
1924     ret = GetObjectType(hbmp);
1925     ok_(__FILE__, line)(ret == OBJ_BITMAP, "the object %p is not bitmap\n", hbmp);
1926
1927     ret = GetObject(hbmp, 0, 0);
1928     ok_(__FILE__, line)(ret == sizeof(BITMAP) /* XP */ ||
1929                         ret == sizeof(DIBSECTION) /* Win9x */, "object size %d\n", ret);
1930
1931     memset(&bm, 0xDA, sizeof(bm));
1932     SetLastError(0xdeadbeef);
1933     ret = GetObject(hbmp, sizeof(bm), &bm);
1934     if (!ret) /* XP, only for curObj2 */ return;
1935     ok_(__FILE__, line)(ret == sizeof(BITMAP) ||
1936                         ret == sizeof(DIBSECTION) /* Win9x, only for curObj2 */,
1937                         "GetObject returned %d, error %u\n", ret, GetLastError());
1938     ok_(__FILE__, line)(bm.bmType == 0, "wrong bmType, expected 0 got %d\n", bm.bmType);
1939     ok_(__FILE__, line)(bm.bmWidth == 1, "wrong bmWidth, expected 1 got %d\n", bm.bmWidth);
1940     ok_(__FILE__, line)(bm.bmHeight == 1, "wrong bmHeight, expected 1 got %d\n", bm.bmHeight);
1941     ok_(__FILE__, line)(bm.bmWidthBytes == 2, "wrong bmWidthBytes, expected 2 got %d\n", bm.bmWidthBytes);
1942     ok_(__FILE__, line)(bm.bmPlanes == 1, "wrong bmPlanes, expected 1 got %u\n", bm.bmPlanes);
1943     ok_(__FILE__, line)(bm.bmBitsPixel == 1, "wrong bmBitsPixel, expected 1 got %d\n", bm.bmBitsPixel);
1944     ok_(__FILE__, line)(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1945 }
1946
1947 #define test_mono_1x1_bmp(a) test_mono_1x1_bmp_dbg((a), __LINE__)
1948
1949 static void test_CreateBitmap(void)
1950 {
1951     BITMAP bmp;
1952     HDC screenDC = GetDC(0);
1953     HDC hdc = CreateCompatibleDC(screenDC);
1954     UINT i, expect = 0;
1955
1956     /* all of these are the stock monochrome bitmap */
1957     HBITMAP bm = CreateCompatibleBitmap(hdc, 0, 0);
1958     HBITMAP bm1 = CreateCompatibleBitmap(screenDC, 0, 0);
1959     HBITMAP bm4 = CreateBitmap(0, 1, 0, 0, 0);
1960     HBITMAP bm5 = CreateDiscardableBitmap(hdc, 0, 0);
1961     HBITMAP curObj1 = GetCurrentObject(hdc, OBJ_BITMAP);
1962     HBITMAP curObj2 = GetCurrentObject(screenDC, OBJ_BITMAP);
1963
1964     /* these 2 are not the stock monochrome bitmap */
1965     HBITMAP bm2 = CreateCompatibleBitmap(hdc, 1, 1);
1966     HBITMAP bm3 = CreateBitmap(1, 1, 1, 1, 0);
1967
1968     HBITMAP old1 = SelectObject(hdc, bm2);
1969     HBITMAP old2 = SelectObject(screenDC, bm3);
1970     SelectObject(hdc, old1);
1971     SelectObject(screenDC, old2);
1972
1973     ok(bm == bm1 && bm == bm4 && bm == bm5 && bm == curObj1 && bm == old1,
1974        "0: %p, 1: %p, 4: %p, 5: %p, curObj1 %p, old1 %p\n",
1975        bm, bm1, bm4, bm5, curObj1, old1);
1976     ok(bm != bm2 && bm != bm3, "0: %p, 2: %p, 3: %p\n", bm, bm2, bm3);
1977     ok(bm != curObj2 /* XP */ || bm == curObj2 /* Win9x */,
1978        "0: %p, curObj2 %p\n", bm, curObj2);
1979     ok(old2 == 0, "old2 %p\n", old2);
1980
1981     test_mono_1x1_bmp(bm);
1982     test_mono_1x1_bmp(bm1);
1983     test_mono_1x1_bmp(bm2);
1984     test_mono_1x1_bmp(bm3);
1985     test_mono_1x1_bmp(bm4);
1986     test_mono_1x1_bmp(bm5);
1987     test_mono_1x1_bmp(old1);
1988     test_mono_1x1_bmp(curObj1);
1989     test_mono_1x1_bmp(curObj2);
1990
1991     DeleteObject(bm);
1992     DeleteObject(bm1);
1993     DeleteObject(bm2);
1994     DeleteObject(bm3);
1995     DeleteObject(bm4);
1996     DeleteObject(bm5);
1997
1998     DeleteDC(hdc);
1999     ReleaseDC(0, screenDC);
2000
2001     /* show that Windows ignores the provided bm.bmWidthBytes */
2002     bmp.bmType = 0;
2003     bmp.bmWidth = 1;
2004     bmp.bmHeight = 1;
2005     bmp.bmWidthBytes = 28;
2006     bmp.bmPlanes = 1;
2007     bmp.bmBitsPixel = 1;
2008     bmp.bmBits = NULL;
2009     bm = CreateBitmapIndirect(&bmp);
2010     ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2011     test_mono_1x1_bmp(bm);
2012     DeleteObject(bm);
2013
2014     /* Test how the bmBitsPixel field is treated */
2015     for(i = 1; i <= 33; i++) {
2016         bmp.bmType = 0;
2017         bmp.bmWidth = 1;
2018         bmp.bmHeight = 1;
2019         bmp.bmWidthBytes = 28;
2020         bmp.bmPlanes = 1;
2021         bmp.bmBitsPixel = i;
2022         bmp.bmBits = NULL;
2023         SetLastError(0xdeadbeef);
2024         bm = CreateBitmapIndirect(&bmp);
2025         if(i > 32) {
2026             DWORD error = GetLastError();
2027             ok(bm == 0 ||
2028                broken(bm != 0), /* Win9x and WinMe */
2029                "CreateBitmapIndirect for %d bpp succeeded\n", i);
2030             ok(error == ERROR_INVALID_PARAMETER ||
2031                broken(error == 0xdeadbeef), /* Win9x and WinME */
2032                "Got error %d, expected ERROR_INVALID_PARAMETER\n", error);
2033             DeleteObject(bm);
2034             continue;
2035         }
2036         ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2037         GetObject(bm, sizeof(bmp), &bmp);
2038         if(i == 1) {
2039             expect = 1;
2040         } else if(i <= 4) {
2041             expect = 4;
2042         } else if(i <= 8) {
2043             expect = 8;
2044         } else if(i <= 16) {
2045             expect = 16;
2046         } else if(i <= 24) {
2047             expect = 24;
2048         } else if(i <= 32) {
2049             expect = 32;
2050         }
2051         ok(bmp.bmBitsPixel == expect ||
2052            broken(bmp.bmBitsPixel == i), /* Win9x and WinMe */
2053            "CreateBitmapIndirect for a %d bpp bitmap created a %d bpp bitmap, expected %d\n",
2054            i, bmp.bmBitsPixel, expect);
2055         DeleteObject(bm);
2056     }
2057 }
2058
2059 static void test_bitmapinfoheadersize(void)
2060 {
2061     HBITMAP hdib;
2062     BITMAPINFO bmi;
2063     BITMAPCOREINFO bci;
2064     HDC hdc = GetDC(0);
2065
2066     memset(&bmi, 0, sizeof(BITMAPINFO));
2067     bmi.bmiHeader.biHeight = 100;
2068     bmi.bmiHeader.biWidth = 512;
2069     bmi.bmiHeader.biBitCount = 24;
2070     bmi.bmiHeader.biPlanes = 1;
2071
2072     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER) - 1;
2073
2074     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2075     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2076
2077     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2078
2079     SetLastError(0xdeadbeef);
2080     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2081     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2082     DeleteObject(hdib);
2083
2084     bmi.bmiHeader.biSize++;
2085
2086     SetLastError(0xdeadbeef);
2087     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2088     ok(hdib != NULL ||
2089        broken(!hdib), /* Win98, WinMe */
2090        "CreateDIBSection error %d\n", GetLastError());
2091     DeleteObject(hdib);
2092
2093     bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
2094
2095     SetLastError(0xdeadbeef);
2096     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2097     ok(hdib != NULL ||
2098        broken(!hdib), /* Win98, WinMe */
2099        "CreateDIBSection error %d\n", GetLastError());
2100     DeleteObject(hdib);
2101
2102     bmi.bmiHeader.biSize++;
2103
2104     SetLastError(0xdeadbeef);
2105     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2106     ok(hdib != NULL ||
2107        broken(!hdib), /* Win98, WinMe */
2108        "CreateDIBSection error %d\n", GetLastError());
2109     DeleteObject(hdib);
2110
2111     bmi.bmiHeader.biSize = sizeof(BITMAPV4HEADER);
2112
2113     SetLastError(0xdeadbeef);
2114     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2115     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2116     DeleteObject(hdib);
2117
2118     bmi.bmiHeader.biSize = sizeof(BITMAPV5HEADER);
2119
2120     SetLastError(0xdeadbeef);
2121     hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2122     ok(hdib != NULL ||
2123        broken(!hdib), /* Win95 */
2124        "CreateDIBSection error %d\n", GetLastError());
2125     DeleteObject(hdib);
2126
2127     memset(&bci, 0, sizeof(BITMAPCOREINFO));
2128     bci.bmciHeader.bcHeight = 100;
2129     bci.bmciHeader.bcWidth = 512;
2130     bci.bmciHeader.bcBitCount = 24;
2131     bci.bmciHeader.bcPlanes = 1;
2132
2133     bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER) - 1;
2134
2135     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2136     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2137
2138     bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
2139
2140     SetLastError(0xdeadbeef);
2141     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2142     ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2143     DeleteObject(hdib);
2144
2145     bci.bmciHeader.bcSize++;
2146
2147     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2148     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2149
2150     bci.bmciHeader.bcSize = sizeof(BITMAPCOREINFO);
2151
2152     hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2153     ok(hdib == NULL, "CreateDIBSection succeeded\n");
2154
2155     ReleaseDC(0, hdc);
2156 }
2157
2158 static void test_get16dibits(void)
2159 {
2160     BYTE bits[4 * (16 / sizeof(BYTE))];
2161     HBITMAP hbmp;
2162     HDC screen_dc = GetDC(NULL);
2163     int ret;
2164     BITMAPINFO * info;
2165     int info_len = sizeof(BITMAPINFOHEADER) + 1024;
2166     BYTE *p;
2167     int overwritten_bytes = 0;
2168
2169     memset(bits, 0, sizeof(bits));
2170     hbmp = CreateBitmap(2, 2, 1, 16, bits);
2171     ok(hbmp != NULL, "CreateBitmap failed\n");
2172
2173     info  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info_len);
2174     assert(info);
2175
2176     memset(info, '!', info_len);
2177     memset(info, 0, sizeof(info->bmiHeader));
2178
2179     info->bmiHeader.biSize = sizeof(info->bmiHeader);
2180     info->bmiHeader.biWidth = 2;
2181     info->bmiHeader.biHeight = 2;
2182     info->bmiHeader.biPlanes = 1;
2183     info->bmiHeader.biCompression = BI_RGB;
2184
2185     ret = GetDIBits(screen_dc, hbmp, 0, 0, NULL, info, 0);
2186     ok(ret != 0, "GetDIBits failed\n");
2187
2188     for (p = ((BYTE *) info) + sizeof(info->bmiHeader); (p - ((BYTE *) info)) < info_len; p++)
2189         if (*p != '!')
2190             overwritten_bytes++;
2191     ok(overwritten_bytes == 0, "GetDIBits wrote past the buffer given\n");
2192
2193     HeapFree(GetProcessHeap(), 0, info);
2194     DeleteObject(hbmp);
2195     ReleaseDC(NULL, screen_dc);
2196 }
2197
2198 static void test_GdiAlphaBlend(void)
2199 {
2200     /* test out-of-bound parameters for GdiAlphaBlend */
2201     HDC hdcNull;
2202
2203     HDC hdcDst;
2204     HBITMAP bmpDst;
2205     HBITMAP oldDst;
2206
2207     BITMAPINFO bmi;
2208     HDC hdcSrc;
2209     HBITMAP bmpSrc;
2210     HBITMAP oldSrc;
2211     LPVOID bits;
2212
2213     BLENDFUNCTION blend;
2214
2215     if (!pGdiAlphaBlend)
2216     {
2217         win_skip("GdiAlphaBlend() is not implemented\n");
2218         return;
2219     }
2220
2221     hdcNull = GetDC(NULL);
2222     hdcDst = CreateCompatibleDC(hdcNull);
2223     bmpDst = CreateCompatibleBitmap(hdcNull, 100, 100);
2224     hdcSrc = CreateCompatibleDC(hdcNull);
2225
2226     memset(&bmi, 0, sizeof(bmi));  /* as of Wine 0.9.44 we require the src to be a DIB section */
2227     bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
2228     bmi.bmiHeader.biHeight = 20;
2229     bmi.bmiHeader.biWidth = 20;
2230     bmi.bmiHeader.biBitCount = 32;
2231     bmi.bmiHeader.biPlanes = 1;
2232     bmi.bmiHeader.biCompression = BI_RGB;
2233     bmpSrc = CreateDIBSection(hdcDst, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
2234     ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2235
2236     oldDst = SelectObject(hdcDst, bmpDst);
2237     oldSrc = SelectObject(hdcSrc, bmpSrc);
2238
2239     blend.BlendOp = AC_SRC_OVER;
2240     blend.BlendFlags = 0;
2241     blend.SourceConstantAlpha = 128;
2242     blend.AlphaFormat = 0;
2243
2244     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, 0, 20, 20, blend), TRUE, BOOL, "%d");
2245     SetLastError(0xdeadbeef);
2246     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), FALSE, BOOL, "%d");
2247     expect_eq(GetLastError(), ERROR_INVALID_PARAMETER, int, "%d");
2248     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), FALSE, BOOL, "%d");
2249     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 15, 0, 10, 10, blend), FALSE, BOOL, "%d");
2250     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2251     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2252
2253     SetWindowOrgEx(hdcSrc, -10, -10, NULL);
2254     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), TRUE, BOOL, "%d");
2255     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), TRUE, BOOL, "%d");
2256     SetMapMode(hdcSrc, MM_ANISOTROPIC);
2257     ScaleWindowExtEx(hdcSrc, 10, 1, 10, 1, NULL);
2258     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 30, 30, blend), TRUE, BOOL, "%d");
2259     expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 30, 30, blend), TRUE, BOOL, "%d");
2260
2261     SelectObject(hdcDst, oldDst);
2262     SelectObject(hdcSrc, oldSrc);
2263     DeleteObject(bmpSrc);
2264     DeleteObject(bmpDst);
2265     DeleteDC(hdcDst);
2266     DeleteDC(hdcSrc);
2267
2268     ReleaseDC(NULL, hdcNull);
2269
2270 }
2271
2272 static void test_clipping(void)
2273 {
2274     HBITMAP bmpDst;
2275     HBITMAP oldDst;
2276     HBITMAP bmpSrc;
2277     HBITMAP oldSrc;
2278     HRGN hRgn;
2279     LPVOID bits;
2280     BOOL result;
2281
2282     HDC hdcDst = CreateCompatibleDC( NULL );
2283     HDC hdcSrc = CreateCompatibleDC( NULL );
2284
2285     BITMAPINFO bmpinfo={{0}};
2286     bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2287     bmpinfo.bmiHeader.biWidth = 100;
2288     bmpinfo.bmiHeader.biHeight = 100;
2289     bmpinfo.bmiHeader.biPlanes = 1;
2290     bmpinfo.bmiHeader.biBitCount = GetDeviceCaps( hdcDst, BITSPIXEL );
2291     bmpinfo.bmiHeader.biCompression = BI_RGB;
2292
2293     bmpDst = CreateDIBSection( hdcDst, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2294     ok(bmpDst != NULL, "Couldn't create destination bitmap\n");
2295     oldDst = SelectObject( hdcDst, bmpDst );
2296
2297     bmpSrc = CreateDIBSection( hdcSrc, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2298     ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2299     oldSrc = SelectObject( hdcSrc, bmpSrc );
2300
2301     result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 100, 100, SRCCOPY );
2302     ok(result, "BitBlt failed\n");
2303
2304     hRgn = CreateRectRgn( 0,0,0,0 );
2305     SelectClipRgn( hdcDst, hRgn );
2306
2307     result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 0, 0, SRCCOPY );
2308     ok(result, "BitBlt failed\n");
2309
2310     DeleteObject( bmpDst );
2311     DeleteObject( bmpSrc );
2312     DeleteObject( hRgn );
2313     DeleteDC( hdcDst );
2314     DeleteDC( hdcSrc );
2315 }
2316
2317 START_TEST(bitmap)
2318 {
2319     HMODULE hdll;
2320     is_win9x = GetWindowLongPtrW(GetDesktopWindow(), GWLP_WNDPROC) == 0;
2321
2322     hdll = GetModuleHandle("gdi32.dll");
2323     pGdiAlphaBlend = (void*)GetProcAddress(hdll, "GdiAlphaBlend");
2324
2325     test_createdibitmap();
2326     test_dibsections();
2327     test_mono_dibsection();
2328     test_bitmap();
2329     test_bmBits();
2330     test_GetDIBits_selected_DIB(1);
2331     test_GetDIBits_selected_DIB(4);
2332     test_GetDIBits_selected_DIB(8);
2333     test_GetDIBits_selected_DDB(TRUE);
2334     test_GetDIBits_selected_DDB(FALSE);
2335     test_GetDIBits();
2336     test_GetDIBits_BI_BITFIELDS();
2337     test_select_object();
2338     test_CreateBitmap();
2339     test_GdiAlphaBlend();
2340     test_bitmapinfoheadersize();
2341     test_get16dibits();
2342     test_clipping();
2343 }