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