gdi32: Return the actual DIB section bitfields instead of default values in GetDIBits.
[wine/hacks.git] / dlls / gdi32 / tests / bitmap.c
blobb25cfd4ae1e58ac1b8ea1d1985a8ed81a3e1d9b5
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 == abs(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 == abs(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, "%d != %d\n", ds.dsBmih.biWidth, bmih->biWidth);
367 ok(ds.dsBmih.biHeight == abs(bmih->biHeight) ||
368 broken(ds.dsBmih.biHeight == bmih->biHeight), /* Win9x/WinMe */
369 "%d != %d\n", ds.dsBmih.biHeight, abs(bmih->biHeight));
370 ok(ds.dsBmih.biPlanes == bmih->biPlanes, "%u != %u\n", ds.dsBmih.biPlanes, bmih->biPlanes);
371 ok(ds.dsBmih.biBitCount == bmih->biBitCount, "%u != %u\n", ds.dsBmih.biBitCount, bmih->biBitCount);
372 ok(ds.dsBmih.biCompression == bmih->biCompression, "%u != %u\n", ds.dsBmih.biCompression, bmih->biCompression);
373 ok(ds.dsBmih.biSizeImage == bmih->biSizeImage, "%u != %u\n", ds.dsBmih.biSizeImage, bmih->biSizeImage);
374 ok(ds.dsBmih.biXPelsPerMeter == bmih->biXPelsPerMeter, "%d != %d\n", ds.dsBmih.biXPelsPerMeter, bmih->biXPelsPerMeter);
375 ok(ds.dsBmih.biYPelsPerMeter == bmih->biYPelsPerMeter, "%d != %d\n", ds.dsBmih.biYPelsPerMeter, bmih->biYPelsPerMeter);
377 memset(&ds, 0xAA, sizeof(ds));
378 ret = GetObject(hbm, sizeof(ds) - 4, &ds);
379 ok(ret == sizeof(ds.dsBm) || broken(ret == (sizeof(ds) - 4) /* Win9x */), "wrong size %d\n", ret);
380 ok(ds.dsBm.bmWidth == bmih->biWidth, "%d != %d\n", ds.dsBmih.biWidth, bmih->biWidth);
381 ok(ds.dsBm.bmHeight == abs(bmih->biHeight), "%d != %d\n", ds.dsBmih.biHeight, abs(bmih->biHeight));
382 ok(ds.dsBm.bmBits == bits, "%p != %p\n", ds.dsBm.bmBits, bits);
384 ret = GetObject(hbm, 0, &ds);
385 ok(ret == 0, "%d != 0\n", ret);
387 ret = GetObject(hbm, 1, &ds);
388 ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
391 #define test_color_todo(got, exp, txt, todo) \
392 if (!todo && got != exp && screen_depth < 24) { \
393 todo_wine ok(0, #txt " failed at %d-bit screen depth: got 0x%06x expected 0x%06x - skipping DIB tests\n", \
394 screen_depth, (UINT)got, (UINT)exp); \
395 return; \
396 } else if (todo) todo_wine { ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp); } \
397 else ok(got == exp, #txt " failed: got 0x%06x expected 0x%06x\n", (UINT)got, (UINT)exp) \
399 #define test_color(hdc, color, exp, todo_setp, todo_getp) \
401 COLORREF c; \
402 c = SetPixel(hdc, 0, 0, color); \
403 if (!is_win9x) { test_color_todo(c, exp, SetPixel, todo_setp); } \
404 c = GetPixel(hdc, 0, 0); \
405 test_color_todo(c, exp, GetPixel, todo_getp); \
408 static void test_dib_bits_access( HBITMAP hdib, void *bits )
410 MEMORY_BASIC_INFORMATION info;
411 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
412 DWORD data[256];
413 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
414 HDC hdc;
415 char filename[MAX_PATH];
416 HANDLE file;
417 DWORD written;
418 INT ret;
420 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
421 "VirtualQuery failed\n");
422 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
423 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
424 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
425 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
426 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
427 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
429 memset( pbmi, 0, sizeof(bmibuf) );
430 memset( data, 0xcc, sizeof(data) );
431 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
432 pbmi->bmiHeader.biHeight = 16;
433 pbmi->bmiHeader.biWidth = 16;
434 pbmi->bmiHeader.biBitCount = 32;
435 pbmi->bmiHeader.biPlanes = 1;
436 pbmi->bmiHeader.biCompression = BI_RGB;
438 hdc = GetDC(0);
440 ret = SetDIBits( hdc, hdib, 0, 16, data, pbmi, DIB_RGB_COLORS );
441 ok(ret == 16 ||
442 broken(ret == 0), /* win9x */
443 "SetDIBits failed: expected 16 got %d\n", ret);
445 ReleaseDC(0, hdc);
447 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
448 "VirtualQuery failed\n");
449 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
450 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
451 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
452 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
453 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
454 /* it has been protected now */
455 todo_wine ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
457 /* try writing protected bits to a file */
459 GetTempFileNameA( ".", "dib", 0, filename );
460 file = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
461 CREATE_ALWAYS, 0, 0 );
462 ok( file != INVALID_HANDLE_VALUE, "failed to open %s error %u\n", filename, GetLastError() );
463 ret = WriteFile( file, bits, 8192, &written, NULL );
464 ok( ret, "WriteFile failed error %u\n", GetLastError() );
465 if (ret) ok( written == 8192, "only wrote %u bytes\n", written );
466 CloseHandle( file );
467 DeleteFileA( filename );
470 static void test_dibsections(void)
472 HDC hdc, hdcmem, hdcmem2;
473 HBITMAP hdib, oldbm, hdib2, oldbm2;
474 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
475 char bcibuf[sizeof(BITMAPCOREINFO) + 256 * sizeof(RGBTRIPLE)];
476 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
477 BITMAPCOREINFO *pbci = (BITMAPCOREINFO *)bcibuf;
478 HBITMAP hcoredib;
479 char coreBits[256];
480 BYTE *bits;
481 RGBQUAD rgb[256];
482 int ret;
483 char logpalbuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
484 LOGPALETTE *plogpal = (LOGPALETTE*)logpalbuf;
485 WORD *index;
486 DWORD *bits32;
487 HPALETTE hpal, oldpal;
488 DIBSECTION dibsec;
489 COLORREF c0, c1;
490 int i;
491 int screen_depth;
492 MEMORY_BASIC_INFORMATION info;
494 hdc = GetDC(0);
495 screen_depth = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
497 memset(pbmi, 0, sizeof(bmibuf));
498 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
499 pbmi->bmiHeader.biHeight = 100;
500 pbmi->bmiHeader.biWidth = 512;
501 pbmi->bmiHeader.biBitCount = 24;
502 pbmi->bmiHeader.biPlanes = 1;
503 pbmi->bmiHeader.biCompression = BI_RGB;
505 SetLastError(0xdeadbeef);
507 /* invalid pointer for BITMAPINFO
508 (*bits should be NULL on error) */
509 bits = (BYTE*)0xdeadbeef;
510 hdib = CreateDIBSection(hdc, NULL, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
511 ok(hdib == NULL && bits == NULL, "CreateDIBSection failed for invalid parameter: bmi == 0x0\n");
513 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
514 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
515 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
516 ok(dibsec.dsBm.bmBits == bits, "dibsec.dsBits %p != bits %p\n", dibsec.dsBm.bmBits, bits);
518 /* test the DIB memory */
519 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
520 "VirtualQuery failed\n");
521 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
522 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
523 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
524 ok(info.RegionSize == 0x26000, "0x%lx != 0x26000\n", info.RegionSize);
525 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
526 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
527 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
529 test_dib_bits_access( hdib, bits );
531 test_dib_info(hdib, bits, &pbmi->bmiHeader);
532 DeleteObject(hdib);
534 /* Test a top-down DIB. */
535 pbmi->bmiHeader.biHeight = -100;
536 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
537 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
538 test_dib_info(hdib, bits, &pbmi->bmiHeader);
539 DeleteObject(hdib);
541 pbmi->bmiHeader.biHeight = 100;
542 pbmi->bmiHeader.biBitCount = 8;
543 pbmi->bmiHeader.biCompression = BI_RLE8;
544 SetLastError(0xdeadbeef);
545 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
546 ok(hdib == NULL, "CreateDIBSection should fail when asked to create a compressed DIB section\n");
547 ok(GetLastError() == 0xdeadbeef, "wrong error %d\n", GetLastError());
549 pbmi->bmiHeader.biBitCount = 16;
550 pbmi->bmiHeader.biCompression = BI_BITFIELDS;
551 ((PDWORD)pbmi->bmiColors)[0] = 0xf800;
552 ((PDWORD)pbmi->bmiColors)[1] = 0x07e0;
553 ((PDWORD)pbmi->bmiColors)[2] = 0x001f;
554 SetLastError(0xdeadbeef);
555 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
556 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
558 /* test the DIB memory */
559 ok(VirtualQuery(bits, &info, sizeof(info)) == sizeof(info),
560 "VirtualQuery failed\n");
561 ok(info.BaseAddress == bits, "%p != %p\n", info.BaseAddress, bits);
562 ok(info.AllocationBase == bits, "%p != %p\n", info.AllocationBase, bits);
563 ok(info.AllocationProtect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.AllocationProtect);
564 ok(info.RegionSize == 0x19000, "0x%lx != 0x19000\n", info.RegionSize);
565 ok(info.State == MEM_COMMIT, "%x != MEM_COMMIT\n", info.State);
566 ok(info.Protect == PAGE_READWRITE, "%x != PAGE_READWRITE\n", info.Protect);
567 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
569 test_dib_info(hdib, bits, &pbmi->bmiHeader);
570 DeleteObject(hdib);
572 memset(pbmi, 0, sizeof(bmibuf));
573 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
574 pbmi->bmiHeader.biHeight = 16;
575 pbmi->bmiHeader.biWidth = 16;
576 pbmi->bmiHeader.biBitCount = 1;
577 pbmi->bmiHeader.biPlanes = 1;
578 pbmi->bmiHeader.biCompression = BI_RGB;
579 pbmi->bmiColors[0].rgbRed = 0xff;
580 pbmi->bmiColors[0].rgbGreen = 0;
581 pbmi->bmiColors[0].rgbBlue = 0;
582 pbmi->bmiColors[1].rgbRed = 0;
583 pbmi->bmiColors[1].rgbGreen = 0;
584 pbmi->bmiColors[1].rgbBlue = 0xff;
586 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
587 ok(hdib != NULL, "CreateDIBSection failed\n");
588 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIBSection\n");
589 ok(dibsec.dsBmih.biClrUsed == 2,
590 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
592 /* Test if the old BITMAPCOREINFO structure is supported */
594 pbci->bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
595 pbci->bmciHeader.bcBitCount = 0;
597 if (!is_win9x) {
598 ret = GetDIBits(hdc, hdib, 0, 16, NULL, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
599 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
600 ok((pbci->bmciHeader.bcWidth == 16) && (pbci->bmciHeader.bcHeight == 16)
601 && (pbci->bmciHeader.bcBitCount == 1) && (pbci->bmciHeader.bcPlanes == 1),
602 "GetDIBits did't fill in the BITMAPCOREHEADER structure properly\n");
604 ret = GetDIBits(hdc, hdib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
605 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
606 ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
607 (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
608 (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
609 "The color table has not been translated to the old BITMAPCOREINFO format\n");
611 hcoredib = CreateDIBSection(hdc, (BITMAPINFO*) pbci, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
612 ok(hcoredib != NULL, "CreateDIBSection failed with a BITMAPCOREINFO\n");
614 ZeroMemory(pbci->bmciColors, 256 * sizeof(RGBTRIPLE));
615 ret = GetDIBits(hdc, hcoredib, 0, 16, &coreBits, (BITMAPINFO*) pbci, DIB_RGB_COLORS);
616 ok(ret, "GetDIBits doesn't work with a BITMAPCOREHEADER\n");
617 ok((pbci->bmciColors[0].rgbtRed == 0xff) && (pbci->bmciColors[0].rgbtGreen == 0) &&
618 (pbci->bmciColors[0].rgbtBlue == 0) && (pbci->bmciColors[1].rgbtRed == 0) &&
619 (pbci->bmciColors[1].rgbtGreen == 0) && (pbci->bmciColors[1].rgbtBlue == 0xff),
620 "The color table has not been translated to the old BITMAPCOREINFO format\n");
622 DeleteObject(hcoredib);
625 hdcmem = CreateCompatibleDC(hdc);
626 oldbm = SelectObject(hdcmem, hdib);
628 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
629 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
630 ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
631 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
632 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
633 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
635 c0 = RGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen, pbmi->bmiColors[0].rgbBlue);
636 c1 = RGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen, pbmi->bmiColors[1].rgbBlue);
638 test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
639 test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
640 test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
641 test_color(hdcmem, PALETTEINDEX(0), c0, 1, 1);
642 test_color(hdcmem, PALETTEINDEX(1), c0, 1, 1);
643 test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
644 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[0].rgbRed, pbmi->bmiColors[0].rgbGreen,
645 pbmi->bmiColors[0].rgbBlue), c0, 1, 1);
646 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[1].rgbRed, pbmi->bmiColors[1].rgbGreen,
647 pbmi->bmiColors[1].rgbBlue), c1, 1, 1);
648 test_color(hdcmem, PALETTERGB(0, 0, 0), c0, 1, 1);
649 test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
650 test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c1, 1, 1);
652 SelectObject(hdcmem, oldbm);
653 DeleteObject(hdib);
655 pbmi->bmiColors[0].rgbRed = 0xff;
656 pbmi->bmiColors[0].rgbGreen = 0xff;
657 pbmi->bmiColors[0].rgbBlue = 0xff;
658 pbmi->bmiColors[1].rgbRed = 0;
659 pbmi->bmiColors[1].rgbGreen = 0;
660 pbmi->bmiColors[1].rgbBlue = 0;
662 hdib = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
663 ok(hdib != NULL, "CreateDIBSection failed\n");
665 test_dib_info(hdib, bits, &pbmi->bmiHeader);
667 oldbm = SelectObject(hdcmem, hdib);
669 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
670 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
671 ok(!memcmp(rgb, pbmi->bmiColors, 2 * sizeof(RGBQUAD)),
672 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
673 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
674 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
676 SelectObject(hdcmem, oldbm);
677 test_dib_info(hdib, bits, &pbmi->bmiHeader);
678 DeleteObject(hdib);
680 pbmi->bmiHeader.biBitCount = 4;
681 for (i = 0; i < 16; i++) {
682 pbmi->bmiColors[i].rgbRed = i;
683 pbmi->bmiColors[i].rgbGreen = 16-i;
684 pbmi->bmiColors[i].rgbBlue = 0;
686 hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
687 ok(hdib != NULL, "CreateDIBSection failed\n");
688 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
689 ok(dibsec.dsBmih.biClrUsed == 16,
690 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 16);
691 test_dib_info(hdib, bits, &pbmi->bmiHeader);
692 DeleteObject(hdib);
694 pbmi->bmiHeader.biBitCount = 8;
696 for (i = 0; i < 128; i++) {
697 pbmi->bmiColors[i].rgbRed = 255 - i * 2;
698 pbmi->bmiColors[i].rgbGreen = i * 2;
699 pbmi->bmiColors[i].rgbBlue = 0;
700 pbmi->bmiColors[255 - i].rgbRed = 0;
701 pbmi->bmiColors[255 - i].rgbGreen = i * 2;
702 pbmi->bmiColors[255 - i].rgbBlue = 255 - i * 2;
704 hdib = CreateDIBSection(hdcmem, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
705 ok(hdib != NULL, "CreateDIBSection failed\n");
706 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
707 ok(dibsec.dsBmih.biClrUsed == 256,
708 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
710 oldbm = SelectObject(hdcmem, hdib);
712 for (i = 0; i < 256; i++) {
713 test_color(hdcmem, DIBINDEX(i),
714 RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
715 test_color(hdcmem, PALETTERGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue),
716 RGB(pbmi->bmiColors[i].rgbRed, pbmi->bmiColors[i].rgbGreen, pbmi->bmiColors[i].rgbBlue), 0, 0);
719 SelectObject(hdcmem, oldbm);
720 test_dib_info(hdib, bits, &pbmi->bmiHeader);
721 DeleteObject(hdib);
723 pbmi->bmiHeader.biBitCount = 1;
725 /* Now create a palette and a palette indexed dib section */
726 memset(plogpal, 0, sizeof(logpalbuf));
727 plogpal->palVersion = 0x300;
728 plogpal->palNumEntries = 2;
729 plogpal->palPalEntry[0].peRed = 0xff;
730 plogpal->palPalEntry[0].peBlue = 0xff;
731 plogpal->palPalEntry[1].peGreen = 0xff;
733 index = (WORD*)pbmi->bmiColors;
734 *index++ = 0;
735 *index = 1;
736 hpal = CreatePalette(plogpal);
737 ok(hpal != NULL, "CreatePalette failed\n");
738 oldpal = SelectPalette(hdc, hpal, TRUE);
739 hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
740 ok(hdib != NULL, "CreateDIBSection failed\n");
741 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
742 ok(dibsec.dsBmih.biClrUsed == 2 ||
743 broken(dibsec.dsBmih.biClrUsed == 0), /* win9x */
744 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 2);
746 /* The colour table has already been grabbed from the dc, so we select back the
747 old palette */
749 SelectPalette(hdc, oldpal, TRUE);
750 oldbm = SelectObject(hdcmem, hdib);
751 oldpal = SelectPalette(hdcmem, hpal, TRUE);
753 ret = GetDIBColorTable(hdcmem, 0, 2, rgb);
754 ok(ret == 2, "GetDIBColorTable returned %d\n", ret);
755 ok(rgb[0].rgbRed == 0xff && rgb[0].rgbBlue == 0xff && rgb[0].rgbGreen == 0 &&
756 rgb[1].rgbRed == 0 && rgb[1].rgbBlue == 0 && rgb[1].rgbGreen == 0xff,
757 "GetDIBColorTable returns table 0: r%02x g%02x b%02x res%02x 1: r%02x g%02x b%02x res%02x\n",
758 rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue, rgb[0].rgbReserved,
759 rgb[1].rgbRed, rgb[1].rgbGreen, rgb[1].rgbBlue, rgb[1].rgbReserved);
761 c0 = RGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen, plogpal->palPalEntry[0].peBlue);
762 c1 = RGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen, plogpal->palPalEntry[1].peBlue);
764 test_color(hdcmem, DIBINDEX(0), c0, 0, 1);
765 test_color(hdcmem, DIBINDEX(1), c1, 0, 1);
766 test_color(hdcmem, DIBINDEX(2), c0, 1, 1);
767 test_color(hdcmem, PALETTEINDEX(0), c0, 0, 1);
768 test_color(hdcmem, PALETTEINDEX(1), c1, 0, 1);
769 test_color(hdcmem, PALETTEINDEX(2), c0, 1, 1);
770 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[0].peRed, plogpal->palPalEntry[0].peGreen,
771 plogpal->palPalEntry[0].peBlue), c0, 1, 1);
772 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[1].peRed, plogpal->palPalEntry[1].peGreen,
773 plogpal->palPalEntry[1].peBlue), c1, 1, 1);
774 test_color(hdcmem, PALETTERGB(0, 0, 0), c1, 1, 1);
775 test_color(hdcmem, PALETTERGB(0xff, 0xff, 0xff), c0, 1, 1);
776 test_color(hdcmem, PALETTERGB(0, 0, 0xfe), c0, 1, 1);
777 test_color(hdcmem, PALETTERGB(0, 1, 0), c1, 1, 1);
778 test_color(hdcmem, PALETTERGB(0x3f, 0, 0x3f), c1, 1, 1);
779 test_color(hdcmem, PALETTERGB(0x40, 0, 0x40), c0, 1, 1);
781 /* Bottom and 2nd row from top green, everything else magenta */
782 bits[0] = bits[1] = 0xff;
783 bits[13 * 4] = bits[13*4 + 1] = 0xff;
785 test_dib_info(hdib, bits, &pbmi->bmiHeader);
787 pbmi->bmiHeader.biBitCount = 32;
789 hdib2 = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&bits32, NULL, 0);
790 ok(hdib2 != NULL, "CreateDIBSection failed\n");
791 hdcmem2 = CreateCompatibleDC(hdc);
792 oldbm2 = SelectObject(hdcmem2, hdib2);
794 BitBlt(hdcmem2, 0, 0, 16,16, hdcmem, 0, 0, SRCCOPY);
796 ok(bits32[0] == 0xff00, "lower left pixel is %08x\n", bits32[0]);
797 ok(bits32[17] == 0xff00ff ||
798 broken(bits32[17] == 0x00ff00), /* Intermittent on Win9x/ME */
799 "bottom but one, left pixel is %08x\n", bits32[17]);
801 SelectObject(hdcmem2, oldbm2);
802 test_dib_info(hdib2, bits32, &pbmi->bmiHeader);
803 DeleteObject(hdib2);
805 SelectObject(hdcmem, oldbm);
806 SelectObject(hdcmem, oldpal);
807 DeleteObject(hdib);
808 DeleteObject(hpal);
811 pbmi->bmiHeader.biBitCount = 8;
813 memset(plogpal, 0, sizeof(logpalbuf));
814 plogpal->palVersion = 0x300;
815 plogpal->palNumEntries = 256;
817 for (i = 0; i < 128; i++) {
818 plogpal->palPalEntry[i].peRed = 255 - i * 2;
819 plogpal->palPalEntry[i].peBlue = i * 2;
820 plogpal->palPalEntry[i].peGreen = 0;
821 plogpal->palPalEntry[255 - i].peRed = 0;
822 plogpal->palPalEntry[255 - i].peGreen = i * 2;
823 plogpal->palPalEntry[255 - i].peBlue = 255 - i * 2;
826 index = (WORD*)pbmi->bmiColors;
827 for (i = 0; i < 256; i++) {
828 *index++ = i;
831 hpal = CreatePalette(plogpal);
832 ok(hpal != NULL, "CreatePalette failed\n");
833 oldpal = SelectPalette(hdc, hpal, TRUE);
834 hdib = CreateDIBSection(hdc, pbmi, DIB_PAL_COLORS, (void**)&bits, NULL, 0);
835 ok(hdib != NULL, "CreateDIBSection failed\n");
836 ok(GetObject(hdib, sizeof(DIBSECTION), &dibsec) != 0, "GetObject failed for DIB Section\n");
837 ok(dibsec.dsBmih.biClrUsed == 256 ||
838 broken(dibsec.dsBmih.biClrUsed == 0), /* win9x */
839 "created DIBSection: wrong biClrUsed field: %u, should be: %u\n", dibsec.dsBmih.biClrUsed, 256);
841 test_dib_info(hdib, bits, &pbmi->bmiHeader);
843 SelectPalette(hdc, oldpal, TRUE);
844 oldbm = SelectObject(hdcmem, hdib);
845 oldpal = SelectPalette(hdcmem, hpal, TRUE);
847 ret = GetDIBColorTable(hdcmem, 0, 256, rgb);
848 ok(ret == 256, "GetDIBColorTable returned %d\n", ret);
849 for (i = 0; i < 256; i++) {
850 ok(rgb[i].rgbRed == plogpal->palPalEntry[i].peRed &&
851 rgb[i].rgbBlue == plogpal->palPalEntry[i].peBlue &&
852 rgb[i].rgbGreen == plogpal->palPalEntry[i].peGreen,
853 "GetDIBColorTable returns table %d: r%02x g%02x b%02x res%02x\n",
854 i, rgb[i].rgbRed, rgb[i].rgbGreen, rgb[i].rgbBlue, rgb[i].rgbReserved);
857 for (i = 0; i < 256; i++) {
858 test_color(hdcmem, DIBINDEX(i),
859 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
860 test_color(hdcmem, PALETTEINDEX(i),
861 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
862 test_color(hdcmem, PALETTERGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue),
863 RGB(plogpal->palPalEntry[i].peRed, plogpal->palPalEntry[i].peGreen, plogpal->palPalEntry[i].peBlue), 0, 0);
866 SelectPalette(hdcmem, oldpal, TRUE);
867 SelectObject(hdcmem, oldbm);
868 DeleteObject(hdib);
869 DeleteObject(hpal);
871 DeleteDC(hdcmem);
872 DeleteDC(hdcmem2);
873 ReleaseDC(0, hdc);
876 static void test_mono_dibsection(void)
878 HDC hdc, memdc;
879 HBITMAP old_bm, mono_ds;
880 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
881 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
882 BYTE bits[10 * 4];
883 BYTE *ds_bits;
884 int num;
886 hdc = GetDC(0);
888 memdc = CreateCompatibleDC(hdc);
890 memset(pbmi, 0, sizeof(bmibuf));
891 pbmi->bmiHeader.biSize = sizeof(pbmi->bmiHeader);
892 pbmi->bmiHeader.biHeight = 10;
893 pbmi->bmiHeader.biWidth = 10;
894 pbmi->bmiHeader.biBitCount = 1;
895 pbmi->bmiHeader.biPlanes = 1;
896 pbmi->bmiHeader.biCompression = BI_RGB;
897 pbmi->bmiColors[0].rgbRed = 0xff;
898 pbmi->bmiColors[0].rgbGreen = 0xff;
899 pbmi->bmiColors[0].rgbBlue = 0xff;
900 pbmi->bmiColors[1].rgbRed = 0x0;
901 pbmi->bmiColors[1].rgbGreen = 0x0;
902 pbmi->bmiColors[1].rgbBlue = 0x0;
905 * First dib section is 'inverted' ie color[0] is white, color[1] is black
908 mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
909 ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
910 old_bm = SelectObject(memdc, mono_ds);
912 /* black border, white interior */
913 Rectangle(memdc, 0, 0, 10, 10);
914 ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
915 ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
917 /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
919 memset(bits, 0, sizeof(bits));
920 bits[0] = 0xaa;
922 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
923 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
925 /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
927 pbmi->bmiColors[0].rgbRed = 0x0;
928 pbmi->bmiColors[0].rgbGreen = 0x0;
929 pbmi->bmiColors[0].rgbBlue = 0x0;
930 pbmi->bmiColors[1].rgbRed = 0xff;
931 pbmi->bmiColors[1].rgbGreen = 0xff;
932 pbmi->bmiColors[1].rgbBlue = 0xff;
934 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
935 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
937 SelectObject(memdc, old_bm);
938 DeleteObject(mono_ds);
941 * Next dib section is 'normal' ie color[0] is black, color[1] is white
944 pbmi->bmiColors[0].rgbRed = 0x0;
945 pbmi->bmiColors[0].rgbGreen = 0x0;
946 pbmi->bmiColors[0].rgbBlue = 0x0;
947 pbmi->bmiColors[1].rgbRed = 0xff;
948 pbmi->bmiColors[1].rgbGreen = 0xff;
949 pbmi->bmiColors[1].rgbBlue = 0xff;
951 mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
952 ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
953 old_bm = SelectObject(memdc, mono_ds);
955 /* black border, white interior */
956 Rectangle(memdc, 0, 0, 10, 10);
957 ok(ds_bits[0] == 0x00, "out_bits %02x\n", ds_bits[0]);
958 ok(ds_bits[4] == 0x7f, "out_bits %02x\n", ds_bits[4]);
960 /* SetDIBitsToDevice with a normal bmi -> normal dib section */
962 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
963 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
965 /* SetDIBitsToDevice with a inverted bmi -> normal dib section */
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;
974 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
975 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
978 * Take that 'normal' dibsection and change its colour table to an 'inverted' one
981 pbmi->bmiColors[0].rgbRed = 0xff;
982 pbmi->bmiColors[0].rgbGreen = 0xff;
983 pbmi->bmiColors[0].rgbBlue = 0xff;
984 pbmi->bmiColors[1].rgbRed = 0x0;
985 pbmi->bmiColors[1].rgbGreen = 0x0;
986 pbmi->bmiColors[1].rgbBlue = 0x0;
987 num = SetDIBColorTable(memdc, 0, 2, pbmi->bmiColors);
988 ok(num == 2, "num = %d\n", num);
990 /* black border, white interior */
991 Rectangle(memdc, 0, 0, 10, 10);
992 todo_wine {
993 ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
994 ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
996 /* SetDIBitsToDevice with an inverted bmi -> inverted dib section */
998 memset(bits, 0, sizeof(bits));
999 bits[0] = 0xaa;
1001 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1002 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
1004 /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
1006 pbmi->bmiColors[0].rgbRed = 0x0;
1007 pbmi->bmiColors[0].rgbGreen = 0x0;
1008 pbmi->bmiColors[0].rgbBlue = 0x0;
1009 pbmi->bmiColors[1].rgbRed = 0xff;
1010 pbmi->bmiColors[1].rgbGreen = 0xff;
1011 pbmi->bmiColors[1].rgbBlue = 0xff;
1013 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1014 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1016 SelectObject(memdc, old_bm);
1017 DeleteObject(mono_ds);
1020 * Now a dib section with a strange colour map just for fun. This behaves just like an inverted one.
1023 pbmi->bmiColors[0].rgbRed = 0xff;
1024 pbmi->bmiColors[0].rgbGreen = 0x0;
1025 pbmi->bmiColors[0].rgbBlue = 0x0;
1026 pbmi->bmiColors[1].rgbRed = 0xfe;
1027 pbmi->bmiColors[1].rgbGreen = 0x0;
1028 pbmi->bmiColors[1].rgbBlue = 0x0;
1030 mono_ds = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&ds_bits, NULL, 0);
1031 ok(mono_ds != NULL, "CreateDIBSection rets NULL\n");
1032 old_bm = SelectObject(memdc, mono_ds);
1034 /* black border, white interior */
1035 Rectangle(memdc, 0, 0, 10, 10);
1036 ok(ds_bits[0] == 0xff, "out_bits %02x\n", ds_bits[0]);
1037 ok(ds_bits[4] == 0x80, "out_bits %02x\n", ds_bits[4]);
1039 /* SetDIBitsToDevice with a normal bmi -> inverted dib section */
1041 pbmi->bmiColors[0].rgbRed = 0x0;
1042 pbmi->bmiColors[0].rgbGreen = 0x0;
1043 pbmi->bmiColors[0].rgbBlue = 0x0;
1044 pbmi->bmiColors[1].rgbRed = 0xff;
1045 pbmi->bmiColors[1].rgbGreen = 0xff;
1046 pbmi->bmiColors[1].rgbBlue = 0xff;
1048 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1049 ok(ds_bits[0] == 0x55, "out_bits %02x\n", ds_bits[0]);
1051 /* SetDIBitsToDevice with a inverted bmi -> inverted dib section */
1053 pbmi->bmiColors[0].rgbRed = 0xff;
1054 pbmi->bmiColors[0].rgbGreen = 0xff;
1055 pbmi->bmiColors[0].rgbBlue = 0xff;
1056 pbmi->bmiColors[1].rgbRed = 0x0;
1057 pbmi->bmiColors[1].rgbGreen = 0x0;
1058 pbmi->bmiColors[1].rgbBlue = 0x0;
1060 SetDIBitsToDevice(memdc, 0, 0, 10, 10, 0, 0, 0, 10, bits, pbmi, DIB_RGB_COLORS);
1061 ok(ds_bits[0] == 0xaa, "out_bits %02x\n", ds_bits[0]);
1063 SelectObject(memdc, old_bm);
1064 DeleteObject(mono_ds);
1066 DeleteDC(memdc);
1067 ReleaseDC(0, hdc);
1070 static void test_bitmap(void)
1072 char buf[256], buf_cmp[256];
1073 HBITMAP hbmp, hbmp_old;
1074 HDC hdc;
1075 BITMAP bm;
1076 BITMAP bma[2];
1077 INT ret;
1079 hdc = CreateCompatibleDC(0);
1080 assert(hdc != 0);
1082 SetLastError(0xdeadbeef);
1083 hbmp = CreateBitmap(0x7ffffff, 1, 1, 1, NULL);
1084 if (!hbmp)
1086 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1087 GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1088 "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1090 else
1091 DeleteObject(hbmp);
1093 SetLastError(0xdeadbeef);
1094 hbmp = CreateBitmap(0x7ffffff, 9, 1, 1, NULL);
1095 if (!hbmp)
1097 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY /* XP */ ||
1098 GetLastError() == ERROR_INVALID_PARAMETER /* Win2k */,
1099 "expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
1101 else
1102 DeleteObject(hbmp);
1104 SetLastError(0xdeadbeef);
1105 hbmp = CreateBitmap(0x7ffffff + 1, 1, 1, 1, NULL);
1106 ok(!hbmp || broken(hbmp != NULL /* Win9x */), "CreateBitmap should fail\n");
1107 if (!hbmp)
1108 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1109 "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1110 else
1111 DeleteObject(hbmp);
1113 hbmp = CreateBitmap(15, 15, 1, 1, NULL);
1114 assert(hbmp != NULL);
1116 ret = GetObject(hbmp, sizeof(bm), &bm);
1117 ok(ret == sizeof(bm), "wrong size %d\n", ret);
1119 ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1120 ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1121 ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1122 ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1123 ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1124 ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1125 ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1127 assert(sizeof(buf) >= bm.bmWidthBytes * bm.bmHeight);
1128 assert(sizeof(buf) == sizeof(buf_cmp));
1130 ret = GetBitmapBits(hbmp, 0, NULL);
1131 ok(ret == bm.bmWidthBytes * bm.bmHeight || broken(ret == 0 /* Win9x */),
1132 "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1134 memset(buf_cmp, 0xAA, sizeof(buf_cmp));
1135 memset(buf_cmp, 0, bm.bmWidthBytes * bm.bmHeight);
1137 memset(buf, 0xAA, sizeof(buf));
1138 ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1139 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1140 ok(!memcmp(buf, buf_cmp, sizeof(buf)) ||
1141 broken(memcmp(buf, buf_cmp, sizeof(buf))), /* win9x doesn't init the bitmap bits */
1142 "buffers do not match\n");
1144 hbmp_old = SelectObject(hdc, hbmp);
1146 ret = GetObject(hbmp, sizeof(bm), &bm);
1147 ok(ret == sizeof(bm), "wrong size %d\n", ret);
1149 ok(bm.bmType == 0, "wrong bm.bmType %d\n", bm.bmType);
1150 ok(bm.bmWidth == 15, "wrong bm.bmWidth %d\n", bm.bmWidth);
1151 ok(bm.bmHeight == 15, "wrong bm.bmHeight %d\n", bm.bmHeight);
1152 ok(bm.bmWidthBytes == 2, "wrong bm.bmWidthBytes %d\n", bm.bmWidthBytes);
1153 ok(bm.bmPlanes == 1, "wrong bm.bmPlanes %d\n", bm.bmPlanes);
1154 ok(bm.bmBitsPixel == 1, "wrong bm.bmBitsPixel %d\n", bm.bmBitsPixel);
1155 ok(bm.bmBits == NULL, "wrong bm.bmBits %p\n", bm.bmBits);
1157 memset(buf, 0xAA, sizeof(buf));
1158 ret = GetBitmapBits(hbmp, sizeof(buf), buf);
1159 ok(ret == bm.bmWidthBytes * bm.bmHeight, "%d != %d\n", ret, bm.bmWidthBytes * bm.bmHeight);
1160 ok(!memcmp(buf, buf_cmp, sizeof(buf)) ||
1161 broken(memcmp(buf, buf_cmp, sizeof(buf))), /* win9x doesn't init the bitmap bits */
1162 "buffers do not match\n");
1164 hbmp_old = SelectObject(hdc, hbmp_old);
1165 ok(hbmp_old == hbmp, "wrong old bitmap %p\n", hbmp_old);
1167 /* test various buffer sizes for GetObject */
1168 ret = GetObject(hbmp, sizeof(*bma) * 2, bma);
1169 ok(ret == sizeof(*bma) || broken(ret == sizeof(*bma) * 2 /* Win9x */), "wrong size %d\n", ret);
1171 ret = GetObject(hbmp, sizeof(bm) / 2, &bm);
1172 ok(ret == 0 || broken(ret == sizeof(bm) / 2 /* Win9x */), "%d != 0\n", ret);
1174 ret = GetObject(hbmp, 0, &bm);
1175 ok(ret == 0, "%d != 0\n", ret);
1177 ret = GetObject(hbmp, 1, &bm);
1178 ok(ret == 0 || broken(ret == 1 /* Win9x */), "%d != 0\n", ret);
1180 DeleteObject(hbmp);
1181 DeleteDC(hdc);
1184 static void test_bmBits(void)
1186 BYTE bits[4];
1187 HBITMAP hbmp;
1188 BITMAP bmp;
1190 memset(bits, 0, sizeof(bits));
1191 hbmp = CreateBitmap(2, 2, 1, 4, bits);
1192 ok(hbmp != NULL, "CreateBitmap failed\n");
1194 memset(&bmp, 0xFF, sizeof(bmp));
1195 ok(GetObject(hbmp, sizeof(bmp), &bmp) == sizeof(bmp),
1196 "GetObject failed or returned a wrong structure size\n");
1197 ok(!bmp.bmBits, "bmBits must be NULL for device-dependent bitmaps\n");
1199 DeleteObject(hbmp);
1202 static void test_GetDIBits_selected_DIB(UINT bpp)
1204 HBITMAP dib;
1205 BITMAPINFO * info;
1206 BITMAPINFO * info2;
1207 void * bits;
1208 void * bits2;
1209 UINT dib_size;
1210 HDC dib_dc, dc;
1211 HBITMAP old_bmp;
1212 BOOL equalContents;
1213 UINT i;
1214 int res;
1216 /* Create a DIB section with a color table */
1218 info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1219 info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + (1 << bpp) * sizeof(RGBQUAD));
1220 assert(info);
1221 assert(info2);
1223 info->bmiHeader.biSize = sizeof(info->bmiHeader);
1225 /* Choose width and height such that the row length (in bytes)
1226 is a multiple of 4 (makes things easier) */
1227 info->bmiHeader.biWidth = 32;
1228 info->bmiHeader.biHeight = 32;
1229 info->bmiHeader.biPlanes = 1;
1230 info->bmiHeader.biBitCount = bpp;
1231 info->bmiHeader.biCompression = BI_RGB;
1233 for (i=0; i < (1u << bpp); i++)
1235 BYTE c = i * (1 << (8 - bpp));
1236 info->bmiColors[i].rgbRed = c;
1237 info->bmiColors[i].rgbGreen = c;
1238 info->bmiColors[i].rgbBlue = c;
1239 info->bmiColors[i].rgbReserved = 0;
1242 dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
1243 assert(dib);
1244 dib_size = bpp * (info->bmiHeader.biWidth * info->bmiHeader.biHeight) / 8;
1246 /* Set the bits of the DIB section */
1247 for (i=0; i < dib_size; i++)
1249 ((BYTE *)bits)[i] = i % 256;
1252 /* Select the DIB into a DC */
1253 dib_dc = CreateCompatibleDC(NULL);
1254 old_bmp = SelectObject(dib_dc, dib);
1255 dc = CreateCompatibleDC(NULL);
1256 bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dib_size);
1257 assert(bits2);
1259 /* Copy the DIB attributes but not the color table */
1260 memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1262 res = GetDIBits(dc, dib, 0, info->bmiHeader.biHeight, bits2, info2, DIB_RGB_COLORS);
1263 ok(res, "GetDIBits failed\n");
1265 /* Compare the color table and the bits */
1266 equalContents = TRUE;
1267 for (i=0; i < (1u << bpp); i++)
1269 if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1270 || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1271 || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1272 || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1274 equalContents = FALSE;
1275 break;
1278 ok(equalContents, "GetDIBits with DIB selected in DC: Invalid DIB color table\n");
1280 equalContents = TRUE;
1281 for (i=0; i < dib_size / sizeof(DWORD); i++)
1283 if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1285 equalContents = FALSE;
1286 break;
1289 ok(equalContents, "GetDIBits with %d bpp DIB selected in DC: Invalid DIB bits\n",bpp);
1291 HeapFree(GetProcessHeap(), 0, bits2);
1292 DeleteDC(dc);
1294 SelectObject(dib_dc, old_bmp);
1295 DeleteDC(dib_dc);
1296 DeleteObject(dib);
1298 HeapFree(GetProcessHeap(), 0, info2);
1299 HeapFree(GetProcessHeap(), 0, info);
1302 static void test_GetDIBits_selected_DDB(BOOL monochrome)
1304 HBITMAP ddb;
1305 BITMAPINFO * info;
1306 BITMAPINFO * info2;
1307 void * bits;
1308 void * bits2;
1309 HDC ddb_dc, dc;
1310 HBITMAP old_bmp;
1311 BOOL equalContents;
1312 UINT width, height;
1313 UINT bpp;
1314 UINT i, j;
1315 int res;
1317 width = height = 16;
1319 /* Create a DDB (device-dependent bitmap) */
1320 if (monochrome)
1322 bpp = 1;
1323 ddb = CreateBitmap(width, height, 1, 1, NULL);
1325 else
1327 HDC screen_dc = GetDC(NULL);
1328 bpp = GetDeviceCaps(screen_dc, BITSPIXEL) * GetDeviceCaps(screen_dc, PLANES);
1329 ddb = CreateCompatibleBitmap(screen_dc, width, height);
1330 ReleaseDC(NULL, screen_dc);
1333 /* Set the pixels */
1334 ddb_dc = CreateCompatibleDC(NULL);
1335 old_bmp = SelectObject(ddb_dc, ddb);
1336 for (i = 0; i < width; i++)
1338 for (j=0; j < height; j++)
1340 BYTE c = (i * width + j) % 256;
1341 SetPixelV(ddb_dc, i, j, RGB(c, c, c));
1344 SelectObject(ddb_dc, old_bmp);
1346 info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1347 info2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1348 assert(info);
1349 assert(info2);
1351 info->bmiHeader.biSize = sizeof(info->bmiHeader);
1352 info->bmiHeader.biWidth = width;
1353 info->bmiHeader.biHeight = height;
1354 info->bmiHeader.biPlanes = 1;
1355 info->bmiHeader.biBitCount = bpp;
1356 info->bmiHeader.biCompression = BI_RGB;
1358 dc = CreateCompatibleDC(NULL);
1360 /* Fill in biSizeImage */
1361 GetDIBits(dc, ddb, 0, height, NULL, info, DIB_RGB_COLORS);
1362 ok(info->bmiHeader.biSizeImage != 0, "GetDIBits failed to get the DIB attributes\n");
1364 bits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1365 bits2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage);
1366 assert(bits);
1367 assert(bits2);
1369 /* Get the bits */
1370 res = GetDIBits(dc, ddb, 0, height, bits, info, DIB_RGB_COLORS);
1371 ok(res, "GetDIBits failed\n");
1373 /* Copy the DIB attributes but not the color table */
1374 memcpy(info2, info, sizeof(BITMAPINFOHEADER));
1376 /* Select the DDB into another DC */
1377 old_bmp = SelectObject(ddb_dc, ddb);
1379 /* Get the bits */
1380 res = GetDIBits(dc, ddb, 0, height, bits2, info2, DIB_RGB_COLORS);
1381 ok(res, "GetDIBits failed\n");
1383 /* Compare the color table and the bits */
1384 if (bpp <= 8)
1386 equalContents = TRUE;
1387 for (i=0; i < (1u << bpp); i++)
1389 if ((info->bmiColors[i].rgbRed != info2->bmiColors[i].rgbRed)
1390 || (info->bmiColors[i].rgbGreen != info2->bmiColors[i].rgbGreen)
1391 || (info->bmiColors[i].rgbBlue != info2->bmiColors[i].rgbBlue)
1392 || (info->bmiColors[i].rgbReserved != info2->bmiColors[i].rgbReserved))
1394 equalContents = FALSE;
1395 break;
1398 ok(equalContents, "GetDIBits with DDB selected in DC: Got a different color table\n");
1401 equalContents = TRUE;
1402 for (i=0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++)
1404 if (((DWORD *)bits)[i] != ((DWORD *)bits2)[i])
1406 equalContents = FALSE;
1409 ok(equalContents, "GetDIBits with DDB selected in DC: Got different DIB bits\n");
1411 /* Test the palette */
1412 equalContents = TRUE;
1413 if (info2->bmiHeader.biBitCount <= 8)
1415 WORD *colors = (WORD*)info2->bmiColors;
1417 /* Get the palette indices */
1418 res = GetDIBits(dc, ddb, 0, 0, NULL, info2, DIB_PAL_COLORS);
1419 if (res == 0 && GetLastError() == ERROR_INVALID_PARAMETER) /* Win9x */
1420 res = GetDIBits(dc, ddb, 0, height, NULL, info2, DIB_PAL_COLORS);
1421 ok(res, "GetDIBits failed\n");
1423 for (i=0;i < 1 << info->bmiHeader.biSizeImage; i++)
1425 if (colors[i] != i)
1427 equalContents = FALSE;
1428 break;
1433 ok(equalContents, "GetDIBits with DDB selected in DC: non 1:1 palette indices\n");
1435 HeapFree(GetProcessHeap(), 0, bits2);
1436 HeapFree(GetProcessHeap(), 0, bits);
1437 DeleteDC(dc);
1439 SelectObject(ddb_dc, old_bmp);
1440 DeleteDC(ddb_dc);
1441 DeleteObject(ddb);
1443 HeapFree(GetProcessHeap(), 0, info2);
1444 HeapFree(GetProcessHeap(), 0, info);
1447 static void test_GetDIBits(void)
1449 /* 2-bytes aligned 1-bit bitmap data: 16x16 */
1450 static const BYTE bmp_bits_1[16 * 2] =
1452 0xff,0xff, 0,0, 0xff,0xff, 0,0,
1453 0xff,0xff, 0,0, 0xff,0xff, 0,0,
1454 0xff,0xff, 0,0, 0xff,0xff, 0,0,
1455 0xff,0xff, 0,0, 0xff,0xff, 0,0
1457 /* 4-bytes aligned 1-bit DIB data: 16x16 */
1458 static const BYTE dib_bits_1[16 * 4] =
1460 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1461 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1462 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0,
1463 0,0,0,0, 0xff,0xff,0,0, 0,0,0,0, 0xff,0xff,0,0
1465 static const BYTE dib_bits_1_9x[16 * 4] =
1467 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa,
1468 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa,
1469 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa,
1470 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa, 0,0,0xaa,0xaa, 0xff,0xff,0xaa,0xaa
1472 /* 2-bytes aligned 24-bit bitmap data: 16x16 */
1473 static const BYTE bmp_bits_24[16 * 16*3] =
1475 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1476 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1477 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1478 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1479 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1480 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1481 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1482 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1483 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1484 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1485 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1486 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1487 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1488 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1489 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1490 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1491 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1492 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1493 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 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1495 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1496 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
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
1508 /* 4-bytes aligned 24-bit DIB data: 16x16 */
1509 static const BYTE dib_bits_24[16 * 16*3] =
1511 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1512 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1513 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1514 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1515 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1516 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1517 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1518 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1519 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1520 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1521 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1522 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1523 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1524 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1525 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1526 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1527 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1528 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1529 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 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1531 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1532 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1533 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1534 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1535 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1536 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1537 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1538 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1539 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1540 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1541 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
1542 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
1544 HBITMAP hbmp;
1545 BITMAP bm;
1546 HDC hdc;
1547 int i, bytes, lines;
1548 BYTE buf[1024];
1549 char bi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
1550 BITMAPINFO *bi = (BITMAPINFO *)bi_buf;
1552 hdc = GetDC(0);
1554 /* 1-bit source bitmap data */
1555 hbmp = CreateBitmap(16, 16, 1, 1, bmp_bits_1);
1556 ok(hbmp != 0, "CreateBitmap failed\n");
1558 memset(&bm, 0xAA, sizeof(bm));
1559 bytes = GetObject(hbmp, sizeof(bm), &bm);
1560 ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1561 ok(bm.bmType == 0, "wrong bmType %d\n", bm.bmType);
1562 ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1563 ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1564 ok(bm.bmWidthBytes == 2, "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1565 ok(bm.bmPlanes == 1, "wrong bmPlanes %u\n", bm.bmPlanes);
1566 ok(bm.bmBitsPixel == 1, "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1567 ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1569 bytes = GetBitmapBits(hbmp, 0, NULL);
1570 ok(bytes == sizeof(bmp_bits_1) || broken(bytes == 0 /* Win9x */), "expected 16*2 got %d bytes\n", bytes);
1571 bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1572 ok(bytes == sizeof(bmp_bits_1), "expected 16*2 got %d bytes\n", bytes);
1573 ok(!memcmp(buf, bmp_bits_1, sizeof(bmp_bits_1)), "bitmap bits don't match\n");
1575 /* retrieve 1-bit DIB data */
1576 memset(bi, 0, sizeof(*bi));
1577 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1578 bi->bmiHeader.biWidth = bm.bmWidth;
1579 bi->bmiHeader.biHeight = bm.bmHeight;
1580 bi->bmiHeader.biPlanes = 1;
1581 bi->bmiHeader.biBitCount = 1;
1582 bi->bmiHeader.biCompression = BI_RGB;
1583 bi->bmiHeader.biSizeImage = 0;
1584 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1585 SetLastError(0xdeadbeef);
1586 lines = GetDIBits(0, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1587 ok(lines == 0, "GetDIBits copied %d lines with hdc = 0\n", lines);
1588 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
1589 broken(GetLastError() == 0xdeadbeef), /* winnt */
1590 "wrong error %u\n", GetLastError());
1591 ok(bi->bmiHeader.biSizeImage == 0, "expected 0, got %u\n", bi->bmiHeader.biSizeImage);
1593 memset(buf, 0xAA, sizeof(buf));
1594 SetLastError(0xdeadbeef);
1595 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1596 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1597 lines, bm.bmHeight, GetLastError());
1598 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1) ||
1599 broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1600 "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1602 /* the color table consists of black and white */
1603 ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1604 bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1605 "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1606 bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1607 bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1608 ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1609 bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1610 "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1611 bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1612 bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1613 for (i = 2; i < 256; i++)
1615 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1616 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1617 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1618 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1619 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1622 /* returned bits are DWORD aligned and upside down */
1623 ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)) ||
1624 broken(!memcmp(buf, dib_bits_1_9x, sizeof(dib_bits_1_9x))), /* Win9x, WinME */
1625 "DIB bits don't match\n");
1627 /* Test the palette indices */
1628 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1629 SetLastError(0xdeadbeef);
1630 lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1631 if (lines == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
1632 win_skip("Win9x/WinMe doesn't handle 0 for the number of scan lines\n");
1633 else
1635 ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1636 ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1638 for (i = 2; i < 256; i++)
1639 ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[1]);
1641 /* retrieve 24-bit DIB data */
1642 memset(bi, 0, sizeof(*bi));
1643 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1644 bi->bmiHeader.biWidth = bm.bmWidth;
1645 bi->bmiHeader.biHeight = bm.bmHeight;
1646 bi->bmiHeader.biPlanes = 1;
1647 bi->bmiHeader.biBitCount = 24;
1648 bi->bmiHeader.biCompression = BI_RGB;
1649 bi->bmiHeader.biSizeImage = 0;
1650 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1651 memset(buf, 0xAA, sizeof(buf));
1652 SetLastError(0xdeadbeef);
1653 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1654 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1655 lines, bm.bmHeight, GetLastError());
1656 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24) ||
1657 broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1658 "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1660 /* the color table doesn't exist for 24-bit images */
1661 for (i = 0; i < 256; i++)
1663 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1664 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1665 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1666 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1667 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1670 /* returned bits are DWORD aligned and upside down */
1671 ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1672 DeleteObject(hbmp);
1674 /* 24-bit source bitmap data */
1675 hbmp = CreateCompatibleBitmap(hdc, 16, 16);
1676 ok(hbmp != 0, "CreateBitmap failed\n");
1677 SetLastError(0xdeadbeef);
1678 bi->bmiHeader.biHeight = -bm.bmHeight; /* indicate bottom-up data */
1679 lines = SetDIBits(hdc, hbmp, 0, bm.bmHeight, bmp_bits_24, bi, DIB_RGB_COLORS);
1680 ok(lines == bm.bmHeight, "SetDIBits copied %d lines of %d, error %u\n",
1681 lines, bm.bmHeight, GetLastError());
1683 memset(&bm, 0xAA, sizeof(bm));
1684 bytes = GetObject(hbmp, sizeof(bm), &bm);
1685 ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
1686 ok(bm.bmType == 0 ||
1687 broken(bm.bmType == 21072), /* win9x */
1688 "wrong bmType %d\n", bm.bmType);
1689 ok(bm.bmWidth == 16, "wrong bmWidth %d\n", bm.bmWidth);
1690 ok(bm.bmHeight == 16, "wrong bmHeight %d\n", bm.bmHeight);
1691 ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
1692 ok(bm.bmPlanes == GetDeviceCaps(hdc, PLANES), "wrong bmPlanes %u\n", bm.bmPlanes);
1693 ok(bm.bmBitsPixel == GetDeviceCaps(hdc, BITSPIXEL), "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
1694 ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
1696 bytes = GetBitmapBits(hbmp, 0, NULL);
1697 ok(bytes == bm.bmWidthBytes * bm.bmHeight ||
1698 broken(bytes == 0), /* win9x */
1699 "expected %d got %d bytes\n", bm.bmWidthBytes * bm.bmHeight, bytes);
1700 bytes = GetBitmapBits(hbmp, sizeof(buf), buf);
1701 ok(bytes == bm.bmWidthBytes * bm.bmHeight, "expected %d got %d bytes\n",
1702 bm.bmWidthBytes * bm.bmHeight, bytes);
1704 /* retrieve 1-bit DIB data */
1705 memset(bi, 0, sizeof(*bi));
1706 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1707 bi->bmiHeader.biWidth = bm.bmWidth;
1708 bi->bmiHeader.biHeight = bm.bmHeight;
1709 bi->bmiHeader.biPlanes = 1;
1710 bi->bmiHeader.biBitCount = 1;
1711 bi->bmiHeader.biCompression = BI_RGB;
1712 bi->bmiHeader.biSizeImage = 0;
1713 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1714 memset(buf, 0xAA, sizeof(buf));
1715 SetLastError(0xdeadbeef);
1716 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1717 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1718 lines, bm.bmHeight, GetLastError());
1719 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_1) ||
1720 broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1721 "expected 16*4, got %u\n", bi->bmiHeader.biSizeImage);
1723 /* the color table consists of black and white */
1724 ok(bi->bmiColors[0].rgbRed == 0 && bi->bmiColors[0].rgbGreen == 0 &&
1725 bi->bmiColors[0].rgbBlue == 0 && bi->bmiColors[0].rgbReserved == 0,
1726 "expected bmiColors[0] 0,0,0,0 - got %x %x %x %x\n",
1727 bi->bmiColors[0].rgbRed, bi->bmiColors[0].rgbGreen,
1728 bi->bmiColors[0].rgbBlue, bi->bmiColors[0].rgbReserved);
1729 ok(bi->bmiColors[1].rgbRed == 0xff && bi->bmiColors[1].rgbGreen == 0xff &&
1730 bi->bmiColors[1].rgbBlue == 0xff && bi->bmiColors[1].rgbReserved == 0,
1731 "expected bmiColors[0] 0xff,0xff,0xff,0 - got %x %x %x %x\n",
1732 bi->bmiColors[1].rgbRed, bi->bmiColors[1].rgbGreen,
1733 bi->bmiColors[1].rgbBlue, bi->bmiColors[1].rgbReserved);
1734 for (i = 2; i < 256; i++)
1736 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1737 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1738 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1739 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1740 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1743 /* returned bits are DWORD aligned and upside down */
1744 todo_wine
1745 ok(!memcmp(buf, dib_bits_1, sizeof(dib_bits_1)) ||
1746 broken(!memcmp(buf, dib_bits_1_9x, sizeof(dib_bits_1_9x))), /* Win9x, WinME */
1747 "DIB bits don't match\n");
1749 /* Test the palette indices */
1750 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1751 SetLastError(0xdeadbeef);
1752 lines = GetDIBits(hdc, hbmp, 0, 0, NULL, bi, DIB_PAL_COLORS);
1753 if (lines == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
1754 win_skip("Win9x/WinMe doesn't handle 0 for the number of scan lines\n");
1755 else
1757 ok(((WORD*)bi->bmiColors)[0] == 0, "Color 0 is %d\n", ((WORD*)bi->bmiColors)[0]);
1758 ok(((WORD*)bi->bmiColors)[1] == 1, "Color 1 is %d\n", ((WORD*)bi->bmiColors)[1]);
1760 for (i = 2; i < 256; i++)
1761 ok(((WORD*)bi->bmiColors)[i] == 0xAAAA, "Color %d is %d\n", i, ((WORD*)bi->bmiColors)[i]);
1763 /* retrieve 24-bit DIB data */
1764 memset(bi, 0, sizeof(*bi));
1765 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1766 bi->bmiHeader.biWidth = bm.bmWidth;
1767 bi->bmiHeader.biHeight = bm.bmHeight;
1768 bi->bmiHeader.biPlanes = 1;
1769 bi->bmiHeader.biBitCount = 24;
1770 bi->bmiHeader.biCompression = BI_RGB;
1771 bi->bmiHeader.biSizeImage = 0;
1772 memset(bi->bmiColors, 0xAA, sizeof(RGBQUAD) * 256);
1773 memset(buf, 0xAA, sizeof(buf));
1774 SetLastError(0xdeadbeef);
1775 lines = GetDIBits(hdc, hbmp, 0, bm.bmHeight, buf, bi, DIB_RGB_COLORS);
1776 ok(lines == bm.bmHeight, "GetDIBits copied %d lines of %d, error %u\n",
1777 lines, bm.bmHeight, GetLastError());
1778 ok(bi->bmiHeader.biSizeImage == sizeof(dib_bits_24) ||
1779 broken(bi->bmiHeader.biSizeImage == 0), /* win9x */
1780 "expected 16*16*3, got %u\n", bi->bmiHeader.biSizeImage);
1782 /* the color table doesn't exist for 24-bit images */
1783 for (i = 0; i < 256; i++)
1785 ok(bi->bmiColors[i].rgbRed == 0xAA && bi->bmiColors[i].rgbGreen == 0xAA &&
1786 bi->bmiColors[i].rgbBlue == 0xAA && bi->bmiColors[i].rgbReserved == 0xAA,
1787 "expected bmiColors[%d] 0xAA,0xAA,0xAA,0xAA - got %x %x %x %x\n", i,
1788 bi->bmiColors[i].rgbRed, bi->bmiColors[i].rgbGreen,
1789 bi->bmiColors[i].rgbBlue, bi->bmiColors[i].rgbReserved);
1792 /* returned bits are DWORD aligned and upside down */
1793 ok(!memcmp(buf, dib_bits_24, sizeof(dib_bits_24)), "DIB bits don't match\n");
1794 DeleteObject(hbmp);
1796 ReleaseDC(0, hdc);
1799 static void test_GetDIBits_BI_BITFIELDS(void)
1801 /* Try a screen resolution detection technique
1802 * from the September 1999 issue of Windows Developer's Journal
1803 * which seems to be in widespread use.
1804 * http://www.lesher.ws/highcolor.html
1805 * http://www.lesher.ws/vidfmt.c
1806 * It hinges on being able to retrieve the bitmaps
1807 * for the three primary colors in non-paletted 16 bit mode.
1809 char dibinfo_buf[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
1810 DWORD bits[32];
1811 LPBITMAPINFO dibinfo = (LPBITMAPINFO) dibinfo_buf;
1812 DWORD *bitmasks = (DWORD *)dibinfo->bmiColors;
1813 HDC hdc;
1814 HBITMAP hbm;
1815 int ret;
1816 void *ptr;
1818 memset(dibinfo, 0, sizeof(dibinfo_buf));
1819 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1821 hdc = GetDC(NULL);
1822 ok(hdc != NULL, "GetDC failed?\n");
1823 hbm = CreateCompatibleBitmap(hdc, 1, 1);
1824 ok(hbm != NULL, "CreateCompatibleBitmap failed?\n");
1826 /* Call GetDIBits to fill in bmiHeader. */
1827 ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1828 ok(ret == 1, "GetDIBits failed\n");
1829 if (dibinfo->bmiHeader.biBitCount > 8)
1831 ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS,
1832 "compression is %u\n", dibinfo->bmiHeader.biCompression );
1834 ok( !bitmasks[0], "red mask is set\n" );
1835 ok( !bitmasks[1], "green mask is set\n" );
1836 ok( !bitmasks[2], "blue mask is set\n" );
1838 /* test with NULL bits pointer and correct bpp */
1839 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1840 ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1841 ok(ret == 1, "GetDIBits failed\n");
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, "size image not set\n" );
1848 /* test with valid bits pointer */
1849 memset(dibinfo, 0, sizeof(dibinfo_buf));
1850 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1851 ret = GetDIBits(hdc, hbm, 0, 1, NULL, dibinfo, DIB_RGB_COLORS);
1852 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1853 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1854 ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1855 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1857 ok( bitmasks[0] != 0, "red mask is not set\n" );
1858 ok( bitmasks[1] != 0, "green mask is not set\n" );
1859 ok( bitmasks[2] != 0, "blue mask is not set\n" );
1860 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef ||
1861 broken(dibinfo->bmiHeader.biSizeImage == 0xdeadbeef), /* win9x */
1862 "size image not set\n" );
1864 /* now with bits and 0 lines */
1865 memset(dibinfo, 0, sizeof(dibinfo_buf));
1866 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1867 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1868 SetLastError(0xdeadbeef);
1869 ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1870 if (ret == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
1871 win_skip("Win9x/WinMe doesn't handle 0 for the number of scan lines\n");
1872 else
1874 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1876 ok( !bitmasks[0], "red mask is set\n" );
1877 ok( !bitmasks[1], "green mask is set\n" );
1878 ok( !bitmasks[2], "blue mask is set\n" );
1879 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1881 memset(bitmasks, 0, 3*sizeof(DWORD));
1882 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1883 ret = GetDIBits(hdc, hbm, 0, 0, bits, dibinfo, DIB_RGB_COLORS);
1884 ok(ret == 1, "GetDIBits failed ret %u err %u\n",ret,GetLastError());
1886 ok( bitmasks[0] != 0, "red mask is not set\n" );
1887 ok( bitmasks[1] != 0, "green mask is not set\n" );
1888 ok( bitmasks[2] != 0, "blue mask is not set\n" );
1889 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1892 else skip("not in 16 bpp BI_BITFIELDS mode, skipping that test\n");
1894 DeleteObject(hbm);
1896 /* same thing now with a 32-bpp DIB section */
1898 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1899 dibinfo->bmiHeader.biWidth = 1;
1900 dibinfo->bmiHeader.biHeight = 1;
1901 dibinfo->bmiHeader.biPlanes = 1;
1902 dibinfo->bmiHeader.biBitCount = 32;
1903 dibinfo->bmiHeader.biCompression = BI_RGB;
1904 dibinfo->bmiHeader.biSizeImage = 0;
1905 dibinfo->bmiHeader.biXPelsPerMeter = 0;
1906 dibinfo->bmiHeader.biYPelsPerMeter = 0;
1907 dibinfo->bmiHeader.biClrUsed = 0;
1908 dibinfo->bmiHeader.biClrImportant = 0;
1909 bitmasks[0] = 0x0000ff;
1910 bitmasks[1] = 0x00ff00;
1911 bitmasks[2] = 0xff0000;
1912 hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
1913 ok( hbm != 0, "failed to create bitmap\n" );
1915 memset(dibinfo, 0, sizeof(dibinfo_buf));
1916 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1917 ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
1918 ok(ret == 1, "GetDIBits failed\n");
1919 ok( dibinfo->bmiHeader.biBitCount == 32, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
1921 ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS,
1922 "compression is %u\n", dibinfo->bmiHeader.biCompression );
1923 ok( !bitmasks[0], "red mask is set\n" );
1924 ok( !bitmasks[1], "green mask is set\n" );
1925 ok( !bitmasks[2], "blue mask is set\n" );
1927 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1928 ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1929 ok(ret == 1, "GetDIBits failed\n");
1930 ok( dibinfo->bmiHeader.biBitCount == 32, "wrong bit count %u\n", dibinfo->bmiHeader.biBitCount );
1931 ok( bitmasks[0] == 0xff0000, "wrong red mask %08x\n", bitmasks[0] );
1932 ok( bitmasks[1] == 0x00ff00, "wrong green mask %08x\n", bitmasks[1] );
1933 ok( bitmasks[2] == 0x0000ff, "wrong blue mask %08x\n", bitmasks[2] );
1934 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef ||
1935 broken(dibinfo->bmiHeader.biSizeImage == 0xdeadbeef), /* win9x */
1936 "size image not set\n" );
1938 DeleteObject(hbm);
1940 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1941 dibinfo->bmiHeader.biWidth = 1;
1942 dibinfo->bmiHeader.biHeight = 1;
1943 dibinfo->bmiHeader.biPlanes = 1;
1944 dibinfo->bmiHeader.biBitCount = 32;
1945 dibinfo->bmiHeader.biCompression = BI_BITFIELDS;
1946 dibinfo->bmiHeader.biSizeImage = 0;
1947 dibinfo->bmiHeader.biXPelsPerMeter = 0;
1948 dibinfo->bmiHeader.biYPelsPerMeter = 0;
1949 dibinfo->bmiHeader.biClrUsed = 0;
1950 dibinfo->bmiHeader.biClrImportant = 0;
1951 bitmasks[0] = 0x0000ff;
1952 bitmasks[1] = 0x00ff00;
1953 bitmasks[2] = 0xff0000;
1954 hbm = CreateDIBSection( hdc, dibinfo, DIB_RGB_COLORS, &ptr, NULL, 0 );
1955 ok( hbm != 0 || broken(!hbm), /* win9x */ "failed to create bitmap\n" );
1957 if (hbm)
1959 memset(dibinfo, 0, sizeof(dibinfo_buf));
1960 dibinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1961 ret = GetDIBits(hdc, hbm, 0, 0, NULL, dibinfo, DIB_RGB_COLORS);
1962 ok(ret == 1, "GetDIBits failed\n");
1964 ok( dibinfo->bmiHeader.biCompression == BI_BITFIELDS,
1965 "compression is %u\n", dibinfo->bmiHeader.biCompression );
1966 ok( !bitmasks[0], "red mask is set\n" );
1967 ok( !bitmasks[1], "green mask is set\n" );
1968 ok( !bitmasks[2], "blue mask is set\n" );
1970 dibinfo->bmiHeader.biSizeImage = 0xdeadbeef;
1971 ret = GetDIBits(hdc, hbm, 0, 1, bits, dibinfo, DIB_RGB_COLORS);
1972 ok(ret == 1, "GetDIBits failed\n");
1973 ok( bitmasks[0] == 0x0000ff, "wrong red mask %08x\n", bitmasks[0] );
1974 ok( bitmasks[1] == 0x00ff00, "wrong green mask %08x\n", bitmasks[1] );
1975 ok( bitmasks[2] == 0xff0000, "wrong blue mask %08x\n", bitmasks[2] );
1976 ok( dibinfo->bmiHeader.biSizeImage != 0xdeadbeef, "size image not set\n" );
1978 DeleteObject(hbm);
1981 ReleaseDC(NULL, hdc);
1984 static void test_select_object(void)
1986 HDC hdc;
1987 HBITMAP hbm, hbm_old;
1988 INT planes, bpp, i;
1989 DWORD depths[] = {8, 15, 16, 24, 32};
1990 BITMAP bm;
1991 DWORD bytes;
1993 hdc = GetDC(0);
1994 ok(hdc != 0, "GetDC(0) failed\n");
1995 hbm = CreateCompatibleBitmap(hdc, 10, 10);
1996 ok(hbm != 0, "CreateCompatibleBitmap failed\n");
1998 hbm_old = SelectObject(hdc, hbm);
1999 ok(hbm_old == 0, "SelectObject should fail\n");
2001 DeleteObject(hbm);
2002 ReleaseDC(0, hdc);
2004 hdc = CreateCompatibleDC(0);
2005 ok(hdc != 0, "GetDC(0) failed\n");
2006 hbm = CreateCompatibleBitmap(hdc, 10, 10);
2007 ok(hbm != 0, "CreateCompatibleBitmap failed\n");
2009 hbm_old = SelectObject(hdc, hbm);
2010 ok(hbm_old != 0, "SelectObject failed\n");
2011 hbm_old = SelectObject(hdc, hbm_old);
2012 ok(hbm_old == hbm, "SelectObject failed\n");
2014 DeleteObject(hbm);
2016 /* test an 1-bpp bitmap */
2017 planes = GetDeviceCaps(hdc, PLANES);
2018 bpp = 1;
2020 hbm = CreateBitmap(10, 10, planes, bpp, NULL);
2021 ok(hbm != 0, "CreateBitmap failed\n");
2023 hbm_old = SelectObject(hdc, hbm);
2024 ok(hbm_old != 0, "SelectObject failed\n");
2025 hbm_old = SelectObject(hdc, hbm_old);
2026 ok(hbm_old == hbm, "SelectObject failed\n");
2028 DeleteObject(hbm);
2030 for(i = 0; i < sizeof(depths)/sizeof(depths[0]); i++) {
2031 /* test a color bitmap to dc bpp matching */
2032 planes = GetDeviceCaps(hdc, PLANES);
2033 bpp = GetDeviceCaps(hdc, BITSPIXEL);
2035 hbm = CreateBitmap(10, 10, planes, depths[i], NULL);
2036 ok(hbm != 0, "CreateBitmap failed\n");
2038 hbm_old = SelectObject(hdc, hbm);
2039 if(depths[i] == bpp ||
2040 (bpp == 16 && depths[i] == 15) /* 16 and 15 bpp are compatible */
2042 ok(hbm_old != 0, "SelectObject failed, BITSPIXEL: %d, created depth: %d\n", bpp, depths[i]);
2043 SelectObject(hdc, hbm_old);
2044 } else {
2045 ok(hbm_old == 0, "SelectObject should fail. BITSPIXELS: %d, created depth: %d\n", bpp, depths[i]);
2048 memset(&bm, 0xAA, sizeof(bm));
2049 bytes = GetObject(hbm, sizeof(bm), &bm);
2050 ok(bytes == sizeof(bm), "GetObject returned %d\n", bytes);
2051 ok(bm.bmType == 0 ||
2052 broken(bm.bmType == 21072), /* win9x */
2053 "wrong bmType %d\n", bm.bmType);
2054 ok(bm.bmWidth == 10, "wrong bmWidth %d\n", bm.bmWidth);
2055 ok(bm.bmHeight == 10, "wrong bmHeight %d\n", bm.bmHeight);
2056 ok(bm.bmWidthBytes == BITMAP_GetWidthBytes(bm.bmWidth, bm.bmBitsPixel), "wrong bmWidthBytes %d\n", bm.bmWidthBytes);
2057 ok(bm.bmPlanes == planes, "wrong bmPlanes %u\n", bm.bmPlanes);
2058 if(depths[i] == 15) {
2059 ok(bm.bmBitsPixel == 16 ||
2060 broken(bm.bmBitsPixel == 15), /* Win9x/WinME */
2061 "wrong bmBitsPixel %d(15 bpp special)\n", bm.bmBitsPixel);
2062 } else {
2063 ok(bm.bmBitsPixel == depths[i], "wrong bmBitsPixel %d\n", bm.bmBitsPixel);
2065 ok(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
2067 DeleteObject(hbm);
2070 DeleteDC(hdc);
2073 static void test_mono_1x1_bmp_dbg(HBITMAP hbmp, int line)
2075 INT ret;
2076 BITMAP bm;
2078 ret = GetObjectType(hbmp);
2079 ok_(__FILE__, line)(ret == OBJ_BITMAP, "the object %p is not bitmap\n", hbmp);
2081 ret = GetObject(hbmp, 0, 0);
2082 ok_(__FILE__, line)(ret == sizeof(BITMAP) /* XP */ ||
2083 ret == sizeof(DIBSECTION) /* Win9x */, "object size %d\n", ret);
2085 memset(&bm, 0xDA, sizeof(bm));
2086 SetLastError(0xdeadbeef);
2087 ret = GetObject(hbmp, sizeof(bm), &bm);
2088 if (!ret) /* XP, only for curObj2 */ return;
2089 ok_(__FILE__, line)(ret == sizeof(BITMAP) ||
2090 ret == sizeof(DIBSECTION) /* Win9x, only for curObj2 */,
2091 "GetObject returned %d, error %u\n", ret, GetLastError());
2092 ok_(__FILE__, line)(bm.bmType == 0, "wrong bmType, expected 0 got %d\n", bm.bmType);
2093 ok_(__FILE__, line)(bm.bmWidth == 1, "wrong bmWidth, expected 1 got %d\n", bm.bmWidth);
2094 ok_(__FILE__, line)(bm.bmHeight == 1, "wrong bmHeight, expected 1 got %d\n", bm.bmHeight);
2095 ok_(__FILE__, line)(bm.bmWidthBytes == 2, "wrong bmWidthBytes, expected 2 got %d\n", bm.bmWidthBytes);
2096 ok_(__FILE__, line)(bm.bmPlanes == 1, "wrong bmPlanes, expected 1 got %u\n", bm.bmPlanes);
2097 ok_(__FILE__, line)(bm.bmBitsPixel == 1, "wrong bmBitsPixel, expected 1 got %d\n", bm.bmBitsPixel);
2098 ok_(__FILE__, line)(!bm.bmBits, "wrong bmBits %p\n", bm.bmBits);
2101 #define test_mono_1x1_bmp(a) test_mono_1x1_bmp_dbg((a), __LINE__)
2103 static void test_CreateBitmap(void)
2105 BITMAP bmp;
2106 HDC screenDC = GetDC(0);
2107 HDC hdc = CreateCompatibleDC(screenDC);
2108 UINT i, expect = 0;
2110 /* all of these are the stock monochrome bitmap */
2111 HBITMAP bm = CreateCompatibleBitmap(hdc, 0, 0);
2112 HBITMAP bm1 = CreateCompatibleBitmap(screenDC, 0, 0);
2113 HBITMAP bm4 = CreateBitmap(0, 1, 0, 0, 0);
2114 HBITMAP bm5 = CreateDiscardableBitmap(hdc, 0, 0);
2115 HBITMAP curObj1 = GetCurrentObject(hdc, OBJ_BITMAP);
2116 HBITMAP curObj2 = GetCurrentObject(screenDC, OBJ_BITMAP);
2118 /* these 2 are not the stock monochrome bitmap */
2119 HBITMAP bm2 = CreateCompatibleBitmap(hdc, 1, 1);
2120 HBITMAP bm3 = CreateBitmap(1, 1, 1, 1, 0);
2122 HBITMAP old1 = SelectObject(hdc, bm2);
2123 HBITMAP old2 = SelectObject(screenDC, bm3);
2124 SelectObject(hdc, old1);
2125 SelectObject(screenDC, old2);
2127 ok(bm == bm1 && bm == bm4 && bm == bm5 && bm == curObj1 && bm == old1,
2128 "0: %p, 1: %p, 4: %p, 5: %p, curObj1 %p, old1 %p\n",
2129 bm, bm1, bm4, bm5, curObj1, old1);
2130 ok(bm != bm2 && bm != bm3, "0: %p, 2: %p, 3: %p\n", bm, bm2, bm3);
2131 todo_wine
2132 ok(bm != curObj2 || /* WinXP */
2133 broken(bm == curObj2) /* Win9x */,
2134 "0: %p, curObj2 %p\n", bm, curObj2);
2135 ok(old2 == 0, "old2 %p\n", old2);
2137 test_mono_1x1_bmp(bm);
2138 test_mono_1x1_bmp(bm1);
2139 test_mono_1x1_bmp(bm2);
2140 test_mono_1x1_bmp(bm3);
2141 test_mono_1x1_bmp(bm4);
2142 test_mono_1x1_bmp(bm5);
2143 test_mono_1x1_bmp(old1);
2144 test_mono_1x1_bmp(curObj1);
2146 DeleteObject(bm);
2147 DeleteObject(bm1);
2148 DeleteObject(bm2);
2149 DeleteObject(bm3);
2150 DeleteObject(bm4);
2151 DeleteObject(bm5);
2153 DeleteDC(hdc);
2154 ReleaseDC(0, screenDC);
2156 /* show that Windows ignores the provided bm.bmWidthBytes */
2157 bmp.bmType = 0;
2158 bmp.bmWidth = 1;
2159 bmp.bmHeight = 1;
2160 bmp.bmWidthBytes = 28;
2161 bmp.bmPlanes = 1;
2162 bmp.bmBitsPixel = 1;
2163 bmp.bmBits = NULL;
2164 bm = CreateBitmapIndirect(&bmp);
2165 ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2166 test_mono_1x1_bmp(bm);
2167 DeleteObject(bm);
2169 /* Test how the bmBitsPixel field is treated */
2170 for(i = 1; i <= 33; i++) {
2171 bmp.bmType = 0;
2172 bmp.bmWidth = 1;
2173 bmp.bmHeight = 1;
2174 bmp.bmWidthBytes = 28;
2175 bmp.bmPlanes = 1;
2176 bmp.bmBitsPixel = i;
2177 bmp.bmBits = NULL;
2178 SetLastError(0xdeadbeef);
2179 bm = CreateBitmapIndirect(&bmp);
2180 if(i > 32) {
2181 DWORD error = GetLastError();
2182 ok(bm == 0 ||
2183 broken(bm != 0), /* Win9x and WinMe */
2184 "CreateBitmapIndirect for %d bpp succeeded\n", i);
2185 ok(error == ERROR_INVALID_PARAMETER ||
2186 broken(error == 0xdeadbeef), /* Win9x and WinME */
2187 "Got error %d, expected ERROR_INVALID_PARAMETER\n", error);
2188 DeleteObject(bm);
2189 continue;
2191 ok(bm != 0, "CreateBitmapIndirect error %u\n", GetLastError());
2192 GetObject(bm, sizeof(bmp), &bmp);
2193 if(i == 1) {
2194 expect = 1;
2195 } else if(i <= 4) {
2196 expect = 4;
2197 } else if(i <= 8) {
2198 expect = 8;
2199 } else if(i <= 16) {
2200 expect = 16;
2201 } else if(i <= 24) {
2202 expect = 24;
2203 } else if(i <= 32) {
2204 expect = 32;
2206 ok(bmp.bmBitsPixel == expect ||
2207 broken(bmp.bmBitsPixel == i), /* Win9x and WinMe */
2208 "CreateBitmapIndirect for a %d bpp bitmap created a %d bpp bitmap, expected %d\n",
2209 i, bmp.bmBitsPixel, expect);
2210 DeleteObject(bm);
2214 static void test_bitmapinfoheadersize(void)
2216 HBITMAP hdib;
2217 BITMAPINFO bmi;
2218 BITMAPCOREINFO bci;
2219 HDC hdc = GetDC(0);
2221 memset(&bmi, 0, sizeof(BITMAPINFO));
2222 bmi.bmiHeader.biHeight = 100;
2223 bmi.bmiHeader.biWidth = 512;
2224 bmi.bmiHeader.biBitCount = 24;
2225 bmi.bmiHeader.biPlanes = 1;
2227 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER) - 1;
2229 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2230 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2232 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2234 SetLastError(0xdeadbeef);
2235 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2236 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2237 DeleteObject(hdib);
2239 bmi.bmiHeader.biSize++;
2241 SetLastError(0xdeadbeef);
2242 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2243 ok(hdib != NULL ||
2244 broken(!hdib), /* Win98, WinMe */
2245 "CreateDIBSection error %d\n", GetLastError());
2246 DeleteObject(hdib);
2248 bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
2250 SetLastError(0xdeadbeef);
2251 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2252 ok(hdib != NULL ||
2253 broken(!hdib), /* Win98, WinMe */
2254 "CreateDIBSection error %d\n", GetLastError());
2255 DeleteObject(hdib);
2257 bmi.bmiHeader.biSize++;
2259 SetLastError(0xdeadbeef);
2260 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2261 ok(hdib != NULL ||
2262 broken(!hdib), /* Win98, WinMe */
2263 "CreateDIBSection error %d\n", GetLastError());
2264 DeleteObject(hdib);
2266 bmi.bmiHeader.biSize = sizeof(BITMAPV4HEADER);
2268 SetLastError(0xdeadbeef);
2269 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2270 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2271 DeleteObject(hdib);
2273 bmi.bmiHeader.biSize = sizeof(BITMAPV5HEADER);
2275 SetLastError(0xdeadbeef);
2276 hdib = CreateDIBSection(hdc, &bmi, 0, NULL, NULL, 0);
2277 ok(hdib != NULL ||
2278 broken(!hdib), /* Win95 */
2279 "CreateDIBSection error %d\n", GetLastError());
2280 DeleteObject(hdib);
2282 memset(&bci, 0, sizeof(BITMAPCOREINFO));
2283 bci.bmciHeader.bcHeight = 100;
2284 bci.bmciHeader.bcWidth = 512;
2285 bci.bmciHeader.bcBitCount = 24;
2286 bci.bmciHeader.bcPlanes = 1;
2288 bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER) - 1;
2290 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2291 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2293 bci.bmciHeader.bcSize = sizeof(BITMAPCOREHEADER);
2295 SetLastError(0xdeadbeef);
2296 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2297 ok(hdib != NULL, "CreateDIBSection error %d\n", GetLastError());
2298 DeleteObject(hdib);
2300 bci.bmciHeader.bcSize++;
2302 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2303 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2305 bci.bmciHeader.bcSize = sizeof(BITMAPCOREINFO);
2307 hdib = CreateDIBSection(hdc, (BITMAPINFO *)&bci, 0, NULL, NULL, 0);
2308 ok(hdib == NULL, "CreateDIBSection succeeded\n");
2310 ReleaseDC(0, hdc);
2313 static void test_get16dibits(void)
2315 BYTE bits[4 * (16 / sizeof(BYTE))];
2316 HBITMAP hbmp;
2317 HDC screen_dc = GetDC(NULL);
2318 int ret;
2319 BITMAPINFO * info;
2320 int info_len = sizeof(BITMAPINFOHEADER) + 1024;
2321 BYTE *p;
2322 int overwritten_bytes = 0;
2324 memset(bits, 0, sizeof(bits));
2325 hbmp = CreateBitmap(2, 2, 1, 16, bits);
2326 ok(hbmp != NULL, "CreateBitmap failed\n");
2328 info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, info_len);
2329 assert(info);
2331 memset(info, '!', info_len);
2332 memset(info, 0, sizeof(info->bmiHeader));
2334 info->bmiHeader.biSize = sizeof(info->bmiHeader);
2335 info->bmiHeader.biWidth = 2;
2336 info->bmiHeader.biHeight = 2;
2337 info->bmiHeader.biPlanes = 1;
2338 info->bmiHeader.biCompression = BI_RGB;
2340 ret = GetDIBits(screen_dc, hbmp, 0, 0, NULL, info, 0);
2341 ok(ret != 0 ||
2342 broken(ret == 0), /* win9x */
2343 "GetDIBits failed got %d\n", ret);
2345 for (p = ((BYTE *) info) + sizeof(info->bmiHeader); (p - ((BYTE *) info)) < info_len; p++)
2346 if (*p != '!')
2347 overwritten_bytes++;
2348 ok(overwritten_bytes == 0, "GetDIBits wrote past the buffer given\n");
2350 HeapFree(GetProcessHeap(), 0, info);
2351 DeleteObject(hbmp);
2352 ReleaseDC(NULL, screen_dc);
2355 static BOOL compare_buffers_no_alpha(UINT32 *a, UINT32 *b, int length)
2357 int i;
2358 for(i = 0; i < length; i++)
2359 if((a[i] & 0x00FFFFFF) != (b[i] & 0x00FFFFFF))
2360 return FALSE;
2361 return TRUE;
2364 static void check_BitBlt_pixel(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2365 DWORD dwRop, UINT32 expected, int line)
2367 *srcBuffer = 0xFEDCBA98;
2368 *dstBuffer = 0x89ABCDEF;
2369 Rectangle(hdcSrc, 0, 0, 1, 1); /* A null operation to ensure dibs are coerced to X11 */
2370 BitBlt(hdcDst, 0, 0, 1, 1, hdcSrc, 0, 0, dwRop);
2371 ok(expected == *dstBuffer,
2372 "BitBlt with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2373 dwRop, expected, *dstBuffer, line);
2376 static void test_BitBlt(void)
2378 HBITMAP bmpDst, bmpSrc;
2379 HBITMAP oldDst, oldSrc;
2380 HDC hdcScreen, hdcDst, hdcSrc;
2381 UINT32 *dstBuffer, *srcBuffer;
2382 HBRUSH hBrush, hOldBrush;
2383 BITMAPINFO bitmapInfo;
2385 memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2386 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2387 bitmapInfo.bmiHeader.biWidth = 1;
2388 bitmapInfo.bmiHeader.biHeight = 1;
2389 bitmapInfo.bmiHeader.biPlanes = 1;
2390 bitmapInfo.bmiHeader.biBitCount = 32;
2391 bitmapInfo.bmiHeader.biCompression = BI_RGB;
2392 bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
2394 hdcScreen = CreateCompatibleDC(0);
2395 hdcDst = CreateCompatibleDC(hdcScreen);
2396 hdcSrc = CreateCompatibleDC(hdcDst);
2398 /* Setup the destination dib section */
2399 bmpDst = CreateDIBSection(hdcScreen, &bitmapInfo, DIB_RGB_COLORS, (void**)&dstBuffer,
2400 NULL, 0);
2401 oldDst = SelectObject(hdcDst, bmpDst);
2403 hBrush = CreateSolidBrush(0x012345678);
2404 hOldBrush = SelectObject(hdcDst, hBrush);
2406 /* Setup the source dib section */
2407 bmpSrc = CreateDIBSection(hdcScreen, &bitmapInfo, DIB_RGB_COLORS, (void**)&srcBuffer,
2408 NULL, 0);
2409 oldSrc = SelectObject(hdcSrc, bmpSrc);
2411 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2412 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2413 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2414 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2415 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2416 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2417 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2418 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2419 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2420 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2421 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2422 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2423 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2424 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2425 check_BitBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2427 /* Tidy up */
2428 SelectObject(hdcSrc, oldSrc);
2429 DeleteObject(bmpSrc);
2430 DeleteDC(hdcSrc);
2432 SelectObject(hdcDst, hOldBrush);
2433 DeleteObject(hBrush);
2434 SelectObject(hdcDst, oldDst);
2435 DeleteObject(bmpDst);
2436 DeleteDC(hdcDst);
2439 DeleteDC(hdcScreen);
2442 static void check_StretchBlt_pixel(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2443 DWORD dwRop, UINT32 expected, int line)
2445 *srcBuffer = 0xFEDCBA98;
2446 *dstBuffer = 0x89ABCDEF;
2447 StretchBlt(hdcDst, 0, 0, 2, 1, hdcSrc, 0, 0, 1, 1, dwRop);
2448 ok(expected == *dstBuffer,
2449 "StretchBlt with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2450 dwRop, expected, *dstBuffer, line);
2453 static void check_StretchBlt_stretch(HDC hdcDst, HDC hdcSrc, UINT32 *dstBuffer, UINT32 *srcBuffer,
2454 int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,
2455 int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
2456 UINT32 expected[4], UINT32 legacy_expected[4], int line)
2458 memset(dstBuffer, 0, 16);
2459 StretchBlt(hdcDst, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
2460 hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
2461 ok(memcmp(dstBuffer, expected, 16) == 0 ||
2462 broken(compare_buffers_no_alpha(dstBuffer, legacy_expected, 4)),
2463 "StretchBlt expected { %08X, %08X, %08X, %08X } got { %08X, %08X, %08X, %08X } "
2464 "stretching { %d, %d, %d, %d } to { %d, %d, %d, %d } from line %d\n",
2465 expected[0], expected[1], expected[2], expected[3],
2466 dstBuffer[0], dstBuffer[1], dstBuffer[2], dstBuffer[3],
2467 nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2468 nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, line);
2471 static void test_StretchBlt(void)
2473 HBITMAP bmpDst, bmpSrc;
2474 HBITMAP oldDst, oldSrc;
2475 HDC hdcScreen, hdcDst, hdcSrc;
2476 UINT32 *dstBuffer, *srcBuffer;
2477 HBRUSH hBrush, hOldBrush;
2478 BITMAPINFO biDst, biSrc;
2479 UINT32 expected[4], legacy_expected[4];
2481 memset(&biDst, 0, sizeof(BITMAPINFO));
2482 biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2483 biDst.bmiHeader.biWidth = 2;
2484 biDst.bmiHeader.biHeight = -2;
2485 biDst.bmiHeader.biPlanes = 1;
2486 biDst.bmiHeader.biBitCount = 32;
2487 biDst.bmiHeader.biCompression = BI_RGB;
2488 memcpy(&biSrc, &biDst, sizeof(BITMAPINFO));
2490 hdcScreen = CreateCompatibleDC(0);
2491 hdcDst = CreateCompatibleDC(hdcScreen);
2492 hdcSrc = CreateCompatibleDC(hdcDst);
2494 /* Pixel Tests */
2495 bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2496 NULL, 0);
2497 oldDst = SelectObject(hdcDst, bmpDst);
2499 bmpSrc = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&srcBuffer,
2500 NULL, 0);
2501 oldSrc = SelectObject(hdcSrc, bmpSrc);
2503 hBrush = CreateSolidBrush(0x012345678);
2504 hOldBrush = SelectObject(hdcDst, hBrush);
2506 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2507 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2508 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2509 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2510 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2511 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2512 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2513 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2514 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2515 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2516 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2517 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2518 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2519 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2520 check_StretchBlt_pixel(hdcDst, hdcSrc, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2522 SelectObject(hdcDst, hOldBrush);
2523 DeleteObject(hBrush);
2525 /* Top-down to top-down tests */
2526 srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2527 srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2529 expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2530 expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2531 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2532 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2534 expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2535 expected[2] = 0x00000000, expected[3] = 0x00000000;
2536 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2537 0, 0, 1, 1, 0, 0, 1, 1, expected, expected, __LINE__);
2539 expected[0] = 0xCAFED00D, expected[1] = 0xCAFED00D;
2540 expected[2] = 0xCAFED00D, expected[3] = 0xCAFED00D;
2541 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2542 0, 0, 2, 2, 0, 0, 1, 1, expected, expected, __LINE__);
2544 expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2545 expected[2] = 0x00000000, expected[3] = 0x00000000;
2546 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2547 0, 0, 1, 1, 0, 0, 2, 2, expected, expected, __LINE__);
2549 expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2550 expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2551 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2552 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2554 expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2555 expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2556 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2557 1, 1, -2, -2, 0, 0, 2, 2, expected, expected, __LINE__);
2559 /* This result seems broken. One might expect the following result:
2560 * 0xCAFED00D 0xFEEDFACE
2561 * 0xFEDCBA98 0x76543210
2563 expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2564 expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2565 legacy_expected[0] = 0xCAFED00D, legacy_expected[1] = 0x00000000;
2566 legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2567 todo_wine check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2568 1, 1, -2, -2, 1, 1, -2, -2, expected,
2569 legacy_expected, __LINE__);
2571 expected[0] = 0x00000000, expected[1] = 0x00000000;
2572 expected[2] = 0x00000000, expected[3] = 0xCAFED00D;
2573 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2574 1, 1, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2576 SelectObject(hdcDst, oldDst);
2577 DeleteObject(bmpDst);
2579 /* Top-down to bottom-up tests */
2580 biDst.bmiHeader.biHeight = 2;
2581 bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2582 NULL, 0);
2583 oldDst = SelectObject(hdcDst, bmpDst);
2585 expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2586 expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2587 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2588 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2590 expected[0] = 0xFEEDFACE, expected[1] = 0xCAFED00D;
2591 expected[2] = 0x76543210, expected[3] = 0xFEDCBA98;
2592 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2593 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2595 SelectObject(hdcSrc, oldSrc);
2596 DeleteObject(bmpSrc);
2598 /* Bottom-up to bottom-up tests */
2599 biSrc.bmiHeader.biHeight = 2;
2600 bmpSrc = CreateDIBSection(hdcScreen, &biSrc, DIB_RGB_COLORS, (void**)&srcBuffer,
2601 NULL, 0);
2602 srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2603 srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2604 oldSrc = SelectObject(hdcSrc, bmpSrc);
2606 expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2607 expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2608 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2609 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2611 expected[0] = 0x76543210, expected[1] = 0xFEDCBA98;
2612 expected[2] = 0xFEEDFACE, expected[3] = 0xCAFED00D;
2613 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2614 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2616 SelectObject(hdcDst, oldDst);
2617 DeleteObject(bmpDst);
2619 /* Bottom-up to top-down tests */
2620 biDst.bmiHeader.biHeight = -2;
2621 bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2622 NULL, 0);
2623 oldDst = SelectObject(hdcDst, bmpDst);
2625 expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2626 expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2627 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2628 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2630 expected[0] = 0xFEEDFACE, expected[1] = 0xCAFED00D;
2631 expected[2] = 0x76543210, expected[3] = 0xFEDCBA98;
2632 check_StretchBlt_stretch(hdcDst, hdcSrc, dstBuffer, srcBuffer,
2633 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2635 /* Tidy up */
2636 SelectObject(hdcSrc, oldSrc);
2637 DeleteObject(bmpSrc);
2638 DeleteDC(hdcSrc);
2640 SelectObject(hdcDst, oldDst);
2641 DeleteObject(bmpDst);
2642 DeleteDC(hdcDst);
2644 DeleteDC(hdcScreen);
2647 static void check_StretchDIBits_pixel(HDC hdcDst, UINT32 *dstBuffer, UINT32 *srcBuffer,
2648 DWORD dwRop, UINT32 expected, int line)
2650 const UINT32 buffer[2] = { 0xFEDCBA98, 0 };
2651 BITMAPINFO bitmapInfo;
2653 memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2654 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2655 bitmapInfo.bmiHeader.biWidth = 2;
2656 bitmapInfo.bmiHeader.biHeight = 1;
2657 bitmapInfo.bmiHeader.biPlanes = 1;
2658 bitmapInfo.bmiHeader.biBitCount = 32;
2659 bitmapInfo.bmiHeader.biCompression = BI_RGB;
2660 bitmapInfo.bmiHeader.biSizeImage = sizeof(buffer);
2662 *dstBuffer = 0x89ABCDEF;
2664 StretchDIBits(hdcDst, 0, 0, 2, 1, 0, 0, 1, 1, &buffer, &bitmapInfo, DIB_RGB_COLORS, dwRop);
2665 ok(expected == *dstBuffer,
2666 "StretchDIBits with dwRop %06X. Expected 0x%08X, got 0x%08X from line %d\n",
2667 dwRop, expected, *dstBuffer, line);
2670 static void check_StretchDIBits_stretch(HDC hdcDst, UINT32 *dstBuffer, UINT32 *srcBuffer,
2671 int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,
2672 int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
2673 UINT32 expected[4], UINT32 legacy_expected[4], int line)
2675 BITMAPINFO bitmapInfo;
2677 memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
2678 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2679 bitmapInfo.bmiHeader.biWidth = 2;
2680 bitmapInfo.bmiHeader.biHeight = -2;
2681 bitmapInfo.bmiHeader.biPlanes = 1;
2682 bitmapInfo.bmiHeader.biBitCount = 32;
2683 bitmapInfo.bmiHeader.biCompression = BI_RGB;
2685 memset(dstBuffer, 0, 16);
2686 StretchDIBits(hdcDst, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
2687 nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2688 srcBuffer, &bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
2689 ok(memcmp(dstBuffer, expected, 16) == 0 || /* Win2k/XP */
2690 broken(compare_buffers_no_alpha(dstBuffer, legacy_expected, 4)) || /* Win9X/ME */
2691 broken(nWidthSrc < 0 || nHeightSrc < 0), /* Win9X/ME */
2692 "StretchDIBits expected { %08X, %08X, %08X, %08X } got { %08X, %08X, %08X, %08X } "
2693 "stretching { %d, %d, %d, %d } to { %d, %d, %d, %d } from line %d\n",
2694 expected[0], expected[1], expected[2], expected[3],
2695 dstBuffer[0], dstBuffer[1], dstBuffer[2], dstBuffer[3],
2696 nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
2697 nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, line);
2700 static void test_StretchDIBits(void)
2702 HBITMAP bmpDst;
2703 HBITMAP oldDst;
2704 HDC hdcScreen, hdcDst;
2705 UINT32 *dstBuffer, srcBuffer[4];
2706 HBRUSH hBrush, hOldBrush;
2707 BITMAPINFO biDst;
2708 UINT32 expected[4], legacy_expected[4];
2710 memset(&biDst, 0, sizeof(BITMAPINFO));
2711 biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2712 biDst.bmiHeader.biWidth = 2;
2713 biDst.bmiHeader.biHeight = -2;
2714 biDst.bmiHeader.biPlanes = 1;
2715 biDst.bmiHeader.biBitCount = 32;
2716 biDst.bmiHeader.biCompression = BI_RGB;
2718 hdcScreen = CreateCompatibleDC(0);
2719 hdcDst = CreateCompatibleDC(hdcScreen);
2721 /* Pixel Tests */
2722 bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2723 NULL, 0);
2724 oldDst = SelectObject(hdcDst, bmpDst);
2726 hBrush = CreateSolidBrush(0x012345678);
2727 hOldBrush = SelectObject(hdcDst, hBrush);
2729 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCCOPY, 0xFEDCBA98, __LINE__);
2730 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCPAINT, 0xFFFFFFFF, __LINE__);
2731 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCAND, 0x88888888, __LINE__);
2732 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCINVERT, 0x77777777, __LINE__);
2733 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, SRCERASE, 0x76543210, __LINE__);
2734 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, NOTSRCCOPY, 0x01234567, __LINE__);
2735 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, NOTSRCERASE, 0x00000000, __LINE__);
2736 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, MERGECOPY, 0x00581210, __LINE__);
2737 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, MERGEPAINT, 0x89ABCDEF, __LINE__);
2738 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATCOPY, 0x00785634, __LINE__);
2739 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATPAINT, 0x89FBDFFF, __LINE__);
2740 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, PATINVERT, 0x89D39BDB, __LINE__);
2741 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, DSTINVERT, 0x76543210, __LINE__);
2742 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, BLACKNESS, 0x00000000, __LINE__);
2743 check_StretchDIBits_pixel(hdcDst, dstBuffer, srcBuffer, WHITENESS, 0xFFFFFFFF, __LINE__);
2745 SelectObject(hdcDst, hOldBrush);
2746 DeleteObject(hBrush);
2748 /* Top-down destination tests */
2749 srcBuffer[0] = 0xCAFED00D, srcBuffer[1] = 0xFEEDFACE;
2750 srcBuffer[2] = 0xFEDCBA98, srcBuffer[3] = 0x76543210;
2752 expected[0] = 0xCAFED00D, expected[1] = 0xFEEDFACE;
2753 expected[2] = 0xFEDCBA98, expected[3] = 0x76543210;
2754 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2755 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2757 expected[0] = 0xCAFED00D, expected[1] = 0x00000000;
2758 expected[2] = 0x00000000, expected[3] = 0x00000000;
2759 legacy_expected[0] = 0xFEDCBA98, legacy_expected[1] = 0x00000000;
2760 legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2761 todo_wine check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2762 0, 0, 1, 1, 0, 0, 1, 1, expected, legacy_expected, __LINE__);
2764 expected[0] = 0xFEDCBA98, expected[1] = 0xFEDCBA98;
2765 expected[2] = 0xFEDCBA98, expected[3] = 0xFEDCBA98;
2766 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2767 0, 0, 2, 2, 0, 0, 1, 1, expected, expected, __LINE__);
2769 expected[0] = 0x42441000, expected[1] = 0x00000000;
2770 expected[2] = 0x00000000, expected[3] = 0x00000000;
2771 legacy_expected[0] = 0x00543210, legacy_expected[1] = 0x00000000;
2772 legacy_expected[2] = 0x00000000, legacy_expected[3] = 0x00000000;
2773 todo_wine check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2774 0, 0, 1, 1, 0, 0, 2, 2, expected, legacy_expected, __LINE__);
2776 expected[0] = 0x00000000, expected[1] = 0x00000000;
2777 expected[2] = 0x00000000, expected[3] = 0x00000000;
2778 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2779 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2781 expected[0] = 0x00000000, expected[1] = 0x00000000;
2782 expected[2] = 0x00000000, expected[3] = 0x00000000;
2783 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2784 0, 0, 2, 2, 1, 1, -2, -2, expected, expected, __LINE__);
2786 expected[0] = 0x00000000, expected[1] = 0x00000000;
2787 expected[2] = 0x00000000, expected[3] = 0x00000000;
2788 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2789 1, 1, -2, -2, 1, 1, -2, -2, expected, expected, __LINE__);
2791 expected[0] = 0x00000000, expected[1] = 0x00000000;
2792 expected[2] = 0x00000000, expected[3] = 0xCAFED00D;
2793 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2794 1, 1, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2796 SelectObject(hdcDst, oldDst);
2797 DeleteObject(bmpDst);
2799 /* Bottom up destination tests */
2800 biDst.bmiHeader.biHeight = 2;
2801 bmpDst = CreateDIBSection(hdcScreen, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer,
2802 NULL, 0);
2803 oldDst = SelectObject(hdcDst, bmpDst);
2805 expected[0] = 0xFEDCBA98, expected[1] = 0x76543210;
2806 expected[2] = 0xCAFED00D, expected[3] = 0xFEEDFACE;
2807 check_StretchDIBits_stretch(hdcDst, dstBuffer, srcBuffer,
2808 0, 0, 2, 2, 0, 0, 2, 2, expected, expected, __LINE__);
2810 /* Tidy up */
2811 SelectObject(hdcDst, oldDst);
2812 DeleteObject(bmpDst);
2813 DeleteDC(hdcDst);
2815 DeleteDC(hdcScreen);
2818 static void test_GdiAlphaBlend(void)
2820 /* test out-of-bound parameters for GdiAlphaBlend */
2821 HDC hdcNull;
2823 HDC hdcDst;
2824 HBITMAP bmpDst;
2825 HBITMAP oldDst;
2827 BITMAPINFO bmi;
2828 HDC hdcSrc;
2829 HBITMAP bmpSrc;
2830 HBITMAP oldSrc;
2831 LPVOID bits;
2833 BLENDFUNCTION blend;
2835 if (!pGdiAlphaBlend)
2837 win_skip("GdiAlphaBlend() is not implemented\n");
2838 return;
2841 hdcNull = GetDC(NULL);
2842 hdcDst = CreateCompatibleDC(hdcNull);
2843 bmpDst = CreateCompatibleBitmap(hdcNull, 100, 100);
2844 hdcSrc = CreateCompatibleDC(hdcNull);
2846 memset(&bmi, 0, sizeof(bmi)); /* as of Wine 0.9.44 we require the src to be a DIB section */
2847 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
2848 bmi.bmiHeader.biHeight = 20;
2849 bmi.bmiHeader.biWidth = 20;
2850 bmi.bmiHeader.biBitCount = 32;
2851 bmi.bmiHeader.biPlanes = 1;
2852 bmi.bmiHeader.biCompression = BI_RGB;
2853 bmpSrc = CreateDIBSection(hdcDst, &bmi, DIB_RGB_COLORS, &bits, NULL, 0);
2854 ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2856 oldDst = SelectObject(hdcDst, bmpDst);
2857 oldSrc = SelectObject(hdcSrc, bmpSrc);
2859 blend.BlendOp = AC_SRC_OVER;
2860 blend.BlendFlags = 0;
2861 blend.SourceConstantAlpha = 128;
2862 blend.AlphaFormat = 0;
2864 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, 0, 20, 20, blend), TRUE, BOOL, "%d");
2865 SetLastError(0xdeadbeef);
2866 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), FALSE, BOOL, "%d");
2867 expect_eq(GetLastError(), ERROR_INVALID_PARAMETER, int, "%d");
2868 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), FALSE, BOOL, "%d");
2869 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 15, 0, 10, 10, blend), FALSE, BOOL, "%d");
2870 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2871 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 10, 10, -2, 3, blend), FALSE, BOOL, "%d");
2873 SetWindowOrgEx(hdcSrc, -10, -10, NULL);
2874 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 10, 10, blend), TRUE, BOOL, "%d");
2875 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 10, 10, blend), TRUE, BOOL, "%d");
2876 SetMapMode(hdcSrc, MM_ANISOTROPIC);
2877 ScaleWindowExtEx(hdcSrc, 10, 1, 10, 1, NULL);
2878 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, -1, 0, 30, 30, blend), TRUE, BOOL, "%d");
2879 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, hdcSrc, 0, -1, 30, 30, blend), TRUE, BOOL, "%d");
2881 SetLastError(0xdeadbeef);
2882 expect_eq(pGdiAlphaBlend(hdcDst, 0, 0, 20, 20, NULL, 0, 0, 20, 20, blend), FALSE, BOOL, "%d");
2883 expect_eq(GetLastError(), 0xdeadbeef, int, "%d");
2885 SelectObject(hdcDst, oldDst);
2886 SelectObject(hdcSrc, oldSrc);
2887 DeleteObject(bmpSrc);
2888 DeleteObject(bmpDst);
2889 DeleteDC(hdcDst);
2890 DeleteDC(hdcSrc);
2892 ReleaseDC(NULL, hdcNull);
2896 static void test_clipping(void)
2898 HBITMAP bmpDst;
2899 HBITMAP bmpSrc;
2900 HRGN hRgn;
2901 LPVOID bits;
2902 BOOL result;
2904 HDC hdcDst = CreateCompatibleDC( NULL );
2905 HDC hdcSrc = CreateCompatibleDC( NULL );
2907 BITMAPINFO bmpinfo={{0}};
2908 bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2909 bmpinfo.bmiHeader.biWidth = 100;
2910 bmpinfo.bmiHeader.biHeight = 100;
2911 bmpinfo.bmiHeader.biPlanes = 1;
2912 bmpinfo.bmiHeader.biBitCount = GetDeviceCaps( hdcDst, BITSPIXEL );
2913 bmpinfo.bmiHeader.biCompression = BI_RGB;
2915 bmpDst = CreateDIBSection( hdcDst, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2916 ok(bmpDst != NULL, "Couldn't create destination bitmap\n");
2917 SelectObject( hdcDst, bmpDst );
2919 bmpSrc = CreateDIBSection( hdcSrc, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
2920 ok(bmpSrc != NULL, "Couldn't create source bitmap\n");
2921 SelectObject( hdcSrc, bmpSrc );
2923 result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 100, 100, SRCCOPY );
2924 ok(result, "BitBlt failed\n");
2926 hRgn = CreateRectRgn( 0,0,0,0 );
2927 SelectClipRgn( hdcDst, hRgn );
2929 result = BitBlt( hdcDst, 0, 0, 100, 100, hdcSrc, 0, 0, SRCCOPY );
2930 ok(result, "BitBlt failed\n");
2932 DeleteObject( bmpDst );
2933 DeleteObject( bmpSrc );
2934 DeleteObject( hRgn );
2935 DeleteDC( hdcDst );
2936 DeleteDC( hdcSrc );
2939 static void test_32bit_bitmap_blt(void)
2941 BITMAPINFO biDst;
2942 HBITMAP bmpSrc, bmpDst;
2943 HBITMAP oldSrc, oldDst;
2944 HDC hdcSrc, hdcDst, hdcScreen;
2945 UINT32 *dstBuffer;
2946 DWORD colorSrc = 0x11223344;
2948 memset(&biDst, 0, sizeof(BITMAPINFO));
2949 biDst.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2950 biDst.bmiHeader.biWidth = 2;
2951 biDst.bmiHeader.biHeight = -2;
2952 biDst.bmiHeader.biPlanes = 1;
2953 biDst.bmiHeader.biBitCount = 32;
2954 biDst.bmiHeader.biCompression = BI_RGB;
2956 hdcScreen = CreateCompatibleDC(0);
2957 if(GetDeviceCaps(hdcScreen, BITSPIXEL) != 32)
2959 DeleteDC(hdcScreen);
2960 trace("Skipping 32-bit DDB test\n");
2961 return;
2964 hdcSrc = CreateCompatibleDC(hdcScreen);
2965 bmpSrc = CreateBitmap(1, 1, 1, 32, &colorSrc);
2966 oldSrc = SelectObject(hdcSrc, bmpSrc);
2968 hdcDst = CreateCompatibleDC(hdcScreen);
2969 bmpDst = CreateDIBSection(hdcDst, &biDst, DIB_RGB_COLORS, (void**)&dstBuffer, NULL, 0);
2970 oldDst = SelectObject(hdcDst, bmpDst);
2972 StretchBlt(hdcDst, 0, 0, 1, 1, hdcSrc, 0, 0, 1, 1, SRCCOPY);
2973 ok(dstBuffer[0] == colorSrc, "Expected color=%x, received color=%x\n", colorSrc, dstBuffer[0]);
2975 /* Tidy up */
2976 SelectObject(hdcDst, oldDst);
2977 DeleteObject(bmpDst);
2978 DeleteDC(hdcDst);
2980 SelectObject(hdcSrc, oldSrc);
2981 DeleteObject(bmpSrc);
2982 DeleteDC(hdcSrc);
2984 DeleteDC(hdcScreen);
2987 START_TEST(bitmap)
2989 HMODULE hdll;
2990 is_win9x = GetWindowLongPtrW(GetDesktopWindow(), GWLP_WNDPROC) == 0;
2992 hdll = GetModuleHandle("gdi32.dll");
2993 pGdiAlphaBlend = (void*)GetProcAddress(hdll, "GdiAlphaBlend");
2995 test_createdibitmap();
2996 test_dibsections();
2997 test_mono_dibsection();
2998 test_bitmap();
2999 test_bmBits();
3000 test_GetDIBits_selected_DIB(1);
3001 test_GetDIBits_selected_DIB(4);
3002 test_GetDIBits_selected_DIB(8);
3003 test_GetDIBits_selected_DDB(TRUE);
3004 test_GetDIBits_selected_DDB(FALSE);
3005 test_GetDIBits();
3006 test_GetDIBits_BI_BITFIELDS();
3007 test_select_object();
3008 test_CreateBitmap();
3009 test_BitBlt();
3010 test_StretchBlt();
3011 test_StretchDIBits();
3012 test_GdiAlphaBlend();
3013 test_32bit_bitmap_blt();
3014 test_bitmapinfoheadersize();
3015 test_get16dibits();
3016 test_clipping();