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