1 /*
2  * Copyright 2012 Vincent Povirk for CodeWeavers
3  * Copyright 2012 Dmitry Timoshkov
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19 
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <assert.h>
23 #include <math.h>
24 
25 #define COBJMACROS
26 #define CONST_VTABLE
27 
28 #include "windef.h"
29 #include "objbase.h"
30 #include "wincodec.h"
31 #include "wine/test.h"
32 
33 static IWICImagingFactory *factory;
34 
35 static HRESULT WINAPI bitmapsource_QueryInterface(IWICBitmapSource *iface, REFIID iid, void **ppv)
36 {
37     if (IsEqualIID(&IID_IUnknown, iid) ||
38         IsEqualIID(&IID_IWICBitmapSource, iid))
39     {
40         *ppv = iface;
41     }
42     else
43     {
44         *ppv = NULL;
45         return E_NOINTERFACE;
46     }
47 
48     return S_OK;
49 }
50 
51 static ULONG WINAPI bitmapsource_AddRef(IWICBitmapSource *iface)
52 {
53     return 2;
54 }
55 
56 static ULONG WINAPI bitmapsource_Release(IWICBitmapSource *iface)
57 {
58     return 1;
59 }
60 
61 static HRESULT WINAPI bitmapsource_GetSize(IWICBitmapSource *iface, UINT *width, UINT *height)
62 {
63     *width = *height = 10;
64     return S_OK;
65 }
66 
67 static HRESULT WINAPI bitmapsource_GetPixelFormat(IWICBitmapSource *iface,
68     WICPixelFormatGUID *format)
69 {
70     return E_NOTIMPL;
71 }
72 
73 static HRESULT WINAPI bitmapsource_GetResolution(IWICBitmapSource *iface,
74     double *dpiX, double *dpiY)
75 {
76     return E_NOTIMPL;
77 }
78 
79 static HRESULT WINAPI bitmapsource_CopyPalette(IWICBitmapSource *iface,
80     IWICPalette *palette)
81 {
82     return E_NOTIMPL;
83 }
84 
85 static WICRect g_rect;
86 static BOOL called_CopyPixels;
87 
88 static HRESULT WINAPI bitmapsource_CopyPixels(IWICBitmapSource *iface,
89     const WICRect *rc, UINT stride, UINT buffer_size, BYTE *buffer)
90 {
91     if (rc) g_rect = *rc;
92     called_CopyPixels = TRUE;
93     return S_OK;
94 }
95 
96 static const IWICBitmapSourceVtbl sourcevtbl = {
97     bitmapsource_QueryInterface,
98     bitmapsource_AddRef,
99     bitmapsource_Release,
100     bitmapsource_GetSize,
101     bitmapsource_GetPixelFormat,
102     bitmapsource_GetResolution,
103     bitmapsource_CopyPalette,
104     bitmapsource_CopyPixels
105 };
106 
107 static IWICBitmapSource bitmapsource = { &sourcevtbl };
108 
109 static HBITMAP create_dib(int width, int height, int bpp, LOGPALETTE *pal, const void *data)
110 {
111     char bmibuf[sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 255];
112     BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
113     void *bits;
114     HBITMAP hdib;
115     BITMAP bm;
116 
117     memset(bmibuf, 0, sizeof(bmibuf));
118     bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
119     bmi->bmiHeader.biWidth = width;
120     bmi->bmiHeader.biHeight = -height;
121     bmi->bmiHeader.biBitCount = bpp;
122     bmi->bmiHeader.biPlanes = 1;
123     bmi->bmiHeader.biCompression = BI_RGB;
124     if (pal)
125     {
126         WORD i;
127 
128         assert(pal->palNumEntries <= 256);
129         for (i = 0; i < pal->palNumEntries; i++)
130         {
131             bmi->bmiColors[i].rgbRed = pal->palPalEntry[i].peRed;
132             bmi->bmiColors[i].rgbGreen = pal->palPalEntry[i].peGreen;
133             bmi->bmiColors[i].rgbBlue = pal->palPalEntry[i].peBlue;
134             bmi->bmiColors[i].rgbReserved = 0;
135         }
136 
137         bmi->bmiHeader.biClrUsed = pal->palNumEntries;
138         bmi->bmiHeader.biClrImportant = pal->palNumEntries;
139     }
140     hdib = CreateDIBSection(0, bmi, DIB_RGB_COLORS, &bits, NULL, 0);
141     ok(hdib != 0, "CreateDIBSection(%dx%d,%d bpp) failed\n", width, height, bpp);
142 
143     GetObjectW(hdib, sizeof(bm), &bm);
144     ok(bm.bmWidth == width, "expected %d, got %d\n", width, bm.bmWidth);
145     ok(bm.bmHeight == height, "expected %d, got %d\n", height, bm.bmHeight);
146     ok(bm.bmPlanes == 1, "expected 1, got %d\n", bm.bmPlanes);
147     ok(bm.bmBitsPixel == bpp, "expected %d, got %d\n", bpp, bm.bmBitsPixel);
148 
149     if (data) memcpy(bits, data, bm.bmWidthBytes * bm.bmHeight);
150 
151     return hdib;
152 }
153 
154 static void test_createbitmap(void)
155 {
156     HRESULT hr;
157     IWICBitmap *bitmap;
158     IWICPalette *palette;
159     IWICBitmapLock *lock, *lock2;
160     WICBitmapPaletteType palettetype;
161     int i;
162     WICRect rc;
163     const BYTE bitmap_data[27] = {
164         128,128,255, 128,128,128, 128,255,128,
165         128,128,128, 128,128,128, 255,255,255,
166         255,128,128, 255,255,255, 255,255,255};
167     BYTE returned_data[27] = {0};
168     BYTE *lock_buffer=NULL, *base_lock_buffer=NULL;
169     UINT lock_buffer_size=0;
170     UINT lock_buffer_stride=0;
171     WICPixelFormatGUID pixelformat = {0};
172     UINT width=0, height=0;
173     double dpix=10.0, dpiy=10.0;
174     int can_lock_null = 1;
175 
176     hr = IWICImagingFactory_CreateBitmap(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
177         WICBitmapCacheOnLoad, &bitmap);
178     ok(hr == S_OK, "IWICImagingFactory_CreateBitmap failed hr=%x\n", hr);
179 
180     if (FAILED(hr))
181         return;
182 
183     hr = IWICImagingFactory_CreatePalette(factory, &palette);
184     ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
185 
186     /* Palette is unavailable until explicitly set */
187     hr = IWICBitmap_CopyPalette(bitmap, palette);
188     ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "IWICBitmap_CopyPalette failed hr=%x\n", hr);
189 
190     hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray256, FALSE);
191     ok(hr == S_OK, "IWICPalette_InitializePredefined failed hr=%x\n", hr);
192 
193     hr = IWICBitmap_SetPalette(bitmap, palette);
194     ok(hr == S_OK, "IWICBitmap_SetPalette failed hr=%x\n", hr);
195 
196     hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray4, FALSE);
197     ok(hr == S_OK, "IWICPalette_InitializePredefined failed hr=%x\n", hr);
198 
199     hr = IWICBitmap_CopyPalette(bitmap, palette);
200     ok(hr == S_OK, "IWICBitmap_CopyPalette failed hr=%x\n", hr);
201 
202     hr = IWICPalette_GetType(palette, &palettetype);
203     ok(hr == S_OK, "IWICPalette_GetType failed hr=%x\n", hr);
204     ok(palettetype == WICBitmapPaletteTypeFixedGray256,
205         "expected WICBitmapPaletteTypeFixedGray256, got %x\n", palettetype);
206 
207     IWICPalette_Release(palette);
208 
209     /* pixel data is initially zeroed */
210     hr = IWICBitmap_CopyPixels(bitmap, NULL, 9, 27, returned_data);
211     ok(hr == S_OK, "IWICBitmap_CopyPixels failed hr=%x\n", hr);
212 
213     for (i=0; i<27; i++)
214         ok(returned_data[i] == 0, "returned_data[%i] == %i\n", i, returned_data[i]);
215 
216     /* Invalid lock rects */
217     rc.X = rc.Y = 0;
218     rc.Width = 4;
219     rc.Height = 3;
220     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
221     ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
222     if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
223 
224     rc.Width = 3;
225     rc.Height = 4;
226     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
227     ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
228     if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
229 
230     rc.Height = 3;
231     rc.X = 4;
232     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
233     ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
234     if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
235 
236     rc.X = 0;
237     rc.Y = 4;
238     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
239     ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
240     if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
241 
242     /* NULL lock rect */
243     hr = IWICBitmap_Lock(bitmap, NULL, WICBitmapLockRead, &lock);
244     ok(hr == S_OK || broken(hr == E_INVALIDARG) /* winxp */, "IWICBitmap_Lock failed hr=%x\n", hr);
245 
246     if (SUCCEEDED(hr))
247     {
248         /* entire bitmap is locked */
249         hr = IWICBitmapLock_GetSize(lock, &width, &height);
250         ok(hr == S_OK, "IWICBitmapLock_GetSize failed hr=%x\n", hr);
251         ok(width == 3, "got %d, expected 3\n", width);
252         ok(height == 3, "got %d, expected 3\n", height);
253 
254         IWICBitmapLock_Release(lock);
255     }
256     else
257         can_lock_null = 0;
258 
259     /* lock with a valid rect */
260     rc.Y = 0;
261     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
262     ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
263     if (SUCCEEDED(hr))
264     {
265         hr = IWICBitmapLock_GetStride(lock, &lock_buffer_stride);
266         ok(hr == S_OK, "IWICBitmapLock_GetStride failed hr=%x\n", hr);
267         /* stride is divisible by 4 */
268         ok(lock_buffer_stride == 12, "got %i, expected 12\n", lock_buffer_stride);
269 
270         hr = IWICBitmapLock_GetDataPointer(lock, &lock_buffer_size, &lock_buffer);
271         ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
272         /* buffer size does not include padding from the last row */
273         ok(lock_buffer_size == 33, "got %i, expected 33\n", lock_buffer_size);
274         ok(lock_buffer != NULL, "got NULL data pointer\n");
275         base_lock_buffer = lock_buffer;
276 
277         hr = IWICBitmapLock_GetPixelFormat(lock, &pixelformat);
278         ok(hr == S_OK, "IWICBitmapLock_GetPixelFormat failed hr=%x\n", hr);
279         ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
280 
281         hr = IWICBitmapLock_GetSize(lock, &width, &height);
282         ok(hr == S_OK, "IWICBitmapLock_GetSize failed hr=%x\n", hr);
283         ok(width == 3, "got %d, expected 3\n", width);
284         ok(height == 3, "got %d, expected 3\n", height);
285 
286         /* We can have multiple simultaneous read locks */
287         hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock2);
288         ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
289 
290         if (SUCCEEDED(hr))
291         {
292             hr = IWICBitmapLock_GetDataPointer(lock2, &lock_buffer_size, &lock_buffer);
293             ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
294             ok(lock_buffer_size == 33, "got %i, expected 33\n", lock_buffer_size);
295             ok(lock_buffer == base_lock_buffer, "got %p, expected %p\n", lock_buffer, base_lock_buffer);
296 
297             IWICBitmapLock_Release(lock2);
298         }
299 
300         if (can_lock_null) /* this hangs on xp/vista */
301         {
302             /* But not a read and a write lock */
303             hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock2);
304             ok(hr == WINCODEC_ERR_ALREADYLOCKED, "IWICBitmap_Lock failed hr=%x\n", hr);
305         }
306 
307         /* But we don't need a write lock to write */
308         if (base_lock_buffer)
309         {
310             for (i=0; i<3; i++)
311                 memcpy(base_lock_buffer + lock_buffer_stride*i, bitmap_data + i*9, 9);
312         }
313 
314         IWICBitmapLock_Release(lock);
315     }
316 
317     /* test that the data we wrote is returned by CopyPixels */
318     hr = IWICBitmap_CopyPixels(bitmap, NULL, 9, 27, returned_data);
319     ok(hr == S_OK, "IWICBitmap_CopyPixels failed hr=%x\n", hr);
320 
321     for (i=0; i<27; i++)
322         ok(returned_data[i] == bitmap_data[i], "returned_data[%i] == %i\n", i, returned_data[i]);
323 
324     /* try a valid partial rect, and write mode */
325     rc.X = 2;
326     rc.Y = 0;
327     rc.Width = 1;
328     rc.Height = 2;
329     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock);
330     ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
331 
332     if (SUCCEEDED(hr))
333     {
334         if (can_lock_null) /* this hangs on xp/vista */
335         {
336             /* Can't lock again while locked for writing */
337             hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock2);
338             ok(hr == WINCODEC_ERR_ALREADYLOCKED, "IWICBitmap_Lock failed hr=%x\n", hr);
339 
340             hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock2);
341             ok(hr == WINCODEC_ERR_ALREADYLOCKED, "IWICBitmap_Lock failed hr=%x\n", hr);
342         }
343 
344         hr = IWICBitmapLock_GetStride(lock, &lock_buffer_stride);
345         ok(hr == S_OK, "IWICBitmapLock_GetStride failed hr=%x\n", hr);
346         ok(lock_buffer_stride == 12, "got %i, expected 12\n", lock_buffer_stride);
347 
348         hr = IWICBitmapLock_GetDataPointer(lock, &lock_buffer_size, &lock_buffer);
349         ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
350         ok(lock_buffer_size == 15, "got %i, expected 15\n", lock_buffer_size);
351         ok(lock_buffer == base_lock_buffer+6, "got %p, expected %p+6\n", lock_buffer, base_lock_buffer);
352 
353         hr = IWICBitmapLock_GetPixelFormat(lock, &pixelformat);
354         ok(hr == S_OK, "IWICBitmapLock_GetPixelFormat failed hr=%x\n", hr);
355         ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
356 
357         hr = IWICBitmapLock_GetSize(lock, &width, &height);
358         ok(hr == S_OK, "IWICBitmapLock_GetSize failed hr=%x\n", hr);
359         ok(width == 1, "got %d, expected 1\n", width);
360         ok(height == 2, "got %d, expected 2\n", height);
361 
362         IWICBitmapLock_Release(lock);
363     }
364 
365     hr = IWICBitmap_GetPixelFormat(bitmap, &pixelformat);
366     ok(hr == S_OK, "IWICBitmap_GetPixelFormat failed hr=%x\n", hr);
367     ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
368 
369     hr = IWICBitmap_GetResolution(bitmap, &dpix, &dpiy);
370     ok(hr == S_OK, "IWICBitmap_GetResolution failed hr=%x\n", hr);
371     ok(dpix == 0.0, "got %f, expected 0.0\n", dpix);
372     ok(dpiy == 0.0, "got %f, expected 0.0\n", dpiy);
373 
374     hr = IWICBitmap_SetResolution(bitmap, 12.0, 34.0);
375     ok(hr == S_OK, "IWICBitmap_SetResolution failed hr=%x\n", hr);
376 
377     hr = IWICBitmap_GetResolution(bitmap, &dpix, &dpiy);
378     ok(hr == S_OK, "IWICBitmap_GetResolution failed hr=%x\n", hr);
379     ok(dpix == 12.0, "got %f, expected 12.0\n", dpix);
380     ok(dpiy == 34.0, "got %f, expected 34.0\n", dpiy);
381 
382     hr = IWICBitmap_GetSize(bitmap, &width, &height);
383     ok(hr == S_OK, "IWICBitmap_GetSize failed hr=%x\n", hr);
384     ok(width == 3, "got %d, expected 3\n", width);
385     ok(height == 3, "got %d, expected 3\n", height);
386 
387     IWICBitmap_Release(bitmap);
388 }
389 
390 static void test_createbitmapfromsource(void)
391 {
392     HRESULT hr;
393     IWICBitmap *bitmap, *bitmap2;
394     IWICPalette *palette;
395     IWICBitmapLock *lock;
396     int i;
397     WICRect rc;
398     const BYTE bitmap_data[27] = {
399         128,128,255, 128,128,128, 128,255,128,
400         128,128,128, 128,128,128, 255,255,255,
401         255,128,128, 255,255,255, 255,255,255};
402     BYTE returned_data[27] = {0};
403     BYTE *lock_buffer=NULL;
404     UINT lock_buffer_stride=0;
405     UINT lock_buffer_size=0;
406     WICPixelFormatGUID pixelformat = {0};
407     UINT width=0, height=0;
408     double dpix=10.0, dpiy=10.0;
409     UINT count;
410     WICBitmapPaletteType palette_type;
411 
412     hr = IWICImagingFactory_CreateBitmap(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
413         WICBitmapCacheOnLoad, &bitmap);
414     ok(hr == S_OK, "IWICImagingFactory_CreateBitmap failed hr=%x\n", hr);
415 
416     if (FAILED(hr))
417         return;
418 
419     hr = IWICImagingFactory_CreatePalette(factory, &palette);
420     ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
421 
422     hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray256, FALSE);
423     ok(hr == S_OK, "IWICPalette_InitializePredefined failed hr=%x\n", hr);
424 
425     hr = IWICBitmap_SetPalette(bitmap, palette);
426     ok(hr == S_OK, "IWICBitmap_SetPalette failed hr=%x\n", hr);
427 
428     IWICPalette_Release(palette);
429 
430     rc.X = rc.Y = 0;
431     rc.Width = 3;
432     rc.Height = 3;
433     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock);
434     ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
435     if (SUCCEEDED(hr))
436     {
437         hr = IWICBitmapLock_GetStride(lock, &lock_buffer_stride);
438         ok(hr == S_OK, "IWICBitmapLock_GetStride failed hr=%x\n", hr);
439         ok(lock_buffer_stride == 12, "got %i, expected 12\n", lock_buffer_stride);
440 
441         hr = IWICBitmapLock_GetDataPointer(lock, &lock_buffer_size, &lock_buffer);
442         ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
443         ok(lock_buffer_size == 33, "got %i, expected 33\n", lock_buffer_size);
444         ok(lock_buffer != NULL, "got NULL data pointer\n");
445 
446         for (i=0; i<3; i++)
447             memcpy(lock_buffer + lock_buffer_stride*i, bitmap_data + i*9, 9);
448 
449         IWICBitmapLock_Release(lock);
450     }
451 
452     hr = IWICBitmap_SetResolution(bitmap, 12.0, 34.0);
453     ok(hr == S_OK, "IWICBitmap_SetResolution failed hr=%x\n", hr);
454 
455     hr = IWICImagingFactory_CreateBitmapFromSource(factory, (IWICBitmapSource*)bitmap,
456         WICBitmapCacheOnLoad, &bitmap2);
457     ok(hr == S_OK, "IWICImagingFactory_CreateBitmapFromSource failed hr=%x\n", hr);
458 
459     IWICBitmap_Release(bitmap);
460 
461     if (FAILED(hr)) return;
462 
463     hr = IWICImagingFactory_CreatePalette(factory, &palette);
464     ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
465 
466     /* palette isn't copied for non-indexed formats? */
467     hr = IWICBitmap_CopyPalette(bitmap2, palette);
468     ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "IWICBitmap_CopyPalette failed hr=%x\n", hr);
469 
470     IWICPalette_Release(palette);
471 
472     hr = IWICBitmap_CopyPixels(bitmap2, NULL, 9, 27, returned_data);
473     ok(hr == S_OK, "IWICBitmap_CopyPixels failed hr=%x\n", hr);
474 
475     for (i=0; i<27; i++)
476         ok(returned_data[i] == bitmap_data[i], "returned_data[%i] == %i\n", i, returned_data[i]);
477 
478     hr = IWICBitmap_GetPixelFormat(bitmap2, &pixelformat);
479     ok(hr == S_OK, "IWICBitmap_GetPixelFormat failed hr=%x\n", hr);
480     ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
481 
482     hr = IWICBitmap_GetResolution(bitmap2, &dpix, &dpiy);
483     ok(hr == S_OK, "IWICBitmap_GetResolution failed hr=%x\n", hr);
484     ok(dpix == 12.0, "got %f, expected 12.0\n", dpix);
485     ok(dpiy == 34.0, "got %f, expected 34.0\n", dpiy);
486 
487     hr = IWICBitmap_GetSize(bitmap2, &width, &height);
488     ok(hr == S_OK, "IWICBitmap_GetSize failed hr=%x\n", hr);
489     ok(width == 3, "got %d, expected 3\n", width);
490     ok(height == 3, "got %d, expected 3\n", height);
491 
492     IWICBitmap_Release(bitmap2);
493 
494     /* Ensure palette is copied for indexed formats */
495     hr = IWICImagingFactory_CreateBitmap(factory, 3, 3, &GUID_WICPixelFormat4bppIndexed,
496         WICBitmapCacheOnLoad, &bitmap);
497     ok(hr == S_OK, "IWICImagingFactory_CreateBitmap failed hr=%x\n", hr);
498 
499     hr = IWICImagingFactory_CreatePalette(factory, &palette);
500     ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
501 
502     hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray256, FALSE);
503     ok(hr == S_OK, "IWICPalette_InitializePredefined failed hr=%x\n", hr);
504 
505     hr = IWICBitmap_SetPalette(bitmap, palette);
506     ok(hr == S_OK, "IWICBitmap_SetPalette failed hr=%x\n", hr);
507 
508     IWICPalette_Release(palette);
509 
510     hr = IWICImagingFactory_CreateBitmapFromSource(factory, (IWICBitmapSource*)bitmap,
511         WICBitmapCacheOnLoad, &bitmap2);
512     ok(hr == S_OK, "IWICImagingFactory_CreateBitmapFromSource failed hr=%x\n", hr);
513 
514     IWICBitmap_Release(bitmap);
515 
516     hr = IWICImagingFactory_CreatePalette(factory, &palette);
517     ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
518 
519     hr = IWICBitmap_CopyPalette(bitmap2, palette);
520     ok(hr == S_OK, "IWICBitmap_CopyPalette failed hr=%x\n", hr);
521 
522     hr = IWICPalette_GetColorCount(palette, &count);
523     ok(hr == S_OK, "IWICPalette_GetColorCount failed hr=%x\n", hr);
524     ok(count == 256, "unexpected count %d\n", count);
525 
526     hr = IWICPalette_GetType(palette, &palette_type);
527     ok(hr == S_OK, "IWICPalette_GetType failed hr=%x\n", hr);
528     ok(palette_type == WICBitmapPaletteTypeFixedGray256, "unexpected palette type %d\n", palette_type);
529 
530     IWICPalette_Release(palette);
531 
532     hr = IWICBitmap_GetPixelFormat(bitmap2, &pixelformat);
533     ok(hr == S_OK, "IWICBitmap_GetPixelFormat failed hr=%x\n", hr);
534     ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat4bppIndexed), "unexpected pixel format\n");
535 
536     hr = IWICBitmap_GetSize(bitmap2, &width, &height);
537     ok(hr == S_OK, "IWICBitmap_GetSize failed hr=%x\n", hr);
538     ok(width == 3, "got %d, expected 3\n", width);
539     ok(height == 3, "got %d, expected 3\n", height);
540 
541     IWICBitmap_Release(bitmap2);
542 }
543 
544 static void test_CreateBitmapFromMemory(void)
545 {
546     BYTE orig_data3x3[27] = {
547         128,128,255, 128,128,128, 128,255,128,
548         128,128,128, 128,128,128, 255,255,255,
549         255,128,128, 255,255,255, 255,255,255 };
550     BYTE data3x3[27];
551     BYTE data3x2[27] = {
552         128,128,255, 128,128,128, 128,255,128,
553         0,0,0, 0,128,128, 255,255,255,
554         255,128,128, 255,0,0, 0,0,0 };
555     BYTE data[27];
556     HRESULT hr;
557     IWICBitmap *bitmap;
558     UINT width, height, i;
559 
560     memcpy(data3x3, orig_data3x3, sizeof(data3x3));
561 
562     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
563                                                    0, 0, NULL, &bitmap);
564     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
565 
566     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
567                                                    0, sizeof(data3x3), data3x3, &bitmap);
568     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
569 
570     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
571                                                    6, sizeof(data3x3), data3x3, &bitmap);
572     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
573 
574     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
575                                                    12, sizeof(data3x3), data3x3, &bitmap);
576     ok(hr == WINCODEC_ERR_INSUFFICIENTBUFFER, "expected WINCODEC_ERR_INSUFFICIENTBUFFER, got %#x\n", hr);
577 
578     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
579                                                    9, sizeof(data3x3) - 1, data3x3, &bitmap);
580     ok(hr == WINCODEC_ERR_INSUFFICIENTBUFFER, "expected WINCODEC_ERR_INSUFFICIENTBUFFER, got %#x\n", hr);
581 
582     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
583                                                    9, sizeof(data3x3), data3x3, &bitmap);
584     ok(hr == S_OK, "IWICImagingFactory_CreateBitmapFromMemory error %#x\n", hr);
585 
586     hr = IWICBitmap_GetSize(bitmap, &width, &height);
587     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
588     ok(width == 3, "expected 3, got %u\n", width);
589     ok(height == 3, "expected 3, got %u\n", height);
590 
591     data3x3[2] = 192;
592 
593     memset(data, 0, sizeof(data));
594     hr = IWICBitmap_CopyPixels(bitmap, NULL, 9, sizeof(data), data);
595     ok(hr == S_OK, "IWICBitmap_CopyPixels error %#x\n", hr);
596     for (i = 0; i < sizeof(data); i++)
597         ok(data[i] == orig_data3x3[i], "%u: expected %u, got %u\n", i, data[i], data3x3[i]);
598 
599     IWICBitmap_Release(bitmap);
600 
601     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 2, &GUID_WICPixelFormat24bppBGR,
602                                                    13, sizeof(orig_data3x3), orig_data3x3, &bitmap);
603     ok(hr == S_OK, "IWICImagingFactory_CreateBitmapFromMemory error %#x\n", hr);
604 
605     hr = IWICBitmap_GetSize(bitmap, &width, &height);
606     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
607     ok(width == 3, "expected 3, got %u\n", width);
608     ok(height == 2, "expected 2, got %u\n", height);
609 
610     memset(data, 0, sizeof(data));
611     hr = IWICBitmap_CopyPixels(bitmap, NULL, 13, sizeof(data), data);
612     ok(hr == S_OK, "IWICBitmap_CopyPixels error %#x\n", hr);
613     for (i = 0; i < sizeof(data); i++)
614         ok(data[i] == data3x2[i], "%u: expected %u, got %u\n", i, data3x2[i], data[i]);
615 
616     IWICBitmap_Release(bitmap);
617 }
618 
619 static void test_CreateBitmapFromHICON(void)
620 {
621     static const char bits[4096];
622     HICON icon;
623     ICONINFO info;
624     HRESULT hr;
625     IWICBitmap *bitmap;
626     UINT width, height;
627     WICPixelFormatGUID format;
628 
629     /* 1 bpp mask */
630     info.fIcon = 1;
631     info.xHotspot = 0;
632     info.yHotspot = 0;
633     info.hbmColor = 0;
634     info.hbmMask = CreateBitmap(16, 32, 1, 1, bits);
635     ok(info.hbmMask != 0, "CreateBitmap failed\n");
636     icon = CreateIconIndirect(&info);
637     ok(icon != 0, "CreateIconIndirect failed\n");
638     DeleteObject(info.hbmMask);
639 
640     hr = IWICImagingFactory_CreateBitmapFromHICON(factory, 0, NULL);
641     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
642 
643     hr = IWICImagingFactory_CreateBitmapFromHICON(factory, 0, &bitmap);
644     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_CURSOR_HANDLE), "expected ERROR_INVALID_CURSOR_HANDLE, got %#x\n", hr);
645 
646     hr = IWICImagingFactory_CreateBitmapFromHICON(factory, icon, NULL);
647     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
648 
649     hr = IWICImagingFactory_CreateBitmapFromHICON(factory, icon, &bitmap);
650     ok(hr == S_OK, "CreateBitmapFromHICON error %#x\n", hr);
651     DestroyIcon(icon);
652     if (hr != S_OK) return;
653 
654     IWICBitmap_GetPixelFormat(bitmap, &format);
655     ok(IsEqualGUID(&format, &GUID_WICPixelFormat32bppBGRA),
656        "unexpected pixel format %s\n", wine_dbgstr_guid(&format));
657 
658     hr = IWICBitmap_GetSize(bitmap, &width, &height);
659     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
660     ok(width == 16, "expected 16, got %u\n", width);
661     ok(height == 16, "expected 16, got %u\n", height);
662 
663     IWICBitmap_Release(bitmap);
664 
665     /* 24 bpp color, 1 bpp mask */
666     info.fIcon = 1;
667     info.xHotspot = 0;
668     info.yHotspot = 0;
669     info.hbmColor = CreateBitmap(16, 16, 1, 24, bits);
670     ok(info.hbmColor != 0, "CreateBitmap failed\n");
671     info.hbmMask = CreateBitmap(16, 16, 1, 1, bits);
672     ok(info.hbmMask != 0, "CreateBitmap failed\n");
673     icon = CreateIconIndirect(&info);
674     ok(icon != 0, "CreateIconIndirect failed\n");
675     DeleteObject(info.hbmColor);
676     DeleteObject(info.hbmMask);
677 
678     hr = IWICImagingFactory_CreateBitmapFromHICON(factory, icon, &bitmap);
679     ok(hr == S_OK, "CreateBitmapFromHICON error %#x\n", hr);
680     DestroyIcon(icon);
681 
682     IWICBitmap_GetPixelFormat(bitmap, &format);
683     ok(IsEqualGUID(&format, &GUID_WICPixelFormat32bppBGRA),
684        "unexpected pixel format %s\n", wine_dbgstr_guid(&format));
685 
686     hr = IWICBitmap_GetSize(bitmap, &width, &height);
687     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
688     ok(width == 16, "expected 16, got %u\n", width);
689     ok(height == 16, "expected 16, got %u\n", height);
690 
691     IWICBitmap_Release(bitmap);
692 }
693 
694 static void test_CreateBitmapFromHBITMAP(void)
695 {
696     /* 8 bpp data must be aligned to a DWORD boundary for a DIB */
697     static const BYTE data_8bpp_pal_dib[12] = { 0,1,2,0, 1,2,0,0, 2,1,0,0 };
698     static const BYTE data_8bpp_rgb_dib[12] = { 0xf0,0x0f,0xff,0, 0x0f,0xff,0xf0,0, 0xf0,0x0f,0xff,0 };
699     static const BYTE data_8bpp_pal_wic[12] = { 0xd,0xe,0x10,0, 0xe,0x10,0xd,0, 0x10,0xe,0xd,0 };
700     static const PALETTEENTRY pal_data[3] = { {0xff,0,0,0}, {0,0xff,0,0}, {0,0,0xff,0} };
701     char pal_buf[sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * 255];
702     LOGPALETTE *pal = (LOGPALETTE *)pal_buf;
703     HBITMAP hbmp;
704     HPALETTE hpal;
705     BYTE data[12];
706     HRESULT hr;
707     IWICBitmap *bitmap;
708     UINT width, height, i, count;
709     WICPixelFormatGUID format;
710     IWICPalette *palette;
711     WICBitmapPaletteType type;
712 
713     /* 8 bpp without palette */
714     hbmp = create_dib(3, 3, 8, NULL, data_8bpp_rgb_dib);
715     ok(hbmp != 0, "failed to create bitmap\n");
716 
717     hr = IWICImagingFactory_CreateBitmapFromHBITMAP(factory, 0, 0, WICBitmapIgnoreAlpha, &bitmap);
718     ok(hr == WINCODEC_ERR_WIN32ERROR || hr == 0x88980003 /*XP*/, "expected WINCODEC_ERR_WIN32ERROR, got %#x\n", hr);
719 
720     hr = IWICImagingFactory_CreateBitmapFromHBITMAP(factory, hbmp, 0, WICBitmapIgnoreAlpha, NULL);
721     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
722 
723     hr = IWICImagingFactory_CreateBitmapFromHBITMAP(factory, hbmp, 0, WICBitmapIgnoreAlpha, &bitmap);
724     ok(hr == S_OK, "CreateBitmapFromHBITMAP error %#x\n", hr);
725 
726     IWICBitmap_GetPixelFormat(bitmap, &format);
727     ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
728        "unexpected pixel format %s\n", wine_dbgstr_guid(&format));
729 
730     hr = IWICBitmap_GetSize(bitmap, &width, &height);
731     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
732     ok(width == 3, "expected 3, got %u\n", width);
733     ok(height == 3, "expected 3, got %u\n", height);
734 
735     memset(data, 0, sizeof(data));
736     hr = IWICBitmap_CopyPixels(bitmap, NULL, 4, sizeof(data), data);
737     ok(hr == S_OK, "IWICBitmap_CopyPixels error %#x\n", hr);
738     for (i = 0; i < sizeof(data); i++)
739         ok(data[i] == data_8bpp_rgb_dib[i], "%u: expected %#x, got %#x\n", i, data_8bpp_rgb_dib[i], data[i]);
740 
741     IWICBitmap_Release(bitmap);
742     DeleteObject(hbmp);
743 
744     /* 8 bpp with a 3 entries palette */
745     memset(pal_buf, 0, sizeof(pal_buf));
746     pal->palVersion = 0x300;
747     pal->palNumEntries = 3;
748     memcpy(pal->palPalEntry, pal_data, sizeof(pal_data));
749     hpal = CreatePalette(pal);
750     ok(hpal != 0, "CreatePalette failed\n");
751 
752     hbmp = create_dib(3, 3, 8, pal, data_8bpp_pal_dib);
753     hr = IWICImagingFactory_CreateBitmapFromHBITMAP(factory, hbmp, hpal, WICBitmapIgnoreAlpha, &bitmap);
754     ok(hr == S_OK, "CreateBitmapFromHBITMAP error %#x\n", hr);
755 
756     IWICBitmap_GetPixelFormat(bitmap, &format);
757 todo_wine
758     ok(IsEqualGUID(&format, &GUID_WICPixelFormat4bppIndexed),
759        "unexpected pixel format %s\n", wine_dbgstr_guid(&format));
760 
761     hr = IWICBitmap_GetSize(bitmap, &width, &height);
762     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
763     ok(width == 3, "expected 3, got %u\n", width);
764     ok(height == 3, "expected 3, got %u\n", height);
765 
766     hr = IWICImagingFactory_CreatePalette(factory, &palette);
767     ok(hr == S_OK, "CreatePalette error %#x\n", hr);
768     hr = IWICBitmap_CopyPalette(bitmap, palette);
769     ok(hr == S_OK, "CopyPalette error %#x\n", hr);
770 
771     hr = IWICPalette_GetType(palette, &type);
772     ok(hr == S_OK, "%u: GetType error %#x\n", i, hr);
773     ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %#x\n", type);
774 
775     hr = IWICPalette_GetColorCount(palette, &count);
776     ok(hr == S_OK, "GetColorCount error %#x\n", hr);
777 todo_wine
778     ok(count == 16, "expected 16, got %u\n", count);
779 
780     IWICPalette_Release(palette);
781 
782     IWICBitmap_Release(bitmap);
783     DeleteObject(hbmp);
784     DeleteObject(hpal);
785 
786     /* 8 bpp with a 256 entries palette */
787     memset(pal_buf, 0, sizeof(pal_buf));
788     pal->palVersion = 0x300;
789     pal->palNumEntries = 256;
790     memcpy(pal->palPalEntry, pal_data, sizeof(pal_data));
791     hpal = CreatePalette(pal);
792     ok(hpal != 0, "CreatePalette failed\n");
793 
794     hbmp = create_dib(3, 3, 8, pal, data_8bpp_pal_dib);
795     hr = IWICImagingFactory_CreateBitmapFromHBITMAP(factory, hbmp, hpal, WICBitmapIgnoreAlpha, &bitmap);
796     ok(hr == S_OK, "CreateBitmapFromHBITMAP error %#x\n", hr);
797 
798     IWICBitmap_GetPixelFormat(bitmap, &format);
799     ok(IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed),
800             "unexpected pixel format %s\n", wine_dbgstr_guid(&format));
801 
802     hr = IWICBitmap_GetSize(bitmap, &width, &height);
803     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
804     ok(width == 3, "expected 3, got %u\n", width);
805     ok(height == 3, "expected 3, got %u\n", height);
806 
807     hr = IWICImagingFactory_CreatePalette(factory, &palette);
808     ok(hr == S_OK, "CreatePalette error %#x\n", hr);
809     hr = IWICBitmap_CopyPalette(bitmap, palette);
810     ok(hr == S_OK, "CopyPalette error %#x\n", hr);
811 
812     hr = IWICPalette_GetType(palette, &type);
813     ok(hr == S_OK, "%u: GetType error %#x\n", i, hr);
814     ok(type == WICBitmapPaletteTypeCustom, "expected WICBitmapPaletteTypeCustom, got %#x\n", type);
815 
816     hr = IWICPalette_GetColorCount(palette, &count);
817     ok(hr == S_OK, "GetColorCount error %#x\n", hr);
818     ok(count == 256, "expected 256, got %u\n", count);
819 
820     IWICPalette_Release(palette);
821 
822     memset(data, 0, sizeof(data));
823     hr = IWICBitmap_CopyPixels(bitmap, NULL, 4, sizeof(data), data);
824     ok(hr == S_OK, "IWICBitmap_CopyPixels error %#x\n", hr);
825     for (i = 0; i < sizeof(data); i++)
826         todo_wine_if (data[i] != data_8bpp_pal_wic[i])
827             ok(data[i] == data_8bpp_pal_wic[i], "%u: expected %#x, got %#x\n", i, data_8bpp_pal_wic[i], data[i]);
828 
829     IWICBitmap_Release(bitmap);
830     DeleteObject(hbmp);
831     DeleteObject(hpal);
832 
833     /* 32bpp alpha */
834     hbmp = create_dib(2, 2, 32, NULL, NULL);
835     hr = IWICImagingFactory_CreateBitmapFromHBITMAP(factory, hbmp, NULL, WICBitmapUseAlpha, &bitmap);
836     ok(hr == S_OK, "CreateBitmapFromHBITMAP error %#x\n", hr);
837 
838     hr = IWICBitmap_GetPixelFormat(bitmap, &format);
839     ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
840     ok(IsEqualGUID(&format, &GUID_WICPixelFormat32bppBGRA),
841        "unexpected pixel format %s\n", wine_dbgstr_guid(&format));
842 
843     IWICBitmap_Release(bitmap);
844 
845     /* 32bpp pre-multiplied alpha */
846     hr = IWICImagingFactory_CreateBitmapFromHBITMAP(factory, hbmp, NULL, WICBitmapUsePremultipliedAlpha, &bitmap);
847     ok(hr == S_OK, "CreateBitmapFromHBITMAP error %#x\n", hr);
848 
849     hr = IWICBitmap_GetPixelFormat(bitmap, &format);
850     ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
851     ok(IsEqualGUID(&format, &GUID_WICPixelFormat32bppPBGRA),
852        "unexpected pixel format %s\n", wine_dbgstr_guid(&format));
853 
854     IWICBitmap_Release(bitmap);
855 
856     /* 32bpp no alpha */
857     hr = IWICImagingFactory_CreateBitmapFromHBITMAP(factory, hbmp, NULL, WICBitmapIgnoreAlpha, &bitmap);
858     ok(hr == S_OK, "CreateBitmapFromHBITMAP error %#x\n", hr);
859 
860     hr = IWICBitmap_GetPixelFormat(bitmap, &format);
861     ok(hr == S_OK, "GetPixelFormat error %#x\n", hr);
862     ok(IsEqualGUID(&format, &GUID_WICPixelFormat32bppBGR),
863        "unexpected pixel format %s\n", wine_dbgstr_guid(&format));
864 
865     IWICBitmap_Release(bitmap);
866     DeleteObject(hbmp);
867 }
868 
869 static void test_clipper(void)
870 {
871     IWICBitmapClipper *clipper;
872     UINT height, width;
873     IWICBitmap *bitmap;
874     BYTE buffer[500];
875     WICRect rect;
876     HRESULT hr;
877 
878     hr = IWICImagingFactory_CreateBitmap(factory, 10, 10, &GUID_WICPixelFormat24bppBGR,
879         WICBitmapCacheOnLoad, &bitmap);
880     ok(hr == S_OK, "got 0x%08x\n", hr);
881 
882     hr = IWICImagingFactory_CreateBitmapClipper(factory, &clipper);
883     ok(hr == S_OK, "got 0x%08x\n", hr);
884 
885     rect.X = rect.Y = 0;
886     rect.Width = rect.Height = 11;
887     hr = IWICBitmapClipper_Initialize(clipper, (IWICBitmapSource*)bitmap, &rect);
888     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
889 
890     rect.X = rect.Y = 5;
891     rect.Width = rect.Height = 6;
892     hr = IWICBitmapClipper_Initialize(clipper, (IWICBitmapSource*)bitmap, &rect);
893     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
894 
895     rect.X = rect.Y = 5;
896     rect.Width = rect.Height = 5;
897     hr = IWICBitmapClipper_Initialize(clipper, (IWICBitmapSource*)bitmap, &rect);
898     ok(hr == S_OK, "got 0x%08x\n", hr);
899 
900     width = height = 0;
901     hr = IWICBitmapClipper_GetSize(clipper, &width, &height);
902     ok(hr == S_OK, "got 0x%08x\n", hr);
903     ok(width == 5, "got %d\n", width);
904     ok(height == 5, "got %d\n", height);
905 
906     IWICBitmapClipper_Release(clipper);
907     IWICBitmap_Release(bitmap);
908 
909     /* CopyPixels */
910     hr = IWICImagingFactory_CreateBitmapClipper(factory, &clipper);
911     ok(hr == S_OK, "got 0x%08x\n", hr);
912 
913     rect.X = rect.Y = 5;
914     rect.Width = rect.Height = 5;
915     hr = IWICBitmapClipper_Initialize(clipper, &bitmapsource, &rect);
916     ok(hr == S_OK, "got 0x%08x\n", hr);
917 
918     rect.X = rect.Y = 0;
919     rect.Width = rect.Height = 2;
920 
921     /* passed rectangle is relative to clipper rectangle, underlying source gets intersected
922        rectangle */
923     memset(&g_rect, 0, sizeof(g_rect));
924     called_CopyPixels = FALSE;
925     hr = IWICBitmapClipper_CopyPixels(clipper, &rect, 0, sizeof(buffer), buffer);
926     ok(hr == S_OK, "got 0x%08x\n", hr);
927     ok(called_CopyPixels, "CopyPixels not called\n");
928     ok(g_rect.X == 5 && g_rect.Y == 5 && g_rect.Width == 2 && g_rect.Height == 2,
929         "got wrong rectangle (%d,%d)-(%d,%d)\n", g_rect.X, g_rect.Y, g_rect.Width, g_rect.Height);
930 
931     /* whole clipping rectangle */
932     memset(&g_rect, 0, sizeof(g_rect));
933     called_CopyPixels = FALSE;
934 
935     rect.X = rect.Y = 0;
936     rect.Width = rect.Height = 5;
937 
938     hr = IWICBitmapClipper_CopyPixels(clipper, &rect, 0, sizeof(buffer), buffer);
939     ok(hr == S_OK, "got 0x%08x\n", hr);
940     ok(called_CopyPixels, "CopyPixels not called\n");
941     ok(g_rect.X == 5 && g_rect.Y == 5 && g_rect.Width == 5 && g_rect.Height == 5,
942         "got wrong rectangle (%d,%d)-(%d,%d)\n", g_rect.X, g_rect.Y, g_rect.Width, g_rect.Height);
943 
944     /* larger than clipping rectangle */
945     memset(&g_rect, 0, sizeof(g_rect));
946     called_CopyPixels = FALSE;
947 
948     rect.X = rect.Y = 0;
949     rect.Width = rect.Height = 20;
950 
951     hr = IWICBitmapClipper_CopyPixels(clipper, &rect, 0, sizeof(buffer), buffer);
952     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
953     ok(!called_CopyPixels, "CopyPixels called\n");
954 
955     rect.X = rect.Y = 5;
956     rect.Width = rect.Height = 5;
957 
958     hr = IWICBitmapClipper_CopyPixels(clipper, &rect, 0, sizeof(buffer), buffer);
959     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
960     ok(!called_CopyPixels, "CopyPixels called\n");
961 
962     /* null rectangle */
963     memset(&g_rect, 0, sizeof(g_rect));
964     called_CopyPixels = FALSE;
965 
966     hr = IWICBitmapClipper_CopyPixels(clipper, NULL, 0, sizeof(buffer), buffer);
967     ok(hr == S_OK, "got 0x%08x\n", hr);
968     ok(called_CopyPixels, "CopyPixels not called\n");
969     ok(g_rect.X == 5 && g_rect.Y == 5 && g_rect.Width == 5 && g_rect.Height == 5,
970         "got wrong rectangle (%d,%d)-(%d,%d)\n", g_rect.X, g_rect.Y, g_rect.Width, g_rect.Height);
971 
972     IWICBitmapClipper_Release(clipper);
973 }
974 
975 START_TEST(bitmap)
976 {
977     HRESULT hr;
978 
979     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
980 
981     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
982         &IID_IWICImagingFactory, (void**)&factory);
983     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
984 
985     test_createbitmap();
986     test_createbitmapfromsource();
987     test_CreateBitmapFromMemory();
988     test_CreateBitmapFromHICON();
989     test_CreateBitmapFromHBITMAP();
990     test_clipper();
991 
992     IWICImagingFactory_Release(factory);
993 
994     CoUninitialize();
995 }
996