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
);
48 bm
= (GpBitmap
*)0xdeadbeef;
49 stat
= GdipCreateBitmapFromScan0(-10, 10, 10, PixelFormat24bppRGB
, NULL
, &bm
);
50 expect(InvalidParameter
, stat
);
54 bm
= (GpBitmap
*)0xdeadbeef;
55 stat
= GdipCreateBitmapFromScan0(10, 0, 10, PixelFormat24bppRGB
, NULL
, &bm
);
56 expect(InvalidParameter
, stat
);
61 stat
= GdipCreateBitmapFromScan0(10, 10, 12, PixelFormat24bppRGB
, buff
, &bm
);
63 ok(NULL
!= bm
, "Expected bitmap to be initialized\n");
65 GdipDisposeImage((GpImage
*)bm
);
67 bm
= (GpBitmap
*) 0xdeadbeef;
68 stat
= GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB
, buff
, &bm
);
69 expect(InvalidParameter
, stat
);
72 bm
= (GpBitmap
*)0xdeadbeef;
73 stat
= GdipCreateBitmapFromScan0(10, 10, 0, PixelFormat24bppRGB
, buff
, &bm
);
74 expect(InvalidParameter
, stat
);
75 expect(0xdeadbeef, bm
);
78 stat
= GdipCreateBitmapFromScan0(10, 10, -8, PixelFormat24bppRGB
, buff
, &bm
);
81 ok(NULL
!= bm
, "Expected bitmap to be initialized\n");
84 GdipDisposeImage((GpImage
*)bm
);
86 bm
= (GpBitmap
*)0xdeadbeef;
87 stat
= GdipCreateBitmapFromScan0(10, 10, -10, PixelFormat24bppRGB
, buff
, &bm
);
88 expect(InvalidParameter
, stat
);
92 static void test_GetImageDimension(void)
96 const REAL WIDTH
= 10.0, HEIGHT
= 20.0;
99 bm
= (GpBitmap
*)0xdeadbeef;
100 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
,NULL
, &bm
);
102 ok((GpBitmap
*)0xdeadbeef != bm
, "Expected bitmap to not be 0xdeadbeef\n");
103 ok(NULL
!= bm
, "Expected bitmap to not be NULL\n");
105 stat
= GdipGetImageDimension(NULL
,&w
,&h
);
106 expect(InvalidParameter
, stat
);
108 stat
= GdipGetImageDimension((GpImage
*)bm
,NULL
,&h
);
109 expect(InvalidParameter
, stat
);
111 stat
= GdipGetImageDimension((GpImage
*)bm
,&w
,NULL
);
112 expect(InvalidParameter
, stat
);
116 stat
= GdipGetImageDimension((GpImage
*)bm
,&w
,&h
);
118 ok(fabs(WIDTH
- w
) < 0.0001, "Width wrong\n");
119 ok(fabs(HEIGHT
- h
) < 0.0001, "Height wrong\n");
120 GdipDisposeImage((GpImage
*)bm
);
123 static void test_GdipImageGetFrameDimensionsCount(void)
127 const REAL WIDTH
= 10.0, HEIGHT
= 20.0;
130 bm
= (GpBitmap
*)0xdeadbeef;
131 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
,NULL
, &bm
);
133 ok((GpBitmap
*)0xdeadbeef != bm
, "Expected bitmap to not be 0xdeadbeef\n");
134 ok(NULL
!= bm
, "Expected bitmap to not be NULL\n");
136 stat
= GdipImageGetFrameDimensionsCount(NULL
,&w
);
137 expect(InvalidParameter
, stat
);
139 stat
= GdipImageGetFrameDimensionsCount((GpImage
*)bm
,NULL
);
140 expect(InvalidParameter
, stat
);
143 stat
= GdipImageGetFrameDimensionsCount((GpImage
*)bm
,&w
);
146 GdipDisposeImage((GpImage
*)bm
);
149 static void test_LoadingImages(void)
153 stat
= GdipCreateBitmapFromFile(0, 0);
154 expect(InvalidParameter
, stat
);
156 stat
= GdipCreateBitmapFromFile(0, (GpBitmap
**)0xdeadbeef);
157 expect(InvalidParameter
, stat
);
159 stat
= GdipLoadImageFromFile(0, 0);
160 expect(InvalidParameter
, stat
);
162 stat
= GdipLoadImageFromFile(0, (GpImage
**)0xdeadbeef);
163 expect(InvalidParameter
, stat
);
165 stat
= GdipLoadImageFromFileICM(0, 0);
166 expect(InvalidParameter
, stat
);
168 stat
= GdipLoadImageFromFileICM(0, (GpImage
**)0xdeadbeef);
169 expect(InvalidParameter
, stat
);
172 static void test_SavingImages(void)
178 const REAL WIDTH
= 10.0, HEIGHT
= 20.0;
180 ImageCodecInfo
*codecs
;
181 static const WCHAR filename
[] = { 'a','.','b','m','p',0 };
185 stat
= GdipSaveImageToFile(0, 0, 0, 0);
186 expect(InvalidParameter
, stat
);
189 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
195 stat
= GdipSaveImageToFile((GpImage
*)bm
, 0, 0, 0);
196 expect(InvalidParameter
, stat
);
198 stat
= GdipSaveImageToFile((GpImage
*)bm
, filename
, 0, 0);
199 expect(InvalidParameter
, stat
);
201 /* encoder tests should succeed -- already tested */
202 stat
= GdipGetImageEncodersSize(&n
, &s
);
203 if (stat
!= Ok
|| n
== 0) goto cleanup
;
205 codecs
= GdipAlloc(s
);
206 if (!codecs
) goto cleanup
;
208 stat
= GdipGetImageEncoders(n
, s
, codecs
);
209 if (stat
!= Ok
) goto cleanup
;
211 stat
= GdipSaveImageToFile((GpImage
*)bm
, filename
, &codecs
[0].Clsid
, 0);
214 GdipDisposeImage((GpImage
*)bm
);
217 /* re-load and check image stats */
218 stat
= GdipLoadImageFromFile(filename
, (GpImage
**)&bm
);
220 if (stat
!= Ok
) goto cleanup
;
222 stat
= GdipGetImageDimension((GpImage
*)bm
, &w
, &h
);
223 if (stat
!= Ok
) goto cleanup
;
225 ok((fabs(w
- WIDTH
) < 0.01) && (fabs(h
- HEIGHT
) < 0.01),
226 "Saved image dimensions are different!\n");
231 GdipDisposeImage((GpImage
*)bm
);
232 ok(DeleteFileW(filename
), "Delete failed.\n");
235 static void test_encoders(void)
240 ImageCodecInfo
*codecs
;
244 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0};
246 stat
= GdipGetImageEncodersSize(&n
, &s
);
249 codecs
= GdipAlloc(s
);
253 stat
= GdipGetImageEncoders(n
, s
, NULL
);
254 expect(GenericError
, stat
);
256 stat
= GdipGetImageEncoders(0, s
, codecs
);
257 expect(GenericError
, stat
);
259 stat
= GdipGetImageEncoders(n
, s
-1, codecs
);
260 expect(GenericError
, stat
);
262 stat
= GdipGetImageEncoders(n
, s
+1, codecs
);
263 expect(GenericError
, stat
);
265 stat
= GdipGetImageEncoders(n
, s
, codecs
);
269 for (i
= 0; i
< n
; i
++)
271 if (CompareStringW(LOCALE_SYSTEM_DEFAULT
, 0,
272 codecs
[i
].FormatDescription
, -1,
273 bmp_format
, -1) == CSTR_EQUAL
) {
279 ok(FALSE
, "No BMP codec found.\n");
284 static void test_LockBits(void)
290 const INT WIDTH
= 10, HEIGHT
= 20;
293 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
302 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
306 stat
= GdipBitmapUnlockBits(bm
, &bd
);
310 /* read-only, with NULL rect -> whole bitmap lock */
311 stat
= GdipBitmapLockBits(bm
, NULL
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
313 expect(bd
.Width
, WIDTH
);
314 expect(bd
.Height
, HEIGHT
);
317 stat
= GdipBitmapUnlockBits(bm
, &bd
);
321 /* read-only, consecutive */
322 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
326 stat
= GdipBitmapUnlockBits(bm
, &bd
);
330 stat
= GdipDisposeImage((GpImage
*)bm
);
332 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
336 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
338 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
339 expect(WrongState
, stat
);
341 stat
= GdipBitmapUnlockBits(bm
, &bd
);
344 stat
= GdipDisposeImage((GpImage
*)bm
);
346 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
349 /* write, no modification */
350 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeWrite
, PixelFormat24bppRGB
, &bd
);
354 stat
= GdipBitmapUnlockBits(bm
, &bd
);
358 /* write, consecutive */
359 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeWrite
, PixelFormat24bppRGB
, &bd
);
363 stat
= GdipBitmapUnlockBits(bm
, &bd
);
367 stat
= GdipDisposeImage((GpImage
*)bm
);
369 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
373 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeWrite
, PixelFormat24bppRGB
, &bd
);
378 ((char*)bd
.Scan0
)[2] = 0xff;
380 stat
= GdipBitmapUnlockBits(bm
, &bd
);
384 stat
= GdipDisposeImage((GpImage
*)bm
);
388 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
390 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
392 stat
= GdipDisposeImage((GpImage
*)bm
);
396 static void test_GdipCreateBitmapFromHBITMAP(void)
398 GpBitmap
* gpbm
= NULL
;
400 HPALETTE hpal
= NULL
;
403 LOGPALETTE
* LogPal
= NULL
;
405 const REAL WIDTH1
= 5;
406 const REAL HEIGHT1
= 15;
407 const REAL WIDTH2
= 10;
408 const REAL HEIGHT2
= 20;
412 stat
= GdipCreateBitmapFromHBITMAP(NULL
, NULL
, NULL
);
413 expect(InvalidParameter
, stat
);
415 hbm
= CreateBitmap(WIDTH1
, HEIGHT1
, 1, 1, NULL
);
416 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, NULL
);
417 expect(InvalidParameter
, stat
);
419 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, &gpbm
);
421 expect(Ok
, GdipGetImageDimension((GpImage
*) gpbm
, &width
, &height
));
422 ok(fabs(WIDTH1
- width
) < .0001, "width wrong\n");
423 ok(fabs(HEIGHT1
- height
) < .0001, "height wrong\n");
425 GdipDisposeImage((GpImage
*)gpbm
);
428 hbm
= CreateBitmap(WIDTH2
, HEIGHT2
, 1, 1, &buff
);
429 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, &gpbm
);
431 expect(Ok
, GdipGetImageDimension((GpImage
*) gpbm
, &width
, &height
));
432 ok(fabs(WIDTH2
- width
) < .0001, "width wrong\n");
433 ok(fabs(HEIGHT2
- height
) < .0001, "height wrong\n");
435 GdipDisposeImage((GpImage
*)gpbm
);
438 hdc
= CreateCompatibleDC(0);
439 ok(hdc
!= NULL
, "CreateCompatibleDC failed\n");
440 bmi
.bmiHeader
.biSize
= sizeof(bmi
.bmiHeader
);
441 bmi
.bmiHeader
.biHeight
= HEIGHT1
;
442 bmi
.bmiHeader
.biWidth
= WIDTH1
;
443 bmi
.bmiHeader
.biBitCount
= 24;
444 bmi
.bmiHeader
.biPlanes
= 1;
445 bmi
.bmiHeader
.biCompression
= BI_RGB
;
447 hbm
= CreateDIBSection(hdc
, &bmi
, DIB_RGB_COLORS
, NULL
, NULL
, 0);
448 ok(hbm
!= NULL
, "CreateDIBSection failed\n");
450 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, &gpbm
);
452 expect(Ok
, GdipGetImageDimension((GpImage
*) gpbm
, &width
, &height
));
453 ok(fabs(WIDTH1
- width
) < .0001, "width wrong\n");
454 ok(fabs(HEIGHT1
- height
) < .0001, "height wrong\n");
456 GdipDisposeImage((GpImage
*)gpbm
);
458 LogPal
= GdipAlloc(sizeof(LOGPALETTE
));
459 ok(LogPal
!= NULL
, "unable to allocate LOGPALETTE\n");
460 LogPal
->palVersion
= 0x300;
461 hpal
= CreatePalette((const LOGPALETTE
*) LogPal
);
462 ok(hpal
!= NULL
, "CreatePalette failed\n");
465 stat
= GdipCreateBitmapFromHBITMAP(hbm
, hpal
, &gpbm
);
471 GdipDisposeImage((GpImage
*)gpbm
);
477 static void test_GdipGetImageFlags(void)
483 img
= (GpImage
*)0xdeadbeef;
485 stat
= GdipGetImageFlags(NULL
, NULL
);
486 expect(InvalidParameter
, stat
);
488 stat
= GdipGetImageFlags(NULL
, &flags
);
489 expect(InvalidParameter
, stat
);
491 stat
= GdipGetImageFlags(img
, NULL
);
492 expect(InvalidParameter
, stat
);
495 static void test_GdipCloneImage(void)
501 GpImage
*image_src
, *image_dest
= NULL
;
502 const INT WIDTH
= 10, HEIGHT
= 20;
504 /* Create an image, clone it, delete the original, make sure the copy works */
505 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
509 image_src
= ((GpImage
*)bm
);
510 stat
= GdipCloneImage(image_src
, &image_dest
);
513 stat
= GdipDisposeImage((GpImage
*)bm
);
517 stat
= GdipGetImageBounds(image_dest
, &rectF
, &unit
);
519 stat
= GdipDisposeImage(image_dest
);
526 struct GdiplusStartupInput gdiplusStartupInput
;
527 ULONG_PTR gdiplusToken
;
529 gdiplusStartupInput
.GdiplusVersion
= 1;
530 gdiplusStartupInput
.DebugEventCallback
= NULL
;
531 gdiplusStartupInput
.SuppressBackgroundThread
= 0;
532 gdiplusStartupInput
.SuppressExternalCodecs
= 0;
534 GdiplusStartup(&gdiplusToken
, &gdiplusStartupInput
, NULL
);
537 test_GetImageDimension();
538 test_GdipImageGetFrameDimensionsCount();
539 test_LoadingImages();
543 test_GdipCreateBitmapFromHBITMAP();
544 test_GdipGetImageFlags();
545 test_GdipCloneImage();
547 GdiplusShutdown(gdiplusToken
);