windowscodecs: Add tests for IWICBitmap.
[wine.git] / dlls / windowscodecs / tests / bitmap.c
blobebcefb9ea6c7b918d7fb31f10b04a39ae017f7a2
1 /*
2 * Copyright 2012 Vincent Povirk for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
20 #include <math.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "objbase.h"
26 #include "wincodec.h"
27 #include "wine/test.h"
29 static IWICImagingFactory *factory;
31 static void test_createbitmap(void)
33 HRESULT hr;
34 IWICBitmap *bitmap;
35 IWICPalette *palette;
36 IWICBitmapLock *lock, *lock2;
37 WICBitmapPaletteType palettetype;
38 int i;
39 WICRect rc;
40 const BYTE bitmap_data[27] = {
41 128,128,255, 128,128,128, 128,255,128,
42 128,128,128, 128,128,128, 255,255,255,
43 255,128,128, 255,255,255, 255,255,255};
44 BYTE returned_data[27] = {0};
45 BYTE *lock_buffer=NULL, *base_lock_buffer=NULL;
46 UINT lock_buffer_size=0;
47 UINT lock_buffer_stride=0;
48 WICPixelFormatGUID pixelformat = {0};
49 UINT width=0, height=0;
50 double dpix=10.0, dpiy=10.0;
51 int can_lock_null = 1;
53 hr = IWICImagingFactory_CreateBitmap(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
54 WICBitmapCacheOnLoad, &bitmap);
55 todo_wine ok(hr == S_OK, "IWICImagingFactory_CreateBitmap failed hr=%x\n", hr);
57 if (FAILED(hr))
58 return;
60 hr = IWICImagingFactory_CreatePalette(factory, &palette);
61 ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
63 /* Palette is unavailable until explicitly set */
64 hr = IWICBitmap_CopyPalette(bitmap, palette);
65 ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "IWICBitmap_CopyPalette failed hr=%x\n", hr);
67 hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray256, FALSE);
68 ok(hr == S_OK, "IWICPalette_InitializePredefined failed hr=%x\n", hr);
70 hr = IWICBitmap_SetPalette(bitmap, palette);
71 ok(hr == S_OK, "IWICBitmap_SetPalette failed hr=%x\n", hr);
73 hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray4, FALSE);
74 ok(hr == S_OK, "IWICPalette_InitializePredefined failed hr=%x\n", hr);
76 hr = IWICBitmap_CopyPalette(bitmap, palette);
77 ok(hr == S_OK, "IWICBitmap_CopyPalette failed hr=%x\n", hr);
79 hr = IWICPalette_GetType(palette, &palettetype);
80 ok(hr == S_OK, "IWICPalette_GetType failed hr=%x\n", hr);
81 ok(palettetype == WICBitmapPaletteTypeFixedGray256,
82 "expected WICBitmapPaletteTypeFixedGray256, got %x\n", palettetype);
84 IWICPalette_Release(palette);
86 /* pixel data is initially zeroed */
87 hr = IWICBitmap_CopyPixels(bitmap, NULL, 9, 27, returned_data);
88 ok(hr == S_OK, "IWICBitmap_CopyPixels failed hr=%x\n", hr);
90 for (i=0; i<27; i++)
91 ok(returned_data[i] == 0, "returned_data[%i] == %i\n", i, returned_data[i]);
93 /* Invalid lock rects */
94 rc.X = rc.Y = 0;
95 rc.Width = 4;
96 rc.Height = 3;
97 hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
98 ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
99 if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
101 rc.Width = 3;
102 rc.Height = 4;
103 hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
104 ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
105 if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
107 rc.Height = 3;
108 rc.X = 4;
109 hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
110 ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
111 if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
113 rc.X = 0;
114 rc.Y = 4;
115 hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
116 ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
117 if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
119 /* NULL lock rect */
120 hr = IWICBitmap_Lock(bitmap, NULL, WICBitmapLockRead, &lock);
121 ok(hr == S_OK || broken(hr == E_INVALIDARG) /* winxp */, "IWICBitmap_Lock failed hr=%x\n", hr);
123 if (SUCCEEDED(hr))
125 /* entire bitmap is locked */
126 hr = IWICBitmapLock_GetSize(lock, &width, &height);
127 ok(hr == S_OK, "IWICBitmapLock_GetSize failed hr=%x\n", hr);
128 ok(width == 3, "got %d, expected 3\n", width);
129 ok(height == 3, "got %d, expected 3\n", height);
131 IWICBitmapLock_Release(lock);
133 else
134 can_lock_null = 0;
136 /* lock with a valid rect */
137 rc.Y = 0;
138 hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
139 ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
140 if (SUCCEEDED(hr))
142 hr = IWICBitmapLock_GetStride(lock, &lock_buffer_stride);
143 ok(hr == S_OK, "IWICBitmapLock_GetStride failed hr=%x\n", hr);
144 /* stride is divisible by 4 */
145 ok(lock_buffer_stride == 12, "got %i, expected 12\n", lock_buffer_stride);
147 hr = IWICBitmapLock_GetDataPointer(lock, &lock_buffer_size, &lock_buffer);
148 ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
149 /* buffer size does not include padding from the last row */
150 ok(lock_buffer_size == 33, "got %i, expected 33\n", lock_buffer_size);
151 ok(lock_buffer != NULL, "got NULL data pointer\n");
152 base_lock_buffer = lock_buffer;
154 hr = IWICBitmapLock_GetPixelFormat(lock, &pixelformat);
155 ok(hr == S_OK, "IWICBitmapLock_GetPixelFormat failed hr=%x\n", hr);
156 ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
158 hr = IWICBitmapLock_GetSize(lock, &width, &height);
159 ok(hr == S_OK, "IWICBitmapLock_GetSize failed hr=%x\n", hr);
160 ok(width == 3, "got %d, expected 3\n", width);
161 ok(height == 3, "got %d, expected 3\n", height);
163 /* We can have multiple simultaneous read locks */
164 hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock2);
165 ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
167 if (SUCCEEDED(hr))
169 hr = IWICBitmapLock_GetDataPointer(lock2, &lock_buffer_size, &lock_buffer);
170 ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
171 ok(lock_buffer_size == 33, "got %i, expected 33\n", lock_buffer_size);
172 ok(lock_buffer == base_lock_buffer, "got %p, expected %p\n", lock_buffer, base_lock_buffer);
174 IWICBitmapLock_Release(lock2);
177 if (can_lock_null) /* this hangs on xp/vista */
179 /* But not a read and a write lock */
180 hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock2);
181 ok(hr == WINCODEC_ERR_ALREADYLOCKED, "IWICBitmap_Lock failed hr=%x\n", hr);
184 /* But we don't need a write lock to write */
185 for (i=0; i<3; i++)
186 memcpy(base_lock_buffer + lock_buffer_stride*i, bitmap_data + i*9, 9);
188 IWICBitmapLock_Release(lock);
191 /* test that the data we wrote is returned by CopyPixels */
192 hr = IWICBitmap_CopyPixels(bitmap, NULL, 9, 27, returned_data);
193 ok(hr == S_OK, "IWICBitmap_CopyPixels failed hr=%x\n", hr);
195 for (i=0; i<27; i++)
196 ok(returned_data[i] == bitmap_data[i], "returned_data[%i] == %i\n", i, returned_data[i]);
198 /* try a valid partial rect, and write mode */
199 rc.X = 2;
200 rc.Y = 0;
201 rc.Width = 1;
202 rc.Height = 2;
203 hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock);
204 ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
206 if (SUCCEEDED(hr))
208 if (can_lock_null) /* this hangs on xp/vista */
210 /* Can't lock again while locked for writing */
211 hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock2);
212 ok(hr == WINCODEC_ERR_ALREADYLOCKED, "IWICBitmap_Lock failed hr=%x\n", hr);
214 hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock2);
215 ok(hr == WINCODEC_ERR_ALREADYLOCKED, "IWICBitmap_Lock failed hr=%x\n", hr);
218 hr = IWICBitmapLock_GetStride(lock, &lock_buffer_stride);
219 ok(hr == S_OK, "IWICBitmapLock_GetStride failed hr=%x\n", hr);
220 ok(lock_buffer_stride == 12, "got %i, expected 12\n", lock_buffer_stride);
222 hr = IWICBitmapLock_GetDataPointer(lock, &lock_buffer_size, &lock_buffer);
223 ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
224 ok(lock_buffer_size == 15, "got %i, expected 15\n", lock_buffer_size);
225 ok(lock_buffer == base_lock_buffer+6, "got %p, expected %p+6\n", lock_buffer, base_lock_buffer);
227 hr = IWICBitmapLock_GetPixelFormat(lock, &pixelformat);
228 ok(hr == S_OK, "IWICBitmapLock_GetPixelFormat failed hr=%x\n", hr);
229 ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
231 hr = IWICBitmapLock_GetSize(lock, &width, &height);
232 ok(hr == S_OK, "IWICBitmapLock_GetSize failed hr=%x\n", hr);
233 ok(width == 1, "got %d, expected 1\n", width);
234 ok(height == 2, "got %d, expected 2\n", height);
236 IWICBitmapLock_Release(lock);
239 hr = IWICBitmap_GetPixelFormat(bitmap, &pixelformat);
240 ok(hr == S_OK, "IWICBitmap_GetPixelFormat failed hr=%x\n", hr);
241 ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
243 hr = IWICBitmap_GetResolution(bitmap, &dpix, &dpiy);
244 ok(hr == S_OK, "IWICBitmap_GetResolution failed hr=%x\n", hr);
245 ok(dpix == 0.0, "got %f, expected 0.0\n", dpix);
246 ok(dpiy == 0.0, "got %f, expected 0.0\n", dpiy);
248 hr = IWICBitmap_SetResolution(bitmap, 12.0, 34.0);
249 ok(hr == S_OK, "IWICBitmap_SetResolution failed hr=%x\n", hr);
251 hr = IWICBitmap_GetResolution(bitmap, &dpix, &dpiy);
252 ok(hr == S_OK, "IWICBitmap_GetResolution failed hr=%x\n", hr);
253 ok(dpix == 12.0, "got %f, expected 12.0\n", dpix);
254 ok(dpiy == 34.0, "got %f, expected 34.0\n", dpiy);
256 hr = IWICBitmap_GetSize(bitmap, &width, &height);
257 ok(hr == S_OK, "IWICBitmap_GetSize failed hr=%x\n", hr);
258 ok(width == 3, "got %d, expected 3\n", width);
259 ok(height == 3, "got %d, expected 3\n", height);
261 IWICBitmap_Release(bitmap);
264 START_TEST(bitmap)
266 HRESULT hr;
268 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
270 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
271 &IID_IWICImagingFactory, (void**)&factory);
272 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
274 test_createbitmap();
276 IWICImagingFactory_Release(factory);
278 CoUninitialize();