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