Merge the following revisions from kernel-fun branch:
[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 IWICBitmapDecoder *create_decoder(const void *image_data, UINT image_size)
67 {
68 HGLOBAL hmem;
69 BYTE *data;
70 HRESULT hr;
71 IWICBitmapDecoder *decoder = NULL;
72 IStream *stream;
73 GUID format;
74
75 hmem = GlobalAlloc(0, image_size);
76 data = GlobalLock(hmem);
77 memcpy(data, image_data, image_size);
78 GlobalUnlock(hmem);
79
80 hr = CreateStreamOnHGlobal(hmem, TRUE, &stream);
81 ok(hr == S_OK, "CreateStreamOnHGlobal error %#x\n", hr);
82
83 hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, &decoder);
84 ok(hr == S_OK, "CreateDecoderFromStream error %#x\n", hr);
85
86 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &format);
87 ok(hr == S_OK, "GetContainerFormat error %#x\n", hr);
88 ok(IsEqualGUID(&format, &GUID_ContainerFormatGif),
89 "wrong container format %s\n", wine_dbgstr_guid(&format));
90
91 IStream_Release(stream);
92
93 return decoder;
94 }
95
96 static void test_global_gif_palette(void)
97 {
98 HRESULT hr;
99 IWICBitmapDecoder *decoder;
100 IWICBitmapFrameDecode *frame;
101 IWICPalette *palette;
102 GUID format;
103 UINT count, ret;
104 WICColor color[256];
105
106 decoder = create_decoder(gif_global_palette, sizeof(gif_global_palette));
107 ok(decoder != 0, "Failed to load GIF image data\n");
108
109 hr = IWICImagingFactory_CreatePalette(factory, &palette);
110 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
111
112 /* global palette */
113 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
114 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
115
116 hr = IWICPalette_GetColorCount(palette, &count);
117 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
118 ok(count == 4, "expected 4, got %u\n", count);
119
120 hr = IWICPalette_GetColors(palette, count, color, &ret);
121 ok(hr == S_OK, "GetColors error %#x\n", hr);
122 ok(ret == count, "expected %u, got %u\n", count, ret);
123 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
124 ok(color[1] == 0x00040506, "expected 0x00040506, got %#x\n", color[1]);
125 ok(color[2] == 0xff070809, "expected 0xff070809, got %#x\n", color[2]);
126 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
127
128 /* frame palette */
129 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
130 ok(hr == S_OK, "GetFrame error %#x\n", hr);
131
132 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
133 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
134 ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
135 "wrong pixel format %s\n", wine_dbgstr_guid(&format));
136
137 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
138 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
139
140 hr = IWICPalette_GetColorCount(palette, &count);
141 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
142 ok(count == 4, "expected 4, got %u\n", count);
143
144 hr = IWICPalette_GetColors(palette, count, color, &ret);
145 ok(hr == S_OK, "GetColors error %#x\n", hr);
146 ok(ret == count, "expected %u, got %u\n", count, ret);
147 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
148 ok(color[1] == 0x00040506, "expected 0x00040506, got %#x\n", color[1]);
149 ok(color[2] == 0xff070809, "expected 0xff070809, got %#x\n", color[2]);
150 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
151
152 IWICPalette_Release(palette);
153 IWICBitmapFrameDecode_Release(frame);
154 IWICBitmapDecoder_Release(decoder);
155 }
156
157 static void test_global_gif_palette_2frames(void)
158 {
159 HRESULT hr;
160 IWICBitmapDecoder *decoder;
161 IWICBitmapFrameDecode *frame;
162 IWICPalette *palette;
163 GUID format;
164 UINT count, ret;
165 WICColor color[256];
166
167 decoder = create_decoder(gif_global_palette_2frames, sizeof(gif_global_palette_2frames));
168 ok(decoder != 0, "Failed to load GIF image data\n");
169
170 /* active frame 0, GCE transparent index 1 */
171 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
172 ok(hr == S_OK, "GetFrame error %#x\n", hr);
173
174 hr = IWICImagingFactory_CreatePalette(factory, &palette);
175 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
176
177 /* global palette */
178 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
179 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
180
181 hr = IWICPalette_GetColorCount(palette, &count);
182 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
183 ok(count == 4, "expected 4, got %u\n", count);
184
185 hr = IWICPalette_GetColors(palette, count, color, &ret);
186 ok(hr == S_OK, "GetColors error %#x\n", hr);
187 ok(ret == count, "expected %u, got %u\n", count, ret);
188 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
189 ok(color[1] == 0x00040506, "expected 0x00040506, got %#x\n", color[1]);
190 ok(color[2] == 0xff070809, "expected 0xff070809, got %#x\n", color[2]);
191 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
192
193 /* frame 0 palette */
194 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
195 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
196 ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
197 "wrong pixel format %s\n", wine_dbgstr_guid(&format));
198
199 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
200 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
201
202 hr = IWICPalette_GetColorCount(palette, &count);
203 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
204 ok(count == 4, "expected 4, got %u\n", count);
205
206 hr = IWICPalette_GetColors(palette, count, color, &ret);
207 ok(hr == S_OK, "GetColors error %#x\n", hr);
208 ok(ret == count, "expected %u, got %u\n", count, ret);
209 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
210 ok(color[1] == 0x00040506, "expected 0x00040506, got %#x\n", color[1]);
211 ok(color[2] == 0xff070809, "expected 0xff070809, got %#x\n", color[2]);
212 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
213
214 IWICBitmapFrameDecode_Release(frame);
215
216 /* active frame 1, GCE transparent index 2 */
217 hr = IWICBitmapDecoder_GetFrame(decoder, 1, &frame);
218 ok(hr == S_OK, "GetFrame error %#x\n", hr);
219
220 /* global palette */
221 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
222 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
223
224 hr = IWICPalette_GetColorCount(palette, &count);
225 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
226 ok(count == 4, "expected 4, got %u\n", count);
227
228 hr = IWICPalette_GetColors(palette, count, color, &ret);
229 ok(hr == S_OK, "GetColors error %#x\n", hr);
230 ok(ret == count, "expected %u, got %u\n", count, ret);
231 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
232 ok(color[1] == 0xff040506 || broken(color[1] == 0x00040506) /* XP */, "expected 0xff040506, got %#x\n", color[1]);
233 ok(color[2] == 0x00070809 || broken(color[2] == 0xff070809) /* XP */, "expected 0x00070809, got %#x\n", color[2]);
234 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
235
236 /* frame 1 palette */
237 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
238 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
239 ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
240 "wrong pixel format %s\n", wine_dbgstr_guid(&format));
241
242 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
243 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
244
245 hr = IWICPalette_GetColorCount(palette, &count);
246 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
247 ok(count == 4, "expected 4, got %u\n", count);
248
249 hr = IWICPalette_GetColors(palette, count, color, &ret);
250 ok(hr == S_OK, "GetColors error %#x\n", hr);
251 ok(ret == count, "expected %u, got %u\n", count, ret);
252 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
253 ok(color[1] == 0xff040506, "expected 0xff040506, got %#x\n", color[1]);
254 ok(color[2] == 0x00070809, "expected 0x00070809, got %#x\n", color[2]);
255 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
256
257 IWICPalette_Release(palette);
258 IWICBitmapFrameDecode_Release(frame);
259 IWICBitmapDecoder_Release(decoder);
260 }
261
262 static void test_local_gif_palette(void)
263 {
264 HRESULT hr;
265 IWICBitmapDecoder *decoder;
266 IWICBitmapFrameDecode *frame;
267 IWICPalette *palette;
268 WICBitmapPaletteType type;
269 GUID format;
270 UINT count, ret, i;
271 WICColor color[256];
272
273 decoder = create_decoder(gif_local_palette, sizeof(gif_local_palette));
274 ok(decoder != 0, "Failed to load GIF image data\n");
275
276 hr = IWICImagingFactory_CreatePalette(factory, &palette);
277 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
278
279 /* global palette */
280 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
281 ok(hr == S_OK || broken(hr == WINCODEC_ERR_FRAMEMISSING), "CopyPalette %#x\n", hr);
282 if (hr == S_OK)
283 {
284 type = -1;
285 hr = IWICPalette_GetType(palette, &type);
286 ok(hr == S_OK, "GetType error %#x\n", hr);
287 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %#x\n", type);
288
289 hr = IWICPalette_GetColorCount(palette, &count);
290 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
291 ok(count == 256, "expected 256, got %u\n", count);
292
293 hr = IWICPalette_GetColors(palette, count, color, &ret);
294 ok(hr == S_OK, "GetColors error %#x\n", hr);
295 ok(ret == count, "expected %u, got %u\n", count, ret);
296 ok(color[0] == 0xff000000, "expected 0xff000000, got %#x\n", color[0]);
297 ok(color[1] == 0x00ffffff, "expected 0x00ffffff, got %#x\n", color[1]);
298
299 for (i = 2; i < 256; i++)
300 ok(color[i] == 0xff000000, "expected 0xff000000, got %#x\n", color[i]);
301 }
302
303 /* frame palette */
304 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
305 ok(hr == S_OK, "GetFrame error %#x\n", hr);
306
307 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
308 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
309 ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
310 "wrong pixel format %s\n", wine_dbgstr_guid(&format));
311
312 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
313 ok(hr == S_OK, "CopyPalette error %#x\n", hr);
314
315 hr = IWICPalette_GetColorCount(palette, &count);
316 ok(hr == S_OK, "GetColorCount error %#x\n", hr);
317 ok(count == 4, "expected 4, got %u\n", count);
318
319 type = -1;
320 hr = IWICPalette_GetType(palette, &type);
321 ok(hr == S_OK, "GetType error %#x\n", hr);
322 ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %#x\n", type);
323
324 hr = IWICPalette_GetColors(palette, count, color, &ret);
325 ok(hr == S_OK, "GetColors error %#x\n", hr);
326 ok(ret == count, "expected %u, got %u\n", count, ret);
327 ok(color[0] == 0xff010203, "expected 0xff010203, got %#x\n", color[0]);
328 ok(color[1] == 0x00040506, "expected 0x00040506, got %#x\n", color[1]);
329 ok(color[2] == 0xff070809, "expected 0xff070809, got %#x\n", color[2]);
330 ok(color[3] == 0xff0a0b0c, "expected 0xff0a0b0c, got %#x\n", color[3]);
331
332 IWICPalette_Release(palette);
333 IWICBitmapFrameDecode_Release(frame);
334 IWICBitmapDecoder_Release(decoder);
335 }
336
337 START_TEST(gifformat)
338 {
339 HRESULT hr;
340
341 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
342 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
343 &IID_IWICImagingFactory, (void **)&factory);
344 ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
345 if (FAILED(hr)) return;
346
347 test_global_gif_palette();
348 test_global_gif_palette_2frames();
349 test_local_gif_palette();
350
351 IWICImagingFactory_Release(factory);
352 CoUninitialize();
353 }