mshtml: Added IHTMLWindow2::focus implementation.
[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 BOOL color_match(ARGB c1, ARGB c2, BYTE max_diff)
34 {
35     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
36     c1 >>= 8; c2 >>= 8;
37     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
38     c1 >>= 8; c2 >>= 8;
39     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
40     c1 >>= 8; c2 >>= 8;
41     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
42     return TRUE;
43 }
44
45 static void expect_guid(REFGUID expected, REFGUID got, int line, BOOL todo)
46 {
47     WCHAR bufferW[39];
48     char buffer[39];
49     char buffer2[39];
50
51     StringFromGUID2(got, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
52     WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer, sizeof(buffer), NULL, NULL);
53     StringFromGUID2(expected, bufferW, sizeof(bufferW)/sizeof(bufferW[0]));
54     WideCharToMultiByte(CP_ACP, 0, bufferW, sizeof(bufferW)/sizeof(bufferW[0]), buffer2, sizeof(buffer2), NULL, NULL);
55     if(todo)
56         todo_wine ok_(__FILE__, line)(IsEqualGUID(expected, got), "Expected %s, got %s\n", buffer2, buffer);
57     else
58         ok_(__FILE__, line)(IsEqualGUID(expected, got), "Expected %s, got %s\n", buffer2, buffer);
59 }
60
61 static void expect_rawformat(REFGUID expected, GpImage *img, int line, BOOL todo)
62 {
63     GUID raw;
64     GpStatus stat;
65
66     stat = GdipGetImageRawFormat(img, &raw);
67     ok_(__FILE__, line)(stat == Ok, "GdipGetImageRawFormat failed with %d\n", stat);
68     if(stat != Ok) return;
69     expect_guid(expected, &raw, line, todo);
70 }
71
72 static void test_bufferrawformat(void* buff, int size, REFGUID expected, int line, BOOL todo)
73 {
74     LPSTREAM stream;
75     HGLOBAL  hglob;
76     LPBYTE   data;
77     HRESULT  hres;
78     GpStatus stat;
79     GpImage *img;
80
81     hglob = GlobalAlloc (0, size);
82     data = GlobalLock (hglob);
83     memcpy(data, buff, size);
84     GlobalUnlock(hglob); data = NULL;
85
86     hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
87     ok_(__FILE__, line)(hres == S_OK, "Failed to create a stream\n");
88     if(hres != S_OK) return;
89
90     stat = GdipLoadImageFromStream(stream, &img);
91     ok_(__FILE__, line)(stat == Ok, "Failed to create a Bitmap\n");
92     if(stat != Ok){
93         IStream_Release(stream);
94         return;
95     }
96
97     expect_rawformat(expected, img, line, todo);
98
99     GdipDisposeImage(img);
100     IStream_Release(stream);
101 }
102
103 static void test_Scan0(void)
104 {
105     GpBitmap *bm;
106     GpStatus stat;
107     BYTE buff[360];
108
109     bm = NULL;
110     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
111     expect(Ok, stat);
112     ok(NULL != bm, "Expected bitmap to be initialized\n");
113     if (stat == Ok)
114         GdipDisposeImage((GpImage*)bm);
115
116     bm = (GpBitmap*)0xdeadbeef;
117     stat = GdipCreateBitmapFromScan0(10, -10, 10, PixelFormat24bppRGB, NULL, &bm);
118     expect(InvalidParameter, stat);
119     ok( !bm, "expected null bitmap\n" );
120
121     bm = (GpBitmap*)0xdeadbeef;
122     stat = GdipCreateBitmapFromScan0(-10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
123     expect(InvalidParameter, stat);
124     ok( !bm, "expected null bitmap\n" );
125
126     bm = (GpBitmap*)0xdeadbeef;
127     stat = GdipCreateBitmapFromScan0(10, 0, 10, PixelFormat24bppRGB, NULL, &bm);
128     expect(InvalidParameter, stat);
129     ok( !bm, "expected null bitmap\n" );
130
131     bm = NULL;
132     stat = GdipCreateBitmapFromScan0(10, 10, 12, PixelFormat24bppRGB, buff, &bm);
133     expect(Ok, stat);
134     ok(NULL != bm, "Expected bitmap to be initialized\n");
135     if (stat == Ok)
136         GdipDisposeImage((GpImage*)bm);
137
138     bm = (GpBitmap*) 0xdeadbeef;
139     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, buff, &bm);
140     expect(InvalidParameter, stat);
141     ok( !bm, "expected null bitmap\n" );
142
143     bm = (GpBitmap*)0xdeadbeef;
144     stat = GdipCreateBitmapFromScan0(10, 10, 0, PixelFormat24bppRGB, buff, &bm);
145     expect(InvalidParameter, stat);
146     ok( bm == (GpBitmap*)0xdeadbeef, "expected deadbeef bitmap\n" );
147
148     bm = NULL;
149     stat = GdipCreateBitmapFromScan0(10, 10, -8, PixelFormat24bppRGB, buff, &bm);
150     expect(Ok, stat);
151     ok(NULL != bm, "Expected bitmap to be initialized\n");
152     if (stat == Ok)
153         GdipDisposeImage((GpImage*)bm);
154
155     bm = (GpBitmap*)0xdeadbeef;
156     stat = GdipCreateBitmapFromScan0(10, 10, -10, PixelFormat24bppRGB, buff, &bm);
157     expect(InvalidParameter, stat);
158     ok( !bm, "expected null bitmap\n" );
159 }
160
161 static void test_FromGdiDib(void)
162 {
163     GpBitmap *bm;
164     GpStatus stat;
165     BYTE buff[400];
166     BYTE rbmi[sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)];
167     BITMAPINFO *bmi = (BITMAPINFO*)rbmi;
168     PixelFormat format;
169
170     bm = NULL;
171
172     memset(rbmi, 0, sizeof(rbmi));
173
174     bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
175     bmi->bmiHeader.biWidth = 10;
176     bmi->bmiHeader.biHeight = 10;
177     bmi->bmiHeader.biPlanes = 1;
178     bmi->bmiHeader.biBitCount = 32;
179     bmi->bmiHeader.biCompression = BI_RGB;
180
181     stat = GdipCreateBitmapFromGdiDib(NULL, buff, &bm);
182     expect(InvalidParameter, stat);
183
184     stat = GdipCreateBitmapFromGdiDib(bmi, NULL, &bm);
185     expect(InvalidParameter, stat);
186
187     stat = GdipCreateBitmapFromGdiDib(bmi, buff, NULL);
188     expect(InvalidParameter, stat);
189
190     stat = GdipCreateBitmapFromGdiDib(bmi, buff, &bm);
191     expect(Ok, stat);
192     ok(NULL != bm, "Expected bitmap to be initialized\n");
193     if (stat == Ok)
194     {
195         stat = GdipGetImagePixelFormat((GpImage*)bm, &format);
196         expect(Ok, stat);
197         expect(PixelFormat32bppRGB, format);
198
199         GdipDisposeImage((GpImage*)bm);
200     }
201
202     bmi->bmiHeader.biBitCount = 24;
203     stat = GdipCreateBitmapFromGdiDib(bmi, buff, &bm);
204     expect(Ok, stat);
205     ok(NULL != bm, "Expected bitmap to be initialized\n");
206     if (stat == Ok)
207     {
208         stat = GdipGetImagePixelFormat((GpImage*)bm, &format);
209         expect(Ok, stat);
210         expect(PixelFormat24bppRGB, format);
211
212         GdipDisposeImage((GpImage*)bm);
213     }
214
215     bmi->bmiHeader.biBitCount = 16;
216     stat = GdipCreateBitmapFromGdiDib(bmi, buff, &bm);
217     expect(Ok, stat);
218     ok(NULL != bm, "Expected bitmap to be initialized\n");
219     if (stat == Ok)
220     {
221         stat = GdipGetImagePixelFormat((GpImage*)bm, &format);
222         expect(Ok, stat);
223         expect(PixelFormat16bppRGB555, format);
224
225         GdipDisposeImage((GpImage*)bm);
226     }
227
228     bmi->bmiHeader.biBitCount = 8;
229     stat = GdipCreateBitmapFromGdiDib(bmi, buff, &bm);
230     expect(Ok, stat);
231     ok(NULL != bm, "Expected bitmap to be initialized\n");
232     if (stat == Ok)
233     {
234         stat = GdipGetImagePixelFormat((GpImage*)bm, &format);
235         expect(Ok, stat);
236         expect(PixelFormat8bppIndexed, format);
237
238         GdipDisposeImage((GpImage*)bm);
239     }
240
241     bmi->bmiHeader.biBitCount = 4;
242     stat = GdipCreateBitmapFromGdiDib(bmi, buff, &bm);
243     expect(Ok, stat);
244     ok(NULL != bm, "Expected bitmap to be initialized\n");
245     if (stat == Ok)
246     {
247         stat = GdipGetImagePixelFormat((GpImage*)bm, &format);
248         expect(Ok, stat);
249         expect(PixelFormat4bppIndexed, format);
250
251         GdipDisposeImage((GpImage*)bm);
252     }
253
254     bmi->bmiHeader.biBitCount = 1;
255     stat = GdipCreateBitmapFromGdiDib(bmi, buff, &bm);
256     expect(Ok, stat);
257     ok(NULL != bm, "Expected bitmap to be initialized\n");
258     if (stat == Ok)
259     {
260         stat = GdipGetImagePixelFormat((GpImage*)bm, &format);
261         expect(Ok, stat);
262         expect(PixelFormat1bppIndexed, format);
263
264         GdipDisposeImage((GpImage*)bm);
265     }
266
267     bmi->bmiHeader.biBitCount = 0;
268     stat = GdipCreateBitmapFromGdiDib(bmi, buff, &bm);
269     expect(InvalidParameter, stat);
270 }
271
272 static void test_GetImageDimension(void)
273 {
274     GpBitmap *bm;
275     GpStatus stat;
276     const REAL WIDTH = 10.0, HEIGHT = 20.0;
277     REAL w,h;
278
279     bm = (GpBitmap*)0xdeadbeef;
280     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
281     expect(Ok,stat);
282     ok((GpBitmap*)0xdeadbeef != bm, "Expected bitmap to not be 0xdeadbeef\n");
283     ok(NULL != bm, "Expected bitmap to not be NULL\n");
284
285     stat = GdipGetImageDimension(NULL,&w,&h);
286     expect(InvalidParameter, stat);
287
288     stat = GdipGetImageDimension((GpImage*)bm,NULL,&h);
289     expect(InvalidParameter, stat);
290
291     stat = GdipGetImageDimension((GpImage*)bm,&w,NULL);
292     expect(InvalidParameter, stat);
293
294     w = -1;
295     h = -1;
296     stat = GdipGetImageDimension((GpImage*)bm,&w,&h);
297     expect(Ok, stat);
298     expectf(WIDTH,  w);
299     expectf(HEIGHT, h);
300     GdipDisposeImage((GpImage*)bm);
301 }
302
303 static void test_GdipImageGetFrameDimensionsCount(void)
304 {
305     GpBitmap *bm;
306     GpStatus stat;
307     const REAL WIDTH = 10.0, HEIGHT = 20.0;
308     UINT w;
309     GUID dimension = {0};
310     UINT count;
311     ARGB color;
312
313     bm = (GpBitmap*)0xdeadbeef;
314     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
315     expect(Ok,stat);
316     ok((GpBitmap*)0xdeadbeef != bm, "Expected bitmap to not be 0xdeadbeef\n");
317     ok(NULL != bm, "Expected bitmap to not be NULL\n");
318
319     stat = GdipImageGetFrameDimensionsCount(NULL,&w);
320     expect(InvalidParameter, stat);
321
322     stat = GdipImageGetFrameDimensionsCount((GpImage*)bm,NULL);
323     expect(InvalidParameter, stat);
324
325     w = -1;
326     stat = GdipImageGetFrameDimensionsCount((GpImage*)bm,&w);
327     expect(Ok, stat);
328     expect(1, w);
329
330     stat = GdipImageGetFrameDimensionsList((GpImage*)bm, &dimension, 1);
331     expect(Ok, stat);
332     expect_guid(&FrameDimensionPage, &dimension, __LINE__, FALSE);
333
334     stat = GdipImageGetFrameDimensionsList((GpImage*)bm, &dimension, 2);
335     expect(InvalidParameter, stat);
336
337     stat = GdipImageGetFrameDimensionsList((GpImage*)bm, &dimension, 0);
338     expect(InvalidParameter, stat);
339
340     stat = GdipImageGetFrameCount(NULL, &dimension, &count);
341     expect(InvalidParameter, stat);
342
343     /* WinXP crashes on this test */
344     if(0)
345     {
346         stat = GdipImageGetFrameCount((GpImage*)bm, &dimension, NULL);
347         expect(InvalidParameter, stat);
348     }
349
350     stat = GdipImageGetFrameCount((GpImage*)bm, NULL, &count);
351     expect(Ok, stat);
352
353     count = 12345;
354     stat = GdipImageGetFrameCount((GpImage*)bm, &dimension, &count);
355     expect(Ok, stat);
356     expect(1, count);
357
358     GdipBitmapSetPixel(bm, 0, 0, 0xffffffff);
359
360     stat = GdipImageSelectActiveFrame((GpImage*)bm, &dimension, 0);
361     expect(Ok, stat);
362
363     /* SelectActiveFrame has no effect on image data of memory bitmaps */
364     color = 0xdeadbeef;
365     GdipBitmapGetPixel(bm, 0, 0, &color);
366     expect(0xffffffff, color);
367
368     GdipDisposeImage((GpImage*)bm);
369 }
370
371 static void test_LoadingImages(void)
372 {
373     GpStatus stat;
374
375     stat = GdipCreateBitmapFromFile(0, 0);
376     expect(InvalidParameter, stat);
377
378     stat = GdipCreateBitmapFromFile(0, (GpBitmap**)0xdeadbeef);
379     expect(InvalidParameter, stat);
380
381     stat = GdipLoadImageFromFile(0, 0);
382     expect(InvalidParameter, stat);
383
384     stat = GdipLoadImageFromFile(0, (GpImage**)0xdeadbeef);
385     expect(InvalidParameter, stat);
386
387     stat = GdipLoadImageFromFileICM(0, 0);
388     expect(InvalidParameter, stat);
389
390     stat = GdipLoadImageFromFileICM(0, (GpImage**)0xdeadbeef);
391     expect(InvalidParameter, stat);
392 }
393
394 static void test_SavingImages(void)
395 {
396     GpStatus stat;
397     GpBitmap *bm;
398     UINT n;
399     UINT s;
400     const REAL WIDTH = 10.0, HEIGHT = 20.0;
401     REAL w, h;
402     ImageCodecInfo *codecs;
403     static const CHAR filenameA[] = "a.bmp";
404     static const WCHAR filename[] = { 'a','.','b','m','p',0 };
405
406     codecs = NULL;
407
408     stat = GdipSaveImageToFile(0, 0, 0, 0);
409     expect(InvalidParameter, stat);
410
411     bm = NULL;
412     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
413     expect(Ok, stat);
414     if (!bm)
415         return;
416
417     /* invalid params */
418     stat = GdipSaveImageToFile((GpImage*)bm, 0, 0, 0);
419     expect(InvalidParameter, stat);
420
421     stat = GdipSaveImageToFile((GpImage*)bm, filename, 0, 0);
422     expect(InvalidParameter, stat);
423
424     /* encoder tests should succeed -- already tested */
425     stat = GdipGetImageEncodersSize(&n, &s);
426     if (stat != Ok || n == 0) goto cleanup;
427
428     codecs = GdipAlloc(s);
429     if (!codecs) goto cleanup;
430
431     stat = GdipGetImageEncoders(n, s, codecs);
432     if (stat != Ok) goto cleanup;
433
434     stat = GdipSaveImageToFile((GpImage*)bm, filename, &codecs[0].Clsid, 0);
435     expect(stat, Ok);
436
437     GdipDisposeImage((GpImage*)bm);
438     bm = 0;
439
440     /* re-load and check image stats */
441     stat = GdipLoadImageFromFile(filename, (GpImage**)&bm);
442     expect(stat, Ok);
443     if (stat != Ok) goto cleanup;
444
445     stat = GdipGetImageDimension((GpImage*)bm, &w, &h);
446     if (stat != Ok) goto cleanup;
447
448     expectf(WIDTH, w);
449     expectf(HEIGHT, h);
450
451  cleanup:
452     GdipFree(codecs);
453     if (bm)
454         GdipDisposeImage((GpImage*)bm);
455     ok(DeleteFileA(filenameA), "Delete failed.\n");
456 }
457
458 static void test_encoders(void)
459 {
460     GpStatus stat;
461     UINT n;
462     UINT s;
463     ImageCodecInfo *codecs;
464     int i;
465     int bmp_found;
466
467     static const CHAR bmp_format[] = "BMP";
468
469     stat = GdipGetImageEncodersSize(&n, &s);
470     expect(stat, Ok);
471
472     codecs = GdipAlloc(s);
473     if (!codecs)
474         return;
475
476     stat = GdipGetImageEncoders(n, s, NULL);
477     expect(GenericError, stat);
478
479     stat = GdipGetImageEncoders(0, s, codecs);
480     expect(GenericError, stat);
481
482     stat = GdipGetImageEncoders(n, s-1, codecs);
483     expect(GenericError, stat);
484
485     stat = GdipGetImageEncoders(n, s+1, codecs);
486     expect(GenericError, stat);
487
488     stat = GdipGetImageEncoders(n, s, codecs);
489     expect(stat, Ok);
490
491     bmp_found = FALSE;
492     for (i = 0; i < n; i++)
493         {
494             CHAR desc[32];
495
496             WideCharToMultiByte(CP_ACP, 0, codecs[i].FormatDescription, -1,
497                                 desc, 32, 0, 0);
498
499             if (CompareStringA(LOCALE_SYSTEM_DEFAULT, 0,
500                                desc, -1,
501                                bmp_format, -1) == CSTR_EQUAL) {
502                 bmp_found = TRUE;
503                 break;
504             }
505         }
506     if (!bmp_found)
507         ok(FALSE, "No BMP codec found.\n");
508
509     GdipFree(codecs);
510 }
511
512 static void test_LockBits(void)
513 {
514     GpStatus stat;
515     GpBitmap *bm;
516     GpRect rect;
517     BitmapData bd;
518     const INT WIDTH = 10, HEIGHT = 20;
519
520     bm = NULL;
521     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
522     expect(Ok, stat);
523
524     rect.X = 2;
525     rect.Y = 3;
526     rect.Width = 4;
527     rect.Height = 5;
528
529     /* read-only */
530     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
531     expect(Ok, stat);
532
533     if (stat == Ok) {
534         stat = GdipBitmapUnlockBits(bm, &bd);
535         expect(Ok, stat);
536     }
537
538     /* read-only, with NULL rect -> whole bitmap lock */
539     stat = GdipBitmapLockBits(bm, NULL, ImageLockModeRead, PixelFormat24bppRGB, &bd);
540     expect(Ok, stat);
541     expect(bd.Width,  WIDTH);
542     expect(bd.Height, HEIGHT);
543
544     if (stat == Ok) {
545         stat = GdipBitmapUnlockBits(bm, &bd);
546         expect(Ok, stat);
547     }
548
549     /* read-only, consecutive */
550     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
551     expect(Ok, stat);
552
553     if (stat == Ok) {
554         stat = GdipBitmapUnlockBits(bm, &bd);
555         expect(Ok, stat);
556     }
557
558     stat = GdipDisposeImage((GpImage*)bm);
559     expect(Ok, stat);
560     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
561     expect(Ok, stat);
562
563     /* read x2 */
564     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
565     expect(Ok, stat);
566     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
567     expect(WrongState, stat);
568
569     stat = GdipBitmapUnlockBits(bm, &bd);
570     expect(Ok, stat);
571
572     stat = GdipDisposeImage((GpImage*)bm);
573     expect(Ok, stat);
574     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
575     expect(Ok, stat);
576
577     /* write, no modification */
578     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
579     expect(Ok, stat);
580
581     if (stat == Ok) {
582         stat = GdipBitmapUnlockBits(bm, &bd);
583         expect(Ok, stat);
584     }
585
586     /* write, consecutive */
587     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
588     expect(Ok, stat);
589
590     if (stat == Ok) {
591         stat = GdipBitmapUnlockBits(bm, &bd);
592         expect(Ok, stat);
593     }
594
595     stat = GdipDisposeImage((GpImage*)bm);
596     expect(Ok, stat);
597     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
598     expect(Ok, stat);
599
600     /* write, modify */
601     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
602     expect(Ok, stat);
603
604     if (stat == Ok) {
605         if (bd.Scan0)
606             ((char*)bd.Scan0)[2] = 0xff;
607
608         stat = GdipBitmapUnlockBits(bm, &bd);
609         expect(Ok, stat);
610     }
611
612     stat = GdipDisposeImage((GpImage*)bm);
613     expect(Ok, stat);
614
615     /* dispose locked */
616     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
617     expect(Ok, stat);
618     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
619     expect(Ok, stat);
620     stat = GdipDisposeImage((GpImage*)bm);
621     expect(Ok, stat);
622 }
623
624 static void test_GdipCreateBitmapFromHBITMAP(void)
625 {
626     GpBitmap* gpbm = NULL;
627     HBITMAP hbm = NULL;
628     HPALETTE hpal = NULL;
629     GpStatus stat;
630     BYTE buff[1000];
631     LOGPALETTE* LogPal = NULL;
632     REAL width, height;
633     const REAL WIDTH1 = 5;
634     const REAL HEIGHT1 = 15;
635     const REAL WIDTH2 = 10;
636     const REAL HEIGHT2 = 20;
637     HDC hdc;
638     BITMAPINFO bmi;
639     BYTE *bits;
640
641     stat = GdipCreateBitmapFromHBITMAP(NULL, NULL, NULL);
642     expect(InvalidParameter, stat);
643
644     hbm = CreateBitmap(WIDTH1, HEIGHT1, 1, 1, NULL);
645     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, NULL);
646     expect(InvalidParameter, stat);
647
648     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
649     expect(Ok, stat);
650     expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
651     expectf(WIDTH1,  width);
652     expectf(HEIGHT1, height);
653     if (stat == Ok)
654         GdipDisposeImage((GpImage*)gpbm);
655     DeleteObject(hbm);
656
657     memset(buff, 0, sizeof(buff));
658     hbm = CreateBitmap(WIDTH2, HEIGHT2, 1, 1, &buff);
659     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
660     expect(Ok, stat);
661     /* raw format */
662     expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)gpbm, __LINE__, FALSE);
663
664     expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
665     expectf(WIDTH2,  width);
666     expectf(HEIGHT2, height);
667     if (stat == Ok)
668         GdipDisposeImage((GpImage*)gpbm);
669     DeleteObject(hbm);
670
671     hdc = CreateCompatibleDC(0);
672     ok(hdc != NULL, "CreateCompatibleDC failed\n");
673     bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
674     bmi.bmiHeader.biHeight = HEIGHT1;
675     bmi.bmiHeader.biWidth = WIDTH1;
676     bmi.bmiHeader.biBitCount = 24;
677     bmi.bmiHeader.biPlanes = 1;
678     bmi.bmiHeader.biCompression = BI_RGB;
679
680     hbm = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
681     ok(hbm != NULL, "CreateDIBSection failed\n");
682
683     bits[0] = 0;
684
685     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
686     expect(Ok, stat);
687     expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
688     expectf(WIDTH1,  width);
689     expectf(HEIGHT1, height);
690     if (stat == Ok)
691     {
692         /* test whether writing to the bitmap affects the original */
693         stat = GdipBitmapSetPixel(gpbm, 0, 0, 0xffffffff);
694         expect(Ok, stat);
695
696         expect(0, bits[0]);
697
698         GdipDisposeImage((GpImage*)gpbm);
699     }
700
701     LogPal = GdipAlloc(sizeof(LOGPALETTE));
702     ok(LogPal != NULL, "unable to allocate LOGPALETTE\n");
703     LogPal->palVersion = 0x300;
704     LogPal->palNumEntries = 1;
705     hpal = CreatePalette(LogPal);
706     ok(hpal != NULL, "CreatePalette failed\n");
707     GdipFree(LogPal);
708
709     stat = GdipCreateBitmapFromHBITMAP(hbm, hpal, &gpbm);
710     todo_wine
711     {
712         expect(Ok, stat);
713     }
714     if (stat == Ok)
715         GdipDisposeImage((GpImage*)gpbm);
716
717     DeleteObject(hpal);
718     DeleteObject(hbm);
719 }
720
721 static void test_GdipGetImageFlags(void)
722 {
723     GpImage *img;
724     GpStatus stat;
725     UINT flags;
726
727     img = (GpImage*)0xdeadbeef;
728
729     stat = GdipGetImageFlags(NULL, NULL);
730     expect(InvalidParameter, stat);
731
732     stat = GdipGetImageFlags(NULL, &flags);
733     expect(InvalidParameter, stat);
734
735     stat = GdipGetImageFlags(img, NULL);
736     expect(InvalidParameter, stat);
737
738     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat1bppIndexed, NULL, (GpBitmap**)&img);
739     expect(Ok, stat);
740     stat = GdipGetImageFlags(img, &flags);
741     expect(Ok, stat);
742     expect(ImageFlagsHasAlpha, flags);
743     GdipDisposeImage(img);
744
745     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat4bppIndexed, NULL, (GpBitmap**)&img);
746     expect(Ok, stat);
747     stat = GdipGetImageFlags(img, &flags);
748     expect(Ok, stat);
749     expect(ImageFlagsHasAlpha, flags);
750     GdipDisposeImage(img);
751
752     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat8bppIndexed, NULL, (GpBitmap**)&img);
753     expect(Ok, stat);
754     stat = GdipGetImageFlags(img, &flags);
755     expect(Ok, stat);
756     expect(ImageFlagsHasAlpha, flags);
757     GdipDisposeImage(img);
758
759     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat16bppGrayScale, NULL, (GpBitmap**)&img);
760     expect(Ok, stat);
761     stat = GdipGetImageFlags(img, &flags);
762     expect(Ok, stat);
763     expect(ImageFlagsNone, flags);
764     GdipDisposeImage(img);
765
766     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat16bppRGB555, NULL, (GpBitmap**)&img);
767     expect(Ok, stat);
768     stat = GdipGetImageFlags(img, &flags);
769     expect(Ok, stat);
770     expect(ImageFlagsNone, flags);
771     GdipDisposeImage(img);
772
773     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat16bppRGB565, NULL, (GpBitmap**)&img);
774     expect(Ok, stat);
775     stat = GdipGetImageFlags(img, &flags);
776     expect(Ok, stat);
777     expect(ImageFlagsNone, flags);
778     GdipDisposeImage(img);
779
780     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat16bppARGB1555, NULL, (GpBitmap**)&img);
781     expect(Ok, stat);
782     stat = GdipGetImageFlags(img, &flags);
783     expect(Ok, stat);
784     expect(ImageFlagsHasAlpha, flags);
785     GdipDisposeImage(img);
786
787     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, NULL, (GpBitmap**)&img);
788     expect(Ok, stat);
789     stat = GdipGetImageFlags(img, &flags);
790     expect(Ok, stat);
791     expect(ImageFlagsNone, flags);
792     GdipDisposeImage(img);
793
794     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat32bppRGB, NULL, (GpBitmap**)&img);
795     expect(Ok, stat);
796     stat = GdipGetImageFlags(img, &flags);
797     expect(Ok, stat);
798     expect(ImageFlagsNone, flags);
799     GdipDisposeImage(img);
800
801     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat32bppARGB, NULL, (GpBitmap**)&img);
802     expect(Ok, stat);
803     stat = GdipGetImageFlags(img, &flags);
804     expect(Ok, stat);
805     expect(ImageFlagsHasAlpha, flags);
806     GdipDisposeImage(img);
807
808     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat32bppPARGB, NULL, (GpBitmap**)&img);
809     expect(Ok, stat);
810     stat = GdipGetImageFlags(img, &flags);
811     expect(Ok, stat);
812     expect(ImageFlagsHasAlpha, flags);
813     GdipDisposeImage(img);
814
815     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat48bppRGB, NULL, (GpBitmap**)&img);
816     expect(Ok, stat);
817     if (stat == Ok)
818     {
819         stat = GdipGetImageFlags(img, &flags);
820         expect(Ok, stat);
821         expect(ImageFlagsNone, flags);
822         GdipDisposeImage(img);
823     }
824
825     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat64bppARGB, NULL, (GpBitmap**)&img);
826     expect(Ok, stat);
827     if (stat == Ok)
828     {
829         expect(Ok, stat);
830         stat = GdipGetImageFlags(img, &flags);
831         expect(Ok, stat);
832         expect(ImageFlagsHasAlpha, flags);
833         GdipDisposeImage(img);
834     }
835
836     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat64bppPARGB, NULL, (GpBitmap**)&img);
837     expect(Ok, stat);
838     if (stat == Ok)
839     {
840         expect(Ok, stat);
841         stat = GdipGetImageFlags(img, &flags);
842         expect(Ok, stat);
843         expect(ImageFlagsHasAlpha, flags);
844         GdipDisposeImage(img);
845     }
846 }
847
848 static void test_GdipCloneImage(void)
849 {
850     GpStatus stat;
851     GpRectF rectF;
852     GpUnit unit;
853     GpBitmap *bm;
854     GpImage *image_src, *image_dest = NULL;
855     const INT WIDTH = 10, HEIGHT = 20;
856
857     /* Create an image, clone it, delete the original, make sure the copy works */
858     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
859     expect(Ok, stat);
860     expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bm, __LINE__, FALSE);
861
862     image_src = ((GpImage*)bm);
863     stat = GdipCloneImage(image_src, &image_dest);
864     expect(Ok, stat);
865     expect_rawformat(&ImageFormatMemoryBMP, image_dest, __LINE__, FALSE);
866
867     stat = GdipDisposeImage((GpImage*)bm);
868     expect(Ok, stat);
869     stat = GdipGetImageBounds(image_dest, &rectF, &unit);
870     expect(Ok, stat);
871
872     /* Treat FP values carefully */
873     expectf((REAL)WIDTH, rectF.Width);
874     expectf((REAL)HEIGHT, rectF.Height);
875
876     stat = GdipDisposeImage(image_dest);
877     expect(Ok, stat);
878 }
879
880 static void test_testcontrol(void)
881 {
882     GpStatus stat;
883     DWORD param;
884
885     param = 0;
886     stat = GdipTestControl(TestControlGetBuildNumber, &param);
887     expect(Ok, stat);
888     ok(param != 0, "Build number expected, got %u\n", param);
889 }
890
891 static void test_fromhicon(void)
892 {
893     static const BYTE bmp_bits[1024];
894     HBITMAP hbmMask, hbmColor;
895     ICONINFO info;
896     HICON hIcon;
897     GpStatus stat;
898     GpBitmap *bitmap = NULL;
899     UINT dim;
900     ImageType type;
901     PixelFormat format;
902
903     /* NULL */
904     stat = GdipCreateBitmapFromHICON(NULL, NULL);
905     expect(InvalidParameter, stat);
906     stat = GdipCreateBitmapFromHICON(NULL, &bitmap);
907     expect(InvalidParameter, stat);
908
909     /* color icon 1 bit */
910     hbmMask = CreateBitmap(16, 16, 1, 1, bmp_bits);
911     ok(hbmMask != 0, "CreateBitmap failed\n");
912     hbmColor = CreateBitmap(16, 16, 1, 1, bmp_bits);
913     ok(hbmColor != 0, "CreateBitmap failed\n");
914     info.fIcon = TRUE;
915     info.xHotspot = 8;
916     info.yHotspot = 8;
917     info.hbmMask = hbmMask;
918     info.hbmColor = hbmColor;
919     hIcon = CreateIconIndirect(&info);
920     ok(hIcon != 0, "CreateIconIndirect failed\n");
921     DeleteObject(hbmMask);
922     DeleteObject(hbmColor);
923
924     stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
925     ok(stat == Ok ||
926        broken(stat == InvalidParameter), /* Win98 */
927        "Expected Ok, got %.8x\n", stat);
928     if(stat == Ok){
929        /* check attributes */
930        stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
931        expect(Ok, stat);
932        expect(16, dim);
933        stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
934        expect(Ok, stat);
935        expect(16, dim);
936        stat = GdipGetImageType((GpImage*)bitmap, &type);
937        expect(Ok, stat);
938        expect(ImageTypeBitmap, type);
939        stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
940        expect(PixelFormat32bppARGB, format);
941        /* raw format */
942        expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bitmap, __LINE__, FALSE);
943        GdipDisposeImage((GpImage*)bitmap);
944     }
945     DestroyIcon(hIcon);
946
947     /* color icon 8 bpp */
948     hbmMask = CreateBitmap(16, 16, 1, 8, bmp_bits);
949     ok(hbmMask != 0, "CreateBitmap failed\n");
950     hbmColor = CreateBitmap(16, 16, 1, 8, bmp_bits);
951     ok(hbmColor != 0, "CreateBitmap failed\n");
952     info.fIcon = TRUE;
953     info.xHotspot = 8;
954     info.yHotspot = 8;
955     info.hbmMask = hbmMask;
956     info.hbmColor = hbmColor;
957     hIcon = CreateIconIndirect(&info);
958     ok(hIcon != 0, "CreateIconIndirect failed\n");
959     DeleteObject(hbmMask);
960     DeleteObject(hbmColor);
961
962     stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
963     expect(Ok, stat);
964     if(stat == Ok){
965         /* check attributes */
966         stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
967         expect(Ok, stat);
968         expect(16, dim);
969         stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
970         expect(Ok, stat);
971         expect(16, dim);
972         stat = GdipGetImageType((GpImage*)bitmap, &type);
973         expect(Ok, stat);
974         expect(ImageTypeBitmap, type);
975         stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
976         expect(PixelFormat32bppARGB, format);
977         /* raw format */
978         expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bitmap, __LINE__, FALSE);
979         GdipDisposeImage((GpImage*)bitmap);
980     }
981     DestroyIcon(hIcon);
982 }
983
984 /* 1x1 pixel png */
985 static const unsigned char pngimage[285] = {
986 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
987 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
988 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
989 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
990 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
991 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
992 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
993 };
994 /* 1x1 pixel gif */
995 static const unsigned char gifimage[35] = {
996 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
997 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
998 0x01,0x00,0x3b
999 };
1000 /* 1x1 pixel bmp */
1001 static const unsigned char bmpimage[66] = {
1002 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
1003 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
1004 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
1005 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
1006 0x00,0x00
1007 };
1008 /* 1x1 pixel jpg */
1009 static const unsigned char jpgimage[285] = {
1010 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2c,
1011 0x01,0x2c,0x00,0x00,0xff,0xdb,0x00,0x43,0x00,0x05,0x03,0x04,0x04,0x04,0x03,0x05,
1012 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x07,0x0c,0x08,0x07,0x07,0x07,0x07,0x0f,0x0b,
1013 0x0b,0x09,0x0c,0x11,0x0f,0x12,0x12,0x11,0x0f,0x11,0x11,0x13,0x16,0x1c,0x17,0x13,
1014 0x14,0x1a,0x15,0x11,0x11,0x18,0x21,0x18,0x1a,0x1d,0x1d,0x1f,0x1f,0x1f,0x13,0x17,
1015 0x22,0x24,0x22,0x1e,0x24,0x1c,0x1e,0x1f,0x1e,0xff,0xdb,0x00,0x43,0x01,0x05,0x05,
1016 0x05,0x07,0x06,0x07,0x0e,0x08,0x08,0x0e,0x1e,0x14,0x11,0x14,0x1e,0x1e,0x1e,0x1e,
1017 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
1018 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
1019 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0xff,0xc0,
1020 0x00,0x11,0x08,0x00,0x01,0x00,0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,
1021 0x01,0xff,0xc4,0x00,0x15,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1022 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xff,0xc4,0x00,0x14,0x10,0x01,0x00,0x00,
1023 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xc4,
1024 0x00,0x14,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1025 0x00,0x00,0x00,0x00,0xff,0xc4,0x00,0x14,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
1026 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xda,0x00,0x0c,0x03,0x01,
1027 0x00,0x02,0x11,0x03,0x11,0x00,0x3f,0x00,0xb2,0xc0,0x07,0xff,0xd9
1028 };
1029 /* 1x1 pixel tiff */
1030 static const unsigned char tiffimage[] = {
1031 0x49,0x49,0x2a,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0xfe,0x00,
1032 0x04,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x00,0x01,0x00,
1033 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,
1034 0x00,0x00,0x02,0x01,0x03,0x00,0x03,0x00,0x00,0x00,0xd2,0x00,0x00,0x00,0x03,0x01,
1035 0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x06,0x01,0x03,0x00,0x01,0x00,
1036 0x00,0x00,0x02,0x00,0x00,0x00,0x0d,0x01,0x02,0x00,0x1b,0x00,0x00,0x00,0xd8,0x00,
1037 0x00,0x00,0x11,0x01,0x04,0x00,0x01,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x12,0x01,
1038 0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x15,0x01,0x03,0x00,0x01,0x00,
1039 0x00,0x00,0x03,0x00,0x00,0x00,0x16,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x40,0x00,
1040 0x00,0x00,0x17,0x01,0x04,0x00,0x01,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x1a,0x01,
1041 0x05,0x00,0x01,0x00,0x00,0x00,0xf4,0x00,0x00,0x00,0x1b,0x01,0x05,0x00,0x01,0x00,
1042 0x00,0x00,0xfc,0x00,0x00,0x00,0x1c,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,
1043 0x00,0x00,0x28,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,
1044 0x00,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x2f,0x68,0x6f,0x6d,0x65,0x2f,0x6d,0x65,
1045 0x68,0x2f,0x44,0x65,0x73,0x6b,0x74,0x6f,0x70,0x2f,0x74,0x65,0x73,0x74,0x2e,0x74,
1046 0x69,0x66,0x00,0x00,0x00,0x00,0x00,0x48,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x48,
1047 0x00,0x00,0x00,0x01
1048 };
1049 /* 320x320 twip wmf */
1050 static const unsigned char wmfimage[180] = {
1051 0xd7,0xcd,0xc6,0x9a,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x01,0x40,0x01,0xa0,0x05,
1052 0x00,0x00,0x00,0x00,0xb1,0x52,0x01,0x00,0x09,0x00,0x00,0x03,0x4f,0x00,0x00,0x00,
1053 0x0f,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0b,0x02,0x00,0x00,
1054 0x00,0x00,0x05,0x00,0x00,0x00,0x0c,0x02,0x40,0x01,0x40,0x01,0x04,0x00,0x00,0x00,
1055 0x02,0x01,0x01,0x00,0x04,0x00,0x00,0x00,0x04,0x01,0x0d,0x00,0x08,0x00,0x00,0x00,
1056 0xfa,0x02,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
1057 0x2d,0x01,0x00,0x00,0x07,0x00,0x00,0x00,0xfc,0x02,0x01,0x00,0x00,0x00,0x00,0x00,
1058 0x00,0x00,0x04,0x00,0x00,0x00,0x2d,0x01,0x01,0x00,0x07,0x00,0x00,0x00,0xfc,0x02,
1059 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x2d,0x01,0x02,0x00,
1060 0x07,0x00,0x00,0x00,0x1b,0x04,0x40,0x01,0x40,0x01,0x00,0x00,0x00,0x00,0x04,0x00,
1061 0x00,0x00,0xf0,0x01,0x00,0x00,0x04,0x00,0x00,0x00,0xf0,0x01,0x01,0x00,0x03,0x00,
1062 0x00,0x00,0x00,0x00
1063 };
1064 static void test_getrawformat(void)
1065 {
1066     test_bufferrawformat((void*)pngimage, sizeof(pngimage), &ImageFormatPNG,  __LINE__, FALSE);
1067     test_bufferrawformat((void*)gifimage, sizeof(gifimage), &ImageFormatGIF,  __LINE__, FALSE);
1068     test_bufferrawformat((void*)bmpimage, sizeof(bmpimage), &ImageFormatBMP,  __LINE__, FALSE);
1069     test_bufferrawformat((void*)jpgimage, sizeof(jpgimage), &ImageFormatJPEG, __LINE__, FALSE);
1070     test_bufferrawformat((void*)tiffimage, sizeof(tiffimage), &ImageFormatTIFF, __LINE__, FALSE);
1071     test_bufferrawformat((void*)wmfimage, sizeof(wmfimage), &ImageFormatWMF, __LINE__, FALSE);
1072 }
1073
1074 static void test_loadwmf(void)
1075 {
1076     LPSTREAM stream;
1077     HGLOBAL  hglob;
1078     LPBYTE   data;
1079     HRESULT  hres;
1080     GpStatus stat;
1081     GpImage *img;
1082     GpRectF bounds;
1083     GpUnit unit;
1084     REAL res = 12345.0;
1085     MetafileHeader header;
1086
1087     hglob = GlobalAlloc (0, sizeof(wmfimage));
1088     data = GlobalLock (hglob);
1089     memcpy(data, wmfimage, sizeof(wmfimage));
1090     GlobalUnlock(hglob); data = NULL;
1091
1092     hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
1093     ok(hres == S_OK, "Failed to create a stream\n");
1094     if(hres != S_OK) return;
1095
1096     stat = GdipLoadImageFromStream(stream, &img);
1097     ok(stat == Ok, "Failed to create a Bitmap\n");
1098     if(stat != Ok){
1099         IStream_Release(stream);
1100         return;
1101     }
1102
1103     IStream_Release(stream);
1104
1105     stat = GdipGetImageBounds(img, &bounds, &unit);
1106     expect(Ok, stat);
1107     todo_wine expect(UnitPixel, unit);
1108     expectf(0.0, bounds.X);
1109     expectf(0.0, bounds.Y);
1110     todo_wine expectf(320.0, bounds.Width);
1111     todo_wine expectf(320.0, bounds.Height);
1112
1113     stat = GdipGetImageHorizontalResolution(img, &res);
1114     expect(Ok, stat);
1115     todo_wine expectf(1440.0, res);
1116
1117     stat = GdipGetImageVerticalResolution(img, &res);
1118     expect(Ok, stat);
1119     todo_wine expectf(1440.0, res);
1120
1121     memset(&header, 0, sizeof(header));
1122     stat = GdipGetMetafileHeaderFromMetafile((GpMetafile*)img, &header);
1123     expect(Ok, stat);
1124     if (stat == Ok)
1125     {
1126         todo_wine expect(MetafileTypeWmfPlaceable, header.Type);
1127         todo_wine expect(sizeof(wmfimage)-sizeof(WmfPlaceableFileHeader), header.Size);
1128         todo_wine expect(0x300, header.Version);
1129         expect(0, header.EmfPlusFlags);
1130         todo_wine expectf(1440.0, header.DpiX);
1131         todo_wine expectf(1440.0, header.DpiY);
1132         expect(0, header.X);
1133         expect(0, header.Y);
1134         todo_wine expect(320, header.Width);
1135         todo_wine expect(320, header.Height);
1136         todo_wine expect(1, U(header).WmfHeader.mtType);
1137         expect(0, header.EmfPlusHeaderSize);
1138         expect(0, header.LogicalDpiX);
1139         expect(0, header.LogicalDpiY);
1140     }
1141
1142     GdipDisposeImage(img);
1143 }
1144
1145 static void test_createfromwmf(void)
1146 {
1147     HMETAFILE hwmf;
1148     GpImage *img;
1149     GpStatus stat;
1150     GpRectF bounds;
1151     GpUnit unit;
1152     REAL res = 12345.0;
1153     MetafileHeader header;
1154
1155     hwmf = SetMetaFileBitsEx(sizeof(wmfimage)-sizeof(WmfPlaceableFileHeader),
1156         wmfimage+sizeof(WmfPlaceableFileHeader));
1157     ok(hwmf != 0, "SetMetaFileBitsEx failed\n");
1158
1159     stat = GdipCreateMetafileFromWmf(hwmf, TRUE,
1160         (WmfPlaceableFileHeader*)wmfimage, (GpMetafile**)&img);
1161     expect(Ok, stat);
1162
1163     stat = GdipGetImageBounds(img, &bounds, &unit);
1164     expect(Ok, stat);
1165     expect(UnitPixel, unit);
1166     expectf(0.0, bounds.X);
1167     expectf(0.0, bounds.Y);
1168     expectf(320.0, bounds.Width);
1169     expectf(320.0, bounds.Height);
1170
1171     stat = GdipGetImageHorizontalResolution(img, &res);
1172     expect(Ok, stat);
1173     expectf(1440.0, res);
1174
1175     stat = GdipGetImageVerticalResolution(img, &res);
1176     expect(Ok, stat);
1177     expectf(1440.0, res);
1178
1179     memset(&header, 0, sizeof(header));
1180     stat = GdipGetMetafileHeaderFromMetafile((GpMetafile*)img, &header);
1181     expect(Ok, stat);
1182     if (stat == Ok)
1183     {
1184         todo_wine expect(MetafileTypeWmfPlaceable, header.Type);
1185         todo_wine expect(sizeof(wmfimage)-sizeof(WmfPlaceableFileHeader), header.Size);
1186         todo_wine expect(0x300, header.Version);
1187         expect(0, header.EmfPlusFlags);
1188         todo_wine expectf(1440.0, header.DpiX);
1189         todo_wine expectf(1440.0, header.DpiY);
1190         expect(0, header.X);
1191         expect(0, header.Y);
1192         todo_wine expect(320, header.Width);
1193         todo_wine expect(320, header.Height);
1194         todo_wine expect(1, U(header).WmfHeader.mtType);
1195         expect(0, header.EmfPlusHeaderSize);
1196         expect(0, header.LogicalDpiX);
1197         expect(0, header.LogicalDpiY);
1198     }
1199
1200     GdipDisposeImage(img);
1201 }
1202
1203 static void test_resolution(void)
1204 {
1205     GpStatus stat;
1206     GpBitmap *bitmap;
1207     REAL res=-1.0;
1208     HDC screendc;
1209     int screenxres, screenyres;
1210
1211     /* create Bitmap */
1212     stat = GdipCreateBitmapFromScan0(1, 1, 32, PixelFormat24bppRGB, NULL, &bitmap);
1213     expect(Ok, stat);
1214
1215     /* test invalid values */
1216     stat = GdipGetImageHorizontalResolution(NULL, &res);
1217     expect(InvalidParameter, stat);
1218
1219     stat = GdipGetImageHorizontalResolution((GpImage*)bitmap, NULL);
1220     expect(InvalidParameter, stat);
1221
1222     stat = GdipGetImageVerticalResolution(NULL, &res);
1223     expect(InvalidParameter, stat);
1224
1225     stat = GdipGetImageVerticalResolution((GpImage*)bitmap, NULL);
1226     expect(InvalidParameter, stat);
1227
1228     stat = GdipBitmapSetResolution(NULL, 96.0, 96.0);
1229     expect(InvalidParameter, stat);
1230
1231     stat = GdipBitmapSetResolution(bitmap, 0.0, 0.0);
1232     expect(InvalidParameter, stat);
1233
1234     /* defaults to screen resolution */
1235     screendc = GetDC(0);
1236
1237     screenxres = GetDeviceCaps(screendc, LOGPIXELSX);
1238     screenyres = GetDeviceCaps(screendc, LOGPIXELSY);
1239
1240     ReleaseDC(0, screendc);
1241
1242     stat = GdipGetImageHorizontalResolution((GpImage*)bitmap, &res);
1243     expect(Ok, stat);
1244     expectf((REAL)screenxres, res);
1245
1246     stat = GdipGetImageVerticalResolution((GpImage*)bitmap, &res);
1247     expect(Ok, stat);
1248     expectf((REAL)screenyres, res);
1249
1250     /* test changing the resolution */
1251     stat = GdipBitmapSetResolution(bitmap, screenxres*2.0, screenyres*3.0);
1252     expect(Ok, stat);
1253
1254     stat = GdipGetImageHorizontalResolution((GpImage*)bitmap, &res);
1255     expect(Ok, stat);
1256     expectf(screenxres*2.0, res);
1257
1258     stat = GdipGetImageVerticalResolution((GpImage*)bitmap, &res);
1259     expect(Ok, stat);
1260     expectf(screenyres*3.0, res);
1261
1262     stat = GdipDisposeImage((GpImage*)bitmap);
1263     expect(Ok, stat);
1264 }
1265
1266 static void test_createhbitmap(void)
1267 {
1268     GpStatus stat;
1269     GpBitmap *bitmap;
1270     HBITMAP hbitmap, oldhbitmap;
1271     BITMAP bm;
1272     int ret;
1273     HDC hdc;
1274     COLORREF pixel;
1275     BYTE bits[640];
1276
1277     memset(bits, 0x68, 640);
1278
1279     /* create Bitmap */
1280     stat = GdipCreateBitmapFromScan0(10, 20, 32, PixelFormat24bppRGB, bits, &bitmap);
1281     expect(Ok, stat);
1282
1283     /* test NULL values */
1284     stat = GdipCreateHBITMAPFromBitmap(NULL, &hbitmap, 0);
1285     expect(InvalidParameter, stat);
1286
1287     stat = GdipCreateHBITMAPFromBitmap(bitmap, NULL, 0);
1288     expect(InvalidParameter, stat);
1289
1290     /* create HBITMAP */
1291     stat = GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
1292     expect(Ok, stat);
1293
1294     if (stat == Ok)
1295     {
1296         ret = GetObjectA(hbitmap, sizeof(BITMAP), &bm);
1297         expect(sizeof(BITMAP), ret);
1298
1299         expect(0, bm.bmType);
1300         expect(10, bm.bmWidth);
1301         expect(20, bm.bmHeight);
1302         expect(40, bm.bmWidthBytes);
1303         expect(1, bm.bmPlanes);
1304         expect(32, bm.bmBitsPixel);
1305         ok(bm.bmBits != NULL, "got DDB, expected DIB\n");
1306
1307         hdc = CreateCompatibleDC(NULL);
1308
1309         oldhbitmap = SelectObject(hdc, hbitmap);
1310         pixel = GetPixel(hdc, 5, 5);
1311         SelectObject(hdc, oldhbitmap);
1312
1313         DeleteDC(hdc);
1314
1315         expect(0x686868, pixel);
1316
1317         DeleteObject(hbitmap);
1318     }
1319
1320     stat = GdipDisposeImage((GpImage*)bitmap);
1321     expect(Ok, stat);
1322 }
1323
1324 static void test_getthumbnail(void)
1325 {
1326     GpStatus stat;
1327     GpImage *bitmap1, *bitmap2;
1328     UINT width, height;
1329
1330     stat = GdipGetImageThumbnail(NULL, 0, 0, &bitmap2, NULL, NULL);
1331     expect(InvalidParameter, stat);
1332
1333     stat = GdipCreateBitmapFromScan0(128, 128, 0, PixelFormat32bppRGB, NULL, (GpBitmap**)&bitmap1);
1334     expect(Ok, stat);
1335
1336     stat = GdipGetImageThumbnail(bitmap1, 0, 0, NULL, NULL, NULL);
1337     expect(InvalidParameter, stat);
1338
1339     stat = GdipGetImageThumbnail(bitmap1, 0, 0, &bitmap2, NULL, NULL);
1340     expect(Ok, stat);
1341
1342     if (stat == Ok)
1343     {
1344         stat = GdipGetImageWidth(bitmap2, &width);
1345         expect(Ok, stat);
1346         expect(120, width);
1347
1348         stat = GdipGetImageHeight(bitmap2, &height);
1349         expect(Ok, stat);
1350         expect(120, height);
1351
1352         GdipDisposeImage(bitmap2);
1353     }
1354
1355     GdipDisposeImage(bitmap1);
1356
1357
1358     stat = GdipCreateBitmapFromScan0(64, 128, 0, PixelFormat32bppRGB, NULL, (GpBitmap**)&bitmap1);
1359     expect(Ok, stat);
1360
1361     stat = GdipGetImageThumbnail(bitmap1, 32, 32, &bitmap2, NULL, NULL);
1362     expect(Ok, stat);
1363
1364     if (stat == Ok)
1365     {
1366         stat = GdipGetImageWidth(bitmap2, &width);
1367         expect(Ok, stat);
1368         expect(32, width);
1369
1370         stat = GdipGetImageHeight(bitmap2, &height);
1371         expect(Ok, stat);
1372         expect(32, height);
1373
1374         GdipDisposeImage(bitmap2);
1375     }
1376
1377     stat = GdipGetImageThumbnail(bitmap1, 0, 0, &bitmap2, NULL, NULL);
1378     expect(Ok, stat);
1379
1380     if (stat == Ok)
1381     {
1382         stat = GdipGetImageWidth(bitmap2, &width);
1383         expect(Ok, stat);
1384         expect(120, width);
1385
1386         stat = GdipGetImageHeight(bitmap2, &height);
1387         expect(Ok, stat);
1388         expect(120, height);
1389
1390         GdipDisposeImage(bitmap2);
1391     }
1392
1393     GdipDisposeImage(bitmap1);
1394 }
1395
1396 static void test_getsetpixel(void)
1397 {
1398     GpStatus stat;
1399     GpBitmap *bitmap;
1400     ARGB color;
1401     BYTE bits[16] = {0x00,0x00,0x00,0x00, 0x00,0xff,0xff,0x00,
1402                      0xff,0x00,0x00,0x00, 0xff,0xff,0xff,0x00};
1403
1404     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat32bppRGB, bits, &bitmap);
1405     expect(Ok, stat);
1406
1407     /* null parameters */
1408     stat = GdipBitmapGetPixel(NULL, 1, 1, &color);
1409     expect(InvalidParameter, stat);
1410
1411     stat = GdipBitmapGetPixel(bitmap, 1, 1, NULL);
1412     expect(InvalidParameter, stat);
1413
1414     stat = GdipBitmapSetPixel(NULL, 1, 1, 0);
1415     expect(InvalidParameter, stat);
1416
1417     /* out of bounds */
1418     stat = GdipBitmapGetPixel(bitmap, -1, 1, &color);
1419     expect(InvalidParameter, stat);
1420
1421     stat = GdipBitmapSetPixel(bitmap, -1, 1, 0);
1422     expect(InvalidParameter, stat);
1423
1424     stat = GdipBitmapGetPixel(bitmap, 1, -1, &color);
1425     ok(stat == InvalidParameter ||
1426        broken(stat == Ok), /* Older gdiplus */
1427        "Expected InvalidParameter, got %.8x\n", stat);
1428
1429     stat = GdipBitmapSetPixel(bitmap, 1, -1, 0);
1430     ok(stat == InvalidParameter ||
1431        broken(stat == Ok), /* Older gdiplus */
1432        "Expected InvalidParameter, got %.8x\n", stat);
1433
1434     stat = GdipBitmapGetPixel(bitmap, 2, 1, &color);
1435     expect(InvalidParameter, stat);
1436
1437     stat = GdipBitmapSetPixel(bitmap, 2, 1, 0);
1438     expect(InvalidParameter, stat);
1439
1440     stat = GdipBitmapGetPixel(bitmap, 1, 2, &color);
1441     expect(InvalidParameter, stat);
1442
1443     stat = GdipBitmapSetPixel(bitmap, 1, 2, 0);
1444     expect(InvalidParameter, stat);
1445
1446     /* valid use */
1447     stat = GdipBitmapGetPixel(bitmap, 1, 1, &color);
1448     expect(Ok, stat);
1449     expect(0xffffffff, color);
1450
1451     stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
1452     expect(Ok, stat);
1453     expect(0xff0000ff, color);
1454
1455     stat = GdipBitmapSetPixel(bitmap, 1, 1, 0xff676869);
1456     expect(Ok, stat);
1457
1458     stat = GdipBitmapSetPixel(bitmap, 0, 0, 0xff474849);
1459     expect(Ok, stat);
1460
1461     stat = GdipBitmapGetPixel(bitmap, 1, 1, &color);
1462     expect(Ok, stat);
1463     expect(0xff676869, color);
1464
1465     stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
1466     expect(Ok, stat);
1467     expect(0xff474849, color);
1468
1469     stat = GdipDisposeImage((GpImage*)bitmap);
1470     expect(Ok, stat);
1471 }
1472
1473 static void check_halftone_palette(ColorPalette *palette)
1474 {
1475     static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1476     UINT i;
1477
1478     for (i=0; i<palette->Count; i++)
1479     {
1480         ARGB expected=0xff000000;
1481         if (i<8)
1482         {
1483             if (i&1) expected |= 0x800000;
1484             if (i&2) expected |= 0x8000;
1485             if (i&4) expected |= 0x80;
1486         }
1487         else if (i == 8)
1488         {
1489             expected = 0xffc0c0c0;
1490         }
1491         else if (i < 16)
1492         {
1493             if (i&1) expected |= 0xff0000;
1494             if (i&2) expected |= 0xff00;
1495             if (i&4) expected |= 0xff;
1496         }
1497         else if (i < 40)
1498         {
1499             expected = 0x00000000;
1500         }
1501         else
1502         {
1503             expected |= halftone_values[(i-40)%6];
1504             expected |= halftone_values[((i-40)/6)%6] << 8;
1505             expected |= halftone_values[((i-40)/36)%6] << 16;
1506         }
1507         ok(expected == palette->Entries[i], "Expected %.8x, got %.8x, i=%u/%u\n",
1508             expected, palette->Entries[i], i, palette->Count);
1509     }
1510 }
1511
1512 static void test_palette(void)
1513 {
1514     GpStatus stat;
1515     GpBitmap *bitmap;
1516     INT size;
1517     BYTE buffer[1040];
1518     ColorPalette *palette=(ColorPalette*)buffer;
1519     ARGB color=0;
1520
1521     /* test initial palette from non-indexed bitmap */
1522     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat32bppRGB, NULL, &bitmap);
1523     expect(Ok, stat);
1524
1525     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1526     expect(Ok, stat);
1527     expect(sizeof(UINT)*2+sizeof(ARGB), size);
1528
1529     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1530     expect(Ok, stat);
1531     expect(0, palette->Count);
1532
1533     /* test setting palette on not-indexed bitmap */
1534     palette->Count = 3;
1535
1536     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1537     expect(Ok, stat);
1538
1539     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1540     expect(Ok, stat);
1541     expect(sizeof(UINT)*2+sizeof(ARGB)*3, size);
1542
1543     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1544     expect(Ok, stat);
1545     expect(3, palette->Count);
1546
1547     GdipDisposeImage((GpImage*)bitmap);
1548
1549     /* test initial palette on 1-bit bitmap */
1550     stat = GdipCreateBitmapFromScan0(2, 2, 4, PixelFormat1bppIndexed, NULL, &bitmap);
1551     expect(Ok, stat);
1552
1553     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1554     expect(Ok, stat);
1555     expect(sizeof(UINT)*2+sizeof(ARGB)*2, size);
1556
1557     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1558     expect(Ok, stat);
1559     expect(PaletteFlagsGrayScale, palette->Flags);
1560     expect(2, palette->Count);
1561
1562     expect(0xff000000, palette->Entries[0]);
1563     expect(0xffffffff, palette->Entries[1]);
1564
1565     /* test getting/setting pixels */
1566     stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
1567     expect(Ok, stat);
1568     expect(0xff000000, color);
1569
1570     stat = GdipBitmapSetPixel(bitmap, 0, 1, 0xffffffff);
1571     ok((stat == Ok) ||
1572        broken(stat == InvalidParameter) /* pre-win7 */, "stat=%.8x\n", stat);
1573
1574     if (stat == Ok)
1575     {
1576         stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
1577         expect(Ok, stat);
1578         expect(0xffffffff, color);
1579     }
1580
1581     GdipDisposeImage((GpImage*)bitmap);
1582
1583     /* test initial palette on 4-bit bitmap */
1584     stat = GdipCreateBitmapFromScan0(2, 2, 4, PixelFormat4bppIndexed, NULL, &bitmap);
1585     expect(Ok, stat);
1586
1587     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1588     expect(Ok, stat);
1589     expect(sizeof(UINT)*2+sizeof(ARGB)*16, size);
1590
1591     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1592     expect(Ok, stat);
1593     expect(0, palette->Flags);
1594     expect(16, palette->Count);
1595
1596     check_halftone_palette(palette);
1597
1598     /* test getting/setting pixels */
1599     stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
1600     expect(Ok, stat);
1601     expect(0xff000000, color);
1602
1603     stat = GdipBitmapSetPixel(bitmap, 0, 1, 0xffff00ff);
1604     ok((stat == Ok) ||
1605        broken(stat == InvalidParameter) /* pre-win7 */, "stat=%.8x\n", stat);
1606
1607     if (stat == Ok)
1608     {
1609         stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
1610         expect(Ok, stat);
1611         expect(0xffff00ff, color);
1612     }
1613
1614     GdipDisposeImage((GpImage*)bitmap);
1615
1616     /* test initial palette on 8-bit bitmap */
1617     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat8bppIndexed, NULL, &bitmap);
1618     expect(Ok, stat);
1619
1620     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1621     expect(Ok, stat);
1622     expect(sizeof(UINT)*2+sizeof(ARGB)*256, size);
1623
1624     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1625     expect(Ok, stat);
1626     expect(PaletteFlagsHalftone, palette->Flags);
1627     expect(256, palette->Count);
1628
1629     check_halftone_palette(palette);
1630
1631     /* test getting/setting pixels */
1632     stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
1633     expect(Ok, stat);
1634     expect(0xff000000, color);
1635
1636     stat = GdipBitmapSetPixel(bitmap, 0, 1, 0xffcccccc);
1637     ok((stat == Ok) ||
1638        broken(stat == InvalidParameter) /* pre-win7 */, "stat=%.8x\n", stat);
1639
1640     if (stat == Ok)
1641     {
1642         stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
1643         expect(Ok, stat);
1644         expect(0xffcccccc, color);
1645     }
1646
1647     /* test setting/getting a different palette */
1648     palette->Entries[1] = 0xffcccccc;
1649
1650     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1651     expect(Ok, stat);
1652
1653     palette->Entries[1] = 0;
1654
1655     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1656     expect(Ok, stat);
1657     expect(sizeof(UINT)*2+sizeof(ARGB)*256, size);
1658
1659     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1660     expect(Ok, stat);
1661     expect(PaletteFlagsHalftone, palette->Flags);
1662     expect(256, palette->Count);
1663     expect(0xffcccccc, palette->Entries[1]);
1664
1665     /* test count < 256 */
1666     palette->Flags = 12345;
1667     palette->Count = 3;
1668
1669     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1670     expect(Ok, stat);
1671
1672     palette->Entries[1] = 0;
1673     palette->Entries[3] = 0xdeadbeef;
1674
1675     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1676     expect(Ok, stat);
1677     expect(sizeof(UINT)*2+sizeof(ARGB)*3, size);
1678
1679     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1680     expect(Ok, stat);
1681     expect(12345, palette->Flags);
1682     expect(3, palette->Count);
1683     expect(0xffcccccc, palette->Entries[1]);
1684     expect(0xdeadbeef, palette->Entries[3]);
1685
1686     /* test count > 256 */
1687     palette->Count = 257;
1688
1689     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1690     ok(stat == InvalidParameter ||
1691        broken(stat == Ok), /* Old gdiplus behavior */
1692        "Expected %.8x, got %.8x\n", InvalidParameter, stat);
1693
1694     GdipDisposeImage((GpImage*)bitmap);
1695 }
1696
1697 static void test_colormatrix(void)
1698 {
1699     GpStatus stat;
1700     ColorMatrix colormatrix, graymatrix;
1701     GpImageAttributes *imageattr;
1702     const ColorMatrix identity = {{
1703         {1.0,0.0,0.0,0.0,0.0},
1704         {0.0,1.0,0.0,0.0,0.0},
1705         {0.0,0.0,1.0,0.0,0.0},
1706         {0.0,0.0,0.0,1.0,0.0},
1707         {0.0,0.0,0.0,0.0,1.0}}};
1708     const ColorMatrix double_red = {{
1709         {2.0,0.0,0.0,0.0,0.0},
1710         {0.0,1.0,0.0,0.0,0.0},
1711         {0.0,0.0,1.0,0.0,0.0},
1712         {0.0,0.0,0.0,1.0,0.0},
1713         {0.0,0.0,0.0,0.0,1.0}}};
1714     GpBitmap *bitmap1, *bitmap2;
1715     GpGraphics *graphics;
1716     ARGB color;
1717
1718     colormatrix = identity;
1719     graymatrix = identity;
1720
1721     stat = GdipSetImageAttributesColorMatrix(NULL, ColorAdjustTypeDefault,
1722         TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1723     expect(InvalidParameter, stat);
1724
1725     stat = GdipCreateImageAttributes(&imageattr);
1726     expect(Ok, stat);
1727
1728     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1729         TRUE, &colormatrix, NULL, ColorMatrixFlagsDefault);
1730     expect(Ok, stat);
1731
1732     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1733         TRUE, NULL, NULL, ColorMatrixFlagsDefault);
1734     expect(InvalidParameter, stat);
1735
1736     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1737         TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1738     expect(Ok, stat);
1739
1740     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1741         TRUE, &colormatrix, NULL, ColorMatrixFlagsSkipGrays);
1742     expect(Ok, stat);
1743
1744     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1745         TRUE, &colormatrix, NULL, ColorMatrixFlagsAltGray);
1746     expect(InvalidParameter, stat);
1747
1748     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1749         TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsAltGray);
1750     expect(Ok, stat);
1751
1752     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1753         TRUE, &colormatrix, &graymatrix, 3);
1754     expect(InvalidParameter, stat);
1755
1756     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeCount,
1757         TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1758     expect(InvalidParameter, stat);
1759
1760     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeAny,
1761         TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1762     expect(InvalidParameter, stat);
1763
1764     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1765         FALSE, NULL, NULL, ColorMatrixFlagsDefault);
1766     expect(Ok, stat);
1767
1768     /* Drawing a bitmap transforms the colors */
1769     colormatrix = double_red;
1770     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1771         TRUE, &colormatrix, NULL, ColorMatrixFlagsDefault);
1772     expect(Ok, stat);
1773
1774     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap1);
1775     expect(Ok, stat);
1776
1777     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap2);
1778     expect(Ok, stat);
1779
1780     stat = GdipBitmapSetPixel(bitmap1, 0, 0, 0xff40ffff);
1781     expect(Ok, stat);
1782
1783     stat = GdipGetImageGraphicsContext((GpImage*)bitmap2, &graphics);
1784     expect(Ok, stat);
1785
1786     stat = GdipDrawImageRectRectI(graphics, (GpImage*)bitmap1, 0,0,1,1, 0,0,1,1,
1787         UnitPixel, imageattr, NULL, NULL);
1788     expect(Ok, stat);
1789
1790     stat = GdipBitmapGetPixel(bitmap2, 0, 0, &color);
1791     expect(Ok, stat);
1792     todo_wine expect(0xff80ffff, color);
1793
1794     GdipDeleteGraphics(graphics);
1795     GdipDisposeImage((GpImage*)bitmap1);
1796     GdipDisposeImage((GpImage*)bitmap2);
1797     GdipDisposeImageAttributes(imageattr);
1798 }
1799
1800 static void test_gamma(void)
1801 {
1802     GpStatus stat;
1803     GpImageAttributes *imageattr;
1804     GpBitmap *bitmap1, *bitmap2;
1805     GpGraphics *graphics;
1806     ARGB color;
1807
1808     stat = GdipSetImageAttributesGamma(NULL, ColorAdjustTypeDefault, TRUE, 1.0);
1809     expect(InvalidParameter, stat);
1810
1811     stat = GdipCreateImageAttributes(&imageattr);
1812     expect(Ok, stat);
1813
1814     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, TRUE, 1.0);
1815     expect(Ok, stat);
1816
1817     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeAny, TRUE, 1.0);
1818     expect(InvalidParameter, stat);
1819
1820     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, TRUE, -1.0);
1821     expect(InvalidParameter, stat);
1822
1823     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, TRUE, 0.0);
1824     expect(InvalidParameter, stat);
1825
1826     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, TRUE, 0.5);
1827     expect(Ok, stat);
1828
1829     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, FALSE, 0.0);
1830     expect(Ok, stat);
1831
1832     /* Drawing a bitmap transforms the colors */
1833     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, TRUE, 3.0);
1834     expect(Ok, stat);
1835
1836     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap1);
1837     expect(Ok, stat);
1838
1839     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap2);
1840     expect(Ok, stat);
1841
1842     stat = GdipBitmapSetPixel(bitmap1, 0, 0, 0xff80ffff);
1843     expect(Ok, stat);
1844
1845     stat = GdipGetImageGraphicsContext((GpImage*)bitmap2, &graphics);
1846     expect(Ok, stat);
1847
1848     stat = GdipDrawImageRectRectI(graphics, (GpImage*)bitmap1, 0,0,1,1, 0,0,1,1,
1849         UnitPixel, imageattr, NULL, NULL);
1850     expect(Ok, stat);
1851
1852     stat = GdipBitmapGetPixel(bitmap2, 0, 0, &color);
1853     expect(Ok, stat);
1854     todo_wine ok(color_match(0xff20ffff, color, 1), "Expected ff20ffff, got %.8x\n", color);
1855
1856     GdipDeleteGraphics(graphics);
1857     GdipDisposeImage((GpImage*)bitmap1);
1858     GdipDisposeImage((GpImage*)bitmap2);
1859     GdipDisposeImageAttributes(imageattr);
1860 }
1861
1862 /* 1x1 pixel gif, 2 frames; first frame is white, second is black */
1863 static const unsigned char gifanimation[72] = {
1864 0x47,0x49,0x46,0x38,0x39,0x61,0x01,0x00,0x01,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,
1865 0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x21,0xf9,0x04,0x00,0x0a,0x00,0xff,
1866 0x00,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x4c,0x01,0x00,
1867 0x21,0xf9,0x04,0x01,0x0a,0x00,0x01,0x00,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
1868 0x00,0x00,0x02,0x02,0x44,0x01,0x00,0x3b
1869 };
1870
1871 static void test_multiframegif(void)
1872 {
1873     LPSTREAM stream;
1874     HGLOBAL hglob;
1875     LPBYTE data;
1876     HRESULT hres;
1877     GpStatus stat;
1878     GpBitmap *bmp;
1879     ARGB color;
1880     UINT count;
1881     GUID dimension;
1882
1883     /* Test frame functions with an animated GIF */
1884     hglob = GlobalAlloc (0, sizeof(gifanimation));
1885     data = GlobalLock (hglob);
1886     memcpy(data, gifanimation, sizeof(gifanimation));
1887     GlobalUnlock(hglob);
1888
1889     hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
1890     ok(hres == S_OK, "Failed to create a stream\n");
1891     if(hres != S_OK) return;
1892
1893     stat = GdipCreateBitmapFromStream(stream, &bmp);
1894     ok(stat == Ok, "Failed to create a Bitmap\n");
1895     if(stat != Ok){
1896         IStream_Release(stream);
1897         return;
1898     }
1899
1900     /* Bitmap starts at frame 0 */
1901     color = 0xdeadbeef;
1902     stat = GdipBitmapGetPixel(bmp, 0, 0, &color);
1903     expect(Ok, stat);
1904     expect(0xffffffff, color);
1905
1906     /* Check that we get correct metadata */
1907     stat = GdipImageGetFrameDimensionsCount((GpImage*)bmp,&count);
1908     expect(Ok, stat);
1909     expect(1, count);
1910
1911     stat = GdipImageGetFrameDimensionsList((GpImage*)bmp, &dimension, 1);
1912     expect(Ok, stat);
1913     expect_guid(&FrameDimensionTime, &dimension, __LINE__, FALSE);
1914
1915     count = 12345;
1916     stat = GdipImageGetFrameCount((GpImage*)bmp, &dimension, &count);
1917     expect(Ok, stat);
1918     todo_wine expect(2, count);
1919
1920     /* SelectActiveFrame overwrites our current data */
1921     stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 1);
1922     expect(Ok, stat);
1923
1924     color = 0xdeadbeef;
1925     GdipBitmapGetPixel(bmp, 0, 0, &color);
1926     expect(Ok, stat);
1927     todo_wine expect(0xff000000, color);
1928
1929     stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 0);
1930     expect(Ok, stat);
1931
1932     color = 0xdeadbeef;
1933     GdipBitmapGetPixel(bmp, 0, 0, &color);
1934     expect(Ok, stat);
1935     expect(0xffffffff, color);
1936
1937     /* Write over the image data */
1938     stat = GdipBitmapSetPixel(bmp, 0, 0, 0xff000000);
1939     expect(Ok, stat);
1940
1941     /* Switching to the same frame does not overwrite our changes */
1942     stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 0);
1943     expect(Ok, stat);
1944
1945     stat = GdipBitmapGetPixel(bmp, 0, 0, &color);
1946     expect(Ok, stat);
1947     expect(0xff000000, color);
1948
1949     /* But switching to another frame and back does */
1950     stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 1);
1951     expect(Ok, stat);
1952
1953     stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 0);
1954     expect(Ok, stat);
1955
1956     stat = GdipBitmapGetPixel(bmp, 0, 0, &color);
1957     expect(Ok, stat);
1958     todo_wine expect(0xffffffff, color);
1959
1960     GdipDisposeImage((GpImage*)bmp);
1961     IStream_Release(stream);
1962
1963     /* Test with a non-animated gif */
1964     hglob = GlobalAlloc (0, sizeof(gifimage));
1965     data = GlobalLock (hglob);
1966     memcpy(data, gifimage, sizeof(gifimage));
1967     GlobalUnlock(hglob);
1968
1969     hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
1970     ok(hres == S_OK, "Failed to create a stream\n");
1971     if(hres != S_OK) return;
1972
1973     stat = GdipCreateBitmapFromStream(stream, &bmp);
1974     ok(stat == Ok, "Failed to create a Bitmap\n");
1975     if(stat != Ok){
1976         IStream_Release(stream);
1977         return;
1978     }
1979
1980     /* Check metadata */
1981     stat = GdipImageGetFrameDimensionsCount((GpImage*)bmp,&count);
1982     expect(Ok, stat);
1983     expect(1, count);
1984
1985     stat = GdipImageGetFrameDimensionsList((GpImage*)bmp, &dimension, 1);
1986     expect(Ok, stat);
1987     expect_guid(&FrameDimensionTime, &dimension, __LINE__, FALSE);
1988
1989     count = 12345;
1990     stat = GdipImageGetFrameCount((GpImage*)bmp, &dimension, &count);
1991     expect(Ok, stat);
1992     expect(1, count);
1993
1994     GdipDisposeImage((GpImage*)bmp);
1995     IStream_Release(stream);
1996 }
1997
1998 static void test_rotateflip(void)
1999 {
2000     GpImage *bitmap;
2001     GpStatus stat;
2002     BYTE bits[24];
2003     static const BYTE orig_bits[24] = {
2004         0,0,0xff,    0,0xff,0,    0xff,0,0,    23,23,23,
2005         0xff,0xff,0, 0xff,0,0xff, 0,0xff,0xff, 23,23,23};
2006     UINT width, height;
2007     ARGB color;
2008
2009     memcpy(bits, orig_bits, sizeof(bits));
2010     stat = GdipCreateBitmapFromScan0(3, 2, 12, PixelFormat24bppRGB, bits, (GpBitmap**)&bitmap);
2011     expect(Ok, stat);
2012
2013     stat = GdipImageRotateFlip(bitmap, Rotate90FlipNone);
2014     expect(Ok, stat);
2015
2016     stat = GdipGetImageWidth(bitmap, &width);
2017     expect(Ok, stat);
2018     stat = GdipGetImageHeight(bitmap, &height);
2019     expect(Ok, stat);
2020     expect(2, width);
2021     expect(3, height);
2022
2023     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 0, &color);
2024     expect(Ok, stat);
2025     expect(0xff00ffff, color);
2026
2027     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 1, 0, &color);
2028     expect(Ok, stat);
2029     expect(0xffff0000, color);
2030
2031     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 2, &color);
2032     expect(Ok, stat);
2033     expect(0xffffff00, color);
2034
2035     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 1, 2, &color);
2036     expect(Ok, stat);
2037     expect(0xff0000ff, color);
2038
2039     expect(0, bits[0]);
2040     expect(0, bits[1]);
2041     expect(0xff, bits[2]);
2042
2043     GdipDisposeImage(bitmap);
2044
2045     memcpy(bits, orig_bits, sizeof(bits));
2046     stat = GdipCreateBitmapFromScan0(3, 2, 12, PixelFormat24bppRGB, bits, (GpBitmap**)&bitmap);
2047     expect(Ok, stat);
2048
2049     stat = GdipImageRotateFlip(bitmap, RotateNoneFlipX);
2050     expect(Ok, stat);
2051
2052     stat = GdipGetImageWidth(bitmap, &width);
2053     expect(Ok, stat);
2054     stat = GdipGetImageHeight(bitmap, &height);
2055     expect(Ok, stat);
2056     expect(3, width);
2057     expect(2, height);
2058
2059     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 0, &color);
2060     expect(Ok, stat);
2061     expect(0xff0000ff, color);
2062
2063     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 2, 0, &color);
2064     expect(Ok, stat);
2065     expect(0xffff0000, color);
2066
2067     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 1, &color);
2068     expect(Ok, stat);
2069     expect(0xffffff00, color);
2070
2071     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 2, 1, &color);
2072     expect(Ok, stat);
2073     expect(0xff00ffff, color);
2074
2075     expect(0, bits[0]);
2076     expect(0, bits[1]);
2077     expect(0xff, bits[2]);
2078
2079     GdipDisposeImage(bitmap);
2080
2081     memcpy(bits, orig_bits, sizeof(bits));
2082     stat = GdipCreateBitmapFromScan0(3, 2, 12, PixelFormat24bppRGB, bits, (GpBitmap**)&bitmap);
2083     expect(Ok, stat);
2084
2085     stat = GdipImageRotateFlip(bitmap, RotateNoneFlipY);
2086     expect(Ok, stat);
2087
2088     stat = GdipGetImageWidth(bitmap, &width);
2089     expect(Ok, stat);
2090     stat = GdipGetImageHeight(bitmap, &height);
2091     expect(Ok, stat);
2092     expect(3, width);
2093     expect(2, height);
2094
2095     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 0, &color);
2096     expect(Ok, stat);
2097     expect(0xff00ffff, color);
2098
2099     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 2, 0, &color);
2100     expect(Ok, stat);
2101     expect(0xffffff00, color);
2102
2103     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 1, &color);
2104     expect(Ok, stat);
2105     expect(0xffff0000, color);
2106
2107     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 2, 1, &color);
2108     expect(Ok, stat);
2109     expect(0xff0000ff, color);
2110
2111     expect(0, bits[0]);
2112     expect(0, bits[1]);
2113     expect(0xff, bits[2]);
2114
2115     GdipDisposeImage(bitmap);
2116 }
2117
2118 static void test_remaptable(void)
2119 {
2120     GpStatus stat;
2121     GpImageAttributes *imageattr;
2122     GpBitmap *bitmap1, *bitmap2;
2123     GpGraphics *graphics;
2124     ARGB color;
2125     ColorMap *map;
2126
2127     map = GdipAlloc(sizeof(ColorMap));
2128
2129     map->oldColor.Argb = 0xff00ff00;
2130     map->newColor.Argb = 0xffff00ff;
2131
2132     stat = GdipSetImageAttributesRemapTable(NULL, ColorAdjustTypeDefault, TRUE, 1, map);
2133     expect(InvalidParameter, stat);
2134
2135     stat = GdipCreateImageAttributes(&imageattr);
2136     expect(Ok, stat);
2137
2138     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeDefault, TRUE, 1, NULL);
2139     expect(InvalidParameter, stat);
2140
2141     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeCount, TRUE, 1, map);
2142     expect(InvalidParameter, stat);
2143
2144     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeAny, TRUE, 1, map);
2145     expect(InvalidParameter, stat);
2146
2147     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeDefault, TRUE, 0, map);
2148     expect(InvalidParameter, stat);
2149
2150     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeDefault, FALSE, 0, NULL);
2151     expect(Ok, stat);
2152
2153     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeDefault, TRUE, 1, map);
2154     expect(Ok, stat);
2155
2156     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap1);
2157     expect(Ok, stat);
2158
2159     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap2);
2160     expect(Ok, stat);
2161
2162     stat = GdipBitmapSetPixel(bitmap1, 0, 0, 0xff00ff00);
2163     expect(Ok, stat);
2164
2165     stat = GdipGetImageGraphicsContext((GpImage*)bitmap2, &graphics);
2166     expect(Ok, stat);
2167
2168     stat = GdipDrawImageRectRectI(graphics, (GpImage*)bitmap1, 0,0,1,1, 0,0,1,1,
2169         UnitPixel, imageattr, NULL, NULL);
2170     expect(Ok, stat);
2171
2172     stat = GdipBitmapGetPixel(bitmap2, 0, 0, &color);
2173     expect(Ok, stat);
2174     ok(color_match(0xffff00ff, color, 1), "Expected ffff00ff, got %.8x\n", color);
2175
2176     GdipDeleteGraphics(graphics);
2177     GdipDisposeImage((GpImage*)bitmap1);
2178     GdipDisposeImage((GpImage*)bitmap2);
2179     GdipDisposeImageAttributes(imageattr);
2180     GdipFree(map);
2181 }
2182
2183 static void test_colorkey(void)
2184 {
2185     GpStatus stat;
2186     GpImageAttributes *imageattr;
2187     GpBitmap *bitmap1, *bitmap2;
2188     GpGraphics *graphics;
2189     ARGB color;
2190
2191     stat = GdipSetImageAttributesColorKeys(NULL, ColorAdjustTypeDefault, TRUE, 0xff405060, 0xff708090);
2192     expect(InvalidParameter, stat);
2193
2194     stat = GdipCreateImageAttributes(&imageattr);
2195     expect(Ok, stat);
2196
2197     stat = GdipSetImageAttributesColorKeys(imageattr, ColorAdjustTypeCount, TRUE, 0xff405060, 0xff708090);
2198     expect(InvalidParameter, stat);
2199
2200     stat = GdipSetImageAttributesColorKeys(imageattr, ColorAdjustTypeAny, TRUE, 0xff405060, 0xff708090);
2201     expect(InvalidParameter, stat);
2202
2203     stat = GdipSetImageAttributesColorKeys(imageattr, ColorAdjustTypeDefault, TRUE, 0xff405060, 0xff708090);
2204     expect(Ok, stat);
2205
2206     stat = GdipCreateBitmapFromScan0(2, 2, 0, PixelFormat32bppARGB, NULL, &bitmap1);
2207     expect(Ok, stat);
2208
2209     stat = GdipCreateBitmapFromScan0(2, 2, 0, PixelFormat32bppARGB, NULL, &bitmap2);
2210     expect(Ok, stat);
2211
2212     stat = GdipBitmapSetPixel(bitmap1, 0, 0, 0x20405060);
2213     expect(Ok, stat);
2214
2215     stat = GdipBitmapSetPixel(bitmap1, 0, 1, 0x40506070);
2216     expect(Ok, stat);
2217
2218     stat = GdipBitmapSetPixel(bitmap1, 1, 0, 0x60708090);
2219     expect(Ok, stat);
2220
2221     stat = GdipBitmapSetPixel(bitmap1, 1, 1, 0xffffffff);
2222     expect(Ok, stat);
2223
2224     stat = GdipGetImageGraphicsContext((GpImage*)bitmap2, &graphics);
2225     expect(Ok, stat);
2226
2227     stat = GdipDrawImageRectRectI(graphics, (GpImage*)bitmap1, 0,0,2,2, 0,0,2,2,
2228         UnitPixel, imageattr, NULL, NULL);
2229     expect(Ok, stat);
2230
2231     stat = GdipBitmapGetPixel(bitmap2, 0, 0, &color);
2232     expect(Ok, stat);
2233     ok(color_match(0x00000000, color, 1), "Expected ffff00ff, got %.8x\n", color);
2234
2235     stat = GdipBitmapGetPixel(bitmap2, 0, 1, &color);
2236     expect(Ok, stat);
2237     ok(color_match(0x00000000, color, 1), "Expected ffff00ff, got %.8x\n", color);
2238
2239     stat = GdipBitmapGetPixel(bitmap2, 1, 0, &color);
2240     expect(Ok, stat);
2241     ok(color_match(0x00000000, color, 1), "Expected ffff00ff, got %.8x\n", color);
2242
2243     stat = GdipBitmapGetPixel(bitmap2, 1, 1, &color);
2244     expect(Ok, stat);
2245     ok(color_match(0xffffffff, color, 1), "Expected ffff00ff, got %.8x\n", color);
2246
2247     GdipDeleteGraphics(graphics);
2248     GdipDisposeImage((GpImage*)bitmap1);
2249     GdipDisposeImage((GpImage*)bitmap2);
2250     GdipDisposeImageAttributes(imageattr);
2251 }
2252
2253 static void test_dispose(void)
2254 {
2255     GpStatus stat;
2256     GpImage *image;
2257     char invalid_image[256];
2258
2259     stat = GdipDisposeImage(NULL);
2260     expect(InvalidParameter, stat);
2261
2262     stat = GdipCreateBitmapFromScan0(2, 2, 0, PixelFormat32bppARGB, NULL, (GpBitmap**)&image);
2263     expect(Ok, stat);
2264
2265     stat = GdipDisposeImage(image);
2266     expect(Ok, stat);
2267
2268     stat = GdipDisposeImage(image);
2269     expect(ObjectBusy, stat);
2270
2271     memset(invalid_image, 0, 256);
2272     stat = GdipDisposeImage((GpImage*)invalid_image);
2273     expect(ObjectBusy, stat);
2274 }
2275
2276 START_TEST(image)
2277 {
2278     struct GdiplusStartupInput gdiplusStartupInput;
2279     ULONG_PTR gdiplusToken;
2280
2281     gdiplusStartupInput.GdiplusVersion              = 1;
2282     gdiplusStartupInput.DebugEventCallback          = NULL;
2283     gdiplusStartupInput.SuppressBackgroundThread    = 0;
2284     gdiplusStartupInput.SuppressExternalCodecs      = 0;
2285
2286     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
2287
2288     test_Scan0();
2289     test_FromGdiDib();
2290     test_GetImageDimension();
2291     test_GdipImageGetFrameDimensionsCount();
2292     test_LoadingImages();
2293     test_SavingImages();
2294     test_encoders();
2295     test_LockBits();
2296     test_GdipCreateBitmapFromHBITMAP();
2297     test_GdipGetImageFlags();
2298     test_GdipCloneImage();
2299     test_testcontrol();
2300     test_fromhicon();
2301     test_getrawformat();
2302     test_loadwmf();
2303     test_createfromwmf();
2304     test_resolution();
2305     test_createhbitmap();
2306     test_getthumbnail();
2307     test_getsetpixel();
2308     test_palette();
2309     test_colormatrix();
2310     test_gamma();
2311     test_multiframegif();
2312     test_rotateflip();
2313     test_remaptable();
2314     test_colorkey();
2315     test_dispose();
2316
2317     GdiplusShutdown(gdiplusToken);
2318 }