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 static void test_GetImageDimension(void)
82 const REAL WIDTH
= 10.0, HEIGHT
= 20.0;
85 bm
= (GpBitmap
*)0xdeadbeef;
86 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
,NULL
, &bm
);
88 ok((GpBitmap
*)0xdeadbeef != bm
, "Expected bitmap to not be 0xdeadbeef\n");
89 ok(NULL
!= bm
, "Expected bitmap to not be NULL\n");
91 stat
= GdipGetImageDimension(NULL
,&w
,&h
);
92 expect(InvalidParameter
, stat
);
94 stat
= GdipGetImageDimension((GpImage
*)bm
,NULL
,&h
);
95 expect(InvalidParameter
, stat
);
97 stat
= GdipGetImageDimension((GpImage
*)bm
,&w
,NULL
);
98 expect(InvalidParameter
, stat
);
102 stat
= GdipGetImageDimension((GpImage
*)bm
,&w
,&h
);
104 ok(fabs(WIDTH
- w
) < 0.0001, "Width wrong\n");
105 ok(fabs(HEIGHT
- h
) < 0.0001, "Height wrong\n");
106 GdipDisposeImage((GpImage
*)bm
);
109 static void test_GdipImageGetFrameDimensionsCount(void)
113 const REAL WIDTH
= 10.0, HEIGHT
= 20.0;
116 bm
= (GpBitmap
*)0xdeadbeef;
117 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
,NULL
, &bm
);
119 ok((GpBitmap
*)0xdeadbeef != bm
, "Expected bitmap to not be 0xdeadbeef\n");
120 ok(NULL
!= bm
, "Expected bitmap to not be NULL\n");
122 stat
= GdipImageGetFrameDimensionsCount(NULL
,&w
);
123 expect(InvalidParameter
, stat
);
125 stat
= GdipImageGetFrameDimensionsCount((GpImage
*)bm
,NULL
);
126 expect(InvalidParameter
, stat
);
129 stat
= GdipImageGetFrameDimensionsCount((GpImage
*)bm
,&w
);
132 GdipDisposeImage((GpImage
*)bm
);
135 static void test_LoadingImages(void)
139 stat
= GdipCreateBitmapFromFile(0, 0);
140 expect(InvalidParameter
, stat
);
142 stat
= GdipCreateBitmapFromFile(0, (GpBitmap
**)0xdeadbeef);
143 expect(InvalidParameter
, stat
);
145 stat
= GdipLoadImageFromFile(0, 0);
146 expect(InvalidParameter
, stat
);
148 stat
= GdipLoadImageFromFile(0, (GpImage
**)0xdeadbeef);
149 expect(InvalidParameter
, stat
);
151 stat
= GdipLoadImageFromFileICM(0, 0);
152 expect(InvalidParameter
, stat
);
154 stat
= GdipLoadImageFromFileICM(0, (GpImage
**)0xdeadbeef);
155 expect(InvalidParameter
, stat
);
158 static void test_SavingImages(void)
164 const REAL WIDTH
= 10.0, HEIGHT
= 20.0;
166 ImageCodecInfo
*codecs
;
167 static const WCHAR filename
[] = { 'a','.','b','m','p',0 };
171 stat
= GdipSaveImageToFile(0, 0, 0, 0);
172 expect(InvalidParameter
, stat
);
175 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
181 stat
= GdipSaveImageToFile((GpImage
*)bm
, 0, 0, 0);
182 expect(InvalidParameter
, stat
);
184 stat
= GdipSaveImageToFile((GpImage
*)bm
, filename
, 0, 0);
185 expect(InvalidParameter
, stat
);
187 /* encoder tests should succeed -- already tested */
188 stat
= GdipGetImageEncodersSize(&n
, &s
);
189 if (stat
!= Ok
|| n
== 0) goto cleanup
;
191 codecs
= GdipAlloc(s
);
192 if (!codecs
) goto cleanup
;
194 stat
= GdipGetImageEncoders(n
, s
, codecs
);
195 if (stat
!= Ok
) goto cleanup
;
197 stat
= GdipSaveImageToFile((GpImage
*)bm
, filename
, &codecs
[0].Clsid
, 0);
200 GdipDisposeImage((GpImage
*)bm
);
203 /* re-load and check image stats */
204 stat
= GdipLoadImageFromFile(filename
, (GpImage
**)&bm
);
206 if (stat
!= Ok
) goto cleanup
;
208 stat
= GdipGetImageDimension((GpImage
*)bm
, &w
, &h
);
209 if (stat
!= Ok
) goto cleanup
;
211 ok((fabs(w
- WIDTH
) < 0.01) && (fabs(h
- HEIGHT
) < 0.01),
212 "Saved image dimensions are different!\n");
217 GdipDisposeImage((GpImage
*)bm
);
218 ok(DeleteFileW(filename
), "Delete failed.\n");
221 static void test_encoders(void)
226 ImageCodecInfo
*codecs
;
230 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0};
232 stat
= GdipGetImageEncodersSize(&n
, &s
);
235 codecs
= GdipAlloc(s
);
239 stat
= GdipGetImageEncoders(n
, s
, NULL
);
240 expect(GenericError
, stat
);
242 stat
= GdipGetImageEncoders(0, s
, codecs
);
243 expect(GenericError
, stat
);
245 stat
= GdipGetImageEncoders(n
, s
-1, codecs
);
246 expect(GenericError
, stat
);
248 stat
= GdipGetImageEncoders(n
, s
+1, codecs
);
249 expect(GenericError
, stat
);
251 stat
= GdipGetImageEncoders(n
, s
, codecs
);
255 for (i
= 0; i
< n
; i
++)
257 if (CompareStringW(LOCALE_SYSTEM_DEFAULT
, 0,
258 codecs
[i
].FormatDescription
, -1,
259 bmp_format
, -1) == CSTR_EQUAL
) {
265 ok(FALSE
, "No BMP codec found.\n");
270 static void test_LockBits(void)
276 const INT WIDTH
= 10, HEIGHT
= 20;
279 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
288 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
292 stat
= GdipBitmapUnlockBits(bm
, &bd
);
296 /* read-only, with NULL rect -> whole bitmap lock */
297 stat
= GdipBitmapLockBits(bm
, NULL
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
299 expect(bd
.Width
, WIDTH
);
300 expect(bd
.Height
, HEIGHT
);
303 stat
= GdipBitmapUnlockBits(bm
, &bd
);
307 /* read-only, consecutive */
308 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
312 stat
= GdipBitmapUnlockBits(bm
, &bd
);
316 stat
= GdipDisposeImage((GpImage
*)bm
);
318 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
322 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
324 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
325 expect(WrongState
, stat
);
327 stat
= GdipBitmapUnlockBits(bm
, &bd
);
330 stat
= GdipDisposeImage((GpImage
*)bm
);
332 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
335 /* write, no modification */
336 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeWrite
, PixelFormat24bppRGB
, &bd
);
340 stat
= GdipBitmapUnlockBits(bm
, &bd
);
344 /* write, consecutive */
345 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeWrite
, PixelFormat24bppRGB
, &bd
);
349 stat
= GdipBitmapUnlockBits(bm
, &bd
);
353 stat
= GdipDisposeImage((GpImage
*)bm
);
355 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
359 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeWrite
, PixelFormat24bppRGB
, &bd
);
364 ((char*)bd
.Scan0
)[2] = 0xff;
366 stat
= GdipBitmapUnlockBits(bm
, &bd
);
370 stat
= GdipDisposeImage((GpImage
*)bm
);
374 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
376 stat
= GdipBitmapLockBits(bm
, &rect
, ImageLockModeRead
, PixelFormat24bppRGB
, &bd
);
378 stat
= GdipDisposeImage((GpImage
*)bm
);
382 static void test_GdipCreateBitmapFromHBITMAP(void)
384 GpBitmap
* gpbm
= NULL
;
386 HPALETTE hpal
= NULL
;
389 LOGPALETTE
* LogPal
= NULL
;
391 const REAL WIDTH1
= 5;
392 const REAL HEIGHT1
= 15;
393 const REAL WIDTH2
= 10;
394 const REAL HEIGHT2
= 20;
398 stat
= GdipCreateBitmapFromHBITMAP(NULL
, NULL
, NULL
);
399 expect(InvalidParameter
, stat
);
401 hbm
= CreateBitmap(WIDTH1
, HEIGHT1
, 1, 1, NULL
);
402 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, NULL
);
403 expect(InvalidParameter
, stat
);
405 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, &gpbm
);
407 expect(Ok
, GdipGetImageDimension((GpImage
*) gpbm
, &width
, &height
));
408 ok(fabs(WIDTH1
- width
) < .0001, "width wrong\n");
409 ok(fabs(HEIGHT1
- height
) < .0001, "height wrong\n");
411 GdipDisposeImage((GpImage
*)gpbm
);
414 hbm
= CreateBitmap(WIDTH2
, HEIGHT2
, 1, 1, &buff
);
415 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, &gpbm
);
417 expect(Ok
, GdipGetImageDimension((GpImage
*) gpbm
, &width
, &height
));
418 ok(fabs(WIDTH2
- width
) < .0001, "width wrong\n");
419 ok(fabs(HEIGHT2
- height
) < .0001, "height wrong\n");
421 GdipDisposeImage((GpImage
*)gpbm
);
424 hdc
= CreateCompatibleDC(0);
425 ok(hdc
!= NULL
, "CreateCompatibleDC failed\n");
426 bmi
.bmiHeader
.biSize
= sizeof(bmi
.bmiHeader
);
427 bmi
.bmiHeader
.biHeight
= HEIGHT1
;
428 bmi
.bmiHeader
.biWidth
= WIDTH1
;
429 bmi
.bmiHeader
.biBitCount
= 24;
430 bmi
.bmiHeader
.biPlanes
= 1;
431 bmi
.bmiHeader
.biCompression
= BI_RGB
;
433 hbm
= CreateDIBSection(hdc
, &bmi
, DIB_RGB_COLORS
, NULL
, NULL
, 0);
434 ok(hbm
!= NULL
, "CreateDIBSection failed\n");
436 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, &gpbm
);
438 expect(Ok
, GdipGetImageDimension((GpImage
*) gpbm
, &width
, &height
));
439 ok(fabs(WIDTH1
- width
) < .0001, "width wrong\n");
440 ok(fabs(HEIGHT1
- height
) < .0001, "height wrong\n");
442 GdipDisposeImage((GpImage
*)gpbm
);
444 LogPal
= GdipAlloc(sizeof(LOGPALETTE
));
445 ok(LogPal
!= NULL
, "unable to allocate LOGPALETTE\n");
446 LogPal
->palVersion
= 0x300;
447 hpal
= CreatePalette((const LOGPALETTE
*) LogPal
);
448 ok(hpal
!= NULL
, "CreatePalette failed\n");
451 stat
= GdipCreateBitmapFromHBITMAP(hbm
, hpal
, &gpbm
);
457 GdipDisposeImage((GpImage
*)gpbm
);
463 static void test_GdipGetImageFlags(void)
469 img
= (GpImage
*)0xdeadbeef;
471 stat
= GdipGetImageFlags(NULL
, NULL
);
472 expect(InvalidParameter
, stat
);
474 stat
= GdipGetImageFlags(NULL
, &flags
);
475 expect(InvalidParameter
, stat
);
477 stat
= GdipGetImageFlags(img
, NULL
);
478 expect(InvalidParameter
, stat
);
481 static void test_GdipCloneImage(void)
487 GpImage
*image_src
, *image_dest
= NULL
;
488 const INT WIDTH
= 10, HEIGHT
= 20;
490 /* Create an image, clone it, delete the original, make sure the copy works */
491 stat
= GdipCreateBitmapFromScan0(WIDTH
, HEIGHT
, 0, PixelFormat24bppRGB
, NULL
, &bm
);
495 image_src
= ((GpImage
*)bm
);
496 stat
= GdipCloneImage(image_src
, &image_dest
);
499 stat
= GdipDisposeImage((GpImage
*)bm
);
503 stat
= GdipGetImageBounds(image_dest
, &rectF
, &unit
);
505 stat
= GdipDisposeImage(image_dest
);
512 struct GdiplusStartupInput gdiplusStartupInput
;
513 ULONG_PTR gdiplusToken
;
515 gdiplusStartupInput
.GdiplusVersion
= 1;
516 gdiplusStartupInput
.DebugEventCallback
= NULL
;
517 gdiplusStartupInput
.SuppressBackgroundThread
= 0;
518 gdiplusStartupInput
.SuppressExternalCodecs
= 0;
520 GdiplusStartup(&gdiplusToken
, &gdiplusStartupInput
, NULL
);
523 test_GetImageDimension();
524 test_GdipImageGetFrameDimensionsCount();
525 test_LoadingImages();
529 test_GdipCreateBitmapFromHBITMAP();
530 test_GdipGetImageFlags();
531 test_GdipCloneImage();
533 GdiplusShutdown(gdiplusToken
);