2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2012 Dmitry Timoshkov
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.
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.
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
28 #include "wine/test.h"
30 static void test_custom_palette(void)
32 IWICImagingFactory *factory;
35 WICBitmapPaletteType type=0xffffffff;
37 const WICColor initcolors[4]={0xff000000,0xff0000ff,0xffffff00,0xffffffff};
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;
46 hr = IWICImagingFactory_CreatePalette(factory, &palette);
47 ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
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);
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);
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);
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);
66 memcpy(colors, initcolors, sizeof(initcolors));
67 hr = IWICPalette_InitializeCustom(palette, colors, 4);
68 ok(SUCCEEDED(hr), "InitializeCustom failed, hr=%x\n", hr);
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);
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);
78 memset(colors, 0, sizeof(colors));
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");
85 memset(colors, 0, sizeof(colors));
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");
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);
97 hr = IWICPalette_HasAlpha(palette, &boolresult);
98 ok(SUCCEEDED(hr), "HasAlpha failed, hr=%x\n", hr);
99 ok(!boolresult, "expected FALSE, got TRUE\n");
101 hr = IWICPalette_IsBlackWhite(palette, &boolresult);
102 ok(SUCCEEDED(hr), "IsBlackWhite failed, hr=%x\n", hr);
103 ok(!boolresult, "expected FALSE, got TRUE\n");
105 hr = IWICPalette_IsGrayscale(palette, &boolresult);
106 ok(SUCCEEDED(hr), "IsGrayscale failed, hr=%x\n", hr);
107 ok(!boolresult, "expected FALSE, got TRUE\n");
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);
114 hr = IWICPalette_HasAlpha(palette, &boolresult);
115 ok(SUCCEEDED(hr), "HasAlpha failed, hr=%x\n", hr);
116 ok(boolresult, "expected TRUE, got FALSE\n");
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);
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);
126 hr = IWICPalette_GetColorCount(palette, NULL);
127 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
129 hr = IWICPalette_InitializeCustom(palette, NULL, 4);
130 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
132 hr = IWICPalette_GetColors(palette, 4, NULL, &count);
133 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
135 hr = IWICPalette_GetColors(palette, 4, colors, NULL);
136 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
138 hr = IWICPalette_HasAlpha(palette, NULL);
139 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
141 hr = IWICPalette_IsBlackWhite(palette, NULL);
142 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
144 hr = IWICPalette_IsGrayscale(palette, NULL);
145 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
147 IWICPalette_Release(palette);
150 IWICImagingFactory_Release(factory);
153 static void generate_gray16_palette(DWORD *entries, UINT count)
159 for (i = 0; i < 16; i++)
161 entries[i] = 0xff000000;
162 entries[i] |= (i << 20) | (i << 16) | (i << 12) | (i << 8) | (i << 4) | i;
166 static void generate_gray256_palette(DWORD *entries, UINT count)
170 assert(count == 256);
172 for (i = 0; i < 256; i++)
174 entries[i] = 0xff000000;
175 entries[i] |= (i << 16) | (i << 8) | i;
179 static void generate_halftone8_palette(DWORD *entries, UINT count)
185 for (i = 0; i < 8; i++)
187 entries[i] = 0xff000000;
188 if (i & 1) entries[i] |= 0xff;
189 if (i & 2) entries[i] |= 0xff00;
190 if (i & 4) entries[i] |= 0xff0000;
193 for (i = 8; i < 16; i++)
195 static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
196 0x000080, 0x808000, 0x800080, 0x008080 };
197 entries[i] = 0xff000000;
198 entries[i] |= halftone[i-8];
202 static void generate_halftone27_palette(DWORD *entries, UINT count)
204 static const BYTE halftone_values[4] = { 0x00,0x80,0xff };
209 for (i = 0; i < 27; i++)
211 entries[i] = 0xff000000;
212 entries[i] |= halftone_values[i%3];
213 entries[i] |= halftone_values[(i/3)%3] << 8;
214 entries[i] |= halftone_values[(i/9)%3] << 16;
217 entries[i] = 0xffc0c0c0;
220 static void generate_halftone64_palette(DWORD *entries, UINT count)
222 static const BYTE halftone_values[4] = { 0x00,0x55,0xaa,0xff };
227 for (i = 0; i < 64; i++)
229 entries[i] = 0xff000000;
230 entries[i] |= halftone_values[i%4];
231 entries[i] |= halftone_values[(i/4)%4] << 8;
232 entries[i] |= halftone_values[(i/16)%4] << 16;
235 for (i = 64; i < 72; i++)
237 static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
238 0x000080, 0x808000, 0x800080, 0x008080 };
239 entries[i] = 0xff000000;
240 entries[i] |= halftone[i-64];
244 static void generate_halftone125_palette(DWORD *entries, UINT count)
246 static const BYTE halftone_values[5] = { 0x00,0x40,0x80,0xbf,0xff };
249 assert(count == 126);
251 for (i = 0; i < 125; i++)
253 entries[i] = 0xff000000;
254 entries[i] |= halftone_values[i%5];
255 entries[i] |= halftone_values[(i/5)%5] << 8;
256 entries[i] |= halftone_values[(i/25)%5] << 16;
259 entries[i] = 0xffc0c0c0;
262 static void generate_halftone216_palette(DWORD *entries, UINT count)
264 static const BYTE halftone_values[6] = { 0x00,0x33,0x66,0x99,0xcc,0xff };
267 assert(count == 224);
269 for (i = 0; i < 216; i++)
271 entries[i] = 0xff000000;
272 entries[i] |= halftone_values[i%6];
273 entries[i] |= halftone_values[(i/6)%6] << 8;
274 entries[i] |= halftone_values[(i/36)%6] << 16;
277 for (i = 216; i < 224; i++)
279 static const DWORD halftone[8] = { 0xc0c0c0, 0x808080, 0x800000, 0x008000,
280 0x000080, 0x808000, 0x800080, 0x008080 };
281 entries[i] = 0xff000000;
282 entries[i] |= halftone[i-216];
286 static void generate_halftone256_palette(DWORD *entries, UINT count)
288 static const BYTE halftone_values_b[4] = { 0x00,0x55,0xaa,0xff };
289 static const BYTE halftone_values_gr[8] = { 0x00,0x24,0x49,0x6d,0x92,0xb6,0xdb,0xff };
292 assert(count == 256);
294 for (i = 0; i < 256; i++)
296 entries[i] = 0xff000000;
297 entries[i] |= halftone_values_b[i%4];
298 entries[i] |= halftone_values_gr[(i/4)%8] << 8;
299 entries[i] |= halftone_values_gr[(i/32)%8] << 16;
303 static void test_predefined_palette(void)
305 static struct test_data
307 WICBitmapPaletteType type;
313 { WICBitmapPaletteTypeFixedBW, 1, 1, 2, { 0xff000000, 0xffffffff } },
314 { WICBitmapPaletteTypeFixedGray4, 0, 1, 4,
315 { 0xff000000, 0xff555555, 0xffaaaaaa, 0xffffffff } },
316 { WICBitmapPaletteTypeFixedGray16, 0, 1, 16, { 0 } },
317 { WICBitmapPaletteTypeFixedGray256, 0, 1, 256, { 0 } },
318 { WICBitmapPaletteTypeFixedHalftone8, 0, 0, 16, { 0 } },
319 { WICBitmapPaletteTypeFixedHalftone27, 0, 0, 28, { 0 } },
320 { WICBitmapPaletteTypeFixedHalftone64, 0, 0, 72, { 0 } },
321 { WICBitmapPaletteTypeFixedHalftone125, 0, 0, 126, { 0 } },
322 { WICBitmapPaletteTypeFixedHalftone216, 0, 0, 224, { 0 } },
323 { WICBitmapPaletteTypeFixedHalftone256, 0, 0, 256, { 0 } },
325 IWICImagingFactory *factory;
326 IWICPalette *palette;
328 WICBitmapPaletteType type;
333 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
334 &IID_IWICImagingFactory, (void **)&factory);
335 ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
337 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
339 hr = IWICImagingFactory_CreatePalette(factory, &palette);
340 ok(hr == S_OK, "%u: CreatePalette error %#x\n", i, hr);
342 hr = IWICPalette_InitializePredefined(palette, td[i].type, FALSE);
343 ok(hr == S_OK, "%u: InitializePredefined error %#x\n", i, hr);
346 hr = IWICPalette_IsBlackWhite(palette, &bret);
347 ok(hr == S_OK, "%u: IsBlackWhite error %#x\n", i, hr);
348 ok(bret == td[i].is_bw ||
349 broken(td[i].type == WICBitmapPaletteTypeFixedBW && bret != td[i].is_bw), /* XP */
350 "%u: expected %d, got %d\n",i, td[i].is_bw, bret);
353 hr = IWICPalette_IsGrayscale(palette, &bret);
354 ok(hr == S_OK, "%u: IsGrayscale error %#x\n", i, hr);
355 ok(bret == td[i].is_gray, "%u: expected %d, got %d\n", i, td[i].is_gray, bret);
358 hr = IWICPalette_GetType(palette, &type);
359 ok(hr == S_OK, "%u: GetType error %#x\n", i, hr);
360 ok(type == td[i].type, "%u: expected %#x, got %#x\n", i, td[i].type, type);
363 hr = IWICPalette_GetColorCount(palette, &count);
364 ok(hr == S_OK, "%u: GetColorCount error %#x\n", i, hr);
365 ok(count == td[i].count, "%u: expected %u, got %u\n", i, td[i].count, count);
367 hr = IWICPalette_GetColors(palette, count, color, &ret);
368 ok(hr == S_OK, "%u: GetColors error %#x\n", i, hr);
369 ok(ret == count, "%u: expected %u, got %u\n", i, count, ret);
370 if (ret == td[i].count)
374 if (td[i].type == WICBitmapPaletteTypeFixedGray16)
375 generate_gray16_palette(td[i].color, td[i].count);
376 else if (td[i].type == WICBitmapPaletteTypeFixedGray256)
377 generate_gray256_palette(td[i].color, td[i].count);
378 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone8)
379 generate_halftone8_palette(td[i].color, td[i].count);
380 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone27)
381 generate_halftone27_palette(td[i].color, td[i].count);
382 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone64)
383 generate_halftone64_palette(td[i].color, td[i].count);
384 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone125)
385 generate_halftone125_palette(td[i].color, td[i].count);
386 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone216)
387 generate_halftone216_palette(td[i].color, td[i].count);
388 else if (td[i].type == WICBitmapPaletteTypeFixedHalftone256)
389 generate_halftone256_palette(td[i].color, td[i].count);
391 for (j = 0; j < count; j++)
393 ok(color[j] == td[i].color[j], "%u:[%u]: expected %#x, got %#x\n",
394 i, j, td[i].color[j], color[j]);
398 IWICPalette_Release(palette);
401 IWICImagingFactory_Release(factory);
406 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
408 test_custom_palette();
409 test_predefined_palette();