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