wbemprox: Both signature parameters are optional in IWbemClassObject::GetMethod.
[wine] / dlls / windowscodecs / tests / bitmap.c
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 <math.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "objbase.h"
28 #include "wincodec.h"
29 #include "wine/test.h"
30
31 static IWICImagingFactory *factory;
32
33 static const char *debugstr_guid(const GUID *guid)
34 {
35     static char buf[50];
36
37     if (!guid) return "(null)";
38     sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
39             guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
40             guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
41             guid->Data4[5], guid->Data4[6], guid->Data4[7]);
42     return buf;
43 }
44
45 static void test_createbitmap(void)
46 {
47     HRESULT hr;
48     IWICBitmap *bitmap;
49     IWICPalette *palette;
50     IWICBitmapLock *lock, *lock2;
51     WICBitmapPaletteType palettetype;
52     int i;
53     WICRect rc;
54     const BYTE bitmap_data[27] = {
55         128,128,255, 128,128,128, 128,255,128,
56         128,128,128, 128,128,128, 255,255,255,
57         255,128,128, 255,255,255, 255,255,255};
58     BYTE returned_data[27] = {0};
59     BYTE *lock_buffer=NULL, *base_lock_buffer=NULL;
60     UINT lock_buffer_size=0;
61     UINT lock_buffer_stride=0;
62     WICPixelFormatGUID pixelformat = {0};
63     UINT width=0, height=0;
64     double dpix=10.0, dpiy=10.0;
65     int can_lock_null = 1;
66
67     hr = IWICImagingFactory_CreateBitmap(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
68         WICBitmapCacheOnLoad, &bitmap);
69     ok(hr == S_OK, "IWICImagingFactory_CreateBitmap failed hr=%x\n", hr);
70
71     if (FAILED(hr))
72         return;
73
74     hr = IWICImagingFactory_CreatePalette(factory, &palette);
75     ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
76
77     /* Palette is unavailable until explicitly set */
78     hr = IWICBitmap_CopyPalette(bitmap, palette);
79     ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "IWICBitmap_CopyPalette failed hr=%x\n", hr);
80
81     hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray256, FALSE);
82     ok(hr == S_OK, "IWICPalette_InitializePredefined failed hr=%x\n", hr);
83
84     hr = IWICBitmap_SetPalette(bitmap, palette);
85     ok(hr == S_OK, "IWICBitmap_SetPalette failed hr=%x\n", hr);
86
87     hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray4, FALSE);
88     ok(hr == S_OK, "IWICPalette_InitializePredefined failed hr=%x\n", hr);
89
90     hr = IWICBitmap_CopyPalette(bitmap, palette);
91     ok(hr == S_OK, "IWICBitmap_CopyPalette failed hr=%x\n", hr);
92
93     hr = IWICPalette_GetType(palette, &palettetype);
94     ok(hr == S_OK, "IWICPalette_GetType failed hr=%x\n", hr);
95     ok(palettetype == WICBitmapPaletteTypeFixedGray256,
96         "expected WICBitmapPaletteTypeFixedGray256, got %x\n", palettetype);
97
98     IWICPalette_Release(palette);
99
100     /* pixel data is initially zeroed */
101     hr = IWICBitmap_CopyPixels(bitmap, NULL, 9, 27, returned_data);
102     ok(hr == S_OK, "IWICBitmap_CopyPixels failed hr=%x\n", hr);
103
104     for (i=0; i<27; i++)
105         ok(returned_data[i] == 0, "returned_data[%i] == %i\n", i, returned_data[i]);
106
107     /* Invalid lock rects */
108     rc.X = rc.Y = 0;
109     rc.Width = 4;
110     rc.Height = 3;
111     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
112     ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
113     if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
114
115     rc.Width = 3;
116     rc.Height = 4;
117     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
118     ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
119     if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
120
121     rc.Height = 3;
122     rc.X = 4;
123     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
124     ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
125     if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
126
127     rc.X = 0;
128     rc.Y = 4;
129     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
130     ok(hr == E_INVALIDARG, "IWICBitmap_Lock failed hr=%x\n", hr);
131     if (SUCCEEDED(hr)) IWICBitmapLock_Release(lock);
132
133     /* NULL lock rect */
134     hr = IWICBitmap_Lock(bitmap, NULL, WICBitmapLockRead, &lock);
135     ok(hr == S_OK || broken(hr == E_INVALIDARG) /* winxp */, "IWICBitmap_Lock failed hr=%x\n", hr);
136
137     if (SUCCEEDED(hr))
138     {
139         /* entire bitmap is locked */
140         hr = IWICBitmapLock_GetSize(lock, &width, &height);
141         ok(hr == S_OK, "IWICBitmapLock_GetSize failed hr=%x\n", hr);
142         ok(width == 3, "got %d, expected 3\n", width);
143         ok(height == 3, "got %d, expected 3\n", height);
144
145         IWICBitmapLock_Release(lock);
146     }
147     else
148         can_lock_null = 0;
149
150     /* lock with a valid rect */
151     rc.Y = 0;
152     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock);
153     ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
154     if (SUCCEEDED(hr))
155     {
156         hr = IWICBitmapLock_GetStride(lock, &lock_buffer_stride);
157         ok(hr == S_OK, "IWICBitmapLock_GetStride failed hr=%x\n", hr);
158         /* stride is divisible by 4 */
159         ok(lock_buffer_stride == 12, "got %i, expected 12\n", lock_buffer_stride);
160
161         hr = IWICBitmapLock_GetDataPointer(lock, &lock_buffer_size, &lock_buffer);
162         ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
163         /* buffer size does not include padding from the last row */
164         ok(lock_buffer_size == 33, "got %i, expected 33\n", lock_buffer_size);
165         ok(lock_buffer != NULL, "got NULL data pointer\n");
166         base_lock_buffer = lock_buffer;
167
168         hr = IWICBitmapLock_GetPixelFormat(lock, &pixelformat);
169         ok(hr == S_OK, "IWICBitmapLock_GetPixelFormat failed hr=%x\n", hr);
170         ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
171
172         hr = IWICBitmapLock_GetSize(lock, &width, &height);
173         ok(hr == S_OK, "IWICBitmapLock_GetSize failed hr=%x\n", hr);
174         ok(width == 3, "got %d, expected 3\n", width);
175         ok(height == 3, "got %d, expected 3\n", height);
176
177         /* We can have multiple simultaneous read locks */
178         hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock2);
179         ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
180
181         if (SUCCEEDED(hr))
182         {
183             hr = IWICBitmapLock_GetDataPointer(lock2, &lock_buffer_size, &lock_buffer);
184             ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
185             ok(lock_buffer_size == 33, "got %i, expected 33\n", lock_buffer_size);
186             ok(lock_buffer == base_lock_buffer, "got %p, expected %p\n", lock_buffer, base_lock_buffer);
187
188             IWICBitmapLock_Release(lock2);
189         }
190
191         if (can_lock_null) /* this hangs on xp/vista */
192         {
193             /* But not a read and a write lock */
194             hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock2);
195             ok(hr == WINCODEC_ERR_ALREADYLOCKED, "IWICBitmap_Lock failed hr=%x\n", hr);
196         }
197
198         /* But we don't need a write lock to write */
199         if (base_lock_buffer)
200         {
201             for (i=0; i<3; i++)
202                 memcpy(base_lock_buffer + lock_buffer_stride*i, bitmap_data + i*9, 9);
203         }
204
205         IWICBitmapLock_Release(lock);
206     }
207
208     /* test that the data we wrote is returned by CopyPixels */
209     hr = IWICBitmap_CopyPixels(bitmap, NULL, 9, 27, returned_data);
210     ok(hr == S_OK, "IWICBitmap_CopyPixels failed hr=%x\n", hr);
211
212     for (i=0; i<27; i++)
213         ok(returned_data[i] == bitmap_data[i], "returned_data[%i] == %i\n", i, returned_data[i]);
214
215     /* try a valid partial rect, and write mode */
216     rc.X = 2;
217     rc.Y = 0;
218     rc.Width = 1;
219     rc.Height = 2;
220     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock);
221     ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
222
223     if (SUCCEEDED(hr))
224     {
225         if (can_lock_null) /* this hangs on xp/vista */
226         {
227             /* Can't lock again while locked for writing */
228             hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock2);
229             ok(hr == WINCODEC_ERR_ALREADYLOCKED, "IWICBitmap_Lock failed hr=%x\n", hr);
230
231             hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockRead, &lock2);
232             ok(hr == WINCODEC_ERR_ALREADYLOCKED, "IWICBitmap_Lock failed hr=%x\n", hr);
233         }
234
235         hr = IWICBitmapLock_GetStride(lock, &lock_buffer_stride);
236         ok(hr == S_OK, "IWICBitmapLock_GetStride failed hr=%x\n", hr);
237         ok(lock_buffer_stride == 12, "got %i, expected 12\n", lock_buffer_stride);
238
239         hr = IWICBitmapLock_GetDataPointer(lock, &lock_buffer_size, &lock_buffer);
240         ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
241         ok(lock_buffer_size == 15, "got %i, expected 15\n", lock_buffer_size);
242         ok(lock_buffer == base_lock_buffer+6, "got %p, expected %p+6\n", lock_buffer, base_lock_buffer);
243
244         hr = IWICBitmapLock_GetPixelFormat(lock, &pixelformat);
245         ok(hr == S_OK, "IWICBitmapLock_GetPixelFormat failed hr=%x\n", hr);
246         ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
247
248         hr = IWICBitmapLock_GetSize(lock, &width, &height);
249         ok(hr == S_OK, "IWICBitmapLock_GetSize failed hr=%x\n", hr);
250         ok(width == 1, "got %d, expected 1\n", width);
251         ok(height == 2, "got %d, expected 2\n", height);
252
253         IWICBitmapLock_Release(lock);
254     }
255
256     hr = IWICBitmap_GetPixelFormat(bitmap, &pixelformat);
257     ok(hr == S_OK, "IWICBitmap_GetPixelFormat failed hr=%x\n", hr);
258     ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
259
260     hr = IWICBitmap_GetResolution(bitmap, &dpix, &dpiy);
261     ok(hr == S_OK, "IWICBitmap_GetResolution failed hr=%x\n", hr);
262     ok(dpix == 0.0, "got %f, expected 0.0\n", dpix);
263     ok(dpiy == 0.0, "got %f, expected 0.0\n", dpiy);
264
265     hr = IWICBitmap_SetResolution(bitmap, 12.0, 34.0);
266     ok(hr == S_OK, "IWICBitmap_SetResolution failed hr=%x\n", hr);
267
268     hr = IWICBitmap_GetResolution(bitmap, &dpix, &dpiy);
269     ok(hr == S_OK, "IWICBitmap_GetResolution failed hr=%x\n", hr);
270     ok(dpix == 12.0, "got %f, expected 12.0\n", dpix);
271     ok(dpiy == 34.0, "got %f, expected 34.0\n", dpiy);
272
273     hr = IWICBitmap_GetSize(bitmap, &width, &height);
274     ok(hr == S_OK, "IWICBitmap_GetSize failed hr=%x\n", hr);
275     ok(width == 3, "got %d, expected 3\n", width);
276     ok(height == 3, "got %d, expected 3\n", height);
277
278     IWICBitmap_Release(bitmap);
279 }
280
281 static void test_createbitmapfromsource(void)
282 {
283     HRESULT hr;
284     IWICBitmap *bitmap, *bitmap2;
285     IWICPalette *palette;
286     IWICBitmapLock *lock;
287     int i;
288     WICRect rc;
289     const BYTE bitmap_data[27] = {
290         128,128,255, 128,128,128, 128,255,128,
291         128,128,128, 128,128,128, 255,255,255,
292         255,128,128, 255,255,255, 255,255,255};
293     BYTE returned_data[27] = {0};
294     BYTE *lock_buffer=NULL;
295     UINT lock_buffer_stride=0;
296     UINT lock_buffer_size=0;
297     WICPixelFormatGUID pixelformat = {0};
298     UINT width=0, height=0;
299     double dpix=10.0, dpiy=10.0;
300     UINT count;
301     WICBitmapPaletteType palette_type;
302
303     hr = IWICImagingFactory_CreateBitmap(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
304         WICBitmapCacheOnLoad, &bitmap);
305     ok(hr == S_OK, "IWICImagingFactory_CreateBitmap failed hr=%x\n", hr);
306
307     if (FAILED(hr))
308         return;
309
310     hr = IWICImagingFactory_CreatePalette(factory, &palette);
311     ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
312
313     hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray256, FALSE);
314     ok(hr == S_OK, "IWICPalette_InitializePredefined failed hr=%x\n", hr);
315
316     hr = IWICBitmap_SetPalette(bitmap, palette);
317     ok(hr == S_OK, "IWICBitmap_SetPalette failed hr=%x\n", hr);
318
319     IWICPalette_Release(palette);
320
321     rc.X = rc.Y = 0;
322     rc.Width = 3;
323     rc.Height = 3;
324     hr = IWICBitmap_Lock(bitmap, &rc, WICBitmapLockWrite, &lock);
325     ok(hr == S_OK, "IWICBitmap_Lock failed hr=%x\n", hr);
326     if (SUCCEEDED(hr))
327     {
328         hr = IWICBitmapLock_GetStride(lock, &lock_buffer_stride);
329         ok(hr == S_OK, "IWICBitmapLock_GetStride failed hr=%x\n", hr);
330         ok(lock_buffer_stride == 12, "got %i, expected 12\n", lock_buffer_stride);
331
332         hr = IWICBitmapLock_GetDataPointer(lock, &lock_buffer_size, &lock_buffer);
333         ok(hr == S_OK, "IWICBitmapLock_GetDataPointer failed hr=%x\n", hr);
334         ok(lock_buffer_size == 33, "got %i, expected 33\n", lock_buffer_size);
335         ok(lock_buffer != NULL, "got NULL data pointer\n");
336
337         for (i=0; i<3; i++)
338             memcpy(lock_buffer + lock_buffer_stride*i, bitmap_data + i*9, 9);
339
340         IWICBitmapLock_Release(lock);
341     }
342
343     hr = IWICBitmap_SetResolution(bitmap, 12.0, 34.0);
344     ok(hr == S_OK, "IWICBitmap_SetResolution failed hr=%x\n", hr);
345
346     hr = IWICImagingFactory_CreateBitmapFromSource(factory, (IWICBitmapSource*)bitmap,
347         WICBitmapCacheOnLoad, &bitmap2);
348     ok(hr == S_OK, "IWICImagingFactory_CreateBitmapFromSource failed hr=%x\n", hr);
349
350     IWICBitmap_Release(bitmap);
351
352     if (FAILED(hr)) return;
353
354     hr = IWICImagingFactory_CreatePalette(factory, &palette);
355     ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
356
357     /* palette isn't copied for non-indexed formats? */
358     hr = IWICBitmap_CopyPalette(bitmap2, palette);
359     ok(hr == WINCODEC_ERR_PALETTEUNAVAILABLE, "IWICBitmap_CopyPalette failed hr=%x\n", hr);
360
361     IWICPalette_Release(palette);
362
363     hr = IWICBitmap_CopyPixels(bitmap2, NULL, 9, 27, returned_data);
364     ok(hr == S_OK, "IWICBitmap_CopyPixels failed hr=%x\n", hr);
365
366     for (i=0; i<27; i++)
367         ok(returned_data[i] == bitmap_data[i], "returned_data[%i] == %i\n", i, returned_data[i]);
368
369     hr = IWICBitmap_GetPixelFormat(bitmap2, &pixelformat);
370     ok(hr == S_OK, "IWICBitmap_GetPixelFormat failed hr=%x\n", hr);
371     ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat24bppBGR), "unexpected pixel format\n");
372
373     hr = IWICBitmap_GetResolution(bitmap2, &dpix, &dpiy);
374     ok(hr == S_OK, "IWICBitmap_GetResolution failed hr=%x\n", hr);
375     ok(dpix == 12.0, "got %f, expected 12.0\n", dpix);
376     ok(dpiy == 34.0, "got %f, expected 34.0\n", dpiy);
377
378     hr = IWICBitmap_GetSize(bitmap2, &width, &height);
379     ok(hr == S_OK, "IWICBitmap_GetSize failed hr=%x\n", hr);
380     ok(width == 3, "got %d, expected 3\n", width);
381     ok(height == 3, "got %d, expected 3\n", height);
382
383     IWICBitmap_Release(bitmap2);
384
385     /* Ensure palette is copied for indexed formats */
386     hr = IWICImagingFactory_CreateBitmap(factory, 3, 3, &GUID_WICPixelFormat4bppIndexed,
387         WICBitmapCacheOnLoad, &bitmap);
388     ok(hr == S_OK, "IWICImagingFactory_CreateBitmap failed hr=%x\n", hr);
389
390     hr = IWICImagingFactory_CreatePalette(factory, &palette);
391     ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
392
393     hr = IWICPalette_InitializePredefined(palette, WICBitmapPaletteTypeFixedGray256, FALSE);
394     ok(hr == S_OK, "IWICPalette_InitializePredefined failed hr=%x\n", hr);
395
396     hr = IWICBitmap_SetPalette(bitmap, palette);
397     ok(hr == S_OK, "IWICBitmap_SetPalette failed hr=%x\n", hr);
398
399     IWICPalette_Release(palette);
400
401     hr = IWICImagingFactory_CreateBitmapFromSource(factory, (IWICBitmapSource*)bitmap,
402         WICBitmapCacheOnLoad, &bitmap2);
403     ok(hr == S_OK, "IWICImagingFactory_CreateBitmapFromSource failed hr=%x\n", hr);
404
405     IWICBitmap_Release(bitmap);
406
407     hr = IWICImagingFactory_CreatePalette(factory, &palette);
408     ok(hr == S_OK, "IWICImagingFactory_CreatePalette failed hr=%x\n", hr);
409
410     hr = IWICBitmap_CopyPalette(bitmap2, palette);
411     ok(hr == S_OK, "IWICBitmap_CopyPalette failed hr=%x\n", hr);
412
413     hr = IWICPalette_GetColorCount(palette, &count);
414     ok(hr == S_OK, "IWICPalette_GetColorCount failed hr=%x\n", hr);
415     ok(count == 256, "unexpected count %d\n", count);
416
417     hr = IWICPalette_GetType(palette, &palette_type);
418     ok(hr == S_OK, "IWICPalette_GetType failed hr=%x\n", hr);
419     ok(palette_type == WICBitmapPaletteTypeFixedGray256, "unexpected palette type %d\n", palette_type);
420
421     IWICPalette_Release(palette);
422
423     hr = IWICBitmap_GetPixelFormat(bitmap2, &pixelformat);
424     ok(hr == S_OK, "IWICBitmap_GetPixelFormat failed hr=%x\n", hr);
425     ok(IsEqualGUID(&pixelformat, &GUID_WICPixelFormat4bppIndexed), "unexpected pixel format\n");
426
427     hr = IWICBitmap_GetSize(bitmap2, &width, &height);
428     ok(hr == S_OK, "IWICBitmap_GetSize failed hr=%x\n", hr);
429     ok(width == 3, "got %d, expected 3\n", width);
430     ok(height == 3, "got %d, expected 3\n", height);
431
432     IWICBitmap_Release(bitmap2);
433 }
434
435 static void test_CreateBitmapFromMemory(void)
436 {
437     BYTE orig_data3x3[27] = {
438         128,128,255, 128,128,128, 128,255,128,
439         128,128,128, 128,128,128, 255,255,255,
440         255,128,128, 255,255,255, 255,255,255 };
441     BYTE data3x3[27];
442     BYTE data3x2[27] = {
443         128,128,255, 128,128,128, 128,255,128,
444         0,0,0, 0,128,128, 255,255,255,
445         255,128,128, 255,0,0, 0,0,0 };
446     BYTE data[27];
447     HRESULT hr;
448     IWICBitmap *bitmap;
449     UINT width, height, i;
450
451     memcpy(data3x3, orig_data3x3, sizeof(data3x3));
452
453     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
454                                                    0, 0, NULL, &bitmap);
455     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
456
457     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
458                                                    0, sizeof(data3x3), data3x3, &bitmap);
459     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
460
461     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
462                                                    6, sizeof(data3x3), data3x3, &bitmap);
463     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
464
465     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
466                                                    12, sizeof(data3x3), data3x3, &bitmap);
467     ok(hr == WINCODEC_ERR_INSUFFICIENTBUFFER, "expected WINCODEC_ERR_INSUFFICIENTBUFFER, got %#x\n", hr);
468
469     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
470                                                    9, sizeof(data3x3) - 1, data3x3, &bitmap);
471     ok(hr == WINCODEC_ERR_INSUFFICIENTBUFFER, "expected WINCODEC_ERR_INSUFFICIENTBUFFER, got %#x\n", hr);
472
473     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 3, &GUID_WICPixelFormat24bppBGR,
474                                                    9, sizeof(data3x3), data3x3, &bitmap);
475     ok(hr == S_OK, "IWICImagingFactory_CreateBitmapFromMemory error %#x\n", hr);
476
477     hr = IWICBitmap_GetSize(bitmap, &width, &height);
478     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
479     ok(width == 3, "expected 3, got %u\n", width);
480     ok(height == 3, "expected 3, got %u\n", height);
481
482     data3x3[2] = 192;
483
484     memset(data, 0, sizeof(data));
485     hr = IWICBitmap_CopyPixels(bitmap, NULL, 9, sizeof(data), data);
486     ok(hr == S_OK, "IWICBitmap_CopyPixels error %#x\n", hr);
487     for (i = 0; i < sizeof(data); i++)
488         ok(data[i] == orig_data3x3[i], "%u: expected %u, got %u\n", i, data[i], data3x3[i]);
489
490     IWICBitmap_Release(bitmap);
491
492     hr = IWICImagingFactory_CreateBitmapFromMemory(factory, 3, 2, &GUID_WICPixelFormat24bppBGR,
493                                                    13, sizeof(orig_data3x3), orig_data3x3, &bitmap);
494     ok(hr == S_OK, "IWICImagingFactory_CreateBitmapFromMemory error %#x\n", hr);
495
496     hr = IWICBitmap_GetSize(bitmap, &width, &height);
497     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
498     ok(width == 3, "expected 3, got %u\n", width);
499     ok(height == 2, "expected 2, got %u\n", height);
500
501     memset(data, 0, sizeof(data));
502     hr = IWICBitmap_CopyPixels(bitmap, NULL, 13, sizeof(data), data);
503     ok(hr == S_OK, "IWICBitmap_CopyPixels error %#x\n", hr);
504     for (i = 0; i < sizeof(data); i++)
505         ok(data[i] == data3x2[i], "%u: expected %u, got %u\n", i, data3x2[i], data[i]);
506
507     IWICBitmap_Release(bitmap);
508 }
509
510 static void test_CreateBitmapFromHICON(void)
511 {
512     static const char bits[4096];
513     HICON icon;
514     ICONINFO info;
515     HRESULT hr;
516     IWICBitmap *bitmap;
517     UINT width, height;
518     WICPixelFormatGUID format;
519
520     /* 1 bpp mask */
521     info.fIcon = 1;
522     info.xHotspot = 0;
523     info.yHotspot = 0;
524     info.hbmColor = 0;
525     info.hbmMask = CreateBitmap(16, 32, 1, 1, bits);
526     ok(info.hbmMask != 0, "CreateBitmap failed\n");
527     icon = CreateIconIndirect(&info);
528     ok(icon != 0, "CreateIconIndirect failed\n");
529     DeleteObject(info.hbmMask);
530
531     hr = IWICImagingFactory_CreateBitmapFromHICON(factory, 0, NULL);
532 todo_wine
533     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
534
535     hr = IWICImagingFactory_CreateBitmapFromHICON(factory, 0, &bitmap);
536 todo_wine
537     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_CURSOR_HANDLE), "expected ERROR_INVALID_CURSOR_HANDLE, got %#x\n", hr);
538
539     hr = IWICImagingFactory_CreateBitmapFromHICON(factory, icon, NULL);
540 todo_wine
541     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got %#x\n", hr);
542
543     hr = IWICImagingFactory_CreateBitmapFromHICON(factory, icon, &bitmap);
544 todo_wine
545     ok(hr == S_OK, "CreateBitmapFromHICON error %#x\n", hr);
546     DestroyIcon(icon);
547     if (hr != S_OK) return;
548
549     IWICBitmap_GetPixelFormat(bitmap, &format);
550     ok(IsEqualGUID(&format, &GUID_WICPixelFormat32bppBGRA),
551        "unexpected pixel format %s\n", debugstr_guid(&format));
552
553     IWICBitmap_GetSize(bitmap, &width, &height);
554     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
555     ok(width == 16, "expected 16, got %u\n", width);
556     ok(height == 16, "expected 16, got %u\n", height);
557
558     IWICBitmap_Release(bitmap);
559
560     /* 24 bpp color, 1 bpp mask */
561     info.fIcon = 1;
562     info.xHotspot = 0;
563     info.yHotspot = 0;
564     info.hbmColor = CreateBitmap(16, 16, 1, 24, bits);
565     ok(info.hbmColor != 0, "CreateBitmap failed\n");
566     info.hbmMask = CreateBitmap(16, 16, 1, 1, bits);
567     ok(info.hbmMask != 0, "CreateBitmap failed\n");
568     icon = CreateIconIndirect(&info);
569     ok(icon != 0, "CreateIconIndirect failed\n");
570     DeleteObject(info.hbmColor);
571     DeleteObject(info.hbmMask);
572
573     hr = IWICImagingFactory_CreateBitmapFromHICON(factory, icon, &bitmap);
574     ok(hr == S_OK, "CreateBitmapFromHICON error %#x\n", hr);
575     DestroyIcon(icon);
576
577     IWICBitmap_GetPixelFormat(bitmap, &format);
578     ok(IsEqualGUID(&format, &GUID_WICPixelFormat32bppBGRA),
579        "unexpected pixel format %s\n", debugstr_guid(&format));
580
581     IWICBitmap_GetSize(bitmap, &width, &height);
582     ok(hr == S_OK, "IWICBitmap_GetSize error %#x\n", hr);
583     ok(width == 16, "expected 16, got %u\n", width);
584     ok(height == 16, "expected 16, got %u\n", height);
585
586     IWICBitmap_Release(bitmap);
587 }
588
589 START_TEST(bitmap)
590 {
591     HRESULT hr;
592
593     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
594
595     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
596         &IID_IWICImagingFactory, (void**)&factory);
597     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
598
599     test_createbitmap();
600     test_createbitmapfromsource();
601     test_CreateBitmapFromMemory();
602     test_CreateBitmapFromHICON();
603
604     IWICImagingFactory_Release(factory);
605
606     CoUninitialize();
607 }