[WINDOWSCODECS_WINETEST]
[reactos.git] / 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 <stdarg.h>
20 #include <stdio.h>
21
22 #define COBJMACROS
23
24 #include "windef.h"
25 #include "wincodec.h"
26 #include "wine/test.h"
27
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
40
41 #include "pshpack2.h"
42 struct IFD_entry
43 {
44 SHORT id;
45 SHORT type;
46 ULONG count;
47 LONG value;
48 };
49
50 struct IFD_rational
51 {
52 LONG numerator;
53 LONG denominator;
54 };
55
56 static const struct tiff_1bpp_data
57 {
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 =
67 {
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,
76 {
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 */
90 },
91 0,
92 { 900, 3 },
93 { 0x11, 0x22, 0x33, 0 }
94 };
95 #include "poppack.h"
96
97 static IWICImagingFactory *factory;
98
99 static const char *debugstr_guid(const GUID *guid)
100 {
101 static char buf[50];
102
103 if (!guid) return "(null)";
104 sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
105 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
106 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
107 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
108 return buf;
109 }
110
111 static IStream *create_stream(const void *data, int data_size)
112 {
113 HRESULT hr;
114 IStream *stream;
115 HGLOBAL hdata;
116 void *locked_data;
117
118 hdata = GlobalAlloc(GMEM_MOVEABLE, data_size);
119 ok(hdata != 0, "GlobalAlloc failed\n");
120 if (!hdata) return NULL;
121
122 locked_data = GlobalLock(hdata);
123 memcpy(locked_data, data, data_size);
124 GlobalUnlock(hdata);
125
126 hr = CreateStreamOnHGlobal(hdata, TRUE, &stream);
127 ok(hr == S_OK, "CreateStreamOnHGlobal failed, hr=%x\n", hr);
128
129 return stream;
130 }
131
132 static IWICBitmapDecoder *create_decoder(const void *image_data, UINT image_size)
133 {
134 HRESULT hr;
135 IStream *stream;
136 IWICBitmapDecoder *decoder = NULL;
137 GUID guid;
138
139 stream = create_stream(image_data, image_size);
140
141 hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, &decoder);
142 ok(hr == S_OK, "CreateDecoderFromStream error %#x\n", hr);
143
144 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guid);
145 ok(hr == S_OK, "GetContainerFormat error %#x\n", hr);
146 ok(IsEqualGUID(&guid, &GUID_ContainerFormatTiff), "container format is not TIFF\n");
147
148 IStream_Release(stream);
149
150 return decoder;
151 }
152
153 static void test_tiff_palette(void)
154 {
155 HRESULT hr;
156 IWICBitmapDecoder *decoder;
157 IWICBitmapFrameDecode *frame;
158 IWICPalette *palette;
159 GUID format;
160
161 decoder = create_decoder(&tiff_1bpp_data, sizeof(tiff_1bpp_data));
162 ok(decoder != 0, "Failed to load TIFF image data\n");
163
164 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
165 ok(hr == S_OK, "GetFrame error %#x\n", hr);
166
167 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
168 ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
169 ok(IsEqualGUID(&format, &GUID_WICPixelFormatBlackWhite),
170 "got wrong format %s\n", debugstr_guid(&format));
171
172 hr = IWICImagingFactory_CreatePalette(factory, &palette);
173 ok(hr == S_OK, "CreatePalette error %#x\n", hr);
174 hr = IWICBitmapFrameDecode_CopyPalette(frame, palette);
175 ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE,
176 "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %#x\n", hr);
177
178 IWICPalette_Release(palette);
179 IWICBitmapFrameDecode_Release(frame);
180 IWICBitmapDecoder_Release(decoder);
181 }
182
183 static void test_QueryCapability(void)
184 {
185 HRESULT hr;
186 IStream *stream;
187 IWICBitmapDecoder *decoder;
188 IWICBitmapFrameDecode *frame;
189 static const DWORD exp_caps = WICBitmapDecoderCapabilityCanDecodeAllImages |
190 WICBitmapDecoderCapabilityCanDecodeSomeImages |
191 WICBitmapDecoderCapabilityCanEnumerateMetadata;
192 static const DWORD exp_caps_xp = WICBitmapDecoderCapabilityCanDecodeAllImages |
193 WICBitmapDecoderCapabilityCanDecodeSomeImages;
194 DWORD capability;
195 LARGE_INTEGER pos;
196 ULARGE_INTEGER cur_pos;
197 UINT frame_count;
198
199 stream = create_stream(&tiff_1bpp_data, sizeof(tiff_1bpp_data));
200 if (!stream) return;
201
202 hr = IWICImagingFactory_CreateDecoder(factory, &GUID_ContainerFormatTiff, NULL, &decoder);
203 ok(hr == S_OK, "CreateDecoder error %#x\n", hr);
204
205 frame_count = 0xdeadbeef;
206 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
207 ok(hr == S_OK || broken(hr == E_POINTER) /* XP */, "GetFrameCount error %#x\n", hr);
208 ok(frame_count == 0, "expected 0, got %u\n", frame_count);
209
210 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
211 ok(hr == WINCODEC_ERR_FRAMEMISSING || broken(hr == E_POINTER) /* XP */, "expected WINCODEC_ERR_FRAMEMISSING, got %#x\n", hr);
212
213 pos.QuadPart = 4;
214 hr = IStream_Seek(stream, pos, SEEK_SET, NULL);
215 ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
216
217 capability = 0xdeadbeef;
218 hr = IWICBitmapDecoder_QueryCapability(decoder, stream, &capability);
219 ok(hr == S_OK, "QueryCapability error %#x\n", hr);
220 ok(capability == exp_caps || capability == exp_caps_xp,
221 "expected %#x, got %#x\n", exp_caps, capability);
222
223 frame_count = 0xdeadbeef;
224 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
225 ok(hr == S_OK, "GetFrameCount error %#x\n", hr);
226 ok(frame_count == 1, "expected 1, got %u\n", frame_count);
227
228 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
229 ok(hr == S_OK, "GetFrame error %#x\n", hr);
230 IWICBitmapFrameDecode_Release(frame);
231
232 pos.QuadPart = 0;
233 hr = IStream_Seek(stream, pos, SEEK_CUR, &cur_pos);
234 ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
235 ok(cur_pos.QuadPart > 4 && cur_pos.QuadPart < sizeof(tiff_1bpp_data),
236 "current stream pos is at %x/%x\n", cur_pos.u.LowPart, cur_pos.u.HighPart);
237
238 hr = IWICBitmapDecoder_QueryCapability(decoder, stream, &capability);
239 ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
240
241 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnDemand);
242 ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
243
244 IWICBitmapDecoder_Release(decoder);
245
246 hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, &decoder);
247 todo_wine
248 ok(hr == WINCODEC_ERR_COMPONENTNOTFOUND, "expected WINCODEC_ERR_COMPONENTNOTFOUND, got %#x\n", hr);
249
250 pos.QuadPart = 0;
251 hr = IStream_Seek(stream, pos, SEEK_SET, NULL);
252 ok(hr == S_OK, "IStream_Seek error %#x\n", hr);
253
254 hr = IWICImagingFactory_CreateDecoderFromStream(factory, stream, NULL, 0, &decoder);
255 ok(hr == S_OK, "CreateDecoderFromStream error %#x\n", hr);
256
257 frame_count = 0xdeadbeef;
258 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
259 ok(hr == S_OK, "GetFrameCount error %#x\n", hr);
260 ok(frame_count == 1, "expected 1, got %u\n", frame_count);
261
262 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
263 ok(hr == S_OK, "GetFrame error %#x\n", hr);
264 IWICBitmapFrameDecode_Release(frame);
265
266 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnDemand);
267 ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
268
269 hr = IWICBitmapDecoder_QueryCapability(decoder, stream, &capability);
270 ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, got %#x\n", hr);
271
272 IWICBitmapDecoder_Release(decoder);
273 IStream_Release(stream);
274 }
275
276 START_TEST(tiffformat)
277 {
278 HRESULT hr;
279
280 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
281
282 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
283 &IID_IWICImagingFactory, (void **)&factory);
284 ok(hr == S_OK, "CoCreateInstance error %#x\n", hr);
285 if (FAILED(hr)) return;
286
287 test_tiff_palette();
288 test_QueryCapability();
289
290 IWICImagingFactory_Release(factory);
291 CoUninitialize();
292 }