1 /*
2  * Copyright 2012,2016 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 <math.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 
23 #define COBJMACROS
24 
25 #include "windef.h"
26 #include "wincodec.h"
27 #include "wine/test.h"
28 
29 #define IFD_BYTE 1
30 #define IFD_ASCII 2
31 #define IFD_SHORT 3
32 #define IFD_LONG 4
33 #define IFD_RATIONAL 5
34 #define IFD_SBYTE 6
35 #define IFD_UNDEFINED 7
36 #define IFD_SSHORT 8
37 #define IFD_SLONG 9
38 #define IFD_SRATIONAL 10
39 #define IFD_FLOAT 11
40 #define IFD_DOUBLE 12
41 
42 #include "pshpack2.h"
43 struct IFD_entry
44 {
45     SHORT id;
46     SHORT type;
47     ULONG count;
48     LONG  value;
49 };
50 
51 struct IFD_rational
52 {
53     LONG numerator;
54     LONG denominator;
55 };
56 
57 static const struct tiff_1bpp_data
58 {
59     USHORT byte_order;
60     USHORT version;
61     ULONG  dir_offset;
62     USHORT number_of_entries;
63     struct IFD_entry entry[13];
64     ULONG next_IFD;
65     struct IFD_rational res;
66     BYTE pixel_data[4];
67 } tiff_1bpp_data =
68 {
69 #ifdef WORDS_BIGENDIAN
70     'M' | 'M' << 8,
71 #else
72     'I' | 'I' << 8,
73 #endif
74     42,
75     FIELD_OFFSET(struct tiff_1bpp_data, number_of_entries),
76     13,
77     {
78         { 0xff, IFD_SHORT, 1, 0 }, /* SUBFILETYPE */
79         { 0x100, IFD_LONG, 1, 1 }, /* IMAGEWIDTH */
80         { 0x101, IFD_LONG, 1, 1 }, /* IMAGELENGTH */
81         { 0x102, IFD_SHORT, 1, 1 }, /* BITSPERSAMPLE */
82         { 0x103, IFD_SHORT, 1, 1 }, /* COMPRESSION: XP doesn't accept IFD_LONG here */
83         { 0x106, IFD_SHORT, 1, 1 }, /* PHOTOMETRIC */
84         { 0x111, IFD_LONG, 1, FIELD_OFFSET(struct tiff_1bpp_data, pixel_data) }, /* STRIPOFFSETS */
85         { 0x115, IFD_SHORT, 1, 1 }, /* SAMPLESPERPIXEL */
86         { 0x116, IFD_LONG, 1, 1 }, /* ROWSPERSTRIP */
87         { 0x117, IFD_LONG, 1, 1 }, /* STRIPBYTECOUNT */
88         { 0x11a, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_1bpp_data, res) }, /* XRESOLUTION */
89         { 0x11b, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_1bpp_data, res) }, /* YRESOLUTION */
90         { 0x128, IFD_SHORT, 1, 2 }, /* RESOLUTIONUNIT */
91     },
92     0,
93     { 900, 3 },
94     { 0x11, 0x22, 0x33, 0 }
95 };
96 
97 static const struct tiff_8bpp_alpha
98 {
99     USHORT byte_order;
100     USHORT version;
101     ULONG  dir_offset;
102     USHORT number_of_entries;
103     struct IFD_entry entry[15];
104     ULONG next_IFD;
105     struct IFD_rational res;
106     BYTE pixel_data[8];
107 } tiff_8bpp_alpha =
108 {
109 #ifdef WORDS_BIGENDIAN
110     'M' | 'M' << 8,
111 #else
112     'I' | 'I' << 8,
113 #endif
114     42,
115     FIELD_OFFSET(struct tiff_8bpp_alpha, number_of_entries),
116     15,
117     {
118         { 0xff, IFD_SHORT, 1, 0 }, /* SUBFILETYPE */
119         { 0x100, IFD_LONG, 1, 2 }, /* IMAGEWIDTH */
120         { 0x101, IFD_LONG, 1, 2 }, /* IMAGELENGTH */
121         { 0x102, IFD_SHORT, 2, MAKELONG(8, 8) }, /* BITSPERSAMPLE */
122         { 0x103, IFD_SHORT, 1, 1 }, /* COMPRESSION: XP doesn't accept IFD_LONG here */
123         { 0x106, IFD_SHORT, 1, 1 }, /* PHOTOMETRIC */
124         { 0x111, IFD_LONG, 1, FIELD_OFFSET(struct tiff_8bpp_alpha, pixel_data) }, /* STRIPOFFSETS */
125         { 0x115, IFD_SHORT, 1, 2 }, /* SAMPLESPERPIXEL */
126         { 0x116, IFD_LONG, 1, 2 }, /* ROWSPERSTRIP */
127         { 0x117, IFD_LONG, 1, 8 }, /* STRIPBYTECOUNT */
128         { 0x11a, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_8bpp_alpha, res) }, /* XRESOLUTION */
129         { 0x11b, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_8bpp_alpha, res) }, /* YRESOLUTION */
130         { 0x11c, IFD_SHORT, 1, 1 }, /* PLANARCONFIGURATION */
131         { 0x128, IFD_SHORT, 1, 2 }, /* RESOLUTIONUNIT */
132         { 0x152, IFD_SHORT, 1, 1 } /* EXTRASAMPLES: 1 - Associated alpha with pre-multiplied color */
133     },
134     0,
135     { 96, 1 },
136     { 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88 }
137 };
138 
139 static const struct tiff_8bpp_data
140 {
141     USHORT byte_order;
142     USHORT version;
143     ULONG  dir_offset;
144     USHORT number_of_entries;
145     struct IFD_entry entry[14];
146     ULONG next_IFD;
147     struct IFD_rational res;
148     short palette_data[3][256];
149     BYTE pixel_data[4];
150 } tiff_8bpp_data =
151 {
152 #ifdef WORDS_BIGENDIAN
153     'M' | 'M' << 8,
154 #else
155     'I' | 'I' << 8,
156 #endif
157     42,
158     FIELD_OFFSET(struct tiff_8bpp_data, number_of_entries),
159     14,
160     {
161         { 0xff, IFD_SHORT, 1, 0 }, /* SUBFILETYPE */
162         { 0x100, IFD_LONG, 1, 4 }, /* IMAGEWIDTH */
163         { 0x101, IFD_LONG, 1, 1 }, /* IMAGELENGTH */
164         { 0x102, IFD_SHORT, 1, 8 }, /* BITSPERSAMPLE: XP doesn't accept IFD_LONG here */
165         { 0x103, IFD_SHORT, 1, 1 }, /* COMPRESSION: XP doesn't accept IFD_LONG here */
166         { 0x106, IFD_SHORT, 1, 3 }, /* PHOTOMETRIC */
167         { 0x111, IFD_LONG, 1, FIELD_OFFSET(struct tiff_8bpp_data, pixel_data) }, /* STRIPOFFSETS */
168         { 0x115, IFD_SHORT, 1, 1 }, /* SAMPLESPERPIXEL */
169         { 0x116, IFD_LONG, 1, 1 }, /* ROWSPERSTRIP */
170         { 0x117, IFD_LONG, 1, 1 }, /* STRIPBYTECOUNT */
171         { 0x11a, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_8bpp_data, res) },
172         { 0x11b, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_8bpp_data, res) },
173         { 0x128, IFD_SHORT, 1, 2 }, /* RESOLUTIONUNIT */
174         { 0x140, IFD_SHORT, 256*3, FIELD_OFFSET(struct tiff_8bpp_data, palette_data) } /* COLORMAP */
175     },
176     0,
177     { 96, 1 },
178     { { 0 } },
179     { 0,1,2,3 }
180 };
181 
182 static const struct tiff_resolution_test_data
183 {
184     struct IFD_rational resx;
185     struct IFD_rational resy;
186     LONG resolution_unit;
187     double expected_dpi_x;
188     double expected_dpi_y;
189     /* if != 0: values for different behavior of some Windows versions */
190     double broken_dpi_x;
191     double broken_dpi_y;
192 } tiff_resolution_test_data[] =
193 {
194     { { 100, 1 }, {  50, 1 }, 0, 100.0,  50.0,     0,      0  }, /* invalid resolution unit */
195     { {  50, 1 }, { 100, 1 }, 0,  50.0, 100.0,     0,      0  },
196 
197     { { 100, 1 }, {  50, 1 }, 1, 100.0,  50.0,     0,      0  }, /* RESUNIT_NONE */
198     { {  50, 1 }, { 100, 1 }, 1,  50.0, 100.0,     0,      0  },
199 
200     { {  49, 1 }, {  49, 1 }, 2,  49.0,  49.0,     0,      0  }, /* same resolution for both X and Y */
201     { {  33, 1 }, {  55, 1 }, 2,  33.0,  55.0,     0,      0  }, /* different resolutions for X and Y */
202     { {  50, 2 }, {  66, 3 }, 2,  25.0,  22.0,     0,      0  }, /* denominator != 1 */
203 
204     { { 100, 1 }, { 200, 1 }, 3, 254.0, 508.0,     0,      0  }, /* unit = centimeters */
205 
206     /* XP and Server 2003 do not discard both resolution values if only one of them is invalid */
207     { {   0, 1 }, {  29, 1 }, 2,  96.0,  96.0,     0,   29.0  }, /* resolution 0 */
208     { {  58, 1 }, {  29, 0 }, 2,  96.0,  96.0,  58.0,      0  }, /* denominator 0 (division by zero) */
209 
210     /* XP and Server 2003 return 96 dots per centimeter (= 243.84 dpi) as fallback value */
211     { {   0, 1 }, { 100, 1 }, 3,  96.0,  96.0, 243.84, 254.0  }, /* resolution 0 and unit = centimeters */
212     { {  50, 1 }, {  72, 0 }, 3,  96.0,  96.0, 127.0,  243.84 }  /* denominator 0 and unit = centimeters */
213 };
214 
215 static struct tiff_resolution_image_data
216 {
217     USHORT byte_order;
218     USHORT version;
219     ULONG dir_offset;
220     USHORT number_of_entries;
221     struct IFD_entry entry[13];
222     ULONG next_IFD;
223     struct IFD_rational resx;
224     struct IFD_rational resy;
225     BYTE pixel_data[4];
226 } tiff_resolution_image_data =
227 {
228 #ifdef WORDS_BIGENDIAN
229     'M' | 'M' << 8,
230 #else
231     'I' | 'I' << 8,
232 #endif
233     42,
234     FIELD_OFFSET(struct tiff_resolution_image_data, number_of_entries),
235     13,
236     {
237         { 0xff, IFD_SHORT, 1, 0 }, /* SUBFILETYPE */
238         { 0x100, IFD_LONG, 1, 1 }, /* IMAGEWIDTH */
239         { 0x101, IFD_LONG, 1, 1 }, /* IMAGELENGTH */
240         { 0x102, IFD_SHORT, 1, 1 }, /* BITSPERSAMPLE */
241         { 0x103, IFD_SHORT, 1, 1 }, /* COMPRESSION: XP doesn't accept IFD_LONG here */
242         { 0x106, IFD_SHORT, 1, 1 }, /* PHOTOMETRIC */
243         { 0x111, IFD_LONG, 1, FIELD_OFFSET(struct tiff_resolution_image_data, pixel_data) }, /* STRIPOFFSETS */
244         { 0x115, IFD_SHORT, 1, 1 }, /* SAMPLESPERPIXEL */
245         { 0x116, IFD_LONG, 1, 1 }, /* ROWSPERSTRIP */
246         { 0x117, IFD_LONG, 1, 1 }, /* STRIPBYTECOUNT */
247         { 0x11a, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_resolution_image_data, resx) }, /* XRESOLUTION */
248         { 0x11b, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_resolution_image_data, resy) }, /* YRESOLUTION */
249         { 0x128, IFD_SHORT, 1, 1 }, /* RESOLUTIONUNIT -- value will be filled with test data */
250     },
251     0,
252     { 72, 1 }, /* value will be filled with test data */
253     { 72, 1 }, /* value will be filled with test data */
254     { 0x11, 0x22, 0x33, 0 }
255 };
256 #include "poppack.h"
257 
258 static IWICImagingFactory *factory;
259 
260 static IStream *create_stream(const void *data, int data_size)
261 {
262     HRESULT hr;
263     IStream *stream;
264     HGLOBAL hdata;
265     void *locked_data;
266 
267     hdata = GlobalAlloc(GMEM_MOVEABLE, data_size);
268     ok(hdata != 0, "GlobalAlloc failed\n");
269     if (!hdata) return NULL;
270 
271     locked_data = GlobalLock(hdata);
272     memcpy(locked_data, data, data_size);
273     GlobalUnlock(hdata);
274 
275     hr = CreateStreamOnHGlobal(hdata, TRUE, &stream);
276     ok(hr == S_OK, "CreateStreamOnHGlobal failed, hr=%x\n", hr);
277 
278     return stream;
279 }
280 
281 static HRESULT create_decoder(const void *image_data, UINT image_size, IWICBitmapDecoder **decoder)
282 {
283     HGLOBAL hmem;
284     BYTE *data;
285     HRESULT hr;
286     IStream *stream;
287     GUID format;
288     LONG refcount;
289 
290     *decoder = NULL;
291 
292     hmem = GlobalAlloc(0, image_size);
293     data = GlobalLock(hmem);
294     memcpy(data, image_data, image_size);
295     GlobalUnlock(hmem);
296 
297     hr = CreateStreamOnHGlobal(hmem, TRUE, &stream);
298     ok(hr == S_OK, "CreateStreamOnHGlobal error %#x\n", hr);
299 
300     hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, decoder);
301     if (hr == S_OK)
302     {
303         hr = IWICBitmapDecoder_GetContainerFormat(*decoder, &format);
304         ok(hr == S_OK, "GetContainerFormat error %#x\n", hr);
305         ok(IsEqualGUID(&format, &GUID_ContainerFormatTiff),
306            "wrong container format %s\n", wine_dbgstr_guid(&format));
307 
308         refcount = IStream_Release(stream);
309         ok(refcount > 0, "expected stream refcount > 0\n");
310     }
311 
312     return hr;
313 }
314 
315 static HRESULT get_pixelformat_info(const GUID *format, UINT *bpp, UINT *channels, BOOL *trasparency)
316 {
317     HRESULT hr;
318     IWICComponentInfo *info;
319     IWICPixelFormatInfo2 *formatinfo;
320 
321     hr = IWICImagingFactory_CreateComponentInfo(factory, format, &info);
322     ok(hr == S_OK, "CreateComponentInfo(%s) error %#x\n", wine_dbgstr_guid(format), hr);
323     if (hr == S_OK)
324     {
325         hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo2, (void **)&formatinfo);
326         if (hr == S_OK)
327         {
328             hr = IWICPixelFormatInfo2_SupportsTransparency(formatinfo, trasparency);
329             ok(hr == S_OK, "SupportsTransparency error %#x\n", hr);
330             IWICPixelFormatInfo2_Release(formatinfo);
331         }
332         hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo, (void **)&formatinfo);
333         if (hr == S_OK)
334         {
335             hr = IWICPixelFormatInfo2_GetBitsPerPixel(formatinfo, bpp);
336             ok(hr == S_OK, "GetBitsPerPixel error %#x\n", hr);
337             hr = IWICPixelFormatInfo2_GetChannelCount(formatinfo, channels);
338             ok(hr == S_OK, "GetChannelCount error %#x\n", hr);
339             IWICPixelFormatInfo2_Release(formatinfo);
340         }
341         IWICComponentInfo_Release(info);
342     }
343     return hr;
344 }
345 
346 static void dump_tiff(void *buf)
347 {
348     UINT count, i;
349     struct tiff_1bpp_data *tiff;
350     struct IFD_entry *tag;
351 
352     tiff = buf;
353     count = *(short *)((char *)tiff + tiff->dir_offset);
354     tag = (struct IFD_entry *)((char *)tiff + tiff->dir_offset + sizeof(short));
355 
356     for (i = 0; i < count; i++)
357     {
358         printf("tag %u: id %04x, type %04x, count %u, value %d",
359                 i, tag[i].id, tag[i].type, tag[i].count, tag[i].value);
360         if (tag[i].id == 0x102 && tag[i].count > 2)
361         {
362             short *bps = (short *)((char *)tiff + tag[i].value);
363             printf(" (%d,%d,%d,%d)\n", bps[0], bps[1], bps[2], bps[3]);
364         }
365         else
366             printf("\n");
367     }
368 }
369 
370 static void test_tiff_1bpp_palette(void)
371 {
372     HRESULT hr;
373     IWICBitmapDecoder *decoder;
374     IWICBitmapFrameDecode *frame;
375     IWICPalette *palette;
376     GUID format;
377 
378     hr = create_decoder(&tiff_1bpp_data, sizeof(tiff_1bpp_data), &decoder);
379     ok(hr == S_OK, "Failed to load TIFF image data %#x\n", hr);
380     if (hr != S_OK) return;
381 
382     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
383     ok(hr == S_OK, "GetFrame error %#x\n", hr);
384 
385     hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
386     ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
387     ok(IsEqualGUID(&format, &GUID_WICPixelFormatBlackWhite),
388        "got wrong format %s\n", wine_dbgstr_guid(&format));
389 
390     hr = IWICImagingFactory_CreatePalette(factory, &palette);
391     ok(hr == S_OK, "CreatePalette error %#x\n", hr);
392     hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
393     ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE,
394        "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %#x\n", hr);
395 
396     IWICPalette_Release(palette);
397     IWICBitmapFrameDecode_Release(frame);
398     IWICBitmapDecoder_Release(decoder);
399 }
400 
401 static void test_QueryCapability(void)
402 {
403     HRESULT hr;
404     IStream *stream;
405     IWICBitmapDecoder *decoder;
406     IWICBitmapFrameDecode *frame;
407     static const DWORD exp_caps = WICBitmapDecoderCapabilityCanDecodeAllImages |
408                                   WICBitmapDecoderCapabilityCanDecodeSomeImages |
409                                   WICBitmapDecoderCapabilityCanEnumerateMetadata;
410     static const DWORD exp_caps_xp = WICBitmapDecoderCapabilityCanDecodeAllImages |
411                                      WICBitmapDecoderCapabilityCanDecodeSomeImages;
412     DWORD capability;
413     LARGE_INTEGER pos;
414     ULARGE_INTEGER cur_pos;
415     UINT frame_count;
416 
417     stream = create_stream(&tiff_1bpp_data, sizeof(tiff_1bpp_data));
418     if (!stream) return;
419 
420     hr = IWICImagingFactory_CreateDecoder(factory, &GUID_ContainerFormatTiff, NULL, &decoder);
421     ok(hr == S_OK, "CreateDecoder error %#x\n", hr);
422     if (FAILED(hr)) return;
423 
424     frame_count = 0xdeadbeef;
425     hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
426     ok(hr == S_OK || broken(hr == E_POINTER) /* XP */, "GetFrameCount error %#x\n", hr);
427     ok(frame_count == 0, "expected 0, got %u\n", frame_count);
428 
429     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
430     ok(hr == WINCODEC_ERR_FRAMEMISSING || broken(hr == E_POINTER) /* XP */, "expected WINCODEC_ERR_FRAMEMISSING, got %#x\n", hr);
431 
432     pos.QuadPart = 4;
433     hr = IStream_Seek(stream, pos, SEEK_SET, NULL);
434     ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
435 
436     capability = 0xdeadbeef;
437     hr = IWICBitmapDecoder_QueryCapability(decoder, stream, &capability);
438     ok(hr == S_OK, "QueryCapability error %#x\n", hr);
439     ok(capability == exp_caps || capability == exp_caps_xp,
440        "expected %#x, got %#x\n", exp_caps, capability);
441 
442     frame_count = 0xdeadbeef;
443     hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
444     ok(hr == S_OK, "GetFrameCount error %#x\n", hr);
445     ok(frame_count == 1, "expected 1, got %u\n", frame_count);
446 
447     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
448     ok(hr == S_OK, "GetFrame error %#x\n", hr);
449     IWICBitmapFrameDecode_Release(frame);
450 
451     pos.QuadPart = 0;
452     hr = IStream_Seek(stream, pos, SEEK_CUR, &cur_pos);
453     ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
454     ok(cur_pos.QuadPart > 4 && cur_pos.QuadPart < sizeof(tiff_1bpp_data),
455        "current stream pos is at %x/%x\n", cur_pos.u.LowPart, cur_pos.u.HighPart);
456 
457     hr = IWICBitmapDecoder_QueryCapability(decoder, stream, &capability);
458     ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
459 
460     hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnDemand);
461     ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
462 
463     IWICBitmapDecoder_Release(decoder);
464 
465     hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, &decoder);
466 todo_wine
467     ok(hr == WINCODEC_ERR_COMPONENTNOTFOUND, "expected WINCODEC_ERR_COMPONENTNOTFOUND, got %#x\n", hr);
468 
469     if (SUCCEEDED(hr))
470         IWICBitmapDecoder_Release(decoder);
471 
472     pos.QuadPart = 0;
473     hr = IStream_Seek(stream, pos, SEEK_SET, NULL);
474     ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
475 
476     hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, &decoder);
477     ok(hr == S_OK, "CreateDecoderFromStream error %#x\n", hr);
478 
479     frame_count = 0xdeadbeef;
480     hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
481     ok(hr == S_OK, "GetFrameCount error %#x\n", hr);
482     ok(frame_count == 1, "expected 1, got %u\n", frame_count);
483 
484     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
485     ok(hr == S_OK, "GetFrame error %#x\n", hr);
486     IWICBitmapFrameDecode_Release(frame);
487 
488     hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnDemand);
489     ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
490 
491     hr = IWICBitmapDecoder_QueryCapability(decoder, stream, &capability);
492     ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
493 
494     IWICBitmapDecoder_Release(decoder);
495     IStream_Release(stream);
496 }
497 
498 static void test_tiff_8bpp_alpha(void)
499 {
500     HRESULT hr;
501     IWICBitmapDecoder *decoder;
502     IWICBitmapFrameDecode *frame;
503     UINT frame_count, width, height, i;
504     double dpi_x, dpi_y;
505     IWICPalette *palette;
506     GUID format;
507     WICRect rc;
508     BYTE data[16];
509     static const BYTE expected_data[16] = { 0x11,0x11,0x11,0x22,0x33,0x33,0x33,0x44,
510                                             0x55,0x55,0x55,0x66,0x77,0x77,0x77,0x88 };
511 
512     hr = create_decoder(&tiff_8bpp_alpha, sizeof(tiff_8bpp_alpha), &decoder);
513     ok(hr == S_OK, "Failed to load TIFF image data %#x\n", hr);
514     if (hr != S_OK) return;
515 
516     hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
517     ok(hr == S_OK, "GetFrameCount error %#x\n", hr);
518     ok(frame_count == 1, "expected 1, got %u\n", frame_count);
519 
520     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
521     ok(hr == S_OK, "GetFrame error %#x\n", hr);
522 
523     hr = IWICBitmapFrameDecode_GetSize(frame, &width, &height);
524     ok(hr == S_OK, "GetSize error %#x\n", hr);
525     ok(width == 2, "expected 2, got %u\n", width);
526     ok(height == 2, "expected 2, got %u\n", height);
527 
528     hr = IWICBitmapFrameDecode_GetResolution(frame, &dpi_x, &dpi_y);
529     ok(hr == S_OK, "GetResolution error %#x\n", hr);
530     ok(dpi_x == 96.0, "expected 96.0, got %f\n", dpi_x);
531     ok(dpi_y == 96.0, "expected 96.0, got %f\n", dpi_y);
532 
533     hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
534     ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
535     ok(IsEqualGUID(&format, &GUID_WICPixelFormat32bppPBGRA),
536        "got wrong format %s\n", wine_dbgstr_guid(&format));
537 
538     hr = IWICImagingFactory_CreatePalette(factory, &palette);
539     ok(hr == S_OK, "CreatePalette error %#x\n", hr);
540     hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
541     ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE,
542        "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %#x\n", hr);
543     IWICPalette_Release(palette);
544 
545     rc.X = 0;
546     rc.Y = 0;
547     rc.Width = 2;
548     rc.Height = 2;
549     hr = IWICBitmapFrameDecode_CopyPixels(frame, &rc, 8, sizeof(data), data);
550     ok(hr == S_OK, "CopyPixels error %#x\n", hr);
551 
552     for (i = 0; i < sizeof(data); i++)
553         ok(data[i] == expected_data[i], "%u: expected %02x, got %02x\n", i, expected_data[i], data[i]);
554 
555     IWICBitmapFrameDecode_Release(frame);
556     IWICBitmapDecoder_Release(decoder);
557 }
558 
559 static void generate_tiff_palette(void *buf, unsigned count)
560 {
561     unsigned short *r, *g, *b;
562     unsigned i;
563 
564     r = buf;
565     g = r + count;
566     b = g + count;
567 
568     r[0] = 0x11 * 257;
569     g[0] = 0x22 * 257;
570     b[0] = 0x33 * 257;
571     r[1] = 0x44 * 257;
572     g[1] = 0x55 * 257;
573     b[1] = 0x66 * 257;
574     r[2] = 0x77 * 257;
575     g[2] = 0x88 * 257;
576     b[2] = 0x99 * 257;
577     r[3] = 0xa1 * 257;
578     g[3] = 0xb5 * 257;
579     b[3] = 0xff * 257;
580 
581     for (i = 4; i < count; i++)
582     {
583         r[i] = i * 257;
584         g[i] = (i | 0x40) * 257;
585         b[i] = (i | 0x80) * 257;
586     }
587 }
588 
589 static void test_tiff_8bpp_palette(void)
590 {
591     char buf[sizeof(tiff_8bpp_data)];
592     HRESULT hr;
593     IWICBitmapDecoder *decoder;
594     IWICBitmapFrameDecode *frame;
595     IWICPalette *palette;
596     GUID format;
597     UINT count, ret;
598     WICColor color[256];
599 
600     memcpy(buf, &tiff_8bpp_data, sizeof(tiff_8bpp_data));
601     generate_tiff_palette(buf + FIELD_OFFSET(struct tiff_8bpp_data, palette_data), 256);
602 
603     hr = create_decoder(buf, sizeof(buf), &decoder);
604     ok(hr == S_OK, "Failed to load TIFF image data %#x\n", hr);
605     if (hr != S_OK) return;
606 
607     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
608     ok(hr == S_OK, "GetFrame error %#x\n", hr);
609 
610     hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
611     ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
612     ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
613        "expected GUID_WICPixelFormat8bppIndexed, got %s\n", wine_dbgstr_guid(&format));
614 
615     hr = IWICImagingFactory_CreatePalette(factory, &palette);
616     ok(hr == S_OK, "CreatePalette error %#x\n", hr);
617     hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
618     ok(hr == S_OK, "CopyPalette error %#x\n", hr);
619 
620     hr = IWICPalette_GetColorCount(palette, &count);
621     ok(hr == S_OK, "GetColorCount error %#x\n", hr);
622     ok(count == 256, "expected 256, got %u\n", count);
623 
624     hr = IWICPalette_GetColors(palette, 256, color, &ret);
625     ok(hr == S_OK, "GetColors error %#x\n", hr);
626     ok(ret == count, "expected %u, got %u\n", count, ret);
627     ok(color[0] == 0xff112233, "got %#x\n", color[0]);
628     ok(color[1] == 0xff445566, "got %#x\n", color[1]);
629     ok(color[2] == 0xff778899, "got %#x\n", color[2]);
630     ok(color[3] == 0xffa1b5ff, "got %#x\n", color[3]);
631 
632     IWICPalette_Release(palette);
633     IWICBitmapFrameDecode_Release(frame);
634     IWICBitmapDecoder_Release(decoder);
635 }
636 
637 static void test_tiff_resolution(void)
638 {
639     HRESULT hr;
640     IWICBitmapDecoder *decoder;
641     IWICBitmapFrameDecode *frame;
642     double dpi_x, dpi_y;
643     int i;
644 
645     for (i = 0; i < sizeof(tiff_resolution_test_data)/sizeof(tiff_resolution_test_data[0]); i++)
646     {
647         const struct tiff_resolution_test_data *test_data = &tiff_resolution_test_data[i];
648         tiff_resolution_image_data.resx = test_data->resx;
649         tiff_resolution_image_data.resy = test_data->resy;
650         tiff_resolution_image_data.entry[12].value = test_data->resolution_unit;
651 
652         hr = create_decoder(&tiff_resolution_image_data, sizeof(tiff_resolution_image_data), &decoder);
653         ok(hr == S_OK, "Failed to load TIFF image data %#x\n", hr);
654         if (hr != S_OK) return;
655 
656         hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
657         ok(hr == S_OK, "%d: GetFrame error %#x\n", i, hr);
658 
659         hr = IWICBitmapFrameDecode_GetResolution(frame, &dpi_x, &dpi_y);
660         ok(hr == S_OK, "%d: GetResolution error %#x\n", i, hr);
661 
662         if (test_data->broken_dpi_x != 0)
663         {
664             ok(fabs(dpi_x - test_data->expected_dpi_x) < 0.01 || broken(fabs(dpi_x - test_data->broken_dpi_x) < 0.01),
665                 "%d: x: expected %f or %f, got %f\n", i, test_data->expected_dpi_x, test_data->broken_dpi_x, dpi_x);
666         }
667         else
668         {
669             ok(fabs(dpi_x - test_data->expected_dpi_x) < 0.01,
670                 "%d: x: expected %f, got %f\n", i, test_data->expected_dpi_x, dpi_x);
671         }
672 
673         if (test_data->broken_dpi_y != 0)
674         {
675             ok(fabs(dpi_y - test_data->expected_dpi_y) < 0.01 || broken(fabs(dpi_y - test_data->broken_dpi_y) < 0.01),
676                 "%d: y: expected %f or %f, got %f\n", i, test_data->expected_dpi_y, test_data->broken_dpi_y, dpi_y);
677         }
678         else
679         {
680             ok(fabs(dpi_y - test_data->expected_dpi_y) < 0.01,
681                 "%d: y: expected %f, got %f\n", i, test_data->expected_dpi_y, dpi_y);
682         }
683 
684         IWICBitmapFrameDecode_Release(frame);
685         IWICBitmapDecoder_Release(decoder);
686     }
687 }
688 
689 #include "pshpack2.h"
690 static const struct tiff_1x1_data
691 {
692     USHORT byte_order;
693     USHORT version;
694     ULONG  dir_offset;
695     USHORT number_of_entries;
696     struct IFD_entry entry[12];
697     ULONG next_IFD;
698     struct IFD_rational res;
699     short palette_data[3][256];
700     short bps_data[4];
701     BYTE pixel_data[32];
702 } tiff_1x1_data =
703 {
704 #ifdef WORDS_BIGENDIAN
705     'M' | 'M' << 8,
706 #else
707     'I' | 'I' << 8,
708 #endif
709     42,
710     FIELD_OFFSET(struct tiff_1x1_data, number_of_entries),
711     12,
712     {
713         { 0xff, IFD_SHORT, 1, 0 }, /* SUBFILETYPE */
714         { 0x100, IFD_LONG, 1, 1 }, /* IMAGEWIDTH */
715         { 0x101, IFD_LONG, 1, 1 }, /* IMAGELENGTH */
716         { 0x102, IFD_SHORT, 3, FIELD_OFFSET(struct tiff_1x1_data, bps_data) }, /* BITSPERSAMPLE */
717         { 0x103, IFD_SHORT, 1, 1 }, /* COMPRESSION: XP doesn't accept IFD_LONG here */
718         { 0x106, IFD_SHORT, 1, 2 }, /* PHOTOMETRIC */
719         { 0x111, IFD_LONG, 1, FIELD_OFFSET(struct tiff_1x1_data, pixel_data) }, /* STRIPOFFSETS */
720         { 0x115, IFD_SHORT, 1, 3 }, /* SAMPLESPERPIXEL */
721         { 0x11a, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_1x1_data, res) },
722         { 0x11b, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_1x1_data, res) },
723         { 0x128, IFD_SHORT, 1, 2 }, /* RESOLUTIONUNIT */
724         { 0x140, IFD_SHORT, 256*3, FIELD_OFFSET(struct tiff_1x1_data, palette_data) } /* COLORMAP */
725     },
726     0,
727     { 96, 1 },
728     { { 0 } },
729     { 8,8,8,0 },
730     { 1,0,2,3,4,5,6,7,8,9,0,1,2,3,4,5 }
731 };
732 #include "poppack.h"
733 
734 static UINT width_bytes(UINT width, UINT bpp)
735 {
736     return (width * bpp + 7) / 8;
737 }
738 
739 static void test_color_formats(void)
740 {
741     struct bitmap_data
742     {
743         UINT bpp;
744         UINT width;
745         UINT height;
746         const WICPixelFormatGUID *format;
747         const BYTE *bits;
748     };
749     static const BYTE bits_1bpsBGR[] = { 0,255,0,255,0,255,255,255,0,0,0,255,255,0,0,0,255,255,255,255,255,0,0,0,0,255,0,255,0,255 };
750     static const struct bitmap_data data_1bpsBGR =
751     {
752         24, 10, 2, &GUID_WICPixelFormat24bppBGR, bits_1bpsBGR
753     };
754     static const BYTE bits_4bpsBGR[] = { 204,85,85,136,187,51,0,85,85,85,0,68,0,102,0,136,0,119,0,153,0 };
755     static const struct bitmap_data data_4bpsBGR =
756     {
757         24, 5, 2, &GUID_WICPixelFormat24bppBGR, bits_4bpsBGR
758     };
759     static const BYTE bits_8bpsBGR[] = { 2,0,1,5,4,3,8,7,6 };
760     static const struct bitmap_data data_8bpsBGR =
761     {
762         24, 3, 1, &GUID_WICPixelFormat24bppBGR, bits_8bpsBGR
763     };
764     static const BYTE bits_48bppRGB[] = { 1,0,2,3,4,5,6,7,8,9,0,1 };
765     static const struct bitmap_data data_48bppRGB =
766     {
767         48, 2, 1, &GUID_WICPixelFormat48bppRGB, bits_48bppRGB
768     };
769     static const BYTE bits_1bpsBGRA[] = { 0,255,0,255,0,255,0,255,0,255,255,0,255,0,0,255,255,0,255,255,0,0,255,0,0,255,0,255,0,255,0,255,0,0,0,0,0,255,0,0 };
770     static const struct bitmap_data data_1bpsBGRA =
771     {
772         32, 5, 2, &GUID_WICPixelFormat32bppBGRA, bits_1bpsBGRA
773     };
774     static const BYTE bits_4bpsBGRA[] = { 204,85,85,51,85,136,187,85,0,68,0,85,0,102,0,119,0,136,0,153,0,0,0,17,0,34,0,51 };
775     static const struct bitmap_data data_4bpsBGRA =
776     {
777         32, 5, 2, &GUID_WICPixelFormat32bppBGRA, bits_4bpsBGRA
778     };
779     static const BYTE bits_8bpsBGRA[] = { 2,0,1,3,6,5,4,7,0,9,8,1,4,3,2,5 };
780     static const struct bitmap_data data_8bpsBGRA =
781     {
782         32, 4, 1, &GUID_WICPixelFormat32bppBGRA, bits_8bpsBGRA
783     };
784     static const BYTE bits_64bppRGBA[] = { 1,0,2,3,4,5,6,7,8,9,0,1,2,3,4,5 };
785     static const struct bitmap_data data_64bppRGBA =
786     {
787         64, 2, 1, &GUID_WICPixelFormat64bppRGBA, bits_64bppRGBA
788     };
789     static const BYTE bits_BlackWhite[] = { 85,195,184,85 };
790     static const struct bitmap_data data_BlackWhite =
791     {
792         1, 30, 1, &GUID_WICPixelFormatBlackWhite, bits_BlackWhite
793     };
794     static const BYTE bits_BlackWhite_xp[] = { 85,195,184,84 };
795     static const struct bitmap_data data_BlackWhite_xp =
796     {
797         1, 30, 1, &GUID_WICPixelFormatBlackWhite, bits_BlackWhite_xp
798     };
799     static const BYTE bits_4bppGray[] = { 85,195,184,85 };
800     static const struct bitmap_data data_4bppGray =
801     {
802         4, 7, 1, &GUID_WICPixelFormat4bppGray, bits_4bppGray
803     };
804     static const BYTE bits_4bppGray_xp[] = { 85,195,184,80 };
805     static const struct bitmap_data data_4bppGray_xp =
806     {
807         4, 7, 1, &GUID_WICPixelFormat4bppGray, bits_4bppGray_xp
808     };
809     static const BYTE bits_8bppGray[] = { 1,0,2,3,4,5,6,7,8,9 };
810     static const struct bitmap_data data_8bppGray =
811     {
812         8, 10, 1, &GUID_WICPixelFormat8bppGray, bits_8bppGray
813     };
814     static const BYTE bits_16bppGray[] = { 1,0,2,3,4,5 };
815     static const struct bitmap_data data_16bppGray =
816     {
817         16, 3, 1, &GUID_WICPixelFormat16bppGray, bits_16bppGray
818     };
819     static const BYTE bits_32bppGrayFloat[] = { 1,0,2,3,4,5,6,7,8,9,0,1 };
820     static const struct bitmap_data data_32bppGrayFloat =
821     {
822         32, 3, 1, &GUID_WICPixelFormat32bppGrayFloat, bits_32bppGrayFloat
823     };
824 #if 0 /* FIXME */
825     static const BYTE bits_96bpp3Channels[] = { 0 };
826     static const struct bitmap_data data_96bpp3Channels =
827     {
828         64, 1, 1, &GUID_WICPixelFormat96bpp3Channels, bits_96bpp3Channels
829     };
830 #endif
831     static const BYTE bits_128bppRGBAFloat[] = { 1,0,2,3,4,5,6,7,8,9,0,1,2,3,4,5 };
832     static const struct bitmap_data data_128bppRGBAFloat =
833     {
834         128, 1, 1, &GUID_WICPixelFormat128bppRGBAFloat, bits_128bppRGBAFloat
835     };
836     static const BYTE bits_1bppIndexed[] = { 85,195,184,85 };
837     static const struct bitmap_data data_1bppIndexed =
838     {
839         1, 32, 1, &GUID_WICPixelFormat1bppIndexed, bits_1bppIndexed
840     };
841     static const BYTE bits_4bppIndexed[] = { 85,195,184,85 };
842     static const struct bitmap_data data_4bppIndexed =
843     {
844         4, 7, 1, &GUID_WICPixelFormat4bppIndexed, bits_4bppIndexed
845     };
846     static const BYTE bits_4bppIndexed_xp[] = { 85,195,184,80 };
847     static const struct bitmap_data data_4bppIndexed_xp =
848     {
849         4, 7, 1, &GUID_WICPixelFormat4bppIndexed, bits_4bppIndexed_xp
850     };
851     static const BYTE bits_8bppIndexed[] = { 1,0,2,3,4,5,6,7,8,9 };
852     static const struct bitmap_data data_8bppIndexed =
853     {
854         8, 3, 1, &GUID_WICPixelFormat8bppIndexed, bits_8bppIndexed
855     };
856     static const BYTE bits_32bppCMYK[] = { 1,0,2,3,4,5,6,7,8,9,0,1 };
857     static const struct bitmap_data data_32bppCMYK =
858     {
859         32, 3, 1, &GUID_WICPixelFormat32bppCMYK, bits_32bppCMYK
860     };
861     static const BYTE bits_64bppCMYK[] = { 1,0,2,3,4,5,6,7,8,9,0,1,2,3,4,5 };
862     static const struct bitmap_data data_64bppCMYK =
863     {
864         64, 2, 1, &GUID_WICPixelFormat64bppCMYK, bits_64bppCMYK
865     };
866     static const struct
867     {
868         int photometric; /* PhotometricInterpretation */
869         int samples; /* SamplesPerPixel */
870         int bps; /* BitsPerSample */
871         const struct bitmap_data *data;
872         const struct bitmap_data *alt_data;
873     } td[] =
874     {
875         /* 2 - RGB */
876         { 2, 3, 1, &data_1bpsBGR },
877         { 2, 3, 4, &data_4bpsBGR },
878         { 2, 3, 8, &data_8bpsBGR },
879         { 2, 3, 16, &data_48bppRGB },
880         { 2, 3, 24, NULL },
881 #if 0 /* FIXME */
882         { 2, 3, 32, &data_96bpp3Channels },
883 #endif
884         { 2, 4, 1, &data_1bpsBGRA },
885         { 2, 4, 4, &data_4bpsBGRA },
886         { 2, 4, 8, &data_8bpsBGRA },
887         { 2, 4, 16, &data_64bppRGBA },
888         { 2, 4, 24, NULL },
889         { 2, 4, 32, &data_128bppRGBAFloat },
890         /* 1 - BlackIsZero (Bilevel) */
891         { 1, 1, 1, &data_BlackWhite, &data_BlackWhite_xp },
892         { 1, 1, 4, &data_4bppGray, &data_4bppGray_xp },
893         { 1, 1, 8, &data_8bppGray },
894         { 1, 1, 16, &data_16bppGray },
895         { 1, 1, 24, NULL },
896         { 1, 1, 32, &data_32bppGrayFloat },
897         /* 3 - Palette Color */
898         { 3, 1, 1, &data_1bppIndexed },
899         { 3, 1, 4, &data_4bppIndexed, &data_4bppIndexed_xp },
900         { 3, 1, 8, &data_8bppIndexed },
901 #if 0 /* FIXME: for some reason libtiff replaces photometric 3 by 1 for bps > 8 */
902         { 3, 1, 16, &data_8bppIndexed },
903         { 3, 1, 24, &data_8bppIndexed },
904         { 3, 1, 32, &data_8bppIndexed },
905 #endif
906         /* 5 - Separated */
907         { 5, 4, 1, NULL },
908         { 5, 4, 4, NULL },
909         { 5, 4, 8, &data_32bppCMYK },
910         { 5, 4, 16, &data_64bppCMYK },
911         { 5, 4, 24, NULL },
912         { 5, 4, 32, NULL },
913     };
914     BYTE buf[sizeof(tiff_1x1_data)];
915     BYTE pixels[256];
916     HRESULT hr;
917     IWICBitmapDecoder *decoder;
918     IWICBitmapFrameDecode *frame;
919     GUID format;
920     UINT count, i, bpp, channels, ret;
921     BOOL trasparency;
922     struct IFD_entry *tag, *tag_photo = NULL, *tag_bps = NULL, *tag_samples = NULL, *tag_colormap = NULL;
923     struct IFD_entry *tag_width = NULL, *tag_height = NULL;
924     short *bps;
925 
926     memcpy(buf, &tiff_1x1_data, sizeof(tiff_1x1_data));
927     generate_tiff_palette(buf + FIELD_OFFSET(struct tiff_1x1_data, palette_data), 256);
928 
929     count = *(short *)(buf + tiff_1x1_data.dir_offset);
930     tag = (struct IFD_entry *)(buf + tiff_1x1_data.dir_offset + sizeof(short));
931 
932     /* verify the TIFF structure */
933     for (i = 0; i < count; i++)
934     {
935         if (tag[i].id == 0x100) /* ImageWidth */
936             tag_width = &tag[i];
937         else if (tag[i].id == 0x101) /* ImageLength */
938             tag_height = &tag[i];
939         else if (tag[i].id == 0x102) /* BitsPerSample */
940             tag_bps = &tag[i];
941         else if (tag[i].id == 0x106) /* PhotometricInterpretation */
942             tag_photo = &tag[i];
943         else if (tag[i].id == 0x115) /* SamplesPerPixel */
944             tag_samples = &tag[i];
945         else if (tag[i].id == 0x140) /* ColorMap */
946             tag_colormap = &tag[i];
947     }
948 
949     ok(tag_bps && tag_photo && tag_samples && tag_colormap, "tag 0x102,0x106,0x115 or 0x140 is missing\n");
950     if (!tag_bps || !tag_photo || !tag_samples || !tag_colormap) return;
951 
952     ok(tag_bps->type == IFD_SHORT, "tag 0x102 should have type IFD_SHORT\n");
953     bps = (short *)(buf + tag_bps->value);
954     ok(bps[0] == 8 && bps[1] == 8 && bps[2] == 8 && bps[3] == 0,
955        "expected bps 8,8,8,0 got %d,%d,%d,%d\n", bps[0], bps[1], bps[2], bps[3]);
956 
957     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
958     {
959         if (td[i].data)
960         {
961             bpp = td[i].samples * td[i].bps;
962             if (winetest_debug > 1)
963                 trace("samples %u, bps %u, bpp %u, width %u => width_bytes %u\n", td[i].samples, td[i].bps, bpp,
964                       td[i].data->width, width_bytes(td[i].data->width, bpp));
965             tag_width->value = td[i].data->width;
966             tag_height->value = td[i].data->height;
967         }
968         else
969         {
970             tag_width->value = 1;
971             tag_height->value = 1;
972         }
973 
974         tag_colormap->count = (1 << td[i].bps) * 3;
975 
976         if (td[i].bps < 8)
977         {
978             buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data)] = 0x55;
979             buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 1] = 0xc3;
980             buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 2] = 0xb8;
981             buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 3] = 0x55;
982         }
983         else
984         {
985             buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data)] = 1;
986             buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 1] = 0;
987             buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 2] = 2;
988             buf[FIELD_OFFSET(struct tiff_1x1_data, pixel_data) + 3] = 3;
989         }
990 
991         tag_photo->value = td[i].photometric;
992         tag_bps->count = td[i].samples;
993         tag_samples->value = td[i].samples;
994 
995         if (td[i].samples == 1)
996             tag_bps->value = td[i].bps;
997         else if (td[i].samples == 2)
998             tag_bps->value = MAKELONG(td[i].bps, td[i].bps);
999         else if (td[i].samples == 3)
1000         {
1001             tag_bps->value = (BYTE *)bps - buf;
1002             bps[0] = bps[1] = bps[2] = td[i].bps;
1003         }
1004         else if (td[i].samples == 4)
1005         {
1006             tag_bps->value = (BYTE *)bps - buf;
1007             bps[0] = bps[1] = bps[2] = bps[3] = td[i].bps;
1008         }
1009         else
1010         {
1011             ok(0, "%u: unsupported samples count %d\n", i, td[i].samples);
1012             continue;
1013         }
1014 
1015         hr = create_decoder(buf, sizeof(buf), &decoder);
1016         if (!td[i].data)
1017         {
1018             ok(hr == WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT || hr == WINCODEC_ERR_COMPONENTNOTFOUND /* win8+ */ || WINCODEC_ERR_BADIMAGE /* XP */,
1019                "%u: (%d,%d,%d) wrong error %#x\n", i, td[i].photometric, td[i].samples, td[i].bps, hr);
1020             if (hr == S_OK)
1021             {
1022                 IWICBitmapDecoder_Release(decoder);
1023                 dump_tiff(buf);
1024             }
1025             continue;
1026         }
1027         else
1028             ok(hr == S_OK || broken(hr == WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT || hr == WINCODEC_ERR_BADIMAGE) /* XP */,
1029                "%u: failed to load TIFF image data (%d,%d,%d) %#x\n",
1030                i, td[i].photometric, td[i].samples, td[i].bps, hr);
1031         if (hr != S_OK) continue;
1032 
1033         hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
1034         ok(hr == S_OK, "%u: GetFrame error %#x\n", i, hr);
1035 
1036         hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
1037         ok(hr == S_OK, "%u: GetPixelFormat error %#x\n", i, hr);
1038         ok(IsEqualGUID(&format, td[i].data->format),
1039            "%u (%d,%d,%d): expected %s, got %s\n",
1040             i, td[i].photometric, td[i].samples, td[i].bps,
1041             wine_dbgstr_guid(td[i].data->format), wine_dbgstr_guid(&format));
1042 
1043         trasparency = (td[i].photometric == 2 && td[i].samples == 4); /* for XP */
1044         hr = get_pixelformat_info(&format, &bpp, &channels, &trasparency);
1045         ok(hr == S_OK, "%u: get_pixelformat_bpp error %#x\n", i, hr);
1046         ok(bpp == td[i].data->bpp, "%u: expected %u, got %u\n", i, td[i].data->bpp, bpp);
1047         ok(channels == td[i].samples, "%u: expected %u, got %u\n", i, td[i].samples, channels);
1048         ok(trasparency == (td[i].photometric == 2 && td[i].samples == 4), "%u: got %u\n", i, trasparency);
1049 
1050         memset(pixels, 0, sizeof(pixels));
1051         hr = IWICBitmapFrameDecode_CopyPixels(frame, NULL, width_bytes(td[i].data->width, bpp), sizeof(pixels), pixels);
1052         ok(hr == S_OK, "%u: CopyPixels error %#x\n", i, hr);
1053         ret = memcmp(pixels, td[i].data->bits, width_bytes(td[i].data->width, bpp));
1054         if (ret && td[i].alt_data)
1055             ret = memcmp(pixels, td[i].alt_data->bits, width_bytes(td[i].data->width, bpp));
1056         ok(ret == 0, "%u: (%d,%d,%d) wrong pixel data\n", i, td[i].photometric, td[i].samples, td[i].bps);
1057         if (ret)
1058         {
1059             UINT j, n = width_bytes(td[i].data->width, bpp);
1060             for (j = 0; j < n; j++)
1061                 printf("%u%s", pixels[j], (j + 1) < n ? "," : "\n");
1062         }
1063 
1064         IWICBitmapFrameDecode_Release(frame);
1065         IWICBitmapDecoder_Release(decoder);
1066     }
1067 }
1068 
1069 START_TEST(tiffformat)
1070 {
1071     HRESULT hr;
1072 
1073     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1074 
1075     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
1076                           &IID_IWICImagingFactory, (void **)&factory);
1077     ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
1078     if (FAILED(hr)) return;
1079 
1080     test_color_formats();
1081     test_tiff_1bpp_palette();
1082     test_tiff_8bpp_palette();
1083     test_QueryCapability();
1084     test_tiff_8bpp_alpha();
1085     test_tiff_resolution();
1086 
1087     IWICImagingFactory_Release(factory);
1088     CoUninitialize();
1089 }
1090