7302d72582155643f43fad4436fb15be75dd1f9a
[reactos.git] / modules / rostests / winetests / windowscodecs / palette.c
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2012,2016 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 WIN32_NO_STATUS
24 #define _INC_WINDOWS
25 #define COM_NO_WINDOWS_H
26
27 #define COBJMACROS
28
29 #include <windef.h>
30 #include <winbase.h>
31 #include <objbase.h>
32 #include <wincodec.h>
33 #include <wine/test.h>
34
35 static IWICImagingFactory *factory;
36
37 static void test_custom_palette(void)
38 {
39 IWICPalette *palette, *palette2;
40 HRESULT hr;
41 WICBitmapPaletteType type=0xffffffff;
42 UINT count=1;
43 const WICColor initcolors[4]={0xff000000,0xff0000ff,0xffffff00,0xffffffff};
44 WICColor colors[4];
45 BOOL boolresult;
46
47 hr = IWICImagingFactory_CreatePalette(factory, &palette);
48 ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
49 if (SUCCEEDED(hr))
50 {
51 hr = IWICPalette_GetType(palette, &type);
52 ok(SUCCEEDED(hr), "GetType failed, hr=%x\n", hr);
53 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %x\n", type);
54
55 hr = IWICPalette_GetColorCount(palette, &count);
56 ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
57 ok(count == 0, "expected 0, got %u\n", count);
58
59 hr = IWICPalette_GetColors(palette, 0, colors, &count);
60 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
61 ok(count == 0, "expected 0, got %u\n", count);
62
63 hr = IWICPalette_GetColors(palette, 4, colors, &count);
64 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
65 ok(count == 0, "expected 0, got %u\n", count);
66
67 memcpy(colors, initcolors, sizeof(initcolors));
68 hr = IWICPalette_InitializeCustom(palette, colors, 4);
69 ok(SUCCEEDED(hr), "InitializeCustom failed, hr=%x\n", hr);
70
71 hr = IWICPalette_GetType(palette, &type);
72 ok(SUCCEEDED(hr), "GetType failed, hr=%x\n", hr);
73 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %x\n", type);
74
75 hr = IWICPalette_GetColorCount(palette, &count);
76 ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
77 ok(count == 4, "expected 4, got %u\n", count);
78
79 memset(colors, 0, sizeof(colors));
80 count = 0;
81 hr = IWICPalette_GetColors(palette, 4, colors, &count);
82 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
83 ok(count == 4, "expected 4, got %u\n", count);
84 ok(!memcmp(colors, initcolors, sizeof(colors)), "got unexpected palette data\n");
85
86 memset(colors, 0, sizeof(colors));
87 count = 0;
88 hr = IWICPalette_GetColors(palette, 2, colors, &count);
89 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
90 ok(count == 2, "expected 2, got %u\n", count);
91 ok(!memcmp(colors, initcolors, sizeof(WICColor)*2), "got unexpected palette data\n");
92
93 count = 0;
94 hr = IWICPalette_GetColors(palette, 6, colors, &count);
95 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
96 ok(count == 4, "expected 4, got %u\n", count);
97
98 hr = IWICPalette_HasAlpha(palette, &boolresult);
99 ok(SUCCEEDED(hr), "HasAlpha failed, hr=%x\n", hr);
100 ok(!boolresult, "expected FALSE, got TRUE\n");
101
102 hr = IWICPalette_IsBlackWhite(palette, &boolresult);
103 ok(SUCCEEDED(hr), "IsBlackWhite failed, hr=%x\n", hr);
104 ok(!boolresult, "expected FALSE, got TRUE\n");
105
106 hr = IWICPalette_IsGrayscale(palette, &boolresult);
107 ok(SUCCEEDED(hr), "IsGrayscale failed, hr=%x\n", hr);
108 ok(!boolresult, "expected FALSE, got TRUE\n");
109
110 hr = IWICImagingFactory_CreatePalette(factory, &palette2);
111 ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
112
113 hr = IWICPalette_InitializeFromPalette(palette2, palette);
114 ok(SUCCEEDED(hr), "InitializeFromPalette failed, hr=%x\n", hr);
115
116 type = 0xdeadbeef;
117 hr = IWICPalette_GetType(palette2, &type);
118 ok(SUCCEEDED(hr), "GetType failed, hr=%x\n", hr);
119 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %x\n", type);
120
121 count = 0xdeadbeef;
122 hr = IWICPalette_GetColorCount(palette2, &count);
123 ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
124 ok(count == 4, "expected 4, got %u\n", count);
125
126 memset(colors, 0, sizeof(colors));
127 count = 0xdeadbeef;
128 hr = IWICPalette_GetColors(palette2, 4, colors, &count);
129 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
130 ok(count == 4, "expected 4, got %u\n", count);
131 ok(!memcmp(colors, initcolors, sizeof(colors)), "got unexpected palette data\n");
132
133 /* try a palette with some alpha in it */
134 colors[2] = 0x80ffffff;
135 hr = IWICPalette_InitializeCustom(palette, colors, 4);
136 ok(SUCCEEDED(hr), "InitializeCustom failed, hr=%x\n", hr);
137
138 hr = IWICPalette_HasAlpha(palette, &boolresult);
139 ok(SUCCEEDED(hr), "HasAlpha failed, hr=%x\n", hr);
140 ok(boolresult, "expected TRUE, got FALSE\n");
141
142 /* setting to a 0-color palette is acceptable */
143 hr = IWICPalette_InitializeCustom(palette, NULL, 0);
144 ok(SUCCEEDED(hr), "InitializeCustom failed, hr=%x\n", hr);
145
146 type = 0xdeadbeef;
147 hr = IWICPalette_GetType(palette, &type);
148 ok(SUCCEEDED(hr), "GetType failed, hr=%x\n", hr);
149 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %x\n", type);
150
151 count = 0xdeadbeef;
152 hr = IWICPalette_GetColorCount(palette, &count);
153 ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
154 ok(count == 0, "expected 0, got %u\n", count);
155
156 count = 0xdeadbeef;
157 hr = IWICPalette_GetColors(palette, 4, colors, &count);
158 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
159 ok(count == 0, "expected 0, got %u\n", count);
160
161 hr = IWICPalette_InitializeFromPalette(palette2, palette);
162 ok(SUCCEEDED(hr), "InitializeFromPalette failed, hr=%x\n", hr);
163
164 type = 0xdeadbeef;
165 hr = IWICPalette_GetType(palette2, &type);
166 ok(SUCCEEDED(hr), "GetType failed, hr=%x\n", hr);
167 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %x\n", type);
168
169 count = 0xdeadbeef;
170 hr = IWICPalette_GetColorCount(palette2, &count);
171 ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
172 ok(count == 0, "expected 0, got %u\n", count);
173
174 memset(colors, 0, sizeof(colors));
175 count = 0xdeadbeef;
176 hr = IWICPalette_GetColors(palette2, 4, colors, &count);
177 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
178 ok(count == 0, "expected 0, got %u\n", count);
179
180 /* IWICPalette is paranoid about NULL pointers */
181 hr = IWICPalette_GetType(palette, NULL);
182 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
183
184 hr = IWICPalette_GetColorCount(palette, NULL);
185 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
186
187 hr = IWICPalette_InitializeCustom(palette, NULL, 4);
188 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
189
190 hr = IWICPalette_GetColors(palette, 4, NULL, &count);
191 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
192
193 hr = IWICPalette_GetColors(palette, 4, colors, NULL);
194 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
195
196 hr = IWICPalette_HasAlpha(palette, NULL);
197 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
198
199 hr = IWICPalette_IsBlackWhite(palette, NULL);
200 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
201
202 hr = IWICPalette_IsGrayscale(palette, NULL);
203 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
204
205 hr = IWICPalette_InitializeFromPalette(palette, NULL);
206 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
207
208 IWICPalette_Release(palette2);
209 IWICPalette_Release(palette);
210 }
211 }
212
213 static void generate_gray16_palette(DWORD *entries, UINT count)
214 {
215 UINT i;
216
217 assert(count == 16);
218
219 for (i = 0; i < 16; i++)
220 {
221 entries[i] = 0xff000000;
222 entries[i] |= (i << 20) | (i << 16) | (i << 12) | (i << 8) | (i << 4) | i;
223 }
224 }
225
226 static void generate_gray256_palette(DWORD *entries, UINT count)
227 {
228 UINT i;
229
230 assert(count == 256);
231
232 for (i = 0; i < 256; i++)
233 {
234 entries[i] = 0xff000000;
235 entries[i] |= (i << 16) | (i << 8) | i;
236 }
237 }
238
239 static void generate_halftone8_palette(DWORD *entries, UINT count, BOOL add_transparent)
240 {
241 UINT i;
242
243 if (add_transparent)
244 ok(count == 17, "expected 17, got %u\n", count);
245 else
246 ok(count == 16, "expected 16, got %u\n", count);
247
248 for (i = 0; i < 8; i++)
249 {
250 entries[i] = 0xff000000;
251 if (i & 1) entries[i] |= 0xff;
252 if (i & 2) entries[i] |= 0xff00;
253 if (i & 4) entries[i] |= 0xff0000;
254 }
255
256 for (i = 8; i < 16; i++)
257 {
258 static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
259 0x000080, 0x808000, 0x800080, 0x008080 };
260 entries[i] = 0xff000000;
261 entries[i] |= halftone[i-8];
262 }
263
264 if (add_transparent)
265 entries[i] = 0;
266 }
267
268 static void generate_halftone27_palette(DWORD *entries, UINT count, BOOL add_transparent)
269 {
270 static const BYTE halftone_values[4] = { 0x00,0x80,0xff };
271 UINT i;
272
273 if (add_transparent)
274 ok(count == 29, "expected 29, got %u\n", count);
275 else
276 ok(count == 28, "expected 28, got %u\n", count);
277
278 for (i = 0; i < 27; i++)
279 {
280 entries[i] = 0xff000000;
281 entries[i] |= halftone_values[i%3];
282 entries[i] |= halftone_values[(i/3)%3] << 8;
283 entries[i] |= halftone_values[(i/9)%3] << 16;
284 }
285
286 entries[i++] = 0xffc0c0c0;
287 if (add_transparent)
288 entries[i] = 0;
289 }
290
291 static void generate_halftone64_palette(DWORD *entries, UINT count, BOOL add_transparent)
292 {
293 static const BYTE halftone_values[4] = { 0x00,0x55,0xaa,0xff };
294 UINT i;
295
296 if (add_transparent)
297 ok(count == 73, "expected 73, got %u\n", count);
298 else
299 ok(count == 72, "expected 72, got %u\n", count);
300
301 for (i = 0; i < 64; i++)
302 {
303 entries[i] = 0xff000000;
304 entries[i] |= halftone_values[i%4];
305 entries[i] |= halftone_values[(i/4)%4] << 8;
306 entries[i] |= halftone_values[(i/16)%4] << 16;
307 }
308
309 for (i = 64; i < 72; i++)
310 {
311 static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
312 0x000080, 0x808000, 0x800080, 0x008080 };
313 entries[i] = 0xff000000;
314 entries[i] |= halftone[i-64];
315 }
316
317 if (add_transparent)
318 entries[i] = 0;
319 }
320
321 static void generate_halftone125_palette(DWORD *entries, UINT count, BOOL add_transparent)
322 {
323 static const BYTE halftone_values[5] = { 0x00, 0x40, 0x80, 0xbf, 0xff };
324 UINT i;
325
326 if (add_transparent)
327 ok(count == 127, "expected 127, got %u\n", count);
328 else
329 ok(count == 126, "expected 126, got %u\n", count);
330
331 for (i = 0; i < 125; i++)
332 {
333 entries[i] = 0xff000000;
334 entries[i] |= halftone_values[i%5];
335 entries[i] |= halftone_values[(i/5)%5] << 8;
336 entries[i] |= halftone_values[(i/25)%5] << 16;
337 }
338
339 entries[i++] = 0xffc0c0c0;
340 if (add_transparent)
341 entries[i] = 0;
342 }
343
344 static void generate_halftone216_palette(DWORD *entries, UINT count, BOOL add_transparent)
345 {
346 static const BYTE halftone_values[6] = { 0x00,0x33,0x66,0x99,0xcc,0xff };
347 UINT i;
348
349 if (add_transparent)
350 ok(count == 225, "expected 225, got %u\n", count);
351 else
352 ok(count == 224, "expected 224, got %u\n", count);
353
354 for (i = 0; i < 216; i++)
355 {
356 entries[i] = 0xff000000;
357 entries[i] |= halftone_values[i%6];
358 entries[i] |= halftone_values[(i/6)%6] << 8;
359 entries[i] |= halftone_values[(i/36)%6] << 16;
360 }
361
362 for (i = 216; i < 224; i++)
363 {
364 static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
365 0x000080, 0x808000, 0x800080, 0x008080 };
366 entries[i] = 0xff000000;
367 entries[i] |= halftone[i-216];
368 }
369
370 if (add_transparent)
371 entries[i] = 0;
372 }
373
374 static void generate_halftone252_palette(DWORD *entries, UINT count, BOOL add_transparent)
375 {
376 static const BYTE halftone_values_rb[6] = { 0x00,0x33,0x66,0x99,0xcc,0xff };
377 static const BYTE halftone_values_g[7] = { 0x00,0x2b,0x55,0x80,0xaa,0xd5,0xff };
378 UINT i;
379
380 if (add_transparent)
381 ok(count == 253, "expected 253, got %u\n", count);
382 else
383 ok(count == 252, "expected 252, got %u\n", count);
384
385 for (i = 0; i < 252; i++)
386 {
387 entries[i] = 0xff000000;
388 entries[i] |= halftone_values_rb[i%6];
389 entries[i] |= halftone_values_g[(i/6)%7] << 8;
390 entries[i] |= halftone_values_rb[(i/42)%6] << 16;
391 }
392
393 if (add_transparent)
394 entries[i] = 0;
395 }
396
397 static void generate_halftone256_palette(DWORD *entries, UINT count, BOOL add_transparent)
398 {
399 static const BYTE halftone_values_b[4] = { 0x00,0x55,0xaa,0xff };
400 static const BYTE halftone_values_gr[8] = { 0x00,0x24,0x49,0x6d,0x92,0xb6,0xdb,0xff };
401 UINT i;
402
403 assert(count == 256);
404
405 for (i = 0; i < 256; i++)
406 {
407 entries[i] = 0xff000000;
408 entries[i] |= halftone_values_b[i%4];
409 entries[i] |= halftone_values_gr[(i/4)%8] << 8;
410 entries[i] |= halftone_values_gr[(i/32)%8] << 16;
411 }
412
413 if (add_transparent)
414 entries[255] = 0;
415 }
416
417 static void test_predefined_palette(void)
418 {
419 static struct test_data
420 {
421 WICBitmapPaletteType type;
422 BOOL is_bw, is_gray;
423 UINT count;
424 WICColor color[256];
425 BOOL add_transparent;
426 } td[] =
427 {
428 { WICBitmapPaletteTypeFixedBW, 1, 1, 2, { 0xff000000, 0xffffffff } },
429 { WICBitmapPaletteTypeFixedBW, 1, 1, 2, { 0xff000000, 0xffffffff }, 1 },
430 { WICBitmapPaletteTypeFixedGray4, 0, 1, 4,
431 { 0xff000000, 0xff555555, 0xffaaaaaa, 0xffffffff } },
432 { WICBitmapPaletteTypeFixedGray4, 0, 1, 4,
433 { 0xff000000, 0xff555555, 0xffaaaaaa, 0xffffffff }, 1 },
434 { WICBitmapPaletteTypeFixedGray16, 0, 1, 16, { 0 } },
435 { WICBitmapPaletteTypeFixedGray16, 0, 1, 16, { 0 }, 1 },
436 { WICBitmapPaletteTypeFixedGray256, 0, 1, 256, { 0 } },
437 { WICBitmapPaletteTypeFixedGray256, 0, 1, 256, { 0 }, 1 },
438 { WICBitmapPaletteTypeFixedHalftone8, 0, 0, 16, { 0 } },
439 { WICBitmapPaletteTypeFixedHalftone8, 0, 0, 17, { 0 }, 1 },
440 { WICBitmapPaletteTypeFixedHalftone27, 0, 0, 28, { 0 } },
441 { WICBitmapPaletteTypeFixedHalftone27, 0, 0, 29, { 0 }, 1 },
442 { WICBitmapPaletteTypeFixedHalftone64, 0, 0, 72, { 0 } },
443 { WICBitmapPaletteTypeFixedHalftone64, 0, 0, 73, { 0 }, 1 },
444 { WICBitmapPaletteTypeFixedHalftone125, 0, 0, 126, { 0 } },
445 { WICBitmapPaletteTypeFixedHalftone125, 0, 0, 127, { 0 }, 1 },
446 { WICBitmapPaletteTypeFixedHalftone216, 0, 0, 224, { 0 } },
447 { WICBitmapPaletteTypeFixedHalftone216, 0, 0, 225, { 0 }, 1 },
448 { WICBitmapPaletteTypeFixedHalftone252, 0, 0, 252, { 0 } },
449 { WICBitmapPaletteTypeFixedHalftone252, 0, 0, 253, { 0 }, 1 },
450 { WICBitmapPaletteTypeFixedHalftone256, 0, 0, 256, { 0 } },
451 { WICBitmapPaletteTypeFixedHalftone256, 0, 0, 256, { 0 }, 1 }
452 };
453 IWICPalette *palette;
454 HRESULT hr;
455 WICBitmapPaletteType type;
456 UINT count, i, ret;
457 BOOL bret;
458 WICColor color[256];
459
460 hr = IWICImagingFactory_CreatePalette(factory, &palette);
461 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
462 hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeCustom, FALSE);
463 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
464 hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeMedianCut, FALSE);
465 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
466 hr = IWICPalette_InitializePredefined(palette, 0x0f, FALSE);
467 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
468 IWICPalette_Release(palette);
469
470 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
471 {
472 hr = IWICImagingFactory_CreatePalette(factory, &palette);
473 ok(hr == S_OK, "%u: CreatePalette error %#x\n", i, hr);
474
475 hr = IWICPalette_InitializePredefined(palette, td[i].type, td[i].add_transparent);
476 ok(hr == S_OK, "%u: InitializePredefined error %#x\n", i, hr);
477
478 bret = -1;
479 hr = IWICPalette_IsBlackWhite(palette, &bret);
480 ok(hr == S_OK, "%u: IsBlackWhite error %#x\n", i, hr);
481 ok(bret == td[i].is_bw ||
482 broken(td[i].type == WICBitmapPaletteTypeFixedBW && bret != td[i].is_bw), /* XP */
483 "%u: expected %d, got %d\n",i, td[i].is_bw, bret);
484
485 bret = -1;
486 hr = IWICPalette_IsGrayscale(palette, &bret);
487 ok(hr == S_OK, "%u: IsGrayscale error %#x\n", i, hr);
488 ok(bret == td[i].is_gray, "%u: expected %d, got %d\n", i, td[i].is_gray, bret);
489
490 type = -1;
491 hr = IWICPalette_GetType(palette, &type);
492 ok(hr == S_OK, "%u: GetType error %#x\n", i, hr);
493 ok(type == td[i].type, "%u: expected %#x, got %#x\n", i, td[i].type, type);
494
495 count = 0xdeadbeef;
496 hr = IWICPalette_GetColorCount(palette, &count);
497 ok(hr == S_OK, "%u: GetColorCount error %#x\n", i, hr);
498 ok(count == td[i].count, "%u: expected %u, got %u\n", i, td[i].count, count);
499
500 hr = IWICPalette_GetColors(palette, count, color, &ret);
501 ok(hr == S_OK, "%u: GetColors error %#x\n", i, hr);
502 ok(ret == count, "%u: expected %u, got %u\n", i, count, ret);
503 if (ret == td[i].count)
504 {
505 UINT j;
506
507 if (td[i].type == WICBitmapPaletteTypeFixedGray16)
508 generate_gray16_palette(td[i].color, td[i].count);
509 else if (td[i].type == WICBitmapPaletteTypeFixedGray256)
510 generate_gray256_palette(td[i].color, td[i].count);
511 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone8)
512 generate_halftone8_palette(td[i].color, td[i].count, td[i].add_transparent);
513 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone27)
514 generate_halftone27_palette(td[i].color, td[i].count, td[i].add_transparent);
515 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone64)
516 generate_halftone64_palette(td[i].color, td[i].count, td[i].add_transparent);
517 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone125)
518 generate_halftone125_palette(td[i].color, td[i].count, td[i].add_transparent);
519 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone216)
520 generate_halftone216_palette(td[i].color, td[i].count, td[i].add_transparent);
521 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone252)
522 generate_halftone252_palette(td[i].color, td[i].count, td[i].add_transparent);
523 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone256)
524 generate_halftone256_palette(td[i].color, td[i].count, td[i].add_transparent);
525
526 for (j = 0; j < count; j++)
527 {
528 ok(color[j] == td[i].color[j], "%u:[%u]: expected %#x, got %#x\n",
529 i, j, td[i].color[j], color[j]);
530 }
531 }
532
533 IWICPalette_Release(palette);
534 }
535 }
536
537 static BYTE *init_bitmap(UINT *width, UINT *height, UINT *stride)
538 {
539 BYTE *src;
540 UINT i, j, scale;
541
542 *width = 256;
543 *height = 256;
544 *stride = (*width * 3 + 3) & ~3;
545 trace("width %d, height %d, stride %d\n", *width, *height, *stride);
546
547 src = HeapAlloc(GetProcessHeap(), 0, *stride * *height);
548
549 scale = 256 / *width;
550 if (!scale) scale = 1;
551
552 for (i = 0; i < *height; i++)
553 {
554 for (j = 0; j < *width; j++)
555 {
556 src[i * *stride + j*3 + 0] = scale * i;
557 src[i * *stride + j*3 + 1] = scale * (255 - (i+j)/2);
558 src[i * *stride + j*3 + 2] = scale * j;
559 }
560 }
561
562 return src;
563 }
564
565 static void test_palette_from_bitmap(void)
566 {
567 HRESULT hr;
568 BYTE *data;
569 IWICBitmap *bitmap;
570 IWICPalette *palette;
571 WICBitmapPaletteType type;
572 UINT width, height, stride, count, ret;
573 WICColor color[257];
574
575 data = init_bitmap(&width, &height, &stride);
576
577 hr = IWICImagingFactory_CreateBitmapFromMemory(factory, width, height, &GUID_WICPixelFormat24bppRGB,
578 stride, stride * height, data, &bitmap);
579 ok(hr == S_OK, "CreateBitmapFromMemory error %#x\n", hr);
580
581 hr = IWICImagingFactory_CreatePalette(factory, &palette);
582 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
583
584 hr = IWICPalette_InitializeFromBitmap(palette, (IWICBitmapSource *)bitmap, 0, FALSE);
585 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
586
587 hr = IWICPalette_InitializeFromBitmap(palette, (IWICBitmapSource *)bitmap, 1, FALSE);
588 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
589
590 hr = IWICPalette_InitializeFromBitmap(palette, (IWICBitmapSource *)bitmap, 257, FALSE);
591 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
592
593 hr = IWICPalette_InitializeFromBitmap(palette, NULL, 16, FALSE);
594 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
595
596 hr = IWICPalette_InitializeFromBitmap(palette, (IWICBitmapSource *)bitmap, 2, FALSE);
597 ok(hr == S_OK, "InitializeFromBitmap error %#x\n", hr);
598 count = 0;
599 hr = IWICPalette_GetColorCount(palette, &count);
600 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
601 ok(count == 2, "expected 2, got %u\n", count);
602
603 hr = IWICPalette_InitializeFromBitmap(palette, (IWICBitmapSource *)bitmap, 2, TRUE);
604 ok(hr == S_OK, "InitializeFromBitmap error %#x\n", hr);
605 count = 0;
606 hr = IWICPalette_GetColorCount(palette, &count);
607 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
608 ok(count == 2, "expected 2, got %u\n", count);
609
610 /* without trasparent color */
611 hr = IWICPalette_InitializeFromBitmap(palette, (IWICBitmapSource *)bitmap, 16, FALSE);
612 ok(hr == S_OK, "InitializeFromBitmap error %#x\n", hr);
613 type = -1;
614 hr = IWICPalette_GetType(palette, &type);
615 ok(hr == S_OK, "GetType error %#x\n", hr);
616 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %#x\n", type);
617 count = 0;
618 hr = IWICPalette_GetColorCount(palette, &count);
619 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
620 ok(count == 16, "expected 16, got %u\n", count);
621 memset(color, 0, sizeof(color));
622 hr = IWICPalette_GetColors(palette, count, color, &ret);
623 ok(hr == S_OK, "GetColors error %#x\n", hr);
624 ok(ret == count, "expected %u, got %u\n", count, ret);
625 ok(color[count - 1] != 0, "expected !0, got %08x\n", color[count - 1]);
626
627 /* with trasparent color */
628 hr = IWICPalette_InitializeFromBitmap(palette, (IWICBitmapSource *)bitmap, 16, TRUE);
629 ok(hr == S_OK, "InitializeFromBitmap error %#x\n", hr);
630 type = -1;
631 hr = IWICPalette_GetType(palette, &type);
632 ok(hr == S_OK, "GetType error %#x\n", hr);
633 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %#x\n", type);
634 count = 0;
635 hr = IWICPalette_GetColorCount(palette, &count);
636 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
637 ok(count == 16, "expected 16, got %u\n", count);
638 memset(color, 0xff, sizeof(color));
639 hr = IWICPalette_GetColors(palette, count, color, &ret);
640 ok(hr == S_OK, "GetColors error %#x\n", hr);
641 ok(ret == count, "expected %u, got %u\n", count, ret);
642 ok(color[count - 1] == 0, "expected 0, got %08x\n", color[count - 1]);
643
644 IWICPalette_Release(palette);
645 IWICBitmap_Release(bitmap);
646
647 HeapFree(GetProcessHeap(), 0, data);
648 }
649
650 START_TEST(palette)
651 {
652 HRESULT hr;
653
654 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
655
656 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
657 &IID_IWICImagingFactory, (void **)&factory);
658 ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
659
660 test_custom_palette();
661 test_predefined_palette();
662 test_palette_from_bitmap();
663
664 IWICImagingFactory_Release(factory);
665
666 CoUninitialize();
667 }