gdiplus/tests: Fix some test failures on older gdiplus.
[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     hpal = CreatePalette(LogPal);
533     ok(hpal != NULL, "CreatePalette failed\n");
534     GdipFree(LogPal);
535
536     stat = GdipCreateBitmapFromHBITMAP(hbm, hpal, &gpbm);
537     todo_wine
538     {
539         expect(Ok, stat);
540     }
541     if (stat == Ok)
542         GdipDisposeImage((GpImage*)gpbm);
543
544     DeleteObject(hpal);
545     DeleteObject(hbm);
546 }
547
548 static void test_GdipGetImageFlags(void)
549 {
550     GpImage *img;
551     GpStatus stat;
552     UINT flags;
553
554     img = (GpImage*)0xdeadbeef;
555
556     stat = GdipGetImageFlags(NULL, NULL);
557     expect(InvalidParameter, stat);
558
559     stat = GdipGetImageFlags(NULL, &flags);
560     expect(InvalidParameter, stat);
561
562     stat = GdipGetImageFlags(img, NULL);
563     expect(InvalidParameter, stat);
564 }
565
566 static void test_GdipCloneImage(void)
567 {
568     GpStatus stat;
569     GpRectF rectF;
570     GpUnit unit;
571     GpBitmap *bm;
572     GpImage *image_src, *image_dest = NULL;
573     const INT WIDTH = 10, HEIGHT = 20;
574
575     /* Create an image, clone it, delete the original, make sure the copy works */
576     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
577     expect(Ok, stat);
578     expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bm, __LINE__, FALSE);
579
580     image_src = ((GpImage*)bm);
581     stat = GdipCloneImage(image_src, &image_dest);
582     expect(Ok, stat);
583     expect_rawformat(&ImageFormatMemoryBMP, image_dest, __LINE__, FALSE);
584
585     stat = GdipDisposeImage((GpImage*)bm);
586     expect(Ok, stat);
587     stat = GdipGetImageBounds(image_dest, &rectF, &unit);
588     expect(Ok, stat);
589
590     /* Treat FP values carefully */
591     expectf((REAL)WIDTH, rectF.Width);
592     expectf((REAL)HEIGHT, rectF.Height);
593
594     stat = GdipDisposeImage(image_dest);
595     expect(Ok, stat);
596 }
597
598 static void test_testcontrol(void)
599 {
600     GpStatus stat;
601     DWORD param;
602
603     param = 0;
604     stat = GdipTestControl(TestControlGetBuildNumber, &param);
605     expect(Ok, stat);
606     ok(param != 0, "Build number expected, got %u\n", param);
607 }
608
609 static void test_fromhicon(void)
610 {
611     static const BYTE bmp_bits[1024];
612     HBITMAP hbmMask, hbmColor;
613     ICONINFO info;
614     HICON hIcon;
615     GpStatus stat;
616     GpBitmap *bitmap = NULL;
617     UINT dim;
618     ImageType type;
619     PixelFormat format;
620
621     /* NULL */
622     stat = GdipCreateBitmapFromHICON(NULL, NULL);
623     expect(InvalidParameter, stat);
624     stat = GdipCreateBitmapFromHICON(NULL, &bitmap);
625     expect(InvalidParameter, stat);
626
627     /* color icon 1 bit */
628     hbmMask = CreateBitmap(16, 16, 1, 1, bmp_bits);
629     ok(hbmMask != 0, "CreateBitmap failed\n");
630     hbmColor = CreateBitmap(16, 16, 1, 1, bmp_bits);
631     ok(hbmColor != 0, "CreateBitmap failed\n");
632     info.fIcon = TRUE;
633     info.xHotspot = 8;
634     info.yHotspot = 8;
635     info.hbmMask = hbmMask;
636     info.hbmColor = hbmColor;
637     hIcon = CreateIconIndirect(&info);
638     ok(hIcon != 0, "CreateIconIndirect failed\n");
639     DeleteObject(hbmMask);
640     DeleteObject(hbmColor);
641
642     stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
643     ok(stat == Ok ||
644        broken(stat == InvalidParameter), /* Win98 */
645        "Expected Ok, got %.8x\n", stat);
646     if(stat == Ok){
647        /* check attributes */
648        stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
649        expect(Ok, stat);
650        expect(16, dim);
651        stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
652        expect(Ok, stat);
653        expect(16, dim);
654        stat = GdipGetImageType((GpImage*)bitmap, &type);
655        expect(Ok, stat);
656        expect(ImageTypeBitmap, type);
657        stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
658        expect(PixelFormat32bppARGB, format);
659        /* raw format */
660        expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bitmap, __LINE__, FALSE);
661        GdipDisposeImage((GpImage*)bitmap);
662     }
663     DestroyIcon(hIcon);
664
665     /* color icon 8 bpp */
666     hbmMask = CreateBitmap(16, 16, 1, 8, bmp_bits);
667     ok(hbmMask != 0, "CreateBitmap failed\n");
668     hbmColor = CreateBitmap(16, 16, 1, 8, bmp_bits);
669     ok(hbmColor != 0, "CreateBitmap failed\n");
670     info.fIcon = TRUE;
671     info.xHotspot = 8;
672     info.yHotspot = 8;
673     info.hbmMask = hbmMask;
674     info.hbmColor = hbmColor;
675     hIcon = CreateIconIndirect(&info);
676     ok(hIcon != 0, "CreateIconIndirect failed\n");
677     DeleteObject(hbmMask);
678     DeleteObject(hbmColor);
679
680     stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
681     expect(Ok, stat);
682     if(stat == Ok){
683         /* check attributes */
684         stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
685         expect(Ok, stat);
686         expect(16, dim);
687         stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
688         expect(Ok, stat);
689         expect(16, dim);
690         stat = GdipGetImageType((GpImage*)bitmap, &type);
691         expect(Ok, stat);
692         expect(ImageTypeBitmap, type);
693         stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
694         expect(PixelFormat32bppARGB, format);
695         /* raw format */
696         expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bitmap, __LINE__, FALSE);
697         GdipDisposeImage((GpImage*)bitmap);
698     }
699     DestroyIcon(hIcon);
700 }
701
702 /* 1x1 pixel png */
703 static const unsigned char pngimage[285] = {
704 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
705 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
706 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
707 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
708 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
709 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
710 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
711 };
712 /* 1x1 pixel gif */
713 static const unsigned char gifimage[35] = {
714 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
715 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
716 0x01,0x00,0x3b
717 };
718 /* 1x1 pixel bmp */
719 static const unsigned char bmpimage[66] = {
720 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
721 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
722 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
723 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
724 0x00,0x00
725 };
726 /* 1x1 pixel jpg */
727 static const unsigned char jpgimage[285] = {
728 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2c,
729 0x01,0x2c,0x00,0x00,0xff,0xdb,0x00,0x43,0x00,0x05,0x03,0x04,0x04,0x04,0x03,0x05,
730 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x07,0x0c,0x08,0x07,0x07,0x07,0x07,0x0f,0x0b,
731 0x0b,0x09,0x0c,0x11,0x0f,0x12,0x12,0x11,0x0f,0x11,0x11,0x13,0x16,0x1c,0x17,0x13,
732 0x14,0x1a,0x15,0x11,0x11,0x18,0x21,0x18,0x1a,0x1d,0x1d,0x1f,0x1f,0x1f,0x13,0x17,
733 0x22,0x24,0x22,0x1e,0x24,0x1c,0x1e,0x1f,0x1e,0xff,0xdb,0x00,0x43,0x01,0x05,0x05,
734 0x05,0x07,0x06,0x07,0x0e,0x08,0x08,0x0e,0x1e,0x14,0x11,0x14,0x1e,0x1e,0x1e,0x1e,
735 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,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,0xff,0xc0,
738 0x00,0x11,0x08,0x00,0x01,0x00,0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,
739 0x01,0xff,0xc4,0x00,0x15,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
740 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xff,0xc4,0x00,0x14,0x10,0x01,0x00,0x00,
741 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xc4,
742 0x00,0x14,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
743 0x00,0x00,0x00,0x00,0xff,0xc4,0x00,0x14,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
744 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xda,0x00,0x0c,0x03,0x01,
745 0x00,0x02,0x11,0x03,0x11,0x00,0x3f,0x00,0xb2,0xc0,0x07,0xff,0xd9
746 };
747 static void test_getrawformat(void)
748 {
749     test_bufferrawformat((void*)pngimage, sizeof(pngimage), &ImageFormatPNG,  __LINE__, FALSE);
750     test_bufferrawformat((void*)gifimage, sizeof(gifimage), &ImageFormatGIF,  __LINE__, FALSE);
751     test_bufferrawformat((void*)bmpimage, sizeof(bmpimage), &ImageFormatBMP,  __LINE__, FALSE);
752     test_bufferrawformat((void*)jpgimage, sizeof(jpgimage), &ImageFormatJPEG, __LINE__, FALSE);
753 }
754
755 static void test_createhbitmap(void)
756 {
757     GpStatus stat;
758     GpBitmap *bitmap;
759     HBITMAP hbitmap, oldhbitmap;
760     BITMAP bm;
761     int ret;
762     HDC hdc;
763     COLORREF pixel;
764     BYTE bits[640];
765
766     memset(bits, 0x68, 640);
767
768     /* create Bitmap */
769     stat = GdipCreateBitmapFromScan0(10, 20, 32, PixelFormat24bppRGB, bits, &bitmap);
770     expect(Ok, stat);
771
772     /* test NULL values */
773     stat = GdipCreateHBITMAPFromBitmap(NULL, &hbitmap, 0);
774     expect(InvalidParameter, stat);
775
776     stat = GdipCreateHBITMAPFromBitmap(bitmap, NULL, 0);
777     expect(InvalidParameter, stat);
778
779     /* create HBITMAP */
780     stat = GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
781     expect(Ok, stat);
782
783     if (stat == Ok)
784     {
785         ret = GetObjectA(hbitmap, sizeof(BITMAP), &bm);
786         expect(sizeof(BITMAP), ret);
787
788         expect(0, bm.bmType);
789         expect(10, bm.bmWidth);
790         expect(20, bm.bmHeight);
791         expect(40, bm.bmWidthBytes);
792         expect(1, bm.bmPlanes);
793         expect(32, bm.bmBitsPixel);
794         ok(bm.bmBits != NULL, "got DDB, expected DIB\n");
795
796         hdc = CreateCompatibleDC(NULL);
797
798         oldhbitmap = SelectObject(hdc, hbitmap);
799         pixel = GetPixel(hdc, 5, 5);
800         SelectObject(hdc, oldhbitmap);
801
802         DeleteDC(hdc);
803
804         expect(0x686868, pixel);
805
806         DeleteObject(hbitmap);
807     }
808
809     stat = GdipDisposeImage((GpImage*)bitmap);
810     expect(Ok, stat);
811 }
812
813 static void test_getsetpixel(void)
814 {
815     GpStatus stat;
816     GpBitmap *bitmap;
817     ARGB color;
818     BYTE bits[16] = {0x00,0x00,0x00,0x00, 0x00,0xff,0xff,0x00,
819                      0xff,0x00,0x00,0x00, 0xff,0xff,0xff,0x00};
820
821     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat32bppRGB, bits, &bitmap);
822     expect(Ok, stat);
823
824     /* null parameters */
825     stat = GdipBitmapGetPixel(NULL, 1, 1, &color);
826     expect(InvalidParameter, stat);
827
828     stat = GdipBitmapGetPixel(bitmap, 1, 1, NULL);
829     expect(InvalidParameter, stat);
830
831     stat = GdipBitmapSetPixel(NULL, 1, 1, 0);
832     expect(InvalidParameter, stat);
833
834     /* out of bounds */
835     stat = GdipBitmapGetPixel(bitmap, -1, 1, &color);
836     expect(InvalidParameter, stat);
837
838     stat = GdipBitmapSetPixel(bitmap, -1, 1, 0);
839     expect(InvalidParameter, stat);
840
841     stat = GdipBitmapGetPixel(bitmap, 1, -1, &color);
842     ok(stat == InvalidParameter ||
843        broken(stat == Ok), /* Older gdiplus */
844        "Expected InvalidParameter, got %.8x\n", stat);
845
846     stat = GdipBitmapSetPixel(bitmap, 1, -1, 0);
847     ok(stat == InvalidParameter ||
848        broken(stat == Ok), /* Older gdiplus */
849        "Expected InvalidParameter, got %.8x\n", stat);
850
851     stat = GdipBitmapGetPixel(bitmap, 2, 1, &color);
852     expect(InvalidParameter, stat);
853
854     stat = GdipBitmapSetPixel(bitmap, 2, 1, 0);
855     expect(InvalidParameter, stat);
856
857     stat = GdipBitmapGetPixel(bitmap, 1, 2, &color);
858     expect(InvalidParameter, stat);
859
860     stat = GdipBitmapSetPixel(bitmap, 1, 2, 0);
861     expect(InvalidParameter, stat);
862
863     /* valid use */
864     stat = GdipBitmapGetPixel(bitmap, 1, 1, &color);
865     expect(Ok, stat);
866     expect(0xffffffff, color);
867
868     stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
869     expect(Ok, stat);
870     expect(0xff0000ff, color);
871
872     stat = GdipBitmapSetPixel(bitmap, 1, 1, 0xff676869);
873     expect(Ok, stat);
874
875     stat = GdipBitmapSetPixel(bitmap, 0, 0, 0xff474849);
876     expect(Ok, stat);
877
878     stat = GdipBitmapGetPixel(bitmap, 1, 1, &color);
879     expect(Ok, stat);
880     expect(0xff676869, color);
881
882     stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
883     expect(Ok, stat);
884     expect(0xff474849, color);
885
886     stat = GdipDisposeImage((GpImage*)bitmap);
887     expect(Ok, stat);
888 }
889
890 static void check_halftone_palette(ColorPalette *palette)
891 {
892     static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
893     UINT i;
894
895     for (i=0; i<palette->Count; i++)
896     {
897         ARGB expected=0xff000000;
898         if (i<8)
899         {
900             if (i&1) expected |= 0x800000;
901             if (i&2) expected |= 0x8000;
902             if (i&4) expected |= 0x80;
903         }
904         else if (i == 8)
905         {
906             expected = 0xffc0c0c0;
907         }
908         else if (i < 16)
909         {
910             if (i&1) expected |= 0xff0000;
911             if (i&2) expected |= 0xff00;
912             if (i&4) expected |= 0xff;
913         }
914         else if (i < 40)
915         {
916             expected = 0x00000000;
917         }
918         else
919         {
920             expected |= halftone_values[(i-40)%6];
921             expected |= halftone_values[((i-40)/6)%6] << 8;
922             expected |= halftone_values[((i-40)/36)%6] << 16;
923         }
924         ok(expected == palette->Entries[i], "Expected %.8x, got %.8x, i=%u/%u\n",
925             expected, palette->Entries[i], i, palette->Count);
926     }
927 }
928
929 static void test_palette(void)
930 {
931     GpStatus stat;
932     GpBitmap *bitmap;
933     INT size;
934     BYTE buffer[1040];
935     ColorPalette *palette=(ColorPalette*)buffer;
936
937     /* test initial palette from non-indexed bitmap */
938     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat32bppRGB, NULL, &bitmap);
939     expect(Ok, stat);
940
941     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
942     expect(Ok, stat);
943     expect(sizeof(UINT)*2+sizeof(ARGB), size);
944
945     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
946     expect(Ok, stat);
947     expect(0, palette->Count);
948
949     /* test setting palette on not-indexed bitmap */
950     palette->Count = 3;
951
952     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
953     expect(Ok, stat);
954
955     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
956     expect(Ok, stat);
957     expect(sizeof(UINT)*2+sizeof(ARGB)*3, size);
958
959     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
960     expect(Ok, stat);
961     expect(3, palette->Count);
962
963     GdipDisposeImage((GpImage*)bitmap);
964
965     /* test initial palette on 1-bit bitmap */
966     stat = GdipCreateBitmapFromScan0(2, 2, 4, PixelFormat1bppIndexed, NULL, &bitmap);
967     expect(Ok, stat);
968
969     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
970     expect(Ok, stat);
971     expect(sizeof(UINT)*2+sizeof(ARGB)*2, size);
972
973     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
974     expect(Ok, stat);
975     expect(PaletteFlagsGrayScale, palette->Flags);
976     expect(2, palette->Count);
977
978     expect(0xff000000, palette->Entries[0]);
979     expect(0xffffffff, palette->Entries[1]);
980
981     GdipDisposeImage((GpImage*)bitmap);
982
983     /* test initial palette on 4-bit bitmap */
984     stat = GdipCreateBitmapFromScan0(2, 2, 4, PixelFormat4bppIndexed, NULL, &bitmap);
985     expect(Ok, stat);
986
987     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
988     expect(Ok, stat);
989     expect(sizeof(UINT)*2+sizeof(ARGB)*16, size);
990
991     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
992     expect(Ok, stat);
993     expect(0, palette->Flags);
994     expect(16, palette->Count);
995
996     check_halftone_palette(palette);
997
998     GdipDisposeImage((GpImage*)bitmap);
999
1000     /* test initial palette on 8-bit bitmap */
1001     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat8bppIndexed, NULL, &bitmap);
1002     expect(Ok, stat);
1003
1004     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1005     expect(Ok, stat);
1006     expect(sizeof(UINT)*2+sizeof(ARGB)*256, size);
1007
1008     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1009     expect(Ok, stat);
1010     expect(PaletteFlagsHalftone, palette->Flags);
1011     expect(256, palette->Count);
1012
1013     check_halftone_palette(palette);
1014
1015     /* test setting/getting a different palette */
1016     palette->Entries[1] = 0xffcccccc;
1017
1018     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1019     expect(Ok, stat);
1020
1021     palette->Entries[1] = 0;
1022
1023     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1024     expect(Ok, stat);
1025     expect(sizeof(UINT)*2+sizeof(ARGB)*256, size);
1026
1027     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1028     expect(Ok, stat);
1029     expect(PaletteFlagsHalftone, palette->Flags);
1030     expect(256, palette->Count);
1031     expect(0xffcccccc, palette->Entries[1]);
1032
1033     /* test count < 256 */
1034     palette->Flags = 12345;
1035     palette->Count = 3;
1036
1037     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1038     expect(Ok, stat);
1039
1040     palette->Entries[1] = 0;
1041     palette->Entries[3] = 0xdeadbeef;
1042
1043     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1044     expect(Ok, stat);
1045     expect(sizeof(UINT)*2+sizeof(ARGB)*3, size);
1046
1047     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1048     expect(Ok, stat);
1049     expect(12345, palette->Flags);
1050     expect(3, palette->Count);
1051     expect(0xffcccccc, palette->Entries[1]);
1052     expect(0xdeadbeef, palette->Entries[3]);
1053
1054     /* test count > 256 */
1055     palette->Count = 257;
1056
1057     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1058     ok(stat == InvalidParameter ||
1059        broken(stat == Ok), /* Old gdiplus behavior */
1060        "Expected %.8x, got %.8x\n", InvalidParameter, stat);
1061
1062     GdipDisposeImage((GpImage*)bitmap);
1063 }
1064
1065 START_TEST(image)
1066 {
1067     struct GdiplusStartupInput gdiplusStartupInput;
1068     ULONG_PTR gdiplusToken;
1069
1070     gdiplusStartupInput.GdiplusVersion              = 1;
1071     gdiplusStartupInput.DebugEventCallback          = NULL;
1072     gdiplusStartupInput.SuppressBackgroundThread    = 0;
1073     gdiplusStartupInput.SuppressExternalCodecs      = 0;
1074
1075     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
1076
1077     test_Scan0();
1078     test_GetImageDimension();
1079     test_GdipImageGetFrameDimensionsCount();
1080     test_LoadingImages();
1081     test_SavingImages();
1082     test_encoders();
1083     test_LockBits();
1084     test_GdipCreateBitmapFromHBITMAP();
1085     test_GdipGetImageFlags();
1086     test_GdipCloneImage();
1087     test_testcontrol();
1088     test_fromhicon();
1089     test_getrawformat();
1090     test_createhbitmap();
1091     test_getsetpixel();
1092     test_palette();
1093
1094     GdiplusShutdown(gdiplusToken);
1095 }