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