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