4 * Copyright 2011 Huw Davies
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.
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.
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
30 #include "wine/test.h"
32 static HCRYPTPROV crypt_prov;
34 static const char *sha1_graphics_a8r8g8b8[] =
36 "a3cadd34d95d3d5cc23344f69aab1c2e55935fcf",
37 "2426172d9e8fec27d9228088f382ef3c93717da9",
41 static inline DWORD get_stride(BITMAPINFO *bmi)
43 return ((bmi->bmiHeader.biBitCount * bmi->bmiHeader.biWidth + 31) >> 3) & ~3;
46 static inline DWORD get_dib_size(BITMAPINFO *bmi)
48 return get_stride(bmi) * abs(bmi->bmiHeader.biHeight);
51 static char *hash_dib(BITMAPINFO *bmi, void *bits)
53 DWORD dib_size = get_dib_size(bmi);
57 DWORD hash_size = sizeof(hash_buf);
59 static const char *hex = "0123456789abcdef";
61 if(!crypt_prov) return NULL;
63 if(!CryptCreateHash(crypt_prov, CALG_SHA1, 0, 0, &hash)) return NULL;
65 CryptHashData(hash, bits, dib_size, 0);
67 CryptGetHashParam(hash, HP_HASHVAL, NULL, &hash_size, 0);
68 if(hash_size != sizeof(hash_buf)) return NULL;
70 CryptGetHashParam(hash, HP_HASHVAL, hash_buf, &hash_size, 0);
71 CryptDestroyHash(hash);
73 buf = HeapAlloc(GetProcessHeap(), 0, hash_size * 2 + 1);
75 for(i = 0; i < hash_size; i++)
77 buf[i * 2] = hex[hash_buf[i] >> 4];
78 buf[i * 2 + 1] = hex[hash_buf[i] & 0xf];
85 static void compare_hash(BITMAPINFO *bmi, BYTE *bits, const char ***sha1, const char *info)
87 char *hash = hash_dib(bmi, bits);
91 skip("SHA1 hashing unavailable on this platform\n");
97 ok(!strcmp(hash, **sha1), "%d: %s: expected hash %s got %s\n",
98 bmi->bmiHeader.biBitCount, info, **sha1, hash);
101 else trace("\"%s\",\n", hash);
103 HeapFree(GetProcessHeap(), 0, hash);
106 static void draw_graphics(HDC hdc, BITMAPINFO *bmi, BYTE *bits, const char ***sha1)
108 DWORD dib_size = get_dib_size(bmi);
109 HPEN solid_pen, orig_pen;
112 memset(bits, 0xcc, dib_size);
113 compare_hash(bmi, bits, sha1, "empty");
115 solid_pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0xff));
116 orig_pen = SelectObject(hdc, solid_pen);
118 /* horizontal and vertical lines */
119 for(i = 1; i <= 16; i++)
122 MoveToEx(hdc, 10, i * 3, NULL);
123 LineTo(hdc, 100, i * 3); /* l -> r */
124 MoveToEx(hdc, 100, 50 + i * 3, NULL);
125 LineTo(hdc, 10, 50 + i * 3); /* r -> l */
126 MoveToEx(hdc, 120 + i * 3, 10, NULL);
127 LineTo(hdc, 120 + i * 3, 100); /* t -> b */
128 MoveToEx(hdc, 170 + i * 3, 100, NULL);
129 LineTo(hdc, 170 + i * 3, 10); /* b -> t */
131 compare_hash(bmi, bits, sha1, "h and v solid lines");
133 SelectObject(hdc, orig_pen);
134 DeleteObject(solid_pen);
137 static void test_simple_graphics(void)
139 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
140 BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
143 HBITMAP dib, orig_bm;
148 memset(bmi, 0, sizeof(bmibuf));
149 bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
150 bmi->bmiHeader.biHeight = 512;
151 bmi->bmiHeader.biWidth = 512;
152 bmi->bmiHeader.biBitCount = 32;
153 bmi->bmiHeader.biPlanes = 1;
154 bmi->bmiHeader.biCompression = BI_RGB;
156 dib = CreateDIBSection(0, bmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
157 ok(dib != NULL, "ret NULL\n");
158 mem_dc = CreateCompatibleDC(NULL);
159 orig_bm = SelectObject(mem_dc, dib);
161 sha1 = sha1_graphics_a8r8g8b8;
162 draw_graphics(mem_dc, bmi, bits, &sha1);
164 SelectObject(mem_dc, orig_bm);
171 CryptAcquireContextW(&crypt_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
173 test_simple_graphics();
175 CryptReleaseContext(crypt_prov, 0);