comctl32: A couple fixes for tab icon offsets.
[wine] / dlls / gdi / tests / bitmap.c
1 /*
2  * Unit test suite for bitmaps
3  *
4  * Copyright 2004 Huw Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <assert.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "mmsystem.h"
29
30 #include "wine/test.h"
31
32 static BOOL is_win9x;
33
34 static void test_createdibitmap(void)
35 {
36     HDC hdc, hdcmem;
37     BITMAPINFOHEADER bmih;
38     BITMAP bm;
39     HBITMAP hbm, hbm_colour, hbm_old;
40     INT screen_depth;
41
42     hdc = GetDC(0);
43     screen_depth = GetDeviceCaps(hdc, BITSPIXEL);
44     memset(&bmih, 0, sizeof(bmih));
45     bmih.biSize = sizeof(bmih);
46     bmih.biWidth = 10;
47     bmih.biHeight = 10;
48     bmih.biPlanes = 1;
49     bmih.biBitCount = 32;
50     bmih.biCompression = BI_RGB;
51  
52     /* First create an un-initialised bitmap.  The depth of the bitmap
53        should match that of the hdc and not that supplied in bmih.
54     */
55
56     /* First try 32 bits */
57     hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
58     ok(hbm != NULL, "CreateDIBitmap failed\n");
59     ok(GetObject(hbm, sizeof(bm), &bm), "GetObject failed\n");
60
61     ok(bm.bmBitsPixel == screen_depth, "CreateDIBitmap created bitmap of incorrect depth %d != %d\n", bm.bmBitsPixel, screen_depth);
62     DeleteObject(hbm);
63     
64     /* Then 16 */
65     bmih.biBitCount = 16;
66     hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
67     ok(hbm != NULL, "CreateDIBitmap failed\n");
68     ok(GetObject(hbm, sizeof(bm), &bm), "GetObject failed\n");
69
70     ok(bm.bmBitsPixel == screen_depth, "CreateDIBitmap created bitmap of incorrect depth %d != %d\n", bm.bmBitsPixel, screen_depth);
71     DeleteObject(hbm);
72
73     /* Then 1 */
74     bmih.biBitCount = 1;
75     hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
76     ok(hbm != NULL, "CreateDIBitmap failed\n");
77     ok(GetObject(hbm, sizeof(bm), &bm), "GetObject failed\n");
78
79     ok(bm.bmBitsPixel == screen_depth, "CreateDIBitmap created bitmap of incorrect depth %d != %d\n", bm.bmBitsPixel, screen_depth);
80     DeleteObject(hbm);
81
82     /* Now with a monochrome dc we expect a monochrome bitmap */
83     hdcmem = CreateCompatibleDC(hdc);
84
85     /* First try 32 bits */
86     bmih.biBitCount = 32;
87     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
88     ok(hbm != NULL, "CreateDIBitmap failed\n");
89     ok(GetObject(hbm, sizeof(bm), &bm), "GetObject failed\n");
90
91     ok(bm.bmBitsPixel == 1, "CreateDIBitmap created bitmap of incorrect depth %d != %d\n", bm.bmBitsPixel, 1);
92     DeleteObject(hbm);
93     
94     /* Then 16 */
95     bmih.biBitCount = 16;
96     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
97     ok(hbm != NULL, "CreateDIBitmap failed\n");
98     ok(GetObject(hbm, sizeof(bm), &bm), "GetObject failed\n");
99
100     ok(bm.bmBitsPixel == 1, "CreateDIBitmap created bitmap of incorrect depth %d != %d\n", bm.bmBitsPixel, 1);
101     DeleteObject(hbm);
102     
103     /* Then 1 */
104     bmih.biBitCount = 1;
105     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
106     ok(hbm != NULL, "CreateDIBitmap failed\n");
107     ok(GetObject(hbm, sizeof(bm), &bm), "GetObject failed\n");
108
109     ok(bm.bmBitsPixel == 1, "CreateDIBitmap created bitmap of incorrect depth %d != %d\n", bm.bmBitsPixel, 1);
110     DeleteObject(hbm);
111
112     /* Now select a polychrome bitmap into the dc and we expect
113        screen_depth bitmaps again */
114     hbm_colour = CreateCompatibleBitmap(hdc, 1, 1);
115     hbm_old = SelectObject(hdcmem, hbm_colour);
116
117     /* First try 32 bits */
118     bmih.biBitCount = 32;
119     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
120     ok(hbm != NULL, "CreateDIBitmap failed\n");
121     ok(GetObject(hbm, sizeof(bm), &bm), "GetObject failed\n");
122
123     ok(bm.bmBitsPixel == screen_depth, "CreateDIBitmap created bitmap of incorrect depth %d != %d\n", bm.bmBitsPixel, screen_depth);
124     DeleteObject(hbm);
125     
126     /* Then 16 */
127     bmih.biBitCount = 16;
128     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
129     ok(hbm != NULL, "CreateDIBitmap failed\n");
130     ok(GetObject(hbm, sizeof(bm), &bm), "GetObject failed\n");
131
132     ok(bm.bmBitsPixel == screen_depth, "CreateDIBitmap created bitmap of incorrect depth %d != %d\n", bm.bmBitsPixel, screen_depth);
133     DeleteObject(hbm);
134     
135     /* Then 1 */
136     bmih.biBitCount = 1;
137     hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
138     ok(hbm != NULL, "CreateDIBitmap failed\n");
139     ok(GetObject(hbm, sizeof(bm), &bm), "GetObject failed\n");
140
141     ok(bm.bmBitsPixel == screen_depth, "CreateDIBitmap created bitmap of incorrect depth %d != %d\n", bm.bmBitsPixel, screen_depth);
142     DeleteObject(hbm);
143
144     SelectObject(hdcmem, hbm_old);
145     DeleteObject(hbm_colour);
146     DeleteDC(hdcmem);
147
148     /* If hdc == 0 then we get a 1 bpp bitmap */
149     if (!is_win9x) {
150         bmih.biBitCount = 32;
151         hbm = CreateDIBitmap(0, &bmih, 0, NULL, NULL, 0);
152         ok(hbm != NULL, "CreateDIBitmap failed\n");
153         ok(GetObject(hbm, sizeof(bm), &bm), "GetObject failed\n");
154
155         ok(bm.bmBitsPixel == 1, "CreateDIBitmap created bitmap of incorrect depth %d != %d\n", bm.bmBitsPixel, 1);
156         DeleteObject(hbm);
157     }
158     
159     ReleaseDC(0, hdc);
160 }
161
162 #define test_color_todo(got, exp, txt, todo) \
163     if (!todo && got != exp && screen_depth < 24) { \
164       todo_wine ok(0, #txt " failed at %d-bit screen depth: got 0x%06x expected 0x%06x - skipping DIB tests\n", \
165                    screen_depth, (UINT)got, (UINT)exp); \
166       return; \
167     } else if (todo) todo_wine { ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp); } \
168     else ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp) \
169
170 #define test_color(hdc, color, exp, todo_setp, todo_getp) \
171 { \
172     COLORREF c; \
173     c = SetPixel(hdc, 0, 0, color); \
174     if (!is_win9x) { test_color_todo(c, exp, SetPixel, todo_setp); } \
175     c = GetPixel(hdc, 0, 0); \
176     test_color_todo(c, exp, GetPixel, todo_getp); \
177 }
178
179 static void test_dibsections(void)
180 {
181     HDC hdc, hdcmem, hdcmem2;
182     HBITMAP hdib, oldbm, hdib2, oldbm2;
183     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
184     char bcibuf[sizeof(BITMAPCOREINFO) + 256 * sizeof(RGBTRIPLE)];
185     BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
186     BITMAPCOREINFO *pbci = (BITMAPCOREINFO *)bcibuf;
187     HBITMAP hcoredib;
188     char coreBits[256];
189     BYTE *bits;
190     RGBQUAD rgb[256];
191     int ret;
192     char logpalbuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
193     LOGPALETTE *plogpal = (LOGPALETTE*)logpalbuf;
194     WORD *index;
195     DWORD *bits32;
196     HPALETTE hpal, oldpal;
197     DIBSECTION dibsec;
198     COLORREF c0, c1;
199     int i;
200     int screen_depth;
201     MEMORY_BASIC_INFORMATION info;
202
203     hdc = GetDC(0);
204     screen_depth = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
205
206     memset(pbmi, 0, sizeof(bmibuf));
207     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
208     pbmi->bmiHeader.biHeight = 100;
209     pbmi->bmiHeader.biWidth = 512;
210     pbmi->bmiHeader.biBitCount = 24;
211     pbmi->bmiHeader.biPlanes = 1;
212     pbmi->bmiHeader.biCompression = BI_RGB;
213
214     SetLastError(0xdeadbeef);
215     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
216     ok(hdib != NULL, "CreateDIBSection error %ld\n", GetLastError());
217     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
218     ok(dibsec.dsBm.bmBits == bits, "dibsec.dsBits %p != bits %p\n", dibsec.dsBm.bmBits, bits);
219
220     /* test the DIB memory */
221     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
222         "VirtualQuery failed\n");
223     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
224     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
225     ok(info.AllocationProtect == PAGE_READWRITE, "%lx != PAGE_READWRITE\n", info.AllocationProtect);
226     ok(info.RegionSize == 0x26000, "0x%lx != 0x26000\n", info.RegionSize);
227     ok(info.State == MEM_COMMIT, "%lx != MEM_COMMIT\n", info.State);
228     ok(info.Protect == PAGE_READWRITE, "%lx != PAGE_READWRITE\n", info.Protect);
229     ok(info.Type == MEM_PRIVATE, "%lx != MEM_PRIVATE\n", info.Type);
230
231     DeleteObject(hdib);
232
233     pbmi->bmiHeader.biBitCount = 8;
234     pbmi->bmiHeader.biCompression = BI_RLE8;
235     SetLastError(0xdeadbeef);
236     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
237     ok(hdib == NULL, "CreateDIBSection should fail when asked to create a compressed DIB section\n");
238     ok(GetLastError() == 0xdeadbeef, "wrong error %ld\n", GetLastError());
239
240     pbmi->bmiHeader.biBitCount = 16;
241     pbmi->bmiHeader.biCompression = BI_BITFIELDS;
242     ((PDWORD)pbmi->bmiColors)[0] = 0xf800;
243     ((PDWORD)pbmi->bmiColors)[1] = 0x07e0;
244     ((PDWORD)pbmi->bmiColors)[2] = 0x001f;
245     SetLastError(0xdeadbeef);
246     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
247     ok(hdib != NULL, "CreateDIBSection error %ld\n", GetLastError());
248
249     /* test the DIB memory */
250     ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
251         "VirtualQuery failed\n");
252     ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
253     ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
254     ok(info.AllocationProtect == PAGE_READWRITE, "%lx != PAGE_READWRITE\n", info.AllocationProtect);
255     ok(info.RegionSize == 0x19000, "0x%lx != 0x19000\n", info.RegionSize);
256     ok(info.State == MEM_COMMIT, "%lx != MEM_COMMIT\n", info.State);
257     ok(info.Protect == PAGE_READWRITE, "%lx != PAGE_READWRITE\n", info.Protect);
258     ok(info.Type == MEM_PRIVATE, "%lx != MEM_PRIVATE\n", info.Type);
259
260     DeleteObject(hdib);
261
262     memset(pbmi, 0, sizeof(bmibuf));
263     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
264     pbmi->bmiHeader.biHeight = 16;
265     pbmi->bmiHeader.biWidth = 16;
266     pbmi->bmiHeader.biBitCount = 1;
267     pbmi->bmiHeader.biPlanes = 1;
268     pbmi->bmiHeader.biCompression = BI_RGB;
269     pbmi->bmiColors[0].rgbRed = 0xff;
270     pbmi->bmiColors[0].rgbGreen = 0;
271     pbmi->bmiColors[0].rgbBlue = 0;
272     pbmi->bmiColors[1].rgbRed = 0;
273     pbmi->bmiColors[1].rgbGreen = 0;
274     pbmi->bmiColors[1].rgbBlue = 0xff;
275
276     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
277     ok(hdib != NULL, "CreateDIBSection failed\n");
278     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
279     ok(dibsec.dsBmih.biClrUsed == 2,
280         "created DIBSection: wrong biClrUsed field: %lu, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
281
282     /* Test if the old BITMAPCOREINFO structure is supported */    
283         
284     pbci->bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
285     pbci->bmciHeader.bcBitCount = 0;
286
287     if (!is_win9x) {
288         ret = GetDIBits(hdc, hdib, 0, 16, NULL, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
289         ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
290         ok((pbci->bmciHeader.bcWidth == 16) && (pbci->bmciHeader.bcHeight == 16)
291             && (pbci->bmciHeader.bcBitCount == 1) && (pbci->bmciHeader.bcPlanes == 1),
292         "GetDIBits did't fill in the BITMAPCOREHEADER structure properly\n");
293
294         ret = GetDIBits(hdc, hdib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
295         ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
296         ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
297             (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
298             (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
299             "The color table has not been translated to the old BITMAPCOREINFO format\n");
300
301         hcoredib = CreateDIBSection(hdc, (BITMAPINFO*) pbci, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
302         ok(hcoredib != NULL, "CreateDIBSection failed with a BITMAPCOREINFO\n");
303
304         ZeroMemory(pbci->bmciColors, 256 * sizeof(RGBTRIPLE));
305         ret = GetDIBits(hdc, hcoredib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
306         ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
307         ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
308             (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
309             (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
310             "The color table has not been translated to the old BITMAPCOREINFO format\n");
311
312         DeleteObject(hcoredib);
313     }
314
315     hdcmem = CreateCompatibleDC(hdc);
316     oldbm = SelectObject(hdcmem, hdib);
317
318     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
319     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
320     ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
321        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
322        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
323        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
324
325     c0 = RGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen, pbmi->bmiColors[0].rgbBlue);
326     c1 = RGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen, pbmi->bmiColors[1].rgbBlue);
327
328     test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
329     test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
330     test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
331     test_color(hdcmem, PALETTEINDEX(0), c0, 1, 1);
332     test_color(hdcmem, PALETTEINDEX(1), c0, 1, 1);
333     test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
334     test_color(hdcmem, PALETTERGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen,
335         pbmi->bmiColors[0].rgbBlue), c0, 1, 1);
336     test_color(hdcmem, PALETTERGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen,
337         pbmi->bmiColors[1].rgbBlue), c1, 1, 1);
338     test_color(hdcmem, PALETTERGB(0, 0, 0), c0, 1, 1);
339     test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
340     test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c1, 1, 1);
341
342     SelectObject(hdcmem, oldbm);
343     DeleteObject(hdib);
344
345     pbmi->bmiColors[0].rgbRed = 0xff;
346     pbmi->bmiColors[0].rgbGreen = 0xff;
347     pbmi->bmiColors[0].rgbBlue = 0xff;
348     pbmi->bmiColors[1].rgbRed = 0;
349     pbmi->bmiColors[1].rgbGreen = 0;
350     pbmi->bmiColors[1].rgbBlue = 0;
351
352     hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
353     ok(hdib != NULL, "CreateDIBSection failed\n");
354     oldbm = SelectObject(hdcmem, hdib);
355
356     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
357     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
358     ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
359        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
360        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
361        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
362
363     SelectObject(hdcmem, oldbm);
364     DeleteObject(hdib);
365
366     pbmi->bmiHeader.biBitCount = 4;
367     for (i = 0; i < 16; i++) {
368         pbmi->bmiColors[i].rgbRed = i;
369         pbmi->bmiColors[i].rgbGreen = 16-i;
370         pbmi->bmiColors[i].rgbBlue = 0;
371     }
372     hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
373     ok(hdib != NULL, "CreateDIBSection failed\n");
374     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
375     ok(dibsec.dsBmih.biClrUsed == 16,
376        "created DIBSection: wrong biClrUsed field: %lu, should be: %u\n", dibsec.dsBmih.biClrUsed, 16);
377     DeleteObject(hdib);
378
379     pbmi->bmiHeader.biBitCount = 8;
380
381     for (i = 0; i < 128; i++) {
382         pbmi->bmiColors[i].rgbRed = 255 - i * 2;
383         pbmi->bmiColors[i].rgbGreen = i * 2;
384         pbmi->bmiColors[i].rgbBlue = 0;
385         pbmi->bmiColors[255 - i].rgbRed = 0;
386         pbmi->bmiColors[255 - i].rgbGreen = i * 2;
387         pbmi->bmiColors[255 - i].rgbBlue = 255 - i * 2;
388     }
389     hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
390     ok(hdib != NULL, "CreateDIBSection failed\n");
391     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
392     ok(dibsec.dsBmih.biClrUsed == 256,
393         "created DIBSection: wrong biClrUsed field: %lu, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
394
395     oldbm = SelectObject(hdcmem, hdib);
396
397     for (i = 0; i < 256; i++) {
398         test_color(hdcmem, DIBINDEX(i), 
399             RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
400         test_color(hdcmem, PALETTERGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 
401             RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
402     }
403
404     SelectObject(hdcmem, oldbm);
405     DeleteObject(hdib);
406
407     pbmi->bmiHeader.biBitCount = 1;
408
409     /* Now create a palette and a palette indexed dib section */
410     memset(plogpal, 0, sizeof(logpalbuf));
411     plogpal->palVersion = 0x300;
412     plogpal->palNumEntries = 2;
413     plogpal->palPalEntry[0].peRed = 0xff;
414     plogpal->palPalEntry[0].peBlue = 0xff;
415     plogpal->palPalEntry[1].peGreen = 0xff;
416
417     index = (WORD*)pbmi->bmiColors;
418     *index++ = 0;
419     *index = 1;
420     hpal = CreatePalette(plogpal);
421     ok(hpal != NULL, "CreatePalette failed\n");
422     oldpal = SelectPalette(hdc, hpal, TRUE);
423     hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
424     ok(hdib != NULL, "CreateDIBSection failed\n");
425     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
426     ok(dibsec.dsBmih.biClrUsed == 2,
427         "created DIBSection: wrong biClrUsed field: %lu, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
428
429     /* The colour table has already been grabbed from the dc, so we select back the
430        old palette */
431
432     SelectPalette(hdc, oldpal, TRUE);
433     oldbm = SelectObject(hdcmem, hdib);
434     oldpal = SelectPalette(hdcmem, hpal, TRUE);
435
436     ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
437     ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
438     ok(rgb[0].rgbRed == 0xff && rgb[0].rgbBlue == 0xff && rgb[0].rgbGreen == 0 &&
439        rgb[1].rgbRed == 0    && rgb[1].rgbBlue == 0    && rgb[1].rgbGreen == 0xff,
440        "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
441        rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
442        rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
443
444     c0 = RGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen, plogpal->palPalEntry[0].peBlue);
445     c1 = RGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen, plogpal->palPalEntry[1].peBlue);
446
447     test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
448     test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
449     test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
450     test_color(hdcmem, PALETTEINDEX(0), c0, 0, 1);
451     test_color(hdcmem, PALETTEINDEX(1), c1, 0, 1);
452     test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
453     test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen,
454         plogpal->palPalEntry[0].peBlue), c0, 1, 1);
455     test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen,
456         plogpal->palPalEntry[1].peBlue), c1, 1, 1);
457     test_color(hdcmem, PALETTERGB(0, 0, 0), c1, 1, 1);
458     test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
459     test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c0, 1, 1);
460     test_color(hdcmem, PALETTERGB(0, 1, 0), c1, 1, 1);
461     test_color(hdcmem, PALETTERGB(0x3f, 0, 0x3f), c1, 1, 1);
462     test_color(hdcmem, PALETTERGB(0x40, 0, 0x40), c0, 1, 1);
463
464     /* Bottom and 2nd row from top green, everything else magenta */
465     bits[0] = bits[1] = 0xff;
466     bits[13 * 4] = bits[13*4 + 1] = 0xff;
467
468
469     pbmi->bmiHeader.biBitCount = 32;
470
471     hdib2 = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&bits32, NULL, 0);
472     ok(hdib2 != NULL, "CreateDIBSection failed\n");
473     hdcmem2 = CreateCompatibleDC(hdc);
474     oldbm2 = SelectObject(hdcmem2, hdib2);
475
476     BitBlt(hdcmem2, 0, 0, 16,16, hdcmem, 0, 0, SRCCOPY);
477
478     ok(bits32[0] == 0xff00, "lower left pixel is %08lx\n", bits32[0]);
479     ok(bits32[17] == 0xff00ff, "bottom but one, left pixel is %08lx\n", bits32[17]);
480
481     SelectObject(hdcmem2, oldbm2);
482     DeleteObject(hdib2);
483
484     SelectObject(hdcmem, oldbm);
485     SelectObject(hdcmem, oldpal);
486     DeleteObject(hdib);
487     DeleteObject(hpal);
488
489
490     pbmi->bmiHeader.biBitCount = 8;
491
492     memset(plogpal, 0, sizeof(logpalbuf));
493     plogpal->palVersion = 0x300;
494     plogpal->palNumEntries = 256;
495
496     for (i = 0; i < 128; i++) {
497         plogpal->palPalEntry[i].peRed = 255 - i * 2;
498         plogpal->palPalEntry[i].peBlue = i * 2;
499         plogpal->palPalEntry[i].peGreen = 0;
500         plogpal->palPalEntry[255 - i].peRed = 0;
501         plogpal->palPalEntry[255 - i].peGreen = i * 2;
502         plogpal->palPalEntry[255 - i].peBlue = 255 - i * 2;
503     }
504
505     index = (WORD*)pbmi->bmiColors;
506     for (i = 0; i < 256; i++) {
507         *index++ = i;
508     }
509
510     hpal = CreatePalette(plogpal);
511     ok(hpal != NULL, "CreatePalette failed\n");
512     oldpal = SelectPalette(hdc, hpal, TRUE);
513     hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
514     ok(hdib != NULL, "CreateDIBSection failed\n");
515     ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
516     ok(dibsec.dsBmih.biClrUsed == 256,
517         "created DIBSection: wrong biClrUsed field: %lu, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
518
519     SelectPalette(hdc, oldpal, TRUE);
520     oldbm = SelectObject(hdcmem, hdib);
521     oldpal = SelectPalette(hdcmem, hpal, TRUE);
522
523     ret = GetDIBColorTable(hdcmem, 0, 256, rgb);
524     ok(ret == 256, "GetDIBColorTable returned %d\n", ret);
525     for (i = 0; i < 256; i++) {
526         ok(rgb[i].rgbRed == plogpal->palPalEntry[i].peRed && 
527             rgb[i].rgbBlue == plogpal->palPalEntry[i].peBlue && 
528             rgb[i].rgbGreen == plogpal->palPalEntry[i].peGreen, 
529             "GetDIBColorTable returns table %d: r%02x g%02x b%02x res%02x\n",
530             i, rgb[i].rgbRed, rgb[i].rgbGreen, rgb[i].rgbBlue, rgb[i].rgbReserved);
531     }
532
533     for (i = 0; i < 256; i++) {
534         test_color(hdcmem, DIBINDEX(i), 
535             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
536         test_color(hdcmem, PALETTEINDEX(i), 
537             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
538         test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 
539             RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
540     }
541
542     SelectPalette(hdcmem, oldpal, TRUE);
543     SelectObject(hdcmem, oldbm);
544     DeleteObject(hdib);
545     DeleteObject(hpal);
546
547
548     DeleteDC(hdcmem);
549     ReleaseDC(0, hdc);
550 }    
551
552 void test_mono_dibsection(void)
553 {
554     HDC hdc, memdc;
555     HBITMAP old_bm, mono_ds;
556     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
557     BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
558     BYTE bits[10 * 4];
559     BYTE *ds_bits;
560     int num;
561
562     hdc = GetDC(0);
563
564     memdc = CreateCompatibleDC(hdc);
565
566     memset(pbmi, 0, sizeof(bmibuf));
567     pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
568     pbmi->bmiHeader.biHeight = 10;
569     pbmi->bmiHeader.biWidth = 10;
570     pbmi->bmiHeader.biBitCount = 1;
571     pbmi->bmiHeader.biPlanes = 1;
572     pbmi->bmiHeader.biCompression = BI_RGB;
573     pbmi->bmiColors[0].rgbRed = 0xff;
574     pbmi->bmiColors[0].rgbGreen = 0xff;
575     pbmi->bmiColors[0].rgbBlue = 0xff;
576     pbmi->bmiColors[1].rgbRed = 0x0;
577     pbmi->bmiColors[1].rgbGreen = 0x0;
578     pbmi->bmiColors[1].rgbBlue = 0x0;
579
580     /*
581      * First dib section is 'inverted' ie color[0] is white, color[1] is black
582      */
583
584     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
585     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
586     old_bm = SelectObject(memdc, mono_ds);
587
588     /* black border, white interior */
589     Rectangle(memdc, 0, 0, 10, 10);
590     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
591     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
592
593     /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
594
595     memset(bits, 0, sizeof(bits));
596     bits[0] = 0xaa;
597
598     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
599     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
600
601     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
602
603     pbmi->bmiColors[0].rgbRed = 0x0;
604     pbmi->bmiColors[0].rgbGreen = 0x0;
605     pbmi->bmiColors[0].rgbBlue = 0x0;
606     pbmi->bmiColors[1].rgbRed = 0xff;
607     pbmi->bmiColors[1].rgbGreen = 0xff;
608     pbmi->bmiColors[1].rgbBlue = 0xff;
609
610     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
611     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
612
613     SelectObject(memdc, old_bm);
614     DeleteObject(mono_ds);
615
616     /*
617      * Next dib section is 'normal' ie color[0] is black, color[1] is white
618      */
619
620     pbmi->bmiColors[0].rgbRed = 0x0;
621     pbmi->bmiColors[0].rgbGreen = 0x0;
622     pbmi->bmiColors[0].rgbBlue = 0x0;
623     pbmi->bmiColors[1].rgbRed = 0xff;
624     pbmi->bmiColors[1].rgbGreen = 0xff;
625     pbmi->bmiColors[1].rgbBlue = 0xff;
626
627     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
628     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
629     old_bm = SelectObject(memdc, mono_ds);
630
631     /* black border, white interior */
632     Rectangle(memdc, 0, 0, 10, 10);
633     ok(ds_bits[0] == 0x00, "out_bits %02x\n", ds_bits[0]);
634     ok(ds_bits[4] == 0x7f, "out_bits %02x\n", ds_bits[4]);
635
636     /* SetDIBitsToDevice with a normal bmi -> normal dib section */
637
638     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
639     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
640
641     /* SetDIBitsToDevice with a inverted bmi -> normal dib section */
642
643     pbmi->bmiColors[0].rgbRed = 0xff;
644     pbmi->bmiColors[0].rgbGreen = 0xff;
645     pbmi->bmiColors[0].rgbBlue = 0xff;
646     pbmi->bmiColors[1].rgbRed = 0x0;
647     pbmi->bmiColors[1].rgbGreen = 0x0;
648     pbmi->bmiColors[1].rgbBlue = 0x0;
649
650     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
651     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
652
653     /*
654      * Take that 'normal' dibsection and change its colour table to an 'inverted' one
655      */
656
657     pbmi->bmiColors[0].rgbRed = 0xff;
658     pbmi->bmiColors[0].rgbGreen = 0xff;
659     pbmi->bmiColors[0].rgbBlue = 0xff;
660     pbmi->bmiColors[1].rgbRed = 0x0;
661     pbmi->bmiColors[1].rgbGreen = 0x0;
662     pbmi->bmiColors[1].rgbBlue = 0x0;
663     num = SetDIBColorTable(memdc, 0, 2, pbmi->bmiColors);
664     ok(num == 2, "num = %d\n", num);
665
666     /* black border, white interior */
667     Rectangle(memdc, 0, 0, 10, 10);
668 todo_wine {
669     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
670     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
671  }
672     /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
673
674     memset(bits, 0, sizeof(bits));
675     bits[0] = 0xaa;
676
677     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
678     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
679
680     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
681
682     pbmi->bmiColors[0].rgbRed = 0x0;
683     pbmi->bmiColors[0].rgbGreen = 0x0;
684     pbmi->bmiColors[0].rgbBlue = 0x0;
685     pbmi->bmiColors[1].rgbRed = 0xff;
686     pbmi->bmiColors[1].rgbGreen = 0xff;
687     pbmi->bmiColors[1].rgbBlue = 0xff;
688
689     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
690     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
691
692     SelectObject(memdc, old_bm);
693     DeleteObject(mono_ds);
694
695     /*
696      * Now a dib section with a strange colour map just for fun.  This behaves just like an inverted one.
697      */
698  
699     pbmi->bmiColors[0].rgbRed = 0xff;
700     pbmi->bmiColors[0].rgbGreen = 0x0;
701     pbmi->bmiColors[0].rgbBlue = 0x0;
702     pbmi->bmiColors[1].rgbRed = 0xfe;
703     pbmi->bmiColors[1].rgbGreen = 0x0;
704     pbmi->bmiColors[1].rgbBlue = 0x0;
705
706     mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
707     ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
708     old_bm = SelectObject(memdc, mono_ds);
709
710     /* black border, white interior */
711     Rectangle(memdc, 0, 0, 10, 10);
712     ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
713     ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
714
715     /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
716
717     pbmi->bmiColors[0].rgbRed = 0x0;
718     pbmi->bmiColors[0].rgbGreen = 0x0;
719     pbmi->bmiColors[0].rgbBlue = 0x0;
720     pbmi->bmiColors[1].rgbRed = 0xff;
721     pbmi->bmiColors[1].rgbGreen = 0xff;
722     pbmi->bmiColors[1].rgbBlue = 0xff;
723
724     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
725     ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
726
727     /* SetDIBitsToDevice with a inverted bmi -> inverted dib section */
728
729     pbmi->bmiColors[0].rgbRed = 0xff;
730     pbmi->bmiColors[0].rgbGreen = 0xff;
731     pbmi->bmiColors[0].rgbBlue = 0xff;
732     pbmi->bmiColors[1].rgbRed = 0x0;
733     pbmi->bmiColors[1].rgbGreen = 0x0;
734     pbmi->bmiColors[1].rgbBlue = 0x0;
735
736     SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
737     ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
738
739     SelectObject(memdc, old_bm);
740     DeleteObject(mono_ds);
741
742     DeleteDC(memdc);
743     ReleaseDC(0, hdc);
744 }
745
746 START_TEST(bitmap)
747 {
748     HWND hWnd;
749
750     hWnd = CreateWindowExA(0, "EDIT", NULL, 0,
751                            10, 10, 300, 300,
752                            NULL, NULL, NULL, NULL);
753     assert(hWnd);
754     is_win9x = GetWindowLongPtrW(hWnd, GWLP_WNDPROC) == 0;
755     DestroyWindow(hWnd);
756
757     test_createdibitmap();
758     test_dibsections();
759     test_mono_dibsection();
760 }