wbemprox: Add support for enumerating class properties.
[wine] / dlls / windowscodecs / tests / palette.c
1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
3  * Copyright 2012 Dmitry Timoshkov
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <stdarg.h>
21 #include <assert.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "objbase.h"
27 #include "wincodec.h"
28 #include "wine/test.h"
29
30 static void test_custom_palette(void)
31 {
32     IWICImagingFactory *factory;
33     IWICPalette *palette;
34     HRESULT hr;
35     WICBitmapPaletteType type=0xffffffff;
36     UINT count=1;
37     const WICColor initcolors[4]={0xff000000,0xff0000ff,0xffffff00,0xffffffff};
38     WICColor colors[4];
39     BOOL boolresult;
40
41     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
42         &IID_IWICImagingFactory, (void**)&factory);
43     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
44     if (FAILED(hr)) return;
45
46     hr = IWICImagingFactory_CreatePalette(factory, &palette);
47     ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
48     if (SUCCEEDED(hr))
49     {
50         hr = IWICPalette_GetType(palette, &type);
51         ok(SUCCEEDED(hr), "GetType failed, hr=%x\n", hr);
52         ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %x\n", type);
53
54         hr = IWICPalette_GetColorCount(palette, &count);
55         ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
56         ok(count == 0, "expected 0, got %u\n", count);
57
58         hr = IWICPalette_GetColors(palette, 0, colors, &count);
59         ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
60         ok(count == 0, "expected 0, got %u\n", count);
61
62         hr = IWICPalette_GetColors(palette, 4, colors, &count);
63         ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
64         ok(count == 0, "expected 0, got %u\n", count);
65
66         memcpy(colors, initcolors, sizeof(initcolors));
67         hr = IWICPalette_InitializeCustom(palette, colors, 4);
68         ok(SUCCEEDED(hr), "InitializeCustom failed, hr=%x\n", hr);
69
70         hr = IWICPalette_GetType(palette, &type);
71         ok(SUCCEEDED(hr), "GetType failed, hr=%x\n", hr);
72         ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %x\n", type);
73
74         hr = IWICPalette_GetColorCount(palette, &count);
75         ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
76         ok(count == 4, "expected 4, got %u\n", count);
77
78         memset(colors, 0, sizeof(colors));
79         count = 0;
80         hr = IWICPalette_GetColors(palette, 4, colors, &count);
81         ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
82         ok(count == 4, "expected 4, got %u\n", count);
83         ok(!memcmp(colors, initcolors, sizeof(colors)), "got unexpected palette data\n");
84
85         memset(colors, 0, sizeof(colors));
86         count = 0;
87         hr = IWICPalette_GetColors(palette, 2, colors, &count);
88         ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
89         ok(count == 2, "expected 2, got %u\n", count);
90         ok(!memcmp(colors, initcolors, sizeof(WICColor)*2), "got unexpected palette data\n");
91
92         count = 0;
93         hr = IWICPalette_GetColors(palette, 6, colors, &count);
94         ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
95         ok(count == 4, "expected 4, got %u\n", count);
96
97         hr = IWICPalette_HasAlpha(palette, &boolresult);
98         ok(SUCCEEDED(hr), "HasAlpha failed, hr=%x\n", hr);
99         ok(!boolresult, "expected FALSE, got TRUE\n");
100
101         hr = IWICPalette_IsBlackWhite(palette, &boolresult);
102         ok(SUCCEEDED(hr), "IsBlackWhite failed, hr=%x\n", hr);
103         ok(!boolresult, "expected FALSE, got TRUE\n");
104
105         hr = IWICPalette_IsGrayscale(palette, &boolresult);
106         ok(SUCCEEDED(hr), "IsGrayscale failed, hr=%x\n", hr);
107         ok(!boolresult, "expected FALSE, got TRUE\n");
108
109         /* try a palette with some alpha in it */
110         colors[2] = 0x80ffffff;
111         hr = IWICPalette_InitializeCustom(palette, colors, 4);
112         ok(SUCCEEDED(hr), "InitializeCustom failed, hr=%x\n", hr);
113
114         hr = IWICPalette_HasAlpha(palette, &boolresult);
115         ok(SUCCEEDED(hr), "HasAlpha failed, hr=%x\n", hr);
116         ok(boolresult, "expected TRUE, got FALSE\n");
117
118         /* setting to a 0-color palette is acceptable */
119         hr = IWICPalette_InitializeCustom(palette, NULL, 0);
120         ok(SUCCEEDED(hr), "InitializeCustom failed, hr=%x\n", hr);
121
122         /* IWICPalette is paranoid about NULL pointers */
123         hr = IWICPalette_GetType(palette, NULL);
124         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
125
126         hr = IWICPalette_GetColorCount(palette, NULL);
127         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
128
129         hr = IWICPalette_InitializeCustom(palette, NULL, 4);
130         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
131
132         hr = IWICPalette_GetColors(palette, 4, NULL, &count);
133         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
134
135         hr = IWICPalette_GetColors(palette, 4, colors, NULL);
136         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
137
138         hr = IWICPalette_HasAlpha(palette, NULL);
139         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
140
141         hr = IWICPalette_IsBlackWhite(palette, NULL);
142         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
143
144         hr = IWICPalette_IsGrayscale(palette, NULL);
145         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
146
147         IWICPalette_Release(palette);
148     }
149
150     IWICImagingFactory_Release(factory);
151 }
152
153 static void generate_gray16_palette(DWORD *entries, UINT count)
154 {
155     UINT i;
156
157     assert(count == 16);
158
159     for (i = 0; i < 16; i++)
160     {
161         entries[i] = 0xff000000;
162         entries[i] |= (i << 20) | (i << 16) | (i << 12) | (i << 8) | (i << 4) | i;
163     }
164 }
165
166 static void generate_gray256_palette(DWORD *entries, UINT count)
167 {
168     UINT i;
169
170     assert(count == 256);
171
172     for (i = 0; i < 256; i++)
173     {
174         entries[i] = 0xff000000;
175         entries[i] |= (i << 16) | (i << 8) | i;
176     }
177 }
178
179 static void generate_halftone8_palette(DWORD *entries, UINT count, BOOL add_transparent)
180 {
181     UINT i;
182
183     if (add_transparent)
184         ok(count == 17, "expected 17, got %u\n", count);
185     else
186         ok(count == 16, "expected 16, got %u\n", count);
187
188     for (i = 0; i < 8; i++)
189     {
190         entries[i] = 0xff000000;
191         if (i & 1) entries[i] |= 0xff;
192         if (i & 2) entries[i] |= 0xff00;
193         if (i & 4) entries[i] |= 0xff0000;
194     }
195
196     for (i = 8; i < 16; i++)
197     {
198         static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
199                                            0x000080, 0x808000, 0x800080, 0x008080 };
200         entries[i] = 0xff000000;
201         entries[i] |= halftone[i-8];
202     }
203
204     if (add_transparent)
205         entries[i] = 0;
206 }
207
208 static void generate_halftone27_palette(DWORD *entries, UINT count, BOOL add_transparent)
209 {
210     static const BYTE halftone_values[4] = { 0x00,0x80,0xff };
211     UINT i;
212
213     if (add_transparent)
214         ok(count == 29, "expected 29, got %u\n", count);
215     else
216         ok(count == 28, "expected 28, got %u\n", count);
217
218     for (i = 0; i < 27; i++)
219     {
220         entries[i] = 0xff000000;
221         entries[i] |= halftone_values[i%3];
222         entries[i] |= halftone_values[(i/3)%3] << 8;
223         entries[i] |= halftone_values[(i/9)%3] << 16;
224     }
225
226     entries[i++] = 0xffc0c0c0;
227     if (add_transparent)
228         entries[i] = 0;
229 }
230
231 static void generate_halftone64_palette(DWORD *entries, UINT count, BOOL add_transparent)
232 {
233     static const BYTE halftone_values[4] = { 0x00,0x55,0xaa,0xff };
234     UINT i;
235
236     if (add_transparent)
237         ok(count == 73, "expected 73, got %u\n", count);
238     else
239         ok(count == 72, "expected 72, got %u\n", count);
240
241     for (i = 0; i < 64; i++)
242     {
243         entries[i] = 0xff000000;
244         entries[i] |= halftone_values[i%4];
245         entries[i] |= halftone_values[(i/4)%4] << 8;
246         entries[i] |= halftone_values[(i/16)%4] << 16;
247     }
248
249     for (i = 64; i < 72; i++)
250     {
251         static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
252                                            0x000080, 0x808000, 0x800080, 0x008080 };
253         entries[i] = 0xff000000;
254         entries[i] |= halftone[i-64];
255     }
256
257     if (add_transparent)
258         entries[i] = 0;
259 }
260
261 static void generate_halftone125_palette(DWORD *entries, UINT count, BOOL add_transparent)
262 {
263     static const BYTE halftone_values[5] = { 0x00, 0x40, 0x80, 0xbf, 0xff };
264     UINT i;
265
266     if (add_transparent)
267         ok(count == 127, "expected 127, got %u\n", count);
268     else
269         ok(count == 126, "expected 126, got %u\n", count);
270
271     for (i = 0; i < 125; i++)
272     {
273         entries[i] = 0xff000000;
274         entries[i] |= halftone_values[i%5];
275         entries[i] |= halftone_values[(i/5)%5] << 8;
276         entries[i] |= halftone_values[(i/25)%5] << 16;
277     }
278
279     entries[i++] = 0xffc0c0c0;
280     if (add_transparent)
281         entries[i] = 0;
282 }
283
284 static void generate_halftone216_palette(DWORD *entries, UINT count, BOOL add_transparent)
285 {
286     static const BYTE halftone_values[6] = { 0x00,0x33,0x66,0x99,0xcc,0xff };
287     UINT i;
288
289     if (add_transparent)
290         ok(count == 225, "expected 225, got %u\n", count);
291     else
292         ok(count == 224, "expected 224, got %u\n", count);
293
294     for (i = 0; i < 216; i++)
295     {
296         entries[i] = 0xff000000;
297         entries[i] |= halftone_values[i%6];
298         entries[i] |= halftone_values[(i/6)%6] << 8;
299         entries[i] |= halftone_values[(i/36)%6] << 16;
300     }
301
302     for (i = 216; i < 224; i++)
303     {
304         static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
305                                            0x000080, 0x808000, 0x800080, 0x008080 };
306         entries[i] = 0xff000000;
307         entries[i] |= halftone[i-216];
308     }
309
310     if (add_transparent)
311         entries[i] = 0;
312 }
313
314 static void generate_halftone252_palette(DWORD *entries, UINT count, BOOL add_transparent)
315 {
316     static const BYTE halftone_values_rb[6] = { 0x00,0x33,0x66,0x99,0xcc,0xff };
317     static const BYTE halftone_values_g[7] = { 0x00,0x2b,0x55,0x80,0xaa,0xd5,0xff };
318     UINT i;
319
320     if (add_transparent)
321         ok(count == 253, "expected 253, got %u\n", count);
322     else
323         ok(count == 252, "expected 252, got %u\n", count);
324
325     for (i = 0; i < 252; i++)
326     {
327         entries[i] = 0xff000000;
328         entries[i] |= halftone_values_rb[i%6];
329         entries[i] |= halftone_values_g[(i/6)%7] << 8;
330         entries[i] |= halftone_values_rb[(i/42)%6] << 16;
331     }
332
333     if (add_transparent)
334         entries[i] = 0;
335 }
336
337 static void generate_halftone256_palette(DWORD *entries, UINT count, BOOL add_transparent)
338 {
339     static const BYTE halftone_values_b[4] = { 0x00,0x55,0xaa,0xff };
340     static const BYTE halftone_values_gr[8] = { 0x00,0x24,0x49,0x6d,0x92,0xb6,0xdb,0xff };
341     UINT i;
342
343     assert(count == 256);
344
345     for (i = 0; i < 256; i++)
346     {
347         entries[i] = 0xff000000;
348         entries[i] |= halftone_values_b[i%4];
349         entries[i] |= halftone_values_gr[(i/4)%8] << 8;
350         entries[i] |= halftone_values_gr[(i/32)%8] << 16;
351     }
352
353     if (add_transparent)
354         entries[255] = 0;
355 }
356
357 static void test_predefined_palette(void)
358 {
359     static struct test_data
360     {
361         WICBitmapPaletteType type;
362         BOOL is_bw, is_gray;
363         UINT count;
364         WICColor color[256];
365         BOOL add_transparent;
366     } td[] =
367     {
368         { WICBitmapPaletteTypeFixedBW, 1, 1, 2, { 0xff000000, 0xffffffff } },
369         { WICBitmapPaletteTypeFixedBW, 1, 1, 2, { 0xff000000, 0xffffffff }, 1 },
370         { WICBitmapPaletteTypeFixedGray4, 0, 1, 4,
371           { 0xff000000, 0xff555555, 0xffaaaaaa, 0xffffffff } },
372         { WICBitmapPaletteTypeFixedGray4, 0, 1, 4,
373           { 0xff000000, 0xff555555, 0xffaaaaaa, 0xffffffff }, 1 },
374         { WICBitmapPaletteTypeFixedGray16, 0, 1, 16, { 0 } },
375         { WICBitmapPaletteTypeFixedGray16, 0, 1, 16, { 0 }, 1 },
376         { WICBitmapPaletteTypeFixedGray256, 0, 1, 256, { 0 } },
377         { WICBitmapPaletteTypeFixedGray256, 0, 1, 256, { 0 }, 1 },
378         { WICBitmapPaletteTypeFixedHalftone8, 0, 0, 16, { 0 } },
379         { WICBitmapPaletteTypeFixedHalftone8, 0, 0, 17, { 0 }, 1 },
380         { WICBitmapPaletteTypeFixedHalftone27, 0, 0, 28, { 0 } },
381         { WICBitmapPaletteTypeFixedHalftone27, 0, 0, 29, { 0 }, 1 },
382         { WICBitmapPaletteTypeFixedHalftone64, 0, 0, 72, { 0 } },
383         { WICBitmapPaletteTypeFixedHalftone64, 0, 0, 73, { 0 }, 1 },
384         { WICBitmapPaletteTypeFixedHalftone125, 0, 0, 126, { 0 } },
385         { WICBitmapPaletteTypeFixedHalftone125, 0, 0, 127, { 0 }, 1 },
386         { WICBitmapPaletteTypeFixedHalftone216, 0, 0, 224, { 0 } },
387         { WICBitmapPaletteTypeFixedHalftone216, 0, 0, 225, { 0 }, 1 },
388         { WICBitmapPaletteTypeFixedHalftone252, 0, 0, 252, { 0 } },
389         { WICBitmapPaletteTypeFixedHalftone252, 0, 0, 253, { 0 }, 1 },
390         { WICBitmapPaletteTypeFixedHalftone256, 0, 0, 256, { 0 } },
391         { WICBitmapPaletteTypeFixedHalftone256, 0, 0, 256, { 0 }, 1 }
392     };
393     IWICImagingFactory *factory;
394     IWICPalette *palette;
395     HRESULT hr;
396     WICBitmapPaletteType type;
397     UINT count, i, ret;
398     BOOL bret;
399     WICColor color[256];
400
401     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
402                           &IID_IWICImagingFactory, (void **)&factory);
403     ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
404
405     hr = IWICImagingFactory_CreatePalette(factory, &palette);
406     ok(hr == S_OK, "CreatePalette error %#x\n", hr);
407     hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeCustom, FALSE);
408     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
409     hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeMedianCut, FALSE);
410     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
411     hr = IWICPalette_InitializePredefined(palette, 0x0f, FALSE);
412     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
413     IWICPalette_Release(palette);
414
415     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
416     {
417         hr = IWICImagingFactory_CreatePalette(factory, &palette);
418         ok(hr == S_OK, "%u: CreatePalette error %#x\n", i, hr);
419
420         hr = IWICPalette_InitializePredefined(palette, td[i].type, td[i].add_transparent);
421         ok(hr == S_OK, "%u: InitializePredefined error %#x\n", i, hr);
422
423         bret = -1;
424         hr = IWICPalette_IsBlackWhite(palette, &bret);
425         ok(hr == S_OK, "%u: IsBlackWhite error %#x\n", i, hr);
426         ok(bret == td[i].is_bw ||
427            broken(td[i].type == WICBitmapPaletteTypeFixedBW && bret != td[i].is_bw), /* XP */
428            "%u: expected %d, got %d\n",i, td[i].is_bw, bret);
429
430         bret = -1;
431         hr = IWICPalette_IsGrayscale(palette, &bret);
432         ok(hr == S_OK, "%u: IsGrayscale error %#x\n", i, hr);
433         ok(bret == td[i].is_gray, "%u: expected %d, got %d\n", i, td[i].is_gray, bret);
434
435         type = -1;
436         hr = IWICPalette_GetType(palette, &type);
437         ok(hr == S_OK, "%u: GetType error %#x\n", i, hr);
438         ok(type == td[i].type, "%u: expected %#x, got %#x\n", i, td[i].type, type);
439
440         count = 0xdeadbeef;
441         hr = IWICPalette_GetColorCount(palette, &count);
442         ok(hr == S_OK, "%u: GetColorCount error %#x\n", i, hr);
443         ok(count == td[i].count, "%u: expected %u, got %u\n", i, td[i].count, count);
444
445         hr = IWICPalette_GetColors(palette, count, color, &ret);
446         ok(hr == S_OK, "%u: GetColors error %#x\n", i, hr);
447         ok(ret == count, "%u: expected %u, got %u\n", i, count, ret);
448         if (ret == td[i].count)
449         {
450             UINT j;
451
452             if (td[i].type == WICBitmapPaletteTypeFixedGray16)
453                 generate_gray16_palette(td[i].color, td[i].count);
454             else if (td[i].type == WICBitmapPaletteTypeFixedGray256)
455                 generate_gray256_palette(td[i].color, td[i].count);
456             else if (td[i].type == WICBitmapPaletteTypeFixedHalftone8)
457                 generate_halftone8_palette(td[i].color, td[i].count, td[i].add_transparent);
458             else if (td[i].type == WICBitmapPaletteTypeFixedHalftone27)
459                 generate_halftone27_palette(td[i].color, td[i].count, td[i].add_transparent);
460             else if (td[i].type == WICBitmapPaletteTypeFixedHalftone64)
461                 generate_halftone64_palette(td[i].color, td[i].count, td[i].add_transparent);
462             else if (td[i].type == WICBitmapPaletteTypeFixedHalftone125)
463                 generate_halftone125_palette(td[i].color, td[i].count, td[i].add_transparent);
464             else if (td[i].type == WICBitmapPaletteTypeFixedHalftone216)
465                 generate_halftone216_palette(td[i].color, td[i].count, td[i].add_transparent);
466             else if (td[i].type == WICBitmapPaletteTypeFixedHalftone252)
467                 generate_halftone252_palette(td[i].color, td[i].count, td[i].add_transparent);
468             else if (td[i].type == WICBitmapPaletteTypeFixedHalftone256)
469                 generate_halftone256_palette(td[i].color, td[i].count, td[i].add_transparent);
470
471             for (j = 0; j < count; j++)
472             {
473                 ok(color[j] == td[i].color[j], "%u:[%u]: expected %#x, got %#x\n",
474                    i, j, td[i].color[j], color[j]);
475             }
476         }
477
478         IWICPalette_Release(palette);
479     }
480
481     IWICImagingFactory_Release(factory);
482 }
483
484 START_TEST(palette)
485 {
486     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
487
488     test_custom_palette();
489     test_predefined_palette();
490
491     CoUninitialize();
492 }