2 * Copyright 2009 Vincent Povirk
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/test.h"
30 typedef struct bitmap_data {
31 const WICPixelFormatGUID *format;
40 typedef struct BitmapTestSrc {
41 IWICBitmapSource IWICBitmapSource_iface;
43 const bitmap_data *data;
46 static inline BitmapTestSrc *impl_from_IWICBitmapSource(IWICBitmapSource *iface)
48 return CONTAINING_RECORD(iface, BitmapTestSrc, IWICBitmapSource_iface);
51 static HRESULT WINAPI BitmapTestSrc_QueryInterface(IWICBitmapSource *iface, REFIID iid,
54 if (!ppv) return E_INVALIDARG;
56 if (IsEqualIID(&IID_IUnknown, iid) ||
57 IsEqualIID(&IID_IWICBitmapSource, iid))
62 IUnknown_AddRef((IUnknown*)*ppv);
66 static ULONG WINAPI BitmapTestSrc_AddRef(IWICBitmapSource *iface)
68 BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
69 ULONG ref = InterlockedIncrement(&This->ref);
73 static ULONG WINAPI BitmapTestSrc_Release(IWICBitmapSource *iface)
75 BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
76 ULONG ref = InterlockedDecrement(&This->ref);
80 static HRESULT WINAPI BitmapTestSrc_GetSize(IWICBitmapSource *iface,
81 UINT *puiWidth, UINT *puiHeight)
83 BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
84 *puiWidth = This->data->width;
85 *puiHeight = This->data->height;
89 static HRESULT WINAPI BitmapTestSrc_GetPixelFormat(IWICBitmapSource *iface,
90 WICPixelFormatGUID *pPixelFormat)
92 BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
93 memcpy(pPixelFormat, This->data->format, sizeof(GUID));
97 static HRESULT WINAPI BitmapTestSrc_GetResolution(IWICBitmapSource *iface,
98 double *pDpiX, double *pDpiY)
100 BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
101 *pDpiX = This->data->xres;
102 *pDpiY = This->data->yres;
106 static HRESULT WINAPI BitmapTestSrc_CopyPalette(IWICBitmapSource *iface,
107 IWICPalette *pIPalette)
112 static HRESULT WINAPI BitmapTestSrc_CopyPixels(IWICBitmapSource *iface,
113 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
115 BitmapTestSrc *This = impl_from_IWICBitmapSource(iface);
125 rc.Width = This->data->width;
126 rc.Height = This->data->height;
131 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->data->width || prc->Y+prc->Height > This->data->height)
135 bytesperrow = ((This->data->bpp * prc->Width)+7)/8;
136 srcstride = ((This->data->bpp * This->data->width)+7)/8;
138 if (cbStride < bytesperrow)
141 if ((cbStride * prc->Height) > cbBufferSize)
144 row_offset = prc->X * This->data->bpp;
146 if (row_offset % 8 == 0)
152 src = This->data->bits + (row_offset / 8) + prc->Y * srcstride;
154 for (row=0; row < prc->Height; row++)
156 memcpy(dst, src, bytesperrow);
164 ok(0, "bitmap %p was asked to copy pixels not aligned on a byte boundary\n", iface);
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
180 static void CreateTestBitmap(const bitmap_data *data, BitmapTestSrc **This)
182 *This = HeapAlloc(GetProcessHeap(), 0, sizeof(**This));
186 (*This)->IWICBitmapSource_iface.lpVtbl = &BitmapTestSrc_Vtbl;
188 (*This)->data = data;
192 static void DeleteTestBitmap(BitmapTestSrc *This)
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);
199 static void compare_bitmap_data(const struct bitmap_data *expect, IWICBitmapSource *source, const char *name)
201 BYTE *converted_bits;
205 UINT stride, buffersize;
206 GUID dst_pixelformat;
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);
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);
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);
225 prc.Width = expect->width;
226 prc.Height = expect->height;
228 stride = (expect->bpp * expect->width + 7) / 8;
229 buffersize = stride * expect->height;
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))
236 /* ignore the padding byte when comparing data */
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))
246 ok(equal, "unexpected pixel data (%s)\n", name);
249 ok(memcmp(expect->bits, converted_bits, buffersize) == 0, "unexpected pixel data (%s)\n", name);
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))
256 /* ignore the padding byte when comparing data */
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))
266 ok(equal, "unexpected pixel data with rc=NULL (%s)\n", name);
269 ok(memcmp(expect->bits, converted_bits, buffersize) == 0, "unexpected pixel data with rc=NULL (%s)\n", name);
271 HeapFree(GetProcessHeap(), 0, converted_bits);
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};
280 static const BYTE bits_24bppRGB[] = {
281 0,0,255, 0,255,0, 255,0,0, 0,0,0,
282 255,255,0, 255,0,255, 0,255,255, 255,255,255};
283 static const struct bitmap_data testdata_24bppRGB = {
284 &GUID_WICPixelFormat24bppRGB, 24, bits_24bppRGB, 4, 2, 96.0, 96.0};
286 static const BYTE bits_32bppBGR[] = {
287 255,0,0,80, 0,255,0,80, 0,0,255,80, 0,0,0,80,
288 0,255,255,80, 255,0,255,80, 255,255,0,80, 255,255,255,80};
289 static const struct bitmap_data testdata_32bppBGR = {
290 &GUID_WICPixelFormat32bppBGR, 32, bits_32bppBGR, 4, 2, 96.0, 96.0};
292 static const BYTE bits_32bppBGRA[] = {
293 255,0,0,255, 0,255,0,255, 0,0,255,255, 0,0,0,255,
294 0,255,255,255, 255,0,255,255, 255,255,0,255, 255,255,255,255};
295 static const struct bitmap_data testdata_32bppBGRA = {
296 &GUID_WICPixelFormat32bppBGRA, 32, bits_32bppBGRA, 4, 2, 96.0, 96.0};
298 static void test_conversion(const struct bitmap_data *src, const struct bitmap_data *dst, const char *name, BOOL todo)
300 BitmapTestSrc *src_obj;
301 IWICBitmapSource *dst_bitmap;
304 CreateTestBitmap(src, &src_obj);
306 hr = WICConvertBitmapSource(dst->format, &src_obj->IWICBitmapSource_iface, &dst_bitmap);
308 todo_wine ok(SUCCEEDED(hr), "WICConvertBitmapSource(%s) failed, hr=%x\n", name, hr);
310 ok(SUCCEEDED(hr), "WICConvertBitmapSource(%s) failed, hr=%x\n", name, hr);
314 compare_bitmap_data(dst, dst_bitmap, name);
316 IWICBitmapSource_Release(dst_bitmap);
319 DeleteTestBitmap(src_obj);
322 static void test_invalid_conversion(void)
324 BitmapTestSrc *src_obj;
325 IWICBitmapSource *dst_bitmap;
328 CreateTestBitmap(&testdata_32bppBGRA, &src_obj);
330 /* convert to a non-pixel-format GUID */
331 hr = WICConvertBitmapSource(&GUID_VendorMicrosoft, &src_obj->IWICBitmapSource_iface, &dst_bitmap);
332 ok(hr == WINCODEC_ERR_COMPONENTNOTFOUND, "WICConvertBitmapSource returned %x\n", hr);
334 DeleteTestBitmap(src_obj);
337 static void test_default_converter(void)
339 BitmapTestSrc *src_obj;
340 IWICFormatConverter *converter;
344 CreateTestBitmap(&testdata_32bppBGRA, &src_obj);
346 hr = CoCreateInstance(&CLSID_WICDefaultFormatConverter, NULL, CLSCTX_INPROC_SERVER,
347 &IID_IWICFormatConverter, (void**)&converter);
348 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
351 hr = IWICFormatConverter_CanConvert(converter, &GUID_WICPixelFormat32bppBGRA,
352 &GUID_WICPixelFormat32bppBGR, &can_convert);
353 ok(SUCCEEDED(hr), "CanConvert returned %x\n", hr);
354 ok(can_convert, "expected TRUE, got %i\n", can_convert);
356 hr = IWICFormatConverter_Initialize(converter, &src_obj->IWICBitmapSource_iface,
357 &GUID_WICPixelFormat32bppBGR, WICBitmapDitherTypeNone, NULL, 0.0,
358 WICBitmapPaletteTypeCustom);
359 ok(SUCCEEDED(hr), "Initialize returned %x\n", hr);
362 compare_bitmap_data(&testdata_32bppBGR, (IWICBitmapSource*)converter, "default converter");
364 IWICFormatConverter_Release(converter);
367 DeleteTestBitmap(src_obj);
370 static void test_multi_encoder(const struct bitmap_data **srcs, const CLSID* clsid_encoder,
371 const struct bitmap_data **dsts, const CLSID *clsid_decoder, const char *name)
374 IWICBitmapEncoder *encoder;
375 BitmapTestSrc *src_obj;
378 IWICBitmapFrameEncode *frameencode;
379 IPropertyBag2 *options=NULL;
380 IWICBitmapDecoder *decoder;
381 IWICBitmapFrameDecode *framedecode;
382 WICPixelFormatGUID pixelformat;
385 hr = CoCreateInstance(clsid_encoder, NULL, CLSCTX_INPROC_SERVER,
386 &IID_IWICBitmapEncoder, (void**)&encoder);
387 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
390 hglobal = GlobalAlloc(GMEM_MOVEABLE, 0);
391 ok(hglobal != NULL, "GlobalAlloc failed\n");
394 hr = CreateStreamOnHGlobal(hglobal, TRUE, &stream);
395 ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
398 if (hglobal && SUCCEEDED(hr))
400 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
401 ok(SUCCEEDED(hr), "Initialize failed, hr=%x\n", hr);
404 while (SUCCEEDED(hr) && srcs[i])
406 CreateTestBitmap(srcs[i], &src_obj);
408 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &options);
409 ok(SUCCEEDED(hr), "CreateFrame failed, hr=%x\n", hr);
412 hr = IWICBitmapFrameEncode_Initialize(frameencode, options);
413 ok(SUCCEEDED(hr), "Initialize failed, hr=%x\n", hr);
415 memcpy(&pixelformat, srcs[i]->format, sizeof(GUID));
416 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &pixelformat);
417 ok(SUCCEEDED(hr), "SetPixelFormat failed, hr=%x\n", hr);
418 ok(IsEqualGUID(&pixelformat, srcs[i]->format), "SetPixelFormat changed the format\n");
420 hr = IWICBitmapFrameEncode_SetSize(frameencode, srcs[i]->width, srcs[i]->height);
421 ok(SUCCEEDED(hr), "SetSize failed, hr=%x\n", hr);
423 hr = IWICBitmapFrameEncode_WriteSource(frameencode, &src_obj->IWICBitmapSource_iface, NULL);
424 ok(SUCCEEDED(hr), "WriteSource failed, hr=%x\n", hr);
426 hr = IWICBitmapFrameEncode_Commit(frameencode);
427 ok(SUCCEEDED(hr), "Commit failed, hr=%x\n", hr);
429 IWICBitmapFrameEncode_Release(frameencode);
430 IPropertyBag2_Release(options);
433 DeleteTestBitmap(src_obj);
440 hr = IWICBitmapEncoder_Commit(encoder);
441 ok(SUCCEEDED(hr), "Commit failed, hr=%x\n", hr);
446 hr = CoCreateInstance(clsid_decoder, NULL, CLSCTX_INPROC_SERVER,
447 &IID_IWICBitmapDecoder, (void**)&decoder);
448 ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
453 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnDemand);
454 ok(SUCCEEDED(hr), "Initialize failed, hr=%x\n", hr);
457 while (SUCCEEDED(hr) && dsts[i])
459 hr = IWICBitmapDecoder_GetFrame(decoder, i, &framedecode);
460 ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
464 compare_bitmap_data(dsts[i], (IWICBitmapSource*)framedecode, name);
466 IWICBitmapFrameDecode_Release(framedecode);
472 IWICBitmapDecoder_Release(decoder);
475 IStream_Release(stream);
478 IWICBitmapEncoder_Release(encoder);
482 static void test_encoder(const struct bitmap_data *src, const CLSID* clsid_encoder,
483 const struct bitmap_data *dst, const CLSID *clsid_decoder, const char *name)
485 const struct bitmap_data *srcs[2];
486 const struct bitmap_data *dsts[2];
493 test_multi_encoder(srcs, clsid_encoder, dsts, clsid_decoder, name);
496 static const struct bitmap_data *multiple_frames[3] = {
501 START_TEST(converter)
503 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
505 test_conversion(&testdata_32bppBGRA, &testdata_32bppBGR, "BGRA -> BGR", 0);
506 test_conversion(&testdata_32bppBGR, &testdata_32bppBGRA, "BGR -> BGRA", 0);
507 test_conversion(&testdata_32bppBGRA, &testdata_32bppBGRA, "BGRA -> BGRA", 0);
509 test_conversion(&testdata_24bppBGR, &testdata_24bppBGR, "24bppBGR -> 24bppBGR", 0);
510 test_conversion(&testdata_24bppBGR, &testdata_24bppRGB, "24bppBGR -> 24bppRGB", 0);
512 test_conversion(&testdata_24bppRGB, &testdata_24bppRGB, "24bppRGB -> 24bppRGB", 0);
513 test_conversion(&testdata_24bppRGB, &testdata_24bppBGR, "24bppRGB -> 24bppBGR", 0);
515 test_conversion(&testdata_32bppBGR, &testdata_24bppRGB, "32bppBGR -> 24bppRGB", 0);
516 test_conversion(&testdata_24bppRGB, &testdata_32bppBGR, "24bppRGB -> 32bppBGR", 0);
518 test_invalid_conversion();
519 test_default_converter();
521 test_encoder(&testdata_32bppBGR, &CLSID_WICBmpEncoder,
522 &testdata_32bppBGR, &CLSID_WICBmpDecoder, "BMP encoder 32bppBGR");
524 test_encoder(&testdata_24bppBGR, &CLSID_WICPngEncoder,
525 &testdata_24bppBGR, &CLSID_WICPngDecoder, "PNG encoder 24bppBGR");
527 test_encoder(&testdata_24bppBGR, &CLSID_WICTiffEncoder,
528 &testdata_24bppBGR, &CLSID_WICTiffDecoder, "TIFF encoder 24bppBGR");
530 test_multi_encoder(multiple_frames, &CLSID_WICTiffEncoder,
531 multiple_frames, &CLSID_WICTiffDecoder, "TIFF encoder multi-frame");