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_LoadingImages(void)
113 stat = GdipCreateBitmapFromFile(0, 0);
114 expect(InvalidParameter, stat);
116 stat = GdipCreateBitmapFromFile(0, (GpBitmap**)0xdeadbeef);
117 expect(InvalidParameter, stat);
119 stat = GdipLoadImageFromFile(0, 0);
120 expect(InvalidParameter, stat);
122 stat = GdipLoadImageFromFile(0, (GpImage**)0xdeadbeef);
123 expect(InvalidParameter, stat);
126 static void test_SavingImages(void)
132 const REAL WIDTH = 10.0, HEIGHT = 20.0;
134 ImageCodecInfo *codecs;
135 static const WCHAR filename[] = { 'a','.','b','m','p',0 };
139 stat = GdipSaveImageToFile(0, 0, 0, 0);
140 expect(InvalidParameter, stat);
143 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
149 stat = GdipSaveImageToFile((GpImage*)bm, 0, 0, 0);
150 expect(InvalidParameter, stat);
152 stat = GdipSaveImageToFile((GpImage*)bm, filename, 0, 0);
153 expect(InvalidParameter, stat);
155 /* encoder tests should succeed -- already tested */
156 stat = GdipGetImageEncodersSize(&n, &s);
157 if (stat != Ok || n == 0) goto cleanup;
159 codecs = GdipAlloc(s);
160 if (!codecs) goto cleanup;
162 stat = GdipGetImageEncoders(n, s, codecs);
163 if (stat != Ok) goto cleanup;
165 stat = GdipSaveImageToFile((GpImage*)bm, filename, &codecs[0].Clsid, 0);
168 GdipDisposeImage((GpImage*)bm);
171 /* re-load and check image stats */
172 stat = GdipLoadImageFromFile(filename, (GpImage**)&bm);
174 if (stat != Ok) goto cleanup;
176 stat = GdipGetImageDimension((GpImage*)bm, &w, &h);
177 if (stat != Ok) goto cleanup;
179 ok((fabs(w - WIDTH) < 0.01) && (fabs(h - HEIGHT) < 0.01),
180 "Saved image dimensions are different!\n");
185 GdipDisposeImage((GpImage*)bm);
186 ok(DeleteFileW(filename), "Delete failed.\n");
189 static void test_encoders(void)
194 ImageCodecInfo *codecs;
198 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0};
200 stat = GdipGetImageEncodersSize(&n, &s);
203 codecs = GdipAlloc(s);
207 stat = GdipGetImageEncoders(n, s, NULL);
208 expect(GenericError, stat);
210 stat = GdipGetImageEncoders(0, s, codecs);
211 expect(GenericError, stat);
213 stat = GdipGetImageEncoders(n, s-1, codecs);
214 expect(GenericError, stat);
216 stat = GdipGetImageEncoders(n, s+1, codecs);
217 expect(GenericError, stat);
219 stat = GdipGetImageEncoders(n, s, codecs);
223 for (i = 0; i < n; i++)
225 if (CompareStringW(LOCALE_SYSTEM_DEFAULT, 0,
226 codecs[i].FormatDescription, -1,
227 bmp_format, -1) == CSTR_EQUAL) {
233 ok(FALSE, "No BMP codec found.\n");
238 static void test_LockBits(void)
244 const REAL WIDTH = 10.0, HEIGHT = 20.0;
247 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
256 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
260 stat = GdipBitmapUnlockBits(bm, &bd);
264 /* read-only, consecutive */
265 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
269 stat = GdipBitmapUnlockBits(bm, &bd);
273 stat = GdipDisposeImage((GpImage*)bm);
275 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
279 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
281 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
282 expect(WrongState, stat);
284 stat = GdipBitmapUnlockBits(bm, &bd);
287 stat = GdipDisposeImage((GpImage*)bm);
289 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
292 /* write, no modification */
293 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
297 stat = GdipBitmapUnlockBits(bm, &bd);
301 /* write, consecutive */
302 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
306 stat = GdipBitmapUnlockBits(bm, &bd);
310 stat = GdipDisposeImage((GpImage*)bm);
312 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
316 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
321 ((char*)bd.Scan0)[2] = 0xff;
323 stat = GdipBitmapUnlockBits(bm, &bd);
327 stat = GdipDisposeImage((GpImage*)bm);
331 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
333 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
335 stat = GdipDisposeImage((GpImage*)bm);
339 static void test_GdipCreateBitmapFromHBITMAP(void)
341 GpBitmap* gpbm = NULL;
343 HPALETTE hpal = NULL;
346 LOGPALETTE* LogPal = NULL;
348 const REAL WIDTH1 = 5;
349 const REAL HEIGHT1 = 15;
350 const REAL WIDTH2 = 10;
351 const REAL HEIGHT2 = 20;
355 stat = GdipCreateBitmapFromHBITMAP(NULL, NULL, NULL);
356 expect(InvalidParameter, stat);
358 hbm = CreateBitmap(WIDTH1, HEIGHT1, 1, 1, NULL);
359 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, NULL);
360 expect(InvalidParameter, stat);
362 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
364 expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
365 ok(fabs(WIDTH1 - width) < .0001, "width wrong\n");
366 ok(fabs(HEIGHT1 - height) < .0001, "height wrong\n");
368 GdipDisposeImage((GpImage*)gpbm);
371 hbm = CreateBitmap(WIDTH2, HEIGHT2, 1, 1, &buff);
372 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
374 expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
375 ok(fabs(WIDTH2 - width) < .0001, "width wrong\n");
376 ok(fabs(HEIGHT2 - height) < .0001, "height wrong\n");
378 GdipDisposeImage((GpImage*)gpbm);
381 hdc = CreateCompatibleDC(0);
382 ok(hdc != NULL, "CreateCompatibleDC failed\n");
383 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
384 bmi.bmiHeader.biHeight = HEIGHT1;
385 bmi.bmiHeader.biWidth = WIDTH1;
386 bmi.bmiHeader.biBitCount = 24;
387 bmi.bmiHeader.biPlanes = 1;
388 bmi.bmiHeader.biCompression = BI_RGB;
390 hbm = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, NULL, NULL, 0);
391 ok(hbm != NULL, "CreateDIBSection failed\n");
393 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
395 expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
396 ok(fabs(WIDTH1 - width) < .0001, "width wrong\n");
397 ok(fabs(HEIGHT1 - height) < .0001, "height wrong\n");
399 GdipDisposeImage((GpImage*)gpbm);
401 LogPal = GdipAlloc(sizeof(LOGPALETTE));
402 ok(LogPal != NULL, "unable to allocate LOGPALETTE\n");
403 LogPal->palVersion = 0x300;
404 hpal = CreatePalette((const LOGPALETTE*) LogPal);
405 ok(hpal != NULL, "CreatePalette failed\n");
408 stat = GdipCreateBitmapFromHBITMAP(hbm, hpal, &gpbm);
414 GdipDisposeImage((GpImage*)gpbm);
422 struct GdiplusStartupInput gdiplusStartupInput;
423 ULONG_PTR gdiplusToken;
425 gdiplusStartupInput.GdiplusVersion = 1;
426 gdiplusStartupInput.DebugEventCallback = NULL;
427 gdiplusStartupInput.SuppressBackgroundThread = 0;
428 gdiplusStartupInput.SuppressExternalCodecs = 0;
430 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
433 test_GetImageDimension();
434 test_LoadingImages();
438 test_GdipCreateBitmapFromHBITMAP();
440 GdiplusShutdown(gdiplusToken);