2 * Unit test suite for images
4 * Copyright (C) 2007 Google (Evan Stade)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/test.h"
30 #define expect(expected, got) ok((UINT)(got) == (UINT)(expected), "Expected %.8x, got %.8x\n", (UINT)(expected), (UINT)(got))
31 #define expectf(expected, got) ok(fabs(expected - got) < 0.0001, "Expected %.2f, got %.2f\n", expected, got)
33 static void expect_guid(REFGUID expected, REFGUID got, int line, BOOL todo)
39 StringFromGUID2(got, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
40 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
41 StringFromGUID2(expected, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
42 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer2, sizeof(buffer2), NULL, NULL);
44 todo_wine ok_(__FILE__, line)(IsEqualGUID(expected, got), "Expected %s, got %s\n", buffer2, buffer);
46 ok_(__FILE__, line)(IsEqualGUID(expected, got), "Expected %s, got %s\n", buffer2, buffer);
49 static void expect_rawformat(REFGUID expected, GpImage *img, int line, BOOL todo)
54 stat = GdipGetImageRawFormat(img, &raw);
55 ok_(__FILE__, line)(stat == Ok, "GdipGetImageRawFormat failed with %d\n", stat);
56 if(stat != Ok) return;
57 expect_guid(expected, &raw, line, todo);
60 static void test_bufferrawformat(void* buff, int size, REFGUID expected, int line, BOOL todo)
69 hglob = GlobalAlloc (0, size);
70 data = GlobalLock (hglob);
71 memcpy(data, buff, size);
72 GlobalUnlock(hglob); data = NULL;
74 hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
75 ok_(__FILE__, line)(hres == S_OK, "Failed to create a stream\n");
76 if(hres != S_OK) return;
78 stat = GdipCreateBitmapFromStream(stream, &bmp);
79 ok_(__FILE__, line)(stat == Ok, "Failed to create a Bitmap\n");
81 IStream_Release(stream);
85 expect_rawformat(expected, (GpImage*)bmp, line, todo);
87 GdipDisposeImage((GpImage*)bmp);
88 IStream_Release(stream);
91 static void test_Scan0(void)
98 stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
100 ok(NULL != bm, "Expected bitmap to be initialized\n");
102 GdipDisposeImage((GpImage*)bm);
104 bm = (GpBitmap*)0xdeadbeef;
105 stat = GdipCreateBitmapFromScan0(10, -10, 10, PixelFormat24bppRGB, NULL, &bm);
106 expect(InvalidParameter, stat);
107 ok( !bm, "expected null bitmap\n" );
109 bm = (GpBitmap*)0xdeadbeef;
110 stat = GdipCreateBitmapFromScan0(-10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
111 expect(InvalidParameter, stat);
112 ok( !bm, "expected null bitmap\n" );
114 bm = (GpBitmap*)0xdeadbeef;
115 stat = GdipCreateBitmapFromScan0(10, 0, 10, PixelFormat24bppRGB, NULL, &bm);
116 expect(InvalidParameter, stat);
117 ok( !bm, "expected null bitmap\n" );
120 stat = GdipCreateBitmapFromScan0(10, 10, 12, PixelFormat24bppRGB, buff, &bm);
122 ok(NULL != bm, "Expected bitmap to be initialized\n");
124 GdipDisposeImage((GpImage*)bm);
126 bm = (GpBitmap*) 0xdeadbeef;
127 stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, buff, &bm);
128 expect(InvalidParameter, stat);
129 ok( !bm, "expected null bitmap\n" );
131 bm = (GpBitmap*)0xdeadbeef;
132 stat = GdipCreateBitmapFromScan0(10, 10, 0, PixelFormat24bppRGB, buff, &bm);
133 expect(InvalidParameter, stat);
134 ok( bm == (GpBitmap*)0xdeadbeef, "expected deadbeef bitmap\n" );
137 stat = GdipCreateBitmapFromScan0(10, 10, -8, PixelFormat24bppRGB, buff, &bm);
139 ok(NULL != bm, "Expected bitmap to be initialized\n");
141 GdipDisposeImage((GpImage*)bm);
143 bm = (GpBitmap*)0xdeadbeef;
144 stat = GdipCreateBitmapFromScan0(10, 10, -10, PixelFormat24bppRGB, buff, &bm);
145 expect(InvalidParameter, stat);
146 ok( !bm, "expected null bitmap\n" );
149 static void test_GetImageDimension(void)
153 const REAL WIDTH = 10.0, HEIGHT = 20.0;
156 bm = (GpBitmap*)0xdeadbeef;
157 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
159 ok((GpBitmap*)0xdeadbeef != bm, "Expected bitmap to not be 0xdeadbeef\n");
160 ok(NULL != bm, "Expected bitmap to not be NULL\n");
162 stat = GdipGetImageDimension(NULL,&w,&h);
163 expect(InvalidParameter, stat);
165 stat = GdipGetImageDimension((GpImage*)bm,NULL,&h);
166 expect(InvalidParameter, stat);
168 stat = GdipGetImageDimension((GpImage*)bm,&w,NULL);
169 expect(InvalidParameter, stat);
173 stat = GdipGetImageDimension((GpImage*)bm,&w,&h);
177 GdipDisposeImage((GpImage*)bm);
180 static void test_GdipImageGetFrameDimensionsCount(void)
184 const REAL WIDTH = 10.0, HEIGHT = 20.0;
186 GUID dimension = {0};
190 bm = (GpBitmap*)0xdeadbeef;
191 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
193 ok((GpBitmap*)0xdeadbeef != bm, "Expected bitmap to not be 0xdeadbeef\n");
194 ok(NULL != bm, "Expected bitmap to not be NULL\n");
196 stat = GdipImageGetFrameDimensionsCount(NULL,&w);
197 expect(InvalidParameter, stat);
199 stat = GdipImageGetFrameDimensionsCount((GpImage*)bm,NULL);
200 expect(InvalidParameter, stat);
203 stat = GdipImageGetFrameDimensionsCount((GpImage*)bm,&w);
207 stat = GdipImageGetFrameDimensionsList((GpImage*)bm, &dimension, 1);
209 expect_guid(&FrameDimensionPage, &dimension, __LINE__, TRUE);
212 stat = GdipImageGetFrameCount((GpImage*)bm, &dimension, &count);
213 todo_wine expect(Ok, stat);
214 todo_wine expect(1, count);
216 GdipBitmapSetPixel(bm, 0, 0, 0xffffffff);
218 stat = GdipImageSelectActiveFrame((GpImage*)bm, &dimension, 0);
221 /* SelectActiveFrame has no effect on image data of memory bitmaps */
223 GdipBitmapGetPixel(bm, 0, 0, &color);
224 expect(0xffffffff, color);
226 GdipDisposeImage((GpImage*)bm);
229 static void test_LoadingImages(void)
233 stat = GdipCreateBitmapFromFile(0, 0);
234 expect(InvalidParameter, stat);
236 stat = GdipCreateBitmapFromFile(0, (GpBitmap**)0xdeadbeef);
237 expect(InvalidParameter, stat);
239 stat = GdipLoadImageFromFile(0, 0);
240 expect(InvalidParameter, stat);
242 stat = GdipLoadImageFromFile(0, (GpImage**)0xdeadbeef);
243 expect(InvalidParameter, stat);
245 stat = GdipLoadImageFromFileICM(0, 0);
246 expect(InvalidParameter, stat);
248 stat = GdipLoadImageFromFileICM(0, (GpImage**)0xdeadbeef);
249 expect(InvalidParameter, stat);
252 static void test_SavingImages(void)
258 const REAL WIDTH = 10.0, HEIGHT = 20.0;
260 ImageCodecInfo *codecs;
261 static const CHAR filenameA[] = "a.bmp";
262 static const WCHAR filename[] = { 'a','.','b','m','p',0 };
266 stat = GdipSaveImageToFile(0, 0, 0, 0);
267 expect(InvalidParameter, stat);
270 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
276 stat = GdipSaveImageToFile((GpImage*)bm, 0, 0, 0);
277 expect(InvalidParameter, stat);
279 stat = GdipSaveImageToFile((GpImage*)bm, filename, 0, 0);
280 expect(InvalidParameter, stat);
282 /* encoder tests should succeed -- already tested */
283 stat = GdipGetImageEncodersSize(&n, &s);
284 if (stat != Ok || n == 0) goto cleanup;
286 codecs = GdipAlloc(s);
287 if (!codecs) goto cleanup;
289 stat = GdipGetImageEncoders(n, s, codecs);
290 if (stat != Ok) goto cleanup;
292 stat = GdipSaveImageToFile((GpImage*)bm, filename, &codecs[0].Clsid, 0);
295 GdipDisposeImage((GpImage*)bm);
298 /* re-load and check image stats */
299 stat = GdipLoadImageFromFile(filename, (GpImage**)&bm);
301 if (stat != Ok) goto cleanup;
303 stat = GdipGetImageDimension((GpImage*)bm, &w, &h);
304 if (stat != Ok) goto cleanup;
312 GdipDisposeImage((GpImage*)bm);
313 ok(DeleteFileA(filenameA), "Delete failed.\n");
316 static void test_encoders(void)
321 ImageCodecInfo *codecs;
325 static const CHAR bmp_format[] = "BMP";
327 stat = GdipGetImageEncodersSize(&n, &s);
330 codecs = GdipAlloc(s);
334 stat = GdipGetImageEncoders(n, s, NULL);
335 expect(GenericError, stat);
337 stat = GdipGetImageEncoders(0, s, codecs);
338 expect(GenericError, stat);
340 stat = GdipGetImageEncoders(n, s-1, codecs);
341 expect(GenericError, stat);
343 stat = GdipGetImageEncoders(n, s+1, codecs);
344 expect(GenericError, stat);
346 stat = GdipGetImageEncoders(n, s, codecs);
350 for (i = 0; i < n; i++)
354 WideCharToMultiByte(CP_ACP, 0, codecs[i].FormatDescription, -1,
357 if (CompareStringA(LOCALE_SYSTEM_DEFAULT, 0,
359 bmp_format, -1) == CSTR_EQUAL) {
365 ok(FALSE, "No BMP codec found.\n");
370 static void test_LockBits(void)
376 const INT WIDTH = 10, HEIGHT = 20;
379 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
388 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
392 stat = GdipBitmapUnlockBits(bm, &bd);
396 /* read-only, with NULL rect -> whole bitmap lock */
397 stat = GdipBitmapLockBits(bm, NULL, ImageLockModeRead, PixelFormat24bppRGB, &bd);
399 expect(bd.Width, WIDTH);
400 expect(bd.Height, HEIGHT);
403 stat = GdipBitmapUnlockBits(bm, &bd);
407 /* read-only, consecutive */
408 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
412 stat = GdipBitmapUnlockBits(bm, &bd);
416 stat = GdipDisposeImage((GpImage*)bm);
418 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
422 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
424 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
425 expect(WrongState, stat);
427 stat = GdipBitmapUnlockBits(bm, &bd);
430 stat = GdipDisposeImage((GpImage*)bm);
432 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
435 /* write, no modification */
436 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
440 stat = GdipBitmapUnlockBits(bm, &bd);
444 /* write, consecutive */
445 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
449 stat = GdipBitmapUnlockBits(bm, &bd);
453 stat = GdipDisposeImage((GpImage*)bm);
455 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
459 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
464 ((char*)bd.Scan0)[2] = 0xff;
466 stat = GdipBitmapUnlockBits(bm, &bd);
470 stat = GdipDisposeImage((GpImage*)bm);
474 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
476 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
478 stat = GdipDisposeImage((GpImage*)bm);
482 static void test_GdipCreateBitmapFromHBITMAP(void)
484 GpBitmap* gpbm = NULL;
486 HPALETTE hpal = NULL;
489 LOGPALETTE* LogPal = NULL;
491 const REAL WIDTH1 = 5;
492 const REAL HEIGHT1 = 15;
493 const REAL WIDTH2 = 10;
494 const REAL HEIGHT2 = 20;
499 stat = GdipCreateBitmapFromHBITMAP(NULL, NULL, NULL);
500 expect(InvalidParameter, stat);
502 hbm = CreateBitmap(WIDTH1, HEIGHT1, 1, 1, NULL);
503 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, NULL);
504 expect(InvalidParameter, stat);
506 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
508 expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
509 expectf(WIDTH1, width);
510 expectf(HEIGHT1, height);
512 GdipDisposeImage((GpImage*)gpbm);
515 hbm = CreateBitmap(WIDTH2, HEIGHT2, 1, 1, &buff);
516 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
519 expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)gpbm, __LINE__, FALSE);
521 expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
522 expectf(WIDTH2, width);
523 expectf(HEIGHT2, height);
525 GdipDisposeImage((GpImage*)gpbm);
528 hdc = CreateCompatibleDC(0);
529 ok(hdc != NULL, "CreateCompatibleDC failed\n");
530 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
531 bmi.bmiHeader.biHeight = HEIGHT1;
532 bmi.bmiHeader.biWidth = WIDTH1;
533 bmi.bmiHeader.biBitCount = 24;
534 bmi.bmiHeader.biPlanes = 1;
535 bmi.bmiHeader.biCompression = BI_RGB;
537 hbm = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
538 ok(hbm != NULL, "CreateDIBSection failed\n");
542 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
544 expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
545 expectf(WIDTH1, width);
546 expectf(HEIGHT1, height);
549 /* test whether writing to the bitmap affects the original */
550 stat = GdipBitmapSetPixel(gpbm, 0, 0, 0xffffffff);
555 GdipDisposeImage((GpImage*)gpbm);
558 LogPal = GdipAlloc(sizeof(LOGPALETTE));
559 ok(LogPal != NULL, "unable to allocate LOGPALETTE\n");
560 LogPal->palVersion = 0x300;
561 LogPal->palNumEntries = 1;
562 hpal = CreatePalette(LogPal);
563 ok(hpal != NULL, "CreatePalette failed\n");
566 stat = GdipCreateBitmapFromHBITMAP(hbm, hpal, &gpbm);
572 GdipDisposeImage((GpImage*)gpbm);
578 static void test_GdipGetImageFlags(void)
584 img = (GpImage*)0xdeadbeef;
586 stat = GdipGetImageFlags(NULL, NULL);
587 expect(InvalidParameter, stat);
589 stat = GdipGetImageFlags(NULL, &flags);
590 expect(InvalidParameter, stat);
592 stat = GdipGetImageFlags(img, NULL);
593 expect(InvalidParameter, stat);
596 static void test_GdipCloneImage(void)
602 GpImage *image_src, *image_dest = NULL;
603 const INT WIDTH = 10, HEIGHT = 20;
605 /* Create an image, clone it, delete the original, make sure the copy works */
606 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
608 expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bm, __LINE__, FALSE);
610 image_src = ((GpImage*)bm);
611 stat = GdipCloneImage(image_src, &image_dest);
613 expect_rawformat(&ImageFormatMemoryBMP, image_dest, __LINE__, FALSE);
615 stat = GdipDisposeImage((GpImage*)bm);
617 stat = GdipGetImageBounds(image_dest, &rectF, &unit);
620 /* Treat FP values carefully */
621 expectf((REAL)WIDTH, rectF.Width);
622 expectf((REAL)HEIGHT, rectF.Height);
624 stat = GdipDisposeImage(image_dest);
628 static void test_testcontrol(void)
634 stat = GdipTestControl(TestControlGetBuildNumber, ¶m);
636 ok(param != 0, "Build number expected, got %u\n", param);
639 static void test_fromhicon(void)
641 static const BYTE bmp_bits[1024];
642 HBITMAP hbmMask, hbmColor;
646 GpBitmap *bitmap = NULL;
652 stat = GdipCreateBitmapFromHICON(NULL, NULL);
653 expect(InvalidParameter, stat);
654 stat = GdipCreateBitmapFromHICON(NULL, &bitmap);
655 expect(InvalidParameter, stat);
657 /* color icon 1 bit */
658 hbmMask = CreateBitmap(16, 16, 1, 1, bmp_bits);
659 ok(hbmMask != 0, "CreateBitmap failed\n");
660 hbmColor = CreateBitmap(16, 16, 1, 1, bmp_bits);
661 ok(hbmColor != 0, "CreateBitmap failed\n");
665 info.hbmMask = hbmMask;
666 info.hbmColor = hbmColor;
667 hIcon = CreateIconIndirect(&info);
668 ok(hIcon != 0, "CreateIconIndirect failed\n");
669 DeleteObject(hbmMask);
670 DeleteObject(hbmColor);
672 stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
674 broken(stat == InvalidParameter), /* Win98 */
675 "Expected Ok, got %.8x\n", stat);
677 /* check attributes */
678 stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
681 stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
684 stat = GdipGetImageType((GpImage*)bitmap, &type);
686 expect(ImageTypeBitmap, type);
687 stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
688 expect(PixelFormat32bppARGB, format);
690 expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bitmap, __LINE__, FALSE);
691 GdipDisposeImage((GpImage*)bitmap);
695 /* color icon 8 bpp */
696 hbmMask = CreateBitmap(16, 16, 1, 8, bmp_bits);
697 ok(hbmMask != 0, "CreateBitmap failed\n");
698 hbmColor = CreateBitmap(16, 16, 1, 8, bmp_bits);
699 ok(hbmColor != 0, "CreateBitmap failed\n");
703 info.hbmMask = hbmMask;
704 info.hbmColor = hbmColor;
705 hIcon = CreateIconIndirect(&info);
706 ok(hIcon != 0, "CreateIconIndirect failed\n");
707 DeleteObject(hbmMask);
708 DeleteObject(hbmColor);
710 stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
713 /* check attributes */
714 stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
717 stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
720 stat = GdipGetImageType((GpImage*)bitmap, &type);
722 expect(ImageTypeBitmap, type);
723 stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
724 expect(PixelFormat32bppARGB, format);
726 expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bitmap, __LINE__, FALSE);
727 GdipDisposeImage((GpImage*)bitmap);
733 static const unsigned char pngimage[285] = {
734 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
735 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
736 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
737 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
738 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
739 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
740 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
743 static const unsigned char gifimage[35] = {
744 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
745 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
749 static const unsigned char bmpimage[66] = {
750 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
751 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
752 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
753 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
757 static const unsigned char jpgimage[285] = {
758 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2c,
759 0x01,0x2c,0x00,0x00,0xff,0xdb,0x00,0x43,0x00,0x05,0x03,0x04,0x04,0x04,0x03,0x05,
760 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x07,0x0c,0x08,0x07,0x07,0x07,0x07,0x0f,0x0b,
761 0x0b,0x09,0x0c,0x11,0x0f,0x12,0x12,0x11,0x0f,0x11,0x11,0x13,0x16,0x1c,0x17,0x13,
762 0x14,0x1a,0x15,0x11,0x11,0x18,0x21,0x18,0x1a,0x1d,0x1d,0x1f,0x1f,0x1f,0x13,0x17,
763 0x22,0x24,0x22,0x1e,0x24,0x1c,0x1e,0x1f,0x1e,0xff,0xdb,0x00,0x43,0x01,0x05,0x05,
764 0x05,0x07,0x06,0x07,0x0e,0x08,0x08,0x0e,0x1e,0x14,0x11,0x14,0x1e,0x1e,0x1e,0x1e,
765 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
766 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
767 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0xff,0xc0,
768 0x00,0x11,0x08,0x00,0x01,0x00,0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,
769 0x01,0xff,0xc4,0x00,0x15,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
770 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xff,0xc4,0x00,0x14,0x10,0x01,0x00,0x00,
771 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xc4,
772 0x00,0x14,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
773 0x00,0x00,0x00,0x00,0xff,0xc4,0x00,0x14,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
774 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xda,0x00,0x0c,0x03,0x01,
775 0x00,0x02,0x11,0x03,0x11,0x00,0x3f,0x00,0xb2,0xc0,0x07,0xff,0xd9
777 static void test_getrawformat(void)
779 test_bufferrawformat((void*)pngimage, sizeof(pngimage), &ImageFormatPNG, __LINE__, FALSE);
780 test_bufferrawformat((void*)gifimage, sizeof(gifimage), &ImageFormatGIF, __LINE__, FALSE);
781 test_bufferrawformat((void*)bmpimage, sizeof(bmpimage), &ImageFormatBMP, __LINE__, FALSE);
782 test_bufferrawformat((void*)jpgimage, sizeof(jpgimage), &ImageFormatJPEG, __LINE__, FALSE);
785 static void test_createhbitmap(void)
789 HBITMAP hbitmap, oldhbitmap;
796 memset(bits, 0x68, 640);
799 stat = GdipCreateBitmapFromScan0(10, 20, 32, PixelFormat24bppRGB, bits, &bitmap);
802 /* test NULL values */
803 stat = GdipCreateHBITMAPFromBitmap(NULL, &hbitmap, 0);
804 expect(InvalidParameter, stat);
806 stat = GdipCreateHBITMAPFromBitmap(bitmap, NULL, 0);
807 expect(InvalidParameter, stat);
810 stat = GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
815 ret = GetObjectA(hbitmap, sizeof(BITMAP), &bm);
816 expect(sizeof(BITMAP), ret);
818 expect(0, bm.bmType);
819 expect(10, bm.bmWidth);
820 expect(20, bm.bmHeight);
821 expect(40, bm.bmWidthBytes);
822 expect(1, bm.bmPlanes);
823 expect(32, bm.bmBitsPixel);
824 ok(bm.bmBits != NULL, "got DDB, expected DIB\n");
826 hdc = CreateCompatibleDC(NULL);
828 oldhbitmap = SelectObject(hdc, hbitmap);
829 pixel = GetPixel(hdc, 5, 5);
830 SelectObject(hdc, oldhbitmap);
834 expect(0x686868, pixel);
836 DeleteObject(hbitmap);
839 stat = GdipDisposeImage((GpImage*)bitmap);
843 static void test_getsetpixel(void)
848 BYTE bits[16] = {0x00,0x00,0x00,0x00, 0x00,0xff,0xff,0x00,
849 0xff,0x00,0x00,0x00, 0xff,0xff,0xff,0x00};
851 stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat32bppRGB, bits, &bitmap);
854 /* null parameters */
855 stat = GdipBitmapGetPixel(NULL, 1, 1, &color);
856 expect(InvalidParameter, stat);
858 stat = GdipBitmapGetPixel(bitmap, 1, 1, NULL);
859 expect(InvalidParameter, stat);
861 stat = GdipBitmapSetPixel(NULL, 1, 1, 0);
862 expect(InvalidParameter, stat);
865 stat = GdipBitmapGetPixel(bitmap, -1, 1, &color);
866 expect(InvalidParameter, stat);
868 stat = GdipBitmapSetPixel(bitmap, -1, 1, 0);
869 expect(InvalidParameter, stat);
871 stat = GdipBitmapGetPixel(bitmap, 1, -1, &color);
872 ok(stat == InvalidParameter ||
873 broken(stat == Ok), /* Older gdiplus */
874 "Expected InvalidParameter, got %.8x\n", stat);
876 stat = GdipBitmapSetPixel(bitmap, 1, -1, 0);
877 ok(stat == InvalidParameter ||
878 broken(stat == Ok), /* Older gdiplus */
879 "Expected InvalidParameter, got %.8x\n", stat);
881 stat = GdipBitmapGetPixel(bitmap, 2, 1, &color);
882 expect(InvalidParameter, stat);
884 stat = GdipBitmapSetPixel(bitmap, 2, 1, 0);
885 expect(InvalidParameter, stat);
887 stat = GdipBitmapGetPixel(bitmap, 1, 2, &color);
888 expect(InvalidParameter, stat);
890 stat = GdipBitmapSetPixel(bitmap, 1, 2, 0);
891 expect(InvalidParameter, stat);
894 stat = GdipBitmapGetPixel(bitmap, 1, 1, &color);
896 expect(0xffffffff, color);
898 stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
900 expect(0xff0000ff, color);
902 stat = GdipBitmapSetPixel(bitmap, 1, 1, 0xff676869);
905 stat = GdipBitmapSetPixel(bitmap, 0, 0, 0xff474849);
908 stat = GdipBitmapGetPixel(bitmap, 1, 1, &color);
910 expect(0xff676869, color);
912 stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
914 expect(0xff474849, color);
916 stat = GdipDisposeImage((GpImage*)bitmap);
920 static void check_halftone_palette(ColorPalette *palette)
922 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
925 for (i=0; i<palette->Count; i++)
927 ARGB expected=0xff000000;
930 if (i&1) expected |= 0x800000;
931 if (i&2) expected |= 0x8000;
932 if (i&4) expected |= 0x80;
936 expected = 0xffc0c0c0;
940 if (i&1) expected |= 0xff0000;
941 if (i&2) expected |= 0xff00;
942 if (i&4) expected |= 0xff;
946 expected = 0x00000000;
950 expected |= halftone_values[(i-40)%6];
951 expected |= halftone_values[((i-40)/6)%6] << 8;
952 expected |= halftone_values[((i-40)/36)%6] << 16;
954 ok(expected == palette->Entries[i], "Expected %.8x, got %.8x, i=%u/%u\n",
955 expected, palette->Entries[i], i, palette->Count);
959 static void test_palette(void)
965 ColorPalette *palette=(ColorPalette*)buffer;
967 /* test initial palette from non-indexed bitmap */
968 stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat32bppRGB, NULL, &bitmap);
971 stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
973 expect(sizeof(UINT)*2+sizeof(ARGB), size);
975 stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
977 expect(0, palette->Count);
979 /* test setting palette on not-indexed bitmap */
982 stat = GdipSetImagePalette((GpImage*)bitmap, palette);
985 stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
987 expect(sizeof(UINT)*2+sizeof(ARGB)*3, size);
989 stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
991 expect(3, palette->Count);
993 GdipDisposeImage((GpImage*)bitmap);
995 /* test initial palette on 1-bit bitmap */
996 stat = GdipCreateBitmapFromScan0(2, 2, 4, PixelFormat1bppIndexed, NULL, &bitmap);
999 stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1001 expect(sizeof(UINT)*2+sizeof(ARGB)*2, size);
1003 stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1005 expect(PaletteFlagsGrayScale, palette->Flags);
1006 expect(2, palette->Count);
1008 expect(0xff000000, palette->Entries[0]);
1009 expect(0xffffffff, palette->Entries[1]);
1011 GdipDisposeImage((GpImage*)bitmap);
1013 /* test initial palette on 4-bit bitmap */
1014 stat = GdipCreateBitmapFromScan0(2, 2, 4, PixelFormat4bppIndexed, NULL, &bitmap);
1017 stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1019 expect(sizeof(UINT)*2+sizeof(ARGB)*16, size);
1021 stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1023 expect(0, palette->Flags);
1024 expect(16, palette->Count);
1026 check_halftone_palette(palette);
1028 GdipDisposeImage((GpImage*)bitmap);
1030 /* test initial palette on 8-bit bitmap */
1031 stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat8bppIndexed, NULL, &bitmap);
1034 stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1036 expect(sizeof(UINT)*2+sizeof(ARGB)*256, size);
1038 stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1040 expect(PaletteFlagsHalftone, palette->Flags);
1041 expect(256, palette->Count);
1043 check_halftone_palette(palette);
1045 /* test setting/getting a different palette */
1046 palette->Entries[1] = 0xffcccccc;
1048 stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1051 palette->Entries[1] = 0;
1053 stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1055 expect(sizeof(UINT)*2+sizeof(ARGB)*256, size);
1057 stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1059 expect(PaletteFlagsHalftone, palette->Flags);
1060 expect(256, palette->Count);
1061 expect(0xffcccccc, palette->Entries[1]);
1063 /* test count < 256 */
1064 palette->Flags = 12345;
1067 stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1070 palette->Entries[1] = 0;
1071 palette->Entries[3] = 0xdeadbeef;
1073 stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1075 expect(sizeof(UINT)*2+sizeof(ARGB)*3, size);
1077 stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1079 expect(12345, palette->Flags);
1080 expect(3, palette->Count);
1081 expect(0xffcccccc, palette->Entries[1]);
1082 expect(0xdeadbeef, palette->Entries[3]);
1084 /* test count > 256 */
1085 palette->Count = 257;
1087 stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1088 ok(stat == InvalidParameter ||
1089 broken(stat == Ok), /* Old gdiplus behavior */
1090 "Expected %.8x, got %.8x\n", InvalidParameter, stat);
1092 GdipDisposeImage((GpImage*)bitmap);
1095 static void test_colormatrix(void)
1098 ColorMatrix colormatrix, graymatrix;
1099 GpImageAttributes *imageattr;
1100 const ColorMatrix identity = {{
1101 {1.0,0.0,0.0,0.0,0.0},
1102 {0.0,1.0,0.0,0.0,0.0},
1103 {0.0,0.0,1.0,0.0,0.0},
1104 {0.0,0.0,0.0,1.0,0.0},
1105 {0.0,0.0,0.0,0.0,1.0}}};
1106 const ColorMatrix double_red = {{
1107 {2.0,0.0,0.0,0.0,0.0},
1108 {0.0,1.0,0.0,0.0,0.0},
1109 {0.0,0.0,1.0,0.0,0.0},
1110 {0.0,0.0,0.0,1.0,0.0},
1111 {0.0,0.0,0.0,0.0,1.0}}};
1112 GpBitmap *bitmap1, *bitmap2;
1113 GpGraphics *graphics;
1116 colormatrix = identity;
1117 graymatrix = identity;
1119 stat = GdipSetImageAttributesColorMatrix(NULL, ColorAdjustTypeDefault,
1120 TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1121 expect(InvalidParameter, stat);
1123 stat = GdipCreateImageAttributes(&imageattr);
1126 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1127 TRUE, &colormatrix, NULL, ColorMatrixFlagsDefault);
1130 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1131 TRUE, NULL, NULL, ColorMatrixFlagsDefault);
1132 expect(InvalidParameter, stat);
1134 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1135 TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1138 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1139 TRUE, &colormatrix, NULL, ColorMatrixFlagsSkipGrays);
1142 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1143 TRUE, &colormatrix, NULL, ColorMatrixFlagsAltGray);
1144 expect(InvalidParameter, stat);
1146 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1147 TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsAltGray);
1150 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1151 TRUE, &colormatrix, &graymatrix, 3);
1152 expect(InvalidParameter, stat);
1154 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeCount,
1155 TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1156 expect(InvalidParameter, stat);
1158 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeAny,
1159 TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1160 expect(InvalidParameter, stat);
1162 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1163 FALSE, NULL, NULL, ColorMatrixFlagsDefault);
1166 /* Drawing a bitmap transforms the colors */
1167 colormatrix = double_red;
1168 stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1169 TRUE, &colormatrix, NULL, ColorMatrixFlagsDefault);
1172 stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap1);
1175 stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap2);
1178 stat = GdipBitmapSetPixel(bitmap1, 0, 0, 0xff40ffff);
1181 stat = GdipGetImageGraphicsContext((GpImage*)bitmap2, &graphics);
1184 stat = GdipDrawImageRectRectI(graphics, (GpImage*)bitmap1, 0,0,1,1, 0,0,1,1,
1185 UnitPixel, imageattr, NULL, NULL);
1188 stat = GdipBitmapGetPixel(bitmap2, 0, 0, &color);
1190 todo_wine expect(0xff80ffff, color);
1192 GdipDeleteGraphics(graphics);
1193 GdipDisposeImage((GpImage*)bitmap1);
1194 GdipDisposeImage((GpImage*)bitmap2);
1195 GdipDisposeImageAttributes(imageattr);
1198 /* 1x1 pixel gif, 2 frames; first frame is white, second is black */
1199 static const unsigned char gifanimation[72] = {
1200 0x47,0x49,0x46,0x38,0x39,0x61,0x01,0x00,0x01,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,
1201 0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x21,0xf9,0x04,0x00,0x0a,0x00,0xff,
1202 0x00,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x4c,0x01,0x00,
1203 0x21,0xf9,0x04,0x01,0x0a,0x00,0x01,0x00,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
1204 0x00,0x00,0x02,0x02,0x44,0x01,0x00,0x3b
1207 static void test_multiframegif(void)
1219 /* Test frame functions with an animated GIF */
1220 hglob = GlobalAlloc (0, sizeof(gifanimation));
1221 data = GlobalLock (hglob);
1222 memcpy(data, gifanimation, sizeof(gifanimation));
1223 GlobalUnlock(hglob);
1225 hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
1226 ok(hres == S_OK, "Failed to create a stream\n");
1227 if(hres != S_OK) return;
1229 stat = GdipCreateBitmapFromStream(stream, &bmp);
1230 ok(stat == Ok, "Failed to create a Bitmap\n");
1232 IStream_Release(stream);
1236 /* Bitmap starts at frame 0 */
1238 stat = GdipBitmapGetPixel(bmp, 0, 0, &color);
1240 expect(0xffffffff, color);
1242 /* Check that we get correct metadata */
1243 stat = GdipImageGetFrameDimensionsCount((GpImage*)bmp,&count);
1247 stat = GdipImageGetFrameDimensionsList((GpImage*)bmp, &dimension, 1);
1249 expect_guid(&FrameDimensionTime, &dimension, __LINE__, TRUE);
1252 stat = GdipImageGetFrameCount((GpImage*)bmp, &dimension, &count);
1253 todo_wine expect(Ok, stat);
1254 todo_wine expect(2, count);
1256 /* SelectActiveFrame overwrites our current data */
1257 stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 1);
1261 GdipBitmapGetPixel(bmp, 0, 0, &color);
1263 todo_wine expect(0xff000000, color);
1265 stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 0);
1269 GdipBitmapGetPixel(bmp, 0, 0, &color);
1271 expect(0xffffffff, color);
1273 /* Write over the image data */
1274 stat = GdipBitmapSetPixel(bmp, 0, 0, 0xff000000);
1277 /* Switching to the same frame does not overwrite our changes */
1278 stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 0);
1281 stat = GdipBitmapGetPixel(bmp, 0, 0, &color);
1283 expect(0xff000000, color);
1285 /* But switching to another frame and back does */
1286 stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 1);
1289 stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 0);
1292 stat = GdipBitmapGetPixel(bmp, 0, 0, &color);
1294 todo_wine expect(0xffffffff, color);
1296 GdipDisposeImage((GpImage*)bmp);
1297 IStream_Release(stream);
1299 /* Test with a non-animated gif */
1300 hglob = GlobalAlloc (0, sizeof(gifimage));
1301 data = GlobalLock (hglob);
1302 memcpy(data, gifimage, sizeof(gifimage));
1303 GlobalUnlock(hglob);
1305 hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
1306 ok(hres == S_OK, "Failed to create a stream\n");
1307 if(hres != S_OK) return;
1309 stat = GdipCreateBitmapFromStream(stream, &bmp);
1310 ok(stat == Ok, "Failed to create a Bitmap\n");
1312 IStream_Release(stream);
1316 /* Check metadata */
1317 stat = GdipImageGetFrameDimensionsCount((GpImage*)bmp,&count);
1321 stat = GdipImageGetFrameDimensionsList((GpImage*)bmp, &dimension, 1);
1323 expect_guid(&FrameDimensionTime, &dimension, __LINE__, TRUE);
1326 stat = GdipImageGetFrameCount((GpImage*)bmp, &dimension, &count);
1327 todo_wine expect(Ok, stat);
1328 todo_wine expect(1, count);
1330 GdipDisposeImage((GpImage*)bmp);
1331 IStream_Release(stream);
1336 struct GdiplusStartupInput gdiplusStartupInput;
1337 ULONG_PTR gdiplusToken;
1339 gdiplusStartupInput.GdiplusVersion = 1;
1340 gdiplusStartupInput.DebugEventCallback = NULL;
1341 gdiplusStartupInput.SuppressBackgroundThread = 0;
1342 gdiplusStartupInput.SuppressExternalCodecs = 0;
1344 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
1347 test_GetImageDimension();
1348 test_GdipImageGetFrameDimensionsCount();
1349 test_LoadingImages();
1350 test_SavingImages();
1353 test_GdipCreateBitmapFromHBITMAP();
1354 test_GdipGetImageFlags();
1355 test_GdipCloneImage();
1358 test_getrawformat();
1359 test_createhbitmap();
1363 test_multiframegif();
1365 GdiplusShutdown(gdiplusToken);