d3dcompiler: Add argument check in D3DReflect().
[wine] / dlls / windowscodecs / tests / converter.c
1 /*
2  * Copyright 2009 Vincent Povirk
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 #include <math.h>
21
22 #define COBJMACROS
23 #define CONST_VTABLE
24
25 #include "windef.h"
26 #include "objbase.h"
27 #include "wincodec.h"
28 #include "wine/test.h"
29
30 typedef struct bitmap_data {
31     const WICPixelFormatGUID *format;
32     UINT bpp;
33     const BYTE *bits;
34     UINT width;
35     UINT height;
36     double xres;
37     double yres;
38 } bitmap_data;
39
40 typedef struct BitmapTestSrc {
41     IWICBitmapSource IWICBitmapSource_iface;
42     LONG ref;
43     const bitmap_data *data;
44 } BitmapTestSrc;
45
46 static inline BitmapTestSrc *impl_from_IWICBitmapSource(IWICBitmapSource *iface)
47 {
48     return CONTAINING_RECORD(iface, BitmapTestSrc, IWICBitmapSource_iface);
49 }
50
51 static HRESULT WINAPI BitmapTestSrc_QueryInterface(IWICBitmapSource *iface, REFIID iid,
52     void **ppv)
53 {
54     if (!ppv) return E_INVALIDARG;
55
56     if (IsEqualIID(&IID_IUnknown, iid) ||
57         IsEqualIID(&IID_IWICBitmapSource, iid))
58         *ppv = iface;
59     else
60         return E_NOINTERFACE;
61
62     IUnknown_AddRef((IUnknown*)*ppv);
63     return S_OK;
64 }
65
66 static ULONG WINAPI BitmapTestSrc_AddRef(IWICBitmapSource *iface)
67 {
68     BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
69     ULONG ref = InterlockedIncrement(&This->ref);
70     return ref;
71 }
72
73 static ULONG WINAPI BitmapTestSrc_Release(IWICBitmapSource *iface)
74 {
75     BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
76     ULONG ref = InterlockedDecrement(&This->ref);
77     return ref;
78 }
79
80 static HRESULT WINAPI BitmapTestSrc_GetSize(IWICBitmapSource *iface,
81     UINT *puiWidth, UINT *puiHeight)
82 {
83     BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
84     *puiWidth = This->data->width;
85     *puiHeight = This->data->height;
86     return S_OK;
87 }
88
89 static HRESULT WINAPI BitmapTestSrc_GetPixelFormat(IWICBitmapSource *iface,
90     WICPixelFormatGUID *pPixelFormat)
91 {
92     BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
93     memcpy(pPixelFormat, This->data->format, sizeof(GUID));
94     return S_OK;
95 }
96
97 static HRESULT WINAPI BitmapTestSrc_GetResolution(IWICBitmapSource *iface,
98     double *pDpiX, double *pDpiY)
99 {
100     BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
101     *pDpiX = This->data->xres;
102     *pDpiY = This->data->yres;
103     return S_OK;
104 }
105
106 static HRESULT WINAPI BitmapTestSrc_CopyPalette(IWICBitmapSource *iface,
107     IWICPalette *pIPalette)
108 {
109     return E_NOTIMPL;
110 }
111
112 static HRESULT WINAPI BitmapTestSrc_CopyPixels(IWICBitmapSource *iface,
113     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
114 {
115     BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
116     UINT bytesperrow;
117     UINT srcstride;
118     UINT row_offset;
119     WICRect rc;
120
121     if (!prc)
122     {
123         rc.X = 0;
124         rc.Y = 0;
125         rc.Width = This->data->width;
126         rc.Height = This->data->height;
127         prc = &rc;
128     }
129     else
130     {
131         if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->data->width || prc->Y+prc->Height > This->data->height)
132             return E_INVALIDARG;
133     }
134
135     bytesperrow = ((This->data->bpp * prc->Width)+7)/8;
136     srcstride = ((This->data->bpp * This->data->width)+7)/8;
137
138     if (cbStride < bytesperrow)
139         return E_INVALIDARG;
140
141     if ((cbStride * prc->Height) > cbBufferSize)
142         return E_INVALIDARG;
143
144     row_offset = prc->X * This->data->bpp;
145
146     if (row_offset % 8 == 0)
147     {
148         UINT row;
149         const BYTE *src;
150         BYTE *dst;
151
152         src = This->data->bits + (row_offset / 8) + prc->Y * srcstride;
153         dst = pbBuffer;
154         for (row=0; row < prc->Height; row++)
155         {
156             memcpy(dst, src, bytesperrow);
157             src += srcstride;
158             dst += cbStride;
159         }
160         return S_OK;
161     }
162     else
163     {
164         ok(0, "bitmap %p was asked to copy pixels not aligned on a byte boundary\n", iface);
165         return E_FAIL;
166     }
167 }
168
169 static const IWICBitmapSourceVtbl BitmapTestSrc_Vtbl = {
170     BitmapTestSrc_QueryInterface,
171     BitmapTestSrc_AddRef,
172     BitmapTestSrc_Release,
173     BitmapTestSrc_GetSize,
174     BitmapTestSrc_GetPixelFormat,
175     BitmapTestSrc_GetResolution,
176     BitmapTestSrc_CopyPalette,
177     BitmapTestSrc_CopyPixels
178 };
179
180 static void CreateTestBitmap(const bitmap_data *data, BitmapTestSrc **This)
181 {
182     *This = HeapAlloc(GetProcessHeap(), 0, sizeof(**This));
183
184     if (*This)
185     {
186         (*This)->IWICBitmapSource_iface.lpVtbl = &BitmapTestSrc_Vtbl;
187         (*This)->ref = 1;
188         (*This)->data = data;
189     }
190 }
191
192 static void DeleteTestBitmap(BitmapTestSrc *This)
193 {
194     ok(This->IWICBitmapSource_iface.lpVtbl == &BitmapTestSrc_Vtbl, "test bitmap %p deleted with incorrect vtable\n", This);
195     ok(This->ref == 1, "test bitmap %p deleted with %i references instead of 1\n", This, This->ref);
196     HeapFree(GetProcessHeap(), 0, This);
197 }
198
199 static void compare_bitmap_data(const struct bitmap_data *expect, IWICBitmapSource *source, const char *name)
200 {
201     BYTE *converted_bits;
202     UINT width, height;
203     double xres, yres;
204     WICRect prc;
205     UINT stride, buffersize;
206     GUID dst_pixelformat;
207     HRESULT hr;
208
209     hr = IWICBitmapSource_GetSize(source, &width, &height);
210     ok(SUCCEEDED(hr), "GetSize(%s) failed, hr=%x\n", name, hr);
211     ok(width == expect->width, "expecting %u, got %u (%s)\n", expect->width, width, name);
212     ok(height == expect->height, "expecting %u, got %u (%s)\n", expect->height, height, name);
213
214     hr = IWICBitmapSource_GetResolution(source, &xres, &yres);
215     ok(SUCCEEDED(hr), "GetResolution(%s) failed, hr=%x\n", name, hr);
216     ok(fabs(xres - expect->xres) < 0.02, "expecting %0.2f, got %0.2f (%s)\n", expect->xres, xres, name);
217     ok(fabs(yres - expect->yres) < 0.02, "expecting %0.2f, got %0.2f (%s)\n", expect->yres, yres, name);
218
219     hr = IWICBitmapSource_GetPixelFormat(source, &dst_pixelformat);
220     ok(SUCCEEDED(hr), "GetPixelFormat(%s) failed, hr=%x\n", name, hr);
221     ok(IsEqualGUID(&dst_pixelformat, expect->format), "got unexpected pixel format (%s)\n", name);
222
223     prc.X = 0;
224     prc.Y = 0;
225     prc.Width = expect->width;
226     prc.Height = expect->height;
227
228     stride = (expect->bpp * expect->width + 7) / 8;
229     buffersize = stride * expect->height;
230
231     converted_bits = HeapAlloc(GetProcessHeap(), 0, buffersize);
232     hr = IWICBitmapSource_CopyPixels(source, &prc, stride, buffersize, converted_bits);
233     ok(SUCCEEDED(hr), "CopyPixels(%s) failed, hr=%x\n", name, hr);
234     if (IsEqualGUID(expect->format, &GUID_WICPixelFormat32bppBGR))
235     {
236         /* ignore the padding byte when comparing data */
237         UINT i;
238         BOOL equal=TRUE;
239         const DWORD *a=(const DWORD*)expect->bits, *b=(const DWORD*)converted_bits;
240         for (i=0; i<(buffersize/4); i++)
241             if ((a[i]&0xffffff) != (b[i]&0xffffff))
242             {
243                 equal = FALSE;
244                 break;
245             }
246         ok(equal, "unexpected pixel data (%s)\n", name);
247     }
248     else
249         ok(memcmp(expect->bits, converted_bits, buffersize) == 0, "unexpected pixel data (%s)\n", name);
250
251     /* Test with NULL rectangle - should copy the whole bitmap */
252     hr = IWICBitmapSource_CopyPixels(source, NULL, stride, buffersize, converted_bits);
253     ok(SUCCEEDED(hr), "CopyPixels(%s,rc=NULL) failed, hr=%x\n", name, hr);
254     if (IsEqualGUID(expect->format, &GUID_WICPixelFormat32bppBGR))
255     {
256         /* ignore the padding byte when comparing data */
257         UINT i;
258         BOOL equal=TRUE;
259         const DWORD *a=(const DWORD*)expect->bits, *b=(const DWORD*)converted_bits;
260         for (i=0; i<(buffersize/4); i++)
261             if ((a[i]&0xffffff) != (b[i]&0xffffff))
262             {
263                 equal = FALSE;
264                 break;
265             }
266         ok(equal, "unexpected pixel data with rc=NULL (%s)\n", name);
267     }
268     else
269         ok(memcmp(expect->bits, converted_bits, buffersize) == 0, "unexpected pixel data with rc=NULL (%s)\n", name);
270
271     HeapFree(GetProcessHeap(), 0, converted_bits);
272 }
273
274 static const BYTE bits_24bppBGR[] = {
275     255,0,0, 0,255,0, 0,0,255, 0,0,0,
276     0,255,255, 255,0,255, 255,255,0, 255,255,255};
277 static const struct bitmap_data testdata_24bppBGR = {
278     &GUID_WICPixelFormat24bppBGR, 24, bits_24bppBGR, 4, 2, 96.0, 96.0};
279
280 static const BYTE bits_32bppBGR[] = {
281     255,0,0,80, 0,255,0,80, 0,0,255,80, 0,0,0,80,
282     0,255,255,80, 255,0,255,80, 255,255,0,80, 255,255,255,80};
283 static const struct bitmap_data testdata_32bppBGR = {
284     &GUID_WICPixelFormat32bppBGR, 32, bits_32bppBGR, 4, 2, 96.0, 96.0};
285
286 static const BYTE bits_32bppBGRA[] = {
287     255,0,0,255, 0,255,0,255, 0,0,255,255, 0,0,0,255,
288     0,255,255,255, 255,0,255,255, 255,255,0,255, 255,255,255,255};
289 static const struct bitmap_data testdata_32bppBGRA = {
290     &GUID_WICPixelFormat32bppBGRA, 32, bits_32bppBGRA, 4, 2, 96.0, 96.0};
291
292 static void test_conversion(const struct bitmap_data *src, const struct bitmap_data *dst, const char *name, BOOL todo)
293 {
294     BitmapTestSrc *src_obj;
295     IWICBitmapSource *dst_bitmap;
296     HRESULT hr;
297
298     CreateTestBitmap(src, &src_obj);
299
300     hr = WICConvertBitmapSource(dst->format, &src_obj->IWICBitmapSource_iface, &dst_bitmap);
301     if (todo)
302         todo_wine ok(SUCCEEDED(hr), "WICConvertBitmapSource(%s) failed, hr=%x\n", name, hr);
303     else
304         ok(SUCCEEDED(hr), "WICConvertBitmapSource(%s) failed, hr=%x\n", name, hr);
305
306     if (SUCCEEDED(hr))
307     {
308         compare_bitmap_data(dst, dst_bitmap, name);
309
310         IWICBitmapSource_Release(dst_bitmap);
311     }
312
313     DeleteTestBitmap(src_obj);
314 }
315
316 static void test_invalid_conversion(void)
317 {
318     BitmapTestSrc *src_obj;
319     IWICBitmapSource *dst_bitmap;
320     HRESULT hr;
321
322     CreateTestBitmap(&testdata_32bppBGRA, &src_obj);
323
324     /* convert to a non-pixel-format GUID */
325     hr = WICConvertBitmapSource(&GUID_VendorMicrosoft, &src_obj->IWICBitmapSource_iface, &dst_bitmap);
326     ok(hr == WINCODEC_ERR_COMPONENTNOTFOUND, "WICConvertBitmapSource returned %x\n", hr);
327
328     DeleteTestBitmap(src_obj);
329 }
330
331 static void test_default_converter(void)
332 {
333     BitmapTestSrc *src_obj;
334     IWICFormatConverter *converter;
335     BOOL can_convert=1;
336     HRESULT hr;
337
338     CreateTestBitmap(&testdata_32bppBGRA, &src_obj);
339
340     hr = CoCreateInstance(&CLSID_WICDefaultFormatConverter, NULL, CLSCTX_INPROC_SERVER,
341         &IID_IWICFormatConverter, (void**)&converter);
342     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
343     if (SUCCEEDED(hr))
344     {
345         hr = IWICFormatConverter_CanConvert(converter, &GUID_WICPixelFormat32bppBGRA,
346             &GUID_WICPixelFormat32bppBGR, &can_convert);
347         ok(SUCCEEDED(hr), "CanConvert returned %x\n", hr);
348         ok(can_convert, "expected TRUE, got %i\n", can_convert);
349
350         hr = IWICFormatConverter_Initialize(converter, &src_obj->IWICBitmapSource_iface,
351             &GUID_WICPixelFormat32bppBGR, WICBitmapDitherTypeNone, NULL, 0.0,
352             WICBitmapPaletteTypeCustom);
353         ok(SUCCEEDED(hr), "Initialize returned %x\n", hr);
354
355         if (SUCCEEDED(hr))
356             compare_bitmap_data(&testdata_32bppBGR, (IWICBitmapSource*)converter, "default converter");
357
358         IWICFormatConverter_Release(converter);
359     }
360
361     DeleteTestBitmap(src_obj);
362 }
363
364 static void test_encoder(const struct bitmap_data *src, const CLSID* clsid_encoder,
365     const struct bitmap_data *dst, const CLSID *clsid_decoder, const char *name)
366 {
367     HRESULT hr;
368     IWICBitmapEncoder *encoder;
369     BitmapTestSrc *src_obj;
370     HGLOBAL hglobal;
371     IStream *stream;
372     IWICBitmapFrameEncode *frameencode;
373     IPropertyBag2 *options=NULL;
374     IWICBitmapDecoder *decoder;
375     IWICBitmapFrameDecode *framedecode;
376     WICPixelFormatGUID pixelformat;
377
378     CreateTestBitmap(src, &src_obj);
379
380     hr = CoCreateInstance(clsid_encoder, NULL, CLSCTX_INPROC_SERVER,
381         &IID_IWICBitmapEncoder, (void**)&encoder);
382     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
383     if (SUCCEEDED(hr))
384     {
385         hglobal = GlobalAlloc(GMEM_MOVEABLE, 0);
386         ok(hglobal != NULL, "GlobalAlloc failed\n");
387         if (hglobal)
388         {
389             hr = CreateStreamOnHGlobal(hglobal, TRUE, &stream);
390             ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
391         }
392
393         if (hglobal && SUCCEEDED(hr))
394         {
395             hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
396             ok(SUCCEEDED(hr), "Initialize failed, hr=%x\n", hr);
397
398             hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &options);
399             ok(SUCCEEDED(hr), "CreateFrame failed, hr=%x\n", hr);
400             if (SUCCEEDED(hr))
401             {
402                 hr = IWICBitmapFrameEncode_Initialize(frameencode, options);
403                 ok(SUCCEEDED(hr), "Initialize failed, hr=%x\n", hr);
404
405                 memcpy(&pixelformat, src->format, sizeof(GUID));
406                 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &pixelformat);
407                 ok(SUCCEEDED(hr), "SetPixelFormat failed, hr=%x\n", hr);
408                 ok(IsEqualGUID(&pixelformat, src->format), "SetPixelFormat changed the format\n");
409
410                 hr = IWICBitmapFrameEncode_SetSize(frameencode, src->width, src->height);
411                 ok(SUCCEEDED(hr), "SetSize failed, hr=%x\n", hr);
412
413                 hr = IWICBitmapFrameEncode_WriteSource(frameencode, &src_obj->IWICBitmapSource_iface, NULL);
414                 ok(SUCCEEDED(hr), "WriteSource failed, hr=%x\n", hr);
415
416                 hr = IWICBitmapFrameEncode_Commit(frameencode);
417                 ok(SUCCEEDED(hr), "Commit failed, hr=%x\n", hr);
418
419                 hr = IWICBitmapEncoder_Commit(encoder);
420                 ok(SUCCEEDED(hr), "Commit failed, hr=%x\n", hr);
421
422                 IWICBitmapFrameEncode_Release(frameencode);
423                 IPropertyBag2_Release(options);
424             }
425
426             if (SUCCEEDED(hr))
427             {
428                 hr = CoCreateInstance(clsid_decoder, NULL, CLSCTX_INPROC_SERVER,
429                     &IID_IWICBitmapDecoder, (void**)&decoder);
430                 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
431             }
432
433             if (SUCCEEDED(hr))
434             {
435                 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnDemand);
436                 ok(SUCCEEDED(hr), "Initialize failed, hr=%x\n", hr);
437
438                 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
439                 ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
440
441                 if (SUCCEEDED(hr))
442                 {
443                     compare_bitmap_data(dst, (IWICBitmapSource*)framedecode, name);
444
445                     IWICBitmapFrameDecode_Release(framedecode);
446                 }
447
448                 IWICBitmapDecoder_Release(decoder);
449             }
450
451             IStream_Release(stream);
452         }
453
454         IWICBitmapEncoder_Release(encoder);
455     }
456
457     DeleteTestBitmap(src_obj);
458 }
459
460 START_TEST(converter)
461 {
462     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
463
464     test_conversion(&testdata_32bppBGRA, &testdata_32bppBGR, "BGRA -> BGR", 0);
465     test_conversion(&testdata_32bppBGR, &testdata_32bppBGRA, "BGR -> BGRA", 0);
466     test_conversion(&testdata_32bppBGRA, &testdata_32bppBGRA, "BGRA -> BGRA", 0);
467     test_invalid_conversion();
468     test_default_converter();
469
470     test_encoder(&testdata_32bppBGR, &CLSID_WICBmpEncoder,
471                  &testdata_32bppBGR, &CLSID_WICBmpDecoder, "BMP encoder 32bppBGR");
472
473     test_encoder(&testdata_24bppBGR, &CLSID_WICPngEncoder,
474                  &testdata_24bppBGR, &CLSID_WICPngDecoder, "PNG encoder 24bppBGR");
475
476     CoUninitialize();
477 }