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