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