windowscodecs: Add test for BMP 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
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "initguid.h"
25 #include "objbase.h"
26 #include "wincodec.h"
27 #include "wine/test.h"
28
29 static const char testbmp_24bpp[] = {
30     /* BITMAPFILEHEADER */
31     66,77, /* "BM" */
32     50,0,0,0, /* file size */
33     0,0,0,0, /* reserved */
34     26,0,0,0, /* offset to bits */
35     /* BITMAPCOREHEADER */
36     12,0,0,0, /* header size */
37     2,0, /* width */
38     3,0, /* height */
39     1,0, /* planes */
40     24,0, /* bit count */
41     /* bits */
42     0,0,0,     0,255,0,     0,0,
43     255,0,0,   255,255,0,   0,0,
44     255,0,255, 255,255,255, 0,0
45 };
46
47 static void test_decode_24bpp(void)
48 {
49     IWICBitmapDecoder *decoder, *decoder2;
50     IWICBitmapFrameDecode *framedecode;
51     HRESULT hr;
52     HGLOBAL hbmpdata;
53     char *bmpdata;
54     IStream *bmpstream;
55     DWORD capability=0;
56     GUID guidresult;
57     UINT count=0, width=0, height=0;
58     double dpiX, dpiY;
59
60     hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
61         &IID_IWICBitmapDecoder, (void**)&decoder);
62     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
63     if (!SUCCEEDED(hr)) return;
64
65     hbmpdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(testbmp_24bpp));
66     ok(hbmpdata != 0, "GlobalAlloc failed\n");
67     if (hbmpdata)
68     {
69         bmpdata = GlobalLock(hbmpdata);
70         memcpy(bmpdata, testbmp_24bpp, sizeof(testbmp_24bpp));
71         GlobalUnlock(hbmpdata);
72
73         hr = CreateStreamOnHGlobal(hbmpdata, FALSE, &bmpstream);
74         ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
75         if (SUCCEEDED(hr))
76         {
77             hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
78             ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
79
80             hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
81             ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
82             ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatBmp), "unexpected container format\n");
83
84             hr = IWICBitmapDecoder_GetFrameCount(decoder, &count);
85             ok(SUCCEEDED(hr), "GetFrameCount failed, hr=%x\n", hr);
86             ok(count == 1, "unexpected count %u\n", count);
87
88             hr = IWICBitmapDecoder_GetFrame(decoder, 1, &framedecode);
89             ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
90
91             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
92             ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
93             if (SUCCEEDED(hr))
94             {
95                 hr = IWICBitmapFrameDecode_GetSize(framedecode, &width, &height);
96                 ok(SUCCEEDED(hr), "GetSize failed, hr=%x\n", hr);
97                 ok(width == 2, "expected width=2, got %u\n", width);
98                 ok(height == 3, "expected height=2, got %u\n", height);
99
100                 hr = IWICBitmapFrameDecode_GetResolution(framedecode, &dpiX, &dpiY);
101                 ok(SUCCEEDED(hr), "GetResolution failed, hr=%x\n", hr);
102                 ok(dpiX == 96.0, "expected dpiX=96.0, got %f\n", dpiX);
103                 ok(dpiY == 96.0, "expected dpiY=96.0, got %f\n", dpiY);
104
105                 IWICBitmapFrameDecode_Release(framedecode);
106             }
107
108             /* cannot initialize twice */
109             hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
110             ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
111
112             /* cannot querycapability after initialize */
113             hr = IWICBitmapDecoder_QueryCapability(decoder, bmpstream, &capability);
114             todo_wine ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
115
116             hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
117                 &IID_IWICBitmapDecoder, (void**)&decoder2);
118             ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
119             if (SUCCEEDED(hr))
120             {
121                 hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
122                 todo_wine ok(hr == S_OK, "QueryCapability failed, hr=%x\n", hr);
123                 todo_wine ok(capability == (WICBitmapDecoderCapabilityCanDecodeAllImages),
124                     "unexpected capabilities: %x\n", capability);
125
126                 /* cannot initialize after querycapability */
127                 hr = IWICBitmapDecoder_Initialize(decoder2, bmpstream, WICDecodeMetadataCacheOnLoad);
128                 todo_wine ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
129
130                 /* cannot querycapability twice */
131                 hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
132                 todo_wine ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
133             }
134
135             IStream_Release(bmpstream);
136         }
137
138         GlobalFree(hbmpdata);
139     }
140
141     IWICBitmapDecoder_Release(decoder);
142 }
143
144 START_TEST(bmpformat)
145 {
146     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
147
148     test_decode_24bpp();
149
150     CoUninitialize();
151 }