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