gdiplus: Add test for GdipGetImageThumbnail.
[wine] / dlls / gdiplus / image.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #define NONAMELESSUNION
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27
28 #define COBJMACROS
29 #include "objbase.h"
30 #include "olectl.h"
31 #include "ole2.h"
32
33 #include "initguid.h"
34 #include "wincodec.h"
35 #include "gdiplus.h"
36 #include "gdiplus_private.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
40
41 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
42
43 static INT ipicture_pixel_height(IPicture *pic)
44 {
45     HDC hdcref;
46     OLE_YSIZE_HIMETRIC y;
47
48     IPicture_get_Height(pic, &y);
49
50     hdcref = GetDC(0);
51
52     y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
53     ReleaseDC(0, hdcref);
54
55     return y;
56 }
57
58 static INT ipicture_pixel_width(IPicture *pic)
59 {
60     HDC hdcref;
61     OLE_XSIZE_HIMETRIC x;
62
63     IPicture_get_Width(pic, &x);
64
65     hdcref = GetDC(0);
66
67     x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
68
69     ReleaseDC(0, hdcref);
70
71     return x;
72 }
73
74 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
75     RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
76 {
77     FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
78     /*
79      * Note: According to Jose Roca's GDI+ docs, this function is not
80      * implemented in Windows's GDI+.
81      */
82     return NotImplemented;
83 }
84
85 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
86     INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
87     GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
88 {
89     FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
90     /*
91      * Note: According to Jose Roca's GDI+ docs, this function is not
92      * implemented in Windows's GDI+.
93      */
94     return NotImplemented;
95 }
96
97 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
98 {
99     *index = (row[x/8]>>(7-x%8)) & 1;
100 }
101
102 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
103 {
104     if (x & 1)
105         *index = row[x/2]&0xf;
106     else
107         *index = row[x/2]>>4;
108 }
109
110 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
111 {
112     *index = row[x];
113 }
114
115 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
116     const BYTE *row, UINT x)
117 {
118     *r = *g = *b = row[x*2+1];
119     *a = 255;
120 }
121
122 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
123     const BYTE *row, UINT x)
124 {
125     WORD pixel = *((const WORD*)(row)+x);
126     *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
127     *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
128     *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
129     *a = 255;
130 }
131
132 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
133     const BYTE *row, UINT x)
134 {
135     WORD pixel = *((const WORD*)(row)+x);
136     *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
137     *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
138     *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
139     *a = 255;
140 }
141
142 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
143     const BYTE *row, UINT x)
144 {
145     WORD pixel = *((const WORD*)(row)+x);
146     *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
147     *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
148     *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
149     if ((pixel&0x8000) == 0x8000)
150         *a = 255;
151     else
152         *a = 0;
153 }
154
155 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
156     const BYTE *row, UINT x)
157 {
158     *r = row[x*3+2];
159     *g = row[x*3+1];
160     *b = row[x*3];
161     *a = 255;
162 }
163
164 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
165     const BYTE *row, UINT x)
166 {
167     *r = row[x*4+2];
168     *g = row[x*4+1];
169     *b = row[x*4];
170     *a = 255;
171 }
172
173 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
174     const BYTE *row, UINT x)
175 {
176     *r = row[x*4+2];
177     *g = row[x*4+1];
178     *b = row[x*4];
179     *a = row[x*4+3];
180 }
181
182 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
183     const BYTE *row, UINT x)
184 {
185     *a = row[x*4+3];
186     if (*a == 0)
187         *r = *g = *b = 0;
188     else
189     {
190         *r = row[x*4+2] * 255 / *a;
191         *g = row[x*4+1] * 255 / *a;
192         *b = row[x*4] * 255 / *a;
193     }
194 }
195
196 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
197     const BYTE *row, UINT x)
198 {
199     *r = row[x*6+5];
200     *g = row[x*6+3];
201     *b = row[x*6+1];
202     *a = 255;
203 }
204
205 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
206     const BYTE *row, UINT x)
207 {
208     *r = row[x*8+5];
209     *g = row[x*8+3];
210     *b = row[x*8+1];
211     *a = row[x*8+7];
212 }
213
214 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
215     const BYTE *row, UINT x)
216 {
217     *a = row[x*8+7];
218     if (*a == 0)
219         *r = *g = *b = 0;
220     else
221     {
222         *r = row[x*8+5] * 255 / *a;
223         *g = row[x*8+3] * 255 / *a;
224         *b = row[x*8+1] * 255 / *a;
225     }
226 }
227
228 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
229     ARGB *color)
230 {
231     BYTE r, g, b, a;
232     BYTE index;
233     BYTE *row;
234     TRACE("%p %d %d %p\n", bitmap, x, y, color);
235
236     if(!bitmap || !color ||
237        x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
238         return InvalidParameter;
239
240     row = bitmap->bits+bitmap->stride*y;
241
242     switch (bitmap->format)
243     {
244         case PixelFormat1bppIndexed:
245             getpixel_1bppIndexed(&index,row,x);
246             break;
247         case PixelFormat4bppIndexed:
248             getpixel_4bppIndexed(&index,row,x);
249             break;
250         case PixelFormat8bppIndexed:
251             getpixel_8bppIndexed(&index,row,x);
252             break;
253         case PixelFormat16bppGrayScale:
254             getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
255             break;
256         case PixelFormat16bppRGB555:
257             getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
258             break;
259         case PixelFormat16bppRGB565:
260             getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
261             break;
262         case PixelFormat16bppARGB1555:
263             getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
264             break;
265         case PixelFormat24bppRGB:
266             getpixel_24bppRGB(&r,&g,&b,&a,row,x);
267             break;
268         case PixelFormat32bppRGB:
269             getpixel_32bppRGB(&r,&g,&b,&a,row,x);
270             break;
271         case PixelFormat32bppARGB:
272             getpixel_32bppARGB(&r,&g,&b,&a,row,x);
273             break;
274         case PixelFormat32bppPARGB:
275             getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
276             break;
277         case PixelFormat48bppRGB:
278             getpixel_48bppRGB(&r,&g,&b,&a,row,x);
279             break;
280         case PixelFormat64bppARGB:
281             getpixel_64bppARGB(&r,&g,&b,&a,row,x);
282             break;
283         case PixelFormat64bppPARGB:
284             getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
285             break;
286         default:
287             FIXME("not implemented for format 0x%x\n", bitmap->format);
288             return NotImplemented;
289     }
290
291     if (bitmap->format & PixelFormatIndexed)
292         *color = bitmap->image.palette_entries[index];
293     else
294         *color = a<<24|r<<16|g<<8|b;
295
296     return Ok;
297 }
298
299 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
300     BYTE *row, UINT x)
301 {
302     *((WORD*)(row)+x) = (r+g+b)*85;
303 }
304
305 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
306     BYTE *row, UINT x)
307 {
308     *((WORD*)(row)+x) = (r<<7&0x7c00)|
309                         (g<<2&0x03e0)|
310                         (b>>3&0x001f);
311 }
312
313 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
314     BYTE *row, UINT x)
315 {
316     *((WORD*)(row)+x) = (r<<8&0xf800)|
317                          (g<<3&0x07e0)|
318                          (b>>3&0x001f);
319 }
320
321 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
322     BYTE *row, UINT x)
323 {
324     *((WORD*)(row)+x) = (a<<8&0x8000)|
325                         (r<<7&0x7c00)|
326                         (g<<2&0x03e0)|
327                         (b>>3&0x001f);
328 }
329
330 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
331     BYTE *row, UINT x)
332 {
333     row[x*3+2] = r;
334     row[x*3+1] = g;
335     row[x*3] = b;
336 }
337
338 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
339     BYTE *row, UINT x)
340 {
341     *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
342 }
343
344 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
345     BYTE *row, UINT x)
346 {
347     *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
348 }
349
350 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
351     BYTE *row, UINT x)
352 {
353     r = r * a / 255;
354     g = g * a / 255;
355     b = b * a / 255;
356     *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
357 }
358
359 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
360     BYTE *row, UINT x)
361 {
362     row[x*6+5] = row[x*6+4] = r;
363     row[x*6+3] = row[x*6+2] = g;
364     row[x*6+1] = row[x*6] = b;
365 }
366
367 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
368     BYTE *row, UINT x)
369 {
370     UINT64 a64=a, r64=r, g64=g, b64=b;
371     *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
372 }
373
374 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
375     BYTE *row, UINT x)
376 {
377     UINT64 a64, r64, g64, b64;
378     a64 = a * 257;
379     r64 = r * a / 255;
380     g64 = g * a / 255;
381     b64 = b * a / 255;
382     *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
383 }
384
385 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
386     ARGB color)
387 {
388     BYTE a, r, g, b;
389     BYTE *row;
390     TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
391
392     if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
393         return InvalidParameter;
394
395     a = color>>24;
396     r = color>>16;
397     g = color>>8;
398     b = color;
399
400     row = bitmap->bits + bitmap->stride * y;
401
402     switch (bitmap->format)
403     {
404         case PixelFormat16bppGrayScale:
405             setpixel_16bppGrayScale(r,g,b,a,row,x);
406             break;
407         case PixelFormat16bppRGB555:
408             setpixel_16bppRGB555(r,g,b,a,row,x);
409             break;
410         case PixelFormat16bppRGB565:
411             setpixel_16bppRGB565(r,g,b,a,row,x);
412             break;
413         case PixelFormat16bppARGB1555:
414             setpixel_16bppARGB1555(r,g,b,a,row,x);
415             break;
416         case PixelFormat24bppRGB:
417             setpixel_24bppRGB(r,g,b,a,row,x);
418             break;
419         case PixelFormat32bppRGB:
420             setpixel_32bppRGB(r,g,b,a,row,x);
421             break;
422         case PixelFormat32bppARGB:
423             setpixel_32bppARGB(r,g,b,a,row,x);
424             break;
425         case PixelFormat32bppPARGB:
426             setpixel_32bppPARGB(r,g,b,a,row,x);
427             break;
428         case PixelFormat48bppRGB:
429             setpixel_48bppRGB(r,g,b,a,row,x);
430             break;
431         case PixelFormat64bppARGB:
432             setpixel_64bppARGB(r,g,b,a,row,x);
433             break;
434         case PixelFormat64bppPARGB:
435             setpixel_64bppPARGB(r,g,b,a,row,x);
436             break;
437         default:
438             FIXME("not implemented for format 0x%x\n", bitmap->format);
439             return NotImplemented;
440     }
441
442     return Ok;
443 }
444
445 GpStatus convert_pixels(UINT width, UINT height,
446     INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
447     INT src_stride, const BYTE *src_bits, PixelFormat src_format, ARGB *src_palette)
448 {
449     UINT x, y;
450
451     if (src_format == dst_format ||
452         (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
453     {
454         UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
455         for (y=0; y<height; y++)
456             memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
457         return Ok;
458     }
459
460 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
461     for (x=0; x<width; x++) \
462         for (y=0; y<height; y++) { \
463             BYTE index; \
464             BYTE *color; \
465             getpixel_function(&index, src_bits+src_stride*y, x); \
466             color = (BYTE*)(&src_palette[index]); \
467             setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
468         } \
469     return Ok; \
470 } while (0);
471
472 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
473     for (x=0; x<width; x++) \
474         for (y=0; y<height; y++) { \
475             BYTE r, g, b, a; \
476             getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
477             setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
478         } \
479     return Ok; \
480 } while (0);
481
482     switch (src_format)
483     {
484     case PixelFormat1bppIndexed:
485         switch (dst_format)
486         {
487         case PixelFormat16bppGrayScale:
488             convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
489         case PixelFormat16bppRGB555:
490             convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
491         case PixelFormat16bppRGB565:
492             convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
493         case PixelFormat16bppARGB1555:
494             convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
495         case PixelFormat24bppRGB:
496             convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
497         case PixelFormat32bppRGB:
498             convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
499         case PixelFormat32bppARGB:
500             convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
501         case PixelFormat32bppPARGB:
502             convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
503         case PixelFormat48bppRGB:
504             convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
505         case PixelFormat64bppARGB:
506             convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
507         default:
508             break;
509         }
510         break;
511     case PixelFormat4bppIndexed:
512         switch (dst_format)
513         {
514         case PixelFormat16bppGrayScale:
515             convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
516         case PixelFormat16bppRGB555:
517             convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
518         case PixelFormat16bppRGB565:
519             convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
520         case PixelFormat16bppARGB1555:
521             convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
522         case PixelFormat24bppRGB:
523             convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
524         case PixelFormat32bppRGB:
525             convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
526         case PixelFormat32bppARGB:
527             convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
528         case PixelFormat32bppPARGB:
529             convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
530         case PixelFormat48bppRGB:
531             convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
532         case PixelFormat64bppARGB:
533             convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
534         default:
535             break;
536         }
537         break;
538     case PixelFormat8bppIndexed:
539         switch (dst_format)
540         {
541         case PixelFormat16bppGrayScale:
542             convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
543         case PixelFormat16bppRGB555:
544             convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
545         case PixelFormat16bppRGB565:
546             convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
547         case PixelFormat16bppARGB1555:
548             convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
549         case PixelFormat24bppRGB:
550             convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
551         case PixelFormat32bppRGB:
552             convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
553         case PixelFormat32bppARGB:
554             convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
555         case PixelFormat32bppPARGB:
556             convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
557         case PixelFormat48bppRGB:
558             convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
559         case PixelFormat64bppARGB:
560             convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
561         default:
562             break;
563         }
564         break;
565     case PixelFormat16bppGrayScale:
566         switch (dst_format)
567         {
568         case PixelFormat16bppRGB555:
569             convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
570         case PixelFormat16bppRGB565:
571             convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
572         case PixelFormat16bppARGB1555:
573             convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
574         case PixelFormat24bppRGB:
575             convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
576         case PixelFormat32bppRGB:
577             convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
578         case PixelFormat32bppARGB:
579             convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
580         case PixelFormat32bppPARGB:
581             convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
582         case PixelFormat48bppRGB:
583             convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
584         case PixelFormat64bppARGB:
585             convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
586         default:
587             break;
588         }
589         break;
590     case PixelFormat16bppRGB555:
591         switch (dst_format)
592         {
593         case PixelFormat16bppGrayScale:
594             convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
595         case PixelFormat16bppRGB565:
596             convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
597         case PixelFormat16bppARGB1555:
598             convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
599         case PixelFormat24bppRGB:
600             convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
601         case PixelFormat32bppRGB:
602             convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
603         case PixelFormat32bppARGB:
604             convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
605         case PixelFormat32bppPARGB:
606             convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
607         case PixelFormat48bppRGB:
608             convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
609         case PixelFormat64bppARGB:
610             convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
611         default:
612             break;
613         }
614         break;
615     case PixelFormat16bppRGB565:
616         switch (dst_format)
617         {
618         case PixelFormat16bppGrayScale:
619             convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
620         case PixelFormat16bppRGB555:
621             convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
622         case PixelFormat16bppARGB1555:
623             convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
624         case PixelFormat24bppRGB:
625             convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
626         case PixelFormat32bppRGB:
627             convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
628         case PixelFormat32bppARGB:
629             convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
630         case PixelFormat32bppPARGB:
631             convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
632         case PixelFormat48bppRGB:
633             convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
634         case PixelFormat64bppARGB:
635             convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
636         default:
637             break;
638         }
639         break;
640     case PixelFormat16bppARGB1555:
641         switch (dst_format)
642         {
643         case PixelFormat16bppGrayScale:
644             convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
645         case PixelFormat16bppRGB555:
646             convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
647         case PixelFormat16bppRGB565:
648             convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
649         case PixelFormat24bppRGB:
650             convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
651         case PixelFormat32bppRGB:
652             convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
653         case PixelFormat32bppARGB:
654             convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
655         case PixelFormat32bppPARGB:
656             convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
657         case PixelFormat48bppRGB:
658             convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
659         case PixelFormat64bppARGB:
660             convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
661         default:
662             break;
663         }
664         break;
665     case PixelFormat24bppRGB:
666         switch (dst_format)
667         {
668         case PixelFormat16bppGrayScale:
669             convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
670         case PixelFormat16bppRGB555:
671             convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
672         case PixelFormat16bppRGB565:
673             convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
674         case PixelFormat16bppARGB1555:
675             convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
676         case PixelFormat32bppRGB:
677             convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
678         case PixelFormat32bppARGB:
679             convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
680         case PixelFormat32bppPARGB:
681             convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
682         case PixelFormat48bppRGB:
683             convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
684         case PixelFormat64bppARGB:
685             convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
686         default:
687             break;
688         }
689         break;
690     case PixelFormat32bppRGB:
691         switch (dst_format)
692         {
693         case PixelFormat16bppGrayScale:
694             convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
695         case PixelFormat16bppRGB555:
696             convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
697         case PixelFormat16bppRGB565:
698             convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
699         case PixelFormat16bppARGB1555:
700             convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
701         case PixelFormat24bppRGB:
702             convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
703         case PixelFormat32bppARGB:
704             convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
705         case PixelFormat32bppPARGB:
706             convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
707         case PixelFormat48bppRGB:
708             convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
709         case PixelFormat64bppARGB:
710             convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
711         default:
712             break;
713         }
714         break;
715     case PixelFormat32bppARGB:
716         switch (dst_format)
717         {
718         case PixelFormat16bppGrayScale:
719             convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
720         case PixelFormat16bppRGB555:
721             convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
722         case PixelFormat16bppRGB565:
723             convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
724         case PixelFormat16bppARGB1555:
725             convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
726         case PixelFormat24bppRGB:
727             convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
728         case PixelFormat32bppPARGB:
729             convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
730             return Ok;
731         case PixelFormat48bppRGB:
732             convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
733         case PixelFormat64bppARGB:
734             convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
735         default:
736             break;
737         }
738         break;
739     case PixelFormat32bppPARGB:
740         switch (dst_format)
741         {
742         case PixelFormat16bppGrayScale:
743             convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
744         case PixelFormat16bppRGB555:
745             convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
746         case PixelFormat16bppRGB565:
747             convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
748         case PixelFormat16bppARGB1555:
749             convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
750         case PixelFormat24bppRGB:
751             convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
752         case PixelFormat32bppRGB:
753             convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
754         case PixelFormat32bppARGB:
755             convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
756         case PixelFormat48bppRGB:
757             convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
758         case PixelFormat64bppARGB:
759             convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
760         default:
761             break;
762         }
763         break;
764     case PixelFormat48bppRGB:
765         switch (dst_format)
766         {
767         case PixelFormat16bppGrayScale:
768             convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
769         case PixelFormat16bppRGB555:
770             convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
771         case PixelFormat16bppRGB565:
772             convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
773         case PixelFormat16bppARGB1555:
774             convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
775         case PixelFormat24bppRGB:
776             convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
777         case PixelFormat32bppRGB:
778             convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
779         case PixelFormat32bppARGB:
780             convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
781         case PixelFormat32bppPARGB:
782             convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
783         case PixelFormat64bppARGB:
784             convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
785         default:
786             break;
787         }
788         break;
789     case PixelFormat64bppARGB:
790         switch (dst_format)
791         {
792         case PixelFormat16bppGrayScale:
793             convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
794         case PixelFormat16bppRGB555:
795             convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
796         case PixelFormat16bppRGB565:
797             convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
798         case PixelFormat16bppARGB1555:
799             convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
800         case PixelFormat24bppRGB:
801             convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
802         case PixelFormat32bppRGB:
803             convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
804         case PixelFormat32bppARGB:
805             convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
806         case PixelFormat32bppPARGB:
807             convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
808         case PixelFormat48bppRGB:
809             convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
810         default:
811             break;
812         }
813         break;
814     case PixelFormat64bppPARGB:
815         switch (dst_format)
816         {
817         case PixelFormat16bppGrayScale:
818             convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
819         case PixelFormat16bppRGB555:
820             convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
821         case PixelFormat16bppRGB565:
822             convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
823         case PixelFormat16bppARGB1555:
824             convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
825         case PixelFormat24bppRGB:
826             convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
827         case PixelFormat32bppRGB:
828             convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
829         case PixelFormat32bppARGB:
830             convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
831         case PixelFormat32bppPARGB:
832             convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
833         case PixelFormat48bppRGB:
834             convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
835         case PixelFormat64bppARGB:
836             convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
837         default:
838             break;
839         }
840         break;
841     default:
842         break;
843     }
844
845 #undef convert_indexed_to_rgb
846 #undef convert_rgb_to_rgb
847
848     return NotImplemented;
849 }
850
851 /* This function returns a pointer to an array of pixels that represents the
852  * bitmap. The *entire* bitmap is locked according to the lock mode specified by
853  * flags.  It is correct behavior that a user who calls this function with write
854  * privileges can write to the whole bitmap (not just the area in rect).
855  *
856  * FIXME: only used portion of format is bits per pixel. */
857 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
858     UINT flags, PixelFormat format, BitmapData* lockeddata)
859 {
860     INT stride, bitspp = PIXELFORMATBPP(format);
861     BYTE *buff = NULL;
862     UINT abs_height;
863     GpRect act_rect; /* actual rect to be used */
864     GpStatus stat;
865
866     TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
867
868     if(!lockeddata || !bitmap)
869         return InvalidParameter;
870
871     if(rect){
872         if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
873           (rect->Y + rect->Height > bitmap->height) || !flags)
874             return InvalidParameter;
875
876         act_rect = *rect;
877     }
878     else{
879         act_rect.X = act_rect.Y = 0;
880         act_rect.Width  = bitmap->width;
881         act_rect.Height = bitmap->height;
882     }
883
884     if(flags & ImageLockModeUserInputBuf)
885     {
886         static int fixme=0;
887         if (!fixme++) FIXME("ImageLockModeUserInputBuf not implemented\n");
888         return NotImplemented;
889     }
890
891     if(bitmap->lockmode)
892     {
893         WARN("bitmap is already locked and cannot be locked again\n");
894         return WrongState;
895     }
896
897     if (bitmap->bits && bitmap->format == format)
898     {
899         /* no conversion is necessary; just use the bits directly */
900         lockeddata->Width = act_rect.Width;
901         lockeddata->Height = act_rect.Height;
902         lockeddata->PixelFormat = format;
903         lockeddata->Reserved = flags;
904         lockeddata->Stride = bitmap->stride;
905         lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
906                             bitmap->stride * act_rect.Y;
907
908         bitmap->lockmode = flags;
909         bitmap->numlocks++;
910
911         return Ok;
912     }
913
914     /* Make sure we can convert to the requested format. */
915     stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
916     if (stat == NotImplemented)
917     {
918         FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
919         return NotImplemented;
920     }
921
922     /* If we're opening for writing, make sure we'll be able to write back in
923      * the original format. */
924     if (flags & ImageLockModeWrite)
925     {
926         stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
927         if (stat == NotImplemented)
928         {
929             FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
930             return NotImplemented;
931         }
932     }
933
934     abs_height = bitmap->height;
935     stride = (bitmap->width * bitspp + 7) / 8;
936     stride = (stride + 3) & ~3;
937
938     buff = GdipAlloc(stride * abs_height);
939
940     if (!buff) return OutOfMemory;
941
942     stat = convert_pixels(bitmap->width, bitmap->height,
943         stride, buff, format,
944         bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
945
946     if (stat != Ok)
947     {
948         GdipFree(buff);
949         return stat;
950     }
951
952     lockeddata->Width  = act_rect.Width;
953     lockeddata->Height = act_rect.Height;
954     lockeddata->PixelFormat = format;
955     lockeddata->Reserved = flags;
956     lockeddata->Stride = stride;
957     lockeddata->Scan0  = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
958
959     bitmap->lockmode = flags;
960     bitmap->numlocks++;
961     bitmap->bitmapbits = buff;
962
963     return Ok;
964 }
965
966 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
967 {
968     TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
969
970     if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
971         return InvalidParameter;
972
973     bitmap->image.xres = xdpi;
974     bitmap->image.yres = ydpi;
975
976     return Ok;
977 }
978
979 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
980     BitmapData* lockeddata)
981 {
982     GpStatus stat;
983
984     TRACE("(%p,%p)\n", bitmap, lockeddata);
985
986     if(!bitmap || !lockeddata)
987         return InvalidParameter;
988
989     if(!bitmap->lockmode)
990         return WrongState;
991
992     if(lockeddata->Reserved & ImageLockModeUserInputBuf)
993         return NotImplemented;
994
995     if(lockeddata->Reserved & ImageLockModeRead){
996         if(!(--bitmap->numlocks))
997             bitmap->lockmode = 0;
998
999         GdipFree(bitmap->bitmapbits);
1000         bitmap->bitmapbits = NULL;
1001         return Ok;
1002     }
1003
1004     if (!bitmap->bitmapbits)
1005     {
1006         /* we passed a direct reference; no need to do anything */
1007         bitmap->lockmode = 0;
1008         bitmap->numlocks = 0;
1009         return Ok;
1010     }
1011
1012     stat = convert_pixels(bitmap->width, bitmap->height,
1013         bitmap->stride, bitmap->bits, bitmap->format,
1014         lockeddata->Stride, bitmap->bitmapbits, lockeddata->PixelFormat, NULL);
1015
1016     if (stat != Ok)
1017     {
1018         ERR("failed to convert pixels; this should never happen\n");
1019     }
1020
1021     GdipFree(bitmap->bitmapbits);
1022     bitmap->bitmapbits = NULL;
1023     bitmap->lockmode = 0;
1024     bitmap->numlocks = 0;
1025
1026     return stat;
1027 }
1028
1029 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1030     PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1031 {
1032     BitmapData lockeddata_src, lockeddata_dst;
1033     int i;
1034     UINT row_size;
1035     Rect area;
1036     GpStatus stat;
1037
1038     TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1039
1040     if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1041         x < 0 || y < 0 ||
1042         x + width > srcBitmap->width || y + height > srcBitmap->height)
1043     {
1044         TRACE("<-- InvalidParameter\n");
1045         return InvalidParameter;
1046     }
1047
1048     if (format == PixelFormatDontCare)
1049         format = srcBitmap->format;
1050
1051     area.X = roundr(x);
1052     area.Y = roundr(y);
1053     area.Width = roundr(width);
1054     area.Height = roundr(height);
1055
1056     stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
1057         &lockeddata_src);
1058     if (stat != Ok) return stat;
1059
1060     stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1061         0, lockeddata_src.PixelFormat, NULL, dstBitmap);
1062     if (stat == Ok)
1063     {
1064         stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
1065             lockeddata_src.PixelFormat, &lockeddata_dst);
1066
1067         if (stat == Ok)
1068         {
1069             /* copy the image data */
1070             row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1071             for (i=0; i<lockeddata_src.Height; i++)
1072                 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1073                        (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1074                        row_size);
1075
1076             GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
1077         }
1078
1079         if (stat != Ok)
1080             GdipDisposeImage((GpImage*)*dstBitmap);
1081     }
1082
1083     GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
1084
1085     if (stat != Ok)
1086     {
1087         *dstBitmap = NULL;
1088     }
1089
1090     return stat;
1091 }
1092
1093 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1094     PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1095 {
1096     TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1097
1098     return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1099 }
1100
1101 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1102 {
1103     GpStatus stat = GenericError;
1104
1105     TRACE("%p, %p\n", image, cloneImage);
1106
1107     if (!image || !cloneImage)
1108         return InvalidParameter;
1109
1110     if (image->picture)
1111     {
1112         IStream* stream;
1113         HRESULT hr;
1114         INT size;
1115         LARGE_INTEGER move;
1116
1117         hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1118         if (FAILED(hr))
1119             return GenericError;
1120
1121         hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1122         if(FAILED(hr))
1123         {
1124             WARN("Failed to save image on stream\n");
1125             goto out;
1126         }
1127
1128         /* Set seek pointer back to the beginning of the picture */
1129         move.QuadPart = 0;
1130         hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1131         if (FAILED(hr))
1132             goto out;
1133
1134         stat = GdipLoadImageFromStream(stream, cloneImage);
1135         if (stat != Ok) WARN("Failed to load image from stream\n");
1136
1137     out:
1138         IStream_Release(stream);
1139         return stat;
1140     }
1141     else if (image->type == ImageTypeBitmap)
1142     {
1143         GpBitmap *bitmap = (GpBitmap*)image;
1144         BitmapData lockeddata_src, lockeddata_dst;
1145         int i;
1146         UINT row_size;
1147
1148         stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
1149             &lockeddata_src);
1150         if (stat != Ok) return stat;
1151
1152         stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1153             0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
1154         if (stat == Ok)
1155         {
1156             stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
1157                 lockeddata_src.PixelFormat, &lockeddata_dst);
1158
1159             if (stat == Ok)
1160             {
1161                 /* copy the image data */
1162                 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1163                 for (i=0; i<lockeddata_src.Height; i++)
1164                     memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1165                            (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1166                            row_size);
1167
1168                 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
1169             }
1170
1171             if (stat != Ok)
1172                 GdipDisposeImage(*cloneImage);
1173         }
1174
1175         GdipBitmapUnlockBits(bitmap, &lockeddata_src);
1176
1177         if (stat != Ok)
1178         {
1179             *cloneImage = NULL;
1180         }
1181         else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
1182
1183         return stat;
1184     }
1185     else
1186     {
1187         ERR("GpImage with no IPicture or bitmap?!\n");
1188         return NotImplemented;
1189     }
1190 }
1191
1192 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1193     GpBitmap **bitmap)
1194 {
1195     GpStatus stat;
1196     IStream *stream;
1197
1198     TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1199
1200     if(!filename || !bitmap)
1201         return InvalidParameter;
1202
1203     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1204
1205     if(stat != Ok)
1206         return stat;
1207
1208     stat = GdipCreateBitmapFromStream(stream, bitmap);
1209
1210     IStream_Release(stream);
1211
1212     return stat;
1213 }
1214
1215 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1216                                                VOID *bits, GpBitmap **bitmap)
1217 {
1218     DWORD height, stride;
1219     PixelFormat format;
1220
1221     FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1222
1223     if (!info || !bits || !bitmap)
1224         return InvalidParameter;
1225
1226     height = abs(info->bmiHeader.biHeight);
1227     stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1228
1229     if(info->bmiHeader.biHeight > 0) /* bottom-up */
1230     {
1231         bits = (BYTE*)bits + (height - 1) * stride;
1232         stride = -stride;
1233     }
1234
1235     switch(info->bmiHeader.biBitCount) {
1236     case 1:
1237         format = PixelFormat1bppIndexed;
1238         break;
1239     case 4:
1240         format = PixelFormat4bppIndexed;
1241         break;
1242     case 8:
1243         format = PixelFormat8bppIndexed;
1244         break;
1245     case 16:
1246         format = PixelFormat16bppRGB555;
1247         break;
1248     case 24:
1249         format = PixelFormat24bppRGB;
1250         break;
1251     case 32:
1252         format = PixelFormat32bppRGB;
1253         break;
1254     default:
1255         FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1256         *bitmap = NULL;
1257         return InvalidParameter;
1258     }
1259
1260     return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1261                                      bits, bitmap);
1262
1263 }
1264
1265 /* FIXME: no icm */
1266 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1267     GpBitmap **bitmap)
1268 {
1269     TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1270
1271     return GdipCreateBitmapFromFile(filename, bitmap);
1272 }
1273
1274 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1275     GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1276 {
1277     HBITMAP hbm;
1278     GpStatus stat = InvalidParameter;
1279
1280     TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1281
1282     if(!lpBitmapName || !bitmap)
1283         return InvalidParameter;
1284
1285     /* load DIB */
1286     hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1287                      LR_CREATEDIBSECTION);
1288
1289     if(hbm){
1290         stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1291         DeleteObject(hbm);
1292     }
1293
1294     return stat;
1295 }
1296
1297 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1298     HBITMAP* hbmReturn, ARGB background)
1299 {
1300     GpStatus stat;
1301     HBITMAP result, oldbitmap;
1302     UINT width, height;
1303     HDC hdc;
1304     GpGraphics *graphics;
1305     BITMAPINFOHEADER bih;
1306     void *bits;
1307     TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1308
1309     if (!bitmap || !hbmReturn) return InvalidParameter;
1310
1311     GdipGetImageWidth((GpImage*)bitmap, &width);
1312     GdipGetImageHeight((GpImage*)bitmap, &height);
1313
1314     bih.biSize = sizeof(bih);
1315     bih.biWidth = width;
1316     bih.biHeight = height;
1317     bih.biPlanes = 1;
1318     bih.biBitCount = 32;
1319     bih.biCompression = BI_RGB;
1320     bih.biSizeImage = 0;
1321     bih.biXPelsPerMeter = 0;
1322     bih.biYPelsPerMeter = 0;
1323     bih.biClrUsed = 0;
1324     bih.biClrImportant = 0;
1325
1326     hdc = CreateCompatibleDC(NULL);
1327     if (!hdc) return GenericError;
1328
1329     result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
1330         NULL, 0);
1331
1332     if (result)
1333     {
1334         oldbitmap = SelectObject(hdc, result);
1335
1336         stat = GdipCreateFromHDC(hdc, &graphics);
1337         if (stat == Ok)
1338         {
1339             stat = GdipGraphicsClear(graphics, background);
1340
1341             if (stat == Ok)
1342                 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
1343
1344             GdipDeleteGraphics(graphics);
1345         }
1346
1347         SelectObject(hdc, oldbitmap);
1348     }
1349     else
1350         stat = GenericError;
1351
1352     DeleteDC(hdc);
1353
1354     if (stat != Ok && result)
1355     {
1356         DeleteObject(result);
1357         result = NULL;
1358     }
1359
1360     *hbmReturn = result;
1361
1362     return stat;
1363 }
1364
1365 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1366     GpMetafile* metafile, BOOL* succ, EmfType emfType,
1367     const WCHAR* description, GpMetafile** out_metafile)
1368 {
1369     static int calls;
1370
1371     TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1372         debugstr_w(description), out_metafile);
1373
1374     if(!ref || !metafile || !out_metafile)
1375         return InvalidParameter;
1376
1377     *succ = FALSE;
1378     *out_metafile = NULL;
1379
1380     if(!(calls++))
1381         FIXME("not implemented\n");
1382
1383     return NotImplemented;
1384 }
1385
1386 /* FIXME: this should create a bitmap in the given size with the attributes
1387  * (resolution etc.) of the graphics object */
1388 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1389     GpGraphics* target, GpBitmap** bitmap)
1390 {
1391     static int calls;
1392     GpStatus ret;
1393
1394     if(!target || !bitmap)
1395         return InvalidParameter;
1396
1397     if(!(calls++))
1398         FIXME("hacked stub\n");
1399
1400     ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1401                                     NULL, bitmap);
1402
1403     return ret;
1404 }
1405
1406 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1407 {
1408     GpStatus stat;
1409     ICONINFO iinfo;
1410     BITMAP bm;
1411     int ret;
1412     UINT width, height;
1413     GpRect rect;
1414     BitmapData lockeddata;
1415     HDC screendc;
1416     BOOL has_alpha;
1417     int x, y;
1418     BYTE *bits;
1419     BITMAPINFOHEADER bih;
1420     DWORD *src;
1421     BYTE *dst_row;
1422     DWORD *dst;
1423
1424     TRACE("%p, %p\n", hicon, bitmap);
1425
1426     if(!bitmap || !GetIconInfo(hicon, &iinfo))
1427         return InvalidParameter;
1428
1429     /* get the size of the icon */
1430     ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1431     if (ret == 0) {
1432         DeleteObject(iinfo.hbmColor);
1433         DeleteObject(iinfo.hbmMask);
1434         return GenericError;
1435     }
1436
1437     width = bm.bmWidth;
1438
1439     if (iinfo.hbmColor)
1440         height = abs(bm.bmHeight);
1441     else /* combined bitmap + mask */
1442         height = abs(bm.bmHeight) / 2;
1443
1444     bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1445     if (!bits) {
1446         DeleteObject(iinfo.hbmColor);
1447         DeleteObject(iinfo.hbmMask);
1448         return OutOfMemory;
1449     }
1450
1451     stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1452     if (stat != Ok) {
1453         DeleteObject(iinfo.hbmColor);
1454         DeleteObject(iinfo.hbmMask);
1455         HeapFree(GetProcessHeap(), 0, bits);
1456         return stat;
1457     }
1458
1459     rect.X = 0;
1460     rect.Y = 0;
1461     rect.Width = width;
1462     rect.Height = height;
1463
1464     stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1465     if (stat != Ok) {
1466         DeleteObject(iinfo.hbmColor);
1467         DeleteObject(iinfo.hbmMask);
1468         HeapFree(GetProcessHeap(), 0, bits);
1469         GdipDisposeImage((GpImage*)*bitmap);
1470         return stat;
1471     }
1472
1473     bih.biSize = sizeof(bih);
1474     bih.biWidth = width;
1475     bih.biHeight = -height;
1476     bih.biPlanes = 1;
1477     bih.biBitCount = 32;
1478     bih.biCompression = BI_RGB;
1479     bih.biSizeImage = 0;
1480     bih.biXPelsPerMeter = 0;
1481     bih.biYPelsPerMeter = 0;
1482     bih.biClrUsed = 0;
1483     bih.biClrImportant = 0;
1484
1485     screendc = GetDC(0);
1486     if (iinfo.hbmColor)
1487     {
1488         GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1489
1490         if (bm.bmBitsPixel == 32)
1491         {
1492             has_alpha = FALSE;
1493
1494             /* If any pixel has a non-zero alpha, ignore hbmMask */
1495             src = (DWORD*)bits;
1496             for (x=0; x<width && !has_alpha; x++)
1497                 for (y=0; y<height && !has_alpha; y++)
1498                     if ((*src++ & 0xff000000) != 0)
1499                         has_alpha = TRUE;
1500         }
1501         else has_alpha = FALSE;
1502     }
1503     else
1504     {
1505         GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1506         has_alpha = FALSE;
1507     }
1508
1509     /* copy the image data to the Bitmap */
1510     src = (DWORD*)bits;
1511     dst_row = lockeddata.Scan0;
1512     for (y=0; y<height; y++)
1513     {
1514         memcpy(dst_row, src, width*4);
1515         src += width;
1516         dst_row += lockeddata.Stride;
1517     }
1518
1519     if (!has_alpha)
1520     {
1521         if (iinfo.hbmMask)
1522         {
1523             /* read alpha data from the mask */
1524             if (iinfo.hbmColor)
1525                 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1526             else
1527                 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1528
1529             src = (DWORD*)bits;
1530             dst_row = lockeddata.Scan0;
1531             for (y=0; y<height; y++)
1532             {
1533                 dst = (DWORD*)dst_row;
1534                 for (x=0; x<height; x++)
1535                 {
1536                     DWORD src_value = *src++;
1537                     if (src_value)
1538                         *dst++ = 0;
1539                     else
1540                         *dst++ |= 0xff000000;
1541                 }
1542                 dst_row += lockeddata.Stride;
1543             }
1544         }
1545         else
1546         {
1547             /* set constant alpha of 255 */
1548             dst_row = bits;
1549             for (y=0; y<height; y++)
1550             {
1551                 dst = (DWORD*)dst_row;
1552                 for (x=0; x<height; x++)
1553                     *dst++ |= 0xff000000;
1554                 dst_row += lockeddata.Stride;
1555             }
1556         }
1557     }
1558
1559     ReleaseDC(0, screendc);
1560
1561     DeleteObject(iinfo.hbmColor);
1562     DeleteObject(iinfo.hbmMask);
1563
1564     GdipBitmapUnlockBits(*bitmap, &lockeddata);
1565
1566     HeapFree(GetProcessHeap(), 0, bits);
1567
1568     return Ok;
1569 }
1570
1571 static void generate_halftone_palette(ARGB *entries, UINT count)
1572 {
1573     static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1574     UINT i;
1575
1576     for (i=0; i<8 && i<count; i++)
1577     {
1578         entries[i] = 0xff000000;
1579         if (i&1) entries[i] |= 0x800000;
1580         if (i&2) entries[i] |= 0x8000;
1581         if (i&4) entries[i] |= 0x80;
1582     }
1583
1584     if (8 < count)
1585         entries[i] = 0xffc0c0c0;
1586
1587     for (i=9; i<16 && i<count; i++)
1588     {
1589         entries[i] = 0xff000000;
1590         if (i&1) entries[i] |= 0xff0000;
1591         if (i&2) entries[i] |= 0xff00;
1592         if (i&4) entries[i] |= 0xff;
1593     }
1594
1595     for (i=16; i<40 && i<count; i++)
1596     {
1597         entries[i] = 0;
1598     }
1599
1600     for (i=40; i<256 && i<count; i++)
1601     {
1602         entries[i] = 0xff000000;
1603         entries[i] |= halftone_values[(i-40)%6];
1604         entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1605         entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1606     }
1607 }
1608
1609 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1610 {
1611     HDC screendc = GetDC(0);
1612
1613     if (!screendc) return GenericError;
1614
1615     *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1616     *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1617
1618     ReleaseDC(0, screendc);
1619
1620     return Ok;
1621 }
1622
1623 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1624     PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1625 {
1626     BITMAPINFO* pbmi;
1627     HBITMAP hbitmap;
1628     INT row_size, dib_stride;
1629     HDC hdc;
1630     BYTE *bits;
1631     int i;
1632     REAL xres, yres;
1633     GpStatus stat;
1634
1635     TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1636
1637     if (!bitmap) return InvalidParameter;
1638
1639     if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1640         *bitmap = NULL;
1641         return InvalidParameter;
1642     }
1643
1644     if(scan0 && !stride)
1645         return InvalidParameter;
1646
1647     stat = get_screen_resolution(&xres, &yres);
1648     if (stat != Ok) return stat;
1649
1650     row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1651     dib_stride = (row_size + 3) & ~3;
1652
1653     if(stride == 0)
1654         stride = dib_stride;
1655
1656     pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1657     if (!pbmi)
1658         return OutOfMemory;
1659
1660     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1661     pbmi->bmiHeader.biWidth = width;
1662     pbmi->bmiHeader.biHeight = -height;
1663     pbmi->bmiHeader.biPlanes = 1;
1664     /* FIXME: use the rest of the data from format */
1665     pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1666     pbmi->bmiHeader.biCompression = BI_RGB;
1667     pbmi->bmiHeader.biSizeImage = 0;
1668     pbmi->bmiHeader.biXPelsPerMeter = 0;
1669     pbmi->bmiHeader.biYPelsPerMeter = 0;
1670     pbmi->bmiHeader.biClrUsed = 0;
1671     pbmi->bmiHeader.biClrImportant = 0;
1672
1673     hdc = CreateCompatibleDC(NULL);
1674     if (!hdc) {
1675         GdipFree(pbmi);
1676         return GenericError;
1677     }
1678
1679     hbitmap = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1680
1681     DeleteDC(hdc);
1682     GdipFree(pbmi);
1683
1684     if (!hbitmap) return GenericError;
1685
1686     /* copy bits to the dib if necessary */
1687     /* FIXME: should reference the bits instead of copying them */
1688     if (scan0)
1689         for (i=0; i<height; i++)
1690             memcpy(bits+i*dib_stride, scan0+i*stride, row_size);
1691
1692     *bitmap = GdipAlloc(sizeof(GpBitmap));
1693     if(!*bitmap)
1694     {
1695         DeleteObject(hbitmap);
1696         return OutOfMemory;
1697     }
1698
1699     (*bitmap)->image.type = ImageTypeBitmap;
1700     memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1701     (*bitmap)->image.flags = ImageFlagsNone;
1702     (*bitmap)->image.palette_flags = 0;
1703     (*bitmap)->image.palette_count = 0;
1704     (*bitmap)->image.palette_size = 0;
1705     (*bitmap)->image.palette_entries = NULL;
1706     (*bitmap)->image.xres = xres;
1707     (*bitmap)->image.yres = yres;
1708     (*bitmap)->width = width;
1709     (*bitmap)->height = height;
1710     (*bitmap)->format = format;
1711     (*bitmap)->image.picture = NULL;
1712     (*bitmap)->hbitmap = hbitmap;
1713     (*bitmap)->hdc = NULL;
1714     (*bitmap)->bits = bits;
1715     (*bitmap)->stride = dib_stride;
1716
1717     if (format == PixelFormat1bppIndexed ||
1718         format == PixelFormat4bppIndexed ||
1719         format == PixelFormat8bppIndexed)
1720     {
1721         (*bitmap)->image.palette_size = (*bitmap)->image.palette_count = 1 << PIXELFORMATBPP(format);
1722         (*bitmap)->image.palette_entries = GdipAlloc(sizeof(ARGB) * ((*bitmap)->image.palette_size));
1723
1724         if (!(*bitmap)->image.palette_entries)
1725         {
1726             GdipDisposeImage(&(*bitmap)->image);
1727             *bitmap = NULL;
1728             return OutOfMemory;
1729         }
1730
1731         if (format == PixelFormat1bppIndexed)
1732         {
1733             (*bitmap)->image.palette_flags = PaletteFlagsGrayScale;
1734             (*bitmap)->image.palette_entries[0] = 0xff000000;
1735             (*bitmap)->image.palette_entries[1] = 0xffffffff;
1736         }
1737         else
1738         {
1739             if (format == PixelFormat8bppIndexed)
1740                 (*bitmap)->image.palette_flags = PaletteFlagsHalftone;
1741
1742             generate_halftone_palette((*bitmap)->image.palette_entries,
1743                 (*bitmap)->image.palette_count);
1744         }
1745     }
1746
1747     TRACE("<-- %p\n", *bitmap);
1748
1749     return Ok;
1750 }
1751
1752 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1753     GpBitmap **bitmap)
1754 {
1755     GpStatus stat;
1756
1757     TRACE("%p %p\n", stream, bitmap);
1758
1759     stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1760
1761     if(stat != Ok)
1762         return stat;
1763
1764     if((*bitmap)->image.type != ImageTypeBitmap){
1765         GdipDisposeImage(&(*bitmap)->image);
1766         *bitmap = NULL;
1767         return GenericError; /* FIXME: what error to return? */
1768     }
1769
1770     return Ok;
1771 }
1772
1773 /* FIXME: no icm */
1774 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1775     GpBitmap **bitmap)
1776 {
1777     TRACE("%p %p\n", stream, bitmap);
1778
1779     return GdipCreateBitmapFromStream(stream, bitmap);
1780 }
1781
1782 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1783     GpCachedBitmap **cachedbmp)
1784 {
1785     GpStatus stat;
1786
1787     TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1788
1789     if(!bitmap || !graphics || !cachedbmp)
1790         return InvalidParameter;
1791
1792     *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1793     if(!*cachedbmp)
1794         return OutOfMemory;
1795
1796     stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1797     if(stat != Ok){
1798         GdipFree(*cachedbmp);
1799         return stat;
1800     }
1801
1802     return Ok;
1803 }
1804
1805 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1806 {
1807     GpStatus stat;
1808     BitmapData lockeddata;
1809     ULONG andstride, xorstride, bitssize;
1810     LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1811     UINT x, y;
1812
1813     TRACE("(%p, %p)\n", bitmap, hicon);
1814
1815     if (!bitmap || !hicon)
1816         return InvalidParameter;
1817
1818     stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1819         PixelFormat32bppPARGB, &lockeddata);
1820     if (stat == Ok)
1821     {
1822         andstride = ((lockeddata.Width+31)/32)*4;
1823         xorstride = lockeddata.Width*4;
1824         bitssize = (andstride + xorstride) * lockeddata.Height;
1825
1826         andbits = GdipAlloc(bitssize);
1827
1828         if (andbits)
1829         {
1830             xorbits = andbits + andstride * lockeddata.Height;
1831
1832             for (y=0; y<lockeddata.Height; y++)
1833             {
1834                 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
1835
1836                 androw = andbits + andstride * y;
1837                 for (x=0; x<lockeddata.Width; x++)
1838                     if (srcrow[3+4*x] >= 128)
1839                         androw[x/8] |= 1 << (7-x%8);
1840
1841                 xorrow = xorbits + xorstride * y;
1842                 memcpy(xorrow, srcrow, xorstride);
1843             }
1844
1845             *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
1846                 andbits, xorbits);
1847
1848             GdipFree(andbits);
1849         }
1850         else
1851             stat = OutOfMemory;
1852
1853         GdipBitmapUnlockBits(bitmap, &lockeddata);
1854     }
1855
1856     return stat;
1857 }
1858
1859 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1860 {
1861     TRACE("%p\n", cachedbmp);
1862
1863     if(!cachedbmp)
1864         return InvalidParameter;
1865
1866     GdipDisposeImage(cachedbmp->image);
1867     GdipFree(cachedbmp);
1868
1869     return Ok;
1870 }
1871
1872 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1873     GpCachedBitmap *cachedbmp, INT x, INT y)
1874 {
1875     TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1876
1877     if(!graphics || !cachedbmp)
1878         return InvalidParameter;
1879
1880     return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1881 }
1882
1883 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1884     LPBYTE pData16, INT iMapMode, INT eFlags)
1885 {
1886     FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1887     return NotImplemented;
1888 }
1889
1890 /* Internal utility function: Replace the image data of dst with that of src,
1891  * and free src. */
1892 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
1893 {
1894     GdipFree(dst->bitmapbits);
1895     DeleteDC(dst->hdc);
1896     DeleteObject(dst->hbitmap);
1897
1898     if (clobber_palette)
1899     {
1900         GdipFree(dst->image.palette_entries);
1901         dst->image.palette_flags = src->image.palette_flags;
1902         dst->image.palette_count = src->image.palette_count;
1903         dst->image.palette_entries = src->image.palette_entries;
1904     }
1905     else
1906         GdipFree(src->image.palette_entries);
1907
1908     dst->image.xres = src->image.xres;
1909     dst->image.yres = src->image.yres;
1910     dst->width = src->width;
1911     dst->height = src->height;
1912     dst->format = src->format;
1913     dst->hbitmap = src->hbitmap;
1914     dst->hdc = src->hdc;
1915     dst->bits = src->bits;
1916     dst->stride = src->stride;
1917
1918     GdipFree(src);
1919 }
1920
1921 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
1922 {
1923     TRACE("%p\n", image);
1924
1925     if(!image)
1926         return InvalidParameter;
1927
1928     if (image->picture)
1929         IPicture_Release(image->picture);
1930     if (image->type == ImageTypeBitmap)
1931     {
1932         GdipFree(((GpBitmap*)image)->bitmapbits);
1933         DeleteDC(((GpBitmap*)image)->hdc);
1934         DeleteObject(((GpBitmap*)image)->hbitmap);
1935     }
1936     GdipFree(image->palette_entries);
1937     GdipFree(image);
1938
1939     return Ok;
1940 }
1941
1942 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
1943 {
1944     static int calls;
1945
1946     TRACE("(%p,%p)\n", image, item);
1947
1948     if(!image || !item)
1949         return InvalidParameter;
1950
1951     if (!(calls++))
1952         FIXME("not implemented\n");
1953
1954     return NotImplemented;
1955 }
1956
1957 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
1958 {
1959     static int calls;
1960
1961     TRACE("(%p,%p)\n", image, item);
1962
1963     if (!(calls++))
1964         FIXME("not implemented\n");
1965
1966     return NotImplemented;
1967 }
1968
1969 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
1970     GpUnit *srcUnit)
1971 {
1972     TRACE("%p %p %p\n", image, srcRect, srcUnit);
1973
1974     if(!image || !srcRect || !srcUnit)
1975         return InvalidParameter;
1976     if(image->type == ImageTypeMetafile){
1977         *srcRect = ((GpMetafile*)image)->bounds;
1978         *srcUnit = ((GpMetafile*)image)->unit;
1979     }
1980     else if(image->type == ImageTypeBitmap){
1981         srcRect->X = srcRect->Y = 0.0;
1982         srcRect->Width = (REAL) ((GpBitmap*)image)->width;
1983         srcRect->Height = (REAL) ((GpBitmap*)image)->height;
1984         *srcUnit = UnitPixel;
1985     }
1986     else{
1987         srcRect->X = srcRect->Y = 0.0;
1988         srcRect->Width = ipicture_pixel_width(image->picture);
1989         srcRect->Height = ipicture_pixel_height(image->picture);
1990         *srcUnit = UnitPixel;
1991     }
1992
1993     TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
1994           srcRect->Width, srcRect->Height, *srcUnit);
1995
1996     return Ok;
1997 }
1998
1999 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2000     REAL *height)
2001 {
2002     TRACE("%p %p %p\n", image, width, height);
2003
2004     if(!image || !height || !width)
2005         return InvalidParameter;
2006
2007     if(image->type == ImageTypeMetafile){
2008         HDC hdc = GetDC(0);
2009
2010         *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
2011                         ((GpMetafile*)image)->bounds.Height;
2012
2013         *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
2014                         ((GpMetafile*)image)->bounds.Width;
2015
2016         ReleaseDC(0, hdc);
2017     }
2018
2019     else if(image->type == ImageTypeBitmap){
2020         *height = ((GpBitmap*)image)->height;
2021         *width = ((GpBitmap*)image)->width;
2022     }
2023     else{
2024         *height = ipicture_pixel_height(image->picture);
2025         *width = ipicture_pixel_width(image->picture);
2026     }
2027
2028     TRACE("returning (%f, %f)\n", *height, *width);
2029     return Ok;
2030 }
2031
2032 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2033     GpGraphics **graphics)
2034 {
2035     HDC hdc;
2036     GpStatus stat;
2037
2038     TRACE("%p %p\n", image, graphics);
2039
2040     if(!image || !graphics)
2041         return InvalidParameter;
2042
2043     if(image->type != ImageTypeBitmap){
2044         FIXME("not implemented for image type %d\n", image->type);
2045         return NotImplemented;
2046     }
2047
2048     hdc = ((GpBitmap*)image)->hdc;
2049
2050     if(!hdc){
2051         hdc = CreateCompatibleDC(0);
2052         SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2053         ((GpBitmap*)image)->hdc = hdc;
2054     }
2055
2056     stat = GdipCreateFromHDC(hdc, graphics);
2057
2058     if (stat == Ok)
2059         (*graphics)->image = image;
2060
2061     return stat;
2062 }
2063
2064 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2065 {
2066     TRACE("%p %p\n", image, height);
2067
2068     if(!image || !height)
2069         return InvalidParameter;
2070
2071     if(image->type == ImageTypeMetafile){
2072         HDC hdc = GetDC(0);
2073
2074         *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
2075                         ((GpMetafile*)image)->bounds.Height);
2076
2077         ReleaseDC(0, hdc);
2078     }
2079     else if(image->type == ImageTypeBitmap)
2080         *height = ((GpBitmap*)image)->height;
2081     else
2082         *height = ipicture_pixel_height(image->picture);
2083
2084     TRACE("returning %d\n", *height);
2085
2086     return Ok;
2087 }
2088
2089 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2090 {
2091     if(!image || !res)
2092         return InvalidParameter;
2093
2094     *res = image->xres;
2095
2096     TRACE("(%p) <-- %0.2f\n", image, *res);
2097
2098     return Ok;
2099 }
2100
2101 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2102 {
2103     TRACE("%p %p\n", image, size);
2104
2105     if(!image || !size)
2106         return InvalidParameter;
2107
2108     if (image->palette_count == 0)
2109         *size = sizeof(ColorPalette);
2110     else
2111         *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette_count;
2112
2113     TRACE("<-- %u\n", *size);
2114
2115     return Ok;
2116 }
2117
2118 /* FIXME: test this function for non-bitmap types */
2119 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2120 {
2121     TRACE("%p %p\n", image, format);
2122
2123     if(!image || !format)
2124         return InvalidParameter;
2125
2126     if(image->type != ImageTypeBitmap)
2127         *format = PixelFormat24bppRGB;
2128     else
2129         *format = ((GpBitmap*) image)->format;
2130
2131     return Ok;
2132 }
2133
2134 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2135 {
2136     if(!image || !format)
2137         return InvalidParameter;
2138
2139     memcpy(format, &image->format, sizeof(GUID));
2140
2141     return Ok;
2142 }
2143
2144 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2145 {
2146     TRACE("%p %p\n", image, type);
2147
2148     if(!image || !type)
2149         return InvalidParameter;
2150
2151     *type = image->type;
2152
2153     return Ok;
2154 }
2155
2156 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2157 {
2158     if(!image || !res)
2159         return InvalidParameter;
2160
2161     *res = image->yres;
2162
2163     TRACE("(%p) <-- %0.2f\n", image, *res);
2164
2165     return Ok;
2166 }
2167
2168 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2169 {
2170     TRACE("%p %p\n", image, width);
2171
2172     if(!image || !width)
2173         return InvalidParameter;
2174
2175     if(image->type == ImageTypeMetafile){
2176         HDC hdc = GetDC(0);
2177
2178         *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
2179                         ((GpMetafile*)image)->bounds.Width);
2180
2181         ReleaseDC(0, hdc);
2182     }
2183     else if(image->type == ImageTypeBitmap)
2184         *width = ((GpBitmap*)image)->width;
2185     else
2186         *width = ipicture_pixel_width(image->picture);
2187
2188     TRACE("returning %d\n", *width);
2189
2190     return Ok;
2191 }
2192
2193 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2194     MetafileHeader * header)
2195 {
2196     static int calls;
2197
2198     if(!metafile || !header)
2199         return InvalidParameter;
2200
2201     if(!(calls++))
2202         FIXME("not implemented\n");
2203
2204     memset(header, 0, sizeof(MetafileHeader));
2205
2206     return Ok;
2207 }
2208
2209 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2210     UINT num, PropertyItem* items)
2211 {
2212     static int calls;
2213
2214     if(!(calls++))
2215         FIXME("not implemented\n");
2216
2217     return InvalidParameter;
2218 }
2219
2220 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
2221 {
2222     static int calls;
2223
2224     if(!(calls++))
2225         FIXME("not implemented\n");
2226
2227     return InvalidParameter;
2228 }
2229
2230 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
2231 {
2232     static int calls;
2233
2234     if(!(calls++))
2235         FIXME("not implemented\n");
2236
2237     return InvalidParameter;
2238 }
2239
2240 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
2241     PropertyItem* buffer)
2242 {
2243     static int calls;
2244
2245     if(!(calls++))
2246         FIXME("not implemented\n");
2247
2248     return InvalidParameter;
2249 }
2250
2251 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
2252     UINT* size)
2253 {
2254     static int calls;
2255
2256     TRACE("%p %x %p\n", image, pid, size);
2257
2258     if(!size || !image)
2259         return InvalidParameter;
2260
2261     if(!(calls++))
2262         FIXME("not implemented\n");
2263
2264     return NotImplemented;
2265 }
2266
2267 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
2268 {
2269     static int calls;
2270
2271     TRACE("(%p,%p,%p)\n", image, size, num);
2272
2273     if(!(calls++))
2274         FIXME("not implemented\n");
2275
2276     return InvalidParameter;
2277 }
2278
2279 struct image_format_dimension
2280 {
2281     const GUID *format;
2282     const GUID *dimension;
2283 };
2284
2285 struct image_format_dimension image_format_dimensions[] =
2286 {
2287     {&ImageFormatGIF, &FrameDimensionTime},
2288     {&ImageFormatIcon, &FrameDimensionResolution},
2289     {NULL}
2290 };
2291
2292 /* FIXME: Need to handle multi-framed images */
2293 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2294     GDIPCONST GUID* dimensionID, UINT* count)
2295 {
2296     static int calls;
2297
2298     TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2299
2300     if(!image || !count)
2301         return InvalidParameter;
2302
2303     if(!(calls++))
2304         FIXME("returning frame count of 1\n");
2305
2306     *count = 1;
2307
2308     return Ok;
2309 }
2310
2311 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2312     UINT* count)
2313 {
2314     /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2315
2316     if(!image || !count)
2317         return InvalidParameter;
2318
2319     *count = 1;
2320
2321     return Ok;
2322 }
2323
2324 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2325     GUID* dimensionIDs, UINT count)
2326 {
2327     int i;
2328     const GUID *result=NULL;
2329
2330     TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2331
2332     if(!image || !dimensionIDs || count != 1)
2333         return InvalidParameter;
2334
2335     for (i=0; image_format_dimensions[i].format; i++)
2336     {
2337         if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2338         {
2339             result = image_format_dimensions[i].dimension;
2340             break;
2341         }
2342     }
2343
2344     if (!result)
2345         result = &FrameDimensionPage;
2346
2347     memcpy(dimensionIDs, result, sizeof(GUID));
2348
2349     return Ok;
2350 }
2351
2352 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
2353     GDIPCONST GUID* dimensionID, UINT frameidx)
2354 {
2355     static int calls;
2356
2357     if(!image || !dimensionID)
2358         return InvalidParameter;
2359
2360     if(!(calls++))
2361         FIXME("not implemented\n");
2362
2363     return Ok;
2364 }
2365
2366 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2367                                           GpImage **image)
2368 {
2369     GpStatus stat;
2370     IStream *stream;
2371
2372     TRACE("(%s) %p\n", debugstr_w(filename), image);
2373
2374     if (!filename || !image)
2375         return InvalidParameter;
2376
2377     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2378
2379     if (stat != Ok)
2380         return stat;
2381
2382     stat = GdipLoadImageFromStream(stream, image);
2383
2384     IStream_Release(stream);
2385
2386     return stat;
2387 }
2388
2389 /* FIXME: no icm handling */
2390 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2391 {
2392     TRACE("(%s) %p\n", debugstr_w(filename), image);
2393
2394     return GdipLoadImageFromFile(filename, image);
2395 }
2396
2397 static const WICPixelFormatGUID *wic_pixel_formats[] = {
2398     &GUID_WICPixelFormat16bppBGR555,
2399     &GUID_WICPixelFormat24bppBGR,
2400     &GUID_WICPixelFormat32bppBGR,
2401     &GUID_WICPixelFormat32bppBGRA,
2402     &GUID_WICPixelFormat32bppPBGRA,
2403     NULL
2404 };
2405
2406 static const PixelFormat wic_gdip_formats[] = {
2407     PixelFormat16bppRGB555,
2408     PixelFormat24bppRGB,
2409     PixelFormat32bppRGB,
2410     PixelFormat32bppARGB,
2411     PixelFormat32bppPARGB,
2412 };
2413
2414 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
2415 {
2416     GpStatus status=Ok;
2417     GpBitmap *bitmap;
2418     HRESULT hr;
2419     IWICBitmapDecoder *decoder;
2420     IWICBitmapFrameDecode *frame;
2421     IWICBitmapSource *source=NULL;
2422     WICPixelFormatGUID wic_format;
2423     PixelFormat gdip_format=0;
2424     int i;
2425     UINT width, height;
2426     BitmapData lockeddata;
2427     WICRect wrc;
2428     HRESULT initresult;
2429
2430     initresult = CoInitialize(NULL);
2431
2432     hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2433         &IID_IWICBitmapDecoder, (void**)&decoder);
2434     if (FAILED(hr)) goto end;
2435
2436     hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
2437     if (SUCCEEDED(hr))
2438         hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
2439
2440     if (SUCCEEDED(hr)) /* got frame */
2441     {
2442         hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
2443
2444         if (SUCCEEDED(hr))
2445         {
2446             for (i=0; wic_pixel_formats[i]; i++)
2447             {
2448                 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
2449                 {
2450                     source = (IWICBitmapSource*)frame;
2451                     IWICBitmapSource_AddRef(source);
2452                     gdip_format = wic_gdip_formats[i];
2453                     break;
2454                 }
2455             }
2456             if (!source)
2457             {
2458                 /* unknown format; fall back on 32bppARGB */
2459                 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
2460                 gdip_format = PixelFormat32bppARGB;
2461             }
2462         }
2463
2464         if (SUCCEEDED(hr)) /* got source */
2465         {
2466             hr = IWICBitmapSource_GetSize(source, &width, &height);
2467
2468             if (SUCCEEDED(hr))
2469                 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
2470                     NULL, &bitmap);
2471
2472             if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
2473             {
2474                 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
2475                     gdip_format, &lockeddata);
2476                 if (status == Ok) /* locked bitmap */
2477                 {
2478                     wrc.X = 0;
2479                     wrc.Width = width;
2480                     wrc.Height = 1;
2481                     for (i=0; i<height; i++)
2482                     {
2483                         wrc.Y = i;
2484                         hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
2485                             abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
2486                         if (FAILED(hr)) break;
2487                     }
2488
2489                     GdipBitmapUnlockBits(bitmap, &lockeddata);
2490                 }
2491
2492                 if (SUCCEEDED(hr) && status == Ok)
2493                     *image = (GpImage*)bitmap;
2494                 else
2495                 {
2496                     *image = NULL;
2497                     GdipDisposeImage((GpImage*)bitmap);
2498                 }
2499             }
2500
2501             IWICBitmapSource_Release(source);
2502         }
2503
2504         IWICBitmapFrameDecode_Release(frame);
2505     }
2506
2507     IWICBitmapDecoder_Release(decoder);
2508
2509 end:
2510     if (SUCCEEDED(initresult)) CoUninitialize();
2511
2512     if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
2513
2514     return status;
2515 }
2516
2517 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
2518 {
2519     return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
2520 }
2521
2522 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, GpImage **image)
2523 {
2524     GpStatus status;
2525     GpBitmap* bitmap;
2526
2527     status = decode_image_wic(stream, &CLSID_WICBmpDecoder, image);
2528
2529     bitmap = (GpBitmap*)*image;
2530
2531     if (status == Ok && bitmap->format == PixelFormat32bppARGB)
2532     {
2533         /* WIC supports bmp files with alpha, but gdiplus does not */
2534         bitmap->format = PixelFormat32bppRGB;
2535     }
2536
2537     return status;
2538 }
2539
2540 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
2541 {
2542     return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
2543 }
2544
2545 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, GpImage **image)
2546 {
2547     return decode_image_wic(stream, &CLSID_WICPngDecoder, image);
2548 }
2549
2550 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
2551 {
2552     return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
2553 }
2554
2555 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, GpImage **image)
2556 {
2557     return decode_image_wic(stream, &CLSID_WICTiffDecoder, image);
2558 }
2559
2560 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
2561 {
2562     IPicture *pic;
2563
2564     TRACE("%p %p\n", stream, image);
2565
2566     if(!stream || !image)
2567         return InvalidParameter;
2568
2569     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2570         (LPVOID*) &pic) != S_OK){
2571         TRACE("Could not load picture\n");
2572         return GenericError;
2573     }
2574
2575     /* FIXME: missing initialization code */
2576     *image = GdipAlloc(sizeof(GpMetafile));
2577     if(!*image) return OutOfMemory;
2578     (*image)->type = ImageTypeMetafile;
2579     (*image)->picture = pic;
2580     (*image)->flags   = ImageFlagsNone;
2581     (*image)->palette_flags = 0;
2582     (*image)->palette_count = 0;
2583     (*image)->palette_size = 0;
2584     (*image)->palette_entries = NULL;
2585
2586     TRACE("<-- %p\n", *image);
2587
2588     return Ok;
2589 }
2590
2591 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
2592     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
2593
2594 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
2595
2596 typedef struct image_codec {
2597     ImageCodecInfo info;
2598     encode_image_func encode_func;
2599     decode_image_func decode_func;
2600 } image_codec;
2601
2602 typedef enum {
2603     BMP,
2604     JPEG,
2605     GIF,
2606     TIFF,
2607     EMF,
2608     WMF,
2609     PNG,
2610     ICO,
2611     NUM_CODECS
2612 } ImageFormat;
2613
2614 static const struct image_codec codecs[NUM_CODECS];
2615
2616 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
2617 {
2618     BYTE signature[8];
2619     const BYTE *pattern, *mask;
2620     LARGE_INTEGER seek;
2621     HRESULT hr;
2622     UINT bytesread;
2623     int i, j, sig;
2624
2625     /* seek to the start of the stream */
2626     seek.QuadPart = 0;
2627     hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2628     if (FAILED(hr)) return hresult_to_status(hr);
2629
2630     /* read the first 8 bytes */
2631     /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
2632     hr = IStream_Read(stream, signature, 8, &bytesread);
2633     if (FAILED(hr)) return hresult_to_status(hr);
2634     if (hr == S_FALSE || bytesread == 0) return GenericError;
2635
2636     for (i = 0; i < NUM_CODECS; i++) {
2637         if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
2638             bytesread >= codecs[i].info.SigSize)
2639         {
2640             for (sig=0; sig<codecs[i].info.SigCount; sig++)
2641             {
2642                 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
2643                 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
2644                 for (j=0; j<codecs[i].info.SigSize; j++)
2645                     if ((signature[j] & mask[j]) != pattern[j])
2646                         break;
2647                 if (j == codecs[i].info.SigSize)
2648                 {
2649                     *result = &codecs[i];
2650                     return Ok;
2651                 }
2652             }
2653         }
2654     }
2655
2656     TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
2657         signature[0],signature[1],signature[2],signature[3],
2658         signature[4],signature[5],signature[6],signature[7]);
2659
2660     return GenericError;
2661 }
2662
2663 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
2664 {
2665     GpStatus stat;
2666     LARGE_INTEGER seek;
2667     HRESULT hr;
2668     const struct image_codec *codec=NULL;
2669
2670     /* choose an appropriate image decoder */
2671     stat = get_decoder_info(stream, &codec);
2672     if (stat != Ok) return stat;
2673
2674     /* seek to the start of the stream */
2675     seek.QuadPart = 0;
2676     hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2677     if (FAILED(hr)) return hresult_to_status(hr);
2678
2679     /* call on the image decoder to do the real work */
2680     stat = codec->decode_func(stream, &codec->info.Clsid, image);
2681
2682     /* take note of the original data format */
2683     if (stat == Ok)
2684     {
2685         memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
2686     }
2687
2688     return stat;
2689 }
2690
2691 /* FIXME: no ICM */
2692 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
2693 {
2694     TRACE("%p %p\n", stream, image);
2695
2696     return GdipLoadImageFromStream(stream, image);
2697 }
2698
2699 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
2700 {
2701     static int calls;
2702
2703     TRACE("(%p,%u)\n", image, propId);
2704
2705     if(!image)
2706         return InvalidParameter;
2707
2708     if(!(calls++))
2709         FIXME("not implemented\n");
2710
2711     return NotImplemented;
2712 }
2713
2714 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
2715 {
2716     static int calls;
2717
2718     TRACE("(%p,%p)\n", image, item);
2719
2720     if(!(calls++))
2721         FIXME("not implemented\n");
2722
2723     return NotImplemented;
2724 }
2725
2726 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
2727                                         GDIPCONST CLSID *clsidEncoder,
2728                                         GDIPCONST EncoderParameters *encoderParams)
2729 {
2730     GpStatus stat;
2731     IStream *stream;
2732
2733     TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
2734
2735     if (!image || !filename|| !clsidEncoder)
2736         return InvalidParameter;
2737
2738     stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
2739     if (stat != Ok)
2740         return GenericError;
2741
2742     stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
2743
2744     IStream_Release(stream);
2745     return stat;
2746 }
2747
2748 /*************************************************************************
2749  * Encoding functions -
2750  *   These functions encode an image in different image file formats.
2751  */
2752 #define BITMAP_FORMAT_BMP   0x4d42 /* "BM" */
2753 #define BITMAP_FORMAT_JPEG  0xd8ff
2754 #define BITMAP_FORMAT_GIF   0x4947
2755 #define BITMAP_FORMAT_PNG   0x5089
2756 #define BITMAP_FORMAT_APM   0xcdd7
2757
2758 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
2759     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2760 {
2761     GpStatus stat;
2762     GpBitmap *bitmap;
2763     IWICBitmapEncoder *encoder;
2764     IWICBitmapFrameEncode *frameencode;
2765     IPropertyBag2 *encoderoptions;
2766     HRESULT hr;
2767     UINT width, height;
2768     PixelFormat gdipformat=0;
2769     WICPixelFormatGUID wicformat;
2770     GpRect rc;
2771     BitmapData lockeddata;
2772     HRESULT initresult;
2773     UINT i;
2774
2775     if (image->type != ImageTypeBitmap)
2776         return GenericError;
2777
2778     bitmap = (GpBitmap*)image;
2779
2780     GdipGetImageWidth(image, &width);
2781     GdipGetImageHeight(image, &height);
2782
2783     rc.X = 0;
2784     rc.Y = 0;
2785     rc.Width = width;
2786     rc.Height = height;
2787
2788     initresult = CoInitialize(NULL);
2789
2790     hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2791         &IID_IWICBitmapEncoder, (void**)&encoder);
2792     if (FAILED(hr))
2793     {
2794         if (SUCCEEDED(initresult)) CoUninitialize();
2795         return hresult_to_status(hr);
2796     }
2797
2798     hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2799
2800     if (SUCCEEDED(hr))
2801     {
2802         hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
2803     }
2804
2805     if (SUCCEEDED(hr)) /* created frame */
2806     {
2807         hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
2808
2809         if (SUCCEEDED(hr))
2810             hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
2811
2812         if (SUCCEEDED(hr))
2813             /* FIXME: use the resolution from the image */
2814             hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
2815
2816         if (SUCCEEDED(hr))
2817         {
2818             for (i=0; wic_pixel_formats[i]; i++)
2819             {
2820                 if (wic_gdip_formats[i] == bitmap->format)
2821                     break;
2822             }
2823             if (wic_pixel_formats[i])
2824                 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
2825             else
2826                 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
2827
2828             hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
2829
2830             for (i=0; wic_pixel_formats[i]; i++)
2831             {
2832                 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
2833                     break;
2834             }
2835             if (wic_pixel_formats[i])
2836                 gdipformat = wic_gdip_formats[i];
2837             else
2838             {
2839                 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
2840                 hr = E_FAIL;
2841             }
2842         }
2843
2844         if (SUCCEEDED(hr))
2845         {
2846             stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
2847                 &lockeddata);
2848
2849             if (stat == Ok)
2850             {
2851                 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
2852                 BYTE *row;
2853
2854                 /* write one row at a time in case stride is negative */
2855                 row = lockeddata.Scan0;
2856                 for (i=0; i<lockeddata.Height; i++)
2857                 {
2858                     hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
2859                     if (FAILED(hr)) break;
2860                     row += lockeddata.Stride;
2861                 }
2862
2863                 GdipBitmapUnlockBits(bitmap, &lockeddata);
2864             }
2865             else
2866                 hr = E_FAIL;
2867         }
2868
2869         if (SUCCEEDED(hr))
2870             hr = IWICBitmapFrameEncode_Commit(frameencode);
2871
2872         IWICBitmapFrameEncode_Release(frameencode);
2873         IPropertyBag2_Release(encoderoptions);
2874     }
2875
2876     if (SUCCEEDED(hr))
2877         hr = IWICBitmapEncoder_Commit(encoder);
2878
2879     IWICBitmapEncoder_Release(encoder);
2880
2881     if (SUCCEEDED(initresult)) CoUninitialize();
2882
2883     return hresult_to_status(hr);
2884 }
2885
2886 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
2887     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2888 {
2889     return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
2890 }
2891
2892 static GpStatus encode_image_png(GpImage *image, IStream* stream,
2893     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2894 {
2895     return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
2896 }
2897
2898 /*****************************************************************************
2899  * GdipSaveImageToStream [GDIPLUS.@]
2900  */
2901 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
2902     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2903 {
2904     GpStatus stat;
2905     encode_image_func encode_image;
2906     int i;
2907
2908     TRACE("%p %p %p %p\n", image, stream, clsid, params);
2909
2910     if(!image || !stream)
2911         return InvalidParameter;
2912
2913     /* select correct encoder */
2914     encode_image = NULL;
2915     for (i = 0; i < NUM_CODECS; i++) {
2916         if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
2917             IsEqualCLSID(clsid, &codecs[i].info.Clsid))
2918             encode_image = codecs[i].encode_func;
2919     }
2920     if (encode_image == NULL)
2921         return UnknownImageFormat;
2922
2923     stat = encode_image(image, stream, clsid, params);
2924
2925     return stat;
2926 }
2927
2928 /*****************************************************************************
2929  * GdipGetImagePalette [GDIPLUS.@]
2930  */
2931 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
2932 {
2933     TRACE("(%p,%p,%i)\n", image, palette, size);
2934
2935     if (!image || !palette)
2936         return InvalidParameter;
2937
2938     if (size < (sizeof(UINT)*2+sizeof(ARGB)*image->palette_count))
2939     {
2940         TRACE("<-- InsufficientBuffer\n");
2941         return InsufficientBuffer;
2942     }
2943
2944     palette->Flags = image->palette_flags;
2945     palette->Count = image->palette_count;
2946     memcpy(palette->Entries, image->palette_entries, sizeof(ARGB)*image->palette_count);
2947
2948     return Ok;
2949 }
2950
2951 /*****************************************************************************
2952  * GdipSetImagePalette [GDIPLUS.@]
2953  */
2954 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
2955     GDIPCONST ColorPalette *palette)
2956 {
2957     TRACE("(%p,%p)\n", image, palette);
2958
2959     if(!image || !palette || palette->Count > 256)
2960         return InvalidParameter;
2961
2962     if (palette->Count > image->palette_size)
2963     {
2964         ARGB *new_palette;
2965
2966         new_palette = GdipAlloc(sizeof(ARGB) * palette->Count);
2967         if (!new_palette) return OutOfMemory;
2968
2969         GdipFree(image->palette_entries);
2970         image->palette_entries = new_palette;
2971         image->palette_size = palette->Count;
2972     }
2973
2974     image->palette_flags = palette->Flags;
2975     image->palette_count = palette->Count;
2976     memcpy(image->palette_entries, palette->Entries, sizeof(ARGB)*palette->Count);
2977
2978     return Ok;
2979 }
2980
2981 /*************************************************************************
2982  * Encoders -
2983  *   Structures that represent which formats we support for encoding.
2984  */
2985
2986 /* ImageCodecInfo creation routines taken from libgdiplus */
2987 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
2988 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
2989 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
2990 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
2991 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
2992 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
2993
2994 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
2995 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
2996 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
2997 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
2998 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
2999 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
3000
3001 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
3002 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
3003 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
3004 static const WCHAR gif_format[] = {'G','I','F',0};
3005 static const BYTE gif_sig_pattern[4] = "GIF8";
3006 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3007
3008 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
3009 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
3010 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
3011 static const WCHAR tiff_format[] = {'T','I','F','F',0};
3012 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
3013 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3014
3015 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
3016 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
3017 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
3018 static const WCHAR emf_format[] = {'E','M','F',0};
3019 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
3020 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3021
3022 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
3023 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
3024 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
3025 static const WCHAR wmf_format[] = {'W','M','F',0};
3026 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
3027 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
3028
3029 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
3030 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
3031 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
3032 static const WCHAR png_format[] = {'P','N','G',0};
3033 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
3034 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3035
3036 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
3037 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
3038 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
3039 static const WCHAR ico_format[] = {'I','C','O',0};
3040 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
3041 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3042
3043 static const struct image_codec codecs[NUM_CODECS] = {
3044     {
3045         { /* BMP */
3046             /* Clsid */              { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3047             /* FormatID */           { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3048             /* CodecName */          bmp_codecname,
3049             /* DllName */            NULL,
3050             /* FormatDescription */  bmp_format,
3051             /* FilenameExtension */  bmp_extension,
3052             /* MimeType */           bmp_mimetype,
3053             /* Flags */              ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3054             /* Version */            1,
3055             /* SigCount */           1,
3056             /* SigSize */            2,
3057             /* SigPattern */         bmp_sig_pattern,
3058             /* SigMask */            bmp_sig_mask,
3059         },
3060         encode_image_BMP,
3061         decode_image_bmp
3062     },
3063     {
3064         { /* JPEG */
3065             /* Clsid */              { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3066             /* FormatID */           { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3067             /* CodecName */          jpeg_codecname,
3068             /* DllName */            NULL,
3069             /* FormatDescription */  jpeg_format,
3070             /* FilenameExtension */  jpeg_extension,
3071             /* MimeType */           jpeg_mimetype,
3072             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3073             /* Version */            1,
3074             /* SigCount */           1,
3075             /* SigSize */            2,
3076             /* SigPattern */         jpeg_sig_pattern,
3077             /* SigMask */            jpeg_sig_mask,
3078         },
3079         NULL,
3080         decode_image_jpeg
3081     },
3082     {
3083         { /* GIF */
3084             /* Clsid */              { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3085             /* FormatID */           { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3086             /* CodecName */          gif_codecname,
3087             /* DllName */            NULL,
3088             /* FormatDescription */  gif_format,
3089             /* FilenameExtension */  gif_extension,
3090             /* MimeType */           gif_mimetype,
3091             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3092             /* Version */            1,
3093             /* SigCount */           1,
3094             /* SigSize */            4,
3095             /* SigPattern */         gif_sig_pattern,
3096             /* SigMask */            gif_sig_mask,
3097         },
3098         NULL,
3099         decode_image_gif
3100     },
3101     {
3102         { /* TIFF */
3103             /* Clsid */              { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3104             /* FormatID */           { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3105             /* CodecName */          tiff_codecname,
3106             /* DllName */            NULL,
3107             /* FormatDescription */  tiff_format,
3108             /* FilenameExtension */  tiff_extension,
3109             /* MimeType */           tiff_mimetype,
3110             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3111             /* Version */            1,
3112             /* SigCount */           2,
3113             /* SigSize */            4,
3114             /* SigPattern */         tiff_sig_pattern,
3115             /* SigMask */            tiff_sig_mask,
3116         },
3117         NULL,
3118         decode_image_tiff
3119     },
3120     {
3121         { /* EMF */
3122             /* Clsid */              { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3123             /* FormatID */           { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3124             /* CodecName */          emf_codecname,
3125             /* DllName */            NULL,
3126             /* FormatDescription */  emf_format,
3127             /* FilenameExtension */  emf_extension,
3128             /* MimeType */           emf_mimetype,
3129             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3130             /* Version */            1,
3131             /* SigCount */           1,
3132             /* SigSize */            4,
3133             /* SigPattern */         emf_sig_pattern,
3134             /* SigMask */            emf_sig_mask,
3135         },
3136         NULL,
3137         decode_image_olepicture_metafile
3138     },
3139     {
3140         { /* WMF */
3141             /* Clsid */              { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3142             /* FormatID */           { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3143             /* CodecName */          wmf_codecname,
3144             /* DllName */            NULL,
3145             /* FormatDescription */  wmf_format,
3146             /* FilenameExtension */  wmf_extension,
3147             /* MimeType */           wmf_mimetype,
3148             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3149             /* Version */            1,
3150             /* SigCount */           1,
3151             /* SigSize */            2,
3152             /* SigPattern */         wmf_sig_pattern,
3153             /* SigMask */            wmf_sig_mask,
3154         },
3155         NULL,
3156         decode_image_olepicture_metafile
3157     },
3158     {
3159         { /* PNG */
3160             /* Clsid */              { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3161             /* FormatID */           { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3162             /* CodecName */          png_codecname,
3163             /* DllName */            NULL,
3164             /* FormatDescription */  png_format,
3165             /* FilenameExtension */  png_extension,
3166             /* MimeType */           png_mimetype,
3167             /* Flags */              ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3168             /* Version */            1,
3169             /* SigCount */           1,
3170             /* SigSize */            8,
3171             /* SigPattern */         png_sig_pattern,
3172             /* SigMask */            png_sig_mask,
3173         },
3174         encode_image_png,
3175         decode_image_png
3176     },
3177     {
3178         { /* ICO */
3179             /* Clsid */              { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3180             /* FormatID */           { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3181             /* CodecName */          ico_codecname,
3182             /* DllName */            NULL,
3183             /* FormatDescription */  ico_format,
3184             /* FilenameExtension */  ico_extension,
3185             /* MimeType */           ico_mimetype,
3186             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3187             /* Version */            1,
3188             /* SigCount */           1,
3189             /* SigSize */            4,
3190             /* SigPattern */         ico_sig_pattern,
3191             /* SigMask */            ico_sig_mask,
3192         },
3193         NULL,
3194         decode_image_icon
3195     },
3196 };
3197
3198 /*****************************************************************************
3199  * GdipGetImageDecodersSize [GDIPLUS.@]
3200  */
3201 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
3202 {
3203     int decoder_count=0;
3204     int i;
3205     TRACE("%p %p\n", numDecoders, size);
3206
3207     if (!numDecoders || !size)
3208         return InvalidParameter;
3209
3210     for (i=0; i<NUM_CODECS; i++)
3211     {
3212         if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3213             decoder_count++;
3214     }
3215
3216     *numDecoders = decoder_count;
3217     *size = decoder_count * sizeof(ImageCodecInfo);
3218
3219     return Ok;
3220 }
3221
3222 /*****************************************************************************
3223  * GdipGetImageDecoders [GDIPLUS.@]
3224  */
3225 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
3226 {
3227     int i, decoder_count=0;
3228     TRACE("%u %u %p\n", numDecoders, size, decoders);
3229
3230     if (!decoders ||
3231         size != numDecoders * sizeof(ImageCodecInfo))
3232         return GenericError;
3233
3234     for (i=0; i<NUM_CODECS; i++)
3235     {
3236         if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3237         {
3238             if (decoder_count == numDecoders) return GenericError;
3239             memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3240             decoder_count++;
3241         }
3242     }
3243
3244     if (decoder_count < numDecoders) return GenericError;
3245
3246     return Ok;
3247 }
3248
3249 /*****************************************************************************
3250  * GdipGetImageEncodersSize [GDIPLUS.@]
3251  */
3252 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
3253 {
3254     int encoder_count=0;
3255     int i;
3256     TRACE("%p %p\n", numEncoders, size);
3257
3258     if (!numEncoders || !size)
3259         return InvalidParameter;
3260
3261     for (i=0; i<NUM_CODECS; i++)
3262     {
3263         if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3264             encoder_count++;
3265     }
3266
3267     *numEncoders = encoder_count;
3268     *size = encoder_count * sizeof(ImageCodecInfo);
3269
3270     return Ok;
3271 }
3272
3273 /*****************************************************************************
3274  * GdipGetImageEncoders [GDIPLUS.@]
3275  */
3276 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
3277 {
3278     int i, encoder_count=0;
3279     TRACE("%u %u %p\n", numEncoders, size, encoders);
3280
3281     if (!encoders ||
3282         size != numEncoders * sizeof(ImageCodecInfo))
3283         return GenericError;
3284
3285     for (i=0; i<NUM_CODECS; i++)
3286     {
3287         if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3288         {
3289             if (encoder_count == numEncoders) return GenericError;
3290             memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3291             encoder_count++;
3292         }
3293     }
3294
3295     if (encoder_count < numEncoders) return GenericError;
3296
3297     return Ok;
3298 }
3299
3300 /*****************************************************************************
3301  * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
3302  */
3303 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
3304 {
3305     BITMAP bm;
3306     GpStatus retval;
3307     PixelFormat format;
3308     BitmapData lockeddata;
3309     INT y;
3310
3311     TRACE("%p %p %p\n", hbm, hpal, bitmap);
3312
3313     if(!hbm || !bitmap)
3314         return InvalidParameter;
3315
3316     /* TODO: Support for device-dependent bitmaps */
3317     if(hpal){
3318         FIXME("no support for device-dependent bitmaps\n");
3319         return NotImplemented;
3320     }
3321
3322     if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
3323             return InvalidParameter;
3324
3325     /* TODO: Figure out the correct format for 16, 32, 64 bpp */
3326     switch(bm.bmBitsPixel) {
3327         case 1:
3328             format = PixelFormat1bppIndexed;
3329             break;
3330         case 4:
3331             format = PixelFormat4bppIndexed;
3332             break;
3333         case 8:
3334             format = PixelFormat8bppIndexed;
3335             break;
3336         case 24:
3337             format = PixelFormat24bppRGB;
3338             break;
3339         case 32:
3340             format = PixelFormat32bppRGB;
3341             break;
3342         case 48:
3343             format = PixelFormat48bppRGB;
3344             break;
3345         default:
3346             FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
3347             return InvalidParameter;
3348     }
3349
3350     retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
3351         format, NULL, bitmap);
3352
3353     if (retval == Ok)
3354     {
3355         retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
3356             format, &lockeddata);
3357         if (retval == Ok)
3358         {
3359             if (bm.bmBits)
3360             {
3361                 for (y=0; y<bm.bmHeight; y++)
3362                 {
3363                     memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
3364                            (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
3365                            bm.bmWidthBytes);
3366                 }
3367             }
3368             else
3369             {
3370                 HDC hdc;
3371                 HBITMAP oldhbm;
3372                 BITMAPINFO *pbmi;
3373                 INT src_height, dst_stride;
3374                 BYTE *dst_bits;
3375
3376                 hdc = CreateCompatibleDC(NULL);
3377                 oldhbm = SelectObject(hdc, hbm);
3378
3379                 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
3380
3381                 if (pbmi)
3382                 {
3383                     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3384                     pbmi->bmiHeader.biBitCount = 0;
3385
3386                     GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
3387
3388                     src_height = abs(pbmi->bmiHeader.biHeight);
3389
3390                     if (pbmi->bmiHeader.biHeight > 0)
3391                     {
3392                         dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
3393                         dst_stride = -lockeddata.Stride;
3394                     }
3395                     else
3396                     {
3397                         dst_bits = lockeddata.Scan0;
3398                         dst_stride = lockeddata.Stride;
3399                     }
3400
3401                     for (y=0; y<src_height; y++)
3402                     {
3403                         GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
3404                             pbmi, DIB_RGB_COLORS);
3405                     }
3406
3407                     GdipFree(pbmi);
3408                 }
3409                 else
3410                     retval = OutOfMemory;
3411
3412                 SelectObject(hdc, oldhbm);
3413                 DeleteDC(hdc);
3414             }
3415
3416             GdipBitmapUnlockBits(*bitmap, &lockeddata);
3417         }
3418     }
3419
3420     return retval;
3421 }
3422
3423 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
3424 {
3425     FIXME("(%p): stub\n", effect);
3426     /* note: According to Jose Roca's GDI+ Docs, this is not implemented
3427      * in Windows's gdiplus */
3428     return NotImplemented;
3429 }
3430
3431 /*****************************************************************************
3432  * GdipSetEffectParameters [GDIPLUS.@]
3433  */
3434 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
3435     const VOID *params, const UINT size)
3436 {
3437     static int calls;
3438
3439     TRACE("(%p,%p,%u)\n", effect, params, size);
3440
3441     if(!(calls++))
3442         FIXME("not implemented\n");
3443
3444     return NotImplemented;
3445 }
3446
3447 /*****************************************************************************
3448  * GdipGetImageFlags [GDIPLUS.@]
3449  */
3450 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
3451 {
3452     TRACE("%p %p\n", image, flags);
3453
3454     if(!image || !flags)
3455         return InvalidParameter;
3456
3457     *flags = image->flags;
3458
3459     return Ok;
3460 }
3461
3462 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
3463 {
3464     TRACE("(%d, %p)\n", control, param);
3465
3466     switch(control){
3467         case TestControlForceBilinear:
3468             if(param)
3469                 FIXME("TestControlForceBilinear not handled\n");
3470             break;
3471         case TestControlNoICM:
3472             if(param)
3473                 FIXME("TestControlNoICM not handled\n");
3474             break;
3475         case TestControlGetBuildNumber:
3476             *((DWORD*)param) = 3102;
3477             break;
3478     }
3479
3480     return Ok;
3481 }
3482
3483 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
3484                             HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
3485                             MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
3486                             GpMetafile **metafile)
3487 {
3488     FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
3489                                  frameUnit, debugstr_w(desc), metafile);
3490
3491     return NotImplemented;
3492 }
3493
3494 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
3495                             GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
3496                             GDIPCONST WCHAR *desc, GpMetafile **metafile)
3497 {
3498     FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
3499                                  frameUnit, debugstr_w(desc), metafile);
3500
3501     return NotImplemented;
3502 }
3503
3504 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
3505 {
3506     TRACE("%p\n", image);
3507
3508     return Ok;
3509 }
3510
3511 /*****************************************************************************
3512  * GdipGetImageThumbnail [GDIPLUS.@]
3513  */
3514 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
3515                             GpImage **ret_image, GetThumbnailImageAbort cb,
3516                             VOID * cb_data)
3517 {
3518     FIXME("(%p %u %u %p %p %p) stub\n",
3519         image, width, height, ret_image, cb, cb_data);
3520     return NotImplemented;
3521 }
3522
3523 /*****************************************************************************
3524  * GdipImageRotateFlip [GDIPLUS.@]
3525  */
3526 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
3527 {
3528     GpBitmap *new_bitmap;
3529     GpBitmap *bitmap;
3530     int bpp, bytesperpixel;
3531     int rotate_90, flip_x, flip_y;
3532     int src_x_offset, src_y_offset;
3533     LPBYTE src_origin;
3534     UINT x, y, width, height;
3535     BitmapData src_lock, dst_lock;
3536     GpStatus stat;
3537
3538     TRACE("(%p, %u)\n", image, type);
3539
3540     rotate_90 = type&1;
3541     flip_x = (type&6) == 2 || (type&6) == 4;
3542     flip_y = (type&3) == 1 || (type&3) == 2;
3543
3544     if (image->type != ImageTypeBitmap)
3545     {
3546         FIXME("Not implemented for type %i\n", image->type);
3547         return NotImplemented;
3548     }
3549
3550     bitmap = (GpBitmap*)image;
3551     bpp = PIXELFORMATBPP(bitmap->format);
3552
3553     if (bpp < 8)
3554     {
3555         FIXME("Not implemented for %i bit images\n", bpp);
3556         return NotImplemented;
3557     }
3558
3559     if (rotate_90)
3560     {
3561         width = bitmap->height;
3562         height = bitmap->width;
3563     }
3564     else
3565     {
3566         width = bitmap->width;
3567         height = bitmap->height;
3568     }
3569
3570     bytesperpixel = bpp/8;
3571
3572     stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
3573
3574     if (stat != Ok)
3575         return stat;
3576
3577     stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
3578
3579     if (stat == Ok)
3580     {
3581         stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
3582
3583         if (stat == Ok)
3584         {
3585             LPBYTE src_row, src_pixel;
3586             LPBYTE dst_row, dst_pixel;
3587
3588             src_origin = src_lock.Scan0;
3589             if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
3590             if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
3591
3592             if (rotate_90)
3593             {
3594                 if (flip_y) src_x_offset = -src_lock.Stride;
3595                 else src_x_offset = src_lock.Stride;
3596                 if (flip_x) src_y_offset = -bytesperpixel;
3597                 else src_y_offset = bytesperpixel;
3598             }
3599             else
3600             {
3601                 if (flip_x) src_x_offset = -bytesperpixel;
3602                 else src_x_offset = bytesperpixel;
3603                 if (flip_y) src_y_offset = -src_lock.Stride;
3604                 else src_y_offset = src_lock.Stride;
3605             }
3606
3607             src_row = src_origin;
3608             dst_row = dst_lock.Scan0;
3609             for (y=0; y<height; y++)
3610             {
3611                 src_pixel = src_row;
3612                 dst_pixel = dst_row;
3613                 for (x=0; x<width; x++)
3614                 {
3615                     /* FIXME: This could probably be faster without memcpy. */
3616                     memcpy(dst_pixel, src_pixel, bytesperpixel);
3617                     dst_pixel += bytesperpixel;
3618                     src_pixel += src_x_offset;
3619                 }
3620                 src_row += src_y_offset;
3621                 dst_row += dst_lock.Stride;
3622             }
3623
3624             GdipBitmapUnlockBits(new_bitmap, &dst_lock);
3625         }
3626
3627         GdipBitmapUnlockBits(bitmap, &src_lock);
3628     }
3629
3630     if (stat == Ok)
3631         move_bitmap(bitmap, new_bitmap, FALSE);
3632     else
3633         GdipDisposeImage((GpImage*)new_bitmap);
3634
3635     return stat;
3636 }