windowscodecs: Fix the tile coordinate translation in the TIFF decoder.
[wine] / dlls / windowscodecs / tests / bmpformat.c
1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
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 <math.h>
21
22 #define COBJMACROS
23
24 #include "windef.h"
25 #include "initguid.h"
26 #include "objbase.h"
27 #include "wincodec.h"
28 #include "wine/test.h"
29
30 static const char testbmp_24bpp[] = {
31     /* BITMAPFILEHEADER */
32     66,77, /* "BM" */
33     50,0,0,0, /* file size */
34     0,0,0,0, /* reserved */
35     26,0,0,0, /* offset to bits */
36     /* BITMAPCOREHEADER */
37     12,0,0,0, /* header size */
38     2,0, /* width */
39     3,0, /* height */
40     1,0, /* planes */
41     24,0, /* bit count */
42     /* bits */
43     0,0,0,     0,255,0,     0,0,
44     255,0,0,   255,255,0,   0,0,
45     255,0,255, 255,255,255, 0,0
46 };
47
48 static void test_decode_24bpp(void)
49 {
50     IWICBitmapDecoder *decoder, *decoder2;
51     IWICBitmapFrameDecode *framedecode;
52     IWICMetadataQueryReader *queryreader;
53     IWICColorContext *colorcontext;
54     IWICBitmapSource *thumbnail;
55     HRESULT hr;
56     HGLOBAL hbmpdata;
57     char *bmpdata;
58     IStream *bmpstream;
59     DWORD capability=0;
60     GUID guidresult;
61     UINT count=0, width=0, height=0;
62     double dpiX, dpiY;
63     BYTE imagedata[36] = {1};
64     const BYTE expected_imagedata[36] = {
65         255,0,255, 255,255,255,
66         255,0,0,   255,255,0,
67         0,0,0,     0,255,0};
68     WICRect rc;
69
70     hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
71         &IID_IWICBitmapDecoder, (void**)&decoder);
72     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
73     if (FAILED(hr)) return;
74
75     hbmpdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(testbmp_24bpp));
76     ok(hbmpdata != 0, "GlobalAlloc failed\n");
77     if (hbmpdata)
78     {
79         bmpdata = GlobalLock(hbmpdata);
80         memcpy(bmpdata, testbmp_24bpp, sizeof(testbmp_24bpp));
81         GlobalUnlock(hbmpdata);
82
83         hr = CreateStreamOnHGlobal(hbmpdata, FALSE, &bmpstream);
84         ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
85         if (SUCCEEDED(hr))
86         {
87             hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
88             ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
89
90             hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
91             ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
92             ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatBmp), "unexpected container format\n");
93
94             hr = IWICBitmapDecoder_GetMetadataQueryReader(decoder, &queryreader);
95             ok(hr == WINCODEC_ERR_UNSUPPORTEDOPERATION, "expected WINCODEC_ERR_UNSUPPORTEDOPERATION, got %x\n", hr);
96
97             hr = IWICBitmapDecoder_GetColorContexts(decoder, 1, &colorcontext, &count);
98             ok(hr == WINCODEC_ERR_UNSUPPORTEDOPERATION, "expected WINCODEC_ERR_UNSUPPORTEDOPERATION, got %x\n", hr);
99
100             hr = IWICBitmapDecoder_GetThumbnail(decoder, &thumbnail);
101             ok(hr == WINCODEC_ERR_CODECNOTHUMBNAIL, "expected WINCODEC_ERR_CODECNOTHUMBNAIL, got %x\n", hr);
102
103             hr = IWICBitmapDecoder_GetPreview(decoder, &thumbnail);
104             ok(hr == WINCODEC_ERR_UNSUPPORTEDOPERATION, "expected WINCODEC_ERR_UNSUPPORTEDOPERATION, got %x\n", hr);
105
106             hr = IWICBitmapDecoder_GetFrameCount(decoder, &count);
107             ok(SUCCEEDED(hr), "GetFrameCount failed, hr=%x\n", hr);
108             ok(count == 1, "unexpected count %u\n", count);
109
110             hr = IWICBitmapDecoder_GetFrame(decoder, 1, &framedecode);
111             ok(hr == E_INVALIDARG || hr == WINCODEC_ERR_FRAMEMISSING, "GetFrame returned %x\n", hr);
112
113             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
114             ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
115             if (SUCCEEDED(hr))
116             {
117                 IWICImagingFactory *factory;
118                 IWICPalette *palette;
119
120                 hr = IWICBitmapFrameDecode_GetSize(framedecode, &width, &height);
121                 ok(SUCCEEDED(hr), "GetSize failed, hr=%x\n", hr);
122                 ok(width == 2, "expected width=2, got %u\n", width);
123                 ok(height == 3, "expected height=2, got %u\n", height);
124
125                 hr = IWICBitmapFrameDecode_GetResolution(framedecode, &dpiX, &dpiY);
126                 ok(SUCCEEDED(hr), "GetResolution failed, hr=%x\n", hr);
127                 ok(dpiX == 96.0, "expected dpiX=96.0, got %f\n", dpiX);
128                 ok(dpiY == 96.0, "expected dpiY=96.0, got %f\n", dpiY);
129
130                 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &guidresult);
131                 ok(SUCCEEDED(hr), "GetPixelFormat failed, hr=%x\n", hr);
132                 ok(IsEqualGUID(&guidresult, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
133
134                 hr = IWICBitmapFrameDecode_GetMetadataQueryReader(framedecode, &queryreader);
135                 ok(hr == WINCODEC_ERR_UNSUPPORTEDOPERATION, "expected WINCODEC_ERR_UNSUPPORTEDOPERATION, got %x\n", hr);
136
137                 hr = IWICBitmapFrameDecode_GetColorContexts(framedecode, 1, &colorcontext, &count);
138                 ok(hr == WINCODEC_ERR_UNSUPPORTEDOPERATION, "expected WINCODEC_ERR_UNSUPPORTEDOPERATION, got %x\n", hr);
139
140                 hr = IWICBitmapFrameDecode_GetThumbnail(framedecode, &thumbnail);
141                 ok(hr == WINCODEC_ERR_CODECNOTHUMBNAIL, "expected WINCODEC_ERR_CODECNOTHUMBNAIL, got %x\n", hr);
142
143                 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
144                     &IID_IWICImagingFactory, (void**)&factory);
145                 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
146                 if (SUCCEEDED(hr))
147                 {
148                     hr = IWICImagingFactory_CreatePalette(factory, &palette);
149                     ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
150                     if (SUCCEEDED(hr))
151                     {
152                         hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
153                         ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %x\n", hr);
154
155                         hr = IWICBitmapFrameDecode_CopyPalette(framedecode, palette);
156                         ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %x\n", hr);
157
158                         IWICPalette_Release(palette);
159                     }
160
161                     IWICImagingFactory_Release(factory);
162                 }
163
164                 rc.X = 0;
165                 rc.Y = 0;
166                 rc.Width = 3;
167                 rc.Height = 3;
168                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 6, sizeof(imagedata), imagedata);
169                 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
170
171                 rc.X = -1;
172                 rc.Y = 0;
173                 rc.Width = 2;
174                 rc.Height = 3;
175                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 6, sizeof(imagedata), imagedata);
176                 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
177
178                 rc.X = 0;
179                 rc.Y = 0;
180                 rc.Width = 2;
181                 rc.Height = 3;
182                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 4, sizeof(imagedata), imagedata);
183                 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
184
185                 rc.X = 0;
186                 rc.Y = 0;
187                 rc.Width = 2;
188                 rc.Height = 3;
189                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 4, 5, imagedata);
190                 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
191
192                 rc.X = 0;
193                 rc.Y = 0;
194                 rc.Width = 2;
195                 rc.Height = 3;
196                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 6, sizeof(imagedata), imagedata);
197                 ok(SUCCEEDED(hr), "CopyPixels failed, hr=%x\n", hr);
198                 ok(!memcmp(imagedata, expected_imagedata, sizeof(imagedata)), "unexpected image data\n");
199
200                 IWICBitmapFrameDecode_Release(framedecode);
201             }
202
203             /* cannot initialize twice */
204             hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
205             ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
206
207             /* cannot querycapability after initialize */
208             hr = IWICBitmapDecoder_QueryCapability(decoder, bmpstream, &capability);
209             ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
210
211             hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
212                 &IID_IWICBitmapDecoder, (void**)&decoder2);
213             ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
214             if (SUCCEEDED(hr))
215             {
216                 hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
217                 ok(hr == S_OK, "QueryCapability failed, hr=%x\n", hr);
218                 ok(capability == (WICBitmapDecoderCapabilityCanDecodeAllImages),
219                     "unexpected capabilities: %x\n", capability);
220
221                 /* cannot initialize after querycapability */
222                 hr = IWICBitmapDecoder_Initialize(decoder2, bmpstream, WICDecodeMetadataCacheOnLoad);
223                 ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
224
225                 /* cannot querycapability twice */
226                 hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
227                 ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
228
229                 IWICBitmapDecoder_Release(decoder2);
230             }
231
232             IStream_Release(bmpstream);
233         }
234
235         GlobalFree(hbmpdata);
236     }
237
238     IWICBitmapDecoder_Release(decoder);
239 }
240
241 static const char testbmp_1bpp[] = {
242     /* BITMAPFILEHEADER */
243     66,77, /* "BM" */
244     40,0,0,0, /* file size */
245     0,0,0,0, /* reserved */
246     32,0,0,0, /* offset to bits */
247     /* BITMAPCOREHEADER */
248     12,0,0,0, /* header size */
249     2,0, /* width */
250     2,0, /* height */
251     1,0, /* planes */
252     1,0, /* bit count */
253     /* color table */
254     255,0,0,
255     0,255,0,
256     /* bits */
257     0xc0,0,0,0,
258     0x80,0,0,0
259 };
260
261 static void test_decode_1bpp(void)
262 {
263     IWICBitmapDecoder *decoder, *decoder2;
264     IWICBitmapFrameDecode *framedecode;
265     HRESULT hr;
266     HGLOBAL hbmpdata;
267     char *bmpdata;
268     IStream *bmpstream;
269     DWORD capability=0;
270     GUID guidresult;
271     UINT count=0, width=0, height=0;
272     double dpiX, dpiY;
273     BYTE imagedata[2] = {1};
274     const BYTE expected_imagedata[2] = {0x80,0xc0};
275     WICColor palettedata[2] = {1};
276     const WICColor expected_palettedata[2] = {0xff0000ff,0xff00ff00};
277     WICRect rc;
278
279     hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
280         &IID_IWICBitmapDecoder, (void**)&decoder);
281     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
282     if (FAILED(hr)) return;
283
284     hbmpdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(testbmp_1bpp));
285     ok(hbmpdata != 0, "GlobalAlloc failed\n");
286     if (hbmpdata)
287     {
288         bmpdata = GlobalLock(hbmpdata);
289         memcpy(bmpdata, testbmp_1bpp, sizeof(testbmp_1bpp));
290         GlobalUnlock(hbmpdata);
291
292         hr = CreateStreamOnHGlobal(hbmpdata, FALSE, &bmpstream);
293         ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
294         if (SUCCEEDED(hr))
295         {
296             hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
297             ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
298
299             hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
300             ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
301             ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatBmp), "unexpected container format\n");
302
303             hr = IWICBitmapDecoder_GetFrameCount(decoder, &count);
304             ok(SUCCEEDED(hr), "GetFrameCount failed, hr=%x\n", hr);
305             ok(count == 1, "unexpected count %u\n", count);
306
307             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
308             ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
309             if (SUCCEEDED(hr))
310             {
311                 IWICImagingFactory *factory;
312                 IWICPalette *palette;
313
314                 hr = IWICBitmapFrameDecode_GetSize(framedecode, &width, &height);
315                 ok(SUCCEEDED(hr), "GetSize failed, hr=%x\n", hr);
316                 ok(width == 2, "expected width=2, got %u\n", width);
317                 ok(height == 2, "expected height=2, got %u\n", height);
318
319                 hr = IWICBitmapFrameDecode_GetResolution(framedecode, &dpiX, &dpiY);
320                 ok(SUCCEEDED(hr), "GetResolution failed, hr=%x\n", hr);
321                 ok(dpiX == 96.0, "expected dpiX=96.0, got %f\n", dpiX);
322                 ok(dpiY == 96.0, "expected dpiY=96.0, got %f\n", dpiY);
323
324                 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &guidresult);
325                 ok(SUCCEEDED(hr), "GetPixelFormat failed, hr=%x\n", hr);
326                 ok(IsEqualGUID(&guidresult, &GUID_WICPixelFormat1bppIndexed), "unexpected pixel format\n");
327
328                 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
329                     &IID_IWICImagingFactory, (void**)&factory);
330                 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
331                 if (SUCCEEDED(hr))
332                 {
333                     hr = IWICImagingFactory_CreatePalette(factory, &palette);
334                     ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
335                     if (SUCCEEDED(hr))
336                     {
337                         hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
338                         ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %x\n", hr);
339
340                         hr = IWICBitmapFrameDecode_CopyPalette(framedecode, palette);
341                         ok(SUCCEEDED(hr), "CopyPalette failed, hr=%x\n", hr);
342
343                         hr = IWICPalette_GetColorCount(palette, &count);
344                         ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
345                         ok(count == 2, "expected count=2, got %u\n", count);
346
347                         hr = IWICPalette_GetColors(palette, 2, palettedata, &count);
348                         ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
349                         ok(count == 2, "expected count=2, got %u\n", count);
350                         ok(!memcmp(palettedata, expected_palettedata, sizeof(palettedata)), "unexpected palette data\n");
351
352                         IWICPalette_Release(palette);
353                     }
354
355                     IWICImagingFactory_Release(factory);
356                 }
357
358                 rc.X = 0;
359                 rc.Y = 0;
360                 rc.Width = 2;
361                 rc.Height = 2;
362                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 1, sizeof(imagedata), imagedata);
363                 ok(SUCCEEDED(hr), "CopyPixels failed, hr=%x\n", hr);
364                 ok(!memcmp(imagedata, expected_imagedata, sizeof(imagedata)), "unexpected image data\n");
365
366                 IWICBitmapFrameDecode_Release(framedecode);
367             }
368
369             hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
370                 &IID_IWICBitmapDecoder, (void**)&decoder2);
371             ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
372             if (SUCCEEDED(hr))
373             {
374                 hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
375                 ok(hr == S_OK, "QueryCapability failed, hr=%x\n", hr);
376                 ok(capability == (WICBitmapDecoderCapabilityCanDecodeAllImages),
377                     "unexpected capabilities: %x\n", capability);
378                 IWICBitmapDecoder_Release(decoder2);
379             }
380
381             IStream_Release(bmpstream);
382         }
383
384         GlobalFree(hbmpdata);
385     }
386
387     IWICBitmapDecoder_Release(decoder);
388 }
389
390 static const char testbmp_4bpp[] = {
391     /* BITMAPFILEHEADER */
392     66,77, /* "BM" */
393     82,0,0,0, /* file size */
394     0,0,0,0, /* reserved */
395     74,0,0,0, /* offset to bits */
396     /* BITMAPINFOHEADER */
397     40,0,0,0, /* header size */
398     2,0,0,0, /* width */
399     254,255,255,255, /* height = -2 */
400     1,0, /* planes */
401     4,0, /* bit count */
402     0,0,0,0, /* compression = BI_RGB */
403     0,0,0,0, /* image size = 0 */
404     16,39,0,0, /* X pixels per meter = 10000 */
405     32,78,0,0, /* Y pixels per meter = 20000 */
406     5,0,0,0, /* colors used */
407     5,0,0,0, /* colors important */
408     /* color table */
409     255,0,0,0,
410     0,255,0,255,
411     0,0,255,23,
412     128,0,128,1,
413     255,255,255,0,
414     /* bits */
415     0x01,0,0,0,
416     0x23,0,0,0,
417 };
418
419 static void test_decode_4bpp(void)
420 {
421     IWICBitmapDecoder *decoder, *decoder2;
422     IWICBitmapFrameDecode *framedecode;
423     HRESULT hr;
424     HGLOBAL hbmpdata;
425     char *bmpdata;
426     IStream *bmpstream;
427     DWORD capability=0;
428     GUID guidresult;
429     UINT count=0, width=0, height=0;
430     double dpiX, dpiY;
431     BYTE imagedata[2] = {1};
432     const BYTE expected_imagedata[2] = {0x01,0x23};
433     WICColor palettedata[5] = {1};
434     const WICColor expected_palettedata[5] =
435         {0xff0000ff,0xff00ff00,0xffff0000,0xff800080,0xffffffff};
436     WICRect rc;
437
438     hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
439         &IID_IWICBitmapDecoder, (void**)&decoder);
440     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
441     if (FAILED(hr)) return;
442
443     hbmpdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(testbmp_4bpp));
444     ok(hbmpdata != 0, "GlobalAlloc failed\n");
445     if (hbmpdata)
446     {
447         bmpdata = GlobalLock(hbmpdata);
448         memcpy(bmpdata, testbmp_4bpp, sizeof(testbmp_4bpp));
449         GlobalUnlock(hbmpdata);
450
451         hr = CreateStreamOnHGlobal(hbmpdata, FALSE, &bmpstream);
452         ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
453         if (SUCCEEDED(hr))
454         {
455             hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
456             ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
457
458             hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
459             ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
460             ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatBmp), "unexpected container format\n");
461
462             hr = IWICBitmapDecoder_GetFrameCount(decoder, &count);
463             ok(SUCCEEDED(hr), "GetFrameCount failed, hr=%x\n", hr);
464             ok(count == 1, "unexpected count %u\n", count);
465
466             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
467             ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
468             if (SUCCEEDED(hr))
469             {
470                 IWICImagingFactory *factory;
471                 IWICPalette *palette;
472
473                 hr = IWICBitmapFrameDecode_GetSize(framedecode, &width, &height);
474                 ok(SUCCEEDED(hr), "GetSize failed, hr=%x\n", hr);
475                 ok(width == 2, "expected width=2, got %u\n", width);
476                 ok(height == 2, "expected height=2, got %u\n", height);
477
478                 hr = IWICBitmapFrameDecode_GetResolution(framedecode, &dpiX, &dpiY);
479                 ok(SUCCEEDED(hr), "GetResolution failed, hr=%x\n", hr);
480                 ok(fabs(dpiX - 254.0) < 0.01, "expected dpiX=96.0, got %f\n", dpiX);
481                 ok(fabs(dpiY - 508.0) < 0.01, "expected dpiY=96.0, got %f\n", dpiY);
482
483                 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &guidresult);
484                 ok(SUCCEEDED(hr), "GetPixelFormat failed, hr=%x\n", hr);
485                 ok(IsEqualGUID(&guidresult, &GUID_WICPixelFormat4bppIndexed), "unexpected pixel format\n");
486
487                 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
488                     &IID_IWICImagingFactory, (void**)&factory);
489                 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
490                 if (SUCCEEDED(hr))
491                 {
492                     hr = IWICImagingFactory_CreatePalette(factory, &palette);
493                     ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
494                     if (SUCCEEDED(hr))
495                     {
496                         hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
497                         ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %x\n", hr);
498
499                         hr = IWICBitmapFrameDecode_CopyPalette(framedecode, palette);
500                         ok(SUCCEEDED(hr), "CopyPalette failed, hr=%x\n", hr);
501
502                         hr = IWICPalette_GetColorCount(palette, &count);
503                         ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
504                         ok(count == 5, "expected count=5, got %u\n", count);
505
506                         hr = IWICPalette_GetColors(palette, 5, palettedata, &count);
507                         ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
508                         ok(count == 5, "expected count=5, got %u\n", count);
509                         ok(!memcmp(palettedata, expected_palettedata, sizeof(palettedata)), "unexpected palette data\n");
510
511                         IWICPalette_Release(palette);
512                     }
513
514                     IWICImagingFactory_Release(factory);
515                 }
516
517                 rc.X = 0;
518                 rc.Y = 0;
519                 rc.Width = 2;
520                 rc.Height = 2;
521                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 1, sizeof(imagedata), imagedata);
522                 ok(SUCCEEDED(hr), "CopyPixels failed, hr=%x\n", hr);
523                 ok(!memcmp(imagedata, expected_imagedata, sizeof(imagedata)), "unexpected image data\n");
524
525                 IWICBitmapFrameDecode_Release(framedecode);
526             }
527
528             hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
529                 &IID_IWICBitmapDecoder, (void**)&decoder2);
530             ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
531             if (SUCCEEDED(hr))
532             {
533                 hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
534                 ok(hr == S_OK, "QueryCapability failed, hr=%x\n", hr);
535                 ok(capability == (WICBitmapDecoderCapabilityCanDecodeAllImages),
536                     "unexpected capabilities: %x\n", capability);
537                 IWICBitmapDecoder_Release(decoder2);
538             }
539
540             IStream_Release(bmpstream);
541         }
542
543         GlobalFree(hbmpdata);
544     }
545
546     IWICBitmapDecoder_Release(decoder);
547 }
548
549 static const char testbmp_rle8[] = {
550     /* BITMAPFILEHEADER */
551     66,77, /* "BM" */
552     202,0,0,0, /* file size */
553     0,0,0,0, /* reserved */
554     122,0,0,0, /* offset to bits */
555     /* BITMAPINFOHEADER */
556     40,0,0,0, /* header size */
557     8,0,0,0, /* width */
558     8,0,0,0, /* height */
559     1,0, /* planes */
560     8,0, /* bit count */
561     1,0,0,0, /* compression = BI_RLE8 */
562     80,0,0,0, /* image size */
563     19,11,0,0, /* X pixels per meter */
564     19,11,0,0, /* Y pixels per meter */
565     17,0,0,0, /* colors used */
566     17,0,0,0, /* colors important */
567     /* color table */
568     0,0,0,0,
569     17,17,17,0,
570     255,0,0,0,
571     34,34,34,0,
572     0,0,204,0,
573     0,0,221,0,
574     0,0,238,0,
575     51,51,51,0,
576     0,0,255,0,
577     68,68,68,0,
578     255,0,255,0,
579     85,85,85,0,
580     0,204,0,0,
581     0,221,0,0,
582     0,238,0,0,
583     0,255,0,0,
584     255,255,255,0,
585     /* bits */
586     4,15,0,4,11,9,9,0,0,0,4,14,0,4,3,10,10,7,0,0,4,13,0,4,3,10,10,7,0,0,4,12,0,4,0,1,1,11,0,0,0,4,16,2,16,2,4,4,0,0,0,4,2,16,2,16,4,5,0,0,0,4,16,2,16,2,4,6,0,0,0,4,2,16,2,16,4,8,0,1
587 };
588
589 static void test_decode_rle8(void)
590 {
591     IWICBitmapDecoder *decoder, *decoder2;
592     IWICBitmapFrameDecode *framedecode;
593     HRESULT hr;
594     HGLOBAL hbmpdata;
595     char *bmpdata;
596     IStream *bmpstream;
597     DWORD capability=0;
598     GUID guidresult;
599     UINT count=0, width=0, height=0;
600     double dpiX, dpiY;
601     DWORD imagedata[64] = {1};
602     const DWORD expected_imagedata[64] = {
603         0x0000ff,0xffffff,0x0000ff,0xffffff,0xff0000,0xff0000,0xff0000,0xff0000,
604         0xffffff,0x0000ff,0xffffff,0x0000ff,0xee0000,0xee0000,0xee0000,0xee0000,
605         0x0000ff,0xffffff,0x0000ff,0xffffff,0xdd0000,0xdd0000,0xdd0000,0xdd0000,
606         0xffffff,0x0000ff,0xffffff,0x0000ff,0xcc0000,0xcc0000,0xcc0000,0xcc0000,
607         0x00cc00,0x00cc00,0x00cc00,0x00cc00,0x000000,0x111111,0x111111,0x555555,
608         0x00dd00,0x00dd00,0x00dd00,0x00dd00,0x222222,0xff00ff,0xff00ff,0x333333,
609         0x00ee00,0x00ee00,0x00ee00,0x00ee00,0x222222,0xff00ff,0xff00ff,0x333333,
610         0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x555555,0x444444,0x444444,0x000000};
611     WICColor palettedata[17] = {1};
612     const WICColor expected_palettedata[17] = {
613         0xff000000,0xff111111,0xff0000ff,0xff222222,0xffcc0000,0xffdd0000,
614         0xffee0000,0xff333333,0xffff0000,0xff444444,0xffff00ff,0xff555555,
615         0xff00cc00,0xff00dd00,0xff00ee00,0xff00ff00,0xffffffff};
616     WICRect rc;
617
618     hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
619         &IID_IWICBitmapDecoder, (void**)&decoder);
620     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
621     if (FAILED(hr)) return;
622
623     hbmpdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(testbmp_rle8));
624     ok(hbmpdata != 0, "GlobalAlloc failed\n");
625     if (hbmpdata)
626     {
627         bmpdata = GlobalLock(hbmpdata);
628         memcpy(bmpdata, testbmp_rle8, sizeof(testbmp_rle8));
629         GlobalUnlock(hbmpdata);
630
631         hr = CreateStreamOnHGlobal(hbmpdata, FALSE, &bmpstream);
632         ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
633         if (SUCCEEDED(hr))
634         {
635             hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
636             ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
637
638             hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
639             ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
640             ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatBmp), "unexpected container format\n");
641
642             hr = IWICBitmapDecoder_GetFrameCount(decoder, &count);
643             ok(SUCCEEDED(hr), "GetFrameCount failed, hr=%x\n", hr);
644             ok(count == 1, "unexpected count %u\n", count);
645
646             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
647             ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
648             if (SUCCEEDED(hr))
649             {
650                 IWICImagingFactory *factory;
651                 IWICPalette *palette;
652
653                 hr = IWICBitmapFrameDecode_GetSize(framedecode, &width, &height);
654                 ok(SUCCEEDED(hr), "GetSize failed, hr=%x\n", hr);
655                 ok(width == 8, "expected width=8, got %u\n", width);
656                 ok(height == 8, "expected height=8, got %u\n", height);
657
658                 hr = IWICBitmapFrameDecode_GetResolution(framedecode, &dpiX, &dpiY);
659                 ok(SUCCEEDED(hr), "GetResolution failed, hr=%x\n", hr);
660                 ok(fabs(dpiX - 72.0) < 0.01, "expected dpiX=96.0, got %f\n", dpiX);
661                 ok(fabs(dpiY - 72.0) < 0.01, "expected dpiY=96.0, got %f\n", dpiY);
662
663                 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &guidresult);
664                 ok(SUCCEEDED(hr), "GetPixelFormat failed, hr=%x\n", hr);
665                 ok(IsEqualGUID(&guidresult, &GUID_WICPixelFormat32bppBGR), "unexpected pixel format\n");
666
667                 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
668                     &IID_IWICImagingFactory, (void**)&factory);
669                 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
670                 if (SUCCEEDED(hr))
671                 {
672                     hr = IWICImagingFactory_CreatePalette(factory, &palette);
673                     ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
674                     if (SUCCEEDED(hr))
675                     {
676                         hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
677                         ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %x\n", hr);
678
679                         hr = IWICBitmapFrameDecode_CopyPalette(framedecode, palette);
680                         ok(SUCCEEDED(hr), "CopyPalette failed, hr=%x\n", hr);
681
682                         hr = IWICPalette_GetColorCount(palette, &count);
683                         ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
684                         ok(count == 17, "expected count=17, got %u\n", count);
685
686                         hr = IWICPalette_GetColors(palette, 17, palettedata, &count);
687                         ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
688                         ok(count == 17, "expected count=17, got %u\n", count);
689                         ok(!memcmp(palettedata, expected_palettedata, sizeof(palettedata)), "unexpected palette data\n");
690
691                         IWICPalette_Release(palette);
692                     }
693
694                     IWICImagingFactory_Release(factory);
695                 }
696
697                 rc.X = 0;
698                 rc.Y = 0;
699                 rc.Width = 8;
700                 rc.Height = 8;
701                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 32, sizeof(imagedata), (BYTE*)imagedata);
702                 ok(SUCCEEDED(hr), "CopyPixels failed, hr=%x\n", hr);
703                 ok(!memcmp(imagedata, expected_imagedata, sizeof(imagedata)), "unexpected image data\n");
704
705                 IWICBitmapFrameDecode_Release(framedecode);
706             }
707
708             hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
709                 &IID_IWICBitmapDecoder, (void**)&decoder2);
710             ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
711             if (SUCCEEDED(hr))
712             {
713                 hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
714                 ok(hr == S_OK, "QueryCapability failed, hr=%x\n", hr);
715                 ok(capability == (WICBitmapDecoderCapabilityCanDecodeAllImages),
716                     "unexpected capabilities: %x\n", capability);
717                 IWICBitmapDecoder_Release(decoder2);
718             }
719
720             IStream_Release(bmpstream);
721         }
722
723         GlobalFree(hbmpdata);
724     }
725
726     IWICBitmapDecoder_Release(decoder);
727 }
728
729 static const char testbmp_rle4[] = {
730     /* BITMAPFILEHEADER */
731     66,77, /* "BM" */
732     142,0,0,0, /* file size */
733     0,0,0,0, /* reserved */
734     78,0,0,0, /* offset to bits */
735     /* BITMAPINFOHEADER */
736     40,0,0,0, /* header size */
737     8,0,0,0, /* width */
738     8,0,0,0, /* height */
739     1,0, /* planes */
740     4,0, /* bit count */
741     2,0,0,0, /* compression = BI_RLE4 */
742     64,0,0,0, /* image size */
743     19,11,0,0, /* X pixels per meter */
744     19,11,0,0, /* Y pixels per meter */
745     6,0,0,0, /* colors used */
746     6,0,0,0, /* colors important */
747     /* color table */
748     0,0,0,0,
749     255,0,0,0,
750     0,0,255,0,
751     255,0,255,0,
752     0,255,0,0,
753     255,255,255,0,
754     /* bits */
755     0,8,68,68,0,0,0,0,0,8,68,68,3,48,0,0,0,8,68,68,3,48,0,0,0,8,68,68,0,0,0,0,0,8,81,81,34,34,0,0,0,8,21,21,34,34,0,0,0,8,81,81,34,34,0,0,0,8,21,21,34,34,0,1
756 };
757
758 static void test_decode_rle4(void)
759 {
760     IWICBitmapDecoder *decoder, *decoder2;
761     IWICBitmapFrameDecode *framedecode;
762     HRESULT hr;
763     HGLOBAL hbmpdata;
764     char *bmpdata;
765     IStream *bmpstream;
766     DWORD capability=0;
767     GUID guidresult;
768     UINT count=0, width=0, height=0;
769     double dpiX, dpiY;
770     DWORD imagedata[64] = {1};
771     const DWORD expected_imagedata[64] = {
772         0x0000ff,0xffffff,0x0000ff,0xffffff,0xff0000,0xff0000,0xff0000,0xff0000,
773         0xffffff,0x0000ff,0xffffff,0x0000ff,0xff0000,0xff0000,0xff0000,0xff0000,
774         0x0000ff,0xffffff,0x0000ff,0xffffff,0xff0000,0xff0000,0xff0000,0xff0000,
775         0xffffff,0x0000ff,0xffffff,0x0000ff,0xff0000,0xff0000,0xff0000,0xff0000,
776         0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x000000,0x000000,0x000000,0x000000,
777         0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x000000,0xff00ff,0xff00ff,0x000000,
778         0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x000000,0xff00ff,0xff00ff,0x000000,
779         0x00ff00,0x00ff00,0x00ff00,0x00ff00,0x000000,0x000000,0x000000,0x000000};
780     WICColor palettedata[6] = {1};
781     const WICColor expected_palettedata[6] = {
782         0xff000000,0xff0000ff,0xffff0000,0xffff00ff,0xff00ff00,0xffffffff};
783     WICRect rc;
784
785     hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
786         &IID_IWICBitmapDecoder, (void**)&decoder);
787     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
788     if (FAILED(hr)) return;
789
790     hbmpdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(testbmp_rle4));
791     ok(hbmpdata != 0, "GlobalAlloc failed\n");
792     if (hbmpdata)
793     {
794         bmpdata = GlobalLock(hbmpdata);
795         memcpy(bmpdata, testbmp_rle4, sizeof(testbmp_rle4));
796         GlobalUnlock(hbmpdata);
797
798         hr = CreateStreamOnHGlobal(hbmpdata, FALSE, &bmpstream);
799         ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
800         if (SUCCEEDED(hr))
801         {
802             hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
803             ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
804
805             hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
806             ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
807             ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatBmp), "unexpected container format\n");
808
809             hr = IWICBitmapDecoder_GetFrameCount(decoder, &count);
810             ok(SUCCEEDED(hr), "GetFrameCount failed, hr=%x\n", hr);
811             ok(count == 1, "unexpected count %u\n", count);
812
813             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
814             ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
815             if (SUCCEEDED(hr))
816             {
817                 IWICImagingFactory *factory;
818                 IWICPalette *palette;
819
820                 hr = IWICBitmapFrameDecode_GetSize(framedecode, &width, &height);
821                 ok(SUCCEEDED(hr), "GetSize failed, hr=%x\n", hr);
822                 ok(width == 8, "expected width=8, got %u\n", width);
823                 ok(height == 8, "expected height=8, got %u\n", height);
824
825                 hr = IWICBitmapFrameDecode_GetResolution(framedecode, &dpiX, &dpiY);
826                 ok(SUCCEEDED(hr), "GetResolution failed, hr=%x\n", hr);
827                 ok(fabs(dpiX - 72.0) < 0.01, "expected dpiX=96.0, got %f\n", dpiX);
828                 ok(fabs(dpiY - 72.0) < 0.01, "expected dpiY=96.0, got %f\n", dpiY);
829
830                 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &guidresult);
831                 ok(SUCCEEDED(hr), "GetPixelFormat failed, hr=%x\n", hr);
832                 ok(IsEqualGUID(&guidresult, &GUID_WICPixelFormat32bppBGR), "unexpected pixel format\n");
833
834                 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
835                     &IID_IWICImagingFactory, (void**)&factory);
836                 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
837                 if (SUCCEEDED(hr))
838                 {
839                     hr = IWICImagingFactory_CreatePalette(factory, &palette);
840                     ok(SUCCEEDED(hr), "CreatePalette failed, hr=%x\n", hr);
841                     if (SUCCEEDED(hr))
842                     {
843                         hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
844                         ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "expected WINCODEC_ERR_PALETTEUNAVAILABLE, got %x\n", hr);
845
846                         hr = IWICBitmapFrameDecode_CopyPalette(framedecode, palette);
847                         ok(SUCCEEDED(hr), "CopyPalette failed, hr=%x\n", hr);
848
849                         hr = IWICPalette_GetColorCount(palette, &count);
850                         ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
851                         ok(count == 6, "expected count=6, got %u\n", count);
852
853                         hr = IWICPalette_GetColors(palette, 6, palettedata, &count);
854                         ok(SUCCEEDED(hr), "GetColorCount failed, hr=%x\n", hr);
855                         ok(count == 6, "expected count=6, got %u\n", count);
856                         ok(!memcmp(palettedata, expected_palettedata, sizeof(palettedata)), "unexpected palette data\n");
857
858                         IWICPalette_Release(palette);
859                     }
860
861                     IWICImagingFactory_Release(factory);
862                 }
863
864                 rc.X = 0;
865                 rc.Y = 0;
866                 rc.Width = 8;
867                 rc.Height = 8;
868                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 32, sizeof(imagedata), (BYTE*)imagedata);
869                 ok(SUCCEEDED(hr), "CopyPixels failed, hr=%x\n", hr);
870                 ok(!memcmp(imagedata, expected_imagedata, sizeof(imagedata)), "unexpected image data\n");
871
872                 IWICBitmapFrameDecode_Release(framedecode);
873             }
874
875             hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
876                 &IID_IWICBitmapDecoder, (void**)&decoder2);
877             ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
878             if (SUCCEEDED(hr))
879             {
880                 hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
881                 ok(hr == S_OK, "QueryCapability failed, hr=%x\n", hr);
882                 ok(capability == (WICBitmapDecoderCapabilityCanDecodeAllImages),
883                     "unexpected capabilities: %x\n", capability);
884                 IWICBitmapDecoder_Release(decoder2);
885             }
886
887             IStream_Release(bmpstream);
888         }
889
890         GlobalFree(hbmpdata);
891     }
892
893     IWICBitmapDecoder_Release(decoder);
894 }
895
896 static void test_componentinfo(void)
897 {
898     IWICImagingFactory *factory;
899     IWICComponentInfo *info;
900     IWICBitmapDecoderInfo *decoderinfo;
901     IWICBitmapDecoder *decoder;
902     HRESULT hr;
903     WICBitmapPattern *patterns;
904     UINT pattern_count, pattern_size;
905     WICComponentType type;
906     GUID guidresult;
907     HGLOBAL hbmpdata;
908     char *bmpdata;
909     IStream *bmpstream;
910     BOOL boolresult;
911
912     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
913         &IID_IWICImagingFactory, (void**)&factory);
914     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
915     if (SUCCEEDED(hr))
916     {
917         hr = IWICImagingFactory_CreateComponentInfo(factory, &CLSID_WICBmpDecoder, &info);
918         ok(SUCCEEDED(hr), "CreateComponentInfo failed, hr=%x\n", hr);
919         if (SUCCEEDED(hr))
920         {
921             hr = IWICComponentInfo_GetComponentType(info, &type);
922             ok(SUCCEEDED(hr), "GetComponentType failed, hr=%x\n", hr);
923             ok(type == WICDecoder, "got %i, expected WICDecoder\n", type);
924
925             hr = IWICComponentInfo_QueryInterface(info, &IID_IWICBitmapDecoderInfo, (void**)&decoderinfo);
926             ok(SUCCEEDED(hr), "QueryInterface failed, hr=%x\n", hr);
927             if (SUCCEEDED(hr))
928             {
929                 pattern_count = 0;
930                 pattern_size = 0;
931                 hr = IWICBitmapDecoderInfo_GetPatterns(decoderinfo, 0, NULL, &pattern_count, &pattern_size);
932                 ok(SUCCEEDED(hr), "GetPatterns failed, hr=%x\n", hr);
933                 ok(pattern_count != 0, "pattern count is 0\n");
934                 ok(pattern_size > pattern_count * sizeof(WICBitmapPattern), "size=%i, count=%i\n", pattern_size, pattern_count);
935
936                 patterns = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pattern_size);
937                 hr = IWICBitmapDecoderInfo_GetPatterns(decoderinfo, pattern_size, patterns, &pattern_count, &pattern_size);
938                 ok(SUCCEEDED(hr), "GetPatterns failed, hr=%x\n", hr);
939                 ok(pattern_count != 0, "pattern count is 0\n");
940                 ok(pattern_size > pattern_count * sizeof(WICBitmapPattern), "size=%i, count=%i\n", pattern_size, pattern_count);
941                 ok(patterns[0].Length != 0, "pattern length is 0\n");
942                 ok(patterns[0].Pattern != NULL, "pattern is NULL\n");
943                 ok(patterns[0].Mask != NULL, "mask is NULL\n");
944
945                 pattern_size -= 1;
946                 hr = IWICBitmapDecoderInfo_GetPatterns(decoderinfo, pattern_size, patterns, &pattern_count, &pattern_size);
947                 ok(hr == WINCODEC_ERR_INSUFFICIENTBUFFER, "GetPatterns returned %x, expected WINCODEC_ERR_INSUFFICIENTBUFFER\n", hr);
948
949                 HeapFree(GetProcessHeap(), 0, patterns);
950
951                 hr = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &decoder);
952                 ok(SUCCEEDED(hr), "CreateInstance failed, hr=%x\n", hr);
953                 if (SUCCEEDED(hr))
954                 {
955                     hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
956                     ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
957                     ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatBmp), "unexpected container format\n");
958
959                     IWICBitmapDecoder_Release(decoder);
960                 }
961
962                 hbmpdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(testbmp_rle4));
963                 ok(hbmpdata != 0, "GlobalAlloc failed\n");
964                 if (hbmpdata)
965                 {
966                     bmpdata = GlobalLock(hbmpdata);
967                     memcpy(bmpdata, testbmp_rle4, sizeof(testbmp_rle4));
968                     GlobalUnlock(hbmpdata);
969
970                     hr = CreateStreamOnHGlobal(hbmpdata, FALSE, &bmpstream);
971                     ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
972                     if (SUCCEEDED(hr))
973                     {
974                         boolresult = 0;
975                         hr = IWICBitmapDecoderInfo_MatchesPattern(decoderinfo, bmpstream, &boolresult);
976                         ok(SUCCEEDED(hr), "MatchesPattern failed, hr=%x\n", hr);
977                         ok(boolresult, "pattern not matched\n");
978
979                         IStream_Release(bmpstream);
980                     }
981
982                     GlobalFree(hbmpdata);
983                 }
984
985                 IWICBitmapDecoderInfo_Release(decoderinfo);
986             }
987
988             IWICComponentInfo_Release(info);
989         }
990
991         IWICImagingFactory_Release(factory);
992     }
993 }
994
995 static void test_createfromstream(void)
996 {
997     IWICBitmapDecoder *decoder;
998     IWICImagingFactory *factory;
999     HRESULT hr;
1000     HGLOBAL hbmpdata;
1001     char *bmpdata;
1002     IStream *bmpstream;
1003     GUID guidresult;
1004
1005     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
1006         &IID_IWICImagingFactory, (void**)&factory);
1007     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
1008     if (FAILED(hr)) return;
1009
1010     hbmpdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(testbmp_1bpp));
1011     ok(hbmpdata != 0, "GlobalAlloc failed\n");
1012     if (hbmpdata)
1013     {
1014         bmpdata = GlobalLock(hbmpdata);
1015         memcpy(bmpdata, testbmp_1bpp, sizeof(testbmp_1bpp));
1016         GlobalUnlock(hbmpdata);
1017
1018         hr = CreateStreamOnHGlobal(hbmpdata, FALSE, &bmpstream);
1019         ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
1020         if (SUCCEEDED(hr))
1021         {
1022             hr = IWICImagingFactory_CreateDecoderFromStream(factory, bmpstream,
1023                 NULL, WICDecodeMetadataCacheOnDemand, &decoder);
1024             ok(SUCCEEDED(hr), "CreateDecoderFromStream failed, hr=%x\n", hr);
1025             if (SUCCEEDED(hr))
1026             {
1027                 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
1028                 ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
1029                 ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatBmp), "unexpected container format\n");
1030
1031                 IWICBitmapDecoder_Release(decoder);
1032             }
1033
1034             IStream_Release(bmpstream);
1035         }
1036
1037         GlobalFree(hbmpdata);
1038     }
1039
1040     IWICImagingFactory_Release(factory);
1041 }
1042
1043 /* 1x1 pixel gif, missing trailer */
1044 static unsigned char gifimage_notrailer[] = {
1045 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
1046 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
1047 0x01,0x00
1048 };
1049
1050 static void test_gif_notrailer(void)
1051 {
1052     IWICBitmapDecoder *decoder;
1053     IWICImagingFactory *factory;
1054     HRESULT hr;
1055     IWICStream *gifstream;
1056     IWICBitmapFrameDecode *framedecode;
1057     UINT framecount;
1058
1059     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
1060         &IID_IWICImagingFactory, (void**)&factory);
1061     ok(hr == S_OK, "CoCreateInstance failed, hr=%x\n", hr);
1062     if (FAILED(hr)) return;
1063
1064     hr = IWICImagingFactory_CreateStream(factory, &gifstream);
1065     ok(hr == S_OK, "CreateStream failed, hr=%x\n", hr);
1066     if (SUCCEEDED(hr))
1067     {
1068         hr = IWICStream_InitializeFromMemory(gifstream, gifimage_notrailer,
1069             sizeof(gifimage_notrailer));
1070         ok(hr == S_OK, "InitializeFromMemory failed, hr=%x\n", hr);
1071
1072         if (SUCCEEDED(hr))
1073         {
1074             hr = CoCreateInstance(&CLSID_WICGifDecoder, NULL, CLSCTX_INPROC_SERVER,
1075                 &IID_IWICBitmapDecoder, (void**)&decoder);
1076             ok(hr == S_OK, "CoCreateInstance failed, hr=%x\n", hr);
1077         }
1078
1079         if (SUCCEEDED(hr))
1080         {
1081             hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)gifstream,
1082                 WICDecodeMetadataCacheOnDemand);
1083             ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
1084
1085             if (SUCCEEDED(hr))
1086             {
1087                 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
1088                 ok(hr == S_OK, "GetFrame failed, hr=%x\n", hr);
1089                 if (SUCCEEDED(hr)) IWICBitmapFrameDecode_Release(framedecode);
1090             }
1091
1092             if (SUCCEEDED(hr))
1093             {
1094                 hr = IWICBitmapDecoder_GetFrameCount(decoder, &framecount);
1095                 ok(hr == S_OK, "GetFrameCount failed, hr=%x\n", hr);
1096                 ok(framecount == 1, "framecount=%u\n", framecount);
1097             }
1098
1099             IWICBitmapDecoder_Release(decoder);
1100         }
1101
1102         IWICStream_Release(gifstream);
1103     }
1104
1105     IWICImagingFactory_Release(factory);
1106 }
1107
1108 START_TEST(bmpformat)
1109 {
1110     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1111
1112     test_decode_24bpp();
1113     test_decode_1bpp();
1114     test_decode_4bpp();
1115     test_decode_rle8();
1116     test_decode_rle4();
1117     test_componentinfo();
1118     test_createfromstream();
1119     test_gif_notrailer();
1120
1121     CoUninitialize();
1122 }