windowscodecs/tests: Handle failure to create TIFF decoder.
[wine/multimedia.git] / dlls / windowscodecs / tests / tiffformat.c
blobe4a4b66ea8436408c807a48a9045932fbb8d6d9b
1 /*
2 * Copyright 2012 Dmitry Timoshkov
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <stdio.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "wincodec.h"
26 #include "wine/test.h"
28 #define IFD_BYTE 1
29 #define IFD_ASCII 2
30 #define IFD_SHORT 3
31 #define IFD_LONG 4
32 #define IFD_RATIONAL 5
33 #define IFD_SBYTE 6
34 #define IFD_UNDEFINED 7
35 #define IFD_SSHORT 8
36 #define IFD_SLONG 9
37 #define IFD_SRATIONAL 10
38 #define IFD_FLOAT 11
39 #define IFD_DOUBLE 12
41 #include "pshpack2.h"
42 struct IFD_entry
44 SHORT id;
45 SHORT type;
46 ULONG count;
47 LONG value;
50 struct IFD_rational
52 LONG numerator;
53 LONG denominator;
56 static const struct tiff_1bpp_data
58 USHORT byte_order;
59 USHORT version;
60 ULONG dir_offset;
61 USHORT number_of_entries;
62 struct IFD_entry entry[13];
63 ULONG next_IFD;
64 struct IFD_rational res;
65 BYTE pixel_data[4];
66 } tiff_1bpp_data =
68 #ifdef WORDS_BIGENDIAN
69 'M' | 'M' << 8,
70 #else
71 'I' | 'I' << 8,
72 #endif
73 42,
74 FIELD_OFFSET(struct tiff_1bpp_data, number_of_entries),
75 13,
77 { 0xff, IFD_SHORT, 1, 0 }, /* SUBFILETYPE */
78 { 0x100, IFD_LONG, 1, 1 }, /* IMAGEWIDTH */
79 { 0x101, IFD_LONG, 1, 1 }, /* IMAGELENGTH */
80 { 0x102, IFD_SHORT, 1, 1 }, /* BITSPERSAMPLE */
81 { 0x103, IFD_SHORT, 1, 1 }, /* COMPRESSION: XP doesn't accept IFD_LONG here */
82 { 0x106, IFD_SHORT, 1, 1 }, /* PHOTOMETRIC */
83 { 0x111, IFD_LONG, 1, FIELD_OFFSET(struct tiff_1bpp_data, pixel_data) }, /* STRIPOFFSETS */
84 { 0x115, IFD_SHORT, 1, 1 }, /* SAMPLESPERPIXEL */
85 { 0x116, IFD_LONG, 1, 1 }, /* ROWSPERSTRIP */
86 { 0x117, IFD_LONG, 1, 1 }, /* STRIPBYTECOUNT */
87 { 0x11a, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_1bpp_data, res) },
88 { 0x11b, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_1bpp_data, res) },
89 { 0x128, IFD_SHORT, 1, 2 }, /* RESOLUTIONUNIT */
92 { 900, 3 },
93 { 0x11, 0x22, 0x33, 0 }
96 static const struct tiff_8bpp_alpha
98 USHORT byte_order;
99 USHORT version;
100 ULONG dir_offset;
101 USHORT number_of_entries;
102 struct IFD_entry entry[15];
103 ULONG next_IFD;
104 struct IFD_rational res;
105 BYTE pixel_data[8];
106 } tiff_8bpp_alpha =
108 #ifdef WORDS_BIGENDIAN
109 'M' | 'M' << 8,
110 #else
111 'I' | 'I' << 8,
112 #endif
114 FIELD_OFFSET(struct tiff_8bpp_alpha, number_of_entries),
117 { 0xff, IFD_SHORT, 1, 0 }, /* SUBFILETYPE */
118 { 0x100, IFD_LONG, 1, 2 }, /* IMAGEWIDTH */
119 { 0x101, IFD_LONG, 1, 2 }, /* IMAGELENGTH */
120 { 0x102, IFD_SHORT, 2, MAKELONG(8, 8) }, /* BITSPERSAMPLE */
121 { 0x103, IFD_SHORT, 1, 1 }, /* COMPRESSION: XP doesn't accept IFD_LONG here */
122 { 0x106, IFD_SHORT, 1, 1 }, /* PHOTOMETRIC */
123 { 0x111, IFD_LONG, 1, FIELD_OFFSET(struct tiff_8bpp_alpha, pixel_data) }, /* STRIPOFFSETS */
124 { 0x115, IFD_SHORT, 1, 2 }, /* SAMPLESPERPIXEL */
125 { 0x116, IFD_LONG, 1, 2 }, /* ROWSPERSTRIP */
126 { 0x117, IFD_LONG, 1, 8 }, /* STRIPBYTECOUNT */
127 { 0x11a, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_8bpp_alpha, res) },
128 { 0x11b, IFD_RATIONAL, 1, FIELD_OFFSET(struct tiff_8bpp_alpha, res) },
129 { 0x11c, IFD_SHORT, 1, 1 }, /* PLANARCONFIGURATION */
130 { 0x128, IFD_SHORT, 1, 2 }, /* RESOLUTIONUNIT */
131 { 0x152, IFD_SHORT, 1, 1 } /* EXTRASAMPLES: 1 - Associated alpha with pre-multiplied color */
134 { 96, 1 },
135 { 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88 }
137 #include "poppack.h"
139 static IWICImagingFactory *factory;
141 static IStream *create_stream(const void *data, int data_size)
143 HRESULT hr;
144 IStream *stream;
145 HGLOBAL hdata;
146 void *locked_data;
148 hdata = GlobalAlloc(GMEM_MOVEABLE, data_size);
149 ok(hdata != 0, "GlobalAlloc failed\n");
150 if (!hdata) return NULL;
152 locked_data = GlobalLock(hdata);
153 memcpy(locked_data, data, data_size);
154 GlobalUnlock(hdata);
156 hr = CreateStreamOnHGlobal(hdata, TRUE, &stream);
157 ok(hr == S_OK, "CreateStreamOnHGlobal failed, hr=%x\n", hr);
159 return stream;
162 static IWICBitmapDecoder *create_decoder(const void *image_data, UINT image_size)
164 HRESULT hr;
165 IStream *stream;
166 IWICBitmapDecoder *decoder = NULL;
167 GUID guid;
169 stream = create_stream(image_data, image_size);
171 hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, &decoder);
172 ok(hr == S_OK, "CreateDecoderFromStream error %#x\n", hr);
173 if (FAILED(hr)) return NULL;
175 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guid);
176 ok(hr == S_OK, "GetContainerFormat error %#x\n", hr);
177 ok(IsEqualGUID(&guid, &GUID_ContainerFormatTiff), "container format is not TIFF\n");
179 IStream_Release(stream);
181 return decoder;
184 static void test_tiff_palette(void)
186 HRESULT hr;
187 IWICBitmapDecoder *decoder;
188 IWICBitmapFrameDecode *frame;
189 IWICPalette *palette;
190 GUID format;
192 decoder = create_decoder(&tiff_1bpp_data, sizeof(tiff_1bpp_data));
193 ok(decoder != 0, "Failed to load TIFF image data\n");
194 if (!decoder) return;
196 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
197 ok(hr == S_OK, "GetFrame error %#x\n", hr);
199 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
200 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
201 ok(IsEqualGUID(&format, &GUID_WICPixelFormatBlackWhite),
202 "got wrong format %s\n", wine_dbgstr_guid(&format));
204 hr = IWICImagingFactory_CreatePalette(factory, &palette);
205 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
206 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
207 ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE,
208 "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %#x\n", hr);
210 IWICPalette_Release(palette);
211 IWICBitmapFrameDecode_Release(frame);
212 IWICBitmapDecoder_Release(decoder);
215 static void test_QueryCapability(void)
217 HRESULT hr;
218 IStream *stream;
219 IWICBitmapDecoder *decoder;
220 IWICBitmapFrameDecode *frame;
221 static const DWORD exp_caps = WICBitmapDecoderCapabilityCanDecodeAllImages |
222 WICBitmapDecoderCapabilityCanDecodeSomeImages |
223 WICBitmapDecoderCapabilityCanEnumerateMetadata;
224 static const DWORD exp_caps_xp = WICBitmapDecoderCapabilityCanDecodeAllImages |
225 WICBitmapDecoderCapabilityCanDecodeSomeImages;
226 DWORD capability;
227 LARGE_INTEGER pos;
228 ULARGE_INTEGER cur_pos;
229 UINT frame_count;
231 stream = create_stream(&tiff_1bpp_data, sizeof(tiff_1bpp_data));
232 if (!stream) return;
234 hr = IWICImagingFactory_CreateDecoder(factory, &GUID_ContainerFormatTiff, NULL, &decoder);
235 ok(hr == S_OK, "CreateDecoder error %#x\n", hr);
236 if (FAILED(hr)) return;
238 frame_count = 0xdeadbeef;
239 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
240 ok(hr == S_OK || broken(hr == E_POINTER) /* XP */, "GetFrameCount error %#x\n", hr);
241 ok(frame_count == 0, "expected 0, got %u\n", frame_count);
243 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
244 ok(hr == WINCODEC_ERR_FRAMEMISSING || broken(hr == E_POINTER) /* XP */, "expected WINCODEC_ERR_FRAMEMISSING, got %#x\n", hr);
246 pos.QuadPart = 4;
247 hr = IStream_Seek(stream, pos, SEEK_SET, NULL);
248 ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
250 capability = 0xdeadbeef;
251 hr = IWICBitmapDecoder_QueryCapability(decoder, stream, &capability);
252 ok(hr == S_OK, "QueryCapability error %#x\n", hr);
253 ok(capability == exp_caps || capability == exp_caps_xp,
254 "expected %#x, got %#x\n", exp_caps, capability);
256 frame_count = 0xdeadbeef;
257 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
258 ok(hr == S_OK, "GetFrameCount error %#x\n", hr);
259 ok(frame_count == 1, "expected 1, got %u\n", frame_count);
261 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
262 ok(hr == S_OK, "GetFrame error %#x\n", hr);
263 IWICBitmapFrameDecode_Release(frame);
265 pos.QuadPart = 0;
266 hr = IStream_Seek(stream, pos, SEEK_CUR, &cur_pos);
267 ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
268 ok(cur_pos.QuadPart > 4 && cur_pos.QuadPart < sizeof(tiff_1bpp_data),
269 "current stream pos is at %x/%x\n", cur_pos.u.LowPart, cur_pos.u.HighPart);
271 hr = IWICBitmapDecoder_QueryCapability(decoder, stream, &capability);
272 ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
274 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnDemand);
275 ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
277 IWICBitmapDecoder_Release(decoder);
279 hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, &decoder);
280 todo_wine
281 ok(hr == WINCODEC_ERR_COMPONENTNOTFOUND, "expected WINCODEC_ERR_COMPONENTNOTFOUND, got %#x\n", hr);
283 if (SUCCEEDED(hr))
284 IWICBitmapDecoder_Release(decoder);
286 pos.QuadPart = 0;
287 hr = IStream_Seek(stream, pos, SEEK_SET, NULL);
288 ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
290 hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, &decoder);
291 ok(hr == S_OK, "CreateDecoderFromStream error %#x\n", hr);
293 frame_count = 0xdeadbeef;
294 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
295 ok(hr == S_OK, "GetFrameCount error %#x\n", hr);
296 ok(frame_count == 1, "expected 1, got %u\n", frame_count);
298 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
299 ok(hr == S_OK, "GetFrame error %#x\n", hr);
300 IWICBitmapFrameDecode_Release(frame);
302 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnDemand);
303 ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
305 hr = IWICBitmapDecoder_QueryCapability(decoder, stream, &capability);
306 ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
308 IWICBitmapDecoder_Release(decoder);
309 IStream_Release(stream);
312 static void test_tiff_8bpp_alpha(void)
314 HRESULT hr;
315 IWICBitmapDecoder *decoder;
316 IWICBitmapFrameDecode *frame;
317 UINT frame_count, width, height, i;
318 double dpi_x, dpi_y;
319 IWICPalette *palette;
320 GUID format;
321 WICRect rc;
322 BYTE data[16];
323 static const BYTE expected_data[16] = { 0x11,0x11,0x11,0x22,0x33,0x33,0x33,0x44,
324 0x55,0x55,0x55,0x66,0x77,0x77,0x77,0x88 };
326 decoder = create_decoder(&tiff_8bpp_alpha, sizeof(tiff_8bpp_alpha));
327 ok(decoder != 0, "Failed to load TIFF image data\n");
328 if (!decoder) return;
330 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
331 ok(hr == S_OK, "GetFrameCount error %#x\n", hr);
332 ok(frame_count == 1, "expected 1, got %u\n", frame_count);
334 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
335 ok(hr == S_OK, "GetFrame error %#x\n", hr);
337 hr = IWICBitmapFrameDecode_GetSize(frame, &width, &height);
338 ok(hr == S_OK, "GetSize error %#x\n", hr);
339 ok(width == 2, "expected 2, got %u\n", width);
340 ok(height == 2, "expected 2, got %u\n", height);
342 hr = IWICBitmapFrameDecode_GetResolution(frame, &dpi_x, &dpi_y);
343 ok(hr == S_OK, "GetResolution error %#x\n", hr);
344 ok(dpi_x == 96.0, "expected 96.0, got %f\n", dpi_x);
345 ok(dpi_y == 96.0, "expected 96.0, got %f\n", dpi_y);
347 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
348 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
349 ok(IsEqualGUID(&format, &GUID_WICPixelFormat32bppPBGRA),
350 "got wrong format %s\n", wine_dbgstr_guid(&format));
352 hr = IWICImagingFactory_CreatePalette(factory, &palette);
353 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
354 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
355 ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE,
356 "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %#x\n", hr);
357 IWICPalette_Release(palette);
359 rc.X = 0;
360 rc.Y = 0;
361 rc.Width = 2;
362 rc.Height = 2;
363 hr = IWICBitmapFrameDecode_CopyPixels(frame, &rc, 8, sizeof(data), data);
364 ok(hr == S_OK, "CopyPixels error %#x\n", hr);
366 for (i = 0; i < sizeof(data); i++)
367 ok(data[i] == expected_data[i], "%u: expected %02x, got %02x\n", i, expected_data[i], data[i]);
369 IWICBitmapFrameDecode_Release(frame);
370 IWICBitmapDecoder_Release(decoder);
373 START_TEST(tiffformat)
375 HRESULT hr;
377 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
379 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
380 &IID_IWICImagingFactory, (void **)&factory);
381 ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
382 if (FAILED(hr)) return;
384 test_tiff_palette();
385 test_QueryCapability();
386 test_tiff_8bpp_alpha();
388 IWICImagingFactory_Release(factory);
389 CoUninitialize();