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