advapi32: Fix RegGetValue when dwFlags includes RRF_RT_ANY.
[wine/multimedia.git] / dlls / gdiplus / tests / image.c
blob38a58b9c689680c74f9291eb9adf545d92fcb733
1 /*
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
21 #include "windows.h"
22 #include "gdiplus.h"
23 #include "wine/test.h"
24 #include <math.h>
25 #include "wingdi.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)
31 GpBitmap *bm;
32 GpStatus stat;
33 BYTE buff[360];
35 bm = NULL;
36 stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
37 expect(Ok, stat);
38 ok(NULL != bm, "Expected bitmap to be initialized\n");
39 if (stat == Ok)
40 GdipDisposeImage((GpImage*)bm);
42 bm = (GpBitmap*)0xdeadbeef;
43 stat = GdipCreateBitmapFromScan0(10, -10, 10, PixelFormat24bppRGB, NULL, &bm);
44 expect(InvalidParameter, stat);
46 expect(NULL, bm);
48 bm = (GpBitmap*)0xdeadbeef;
49 stat = GdipCreateBitmapFromScan0(-10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
50 expect(InvalidParameter, stat);
52 expect(NULL, bm);
54 bm = (GpBitmap*)0xdeadbeef;
55 stat = GdipCreateBitmapFromScan0(10, 0, 10, PixelFormat24bppRGB, NULL, &bm);
56 expect(InvalidParameter, stat);
58 expect(NULL, bm);
60 bm = NULL;
61 stat = GdipCreateBitmapFromScan0(10, 10, 12, PixelFormat24bppRGB, buff, &bm);
62 expect(Ok, stat);
63 ok(NULL != bm, "Expected bitmap to be initialized\n");
64 if (stat == Ok)
65 GdipDisposeImage((GpImage*)bm);
67 bm = (GpBitmap*) 0xdeadbeef;
68 stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, buff, &bm);
69 expect(InvalidParameter, stat);
70 expect(NULL, bm);
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)
80 GpBitmap *bm;
81 GpStatus stat;
82 const REAL WIDTH = 10.0, HEIGHT = 20.0;
83 REAL w,h;
85 bm = (GpBitmap*)0xdeadbeef;
86 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
87 expect(Ok,stat);
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);
100 w = -1;
101 h = -1;
102 stat = GdipGetImageDimension((GpImage*)bm,&w,&h);
103 expect(Ok, stat);
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)
111 GpBitmap *bm;
112 GpStatus stat;
113 const REAL WIDTH = 10.0, HEIGHT = 20.0;
114 UINT w;
116 bm = (GpBitmap*)0xdeadbeef;
117 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
118 expect(Ok,stat);
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);
128 w = -1;
129 stat = GdipImageGetFrameDimensionsCount((GpImage*)bm,&w);
130 expect(Ok, stat);
131 expect(1, w);
132 GdipDisposeImage((GpImage*)bm);
135 static void test_LoadingImages(void)
137 GpStatus stat;
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)
160 GpStatus stat;
161 GpBitmap *bm;
162 UINT n;
163 UINT s;
164 const REAL WIDTH = 10.0, HEIGHT = 20.0;
165 REAL w, h;
166 ImageCodecInfo *codecs;
167 static const WCHAR filename[] = { 'a','.','b','m','p',0 };
169 codecs = NULL;
171 stat = GdipSaveImageToFile(0, 0, 0, 0);
172 expect(InvalidParameter, stat);
174 bm = NULL;
175 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
176 expect(Ok, stat);
177 if (!bm)
178 return;
180 /* invalid params */
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);
198 expect(stat, Ok);
200 GdipDisposeImage((GpImage*)bm);
201 bm = 0;
203 /* re-load and check image stats */
204 stat = GdipLoadImageFromFile(filename, (GpImage**)&bm);
205 expect(stat, Ok);
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");
214 cleanup:
215 GdipFree(codecs);
216 if (bm)
217 GdipDisposeImage((GpImage*)bm);
218 ok(DeleteFileW(filename), "Delete failed.\n");
221 static void test_encoders(void)
223 GpStatus stat;
224 UINT n;
225 UINT s;
226 ImageCodecInfo *codecs;
227 int i;
228 int bmp_found;
230 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0};
232 stat = GdipGetImageEncodersSize(&n, &s);
233 expect(stat, Ok);
235 codecs = GdipAlloc(s);
236 if (!codecs)
237 return;
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);
252 expect(stat, Ok);
254 bmp_found = FALSE;
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) {
260 bmp_found = TRUE;
261 break;
264 if (!bmp_found)
265 ok(FALSE, "No BMP codec found.\n");
267 GdipFree(codecs);
270 static void test_LockBits(void)
272 GpStatus stat;
273 GpBitmap *bm;
274 GpRect rect;
275 BitmapData bd;
276 const INT WIDTH = 10, HEIGHT = 20;
278 bm = NULL;
279 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
280 expect(Ok, stat);
282 rect.X = 2;
283 rect.Y = 3;
284 rect.Width = 4;
285 rect.Height = 5;
287 /* read-only */
288 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
289 expect(Ok, stat);
291 if (stat == Ok) {
292 stat = GdipBitmapUnlockBits(bm, &bd);
293 expect(Ok, stat);
296 /* read-only, with NULL rect -> whole bitmap lock */
297 stat = GdipBitmapLockBits(bm, NULL, ImageLockModeRead, PixelFormat24bppRGB, &bd);
298 expect(Ok, stat);
299 expect(bd.Width, WIDTH);
300 expect(bd.Height, HEIGHT);
302 if (stat == Ok) {
303 stat = GdipBitmapUnlockBits(bm, &bd);
304 expect(Ok, stat);
307 /* read-only, consecutive */
308 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
309 expect(Ok, stat);
311 if (stat == Ok) {
312 stat = GdipBitmapUnlockBits(bm, &bd);
313 expect(Ok, stat);
316 stat = GdipDisposeImage((GpImage*)bm);
317 expect(Ok, stat);
318 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
319 expect(Ok, stat);
321 /* read x2 */
322 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
323 expect(Ok, stat);
324 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
325 expect(WrongState, stat);
327 stat = GdipBitmapUnlockBits(bm, &bd);
328 expect(Ok, stat);
330 stat = GdipDisposeImage((GpImage*)bm);
331 expect(Ok, stat);
332 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
333 expect(Ok, stat);
335 /* write, no modification */
336 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
337 expect(Ok, stat);
339 if (stat == Ok) {
340 stat = GdipBitmapUnlockBits(bm, &bd);
341 expect(Ok, stat);
344 /* write, consecutive */
345 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
346 expect(Ok, stat);
348 if (stat == Ok) {
349 stat = GdipBitmapUnlockBits(bm, &bd);
350 expect(Ok, stat);
353 stat = GdipDisposeImage((GpImage*)bm);
354 expect(Ok, stat);
355 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
356 expect(Ok, stat);
358 /* write, modify */
359 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
360 expect(Ok, stat);
362 if (stat == Ok) {
363 if (bd.Scan0)
364 ((char*)bd.Scan0)[2] = 0xff;
366 stat = GdipBitmapUnlockBits(bm, &bd);
367 expect(Ok, stat);
370 stat = GdipDisposeImage((GpImage*)bm);
371 expect(Ok, stat);
373 /* dispose locked */
374 stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
375 expect(Ok, stat);
376 stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
377 expect(Ok, stat);
378 stat = GdipDisposeImage((GpImage*)bm);
379 expect(Ok, stat);
382 static void test_GdipCreateBitmapFromHBITMAP(void)
384 GpBitmap* gpbm = NULL;
385 HBITMAP hbm = NULL;
386 HPALETTE hpal = NULL;
387 GpStatus stat;
388 BYTE buff[1000];
389 LOGPALETTE* LogPal = NULL;
390 REAL width, height;
391 const REAL WIDTH1 = 5;
392 const REAL HEIGHT1 = 15;
393 const REAL WIDTH2 = 10;
394 const REAL HEIGHT2 = 20;
395 HDC hdc;
396 BITMAPINFO bmi;
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);
406 expect(Ok, stat);
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");
410 if (stat == Ok)
411 GdipDisposeImage((GpImage*)gpbm);
412 GlobalFree(hbm);
414 hbm = CreateBitmap(WIDTH2, HEIGHT2, 1, 1, &buff);
415 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
416 expect(Ok, stat);
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");
420 if (stat == Ok)
421 GdipDisposeImage((GpImage*)gpbm);
422 GlobalFree(hbm);
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);
437 expect(Ok, stat);
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");
441 if (stat == Ok)
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");
449 GdipFree(LogPal);
451 stat = GdipCreateBitmapFromHBITMAP(hbm, hpal, &gpbm);
452 todo_wine
454 expect(Ok, stat);
456 if (stat == Ok)
457 GdipDisposeImage((GpImage*)gpbm);
459 GlobalFree(hpal);
460 GlobalFree(hbm);
463 static void test_GdipGetImageFlags(void)
465 GpImage *img;
466 GpStatus stat;
467 UINT flags;
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)
483 GpStatus stat;
484 GpRectF rectF;
485 GpUnit unit;
486 GpBitmap *bm;
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);
492 expect(Ok, stat);
493 todo_wine
495 image_src = ((GpImage*)bm);
496 stat = GdipCloneImage(image_src, &image_dest);
497 expect(Ok, stat);
499 stat = GdipDisposeImage((GpImage*)bm);
500 expect(Ok, stat);
501 todo_wine
503 stat = GdipGetImageBounds(image_dest, &rectF, &unit);
504 expect(Ok, stat);
505 stat = GdipDisposeImage(image_dest);
506 expect(Ok, stat);
510 START_TEST(image)
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);
522 test_Scan0();
523 test_GetImageDimension();
524 test_GdipImageGetFrameDimensionsCount();
525 test_LoadingImages();
526 test_SavingImages();
527 test_encoders();
528 test_LockBits();
529 test_GdipCreateBitmapFromHBITMAP();
530 test_GdipGetImageFlags();
531 test_GdipCloneImage();
533 GdiplusShutdown(gdiplusToken);