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"
26 #define expect(expected, got) ok(((UINT)got) == ((UINT)expected), "Expected %.8x, got %.8x\n", (UINT)expected, (UINT)got)
28 static void test_Scan0(void)
35 stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
37 ok(NULL != bm, "Expected bitmap to be initialized\n");
39 GdipDisposeImage((GpImage*)bm);
41 bm = (GpBitmap*)0xdeadbeef;
42 stat = GdipCreateBitmapFromScan0(10, -10, 10, PixelFormat24bppRGB, NULL, &bm);
43 expect(InvalidParameter, stat);
47 bm = (GpBitmap*)0xdeadbeef;
48 stat = GdipCreateBitmapFromScan0(-10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
49 expect(InvalidParameter, stat);
53 bm = (GpBitmap*)0xdeadbeef;
54 stat = GdipCreateBitmapFromScan0(10, 0, 10, PixelFormat24bppRGB, NULL, &bm);
55 expect(InvalidParameter, stat);
60 stat = GdipCreateBitmapFromScan0(10, 10, 12, PixelFormat24bppRGB, buff, &bm);
62 ok(NULL != bm, "Expected bitmap to be initialized\n");
64 GdipDisposeImage((GpImage*)bm);
66 bm = (GpBitmap*) 0xdeadbeef;
67 stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, buff, &bm);
68 expect(InvalidParameter, stat);
71 bm = (GpBitmap*)0xdeadbeef;
72 stat = GdipCreateBitmapFromScan0(10, 10, 0, PixelFormat24bppRGB, buff, &bm);
73 expect(InvalidParameter, stat);
74 expect(0xdeadbeef, bm);
77 static void test_GetImageDimension(void)
81 const REAL WIDTH = 10.0, HEIGHT = 20.0;
84 bm = (GpBitmap*)0xdeadbeef;
85 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
87 ok((GpBitmap*)0xdeadbeef != bm, "Expected bitmap to not be 0xdeadbeef\n");
88 ok(NULL != bm, "Expected bitmap to not be NULL\n");
90 stat = GdipGetImageDimension(NULL,&w,&h);
91 expect(InvalidParameter, stat);
93 stat = GdipGetImageDimension((GpImage*)bm,NULL,&h);
94 expect(InvalidParameter, stat);
96 stat = GdipGetImageDimension((GpImage*)bm,&w,NULL);
97 expect(InvalidParameter, stat);
101 stat = GdipGetImageDimension((GpImage*)bm,&w,&h);
103 ok(fabs(WIDTH - w) < 0.0001, "Width wrong\n");
104 ok(fabs(HEIGHT - h) < 0.0001, "Height wrong\n");
105 GdipDisposeImage((GpImage*)bm);
108 static void test_LoadingImages(void)
112 stat = GdipCreateBitmapFromFile(0, 0);
113 expect(InvalidParameter, stat);
115 stat = GdipCreateBitmapFromFile(0, (GpBitmap**)0xdeadbeef);
116 expect(InvalidParameter, stat);
118 stat = GdipLoadImageFromFile(0, 0);
119 expect(InvalidParameter, stat);
121 stat = GdipLoadImageFromFile(0, (GpImage**)0xdeadbeef);
122 expect(InvalidParameter, stat);
125 static void test_SavingImages(void)
131 const REAL WIDTH = 10.0, HEIGHT = 20.0;
133 ImageCodecInfo *codecs;
134 static const WCHAR filename[] = { 'a','.','b','m','p',0 };
138 stat = GdipSaveImageToFile(0, 0, 0, 0);
139 expect(InvalidParameter, stat);
142 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
148 stat = GdipSaveImageToFile((GpImage*)bm, 0, 0, 0);
149 expect(InvalidParameter, stat);
151 stat = GdipSaveImageToFile((GpImage*)bm, filename, 0, 0);
152 expect(InvalidParameter, stat);
154 /* encoder tests should succeed -- already tested */
155 stat = GdipGetImageEncodersSize(&n, &s);
156 if (stat != Ok || n == 0) goto cleanup;
158 codecs = GdipAlloc(s);
159 if (!codecs) goto cleanup;
161 stat = GdipGetImageEncoders(n, s, codecs);
162 if (stat != Ok) goto cleanup;
164 stat = GdipSaveImageToFile((GpImage*)bm, filename, &codecs[0].Clsid, 0);
167 GdipDisposeImage((GpImage*)bm);
170 /* re-load and check image stats */
171 stat = GdipLoadImageFromFile(filename, (GpImage**)&bm);
173 if (stat != Ok) goto cleanup;
175 stat = GdipGetImageDimension((GpImage*)bm, &w, &h);
176 if (stat != Ok) goto cleanup;
178 ok((fabs(w - WIDTH) < 0.01) && (fabs(h - HEIGHT) < 0.01),
179 "Saved image dimensions are different!\n");
184 GdipDisposeImage((GpImage*)bm);
185 ok(DeleteFileW(filename), "Delete failed.\n");
188 static void test_encoders(void)
193 ImageCodecInfo *codecs;
197 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0};
199 stat = GdipGetImageEncodersSize(&n, &s);
202 codecs = GdipAlloc(s);
206 stat = GdipGetImageEncoders(n, s, NULL);
207 expect(GenericError, stat);
209 stat = GdipGetImageEncoders(0, s, codecs);
210 expect(GenericError, stat);
212 stat = GdipGetImageEncoders(n, s-1, codecs);
213 expect(GenericError, stat);
215 stat = GdipGetImageEncoders(n, s+1, codecs);
216 expect(GenericError, stat);
218 stat = GdipGetImageEncoders(n, s, codecs);
222 for (i = 0; i < n; i++)
224 if (CompareStringW(LOCALE_SYSTEM_DEFAULT, 0,
225 codecs[i].FormatDescription, -1,
226 bmp_format, -1) == CSTR_EQUAL) {
232 ok(FALSE, "No BMP codec found.\n");
237 static void test_LockBits(void)
243 const REAL WIDTH = 10.0, HEIGHT = 20.0;
246 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
255 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
259 stat = GdipBitmapUnlockBits(bm, &bd);
263 /* read-only, consecutive */
264 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
268 stat = GdipBitmapUnlockBits(bm, &bd);
272 stat = GdipDisposeImage((GpImage*)bm);
274 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
278 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
280 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
281 expect(WrongState, stat);
283 stat = GdipBitmapUnlockBits(bm, &bd);
286 stat = GdipDisposeImage((GpImage*)bm);
288 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
291 /* write, no modification */
292 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
296 stat = GdipBitmapUnlockBits(bm, &bd);
300 /* write, consecutive */
301 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
305 stat = GdipBitmapUnlockBits(bm, &bd);
309 stat = GdipDisposeImage((GpImage*)bm);
311 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
315 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
320 ((char*)bd.Scan0)[2] = 0xff;
322 stat = GdipBitmapUnlockBits(bm, &bd);
326 stat = GdipDisposeImage((GpImage*)bm);
330 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
332 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
334 stat = GdipDisposeImage((GpImage*)bm);
340 struct GdiplusStartupInput gdiplusStartupInput;
341 ULONG_PTR gdiplusToken;
343 gdiplusStartupInput.GdiplusVersion = 1;
344 gdiplusStartupInput.DebugEventCallback = NULL;
345 gdiplusStartupInput.SuppressBackgroundThread = 0;
346 gdiplusStartupInput.SuppressExternalCodecs = 0;
348 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
351 test_GetImageDimension();
352 test_LoadingImages();
357 GdiplusShutdown(gdiplusToken);