dplayx: Code to forward player creation
[wine/gsoc_dplay.git] / dlls / gdi32 / tests / bitmap.c
blob8666da229b005f8bbb88ba9a9171852a2139c1e7
1 /*
2 * Unit test suite for bitmaps
4 * Copyright 2004 Huw Davies
5 * Copyright 2006 Dmitry Timoshkov
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.
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.
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
22 #include <stdarg.h>
23 #include <assert.h>
24 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "mmsystem.h"
33 #include "wine/test.h"
35 static BOOL (WINAPI *pGdiAlphaBlend)(HDC,int,int,int,int,HDC,int,int,int,int,BLENDFUNCTION);
37 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
39 static BOOL is_win9x;
41 static INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
43 switch(bpp)
45 case 1:
46 return 2 * ((bmWidth+15) >> 4);
48 case 24:
49 bmWidth *= 3; /* fall through */
50 case 8:
51 return bmWidth + (bmWidth & 1);
53 case 32:
54 return bmWidth * 4;
56 case 16:
57 case 15:
58 return bmWidth * 2;
60 case 4:
61 return 2 * ((bmWidth+3) >> 2);
63 default:
64 trace("Unknown depth %d, please report.\n", bpp );
65 assert(0);
67 return -1;
70 static void test_bitmap_info(HBITMAP hbm, INT expected_depth, const BITMAPINFOHEADER *bmih)
72 BITMAP bm;
73 BITMAP bma[2];
74 INT ret, width_bytes;
75 BYTE buf[512], buf_cmp[512];
76 DWORD gle;
78 ret = GetObject(hbm, sizeof(bm), &bm);
79 ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
81 ok(bm.bmType == 0 || broken(bm.bmType == 21072 /* Win9x */), "wrong bm.bmType %d\n", bm.bmType);
82 ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
83 ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
84 width_bytes = BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
85 ok(bm.bmWidthBytes == width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, width_bytes);
86 ok(bm.bmPlanes == bmih->biPlanes, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
87 ok(bm.bmBitsPixel == expected_depth, "wrong bm.bmBitsPixel %d != %d\n", bm.bmBitsPixel, expected_depth);
88 ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
90 assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
91 assert(sizeof(buf) == sizeof(buf_cmp));
93 SetLastError(0xdeadbeef);
94 ret = GetBitmapBits(hbm, 0, NULL);
95 gle=GetLastError();
96 ok(ret == bm.bmWidthBytes * bm.bmHeight || (ret == 0 && gle == ERROR_INVALID_PARAMETER /* Win9x */), "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
98 memset(buf_cmp, 0xAA, sizeof(buf_cmp));
99 memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
101 memset(buf, 0xAA, sizeof(buf));
102 ret = GetBitmapBits(hbm, sizeof(buf), buf);
103 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
104 ok(!memcmp(buf, buf_cmp, sizeof(buf)) ||
105 broken(memcmp(buf, buf_cmp, sizeof(buf))), /* win9x doesn't init the bitmap bits */
106 "buffers do not match, depth %d\n", bmih->biBitCount);
108 /* test various buffer sizes for GetObject */
109 ret = GetObject(hbm, sizeof(*bma) * 2, bma);
110 ok(ret == sizeof(*bma) || broken(ret == sizeof(*bma) * 2 /* Win9x */), "wrong size %d\n", ret);
112 ret = GetObject(hbm, sizeof(bm) / 2, &bm);
113 ok(ret == 0 || broken(ret == sizeof(bm) / 2 /* Win9x */), "%d != 0\n", ret);
115 ret = GetObject(hbm, 0, &bm);
116 ok(ret == 0, "%d != 0\n", ret);
118 ret = GetObject(hbm, 1, &bm);
119 ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
121 /* Don't trust Win9x not to try to write to NULL */
122 if (ret == 0)
124 ret = GetObject(hbm, 0, NULL);
125 ok(ret == sizeof(bm), "wrong size %d\n", ret);
129 static void test_createdibitmap(void)
131 HDC hdc, hdcmem;
132 BITMAPINFOHEADER bmih;
133 BITMAPINFO bm;
134 HBITMAP hbm, hbm_colour, hbm_old;
135 INT screen_depth;
136 DWORD pixel;
138 hdc = GetDC(0);
139 screen_depth = GetDeviceCaps(hdc, BITSPIXEL);
140 memset(&bmih, 0, sizeof(bmih));
141 bmih.biSize = sizeof(bmih);
142 bmih.biWidth = 10;
143 bmih.biHeight = 10;
144 bmih.biPlanes = 1;
145 bmih.biBitCount = 32;
146 bmih.biCompression = BI_RGB;
148 hbm = CreateDIBitmap(hdc, NULL, CBM_INIT, NULL, NULL, 0);
149 ok(hbm == NULL, "CreateDIBitmap should fail\n");
150 hbm = CreateDIBitmap(hdc, NULL, 0, NULL, NULL, 0);
151 ok(hbm == NULL, "CreateDIBitmap should fail\n");
153 /* First create an un-initialised bitmap. The depth of the bitmap
154 should match that of the hdc and not that supplied in bmih.
157 /* First try 32 bits */
158 hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
159 ok(hbm != NULL, "CreateDIBitmap failed\n");
160 test_bitmap_info(hbm, screen_depth, &bmih);
161 DeleteObject(hbm);
163 /* Then 16 */
164 bmih.biBitCount = 16;
165 hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
166 ok(hbm != NULL, "CreateDIBitmap failed\n");
167 test_bitmap_info(hbm, screen_depth, &bmih);
168 DeleteObject(hbm);
170 /* Then 1 */
171 bmih.biBitCount = 1;
172 hbm = CreateDIBitmap(hdc, &bmih, 0, NULL, NULL, 0);
173 ok(hbm != NULL, "CreateDIBitmap failed\n");
174 test_bitmap_info(hbm, screen_depth, &bmih);
175 DeleteObject(hbm);
177 /* Now with a monochrome dc we expect a monochrome bitmap */
178 hdcmem = CreateCompatibleDC(hdc);
180 /* First try 32 bits */
181 bmih.biBitCount = 32;
182 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
183 ok(hbm != NULL, "CreateDIBitmap failed\n");
184 test_bitmap_info(hbm, 1, &bmih);
185 DeleteObject(hbm);
187 /* Then 16 */
188 bmih.biBitCount = 16;
189 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
190 ok(hbm != NULL, "CreateDIBitmap failed\n");
191 test_bitmap_info(hbm, 1, &bmih);
192 DeleteObject(hbm);
194 /* Then 1 */
195 bmih.biBitCount = 1;
196 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
197 ok(hbm != NULL, "CreateDIBitmap failed\n");
198 test_bitmap_info(hbm, 1, &bmih);
199 DeleteObject(hbm);
201 /* Now select a polychrome bitmap into the dc and we expect
202 screen_depth bitmaps again */
203 hbm_colour = CreateCompatibleBitmap(hdc, bmih.biWidth, bmih.biHeight);
204 test_bitmap_info(hbm_colour, screen_depth, &bmih);
205 hbm_old = SelectObject(hdcmem, hbm_colour);
207 /* First try 32 bits */
208 bmih.biBitCount = 32;
209 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
210 ok(hbm != NULL, "CreateDIBitmap failed\n");
211 test_bitmap_info(hbm, screen_depth, &bmih);
212 DeleteObject(hbm);
214 /* Then 16 */
215 bmih.biBitCount = 16;
216 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
217 ok(hbm != NULL, "CreateDIBitmap failed\n");
218 test_bitmap_info(hbm, screen_depth, &bmih);
219 DeleteObject(hbm);
221 /* Then 1 */
222 bmih.biBitCount = 1;
223 hbm = CreateDIBitmap(hdcmem, &bmih, 0, NULL, NULL, 0);
224 ok(hbm != NULL, "CreateDIBitmap failed\n");
225 test_bitmap_info(hbm, screen_depth, &bmih);
226 DeleteObject(hbm);
228 SelectObject(hdcmem, hbm_old);
229 DeleteObject(hbm_colour);
230 DeleteDC(hdcmem);
232 /* If hdc == 0 then we get a 1 bpp bitmap */
233 if (!is_win9x) {
234 bmih.biBitCount = 32;
235 hbm = CreateDIBitmap(0, &bmih, 0, NULL, NULL, 0);
236 ok(hbm != NULL, "CreateDIBitmap failed\n");
237 test_bitmap_info(hbm, 1, &bmih);
238 DeleteObject(hbm);
241 /* Test how formats are converted */
242 pixel = 0xffffffff;
243 bmih.biBitCount = 1;
244 bmih.biWidth = 1;
245 bmih.biHeight = 1;
247 memset(&bm, 0, sizeof(bm));
248 bm.bmiHeader.biSize = sizeof(bm.bmiHeader);
249 bm.bmiHeader.biWidth = 1;
250 bm.bmiHeader.biHeight = 1;
251 bm.bmiHeader.biPlanes = 1;
252 bm.bmiHeader.biBitCount= 24;
253 bm.bmiHeader.biCompression= BI_RGB;
254 bm.bmiHeader.biSizeImage = 0;
255 hbm = CreateDIBitmap(hdc, &bmih, CBM_INIT, &pixel, &bm, DIB_RGB_COLORS);
256 ok(hbm != NULL, "CreateDIBitmap failed\n");
258 pixel = 0xdeadbeef;
259 bm.bmiHeader.biBitCount= 32;
260 GetDIBits(hdc, hbm, 0, 1, &pixel, &bm, DIB_RGB_COLORS);
261 ok(pixel == 0x00ffffff, "Reading a 32 bit pixel from a DDB returned %08x\n", pixel);
262 DeleteObject(hbm);
264 ReleaseDC(0, hdc);
267 static INT DIB_GetWidthBytes( int width, int bpp )
269 int words;
271 switch (bpp)
273 case 1: words = (width + 31) / 32; break;
274 case 4: words = (width + 7) / 8; break;
275 case 8: words = (width + 3) / 4; break;
276 case 15:
277 case 16: words = (width + 1) / 2; break;
278 case 24: words = (width * 3 + 3)/4; break;
279 case 32: words = width; break;
281 default:
282 words=0;
283 trace("Unknown depth %d, please report.\n", bpp );
284 assert(0);
285 break;
287 return 4 * words;
290 static void test_dib_info(HBITMAP hbm, const void *bits, const BITMAPINFOHEADER *bmih)
292 BITMAP bm;
293 BITMAP bma[2];
294 DIBSECTION ds;
295 DIBSECTION dsa[2];
296 INT ret, bm_width_bytes, dib_width_bytes;
297 BYTE *buf;
299 ret = GetObject(hbm, sizeof(bm), &bm);
300 ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
302 ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
303 ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
304 ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
305 dib_width_bytes = DIB_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
306 bm_width_bytes = BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel);
307 if (bm.bmWidthBytes != dib_width_bytes) /* Win2k bug */
308 ok(bm.bmWidthBytes == bm_width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, bm_width_bytes);
309 else
310 ok(bm.bmWidthBytes == dib_width_bytes, "wrong bm.bmWidthBytes %d != %d\n", bm.bmWidthBytes, dib_width_bytes);
311 ok(bm.bmPlanes == bmih->biPlanes, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
312 ok(bm.bmBitsPixel == bmih->biBitCount, "bm.bmBitsPixel %d != %d\n", bm.bmBitsPixel, bmih->biBitCount);
313 ok(bm.bmBits == bits, "wrong bm.bmBits %p != %p\n", bm.bmBits, bits);
315 buf = HeapAlloc(GetProcessHeap(), 0, bm.bmWidthBytes * bm.bmHeight + 4096);
317 /* GetBitmapBits returns not 32-bit aligned data */
318 SetLastError(0xdeadbeef);
319 ret = GetBitmapBits(hbm, 0, NULL);
320 ok(ret == bm_width_bytes * bm.bmHeight ||
321 broken(ret == 0 && GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
322 "%d != %d\n", ret, bm_width_bytes * bm.bmHeight);
324 memset(buf, 0xAA, bm.bmWidthBytes * bm.bmHeight + 4096);
325 ret = GetBitmapBits(hbm, bm.bmWidthBytes * bm.bmHeight + 4096, buf);
326 ok(ret == bm_width_bytes * bm.bmHeight, "%d != %d\n", ret, bm_width_bytes * bm.bmHeight);
328 HeapFree(GetProcessHeap(), 0, buf);
330 /* test various buffer sizes for GetObject */
331 memset(&ds, 0xAA, sizeof(ds));
332 ret = GetObject(hbm, sizeof(*bma) * 2, bma);
333 ok(ret == sizeof(*bma) || broken(ret == sizeof(*bma) * 2 /* Win9x */), "wrong size %d\n", ret);
334 ok(bm.bmWidth == bmih->biWidth, "wrong bm.bmWidth %d\n", bm.bmWidth);
335 ok(bm.bmHeight == bmih->biHeight, "wrong bm.bmHeight %d\n", bm.bmHeight);
336 ok(bm.bmBits == bits, "wrong bm.bmBits %p != %p\n", bm.bmBits, bits);
338 ret = GetObject(hbm, sizeof(bm) / 2, &bm);
339 ok(ret == 0 || broken(ret == sizeof(bm) / 2 /* Win9x */), "%d != 0\n", ret);
341 ret = GetObject(hbm, 0, &bm);
342 ok(ret == 0, "%d != 0\n", ret);
344 ret = GetObject(hbm, 1, &bm);
345 ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
347 /* test various buffer sizes for GetObject */
348 ret = GetObject(hbm, 0, NULL);
349 ok(ret == sizeof(bm) || broken(ret == sizeof(DIBSECTION) /* Win9x */), "wrong size %d\n", ret);
351 ret = GetObject(hbm, sizeof(*dsa) * 2, dsa);
352 ok(ret == sizeof(*dsa) || broken(ret == sizeof(*dsa) * 2 /* Win9x */), "wrong size %d\n", ret);
354 memset(&ds, 0xAA, sizeof(ds));
355 ret = GetObject(hbm, sizeof(ds), &ds);
356 ok(ret == sizeof(ds), "wrong size %d\n", ret);
358 ok(ds.dsBm.bmBits == bits, "wrong bm.bmBits %p != %p\n", ds.dsBm.bmBits, bits);
359 if (ds.dsBm.bmWidthBytes != bm_width_bytes) /* Win2k bug */
360 ok(ds.dsBmih.biSizeImage == ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight, "%u != %u\n",
361 ds.dsBmih.biSizeImage, ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight);
362 ok(bmih->biSizeImage == 0, "%u != 0\n", bmih->biSizeImage);
363 ds.dsBmih.biSizeImage = 0;
365 ok(ds.dsBmih.biSize == bmih->biSize, "%u != %u\n", ds.dsBmih.biSize, bmih->biSize);
366 ok(ds.dsBmih.biWidth == bmih->biWidth, "%u != %u\n", ds.dsBmih.biWidth, bmih->biWidth);
367 ok(ds.dsBmih.biHeight == bmih->biHeight, "%u != %u\n", ds.dsBmih.biHeight, bmih->biHeight);
368 ok(ds.dsBmih.biPlanes == bmih->biPlanes, "%u != %u\n", ds.dsBmih.biPlanes, bmih->biPlanes);
369 ok(ds.dsBmih.biBitCount == bmih->biBitCount, "%u != %u\n", ds.dsBmih.biBitCount, bmih->biBitCount);
370 ok(ds.dsBmih.biCompression == bmih->biCompression, "%u != %u\n", ds.dsBmih.biCompression, bmih->biCompression);
371 ok(ds.dsBmih.biSizeImage == bmih->biSizeImage, "%u != %u\n", ds.dsBmih.biSizeImage, bmih->biSizeImage);
372 ok(ds.dsBmih.biXPelsPerMeter == bmih->biXPelsPerMeter, "%u != %u\n", ds.dsBmih.biXPelsPerMeter, bmih->biXPelsPerMeter);
373 ok(ds.dsBmih.biYPelsPerMeter == bmih->biYPelsPerMeter, "%u != %u\n", ds.dsBmih.biYPelsPerMeter, bmih->biYPelsPerMeter);
375 memset(&ds, 0xAA, sizeof(ds));
376 ret = GetObject(hbm, sizeof(ds) - 4, &ds);
377 ok(ret == sizeof(ds.dsBm) || broken(ret == (sizeof(ds) - 4) /* Win9x */), "wrong size %d\n", ret);
378 ok(ds.dsBm.bmWidth == bmih->biWidth, "%u != %u\n", ds.dsBmih.biWidth, bmih->biWidth);
379 ok(ds.dsBm.bmHeight == bmih->biHeight, "%u != %u\n", ds.dsBmih.biHeight, bmih->biHeight);
380 ok(ds.dsBm.bmBits == bits, "%p != %p\n", ds.dsBm.bmBits, bits);
382 ret = GetObject(hbm, 0, &ds);
383 ok(ret == 0, "%d != 0\n", ret);
385 ret = GetObject(hbm, 1, &ds);
386 ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
389 #define test_color_todo(got, exp, txt, todo) \
390 if (!todo && got != exp && screen_depth < 24) { \
391 todo_wine ok(0, #txt " failed at %d-bit screen depth: got 0x%06x expected 0x%06x - skipping DIB tests\n", \
392 screen_depth, (UINT)got, (UINT)exp); \
393 return; \
394 } else if (todo) todo_wine { ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp); } \
395 else ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp) \
397 #define test_color(hdc, color, exp, todo_setp, todo_getp) \
399 COLORREF c; \
400 c = SetPixel(hdc, 0, 0, color); \
401 if (!is_win9x) { test_color_todo(c, exp, SetPixel, todo_setp); } \
402 c = GetPixel(hdc, 0, 0); \
403 test_color_todo(c, exp, GetPixel, todo_getp); \
406 static void test_dib_bits_access( HBITMAP hdib, void *bits )
408 MEMORY_BASIC_INFORMATION info;
409 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
410 DWORD data[256];
411 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
412 HDC hdc = GetDC(0);
413 char filename[MAX_PATH];
414 HANDLE file;
415 DWORD written;
416 INT ret;
418 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
419 "VirtualQuery failed\n");
420 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
421 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
422 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
423 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
424 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
425 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
427 memset( pbmi, 0, sizeof(bmibuf) );
428 memset( data, 0xcc, sizeof(data) );
429 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
430 pbmi->bmiHeader.biHeight = 16;
431 pbmi->bmiHeader.biWidth = 16;
432 pbmi->bmiHeader.biBitCount = 32;
433 pbmi->bmiHeader.biPlanes = 1;
434 pbmi->bmiHeader.biCompression = BI_RGB;
436 ret = SetDIBits( hdc, hdib, 0, 16, data, pbmi, DIB_RGB_COLORS );
437 ok(ret == 16 ||
438 broken(ret == 0), /* win9x */
439 "SetDIBits failed: expected 16 got %d\n", ret);
441 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
442 "VirtualQuery failed\n");
443 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
444 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
445 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
446 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
447 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
448 /* it has been protected now */
449 todo_wine ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
451 /* try writing protected bits to a file */
453 GetTempFileNameA( ".", "dib", 0, filename );
454 file = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
455 CREATE_ALWAYS, 0, 0 );
456 ok( file != INVALID_HANDLE_VALUE, "failed to open %s error %u\n", filename, GetLastError() );
457 ret = WriteFile( file, bits, 8192, &written, NULL );
458 ok( ret, "WriteFile failed error %u\n", GetLastError() );
459 if (ret) ok( written == 8192, "only wrote %u bytes\n", written );
460 CloseHandle( file );
461 DeleteFileA( filename );
464 static void test_dibsections(void)
466 HDC hdc, hdcmem, hdcmem2;
467 HBITMAP hdib, oldbm, hdib2, oldbm2;
468 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
469 char bcibuf[sizeof(BITMAPCOREINFO) + 256 * sizeof(RGBTRIPLE)];
470 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
471 BITMAPCOREINFO *pbci = (BITMAPCOREINFO *)bcibuf;
472 HBITMAP hcoredib;
473 char coreBits[256];
474 BYTE *bits;
475 RGBQUAD rgb[256];
476 int ret;
477 char logpalbuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
478 LOGPALETTE *plogpal = (LOGPALETTE*)logpalbuf;
479 WORD *index;
480 DWORD *bits32;
481 HPALETTE hpal, oldpal;
482 DIBSECTION dibsec;
483 COLORREF c0, c1;
484 int i;
485 int screen_depth;
486 MEMORY_BASIC_INFORMATION info;
488 hdc = GetDC(0);
489 screen_depth = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
491 memset(pbmi, 0, sizeof(bmibuf));
492 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
493 pbmi->bmiHeader.biHeight = 100;
494 pbmi->bmiHeader.biWidth = 512;
495 pbmi->bmiHeader.biBitCount = 24;
496 pbmi->bmiHeader.biPlanes = 1;
497 pbmi->bmiHeader.biCompression = BI_RGB;
499 SetLastError(0xdeadbeef);
501 /* invalid pointer for BITMAPINFO
502 (*bits should be NULL on error) */
503 bits = (BYTE*)0xdeadbeef;
504 hdib = CreateDIBSection(hdc, NULL, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
505 ok(hdib == NULL && bits == NULL, "CreateDIBSection failed for invalid parameter: bmi == 0x0\n");
507 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
508 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
509 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
510 ok(dibsec.dsBm.bmBits == bits, "dibsec.dsBits %p != bits %p\n", dibsec.dsBm.bmBits, bits);
512 /* test the DIB memory */
513 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
514 "VirtualQuery failed\n");
515 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
516 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
517 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
518 ok(info.RegionSize == 0x26000, "0x%lx != 0x26000\n", info.RegionSize);
519 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
520 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
521 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
523 test_dib_bits_access( hdib, bits );
525 test_dib_info(hdib, bits, &pbmi->bmiHeader);
526 DeleteObject(hdib);
528 pbmi->bmiHeader.biBitCount = 8;
529 pbmi->bmiHeader.biCompression = BI_RLE8;
530 SetLastError(0xdeadbeef);
531 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
532 ok(hdib == NULL, "CreateDIBSection should fail when asked to create a compressed DIB section\n");
533 ok(GetLastError() == 0xdeadbeef, "wrong error %d\n", GetLastError());
535 pbmi->bmiHeader.biBitCount = 16;
536 pbmi->bmiHeader.biCompression = BI_BITFIELDS;
537 ((PDWORD)pbmi->bmiColors)[0] = 0xf800;
538 ((PDWORD)pbmi->bmiColors)[1] = 0x07e0;
539 ((PDWORD)pbmi->bmiColors)[2] = 0x001f;
540 SetLastError(0xdeadbeef);
541 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
542 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
544 /* test the DIB memory */
545 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
546 "VirtualQuery failed\n");
547 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
548 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
549 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
550 ok(info.RegionSize == 0x19000, "0x%lx != 0x19000\n", info.RegionSize);
551 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
552 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
553 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
555 test_dib_info(hdib, bits, &pbmi->bmiHeader);
556 DeleteObject(hdib);
558 memset(pbmi, 0, sizeof(bmibuf));
559 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
560 pbmi->bmiHeader.biHeight = 16;
561 pbmi->bmiHeader.biWidth = 16;
562 pbmi->bmiHeader.biBitCount = 1;
563 pbmi->bmiHeader.biPlanes = 1;
564 pbmi->bmiHeader.biCompression = BI_RGB;
565 pbmi->bmiColors[0].rgbRed = 0xff;
566 pbmi->bmiColors[0].rgbGreen = 0;
567 pbmi->bmiColors[0].rgbBlue = 0;
568 pbmi->bmiColors[1].rgbRed = 0;
569 pbmi->bmiColors[1].rgbGreen = 0;
570 pbmi->bmiColors[1].rgbBlue = 0xff;
572 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
573 ok(hdib != NULL, "CreateDIBSection failed\n");
574 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
575 ok(dibsec.dsBmih.biClrUsed == 2,
576 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
578 /* Test if the old BITMAPCOREINFO structure is supported */
580 pbci->bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
581 pbci->bmciHeader.bcBitCount = 0;
583 if (!is_win9x) {
584 ret = GetDIBits(hdc, hdib, 0, 16, NULL, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
585 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
586 ok((pbci->bmciHeader.bcWidth == 16) && (pbci->bmciHeader.bcHeight == 16)
587 && (pbci->bmciHeader.bcBitCount == 1) && (pbci->bmciHeader.bcPlanes == 1),
588 "GetDIBits did't fill in the BITMAPCOREHEADER structure properly\n");
590 ret = GetDIBits(hdc, hdib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
591 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
592 ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
593 (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
594 (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
595 "The color table has not been translated to the old BITMAPCOREINFO format\n");
597 hcoredib = CreateDIBSection(hdc, (BITMAPINFO*) pbci, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
598 ok(hcoredib != NULL, "CreateDIBSection failed with a BITMAPCOREINFO\n");
600 ZeroMemory(pbci->bmciColors, 256 * sizeof(RGBTRIPLE));
601 ret = GetDIBits(hdc, hcoredib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
602 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
603 ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
604 (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
605 (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
606 "The color table has not been translated to the old BITMAPCOREINFO format\n");
608 DeleteObject(hcoredib);
611 hdcmem = CreateCompatibleDC(hdc);
612 oldbm = SelectObject(hdcmem, hdib);
614 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
615 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
616 ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
617 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
618 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
619 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
621 c0 = RGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen, pbmi->bmiColors[0].rgbBlue);
622 c1 = RGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen, pbmi->bmiColors[1].rgbBlue);
624 test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
625 test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
626 test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
627 test_color(hdcmem, PALETTEINDEX(0), c0, 1, 1);
628 test_color(hdcmem, PALETTEINDEX(1), c0, 1, 1);
629 test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
630 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen,
631 pbmi->bmiColors[0].rgbBlue), c0, 1, 1);
632 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen,
633 pbmi->bmiColors[1].rgbBlue), c1, 1, 1);
634 test_color(hdcmem, PALETTERGB(0, 0, 0), c0, 1, 1);
635 test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
636 test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c1, 1, 1);
638 SelectObject(hdcmem, oldbm);
639 DeleteObject(hdib);
641 pbmi->bmiColors[0].rgbRed = 0xff;
642 pbmi->bmiColors[0].rgbGreen = 0xff;
643 pbmi->bmiColors[0].rgbBlue = 0xff;
644 pbmi->bmiColors[1].rgbRed = 0;
645 pbmi->bmiColors[1].rgbGreen = 0;
646 pbmi->bmiColors[1].rgbBlue = 0;
648 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
649 ok(hdib != NULL, "CreateDIBSection failed\n");
651 test_dib_info(hdib, bits, &pbmi->bmiHeader);
653 oldbm = SelectObject(hdcmem, hdib);
655 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
656 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
657 ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
658 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
659 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
660 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
662 SelectObject(hdcmem, oldbm);
663 test_dib_info(hdib, bits, &pbmi->bmiHeader);
664 DeleteObject(hdib);
666 pbmi->bmiHeader.biBitCount = 4;
667 for (i = 0; i < 16; i++) {
668 pbmi->bmiColors[i].rgbRed = i;
669 pbmi->bmiColors[i].rgbGreen = 16-i;
670 pbmi->bmiColors[i].rgbBlue = 0;
672 hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
673 ok(hdib != NULL, "CreateDIBSection failed\n");
674 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
675 ok(dibsec.dsBmih.biClrUsed == 16,
676 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 16);
677 test_dib_info(hdib, bits, &pbmi->bmiHeader);
678 DeleteObject(hdib);
680 pbmi->bmiHeader.biBitCount = 8;
682 for (i = 0; i < 128; i++) {
683 pbmi->bmiColors[i].rgbRed = 255 - i * 2;
684 pbmi->bmiColors[i].rgbGreen = i * 2;
685 pbmi->bmiColors[i].rgbBlue = 0;
686 pbmi->bmiColors[255 - i].rgbRed = 0;
687 pbmi->bmiColors[255 - i].rgbGreen = i * 2;
688 pbmi->bmiColors[255 - i].rgbBlue = 255 - i * 2;
690 hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
691 ok(hdib != NULL, "CreateDIBSection failed\n");
692 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
693 ok(dibsec.dsBmih.biClrUsed == 256,
694 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
696 oldbm = SelectObject(hdcmem, hdib);
698 for (i = 0; i < 256; i++) {
699 test_color(hdcmem, DIBINDEX(i),
700 RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
701 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue),
702 RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
705 SelectObject(hdcmem, oldbm);
706 test_dib_info(hdib, bits, &pbmi->bmiHeader);
707 DeleteObject(hdib);
709 pbmi->bmiHeader.biBitCount = 1;
711 /* Now create a palette and a palette indexed dib section */
712 memset(plogpal, 0, sizeof(logpalbuf));
713 plogpal->palVersion = 0x300;
714 plogpal->palNumEntries = 2;
715 plogpal->palPalEntry[0].peRed = 0xff;
716 plogpal->palPalEntry[0].peBlue = 0xff;
717 plogpal->palPalEntry[1].peGreen = 0xff;
719 index = (WORD*)pbmi->bmiColors;
720 *index++ = 0;
721 *index = 1;
722 hpal = CreatePalette(plogpal);
723 ok(hpal != NULL, "CreatePalette failed\n");
724 oldpal = SelectPalette(hdc, hpal, TRUE);
725 hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
726 ok(hdib != NULL, "CreateDIBSection failed\n");
727 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
728 ok(dibsec.dsBmih.biClrUsed == 2 ||
729 broken(dibsec.dsBmih.biClrUsed == 0), /* win9x */
730 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
732 /* The colour table has already been grabbed from the dc, so we select back the
733 old palette */
735 SelectPalette(hdc, oldpal, TRUE);
736 oldbm = SelectObject(hdcmem, hdib);
737 oldpal = SelectPalette(hdcmem, hpal, TRUE);
739 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
740 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
741 ok(rgb[0].rgbRed == 0xff && rgb[0].rgbBlue == 0xff && rgb[0].rgbGreen == 0 &&
742 rgb[1].rgbRed == 0 && rgb[1].rgbBlue == 0 && rgb[1].rgbGreen == 0xff,
743 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
744 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
745 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
747 c0 = RGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen, plogpal->palPalEntry[0].peBlue);
748 c1 = RGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen, plogpal->palPalEntry[1].peBlue);
750 test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
751 test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
752 test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
753 test_color(hdcmem, PALETTEINDEX(0), c0, 0, 1);
754 test_color(hdcmem, PALETTEINDEX(1), c1, 0, 1);
755 test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
756 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen,
757 plogpal->palPalEntry[0].peBlue), c0, 1, 1);
758 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen,
759 plogpal->palPalEntry[1].peBlue), c1, 1, 1);
760 test_color(hdcmem, PALETTERGB(0, 0, 0), c1, 1, 1);
761 test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
762 test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c0, 1, 1);
763 test_color(hdcmem, PALETTERGB(0, 1, 0), c1, 1, 1);
764 test_color(hdcmem, PALETTERGB(0x3f, 0, 0x3f), c1, 1, 1);
765 test_color(hdcmem, PALETTERGB(0x40, 0, 0x40), c0, 1, 1);
767 /* Bottom and 2nd row from top green, everything else magenta */
768 bits[0] = bits[1] = 0xff;
769 bits[13 * 4] = bits[13*4 + 1] = 0xff;
771 test_dib_info(hdib, bits, &pbmi->bmiHeader);
773 pbmi->bmiHeader.biBitCount = 32;
775 hdib2 = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&bits32, NULL, 0);
776 ok(hdib2 != NULL, "CreateDIBSection failed\n");
777 hdcmem2 = CreateCompatibleDC(hdc);
778 oldbm2 = SelectObject(hdcmem2, hdib2);
780 BitBlt(hdcmem2, 0, 0, 16,16, hdcmem, 0, 0, SRCCOPY);
782 ok(bits32[0] == 0xff00, "lower left pixel is %08x\n", bits32[0]);
783 ok(bits32[17] == 0xff00ff ||
784 broken(bits32[17] == 0x00ff00), /* Intermittent on Win9x/ME */
785 "bottom but one, left pixel is %08x\n", bits32[17]);
787 SelectObject(hdcmem2, oldbm2);
788 test_dib_info(hdib2, bits32, &pbmi->bmiHeader);
789 DeleteObject(hdib2);
791 SelectObject(hdcmem, oldbm);
792 SelectObject(hdcmem, oldpal);
793 DeleteObject(hdib);
794 DeleteObject(hpal);
797 pbmi->bmiHeader.biBitCount = 8;
799 memset(plogpal, 0, sizeof(logpalbuf));
800 plogpal->palVersion = 0x300;
801 plogpal->palNumEntries = 256;
803 for (i = 0; i < 128; i++) {
804 plogpal->palPalEntry[i].peRed = 255 - i * 2;
805 plogpal->palPalEntry[i].peBlue = i * 2;
806 plogpal->palPalEntry[i].peGreen = 0;
807 plogpal->palPalEntry[255 - i].peRed = 0;
808 plogpal->palPalEntry[255 - i].peGreen = i * 2;
809 plogpal->palPalEntry[255 - i].peBlue = 255 - i * 2;
812 index = (WORD*)pbmi->bmiColors;
813 for (i = 0; i < 256; i++) {
814 *index++ = i;
817 hpal = CreatePalette(plogpal);
818 ok(hpal != NULL, "CreatePalette failed\n");
819 oldpal = SelectPalette(hdc, hpal, TRUE);
820 hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
821 ok(hdib != NULL, "CreateDIBSection failed\n");
822 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
823 ok(dibsec.dsBmih.biClrUsed == 256 ||
824 broken(dibsec.dsBmih.biClrUsed == 0), /* win9x */
825 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
827 test_dib_info(hdib, bits, &pbmi->bmiHeader);
829 SelectPalette(hdc, oldpal, TRUE);
830 oldbm = SelectObject(hdcmem, hdib);
831 oldpal = SelectPalette(hdcmem, hpal, TRUE);
833 ret = GetDIBColorTable(hdcmem, 0, 256, rgb);
834 ok(ret == 256, "GetDIBColorTable returned %d\n", ret);
835 for (i = 0; i < 256; i++) {
836 ok(rgb[i].rgbRed == plogpal->palPalEntry[i].peRed &&
837 rgb[i].rgbBlue == plogpal->palPalEntry[i].peBlue &&
838 rgb[i].rgbGreen == plogpal->palPalEntry[i].peGreen,
839 "GetDIBColorTable returns table %d: r%02x g%02x b%02x res%02x\n",
840 i, rgb[i].rgbRed, rgb[i].rgbGreen, rgb[i].rgbBlue, rgb[i].rgbReserved);
843 for (i = 0; i < 256; i++) {
844 test_color(hdcmem, DIBINDEX(i),
845 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
846 test_color(hdcmem, PALETTEINDEX(i),
847 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
848 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue),
849 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
852 SelectPalette(hdcmem, oldpal, TRUE);
853 SelectObject(hdcmem, oldbm);
854 DeleteObject(hdib);
855 DeleteObject(hpal);
858 DeleteDC(hdcmem);
859 ReleaseDC(0, hdc);
862 static void test_mono_dibsection(void)
864 HDC hdc, memdc;
865 HBITMAP old_bm, mono_ds;
866 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
867 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
868 BYTE bits[10 * 4];
869 BYTE *ds_bits;
870 int num;
872 hdc = GetDC(0);
874 memdc = CreateCompatibleDC(hdc);
876 memset(pbmi, 0, sizeof(bmibuf));
877 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
878 pbmi->bmiHeader.biHeight = 10;
879 pbmi->bmiHeader.biWidth = 10;
880 pbmi->bmiHeader.biBitCount = 1;
881 pbmi->bmiHeader.biPlanes = 1;
882 pbmi->bmiHeader.biCompression = BI_RGB;
883 pbmi->bmiColors[0].rgbRed = 0xff;
884 pbmi->bmiColors[0].rgbGreen = 0xff;
885 pbmi->bmiColors[0].rgbBlue = 0xff;
886 pbmi->bmiColors[1].rgbRed = 0x0;
887 pbmi->bmiColors[1].rgbGreen = 0x0;
888 pbmi->bmiColors[1].rgbBlue = 0x0;
891 * First dib section is 'inverted' ie color[0] is white, color[1] is black
894 mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
895 ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
896 old_bm = SelectObject(memdc, mono_ds);
898 /* black border, white interior */
899 Rectangle(memdc, 0, 0, 10, 10);
900 ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
901 ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
903 /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
905 memset(bits, 0, sizeof(bits));
906 bits[0] = 0xaa;
908 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
909 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
911 /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
913 pbmi->bmiColors[0].rgbRed = 0x0;
914 pbmi->bmiColors[0].rgbGreen = 0x0;
915 pbmi->bmiColors[0].rgbBlue = 0x0;
916 pbmi->bmiColors[1].rgbRed = 0xff;
917 pbmi->bmiColors[1].rgbGreen = 0xff;
918 pbmi->bmiColors[1].rgbBlue = 0xff;
920 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
921 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
923 SelectObject(memdc, old_bm);
924 DeleteObject(mono_ds);
927 * Next dib section is 'normal' ie color[0] is black, color[1] is white
930 pbmi->bmiColors[0].rgbRed = 0x0;
931 pbmi->bmiColors[0].rgbGreen = 0x0;
932 pbmi->bmiColors[0].rgbBlue = 0x0;
933 pbmi->bmiColors[1].rgbRed = 0xff;
934 pbmi->bmiColors[1].rgbGreen = 0xff;
935 pbmi->bmiColors[1].rgbBlue = 0xff;
937 mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
938 ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
939 old_bm = SelectObject(memdc, mono_ds);
941 /* black border, white interior */
942 Rectangle(memdc, 0, 0, 10, 10);
943 ok(ds_bits[0] == 0x00, "out_bits %02x\n", ds_bits[0]);
944 ok(ds_bits[4] == 0x7f, "out_bits %02x\n", ds_bits[4]);
946 /* SetDIBitsToDevice with a normal bmi -> normal dib section */
948 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
949 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
951 /* SetDIBitsToDevice with a inverted bmi -> normal dib section */
953 pbmi->bmiColors[0].rgbRed = 0xff;
954 pbmi->bmiColors[0].rgbGreen = 0xff;
955 pbmi->bmiColors[0].rgbBlue = 0xff;
956 pbmi->bmiColors[1].rgbRed = 0x0;
957 pbmi->bmiColors[1].rgbGreen = 0x0;
958 pbmi->bmiColors[1].rgbBlue = 0x0;
960 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
961 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
964 * Take that 'normal' dibsection and change its colour table to an 'inverted' one
967 pbmi->bmiColors[0].rgbRed = 0xff;
968 pbmi->bmiColors[0].rgbGreen = 0xff;
969 pbmi->bmiColors[0].rgbBlue = 0xff;
970 pbmi->bmiColors[1].rgbRed = 0x0;
971 pbmi->bmiColors[1].rgbGreen = 0x0;
972 pbmi->bmiColors[1].rgbBlue = 0x0;
973 num = SetDIBColorTable(memdc, 0, 2, pbmi->bmiColors);
974 ok(num == 2, "num = %d\n", num);
976 /* black border, white interior */
977 Rectangle(memdc, 0, 0, 10, 10);
978 todo_wine {
979 ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
980 ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
982 /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
984 memset(bits, 0, sizeof(bits));
985 bits[0] = 0xaa;
987 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
988 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
990 /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
992 pbmi->bmiColors[0].rgbRed = 0x0;
993 pbmi->bmiColors[0].rgbGreen = 0x0;
994 pbmi->bmiColors[0].rgbBlue = 0x0;
995 pbmi->bmiColors[1].rgbRed = 0xff;
996 pbmi->bmiColors[1].rgbGreen = 0xff;
997 pbmi->bmiColors[1].rgbBlue = 0xff;
999 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1000 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1002 SelectObject(memdc, old_bm);
1003 DeleteObject(mono_ds);
1006 * Now a dib section with a strange colour map just for fun. This behaves just like an inverted one.
1009 pbmi->bmiColors[0].rgbRed = 0xff;
1010 pbmi->bmiColors[0].rgbGreen = 0x0;
1011 pbmi->bmiColors[0].rgbBlue = 0x0;
1012 pbmi->bmiColors[1].rgbRed = 0xfe;
1013 pbmi->bmiColors[1].rgbGreen = 0x0;
1014 pbmi->bmiColors[1].rgbBlue = 0x0;
1016 mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
1017 ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
1018 old_bm = SelectObject(memdc, mono_ds);
1020 /* black border, white interior */
1021 Rectangle(memdc, 0, 0, 10, 10);
1022 ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
1023 ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
1025 /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
1027 pbmi->bmiColors[0].rgbRed = 0x0;
1028 pbmi->bmiColors[0].rgbGreen = 0x0;
1029 pbmi->bmiColors[0].rgbBlue = 0x0;
1030 pbmi->bmiColors[1].rgbRed = 0xff;
1031 pbmi->bmiColors[1].rgbGreen = 0xff;
1032 pbmi->bmiColors[1].rgbBlue = 0xff;
1034 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1035 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1037 /* SetDIBitsToDevice with a inverted bmi -> inverted dib section */
1039 pbmi->bmiColors[0].rgbRed = 0xff;
1040 pbmi->bmiColors[0].rgbGreen = 0xff;
1041 pbmi->bmiColors[0].rgbBlue = 0xff;
1042 pbmi->bmiColors[1].rgbRed = 0x0;
1043 pbmi->bmiColors[1].rgbGreen = 0x0;
1044 pbmi->bmiColors[1].rgbBlue = 0x0;
1046 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1047 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
1049 SelectObject(memdc, old_bm);
1050 DeleteObject(mono_ds);
1052 DeleteDC(memdc);
1053 ReleaseDC(0, hdc);
1056 static void test_bitmap(void)
1058 char buf[256], buf_cmp[256];
1059 HBITMAP hbmp, hbmp_old;
1060 HDC hdc;
1061 BITMAP bm;
1062 BITMAP bma[2];
1063 INT ret;
1065 hdc = CreateCompatibleDC(0);
1066 assert(hdc != 0);
1068 SetLastError(0xdeadbeef);
1069 hbmp = CreateBitmap(0x7ffffff, 1, 1, 1, NULL);
1070 if (!hbmp)
1072 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1073 GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1074 "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1076 else
1077 DeleteObject(hbmp);
1079 SetLastError(0xdeadbeef);
1080 hbmp = CreateBitmap(0x7ffffff, 9, 1, 1, NULL);
1081 if (!hbmp)
1083 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1084 GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1085 "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1087 else
1088 DeleteObject(hbmp);
1090 SetLastError(0xdeadbeef);
1091 hbmp = CreateBitmap(0x7ffffff + 1, 1, 1, 1, NULL);
1092 ok(!hbmp || broken(hbmp != NULL /* Win9x */), "CreateBitmap should fail\n");
1093 if (!hbmp)
1094 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1095 "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1096 else
1097 DeleteObject(hbmp);
1099 hbmp = CreateBitmap(15, 15, 1, 1, NULL);
1100 assert(hbmp != NULL);
1102 ret = GetObject(hbmp, sizeof(bm), &bm);
1103 ok(ret == sizeof(bm), "wrong size %d\n", ret);
1105 ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1106 ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1107 ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1108 ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1109 ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1110 ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1111 ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1113 assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
1114 assert(sizeof(buf) == sizeof(buf_cmp));
1116 ret = GetBitmapBits(hbmp, 0, NULL);
1117 ok(ret == bm.bmWidthBytes * bm.bmHeight || broken(ret == 0 /* Win9x */),
1118 "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1120 memset(buf_cmp, 0xAA, sizeof(buf_cmp));
1121 memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
1123 memset(buf, 0xAA, sizeof(buf));
1124 ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1125 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1126 ok(!memcmp(buf, buf_cmp, sizeof(buf)) ||
1127 broken(memcmp(buf, buf_cmp, sizeof(buf))), /* win9x doesn't init the bitmap bits */
1128 "buffers do not match\n");
1130 hbmp_old = SelectObject(hdc, hbmp);
1132 ret = GetObject(hbmp, sizeof(bm), &bm);
1133 ok(ret == sizeof(bm), "wrong size %d\n", ret);
1135 ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1136 ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1137 ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1138 ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1139 ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1140 ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1141 ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1143 memset(buf, 0xAA, sizeof(buf));
1144 ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1145 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1146 ok(!memcmp(buf, buf_cmp, sizeof(buf)) ||
1147 broken(memcmp(buf, buf_cmp, sizeof(buf))), /* win9x doesn't init the bitmap bits */
1148 "buffers do not match\n");
1150 hbmp_old = SelectObject(hdc, hbmp_old);
1151 ok(hbmp_old == hbmp, "wrong old bitmap %p\n", hbmp_old);
1153 /* test various buffer sizes for GetObject */
1154 ret = GetObject(hbmp, sizeof(*bma) * 2, bma);
1155 ok(ret == sizeof(*bma) || broken(ret == sizeof(*bma) * 2 /* Win9x */), "wrong size %d\n", ret);
1157 ret = GetObject(hbmp, sizeof(bm) / 2, &bm);
1158 ok(ret == 0 || broken(ret == sizeof(bm) / 2 /* Win9x */), "%d != 0\n", ret);
1160 ret = GetObject(hbmp, 0, &bm);
1161 ok(ret == 0, "%d != 0\n", ret);
1163 ret = GetObject(hbmp, 1, &bm);
1164 ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
1166 DeleteObject(hbmp);
1167 DeleteDC(hdc);
1170 static void test_bmBits(void)
1172 BYTE bits[4];
1173 HBITMAP hbmp;
1174 BITMAP bmp;
1176 memset(bits, 0, sizeof(bits));
1177 hbmp = CreateBitmap(2, 2, 1, 4, bits);
1178 ok(hbmp != NULL, "CreateBitmap failed\n");
1180 memset(&bmp, 0xFF, sizeof(bmp));
1181 ok(GetObject(hbmp, sizeof(bmp), &bmp) == sizeof(bmp),
1182 "GetObject failed or returned a wrong structure size\n");
1183 ok(!bmp.bmBits, "bmBits must be NULL for device-dependent bitmaps\n");
1185 DeleteObject(hbmp);
1188 static void test_GetDIBits_selected_DIB(UINT bpp)
1190 HBITMAP dib;
1191 BITMAPINFO * info;
1192 BITMAPINFO * info2;
1193 void * bits;
1194 void * bits2;
1195 UINT dib_size;
1196 HDC dib_dc, dc;
1197 HBITMAP old_bmp;
1198 BOOL equalContents;
1199 UINT i;
1200 int res;
1202 /* Create a DIB section with a color table */
1204 info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1205 info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1206 assert(info);
1207 assert(info2);
1209 info->bmiHeader.biSize = sizeof(info->bmiHeader);
1211 /* Choose width and height such that the row length (in bytes)
1212 is a multiple of 4 (makes things easier) */
1213 info->bmiHeader.biWidth = 32;
1214 info->bmiHeader.biHeight = 32;
1215 info->bmiHeader.biPlanes = 1;
1216 info->bmiHeader.biBitCount = bpp;
1217 info->bmiHeader.biCompression = BI_RGB;
1219 for (i=0; i < (1u << bpp); i++)
1221 BYTE c = i * (1 << (8 - bpp));
1222 info->bmiColors[i].rgbRed = c;
1223 info->bmiColors[i].rgbGreen = c;
1224 info->bmiColors[i].rgbBlue = c;
1225 info->bmiColors[i].rgbReserved = 0;
1228 dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
1229 assert(dib);
1230 dib_size = bpp * (info->bmiHeader.biWidth * info->bmiHeader.biHeight) / 8;
1232 /* Set the bits of the DIB section */
1233 for (i=0; i < dib_size; i++)
1235 ((BYTE *)bits)[i] = i % 256;
1238 /* Select the DIB into a DC */
1239 dib_dc = CreateCompatibleDC(NULL);
1240 old_bmp = SelectObject(dib_dc, dib);
1241 dc = CreateCompatibleDC(NULL);
1242 bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dib_size);
1243 assert(bits2);
1245 /* Copy the DIB attributes but not the color table */
1246 memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1248 res = GetDIBits(dc, dib, 0, info->bmiHeader.biHeight, bits2, info2, DIB_RGB_COLORS);
1249 ok(res, "GetDIBits failed\n");
1251 /* Compare the color table and the bits */
1252 equalContents = TRUE;
1253 for (i=0; i < (1u << bpp); i++)
1255 if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1256 || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1257 || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1258 || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1260 equalContents = FALSE;
1261 break;
1264 ok(equalContents, "GetDIBits with DIB selected in DC: Invalid DIB color table\n");
1266 equalContents = TRUE;
1267 for (i=0; i < dib_size / sizeof(DWORD); i++)
1269 if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1271 equalContents = FALSE;
1272 break;
1275 ok(equalContents, "GetDIBits with %d bpp DIB selected in DC: Invalid DIB bits\n",bpp);
1277 HeapFree(GetProcessHeap(), 0, bits2);
1278 DeleteDC(dc);
1280 SelectObject(dib_dc, old_bmp);
1281 DeleteDC(dib_dc);
1282 DeleteObject(dib);
1284 HeapFree(GetProcessHeap(), 0, info2);
1285 HeapFree(GetProcessHeap(), 0, info);
1288 static void test_GetDIBits_selected_DDB(BOOL monochrome)
1290 HBITMAP ddb;
1291 BITMAPINFO * info;
1292 BITMAPINFO * info2;
1293 void * bits;
1294 void * bits2;
1295 HDC ddb_dc, dc;
1296 HBITMAP old_bmp;
1297 BOOL equalContents;
1298 UINT width, height;
1299 UINT bpp;
1300 UINT i, j;
1301 int res;
1303 width = height = 16;
1305 /* Create a DDB (device-dependent bitmap) */
1306 if (monochrome)
1308 bpp = 1;
1309 ddb = CreateBitmap(width, height, 1, 1, NULL);
1311 else
1313 HDC screen_dc = GetDC(NULL);
1314 bpp = GetDeviceCaps(screen_dc, BITSPIXEL) * GetDeviceCaps(screen_dc, PLANES);
1315 ddb = CreateCompatibleBitmap(screen_dc, width, height);
1316 ReleaseDC(NULL, screen_dc);
1319 /* Set the pixels */
1320 ddb_dc = CreateCompatibleDC(NULL);
1321 old_bmp = SelectObject(ddb_dc, ddb);
1322 for (i = 0; i < width; i++)
1324 for (j=0; j < height; j++)
1326 BYTE c = (i * width + j) % 256;
1327 SetPixelV(ddb_dc, i, j, RGB(c, c, c));
1330 SelectObject(ddb_dc, old_bmp);
1332 info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1333 info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1334 assert(info);
1335 assert(info2);
1337 info->bmiHeader.biSize = sizeof(info->bmiHeader);
1338 info->bmiHeader.biWidth = width;
1339 info->bmiHeader.biHeight = height;
1340 info->bmiHeader.biPlanes = 1;
1341 info->bmiHeader.biBitCount = bpp;
1342 info->bmiHeader.biCompression = BI_RGB;
1344 dc = CreateCompatibleDC(NULL);
1346 /* Fill in biSizeImage */
1347 GetDIBits(dc, ddb, 0, height, NULL, info, DIB_RGB_COLORS);
1348 ok(info->bmiHeader.biSizeImage != 0, "GetDIBits failed to get the DIB attributes\n");
1350 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1351 bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1352 assert(bits);
1353 assert(bits2);
1355 /* Get the bits */
1356 res = GetDIBits(dc, ddb, 0, height, bits, info, DIB_RGB_COLORS);
1357 ok(res, "GetDIBits failed\n");
1359 /* Copy the DIB attributes but not the color table */
1360 memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1362 /* Select the DDB into another DC */
1363 old_bmp = SelectObject(ddb_dc, ddb);
1365 /* Get the bits */
1366 res = GetDIBits(dc, ddb, 0, height, bits2, info2, DIB_RGB_COLORS);
1367 ok(res, "GetDIBits failed\n");
1369 /* Compare the color table and the bits */
1370 if (bpp <= 8)
1372 equalContents = TRUE;
1373 for (i=0; i < (1u << bpp); i++)
1375 if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1376 || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1377 || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1378 || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1380 equalContents = FALSE;
1381 break;
1384 ok(equalContents, "GetDIBits with DDB selected in DC: Got a different color table\n");
1387 equalContents = TRUE;
1388 for (i=0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++)
1390 if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1392 equalContents = FALSE;
1395 ok(equalContents, "GetDIBits with DDB selected in DC: Got different DIB bits\n");
1397 /* Test the palette */
1398 equalContents = TRUE;
1399 if (info2->bmiHeader.biBitCount <= 8)
1401 WORD *colors = (WORD*)info2->bmiColors;
1403 /* Get the palette indices */
1404 res = GetDIBits(dc, ddb, 0, 0, NULL, info2, DIB_PAL_COLORS);
1405 if (res == 0 && GetLastError() == ERROR_INVALID_PARAMETER) /* Win9x */
1406 res = GetDIBits(dc, ddb, 0, height, NULL, info2, DIB_PAL_COLORS);
1407 ok(res, "GetDIBits failed\n");
1409 for (i=0;i < 1 << info->bmiHeader.biSizeImage; i++)
1411 if (colors[i] != i)
1413 equalContents = FALSE;
1414 break;
1419 ok(equalContents, "GetDIBits with DDB selected in DC: non 1:1 palette indices\n");
1421 HeapFree(GetProcessHeap(), 0, bits2);
1422 HeapFree(GetProcessHeap(), 0, bits);
1423 DeleteDC(dc);
1425 SelectObject(ddb_dc, old_bmp);
1426 DeleteDC(ddb_dc);
1427 DeleteObject(ddb);
1429 HeapFree(GetProcessHeap(), 0, info2);
1430 HeapFree(GetProcessHeap(), 0, info);
1433 static void test_GetDIBits(void)
1435 /* 2-bytes aligned 1-bit bitmap data: 16x16 */
1436 static const BYTE bmp_bits_1[16 * 2] =
1438 0xff,0xff, 0,0, 0xff,0xff, 0,0,
1439 0xff,0xff, 0,0, 0xff,0xff, 0,0,
1440 0xff,0xff, 0,0, 0xff,0xff, 0,0,
1441 0xff,0xff, 0,0, 0xff,0xff, 0,0
1443 /* 4-bytes aligned 1-bit DIB data: 16x16 */
1444 static const BYTE dib_bits_1[16 * 4] =
1446 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1447 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1448 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1449 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0
1451 static const BYTE dib_bits_1_9x[16 * 4] =
1453 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa,
1454 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa,
1455 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa,
1456 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa
1458 /* 2-bytes aligned 24-bit bitmap data: 16x16 */
1459 static const BYTE bmp_bits_24[16 * 16*3] =
1461 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1462 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1463 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1464 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1465 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1466 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1467 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1468 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1469 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1470 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1471 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1472 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1473 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1474 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1475 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1476 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1477 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1478 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1479 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1480 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1481 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1482 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1483 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1484 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1485 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1486 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1487 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1488 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1489 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1490 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1491 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1492 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1494 /* 4-bytes aligned 24-bit DIB data: 16x16 */
1495 static const BYTE dib_bits_24[16 * 16*3] =
1497 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1498 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1499 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1500 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1501 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1502 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1503 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1504 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1505 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1506 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1507 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1508 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1509 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1510 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1511 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1512 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1513 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1514 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1515 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1516 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1517 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1518 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1519 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1520 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1521 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1522 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1523 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1524 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1525 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1526 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1527 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1528 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
1530 HBITMAP hbmp;
1531 BITMAP bm;
1532 HDC hdc;
1533 int i, bytes, lines;
1534 BYTE buf[1024];
1535 char bi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
1536 BITMAPINFO *bi = (BITMAPINFO *)bi_buf;
1538 hdc = GetDC(0);
1540 /* 1-bit source bitmap data */
1541 hbmp = CreateBitmap(16, 16, 1, 1, bmp_bits_1);
1542 ok(hbmp != 0, "CreateBitmap failed\n");
1544 memset(&bm, 0xAA, sizeof(bm));
1545 bytes = GetObject(hbmp, sizeof(bm), &bm);
1546 ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1547 ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
1548 ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1549 ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1550 ok(bm.bmWidthBytes == 2, "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1551 ok(bm.bmPlanes == 1, "wrong bmPlanes %u\n", bm.bmPlanes);
1552 ok(bm.bmBitsPixel == 1, "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1553 ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1555 bytes = GetBitmapBits(hbmp, 0, NULL);
1556 ok(bytes == sizeof(bmp_bits_1) || broken(bytes == 0 /* Win9x */), "expected 16*2 got %d bytes\n", bytes);
1557 bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1558 ok(bytes == sizeof(bmp_bits_1), "expected 16*2 got %d bytes\n", bytes);
1559 ok(!memcmp(buf, bmp_bits_1, sizeof(bmp_bits_1)), "bitmap bits don't match\n");
1561 /* retrieve 1-bit DIB data */
1562 memset(bi, 0, sizeof(*bi));
1563 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1564 bi->bmiHeader.biWidth = bm.bmWidth;
1565 bi->bmiHeader.biHeight = bm.bmHeight;
1566 bi->bmiHeader.biPlanes = 1;
1567 bi->bmiHeader.biBitCount = 1;
1568 bi->bmiHeader.biCompression = BI_RGB;
1569 bi->bmiHeader.biSizeImage = 0;
1570 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1571 SetLastError(0xdeadbeef);
1572 lines = GetDIBits(0, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1573 ok(lines == 0, "GetDIBits copied %d lines with hdc = 0\n", lines);
1574 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
1575 broken(GetLastError() == 0xdeadbeef), /* winnt */
1576 "wrong error %u\n", GetLastError());
1577 ok(bi->bmiHeader.biSizeImage == 0, "expected 0, got %u\n", bi->bmiHeader.biSizeImage);
1579 memset(buf, 0xAA, sizeof(buf));
1580 SetLastError(0xdeadbeef);
1581 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1582 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1583 lines, bm.bmHeight, GetLastError());
1584 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1) ||
1585 broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1586 "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1588 /* the color table consists of black and white */
1589 ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1590 bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1591 "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1592 bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1593 bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1594 ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1595 bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1596 "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1597 bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1598 bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1599 for (i = 2; i < 256; i++)
1601 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1602 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1603 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1604 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1605 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1608 /* returned bits are DWORD aligned and upside down */
1609 ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)) ||
1610 broken(!memcmp(buf, dib_bits_1_9x, sizeof(dib_bits_1_9x))), /* Win9x, WinME */
1611 "DIB bits don't match\n");
1613 /* Test the palette indices */
1614 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1615 SetLastError(0xdeadbeef);
1616 lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1617 if (lines == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
1618 win_skip("Win9x/WinMe doesn't handle 0 for the number of scan lines\n");
1619 else
1621 ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1622 ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1624 for (i = 2; i < 256; i++)
1625 ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[1]);
1627 /* retrieve 24-bit DIB data */
1628 memset(bi, 0, sizeof(*bi));
1629 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1630 bi->bmiHeader.biWidth = bm.bmWidth;
1631 bi->bmiHeader.biHeight = bm.bmHeight;
1632 bi->bmiHeader.biPlanes = 1;
1633 bi->bmiHeader.biBitCount = 24;
1634 bi->bmiHeader.biCompression = BI_RGB;
1635 bi->bmiHeader.biSizeImage = 0;
1636 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1637 memset(buf, 0xAA, sizeof(buf));
1638 SetLastError(0xdeadbeef);
1639 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1640 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1641 lines, bm.bmHeight, GetLastError());
1642 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24) ||
1643 broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1644 "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1646 /* the color table doesn't exist for 24-bit images */
1647 for (i = 0; i < 256; i++)
1649 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1650 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1651 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1652 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1653 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1656 /* returned bits are DWORD aligned and upside down */
1657 ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1658 DeleteObject(hbmp);
1660 /* 24-bit source bitmap data */
1661 hbmp = CreateCompatibleBitmap(hdc, 16, 16);
1662 ok(hbmp != 0, "CreateBitmap failed\n");
1663 SetLastError(0xdeadbeef);
1664 bi->bmiHeader.biHeight = -bm.bmHeight; /* indicate bottom-up data */
1665 lines = SetDIBits(hdc, hbmp, 0, bm.bmHeight, bmp_bits_24, bi, DIB_RGB_COLORS);
1666 ok(lines == bm.bmHeight, "SetDIBits copied %d lines of %d, error %u\n",
1667 lines, bm.bmHeight, GetLastError());
1669 memset(&bm, 0xAA, sizeof(bm));
1670 bytes = GetObject(hbmp, sizeof(bm), &bm);
1671 ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1672 ok(bm.bmType == 0 ||
1673 broken(bm.bmType == 21072), /* win9x */
1674 "wrong bmType %d\n", bm.bmType);
1675 ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1676 ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1677 ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1678 ok(bm.bmPlanes == GetDeviceCaps(hdc, PLANES), "wrong bmPlanes %u\n", bm.bmPlanes);
1679 ok(bm.bmBitsPixel == GetDeviceCaps(hdc, BITSPIXEL), "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1680 ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1682 bytes = GetBitmapBits(hbmp, 0, NULL);
1683 ok(bytes == bm.bmWidthBytes * bm.bmHeight ||
1684 broken(bytes == 0), /* win9x */
1685 "expected %d got %d bytes\n", bm.bmWidthBytes * bm.bmHeight, bytes);
1686 bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1687 ok(bytes == bm.bmWidthBytes * bm.bmHeight, "expected %d got %d bytes\n",
1688 bm.bmWidthBytes * bm.bmHeight, bytes);
1690 /* retrieve 1-bit DIB data */
1691 memset(bi, 0, sizeof(*bi));
1692 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1693 bi->bmiHeader.biWidth = bm.bmWidth;
1694 bi->bmiHeader.biHeight = bm.bmHeight;
1695 bi->bmiHeader.biPlanes = 1;
1696 bi->bmiHeader.biBitCount = 1;
1697 bi->bmiHeader.biCompression = BI_RGB;
1698 bi->bmiHeader.biSizeImage = 0;
1699 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1700 memset(buf, 0xAA, sizeof(buf));
1701 SetLastError(0xdeadbeef);
1702 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1703 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1704 lines, bm.bmHeight, GetLastError());
1705 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1) ||
1706 broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1707 "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1709 /* the color table consists of black and white */
1710 ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1711 bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1712 "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1713 bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1714 bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1715 ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1716 bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1717 "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1718 bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1719 bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1720 for (i = 2; i < 256; i++)
1722 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1723 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1724 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1725 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1726 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1729 /* returned bits are DWORD aligned and upside down */
1730 todo_wine
1731 ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)) ||
1732 broken(!memcmp(buf, dib_bits_1_9x, sizeof(dib_bits_1_9x))), /* Win9x, WinME */
1733 "DIB bits don't match\n");
1735 /* Test the palette indices */
1736 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1737 SetLastError(0xdeadbeef);
1738 lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1739 if (lines == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
1740 win_skip("Win9x/WinMe doesn't handle 0 for the number of scan lines\n");
1741 else
1743 ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1744 ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1746 for (i = 2; i < 256; i++)
1747 ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[i]);
1749 /* retrieve 24-bit DIB data */
1750 memset(bi, 0, sizeof(*bi));
1751 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1752 bi->bmiHeader.biWidth = bm.bmWidth;
1753 bi->bmiHeader.biHeight = bm.bmHeight;
1754 bi->bmiHeader.biPlanes = 1;
1755 bi->bmiHeader.biBitCount = 24;
1756 bi->bmiHeader.biCompression = BI_RGB;
1757 bi->bmiHeader.biSizeImage = 0;
1758 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1759 memset(buf, 0xAA, sizeof(buf));
1760 SetLastError(0xdeadbeef);
1761 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1762 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1763 lines, bm.bmHeight, GetLastError());
1764 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24) ||
1765 broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1766 "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1768 /* the color table doesn't exist for 24-bit images */
1769 for (i = 0; i < 256; i++)
1771 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1772 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1773 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1774 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1775 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1778 /* returned bits are DWORD aligned and upside down */
1779 ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1780 DeleteObject(hbmp);
1782 ReleaseDC(0, hdc);
1785 static void test_GetDIBits_BI_BITFIELDS(void)
1787 /* Try a screen resolution detection technique
1788 * from the September 1999 issue of Windows Developer's Journal
1789 * which seems to be in widespread use.
1790 * http://www.lesher.ws/highcolor.html
1791 * http://www.lesher.ws/vidfmt.c
1792 * It hinges on being able to retrieve the bitmaps
1793 * for the three primary colors in non-paletted 16 bit mode.
1795 char dibinfo_buf[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
1796 DWORD bits[32];
1797 LPBITMAPINFO dibinfo = (LPBITMAPINFO) dibinfo_buf;
1798 HDC hdc;
1799 HBITMAP hbm;
1800 int ret;
1802 memset(dibinfo, 0, sizeof(dibinfo_buf));
1803 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1805 hdc = GetDC(NULL);
1806 ok(hdc != NULL, "GetDC failed?\n");
1807 hbm = CreateCompatibleBitmap(hdc, 1, 1);
1808 ok(hbm != NULL, "CreateCompatibleBitmap failed?\n");
1810 /* Call GetDIBits to fill in bmiHeader. */
1811 ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1812 ok(ret == 1, "GetDIBits failed\n");
1813 if (dibinfo->bmiHeader.biBitCount > 8)
1815 DWORD *bitmasks = (DWORD *)dibinfo->bmiColors;
1817 ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS,
1818 "compression is %u\n", dibinfo->bmiHeader.biCompression );
1820 ok( !bitmasks[0], "red mask is set\n" );
1821 ok( !bitmasks[1], "green mask is set\n" );
1822 ok( !bitmasks[2], "blue mask is set\n" );
1824 /* test with NULL bits pointer and correct bpp */
1825 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1826 ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1827 ok(ret == 1, "GetDIBits failed\n");
1829 ok( bitmasks[0] != 0, "red mask is not set\n" );
1830 ok( bitmasks[1] != 0, "green mask is not set\n" );
1831 ok( bitmasks[2] != 0, "blue mask is not set\n" );
1832 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1834 /* test with valid bits pointer */
1835 memset(dibinfo, 0, sizeof(dibinfo_buf));
1836 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1837 ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1838 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1839 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1840 ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1841 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1843 ok( bitmasks[0] != 0, "red mask is not set\n" );
1844 ok( bitmasks[1] != 0, "green mask is not set\n" );
1845 ok( bitmasks[2] != 0, "blue mask is not set\n" );
1846 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef ||
1847 broken(dibinfo->bmiHeader.biSizeImage == 0xdeadbeef), /* win9x */
1848 "size image not set\n" );
1850 /* now with bits and 0 lines */
1851 memset(dibinfo, 0, sizeof(dibinfo_buf));
1852 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1853 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1854 SetLastError(0xdeadbeef);
1855 ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1856 if (ret == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
1857 win_skip("Win9x/WinMe doesn't handle 0 for the number of scan lines\n");
1858 else
1860 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1862 ok( !bitmasks[0], "red mask is set\n" );
1863 ok( !bitmasks[1], "green mask is set\n" );
1864 ok( !bitmasks[2], "blue mask is set\n" );
1865 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1867 memset(bitmasks, 0, 3*sizeof(DWORD));
1868 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1869 ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1870 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1872 ok( bitmasks[0] != 0, "red mask is not set\n" );
1873 ok( bitmasks[1] != 0, "green mask is not set\n" );
1874 ok( bitmasks[2] != 0, "blue mask is not set\n" );
1875 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1878 else skip("not in 16 bpp BI_BITFIELDS mode, skipping that test\n");
1880 DeleteObject(hbm);
1881 ReleaseDC(NULL, hdc);
1884 static void test_select_object(void)
1886 HDC hdc;
1887 HBITMAP hbm, hbm_old;
1888 INT planes, bpp, i;
1889 DWORD depths[] = {8, 15, 16, 24, 32};
1890 BITMAP bm;
1891 DWORD bytes;
1893 hdc = GetDC(0);
1894 ok(hdc != 0, "GetDC(0) failed\n");
1895 hbm = CreateCompatibleBitmap(hdc, 10, 10);
1896 ok(hbm != 0, "CreateCompatibleBitmap failed\n");
1898 hbm_old = SelectObject(hdc, hbm);
1899 ok(hbm_old == 0, "SelectObject should fail\n");
1901 DeleteObject(hbm);
1902 ReleaseDC(0, hdc);
1904 hdc = CreateCompatibleDC(0);
1905 ok(hdc != 0, "GetDC(0) failed\n");
1906 hbm = CreateCompatibleBitmap(hdc, 10, 10);
1907 ok(hbm != 0, "CreateCompatibleBitmap failed\n");
1909 hbm_old = SelectObject(hdc, hbm);
1910 ok(hbm_old != 0, "SelectObject failed\n");
1911 hbm_old = SelectObject(hdc, hbm_old);
1912 ok(hbm_old == hbm, "SelectObject failed\n");
1914 DeleteObject(hbm);
1916 /* test an 1-bpp bitmap */
1917 planes = GetDeviceCaps(hdc, PLANES);
1918 bpp = 1;
1920 hbm = CreateBitmap(10, 10, planes, bpp, NULL);
1921 ok(hbm != 0, "CreateBitmap failed\n");
1923 hbm_old = SelectObject(hdc, hbm);
1924 ok(hbm_old != 0, "SelectObject failed\n");
1925 hbm_old = SelectObject(hdc, hbm_old);
1926 ok(hbm_old == hbm, "SelectObject failed\n");
1928 DeleteObject(hbm);
1930 for(i = 0; i < sizeof(depths)/sizeof(depths[0]); i++) {
1931 /* test a color bitmap to dc bpp matching */
1932 planes = GetDeviceCaps(hdc, PLANES);
1933 bpp = GetDeviceCaps(hdc, BITSPIXEL);
1935 hbm = CreateBitmap(10, 10, planes, depths[i], NULL);
1936 ok(hbm != 0, "CreateBitmap failed\n");
1938 hbm_old = SelectObject(hdc, hbm);
1939 if(depths[i] == bpp ||
1940 (bpp == 16 && depths[i] == 15) /* 16 and 15 bpp are compatible */
1942 ok(hbm_old != 0, "SelectObject failed, BITSPIXEL: %d, created depth: %d\n", bpp, depths[i]);
1943 SelectObject(hdc, hbm_old);
1944 } else {
1945 ok(hbm_old == 0, "SelectObject should fail. BITSPIXELS: %d, created depth: %d\n", bpp, depths[i]);
1948 memset(&bm, 0xAA, sizeof(bm));
1949 bytes = GetObject(hbm, sizeof(bm), &bm);
1950 ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1951 ok(bm.bmType == 0 ||
1952 broken(bm.bmType == 21072), /* win9x */
1953 "wrong bmType %d\n", bm.bmType);
1954 ok(bm.bmWidth == 10, "wrong bmWidth %d\n", bm.bmWidth);
1955 ok(bm.bmHeight == 10, "wrong bmHeight %d\n", bm.bmHeight);
1956 ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1957 ok(bm.bmPlanes == planes, "wrong bmPlanes %u\n", bm.bmPlanes);
1958 if(depths[i] == 15) {
1959 ok(bm.bmBitsPixel == 16 ||
1960 broken(bm.bmBitsPixel == 15), /* Win9x/WinME */
1961 "wrong bmBitsPixel %d(15 bpp special)\n", bm.bmBitsPixel);
1962 } else {
1963 ok(bm.bmBitsPixel == depths[i], "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1965 ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1967 DeleteObject(hbm);
1970 DeleteDC(hdc);
1973 static void test_mono_1x1_bmp_dbg(HBITMAP hbmp, int line)
1975 INT ret;
1976 BITMAP bm;
1978 ret = GetObjectType(hbmp);
1979 ok_(__FILE__, line)(ret == OBJ_BITMAP, "the object %p is not bitmap\n", hbmp);
1981 ret = GetObject(hbmp, 0, 0);
1982 ok_(__FILE__, line)(ret == sizeof(BITMAP) /* XP */ ||
1983 ret == sizeof(DIBSECTION) /* Win9x */, "object size %d\n", ret);
1985 memset(&bm, 0xDA, sizeof(bm));
1986 SetLastError(0xdeadbeef);
1987 ret = GetObject(hbmp, sizeof(bm), &bm);
1988 if (!ret) /* XP, only for curObj2 */ return;
1989 ok_(__FILE__, line)(ret == sizeof(BITMAP) ||
1990 ret == sizeof(DIBSECTION) /* Win9x, only for curObj2 */,
1991 "GetObject returned %d, error %u\n", ret, GetLastError());
1992 ok_(__FILE__, line)(bm.bmType == 0, "wrong bmType, expected 0 got %d\n", bm.bmType);
1993 ok_(__FILE__, line)(bm.bmWidth == 1, "wrong bmWidth, expected 1 got %d\n", bm.bmWidth);
1994 ok_(__FILE__, line)(bm.bmHeight == 1, "wrong bmHeight, expected 1 got %d\n", bm.bmHeight);
1995 ok_(__FILE__, line)(bm.bmWidthBytes == 2, "wrong bmWidthBytes, expected 2 got %d\n", bm.bmWidthBytes);
1996 ok_(__FILE__, line)(bm.bmPlanes == 1, "wrong bmPlanes, expected 1 got %u\n", bm.bmPlanes);
1997 ok_(__FILE__, line)(bm.bmBitsPixel == 1, "wrong bmBitsPixel, expected 1 got %d\n", bm.bmBitsPixel);
1998 ok_(__FILE__, line)(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
2001 #define test_mono_1x1_bmp(a) test_mono_1x1_bmp_dbg((a), __LINE__)
2003 static void test_CreateBitmap(void)
2005 BITMAP bmp;
2006 HDC screenDC = GetDC(0);
2007 HDC hdc = CreateCompatibleDC(screenDC);
2008 UINT i, expect = 0;
2010 /* all of these are the stock monochrome bitmap */
2011 HBITMAP bm = CreateCompatibleBitmap(hdc, 0, 0);
2012 HBITMAP bm1 = CreateCompatibleBitmap(screenDC, 0, 0);
2013 HBITMAP bm4 = CreateBitmap(0, 1, 0, 0, 0);
2014 HBITMAP bm5 = CreateDiscardableBitmap(hdc, 0, 0);
2015 HBITMAP curObj1 = GetCurrentObject(hdc, OBJ_BITMAP);
2016 HBITMAP curObj2 = GetCurrentObject(screenDC, OBJ_BITMAP);
2018 /* these 2 are not the stock monochrome bitmap */
2019 HBITMAP bm2 = CreateCompatibleBitmap(hdc, 1, 1);
2020 HBITMAP bm3 = CreateBitmap(1, 1, 1, 1, 0);
2022 HBITMAP old1 = SelectObject(hdc, bm2);
2023 HBITMAP old2 = SelectObject(screenDC, bm3);
2024 SelectObject(hdc, old1);
2025 SelectObject(screenDC, old2);
2027 ok(bm == bm1 && bm == bm4 && bm == bm5 && bm == curObj1 && bm == old1,
2028 "0: %p, 1: %p, 4: %p, 5: %p, curObj1 %p, old1 %p\n",
2029 bm, bm1, bm4, bm5, curObj1, old1);
2030 ok(bm != bm2 && bm != bm3, "0: %p, 2: %p, 3: %p\n", bm, bm2, bm3);
2031 todo_wine
2032 ok(bm != curObj2 || /* WinXP */
2033 broken(bm == curObj2) /* Win9x */,
2034 "0: %p, curObj2 %p\n", bm, curObj2);
2035 ok(old2 == 0, "old2 %p\n", old2);
2037 test_mono_1x1_bmp(bm);
2038 test_mono_1x1_bmp(bm1);
2039 test_mono_1x1_bmp(bm2);
2040 test_mono_1x1_bmp(bm3);
2041 test_mono_1x1_bmp(bm4);
2042 test_mono_1x1_bmp(bm5);
2043 test_mono_1x1_bmp(old1);
2044 test_mono_1x1_bmp(curObj1);
2046 DeleteObject(bm);
2047 DeleteObject(bm1);
2048 DeleteObject(bm2);
2049 DeleteObject(bm3);
2050 DeleteObject(bm4);
2051 DeleteObject(bm5);
2053 DeleteDC(hdc);
2054 ReleaseDC(0, screenDC);
2056 /* show that Windows ignores the provided bm.bmWidthBytes */
2057 bmp.bmType = 0;
2058 bmp.bmWidth = 1;
2059 bmp.bmHeight = 1;
2060 bmp.bmWidthBytes = 28;
2061 bmp.bmPlanes = 1;
2062 bmp.bmBitsPixel = 1;
2063 bmp.bmBits = NULL;
2064 bm = CreateBitmapIndirect(&bmp);
2065 ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2066 test_mono_1x1_bmp(bm);
2067 DeleteObject(bm);
2069 /* Test how the bmBitsPixel field is treated */
2070 for(i = 1; i <= 33; i++) {
2071 bmp.bmType = 0;
2072 bmp.bmWidth = 1;
2073 bmp.bmHeight = 1;
2074 bmp.bmWidthBytes = 28;
2075 bmp.bmPlanes = 1;
2076 bmp.bmBitsPixel = i;
2077 bmp.bmBits = NULL;
2078 SetLastError(0xdeadbeef);
2079 bm = CreateBitmapIndirect(&bmp);
2080 if(i > 32) {
2081 DWORD error = GetLastError();
2082 ok(bm == 0 ||
2083 broken(bm != 0), /* Win9x and WinMe */
2084 "CreateBitmapIndirect for %d bpp succeeded\n", i);
2085 ok(error == ERROR_INVALID_PARAMETER ||
2086 broken(error == 0xdeadbeef), /* Win9x and WinME */
2087 "Got error %d, expected ERROR_INVALID_PARAMETER\n", error);
2088 DeleteObject(bm);
2089 continue;
2091 ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2092 GetObject(bm, sizeof(bmp), &bmp);
2093 if(i == 1) {
2094 expect = 1;
2095 } else if(i <= 4) {
2096 expect = 4;
2097 } else if(i <= 8) {
2098 expect = 8;
2099 } else if(i <= 16) {
2100 expect = 16;
2101 } else if(i <= 24) {
2102 expect = 24;
2103 } else if(i <= 32) {
2104 expect = 32;
2106 ok(bmp.bmBitsPixel == expect ||
2107 broken(bmp.bmBitsPixel == i), /* Win9x and WinMe */
2108 "CreateBitmapIndirect for a %d bpp bitmap created a %d bpp bitmap, expected %d\n",
2109 i, bmp.bmBitsPixel, expect);
2110 DeleteObject(bm);
2114 static void test_bitmapinfoheadersize(void)
2116 HBITMAP hdib;
2117 BITMAPINFO bmi;
2118 BITMAPCOREINFO bci;
2119 HDC hdc = GetDC(0);
2121 memset(&bmi, 0, sizeof(BITMAPINFO));
2122 bmi.bmiHeader.biHeight = 100;
2123 bmi.bmiHeader.biWidth = 512;
2124 bmi.bmiHeader.biBitCount = 24;
2125 bmi.bmiHeader.biPlanes = 1;
2127 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER) - 1;
2129 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2130 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2132 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2134 SetLastError(0xdeadbeef);
2135 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2136 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2137 DeleteObject(hdib);
2139 bmi.bmiHeader.biSize++;
2141 SetLastError(0xdeadbeef);
2142 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2143 ok(hdib != NULL ||
2144 broken(!hdib), /* Win98, WinMe */
2145 "CreateDIBSection error %d\n", GetLastError());
2146 DeleteObject(hdib);
2148 bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
2150 SetLastError(0xdeadbeef);
2151 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2152 ok(hdib != NULL ||
2153 broken(!hdib), /* Win98, WinMe */
2154 "CreateDIBSection error %d\n", GetLastError());
2155 DeleteObject(hdib);
2157 bmi.bmiHeader.biSize++;
2159 SetLastError(0xdeadbeef);
2160 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2161 ok(hdib != NULL ||
2162 broken(!hdib), /* Win98, WinMe */
2163 "CreateDIBSection error %d\n", GetLastError());
2164 DeleteObject(hdib);
2166 bmi.bmiHeader.biSize = sizeof(BITMAPV4HEADER);
2168 SetLastError(0xdeadbeef);
2169 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2170 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2171 DeleteObject(hdib);
2173 bmi.bmiHeader.biSize = sizeof(BITMAPV5HEADER);
2175 SetLastError(0xdeadbeef);
2176 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2177 ok(hdib != NULL ||
2178 broken(!hdib), /* Win95 */
2179 "CreateDIBSection error %d\n", GetLastError());
2180 DeleteObject(hdib);
2182 memset(&bci, 0, sizeof(BITMAPCOREINFO));
2183 bci.bmciHeader.bcHeight = 100;
2184 bci.bmciHeader.bcWidth = 512;
2185 bci.bmciHeader.bcBitCount = 24;
2186 bci.bmciHeader.bcPlanes = 1;
2188 bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER) - 1;
2190 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2191 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2193 bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
2195 SetLastError(0xdeadbeef);
2196 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2197 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2198 DeleteObject(hdib);
2200 bci.bmciHeader.bcSize++;
2202 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2203 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2205 bci.bmciHeader.bcSize = sizeof(BITMAPCOREINFO);
2207 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2208 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2210 ReleaseDC(0, hdc);
2213 static void test_get16dibits(void)
2215 BYTE bits[4 * (16 / sizeof(BYTE))];
2216 HBITMAP hbmp;
2217 HDC screen_dc = GetDC(NULL);
2218 int ret;
2219 BITMAPINFO * info;
2220 int info_len = sizeof(BITMAPINFOHEADER) + 1024;
2221 BYTE *p;
2222 int overwritten_bytes = 0;
2224 memset(bits, 0, sizeof(bits));
2225 hbmp = CreateBitmap(2, 2, 1, 16, bits);
2226 ok(hbmp != NULL, "CreateBitmap failed\n");
2228 info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info_len);
2229 assert(info);
2231 memset(info, '!', info_len);
2232 memset(info, 0, sizeof(info->bmiHeader));
2234 info->bmiHeader.biSize = sizeof(info->bmiHeader);
2235 info->bmiHeader.biWidth = 2;
2236 info->bmiHeader.biHeight = 2;
2237 info->bmiHeader.biPlanes = 1;
2238 info->bmiHeader.biCompression = BI_RGB;
2240 ret = GetDIBits(screen_dc, hbmp, 0, 0, NULL, info, 0);
2241 ok(ret != 0 ||
2242 broken(ret == 0), /* win9x */
2243 "GetDIBits failed got %d\n", ret);
2245 for (p = ((BYTE *) info) + sizeof(info->bmiHeader); (p - ((BYTE *) info)) < info_len; p++)
2246 if (*p != '!')
2247 overwritten_bytes++;
2248 ok(overwritten_bytes == 0, "GetDIBits wrote past the buffer given\n");
2250 HeapFree(GetProcessHeap(), 0, info);
2251 DeleteObject(hbmp);
2252 ReleaseDC(NULL, screen_dc);
2255 static void check_BitBlt_pixel(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2256 DWORD dwRop, UINT32 expected, int line)
2258 *srcBuffer = 0xFEDCBA98;
2259 *dstBuffer = 0x89ABCDEF;
2260 Rectangle(hdcSrc, 0, 0, 1, 1); /* A null operation to ensure dibs are coerced to X11 */
2261 BitBlt(hdcDst, 0, 0, 1, 1, hdcSrc, 0, 0, dwRop);
2262 ok(expected == *dstBuffer,
2263 "BitBlt with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2264 dwRop, expected, *dstBuffer, line);
2267 static void test_BitBlt(void)
2269 HBITMAP bmpDst, bmpSrc;
2270 HBITMAP oldDst, oldSrc;
2271 HDC hdcScreen, hdcDst, hdcSrc;
2272 UINT32 *dstBuffer, *srcBuffer;
2273 HBRUSH hBrush, hOldBrush;
2274 BITMAPINFO bitmapInfo;
2276 memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2277 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2278 bitmapInfo.bmiHeader.biWidth = 1;
2279 bitmapInfo.bmiHeader.biHeight = 1;
2280 bitmapInfo.bmiHeader.biPlanes = 1;
2281 bitmapInfo.bmiHeader.biBitCount = 32;
2282 bitmapInfo.bmiHeader.biCompression = BI_RGB;
2283 bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
2285 hdcScreen = CreateCompatibleDC(0);
2286 hdcDst = CreateCompatibleDC(hdcScreen);
2287 hdcSrc = CreateCompatibleDC(hdcDst);
2289 /* Setup the destination dib section */
2290 bmpDst = CreateDIBSection(hdcScreen, &bitmapInfo, DIB_RGB_COLORS, (void**)&dstBuffer,
2291 NULL, 0);
2292 oldDst = SelectObject(hdcDst, bmpDst);
2294 hBrush = CreateSolidBrush(0x012345678);
2295 hOldBrush = SelectObject(hdcDst, hBrush);
2297 /* Setup the source dib section */
2298 bmpSrc = CreateDIBSection(hdcScreen, &bitmapInfo, DIB_RGB_COLORS, (void**)&srcBuffer,
2299 NULL, 0);
2300 oldSrc = SelectObject(hdcSrc, bmpSrc);
2302 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2303 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2304 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2305 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2306 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2307 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2308 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2309 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2310 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2311 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2312 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2313 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2314 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2315 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2316 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2318 /* Tidy up */
2319 SelectObject(hdcSrc, oldSrc);
2320 DeleteObject(bmpSrc);
2321 DeleteDC(hdcSrc);
2323 SelectObject(hdcDst, hOldBrush);
2324 DeleteObject(hBrush);
2325 SelectObject(hdcDst, oldDst);
2326 DeleteObject(bmpDst);
2327 DeleteDC(hdcDst);
2330 DeleteDC(hdcScreen);
2333 static void test_GdiAlphaBlend(void)
2335 /* test out-of-bound parameters for GdiAlphaBlend */
2336 HDC hdcNull;
2338 HDC hdcDst;
2339 HBITMAP bmpDst;
2340 HBITMAP oldDst;
2342 BITMAPINFO bmi;
2343 HDC hdcSrc;
2344 HBITMAP bmpSrc;
2345 HBITMAP oldSrc;
2346 LPVOID bits;
2348 BLENDFUNCTION blend;
2350 if (!pGdiAlphaBlend)
2352 win_skip("GdiAlphaBlend() is not implemented\n");
2353 return;
2356 hdcNull = GetDC(NULL);
2357 hdcDst = CreateCompatibleDC(hdcNull);
2358 bmpDst = CreateCompatibleBitmap(hdcNull, 100, 100);
2359 hdcSrc = CreateCompatibleDC(hdcNull);
2361 memset(&bmi, 0, sizeof(bmi)); /* as of Wine 0.9.44 we require the src to be a DIB section */
2362 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
2363 bmi.bmiHeader.biHeight = 20;
2364 bmi.bmiHeader.biWidth = 20;
2365 bmi.bmiHeader.biBitCount = 32;
2366 bmi.bmiHeader.biPlanes = 1;
2367 bmi.bmiHeader.biCompression = BI_RGB;
2368 bmpSrc = CreateDIBSection(hdcDst, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
2369 ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2371 oldDst = SelectObject(hdcDst, bmpDst);
2372 oldSrc = SelectObject(hdcSrc, bmpSrc);
2374 blend.BlendOp = AC_SRC_OVER;
2375 blend.BlendFlags = 0;
2376 blend.SourceConstantAlpha = 128;
2377 blend.AlphaFormat = 0;
2379 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, 0, 20, 20, blend), TRUE, BOOL, "%d");
2380 SetLastError(0xdeadbeef);
2381 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), FALSE, BOOL, "%d");
2382 expect_eq(GetLastError(), ERROR_INVALID_PARAMETER, int, "%d");
2383 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), FALSE, BOOL, "%d");
2384 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 15, 0, 10, 10, blend), FALSE, BOOL, "%d");
2385 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2386 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2388 SetWindowOrgEx(hdcSrc, -10, -10, NULL);
2389 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), TRUE, BOOL, "%d");
2390 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), TRUE, BOOL, "%d");
2391 SetMapMode(hdcSrc, MM_ANISOTROPIC);
2392 ScaleWindowExtEx(hdcSrc, 10, 1, 10, 1, NULL);
2393 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 30, 30, blend), TRUE, BOOL, "%d");
2394 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 30, 30, blend), TRUE, BOOL, "%d");
2396 SelectObject(hdcDst, oldDst);
2397 SelectObject(hdcSrc, oldSrc);
2398 DeleteObject(bmpSrc);
2399 DeleteObject(bmpDst);
2400 DeleteDC(hdcDst);
2401 DeleteDC(hdcSrc);
2403 ReleaseDC(NULL, hdcNull);
2407 static void test_clipping(void)
2409 HBITMAP bmpDst;
2410 HBITMAP oldDst;
2411 HBITMAP bmpSrc;
2412 HBITMAP oldSrc;
2413 HRGN hRgn;
2414 LPVOID bits;
2415 BOOL result;
2417 HDC hdcDst = CreateCompatibleDC( NULL );
2418 HDC hdcSrc = CreateCompatibleDC( NULL );
2420 BITMAPINFO bmpinfo={{0}};
2421 bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2422 bmpinfo.bmiHeader.biWidth = 100;
2423 bmpinfo.bmiHeader.biHeight = 100;
2424 bmpinfo.bmiHeader.biPlanes = 1;
2425 bmpinfo.bmiHeader.biBitCount = GetDeviceCaps( hdcDst, BITSPIXEL );
2426 bmpinfo.bmiHeader.biCompression = BI_RGB;
2428 bmpDst = CreateDIBSection( hdcDst, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2429 ok(bmpDst != NULL, "Couldn't create destination bitmap\n");
2430 oldDst = SelectObject( hdcDst, bmpDst );
2432 bmpSrc = CreateDIBSection( hdcSrc, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2433 ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2434 oldSrc = SelectObject( hdcSrc, bmpSrc );
2436 result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 100, 100, SRCCOPY );
2437 ok(result, "BitBlt failed\n");
2439 hRgn = CreateRectRgn( 0,0,0,0 );
2440 SelectClipRgn( hdcDst, hRgn );
2442 result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 0, 0, SRCCOPY );
2443 ok(result, "BitBlt failed\n");
2445 DeleteObject( bmpDst );
2446 DeleteObject( bmpSrc );
2447 DeleteObject( hRgn );
2448 DeleteDC( hdcDst );
2449 DeleteDC( hdcSrc );
2452 START_TEST(bitmap)
2454 HMODULE hdll;
2455 is_win9x = GetWindowLongPtrW(GetDesktopWindow(), GWLP_WNDPROC) == 0;
2457 hdll = GetModuleHandle("gdi32.dll");
2458 pGdiAlphaBlend = (void*)GetProcAddress(hdll, "GdiAlphaBlend");
2460 test_createdibitmap();
2461 test_dibsections();
2462 test_mono_dibsection();
2463 test_bitmap();
2464 test_bmBits();
2465 test_GetDIBits_selected_DIB(1);
2466 test_GetDIBits_selected_DIB(4);
2467 test_GetDIBits_selected_DIB(8);
2468 test_GetDIBits_selected_DDB(TRUE);
2469 test_GetDIBits_selected_DDB(FALSE);
2470 test_GetDIBits();
2471 test_GetDIBits_BI_BITFIELDS();
2472 test_select_object();
2473 test_CreateBitmap();
2474 test_BitBlt();
2475 test_GdiAlphaBlend();
2476 test_bitmapinfoheadersize();
2477 test_get16dibits();
2478 test_clipping();