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