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