kernel32: Remove superfluous heap reallocation calls in FormatMessageA/W.
[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     height = abs(info->bmiHeader.biHeight);
1224     stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1225
1226     if(info->bmiHeader.biHeight > 0) /* bottom-up */
1227     {
1228         bits = (BYTE*)bits + (height - 1) * stride;
1229         stride = -stride;
1230     }
1231
1232     switch(info->bmiHeader.biBitCount) {
1233     case 1:
1234         format = PixelFormat1bppIndexed;
1235         break;
1236     case 4:
1237         format = PixelFormat4bppIndexed;
1238         break;
1239     case 8:
1240         format = PixelFormat8bppIndexed;
1241         break;
1242     case 24:
1243         format = PixelFormat24bppRGB;
1244         break;
1245     default:
1246         FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1247         *bitmap = NULL;
1248         return InvalidParameter;
1249     }
1250
1251     return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1252                                      bits, bitmap);
1253
1254 }
1255
1256 /* FIXME: no icm */
1257 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1258     GpBitmap **bitmap)
1259 {
1260     TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1261
1262     return GdipCreateBitmapFromFile(filename, bitmap);
1263 }
1264
1265 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1266     GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1267 {
1268     HBITMAP hbm;
1269     GpStatus stat = InvalidParameter;
1270
1271     TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1272
1273     if(!lpBitmapName || !bitmap)
1274         return InvalidParameter;
1275
1276     /* load DIB */
1277     hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1278                      LR_CREATEDIBSECTION);
1279
1280     if(hbm){
1281         stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1282         DeleteObject(hbm);
1283     }
1284
1285     return stat;
1286 }
1287
1288 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1289     HBITMAP* hbmReturn, ARGB background)
1290 {
1291     GpStatus stat;
1292     HBITMAP result, oldbitmap;
1293     UINT width, height;
1294     HDC hdc;
1295     GpGraphics *graphics;
1296     BITMAPINFOHEADER bih;
1297     void *bits;
1298     TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1299
1300     if (!bitmap || !hbmReturn) return InvalidParameter;
1301
1302     GdipGetImageWidth((GpImage*)bitmap, &width);
1303     GdipGetImageHeight((GpImage*)bitmap, &height);
1304
1305     bih.biSize = sizeof(bih);
1306     bih.biWidth = width;
1307     bih.biHeight = height;
1308     bih.biPlanes = 1;
1309     bih.biBitCount = 32;
1310     bih.biCompression = BI_RGB;
1311     bih.biSizeImage = 0;
1312     bih.biXPelsPerMeter = 0;
1313     bih.biYPelsPerMeter = 0;
1314     bih.biClrUsed = 0;
1315     bih.biClrImportant = 0;
1316
1317     hdc = CreateCompatibleDC(NULL);
1318     if (!hdc) return GenericError;
1319
1320     result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
1321         NULL, 0);
1322
1323     if (result)
1324     {
1325         oldbitmap = SelectObject(hdc, result);
1326
1327         stat = GdipCreateFromHDC(hdc, &graphics);
1328         if (stat == Ok)
1329         {
1330             stat = GdipGraphicsClear(graphics, background);
1331
1332             if (stat == Ok)
1333                 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
1334
1335             GdipDeleteGraphics(graphics);
1336         }
1337
1338         SelectObject(hdc, oldbitmap);
1339     }
1340     else
1341         stat = GenericError;
1342
1343     DeleteDC(hdc);
1344
1345     if (stat != Ok && result)
1346     {
1347         DeleteObject(result);
1348         result = NULL;
1349     }
1350
1351     *hbmReturn = result;
1352
1353     return stat;
1354 }
1355
1356 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1357     GpMetafile* metafile, BOOL* succ, EmfType emfType,
1358     const WCHAR* description, GpMetafile** out_metafile)
1359 {
1360     static int calls;
1361
1362     TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1363         debugstr_w(description), out_metafile);
1364
1365     if(!ref || !metafile || !out_metafile)
1366         return InvalidParameter;
1367
1368     *succ = FALSE;
1369     *out_metafile = NULL;
1370
1371     if(!(calls++))
1372         FIXME("not implemented\n");
1373
1374     return NotImplemented;
1375 }
1376
1377 /* FIXME: this should create a bitmap in the given size with the attributes
1378  * (resolution etc.) of the graphics object */
1379 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1380     GpGraphics* target, GpBitmap** bitmap)
1381 {
1382     static int calls;
1383     GpStatus ret;
1384
1385     if(!target || !bitmap)
1386         return InvalidParameter;
1387
1388     if(!(calls++))
1389         FIXME("hacked stub\n");
1390
1391     ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1392                                     NULL, bitmap);
1393
1394     return ret;
1395 }
1396
1397 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1398 {
1399     GpStatus stat;
1400     ICONINFO iinfo;
1401     BITMAP bm;
1402     int ret;
1403     UINT width, height;
1404     GpRect rect;
1405     BitmapData lockeddata;
1406     HDC screendc;
1407     BOOL has_alpha;
1408     int x, y;
1409     BYTE *bits;
1410     BITMAPINFOHEADER bih;
1411     DWORD *src;
1412     BYTE *dst_row;
1413     DWORD *dst;
1414
1415     TRACE("%p, %p\n", hicon, bitmap);
1416
1417     if(!bitmap || !GetIconInfo(hicon, &iinfo))
1418         return InvalidParameter;
1419
1420     /* get the size of the icon */
1421     ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1422     if (ret == 0) {
1423         DeleteObject(iinfo.hbmColor);
1424         DeleteObject(iinfo.hbmMask);
1425         return GenericError;
1426     }
1427
1428     width = bm.bmWidth;
1429
1430     if (iinfo.hbmColor)
1431         height = abs(bm.bmHeight);
1432     else /* combined bitmap + mask */
1433         height = abs(bm.bmHeight) / 2;
1434
1435     bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1436     if (!bits) {
1437         DeleteObject(iinfo.hbmColor);
1438         DeleteObject(iinfo.hbmMask);
1439         return OutOfMemory;
1440     }
1441
1442     stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1443     if (stat != Ok) {
1444         DeleteObject(iinfo.hbmColor);
1445         DeleteObject(iinfo.hbmMask);
1446         HeapFree(GetProcessHeap(), 0, bits);
1447         return stat;
1448     }
1449
1450     rect.X = 0;
1451     rect.Y = 0;
1452     rect.Width = width;
1453     rect.Height = height;
1454
1455     stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1456     if (stat != Ok) {
1457         DeleteObject(iinfo.hbmColor);
1458         DeleteObject(iinfo.hbmMask);
1459         HeapFree(GetProcessHeap(), 0, bits);
1460         GdipDisposeImage((GpImage*)*bitmap);
1461         return stat;
1462     }
1463
1464     bih.biSize = sizeof(bih);
1465     bih.biWidth = width;
1466     bih.biHeight = -height;
1467     bih.biPlanes = 1;
1468     bih.biBitCount = 32;
1469     bih.biCompression = BI_RGB;
1470     bih.biSizeImage = 0;
1471     bih.biXPelsPerMeter = 0;
1472     bih.biYPelsPerMeter = 0;
1473     bih.biClrUsed = 0;
1474     bih.biClrImportant = 0;
1475
1476     screendc = GetDC(0);
1477     if (iinfo.hbmColor)
1478     {
1479         GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1480
1481         if (bm.bmBitsPixel == 32)
1482         {
1483             has_alpha = FALSE;
1484
1485             /* If any pixel has a non-zero alpha, ignore hbmMask */
1486             src = (DWORD*)bits;
1487             for (x=0; x<width && !has_alpha; x++)
1488                 for (y=0; y<height && !has_alpha; y++)
1489                     if ((*src++ & 0xff000000) != 0)
1490                         has_alpha = TRUE;
1491         }
1492         else has_alpha = FALSE;
1493     }
1494     else
1495     {
1496         GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1497         has_alpha = FALSE;
1498     }
1499
1500     /* copy the image data to the Bitmap */
1501     src = (DWORD*)bits;
1502     dst_row = lockeddata.Scan0;
1503     for (y=0; y<height; y++)
1504     {
1505         memcpy(dst_row, src, width*4);
1506         src += width;
1507         dst_row += lockeddata.Stride;
1508     }
1509
1510     if (!has_alpha)
1511     {
1512         if (iinfo.hbmMask)
1513         {
1514             /* read alpha data from the mask */
1515             if (iinfo.hbmColor)
1516                 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1517             else
1518                 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1519
1520             src = (DWORD*)bits;
1521             dst_row = lockeddata.Scan0;
1522             for (y=0; y<height; y++)
1523             {
1524                 dst = (DWORD*)dst_row;
1525                 for (x=0; x<height; x++)
1526                 {
1527                     DWORD src_value = *src++;
1528                     if (src_value)
1529                         *dst++ = 0;
1530                     else
1531                         *dst++ |= 0xff000000;
1532                 }
1533                 dst_row += lockeddata.Stride;
1534             }
1535         }
1536         else
1537         {
1538             /* set constant alpha of 255 */
1539             dst_row = bits;
1540             for (y=0; y<height; y++)
1541             {
1542                 dst = (DWORD*)dst_row;
1543                 for (x=0; x<height; x++)
1544                     *dst++ |= 0xff000000;
1545                 dst_row += lockeddata.Stride;
1546             }
1547         }
1548     }
1549
1550     ReleaseDC(0, screendc);
1551
1552     DeleteObject(iinfo.hbmColor);
1553     DeleteObject(iinfo.hbmMask);
1554
1555     GdipBitmapUnlockBits(*bitmap, &lockeddata);
1556
1557     HeapFree(GetProcessHeap(), 0, bits);
1558
1559     return Ok;
1560 }
1561
1562 static void generate_halftone_palette(ARGB *entries, UINT count)
1563 {
1564     static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1565     UINT i;
1566
1567     for (i=0; i<8 && i<count; i++)
1568     {
1569         entries[i] = 0xff000000;
1570         if (i&1) entries[i] |= 0x800000;
1571         if (i&2) entries[i] |= 0x8000;
1572         if (i&4) entries[i] |= 0x80;
1573     }
1574
1575     if (8 < count)
1576         entries[i] = 0xffc0c0c0;
1577
1578     for (i=9; i<16 && i<count; i++)
1579     {
1580         entries[i] = 0xff000000;
1581         if (i&1) entries[i] |= 0xff0000;
1582         if (i&2) entries[i] |= 0xff00;
1583         if (i&4) entries[i] |= 0xff;
1584     }
1585
1586     for (i=16; i<40 && i<count; i++)
1587     {
1588         entries[i] = 0;
1589     }
1590
1591     for (i=40; i<256 && i<count; i++)
1592     {
1593         entries[i] = 0xff000000;
1594         entries[i] |= halftone_values[(i-40)%6];
1595         entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1596         entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1597     }
1598 }
1599
1600 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1601 {
1602     HDC screendc = GetDC(0);
1603
1604     if (!screendc) return GenericError;
1605
1606     *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1607     *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1608
1609     ReleaseDC(0, screendc);
1610
1611     return Ok;
1612 }
1613
1614 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1615     PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1616 {
1617     BITMAPINFO* pbmi;
1618     HBITMAP hbitmap;
1619     INT row_size, dib_stride;
1620     HDC hdc;
1621     BYTE *bits;
1622     int i;
1623     REAL xres, yres;
1624     GpStatus stat;
1625
1626     TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1627
1628     if (!bitmap) return InvalidParameter;
1629
1630     if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1631         *bitmap = NULL;
1632         return InvalidParameter;
1633     }
1634
1635     if(scan0 && !stride)
1636         return InvalidParameter;
1637
1638     stat = get_screen_resolution(&xres, &yres);
1639     if (stat != Ok) return stat;
1640
1641     row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1642     dib_stride = (row_size + 3) & ~3;
1643
1644     if(stride == 0)
1645         stride = dib_stride;
1646
1647     pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1648     if (!pbmi)
1649         return OutOfMemory;
1650
1651     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1652     pbmi->bmiHeader.biWidth = width;
1653     pbmi->bmiHeader.biHeight = -height;
1654     pbmi->bmiHeader.biPlanes = 1;
1655     /* FIXME: use the rest of the data from format */
1656     pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1657     pbmi->bmiHeader.biCompression = BI_RGB;
1658     pbmi->bmiHeader.biSizeImage = 0;
1659     pbmi->bmiHeader.biXPelsPerMeter = 0;
1660     pbmi->bmiHeader.biYPelsPerMeter = 0;
1661     pbmi->bmiHeader.biClrUsed = 0;
1662     pbmi->bmiHeader.biClrImportant = 0;
1663
1664     hdc = CreateCompatibleDC(NULL);
1665     if (!hdc) {
1666         GdipFree(pbmi);
1667         return GenericError;
1668     }
1669
1670     hbitmap = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1671
1672     DeleteDC(hdc);
1673     GdipFree(pbmi);
1674
1675     if (!hbitmap) return GenericError;
1676
1677     /* copy bits to the dib if necessary */
1678     /* FIXME: should reference the bits instead of copying them */
1679     if (scan0)
1680         for (i=0; i<height; i++)
1681             memcpy(bits+i*dib_stride, scan0+i*stride, row_size);
1682
1683     *bitmap = GdipAlloc(sizeof(GpBitmap));
1684     if(!*bitmap)
1685     {
1686         DeleteObject(hbitmap);
1687         return OutOfMemory;
1688     }
1689
1690     (*bitmap)->image.type = ImageTypeBitmap;
1691     memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1692     (*bitmap)->image.flags = ImageFlagsNone;
1693     (*bitmap)->image.palette_flags = 0;
1694     (*bitmap)->image.palette_count = 0;
1695     (*bitmap)->image.palette_size = 0;
1696     (*bitmap)->image.palette_entries = NULL;
1697     (*bitmap)->image.xres = xres;
1698     (*bitmap)->image.yres = yres;
1699     (*bitmap)->width = width;
1700     (*bitmap)->height = height;
1701     (*bitmap)->format = format;
1702     (*bitmap)->image.picture = NULL;
1703     (*bitmap)->hbitmap = hbitmap;
1704     (*bitmap)->hdc = NULL;
1705     (*bitmap)->bits = bits;
1706     (*bitmap)->stride = dib_stride;
1707
1708     if (format == PixelFormat1bppIndexed ||
1709         format == PixelFormat4bppIndexed ||
1710         format == PixelFormat8bppIndexed)
1711     {
1712         (*bitmap)->image.palette_size = (*bitmap)->image.palette_count = 1 << PIXELFORMATBPP(format);
1713         (*bitmap)->image.palette_entries = GdipAlloc(sizeof(ARGB) * ((*bitmap)->image.palette_size));
1714
1715         if (!(*bitmap)->image.palette_entries)
1716         {
1717             GdipDisposeImage(&(*bitmap)->image);
1718             *bitmap = NULL;
1719             return OutOfMemory;
1720         }
1721
1722         if (format == PixelFormat1bppIndexed)
1723         {
1724             (*bitmap)->image.palette_flags = PaletteFlagsGrayScale;
1725             (*bitmap)->image.palette_entries[0] = 0xff000000;
1726             (*bitmap)->image.palette_entries[1] = 0xffffffff;
1727         }
1728         else
1729         {
1730             if (format == PixelFormat8bppIndexed)
1731                 (*bitmap)->image.palette_flags = PaletteFlagsHalftone;
1732
1733             generate_halftone_palette((*bitmap)->image.palette_entries,
1734                 (*bitmap)->image.palette_count);
1735         }
1736     }
1737
1738     TRACE("<-- %p\n", *bitmap);
1739
1740     return Ok;
1741 }
1742
1743 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1744     GpBitmap **bitmap)
1745 {
1746     GpStatus stat;
1747
1748     TRACE("%p %p\n", stream, bitmap);
1749
1750     stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1751
1752     if(stat != Ok)
1753         return stat;
1754
1755     if((*bitmap)->image.type != ImageTypeBitmap){
1756         GdipDisposeImage(&(*bitmap)->image);
1757         *bitmap = NULL;
1758         return GenericError; /* FIXME: what error to return? */
1759     }
1760
1761     return Ok;
1762 }
1763
1764 /* FIXME: no icm */
1765 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1766     GpBitmap **bitmap)
1767 {
1768     TRACE("%p %p\n", stream, bitmap);
1769
1770     return GdipCreateBitmapFromStream(stream, bitmap);
1771 }
1772
1773 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1774     GpCachedBitmap **cachedbmp)
1775 {
1776     GpStatus stat;
1777
1778     TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1779
1780     if(!bitmap || !graphics || !cachedbmp)
1781         return InvalidParameter;
1782
1783     *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1784     if(!*cachedbmp)
1785         return OutOfMemory;
1786
1787     stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1788     if(stat != Ok){
1789         GdipFree(*cachedbmp);
1790         return stat;
1791     }
1792
1793     return Ok;
1794 }
1795
1796 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1797 {
1798     FIXME("(%p, %p)\n", bitmap, hicon);
1799
1800     return NotImplemented;
1801 }
1802
1803 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1804 {
1805     TRACE("%p\n", cachedbmp);
1806
1807     if(!cachedbmp)
1808         return InvalidParameter;
1809
1810     GdipDisposeImage(cachedbmp->image);
1811     GdipFree(cachedbmp);
1812
1813     return Ok;
1814 }
1815
1816 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1817     GpCachedBitmap *cachedbmp, INT x, INT y)
1818 {
1819     TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1820
1821     if(!graphics || !cachedbmp)
1822         return InvalidParameter;
1823
1824     return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1825 }
1826
1827 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1828     LPBYTE pData16, INT iMapMode, INT eFlags)
1829 {
1830     FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1831     return NotImplemented;
1832 }
1833
1834 /* Internal utility function: Replace the image data of dst with that of src,
1835  * and free src. */
1836 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
1837 {
1838     GdipFree(dst->bitmapbits);
1839     DeleteDC(dst->hdc);
1840     DeleteObject(dst->hbitmap);
1841
1842     if (clobber_palette)
1843     {
1844         GdipFree(dst->image.palette_entries);
1845         dst->image.palette_flags = src->image.palette_flags;
1846         dst->image.palette_count = src->image.palette_count;
1847         dst->image.palette_entries = src->image.palette_entries;
1848     }
1849     else
1850         GdipFree(src->image.palette_entries);
1851
1852     dst->image.xres = src->image.xres;
1853     dst->image.yres = src->image.yres;
1854     dst->width = src->width;
1855     dst->height = src->height;
1856     dst->format = src->format;
1857     dst->hbitmap = src->hbitmap;
1858     dst->hdc = src->hdc;
1859     dst->bits = src->bits;
1860     dst->stride = src->stride;
1861
1862     GdipFree(src);
1863 }
1864
1865 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
1866 {
1867     TRACE("%p\n", image);
1868
1869     if(!image)
1870         return InvalidParameter;
1871
1872     if (image->picture)
1873         IPicture_Release(image->picture);
1874     if (image->type == ImageTypeBitmap)
1875     {
1876         GdipFree(((GpBitmap*)image)->bitmapbits);
1877         DeleteDC(((GpBitmap*)image)->hdc);
1878         DeleteObject(((GpBitmap*)image)->hbitmap);
1879     }
1880     GdipFree(image->palette_entries);
1881     GdipFree(image);
1882
1883     return Ok;
1884 }
1885
1886 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
1887 {
1888     static int calls;
1889
1890     TRACE("(%p,%p)\n", image, item);
1891
1892     if(!image || !item)
1893         return InvalidParameter;
1894
1895     if (!(calls++))
1896         FIXME("not implemented\n");
1897
1898     return NotImplemented;
1899 }
1900
1901 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
1902 {
1903     static int calls;
1904
1905     TRACE("(%p,%p)\n", image, item);
1906
1907     if (!(calls++))
1908         FIXME("not implemented\n");
1909
1910     return NotImplemented;
1911 }
1912
1913 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
1914     GpUnit *srcUnit)
1915 {
1916     TRACE("%p %p %p\n", image, srcRect, srcUnit);
1917
1918     if(!image || !srcRect || !srcUnit)
1919         return InvalidParameter;
1920     if(image->type == ImageTypeMetafile){
1921         *srcRect = ((GpMetafile*)image)->bounds;
1922         *srcUnit = ((GpMetafile*)image)->unit;
1923     }
1924     else if(image->type == ImageTypeBitmap){
1925         srcRect->X = srcRect->Y = 0.0;
1926         srcRect->Width = (REAL) ((GpBitmap*)image)->width;
1927         srcRect->Height = (REAL) ((GpBitmap*)image)->height;
1928         *srcUnit = UnitPixel;
1929     }
1930     else{
1931         srcRect->X = srcRect->Y = 0.0;
1932         srcRect->Width = ipicture_pixel_width(image->picture);
1933         srcRect->Height = ipicture_pixel_height(image->picture);
1934         *srcUnit = UnitPixel;
1935     }
1936
1937     TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
1938           srcRect->Width, srcRect->Height, *srcUnit);
1939
1940     return Ok;
1941 }
1942
1943 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
1944     REAL *height)
1945 {
1946     TRACE("%p %p %p\n", image, width, height);
1947
1948     if(!image || !height || !width)
1949         return InvalidParameter;
1950
1951     if(image->type == ImageTypeMetafile){
1952         HDC hdc = GetDC(0);
1953
1954         *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1955                         ((GpMetafile*)image)->bounds.Height;
1956
1957         *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1958                         ((GpMetafile*)image)->bounds.Width;
1959
1960         ReleaseDC(0, hdc);
1961     }
1962
1963     else if(image->type == ImageTypeBitmap){
1964         *height = ((GpBitmap*)image)->height;
1965         *width = ((GpBitmap*)image)->width;
1966     }
1967     else{
1968         *height = ipicture_pixel_height(image->picture);
1969         *width = ipicture_pixel_width(image->picture);
1970     }
1971
1972     TRACE("returning (%f, %f)\n", *height, *width);
1973     return Ok;
1974 }
1975
1976 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
1977     GpGraphics **graphics)
1978 {
1979     HDC hdc;
1980     GpStatus stat;
1981
1982     TRACE("%p %p\n", image, graphics);
1983
1984     if(!image || !graphics)
1985         return InvalidParameter;
1986
1987     if(image->type != ImageTypeBitmap){
1988         FIXME("not implemented for image type %d\n", image->type);
1989         return NotImplemented;
1990     }
1991
1992     hdc = ((GpBitmap*)image)->hdc;
1993
1994     if(!hdc){
1995         hdc = CreateCompatibleDC(0);
1996         SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
1997         ((GpBitmap*)image)->hdc = hdc;
1998     }
1999
2000     stat = GdipCreateFromHDC(hdc, graphics);
2001
2002     if (stat == Ok)
2003         (*graphics)->image = image;
2004
2005     return stat;
2006 }
2007
2008 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2009 {
2010     TRACE("%p %p\n", image, height);
2011
2012     if(!image || !height)
2013         return InvalidParameter;
2014
2015     if(image->type == ImageTypeMetafile){
2016         HDC hdc = GetDC(0);
2017
2018         *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
2019                         ((GpMetafile*)image)->bounds.Height);
2020
2021         ReleaseDC(0, hdc);
2022     }
2023     else if(image->type == ImageTypeBitmap)
2024         *height = ((GpBitmap*)image)->height;
2025     else
2026         *height = ipicture_pixel_height(image->picture);
2027
2028     TRACE("returning %d\n", *height);
2029
2030     return Ok;
2031 }
2032
2033 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2034 {
2035     if(!image || !res)
2036         return InvalidParameter;
2037
2038     *res = image->xres;
2039
2040     TRACE("(%p) <-- %0.2f\n", image, *res);
2041
2042     return Ok;
2043 }
2044
2045 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2046 {
2047     TRACE("%p %p\n", image, size);
2048
2049     if(!image || !size)
2050         return InvalidParameter;
2051
2052     if (image->palette_count == 0)
2053         *size = sizeof(ColorPalette);
2054     else
2055         *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette_count;
2056
2057     TRACE("<-- %u\n", *size);
2058
2059     return Ok;
2060 }
2061
2062 /* FIXME: test this function for non-bitmap types */
2063 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2064 {
2065     TRACE("%p %p\n", image, format);
2066
2067     if(!image || !format)
2068         return InvalidParameter;
2069
2070     if(image->type != ImageTypeBitmap)
2071         *format = PixelFormat24bppRGB;
2072     else
2073         *format = ((GpBitmap*) image)->format;
2074
2075     return Ok;
2076 }
2077
2078 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2079 {
2080     if(!image || !format)
2081         return InvalidParameter;
2082
2083     memcpy(format, &image->format, sizeof(GUID));
2084
2085     return Ok;
2086 }
2087
2088 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2089 {
2090     TRACE("%p %p\n", image, type);
2091
2092     if(!image || !type)
2093         return InvalidParameter;
2094
2095     *type = image->type;
2096
2097     return Ok;
2098 }
2099
2100 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2101 {
2102     if(!image || !res)
2103         return InvalidParameter;
2104
2105     *res = image->yres;
2106
2107     TRACE("(%p) <-- %0.2f\n", image, *res);
2108
2109     return Ok;
2110 }
2111
2112 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2113 {
2114     TRACE("%p %p\n", image, width);
2115
2116     if(!image || !width)
2117         return InvalidParameter;
2118
2119     if(image->type == ImageTypeMetafile){
2120         HDC hdc = GetDC(0);
2121
2122         *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
2123                         ((GpMetafile*)image)->bounds.Width);
2124
2125         ReleaseDC(0, hdc);
2126     }
2127     else if(image->type == ImageTypeBitmap)
2128         *width = ((GpBitmap*)image)->width;
2129     else
2130         *width = ipicture_pixel_width(image->picture);
2131
2132     TRACE("returning %d\n", *width);
2133
2134     return Ok;
2135 }
2136
2137 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2138     MetafileHeader * header)
2139 {
2140     static int calls;
2141
2142     if(!metafile || !header)
2143         return InvalidParameter;
2144
2145     if(!(calls++))
2146         FIXME("not implemented\n");
2147
2148     memset(header, 0, sizeof(MetafileHeader));
2149
2150     return Ok;
2151 }
2152
2153 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2154     UINT num, PropertyItem* items)
2155 {
2156     static int calls;
2157
2158     if(!(calls++))
2159         FIXME("not implemented\n");
2160
2161     return InvalidParameter;
2162 }
2163
2164 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
2165 {
2166     static int calls;
2167
2168     if(!(calls++))
2169         FIXME("not implemented\n");
2170
2171     return InvalidParameter;
2172 }
2173
2174 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
2175 {
2176     static int calls;
2177
2178     if(!(calls++))
2179         FIXME("not implemented\n");
2180
2181     return InvalidParameter;
2182 }
2183
2184 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
2185     PropertyItem* buffer)
2186 {
2187     static int calls;
2188
2189     if(!(calls++))
2190         FIXME("not implemented\n");
2191
2192     return InvalidParameter;
2193 }
2194
2195 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
2196     UINT* size)
2197 {
2198     static int calls;
2199
2200     TRACE("%p %x %p\n", image, pid, size);
2201
2202     if(!size || !image)
2203         return InvalidParameter;
2204
2205     if(!(calls++))
2206         FIXME("not implemented\n");
2207
2208     return NotImplemented;
2209 }
2210
2211 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
2212 {
2213     static int calls;
2214
2215     TRACE("(%p,%p,%p)\n", image, size, num);
2216
2217     if(!(calls++))
2218         FIXME("not implemented\n");
2219
2220     return InvalidParameter;
2221 }
2222
2223 struct image_format_dimension
2224 {
2225     const GUID *format;
2226     const GUID *dimension;
2227 };
2228
2229 struct image_format_dimension image_format_dimensions[] =
2230 {
2231     {&ImageFormatGIF, &FrameDimensionTime},
2232     {&ImageFormatIcon, &FrameDimensionResolution},
2233     {NULL}
2234 };
2235
2236 /* FIXME: Need to handle multi-framed images */
2237 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2238     GDIPCONST GUID* dimensionID, UINT* count)
2239 {
2240     static int calls;
2241
2242     TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2243
2244     if(!image || !count)
2245         return InvalidParameter;
2246
2247     if(!(calls++))
2248         FIXME("returning frame count of 1\n");
2249
2250     *count = 1;
2251
2252     return Ok;
2253 }
2254
2255 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2256     UINT* count)
2257 {
2258     /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2259
2260     if(!image || !count)
2261         return InvalidParameter;
2262
2263     *count = 1;
2264
2265     return Ok;
2266 }
2267
2268 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2269     GUID* dimensionIDs, UINT count)
2270 {
2271     int i;
2272     const GUID *result=NULL;
2273
2274     TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2275
2276     if(!image || !dimensionIDs || count != 1)
2277         return InvalidParameter;
2278
2279     for (i=0; image_format_dimensions[i].format; i++)
2280     {
2281         if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2282         {
2283             result = image_format_dimensions[i].dimension;
2284             break;
2285         }
2286     }
2287
2288     if (!result)
2289         result = &FrameDimensionPage;
2290
2291     memcpy(dimensionIDs, result, sizeof(GUID));
2292
2293     return Ok;
2294 }
2295
2296 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
2297     GDIPCONST GUID* dimensionID, UINT frameidx)
2298 {
2299     static int calls;
2300
2301     if(!image || !dimensionID)
2302         return InvalidParameter;
2303
2304     if(!(calls++))
2305         FIXME("not implemented\n");
2306
2307     return Ok;
2308 }
2309
2310 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2311                                           GpImage **image)
2312 {
2313     GpStatus stat;
2314     IStream *stream;
2315
2316     TRACE("(%s) %p\n", debugstr_w(filename), image);
2317
2318     if (!filename || !image)
2319         return InvalidParameter;
2320
2321     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2322
2323     if (stat != Ok)
2324         return stat;
2325
2326     stat = GdipLoadImageFromStream(stream, image);
2327
2328     IStream_Release(stream);
2329
2330     return stat;
2331 }
2332
2333 /* FIXME: no icm handling */
2334 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2335 {
2336     TRACE("(%s) %p\n", debugstr_w(filename), image);
2337
2338     return GdipLoadImageFromFile(filename, image);
2339 }
2340
2341 static const WICPixelFormatGUID *wic_pixel_formats[] = {
2342     &GUID_WICPixelFormat16bppBGR555,
2343     &GUID_WICPixelFormat24bppBGR,
2344     &GUID_WICPixelFormat32bppBGR,
2345     &GUID_WICPixelFormat32bppBGRA,
2346     &GUID_WICPixelFormat32bppPBGRA,
2347     NULL
2348 };
2349
2350 static const PixelFormat wic_gdip_formats[] = {
2351     PixelFormat16bppRGB555,
2352     PixelFormat24bppRGB,
2353     PixelFormat32bppRGB,
2354     PixelFormat32bppARGB,
2355     PixelFormat32bppPARGB,
2356 };
2357
2358 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
2359 {
2360     GpStatus status=Ok;
2361     GpBitmap *bitmap;
2362     HRESULT hr;
2363     IWICBitmapDecoder *decoder;
2364     IWICBitmapFrameDecode *frame;
2365     IWICBitmapSource *source=NULL;
2366     WICPixelFormatGUID wic_format;
2367     PixelFormat gdip_format=0;
2368     int i;
2369     UINT width, height;
2370     BitmapData lockeddata;
2371     WICRect wrc;
2372     HRESULT initresult;
2373
2374     initresult = CoInitialize(NULL);
2375
2376     hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2377         &IID_IWICBitmapDecoder, (void**)&decoder);
2378     if (FAILED(hr)) goto end;
2379
2380     hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
2381     if (SUCCEEDED(hr))
2382         hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
2383
2384     if (SUCCEEDED(hr)) /* got frame */
2385     {
2386         hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
2387
2388         if (SUCCEEDED(hr))
2389         {
2390             for (i=0; wic_pixel_formats[i]; i++)
2391             {
2392                 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
2393                 {
2394                     source = (IWICBitmapSource*)frame;
2395                     IWICBitmapSource_AddRef(source);
2396                     gdip_format = wic_gdip_formats[i];
2397                     break;
2398                 }
2399             }
2400             if (!source)
2401             {
2402                 /* unknown format; fall back on 32bppARGB */
2403                 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
2404                 gdip_format = PixelFormat32bppARGB;
2405             }
2406         }
2407
2408         if (SUCCEEDED(hr)) /* got source */
2409         {
2410             hr = IWICBitmapSource_GetSize(source, &width, &height);
2411
2412             if (SUCCEEDED(hr))
2413                 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
2414                     NULL, &bitmap);
2415
2416             if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
2417             {
2418                 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
2419                     gdip_format, &lockeddata);
2420                 if (status == Ok) /* locked bitmap */
2421                 {
2422                     wrc.X = 0;
2423                     wrc.Width = width;
2424                     wrc.Height = 1;
2425                     for (i=0; i<height; i++)
2426                     {
2427                         wrc.Y = i;
2428                         hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
2429                             abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
2430                         if (FAILED(hr)) break;
2431                     }
2432
2433                     GdipBitmapUnlockBits(bitmap, &lockeddata);
2434                 }
2435
2436                 if (SUCCEEDED(hr) && status == Ok)
2437                     *image = (GpImage*)bitmap;
2438                 else
2439                 {
2440                     *image = NULL;
2441                     GdipDisposeImage((GpImage*)bitmap);
2442                 }
2443             }
2444
2445             IWICBitmapSource_Release(source);
2446         }
2447
2448         IWICBitmapFrameDecode_Release(frame);
2449     }
2450
2451     IWICBitmapDecoder_Release(decoder);
2452
2453 end:
2454     if (SUCCEEDED(initresult)) CoUninitialize();
2455
2456     if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
2457
2458     return status;
2459 }
2460
2461 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
2462 {
2463     return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
2464 }
2465
2466 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, GpImage **image)
2467 {
2468     GpStatus status;
2469     GpBitmap* bitmap;
2470
2471     status = decode_image_wic(stream, &CLSID_WICBmpDecoder, image);
2472
2473     bitmap = (GpBitmap*)*image;
2474
2475     if (status == Ok && bitmap->format == PixelFormat32bppARGB)
2476     {
2477         /* WIC supports bmp files with alpha, but gdiplus does not */
2478         bitmap->format = PixelFormat32bppRGB;
2479     }
2480
2481     return status;
2482 }
2483
2484 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
2485 {
2486     return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
2487 }
2488
2489 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, GpImage **image)
2490 {
2491     return decode_image_wic(stream, &CLSID_WICPngDecoder, image);
2492 }
2493
2494 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
2495 {
2496     return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
2497 }
2498
2499 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, GpImage **image)
2500 {
2501     return decode_image_wic(stream, &CLSID_WICTiffDecoder, image);
2502 }
2503
2504 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
2505 {
2506     IPicture *pic;
2507
2508     TRACE("%p %p\n", stream, image);
2509
2510     if(!stream || !image)
2511         return InvalidParameter;
2512
2513     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2514         (LPVOID*) &pic) != S_OK){
2515         TRACE("Could not load picture\n");
2516         return GenericError;
2517     }
2518
2519     /* FIXME: missing initialization code */
2520     *image = GdipAlloc(sizeof(GpMetafile));
2521     if(!*image) return OutOfMemory;
2522     (*image)->type = ImageTypeMetafile;
2523     (*image)->picture = pic;
2524     (*image)->flags   = ImageFlagsNone;
2525     (*image)->palette_flags = 0;
2526     (*image)->palette_count = 0;
2527     (*image)->palette_size = 0;
2528     (*image)->palette_entries = NULL;
2529
2530     TRACE("<-- %p\n", *image);
2531
2532     return Ok;
2533 }
2534
2535 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
2536     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
2537
2538 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
2539
2540 typedef struct image_codec {
2541     ImageCodecInfo info;
2542     encode_image_func encode_func;
2543     decode_image_func decode_func;
2544 } image_codec;
2545
2546 typedef enum {
2547     BMP,
2548     JPEG,
2549     GIF,
2550     TIFF,
2551     EMF,
2552     WMF,
2553     PNG,
2554     ICO,
2555     NUM_CODECS
2556 } ImageFormat;
2557
2558 static const struct image_codec codecs[NUM_CODECS];
2559
2560 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
2561 {
2562     BYTE signature[8];
2563     const BYTE *pattern, *mask;
2564     LARGE_INTEGER seek;
2565     HRESULT hr;
2566     UINT bytesread;
2567     int i, j, sig;
2568
2569     /* seek to the start of the stream */
2570     seek.QuadPart = 0;
2571     hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2572     if (FAILED(hr)) return hresult_to_status(hr);
2573
2574     /* read the first 8 bytes */
2575     /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
2576     hr = IStream_Read(stream, signature, 8, &bytesread);
2577     if (FAILED(hr)) return hresult_to_status(hr);
2578     if (hr == S_FALSE || bytesread == 0) return GenericError;
2579
2580     for (i = 0; i < NUM_CODECS; i++) {
2581         if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
2582             bytesread >= codecs[i].info.SigSize)
2583         {
2584             for (sig=0; sig<codecs[i].info.SigCount; sig++)
2585             {
2586                 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
2587                 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
2588                 for (j=0; j<codecs[i].info.SigSize; j++)
2589                     if ((signature[j] & mask[j]) != pattern[j])
2590                         break;
2591                 if (j == codecs[i].info.SigSize)
2592                 {
2593                     *result = &codecs[i];
2594                     return Ok;
2595                 }
2596             }
2597         }
2598     }
2599
2600     TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
2601         signature[0],signature[1],signature[2],signature[3],
2602         signature[4],signature[5],signature[6],signature[7]);
2603
2604     return GenericError;
2605 }
2606
2607 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
2608 {
2609     GpStatus stat;
2610     LARGE_INTEGER seek;
2611     HRESULT hr;
2612     const struct image_codec *codec=NULL;
2613
2614     /* choose an appropriate image decoder */
2615     stat = get_decoder_info(stream, &codec);
2616     if (stat != Ok) return stat;
2617
2618     /* seek to the start of the stream */
2619     seek.QuadPart = 0;
2620     hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2621     if (FAILED(hr)) return hresult_to_status(hr);
2622
2623     /* call on the image decoder to do the real work */
2624     stat = codec->decode_func(stream, &codec->info.Clsid, image);
2625
2626     /* take note of the original data format */
2627     if (stat == Ok)
2628     {
2629         memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
2630     }
2631
2632     return stat;
2633 }
2634
2635 /* FIXME: no ICM */
2636 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
2637 {
2638     TRACE("%p %p\n", stream, image);
2639
2640     return GdipLoadImageFromStream(stream, image);
2641 }
2642
2643 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
2644 {
2645     static int calls;
2646
2647     TRACE("(%p,%u)\n", image, propId);
2648
2649     if(!image)
2650         return InvalidParameter;
2651
2652     if(!(calls++))
2653         FIXME("not implemented\n");
2654
2655     return NotImplemented;
2656 }
2657
2658 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
2659 {
2660     static int calls;
2661
2662     TRACE("(%p,%p)\n", image, item);
2663
2664     if(!(calls++))
2665         FIXME("not implemented\n");
2666
2667     return NotImplemented;
2668 }
2669
2670 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
2671                                         GDIPCONST CLSID *clsidEncoder,
2672                                         GDIPCONST EncoderParameters *encoderParams)
2673 {
2674     GpStatus stat;
2675     IStream *stream;
2676
2677     TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
2678
2679     if (!image || !filename|| !clsidEncoder)
2680         return InvalidParameter;
2681
2682     stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
2683     if (stat != Ok)
2684         return GenericError;
2685
2686     stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
2687
2688     IStream_Release(stream);
2689     return stat;
2690 }
2691
2692 /*************************************************************************
2693  * Encoding functions -
2694  *   These functions encode an image in different image file formats.
2695  */
2696 #define BITMAP_FORMAT_BMP   0x4d42 /* "BM" */
2697 #define BITMAP_FORMAT_JPEG  0xd8ff
2698 #define BITMAP_FORMAT_GIF   0x4947
2699 #define BITMAP_FORMAT_PNG   0x5089
2700 #define BITMAP_FORMAT_APM   0xcdd7
2701
2702 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
2703     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2704 {
2705     GpStatus stat;
2706     GpBitmap *bitmap;
2707     IWICBitmapEncoder *encoder;
2708     IWICBitmapFrameEncode *frameencode;
2709     IPropertyBag2 *encoderoptions;
2710     HRESULT hr;
2711     UINT width, height;
2712     PixelFormat gdipformat=0;
2713     WICPixelFormatGUID wicformat;
2714     GpRect rc;
2715     BitmapData lockeddata;
2716     HRESULT initresult;
2717     UINT i;
2718
2719     if (image->type != ImageTypeBitmap)
2720         return GenericError;
2721
2722     bitmap = (GpBitmap*)image;
2723
2724     GdipGetImageWidth(image, &width);
2725     GdipGetImageHeight(image, &height);
2726
2727     rc.X = 0;
2728     rc.Y = 0;
2729     rc.Width = width;
2730     rc.Height = height;
2731
2732     initresult = CoInitialize(NULL);
2733
2734     hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2735         &IID_IWICBitmapEncoder, (void**)&encoder);
2736     if (FAILED(hr))
2737     {
2738         if (SUCCEEDED(initresult)) CoUninitialize();
2739         return hresult_to_status(hr);
2740     }
2741
2742     hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2743
2744     if (SUCCEEDED(hr))
2745     {
2746         hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
2747     }
2748
2749     if (SUCCEEDED(hr)) /* created frame */
2750     {
2751         hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
2752
2753         if (SUCCEEDED(hr))
2754             hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
2755
2756         if (SUCCEEDED(hr))
2757             /* FIXME: use the resolution from the image */
2758             hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
2759
2760         if (SUCCEEDED(hr))
2761         {
2762             for (i=0; wic_pixel_formats[i]; i++)
2763             {
2764                 if (wic_gdip_formats[i] == bitmap->format)
2765                     break;
2766             }
2767             if (wic_pixel_formats[i])
2768                 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
2769             else
2770                 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
2771
2772             hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
2773
2774             for (i=0; wic_pixel_formats[i]; i++)
2775             {
2776                 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
2777                     break;
2778             }
2779             if (wic_pixel_formats[i])
2780                 gdipformat = wic_gdip_formats[i];
2781             else
2782             {
2783                 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
2784                 hr = E_FAIL;
2785             }
2786         }
2787
2788         if (SUCCEEDED(hr))
2789         {
2790             stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
2791                 &lockeddata);
2792
2793             if (stat == Ok)
2794             {
2795                 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
2796                 BYTE *row;
2797
2798                 /* write one row at a time in case stride is negative */
2799                 row = lockeddata.Scan0;
2800                 for (i=0; i<lockeddata.Height; i++)
2801                 {
2802                     hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
2803                     if (FAILED(hr)) break;
2804                     row += lockeddata.Stride;
2805                 }
2806
2807                 GdipBitmapUnlockBits(bitmap, &lockeddata);
2808             }
2809             else
2810                 hr = E_FAIL;
2811         }
2812
2813         if (SUCCEEDED(hr))
2814             hr = IWICBitmapFrameEncode_Commit(frameencode);
2815
2816         IWICBitmapFrameEncode_Release(frameencode);
2817         IPropertyBag2_Release(encoderoptions);
2818     }
2819
2820     if (SUCCEEDED(hr))
2821         hr = IWICBitmapEncoder_Commit(encoder);
2822
2823     IWICBitmapEncoder_Release(encoder);
2824
2825     if (SUCCEEDED(initresult)) CoUninitialize();
2826
2827     return hresult_to_status(hr);
2828 }
2829
2830 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
2831     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2832 {
2833     return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
2834 }
2835
2836 static GpStatus encode_image_png(GpImage *image, IStream* stream,
2837     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2838 {
2839     return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
2840 }
2841
2842 /*****************************************************************************
2843  * GdipSaveImageToStream [GDIPLUS.@]
2844  */
2845 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
2846     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2847 {
2848     GpStatus stat;
2849     encode_image_func encode_image;
2850     int i;
2851
2852     TRACE("%p %p %p %p\n", image, stream, clsid, params);
2853
2854     if(!image || !stream)
2855         return InvalidParameter;
2856
2857     /* select correct encoder */
2858     encode_image = NULL;
2859     for (i = 0; i < NUM_CODECS; i++) {
2860         if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
2861             IsEqualCLSID(clsid, &codecs[i].info.Clsid))
2862             encode_image = codecs[i].encode_func;
2863     }
2864     if (encode_image == NULL)
2865         return UnknownImageFormat;
2866
2867     stat = encode_image(image, stream, clsid, params);
2868
2869     return stat;
2870 }
2871
2872 /*****************************************************************************
2873  * GdipGetImagePalette [GDIPLUS.@]
2874  */
2875 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
2876 {
2877     TRACE("(%p,%p,%i)\n", image, palette, size);
2878
2879     if (!image || !palette)
2880         return InvalidParameter;
2881
2882     if (size < (sizeof(UINT)*2+sizeof(ARGB)*image->palette_count))
2883     {
2884         TRACE("<-- InsufficientBuffer\n");
2885         return InsufficientBuffer;
2886     }
2887
2888     palette->Flags = image->palette_flags;
2889     palette->Count = image->palette_count;
2890     memcpy(palette->Entries, image->palette_entries, sizeof(ARGB)*image->palette_count);
2891
2892     return Ok;
2893 }
2894
2895 /*****************************************************************************
2896  * GdipSetImagePalette [GDIPLUS.@]
2897  */
2898 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
2899     GDIPCONST ColorPalette *palette)
2900 {
2901     TRACE("(%p,%p)\n", image, palette);
2902
2903     if(!image || !palette || palette->Count > 256)
2904         return InvalidParameter;
2905
2906     if (palette->Count > image->palette_size)
2907     {
2908         ARGB *new_palette;
2909
2910         new_palette = GdipAlloc(sizeof(ARGB) * palette->Count);
2911         if (!new_palette) return OutOfMemory;
2912
2913         GdipFree(image->palette_entries);
2914         image->palette_entries = new_palette;
2915         image->palette_size = palette->Count;
2916     }
2917
2918     image->palette_flags = palette->Flags;
2919     image->palette_count = palette->Count;
2920     memcpy(image->palette_entries, palette->Entries, sizeof(ARGB)*palette->Count);
2921
2922     return Ok;
2923 }
2924
2925 /*************************************************************************
2926  * Encoders -
2927  *   Structures that represent which formats we support for encoding.
2928  */
2929
2930 /* ImageCodecInfo creation routines taken from libgdiplus */
2931 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
2932 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
2933 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
2934 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
2935 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
2936 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
2937
2938 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
2939 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
2940 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
2941 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
2942 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
2943 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
2944
2945 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
2946 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
2947 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
2948 static const WCHAR gif_format[] = {'G','I','F',0};
2949 static const BYTE gif_sig_pattern[4] = "GIF8";
2950 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2951
2952 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
2953 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
2954 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
2955 static const WCHAR tiff_format[] = {'T','I','F','F',0};
2956 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
2957 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2958
2959 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
2960 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
2961 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
2962 static const WCHAR emf_format[] = {'E','M','F',0};
2963 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
2964 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2965
2966 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
2967 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
2968 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
2969 static const WCHAR wmf_format[] = {'W','M','F',0};
2970 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
2971 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
2972
2973 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
2974 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
2975 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
2976 static const WCHAR png_format[] = {'P','N','G',0};
2977 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
2978 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2979
2980 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
2981 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
2982 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
2983 static const WCHAR ico_format[] = {'I','C','O',0};
2984 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
2985 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2986
2987 static const struct image_codec codecs[NUM_CODECS] = {
2988     {
2989         { /* BMP */
2990             /* Clsid */              { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2991             /* FormatID */           { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2992             /* CodecName */          bmp_codecname,
2993             /* DllName */            NULL,
2994             /* FormatDescription */  bmp_format,
2995             /* FilenameExtension */  bmp_extension,
2996             /* MimeType */           bmp_mimetype,
2997             /* Flags */              ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2998             /* Version */            1,
2999             /* SigCount */           1,
3000             /* SigSize */            2,
3001             /* SigPattern */         bmp_sig_pattern,
3002             /* SigMask */            bmp_sig_mask,
3003         },
3004         encode_image_BMP,
3005         decode_image_bmp
3006     },
3007     {
3008         { /* JPEG */
3009             /* Clsid */              { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3010             /* FormatID */           { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3011             /* CodecName */          jpeg_codecname,
3012             /* DllName */            NULL,
3013             /* FormatDescription */  jpeg_format,
3014             /* FilenameExtension */  jpeg_extension,
3015             /* MimeType */           jpeg_mimetype,
3016             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3017             /* Version */            1,
3018             /* SigCount */           1,
3019             /* SigSize */            2,
3020             /* SigPattern */         jpeg_sig_pattern,
3021             /* SigMask */            jpeg_sig_mask,
3022         },
3023         NULL,
3024         decode_image_jpeg
3025     },
3026     {
3027         { /* GIF */
3028             /* Clsid */              { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3029             /* FormatID */           { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3030             /* CodecName */          gif_codecname,
3031             /* DllName */            NULL,
3032             /* FormatDescription */  gif_format,
3033             /* FilenameExtension */  gif_extension,
3034             /* MimeType */           gif_mimetype,
3035             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3036             /* Version */            1,
3037             /* SigCount */           1,
3038             /* SigSize */            4,
3039             /* SigPattern */         gif_sig_pattern,
3040             /* SigMask */            gif_sig_mask,
3041         },
3042         NULL,
3043         decode_image_gif
3044     },
3045     {
3046         { /* TIFF */
3047             /* Clsid */              { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3048             /* FormatID */           { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3049             /* CodecName */          tiff_codecname,
3050             /* DllName */            NULL,
3051             /* FormatDescription */  tiff_format,
3052             /* FilenameExtension */  tiff_extension,
3053             /* MimeType */           tiff_mimetype,
3054             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3055             /* Version */            1,
3056             /* SigCount */           2,
3057             /* SigSize */            4,
3058             /* SigPattern */         tiff_sig_pattern,
3059             /* SigMask */            tiff_sig_mask,
3060         },
3061         NULL,
3062         decode_image_tiff
3063     },
3064     {
3065         { /* EMF */
3066             /* Clsid */              { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3067             /* FormatID */           { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3068             /* CodecName */          emf_codecname,
3069             /* DllName */            NULL,
3070             /* FormatDescription */  emf_format,
3071             /* FilenameExtension */  emf_extension,
3072             /* MimeType */           emf_mimetype,
3073             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3074             /* Version */            1,
3075             /* SigCount */           1,
3076             /* SigSize */            4,
3077             /* SigPattern */         emf_sig_pattern,
3078             /* SigMask */            emf_sig_mask,
3079         },
3080         NULL,
3081         decode_image_olepicture_metafile
3082     },
3083     {
3084         { /* WMF */
3085             /* Clsid */              { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3086             /* FormatID */           { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3087             /* CodecName */          wmf_codecname,
3088             /* DllName */            NULL,
3089             /* FormatDescription */  wmf_format,
3090             /* FilenameExtension */  wmf_extension,
3091             /* MimeType */           wmf_mimetype,
3092             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3093             /* Version */            1,
3094             /* SigCount */           1,
3095             /* SigSize */            2,
3096             /* SigPattern */         wmf_sig_pattern,
3097             /* SigMask */            wmf_sig_mask,
3098         },
3099         NULL,
3100         decode_image_olepicture_metafile
3101     },
3102     {
3103         { /* PNG */
3104             /* Clsid */              { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3105             /* FormatID */           { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3106             /* CodecName */          png_codecname,
3107             /* DllName */            NULL,
3108             /* FormatDescription */  png_format,
3109             /* FilenameExtension */  png_extension,
3110             /* MimeType */           png_mimetype,
3111             /* Flags */              ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3112             /* Version */            1,
3113             /* SigCount */           1,
3114             /* SigSize */            8,
3115             /* SigPattern */         png_sig_pattern,
3116             /* SigMask */            png_sig_mask,
3117         },
3118         encode_image_png,
3119         decode_image_png
3120     },
3121     {
3122         { /* ICO */
3123             /* Clsid */              { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3124             /* FormatID */           { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3125             /* CodecName */          ico_codecname,
3126             /* DllName */            NULL,
3127             /* FormatDescription */  ico_format,
3128             /* FilenameExtension */  ico_extension,
3129             /* MimeType */           ico_mimetype,
3130             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3131             /* Version */            1,
3132             /* SigCount */           1,
3133             /* SigSize */            4,
3134             /* SigPattern */         ico_sig_pattern,
3135             /* SigMask */            ico_sig_mask,
3136         },
3137         NULL,
3138         decode_image_icon
3139     },
3140 };
3141
3142 /*****************************************************************************
3143  * GdipGetImageDecodersSize [GDIPLUS.@]
3144  */
3145 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
3146 {
3147     int decoder_count=0;
3148     int i;
3149     TRACE("%p %p\n", numDecoders, size);
3150
3151     if (!numDecoders || !size)
3152         return InvalidParameter;
3153
3154     for (i=0; i<NUM_CODECS; i++)
3155     {
3156         if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3157             decoder_count++;
3158     }
3159
3160     *numDecoders = decoder_count;
3161     *size = decoder_count * sizeof(ImageCodecInfo);
3162
3163     return Ok;
3164 }
3165
3166 /*****************************************************************************
3167  * GdipGetImageDecoders [GDIPLUS.@]
3168  */
3169 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
3170 {
3171     int i, decoder_count=0;
3172     TRACE("%u %u %p\n", numDecoders, size, decoders);
3173
3174     if (!decoders ||
3175         size != numDecoders * sizeof(ImageCodecInfo))
3176         return GenericError;
3177
3178     for (i=0; i<NUM_CODECS; i++)
3179     {
3180         if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3181         {
3182             if (decoder_count == numDecoders) return GenericError;
3183             memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3184             decoder_count++;
3185         }
3186     }
3187
3188     if (decoder_count < numDecoders) return GenericError;
3189
3190     return Ok;
3191 }
3192
3193 /*****************************************************************************
3194  * GdipGetImageEncodersSize [GDIPLUS.@]
3195  */
3196 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
3197 {
3198     int encoder_count=0;
3199     int i;
3200     TRACE("%p %p\n", numEncoders, size);
3201
3202     if (!numEncoders || !size)
3203         return InvalidParameter;
3204
3205     for (i=0; i<NUM_CODECS; i++)
3206     {
3207         if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3208             encoder_count++;
3209     }
3210
3211     *numEncoders = encoder_count;
3212     *size = encoder_count * sizeof(ImageCodecInfo);
3213
3214     return Ok;
3215 }
3216
3217 /*****************************************************************************
3218  * GdipGetImageEncoders [GDIPLUS.@]
3219  */
3220 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
3221 {
3222     int i, encoder_count=0;
3223     TRACE("%u %u %p\n", numEncoders, size, encoders);
3224
3225     if (!encoders ||
3226         size != numEncoders * sizeof(ImageCodecInfo))
3227         return GenericError;
3228
3229     for (i=0; i<NUM_CODECS; i++)
3230     {
3231         if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3232         {
3233             if (encoder_count == numEncoders) return GenericError;
3234             memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3235             encoder_count++;
3236         }
3237     }
3238
3239     if (encoder_count < numEncoders) return GenericError;
3240
3241     return Ok;
3242 }
3243
3244 /*****************************************************************************
3245  * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
3246  */
3247 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
3248 {
3249     BITMAP bm;
3250     GpStatus retval;
3251     PixelFormat format;
3252     BitmapData lockeddata;
3253     INT y;
3254
3255     TRACE("%p %p %p\n", hbm, hpal, bitmap);
3256
3257     if(!hbm || !bitmap)
3258         return InvalidParameter;
3259
3260     /* TODO: Support for device-dependent bitmaps */
3261     if(hpal){
3262         FIXME("no support for device-dependent bitmaps\n");
3263         return NotImplemented;
3264     }
3265
3266     if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
3267             return InvalidParameter;
3268
3269     /* TODO: Figure out the correct format for 16, 32, 64 bpp */
3270     switch(bm.bmBitsPixel) {
3271         case 1:
3272             format = PixelFormat1bppIndexed;
3273             break;
3274         case 4:
3275             format = PixelFormat4bppIndexed;
3276             break;
3277         case 8:
3278             format = PixelFormat8bppIndexed;
3279             break;
3280         case 24:
3281             format = PixelFormat24bppRGB;
3282             break;
3283         case 32:
3284             format = PixelFormat32bppRGB;
3285             break;
3286         case 48:
3287             format = PixelFormat48bppRGB;
3288             break;
3289         default:
3290             FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
3291             return InvalidParameter;
3292     }
3293
3294     retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
3295         format, NULL, bitmap);
3296
3297     if (retval == Ok)
3298     {
3299         retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
3300             format, &lockeddata);
3301         if (retval == Ok)
3302         {
3303             if (bm.bmBits)
3304             {
3305                 for (y=0; y<bm.bmHeight; y++)
3306                 {
3307                     memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
3308                            (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
3309                            bm.bmWidthBytes);
3310                 }
3311             }
3312             else
3313             {
3314                 HDC hdc;
3315                 HBITMAP oldhbm;
3316                 BITMAPINFO *pbmi;
3317                 INT src_height, dst_stride;
3318                 BYTE *dst_bits;
3319
3320                 hdc = CreateCompatibleDC(NULL);
3321                 oldhbm = SelectObject(hdc, hbm);
3322
3323                 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
3324
3325                 if (pbmi)
3326                 {
3327                     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3328                     pbmi->bmiHeader.biBitCount = 0;
3329
3330                     GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
3331
3332                     src_height = abs(pbmi->bmiHeader.biHeight);
3333
3334                     if (pbmi->bmiHeader.biHeight > 0)
3335                     {
3336                         dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
3337                         dst_stride = -lockeddata.Stride;
3338                     }
3339                     else
3340                     {
3341                         dst_bits = lockeddata.Scan0;
3342                         dst_stride = lockeddata.Stride;
3343                     }
3344
3345                     for (y=0; y<src_height; y++)
3346                     {
3347                         GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
3348                             pbmi, DIB_RGB_COLORS);
3349                     }
3350
3351                     GdipFree(pbmi);
3352                 }
3353                 else
3354                     retval = OutOfMemory;
3355
3356                 SelectObject(hdc, oldhbm);
3357                 DeleteDC(hdc);
3358             }
3359
3360             GdipBitmapUnlockBits(*bitmap, &lockeddata);
3361         }
3362     }
3363
3364     return retval;
3365 }
3366
3367 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
3368 {
3369     FIXME("(%p): stub\n", effect);
3370     /* note: According to Jose Roca's GDI+ Docs, this is not implemented
3371      * in Windows's gdiplus */
3372     return NotImplemented;
3373 }
3374
3375 /*****************************************************************************
3376  * GdipSetEffectParameters [GDIPLUS.@]
3377  */
3378 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
3379     const VOID *params, const UINT size)
3380 {
3381     static int calls;
3382
3383     TRACE("(%p,%p,%u)\n", effect, params, size);
3384
3385     if(!(calls++))
3386         FIXME("not implemented\n");
3387
3388     return NotImplemented;
3389 }
3390
3391 /*****************************************************************************
3392  * GdipGetImageFlags [GDIPLUS.@]
3393  */
3394 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
3395 {
3396     TRACE("%p %p\n", image, flags);
3397
3398     if(!image || !flags)
3399         return InvalidParameter;
3400
3401     *flags = image->flags;
3402
3403     return Ok;
3404 }
3405
3406 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
3407 {
3408     TRACE("(%d, %p)\n", control, param);
3409
3410     switch(control){
3411         case TestControlForceBilinear:
3412             if(param)
3413                 FIXME("TestControlForceBilinear not handled\n");
3414             break;
3415         case TestControlNoICM:
3416             if(param)
3417                 FIXME("TestControlNoICM not handled\n");
3418             break;
3419         case TestControlGetBuildNumber:
3420             *((DWORD*)param) = 3102;
3421             break;
3422     }
3423
3424     return Ok;
3425 }
3426
3427 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
3428                             HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
3429                             MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
3430                             GpMetafile **metafile)
3431 {
3432     FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
3433                                  frameUnit, debugstr_w(desc), metafile);
3434
3435     return NotImplemented;
3436 }
3437
3438 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
3439                             GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
3440                             GDIPCONST WCHAR *desc, GpMetafile **metafile)
3441 {
3442     FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
3443                                  frameUnit, debugstr_w(desc), metafile);
3444
3445     return NotImplemented;
3446 }
3447
3448 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
3449 {
3450     FIXME("%p\n", image);
3451
3452     return Ok;
3453 }
3454
3455 /*****************************************************************************
3456  * GdipGetImageThumbnail [GDIPLUS.@]
3457  */
3458 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
3459                             GpImage **ret_image, GetThumbnailImageAbort cb,
3460                             VOID * cb_data)
3461 {
3462     FIXME("(%p %u %u %p %p %p) stub\n",
3463         image, width, height, ret_image, cb, cb_data);
3464     return NotImplemented;
3465 }
3466
3467 /*****************************************************************************
3468  * GdipImageRotateFlip [GDIPLUS.@]
3469  */
3470 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
3471 {
3472     GpBitmap *new_bitmap;
3473     GpBitmap *bitmap;
3474     int bpp, bytesperpixel;
3475     int rotate_90, flip_x, flip_y;
3476     int src_x_offset, src_y_offset;
3477     LPBYTE src_origin;
3478     UINT x, y, width, height;
3479     BitmapData src_lock, dst_lock;
3480     GpStatus stat;
3481
3482     TRACE("(%p, %u)\n", image, type);
3483
3484     rotate_90 = type&1;
3485     flip_x = (type&6) == 2 || (type&6) == 4;
3486     flip_y = (type&3) == 1 || (type&3) == 2;
3487
3488     if (image->type != ImageTypeBitmap)
3489     {
3490         FIXME("Not implemented for type %i\n", image->type);
3491         return NotImplemented;
3492     }
3493
3494     bitmap = (GpBitmap*)image;
3495     bpp = PIXELFORMATBPP(bitmap->format);
3496
3497     if (bpp < 8)
3498     {
3499         FIXME("Not implemented for %i bit images\n", bpp);
3500         return NotImplemented;
3501     }
3502
3503     if (rotate_90)
3504     {
3505         width = bitmap->height;
3506         height = bitmap->width;
3507     }
3508     else
3509     {
3510         width = bitmap->width;
3511         height = bitmap->height;
3512     }
3513
3514     bytesperpixel = bpp/8;
3515
3516     stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
3517
3518     if (stat != Ok)
3519         return stat;
3520
3521     stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
3522
3523     if (stat == Ok)
3524     {
3525         stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
3526
3527         if (stat == Ok)
3528         {
3529             LPBYTE src_row, src_pixel;
3530             LPBYTE dst_row, dst_pixel;
3531
3532             src_origin = src_lock.Scan0;
3533             if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
3534             if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
3535
3536             if (rotate_90)
3537             {
3538                 if (flip_y) src_x_offset = -src_lock.Stride;
3539                 else src_x_offset = src_lock.Stride;
3540                 if (flip_x) src_y_offset = -bytesperpixel;
3541                 else src_y_offset = bytesperpixel;
3542             }
3543             else
3544             {
3545                 if (flip_x) src_x_offset = -bytesperpixel;
3546                 else src_x_offset = bytesperpixel;
3547                 if (flip_y) src_y_offset = -src_lock.Stride;
3548                 else src_y_offset = src_lock.Stride;
3549             }
3550
3551             src_row = src_origin;
3552             dst_row = dst_lock.Scan0;
3553             for (y=0; y<height; y++)
3554             {
3555                 src_pixel = src_row;
3556                 dst_pixel = dst_row;
3557                 for (x=0; x<width; x++)
3558                 {
3559                     /* FIXME: This could probably be faster without memcpy. */
3560                     memcpy(dst_pixel, src_pixel, bytesperpixel);
3561                     dst_pixel += bytesperpixel;
3562                     src_pixel += src_x_offset;
3563                 }
3564                 src_row += src_y_offset;
3565                 dst_row += dst_lock.Stride;
3566             }
3567
3568             GdipBitmapUnlockBits(new_bitmap, &dst_lock);
3569         }
3570
3571         GdipBitmapUnlockBits(bitmap, &src_lock);
3572     }
3573
3574     if (stat == Ok)
3575         move_bitmap(bitmap, new_bitmap, FALSE);
3576     else
3577         GdipDisposeImage((GpImage*)new_bitmap);
3578
3579     return stat;
3580 }