d3dcompiler: Add argument check in D3DReflect().
[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     ARGB color;
520
521     bm = NULL;
522     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
523     expect(Ok, stat);
524
525     rect.X = 2;
526     rect.Y = 3;
527     rect.Width = 4;
528     rect.Height = 5;
529
530     stat = GdipBitmapSetPixel(bm, 2, 3, 0xffc30000);
531     expect(Ok, stat);
532
533     stat = GdipBitmapSetPixel(bm, 2, 8, 0xff480000);
534     expect(Ok, stat);
535
536     /* read-only */
537     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
538     expect(Ok, stat);
539
540     if (stat == Ok) {
541         expect(0xc3, ((BYTE*)bd.Scan0)[2]);
542         expect(0x48, ((BYTE*)bd.Scan0)[2 + bd.Stride * 5]);
543
544         ((char*)bd.Scan0)[2] = 0xff;
545
546         stat = GdipBitmapUnlockBits(bm, &bd);
547         expect(Ok, stat);
548     }
549
550     stat = GdipBitmapGetPixel(bm, 2, 3, &color);
551     expect(Ok, stat);
552     expect(0xffff0000, color);
553
554     stat = GdipBitmapSetPixel(bm, 2, 3, 0xffc30000);
555     expect(Ok, stat);
556
557     /* read-only, with NULL rect -> whole bitmap lock */
558     stat = GdipBitmapLockBits(bm, NULL, ImageLockModeRead, PixelFormat24bppRGB, &bd);
559     expect(Ok, stat);
560     expect(bd.Width,  WIDTH);
561     expect(bd.Height, HEIGHT);
562
563     if (stat == Ok) {
564         ((char*)bd.Scan0)[2 + 2*3 + 3*bd.Stride] = 0xff;
565
566         stat = GdipBitmapUnlockBits(bm, &bd);
567         expect(Ok, stat);
568     }
569
570     stat = GdipBitmapGetPixel(bm, 2, 3, &color);
571     expect(Ok, stat);
572     expect(0xffff0000, color);
573
574     /* read-only, consecutive */
575     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
576     expect(Ok, stat);
577
578     if (stat == Ok) {
579         stat = GdipBitmapUnlockBits(bm, &bd);
580         expect(Ok, stat);
581     }
582
583     stat = GdipDisposeImage((GpImage*)bm);
584     expect(Ok, stat);
585     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
586     expect(Ok, stat);
587
588     /* read x2 */
589     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
590     expect(Ok, stat);
591     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
592     expect(WrongState, stat);
593
594     stat = GdipBitmapUnlockBits(bm, &bd);
595     expect(Ok, stat);
596
597     stat = GdipDisposeImage((GpImage*)bm);
598     expect(Ok, stat);
599     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
600     expect(Ok, stat);
601
602     stat = GdipBitmapSetPixel(bm, 2, 3, 0xffff0000);
603     expect(Ok, stat);
604
605     stat = GdipBitmapSetPixel(bm, 2, 8, 0xffc30000);
606     expect(Ok, stat);
607
608     /* write, no conversion */
609     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
610     expect(Ok, stat);
611
612     if (stat == Ok) {
613         /* all bits are readable, inside the rect or not */
614         expect(0xff, ((BYTE*)bd.Scan0)[2]);
615         expect(0xc3, ((BYTE*)bd.Scan0)[2 + bd.Stride * 5]);
616
617         stat = GdipBitmapUnlockBits(bm, &bd);
618         expect(Ok, stat);
619     }
620
621     /* read, conversion */
622     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat32bppARGB, &bd);
623     expect(Ok, stat);
624
625     if (stat == Ok) {
626         expect(0xff, ((BYTE*)bd.Scan0)[2]);
627         if (0)
628             /* Areas outside the rectangle appear to be uninitialized */
629             ok(0xc3 != ((BYTE*)bd.Scan0)[2 + bd.Stride * 5], "original image bits are readable\n");
630
631         ((BYTE*)bd.Scan0)[2] = 0xc3;
632
633         stat = GdipBitmapUnlockBits(bm, &bd);
634         expect(Ok, stat);
635     }
636
637     /* writes do not work in read mode if there was a conversion */
638     stat = GdipBitmapGetPixel(bm, 2, 3, &color);
639     expect(Ok, stat);
640     expect(0xffff0000, color);
641
642     /* read/write, conversion */
643     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead|ImageLockModeWrite, PixelFormat32bppARGB, &bd);
644     expect(Ok, stat);
645
646     if (stat == Ok) {
647         expect(0xff, ((BYTE*)bd.Scan0)[2]);
648         if (0)
649             /* Areas outside the rectangle appear to be uninitialized */
650             ok(0xc3 != ((BYTE*)bd.Scan0)[2 + bd.Stride * 5], "original image bits are readable\n");
651
652         stat = GdipBitmapUnlockBits(bm, &bd);
653         expect(Ok, stat);
654     }
655
656     /* write, conversion */
657     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &bd);
658     expect(Ok, stat);
659
660     if (stat == Ok) {
661         if (0)
662         {
663             /* This is completely uninitialized. */
664             ok(0xff != ((BYTE*)bd.Scan0)[2], "original image bits are readable\n");
665             ok(0xc3 != ((BYTE*)bd.Scan0)[2 + bd.Stride * 5], "original image bits are readable\n");
666         }
667
668         stat = GdipBitmapUnlockBits(bm, &bd);
669         expect(Ok, stat);
670     }
671
672     stat = GdipDisposeImage((GpImage*)bm);
673     expect(Ok, stat);
674     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
675     expect(Ok, stat);
676
677     /* write, no modification */
678     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
679     expect(Ok, stat);
680
681     if (stat == Ok) {
682         stat = GdipBitmapUnlockBits(bm, &bd);
683         expect(Ok, stat);
684     }
685
686     /* write, consecutive */
687     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
688     expect(Ok, stat);
689
690     if (stat == Ok) {
691         stat = GdipBitmapUnlockBits(bm, &bd);
692         expect(Ok, stat);
693     }
694
695     stat = GdipDisposeImage((GpImage*)bm);
696     expect(Ok, stat);
697     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
698     expect(Ok, stat);
699
700     /* write, modify */
701     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
702     expect(Ok, stat);
703
704     if (stat == Ok) {
705         if (bd.Scan0)
706             ((char*)bd.Scan0)[2] = 0xff;
707
708         stat = GdipBitmapUnlockBits(bm, &bd);
709         expect(Ok, stat);
710     }
711
712     stat = GdipBitmapGetPixel(bm, 2, 3, &color);
713     expect(Ok, stat);
714     expect(0xffff0000, color);
715
716     stat = GdipDisposeImage((GpImage*)bm);
717     expect(Ok, stat);
718
719     /* dispose locked */
720     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
721     expect(Ok, stat);
722     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
723     expect(Ok, stat);
724     stat = GdipDisposeImage((GpImage*)bm);
725     expect(Ok, stat);
726 }
727
728 static void test_LockBits_UserBuf(void)
729 {
730     GpStatus stat;
731     GpBitmap *bm;
732     GpRect rect;
733     BitmapData bd;
734     const INT WIDTH = 10, HEIGHT = 20;
735     DWORD bits[200];
736     ARGB color;
737
738     bm = NULL;
739     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat32bppARGB, NULL, &bm);
740     expect(Ok, stat);
741
742     memset(bits, 0xaa, sizeof(bits));
743
744     rect.X = 2;
745     rect.Y = 3;
746     rect.Width = 4;
747     rect.Height = 5;
748
749     bd.Width = 4;
750     bd.Height = 6;
751     bd.Stride = WIDTH * 4;
752     bd.PixelFormat = PixelFormat32bppARGB;
753     bd.Scan0 = &bits[2+3*WIDTH];
754     bd.Reserved = 0xaaaaaaaa;
755
756     /* read-only */
757     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead|ImageLockModeUserInputBuf, PixelFormat32bppARGB, &bd);
758     todo_wine expect(Ok, stat);
759
760     expect(0xaaaaaaaa, bits[0]);
761     todo_wine expect(0, bits[2+3*WIDTH]);
762
763     bits[2+3*WIDTH] = 0xdeadbeef;
764
765     if (stat == Ok) {
766         stat = GdipBitmapUnlockBits(bm, &bd);
767         expect(Ok, stat);
768     }
769
770     stat = GdipBitmapGetPixel(bm, 2, 3, &color);
771     expect(Ok, stat);
772     expect(0, color);
773
774     /* write-only */
775     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite|ImageLockModeUserInputBuf, PixelFormat32bppARGB, &bd);
776     todo_wine expect(Ok, stat);
777
778     expect(0xdeadbeef, bits[2+3*WIDTH]);
779     bits[2+3*WIDTH] = 0x12345678;
780
781     if (stat == Ok) {
782         stat = GdipBitmapUnlockBits(bm, &bd);
783         expect(Ok, stat);
784     }
785
786     stat = GdipBitmapGetPixel(bm, 2, 3, &color);
787     expect(Ok, stat);
788     todo_wine expect(0x12345678, color);
789
790     bits[2+3*WIDTH] = 0;
791
792     /* read/write */
793     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead|ImageLockModeWrite|ImageLockModeUserInputBuf, PixelFormat32bppARGB, &bd);
794     todo_wine expect(Ok, stat);
795
796     todo_wine expect(0x12345678, bits[2+3*WIDTH]);
797     bits[2+3*WIDTH] = 0xdeadbeef;
798
799     if (stat == Ok) {
800         stat = GdipBitmapUnlockBits(bm, &bd);
801         expect(Ok, stat);
802     }
803
804     stat = GdipBitmapGetPixel(bm, 2, 3, &color);
805     expect(Ok, stat);
806     todo_wine expect(0xdeadbeef, color);
807
808     stat = GdipDisposeImage((GpImage*)bm);
809     expect(Ok, stat);
810 }
811
812 static void test_GdipCreateBitmapFromHBITMAP(void)
813 {
814     GpBitmap* gpbm = NULL;
815     HBITMAP hbm = NULL;
816     HPALETTE hpal = NULL;
817     GpStatus stat;
818     BYTE buff[1000];
819     LOGPALETTE* LogPal = NULL;
820     REAL width, height;
821     const REAL WIDTH1 = 5;
822     const REAL HEIGHT1 = 15;
823     const REAL WIDTH2 = 10;
824     const REAL HEIGHT2 = 20;
825     HDC hdc;
826     BITMAPINFO bmi;
827     BYTE *bits;
828
829     stat = GdipCreateBitmapFromHBITMAP(NULL, NULL, NULL);
830     expect(InvalidParameter, stat);
831
832     hbm = CreateBitmap(WIDTH1, HEIGHT1, 1, 1, NULL);
833     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, NULL);
834     expect(InvalidParameter, stat);
835
836     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
837     expect(Ok, stat);
838     expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
839     expectf(WIDTH1,  width);
840     expectf(HEIGHT1, height);
841     if (stat == Ok)
842         GdipDisposeImage((GpImage*)gpbm);
843     DeleteObject(hbm);
844
845     memset(buff, 0, sizeof(buff));
846     hbm = CreateBitmap(WIDTH2, HEIGHT2, 1, 1, &buff);
847     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
848     expect(Ok, stat);
849     /* raw format */
850     expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)gpbm, __LINE__, FALSE);
851
852     expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
853     expectf(WIDTH2,  width);
854     expectf(HEIGHT2, height);
855     if (stat == Ok)
856         GdipDisposeImage((GpImage*)gpbm);
857     DeleteObject(hbm);
858
859     hdc = CreateCompatibleDC(0);
860     ok(hdc != NULL, "CreateCompatibleDC failed\n");
861     bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
862     bmi.bmiHeader.biHeight = HEIGHT1;
863     bmi.bmiHeader.biWidth = WIDTH1;
864     bmi.bmiHeader.biBitCount = 24;
865     bmi.bmiHeader.biPlanes = 1;
866     bmi.bmiHeader.biCompression = BI_RGB;
867
868     hbm = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
869     ok(hbm != NULL, "CreateDIBSection failed\n");
870
871     bits[0] = 0;
872
873     stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, &gpbm);
874     expect(Ok, stat);
875     expect(Ok, GdipGetImageDimension((GpImage*) gpbm, &width, &height));
876     expectf(WIDTH1,  width);
877     expectf(HEIGHT1, height);
878     if (stat == Ok)
879     {
880         /* test whether writing to the bitmap affects the original */
881         stat = GdipBitmapSetPixel(gpbm, 0, 0, 0xffffffff);
882         expect(Ok, stat);
883
884         expect(0, bits[0]);
885
886         GdipDisposeImage((GpImage*)gpbm);
887     }
888
889     LogPal = GdipAlloc(sizeof(LOGPALETTE));
890     ok(LogPal != NULL, "unable to allocate LOGPALETTE\n");
891     LogPal->palVersion = 0x300;
892     LogPal->palNumEntries = 1;
893     hpal = CreatePalette(LogPal);
894     ok(hpal != NULL, "CreatePalette failed\n");
895     GdipFree(LogPal);
896
897     stat = GdipCreateBitmapFromHBITMAP(hbm, hpal, &gpbm);
898     todo_wine
899     {
900         expect(Ok, stat);
901     }
902     if (stat == Ok)
903         GdipDisposeImage((GpImage*)gpbm);
904
905     DeleteObject(hpal);
906     DeleteObject(hbm);
907 }
908
909 static void test_GdipGetImageFlags(void)
910 {
911     GpImage *img;
912     GpStatus stat;
913     UINT flags;
914
915     img = (GpImage*)0xdeadbeef;
916
917     stat = GdipGetImageFlags(NULL, NULL);
918     expect(InvalidParameter, stat);
919
920     stat = GdipGetImageFlags(NULL, &flags);
921     expect(InvalidParameter, stat);
922
923     stat = GdipGetImageFlags(img, NULL);
924     expect(InvalidParameter, stat);
925
926     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat1bppIndexed, NULL, (GpBitmap**)&img);
927     expect(Ok, stat);
928     stat = GdipGetImageFlags(img, &flags);
929     expect(Ok, stat);
930     expect(ImageFlagsHasAlpha, flags);
931     GdipDisposeImage(img);
932
933     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat4bppIndexed, NULL, (GpBitmap**)&img);
934     expect(Ok, stat);
935     stat = GdipGetImageFlags(img, &flags);
936     expect(Ok, stat);
937     expect(ImageFlagsHasAlpha, flags);
938     GdipDisposeImage(img);
939
940     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat8bppIndexed, NULL, (GpBitmap**)&img);
941     expect(Ok, stat);
942     stat = GdipGetImageFlags(img, &flags);
943     expect(Ok, stat);
944     expect(ImageFlagsHasAlpha, flags);
945     GdipDisposeImage(img);
946
947     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat16bppGrayScale, NULL, (GpBitmap**)&img);
948     expect(Ok, stat);
949     stat = GdipGetImageFlags(img, &flags);
950     expect(Ok, stat);
951     expect(ImageFlagsNone, flags);
952     GdipDisposeImage(img);
953
954     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat16bppRGB555, NULL, (GpBitmap**)&img);
955     expect(Ok, stat);
956     stat = GdipGetImageFlags(img, &flags);
957     expect(Ok, stat);
958     expect(ImageFlagsNone, flags);
959     GdipDisposeImage(img);
960
961     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat16bppRGB565, NULL, (GpBitmap**)&img);
962     expect(Ok, stat);
963     stat = GdipGetImageFlags(img, &flags);
964     expect(Ok, stat);
965     expect(ImageFlagsNone, flags);
966     GdipDisposeImage(img);
967
968     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat16bppARGB1555, NULL, (GpBitmap**)&img);
969     expect(Ok, stat);
970     stat = GdipGetImageFlags(img, &flags);
971     expect(Ok, stat);
972     expect(ImageFlagsHasAlpha, flags);
973     GdipDisposeImage(img);
974
975     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, NULL, (GpBitmap**)&img);
976     expect(Ok, stat);
977     stat = GdipGetImageFlags(img, &flags);
978     expect(Ok, stat);
979     expect(ImageFlagsNone, flags);
980     GdipDisposeImage(img);
981
982     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat32bppRGB, NULL, (GpBitmap**)&img);
983     expect(Ok, stat);
984     stat = GdipGetImageFlags(img, &flags);
985     expect(Ok, stat);
986     expect(ImageFlagsNone, flags);
987     GdipDisposeImage(img);
988
989     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat32bppARGB, NULL, (GpBitmap**)&img);
990     expect(Ok, stat);
991     stat = GdipGetImageFlags(img, &flags);
992     expect(Ok, stat);
993     expect(ImageFlagsHasAlpha, flags);
994     GdipDisposeImage(img);
995
996     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat32bppPARGB, NULL, (GpBitmap**)&img);
997     expect(Ok, stat);
998     stat = GdipGetImageFlags(img, &flags);
999     expect(Ok, stat);
1000     expect(ImageFlagsHasAlpha, flags);
1001     GdipDisposeImage(img);
1002
1003     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat48bppRGB, NULL, (GpBitmap**)&img);
1004     expect(Ok, stat);
1005     if (stat == Ok)
1006     {
1007         stat = GdipGetImageFlags(img, &flags);
1008         expect(Ok, stat);
1009         expect(ImageFlagsNone, flags);
1010         GdipDisposeImage(img);
1011     }
1012
1013     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat64bppARGB, NULL, (GpBitmap**)&img);
1014     expect(Ok, stat);
1015     if (stat == Ok)
1016     {
1017         expect(Ok, stat);
1018         stat = GdipGetImageFlags(img, &flags);
1019         expect(Ok, stat);
1020         expect(ImageFlagsHasAlpha, flags);
1021         GdipDisposeImage(img);
1022     }
1023
1024     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat64bppPARGB, NULL, (GpBitmap**)&img);
1025     expect(Ok, stat);
1026     if (stat == Ok)
1027     {
1028         expect(Ok, stat);
1029         stat = GdipGetImageFlags(img, &flags);
1030         expect(Ok, stat);
1031         expect(ImageFlagsHasAlpha, flags);
1032         GdipDisposeImage(img);
1033     }
1034 }
1035
1036 static void test_GdipCloneImage(void)
1037 {
1038     GpStatus stat;
1039     GpRectF rectF;
1040     GpUnit unit;
1041     GpBitmap *bm;
1042     GpImage *image_src, *image_dest = NULL;
1043     const INT WIDTH = 10, HEIGHT = 20;
1044
1045     /* Create an image, clone it, delete the original, make sure the copy works */
1046     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
1047     expect(Ok, stat);
1048     expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bm, __LINE__, FALSE);
1049
1050     image_src = ((GpImage*)bm);
1051     stat = GdipCloneImage(image_src, &image_dest);
1052     expect(Ok, stat);
1053     expect_rawformat(&ImageFormatMemoryBMP, image_dest, __LINE__, FALSE);
1054
1055     stat = GdipDisposeImage((GpImage*)bm);
1056     expect(Ok, stat);
1057     stat = GdipGetImageBounds(image_dest, &rectF, &unit);
1058     expect(Ok, stat);
1059
1060     /* Treat FP values carefully */
1061     expectf((REAL)WIDTH, rectF.Width);
1062     expectf((REAL)HEIGHT, rectF.Height);
1063
1064     stat = GdipDisposeImage(image_dest);
1065     expect(Ok, stat);
1066 }
1067
1068 static void test_testcontrol(void)
1069 {
1070     GpStatus stat;
1071     DWORD param;
1072
1073     param = 0;
1074     stat = GdipTestControl(TestControlGetBuildNumber, &param);
1075     expect(Ok, stat);
1076     ok(param != 0, "Build number expected, got %u\n", param);
1077 }
1078
1079 static void test_fromhicon(void)
1080 {
1081     static const BYTE bmp_bits[1024];
1082     HBITMAP hbmMask, hbmColor;
1083     ICONINFO info;
1084     HICON hIcon;
1085     GpStatus stat;
1086     GpBitmap *bitmap = NULL;
1087     UINT dim;
1088     ImageType type;
1089     PixelFormat format;
1090
1091     /* NULL */
1092     stat = GdipCreateBitmapFromHICON(NULL, NULL);
1093     expect(InvalidParameter, stat);
1094     stat = GdipCreateBitmapFromHICON(NULL, &bitmap);
1095     expect(InvalidParameter, stat);
1096
1097     /* color icon 1 bit */
1098     hbmMask = CreateBitmap(16, 16, 1, 1, bmp_bits);
1099     ok(hbmMask != 0, "CreateBitmap failed\n");
1100     hbmColor = CreateBitmap(16, 16, 1, 1, bmp_bits);
1101     ok(hbmColor != 0, "CreateBitmap failed\n");
1102     info.fIcon = TRUE;
1103     info.xHotspot = 8;
1104     info.yHotspot = 8;
1105     info.hbmMask = hbmMask;
1106     info.hbmColor = hbmColor;
1107     hIcon = CreateIconIndirect(&info);
1108     ok(hIcon != 0, "CreateIconIndirect failed\n");
1109     DeleteObject(hbmMask);
1110     DeleteObject(hbmColor);
1111
1112     stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
1113     ok(stat == Ok ||
1114        broken(stat == InvalidParameter), /* Win98 */
1115        "Expected Ok, got %.8x\n", stat);
1116     if(stat == Ok){
1117        /* check attributes */
1118        stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
1119        expect(Ok, stat);
1120        expect(16, dim);
1121        stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
1122        expect(Ok, stat);
1123        expect(16, dim);
1124        stat = GdipGetImageType((GpImage*)bitmap, &type);
1125        expect(Ok, stat);
1126        expect(ImageTypeBitmap, type);
1127        stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
1128        expect(Ok, stat);
1129        expect(PixelFormat32bppARGB, format);
1130        /* raw format */
1131        expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bitmap, __LINE__, FALSE);
1132        GdipDisposeImage((GpImage*)bitmap);
1133     }
1134     DestroyIcon(hIcon);
1135
1136     /* color icon 8 bpp */
1137     hbmMask = CreateBitmap(16, 16, 1, 8, bmp_bits);
1138     ok(hbmMask != 0, "CreateBitmap failed\n");
1139     hbmColor = CreateBitmap(16, 16, 1, 8, bmp_bits);
1140     ok(hbmColor != 0, "CreateBitmap failed\n");
1141     info.fIcon = TRUE;
1142     info.xHotspot = 8;
1143     info.yHotspot = 8;
1144     info.hbmMask = hbmMask;
1145     info.hbmColor = hbmColor;
1146     hIcon = CreateIconIndirect(&info);
1147     ok(hIcon != 0, "CreateIconIndirect failed\n");
1148     DeleteObject(hbmMask);
1149     DeleteObject(hbmColor);
1150
1151     stat = GdipCreateBitmapFromHICON(hIcon, &bitmap);
1152     expect(Ok, stat);
1153     if(stat == Ok){
1154         /* check attributes */
1155         stat = GdipGetImageHeight((GpImage*)bitmap, &dim);
1156         expect(Ok, stat);
1157         expect(16, dim);
1158         stat = GdipGetImageWidth((GpImage*)bitmap, &dim);
1159         expect(Ok, stat);
1160         expect(16, dim);
1161         stat = GdipGetImageType((GpImage*)bitmap, &type);
1162         expect(Ok, stat);
1163         expect(ImageTypeBitmap, type);
1164         stat = GdipGetImagePixelFormat((GpImage*)bitmap, &format);
1165         expect(Ok, stat);
1166         expect(PixelFormat32bppARGB, format);
1167         /* raw format */
1168         expect_rawformat(&ImageFormatMemoryBMP, (GpImage*)bitmap, __LINE__, FALSE);
1169         GdipDisposeImage((GpImage*)bitmap);
1170     }
1171     DestroyIcon(hIcon);
1172 }
1173
1174 /* 1x1 pixel png */
1175 static const unsigned char pngimage[285] = {
1176 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
1177 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
1178 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
1179 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
1180 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
1181 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
1182 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
1183 };
1184 /* 1x1 pixel gif */
1185 static const unsigned char gifimage[35] = {
1186 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
1187 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
1188 0x01,0x00,0x3b
1189 };
1190 /* 1x1 pixel bmp */
1191 static const unsigned char bmpimage[66] = {
1192 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
1193 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
1194 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
1195 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
1196 0x00,0x00
1197 };
1198 /* 1x1 pixel jpg */
1199 static const unsigned char jpgimage[285] = {
1200 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2c,
1201 0x01,0x2c,0x00,0x00,0xff,0xdb,0x00,0x43,0x00,0x05,0x03,0x04,0x04,0x04,0x03,0x05,
1202 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x07,0x0c,0x08,0x07,0x07,0x07,0x07,0x0f,0x0b,
1203 0x0b,0x09,0x0c,0x11,0x0f,0x12,0x12,0x11,0x0f,0x11,0x11,0x13,0x16,0x1c,0x17,0x13,
1204 0x14,0x1a,0x15,0x11,0x11,0x18,0x21,0x18,0x1a,0x1d,0x1d,0x1f,0x1f,0x1f,0x13,0x17,
1205 0x22,0x24,0x22,0x1e,0x24,0x1c,0x1e,0x1f,0x1e,0xff,0xdb,0x00,0x43,0x01,0x05,0x05,
1206 0x05,0x07,0x06,0x07,0x0e,0x08,0x08,0x0e,0x1e,0x14,0x11,0x14,0x1e,0x1e,0x1e,0x1e,
1207 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
1208 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
1209 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0xff,0xc0,
1210 0x00,0x11,0x08,0x00,0x01,0x00,0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,
1211 0x01,0xff,0xc4,0x00,0x15,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1212 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xff,0xc4,0x00,0x14,0x10,0x01,0x00,0x00,
1213 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xc4,
1214 0x00,0x14,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1215 0x00,0x00,0x00,0x00,0xff,0xc4,0x00,0x14,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
1216 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xda,0x00,0x0c,0x03,0x01,
1217 0x00,0x02,0x11,0x03,0x11,0x00,0x3f,0x00,0xb2,0xc0,0x07,0xff,0xd9
1218 };
1219 /* 1x1 pixel tiff */
1220 static const unsigned char tiffimage[] = {
1221 0x49,0x49,0x2a,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0xfe,0x00,
1222 0x04,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x00,0x01,0x00,
1223 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,
1224 0x00,0x00,0x02,0x01,0x03,0x00,0x03,0x00,0x00,0x00,0xd2,0x00,0x00,0x00,0x03,0x01,
1225 0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x06,0x01,0x03,0x00,0x01,0x00,
1226 0x00,0x00,0x02,0x00,0x00,0x00,0x0d,0x01,0x02,0x00,0x1b,0x00,0x00,0x00,0xd8,0x00,
1227 0x00,0x00,0x11,0x01,0x04,0x00,0x01,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x12,0x01,
1228 0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x15,0x01,0x03,0x00,0x01,0x00,
1229 0x00,0x00,0x03,0x00,0x00,0x00,0x16,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x40,0x00,
1230 0x00,0x00,0x17,0x01,0x04,0x00,0x01,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x1a,0x01,
1231 0x05,0x00,0x01,0x00,0x00,0x00,0xf4,0x00,0x00,0x00,0x1b,0x01,0x05,0x00,0x01,0x00,
1232 0x00,0x00,0xfc,0x00,0x00,0x00,0x1c,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,
1233 0x00,0x00,0x28,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,
1234 0x00,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x2f,0x68,0x6f,0x6d,0x65,0x2f,0x6d,0x65,
1235 0x68,0x2f,0x44,0x65,0x73,0x6b,0x74,0x6f,0x70,0x2f,0x74,0x65,0x73,0x74,0x2e,0x74,
1236 0x69,0x66,0x00,0x00,0x00,0x00,0x00,0x48,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x48,
1237 0x00,0x00,0x00,0x01
1238 };
1239 /* 320x320 twip wmf */
1240 static const unsigned char wmfimage[180] = {
1241 0xd7,0xcd,0xc6,0x9a,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x01,0x40,0x01,0xa0,0x05,
1242 0x00,0x00,0x00,0x00,0xb1,0x52,0x01,0x00,0x09,0x00,0x00,0x03,0x4f,0x00,0x00,0x00,
1243 0x0f,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0b,0x02,0x00,0x00,
1244 0x00,0x00,0x05,0x00,0x00,0x00,0x0c,0x02,0x40,0x01,0x40,0x01,0x04,0x00,0x00,0x00,
1245 0x02,0x01,0x01,0x00,0x04,0x00,0x00,0x00,0x04,0x01,0x0d,0x00,0x08,0x00,0x00,0x00,
1246 0xfa,0x02,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
1247 0x2d,0x01,0x00,0x00,0x07,0x00,0x00,0x00,0xfc,0x02,0x01,0x00,0x00,0x00,0x00,0x00,
1248 0x00,0x00,0x04,0x00,0x00,0x00,0x2d,0x01,0x01,0x00,0x07,0x00,0x00,0x00,0xfc,0x02,
1249 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x2d,0x01,0x02,0x00,
1250 0x07,0x00,0x00,0x00,0x1b,0x04,0x40,0x01,0x40,0x01,0x00,0x00,0x00,0x00,0x04,0x00,
1251 0x00,0x00,0xf0,0x01,0x00,0x00,0x04,0x00,0x00,0x00,0xf0,0x01,0x01,0x00,0x03,0x00,
1252 0x00,0x00,0x00,0x00
1253 };
1254 static void test_getrawformat(void)
1255 {
1256     test_bufferrawformat((void*)pngimage, sizeof(pngimage), &ImageFormatPNG,  __LINE__, FALSE);
1257     test_bufferrawformat((void*)gifimage, sizeof(gifimage), &ImageFormatGIF,  __LINE__, FALSE);
1258     test_bufferrawformat((void*)bmpimage, sizeof(bmpimage), &ImageFormatBMP,  __LINE__, FALSE);
1259     test_bufferrawformat((void*)jpgimage, sizeof(jpgimage), &ImageFormatJPEG, __LINE__, FALSE);
1260     test_bufferrawformat((void*)tiffimage, sizeof(tiffimage), &ImageFormatTIFF, __LINE__, FALSE);
1261     test_bufferrawformat((void*)wmfimage, sizeof(wmfimage), &ImageFormatWMF, __LINE__, FALSE);
1262 }
1263
1264 static void test_loadwmf(void)
1265 {
1266     LPSTREAM stream;
1267     HGLOBAL  hglob;
1268     LPBYTE   data;
1269     HRESULT  hres;
1270     GpStatus stat;
1271     GpImage *img;
1272     GpRectF bounds;
1273     GpUnit unit;
1274     REAL res = 12345.0;
1275     MetafileHeader header;
1276
1277     hglob = GlobalAlloc (0, sizeof(wmfimage));
1278     data = GlobalLock (hglob);
1279     memcpy(data, wmfimage, sizeof(wmfimage));
1280     GlobalUnlock(hglob); data = NULL;
1281
1282     hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
1283     ok(hres == S_OK, "Failed to create a stream\n");
1284     if(hres != S_OK) return;
1285
1286     stat = GdipLoadImageFromStream(stream, &img);
1287     ok(stat == Ok, "Failed to create a Bitmap\n");
1288     if(stat != Ok){
1289         IStream_Release(stream);
1290         return;
1291     }
1292
1293     IStream_Release(stream);
1294
1295     stat = GdipGetImageBounds(img, &bounds, &unit);
1296     expect(Ok, stat);
1297     todo_wine expect(UnitPixel, unit);
1298     expectf(0.0, bounds.X);
1299     expectf(0.0, bounds.Y);
1300     todo_wine expectf(320.0, bounds.Width);
1301     todo_wine expectf(320.0, bounds.Height);
1302
1303     stat = GdipGetImageHorizontalResolution(img, &res);
1304     expect(Ok, stat);
1305     todo_wine expectf(1440.0, res);
1306
1307     stat = GdipGetImageVerticalResolution(img, &res);
1308     expect(Ok, stat);
1309     todo_wine expectf(1440.0, res);
1310
1311     memset(&header, 0, sizeof(header));
1312     stat = GdipGetMetafileHeaderFromMetafile((GpMetafile*)img, &header);
1313     expect(Ok, stat);
1314     if (stat == Ok)
1315     {
1316         todo_wine expect(MetafileTypeWmfPlaceable, header.Type);
1317         todo_wine expect(sizeof(wmfimage)-sizeof(WmfPlaceableFileHeader), header.Size);
1318         todo_wine expect(0x300, header.Version);
1319         expect(0, header.EmfPlusFlags);
1320         todo_wine expectf(1440.0, header.DpiX);
1321         todo_wine expectf(1440.0, header.DpiY);
1322         expect(0, header.X);
1323         expect(0, header.Y);
1324         todo_wine expect(320, header.Width);
1325         todo_wine expect(320, header.Height);
1326         todo_wine expect(1, U(header).WmfHeader.mtType);
1327         expect(0, header.EmfPlusHeaderSize);
1328         expect(0, header.LogicalDpiX);
1329         expect(0, header.LogicalDpiY);
1330     }
1331
1332     GdipDisposeImage(img);
1333 }
1334
1335 static void test_createfromwmf(void)
1336 {
1337     HMETAFILE hwmf;
1338     GpImage *img;
1339     GpStatus stat;
1340     GpRectF bounds;
1341     GpUnit unit;
1342     REAL res = 12345.0;
1343     MetafileHeader header;
1344
1345     hwmf = SetMetaFileBitsEx(sizeof(wmfimage)-sizeof(WmfPlaceableFileHeader),
1346         wmfimage+sizeof(WmfPlaceableFileHeader));
1347     ok(hwmf != 0, "SetMetaFileBitsEx failed\n");
1348
1349     stat = GdipCreateMetafileFromWmf(hwmf, TRUE,
1350         (WmfPlaceableFileHeader*)wmfimage, (GpMetafile**)&img);
1351     expect(Ok, stat);
1352
1353     stat = GdipGetImageBounds(img, &bounds, &unit);
1354     expect(Ok, stat);
1355     expect(UnitPixel, unit);
1356     expectf(0.0, bounds.X);
1357     expectf(0.0, bounds.Y);
1358     expectf(320.0, bounds.Width);
1359     expectf(320.0, bounds.Height);
1360
1361     stat = GdipGetImageHorizontalResolution(img, &res);
1362     expect(Ok, stat);
1363     expectf(1440.0, res);
1364
1365     stat = GdipGetImageVerticalResolution(img, &res);
1366     expect(Ok, stat);
1367     expectf(1440.0, res);
1368
1369     memset(&header, 0, sizeof(header));
1370     stat = GdipGetMetafileHeaderFromMetafile((GpMetafile*)img, &header);
1371     expect(Ok, stat);
1372     if (stat == Ok)
1373     {
1374         todo_wine expect(MetafileTypeWmfPlaceable, header.Type);
1375         todo_wine expect(sizeof(wmfimage)-sizeof(WmfPlaceableFileHeader), header.Size);
1376         todo_wine expect(0x300, header.Version);
1377         expect(0, header.EmfPlusFlags);
1378         todo_wine expectf(1440.0, header.DpiX);
1379         todo_wine expectf(1440.0, header.DpiY);
1380         expect(0, header.X);
1381         expect(0, header.Y);
1382         todo_wine expect(320, header.Width);
1383         todo_wine expect(320, header.Height);
1384         todo_wine expect(1, U(header).WmfHeader.mtType);
1385         expect(0, header.EmfPlusHeaderSize);
1386         expect(0, header.LogicalDpiX);
1387         expect(0, header.LogicalDpiY);
1388     }
1389
1390     GdipDisposeImage(img);
1391 }
1392
1393 static void test_resolution(void)
1394 {
1395     GpStatus stat;
1396     GpBitmap *bitmap;
1397     REAL res=-1.0;
1398     HDC screendc;
1399     int screenxres, screenyres;
1400
1401     /* create Bitmap */
1402     stat = GdipCreateBitmapFromScan0(1, 1, 32, PixelFormat24bppRGB, NULL, &bitmap);
1403     expect(Ok, stat);
1404
1405     /* test invalid values */
1406     stat = GdipGetImageHorizontalResolution(NULL, &res);
1407     expect(InvalidParameter, stat);
1408
1409     stat = GdipGetImageHorizontalResolution((GpImage*)bitmap, NULL);
1410     expect(InvalidParameter, stat);
1411
1412     stat = GdipGetImageVerticalResolution(NULL, &res);
1413     expect(InvalidParameter, stat);
1414
1415     stat = GdipGetImageVerticalResolution((GpImage*)bitmap, NULL);
1416     expect(InvalidParameter, stat);
1417
1418     stat = GdipBitmapSetResolution(NULL, 96.0, 96.0);
1419     expect(InvalidParameter, stat);
1420
1421     stat = GdipBitmapSetResolution(bitmap, 0.0, 0.0);
1422     expect(InvalidParameter, stat);
1423
1424     /* defaults to screen resolution */
1425     screendc = GetDC(0);
1426
1427     screenxres = GetDeviceCaps(screendc, LOGPIXELSX);
1428     screenyres = GetDeviceCaps(screendc, LOGPIXELSY);
1429
1430     ReleaseDC(0, screendc);
1431
1432     stat = GdipGetImageHorizontalResolution((GpImage*)bitmap, &res);
1433     expect(Ok, stat);
1434     expectf((REAL)screenxres, res);
1435
1436     stat = GdipGetImageVerticalResolution((GpImage*)bitmap, &res);
1437     expect(Ok, stat);
1438     expectf((REAL)screenyres, res);
1439
1440     /* test changing the resolution */
1441     stat = GdipBitmapSetResolution(bitmap, screenxres*2.0, screenyres*3.0);
1442     expect(Ok, stat);
1443
1444     stat = GdipGetImageHorizontalResolution((GpImage*)bitmap, &res);
1445     expect(Ok, stat);
1446     expectf(screenxres*2.0, res);
1447
1448     stat = GdipGetImageVerticalResolution((GpImage*)bitmap, &res);
1449     expect(Ok, stat);
1450     expectf(screenyres*3.0, res);
1451
1452     stat = GdipDisposeImage((GpImage*)bitmap);
1453     expect(Ok, stat);
1454 }
1455
1456 static void test_createhbitmap(void)
1457 {
1458     GpStatus stat;
1459     GpBitmap *bitmap;
1460     HBITMAP hbitmap, oldhbitmap;
1461     BITMAP bm;
1462     int ret;
1463     HDC hdc;
1464     COLORREF pixel;
1465     BYTE bits[640];
1466
1467     memset(bits, 0x68, 640);
1468
1469     /* create Bitmap */
1470     stat = GdipCreateBitmapFromScan0(10, 20, 32, PixelFormat24bppRGB, bits, &bitmap);
1471     expect(Ok, stat);
1472
1473     /* test NULL values */
1474     stat = GdipCreateHBITMAPFromBitmap(NULL, &hbitmap, 0);
1475     expect(InvalidParameter, stat);
1476
1477     stat = GdipCreateHBITMAPFromBitmap(bitmap, NULL, 0);
1478     expect(InvalidParameter, stat);
1479
1480     /* create HBITMAP */
1481     stat = GdipCreateHBITMAPFromBitmap(bitmap, &hbitmap, 0);
1482     expect(Ok, stat);
1483
1484     if (stat == Ok)
1485     {
1486         ret = GetObjectA(hbitmap, sizeof(BITMAP), &bm);
1487         expect(sizeof(BITMAP), ret);
1488
1489         expect(0, bm.bmType);
1490         expect(10, bm.bmWidth);
1491         expect(20, bm.bmHeight);
1492         expect(40, bm.bmWidthBytes);
1493         expect(1, bm.bmPlanes);
1494         expect(32, bm.bmBitsPixel);
1495         ok(bm.bmBits != NULL, "got DDB, expected DIB\n");
1496
1497         hdc = CreateCompatibleDC(NULL);
1498
1499         oldhbitmap = SelectObject(hdc, hbitmap);
1500         pixel = GetPixel(hdc, 5, 5);
1501         SelectObject(hdc, oldhbitmap);
1502
1503         DeleteDC(hdc);
1504
1505         expect(0x686868, pixel);
1506
1507         DeleteObject(hbitmap);
1508     }
1509
1510     stat = GdipDisposeImage((GpImage*)bitmap);
1511     expect(Ok, stat);
1512 }
1513
1514 static void test_getthumbnail(void)
1515 {
1516     GpStatus stat;
1517     GpImage *bitmap1, *bitmap2;
1518     UINT width, height;
1519
1520     stat = GdipGetImageThumbnail(NULL, 0, 0, &bitmap2, NULL, NULL);
1521     expect(InvalidParameter, stat);
1522
1523     stat = GdipCreateBitmapFromScan0(128, 128, 0, PixelFormat32bppRGB, NULL, (GpBitmap**)&bitmap1);
1524     expect(Ok, stat);
1525
1526     stat = GdipGetImageThumbnail(bitmap1, 0, 0, NULL, NULL, NULL);
1527     expect(InvalidParameter, stat);
1528
1529     stat = GdipGetImageThumbnail(bitmap1, 0, 0, &bitmap2, NULL, NULL);
1530     expect(Ok, stat);
1531
1532     if (stat == Ok)
1533     {
1534         stat = GdipGetImageWidth(bitmap2, &width);
1535         expect(Ok, stat);
1536         expect(120, width);
1537
1538         stat = GdipGetImageHeight(bitmap2, &height);
1539         expect(Ok, stat);
1540         expect(120, height);
1541
1542         GdipDisposeImage(bitmap2);
1543     }
1544
1545     GdipDisposeImage(bitmap1);
1546
1547
1548     stat = GdipCreateBitmapFromScan0(64, 128, 0, PixelFormat32bppRGB, NULL, (GpBitmap**)&bitmap1);
1549     expect(Ok, stat);
1550
1551     stat = GdipGetImageThumbnail(bitmap1, 32, 32, &bitmap2, NULL, NULL);
1552     expect(Ok, stat);
1553
1554     if (stat == Ok)
1555     {
1556         stat = GdipGetImageWidth(bitmap2, &width);
1557         expect(Ok, stat);
1558         expect(32, width);
1559
1560         stat = GdipGetImageHeight(bitmap2, &height);
1561         expect(Ok, stat);
1562         expect(32, height);
1563
1564         GdipDisposeImage(bitmap2);
1565     }
1566
1567     stat = GdipGetImageThumbnail(bitmap1, 0, 0, &bitmap2, NULL, NULL);
1568     expect(Ok, stat);
1569
1570     if (stat == Ok)
1571     {
1572         stat = GdipGetImageWidth(bitmap2, &width);
1573         expect(Ok, stat);
1574         expect(120, width);
1575
1576         stat = GdipGetImageHeight(bitmap2, &height);
1577         expect(Ok, stat);
1578         expect(120, height);
1579
1580         GdipDisposeImage(bitmap2);
1581     }
1582
1583     GdipDisposeImage(bitmap1);
1584 }
1585
1586 static void test_getsetpixel(void)
1587 {
1588     GpStatus stat;
1589     GpBitmap *bitmap;
1590     ARGB color;
1591     BYTE bits[16] = {0x00,0x00,0x00,0x00, 0x00,0xff,0xff,0x00,
1592                      0xff,0x00,0x00,0x00, 0xff,0xff,0xff,0x00};
1593
1594     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat32bppRGB, bits, &bitmap);
1595     expect(Ok, stat);
1596
1597     /* null parameters */
1598     stat = GdipBitmapGetPixel(NULL, 1, 1, &color);
1599     expect(InvalidParameter, stat);
1600
1601     stat = GdipBitmapGetPixel(bitmap, 1, 1, NULL);
1602     expect(InvalidParameter, stat);
1603
1604     stat = GdipBitmapSetPixel(NULL, 1, 1, 0);
1605     expect(InvalidParameter, stat);
1606
1607     /* out of bounds */
1608     stat = GdipBitmapGetPixel(bitmap, -1, 1, &color);
1609     expect(InvalidParameter, stat);
1610
1611     stat = GdipBitmapSetPixel(bitmap, -1, 1, 0);
1612     expect(InvalidParameter, stat);
1613
1614     stat = GdipBitmapGetPixel(bitmap, 1, -1, &color);
1615     ok(stat == InvalidParameter ||
1616        broken(stat == Ok), /* Older gdiplus */
1617        "Expected InvalidParameter, got %.8x\n", stat);
1618
1619     stat = GdipBitmapSetPixel(bitmap, 1, -1, 0);
1620     ok(stat == InvalidParameter ||
1621        broken(stat == Ok), /* Older gdiplus */
1622        "Expected InvalidParameter, got %.8x\n", stat);
1623
1624     stat = GdipBitmapGetPixel(bitmap, 2, 1, &color);
1625     expect(InvalidParameter, stat);
1626
1627     stat = GdipBitmapSetPixel(bitmap, 2, 1, 0);
1628     expect(InvalidParameter, stat);
1629
1630     stat = GdipBitmapGetPixel(bitmap, 1, 2, &color);
1631     expect(InvalidParameter, stat);
1632
1633     stat = GdipBitmapSetPixel(bitmap, 1, 2, 0);
1634     expect(InvalidParameter, stat);
1635
1636     /* valid use */
1637     stat = GdipBitmapGetPixel(bitmap, 1, 1, &color);
1638     expect(Ok, stat);
1639     expect(0xffffffff, color);
1640
1641     stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
1642     expect(Ok, stat);
1643     expect(0xff0000ff, color);
1644
1645     stat = GdipBitmapSetPixel(bitmap, 1, 1, 0xff676869);
1646     expect(Ok, stat);
1647
1648     stat = GdipBitmapSetPixel(bitmap, 0, 0, 0xff474849);
1649     expect(Ok, stat);
1650
1651     stat = GdipBitmapGetPixel(bitmap, 1, 1, &color);
1652     expect(Ok, stat);
1653     expect(0xff676869, color);
1654
1655     stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
1656     expect(Ok, stat);
1657     expect(0xff474849, color);
1658
1659     stat = GdipDisposeImage((GpImage*)bitmap);
1660     expect(Ok, stat);
1661 }
1662
1663 static void check_halftone_palette(ColorPalette *palette)
1664 {
1665     static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1666     UINT i;
1667
1668     for (i=0; i<palette->Count; i++)
1669     {
1670         ARGB expected=0xff000000;
1671         if (i<8)
1672         {
1673             if (i&1) expected |= 0x800000;
1674             if (i&2) expected |= 0x8000;
1675             if (i&4) expected |= 0x80;
1676         }
1677         else if (i == 8)
1678         {
1679             expected = 0xffc0c0c0;
1680         }
1681         else if (i < 16)
1682         {
1683             if (i&1) expected |= 0xff0000;
1684             if (i&2) expected |= 0xff00;
1685             if (i&4) expected |= 0xff;
1686         }
1687         else if (i < 40)
1688         {
1689             expected = 0x00000000;
1690         }
1691         else
1692         {
1693             expected |= halftone_values[(i-40)%6];
1694             expected |= halftone_values[((i-40)/6)%6] << 8;
1695             expected |= halftone_values[((i-40)/36)%6] << 16;
1696         }
1697         ok(expected == palette->Entries[i], "Expected %.8x, got %.8x, i=%u/%u\n",
1698             expected, palette->Entries[i], i, palette->Count);
1699     }
1700 }
1701
1702 static void test_palette(void)
1703 {
1704     GpStatus stat;
1705     GpBitmap *bitmap;
1706     INT size;
1707     BYTE buffer[1040];
1708     ColorPalette *palette=(ColorPalette*)buffer;
1709     ARGB color=0;
1710
1711     /* test initial palette from non-indexed bitmap */
1712     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat32bppRGB, NULL, &bitmap);
1713     expect(Ok, stat);
1714
1715     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1716     expect(Ok, stat);
1717     expect(sizeof(UINT)*2+sizeof(ARGB), size);
1718
1719     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1720     expect(Ok, stat);
1721     expect(0, palette->Count);
1722
1723     /* test setting palette on not-indexed bitmap */
1724     palette->Count = 3;
1725
1726     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1727     expect(Ok, stat);
1728
1729     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1730     expect(Ok, stat);
1731     expect(sizeof(UINT)*2+sizeof(ARGB)*3, size);
1732
1733     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1734     expect(Ok, stat);
1735     expect(3, palette->Count);
1736
1737     GdipDisposeImage((GpImage*)bitmap);
1738
1739     /* test initial palette on 1-bit bitmap */
1740     stat = GdipCreateBitmapFromScan0(2, 2, 4, PixelFormat1bppIndexed, NULL, &bitmap);
1741     expect(Ok, stat);
1742
1743     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1744     expect(Ok, stat);
1745     expect(sizeof(UINT)*2+sizeof(ARGB)*2, size);
1746
1747     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1748     expect(Ok, stat);
1749     expect(PaletteFlagsGrayScale, palette->Flags);
1750     expect(2, palette->Count);
1751
1752     expect(0xff000000, palette->Entries[0]);
1753     expect(0xffffffff, palette->Entries[1]);
1754
1755     /* test getting/setting pixels */
1756     stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
1757     expect(Ok, stat);
1758     expect(0xff000000, color);
1759
1760     stat = GdipBitmapSetPixel(bitmap, 0, 1, 0xffffffff);
1761     ok((stat == Ok) ||
1762        broken(stat == InvalidParameter) /* pre-win7 */, "stat=%.8x\n", stat);
1763
1764     if (stat == Ok)
1765     {
1766         stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
1767         expect(Ok, stat);
1768         expect(0xffffffff, color);
1769     }
1770
1771     GdipDisposeImage((GpImage*)bitmap);
1772
1773     /* test initial palette on 4-bit bitmap */
1774     stat = GdipCreateBitmapFromScan0(2, 2, 4, PixelFormat4bppIndexed, NULL, &bitmap);
1775     expect(Ok, stat);
1776
1777     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1778     expect(Ok, stat);
1779     expect(sizeof(UINT)*2+sizeof(ARGB)*16, size);
1780
1781     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1782     expect(Ok, stat);
1783     expect(0, palette->Flags);
1784     expect(16, palette->Count);
1785
1786     check_halftone_palette(palette);
1787
1788     /* test getting/setting pixels */
1789     stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
1790     expect(Ok, stat);
1791     expect(0xff000000, color);
1792
1793     stat = GdipBitmapSetPixel(bitmap, 0, 1, 0xffff00ff);
1794     ok((stat == Ok) ||
1795        broken(stat == InvalidParameter) /* pre-win7 */, "stat=%.8x\n", stat);
1796
1797     if (stat == Ok)
1798     {
1799         stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
1800         expect(Ok, stat);
1801         expect(0xffff00ff, color);
1802     }
1803
1804     GdipDisposeImage((GpImage*)bitmap);
1805
1806     /* test initial palette on 8-bit bitmap */
1807     stat = GdipCreateBitmapFromScan0(2, 2, 8, PixelFormat8bppIndexed, NULL, &bitmap);
1808     expect(Ok, stat);
1809
1810     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1811     expect(Ok, stat);
1812     expect(sizeof(UINT)*2+sizeof(ARGB)*256, size);
1813
1814     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1815     expect(Ok, stat);
1816     expect(PaletteFlagsHalftone, palette->Flags);
1817     expect(256, palette->Count);
1818
1819     check_halftone_palette(palette);
1820
1821     /* test getting/setting pixels */
1822     stat = GdipBitmapGetPixel(bitmap, 0, 0, &color);
1823     expect(Ok, stat);
1824     expect(0xff000000, color);
1825
1826     stat = GdipBitmapSetPixel(bitmap, 0, 1, 0xffcccccc);
1827     ok((stat == Ok) ||
1828        broken(stat == InvalidParameter) /* pre-win7 */, "stat=%.8x\n", stat);
1829
1830     if (stat == Ok)
1831     {
1832         stat = GdipBitmapGetPixel(bitmap, 0, 1, &color);
1833         expect(Ok, stat);
1834         expect(0xffcccccc, color);
1835     }
1836
1837     /* test setting/getting a different palette */
1838     palette->Entries[1] = 0xffcccccc;
1839
1840     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1841     expect(Ok, stat);
1842
1843     palette->Entries[1] = 0;
1844
1845     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1846     expect(Ok, stat);
1847     expect(sizeof(UINT)*2+sizeof(ARGB)*256, size);
1848
1849     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1850     expect(Ok, stat);
1851     expect(PaletteFlagsHalftone, palette->Flags);
1852     expect(256, palette->Count);
1853     expect(0xffcccccc, palette->Entries[1]);
1854
1855     /* test count < 256 */
1856     palette->Flags = 12345;
1857     palette->Count = 3;
1858
1859     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1860     expect(Ok, stat);
1861
1862     palette->Entries[1] = 0;
1863     palette->Entries[3] = 0xdeadbeef;
1864
1865     stat = GdipGetImagePaletteSize((GpImage*)bitmap, &size);
1866     expect(Ok, stat);
1867     expect(sizeof(UINT)*2+sizeof(ARGB)*3, size);
1868
1869     stat = GdipGetImagePalette((GpImage*)bitmap, palette, size);
1870     expect(Ok, stat);
1871     expect(12345, palette->Flags);
1872     expect(3, palette->Count);
1873     expect(0xffcccccc, palette->Entries[1]);
1874     expect(0xdeadbeef, palette->Entries[3]);
1875
1876     /* test count > 256 */
1877     palette->Count = 257;
1878
1879     stat = GdipSetImagePalette((GpImage*)bitmap, palette);
1880     ok(stat == InvalidParameter ||
1881        broken(stat == Ok), /* Old gdiplus behavior */
1882        "Expected %.8x, got %.8x\n", InvalidParameter, stat);
1883
1884     GdipDisposeImage((GpImage*)bitmap);
1885 }
1886
1887 static void test_colormatrix(void)
1888 {
1889     GpStatus stat;
1890     ColorMatrix colormatrix, graymatrix;
1891     GpImageAttributes *imageattr;
1892     const ColorMatrix identity = {{
1893         {1.0,0.0,0.0,0.0,0.0},
1894         {0.0,1.0,0.0,0.0,0.0},
1895         {0.0,0.0,1.0,0.0,0.0},
1896         {0.0,0.0,0.0,1.0,0.0},
1897         {0.0,0.0,0.0,0.0,1.0}}};
1898     const ColorMatrix double_red = {{
1899         {2.0,0.0,0.0,0.0,0.0},
1900         {0.0,1.0,0.0,0.0,0.0},
1901         {0.0,0.0,1.0,0.0,0.0},
1902         {0.0,0.0,0.0,1.0,0.0},
1903         {0.0,0.0,0.0,0.0,1.0}}};
1904     GpBitmap *bitmap1, *bitmap2;
1905     GpGraphics *graphics;
1906     ARGB color;
1907
1908     colormatrix = identity;
1909     graymatrix = identity;
1910
1911     stat = GdipSetImageAttributesColorMatrix(NULL, ColorAdjustTypeDefault,
1912         TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1913     expect(InvalidParameter, stat);
1914
1915     stat = GdipCreateImageAttributes(&imageattr);
1916     expect(Ok, stat);
1917
1918     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1919         TRUE, &colormatrix, NULL, ColorMatrixFlagsDefault);
1920     expect(Ok, stat);
1921
1922     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1923         TRUE, NULL, NULL, ColorMatrixFlagsDefault);
1924     expect(InvalidParameter, stat);
1925
1926     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1927         TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1928     expect(Ok, stat);
1929
1930     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1931         TRUE, &colormatrix, NULL, ColorMatrixFlagsSkipGrays);
1932     expect(Ok, stat);
1933
1934     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1935         TRUE, &colormatrix, NULL, ColorMatrixFlagsAltGray);
1936     expect(InvalidParameter, stat);
1937
1938     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1939         TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsAltGray);
1940     expect(Ok, stat);
1941
1942     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1943         TRUE, &colormatrix, &graymatrix, 3);
1944     expect(InvalidParameter, stat);
1945
1946     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeCount,
1947         TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1948     expect(InvalidParameter, stat);
1949
1950     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeAny,
1951         TRUE, &colormatrix, &graymatrix, ColorMatrixFlagsDefault);
1952     expect(InvalidParameter, stat);
1953
1954     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1955         FALSE, NULL, NULL, ColorMatrixFlagsDefault);
1956     expect(Ok, stat);
1957
1958     /* Drawing a bitmap transforms the colors */
1959     colormatrix = double_red;
1960     stat = GdipSetImageAttributesColorMatrix(imageattr, ColorAdjustTypeDefault,
1961         TRUE, &colormatrix, NULL, ColorMatrixFlagsDefault);
1962     expect(Ok, stat);
1963
1964     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap1);
1965     expect(Ok, stat);
1966
1967     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap2);
1968     expect(Ok, stat);
1969
1970     stat = GdipBitmapSetPixel(bitmap1, 0, 0, 0xff40ffff);
1971     expect(Ok, stat);
1972
1973     stat = GdipGetImageGraphicsContext((GpImage*)bitmap2, &graphics);
1974     expect(Ok, stat);
1975
1976     stat = GdipDrawImageRectRectI(graphics, (GpImage*)bitmap1, 0,0,1,1, 0,0,1,1,
1977         UnitPixel, imageattr, NULL, NULL);
1978     expect(Ok, stat);
1979
1980     stat = GdipBitmapGetPixel(bitmap2, 0, 0, &color);
1981     expect(Ok, stat);
1982     todo_wine expect(0xff80ffff, color);
1983
1984     GdipDeleteGraphics(graphics);
1985     GdipDisposeImage((GpImage*)bitmap1);
1986     GdipDisposeImage((GpImage*)bitmap2);
1987     GdipDisposeImageAttributes(imageattr);
1988 }
1989
1990 static void test_gamma(void)
1991 {
1992     GpStatus stat;
1993     GpImageAttributes *imageattr;
1994     GpBitmap *bitmap1, *bitmap2;
1995     GpGraphics *graphics;
1996     ARGB color;
1997
1998     stat = GdipSetImageAttributesGamma(NULL, ColorAdjustTypeDefault, TRUE, 1.0);
1999     expect(InvalidParameter, stat);
2000
2001     stat = GdipCreateImageAttributes(&imageattr);
2002     expect(Ok, stat);
2003
2004     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, TRUE, 1.0);
2005     expect(Ok, stat);
2006
2007     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeAny, TRUE, 1.0);
2008     expect(InvalidParameter, stat);
2009
2010     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, TRUE, -1.0);
2011     expect(InvalidParameter, stat);
2012
2013     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, TRUE, 0.0);
2014     expect(InvalidParameter, stat);
2015
2016     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, TRUE, 0.5);
2017     expect(Ok, stat);
2018
2019     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, FALSE, 0.0);
2020     expect(Ok, stat);
2021
2022     /* Drawing a bitmap transforms the colors */
2023     stat = GdipSetImageAttributesGamma(imageattr, ColorAdjustTypeDefault, TRUE, 3.0);
2024     expect(Ok, stat);
2025
2026     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap1);
2027     expect(Ok, stat);
2028
2029     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap2);
2030     expect(Ok, stat);
2031
2032     stat = GdipBitmapSetPixel(bitmap1, 0, 0, 0xff80ffff);
2033     expect(Ok, stat);
2034
2035     stat = GdipGetImageGraphicsContext((GpImage*)bitmap2, &graphics);
2036     expect(Ok, stat);
2037
2038     stat = GdipDrawImageRectRectI(graphics, (GpImage*)bitmap1, 0,0,1,1, 0,0,1,1,
2039         UnitPixel, imageattr, NULL, NULL);
2040     expect(Ok, stat);
2041
2042     stat = GdipBitmapGetPixel(bitmap2, 0, 0, &color);
2043     expect(Ok, stat);
2044     todo_wine ok(color_match(0xff20ffff, color, 1), "Expected ff20ffff, got %.8x\n", color);
2045
2046     GdipDeleteGraphics(graphics);
2047     GdipDisposeImage((GpImage*)bitmap1);
2048     GdipDisposeImage((GpImage*)bitmap2);
2049     GdipDisposeImageAttributes(imageattr);
2050 }
2051
2052 /* 1x1 pixel gif, 2 frames; first frame is white, second is black */
2053 static const unsigned char gifanimation[72] = {
2054 0x47,0x49,0x46,0x38,0x39,0x61,0x01,0x00,0x01,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,
2055 0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x21,0xf9,0x04,0x00,0x0a,0x00,0xff,
2056 0x00,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x4c,0x01,0x00,
2057 0x21,0xf9,0x04,0x01,0x0a,0x00,0x01,0x00,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
2058 0x00,0x00,0x02,0x02,0x44,0x01,0x00,0x3b
2059 };
2060
2061 static void test_multiframegif(void)
2062 {
2063     LPSTREAM stream;
2064     HGLOBAL hglob;
2065     LPBYTE data;
2066     HRESULT hres;
2067     GpStatus stat;
2068     GpBitmap *bmp;
2069     ARGB color;
2070     UINT count;
2071     GUID dimension;
2072
2073     /* Test frame functions with an animated GIF */
2074     hglob = GlobalAlloc (0, sizeof(gifanimation));
2075     data = GlobalLock (hglob);
2076     memcpy(data, gifanimation, sizeof(gifanimation));
2077     GlobalUnlock(hglob);
2078
2079     hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
2080     ok(hres == S_OK, "Failed to create a stream\n");
2081     if(hres != S_OK) return;
2082
2083     stat = GdipCreateBitmapFromStream(stream, &bmp);
2084     ok(stat == Ok, "Failed to create a Bitmap\n");
2085     if(stat != Ok){
2086         IStream_Release(stream);
2087         return;
2088     }
2089
2090     /* Bitmap starts at frame 0 */
2091     color = 0xdeadbeef;
2092     stat = GdipBitmapGetPixel(bmp, 0, 0, &color);
2093     expect(Ok, stat);
2094     expect(0xffffffff, color);
2095
2096     /* Check that we get correct metadata */
2097     stat = GdipImageGetFrameDimensionsCount((GpImage*)bmp,&count);
2098     expect(Ok, stat);
2099     expect(1, count);
2100
2101     stat = GdipImageGetFrameDimensionsList((GpImage*)bmp, &dimension, 1);
2102     expect(Ok, stat);
2103     expect_guid(&FrameDimensionTime, &dimension, __LINE__, FALSE);
2104
2105     count = 12345;
2106     stat = GdipImageGetFrameCount((GpImage*)bmp, &dimension, &count);
2107     expect(Ok, stat);
2108     todo_wine expect(2, count);
2109
2110     /* SelectActiveFrame overwrites our current data */
2111     stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 1);
2112     expect(Ok, stat);
2113
2114     color = 0xdeadbeef;
2115     GdipBitmapGetPixel(bmp, 0, 0, &color);
2116     expect(Ok, stat);
2117     todo_wine expect(0xff000000, color);
2118
2119     stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 0);
2120     expect(Ok, stat);
2121
2122     color = 0xdeadbeef;
2123     GdipBitmapGetPixel(bmp, 0, 0, &color);
2124     expect(Ok, stat);
2125     expect(0xffffffff, color);
2126
2127     /* Write over the image data */
2128     stat = GdipBitmapSetPixel(bmp, 0, 0, 0xff000000);
2129     expect(Ok, stat);
2130
2131     /* Switching to the same frame does not overwrite our changes */
2132     stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 0);
2133     expect(Ok, stat);
2134
2135     stat = GdipBitmapGetPixel(bmp, 0, 0, &color);
2136     expect(Ok, stat);
2137     expect(0xff000000, color);
2138
2139     /* But switching to another frame and back does */
2140     stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 1);
2141     expect(Ok, stat);
2142
2143     stat = GdipImageSelectActiveFrame((GpImage*)bmp, &dimension, 0);
2144     expect(Ok, stat);
2145
2146     stat = GdipBitmapGetPixel(bmp, 0, 0, &color);
2147     expect(Ok, stat);
2148     todo_wine expect(0xffffffff, color);
2149
2150     GdipDisposeImage((GpImage*)bmp);
2151     IStream_Release(stream);
2152
2153     /* Test with a non-animated gif */
2154     hglob = GlobalAlloc (0, sizeof(gifimage));
2155     data = GlobalLock (hglob);
2156     memcpy(data, gifimage, sizeof(gifimage));
2157     GlobalUnlock(hglob);
2158
2159     hres = CreateStreamOnHGlobal(hglob, TRUE, &stream);
2160     ok(hres == S_OK, "Failed to create a stream\n");
2161     if(hres != S_OK) return;
2162
2163     stat = GdipCreateBitmapFromStream(stream, &bmp);
2164     ok(stat == Ok, "Failed to create a Bitmap\n");
2165     if(stat != Ok){
2166         IStream_Release(stream);
2167         return;
2168     }
2169
2170     /* Check metadata */
2171     stat = GdipImageGetFrameDimensionsCount((GpImage*)bmp,&count);
2172     expect(Ok, stat);
2173     expect(1, count);
2174
2175     stat = GdipImageGetFrameDimensionsList((GpImage*)bmp, &dimension, 1);
2176     expect(Ok, stat);
2177     expect_guid(&FrameDimensionTime, &dimension, __LINE__, FALSE);
2178
2179     count = 12345;
2180     stat = GdipImageGetFrameCount((GpImage*)bmp, &dimension, &count);
2181     expect(Ok, stat);
2182     expect(1, count);
2183
2184     GdipDisposeImage((GpImage*)bmp);
2185     IStream_Release(stream);
2186 }
2187
2188 static void test_rotateflip(void)
2189 {
2190     GpImage *bitmap;
2191     GpStatus stat;
2192     BYTE bits[24];
2193     static const BYTE orig_bits[24] = {
2194         0,0,0xff,    0,0xff,0,    0xff,0,0,    23,23,23,
2195         0xff,0xff,0, 0xff,0,0xff, 0,0xff,0xff, 23,23,23};
2196     UINT width, height;
2197     ARGB color;
2198
2199     memcpy(bits, orig_bits, sizeof(bits));
2200     stat = GdipCreateBitmapFromScan0(3, 2, 12, PixelFormat24bppRGB, bits, (GpBitmap**)&bitmap);
2201     expect(Ok, stat);
2202
2203     stat = GdipImageRotateFlip(bitmap, Rotate90FlipNone);
2204     expect(Ok, stat);
2205
2206     stat = GdipGetImageWidth(bitmap, &width);
2207     expect(Ok, stat);
2208     stat = GdipGetImageHeight(bitmap, &height);
2209     expect(Ok, stat);
2210     expect(2, width);
2211     expect(3, height);
2212
2213     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 0, &color);
2214     expect(Ok, stat);
2215     expect(0xff00ffff, color);
2216
2217     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 1, 0, &color);
2218     expect(Ok, stat);
2219     expect(0xffff0000, color);
2220
2221     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 2, &color);
2222     expect(Ok, stat);
2223     expect(0xffffff00, color);
2224
2225     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 1, 2, &color);
2226     expect(Ok, stat);
2227     expect(0xff0000ff, color);
2228
2229     expect(0, bits[0]);
2230     expect(0, bits[1]);
2231     expect(0xff, bits[2]);
2232
2233     GdipDisposeImage(bitmap);
2234
2235     memcpy(bits, orig_bits, sizeof(bits));
2236     stat = GdipCreateBitmapFromScan0(3, 2, 12, PixelFormat24bppRGB, bits, (GpBitmap**)&bitmap);
2237     expect(Ok, stat);
2238
2239     stat = GdipImageRotateFlip(bitmap, RotateNoneFlipX);
2240     expect(Ok, stat);
2241
2242     stat = GdipGetImageWidth(bitmap, &width);
2243     expect(Ok, stat);
2244     stat = GdipGetImageHeight(bitmap, &height);
2245     expect(Ok, stat);
2246     expect(3, width);
2247     expect(2, height);
2248
2249     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 0, &color);
2250     expect(Ok, stat);
2251     expect(0xff0000ff, color);
2252
2253     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 2, 0, &color);
2254     expect(Ok, stat);
2255     expect(0xffff0000, color);
2256
2257     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 1, &color);
2258     expect(Ok, stat);
2259     expect(0xffffff00, color);
2260
2261     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 2, 1, &color);
2262     expect(Ok, stat);
2263     expect(0xff00ffff, color);
2264
2265     expect(0, bits[0]);
2266     expect(0, bits[1]);
2267     expect(0xff, bits[2]);
2268
2269     GdipDisposeImage(bitmap);
2270
2271     memcpy(bits, orig_bits, sizeof(bits));
2272     stat = GdipCreateBitmapFromScan0(3, 2, 12, PixelFormat24bppRGB, bits, (GpBitmap**)&bitmap);
2273     expect(Ok, stat);
2274
2275     stat = GdipImageRotateFlip(bitmap, RotateNoneFlipY);
2276     expect(Ok, stat);
2277
2278     stat = GdipGetImageWidth(bitmap, &width);
2279     expect(Ok, stat);
2280     stat = GdipGetImageHeight(bitmap, &height);
2281     expect(Ok, stat);
2282     expect(3, width);
2283     expect(2, height);
2284
2285     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 0, &color);
2286     expect(Ok, stat);
2287     expect(0xff00ffff, color);
2288
2289     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 2, 0, &color);
2290     expect(Ok, stat);
2291     expect(0xffffff00, color);
2292
2293     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 0, 1, &color);
2294     expect(Ok, stat);
2295     expect(0xffff0000, color);
2296
2297     stat = GdipBitmapGetPixel((GpBitmap*)bitmap, 2, 1, &color);
2298     expect(Ok, stat);
2299     expect(0xff0000ff, color);
2300
2301     expect(0, bits[0]);
2302     expect(0, bits[1]);
2303     expect(0xff, bits[2]);
2304
2305     GdipDisposeImage(bitmap);
2306 }
2307
2308 static void test_remaptable(void)
2309 {
2310     GpStatus stat;
2311     GpImageAttributes *imageattr;
2312     GpBitmap *bitmap1, *bitmap2;
2313     GpGraphics *graphics;
2314     ARGB color;
2315     ColorMap *map;
2316
2317     map = GdipAlloc(sizeof(ColorMap));
2318
2319     map->oldColor.Argb = 0xff00ff00;
2320     map->newColor.Argb = 0xffff00ff;
2321
2322     stat = GdipSetImageAttributesRemapTable(NULL, ColorAdjustTypeDefault, TRUE, 1, map);
2323     expect(InvalidParameter, stat);
2324
2325     stat = GdipCreateImageAttributes(&imageattr);
2326     expect(Ok, stat);
2327
2328     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeDefault, TRUE, 1, NULL);
2329     expect(InvalidParameter, stat);
2330
2331     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeCount, TRUE, 1, map);
2332     expect(InvalidParameter, stat);
2333
2334     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeAny, TRUE, 1, map);
2335     expect(InvalidParameter, stat);
2336
2337     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeDefault, TRUE, 0, map);
2338     expect(InvalidParameter, stat);
2339
2340     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeDefault, FALSE, 0, NULL);
2341     expect(Ok, stat);
2342
2343     stat = GdipSetImageAttributesRemapTable(imageattr, ColorAdjustTypeDefault, TRUE, 1, map);
2344     expect(Ok, stat);
2345
2346     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap1);
2347     expect(Ok, stat);
2348
2349     stat = GdipCreateBitmapFromScan0(1, 1, 0, PixelFormat32bppRGB, NULL, &bitmap2);
2350     expect(Ok, stat);
2351
2352     stat = GdipBitmapSetPixel(bitmap1, 0, 0, 0xff00ff00);
2353     expect(Ok, stat);
2354
2355     stat = GdipGetImageGraphicsContext((GpImage*)bitmap2, &graphics);
2356     expect(Ok, stat);
2357
2358     stat = GdipDrawImageRectRectI(graphics, (GpImage*)bitmap1, 0,0,1,1, 0,0,1,1,
2359         UnitPixel, imageattr, NULL, NULL);
2360     expect(Ok, stat);
2361
2362     stat = GdipBitmapGetPixel(bitmap2, 0, 0, &color);
2363     expect(Ok, stat);
2364     ok(color_match(0xffff00ff, color, 1), "Expected ffff00ff, got %.8x\n", color);
2365
2366     GdipDeleteGraphics(graphics);
2367     GdipDisposeImage((GpImage*)bitmap1);
2368     GdipDisposeImage((GpImage*)bitmap2);
2369     GdipDisposeImageAttributes(imageattr);
2370     GdipFree(map);
2371 }
2372
2373 static void test_colorkey(void)
2374 {
2375     GpStatus stat;
2376     GpImageAttributes *imageattr;
2377     GpBitmap *bitmap1, *bitmap2;
2378     GpGraphics *graphics;
2379     ARGB color;
2380
2381     stat = GdipSetImageAttributesColorKeys(NULL, ColorAdjustTypeDefault, TRUE, 0xff405060, 0xff708090);
2382     expect(InvalidParameter, stat);
2383
2384     stat = GdipCreateImageAttributes(&imageattr);
2385     expect(Ok, stat);
2386
2387     stat = GdipSetImageAttributesColorKeys(imageattr, ColorAdjustTypeCount, TRUE, 0xff405060, 0xff708090);
2388     expect(InvalidParameter, stat);
2389
2390     stat = GdipSetImageAttributesColorKeys(imageattr, ColorAdjustTypeAny, TRUE, 0xff405060, 0xff708090);
2391     expect(InvalidParameter, stat);
2392
2393     stat = GdipSetImageAttributesColorKeys(imageattr, ColorAdjustTypeDefault, TRUE, 0xff405060, 0xff708090);
2394     expect(Ok, stat);
2395
2396     stat = GdipCreateBitmapFromScan0(2, 2, 0, PixelFormat32bppARGB, NULL, &bitmap1);
2397     expect(Ok, stat);
2398
2399     stat = GdipCreateBitmapFromScan0(2, 2, 0, PixelFormat32bppARGB, NULL, &bitmap2);
2400     expect(Ok, stat);
2401
2402     stat = GdipBitmapSetPixel(bitmap1, 0, 0, 0x20405060);
2403     expect(Ok, stat);
2404
2405     stat = GdipBitmapSetPixel(bitmap1, 0, 1, 0x40506070);
2406     expect(Ok, stat);
2407
2408     stat = GdipBitmapSetPixel(bitmap1, 1, 0, 0x60708090);
2409     expect(Ok, stat);
2410
2411     stat = GdipBitmapSetPixel(bitmap1, 1, 1, 0xffffffff);
2412     expect(Ok, stat);
2413
2414     stat = GdipGetImageGraphicsContext((GpImage*)bitmap2, &graphics);
2415     expect(Ok, stat);
2416
2417     stat = GdipDrawImageRectRectI(graphics, (GpImage*)bitmap1, 0,0,2,2, 0,0,2,2,
2418         UnitPixel, imageattr, NULL, NULL);
2419     expect(Ok, stat);
2420
2421     stat = GdipBitmapGetPixel(bitmap2, 0, 0, &color);
2422     expect(Ok, stat);
2423     ok(color_match(0x00000000, color, 1), "Expected ffff00ff, got %.8x\n", color);
2424
2425     stat = GdipBitmapGetPixel(bitmap2, 0, 1, &color);
2426     expect(Ok, stat);
2427     ok(color_match(0x00000000, color, 1), "Expected ffff00ff, got %.8x\n", color);
2428
2429     stat = GdipBitmapGetPixel(bitmap2, 1, 0, &color);
2430     expect(Ok, stat);
2431     ok(color_match(0x00000000, color, 1), "Expected ffff00ff, got %.8x\n", color);
2432
2433     stat = GdipBitmapGetPixel(bitmap2, 1, 1, &color);
2434     expect(Ok, stat);
2435     ok(color_match(0xffffffff, color, 1), "Expected ffff00ff, got %.8x\n", color);
2436
2437     GdipDeleteGraphics(graphics);
2438     GdipDisposeImage((GpImage*)bitmap1);
2439     GdipDisposeImage((GpImage*)bitmap2);
2440     GdipDisposeImageAttributes(imageattr);
2441 }
2442
2443 static void test_dispose(void)
2444 {
2445     GpStatus stat;
2446     GpImage *image;
2447     char invalid_image[256];
2448
2449     stat = GdipDisposeImage(NULL);
2450     expect(InvalidParameter, stat);
2451
2452     stat = GdipCreateBitmapFromScan0(2, 2, 0, PixelFormat32bppARGB, NULL, (GpBitmap**)&image);
2453     expect(Ok, stat);
2454
2455     stat = GdipDisposeImage(image);
2456     expect(Ok, stat);
2457
2458     stat = GdipDisposeImage(image);
2459     expect(ObjectBusy, stat);
2460
2461     memset(invalid_image, 0, 256);
2462     stat = GdipDisposeImage((GpImage*)invalid_image);
2463     expect(ObjectBusy, stat);
2464 }
2465
2466 START_TEST(image)
2467 {
2468     struct GdiplusStartupInput gdiplusStartupInput;
2469     ULONG_PTR gdiplusToken;
2470
2471     gdiplusStartupInput.GdiplusVersion              = 1;
2472     gdiplusStartupInput.DebugEventCallback          = NULL;
2473     gdiplusStartupInput.SuppressBackgroundThread    = 0;
2474     gdiplusStartupInput.SuppressExternalCodecs      = 0;
2475
2476     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
2477
2478     test_Scan0();
2479     test_FromGdiDib();
2480     test_GetImageDimension();
2481     test_GdipImageGetFrameDimensionsCount();
2482     test_LoadingImages();
2483     test_SavingImages();
2484     test_encoders();
2485     test_LockBits();
2486     test_LockBits_UserBuf();
2487     test_GdipCreateBitmapFromHBITMAP();
2488     test_GdipGetImageFlags();
2489     test_GdipCloneImage();
2490     test_testcontrol();
2491     test_fromhicon();
2492     test_getrawformat();
2493     test_loadwmf();
2494     test_createfromwmf();
2495     test_resolution();
2496     test_createhbitmap();
2497     test_getthumbnail();
2498     test_getsetpixel();
2499     test_palette();
2500     test_colormatrix();
2501     test_gamma();
2502     test_multiframegif();
2503     test_rotateflip();
2504     test_remaptable();
2505     test_colorkey();
2506     test_dispose();
2507
2508     GdiplusShutdown(gdiplusToken);
2509 }