[WINDOWSCODECS_WINETEST]
[reactos.git] / rostests / winetests / windowscodecs / gifformat.c
1 /*
2 * Copyright 2012 Dmitry Timoshkov
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include <stdarg.h>
20 #include <stdio.h>
21
22 #define WIN32_NO_STATUS
23 #define _INC_WINDOWS
24 #define COM_NO_WINDOWS_H
25
26 #define COBJMACROS
27
28 #include <windef.h>
29 #include <winbase.h>
30 #include <ole2.h>
31 #include <wincodec.h>
32 #include <wine/test.h>
33
34 static const char gif_global_palette[] = {
35 /* LSD */'G','I','F','8','7','a',0x01,0x00,0x01,0x00,0xa1,0x02,0x00,
36 /* palette */0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,
37 /* GCE */0x21,0xf9,0x04,0x01,0x05,0x00,0x01,0x00, /* index 1 */
38 /* IMD */0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,
39 0x02,0x02,0x44,0x01,0x00,0x3b
40 };
41
42 /* frame 0, GCE transparent index 1
43 * frame 1, GCE transparent index 2
44 */
45 static const char gif_global_palette_2frames[] = {
46 /* LSD */'G','I','F','8','9','a',0x01,0x00,0x01,0x00,0xa1,0x02,0x00,
47 /* palette */0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,
48 /* GCE */0x21,0xf9,0x04,0x01,0x05,0x00,0x01,0x00, /* index 1 */
49 /* IMD */0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,
50 0x02,0x02,0x44,0x01,0x00,
51 /* GCE */0x21,0xf9,0x04,0x01,0x05,0x00,0x02,0x00, /* index 2 */
52 /* IMD */0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,
53 0x02,0x02,0x44,0x01,0x00,0x3b
54 };
55
56 static const char gif_local_palette[] = {
57 /* LSD */'G','I','F','8','7','a',0x01,0x00,0x01,0x00,0x27,0x02,0x00,
58 /* GCE */0x21,0xf9,0x04,0x01,0x05,0x00,0x01,0x00, /* index 1 */
59 /* IMD */0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x81,
60 /* palette */0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,
61 0x02,0x02,0x44,0x01,0x00,0x3b
62 };
63
64 static IWICImagingFactory *factory;
65
66 static const char *debugstr_guid(const GUID *guid)
67 {
68 static char buf[50];
69
70 if (!guid) return "(null)";
71 sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
72 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
73 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
74 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
75 return buf;
76 }
77
78 static IWICBitmapDecoder *create_decoder(const void *image_data, UINT image_size)
79 {
80 HGLOBAL hmem;
81 BYTE *data;
82 HRESULT hr;
83 IWICBitmapDecoder *decoder = NULL;
84 IStream *stream;
85 GUID format;
86
87 hmem = GlobalAlloc(0, image_size);
88 data = GlobalLock(hmem);
89 memcpy(data, image_data, image_size);
90 GlobalUnlock(hmem);
91
92 hr = CreateStreamOnHGlobal(hmem, TRUE, &stream);
93 ok(hr == S_OK, "CreateStreamOnHGlobal error %#x\n", hr);
94
95 hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, &decoder);
96 ok(hr == S_OK, "CreateDecoderFromStream error %#x\n", hr);
97
98 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &format);
99 ok(hr == S_OK, "GetContainerFormat error %#x\n", hr);
100 ok(IsEqualGUID(&format, &GUID_ContainerFormatGif),
101 "wrong container format %s\n", debugstr_guid(&format));
102
103 IStream_Release(stream);
104
105 return decoder;
106 }
107
108 static void test_global_gif_palette(void)
109 {
110 HRESULT hr;
111 IWICBitmapDecoder *decoder;
112 IWICBitmapFrameDecode *frame;
113 IWICPalette *palette;
114 GUID format;
115 UINT count, ret;
116 WICColor color[256];
117
118 decoder = create_decoder(gif_global_palette, sizeof(gif_global_palette));
119 ok(decoder != 0, "Failed to load GIF image data\n");
120
121 hr = IWICImagingFactory_CreatePalette(factory, &palette);
122 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
123
124 /* global palette */
125 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
126 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
127
128 hr = IWICPalette_GetColorCount(palette, &count);
129 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
130 ok(count == 4, "expected 4, got %u\n", count);
131
132 hr = IWICPalette_GetColors(palette, count, color, &ret);
133 ok(hr == S_OK, "GetColors error %#x\n", hr);
134 ok(ret == count, "expected %u, got %u\n", count, ret);
135 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
136 ok(color[1] == 0x00040506, "expected 0x00040506, got %#x\n", color[1]);
137 ok(color[2] == 0xff070809, "expected 0xff070809, got %#x\n", color[2]);
138 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
139
140 /* frame palette */
141 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
142 ok(hr == S_OK, "GetFrame error %#x\n", hr);
143
144 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
145 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
146 ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
147 "wrong pixel format %s\n", debugstr_guid(&format));
148
149 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
150 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
151
152 hr = IWICPalette_GetColorCount(palette, &count);
153 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
154 ok(count == 4, "expected 4, got %u\n", count);
155
156 hr = IWICPalette_GetColors(palette, count, color, &ret);
157 ok(hr == S_OK, "GetColors error %#x\n", hr);
158 ok(ret == count, "expected %u, got %u\n", count, ret);
159 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
160 ok(color[1] == 0x00040506, "expected 0x00040506, got %#x\n", color[1]);
161 ok(color[2] == 0xff070809, "expected 0xff070809, got %#x\n", color[2]);
162 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
163
164 IWICPalette_Release(palette);
165 IWICBitmapFrameDecode_Release(frame);
166 IWICBitmapDecoder_Release(decoder);
167 }
168
169 static void test_global_gif_palette_2frames(void)
170 {
171 HRESULT hr;
172 IWICBitmapDecoder *decoder;
173 IWICBitmapFrameDecode *frame;
174 IWICPalette *palette;
175 GUID format;
176 UINT count, ret;
177 WICColor color[256];
178
179 decoder = create_decoder(gif_global_palette_2frames, sizeof(gif_global_palette_2frames));
180 ok(decoder != 0, "Failed to load GIF image data\n");
181
182 /* active frame 0, GCE transparent index 1 */
183 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
184 ok(hr == S_OK, "GetFrame error %#x\n", hr);
185
186 hr = IWICImagingFactory_CreatePalette(factory, &palette);
187 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
188
189 /* global palette */
190 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
191 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
192
193 hr = IWICPalette_GetColorCount(palette, &count);
194 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
195 ok(count == 4, "expected 4, got %u\n", count);
196
197 hr = IWICPalette_GetColors(palette, count, color, &ret);
198 ok(hr == S_OK, "GetColors error %#x\n", hr);
199 ok(ret == count, "expected %u, got %u\n", count, ret);
200 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
201 ok(color[1] == 0x00040506, "expected 0x00040506, got %#x\n", color[1]);
202 ok(color[2] == 0xff070809, "expected 0xff070809, got %#x\n", color[2]);
203 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
204
205 /* frame 0 palette */
206 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
207 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
208 ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
209 "wrong pixel format %s\n", debugstr_guid(&format));
210
211 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
212 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
213
214 hr = IWICPalette_GetColorCount(palette, &count);
215 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
216 ok(count == 4, "expected 4, got %u\n", count);
217
218 hr = IWICPalette_GetColors(palette, count, color, &ret);
219 ok(hr == S_OK, "GetColors error %#x\n", hr);
220 ok(ret == count, "expected %u, got %u\n", count, ret);
221 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
222 ok(color[1] == 0x00040506, "expected 0x00040506, got %#x\n", color[1]);
223 ok(color[2] == 0xff070809, "expected 0xff070809, got %#x\n", color[2]);
224 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
225
226 IWICBitmapFrameDecode_Release(frame);
227
228 /* active frame 1, GCE transparent index 2 */
229 hr = IWICBitmapDecoder_GetFrame(decoder, 1, &frame);
230 ok(hr == S_OK, "GetFrame error %#x\n", hr);
231
232 /* global palette */
233 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
234 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
235
236 hr = IWICPalette_GetColorCount(palette, &count);
237 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
238 ok(count == 4, "expected 4, got %u\n", count);
239
240 hr = IWICPalette_GetColors(palette, count, color, &ret);
241 ok(hr == S_OK, "GetColors error %#x\n", hr);
242 ok(ret == count, "expected %u, got %u\n", count, ret);
243 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
244 ok(color[1] == 0xff040506 || broken(color[1] == 0x00040506) /* XP */, "expected 0xff040506, got %#x\n", color[1]);
245 ok(color[2] == 0x00070809 || broken(color[2] == 0xff070809) /* XP */, "expected 0x00070809, got %#x\n", color[2]);
246 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
247
248 /* frame 1 palette */
249 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
250 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
251 ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
252 "wrong pixel format %s\n", debugstr_guid(&format));
253
254 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
255 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
256
257 hr = IWICPalette_GetColorCount(palette, &count);
258 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
259 ok(count == 4, "expected 4, got %u\n", count);
260
261 hr = IWICPalette_GetColors(palette, count, color, &ret);
262 ok(hr == S_OK, "GetColors error %#x\n", hr);
263 ok(ret == count, "expected %u, got %u\n", count, ret);
264 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
265 ok(color[1] == 0xff040506, "expected 0xff040506, got %#x\n", color[1]);
266 ok(color[2] == 0x00070809, "expected 0x00070809, got %#x\n", color[2]);
267 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
268
269 IWICPalette_Release(palette);
270 IWICBitmapFrameDecode_Release(frame);
271 IWICBitmapDecoder_Release(decoder);
272 }
273
274 static void test_local_gif_palette(void)
275 {
276 HRESULT hr;
277 IWICBitmapDecoder *decoder;
278 IWICBitmapFrameDecode *frame;
279 IWICPalette *palette;
280 WICBitmapPaletteType type;
281 GUID format;
282 UINT count, ret, i;
283 WICColor color[256];
284
285 decoder = create_decoder(gif_local_palette, sizeof(gif_local_palette));
286 ok(decoder != 0, "Failed to load GIF image data\n");
287
288 hr = IWICImagingFactory_CreatePalette(factory, &palette);
289 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
290
291 /* global palette */
292 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
293 ok(hr == S_OK || broken(hr == WINCODEC_ERR_FRAMEMISSING), "CopyPalette %#x\n", hr);
294 if (hr == S_OK)
295 {
296 type = -1;
297 hr = IWICPalette_GetType(palette, &type);
298 ok(hr == S_OK, "GetType error %#x\n", hr);
299 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %#x\n", type);
300
301 hr = IWICPalette_GetColorCount(palette, &count);
302 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
303 ok(count == 256, "expected 256, got %u\n", count);
304
305 hr = IWICPalette_GetColors(palette, count, color, &ret);
306 ok(hr == S_OK, "GetColors error %#x\n", hr);
307 ok(ret == count, "expected %u, got %u\n", count, ret);
308 ok(color[0] == 0xff000000, "expected 0xff000000, got %#x\n", color[0]);
309 ok(color[1] == 0x00ffffff, "expected 0x00ffffff, got %#x\n", color[1]);
310
311 for (i = 2; i < 256; i++)
312 ok(color[i] == 0xff000000, "expected 0xff000000, got %#x\n", color[i]);
313 }
314
315 /* frame palette */
316 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
317 ok(hr == S_OK, "GetFrame error %#x\n", hr);
318
319 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
320 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
321 ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
322 "wrong pixel format %s\n", debugstr_guid(&format));
323
324 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
325 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
326
327 hr = IWICPalette_GetColorCount(palette, &count);
328 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
329 ok(count == 4, "expected 4, got %u\n", count);
330
331 type = -1;
332 hr = IWICPalette_GetType(palette, &type);
333 ok(hr == S_OK, "GetType error %#x\n", hr);
334 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %#x\n", type);
335
336 hr = IWICPalette_GetColors(palette, count, color, &ret);
337 ok(hr == S_OK, "GetColors error %#x\n", hr);
338 ok(ret == count, "expected %u, got %u\n", count, ret);
339 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
340 ok(color[1] == 0x00040506, "expected 0x00040506, got %#x\n", color[1]);
341 ok(color[2] == 0xff070809, "expected 0xff070809, got %#x\n", color[2]);
342 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
343
344 IWICPalette_Release(palette);
345 IWICBitmapFrameDecode_Release(frame);
346 IWICBitmapDecoder_Release(decoder);
347 }
348
349 START_TEST(gifformat)
350 {
351 HRESULT hr;
352
353 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
354 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
355 &IID_IWICImagingFactory, (void **)&factory);
356 ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
357 if (FAILED(hr)) return;
358
359 test_global_gif_palette();
360 test_global_gif_palette_2frames();
361 test_local_gif_palette();
362
363 IWICImagingFactory_Release(factory);
364 CoUninitialize();
365 }