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