msvfw32: Drawdib doesn't support inverted DIBs.
[wine/multimedia.git] / dlls / msvfw32 / tests / drawdib.c
blob24d86317acbaa6fc51ea11aa8fe9dbaac58e119f
1 /*
2 * Copyright 2014 Akihiro Sagawa
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define WIN32_LEAN_AND_MEAN
21 #include <windows.h>
22 #include <vfw.h>
23 #include <wincrypt.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "wine/test.h"
29 #define WIDTH 16
30 #define HEIGHT 12
32 static HCRYPTPROV crypt_prov;
34 static inline DWORD get_stride(const BITMAPINFO *bmi)
36 return ((bmi->bmiHeader.biBitCount * bmi->bmiHeader.biWidth + 31) >> 3) & ~3;
39 static inline DWORD get_dib_size(const BITMAPINFO *bmi)
41 return get_stride(bmi) * abs(bmi->bmiHeader.biHeight);
44 static char *hash_dib(const BITMAPINFO *bmi, const void *bits)
46 DWORD dib_size = get_dib_size(bmi);
47 HCRYPTHASH hash;
48 char *buf;
49 BYTE hash_buf[20];
50 DWORD hash_size = sizeof(hash_buf);
51 int i;
52 static const char *hex = "0123456789abcdef";
54 if(!crypt_prov) return NULL;
56 if(!CryptCreateHash(crypt_prov, CALG_SHA1, 0, 0, &hash)) return NULL;
58 CryptHashData(hash, bits, dib_size, 0);
60 CryptGetHashParam(hash, HP_HASHVAL, NULL, &hash_size, 0);
61 if(hash_size != sizeof(hash_buf)) return NULL;
63 CryptGetHashParam(hash, HP_HASHVAL, hash_buf, &hash_size, 0);
64 CryptDestroyHash(hash);
66 buf = HeapAlloc(GetProcessHeap(), 0, hash_size * 2 + 1);
68 for(i = 0; i < hash_size; i++)
70 buf[i * 2] = hex[hash_buf[i] >> 4];
71 buf[i * 2 + 1] = hex[hash_buf[i] & 0xf];
73 buf[i * 2] = '\0';
75 return buf;
78 static void init_bmi(BITMAPINFO *bmi, LONG width, LONG height, DWORD size)
80 memset(bmi, 0, sizeof(*bmi));
81 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
82 bmi->bmiHeader.biWidth = width;
83 bmi->bmiHeader.biHeight = height;
84 bmi->bmiHeader.biPlanes = 1;
85 bmi->bmiHeader.biBitCount = 32;
86 bmi->bmiHeader.biCompression = BI_RGB;
87 bmi->bmiHeader.biSizeImage = size;
90 static void test_DrawDib_sizeimage(void)
92 const struct {
93 LONG width, height;
94 DWORD size;
95 char hash[41];
96 } test_data[] = {
97 /* [0] correct size */
98 { WIDTH, HEIGHT, WIDTH * HEIGHT * sizeof(RGBQUAD), "bc943d5ab024b8b0118d0a80aa283055d39942b8" },
99 /* [1] zero size */
100 { WIDTH, HEIGHT, 0, "bc943d5ab024b8b0118d0a80aa283055d39942b8" },
101 /* error patterns */
102 { WIDTH, -HEIGHT, 0, "" },
103 { -WIDTH, HEIGHT, 0, "" },
104 { -WIDTH, -HEIGHT, 0, "" },
105 { 0, 0, 0, "" },
106 { 0, HEIGHT, 0, "" },
107 { WIDTH, 0, 0, "" },
109 HDC hdc;
110 DWORD src_dib_size, dst_dib_size;
111 BOOL r;
112 HBITMAP dib;
113 BITMAPINFO src_info, dst_info;
114 RGBQUAD *src_bits = NULL, *dst_bits;
115 HDRAWDIB hdd;
116 unsigned int i;
118 hdc = CreateCompatibleDC(NULL);
120 init_bmi(&dst_info, WIDTH, HEIGHT, 0);
121 dib = CreateDIBSection(NULL, &dst_info, DIB_RGB_COLORS, (void **)&dst_bits, NULL, 0);
122 dst_dib_size = get_dib_size(&dst_info);
123 ok(dib != NULL, "CreateDIBSection failed\n");
124 SelectObject(hdc, dib);
126 init_bmi(&src_info, WIDTH, HEIGHT, 0);
127 src_dib_size = get_dib_size(&src_info);
128 src_bits = HeapAlloc(GetProcessHeap(), 0, src_dib_size);
129 ok(src_bits != NULL, "Can't allocate memory\n");
130 memset(src_bits, 0x88, src_dib_size);
132 hdd = DrawDibOpen();
133 ok(hdd != NULL, "DrawDibOpen failed\n");
135 for (i = 0; i < sizeof(test_data)/sizeof(test_data[0]); i++) {
136 char *hash;
137 memset(dst_bits, 0xff, dst_dib_size);
138 init_bmi(&src_info, test_data[i].width, test_data[i].height, test_data[i].size);
139 r = DrawDibDraw(hdd, hdc,
140 0, 0, -1, -1, &src_info.bmiHeader, src_bits,
141 0, 0, test_data[i].width, test_data[i].height, 0);
142 if (test_data[i].hash[0])
143 ok(r, "[%u] DrawDibDraw failed, expected success\n", i);
144 else
145 ok(!r, "[%u] DrawDibDraw succeeded, expected failed\n", i);
146 if (!r || !test_data[i].hash[0])
147 continue;
149 hash = hash_dib(&dst_info, dst_bits);
150 if (!hash) {
151 win_skip("This platform doesn't support SHA-1 hash\n");
152 continue;
154 ok(strcmp(hash, test_data[i].hash) == 0,
155 "[%u] got %s, expected %s\n",
156 i, hash, test_data[i].hash);
157 HeapFree(GetProcessHeap(), 0, hash);
160 r = DrawDibClose(hdd);
161 ok(r, "DrawDibClose failed\n");
163 HeapFree(GetProcessHeap(), 0, src_bits);
165 DeleteDC(hdc);
168 START_TEST(drawdib)
170 CryptAcquireContextW(&crypt_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
171 test_DrawDib_sizeimage();
172 CryptReleaseContext(crypt_prov, 0);