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
23 #include "wine/test.h"
27 #define expect(expected, got) ok(((UINT)got) == ((UINT)expected), "Expected %.8x, got %.8x\n", (UINT)expected, (UINT)got)
29 static void test_Scan0(void)
36 stat
= GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB
, NULL
, &bm
);
38 ok(NULL
!= bm
, "Expected bitmap to be initialized\n");
40 GdipDisposeImage((GpImage
*)bm
);
42 bm
= (GpBitmap
*)0xdeadbeef;
43 stat
= GdipCreateBitmapFromScan0(10, -10, 10, PixelFormat24bppRGB
, NULL
, &bm
);
44 expect(InvalidParameter
, stat
);
45 ok( !bm
, "expected null bitmap\n" );
47 bm
= (GpBitmap
*)0xdeadbeef;
48 stat
= GdipCreateBitmapFromScan0(-10, 10, 10, PixelFormat24bppRGB
, NULL
, &bm
);
49 expect(InvalidParameter
, stat
);
50 ok( !bm
, "expected null bitmap\n" );
52 bm
= (GpBitmap
*)0xdeadbeef;
53 stat
= GdipCreateBitmapFromScan0(10, 0, 10, PixelFormat24bppRGB
, NULL
, &bm
);
54 expect(InvalidParameter
, stat
);
55 ok( !bm
, "expected null bitmap\n" );
58 stat
= GdipCreateBitmapFromScan0(10, 10, 12, PixelFormat24bppRGB
, buff
, &bm
);
60 ok(NULL
!= bm
, "Expected bitmap to be initialized\n");
62 GdipDisposeImage((GpImage
*)bm
);
64 bm
= (GpBitmap
*) 0xdeadbeef;
65 stat
= GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB
, buff
, &bm
);
66 expect(InvalidParameter
, stat
);
67 ok( !bm
, "expected null bitmap\n" );
69 bm
= (GpBitmap
*)0xdeadbeef;
70 stat
= GdipCreateBitmapFromScan0(10, 10, 0, PixelFormat24bppRGB
, buff
, &bm
);
71 expect(InvalidParameter
, stat
);
72 ok( bm
== (GpBitmap
*)0xdeadbeef, "expected deadbeef bitmap\n" );
75 stat
= GdipCreateBitmapFromScan0(10, 10, -8, PixelFormat24bppRGB
, buff
, &bm
);
78 ok(NULL
!= bm
, "Expected bitmap to be initialized\n");
81 GdipDisposeImage((GpImage
*)bm
);
83 bm
= (GpBitmap
*)0xdeadbeef;
84 stat
= GdipCreateBitmapFromScan0(10, 10, -10, PixelFormat24bppRGB
, buff
, &bm
);
85 expect(InvalidParameter
, stat
);
86 ok( !bm
, "expected null bitmap\n" );
89 static void test_GetImageDimension(void)
93 const REAL WIDTH
= 10.0, HEIGHT
= 20.0;
96 bm
= (GpBitmap
*)0xdeadbeef;
97 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
,NULL
, &bm
);
99 ok((GpBitmap
*)0xdeadbeef != bm
, "Expected bitmap to not be 0xdeadbeef\n");
100 ok(NULL
!= bm
, "Expected bitmap to not be NULL\n");
102 stat
= GdipGetImageDimension(NULL
,&w
,&h
);
103 expect(InvalidParameter
, stat
);
105 stat
= GdipGetImageDimension((GpImage
*)bm
,NULL
,&h
);
106 expect(InvalidParameter
, stat
);
108 stat
= GdipGetImageDimension((GpImage
*)bm
,&w
,NULL
);
109 expect(InvalidParameter
, stat
);
113 stat
= GdipGetImageDimension((GpImage
*)bm
,&w
,&h
);
115 ok(fabs(WIDTH
- w
) < 0.0001, "Width wrong\n");
116 ok(fabs(HEIGHT
- h
) < 0.0001, "Height wrong\n");
117 GdipDisposeImage((GpImage
*)bm
);
120 static void test_GdipImageGetFrameDimensionsCount(void)
124 const REAL WIDTH
= 10.0, HEIGHT
= 20.0;
127 bm
= (GpBitmap
*)0xdeadbeef;
128 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
,NULL
, &bm
);
130 ok((GpBitmap
*)0xdeadbeef != bm
, "Expected bitmap to not be 0xdeadbeef\n");
131 ok(NULL
!= bm
, "Expected bitmap to not be NULL\n");
133 stat
= GdipImageGetFrameDimensionsCount(NULL
,&w
);
134 expect(InvalidParameter
, stat
);
136 stat
= GdipImageGetFrameDimensionsCount((GpImage
*)bm
,NULL
);
137 expect(InvalidParameter
, stat
);
140 stat
= GdipImageGetFrameDimensionsCount((GpImage
*)bm
,&w
);
143 GdipDisposeImage((GpImage
*)bm
);
146 static void test_LoadingImages(void)
150 stat
= GdipCreateBitmapFromFile(0, 0);
151 expect(InvalidParameter
, stat
);
153 stat
= GdipCreateBitmapFromFile(0, (GpBitmap
**)0xdeadbeef);
154 expect(InvalidParameter
, stat
);
156 stat
= GdipLoadImageFromFile(0, 0);
157 expect(InvalidParameter
, stat
);
159 stat
= GdipLoadImageFromFile(0, (GpImage
**)0xdeadbeef);
160 expect(InvalidParameter
, stat
);
162 stat
= GdipLoadImageFromFileICM(0, 0);
163 expect(InvalidParameter
, stat
);
165 stat
= GdipLoadImageFromFileICM(0, (GpImage
**)0xdeadbeef);
166 expect(InvalidParameter
, stat
);
169 static void test_SavingImages(void)
175 const REAL WIDTH
= 10.0, HEIGHT
= 20.0;
177 ImageCodecInfo
*codecs
;
178 static const WCHAR filename
[] = { 'a','.','b','m','p',0 };
182 stat
= GdipSaveImageToFile(0, 0, 0, 0);
183 expect(InvalidParameter
, stat
);
186 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
192 stat
= GdipSaveImageToFile((GpImage
*)bm
, 0, 0, 0);
193 expect(InvalidParameter
, stat
);
195 stat
= GdipSaveImageToFile((GpImage
*)bm
, filename
, 0, 0);
196 expect(InvalidParameter
, stat
);
198 /* encoder tests should succeed -- already tested */
199 stat
= GdipGetImageEncodersSize(&n
, &s
);
200 if (stat
!= Ok
|| n
== 0) goto cleanup
;
202 codecs
= GdipAlloc(s
);
203 if (!codecs
) goto cleanup
;
205 stat
= GdipGetImageEncoders(n
, s
, codecs
);
206 if (stat
!= Ok
) goto cleanup
;
208 stat
= GdipSaveImageToFile((GpImage
*)bm
, filename
, &codecs
[0].Clsid
, 0);
211 GdipDisposeImage((GpImage
*)bm
);
214 /* re-load and check image stats */
215 stat
= GdipLoadImageFromFile(filename
, (GpImage
**)&bm
);
217 if (stat
!= Ok
) goto cleanup
;
219 stat
= GdipGetImageDimension((GpImage
*)bm
, &w
, &h
);
220 if (stat
!= Ok
) goto cleanup
;
222 ok((fabs(w
- WIDTH
) < 0.01) && (fabs(h
- HEIGHT
) < 0.01),
223 "Saved image dimensions are different!\n");
228 GdipDisposeImage((GpImage
*)bm
);
229 ok(DeleteFileW(filename
), "Delete failed.\n");
232 static void test_encoders(void)
237 ImageCodecInfo
*codecs
;
241 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0};
243 stat
= GdipGetImageEncodersSize(&n
, &s
);
246 codecs
= GdipAlloc(s
);
250 stat
= GdipGetImageEncoders(n
, s
, NULL
);
251 expect(GenericError
, stat
);
253 stat
= GdipGetImageEncoders(0, s
, codecs
);
254 expect(GenericError
, stat
);
256 stat
= GdipGetImageEncoders(n
, s
-1, codecs
);
257 expect(GenericError
, stat
);
259 stat
= GdipGetImageEncoders(n
, s
+1, codecs
);
260 expect(GenericError
, stat
);
262 stat
= GdipGetImageEncoders(n
, s
, codecs
);
266 for (i
= 0; i
< n
; i
++)
268 if (CompareStringW(LOCALE_SYSTEM_DEFAULT
, 0,
269 codecs
[i
].FormatDescription
, -1,
270 bmp_format
, -1) == CSTR_EQUAL
) {
276 ok(FALSE
, "No BMP codec found.\n");
281 static void test_LockBits(void)
287 const INT WIDTH
= 10, HEIGHT
= 20;
290 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
299 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
303 stat
= GdipBitmapUnlockBits(bm
, &bd
);
307 /* read-only, with NULL rect -> whole bitmap lock */
308 stat
= GdipBitmapLockBits(bm
, NULL
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
310 expect(bd
.Width
, WIDTH
);
311 expect(bd
.Height
, HEIGHT
);
314 stat
= GdipBitmapUnlockBits(bm
, &bd
);
318 /* read-only, consecutive */
319 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
323 stat
= GdipBitmapUnlockBits(bm
, &bd
);
327 stat
= GdipDisposeImage((GpImage
*)bm
);
329 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
333 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
335 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
336 expect(WrongState
, stat
);
338 stat
= GdipBitmapUnlockBits(bm
, &bd
);
341 stat
= GdipDisposeImage((GpImage
*)bm
);
343 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
346 /* write, no modification */
347 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeWrite
, PixelFormat24bppRGB
, &bd
);
351 stat
= GdipBitmapUnlockBits(bm
, &bd
);
355 /* write, consecutive */
356 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeWrite
, PixelFormat24bppRGB
, &bd
);
360 stat
= GdipBitmapUnlockBits(bm
, &bd
);
364 stat
= GdipDisposeImage((GpImage
*)bm
);
366 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
370 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeWrite
, PixelFormat24bppRGB
, &bd
);
375 ((char*)bd
.Scan0
)[2] = 0xff;
377 stat
= GdipBitmapUnlockBits(bm
, &bd
);
381 stat
= GdipDisposeImage((GpImage
*)bm
);
385 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
387 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
389 stat
= GdipDisposeImage((GpImage
*)bm
);
393 static void test_GdipCreateBitmapFromHBITMAP(void)
395 GpBitmap
* gpbm
= NULL
;
397 HPALETTE hpal
= NULL
;
400 LOGPALETTE
* LogPal
= NULL
;
402 const REAL WIDTH1
= 5;
403 const REAL HEIGHT1
= 15;
404 const REAL WIDTH2
= 10;
405 const REAL HEIGHT2
= 20;
409 stat
= GdipCreateBitmapFromHBITMAP(NULL
, NULL
, NULL
);
410 expect(InvalidParameter
, stat
);
412 hbm
= CreateBitmap(WIDTH1
, HEIGHT1
, 1, 1, NULL
);
413 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, NULL
);
414 expect(InvalidParameter
, stat
);
416 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, &gpbm
);
418 expect(Ok
, GdipGetImageDimension((GpImage
*) gpbm
, &width
, &height
));
419 ok(fabs(WIDTH1
- width
) < .0001, "width wrong\n");
420 ok(fabs(HEIGHT1
- height
) < .0001, "height wrong\n");
422 GdipDisposeImage((GpImage
*)gpbm
);
425 hbm
= CreateBitmap(WIDTH2
, HEIGHT2
, 1, 1, &buff
);
426 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, &gpbm
);
428 expect(Ok
, GdipGetImageDimension((GpImage
*) gpbm
, &width
, &height
));
429 ok(fabs(WIDTH2
- width
) < .0001, "width wrong\n");
430 ok(fabs(HEIGHT2
- height
) < .0001, "height wrong\n");
432 GdipDisposeImage((GpImage
*)gpbm
);
435 hdc
= CreateCompatibleDC(0);
436 ok(hdc
!= NULL
, "CreateCompatibleDC failed\n");
437 bmi
.bmiHeader
.biSize
= sizeof(bmi
.bmiHeader
);
438 bmi
.bmiHeader
.biHeight
= HEIGHT1
;
439 bmi
.bmiHeader
.biWidth
= WIDTH1
;
440 bmi
.bmiHeader
.biBitCount
= 24;
441 bmi
.bmiHeader
.biPlanes
= 1;
442 bmi
.bmiHeader
.biCompression
= BI_RGB
;
444 hbm
= CreateDIBSection(hdc
, &bmi
, DIB_RGB_COLORS
, NULL
, NULL
, 0);
445 ok(hbm
!= NULL
, "CreateDIBSection failed\n");
447 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, &gpbm
);
449 expect(Ok
, GdipGetImageDimension((GpImage
*) gpbm
, &width
, &height
));
450 ok(fabs(WIDTH1
- width
) < .0001, "width wrong\n");
451 ok(fabs(HEIGHT1
- height
) < .0001, "height wrong\n");
453 GdipDisposeImage((GpImage
*)gpbm
);
455 LogPal
= GdipAlloc(sizeof(LOGPALETTE
));
456 ok(LogPal
!= NULL
, "unable to allocate LOGPALETTE\n");
457 LogPal
->palVersion
= 0x300;
458 hpal
= CreatePalette((const LOGPALETTE
*) LogPal
);
459 ok(hpal
!= NULL
, "CreatePalette failed\n");
462 stat
= GdipCreateBitmapFromHBITMAP(hbm
, hpal
, &gpbm
);
468 GdipDisposeImage((GpImage
*)gpbm
);
474 static void test_GdipGetImageFlags(void)
480 img
= (GpImage
*)0xdeadbeef;
482 stat
= GdipGetImageFlags(NULL
, NULL
);
483 expect(InvalidParameter
, stat
);
485 stat
= GdipGetImageFlags(NULL
, &flags
);
486 expect(InvalidParameter
, stat
);
488 stat
= GdipGetImageFlags(img
, NULL
);
489 expect(InvalidParameter
, stat
);
492 static void test_GdipCloneImage(void)
498 GpImage
*image_src
, *image_dest
= NULL
;
499 const INT WIDTH
= 10, HEIGHT
= 20;
501 /* Create an image, clone it, delete the original, make sure the copy works */
502 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
505 image_src
= ((GpImage
*)bm
);
506 stat
= GdipCloneImage(image_src
, &image_dest
);
509 stat
= GdipDisposeImage((GpImage
*)bm
);
511 stat
= GdipGetImageBounds(image_dest
, &rectF
, &unit
);
514 /* Treat FP values carefully */
515 ok(fabsf(rectF
.Width
-WIDTH
)<1e-5, "Expected: %d, got %.05f\n", WIDTH
, rectF
.Width
);
516 ok(fabsf(rectF
.Height
-HEIGHT
)<1e-5, "Expected: %d, got %.05f\n", HEIGHT
, rectF
.Height
);
518 stat
= GdipDisposeImage(image_dest
);
522 static void test_testcontrol(void)
528 stat
= GdipTestControl(TestControlGetBuildNumber
, ¶m
);
530 ok(param
!= 0, "Build number expected, got %u\n", param
);
535 struct GdiplusStartupInput gdiplusStartupInput
;
536 ULONG_PTR gdiplusToken
;
538 gdiplusStartupInput
.GdiplusVersion
= 1;
539 gdiplusStartupInput
.DebugEventCallback
= NULL
;
540 gdiplusStartupInput
.SuppressBackgroundThread
= 0;
541 gdiplusStartupInput
.SuppressExternalCodecs
= 0;
543 GdiplusStartup(&gdiplusToken
, &gdiplusStartupInput
, NULL
);
546 test_GetImageDimension();
547 test_GdipImageGetFrameDimensionsCount();
548 test_LoadingImages();
552 test_GdipCreateBitmapFromHBITMAP();
553 test_GdipGetImageFlags();
554 test_GdipCloneImage();
557 GdiplusShutdown(gdiplusToken
);