windowscodecs: Add support for generating WICBitmapPaletteTypeFixedGray16 palette.
[wine/multimedia.git] / dlls / windowscodecs / tests / palette.c
blobaba81681d6be05d4741a26e3b65284b8c3ce567e
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
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 #include <stdarg.h>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "objbase.h"
26 #include "wincodec.h"
27 #include "wine/test.h"
29 static void test_custom_palette(void)
31 IWICImagingFactory *factory;
32 IWICPalette *palette;
33 HRESULT hr;
34 WICBitmapPaletteType type=0xffffffff;
35 UINT count=1;
36 const WICColor initcolors[4]={0xff000000,0xff0000ff,0xffffff00,0xffffffff};
37 WICColor colors[4];
38 BOOL boolresult;
40 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
41 &IID_IWICImagingFactory, (void**)&factory);
42 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
43 if (FAILED(hr)) return;
45 hr = IWICImagingFactory_CreatePalette(factory, &palette);
46 ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
47 if (SUCCEEDED(hr))
49 hr = IWICPalette_GetType(palette, &type);
50 ok(SUCCEEDED(hr), "GetType failed, hr=%x\n", hr);
51 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %x\n", type);
53 hr = IWICPalette_GetColorCount(palette, &count);
54 ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
55 ok(count == 0, "expected 0, got %u\n", count);
57 hr = IWICPalette_GetColors(palette, 0, colors, &count);
58 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
59 ok(count == 0, "expected 0, got %u\n", count);
61 hr = IWICPalette_GetColors(palette, 4, colors, &count);
62 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
63 ok(count == 0, "expected 0, got %u\n", count);
65 memcpy(colors, initcolors, sizeof(initcolors));
66 hr = IWICPalette_InitializeCustom(palette, colors, 4);
67 ok(SUCCEEDED(hr), "InitializeCustom failed, hr=%x\n", hr);
69 hr = IWICPalette_GetType(palette, &type);
70 ok(SUCCEEDED(hr), "GetType failed, hr=%x\n", hr);
71 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %x\n", type);
73 hr = IWICPalette_GetColorCount(palette, &count);
74 ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
75 ok(count == 4, "expected 4, got %u\n", count);
77 memset(colors, 0, sizeof(colors));
78 count = 0;
79 hr = IWICPalette_GetColors(palette, 4, colors, &count);
80 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
81 ok(count == 4, "expected 4, got %u\n", count);
82 ok(!memcmp(colors, initcolors, sizeof(colors)), "got unexpected palette data\n");
84 memset(colors, 0, sizeof(colors));
85 count = 0;
86 hr = IWICPalette_GetColors(palette, 2, colors, &count);
87 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
88 ok(count == 2, "expected 2, got %u\n", count);
89 ok(!memcmp(colors, initcolors, sizeof(WICColor)*2), "got unexpected palette data\n");
91 count = 0;
92 hr = IWICPalette_GetColors(palette, 6, colors, &count);
93 ok(SUCCEEDED(hr), "GetColors failed, hr=%x\n", hr);
94 ok(count == 4, "expected 4, got %u\n", count);
96 hr = IWICPalette_HasAlpha(palette, &boolresult);
97 ok(SUCCEEDED(hr), "HasAlpha failed, hr=%x\n", hr);
98 ok(!boolresult, "expected FALSE, got TRUE\n");
100 hr = IWICPalette_IsBlackWhite(palette, &boolresult);
101 ok(SUCCEEDED(hr), "IsBlackWhite failed, hr=%x\n", hr);
102 ok(!boolresult, "expected FALSE, got TRUE\n");
104 hr = IWICPalette_IsGrayscale(palette, &boolresult);
105 ok(SUCCEEDED(hr), "IsGrayscale failed, hr=%x\n", hr);
106 ok(!boolresult, "expected FALSE, got TRUE\n");
108 /* try a palette with some alpha in it */
109 colors[2] = 0x80ffffff;
110 hr = IWICPalette_InitializeCustom(palette, colors, 4);
111 ok(SUCCEEDED(hr), "InitializeCustom failed, hr=%x\n", hr);
113 hr = IWICPalette_HasAlpha(palette, &boolresult);
114 ok(SUCCEEDED(hr), "HasAlpha failed, hr=%x\n", hr);
115 ok(boolresult, "expected TRUE, got FALSE\n");
117 /* setting to a 0-color palette is acceptable */
118 hr = IWICPalette_InitializeCustom(palette, NULL, 0);
119 ok(SUCCEEDED(hr), "InitializeCustom failed, hr=%x\n", hr);
121 /* IWICPalette is paranoid about NULL pointers */
122 hr = IWICPalette_GetType(palette, NULL);
123 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
125 hr = IWICPalette_GetColorCount(palette, NULL);
126 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
128 hr = IWICPalette_InitializeCustom(palette, NULL, 4);
129 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
131 hr = IWICPalette_GetColors(palette, 4, NULL, &count);
132 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
134 hr = IWICPalette_GetColors(palette, 4, colors, NULL);
135 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
137 hr = IWICPalette_HasAlpha(palette, NULL);
138 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
140 hr = IWICPalette_IsBlackWhite(palette, NULL);
141 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
143 hr = IWICPalette_IsGrayscale(palette, NULL);
144 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
146 IWICPalette_Release(palette);
149 IWICImagingFactory_Release(factory);
152 static void generate_gray16_palette(DWORD *entries, UINT count)
154 UINT i;
156 assert(count == 16);
158 for (i = 0; i < 16; i++)
160 entries[i] = 0xff000000;
161 entries[i] |= (i << 20) | (i << 16) | (i << 12) | (i << 8) | (i << 4) | i;
165 static void test_predefined_palette(void)
167 static struct test_data
169 WICBitmapPaletteType type;
170 BOOL is_bw, is_gray;
171 UINT count;
172 WICColor color[256];
173 } td[] =
175 { WICBitmapPaletteTypeFixedBW, 1, 1, 2, { 0xff000000, 0xffffffff } },
176 { WICBitmapPaletteTypeFixedGray4, 0, 1, 4,
177 { 0xff000000, 0xff555555, 0xffaaaaaa, 0xffffffff } },
178 { WICBitmapPaletteTypeFixedGray16, 0, 1, 16, { 0 } },
180 IWICImagingFactory *factory;
181 IWICPalette *palette;
182 HRESULT hr;
183 WICBitmapPaletteType type;
184 UINT count, i, ret;
185 BOOL bret;
186 WICColor color[256];
188 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
189 &IID_IWICImagingFactory, (void **)&factory);
190 ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
192 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
194 hr = IWICImagingFactory_CreatePalette(factory, &palette);
195 ok(hr == S_OK, "%u: CreatePalette error %#x\n", i, hr);
197 hr = IWICPalette_InitializePredefined(palette, td[i].type, FALSE);
198 ok(hr == S_OK, "%u: InitializePredefined error %#x\n", i, hr);
200 bret = -1;
201 hr = IWICPalette_IsBlackWhite(palette, &bret);
202 ok(hr == S_OK, "%u: IsBlackWhite error %#x\n", i, hr);
203 ok(bret == td[i].is_bw ||
204 broken(td[i].type == WICBitmapPaletteTypeFixedBW && bret != td[i].is_bw), /* XP */
205 "%u: expected %d, got %d\n",i, td[i].is_bw, bret);
207 bret = -1;
208 hr = IWICPalette_IsGrayscale(palette, &bret);
209 ok(hr == S_OK, "%u: IsGrayscale error %#x\n", i, hr);
210 ok(bret == td[i].is_gray, "%u: expected %d, got %d\n", i, td[i].is_gray, bret);
212 type = -1;
213 hr = IWICPalette_GetType(palette, &type);
214 ok(hr == S_OK, "%u: GetType error %#x\n", i, hr);
215 ok(type == td[i].type, "%u: expected %#x, got %#x\n", i, td[i].type, type);
217 count = 0xdeadbeef;
218 hr = IWICPalette_GetColorCount(palette, &count);
219 ok(hr == S_OK, "%u: GetColorCount error %#x\n", i, hr);
220 ok(count == td[i].count, "%u: expected %u, got %u\n", i, td[i].count, count);
222 hr = IWICPalette_GetColors(palette, count, color, &ret);
223 ok(hr == S_OK, "%u: GetColors error %#x\n", i, hr);
224 ok(ret == count, "%u: expected %u, got %u\n", i, count, ret);
225 if (ret == td[i].count)
227 UINT j;
229 if (td[i].type == WICBitmapPaletteTypeFixedGray16)
230 generate_gray16_palette(td[i].color, td[i].count);
232 for (j = 0; j < count; j++)
234 ok(color[j] == td[i].color[j], "%u:[%u]: expected %#x, got %#x\n",
235 i, j, td[i].color[j], color[j]);
239 IWICPalette_Release(palette);
242 IWICImagingFactory_Release(factory);
245 START_TEST(palette)
247 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
249 test_custom_palette();
250 test_predefined_palette();
252 CoUninitialize();