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
26 #include "wine/test.h"
28 #define expect(expected, got) ok(((UINT)got) == ((UINT)expected), "Expected %.8x, got %.8x\n", (UINT)expected, (UINT)got)
30 static void test_Scan0(void)
37 stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
39 ok(NULL != bm, "Expected bitmap to be initialized\n");
41 GdipDisposeImage((GpImage*)bm);
43 bm = (GpBitmap*)0xdeadbeef;
44 stat = GdipCreateBitmapFromScan0(10, -10, 10, PixelFormat24bppRGB, NULL, &bm);
45 expect(InvalidParameter, stat);
46 ok( !bm, "expected null bitmap\n" );
48 bm = (GpBitmap*)0xdeadbeef;
49 stat = GdipCreateBitmapFromScan0(-10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
50 expect(InvalidParameter, stat);
51 ok( !bm, "expected null bitmap\n" );
53 bm = (GpBitmap*)0xdeadbeef;
54 stat = GdipCreateBitmapFromScan0(10, 0, 10, PixelFormat24bppRGB, NULL, &bm);
55 expect(InvalidParameter, stat);
56 ok( !bm, "expected null bitmap\n" );
59 stat = GdipCreateBitmapFromScan0(10, 10, 12, PixelFormat24bppRGB, buff, &bm);
61 ok(NULL != bm, "Expected bitmap to be initialized\n");
63 GdipDisposeImage((GpImage*)bm);
65 bm = (GpBitmap*) 0xdeadbeef;
66 stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, buff, &bm);
67 expect(InvalidParameter, stat);
68 ok( !bm, "expected null bitmap\n" );
70 bm = (GpBitmap*)0xdeadbeef;
71 stat = GdipCreateBitmapFromScan0(10, 10, 0, PixelFormat24bppRGB, buff, &bm);
72 expect(InvalidParameter, stat);
73 ok( bm == (GpBitmap*)0xdeadbeef, "expected deadbeef bitmap\n" );
76 stat = GdipCreateBitmapFromScan0(10, 10, -8, PixelFormat24bppRGB, buff, &bm);
79 ok(NULL != bm, "Expected bitmap to be initialized\n");
82 GdipDisposeImage((GpImage*)bm);
84 bm = (GpBitmap*)0xdeadbeef;
85 stat = GdipCreateBitmapFromScan0(10, 10, -10, PixelFormat24bppRGB, buff, &bm);
86 expect(InvalidParameter, stat);
87 ok( !bm, "expected null bitmap\n" );
90 static void test_GetImageDimension(void)
94 const REAL WIDTH = 10.0, HEIGHT = 20.0;
97 bm = (GpBitmap*)0xdeadbeef;
98 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
100 ok((GpBitmap*)0xdeadbeef != bm, "Expected bitmap to not be 0xdeadbeef\n");
101 ok(NULL != bm, "Expected bitmap to not be NULL\n");
103 stat = GdipGetImageDimension(NULL,&w,&h);
104 expect(InvalidParameter, stat);
106 stat = GdipGetImageDimension((GpImage*)bm,NULL,&h);
107 expect(InvalidParameter, stat);
109 stat = GdipGetImageDimension((GpImage*)bm,&w,NULL);
110 expect(InvalidParameter, stat);
114 stat = GdipGetImageDimension((GpImage*)bm,&w,&h);
116 ok(fabs(WIDTH - w) < 0.0001, "Width wrong\n");
117 ok(fabs(HEIGHT - h) < 0.0001, "Height wrong\n");
118 GdipDisposeImage((GpImage*)bm);
121 static void test_GdipImageGetFrameDimensionsCount(void)
125 const REAL WIDTH = 10.0, HEIGHT = 20.0;
128 bm = (GpBitmap*)0xdeadbeef;
129 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
131 ok((GpBitmap*)0xdeadbeef != bm, "Expected bitmap to not be 0xdeadbeef\n");
132 ok(NULL != bm, "Expected bitmap to not be NULL\n");
134 stat = GdipImageGetFrameDimensionsCount(NULL,&w);
135 expect(InvalidParameter, stat);
137 stat = GdipImageGetFrameDimensionsCount((GpImage*)bm,NULL);
138 expect(InvalidParameter, stat);
141 stat = GdipImageGetFrameDimensionsCount((GpImage*)bm,&w);
144 GdipDisposeImage((GpImage*)bm);
147 static void test_LoadingImages(void)
151 stat = GdipCreateBitmapFromFile(0, 0);
152 expect(InvalidParameter, stat);
154 stat = GdipCreateBitmapFromFile(0, (GpBitmap**)0xdeadbeef);
155 expect(InvalidParameter, stat);
157 stat = GdipLoadImageFromFile(0, 0);
158 expect(InvalidParameter, stat);
160 stat = GdipLoadImageFromFile(0, (GpImage**)0xdeadbeef);
161 expect(InvalidParameter, stat);
163 stat = GdipLoadImageFromFileICM(0, 0);
164 expect(InvalidParameter, stat);
166 stat = GdipLoadImageFromFileICM(0, (GpImage**)0xdeadbeef);
167 expect(InvalidParameter, stat);
170 static void test_SavingImages(void)
176 const REAL WIDTH = 10.0, HEIGHT = 20.0;
178 ImageCodecInfo *codecs;
179 static const WCHAR filename[] = { 'a','.','b','m','p',0 };
183 stat = GdipSaveImageToFile(0, 0, 0, 0);
184 expect(InvalidParameter, stat);
187 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
193 stat = GdipSaveImageToFile((GpImage*)bm, 0, 0, 0);
194 expect(InvalidParameter, stat);
196 stat = GdipSaveImageToFile((GpImage*)bm, filename, 0, 0);
197 expect(InvalidParameter, stat);
199 /* encoder tests should succeed -- already tested */
200 stat = GdipGetImageEncodersSize(&n, &s);
201 if (stat != Ok || n == 0) goto cleanup;
203 codecs = GdipAlloc(s);
204 if (!codecs) goto cleanup;
206 stat = GdipGetImageEncoders(n, s, codecs);
207 if (stat != Ok) goto cleanup;
209 stat = GdipSaveImageToFile((GpImage*)bm, filename, &codecs[0].Clsid, 0);
212 GdipDisposeImage((GpImage*)bm);
215 /* re-load and check image stats */
216 stat = GdipLoadImageFromFile(filename, (GpImage**)&bm);
218 if (stat != Ok) goto cleanup;
220 stat = GdipGetImageDimension((GpImage*)bm, &w, &h);
221 if (stat != Ok) goto cleanup;
223 ok((fabs(w - WIDTH) < 0.01) && (fabs(h - HEIGHT) < 0.01),
224 "Saved image dimensions are different!\n");
229 GdipDisposeImage((GpImage*)bm);
230 ok(DeleteFileW(filename), "Delete failed.\n");
233 static void test_encoders(void)
238 ImageCodecInfo *codecs;
242 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0};
244 stat = GdipGetImageEncodersSize(&n, &s);
247 codecs = GdipAlloc(s);
251 stat = GdipGetImageEncoders(n, s, NULL);
252 expect(GenericError, stat);
254 stat = GdipGetImageEncoders(0, s, codecs);
255 expect(GenericError, stat);
257 stat = GdipGetImageEncoders(n, s-1, codecs);
258 expect(GenericError, stat);
260 stat = GdipGetImageEncoders(n, s+1, codecs);
261 expect(GenericError, stat);
263 stat = GdipGetImageEncoders(n, s, codecs);
267 for (i = 0; i < n; i++)
269 if (CompareStringW(LOCALE_SYSTEM_DEFAULT, 0,
270 codecs[i].FormatDescription, -1,
271 bmp_format, -1) == CSTR_EQUAL) {
277 ok(FALSE, "No BMP codec found.\n");
282 static void test_LockBits(void)
288 const INT WIDTH = 10, HEIGHT = 20;
291 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
300 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
304 stat = GdipBitmapUnlockBits(bm, &bd);
308 /* read-only, with NULL rect -> whole bitmap lock */
309 stat = GdipBitmapLockBits(bm, NULL, ImageLockModeRead, PixelFormat24bppRGB, &bd);
311 expect(bd.Width, WIDTH);
312 expect(bd.Height, HEIGHT);
315 stat = GdipBitmapUnlockBits(bm, &bd);
319 /* read-only, consecutive */
320 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
324 stat = GdipBitmapUnlockBits(bm, &bd);
328 stat = GdipDisposeImage((GpImage*)bm);
330 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
334 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
336 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
337 expect(WrongState, stat);
339 stat = GdipBitmapUnlockBits(bm, &bd);
342 stat = GdipDisposeImage((GpImage*)bm);
344 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
347 /* write, no modification */
348 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
352 stat = GdipBitmapUnlockBits(bm, &bd);
356 /* write, consecutive */
357 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
361 stat = GdipBitmapUnlockBits(bm, &bd);
365 stat = GdipDisposeImage((GpImage*)bm);
367 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
371 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
376 ((char*)bd.Scan0)[2] = 0xff;
378 stat = GdipBitmapUnlockBits(bm, &bd);
382 stat = GdipDisposeImage((GpImage*)bm);
386 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
388 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
390 stat = GdipDisposeImage((GpImage*)bm);
394 static void test_GdipCreateBitmapFromHBITMAP(void)
396 GpBitmap* gpbm = NULL;
398 HPALETTE hpal = NULL;
401 LOGPALETTE* LogPal = NULL;
403 const REAL WIDTH1 = 5;
404 const REAL HEIGHT1 = 15;
405 const REAL WIDTH2 = 10;
406 const REAL HEIGHT2 = 20;
410 stat = GdipCreateBitmapFromHBITMAP(NULL, NULL, NULL);
411 expect(InvalidParameter, stat);
413 hbm = CreateBitmap(WIDTH1, HEIGHT1, 1, 1, NULL);
414 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, NULL);
415 expect(InvalidParameter, stat);
417 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
419 expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
420 ok(fabs(WIDTH1 - width) < .0001, "width wrong\n");
421 ok(fabs(HEIGHT1 - height) < .0001, "height wrong\n");
423 GdipDisposeImage((GpImage*)gpbm);
426 hbm = CreateBitmap(WIDTH2, HEIGHT2, 1, 1, &buff);
427 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
429 expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
430 ok(fabs(WIDTH2 - width) < .0001, "width wrong\n");
431 ok(fabs(HEIGHT2 - height) < .0001, "height wrong\n");
433 GdipDisposeImage((GpImage*)gpbm);
436 hdc = CreateCompatibleDC(0);
437 ok(hdc != NULL, "CreateCompatibleDC failed\n");
438 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
439 bmi.bmiHeader.biHeight = HEIGHT1;
440 bmi.bmiHeader.biWidth = WIDTH1;
441 bmi.bmiHeader.biBitCount = 24;
442 bmi.bmiHeader.biPlanes = 1;
443 bmi.bmiHeader.biCompression = BI_RGB;
445 hbm = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, NULL, NULL, 0);
446 ok(hbm != NULL, "CreateDIBSection failed\n");
448 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
450 expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
451 ok(fabs(WIDTH1 - width) < .0001, "width wrong\n");
452 ok(fabs(HEIGHT1 - height) < .0001, "height wrong\n");
454 GdipDisposeImage((GpImage*)gpbm);
456 LogPal = GdipAlloc(sizeof(LOGPALETTE));
457 ok(LogPal != NULL, "unable to allocate LOGPALETTE\n");
458 LogPal->palVersion = 0x300;
459 hpal = CreatePalette(LogPal);
460 ok(hpal != NULL, "CreatePalette failed\n");
463 stat = GdipCreateBitmapFromHBITMAP(hbm, hpal, &gpbm);
469 GdipDisposeImage((GpImage*)gpbm);
475 static void test_GdipGetImageFlags(void)
481 img = (GpImage*)0xdeadbeef;
483 stat = GdipGetImageFlags(NULL, NULL);
484 expect(InvalidParameter, stat);
486 stat = GdipGetImageFlags(NULL, &flags);
487 expect(InvalidParameter, stat);
489 stat = GdipGetImageFlags(img, NULL);
490 expect(InvalidParameter, stat);
493 static void test_GdipCloneImage(void)
499 GpImage *image_src, *image_dest = NULL;
500 const INT WIDTH = 10, HEIGHT = 20;
502 /* Create an image, clone it, delete the original, make sure the copy works */
503 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
506 image_src = ((GpImage*)bm);
507 stat = GdipCloneImage(image_src, &image_dest);
510 stat = GdipDisposeImage((GpImage*)bm);
512 stat = GdipGetImageBounds(image_dest, &rectF, &unit);
515 /* Treat FP values carefully */
516 ok(fabsf(rectF.Width-WIDTH)<1e-5, "Expected: %d, got %.05f\n", WIDTH, rectF.Width);
517 ok(fabsf(rectF.Height-HEIGHT)<1e-5, "Expected: %d, got %.05f\n", HEIGHT, rectF.Height);
519 stat = GdipDisposeImage(image_dest);
523 static void test_testcontrol(void)
529 stat = GdipTestControl(TestControlGetBuildNumber, ¶m);
531 ok(param != 0, "Build number expected, got %u\n", param);
534 static void test_fromhicon(void)
536 static const BYTE bmp_bits[1024];
537 HBITMAP hbmMask, hbmColor;
541 GpBitmap *bitmap = NULL;
551 stat = GdipCreateBitmapFromHICON(NULL, NULL);
552 expect(InvalidParameter, stat);
553 stat = GdipCreateBitmapFromHICON(NULL, &bitmap);
554 expect(InvalidParameter, stat);
556 /* color icon 1 bit */
557 hbmMask = CreateBitmap(16, 16, 1, 1, bmp_bits);
558 ok(hbmMask != 0, "CreateBitmap failed\n");
559 hbmColor = CreateBitmap(16, 16, 1, 1, bmp_bits);
560 ok(hbmColor != 0, "CreateBitmap failed\n");
564 info.hbmMask = hbmMask;
565 info.hbmColor = hbmColor;
566 hIcon = CreateIconIndirect(&info);
567 ok(hIcon != 0, "CreateIconIndirect failed\n");
568 DeleteObject(hbmMask);
569 DeleteObject(hbmColor);
571 stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
574 /* check attributes */
575 stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
578 stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
581 stat = GdipGetImageType((GpImage*)bitmap, &type);
583 expect(ImageTypeBitmap, type);
584 stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
585 expect(PixelFormat32bppARGB, format);
587 stat = GdipGetImageRawFormat((GpImage*)bitmap, &raw);
588 StringFromGUID2(&raw, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
589 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
590 StringFromGUID2(&ImageFormatMemoryBMP, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
591 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer2, sizeof(buffer2), NULL, NULL);
592 todo_wine ok(IsEqualGUID(&raw, &ImageFormatMemoryBMP), "Expected format %s, got %s\n", buffer2, buffer);
593 GdipDisposeImage((GpImage*)bitmap);
597 /* color icon 8 bpp */
598 hbmMask = CreateBitmap(16, 16, 1, 8, bmp_bits);
599 ok(hbmMask != 0, "CreateBitmap failed\n");
600 hbmColor = CreateBitmap(16, 16, 1, 8, bmp_bits);
601 ok(hbmColor != 0, "CreateBitmap failed\n");
605 info.hbmMask = hbmMask;
606 info.hbmColor = hbmColor;
607 hIcon = CreateIconIndirect(&info);
608 ok(hIcon != 0, "CreateIconIndirect failed\n");
609 DeleteObject(hbmMask);
610 DeleteObject(hbmColor);
612 stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
615 /* check attributes */
616 stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
619 stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
622 stat = GdipGetImageType((GpImage*)bitmap, &type);
624 expect(ImageTypeBitmap, type);
625 stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
626 expect(PixelFormat32bppARGB, format);
628 stat = GdipGetImageRawFormat((GpImage*)bitmap, &raw);
629 StringFromGUID2(&raw, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
630 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
631 StringFromGUID2(&ImageFormatMemoryBMP, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
632 WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer2, sizeof(buffer2), NULL, NULL);
633 todo_wine ok(IsEqualGUID(&raw, &ImageFormatMemoryBMP), "Expected format %s, got %s\n", buffer2, buffer);
634 GdipDisposeImage((GpImage*)bitmap);
641 struct GdiplusStartupInput gdiplusStartupInput;
642 ULONG_PTR gdiplusToken;
644 gdiplusStartupInput.GdiplusVersion = 1;
645 gdiplusStartupInput.DebugEventCallback = NULL;
646 gdiplusStartupInput.SuppressBackgroundThread = 0;
647 gdiplusStartupInput.SuppressExternalCodecs = 0;
649 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
652 test_GetImageDimension();
653 test_GdipImageGetFrameDimensionsCount();
654 test_LoadingImages();
658 test_GdipCreateBitmapFromHBITMAP();
659 test_GdipGetImageFlags();
660 test_GdipCloneImage();
664 GdiplusShutdown(gdiplusToken);