gdi32: Clip solid brush fills.
[wine] / dlls / gdi32 / tests / dib.c
1 /*
2  * DIB driver tests.
3  *
4  * Copyright 2011 Huw Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <math.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "wincrypt.h"
30
31 #include "wine/test.h"
32
33 static HCRYPTPROV crypt_prov;
34
35 static const DWORD rop3[256] =
36 {
37     0x000042, 0x010289, 0x020C89, 0x0300AA, 0x040C88, 0x0500A9, 0x060865, 0x0702C5,
38     0x080F08, 0x090245, 0x0A0329, 0x0B0B2A, 0x0C0324, 0x0D0B25, 0x0E08A5, 0x0F0001,
39     0x100C85, 0x1100A6, 0x120868, 0x1302C8, 0x140869, 0x1502C9, 0x165CCA, 0x171D54,
40     0x180D59, 0x191CC8, 0x1A06C5, 0x1B0768, 0x1C06CA, 0x1D0766, 0x1E01A5, 0x1F0385,
41     0x200F09, 0x210248, 0x220326, 0x230B24, 0x240D55, 0x251CC5, 0x2606C8, 0x271868,
42     0x280369, 0x2916CA, 0x2A0CC9, 0x2B1D58, 0x2C0784, 0x2D060A, 0x2E064A, 0x2F0E2A,
43     0x30032A, 0x310B28, 0x320688, 0x330008, 0x3406C4, 0x351864, 0x3601A8, 0x370388,
44     0x38078A, 0x390604, 0x3A0644, 0x3B0E24, 0x3C004A, 0x3D18A4, 0x3E1B24, 0x3F00EA,
45     0x400F0A, 0x410249, 0x420D5D, 0x431CC4, 0x440328, 0x450B29, 0x4606C6, 0x47076A,
46     0x480368, 0x4916C5, 0x4A0789, 0x4B0605, 0x4C0CC8, 0x4D1954, 0x4E0645, 0x4F0E25,
47     0x500325, 0x510B26, 0x5206C9, 0x530764, 0x5408A9, 0x550009, 0x5601A9, 0x570389,
48     0x580785, 0x590609, 0x5A0049, 0x5B18A9, 0x5C0649, 0x5D0E29, 0x5E1B29, 0x5F00E9,
49     0x600365, 0x6116C6, 0x620786, 0x630608, 0x640788, 0x650606, 0x660046, 0x6718A8,
50     0x6858A6, 0x690145, 0x6A01E9, 0x6B178A, 0x6C01E8, 0x6D1785, 0x6E1E28, 0x6F0C65,
51     0x700CC5, 0x711D5C, 0x720648, 0x730E28, 0x740646, 0x750E26, 0x761B28, 0x7700E6,
52     0x7801E5, 0x791786, 0x7A1E29, 0x7B0C68, 0x7C1E24, 0x7D0C69, 0x7E0955, 0x7F03C9,
53     0x8003E9, 0x810975, 0x820C49, 0x831E04, 0x840C48, 0x851E05, 0x8617A6, 0x8701C5,
54     0x8800C6, 0x891B08, 0x8A0E06, 0x8B0666, 0x8C0E08, 0x8D0668, 0x8E1D7C, 0x8F0CE5,
55     0x900C45, 0x911E08, 0x9217A9, 0x9301C4, 0x9417AA, 0x9501C9, 0x960169, 0x97588A,
56     0x981888, 0x990066, 0x9A0709, 0x9B07A8, 0x9C0704, 0x9D07A6, 0x9E16E6, 0x9F0345,
57     0xA000C9, 0xA11B05, 0xA20E09, 0xA30669, 0xA41885, 0xA50065, 0xA60706, 0xA707A5,
58     0xA803A9, 0xA90189, 0xAA0029, 0xAB0889, 0xAC0744, 0xAD06E9, 0xAE0B06, 0xAF0229,
59     0xB00E05, 0xB10665, 0xB21974, 0xB30CE8, 0xB4070A, 0xB507A9, 0xB616E9, 0xB70348,
60     0xB8074A, 0xB906E6, 0xBA0B09, 0xBB0226, 0xBC1CE4, 0xBD0D7D, 0xBE0269, 0xBF08C9,
61     0xC000CA, 0xC11B04, 0xC21884, 0xC3006A, 0xC40E04, 0xC50664, 0xC60708, 0xC707AA,
62     0xC803A8, 0xC90184, 0xCA0749, 0xCB06E4, 0xCC0020, 0xCD0888, 0xCE0B08, 0xCF0224,
63     0xD00E0A, 0xD1066A, 0xD20705, 0xD307A4, 0xD41D78, 0xD50CE9, 0xD616EA, 0xD70349,
64     0xD80745, 0xD906E8, 0xDA1CE9, 0xDB0D75, 0xDC0B04, 0xDD0228, 0xDE0268, 0xDF08C8,
65     0xE003A5, 0xE10185, 0xE20746, 0xE306EA, 0xE40748, 0xE506E5, 0xE61CE8, 0xE70D79,
66     0xE81D74, 0xE95CE6, 0xEA02E9, 0xEB0849, 0xEC02E8, 0xED0848, 0xEE0086, 0xEF0A08,
67     0xF00021, 0xF10885, 0xF20B05, 0xF3022A, 0xF40B0A, 0xF50225, 0xF60265, 0xF708C5,
68     0xF802E5, 0xF90845, 0xFA0089, 0xFB0A09, 0xFC008A, 0xFD0A0A, 0xFE02A9, 0xFF0062
69 };
70
71 static inline BOOL rop_uses_src(DWORD rop)
72 {
73     return (((rop & 0xcc0000) >> 2) != (rop & 0x330000));
74 }
75
76 static const char *sha1_graphics_a8r8g8b8[] =
77 {
78     "a3cadd34d95d3d5cc23344f69aab1c2e55935fcf",
79     "2426172d9e8fec27d9228088f382ef3c93717da9",
80     "9e8f27ca952cdba01dbf25d07c34e86a7820c012",
81     "17b2c177bdce5e94433574a928bda5c94a8cdfa5",
82     "fe6cc678fb13a3ead67839481bf22348adc69f52",
83     "d51bd330cec510cdccf5394328bd8e5411901e9e",
84     "f2af53dd073a09b1031d0032d28da35c82adc566",
85     NULL
86 };
87
88 static inline DWORD get_stride(BITMAPINFO *bmi)
89 {
90     return ((bmi->bmiHeader.biBitCount * bmi->bmiHeader.biWidth + 31) >> 3) & ~3;
91 }
92
93 static inline DWORD get_dib_size(BITMAPINFO *bmi)
94 {
95     return get_stride(bmi) * abs(bmi->bmiHeader.biHeight);
96 }
97
98 static char *hash_dib(BITMAPINFO *bmi, void *bits)
99 {
100     DWORD dib_size = get_dib_size(bmi);
101     HCRYPTHASH hash;
102     char *buf;
103     BYTE hash_buf[20];
104     DWORD hash_size = sizeof(hash_buf);
105     int i;
106     static const char *hex = "0123456789abcdef";
107
108     if(!crypt_prov) return NULL;
109
110     if(!CryptCreateHash(crypt_prov, CALG_SHA1, 0, 0, &hash)) return NULL;
111
112     CryptHashData(hash, bits, dib_size, 0);
113
114     CryptGetHashParam(hash, HP_HASHVAL, NULL, &hash_size, 0);
115     if(hash_size != sizeof(hash_buf)) return NULL;
116
117     CryptGetHashParam(hash, HP_HASHVAL, hash_buf, &hash_size, 0);
118     CryptDestroyHash(hash);
119
120     buf = HeapAlloc(GetProcessHeap(), 0, hash_size * 2 + 1);
121
122     for(i = 0; i < hash_size; i++)
123     {
124         buf[i * 2] = hex[hash_buf[i] >> 4];
125         buf[i * 2 + 1] = hex[hash_buf[i] & 0xf];
126     }
127     buf[i * 2] = '\0';
128
129     return buf;
130 }
131
132 static void compare_hash(BITMAPINFO *bmi, BYTE *bits, const char ***sha1, const char *info)
133 {
134     char *hash = hash_dib(bmi, bits);
135
136     if(!hash)
137     {
138         skip("SHA1 hashing unavailable on this platform\n");
139         return;
140     }
141
142     if(**sha1)
143     {
144         ok(!strcmp(hash, **sha1), "%d: %s: expected hash %s got %s\n",
145            bmi->bmiHeader.biBitCount, info, **sha1, hash);
146         (*sha1)++;
147     }
148     else ok(**sha1 != NULL, "missing hash, got \"%s\",\n", hash);
149
150     HeapFree(GetProcessHeap(), 0, hash);
151 }
152
153 static const RECT hline_clips[] =
154 {
155     {120, 120, 140, 120}, /* unclipped */
156     {100, 122, 140, 122}, /* l edgecase */
157     { 99, 124, 140, 124}, /* l edgecase clipped */
158     {120, 126, 200, 126}, /* r edgecase */
159     {120, 128, 201, 128}, /* r edgecase clipped */
160     { 99, 130, 201, 130}, /* l and r clipped */
161     {120, 100, 140, 100}, /* t edgecase */
162     {120,  99, 140,  99}, /* t edgecase clipped */
163     {120, 199, 140, 199}, /* b edgecase */
164     {120, 200, 140, 200}, /* b edgecase clipped */
165     {120, 132, 310, 132}, /* inside two clip rects */
166     { 10, 134, 101, 134}, /* r end on l edgecase */
167     { 10, 136, 100, 136}, /* r end on l edgecase clipped */
168     {199, 138, 220, 138}, /* l end on r edgecase */
169     {200, 140, 220, 200}  /* l end on r edgecase clipped */
170 };
171
172 static const RECT vline_clips[] =
173 {
174     {120, 120, 120, 140}, /* unclipped */
175     {100, 120, 100, 140}, /* l edgecase */
176     { 99, 120,  99, 140}, /* l edgecase clipped */
177     {199, 120, 199, 140}, /* r edgecase */
178     {200, 120, 200, 140}, /* r edgecase clipped */
179     {122,  99, 122, 201}, /* t and b clipped */
180     {124, 100, 124, 140}, /* t edgecase */
181     {126,  99, 126, 140}, /* t edgecase clipped */
182     {128, 120, 128, 200}, /* b edgecase */
183     {130, 120, 130, 201}, /* b edgecase clipped */
184     {132,  12, 132, 140}, /* inside two clip rects */
185     {134,  90, 134, 101}, /* b end on t edgecase */
186     {136,  90, 136, 100}, /* b end on t edgecase clipped */
187     {138, 199, 138, 220}, /* t end on b edgecase */
188     {140, 200, 140, 220}  /* t end on b edgecase clipped */
189 };
190
191 static const RECT patblt_clips[] =
192 {
193     {120, 120, 140, 126}, /* unclipped */
194     {100, 130, 140, 136}, /* l edgecase */
195     { 99, 140, 140, 146}, /* l edgecase clipped */
196     {180, 130, 200, 136}, /* r edgecase */
197     {180, 140, 201, 146}, /* r edgecase clipped */
198     {120, 100, 130, 110}, /* t edgecase */
199     {140,  99, 150, 110}, /* t edgecase clipped */
200     {120, 180, 130, 200}, /* b edgecase */
201     {140, 180, 150, 201}, /* b edgecase */
202     {199, 150, 210, 156}, /* l edge on r edgecase */
203     {200, 160, 210, 166}, /* l edge on r edgecase clipped */
204     { 90, 150, 101, 156}, /* r edge on l edgecase */
205     { 90, 160, 100, 166}, /* r edge on l edgecase clipped */
206     {160,  90, 166, 101}, /* b edge on t edgecase */
207     {170,  90, 176, 101}, /* b edge on t edgecase clipped */
208     {160, 199, 166, 210}, /* t edge on b edgecase */
209     {170, 200, 176, 210}, /* t edge on b edgecase clipped */
210 };
211
212 static void draw_graphics(HDC hdc, BITMAPINFO *bmi, BYTE *bits, const char ***sha1)
213 {
214     DWORD dib_size = get_dib_size(bmi);
215     HPEN solid_pen, orig_pen;
216     HBRUSH solid_brush, orig_brush;
217     INT i, y;
218     HRGN hrgn, hrgn2;
219
220     memset(bits, 0xcc, dib_size);
221     compare_hash(bmi, bits, sha1, "empty");
222
223     solid_pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0xff));
224     orig_pen = SelectObject(hdc, solid_pen);
225
226     /* horizontal and vertical lines */
227     for(i = 1; i <= 16; i++)
228     {
229         SetROP2(hdc, i);
230         MoveToEx(hdc, 10, i * 3, NULL);
231         LineTo(hdc, 100, i * 3); /* l -> r */
232         MoveToEx(hdc, 100, 50 + i * 3, NULL);
233         LineTo(hdc, 10, 50 + i * 3); /* r -> l */
234         MoveToEx(hdc, 120 + i * 3, 10, NULL);
235         LineTo(hdc, 120 + i * 3, 100); /* t -> b */
236         MoveToEx(hdc, 170 + i * 3, 100, NULL);
237         LineTo(hdc, 170 + i * 3, 10); /* b -> t */
238     }
239     compare_hash(bmi, bits, sha1, "h and v solid lines");
240     memset(bits, 0xcc, dib_size);
241
242     SetROP2(hdc, R2_COPYPEN);
243     for(i = 0; i < 16; i++)
244     {
245         double s = sin(M_PI * i / 8.0);
246         double c = cos(M_PI * i / 8.0);
247
248         MoveToEx(hdc, 200.5 + 10 * c, 200.5 + 10 * s, NULL);
249         LineTo(hdc, 200.5 + 100 * c, 200.5 + 100 * s);
250     }
251     compare_hash(bmi, bits, sha1, "diagonal solid lines");
252     memset(bits, 0xcc, dib_size);
253
254     /* solid brush PatBlt */
255     solid_brush = CreateSolidBrush(RGB(0x33, 0xaa, 0xff));
256     orig_brush = SelectObject(hdc, solid_brush);
257
258     for(i = 0, y = 10; i < 256; i++)
259     {
260         BOOL ret;
261
262         ret = PatBlt(hdc, 10, y, 100, 10, rop3[i]);
263
264         if(rop_uses_src(rop3[i]))
265             ok(ret == FALSE, "got TRUE for %x\n", rop3[i]);
266         else
267         {
268             ok(ret, "got FALSE for %x\n", rop3[i]);
269             y += 20;
270         }
271
272     }
273     compare_hash(bmi, bits, sha1, "solid patblt");
274     memset(bits, 0xcc, dib_size);
275
276     /* clipped lines */
277     hrgn = CreateRectRgn(10, 10, 200, 20);
278     hrgn2 = CreateRectRgn(100, 100, 200, 200);
279     CombineRgn(hrgn, hrgn, hrgn2, RGN_OR);
280     SetRectRgn(hrgn2, 290, 100, 300, 200);
281     CombineRgn(hrgn, hrgn, hrgn2, RGN_OR);
282     ExtSelectClipRgn(hdc, hrgn, RGN_COPY);
283     DeleteObject(hrgn2);
284     DeleteObject(hrgn);
285
286     for(i = 0; i < sizeof(hline_clips)/sizeof(hline_clips[0]); i++)
287     {
288         MoveToEx(hdc, hline_clips[i].left, hline_clips[i].top, NULL);
289         LineTo(hdc, hline_clips[i].right, hline_clips[i].bottom);
290     }
291     compare_hash(bmi, bits, sha1, "clipped solid hlines");
292     memset(bits, 0xcc, dib_size);
293
294     for(i = 0; i < sizeof(vline_clips)/sizeof(vline_clips[0]); i++)
295     {
296         MoveToEx(hdc, vline_clips[i].left, vline_clips[i].top, NULL);
297         LineTo(hdc, vline_clips[i].right, vline_clips[i].bottom);
298     }
299     compare_hash(bmi, bits, sha1, "clipped solid vlines");
300     memset(bits, 0xcc, dib_size);
301
302     for(i = 0; i < sizeof(patblt_clips) / sizeof(patblt_clips[0]); i++)
303     {
304         PatBlt(hdc, patblt_clips[i].left, patblt_clips[i].top,
305                patblt_clips[i].right - patblt_clips[i].left,
306                patblt_clips[i].bottom - patblt_clips[i].top, PATCOPY);
307     }
308     compare_hash(bmi, bits, sha1, "clipped patblt");
309
310     ExtSelectClipRgn(hdc, NULL, RGN_COPY);
311
312
313     SelectObject(hdc, orig_brush);
314     SelectObject(hdc, orig_pen);
315     DeleteObject(solid_brush);
316     DeleteObject(solid_pen);
317 }
318
319 static void test_simple_graphics(void)
320 {
321     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
322     BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
323     DWORD *bit_fields = (DWORD*)(bmibuf + sizeof(BITMAPINFOHEADER));
324     HDC mem_dc;
325     BYTE *bits;
326     HBITMAP dib, orig_bm;
327     const char **sha1;
328     DIBSECTION ds;
329
330     mem_dc = CreateCompatibleDC(NULL);
331
332     /* a8r8g8b8 */
333     trace("8888\n");
334     memset(bmi, 0, sizeof(bmibuf));
335     bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
336     bmi->bmiHeader.biHeight = 512;
337     bmi->bmiHeader.biWidth = 512;
338     bmi->bmiHeader.biBitCount = 32;
339     bmi->bmiHeader.biPlanes = 1;
340     bmi->bmiHeader.biCompression = BI_RGB;
341
342     dib = CreateDIBSection(0, bmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
343     ok(dib != NULL, "ret NULL\n");
344     ok(GetObjectW( dib, sizeof(ds), &ds ), "GetObject failed\n");
345     ok(ds.dsBitfields[0] == 0, "got %08x\n", ds.dsBitfields[0]);
346     ok(ds.dsBitfields[1] == 0, "got %08x\n", ds.dsBitfields[1]);
347     ok(ds.dsBitfields[2] == 0, "got %08x\n", ds.dsBitfields[2]);
348     ok(ds.dsBmih.biCompression == BI_RGB, "got %x\n", ds.dsBmih.biCompression);
349
350     orig_bm = SelectObject(mem_dc, dib);
351
352     sha1 = sha1_graphics_a8r8g8b8;
353     draw_graphics(mem_dc, bmi, bits, &sha1);
354
355     SelectObject(mem_dc, orig_bm);
356     DeleteObject(dib);
357
358     /* a8r8g8b8 - bitfields.  Should be the same as the regular 32 bit case.*/
359     trace("8888 - bitfields\n");
360     bmi->bmiHeader.biBitCount = 32;
361     bmi->bmiHeader.biCompression = BI_BITFIELDS;
362     bit_fields[0] = 0xff0000;
363     bit_fields[1] = 0x00ff00;
364     bit_fields[2] = 0x0000ff;
365
366     dib = CreateDIBSection(mem_dc, bmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
367     ok(dib != NULL, "ret NULL\n");
368     ok(GetObjectW( dib, sizeof(ds), &ds ), "GetObject failed\n");
369     ok(ds.dsBitfields[0] == 0xff0000, "got %08x\n", ds.dsBitfields[0]);
370     ok(ds.dsBitfields[1] == 0x00ff00, "got %08x\n", ds.dsBitfields[1]);
371     ok(ds.dsBitfields[2] == 0x0000ff, "got %08x\n", ds.dsBitfields[2]);
372     ok(ds.dsBmih.biCompression == BI_BITFIELDS, "got %x\n", ds.dsBmih.biCompression);
373
374     orig_bm = SelectObject(mem_dc, dib);
375
376     sha1 = sha1_graphics_a8r8g8b8;
377     draw_graphics(mem_dc, bmi, bits, &sha1);
378
379     SelectObject(mem_dc, orig_bm);
380     DeleteObject(dib);
381
382
383     /* r5g5b5 */
384     bmi->bmiHeader.biBitCount = 16;
385     bmi->bmiHeader.biCompression = BI_RGB;
386
387     dib = CreateDIBSection(0, bmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
388     ok(dib != NULL, "ret NULL\n");
389     ok(GetObjectW( dib, sizeof(ds), &ds ), "GetObject failed\n");
390     ok(ds.dsBitfields[0] == 0x7c00, "got %08x\n", ds.dsBitfields[0]);
391     ok(ds.dsBitfields[1] == 0x03e0, "got %08x\n", ds.dsBitfields[1]);
392     ok(ds.dsBitfields[2] == 0x001f, "got %08x\n", ds.dsBitfields[2]);
393 todo_wine
394     ok(ds.dsBmih.biCompression == BI_BITFIELDS, "got %x\n", ds.dsBmih.biCompression);
395
396     DeleteObject(dib);
397
398     DeleteDC(mem_dc);
399 }
400
401 START_TEST(dib)
402 {
403     CryptAcquireContextW(&crypt_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
404
405     test_simple_graphics();
406
407     CryptReleaseContext(crypt_prov, 0);
408 }