windowscodecs: Implement CopyPixels 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     BYTE imagedata[36] = {1};
60     const BYTE expected_imagedata[36] = {
61         255,0,255, 255,255,255,
62         255,0,0,   255,255,0,
63         0,0,0,     0,255,0};
64     WICRect rc;
65
66     hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
67         &IID_IWICBitmapDecoder, (void**)&decoder);
68     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
69     if (!SUCCEEDED(hr)) return;
70
71     hbmpdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(testbmp_24bpp));
72     ok(hbmpdata != 0, "GlobalAlloc failed\n");
73     if (hbmpdata)
74     {
75         bmpdata = GlobalLock(hbmpdata);
76         memcpy(bmpdata, testbmp_24bpp, sizeof(testbmp_24bpp));
77         GlobalUnlock(hbmpdata);
78
79         hr = CreateStreamOnHGlobal(hbmpdata, FALSE, &bmpstream);
80         ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
81         if (SUCCEEDED(hr))
82         {
83             hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
84             ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
85
86             hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
87             ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
88             ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatBmp), "unexpected container format\n");
89
90             hr = IWICBitmapDecoder_GetFrameCount(decoder, &count);
91             ok(SUCCEEDED(hr), "GetFrameCount failed, hr=%x\n", hr);
92             ok(count == 1, "unexpected count %u\n", count);
93
94             hr = IWICBitmapDecoder_GetFrame(decoder, 1, &framedecode);
95             ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
96
97             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
98             ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
99             if (SUCCEEDED(hr))
100             {
101                 hr = IWICBitmapFrameDecode_GetSize(framedecode, &width, &height);
102                 ok(SUCCEEDED(hr), "GetSize failed, hr=%x\n", hr);
103                 ok(width == 2, "expected width=2, got %u\n", width);
104                 ok(height == 3, "expected height=2, got %u\n", height);
105
106                 hr = IWICBitmapFrameDecode_GetResolution(framedecode, &dpiX, &dpiY);
107                 ok(SUCCEEDED(hr), "GetResolution failed, hr=%x\n", hr);
108                 ok(dpiX == 96.0, "expected dpiX=96.0, got %f\n", dpiX);
109                 ok(dpiY == 96.0, "expected dpiY=96.0, got %f\n", dpiY);
110
111                 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &guidresult);
112                 ok(SUCCEEDED(hr), "GetPixelFormat failed, hr=%x\n", hr);
113                 ok(IsEqualGUID(&guidresult, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
114
115                 rc.X = 0;
116                 rc.Y = 0;
117                 rc.Width = 3;
118                 rc.Height = 3;
119                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 6, sizeof(imagedata), imagedata);
120                 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
121
122                 rc.X = -1;
123                 rc.Y = 0;
124                 rc.Width = 2;
125                 rc.Height = 3;
126                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 6, sizeof(imagedata), imagedata);
127                 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
128
129                 rc.X = 0;
130                 rc.Y = 0;
131                 rc.Width = 2;
132                 rc.Height = 3;
133                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 4, sizeof(imagedata), imagedata);
134                 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
135
136                 rc.X = 0;
137                 rc.Y = 0;
138                 rc.Width = 2;
139                 rc.Height = 3;
140                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 4, 5, imagedata);
141                 ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %x\n", hr);
142
143                 rc.X = 0;
144                 rc.Y = 0;
145                 rc.Width = 2;
146                 rc.Height = 3;
147                 hr = IWICBitmapFrameDecode_CopyPixels(framedecode, &rc, 6, sizeof(imagedata), imagedata);
148                 ok(SUCCEEDED(hr), "CopyPixels failed, hr=%x\n", hr);
149                 ok(!memcmp(imagedata, expected_imagedata, sizeof(imagedata)), "unexpected image data\n");
150
151                 IWICBitmapFrameDecode_Release(framedecode);
152             }
153
154             /* cannot initialize twice */
155             hr = IWICBitmapDecoder_Initialize(decoder, bmpstream, WICDecodeMetadataCacheOnLoad);
156             ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
157
158             /* cannot querycapability after initialize */
159             hr = IWICBitmapDecoder_QueryCapability(decoder, bmpstream, &capability);
160             todo_wine ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
161
162             hr = CoCreateInstance(&CLSID_WICBmpDecoder, NULL, CLSCTX_INPROC_SERVER,
163                 &IID_IWICBitmapDecoder, (void**)&decoder2);
164             ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
165             if (SUCCEEDED(hr))
166             {
167                 hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
168                 todo_wine ok(hr == S_OK, "QueryCapability failed, hr=%x\n", hr);
169                 todo_wine ok(capability == (WICBitmapDecoderCapabilityCanDecodeAllImages),
170                     "unexpected capabilities: %x\n", capability);
171
172                 /* cannot initialize after querycapability */
173                 hr = IWICBitmapDecoder_Initialize(decoder2, bmpstream, WICDecodeMetadataCacheOnLoad);
174                 todo_wine ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
175
176                 /* cannot querycapability twice */
177                 hr = IWICBitmapDecoder_QueryCapability(decoder2, bmpstream, &capability);
178                 todo_wine ok(hr == WINCODEC_ERR_WRONGSTATE, "expected WINCODEC_ERR_WRONGSTATE, hr=%x\n", hr);
179             }
180
181             IStream_Release(bmpstream);
182         }
183
184         GlobalFree(hbmpdata);
185     }
186
187     IWICBitmapDecoder_Release(decoder);
188 }
189
190 START_TEST(bmpformat)
191 {
192     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
193
194     test_decode_24bpp();
195
196     CoUninitialize();
197 }