ole32: Fix memory leaks in the storage test.
[wine] / dlls / gdiplus / tests / image.c
1 /*
2  * Unit test suite for images
3  *
4  * Copyright (C) 2007 Google (Evan Stade)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define COBJMACROS
22
23 #include <math.h>
24
25 #include "initguid.h"
26 #include "windows.h"
27 #include "gdiplus.h"
28 #include "wine/test.h"
29
30 #define expect(expected, got) ok((UINT)(got) == (UINT)(expected), "Expected %.8x, got %.8x\n", (UINT)(expected), (UINT)(got))
31 #define expectf(expected, got) ok(fabs(expected - got) < 0.0001, "Expected %.2f, got %.2f\n", expected, got)
32
33 static void expect_rawformat(REFGUID expected, GpImage *img, int line, BOOL todo)
34 {
35     GUID raw;
36     WCHAR bufferW[39];
37     char buffer[39];
38     char buffer2[39];
39     GpStatus stat;
40
41     stat = GdipGetImageRawFormat(img, &raw);
42     ok_(__FILE__, line)(stat == Ok, "GdipGetImageRawFormat failed with %d\n", stat);
43     if(stat != Ok) return;
44     StringFromGUID2(&raw, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
45     WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
46     StringFromGUID2(expected, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
47     WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer2, sizeof(buffer2), NULL, NULL);
48     if(todo)
49         todo_wine ok_(__FILE__, line)(IsEqualGUID(&raw, expected), "Expected format %s, got %s\n", buffer2, buffer);
50     else
51         ok_(__FILE__, line)(IsEqualGUID(&raw, expected), "Expected format %s, got %s\n", buffer2, buffer);
52 }
53
54 static void test_bufferrawformat(void* buff, int size, REFGUID expected, int line, BOOL todo)
55 {
56     LPSTREAM stream;
57     HGLOBAL  hglob;
58     LPBYTE   data;
59     HRESULT  hres;
60     GpStatus stat;
61     GpBitmap *bmp;
62
63     hglob = GlobalAlloc (0, size);
64     data = GlobalLock (hglob);
65     memcpy(data, buff, size);
66     GlobalUnlock(hglob); data = NULL;
67
68     hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
69     ok_(__FILE__, line)(hres == S_OK, "Failed to create a stream\n");
70     if(hres != S_OK) return;
71
72     stat = GdipCreateBitmapFromStream(stream, &bmp);
73     ok_(__FILE__, line)(stat == Ok, "Failed to create a Bitmap\n");
74     if(stat != Ok){
75         IStream_Release(stream);
76         return;
77     }
78
79     expect_rawformat(expected, (GpImage*)bmp, line, todo);
80
81     GdipDisposeImage((GpImage*)bmp);
82     IStream_Release(stream);
83 }
84
85 static void test_Scan0(void)
86 {
87     GpBitmap *bm;
88     GpStatus stat;
89     BYTE buff[360];
90
91     bm = NULL;
92     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
93     expect(Ok, stat);
94     ok(NULL != bm, "Expected bitmap to be initialized\n");
95     if (stat == Ok)
96         GdipDisposeImage((GpImage*)bm);
97
98     bm = (GpBitmap*)0xdeadbeef;
99     stat = GdipCreateBitmapFromScan0(10, -10, 10, PixelFormat24bppRGB, NULL, &bm);
100     expect(InvalidParameter, stat);
101     ok( !bm, "expected null bitmap\n" );
102
103     bm = (GpBitmap*)0xdeadbeef;
104     stat = GdipCreateBitmapFromScan0(-10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
105     expect(InvalidParameter, stat);
106     ok( !bm, "expected null bitmap\n" );
107
108     bm = (GpBitmap*)0xdeadbeef;
109     stat = GdipCreateBitmapFromScan0(10, 0, 10, PixelFormat24bppRGB, NULL, &bm);
110     expect(InvalidParameter, stat);
111     ok( !bm, "expected null bitmap\n" );
112
113     bm = NULL;
114     stat = GdipCreateBitmapFromScan0(10, 10, 12, PixelFormat24bppRGB, buff, &bm);
115     expect(Ok, stat);
116     ok(NULL != bm, "Expected bitmap to be initialized\n");
117     if (stat == Ok)
118         GdipDisposeImage((GpImage*)bm);
119
120     bm = (GpBitmap*) 0xdeadbeef;
121     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, buff, &bm);
122     expect(InvalidParameter, stat);
123     ok( !bm, "expected null bitmap\n" );
124
125     bm = (GpBitmap*)0xdeadbeef;
126     stat = GdipCreateBitmapFromScan0(10, 10, 0, PixelFormat24bppRGB, buff, &bm);
127     expect(InvalidParameter, stat);
128     ok( bm == (GpBitmap*)0xdeadbeef, "expected deadbeef bitmap\n" );
129
130     bm = NULL;
131     stat = GdipCreateBitmapFromScan0(10, 10, -8, PixelFormat24bppRGB, buff, &bm);
132     expect(Ok, stat);
133     ok(NULL != bm, "Expected bitmap to be initialized\n");
134     if (stat == Ok)
135         GdipDisposeImage((GpImage*)bm);
136
137     bm = (GpBitmap*)0xdeadbeef;
138     stat = GdipCreateBitmapFromScan0(10, 10, -10, PixelFormat24bppRGB, buff, &bm);
139     expect(InvalidParameter, stat);
140     ok( !bm, "expected null bitmap\n" );
141 }
142
143 static void test_GetImageDimension(void)
144 {
145     GpBitmap *bm;
146     GpStatus stat;
147     const REAL WIDTH = 10.0, HEIGHT = 20.0;
148     REAL w,h;
149
150     bm = (GpBitmap*)0xdeadbeef;
151     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
152     expect(Ok,stat);
153     ok((GpBitmap*)0xdeadbeef != bm, "Expected bitmap to not be 0xdeadbeef\n");
154     ok(NULL != bm, "Expected bitmap to not be NULL\n");
155
156     stat = GdipGetImageDimension(NULL,&w,&h);
157     expect(InvalidParameter, stat);
158
159     stat = GdipGetImageDimension((GpImage*)bm,NULL,&h);
160     expect(InvalidParameter, stat);
161
162     stat = GdipGetImageDimension((GpImage*)bm,&w,NULL);
163     expect(InvalidParameter, stat);
164
165     w = -1;
166     h = -1;
167     stat = GdipGetImageDimension((GpImage*)bm,&w,&h);
168     expect(Ok, stat);
169     expectf(WIDTH,  w);
170     expectf(HEIGHT, h);
171     GdipDisposeImage((GpImage*)bm);
172 }
173
174 static void test_GdipImageGetFrameDimensionsCount(void)
175 {
176     GpBitmap *bm;
177     GpStatus stat;
178     const REAL WIDTH = 10.0, HEIGHT = 20.0;
179     UINT w;
180
181     bm = (GpBitmap*)0xdeadbeef;
182     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
183     expect(Ok,stat);
184     ok((GpBitmap*)0xdeadbeef != bm, "Expected bitmap to not be 0xdeadbeef\n");
185     ok(NULL != bm, "Expected bitmap to not be NULL\n");
186
187     stat = GdipImageGetFrameDimensionsCount(NULL,&w);
188     expect(InvalidParameter, stat);
189
190     stat = GdipImageGetFrameDimensionsCount((GpImage*)bm,NULL);
191     expect(InvalidParameter, stat);
192
193     w = -1;
194     stat = GdipImageGetFrameDimensionsCount((GpImage*)bm,&w);
195     expect(Ok, stat);
196     expect(1, w);
197     GdipDisposeImage((GpImage*)bm);
198 }
199
200 static void test_LoadingImages(void)
201 {
202     GpStatus stat;
203
204     stat = GdipCreateBitmapFromFile(0, 0);
205     expect(InvalidParameter, stat);
206
207     stat = GdipCreateBitmapFromFile(0, (GpBitmap**)0xdeadbeef);
208     expect(InvalidParameter, stat);
209
210     stat = GdipLoadImageFromFile(0, 0);
211     expect(InvalidParameter, stat);
212
213     stat = GdipLoadImageFromFile(0, (GpImage**)0xdeadbeef);
214     expect(InvalidParameter, stat);
215
216     stat = GdipLoadImageFromFileICM(0, 0);
217     expect(InvalidParameter, stat);
218
219     stat = GdipLoadImageFromFileICM(0, (GpImage**)0xdeadbeef);
220     expect(InvalidParameter, stat);
221 }
222
223 static void test_SavingImages(void)
224 {
225     GpStatus stat;
226     GpBitmap *bm;
227     UINT n;
228     UINT s;
229     const REAL WIDTH = 10.0, HEIGHT = 20.0;
230     REAL w, h;
231     ImageCodecInfo *codecs;
232     static const CHAR filenameA[] = "a.bmp";
233     static const WCHAR filename[] = { 'a','.','b','m','p',0 };
234
235     codecs = NULL;
236
237     stat = GdipSaveImageToFile(0, 0, 0, 0);
238     expect(InvalidParameter, stat);
239
240     bm = NULL;
241     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
242     expect(Ok, stat);
243     if (!bm)
244         return;
245
246     /* invalid params */
247     stat = GdipSaveImageToFile((GpImage*)bm, 0, 0, 0);
248     expect(InvalidParameter, stat);
249
250     stat = GdipSaveImageToFile((GpImage*)bm, filename, 0, 0);
251     expect(InvalidParameter, stat);
252
253     /* encoder tests should succeed -- already tested */
254     stat = GdipGetImageEncodersSize(&n, &s);
255     if (stat != Ok || n == 0) goto cleanup;
256
257     codecs = GdipAlloc(s);
258     if (!codecs) goto cleanup;
259
260     stat = GdipGetImageEncoders(n, s, codecs);
261     if (stat != Ok) goto cleanup;
262
263     stat = GdipSaveImageToFile((GpImage*)bm, filename, &codecs[0].Clsid, 0);
264     expect(stat, Ok);
265
266     GdipDisposeImage((GpImage*)bm);
267     bm = 0;
268
269     /* re-load and check image stats */
270     stat = GdipLoadImageFromFile(filename, (GpImage**)&bm);
271     expect(stat, Ok);
272     if (stat != Ok) goto cleanup;
273
274     stat = GdipGetImageDimension((GpImage*)bm, &w, &h);
275     if (stat != Ok) goto cleanup;
276
277     expectf(WIDTH, w);
278     expectf(HEIGHT, h);
279
280  cleanup:
281     GdipFree(codecs);
282     if (bm)
283         GdipDisposeImage((GpImage*)bm);
284     ok(DeleteFileA(filenameA), "Delete failed.\n");
285 }
286
287 static void test_encoders(void)
288 {
289     GpStatus stat;
290     UINT n;
291     UINT s;
292     ImageCodecInfo *codecs;
293     int i;
294     int bmp_found;
295
296     static const CHAR bmp_format[] = "BMP";
297
298     stat = GdipGetImageEncodersSize(&n, &s);
299     expect(stat, Ok);
300
301     codecs = GdipAlloc(s);
302     if (!codecs)
303         return;
304
305     stat = GdipGetImageEncoders(n, s, NULL);
306     expect(GenericError, stat);
307
308     stat = GdipGetImageEncoders(0, s, codecs);
309     expect(GenericError, stat);
310
311     stat = GdipGetImageEncoders(n, s-1, codecs);
312     expect(GenericError, stat);
313
314     stat = GdipGetImageEncoders(n, s+1, codecs);
315     expect(GenericError, stat);
316
317     stat = GdipGetImageEncoders(n, s, codecs);
318     expect(stat, Ok);
319
320     bmp_found = FALSE;
321     for (i = 0; i < n; i++)
322         {
323             CHAR desc[32];
324
325             WideCharToMultiByte(CP_ACP, 0, codecs[i].FormatDescription, -1,
326                                 desc, 32, 0, 0);
327
328             if (CompareStringA(LOCALE_SYSTEM_DEFAULT, 0,
329                                desc, -1,
330                                bmp_format, -1) == CSTR_EQUAL) {
331                 bmp_found = TRUE;
332                 break;
333             }
334         }
335     if (!bmp_found)
336         ok(FALSE, "No BMP codec found.\n");
337
338     GdipFree(codecs);
339 }
340
341 static void test_LockBits(void)
342 {
343     GpStatus stat;
344     GpBitmap *bm;
345     GpRect rect;
346     BitmapData bd;
347     const INT WIDTH = 10, HEIGHT = 20;
348
349     bm = NULL;
350     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
351     expect(Ok, stat);
352
353     rect.X = 2;
354     rect.Y = 3;
355     rect.Width = 4;
356     rect.Height = 5;
357
358     /* read-only */
359     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
360     expect(Ok, stat);
361
362     if (stat == Ok) {
363         stat = GdipBitmapUnlockBits(bm, &bd);
364         expect(Ok, stat);
365     }
366
367     /* read-only, with NULL rect -> whole bitmap lock */
368     stat = GdipBitmapLockBits(bm, NULL, ImageLockModeRead, PixelFormat24bppRGB, &bd);
369     expect(Ok, stat);
370     expect(bd.Width,  WIDTH);
371     expect(bd.Height, HEIGHT);
372
373     if (stat == Ok) {
374         stat = GdipBitmapUnlockBits(bm, &bd);
375         expect(Ok, stat);
376     }
377
378     /* read-only, consecutive */
379     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
380     expect(Ok, stat);
381
382     if (stat == Ok) {
383         stat = GdipBitmapUnlockBits(bm, &bd);
384         expect(Ok, stat);
385     }
386
387     stat = GdipDisposeImage((GpImage*)bm);
388     expect(Ok, stat);
389     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
390     expect(Ok, stat);
391
392     /* read x2 */
393     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
394     expect(Ok, stat);
395     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
396     expect(WrongState, stat);
397
398     stat = GdipBitmapUnlockBits(bm, &bd);
399     expect(Ok, stat);
400
401     stat = GdipDisposeImage((GpImage*)bm);
402     expect(Ok, stat);
403     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
404     expect(Ok, stat);
405
406     /* write, no modification */
407     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
408     expect(Ok, stat);
409
410     if (stat == Ok) {
411         stat = GdipBitmapUnlockBits(bm, &bd);
412         expect(Ok, stat);
413     }
414
415     /* write, consecutive */
416     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
417     expect(Ok, stat);
418
419     if (stat == Ok) {
420         stat = GdipBitmapUnlockBits(bm, &bd);
421         expect(Ok, stat);
422     }
423
424     stat = GdipDisposeImage((GpImage*)bm);
425     expect(Ok, stat);
426     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
427     expect(Ok, stat);
428
429     /* write, modify */
430     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
431     expect(Ok, stat);
432
433     if (stat == Ok) {
434         if (bd.Scan0)
435             ((char*)bd.Scan0)[2] = 0xff;
436
437         stat = GdipBitmapUnlockBits(bm, &bd);
438         expect(Ok, stat);
439     }
440
441     stat = GdipDisposeImage((GpImage*)bm);
442     expect(Ok, stat);
443
444     /* dispose locked */
445     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
446     expect(Ok, stat);
447     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
448     expect(Ok, stat);
449     stat = GdipDisposeImage((GpImage*)bm);
450     expect(Ok, stat);
451 }
452
453 static void test_GdipCreateBitmapFromHBITMAP(void)
454 {
455     GpBitmap* gpbm = NULL;
456     HBITMAP hbm = NULL;
457     HPALETTE hpal = NULL;
458     GpStatus stat;
459     BYTE buff[1000];
460     LOGPALETTE* LogPal = NULL;
461     REAL width, height;
462     const REAL WIDTH1 = 5;
463     const REAL HEIGHT1 = 15;
464     const REAL WIDTH2 = 10;
465     const REAL HEIGHT2 = 20;
466     HDC hdc;
467     BITMAPINFO bmi;
468     BYTE *bits;
469
470     stat = GdipCreateBitmapFromHBITMAP(NULL, NULL, NULL);
471     expect(InvalidParameter, stat);
472
473     hbm = CreateBitmap(WIDTH1, HEIGHT1, 1, 1, NULL);
474     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, NULL);
475     expect(InvalidParameter, stat);
476
477     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
478     expect(Ok, stat);
479     expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
480     expectf(WIDTH1,  width);
481     expectf(HEIGHT1, height);
482     if (stat == Ok)
483         GdipDisposeImage((GpImage*)gpbm);
484     DeleteObject(hbm);
485
486     hbm = CreateBitmap(WIDTH2, HEIGHT2, 1, 1, &buff);
487     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
488     expect(Ok, stat);
489     /* raw format */
490     expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)gpbm, __LINE__, FALSE);
491
492     expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
493     expectf(WIDTH2,  width);
494     expectf(HEIGHT2, height);
495     if (stat == Ok)
496         GdipDisposeImage((GpImage*)gpbm);
497     DeleteObject(hbm);
498
499     hdc = CreateCompatibleDC(0);
500     ok(hdc != NULL, "CreateCompatibleDC failed\n");
501     bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
502     bmi.bmiHeader.biHeight = HEIGHT1;
503     bmi.bmiHeader.biWidth = WIDTH1;
504     bmi.bmiHeader.biBitCount = 24;
505     bmi.bmiHeader.biPlanes = 1;
506     bmi.bmiHeader.biCompression = BI_RGB;
507
508     hbm = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
509     ok(hbm != NULL, "CreateDIBSection failed\n");
510
511     bits[0] = 0;
512
513     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
514     expect(Ok, stat);
515     expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
516     expectf(WIDTH1,  width);
517     expectf(HEIGHT1, height);
518     if (stat == Ok)
519     {
520         /* test whether writing to the bitmap affects the original */
521         stat = GdipBitmapSetPixel(gpbm, 0, 0, 0xffffffff);
522         expect(Ok, stat);
523
524         expect(0, bits[0]);
525
526         GdipDisposeImage((GpImage*)gpbm);
527     }
528
529     LogPal = GdipAlloc(sizeof(LOGPALETTE));
530     ok(LogPal != NULL, "unable to allocate LOGPALETTE\n");
531     LogPal->palVersion = 0x300;
532     LogPal->palNumEntries = 1;
533     hpal = CreatePalette(LogPal);
534     ok(hpal != NULL, "CreatePalette failed\n");
535     GdipFree(LogPal);
536
537     stat = GdipCreateBitmapFromHBITMAP(hbm, hpal, &gpbm);
538     todo_wine
539     {
540         expect(Ok, stat);
541     }
542     if (stat == Ok)
543         GdipDisposeImage((GpImage*)gpbm);
544
545     DeleteObject(hpal);
546     DeleteObject(hbm);
547 }
548
549 static void test_GdipGetImageFlags(void)
550 {
551     GpImage *img;
552     GpStatus stat;
553     UINT flags;
554
555     img = (GpImage*)0xdeadbeef;
556
557     stat = GdipGetImageFlags(NULL, NULL);
558     expect(InvalidParameter, stat);
559
560     stat = GdipGetImageFlags(NULL, &flags);
561     expect(InvalidParameter, stat);
562
563     stat = GdipGetImageFlags(img, NULL);
564     expect(InvalidParameter, stat);
565 }
566
567 static void test_GdipCloneImage(void)
568 {
569     GpStatus stat;
570     GpRectF rectF;
571     GpUnit unit;
572     GpBitmap *bm;
573     GpImage *image_src, *image_dest = NULL;
574     const INT WIDTH = 10, HEIGHT = 20;
575
576     /* Create an image, clone it, delete the original, make sure the copy works */
577     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
578     expect(Ok, stat);
579     expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bm, __LINE__, FALSE);
580
581     image_src = ((GpImage*)bm);
582     stat = GdipCloneImage(image_src, &image_dest);
583     expect(Ok, stat);
584     expect_rawformat(&ImageFormatMemoryBMP, image_dest, __LINE__, FALSE);
585
586     stat = GdipDisposeImage((GpImage*)bm);
587     expect(Ok, stat);
588     stat = GdipGetImageBounds(image_dest, &rectF, &unit);
589     expect(Ok, stat);
590
591     /* Treat FP values carefully */
592     expectf((REAL)WIDTH, rectF.Width);
593     expectf((REAL)HEIGHT, rectF.Height);
594
595     stat = GdipDisposeImage(image_dest);
596     expect(Ok, stat);
597 }
598
599 static void test_testcontrol(void)
600 {
601     GpStatus stat;
602     DWORD param;
603
604     param = 0;
605     stat = GdipTestControl(TestControlGetBuildNumber, &param);
606     expect(Ok, stat);
607     ok(param != 0, "Build number expected, got %u\n", param);
608 }
609
610 static void test_fromhicon(void)
611 {
612     static const BYTE bmp_bits[1024];
613     HBITMAP hbmMask, hbmColor;
614     ICONINFO info;
615     HICON hIcon;
616     GpStatus stat;
617     GpBitmap *bitmap = NULL;
618     UINT dim;
619     ImageType type;
620     PixelFormat format;
621
622     /* NULL */
623     stat = GdipCreateBitmapFromHICON(NULL, NULL);
624     expect(InvalidParameter, stat);
625     stat = GdipCreateBitmapFromHICON(NULL, &bitmap);
626     expect(InvalidParameter, stat);
627
628     /* color icon 1 bit */
629     hbmMask = CreateBitmap(16, 16, 1, 1, bmp_bits);
630     ok(hbmMask != 0, "CreateBitmap failed\n");
631     hbmColor = CreateBitmap(16, 16, 1, 1, bmp_bits);
632     ok(hbmColor != 0, "CreateBitmap failed\n");
633     info.fIcon = TRUE;
634     info.xHotspot = 8;
635     info.yHotspot = 8;
636     info.hbmMask = hbmMask;
637     info.hbmColor = hbmColor;
638     hIcon = CreateIconIndirect(&info);
639     ok(hIcon != 0, "CreateIconIndirect failed\n");
640     DeleteObject(hbmMask);
641     DeleteObject(hbmColor);
642
643     stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
644     ok(stat == Ok ||
645        broken(stat == InvalidParameter), /* Win98 */
646        "Expected Ok, got %.8x\n", stat);
647     if(stat == Ok){
648        /* check attributes */
649        stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
650        expect(Ok, stat);
651        expect(16, dim);
652        stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
653        expect(Ok, stat);
654        expect(16, dim);
655        stat = GdipGetImageType((GpImage*)bitmap, &type);
656        expect(Ok, stat);
657        expect(ImageTypeBitmap, type);
658        stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
659        expect(PixelFormat32bppARGB, format);
660        /* raw format */
661        expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bitmap, __LINE__, FALSE);
662        GdipDisposeImage((GpImage*)bitmap);
663     }
664     DestroyIcon(hIcon);
665
666     /* color icon 8 bpp */
667     hbmMask = CreateBitmap(16, 16, 1, 8, bmp_bits);
668     ok(hbmMask != 0, "CreateBitmap failed\n");
669     hbmColor = CreateBitmap(16, 16, 1, 8, bmp_bits);
670     ok(hbmColor != 0, "CreateBitmap failed\n");
671     info.fIcon = TRUE;
672     info.xHotspot = 8;
673     info.yHotspot = 8;
674     info.hbmMask = hbmMask;
675     info.hbmColor = hbmColor;
676     hIcon = CreateIconIndirect(&info);
677     ok(hIcon != 0, "CreateIconIndirect failed\n");
678     DeleteObject(hbmMask);
679     DeleteObject(hbmColor);
680
681     stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
682     expect(Ok, stat);
683     if(stat == Ok){
684         /* check attributes */
685         stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
686         expect(Ok, stat);
687         expect(16, dim);
688         stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
689         expect(Ok, stat);
690         expect(16, dim);
691         stat = GdipGetImageType((GpImage*)bitmap, &type);
692         expect(Ok, stat);
693         expect(ImageTypeBitmap, type);
694         stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
695         expect(PixelFormat32bppARGB, format);
696         /* raw format */
697         expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bitmap, __LINE__, FALSE);
698         GdipDisposeImage((GpImage*)bitmap);
699     }
700     DestroyIcon(hIcon);
701 }
702
703 /* 1x1 pixel png */
704 static const unsigned char pngimage[285] = {
705 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
706 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
707 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
708 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
709 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
710 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
711 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
712 };
713 /* 1x1 pixel gif */
714 static const unsigned char gifimage[35] = {
715 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
716 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
717 0x01,0x00,0x3b
718 };
719 /* 1x1 pixel bmp */
720 static const unsigned char bmpimage[66] = {
721 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
722 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
723 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
724 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
725 0x00,0x00
726 };
727 /* 1x1 pixel jpg */
728 static const unsigned char jpgimage[285] = {
729 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2c,
730 0x01,0x2c,0x00,0x00,0xff,0xdb,0x00,0x43,0x00,0x05,0x03,0x04,0x04,0x04,0x03,0x05,
731 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x07,0x0c,0x08,0x07,0x07,0x07,0x07,0x0f,0x0b,
732 0x0b,0x09,0x0c,0x11,0x0f,0x12,0x12,0x11,0x0f,0x11,0x11,0x13,0x16,0x1c,0x17,0x13,
733 0x14,0x1a,0x15,0x11,0x11,0x18,0x21,0x18,0x1a,0x1d,0x1d,0x1f,0x1f,0x1f,0x13,0x17,
734 0x22,0x24,0x22,0x1e,0x24,0x1c,0x1e,0x1f,0x1e,0xff,0xdb,0x00,0x43,0x01,0x05,0x05,
735 0x05,0x07,0x06,0x07,0x0e,0x08,0x08,0x0e,0x1e,0x14,0x11,0x14,0x1e,0x1e,0x1e,0x1e,
736 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
737 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
738 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0xff,0xc0,
739 0x00,0x11,0x08,0x00,0x01,0x00,0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,
740 0x01,0xff,0xc4,0x00,0x15,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
741 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xff,0xc4,0x00,0x14,0x10,0x01,0x00,0x00,
742 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xc4,
743 0x00,0x14,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
744 0x00,0x00,0x00,0x00,0xff,0xc4,0x00,0x14,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
745 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xda,0x00,0x0c,0x03,0x01,
746 0x00,0x02,0x11,0x03,0x11,0x00,0x3f,0x00,0xb2,0xc0,0x07,0xff,0xd9
747 };
748 static void test_getrawformat(void)
749 {
750     test_bufferrawformat((void*)pngimage, sizeof(pngimage), &ImageFormatPNG,  __LINE__, FALSE);
751     test_bufferrawformat((void*)gifimage, sizeof(gifimage), &ImageFormatGIF,  __LINE__, FALSE);
752     test_bufferrawformat((void*)bmpimage, sizeof(bmpimage), &ImageFormatBMP,  __LINE__, FALSE);
753     test_bufferrawformat((void*)jpgimage, sizeof(jpgimage), &ImageFormatJPEG, __LINE__, FALSE);
754 }
755
756 static void test_createhbitmap(void)
757 {
758     GpStatus stat;
759     GpBitmap *bitmap;
760     HBITMAP hbitmap, oldhbitmap;
761     BITMAP bm;
762     int ret;
763     HDC hdc;
764     COLORREF pixel;
765     BYTE bits[640];
766
767     memset(bits, 0x68, 640);
768
769     /* create Bitmap */
770     stat = GdipCreateBitmapFromScan0(10, 20, 32, PixelFormat24bppRGB, bits, &bitmap);
771     expect(Ok, stat);
772
773     /* test NULL values */
774     stat = GdipCreateHBITMAPFromBitmap(NULL, &hbitmap, 0);
775     expect(InvalidParameter, stat);
776
777     stat = GdipCreateHBITMAPFromBitmap(bitmap, NULL, 0);
778     expect(InvalidParameter, stat);
779
780     /* create HBITMAP */
781     stat = GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
782     expect(Ok, stat);
783
784     if (stat == Ok)
785     {
786         ret = GetObjectA(hbitmap, sizeof(BITMAP), &bm);
787         expect(sizeof(BITMAP), ret);
788
789         expect(0, bm.bmType);
790         expect(10, bm.bmWidth);
791         expect(20, bm.bmHeight);
792         expect(40, bm.bmWidthBytes);
793         expect(1, bm.bmPlanes);
794         expect(32, bm.bmBitsPixel);
795         ok(bm.bmBits != NULL, "got DDB, expected DIB\n");
796
797         hdc = CreateCompatibleDC(NULL);
798
799         oldhbitmap = SelectObject(hdc, hbitmap);
800         pixel = GetPixel(hdc, 5, 5);
801         SelectObject(hdc, oldhbitmap);
802
803         DeleteDC(hdc);
804
805         expect(0x686868, pixel);
806
807         DeleteObject(hbitmap);
808     }
809
810     stat = GdipDisposeImage((GpImage*)bitmap);
811     expect(Ok, stat);
812 }
813
814 static void test_getsetpixel(void)
815 {
816     GpStatus stat;
817     GpBitmap *bitmap;
818     ARGB color;
819     BYTE bits[16] = {0x00,0x00,0x00,0x00, 0x00,0xff,0xff,0x00,
820                      0xff,0x00,0x00,0x00, 0xff,0xff,0xff,0x00};
821
822     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat32bppRGB, bits, &bitmap);
823     expect(Ok, stat);
824
825     /* null parameters */
826     stat = GdipBitmapGetPixel(NULL, 1, 1, &color);
827     expect(InvalidParameter, stat);
828
829     stat = GdipBitmapGetPixel(bitmap, 1, 1, NULL);
830     expect(InvalidParameter, stat);
831
832     stat = GdipBitmapSetPixel(NULL, 1, 1, 0);
833     expect(InvalidParameter, stat);
834
835     /* out of bounds */
836     stat = GdipBitmapGetPixel(bitmap, -1, 1, &color);
837     expect(InvalidParameter, stat);
838
839     stat = GdipBitmapSetPixel(bitmap, -1, 1, 0);
840     expect(InvalidParameter, stat);
841
842     stat = GdipBitmapGetPixel(bitmap, 1, -1, &color);
843     ok(stat == InvalidParameter ||
844        broken(stat == Ok), /* Older gdiplus */
845        "Expected InvalidParameter, got %.8x\n", stat);
846
847     stat = GdipBitmapSetPixel(bitmap, 1, -1, 0);
848     ok(stat == InvalidParameter ||
849        broken(stat == Ok), /* Older gdiplus */
850        "Expected InvalidParameter, got %.8x\n", stat);
851
852     stat = GdipBitmapGetPixel(bitmap, 2, 1, &color);
853     expect(InvalidParameter, stat);
854
855     stat = GdipBitmapSetPixel(bitmap, 2, 1, 0);
856     expect(InvalidParameter, stat);
857
858     stat = GdipBitmapGetPixel(bitmap, 1, 2, &color);
859     expect(InvalidParameter, stat);
860
861     stat = GdipBitmapSetPixel(bitmap, 1, 2, 0);
862     expect(InvalidParameter, stat);
863
864     /* valid use */
865     stat = GdipBitmapGetPixel(bitmap, 1, 1, &color);
866     expect(Ok, stat);
867     expect(0xffffffff, color);
868
869     stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
870     expect(Ok, stat);
871     expect(0xff0000ff, color);
872
873     stat = GdipBitmapSetPixel(bitmap, 1, 1, 0xff676869);
874     expect(Ok, stat);
875
876     stat = GdipBitmapSetPixel(bitmap, 0, 0, 0xff474849);
877     expect(Ok, stat);
878
879     stat = GdipBitmapGetPixel(bitmap, 1, 1, &color);
880     expect(Ok, stat);
881     expect(0xff676869, color);
882
883     stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
884     expect(Ok, stat);
885     expect(0xff474849, color);
886
887     stat = GdipDisposeImage((GpImage*)bitmap);
888     expect(Ok, stat);
889 }
890
891 static void check_halftone_palette(ColorPalette *palette)
892 {
893     static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
894     UINT i;
895
896     for (i=0; i<palette->Count; i++)
897     {
898         ARGB expected=0xff000000;
899         if (i<8)
900         {
901             if (i&1) expected |= 0x800000;
902             if (i&2) expected |= 0x8000;
903             if (i&4) expected |= 0x80;
904         }
905         else if (i == 8)
906         {
907             expected = 0xffc0c0c0;
908         }
909         else if (i < 16)
910         {
911             if (i&1) expected |= 0xff0000;
912             if (i&2) expected |= 0xff00;
913             if (i&4) expected |= 0xff;
914         }
915         else if (i < 40)
916         {
917             expected = 0x00000000;
918         }
919         else
920         {
921             expected |= halftone_values[(i-40)%6];
922             expected |= halftone_values[((i-40)/6)%6] << 8;
923             expected |= halftone_values[((i-40)/36)%6] << 16;
924         }
925         ok(expected == palette->Entries[i], "Expected %.8x, got %.8x, i=%u/%u\n",
926             expected, palette->Entries[i], i, palette->Count);
927     }
928 }
929
930 static void test_palette(void)
931 {
932     GpStatus stat;
933     GpBitmap *bitmap;
934     INT size;
935     BYTE buffer[1040];
936     ColorPalette *palette=(ColorPalette*)buffer;
937
938     /* test initial palette from non-indexed bitmap */
939     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat32bppRGB, NULL, &bitmap);
940     expect(Ok, stat);
941
942     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
943     expect(Ok, stat);
944     expect(sizeof(UINT)*2+sizeof(ARGB), size);
945
946     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
947     expect(Ok, stat);
948     expect(0, palette->Count);
949
950     /* test setting palette on not-indexed bitmap */
951     palette->Count = 3;
952
953     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
954     expect(Ok, stat);
955
956     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
957     expect(Ok, stat);
958     expect(sizeof(UINT)*2+sizeof(ARGB)*3, size);
959
960     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
961     expect(Ok, stat);
962     expect(3, palette->Count);
963
964     GdipDisposeImage((GpImage*)bitmap);
965
966     /* test initial palette on 1-bit bitmap */
967     stat = GdipCreateBitmapFromScan0(2, 2, 4, PixelFormat1bppIndexed, NULL, &bitmap);
968     expect(Ok, stat);
969
970     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
971     expect(Ok, stat);
972     expect(sizeof(UINT)*2+sizeof(ARGB)*2, size);
973
974     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
975     expect(Ok, stat);
976     expect(PaletteFlagsGrayScale, palette->Flags);
977     expect(2, palette->Count);
978
979     expect(0xff000000, palette->Entries[0]);
980     expect(0xffffffff, palette->Entries[1]);
981
982     GdipDisposeImage((GpImage*)bitmap);
983
984     /* test initial palette on 4-bit bitmap */
985     stat = GdipCreateBitmapFromScan0(2, 2, 4, PixelFormat4bppIndexed, NULL, &bitmap);
986     expect(Ok, stat);
987
988     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
989     expect(Ok, stat);
990     expect(sizeof(UINT)*2+sizeof(ARGB)*16, size);
991
992     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
993     expect(Ok, stat);
994     expect(0, palette->Flags);
995     expect(16, palette->Count);
996
997     check_halftone_palette(palette);
998
999     GdipDisposeImage((GpImage*)bitmap);
1000
1001     /* test initial palette on 8-bit bitmap */
1002     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat8bppIndexed, NULL, &bitmap);
1003     expect(Ok, stat);
1004
1005     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1006     expect(Ok, stat);
1007     expect(sizeof(UINT)*2+sizeof(ARGB)*256, size);
1008
1009     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1010     expect(Ok, stat);
1011     expect(PaletteFlagsHalftone, palette->Flags);
1012     expect(256, palette->Count);
1013
1014     check_halftone_palette(palette);
1015
1016     /* test setting/getting a different palette */
1017     palette->Entries[1] = 0xffcccccc;
1018
1019     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1020     expect(Ok, stat);
1021
1022     palette->Entries[1] = 0;
1023
1024     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1025     expect(Ok, stat);
1026     expect(sizeof(UINT)*2+sizeof(ARGB)*256, size);
1027
1028     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1029     expect(Ok, stat);
1030     expect(PaletteFlagsHalftone, palette->Flags);
1031     expect(256, palette->Count);
1032     expect(0xffcccccc, palette->Entries[1]);
1033
1034     /* test count < 256 */
1035     palette->Flags = 12345;
1036     palette->Count = 3;
1037
1038     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1039     expect(Ok, stat);
1040
1041     palette->Entries[1] = 0;
1042     palette->Entries[3] = 0xdeadbeef;
1043
1044     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1045     expect(Ok, stat);
1046     expect(sizeof(UINT)*2+sizeof(ARGB)*3, size);
1047
1048     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1049     expect(Ok, stat);
1050     expect(12345, palette->Flags);
1051     expect(3, palette->Count);
1052     expect(0xffcccccc, palette->Entries[1]);
1053     expect(0xdeadbeef, palette->Entries[3]);
1054
1055     /* test count > 256 */
1056     palette->Count = 257;
1057
1058     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1059     ok(stat == InvalidParameter ||
1060        broken(stat == Ok), /* Old gdiplus behavior */
1061        "Expected %.8x, got %.8x\n", InvalidParameter, stat);
1062
1063     GdipDisposeImage((GpImage*)bitmap);
1064 }
1065
1066 START_TEST(image)
1067 {
1068     struct GdiplusStartupInput gdiplusStartupInput;
1069     ULONG_PTR gdiplusToken;
1070
1071     gdiplusStartupInput.GdiplusVersion              = 1;
1072     gdiplusStartupInput.DebugEventCallback          = NULL;
1073     gdiplusStartupInput.SuppressBackgroundThread    = 0;
1074     gdiplusStartupInput.SuppressExternalCodecs      = 0;
1075
1076     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
1077
1078     test_Scan0();
1079     test_GetImageDimension();
1080     test_GdipImageGetFrameDimensionsCount();
1081     test_LoadingImages();
1082     test_SavingImages();
1083     test_encoders();
1084     test_LockBits();
1085     test_GdipCreateBitmapFromHBITMAP();
1086     test_GdipGetImageFlags();
1087     test_GdipCloneImage();
1088     test_testcontrol();
1089     test_fromhicon();
1090     test_getrawformat();
1091     test_createhbitmap();
1092     test_getsetpixel();
1093     test_palette();
1094
1095     GdiplusShutdown(gdiplusToken);
1096 }