windowscodecs: Test GIF metadata using a specially created GIF image with a bunch...
[wine] / dlls / windowscodecs / tests / metadata.c
1 /*
2  * Copyright 2011 Vincent Povirk for CodeWeavers
3  * Copyright 2012 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 <stdio.h>
21 #include <stdarg.h>
22 #include <math.h>
23 #include <assert.h>
24
25 #define COBJMACROS
26
27 #include "windef.h"
28 #include "objbase.h"
29 #include "wincodec.h"
30 #include "wincodecsdk.h"
31 #include "wine/test.h"
32
33 #define expect_blob(propvar, data, length) do { \
34     ok((propvar).vt == VT_BLOB, "unexpected vt: %i\n", (propvar).vt); \
35     if ((propvar).vt == VT_BLOB) { \
36         ok(U(propvar).blob.cbSize == (length), "expected size %u, got %u\n", (ULONG)(length), U(propvar).blob.cbSize); \
37         if (U(propvar).blob.cbSize == (length)) { \
38             ok(!memcmp(U(propvar).blob.pBlobData, (data), (length)), "unexpected data\n"); \
39         } \
40     } \
41 } while (0)
42
43 #define IFD_BYTE 1
44 #define IFD_ASCII 2
45 #define IFD_SHORT 3
46 #define IFD_LONG 4
47 #define IFD_RATIONAL 5
48 #define IFD_SBYTE 6
49 #define IFD_UNDEFINED 7
50 #define IFD_SSHORT 8
51 #define IFD_SLONG 9
52 #define IFD_SRATIONAL 10
53 #define IFD_FLOAT 11
54 #define IFD_DOUBLE 12
55 #define IFD_IFD 13
56
57 #include "pshpack2.h"
58 struct IFD_entry
59 {
60     SHORT id;
61     SHORT type;
62     ULONG count;
63     LONG  value;
64 };
65
66 struct IFD_rational
67 {
68     LONG numerator;
69     LONG denominator;
70 };
71
72 static const struct ifd_data
73 {
74     USHORT number_of_entries;
75     struct IFD_entry entry[40];
76     ULONG next_IFD;
77     struct IFD_rational xres;
78     DOUBLE double_val;
79     struct IFD_rational srational_val;
80     char string[14];
81     SHORT short_val[4];
82     LONG long_val[2];
83     FLOAT float_val[2];
84     struct IFD_rational rational[3];
85 } IFD_data =
86 {
87     28,
88     {
89         { 0xfe,  IFD_SHORT, 1, 1 }, /* NEWSUBFILETYPE */
90         { 0x100, IFD_LONG, 1, 222 }, /* IMAGEWIDTH */
91         { 0x101, IFD_LONG, 1, 333 }, /* IMAGELENGTH */
92         { 0x102, IFD_SHORT, 1, 24 }, /* BITSPERSAMPLE */
93         { 0x103, IFD_LONG, 1, 32773 }, /* COMPRESSION: packbits */
94         { 0x11a, IFD_RATIONAL, 1, FIELD_OFFSET(struct ifd_data, xres) },
95         { 0xf001, IFD_BYTE, 1, 0x11223344 },
96         { 0xf002, IFD_BYTE, 4, 0x11223344 },
97         { 0xf003, IFD_SBYTE, 1, 0x11223344 },
98         { 0xf004, IFD_SSHORT, 1, 0x11223344 },
99         { 0xf005, IFD_SSHORT, 2, 0x11223344 },
100         { 0xf006, IFD_SLONG, 1, 0x11223344 },
101         { 0xf007, IFD_FLOAT, 1, 0x11223344 },
102         { 0xf008, IFD_DOUBLE, 1, FIELD_OFFSET(struct ifd_data, double_val) },
103         { 0xf009, IFD_SRATIONAL, 1, FIELD_OFFSET(struct ifd_data, srational_val) },
104         { 0xf00a, IFD_BYTE, 13, FIELD_OFFSET(struct ifd_data, string) },
105         { 0xf00b, IFD_SSHORT, 4, FIELD_OFFSET(struct ifd_data, short_val) },
106         { 0xf00c, IFD_SLONG, 2, FIELD_OFFSET(struct ifd_data, long_val) },
107         { 0xf00d, IFD_FLOAT, 2, FIELD_OFFSET(struct ifd_data, float_val) },
108         { 0xf00e, IFD_ASCII, 13, FIELD_OFFSET(struct ifd_data, string) },
109         { 0xf00f, IFD_ASCII, 4, 'a' | 'b' << 8 | 'c' << 16 | 'd' << 24 },
110         { 0xf010, IFD_UNDEFINED, 13, FIELD_OFFSET(struct ifd_data, string) },
111         { 0xf011, IFD_UNDEFINED, 4, 'a' | 'b' << 8 | 'c' << 16 | 'd' << 24 },
112         { 0xf012, IFD_BYTE, 0, 0x11223344 },
113         { 0xf013, IFD_SHORT, 0, 0x11223344 },
114         { 0xf014, IFD_LONG, 0, 0x11223344 },
115         { 0xf015, IFD_FLOAT, 0, 0x11223344 },
116         { 0xf016, IFD_SRATIONAL, 3, FIELD_OFFSET(struct ifd_data, rational) },
117     },
118     0,
119     { 900, 3 },
120     1234567890.0987654321,
121     { 0x1a2b3c4d, 0x5a6b7c8d },
122     "Hello World!",
123     { 0x0101, 0x0202, 0x0303, 0x0404 },
124     { 0x11223344, 0x55667788 },
125     { (FLOAT)1234.5678, (FLOAT)8765.4321 },
126     { { 0x01020304, 0x05060708 }, { 0x10203040, 0x50607080 }, { 0x11223344, 0x55667788 } },
127 };
128 #include "poppack.h"
129
130 static const char metadata_unknown[] = "lalala";
131
132 static const char metadata_tEXt[] = {
133     0,0,0,14, /* chunk length */
134     't','E','X','t', /* chunk type */
135     'w','i','n','e','t','e','s','t',0, /* keyword */
136     'v','a','l','u','e', /* text */
137     0x3f,0x64,0x19,0xf3 /* chunk CRC */
138 };
139
140 static const char pngimage[285] = {
141 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
142 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
143 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
144 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
145 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
146 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
147 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
148 };
149
150 /* 1x1 pixel gif */
151 static const char gifimage[35] = {
152 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
153 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
154 0x01,0x00,0x3b
155 };
156
157 /* 1x1 pixel gif, 2 frames; first frame is white, second is black */
158 static const char animatedgif[] = {
159 'G','I','F','8','9','a',0x01,0x00,0x01,0x00,0xA1,0x00,0x00,
160 0x6F,0x6F,0x6F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
161 /*0x21,0xFF,0x0B,'N','E','T','S','C','A','P','E','2','.','0',*/
162 0x21,0xFF,0x0B,'A','N','I','M','E','X','T','S','1','.','0',
163 0x03,0x01,0x05,0x00,0x00,
164 0x21,0xFE,0x0C,'H','e','l','l','o',' ','W','o','r','l','d','!',0x00,
165 0x21,0x01,0x0D,'a','n','i','m','a','t','i','o','n','.','g','i','f',0x00,
166 0x21,0xF9,0x04,0x00,0x0A,0x00,0xFF,0x00,0x2C,
167 0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x81,
168 0xDE,0xDE,0xDE,0x00,0x00,0x00,
169 0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x4C,0x01,0x00,
170 0x21,0xFE,0x08,'i','m','a','g','e',' ','#','1',0x00,
171 0x21,0x01,0x0C,'p','l','a','i','n','t','e','x','t',' ','#','1',0x00,
172 0x21,0xF9,0x04,0x01,0x0A,0x00,0x01,0x00,0x2C,
173 0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x81,
174 0x4D,0x4D,0x4D,0x00,0x00,0x00,
175 0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x44,0x01,0x00,
176 0x21,0xFE,0x08,'i','m','a','g','e',' ','#','2',0x00,
177 0x21,0x01,0x0C,'p','l','a','i','n','t','e','x','t',' ','#','2',0x00,0x3B
178 };
179
180 static const char *debugstr_guid(REFIID riid)
181 {
182     static char buf[50];
183
184     if(!riid)
185         return "(null)";
186
187     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
188             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
189             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
190             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
191
192     return buf;
193 }
194
195 static IStream *create_stream(const char *data, int data_size)
196 {
197     HRESULT hr;
198     IStream *stream;
199     HGLOBAL hdata;
200     void *locked_data;
201
202     hdata = GlobalAlloc(GMEM_MOVEABLE, data_size);
203     ok(hdata != 0, "GlobalAlloc failed\n");
204     if (!hdata) return NULL;
205
206     locked_data = GlobalLock(hdata);
207     memcpy(locked_data, data, data_size);
208     GlobalUnlock(hdata);
209
210     hr = CreateStreamOnHGlobal(hdata, TRUE, &stream);
211     ok(hr == S_OK, "CreateStreamOnHGlobal failed, hr=%x\n", hr);
212
213     return stream;
214 }
215
216 static void load_stream(IUnknown *reader, const char *data, int data_size, DWORD persist_options)
217 {
218     HRESULT hr;
219     IWICPersistStream *persist;
220     IStream *stream;
221     LARGE_INTEGER pos;
222     ULARGE_INTEGER cur_pos;
223
224     stream = create_stream(data, data_size);
225     if (!stream)
226         return;
227
228     hr = IUnknown_QueryInterface(reader, &IID_IWICPersistStream, (void**)&persist);
229     ok(hr == S_OK, "QueryInterface failed, hr=%x\n", hr);
230
231     if (SUCCEEDED(hr))
232     {
233         hr = IWICPersistStream_LoadEx(persist, stream, NULL, persist_options);
234         ok(hr == S_OK, "LoadEx failed, hr=%x\n", hr);
235
236         IWICPersistStream_Release(persist);
237     }
238
239     pos.QuadPart = 0;
240     hr = IStream_Seek(stream, pos, SEEK_CUR, &cur_pos);
241     ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
242     /* IFD metadata reader doesn't rewind the stream to the start */
243     ok(cur_pos.QuadPart == 0 || cur_pos.QuadPart <= data_size,
244        "current stream pos is at %x/%x, data size %x\n", cur_pos.u.LowPart, cur_pos.u.HighPart, data_size);
245
246     IStream_Release(stream);
247 }
248
249 static void test_metadata_unknown(void)
250 {
251     HRESULT hr;
252     IWICMetadataReader *reader;
253     IWICEnumMetadataItem *enumerator;
254     IWICMetadataBlockReader *blockreader;
255     PROPVARIANT schema, id, value;
256     ULONG items_returned;
257
258     hr = CoCreateInstance(&CLSID_WICUnknownMetadataReader, NULL, CLSCTX_INPROC_SERVER,
259         &IID_IWICMetadataReader, (void**)&reader);
260     ok(hr == S_OK, "CoCreateInstance failed, hr=%x\n", hr);
261     if (FAILED(hr)) return;
262
263     load_stream((IUnknown*)reader, metadata_unknown, sizeof(metadata_unknown), WICPersistOptionsDefault);
264
265     hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
266     ok(hr == S_OK, "GetEnumerator failed, hr=%x\n", hr);
267
268     if (SUCCEEDED(hr))
269     {
270         PropVariantInit(&schema);
271         PropVariantInit(&id);
272         PropVariantInit(&value);
273
274         hr = IWICEnumMetadataItem_Next(enumerator, 1, &schema, &id, &value, &items_returned);
275         ok(hr == S_OK, "Next failed, hr=%x\n", hr);
276         ok(items_returned == 1, "unexpected item count %i\n", items_returned);
277
278         if (hr == S_OK && items_returned == 1)
279         {
280             ok(schema.vt == VT_EMPTY, "unexpected vt: %i\n", schema.vt);
281             ok(id.vt == VT_EMPTY, "unexpected vt: %i\n", id.vt);
282             expect_blob(value, metadata_unknown, sizeof(metadata_unknown));
283
284             PropVariantClear(&schema);
285             PropVariantClear(&id);
286             PropVariantClear(&value);
287         }
288
289         hr = IWICEnumMetadataItem_Next(enumerator, 1, &schema, &id, &value, &items_returned);
290         ok(hr == S_FALSE, "Next failed, hr=%x\n", hr);
291         ok(items_returned == 0, "unexpected item count %i\n", items_returned);
292
293         IWICEnumMetadataItem_Release(enumerator);
294     }
295
296     hr = IWICMetadataReader_QueryInterface(reader, &IID_IWICMetadataBlockReader, (void**)&blockreader);
297     ok(hr == E_NOINTERFACE, "QueryInterface failed, hr=%x\n", hr);
298
299     if (SUCCEEDED(hr))
300         IWICMetadataBlockReader_Release(blockreader);
301
302     IWICMetadataReader_Release(reader);
303 }
304
305 static void test_metadata_tEXt(void)
306 {
307     HRESULT hr;
308     IWICMetadataReader *reader;
309     IWICEnumMetadataItem *enumerator;
310     PROPVARIANT schema, id, value;
311     ULONG items_returned, count;
312     GUID format;
313
314     PropVariantInit(&schema);
315     PropVariantInit(&id);
316     PropVariantInit(&value);
317
318     hr = CoCreateInstance(&CLSID_WICPngTextMetadataReader, NULL, CLSCTX_INPROC_SERVER,
319         &IID_IWICMetadataReader, (void**)&reader);
320     ok(hr == S_OK, "CoCreateInstance failed, hr=%x\n", hr);
321     if (FAILED(hr)) return;
322
323     hr = IWICMetadataReader_GetCount(reader, NULL);
324     ok(hr == E_INVALIDARG, "GetCount failed, hr=%x\n", hr);
325
326     hr = IWICMetadataReader_GetCount(reader, &count);
327     ok(hr == S_OK, "GetCount failed, hr=%x\n", hr);
328     ok(count == 0, "unexpected count %i\n", count);
329
330     load_stream((IUnknown*)reader, metadata_tEXt, sizeof(metadata_tEXt), WICPersistOptionsDefault);
331
332     hr = IWICMetadataReader_GetCount(reader, &count);
333     ok(hr == S_OK, "GetCount failed, hr=%x\n", hr);
334     ok(count == 1, "unexpected count %i\n", count);
335
336     hr = IWICMetadataReader_GetEnumerator(reader, NULL);
337     ok(hr == E_INVALIDARG, "GetEnumerator failed, hr=%x\n", hr);
338
339     hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
340     ok(hr == S_OK, "GetEnumerator failed, hr=%x\n", hr);
341
342     if (SUCCEEDED(hr))
343     {
344         hr = IWICEnumMetadataItem_Next(enumerator, 1, &schema, &id, &value, &items_returned);
345         ok(hr == S_OK, "Next failed, hr=%x\n", hr);
346         ok(items_returned == 1, "unexpected item count %i\n", items_returned);
347
348         if (hr == S_OK && items_returned == 1)
349         {
350             ok(schema.vt == VT_EMPTY, "unexpected vt: %i\n", schema.vt);
351             ok(id.vt == VT_LPSTR, "unexpected vt: %i\n", id.vt);
352             ok(!strcmp(U(id).pszVal, "winetest"), "unexpected id: %s\n", U(id).pszVal);
353             ok(value.vt == VT_LPSTR, "unexpected vt: %i\n", value.vt);
354             ok(!strcmp(U(value).pszVal, "value"), "unexpected value: %s\n", U(value).pszVal);
355
356             PropVariantClear(&schema);
357             PropVariantClear(&id);
358             PropVariantClear(&value);
359         }
360
361         hr = IWICEnumMetadataItem_Next(enumerator, 1, &schema, &id, &value, &items_returned);
362         ok(hr == S_FALSE, "Next failed, hr=%x\n", hr);
363         ok(items_returned == 0, "unexpected item count %i\n", items_returned);
364
365         IWICEnumMetadataItem_Release(enumerator);
366     }
367
368     hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
369     ok(hr == S_OK, "GetMetadataFormat failed, hr=%x\n", hr);
370     ok(IsEqualGUID(&format, &GUID_MetadataFormatChunktEXt), "unexpected format %s\n", debugstr_guid(&format));
371
372     hr = IWICMetadataReader_GetMetadataFormat(reader, NULL);
373     ok(hr == E_INVALIDARG, "GetMetadataFormat failed, hr=%x\n", hr);
374
375     id.vt = VT_LPSTR;
376     U(id).pszVal = CoTaskMemAlloc(strlen("winetest") + 1);
377     strcpy(U(id).pszVal, "winetest");
378
379     hr = IWICMetadataReader_GetValue(reader, NULL, &id, NULL);
380     ok(hr == S_OK, "GetValue failed, hr=%x\n", hr);
381
382     hr = IWICMetadataReader_GetValue(reader, &schema, NULL, &value);
383     ok(hr == E_INVALIDARG, "GetValue failed, hr=%x\n", hr);
384
385     hr = IWICMetadataReader_GetValue(reader, &schema, &id, &value);
386     ok(hr == S_OK, "GetValue failed, hr=%x\n", hr);
387     ok(value.vt == VT_LPSTR, "unexpected vt: %i\n", id.vt);
388     ok(!strcmp(U(value).pszVal, "value"), "unexpected value: %s\n", U(value).pszVal);
389     PropVariantClear(&value);
390
391     strcpy(U(id).pszVal, "test");
392
393     hr = IWICMetadataReader_GetValue(reader, &schema, &id, &value);
394     ok(hr == WINCODEC_ERR_PROPERTYNOTFOUND, "GetValue failed, hr=%x\n", hr);
395
396     PropVariantClear(&id);
397
398     hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, NULL, NULL);
399     ok(hr == S_OK, "GetValueByIndex failed, hr=%x\n", hr);
400
401     hr = IWICMetadataReader_GetValueByIndex(reader, 0, &schema, NULL, NULL);
402     ok(hr == S_OK, "GetValueByIndex failed, hr=%x\n", hr);
403     ok(schema.vt == VT_EMPTY, "unexpected vt: %i\n", schema.vt);
404
405     hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, &id, NULL);
406     ok(hr == S_OK, "GetValueByIndex failed, hr=%x\n", hr);
407     ok(id.vt == VT_LPSTR, "unexpected vt: %i\n", id.vt);
408     ok(!strcmp(U(id).pszVal, "winetest"), "unexpected id: %s\n", U(id).pszVal);
409     PropVariantClear(&id);
410
411     hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, NULL, &value);
412     ok(hr == S_OK, "GetValueByIndex failed, hr=%x\n", hr);
413     ok(value.vt == VT_LPSTR, "unexpected vt: %i\n", value.vt);
414     ok(!strcmp(U(value).pszVal, "value"), "unexpected value: %s\n", U(value).pszVal);
415     PropVariantClear(&value);
416
417     hr = IWICMetadataReader_GetValueByIndex(reader, 1, NULL, NULL, NULL);
418     ok(hr == E_INVALIDARG, "GetValueByIndex failed, hr=%x\n", hr);
419
420     IWICMetadataReader_Release(reader);
421 }
422
423 static inline USHORT ushort_bswap(USHORT s)
424 {
425     return (s >> 8) | (s << 8);
426 }
427
428 static inline ULONG ulong_bswap(ULONG l)
429 {
430     return ((ULONG)ushort_bswap((USHORT)l) << 16) | ushort_bswap((USHORT)(l >> 16));
431 }
432
433 static inline ULONGLONG ulonglong_bswap(ULONGLONG ll)
434 {
435     return ((ULONGLONG)ulong_bswap((ULONG)ll) << 32) | ulong_bswap((ULONG)(ll >> 32));
436 }
437
438 static void byte_swap_ifd_data(char *data)
439 {
440     USHORT number_of_entries, i;
441     struct IFD_entry *entry;
442     char *data_start = data;
443
444     number_of_entries = *(USHORT *)data;
445     *(USHORT *)data = ushort_bswap(*(USHORT *)data);
446     data += sizeof(USHORT);
447
448     for (i = 0; i < number_of_entries; i++)
449     {
450         entry = (struct IFD_entry *)data;
451
452         switch (entry->type)
453         {
454         case IFD_BYTE:
455         case IFD_SBYTE:
456         case IFD_ASCII:
457         case IFD_UNDEFINED:
458             if (entry->count > 4)
459                 entry->value = ulong_bswap(entry->value);
460             break;
461
462         case IFD_SHORT:
463         case IFD_SSHORT:
464             if (entry->count > 2)
465             {
466                 ULONG j, count = entry->count;
467                 USHORT *us = (USHORT *)(data_start + entry->value);
468                 if (!count) count = 1;
469                 for (j = 0; j < count; j++)
470                     us[j] = ushort_bswap(us[j]);
471
472                 entry->value = ulong_bswap(entry->value);
473             }
474             else
475             {
476                 ULONG j, count = entry->count;
477                 USHORT *us = (USHORT *)&entry->value;
478                 if (!count) count = 1;
479                 for (j = 0; j < count; j++)
480                     us[j] = ushort_bswap(us[j]);
481             }
482             break;
483
484         case IFD_LONG:
485         case IFD_SLONG:
486         case IFD_FLOAT:
487             if (entry->count > 1)
488             {
489                 ULONG j, count = entry->count;
490                 ULONG *ul = (ULONG *)(data_start + entry->value);
491                 if (!count) count = 1;
492                 for (j = 0; j < count; j++)
493                     ul[j] = ulong_bswap(ul[j]);
494             }
495             entry->value = ulong_bswap(entry->value);
496             break;
497
498         case IFD_RATIONAL:
499         case IFD_SRATIONAL:
500             {
501                 ULONG j;
502                 ULONG *ul = (ULONG *)(data_start + entry->value);
503                 for (j = 0; j < entry->count * 2; j++)
504                     ul[j] = ulong_bswap(ul[j]);
505             }
506             entry->value = ulong_bswap(entry->value);
507             break;
508
509         case IFD_DOUBLE:
510             {
511                 ULONG j;
512                 ULONGLONG *ull = (ULONGLONG *)(data_start + entry->value);
513                 for (j = 0; j < entry->count; j++)
514                     ull[j] = ulonglong_bswap(ull[j]);
515             }
516             entry->value = ulong_bswap(entry->value);
517             break;
518
519         default:
520             assert(0);
521             break;
522         }
523
524         entry->id = ushort_bswap(entry->id);
525         entry->type = ushort_bswap(entry->type);
526         entry->count = ulong_bswap(entry->count);
527         data += sizeof(*entry);
528     }
529 }
530
531 struct test_data
532 {
533     ULONG type, id;
534     int count; /* if VT_VECTOR */
535     LONGLONG value[13];
536     const char *string;
537     const WCHAR id_string[32];
538 };
539
540 static void compare_metadata(IWICMetadataReader *reader, const struct test_data *td, ULONG count)
541 {
542     HRESULT hr;
543     IWICEnumMetadataItem *enumerator;
544     PROPVARIANT schema, id, value;
545     ULONG items_returned, i;
546
547     hr = IWICMetadataReader_GetEnumerator(reader, NULL);
548     ok(hr == E_INVALIDARG, "GetEnumerator error %#x\n", hr);
549
550     hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
551     ok(hr == S_OK, "GetEnumerator error %#x\n", hr);
552
553     PropVariantInit(&schema);
554     PropVariantInit(&id);
555     PropVariantInit(&value);
556
557     for (i = 0; i < count; i++)
558     {
559         hr = IWICEnumMetadataItem_Next(enumerator, 1, &schema, &id, &value, &items_returned);
560         ok(hr == S_OK, "Next error %#x\n", hr);
561         ok(items_returned == 1, "unexpected item count %u\n", items_returned);
562
563         ok(schema.vt == VT_EMPTY, "%u: unexpected vt: %u\n", i, schema.vt);
564         ok(id.vt == VT_UI2 || id.vt == VT_LPWSTR || id.vt == VT_EMPTY, "%u: unexpected vt: %u\n", i, id.vt);
565         if (id.vt == VT_UI2)
566             ok(U(id).uiVal == td[i].id, "%u: expected id %#x, got %#x\n", i, td[i].id, U(id).uiVal);
567         else if (id.vt == VT_LPWSTR)
568             ok(!lstrcmpW(td[i].id_string, U(id).pwszVal),
569                "%u: expected %s, got %s\n", i, wine_dbgstr_w(td[i].id_string), wine_dbgstr_w(U(id).pwszVal));
570
571         ok(value.vt == td[i].type, "%u: expected vt %#x, got %#x\n", i, td[i].type, value.vt);
572         if (value.vt & VT_VECTOR)
573         {
574             ULONG j;
575             switch (value.vt & ~VT_VECTOR)
576             {
577             case VT_I1:
578             case VT_UI1:
579                 ok(td[i].count == U(value).caub.cElems, "%u: expected cElems %d, got %d\n", i, td[i].count, U(value).caub.cElems);
580                 for (j = 0; j < U(value).caub.cElems; j++)
581                     ok(td[i].value[j] == U(value).caub.pElems[j], "%u: expected value[%d] %#x/%#x, got %#x\n", i, j, (ULONG)td[i].value[j], (ULONG)(td[i].value[j] >> 32), U(value).caub.pElems[j]);
582                 break;
583             case VT_I2:
584             case VT_UI2:
585                 ok(td[i].count == U(value).caui.cElems, "%u: expected cElems %d, got %d\n", i, td[i].count, U(value).caui.cElems);
586                 for (j = 0; j < U(value).caui.cElems; j++)
587                     ok(td[i].value[j] == U(value).caui.pElems[j], "%u: expected value[%d] %#x/%#x, got %#x\n", i, j, (ULONG)td[i].value[j], (ULONG)(td[i].value[j] >> 32), U(value).caui.pElems[j]);
588                 break;
589             case VT_I4:
590             case VT_UI4:
591             case VT_R4:
592                 ok(td[i].count == U(value).caul.cElems, "%u: expected cElems %d, got %d\n", i, td[i].count, U(value).caul.cElems);
593                 for (j = 0; j < U(value).caul.cElems; j++)
594                     ok(td[i].value[j] == U(value).caul.pElems[j], "%u: expected value[%d] %#x/%#x, got %#x\n", i, j, (ULONG)td[i].value[j], (ULONG)(td[i].value[j] >> 32), U(value).caul.pElems[j]);
595                 break;
596             case VT_I8:
597             case VT_UI8:
598             case VT_R8:
599                 ok(td[i].count == U(value).cauh.cElems, "%u: expected cElems %d, got %d\n", i, td[i].count, U(value).cauh.cElems);
600                 for (j = 0; j < U(value).cauh.cElems; j++)
601                     ok(td[i].value[j] == U(value).cauh.pElems[j].QuadPart, "%u: expected value[%d] %08x/%08x, got %08x/%08x\n", i, j, (ULONG)td[i].value[j], (ULONG)(td[i].value[j] >> 32), U(value).cauh.pElems[j].u.LowPart, U(value).cauh.pElems[j].u.HighPart);
602                 break;
603             case VT_LPSTR:
604                 ok(td[i].count == U(value).calpstr.cElems, "%u: expected cElems %d, got %d\n", i, td[i].count, U(value).caub.cElems);
605                 for (j = 0; j < U(value).calpstr.cElems; j++)
606                     trace("%u: %s\n", j, U(value).calpstr.pElems[j]);
607                 /* fall through to not handled message */
608             default:
609                 ok(0, "%u: array of type %d is not handled\n", i, value.vt & ~VT_VECTOR);
610                 break;
611             }
612         }
613         else if (value.vt == VT_LPSTR)
614         {
615             ok(td[i].count == strlen(U(value).pszVal) ||
616                broken(td[i].count == strlen(U(value).pszVal) + 1), /* before Win7 */
617                "%u: expected count %d, got %d\n", i, td[i].count, lstrlenA(U(value).pszVal));
618             if (td[i].count == strlen(U(value).pszVal))
619                 ok(!strcmp(td[i].string, U(value).pszVal),
620                    "%u: expected %s, got %s\n", i, td[i].string, U(value).pszVal);
621         }
622         else if (value.vt == VT_BLOB)
623         {
624             ok(td[i].count == U(value).blob.cbSize, "%u: expected count %d, got %d\n", i, td[i].count, U(value).blob.cbSize);
625             ok(!memcmp(td[i].string, U(value).blob.pBlobData, td[i].count), "%u: expected %s, got %s\n", i, td[i].string, U(value).blob.pBlobData);
626         }
627         else
628             ok(U(value).uhVal.QuadPart == td[i].value[0], "%u: expected value %#x/%#x got %#x/%#x\n",
629                i, (UINT)td[i].value[0], (UINT)(td[i].value[0] >> 32), U(value).uhVal.u.LowPart, U(value).uhVal.u.HighPart);
630
631         PropVariantClear(&schema);
632         PropVariantClear(&id);
633         PropVariantClear(&value);
634     }
635
636     hr = IWICEnumMetadataItem_Next(enumerator, 1, &schema, &id, &value, &items_returned);
637     ok(hr == S_FALSE, "Next should fail\n");
638     ok(items_returned == 0, "unexpected item count %u\n", items_returned);
639
640     IWICEnumMetadataItem_Release(enumerator);
641 }
642
643 static void test_metadata_IFD(void)
644 {
645     static const struct test_data td[28] =
646     {
647         { VT_UI2, 0xfe, 0, { 1 } },
648         { VT_UI4, 0x100, 0, { 222 } },
649         { VT_UI4, 0x101, 0, { 333 } },
650         { VT_UI2, 0x102, 0, { 24 } },
651         { VT_UI4, 0x103, 0, { 32773 } },
652         { VT_UI8, 0x11a, 0, { ((LONGLONG)3 << 32) | 900 } },
653         { VT_UI1, 0xf001, 0, { 0x44 } },
654         { VT_UI1|VT_VECTOR, 0xf002, 4, { 0x44, 0x33, 0x22, 0x11 } },
655         { VT_I1, 0xf003, 0, { 0x44 } },
656         { VT_I2, 0xf004, 0, { 0x3344 } },
657         { VT_I2|VT_VECTOR, 0xf005, 2, { 0x3344, 0x1122 } },
658         { VT_I4, 0xf006, 0, { 0x11223344 } },
659         { VT_R4, 0xf007, 0, { 0x11223344 } },
660         { VT_R8, 0xf008, 0, { ((LONGLONG)0x41d26580 << 32) | 0xb486522c } },
661         { VT_I8, 0xf009, 0, { ((LONGLONG)0x5a6b7c8d << 32) | 0x1a2b3c4d } },
662         { VT_UI1|VT_VECTOR, 0xf00a, 13, { 'H','e','l','l','o',' ','W','o','r','l','d','!',0 } },
663         { VT_I2|VT_VECTOR, 0xf00b, 4, { 0x0101, 0x0202, 0x0303, 0x0404 } },
664         { VT_I4|VT_VECTOR, 0xf00c, 2, { 0x11223344, 0x55667788 } },
665         { VT_R4|VT_VECTOR, 0xf00d, 2, { 0x449a522b, 0x4608f5ba } },
666         { VT_LPSTR, 0xf00e, 12, { 0 }, "Hello World!" },
667         { VT_LPSTR, 0xf00f, 4, { 0 }, "abcd" },
668         { VT_BLOB, 0xf010, 13, { 0 }, "Hello World!" },
669         { VT_BLOB, 0xf011, 4, { 0 }, "abcd" },
670         { VT_UI1, 0xf012, 0, { 0x44 } },
671         { VT_UI2, 0xf013, 0, { 0x3344 } },
672         { VT_UI4, 0xf014, 0, { 0x11223344 } },
673         { VT_R4, 0xf015, 0, { 0x11223344 } },
674         { VT_I8|VT_VECTOR, 0xf016, 3,
675           { ((LONGLONG)0x05060708 << 32) | 0x01020304,
676             ((LONGLONG)0x50607080 << 32) | 0x10203040,
677             ((LONGLONG)0x55667788 << 32) | 0x11223344 } },
678     };
679     HRESULT hr;
680     IWICMetadataReader *reader;
681     IWICMetadataBlockReader *blockreader;
682     PROPVARIANT schema, id, value;
683     ULONG count;
684     GUID format;
685     char *IFD_data_swapped;
686 #ifdef WORDS_BIGENDIAN
687     DWORD persist_options = WICPersistOptionsBigEndian;
688 #else
689     DWORD persist_options = WICPersistOptionsLittleEndian;
690 #endif
691
692     hr = CoCreateInstance(&CLSID_WICIfdMetadataReader, NULL, CLSCTX_INPROC_SERVER,
693         &IID_IWICMetadataReader, (void**)&reader);
694     ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
695
696     hr = IWICMetadataReader_GetCount(reader, NULL);
697     ok(hr == E_INVALIDARG, "GetCount error %#x\n", hr);
698
699     hr = IWICMetadataReader_GetCount(reader, &count);
700     ok(hr == S_OK, "GetCount error %#x\n", hr);
701     ok(count == 0, "unexpected count %u\n", count);
702
703     load_stream((IUnknown*)reader, (const char *)&IFD_data, sizeof(IFD_data), persist_options);
704
705     hr = IWICMetadataReader_GetCount(reader, &count);
706     ok(hr == S_OK, "GetCount error %#x\n", hr);
707     ok(count == sizeof(td)/sizeof(td[0]), "unexpected count %u\n", count);
708
709     compare_metadata(reader, td, count);
710
711     /* test IFD data with different endianness */
712     if (persist_options == WICPersistOptionsLittleEndian)
713         persist_options = WICPersistOptionsBigEndian;
714     else
715         persist_options = WICPersistOptionsLittleEndian;
716
717     IFD_data_swapped = HeapAlloc(GetProcessHeap(), 0, sizeof(IFD_data));
718     memcpy(IFD_data_swapped, &IFD_data, sizeof(IFD_data));
719     byte_swap_ifd_data(IFD_data_swapped);
720     load_stream((IUnknown *)reader, IFD_data_swapped, sizeof(IFD_data), persist_options);
721     hr = IWICMetadataReader_GetCount(reader, &count);
722     ok(hr == S_OK, "GetCount error %#x\n", hr);
723     ok(count == sizeof(td)/sizeof(td[0]), "unexpected count %u\n", count);
724     compare_metadata(reader, td, count);
725     HeapFree(GetProcessHeap(), 0, IFD_data_swapped);
726
727     hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
728     ok(hr == S_OK, "GetMetadataFormat error %#x\n", hr);
729     ok(IsEqualGUID(&format, &GUID_MetadataFormatIfd), "unexpected format %s\n", debugstr_guid(&format));
730
731     hr = IWICMetadataReader_GetMetadataFormat(reader, NULL);
732     ok(hr == E_INVALIDARG, "GetMetadataFormat should fail\n");
733
734     hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, NULL, NULL);
735     ok(hr == S_OK, "GetValueByIndex error %#x\n", hr);
736
737     PropVariantInit(&schema);
738     PropVariantInit(&id);
739     PropVariantInit(&value);
740
741     hr = IWICMetadataReader_GetValueByIndex(reader, count - 1, NULL, NULL, NULL);
742     ok(hr == S_OK, "GetValueByIndex error %#x\n", hr);
743
744     hr = IWICMetadataReader_GetValueByIndex(reader, 0, &schema, NULL, NULL);
745     ok(hr == S_OK, "GetValueByIndex error %#x\n", hr);
746     ok(schema.vt == VT_EMPTY, "unexpected vt: %u\n", schema.vt);
747
748     hr = IWICMetadataReader_GetValueByIndex(reader, count - 1, &schema, NULL, NULL);
749     ok(hr == S_OK, "GetValueByIndex error %#x\n", hr);
750     ok(schema.vt == VT_EMPTY, "unexpected vt: %u\n", schema.vt);
751
752     hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, &id, NULL);
753     ok(hr == S_OK, "GetValueByIndex error %#x\n", hr);
754     ok(id.vt == VT_UI2, "unexpected vt: %u\n", id.vt);
755     ok(U(id).uiVal == 0xfe, "unexpected id: %#x\n", U(id).uiVal);
756     PropVariantClear(&id);
757
758     hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, NULL, &value);
759     ok(hr == S_OK, "GetValueByIndex error %#x\n", hr);
760     ok(value.vt == VT_UI2, "unexpected vt: %u\n", value.vt);
761     ok(U(value).uiVal == 1, "unexpected id: %#x\n", U(value).uiVal);
762     PropVariantClear(&value);
763
764     hr = IWICMetadataReader_GetValueByIndex(reader, count, &schema, NULL, NULL);
765     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
766
767     PropVariantInit(&schema);
768     PropVariantInit(&id);
769     PropVariantInit(&value);
770
771     hr = IWICMetadataReader_GetValue(reader, &schema, &id, &value);
772     ok(hr == WINCODEC_ERR_PROPERTYNOTFOUND, "expected WINCODEC_ERR_PROPERTYNOTFOUND, got %#x\n", hr);
773
774     hr = IWICMetadataReader_GetValue(reader, NULL, &id, NULL);
775     ok(hr == WINCODEC_ERR_PROPERTYNOTFOUND, "expected WINCODEC_ERR_PROPERTYNOTFOUND, got %#x\n", hr);
776
777     hr = IWICMetadataReader_GetValue(reader, &schema, NULL, NULL);
778     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
779
780     hr = IWICMetadataReader_GetValue(reader, &schema, &id, NULL);
781     ok(hr == WINCODEC_ERR_PROPERTYNOTFOUND, "expected WINCODEC_ERR_PROPERTYNOTFOUND, got %#x\n", hr);
782
783     hr = IWICMetadataReader_GetValue(reader, &schema, NULL, &value);
784     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
785
786     id.vt = VT_UI2;
787     U(id).uiVal = 0xf00e;
788     hr = IWICMetadataReader_GetValue(reader, NULL, &id, NULL);
789     ok(hr == S_OK, "GetValue error %#x\n", hr);
790
791     /* schema is ignored by Ifd metadata reader */
792     schema.vt = VT_UI4;
793     U(schema).ulVal = 0xdeadbeef;
794     hr = IWICMetadataReader_GetValue(reader, &schema, &id, &value);
795     ok(hr == S_OK, "GetValue error %#x\n", hr);
796     ok(value.vt == VT_LPSTR, "unexpected vt: %i\n", id.vt);
797     ok(!strcmp(U(value).pszVal, "Hello World!"), "unexpected value: %s\n", U(value).pszVal);
798     PropVariantClear(&value);
799
800     hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
801     ok(hr == S_OK, "GetValue error %#x\n", hr);
802     ok(value.vt == VT_LPSTR, "unexpected vt: %i\n", id.vt);
803     ok(!strcmp(U(value).pszVal, "Hello World!"), "unexpected value: %s\n", U(value).pszVal);
804     PropVariantClear(&value);
805
806     hr = IWICMetadataReader_QueryInterface(reader, &IID_IWICMetadataBlockReader, (void**)&blockreader);
807     ok(hr == E_NOINTERFACE, "QueryInterface failed, hr=%x\n", hr);
808
809     if (SUCCEEDED(hr))
810         IWICMetadataBlockReader_Release(blockreader);
811
812     IWICMetadataReader_Release(reader);
813 }
814
815 static void test_metadata_Exif(void)
816 {
817     HRESULT hr;
818     IWICMetadataReader *reader;
819     IWICMetadataBlockReader *blockreader;
820     UINT count=0;
821
822     hr = CoCreateInstance(&CLSID_WICExifMetadataReader, NULL, CLSCTX_INPROC_SERVER,
823         &IID_IWICMetadataReader, (void**)&reader);
824     todo_wine ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
825     if (FAILED(hr)) return;
826
827     hr = IWICMetadataReader_GetCount(reader, NULL);
828     ok(hr == E_INVALIDARG, "GetCount error %#x\n", hr);
829
830     hr = IWICMetadataReader_GetCount(reader, &count);
831     ok(hr == S_OK, "GetCount error %#x\n", hr);
832     ok(count == 0, "unexpected count %u\n", count);
833
834     hr = IWICMetadataReader_QueryInterface(reader, &IID_IWICMetadataBlockReader, (void**)&blockreader);
835     ok(hr == E_NOINTERFACE, "QueryInterface failed, hr=%x\n", hr);
836
837     if (SUCCEEDED(hr))
838         IWICMetadataBlockReader_Release(blockreader);
839
840     IWICMetadataReader_Release(reader);
841 }
842
843 static void test_create_reader(void)
844 {
845     HRESULT hr;
846     IWICComponentFactory *factory;
847     IStream *stream;
848     IWICMetadataReader *reader;
849     UINT count=0;
850     GUID format;
851
852     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
853         &IID_IWICComponentFactory, (void**)&factory);
854     ok(hr == S_OK, "CoCreateInstance failed, hr=%x\n", hr);
855
856     stream = create_stream(metadata_tEXt, sizeof(metadata_tEXt));
857
858     hr = IWICComponentFactory_CreateMetadataReaderFromContainer(factory,
859         &GUID_ContainerFormatPng, NULL, WICPersistOptionsDefault,
860         stream, &reader);
861 todo_wine
862     ok(hr == S_OK, "CreateMetadataReaderFromContainer failed, hr=%x\n", hr);
863     if (FAILED(hr)) return;
864
865     if (SUCCEEDED(hr))
866     {
867         hr = IWICMetadataReader_GetCount(reader, &count);
868         ok(hr == S_OK, "GetCount failed, hr=%x\n", hr);
869         ok(count == 1, "unexpected count %i\n", count);
870
871         hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
872         ok(hr == S_OK, "GetMetadataFormat failed, hr=%x\n", hr);
873         ok(IsEqualGUID(&format, &GUID_MetadataFormatChunktEXt), "unexpected format %s\n", debugstr_guid(&format));
874
875         IWICMetadataReader_Release(reader);
876     }
877
878     hr = IWICComponentFactory_CreateMetadataReaderFromContainer(factory,
879         &GUID_ContainerFormatWmp, NULL, WICPersistOptionsDefault,
880         stream, &reader);
881     ok(hr == S_OK, "CreateMetadataReaderFromContainer failed, hr=%x\n", hr);
882
883     if (SUCCEEDED(hr))
884     {
885         hr = IWICMetadataReader_GetCount(reader, &count);
886         ok(hr == S_OK, "GetCount failed, hr=%x\n", hr);
887         ok(count == 1, "unexpected count %i\n", count);
888
889         hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
890         ok(hr == S_OK, "GetMetadataFormat failed, hr=%x\n", hr);
891         ok(IsEqualGUID(&format, &GUID_MetadataFormatUnknown), "unexpected format %s\n", debugstr_guid(&format));
892
893         IWICMetadataReader_Release(reader);
894     }
895
896     IStream_Release(stream);
897
898     IWICComponentFactory_Release(factory);
899 }
900
901 static void test_metadata_png(void)
902 {
903     static const struct test_data td[6] =
904     {
905         { VT_UI2, 0, 0, { 2005 }, NULL, { 'Y','e','a','r',0 } },
906         { VT_UI1, 0, 0, { 6 }, NULL, { 'M','o','n','t','h',0 } },
907         { VT_UI1, 0, 0, { 3 }, NULL, { 'D','a','y',0 } },
908         { VT_UI1, 0, 0, { 15 }, NULL, { 'H','o','u','r',0 } },
909         { VT_UI1, 0, 0, { 7 }, NULL, { 'M','i','n','u','t','e',0 } },
910         { VT_UI1, 0, 0, { 45 }, NULL, { 'S','e','c','o','n','d',0 } }
911     };
912     IStream *stream;
913     IWICBitmapDecoder *decoder;
914     IWICBitmapFrameDecode *frame;
915     IWICMetadataBlockReader *blockreader;
916     IWICMetadataReader *reader;
917     GUID containerformat;
918     HRESULT hr;
919     UINT count;
920
921     hr = CoCreateInstance(&CLSID_WICPngDecoder, NULL, CLSCTX_INPROC_SERVER,
922         &IID_IWICBitmapDecoder, (void**)&decoder);
923     ok(hr == S_OK, "CoCreateInstance failed, hr=%x\n", hr);
924
925     if (FAILED(hr)) return;
926
927     stream = create_stream(pngimage, sizeof(pngimage));
928
929     hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
930     ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
931
932     hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void**)&blockreader);
933     ok(hr == E_NOINTERFACE, "QueryInterface failed, hr=%x\n", hr);
934
935     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
936     ok(hr == S_OK, "GetFrame failed, hr=%x\n", hr);
937
938     hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void**)&blockreader);
939     ok(hr == S_OK, "QueryInterface failed, hr=%x\n", hr);
940
941     if (SUCCEEDED(hr))
942     {
943         hr = IWICMetadataBlockReader_GetContainerFormat(blockreader, NULL);
944         ok(hr == E_INVALIDARG, "GetContainerFormat failed, hr=%x\n", hr);
945
946         hr = IWICMetadataBlockReader_GetContainerFormat(blockreader, &containerformat);
947         ok(hr == S_OK, "GetContainerFormat failed, hr=%x\n", hr);
948         ok(IsEqualGUID(&containerformat, &GUID_ContainerFormatPng), "unexpected container format\n");
949
950         hr = IWICMetadataBlockReader_GetCount(blockreader, NULL);
951         todo_wine ok(hr == E_INVALIDARG, "GetCount failed, hr=%x\n", hr);
952
953         hr = IWICMetadataBlockReader_GetCount(blockreader, &count);
954         todo_wine ok(hr == S_OK, "GetCount failed, hr=%x\n", hr);
955         todo_wine ok(count == 1, "unexpected count %d\n", count);
956
957         if (0)
958         {
959             /* Crashes on Windows XP */
960             hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 0, NULL);
961             ok(hr == E_INVALIDARG, "GetReaderByIndex failed, hr=%x\n", hr);
962         }
963
964         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 0, &reader);
965         todo_wine ok(hr == S_OK, "GetReaderByIndex failed, hr=%x\n", hr);
966
967         if (SUCCEEDED(hr))
968         {
969             hr = IWICMetadataReader_GetMetadataFormat(reader, &containerformat);
970             ok(IsEqualGUID(&containerformat, &GUID_MetadataFormatChunktIME) ||
971                broken(IsEqualGUID(&containerformat, &GUID_MetadataFormatUnknown)) /* Windows XP */,
972                "unexpected container format\n");
973
974             hr = IWICMetadataReader_GetCount(reader, &count);
975             ok(hr == S_OK, "GetCount error %#x\n", hr);
976             ok(count == 6 || broken(count == 1) /* XP */, "expected 6, got %u\n", count);
977             if (count == 6)
978                 compare_metadata(reader, td, count);
979
980             IWICMetadataReader_Release(reader);
981         }
982
983         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 1, &reader);
984         todo_wine ok(hr == WINCODEC_ERR_VALUEOUTOFRANGE, "GetReaderByIndex failed, hr=%x\n", hr);
985
986         IWICMetadataBlockReader_Release(blockreader);
987     }
988
989     IWICBitmapFrameDecode_Release(frame);
990
991     IWICBitmapDecoder_Release(decoder);
992
993     IStream_Release(stream);
994 }
995
996 static void test_metadata_gif(void)
997 {
998     static const struct test_data gif_LSD[9] =
999     {
1000         { VT_UI1|VT_VECTOR, 0, 6, {'G','I','F','8','7','a'}, NULL, { 'S','i','g','n','a','t','u','r','e',0 } },
1001         { VT_UI2, 0, 0, { 1 }, NULL, { 'W','i','d','t','h',0 } },
1002         { VT_UI2, 0, 0, { 1 }, NULL, { 'H','e','i','g','h','t',0 } },
1003         { VT_BOOL, 0, 0, { 1 }, NULL, { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 } },
1004         { VT_UI1, 0, 0, { 0 }, NULL, { 'C','o','l','o','r','R','e','s','o','l','u','t','i','o','n',0 } },
1005         { VT_BOOL, 0, 0, { 0 }, NULL, { 'S','o','r','t','F','l','a','g',0 } },
1006         { VT_UI1, 0, 0, { 0 }, NULL, { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','S','i','z','e',0 } },
1007         { VT_UI1, 0, 0, { 0 }, NULL, { 'B','a','c','k','g','r','o','u','n','d','C','o','l','o','r','I','n','d','e','x',0 } },
1008         { VT_UI1, 0, 0, { 0 }, NULL, { 'P','i','x','e','l','A','s','p','e','c','t','R','a','t','i','o',0 } }
1009     };
1010     static const struct test_data gif_IMD[8] =
1011     {
1012         { VT_UI2, 0, 0, { 0 }, NULL, { 'L','e','f','t',0 } },
1013         { VT_UI2, 0, 0, { 0 }, NULL, { 'T','o','p',0 } },
1014         { VT_UI2, 0, 0, { 1 }, NULL, { 'W','i','d','t','h',0 } },
1015         { VT_UI2, 0, 0, { 1 }, NULL, { 'H','e','i','g','h','t',0 } },
1016         { VT_BOOL, 0, 0, { 0 }, NULL, { 'L','o','c','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 } },
1017         { VT_BOOL, 0, 0, { 0 }, NULL, { 'I','n','t','e','r','l','a','c','e','F','l','a','g',0 } },
1018         { VT_BOOL, 0, 0, { 0 }, NULL, { 'S','o','r','t','F','l','a','g',0 } },
1019         { VT_UI1, 0, 0, { 0 }, NULL, { 'L','o','c','a','l','C','o','l','o','r','T','a','b','l','e','S','i','z','e',0 } }
1020     };
1021     static const struct test_data animated_gif_LSD[9] =
1022     {
1023         { VT_UI1|VT_VECTOR, 0, 6, {'G','I','F','8','9','a'}, NULL, { 'S','i','g','n','a','t','u','r','e',0 } },
1024         { VT_UI2, 0, 0, { 1 }, NULL, { 'W','i','d','t','h',0 } },
1025         { VT_UI2, 0, 0, { 1 }, NULL, { 'H','e','i','g','h','t',0 } },
1026         { VT_BOOL, 0, 0, { 1 }, NULL, { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 } },
1027         { VT_UI1, 0, 0, { 2 }, NULL, { 'C','o','l','o','r','R','e','s','o','l','u','t','i','o','n',0 } },
1028         { VT_BOOL, 0, 0, { 0 }, NULL, { 'S','o','r','t','F','l','a','g',0 } },
1029         { VT_UI1, 0, 0, { 1 }, NULL, { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','S','i','z','e',0 } },
1030         { VT_UI1, 0, 0, { 0 }, NULL, { 'B','a','c','k','g','r','o','u','n','d','C','o','l','o','r','I','n','d','e','x',0 } },
1031         { VT_UI1, 0, 0, { 0 }, NULL, { 'P','i','x','e','l','A','s','p','e','c','t','R','a','t','i','o',0 } }
1032     };
1033     static const struct test_data animated_gif_IMD[8] =
1034     {
1035         { VT_UI2, 0, 0, { 0 }, NULL, { 'L','e','f','t',0 } },
1036         { VT_UI2, 0, 0, { 0 }, NULL, { 'T','o','p',0 } },
1037         { VT_UI2, 0, 0, { 1 }, NULL, { 'W','i','d','t','h',0 } },
1038         { VT_UI2, 0, 0, { 1 }, NULL, { 'H','e','i','g','h','t',0 } },
1039         { VT_BOOL, 0, 0, { 1 }, NULL, { 'L','o','c','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 } },
1040         { VT_BOOL, 0, 0, { 0 }, NULL, { 'I','n','t','e','r','l','a','c','e','F','l','a','g',0 } },
1041         { VT_BOOL, 0, 0, { 0 }, NULL, { 'S','o','r','t','F','l','a','g',0 } },
1042         { VT_UI1, 0, 0, { 1 }, NULL, { 'L','o','c','a','l','C','o','l','o','r','T','a','b','l','e','S','i','z','e',0 } }
1043     };
1044     static const struct test_data animated_gif_GCE[5] =
1045     {
1046         { VT_UI1, 0, 0, { 0 }, NULL, { 'D','i','s','p','o','s','a','l',0 } },
1047         { VT_BOOL, 0, 0, { 0 }, NULL, { 'U','s','e','r','I','n','p','u','t','F','l','a','g',0 } },
1048         { VT_BOOL, 0, 0, { 1 }, NULL, { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 } },
1049         { VT_UI2, 0, 0, { 10 }, NULL, { 'D','e','l','a','y',0 } },
1050         { VT_UI1, 0, 0, { 1 }, NULL, { 'T','r','a','n','s','p','a','r','e','n','t','C','o','l','o','r','I','n','d','e','x',0 } }
1051     };
1052     static const struct test_data animated_gif_APE[2] =
1053     {
1054         { VT_UI1|VT_VECTOR, 0, 11, { 'A','N','I','M','E','X','T','S','1','.','0' }, NULL, { 'A','p','p','l','i','c','a','t','i','o','n',0 } },
1055         { VT_UI1|VT_VECTOR, 0, 4, { 0x03,0x01,0x05,0x00 }, NULL, { 'D','a','t','a',0 } }
1056     };
1057     static const struct test_data animated_gif_comment_1[1] =
1058     {
1059         { VT_LPSTR, 0, 12, { 0 }, "Hello World!", { 'T','e','x','t','E','n','t','r','y',0 } }
1060     };
1061     static const struct test_data animated_gif_comment_2[1] =
1062     {
1063         { VT_LPSTR, 0, 8, { 0 }, "image #1", { 'T','e','x','t','E','n','t','r','y',0 } }
1064     };
1065     static const struct test_data animated_gif_plain_1[1] =
1066     {
1067         { VT_BLOB, 0, 17, { 0 }, "\x21\x01\x0d\x61nimation.gif" }
1068     };
1069     static const struct test_data animated_gif_plain_2[1] =
1070     {
1071         { VT_BLOB, 0, 16, { 0 }, "\x21\x01\x0cplaintext #1" }
1072     };
1073     IStream *stream;
1074     IWICBitmapDecoder *decoder;
1075     IWICBitmapFrameDecode *frame;
1076     IWICMetadataBlockReader *blockreader;
1077     IWICMetadataReader *reader;
1078     GUID format;
1079     HRESULT hr;
1080     UINT count;
1081
1082     /* 1x1 pixel gif */
1083     stream = create_stream(gifimage, sizeof(gifimage));
1084
1085     hr = CoCreateInstance(&CLSID_WICGifDecoder, NULL, CLSCTX_INPROC_SERVER,
1086                           &IID_IWICBitmapDecoder, (void **)&decoder);
1087     ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
1088     hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
1089     ok(hr == S_OK, "Initialize error %#x\n", hr);
1090
1091     IStream_Release(stream);
1092
1093     /* global metadata block */
1094     hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&blockreader);
1095     ok(hr == S_OK || broken(hr == E_NOINTERFACE) /* before Win7 */, "QueryInterface error %#x\n", hr);
1096
1097     if (SUCCEEDED(hr))
1098     {
1099         hr = IWICMetadataBlockReader_GetContainerFormat(blockreader, &format);
1100         ok(hr == S_OK, "GetContainerFormat error %#x\n", hr);
1101         ok(IsEqualGUID(&format, &GUID_ContainerFormatGif),
1102            "wrong container format %s\n", debugstr_guid(&format));
1103
1104         hr = IWICMetadataBlockReader_GetCount(blockreader, &count);
1105         ok(hr == S_OK, "GetCount error %#x\n", hr);
1106         ok(count == 1, "expected 1, got %u\n", count);
1107
1108         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 0, &reader);
1109         ok(hr == S_OK, "GetReaderByIndex error %#x\n", hr);
1110
1111         if (SUCCEEDED(hr))
1112         {
1113             hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1114             ok(IsEqualGUID(&format, &GUID_MetadataFormatLSD), /* Logical Screen Descriptor */
1115                "wrong container format %s\n", debugstr_guid(&format));
1116
1117             hr = IWICMetadataReader_GetCount(reader, &count);
1118             ok(hr == S_OK, "GetCount error %#x\n", hr);
1119             ok(count == sizeof(gif_LSD)/sizeof(gif_LSD[0]), "unexpected count %u\n", count);
1120
1121             compare_metadata(reader, gif_LSD, count);
1122
1123             IWICMetadataReader_Release(reader);
1124         }
1125
1126         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 1, &reader);
1127         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
1128
1129         IWICMetadataBlockReader_Release(blockreader);
1130     }
1131
1132     /* frame metadata block */
1133     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
1134     ok(hr == S_OK, "GetFrame error %#x\n", hr);
1135
1136     hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&blockreader);
1137     ok(hr == S_OK || broken(hr == E_NOINTERFACE) /* before Win7 */, "QueryInterface error %#x\n", hr);
1138
1139     if (SUCCEEDED(hr))
1140     {
1141         hr = IWICMetadataBlockReader_GetContainerFormat(blockreader, NULL);
1142         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
1143
1144         hr = IWICMetadataBlockReader_GetContainerFormat(blockreader, &format);
1145         ok(hr == S_OK, "GetContainerFormat error %#x\n", hr);
1146         ok(IsEqualGUID(&format, &GUID_ContainerFormatGif),
1147            "wrong container format %s\n", debugstr_guid(&format));
1148
1149         hr = IWICMetadataBlockReader_GetCount(blockreader, NULL);
1150         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
1151
1152         hr = IWICMetadataBlockReader_GetCount(blockreader, &count);
1153         ok(hr == S_OK, "GetCount error %#x\n", hr);
1154         ok(count == 1, "expected 1, got %u\n", count);
1155
1156         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 0, &reader);
1157         ok(hr == S_OK, "GetReaderByIndex error %#x\n", hr);
1158
1159         if (SUCCEEDED(hr))
1160         {
1161             hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1162             ok(IsEqualGUID(&format, &GUID_MetadataFormatIMD), /* Image Descriptor */
1163                "wrong container format %s\n", debugstr_guid(&format));
1164
1165             hr = IWICMetadataReader_GetCount(reader, &count);
1166             ok(hr == S_OK, "GetCount error %#x\n", hr);
1167             ok(count == sizeof(gif_IMD)/sizeof(gif_IMD[0]), "unexpected count %u\n", count);
1168
1169             compare_metadata(reader, gif_IMD, count);
1170
1171             IWICMetadataReader_Release(reader);
1172         }
1173
1174         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 1, &reader);
1175         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
1176
1177         IWICMetadataBlockReader_Release(blockreader);
1178     }
1179
1180     IWICBitmapFrameDecode_Release(frame);
1181     IWICBitmapDecoder_Release(decoder);
1182
1183     /* 1x1 pixel gif, 2 frames */
1184     stream = create_stream(animatedgif, sizeof(animatedgif));
1185
1186     hr = CoCreateInstance(&CLSID_WICGifDecoder, NULL, CLSCTX_INPROC_SERVER,
1187                           &IID_IWICBitmapDecoder, (void **)&decoder);
1188     ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
1189     hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
1190     ok(hr == S_OK, "Initialize error %#x\n", hr);
1191
1192     IStream_Release(stream);
1193
1194     /* global metadata block */
1195     hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&blockreader);
1196     ok(hr == S_OK || broken(hr == E_NOINTERFACE) /* before Win7 */, "QueryInterface error %#x\n", hr);
1197
1198     if (SUCCEEDED(hr))
1199     {
1200         hr = IWICMetadataBlockReader_GetContainerFormat(blockreader, &format);
1201         ok(hr == S_OK, "GetContainerFormat error %#x\n", hr);
1202         ok(IsEqualGUID(&format, &GUID_ContainerFormatGif),
1203            "wrong container format %s\n", debugstr_guid(&format));
1204
1205         hr = IWICMetadataBlockReader_GetCount(blockreader, &count);
1206         ok(hr == S_OK, "GetCount error %#x\n", hr);
1207 todo_wine
1208         ok(count == 4, "expected 4, got %u\n", count);
1209
1210         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 0, &reader);
1211         ok(hr == S_OK, "GetReaderByIndex error %#x\n", hr);
1212
1213         if (SUCCEEDED(hr))
1214         {
1215             hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1216             ok(IsEqualGUID(&format, &GUID_MetadataFormatLSD), /* Logical Screen Descriptor */
1217                "wrong container format %s\n", debugstr_guid(&format));
1218
1219             hr = IWICMetadataReader_GetCount(reader, &count);
1220             ok(hr == S_OK, "GetCount error %#x\n", hr);
1221             ok(count == sizeof(animated_gif_LSD)/sizeof(animated_gif_LSD[0]), "unexpected count %u\n", count);
1222
1223             compare_metadata(reader, animated_gif_LSD, count);
1224
1225             IWICMetadataReader_Release(reader);
1226         }
1227
1228         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 1, &reader);
1229 todo_wine
1230         ok(hr == S_OK, "GetReaderByIndex error %#x\n", hr);
1231
1232         if (SUCCEEDED(hr))
1233         {
1234             hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1235             ok(IsEqualGUID(&format, &GUID_MetadataFormatAPE), /* Application Extension */
1236                "wrong container format %s\n", debugstr_guid(&format));
1237
1238             hr = IWICMetadataReader_GetCount(reader, &count);
1239             ok(hr == S_OK, "GetCount error %#x\n", hr);
1240             ok(count == sizeof(animated_gif_APE)/sizeof(animated_gif_APE[0]), "unexpected count %u\n", count);
1241
1242             compare_metadata(reader, animated_gif_APE, count);
1243
1244             IWICMetadataReader_Release(reader);
1245         }
1246
1247         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 2, &reader);
1248 todo_wine
1249         ok(hr == S_OK, "GetReaderByIndex error %#x\n", hr);
1250
1251         if (SUCCEEDED(hr))
1252         {
1253             hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1254             ok(IsEqualGUID(&format, &GUID_MetadataFormatGifComment), /* Comment Extension */
1255                "wrong container format %s\n", debugstr_guid(&format));
1256
1257             hr = IWICMetadataReader_GetCount(reader, &count);
1258             ok(hr == S_OK, "GetCount error %#x\n", hr);
1259             ok(count == sizeof(animated_gif_comment_1)/sizeof(animated_gif_comment_1[0]), "unexpected count %u\n", count);
1260
1261             compare_metadata(reader, animated_gif_comment_1, count);
1262
1263             IWICMetadataReader_Release(reader);
1264         }
1265
1266         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 3, &reader);
1267 todo_wine
1268         ok(hr == S_OK, "GetReaderByIndex error %#x\n", hr);
1269
1270         if (SUCCEEDED(hr))
1271         {
1272             hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1273             ok(IsEqualGUID(&format, &GUID_MetadataFormatUnknown),
1274                "wrong container format %s\n", debugstr_guid(&format));
1275
1276             hr = IWICMetadataReader_GetCount(reader, &count);
1277             ok(hr == S_OK, "GetCount error %#x\n", hr);
1278             ok(count == sizeof(animated_gif_plain_1)/sizeof(animated_gif_plain_1[0]), "unexpected count %u\n", count);
1279
1280             compare_metadata(reader, animated_gif_plain_1, count);
1281
1282             IWICMetadataReader_Release(reader);
1283         }
1284
1285         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 4, &reader);
1286         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
1287
1288         IWICMetadataBlockReader_Release(blockreader);
1289     }
1290
1291     /* frame metadata block */
1292     hr = IWICBitmapDecoder_GetFrame(decoder, 1, &frame);
1293     ok(hr == S_OK, "GetFrame error %#x\n", hr);
1294
1295     hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&blockreader);
1296     ok(hr == S_OK || broken(hr == E_NOINTERFACE) /* before Win7 */, "QueryInterface error %#x\n", hr);
1297
1298     if (SUCCEEDED(hr))
1299     {
1300         hr = IWICMetadataBlockReader_GetContainerFormat(blockreader, NULL);
1301         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
1302
1303         hr = IWICMetadataBlockReader_GetContainerFormat(blockreader, &format);
1304         ok(hr == S_OK, "GetContainerFormat error %#x\n", hr);
1305         ok(IsEqualGUID(&format, &GUID_ContainerFormatGif),
1306            "wrong container format %s\n", debugstr_guid(&format));
1307
1308         hr = IWICMetadataBlockReader_GetCount(blockreader, NULL);
1309         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
1310
1311         hr = IWICMetadataBlockReader_GetCount(blockreader, &count);
1312         ok(hr == S_OK, "GetCount error %#x\n", hr);
1313 todo_wine
1314         ok(count == 4, "expected 4, got %u\n", count);
1315
1316         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 0, &reader);
1317         ok(hr == S_OK, "GetReaderByIndex error %#x\n", hr);
1318
1319         if (SUCCEEDED(hr))
1320         {
1321             hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1322             ok(IsEqualGUID(&format, &GUID_MetadataFormatIMD), /* Image Descriptor */
1323                "wrong container format %s\n", debugstr_guid(&format));
1324
1325             hr = IWICMetadataReader_GetCount(reader, &count);
1326             ok(hr == S_OK, "GetCount error %#x\n", hr);
1327             ok(count == sizeof(animated_gif_IMD)/sizeof(animated_gif_IMD[0]), "unexpected count %u\n", count);
1328
1329             compare_metadata(reader, animated_gif_IMD, count);
1330
1331             IWICMetadataReader_Release(reader);
1332         }
1333
1334         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 1, &reader);
1335         ok(hr == S_OK, "GetReaderByIndex error %#x\n", hr);
1336
1337         if (SUCCEEDED(hr))
1338         {
1339             hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1340 todo_wine
1341             ok(IsEqualGUID(&format, &GUID_MetadataFormatGifComment), /* Comment Extension */
1342                "wrong container format %s\n", debugstr_guid(&format));
1343
1344             hr = IWICMetadataReader_GetCount(reader, &count);
1345             ok(hr == S_OK, "GetCount error %#x\n", hr);
1346 todo_wine
1347             ok(count == sizeof(animated_gif_comment_2)/sizeof(animated_gif_comment_2[0]), "unexpected count %u\n", count);
1348
1349             if (count == 1)
1350             compare_metadata(reader, animated_gif_comment_2, count);
1351
1352             IWICMetadataReader_Release(reader);
1353         }
1354
1355         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 2, &reader);
1356 todo_wine
1357         ok(hr == S_OK, "GetReaderByIndex error %#x\n", hr);
1358
1359         if (SUCCEEDED(hr))
1360         {
1361             hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1362             ok(IsEqualGUID(&format, &GUID_MetadataFormatUnknown),
1363                "wrong container format %s\n", debugstr_guid(&format));
1364
1365             hr = IWICMetadataReader_GetCount(reader, &count);
1366             ok(hr == S_OK, "GetCount error %#x\n", hr);
1367             ok(count == sizeof(animated_gif_plain_2)/sizeof(animated_gif_plain_2[0]), "unexpected count %u\n", count);
1368
1369             compare_metadata(reader, animated_gif_plain_2, count);
1370
1371             IWICMetadataReader_Release(reader);
1372         }
1373
1374         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 3, &reader);
1375 todo_wine
1376         ok(hr == S_OK, "GetReaderByIndex error %#x\n", hr);
1377
1378         if (SUCCEEDED(hr))
1379         {
1380             hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1381             ok(IsEqualGUID(&format, &GUID_MetadataFormatGCE), /* Graphic Control Extension */
1382                "wrong container format %s\n", debugstr_guid(&format));
1383
1384             hr = IWICMetadataReader_GetCount(reader, &count);
1385             ok(hr == S_OK, "GetCount error %#x\n", hr);
1386             ok(count == sizeof(animated_gif_GCE)/sizeof(animated_gif_GCE[0]), "unexpected count %u\n", count);
1387
1388             compare_metadata(reader, animated_gif_GCE, count);
1389
1390             IWICMetadataReader_Release(reader);
1391         }
1392
1393         hr = IWICMetadataBlockReader_GetReaderByIndex(blockreader, 4, &reader);
1394         ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
1395
1396         IWICMetadataBlockReader_Release(blockreader);
1397     }
1398
1399     IWICBitmapFrameDecode_Release(frame);
1400     IWICBitmapDecoder_Release(decoder);
1401 }
1402
1403 static void test_metadata_LSD(void)
1404 {
1405     static const WCHAR LSD_name[] = {'L','o','g','i','c','a','l',' ','S','c','r','e','e','n',' ','D','e','s','c','r','i','p','t','o','r',' ','R','e','a','d','e','r',0};
1406     static const char LSD_data[] = "hello world!\x1\x2\x3\x4\xab\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf";
1407     static const struct test_data td[9] =
1408     {
1409         { VT_UI1|VT_VECTOR, 0, 6, {'w','o','r','l','d','!'}, NULL, { 'S','i','g','n','a','t','u','r','e',0 } },
1410         { VT_UI2, 0, 0, { 0x201 }, NULL, { 'W','i','d','t','h',0 } },
1411         { VT_UI2, 0, 0, { 0x403 }, NULL, { 'H','e','i','g','h','t',0 } },
1412         { VT_BOOL, 0, 0, { 1 }, NULL, { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 } },
1413         { VT_UI1, 0, 0, { 2 }, NULL, { 'C','o','l','o','r','R','e','s','o','l','u','t','i','o','n',0 } },
1414         { VT_BOOL, 0, 0, { 1 }, NULL, { 'S','o','r','t','F','l','a','g',0 } },
1415         { VT_UI1, 0, 0, { 3 }, NULL, { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','S','i','z','e',0 } },
1416         { VT_UI1, 0, 0, { 6 }, NULL, { 'B','a','c','k','g','r','o','u','n','d','C','o','l','o','r','I','n','d','e','x',0 } },
1417         { VT_UI1, 0, 0, { 7 }, NULL, { 'P','i','x','e','l','A','s','p','e','c','t','R','a','t','i','o',0 } }
1418     };
1419     LARGE_INTEGER pos;
1420     HRESULT hr;
1421     IStream *stream;
1422     IWICPersistStream *persist;
1423     IWICMetadataReader *reader;
1424     IWICMetadataHandlerInfo *info;
1425     WCHAR name[64];
1426     UINT count, dummy;
1427     GUID format;
1428     CLSID id;
1429
1430     hr = CoCreateInstance(&CLSID_WICLSDMetadataReader, NULL, CLSCTX_INPROC_SERVER,
1431                           &IID_IWICMetadataReader, (void **)&reader);
1432     ok(hr == S_OK || broken(hr == E_NOINTERFACE || hr == REGDB_E_CLASSNOTREG) /* before Win7 */,
1433        "CoCreateInstance error %#x\n", hr);
1434
1435     stream = create_stream(LSD_data, sizeof(LSD_data));
1436
1437     if (SUCCEEDED(hr))
1438     {
1439         pos.QuadPart = 6;
1440         hr = IStream_Seek(stream, pos, SEEK_SET, NULL);
1441         ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
1442
1443         hr = IUnknown_QueryInterface(reader, &IID_IWICPersistStream, (void **)&persist);
1444         ok(hr == S_OK, "QueryInterface error %#x\n", hr);
1445
1446         hr = IWICPersistStream_Load(persist, stream);
1447         ok(hr == S_OK, "Load error %#x\n", hr);
1448
1449         IWICPersistStream_Release(persist);
1450     }
1451
1452     if (SUCCEEDED(hr))
1453     {
1454         hr = IWICMetadataReader_GetCount(reader, &count);
1455         ok(hr == S_OK, "GetCount error %#x\n", hr);
1456         ok(count == sizeof(td)/sizeof(td[0]), "unexpected count %u\n", count);
1457
1458         compare_metadata(reader, td, count);
1459
1460         hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1461         ok(hr == S_OK, "GetMetadataFormat error %#x\n", hr);
1462         ok(IsEqualGUID(&format, &GUID_MetadataFormatLSD), "wrong format %s\n", debugstr_guid(&format));
1463
1464         hr = IWICMetadataReader_GetMetadataHandlerInfo(reader, &info);
1465         ok(hr == S_OK, "GetMetadataHandlerInfo error %#x\n", hr);
1466
1467         hr = IWICMetadataHandlerInfo_GetCLSID(info, &id);
1468         ok(hr == S_OK, "GetCLSID error %#x\n", hr);
1469         ok(IsEqualGUID(&id, &CLSID_WICLSDMetadataReader), "wrong CLSID %s\n", debugstr_guid(&id));
1470
1471         hr = IWICMetadataHandlerInfo_GetFriendlyName(info, 64, name, &dummy);
1472         ok(hr == S_OK, "GetFriendlyName error %#x\n", hr);
1473         ok(lstrcmpW(name, LSD_name) == 0, "wrong LSD reader name %s\n", wine_dbgstr_w(name));
1474
1475         IWICMetadataHandlerInfo_Release(info);
1476         IWICMetadataReader_Release(reader);
1477     }
1478
1479     IStream_Release(stream);
1480 }
1481
1482 static void test_metadata_IMD(void)
1483 {
1484     static const WCHAR IMD_name[] = {'I','m','a','g','e',' ','D','e','s','c','r','i','p','t','o','r',' ','R','e','a','d','e','r',0};
1485     static const char IMD_data[] = "hello world!\x1\x2\x3\x4\x5\x6\x7\x8\xed\xa\xb\xc\xd\xe\xf";
1486     static const struct test_data td[8] =
1487     {
1488         { VT_UI2, 0, 0, { 0x201 }, NULL, { 'L','e','f','t',0 } },
1489         { VT_UI2, 0, 0, { 0x403 }, NULL, { 'T','o','p',0 } },
1490         { VT_UI2, 0, 0, { 0x605 }, NULL, { 'W','i','d','t','h',0 } },
1491         { VT_UI2, 0, 0, { 0x807 }, NULL, { 'H','e','i','g','h','t',0 } },
1492         { VT_BOOL, 0, 0, { 1 }, NULL, { 'L','o','c','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 } },
1493         { VT_BOOL, 0, 0, { 1 }, NULL, { 'I','n','t','e','r','l','a','c','e','F','l','a','g',0 } },
1494         { VT_BOOL, 0, 0, { 1 }, NULL, { 'S','o','r','t','F','l','a','g',0 } },
1495         { VT_UI1, 0, 0, { 5 }, NULL, { 'L','o','c','a','l','C','o','l','o','r','T','a','b','l','e','S','i','z','e',0 } }
1496     };
1497     LARGE_INTEGER pos;
1498     HRESULT hr;
1499     IStream *stream;
1500     IWICPersistStream *persist;
1501     IWICMetadataReader *reader;
1502     IWICMetadataHandlerInfo *info;
1503     WCHAR name[64];
1504     UINT count, dummy;
1505     GUID format;
1506     CLSID id;
1507
1508     hr = CoCreateInstance(&CLSID_WICIMDMetadataReader, NULL, CLSCTX_INPROC_SERVER,
1509                           &IID_IWICMetadataReader, (void **)&reader);
1510     ok(hr == S_OK || broken(hr == E_NOINTERFACE || hr == REGDB_E_CLASSNOTREG) /* before Win7 */,
1511        "CoCreateInstance error %#x\n", hr);
1512
1513     stream = create_stream(IMD_data, sizeof(IMD_data));
1514
1515     if (SUCCEEDED(hr))
1516     {
1517         pos.QuadPart = 12;
1518         hr = IStream_Seek(stream, pos, SEEK_SET, NULL);
1519         ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
1520
1521         hr = IUnknown_QueryInterface(reader, &IID_IWICPersistStream, (void **)&persist);
1522         ok(hr == S_OK, "QueryInterface error %#x\n", hr);
1523
1524         hr = IWICPersistStream_Load(persist, stream);
1525         ok(hr == S_OK, "Load error %#x\n", hr);
1526
1527         IWICPersistStream_Release(persist);
1528     }
1529
1530     if (SUCCEEDED(hr))
1531     {
1532         hr = IWICMetadataReader_GetCount(reader, &count);
1533         ok(hr == S_OK, "GetCount error %#x\n", hr);
1534         ok(count == sizeof(td)/sizeof(td[0]), "unexpected count %u\n", count);
1535
1536         compare_metadata(reader, td, count);
1537
1538         hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1539         ok(hr == S_OK, "GetMetadataFormat error %#x\n", hr);
1540         ok(IsEqualGUID(&format, &GUID_MetadataFormatIMD), "wrong format %s\n", debugstr_guid(&format));
1541
1542         hr = IWICMetadataReader_GetMetadataHandlerInfo(reader, &info);
1543         ok(hr == S_OK, "GetMetadataHandlerInfo error %#x\n", hr);
1544
1545         hr = IWICMetadataHandlerInfo_GetCLSID(info, &id);
1546         ok(hr == S_OK, "GetCLSID error %#x\n", hr);
1547         ok(IsEqualGUID(&id, &CLSID_WICIMDMetadataReader), "wrong CLSID %s\n", debugstr_guid(&id));
1548
1549         hr = IWICMetadataHandlerInfo_GetFriendlyName(info, 64, name, &dummy);
1550         ok(hr == S_OK, "GetFriendlyName error %#x\n", hr);
1551         ok(lstrcmpW(name, IMD_name) == 0, "wrong IMD reader name %s\n", wine_dbgstr_w(name));
1552
1553         IWICMetadataHandlerInfo_Release(info);
1554         IWICMetadataReader_Release(reader);
1555     }
1556
1557     IStream_Release(stream);
1558 }
1559
1560 static void test_metadata_GCE(void)
1561 {
1562     static const WCHAR GCE_name[] = {'G','r','a','p','h','i','c',' ','C','o','n','t','r','o','l',' ','E','x','t','e','n','s','i','o','n',' ','R','e','a','d','e','r',0};
1563     static const char GCE_data[] = "hello world!\xa\x2\x3\x4\x5\x6\x7\x8\xed\xa\xb\xc\xd\xe\xf";
1564     static const struct test_data td[5] =
1565     {
1566         { VT_UI1, 0, 0, { 2 }, NULL, { 'D','i','s','p','o','s','a','l',0 } },
1567         { VT_BOOL, 0, 0, { 1 }, NULL, { 'U','s','e','r','I','n','p','u','t','F','l','a','g',0 } },
1568         { VT_BOOL, 0, 0, { 0 }, NULL, { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 } },
1569         { VT_UI2, 0, 0, { 0x302 }, NULL, { 'D','e','l','a','y',0 } },
1570         { VT_UI1, 0, 0, { 4 }, NULL, { 'T','r','a','n','s','p','a','r','e','n','t','C','o','l','o','r','I','n','d','e','x',0 } }
1571     };
1572     LARGE_INTEGER pos;
1573     HRESULT hr;
1574     IStream *stream;
1575     IWICPersistStream *persist;
1576     IWICMetadataReader *reader;
1577     IWICMetadataHandlerInfo *info;
1578     WCHAR name[64];
1579     UINT count, dummy;
1580     GUID format;
1581     CLSID id;
1582
1583     hr = CoCreateInstance(&CLSID_WICGCEMetadataReader, NULL, CLSCTX_INPROC_SERVER,
1584                           &IID_IWICMetadataReader, (void **)&reader);
1585     ok(hr == S_OK || broken(hr == E_NOINTERFACE || hr == REGDB_E_CLASSNOTREG) /* before Win7 */,
1586        "CoCreateInstance error %#x\n", hr);
1587
1588     stream = create_stream(GCE_data, sizeof(GCE_data));
1589
1590     if (SUCCEEDED(hr))
1591     {
1592         pos.QuadPart = 12;
1593         hr = IStream_Seek(stream, pos, SEEK_SET, NULL);
1594         ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
1595
1596         hr = IUnknown_QueryInterface(reader, &IID_IWICPersistStream, (void **)&persist);
1597         ok(hr == S_OK, "QueryInterface error %#x\n", hr);
1598
1599         hr = IWICPersistStream_Load(persist, stream);
1600         ok(hr == S_OK, "Load error %#x\n", hr);
1601
1602         IWICPersistStream_Release(persist);
1603     }
1604
1605     if (SUCCEEDED(hr))
1606     {
1607         hr = IWICMetadataReader_GetCount(reader, &count);
1608         ok(hr == S_OK, "GetCount error %#x\n", hr);
1609         ok(count == sizeof(td)/sizeof(td[0]), "unexpected count %u\n", count);
1610
1611         compare_metadata(reader, td, count);
1612
1613         hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1614         ok(hr == S_OK, "GetMetadataFormat error %#x\n", hr);
1615         ok(IsEqualGUID(&format, &GUID_MetadataFormatGCE), "wrong format %s\n", debugstr_guid(&format));
1616
1617         hr = IWICMetadataReader_GetMetadataHandlerInfo(reader, &info);
1618         ok(hr == S_OK, "GetMetadataHandlerInfo error %#x\n", hr);
1619
1620         hr = IWICMetadataHandlerInfo_GetCLSID(info, &id);
1621         ok(hr == S_OK, "GetCLSID error %#x\n", hr);
1622         ok(IsEqualGUID(&id, &CLSID_WICGCEMetadataReader), "wrong CLSID %s\n", debugstr_guid(&id));
1623
1624         hr = IWICMetadataHandlerInfo_GetFriendlyName(info, 64, name, &dummy);
1625         ok(hr == S_OK, "GetFriendlyName error %#x\n", hr);
1626         ok(lstrcmpW(name, GCE_name) == 0, "wrong GCE reader name %s\n", wine_dbgstr_w(name));
1627
1628         IWICMetadataHandlerInfo_Release(info);
1629         IWICMetadataReader_Release(reader);
1630     }
1631
1632     IStream_Release(stream);
1633 }
1634
1635 static void test_metadata_APE(void)
1636 {
1637     static const WCHAR APE_name[] = {'A','p','p','l','i','c','a','t','i','o','n',' ','E','x','t','e','n','s','i','o','n',' ','R','e','a','d','e','r',0};
1638     static const char APE_data[] = { 0x21,0xff,0x0b,'H','e','l','l','o',' ','W','o','r','l','d',
1639                                      /*sub-block*/1,0x11,
1640                                      /*sub-block*/2,0x22,0x33,
1641                                      /*sub-block*/4,0x44,0x55,0x66,0x77,
1642                                      /*terminator*/0 };
1643     static const struct test_data td[2] =
1644     {
1645         { VT_UI1|VT_VECTOR, 0, 11, { 'H','e','l','l','o',' ','W','o','r','l','d' }, NULL, { 'A','p','p','l','i','c','a','t','i','o','n',0 } },
1646         { VT_UI1|VT_VECTOR, 0, 10, { 1,0x11,2,0x22,0x33,4,0x44,0x55,0x66,0x77 }, NULL, { 'D','a','t','a',0 } }
1647     };
1648     HRESULT hr;
1649     IStream *stream;
1650     IWICPersistStream *persist;
1651     IWICMetadataReader *reader;
1652     IWICMetadataHandlerInfo *info;
1653     WCHAR name[64];
1654     UINT count, dummy;
1655     GUID format;
1656     CLSID id;
1657
1658     hr = CoCreateInstance(&CLSID_WICAPEMetadataReader, NULL, CLSCTX_INPROC_SERVER,
1659                           &IID_IWICMetadataReader, (void **)&reader);
1660 todo_wine
1661     ok(hr == S_OK || broken(hr == E_NOINTERFACE || hr == REGDB_E_CLASSNOTREG) /* before Win7 */,
1662        "CoCreateInstance error %#x\n", hr);
1663
1664     stream = create_stream(APE_data, sizeof(APE_data));
1665
1666     if (SUCCEEDED(hr))
1667     {
1668         hr = IUnknown_QueryInterface(reader, &IID_IWICPersistStream, (void **)&persist);
1669         ok(hr == S_OK, "QueryInterface error %#x\n", hr);
1670
1671         hr = IWICPersistStream_Load(persist, stream);
1672         ok(hr == S_OK, "Load error %#x\n", hr);
1673
1674         IWICPersistStream_Release(persist);
1675     }
1676
1677     if (SUCCEEDED(hr))
1678     {
1679         hr = IWICMetadataReader_GetCount(reader, &count);
1680         ok(hr == S_OK, "GetCount error %#x\n", hr);
1681         ok(count == sizeof(td)/sizeof(td[0]), "unexpected count %u\n", count);
1682
1683         compare_metadata(reader, td, count);
1684
1685         hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1686         ok(hr == S_OK, "GetMetadataFormat error %#x\n", hr);
1687         ok(IsEqualGUID(&format, &GUID_MetadataFormatAPE), "wrong format %s\n", debugstr_guid(&format));
1688
1689         hr = IWICMetadataReader_GetMetadataHandlerInfo(reader, &info);
1690         ok(hr == S_OK, "GetMetadataHandlerInfo error %#x\n", hr);
1691
1692         hr = IWICMetadataHandlerInfo_GetCLSID(info, &id);
1693         ok(hr == S_OK, "GetCLSID error %#x\n", hr);
1694         ok(IsEqualGUID(&id, &CLSID_WICAPEMetadataReader), "wrong CLSID %s\n", debugstr_guid(&id));
1695
1696         hr = IWICMetadataHandlerInfo_GetFriendlyName(info, 64, name, &dummy);
1697         ok(hr == S_OK, "GetFriendlyName error %#x\n", hr);
1698         ok(lstrcmpW(name, APE_name) == 0, "wrong APE reader name %s\n", wine_dbgstr_w(name));
1699
1700         IWICMetadataHandlerInfo_Release(info);
1701         IWICMetadataReader_Release(reader);
1702     }
1703
1704     IStream_Release(stream);
1705 }
1706
1707 static void test_metadata_GIF_comment(void)
1708 {
1709     static const WCHAR GIF_comment_name[] = {'C','o','m','m','e','n','t',' ','E','x','t','e','n','s','i','o','n',' ','R','e','a','d','e','r',0};
1710     static const char GIF_comment_data[] = { 0x21,0xfe,
1711                                              /*sub-block*/5,'H','e','l','l','o',
1712                                              /*sub-block*/1,' ',
1713                                              /*sub-block*/6,'W','o','r','l','d','!',
1714                                              /*terminator*/0 };
1715     static const struct test_data td[1] =
1716     {
1717         { VT_LPSTR, 0, 12, { 0 }, "Hello World!", { 'T','e','x','t','E','n','t','r','y',0 } }
1718     };
1719     HRESULT hr;
1720     IStream *stream;
1721     IWICPersistStream *persist;
1722     IWICMetadataReader *reader;
1723     IWICMetadataHandlerInfo *info;
1724     WCHAR name[64];
1725     UINT count, dummy;
1726     GUID format;
1727     CLSID id;
1728
1729     hr = CoCreateInstance(&CLSID_WICGifCommentMetadataReader, NULL, CLSCTX_INPROC_SERVER,
1730                           &IID_IWICMetadataReader, (void **)&reader);
1731 todo_wine
1732     ok(hr == S_OK || broken(hr == E_NOINTERFACE || hr == REGDB_E_CLASSNOTREG) /* before Win7 */,
1733        "CoCreateInstance error %#x\n", hr);
1734
1735     stream = create_stream(GIF_comment_data, sizeof(GIF_comment_data));
1736
1737     if (SUCCEEDED(hr))
1738     {
1739         hr = IUnknown_QueryInterface(reader, &IID_IWICPersistStream, (void **)&persist);
1740         ok(hr == S_OK, "QueryInterface error %#x\n", hr);
1741
1742         hr = IWICPersistStream_Load(persist, stream);
1743         ok(hr == S_OK, "Load error %#x\n", hr);
1744
1745         IWICPersistStream_Release(persist);
1746     }
1747
1748     if (SUCCEEDED(hr))
1749     {
1750         hr = IWICMetadataReader_GetCount(reader, &count);
1751         ok(hr == S_OK, "GetCount error %#x\n", hr);
1752         ok(count == sizeof(td)/sizeof(td[0]), "unexpected count %u\n", count);
1753
1754         compare_metadata(reader, td, count);
1755
1756         hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
1757         ok(hr == S_OK, "GetMetadataFormat error %#x\n", hr);
1758         ok(IsEqualGUID(&format, &GUID_MetadataFormatGifComment), "wrong format %s\n", debugstr_guid(&format));
1759
1760         hr = IWICMetadataReader_GetMetadataHandlerInfo(reader, &info);
1761         ok(hr == S_OK, "GetMetadataHandlerInfo error %#x\n", hr);
1762
1763         hr = IWICMetadataHandlerInfo_GetCLSID(info, &id);
1764         ok(hr == S_OK, "GetCLSID error %#x\n", hr);
1765         ok(IsEqualGUID(&id, &CLSID_WICGifCommentMetadataReader), "wrong CLSID %s\n", debugstr_guid(&id));
1766
1767         hr = IWICMetadataHandlerInfo_GetFriendlyName(info, 64, name, &dummy);
1768         ok(hr == S_OK, "GetFriendlyName error %#x\n", hr);
1769         ok(lstrcmpW(name, GIF_comment_name) == 0, "wrong APE reader name %s\n", wine_dbgstr_w(name));
1770
1771         IWICMetadataHandlerInfo_Release(info);
1772         IWICMetadataReader_Release(reader);
1773     }
1774
1775     IStream_Release(stream);
1776 }
1777
1778 START_TEST(metadata)
1779 {
1780     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1781
1782     test_metadata_unknown();
1783     test_metadata_tEXt();
1784     test_metadata_IFD();
1785     test_metadata_Exif();
1786     test_create_reader();
1787     test_metadata_png();
1788     test_metadata_gif();
1789     test_metadata_LSD();
1790     test_metadata_IMD();
1791     test_metadata_GCE();
1792     test_metadata_APE();
1793     test_metadata_GIF_comment();
1794
1795     CoUninitialize();
1796 }