comctl32/listview: Free ID array when removing all items.
[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_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
98     const BYTE *row, UINT x)
99 {
100     *r = *g = *b = row[x*2+1];
101     *a = 255;
102 }
103
104 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
105     const BYTE *row, UINT x)
106 {
107     WORD pixel = *((WORD*)(row)+x);
108     *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
109     *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
110     *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
111     *a = 255;
112 }
113
114 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
115     const BYTE *row, UINT x)
116 {
117     WORD pixel = *((WORD*)(row)+x);
118     *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
119     *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
120     *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
121     *a = 255;
122 }
123
124 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
125     const BYTE *row, UINT x)
126 {
127     WORD pixel = *((WORD*)(row)+x);
128     *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
129     *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
130     *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
131     if ((pixel&0x8000) == 0x8000)
132         *a = 255;
133     else
134         *a = 0;
135 }
136
137 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
138     const BYTE *row, UINT x)
139 {
140     *r = row[x*3+2];
141     *g = row[x*3+1];
142     *b = row[x*3];
143     *a = 255;
144 }
145
146 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
147     const BYTE *row, UINT x)
148 {
149     *r = row[x*4+2];
150     *g = row[x*4+1];
151     *b = row[x*4];
152     *a = 255;
153 }
154
155 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
156     const BYTE *row, UINT x)
157 {
158     *r = row[x*4+2];
159     *g = row[x*4+1];
160     *b = row[x*4];
161     *a = row[x*4+3];
162 }
163
164 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
165     const BYTE *row, UINT x)
166 {
167     *a = row[x*4+3];
168     if (*a == 0)
169         *r = *g = *b = 0;
170     else
171     {
172         *r = row[x*4+2] * 255 / *a;
173         *g = row[x*4+1] * 255 / *a;
174         *b = row[x*4] * 255 / *a;
175     }
176 }
177
178 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
179     const BYTE *row, UINT x)
180 {
181     *r = row[x*6+5];
182     *g = row[x*6+3];
183     *b = row[x*6+1];
184     *a = 255;
185 }
186
187 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
188     const BYTE *row, UINT x)
189 {
190     *r = row[x*8+5];
191     *g = row[x*8+3];
192     *b = row[x*8+1];
193     *a = row[x*8+7];
194 }
195
196 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
197     const BYTE *row, UINT x)
198 {
199     *a = row[x*8+7];
200     if (*a == 0)
201         *r = *g = *b = 0;
202     else
203     {
204         *r = row[x*8+5] * 255 / *a;
205         *g = row[x*8+3] * 255 / *a;
206         *b = row[x*8+1] * 255 / *a;
207     }
208 }
209
210 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
211     ARGB *color)
212 {
213     BYTE r, g, b, a;
214     BYTE *row;
215     TRACE("%p %d %d %p\n", bitmap, x, y, color);
216
217     if(!bitmap || !color ||
218        x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
219         return InvalidParameter;
220
221     row = bitmap->bits+bitmap->stride*y;
222
223     switch (bitmap->format)
224     {
225         case PixelFormat16bppGrayScale:
226             getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
227             break;
228         case PixelFormat16bppRGB555:
229             getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
230             break;
231         case PixelFormat16bppRGB565:
232             getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
233             break;
234         case PixelFormat16bppARGB1555:
235             getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
236             break;
237         case PixelFormat24bppRGB:
238             getpixel_24bppRGB(&r,&g,&b,&a,row,x);
239             break;
240         case PixelFormat32bppRGB:
241             getpixel_32bppRGB(&r,&g,&b,&a,row,x);
242             break;
243         case PixelFormat32bppARGB:
244             getpixel_32bppARGB(&r,&g,&b,&a,row,x);
245             break;
246         case PixelFormat32bppPARGB:
247             getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
248             break;
249         case PixelFormat48bppRGB:
250             getpixel_48bppRGB(&r,&g,&b,&a,row,x);
251             break;
252         case PixelFormat64bppARGB:
253             getpixel_64bppARGB(&r,&g,&b,&a,row,x);
254             break;
255         case PixelFormat64bppPARGB:
256             getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
257             break;
258         default:
259             FIXME("not implemented for format 0x%x\n", bitmap->format);
260             return NotImplemented;
261     }
262
263     *color = a<<24|r<<16|g<<8|b;
264
265     return Ok;
266 }
267
268 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
269     BYTE *row, UINT x)
270 {
271     *((WORD*)(row)+x) = (r+g+b)*85;
272 }
273
274 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
275     BYTE *row, UINT x)
276 {
277     *((WORD*)(row)+x) = (r<<7&0x7c00)|
278                         (g<<2&0x03e0)|
279                         (b>>3&0x001f);
280 }
281
282 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
283     BYTE *row, UINT x)
284 {
285     *((WORD*)(row)+x) = (r<<8&0xf800)|
286                          (g<<3&0x07e0)|
287                          (b>>3&0x001f);
288 }
289
290 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
291     BYTE *row, UINT x)
292 {
293     *((WORD*)(row)+x) = (a<<8&0x8000)|
294                         (r<<7&0x7c00)|
295                         (g<<2&0x03e0)|
296                         (b>>3&0x001f);
297 }
298
299 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
300     BYTE *row, UINT x)
301 {
302     row[x*3+2] = r;
303     row[x*3+1] = g;
304     row[x*3] = b;
305 }
306
307 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
308     BYTE *row, UINT x)
309 {
310     *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
311 }
312
313 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
314     BYTE *row, UINT x)
315 {
316     *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
317 }
318
319 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
320     BYTE *row, UINT x)
321 {
322     r = r * a / 255;
323     g = g * a / 255;
324     b = b * a / 255;
325     *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
326 }
327
328 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
329     BYTE *row, UINT x)
330 {
331     row[x*6+5] = row[x*6+4] = r;
332     row[x*6+3] = row[x*6+2] = g;
333     row[x*6+1] = row[x*6] = b;
334 }
335
336 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
337     BYTE *row, UINT x)
338 {
339     UINT64 a64=a, r64=r, g64=g, b64=b;
340     *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
341 }
342
343 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
344     BYTE *row, UINT x)
345 {
346     UINT64 a64, r64, g64, b64;
347     a64 = a * 257;
348     r64 = r * a / 255;
349     g64 = g * a / 255;
350     b64 = b * a / 255;
351     *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
352 }
353
354 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
355     ARGB color)
356 {
357     BYTE a, r, g, b;
358     BYTE *row;
359     TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
360
361     if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
362         return InvalidParameter;
363
364     a = color>>24;
365     r = color>>16;
366     g = color>>8;
367     b = color;
368
369     row = bitmap->bits + bitmap->stride * y;
370
371     switch (bitmap->format)
372     {
373         case PixelFormat16bppGrayScale:
374             setpixel_16bppGrayScale(r,g,b,a,row,x);
375             break;
376         case PixelFormat16bppRGB555:
377             setpixel_16bppRGB555(r,g,b,a,row,x);
378             break;
379         case PixelFormat16bppRGB565:
380             setpixel_16bppRGB565(r,g,b,a,row,x);
381             break;
382         case PixelFormat16bppARGB1555:
383             setpixel_16bppARGB1555(r,g,b,a,row,x);
384             break;
385         case PixelFormat24bppRGB:
386             setpixel_24bppRGB(r,g,b,a,row,x);
387             break;
388         case PixelFormat32bppRGB:
389             setpixel_32bppRGB(r,g,b,a,row,x);
390             break;
391         case PixelFormat32bppARGB:
392             setpixel_32bppARGB(r,g,b,a,row,x);
393             break;
394         case PixelFormat32bppPARGB:
395             setpixel_32bppPARGB(r,g,b,a,row,x);
396             break;
397         case PixelFormat48bppRGB:
398             setpixel_48bppRGB(r,g,b,a,row,x);
399             break;
400         case PixelFormat64bppARGB:
401             setpixel_64bppARGB(r,g,b,a,row,x);
402             break;
403         case PixelFormat64bppPARGB:
404             setpixel_64bppPARGB(r,g,b,a,row,x);
405             break;
406         default:
407             FIXME("not implemented for format 0x%x\n", bitmap->format);
408             return NotImplemented;
409     }
410
411     return Ok;
412 }
413
414 /* This function returns a pointer to an array of pixels that represents the
415  * bitmap. The *entire* bitmap is locked according to the lock mode specified by
416  * flags.  It is correct behavior that a user who calls this function with write
417  * privileges can write to the whole bitmap (not just the area in rect).
418  *
419  * FIXME: only used portion of format is bits per pixel. */
420 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
421     UINT flags, PixelFormat format, BitmapData* lockeddata)
422 {
423     BOOL bm_is_selected;
424     INT stride, bitspp = PIXELFORMATBPP(format);
425     HDC hdc;
426     HBITMAP hbm, old = NULL;
427     BITMAPINFO *pbmi;
428     BYTE *buff = NULL;
429     UINT abs_height;
430     GpRect act_rect; /* actual rect to be used */
431
432     TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
433
434     if(!lockeddata || !bitmap)
435         return InvalidParameter;
436
437     if(rect){
438         if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
439           (rect->Y + rect->Height > bitmap->height) || !flags)
440             return InvalidParameter;
441
442         act_rect = *rect;
443     }
444     else{
445         act_rect.X = act_rect.Y = 0;
446         act_rect.Width  = bitmap->width;
447         act_rect.Height = bitmap->height;
448     }
449
450     if(flags & ImageLockModeUserInputBuf)
451         return NotImplemented;
452
453     if(bitmap->lockmode)
454         return WrongState;
455
456     if (bitmap->bits && bitmap->format == format)
457     {
458         /* no conversion is necessary; just use the bits directly */
459         lockeddata->Width = act_rect.Width;
460         lockeddata->Height = act_rect.Height;
461         lockeddata->PixelFormat = format;
462         lockeddata->Reserved = flags;
463         lockeddata->Stride = bitmap->stride;
464         lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
465                             bitmap->stride * act_rect.Y;
466
467         bitmap->lockmode = flags;
468         bitmap->numlocks++;
469
470         return Ok;
471     }
472
473     hbm = bitmap->hbitmap;
474     hdc = bitmap->hdc;
475     bm_is_selected = (hdc != 0);
476
477     pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
478     if (!pbmi)
479         return OutOfMemory;
480     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
481     pbmi->bmiHeader.biBitCount = 0;
482
483     if(!bm_is_selected){
484         hdc = CreateCompatibleDC(0);
485         old = SelectObject(hdc, hbm);
486     }
487
488     /* fill out bmi */
489     GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
490
491     abs_height = abs(pbmi->bmiHeader.biHeight);
492     stride = pbmi->bmiHeader.biWidth * bitspp / 8;
493     stride = (stride + 3) & ~3;
494
495     buff = GdipAlloc(stride * abs_height);
496
497     pbmi->bmiHeader.biBitCount = bitspp;
498
499     if(buff)
500         GetDIBits(hdc, hbm, 0, abs_height, buff, pbmi, DIB_RGB_COLORS);
501
502     if(!bm_is_selected){
503         SelectObject(hdc, old);
504         DeleteDC(hdc);
505     }
506
507     if(!buff){
508         GdipFree(pbmi);
509         return OutOfMemory;
510     }
511
512     lockeddata->Width  = act_rect.Width;
513     lockeddata->Height = act_rect.Height;
514     lockeddata->PixelFormat = format;
515     lockeddata->Reserved = flags;
516
517     if(pbmi->bmiHeader.biHeight > 0){
518         lockeddata->Stride = -stride;
519         lockeddata->Scan0  = buff + (bitspp / 8) * act_rect.X +
520                              stride * (abs_height - 1 - act_rect.Y);
521     }
522     else{
523         lockeddata->Stride = stride;
524         lockeddata->Scan0  = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
525     }
526
527     bitmap->lockmode = flags;
528     bitmap->numlocks++;
529
530     bitmap->bitmapbits = buff;
531
532     GdipFree(pbmi);
533     return Ok;
534 }
535
536 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
537 {
538     FIXME("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
539
540     return NotImplemented;
541 }
542
543 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
544     BitmapData* lockeddata)
545 {
546     HDC hdc;
547     HBITMAP hbm, old = NULL;
548     BOOL bm_is_selected;
549     BITMAPINFO *pbmi;
550
551     if(!bitmap || !lockeddata)
552         return InvalidParameter;
553
554     if(!bitmap->lockmode)
555         return WrongState;
556
557     if(lockeddata->Reserved & ImageLockModeUserInputBuf)
558         return NotImplemented;
559
560     if(lockeddata->Reserved & ImageLockModeRead){
561         if(!(--bitmap->numlocks))
562             bitmap->lockmode = 0;
563
564         GdipFree(bitmap->bitmapbits);
565         bitmap->bitmapbits = NULL;
566         return Ok;
567     }
568
569     if (!bitmap->bitmapbits)
570     {
571         /* we passed a direct reference; no need to do anything */
572         bitmap->lockmode = 0;
573         return Ok;
574     }
575
576     hbm = bitmap->hbitmap;
577     hdc = bitmap->hdc;
578     bm_is_selected = (hdc != 0);
579
580     pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
581     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
582     pbmi->bmiHeader.biBitCount = 0;
583
584     if(!bm_is_selected){
585         hdc = CreateCompatibleDC(0);
586         old = SelectObject(hdc, hbm);
587     }
588
589     GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
590     pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
591     SetDIBits(hdc, hbm, 0, abs(pbmi->bmiHeader.biHeight),
592               bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
593
594     if(!bm_is_selected){
595         SelectObject(hdc, old);
596         DeleteDC(hdc);
597     }
598
599     GdipFree(pbmi);
600     GdipFree(bitmap->bitmapbits);
601     bitmap->bitmapbits = NULL;
602     bitmap->lockmode = 0;
603
604     return Ok;
605 }
606
607 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
608     PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
609 {
610     FIXME("(%f,%f,%f,%f,%i,%p,%p): stub\n", x, y, width, height, format, srcBitmap, dstBitmap);
611
612     return NotImplemented;
613 }
614
615 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
616     PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
617 {
618     FIXME("(%i,%i,%i,%i,%i,%p,%p): stub\n", x, y, width, height, format, srcBitmap, dstBitmap);
619
620     return NotImplemented;
621 }
622
623 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
624 {
625     GpStatus stat = GenericError;
626
627     TRACE("%p, %p\n", image, cloneImage);
628
629     if (!image || !cloneImage)
630         return InvalidParameter;
631
632     if (image->picture)
633     {
634         IStream* stream;
635         HRESULT hr;
636         INT size;
637         LARGE_INTEGER move;
638
639         hr = CreateStreamOnHGlobal(0, TRUE, &stream);
640         if (FAILED(hr))
641             return GenericError;
642
643         hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
644         if(FAILED(hr))
645         {
646             WARN("Failed to save image on stream\n");
647             goto out;
648         }
649
650         /* Set seek pointer back to the beginning of the picture */
651         move.QuadPart = 0;
652         hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
653         if (FAILED(hr))
654             goto out;
655
656         stat = GdipLoadImageFromStream(stream, cloneImage);
657         if (stat != Ok) WARN("Failed to load image from stream\n");
658
659     out:
660         IStream_Release(stream);
661         return stat;
662     }
663     else if (image->type == ImageTypeBitmap)
664     {
665         GpBitmap *bitmap = (GpBitmap*)image;
666         BitmapData lockeddata_src, lockeddata_dst;
667         int i;
668         UINT row_size;
669
670         stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
671             &lockeddata_src);
672         if (stat != Ok) return stat;
673
674         stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
675             0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
676         if (stat == Ok)
677         {
678             stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
679                 lockeddata_src.PixelFormat, &lockeddata_dst);
680
681             if (stat == Ok)
682             {
683                 /* copy the image data */
684                 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
685                 for (i=0; i<lockeddata_src.Height; i++)
686                     memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
687                            (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
688                            row_size);
689
690                 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
691             }
692
693             GdipBitmapUnlockBits(bitmap, &lockeddata_src);
694         }
695
696         if (stat != Ok)
697         {
698             GdipDisposeImage(*cloneImage);
699             *cloneImage = NULL;
700         }
701         else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
702
703         return stat;
704     }
705     else
706     {
707         ERR("GpImage with no IPicture or bitmap?!\n");
708         return NotImplemented;
709     }
710 }
711
712 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
713     GpBitmap **bitmap)
714 {
715     GpStatus stat;
716     IStream *stream;
717
718     TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
719
720     if(!filename || !bitmap)
721         return InvalidParameter;
722
723     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
724
725     if(stat != Ok)
726         return stat;
727
728     stat = GdipCreateBitmapFromStream(stream, bitmap);
729
730     IStream_Release(stream);
731
732     return stat;
733 }
734
735 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
736                                                VOID *bits, GpBitmap **bitmap)
737 {
738     DWORD height, stride;
739     PixelFormat format;
740
741     FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
742
743     height = abs(info->bmiHeader.biHeight);
744     stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
745
746     if(info->bmiHeader.biHeight > 0) /* bottom-up */
747     {
748         bits = (BYTE*)bits + (height - 1) * stride;
749         stride = -stride;
750     }
751
752     switch(info->bmiHeader.biBitCount) {
753     case 1:
754         format = PixelFormat1bppIndexed;
755         break;
756     case 4:
757         format = PixelFormat4bppIndexed;
758         break;
759     case 8:
760         format = PixelFormat8bppIndexed;
761         break;
762     case 24:
763         format = PixelFormat24bppRGB;
764         break;
765     default:
766         FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
767         *bitmap = NULL;
768         return InvalidParameter;
769     }
770
771     return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
772                                      bits, bitmap);
773
774 }
775
776 /* FIXME: no icm */
777 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
778     GpBitmap **bitmap)
779 {
780     TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
781
782     return GdipCreateBitmapFromFile(filename, bitmap);
783 }
784
785 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
786     GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
787 {
788     HBITMAP hbm;
789     GpStatus stat = InvalidParameter;
790
791     TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
792
793     if(!lpBitmapName || !bitmap)
794         return InvalidParameter;
795
796     /* load DIB */
797     hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
798                      LR_CREATEDIBSECTION);
799
800     if(hbm){
801         stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
802         DeleteObject(hbm);
803     }
804
805     return stat;
806 }
807
808 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
809     HBITMAP* hbmReturn, ARGB background)
810 {
811     GpStatus stat;
812     HBITMAP result, oldbitmap;
813     UINT width, height;
814     HDC hdc;
815     GpGraphics *graphics;
816     BITMAPINFOHEADER bih;
817     void *bits;
818     TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
819
820     if (!bitmap || !hbmReturn) return InvalidParameter;
821
822     GdipGetImageWidth((GpImage*)bitmap, &width);
823     GdipGetImageHeight((GpImage*)bitmap, &height);
824
825     bih.biSize = sizeof(bih);
826     bih.biWidth = width;
827     bih.biHeight = height;
828     bih.biPlanes = 1;
829     bih.biBitCount = 32;
830     bih.biCompression = BI_RGB;
831     bih.biSizeImage = 0;
832     bih.biXPelsPerMeter = 0;
833     bih.biYPelsPerMeter = 0;
834     bih.biClrUsed = 0;
835     bih.biClrImportant = 0;
836
837     hdc = CreateCompatibleDC(NULL);
838     if (!hdc) return GenericError;
839
840     result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
841         NULL, 0);
842
843     if (result)
844     {
845         oldbitmap = SelectObject(hdc, result);
846
847         stat = GdipCreateFromHDC(hdc, &graphics);
848         if (stat == Ok)
849         {
850             stat = GdipGraphicsClear(graphics, background);
851
852             if (stat == Ok)
853                 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
854
855             GdipDeleteGraphics(graphics);
856         }
857
858         SelectObject(hdc, oldbitmap);
859     }
860     else
861         stat = GenericError;
862
863     DeleteDC(hdc);
864
865     if (stat != Ok && result)
866     {
867         DeleteObject(result);
868         result = NULL;
869     }
870
871     *hbmReturn = result;
872
873     return stat;
874 }
875
876 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
877     GpMetafile* metafile, BOOL* succ, EmfType emfType,
878     const WCHAR* description, GpMetafile** out_metafile)
879 {
880     static int calls;
881
882     if(!ref || !metafile || !out_metafile)
883         return InvalidParameter;
884
885     *succ = FALSE;
886     *out_metafile = NULL;
887
888     if(!(calls++))
889         FIXME("not implemented\n");
890
891     return NotImplemented;
892 }
893
894 /* FIXME: this should create a bitmap in the given size with the attributes
895  * (resolution etc.) of the graphics object */
896 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
897     GpGraphics* target, GpBitmap** bitmap)
898 {
899     static int calls;
900     GpStatus ret;
901
902     if(!target || !bitmap)
903         return InvalidParameter;
904
905     if(!(calls++))
906         FIXME("hacked stub\n");
907
908     ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
909                                     NULL, bitmap);
910
911     return ret;
912 }
913
914 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
915 {
916     GpStatus stat;
917     ICONINFO iinfo;
918     BITMAP bm;
919     int ret;
920     UINT width, height;
921     GpRect rect;
922     BitmapData lockeddata;
923     HDC screendc;
924     BOOL has_alpha;
925     int x, y;
926     BYTE *bits;
927     BITMAPINFOHEADER bih;
928     DWORD *src;
929     BYTE *dst_row;
930     DWORD *dst;
931
932     TRACE("%p, %p\n", hicon, bitmap);
933
934     if(!bitmap || !GetIconInfo(hicon, &iinfo))
935     {
936         DeleteObject(iinfo.hbmColor);
937         DeleteObject(iinfo.hbmMask);
938         return InvalidParameter;
939     }
940
941     /* get the size of the icon */
942     ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
943     if (ret == 0) {
944         DeleteObject(iinfo.hbmColor);
945         DeleteObject(iinfo.hbmMask);
946         return GenericError;
947     }
948
949     width = bm.bmWidth;
950
951     if (iinfo.hbmColor)
952         height = abs(bm.bmHeight);
953     else /* combined bitmap + mask */
954         height = abs(bm.bmHeight) / 2;
955
956     bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
957     if (!bits) {
958         DeleteObject(iinfo.hbmColor);
959         DeleteObject(iinfo.hbmMask);
960         return OutOfMemory;
961     }
962
963     stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
964     if (stat != Ok) {
965         DeleteObject(iinfo.hbmColor);
966         DeleteObject(iinfo.hbmMask);
967         HeapFree(GetProcessHeap(), 0, bits);
968         return stat;
969     }
970
971     rect.X = 0;
972     rect.Y = 0;
973     rect.Width = width;
974     rect.Height = height;
975
976     stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
977     if (stat != Ok) {
978         DeleteObject(iinfo.hbmColor);
979         DeleteObject(iinfo.hbmMask);
980         HeapFree(GetProcessHeap(), 0, bits);
981         GdipDisposeImage((GpImage*)*bitmap);
982         return stat;
983     }
984
985     bih.biSize = sizeof(bih);
986     bih.biWidth = width;
987     bih.biHeight = -height;
988     bih.biPlanes = 1;
989     bih.biBitCount = 32;
990     bih.biCompression = BI_RGB;
991     bih.biSizeImage = 0;
992     bih.biXPelsPerMeter = 0;
993     bih.biYPelsPerMeter = 0;
994     bih.biClrUsed = 0;
995     bih.biClrImportant = 0;
996
997     screendc = GetDC(0);
998     if (iinfo.hbmColor)
999     {
1000         GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1001
1002         if (bm.bmBitsPixel == 32)
1003         {
1004             has_alpha = FALSE;
1005
1006             /* If any pixel has a non-zero alpha, ignore hbmMask */
1007             src = (DWORD*)bits;
1008             for (x=0; x<width && !has_alpha; x++)
1009                 for (y=0; y<height && !has_alpha; y++)
1010                     if ((*src++ & 0xff000000) != 0)
1011                         has_alpha = TRUE;
1012         }
1013         else has_alpha = FALSE;
1014     }
1015     else
1016     {
1017         GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1018         has_alpha = FALSE;
1019     }
1020
1021     /* copy the image data to the Bitmap */
1022     src = (DWORD*)bits;
1023     dst_row = lockeddata.Scan0;
1024     for (y=0; y<height; y++)
1025     {
1026         memcpy(dst_row, src, width*4);
1027         src += width;
1028         dst_row += lockeddata.Stride;
1029     }
1030
1031     if (!has_alpha)
1032     {
1033         if (iinfo.hbmMask)
1034         {
1035             /* read alpha data from the mask */
1036             if (iinfo.hbmColor)
1037                 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1038             else
1039                 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1040
1041             src = (DWORD*)bits;
1042             dst_row = lockeddata.Scan0;
1043             for (y=0; y<height; y++)
1044             {
1045                 dst = (DWORD*)dst_row;
1046                 for (x=0; x<height; x++)
1047                 {
1048                     DWORD src_value = *src++;
1049                     if (src_value)
1050                         *dst++ = 0;
1051                     else
1052                         *dst++ |= 0xff000000;
1053                 }
1054                 dst_row += lockeddata.Stride;
1055             }
1056         }
1057         else
1058         {
1059             /* set constant alpha of 255 */
1060             dst_row = bits;
1061             for (y=0; y<height; y++)
1062             {
1063                 dst = (DWORD*)dst_row;
1064                 for (x=0; x<height; x++)
1065                     *dst++ |= 0xff000000;
1066                 dst_row += lockeddata.Stride;
1067             }
1068         }
1069     }
1070
1071     ReleaseDC(0, screendc);
1072
1073     DeleteObject(iinfo.hbmColor);
1074     DeleteObject(iinfo.hbmMask);
1075
1076     GdipBitmapUnlockBits(*bitmap, &lockeddata);
1077
1078     HeapFree(GetProcessHeap(), 0, bits);
1079
1080     return Ok;
1081 }
1082
1083 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1084     PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1085 {
1086     BITMAPINFOHEADER bmih;
1087     HBITMAP hbitmap;
1088     INT row_size, dib_stride;
1089     HDC hdc;
1090     BYTE *bits;
1091     int i;
1092
1093     TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
1094
1095     if (!bitmap) return InvalidParameter;
1096
1097     if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1098         *bitmap = NULL;
1099         return InvalidParameter;
1100     }
1101
1102     if(scan0 && !stride)
1103         return InvalidParameter;
1104
1105     row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1106     dib_stride = (row_size + 3) & ~3;
1107
1108     if(stride == 0)
1109         stride = dib_stride;
1110
1111     bmih.biSize = sizeof(BITMAPINFOHEADER);
1112     bmih.biWidth = width;
1113     bmih.biHeight = -height;
1114     bmih.biPlanes = 1;
1115     /* FIXME: use the rest of the data from format */
1116     bmih.biBitCount = PIXELFORMATBPP(format);
1117     bmih.biCompression = BI_RGB;
1118     bmih.biSizeImage = 0;
1119     bmih.biXPelsPerMeter = 0;
1120     bmih.biYPelsPerMeter = 0;
1121     bmih.biClrUsed = 0;
1122     bmih.biClrImportant = 0;
1123
1124     hdc = CreateCompatibleDC(NULL);
1125     if (!hdc) return GenericError;
1126
1127     hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, (void**)&bits,
1128         NULL, 0);
1129
1130     DeleteDC(hdc);
1131
1132     if (!hbitmap) return GenericError;
1133
1134     /* copy bits to the dib if necessary */
1135     /* FIXME: should reference the bits instead of copying them */
1136     if (scan0)
1137         for (i=0; i<height; i++)
1138             memcpy(bits+i*dib_stride, scan0+i*stride, row_size);
1139
1140     *bitmap = GdipAlloc(sizeof(GpBitmap));
1141     if(!*bitmap)
1142     {
1143         DeleteObject(hbitmap);
1144         return OutOfMemory;
1145     }
1146
1147     (*bitmap)->image.type = ImageTypeBitmap;
1148     memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1149     (*bitmap)->image.flags = ImageFlagsNone;
1150     (*bitmap)->width = width;
1151     (*bitmap)->height = height;
1152     (*bitmap)->format = format;
1153     (*bitmap)->image.picture = NULL;
1154     (*bitmap)->hbitmap = hbitmap;
1155     (*bitmap)->hdc = NULL;
1156     (*bitmap)->bits = bits;
1157     (*bitmap)->stride = dib_stride;
1158
1159     return Ok;
1160 }
1161
1162 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1163     GpBitmap **bitmap)
1164 {
1165     GpStatus stat;
1166
1167     TRACE("%p %p\n", stream, bitmap);
1168
1169     stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1170
1171     if(stat != Ok)
1172         return stat;
1173
1174     if((*bitmap)->image.type != ImageTypeBitmap){
1175         GdipDisposeImage(&(*bitmap)->image);
1176         *bitmap = NULL;
1177         return GenericError; /* FIXME: what error to return? */
1178     }
1179
1180     return Ok;
1181 }
1182
1183 /* FIXME: no icm */
1184 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1185     GpBitmap **bitmap)
1186 {
1187     TRACE("%p %p\n", stream, bitmap);
1188
1189     return GdipCreateBitmapFromStream(stream, bitmap);
1190 }
1191
1192 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1193     GpCachedBitmap **cachedbmp)
1194 {
1195     GpStatus stat;
1196
1197     TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1198
1199     if(!bitmap || !graphics || !cachedbmp)
1200         return InvalidParameter;
1201
1202     *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1203     if(!*cachedbmp)
1204         return OutOfMemory;
1205
1206     stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1207     if(stat != Ok){
1208         GdipFree(*cachedbmp);
1209         return stat;
1210     }
1211
1212     return Ok;
1213 }
1214
1215 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1216 {
1217     FIXME("(%p, %p)\n", bitmap, hicon);
1218
1219     return NotImplemented;
1220 }
1221
1222 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1223 {
1224     TRACE("%p\n", cachedbmp);
1225
1226     if(!cachedbmp)
1227         return InvalidParameter;
1228
1229     GdipDisposeImage(cachedbmp->image);
1230     GdipFree(cachedbmp);
1231
1232     return Ok;
1233 }
1234
1235 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1236     GpCachedBitmap *cachedbmp, INT x, INT y)
1237 {
1238     TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1239
1240     if(!graphics || !cachedbmp)
1241         return InvalidParameter;
1242
1243     return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1244 }
1245
1246 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1247     LPBYTE pData16, INT iMapMode, INT eFlags)
1248 {
1249     FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1250     return NotImplemented;
1251 }
1252
1253 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
1254 {
1255     TRACE("%p\n", image);
1256
1257     if(!image)
1258         return InvalidParameter;
1259
1260     if (image->picture)
1261         IPicture_Release(image->picture);
1262     if (image->type == ImageTypeBitmap)
1263     {
1264         GdipFree(((GpBitmap*)image)->bitmapbits);
1265         DeleteDC(((GpBitmap*)image)->hdc);
1266     }
1267     GdipFree(image);
1268
1269     return Ok;
1270 }
1271
1272 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
1273 {
1274     if(!image || !item)
1275         return InvalidParameter;
1276
1277     return NotImplemented;
1278 }
1279
1280 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
1281     GpUnit *srcUnit)
1282 {
1283     TRACE("%p %p %p\n", image, srcRect, srcUnit);
1284
1285     if(!image || !srcRect || !srcUnit)
1286         return InvalidParameter;
1287     if(image->type == ImageTypeMetafile){
1288         *srcRect = ((GpMetafile*)image)->bounds;
1289         *srcUnit = ((GpMetafile*)image)->unit;
1290     }
1291     else if(image->type == ImageTypeBitmap){
1292         srcRect->X = srcRect->Y = 0.0;
1293         srcRect->Width = (REAL) ((GpBitmap*)image)->width;
1294         srcRect->Height = (REAL) ((GpBitmap*)image)->height;
1295         *srcUnit = UnitPixel;
1296     }
1297     else{
1298         srcRect->X = srcRect->Y = 0.0;
1299         srcRect->Width = ipicture_pixel_width(image->picture);
1300         srcRect->Height = ipicture_pixel_height(image->picture);
1301         *srcUnit = UnitPixel;
1302     }
1303
1304     TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
1305           srcRect->Width, srcRect->Height, *srcUnit);
1306
1307     return Ok;
1308 }
1309
1310 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
1311     REAL *height)
1312 {
1313     TRACE("%p %p %p\n", image, width, height);
1314
1315     if(!image || !height || !width)
1316         return InvalidParameter;
1317
1318     if(image->type == ImageTypeMetafile){
1319         HDC hdc = GetDC(0);
1320
1321         *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1322                         ((GpMetafile*)image)->bounds.Height;
1323
1324         *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1325                         ((GpMetafile*)image)->bounds.Width;
1326
1327         ReleaseDC(0, hdc);
1328     }
1329
1330     else if(image->type == ImageTypeBitmap){
1331         *height = ((GpBitmap*)image)->height;
1332         *width = ((GpBitmap*)image)->width;
1333     }
1334     else{
1335         *height = ipicture_pixel_height(image->picture);
1336         *width = ipicture_pixel_width(image->picture);
1337     }
1338
1339     TRACE("returning (%f, %f)\n", *height, *width);
1340     return Ok;
1341 }
1342
1343 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
1344     GpGraphics **graphics)
1345 {
1346     HDC hdc;
1347
1348     TRACE("%p %p\n", image, graphics);
1349
1350     if(!image || !graphics)
1351         return InvalidParameter;
1352
1353     if(image->type != ImageTypeBitmap){
1354         FIXME("not implemented for image type %d\n", image->type);
1355         return NotImplemented;
1356     }
1357
1358     hdc = ((GpBitmap*)image)->hdc;
1359
1360     if(!hdc){
1361         hdc = CreateCompatibleDC(0);
1362         SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
1363         ((GpBitmap*)image)->hdc = hdc;
1364     }
1365
1366     return GdipCreateFromHDC(hdc, graphics);
1367 }
1368
1369 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
1370 {
1371     TRACE("%p %p\n", image, height);
1372
1373     if(!image || !height)
1374         return InvalidParameter;
1375
1376     if(image->type == ImageTypeMetafile){
1377         HDC hdc = GetDC(0);
1378
1379         *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1380                         ((GpMetafile*)image)->bounds.Height);
1381
1382         ReleaseDC(0, hdc);
1383     }
1384     else if(image->type == ImageTypeBitmap)
1385         *height = ((GpBitmap*)image)->height;
1386     else
1387         *height = ipicture_pixel_height(image->picture);
1388
1389     TRACE("returning %d\n", *height);
1390
1391     return Ok;
1392 }
1393
1394 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
1395 {
1396     static int calls;
1397
1398     if(!image || !res)
1399         return InvalidParameter;
1400
1401     if(!(calls++))
1402         FIXME("not implemented\n");
1403
1404     return NotImplemented;
1405 }
1406
1407 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
1408 {
1409     FIXME("%p %p\n", image, size);
1410
1411     if(!image || !size)
1412         return InvalidParameter;
1413
1414     return NotImplemented;
1415 }
1416
1417 /* FIXME: test this function for non-bitmap types */
1418 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
1419 {
1420     TRACE("%p %p\n", image, format);
1421
1422     if(!image || !format)
1423         return InvalidParameter;
1424
1425     if(image->type != ImageTypeBitmap)
1426         *format = PixelFormat24bppRGB;
1427     else
1428         *format = ((GpBitmap*) image)->format;
1429
1430     return Ok;
1431 }
1432
1433 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
1434 {
1435     if(!image || !format)
1436         return InvalidParameter;
1437
1438     memcpy(format, &image->format, sizeof(GUID));
1439
1440     return Ok;
1441 }
1442
1443 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
1444 {
1445     TRACE("%p %p\n", image, type);
1446
1447     if(!image || !type)
1448         return InvalidParameter;
1449
1450     *type = image->type;
1451
1452     return Ok;
1453 }
1454
1455 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
1456 {
1457     static int calls;
1458
1459     if(!image || !res)
1460         return InvalidParameter;
1461
1462     if(!(calls++))
1463         FIXME("not implemented\n");
1464
1465     return NotImplemented;
1466 }
1467
1468 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
1469 {
1470     TRACE("%p %p\n", image, width);
1471
1472     if(!image || !width)
1473         return InvalidParameter;
1474
1475     if(image->type == ImageTypeMetafile){
1476         HDC hdc = GetDC(0);
1477
1478         *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1479                         ((GpMetafile*)image)->bounds.Width);
1480
1481         ReleaseDC(0, hdc);
1482     }
1483     else if(image->type == ImageTypeBitmap)
1484         *width = ((GpBitmap*)image)->width;
1485     else
1486         *width = ipicture_pixel_width(image->picture);
1487
1488     TRACE("returning %d\n", *width);
1489
1490     return Ok;
1491 }
1492
1493 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
1494     MetafileHeader * header)
1495 {
1496     static int calls;
1497
1498     if(!metafile || !header)
1499         return InvalidParameter;
1500
1501     if(!(calls++))
1502         FIXME("not implemented\n");
1503
1504     return Ok;
1505 }
1506
1507 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
1508     UINT num, PropertyItem* items)
1509 {
1510     static int calls;
1511
1512     if(!(calls++))
1513         FIXME("not implemented\n");
1514
1515     return InvalidParameter;
1516 }
1517
1518 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
1519 {
1520     static int calls;
1521
1522     if(!(calls++))
1523         FIXME("not implemented\n");
1524
1525     return InvalidParameter;
1526 }
1527
1528 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
1529 {
1530     static int calls;
1531
1532     if(!(calls++))
1533         FIXME("not implemented\n");
1534
1535     return InvalidParameter;
1536 }
1537
1538 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
1539     PropertyItem* buffer)
1540 {
1541     static int calls;
1542
1543     if(!(calls++))
1544         FIXME("not implemented\n");
1545
1546     return InvalidParameter;
1547 }
1548
1549 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
1550     UINT* size)
1551 {
1552     static int calls;
1553
1554     TRACE("%p %x %p\n", image, pid, size);
1555
1556     if(!size || !image)
1557         return InvalidParameter;
1558
1559     if(!(calls++))
1560         FIXME("not implemented\n");
1561
1562     return NotImplemented;
1563 }
1564
1565 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
1566 {
1567     static int calls;
1568
1569     if(!(calls++))
1570         FIXME("not implemented\n");
1571
1572     return InvalidParameter;
1573 }
1574
1575 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
1576     GDIPCONST GUID* dimensionID, UINT* count)
1577 {
1578     static int calls;
1579
1580     if(!image || !dimensionID || !count)
1581         return InvalidParameter;
1582
1583     if(!(calls++))
1584         FIXME("not implemented\n");
1585
1586     return NotImplemented;
1587 }
1588
1589 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
1590     UINT* count)
1591 {
1592     if(!image || !count)
1593         return InvalidParameter;
1594
1595     *count = 1;
1596
1597     FIXME("stub\n");
1598
1599     return Ok;
1600 }
1601
1602 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
1603     GUID* dimensionIDs, UINT count)
1604 {
1605     static int calls;
1606
1607     if(!image || !dimensionIDs)
1608         return InvalidParameter;
1609
1610     if(!(calls++))
1611         FIXME("not implemented\n");
1612
1613     return Ok;
1614 }
1615
1616 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
1617     GDIPCONST GUID* dimensionID, UINT frameidx)
1618 {
1619     static int calls;
1620
1621     if(!image || !dimensionID)
1622         return InvalidParameter;
1623
1624     if(!(calls++))
1625         FIXME("not implemented\n");
1626
1627     return Ok;
1628 }
1629
1630 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
1631                                           GpImage **image)
1632 {
1633     GpStatus stat;
1634     IStream *stream;
1635
1636     TRACE("(%s) %p\n", debugstr_w(filename), image);
1637
1638     if (!filename || !image)
1639         return InvalidParameter;
1640
1641     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1642
1643     if (stat != Ok)
1644         return stat;
1645
1646     stat = GdipLoadImageFromStream(stream, image);
1647
1648     IStream_Release(stream);
1649
1650     return stat;
1651 }
1652
1653 /* FIXME: no icm handling */
1654 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
1655 {
1656     TRACE("(%s) %p\n", debugstr_w(filename), image);
1657
1658     return GdipLoadImageFromFile(filename, image);
1659 }
1660
1661 static const WICPixelFormatGUID *wic_pixel_formats[] = {
1662     &GUID_WICPixelFormat16bppBGR555,
1663     &GUID_WICPixelFormat24bppBGR,
1664     &GUID_WICPixelFormat32bppBGR,
1665     &GUID_WICPixelFormat32bppBGRA,
1666     &GUID_WICPixelFormat32bppPBGRA,
1667     NULL
1668 };
1669
1670 static const PixelFormat wic_gdip_formats[] = {
1671     PixelFormat16bppRGB555,
1672     PixelFormat24bppRGB,
1673     PixelFormat32bppRGB,
1674     PixelFormat32bppARGB,
1675     PixelFormat32bppPARGB,
1676 };
1677
1678 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
1679 {
1680     GpStatus status=Ok;
1681     GpBitmap *bitmap;
1682     HRESULT hr;
1683     IWICBitmapDecoder *decoder;
1684     IWICBitmapFrameDecode *frame;
1685     IWICBitmapSource *source=NULL;
1686     WICPixelFormatGUID wic_format;
1687     PixelFormat gdip_format=0;
1688     int i;
1689     UINT width, height;
1690     BitmapData lockeddata;
1691     WICRect wrc;
1692     HRESULT initresult;
1693
1694     initresult = CoInitialize(NULL);
1695
1696     hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
1697         &IID_IWICBitmapDecoder, (void**)&decoder);
1698     if (FAILED(hr)) goto end;
1699
1700     hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
1701     if (SUCCEEDED(hr))
1702         hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
1703
1704     if (SUCCEEDED(hr)) /* got frame */
1705     {
1706         hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
1707
1708         if (SUCCEEDED(hr))
1709         {
1710             for (i=0; wic_pixel_formats[i]; i++)
1711             {
1712                 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
1713                 {
1714                     source = (IWICBitmapSource*)frame;
1715                     IWICBitmapSource_AddRef(source);
1716                     gdip_format = wic_gdip_formats[i];
1717                     break;
1718                 }
1719             }
1720             if (!source)
1721             {
1722                 /* unknown format; fall back on 32bppARGB */
1723                 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
1724                 gdip_format = PixelFormat32bppARGB;
1725             }
1726         }
1727
1728         if (SUCCEEDED(hr)) /* got source */
1729         {
1730             hr = IWICBitmapSource_GetSize(source, &width, &height);
1731
1732             if (SUCCEEDED(hr))
1733                 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
1734                     NULL, &bitmap);
1735
1736             if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
1737             {
1738                 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
1739                     gdip_format, &lockeddata);
1740                 if (status == Ok) /* locked bitmap */
1741                 {
1742                     wrc.X = 0;
1743                     wrc.Width = width;
1744                     wrc.Height = 1;
1745                     for (i=0; i<height; i++)
1746                     {
1747                         wrc.Y = i;
1748                         hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
1749                             abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
1750                         if (FAILED(hr)) break;
1751                     }
1752
1753                     GdipBitmapUnlockBits(bitmap, &lockeddata);
1754                 }
1755
1756                 if (SUCCEEDED(hr) && status == Ok)
1757                     *image = (GpImage*)bitmap;
1758                 else
1759                 {
1760                     *image = NULL;
1761                     GdipDisposeImage((GpImage*)bitmap);
1762                 }
1763             }
1764
1765             IWICBitmapSource_Release(source);
1766         }
1767
1768         IWICBitmapFrameDecode_Release(frame);
1769     }
1770
1771     IWICBitmapDecoder_Release(decoder);
1772
1773 end:
1774     if (SUCCEEDED(initresult)) CoUninitialize();
1775
1776     if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
1777
1778     return status;
1779 }
1780
1781 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
1782 {
1783     return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
1784 }
1785
1786 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, GpImage **image)
1787 {
1788     GpStatus status;
1789     GpBitmap* bitmap;
1790
1791     status = decode_image_wic(stream, &CLSID_WICBmpDecoder, image);
1792
1793     bitmap = (GpBitmap*)*image;
1794
1795     if (status == Ok && bitmap->format == PixelFormat32bppARGB)
1796     {
1797         /* WIC supports bmp files with alpha, but gdiplus does not */
1798         bitmap->format = PixelFormat32bppRGB;
1799     }
1800
1801     return status;
1802 }
1803
1804 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
1805 {
1806     return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
1807 }
1808
1809 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, GpImage **image)
1810 {
1811     return decode_image_wic(stream, &CLSID_WICPngDecoder, image);
1812 }
1813
1814 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
1815 {
1816     return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
1817 }
1818
1819 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
1820 {
1821     IPicture *pic;
1822
1823     TRACE("%p %p\n", stream, image);
1824
1825     if(!stream || !image)
1826         return InvalidParameter;
1827
1828     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1829         (LPVOID*) &pic) != S_OK){
1830         TRACE("Could not load picture\n");
1831         return GenericError;
1832     }
1833
1834     /* FIXME: missing initialization code */
1835     *image = GdipAlloc(sizeof(GpMetafile));
1836     if(!*image) return OutOfMemory;
1837     (*image)->type = ImageTypeMetafile;
1838     (*image)->picture = pic;
1839     (*image)->flags   = ImageFlagsNone;
1840
1841     return Ok;
1842 }
1843
1844 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
1845     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
1846
1847 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
1848
1849 typedef struct image_codec {
1850     ImageCodecInfo info;
1851     encode_image_func encode_func;
1852     decode_image_func decode_func;
1853 } image_codec;
1854
1855 typedef enum {
1856     BMP,
1857     JPEG,
1858     GIF,
1859     EMF,
1860     WMF,
1861     PNG,
1862     ICO,
1863     NUM_CODECS
1864 } ImageFormat;
1865
1866 static const struct image_codec codecs[NUM_CODECS];
1867
1868 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
1869 {
1870     BYTE signature[8];
1871     LARGE_INTEGER seek;
1872     HRESULT hr;
1873     UINT bytesread;
1874     int i, j;
1875
1876     /* seek to the start of the stream */
1877     seek.QuadPart = 0;
1878     hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
1879     if (FAILED(hr)) return hresult_to_status(hr);
1880
1881     /* read the first 8 bytes */
1882     /* FIXME: This assumes all codecs have one signature <= 8 bytes in length */
1883     hr = IStream_Read(stream, signature, 8, &bytesread);
1884     if (FAILED(hr)) return hresult_to_status(hr);
1885     if (hr == S_FALSE || bytesread == 0) return GenericError;
1886
1887     for (i = 0; i < NUM_CODECS; i++) {
1888         if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
1889             bytesread >= codecs[i].info.SigSize)
1890         {
1891             for (j=0; j<codecs[i].info.SigSize; j++)
1892                 if ((signature[j] & codecs[i].info.SigMask[j]) != codecs[i].info.SigPattern[j])
1893                     break;
1894             if (j == codecs[i].info.SigSize)
1895             {
1896                 *result = &codecs[i];
1897                 return Ok;
1898             }
1899         }
1900     }
1901
1902     TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
1903         signature[0],signature[1],signature[2],signature[3],
1904         signature[4],signature[5],signature[6],signature[7]);
1905
1906     return GenericError;
1907 }
1908
1909 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
1910 {
1911     GpStatus stat;
1912     LARGE_INTEGER seek;
1913     HRESULT hr;
1914     const struct image_codec *codec=NULL;
1915
1916     /* choose an appropriate image decoder */
1917     stat = get_decoder_info(stream, &codec);
1918     if (stat != Ok) return stat;
1919
1920     /* seek to the start of the stream */
1921     seek.QuadPart = 0;
1922     hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
1923     if (FAILED(hr)) return hresult_to_status(hr);
1924
1925     /* call on the image decoder to do the real work */
1926     stat = codec->decode_func(stream, &codec->info.Clsid, image);
1927
1928     /* take note of the original data format */
1929     if (stat == Ok)
1930     {
1931         memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
1932     }
1933
1934     return stat;
1935 }
1936
1937 /* FIXME: no ICM */
1938 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1939 {
1940     TRACE("%p %p\n", stream, image);
1941
1942     return GdipLoadImageFromStream(stream, image);
1943 }
1944
1945 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1946 {
1947     static int calls;
1948
1949     if(!image)
1950         return InvalidParameter;
1951
1952     if(!(calls++))
1953         FIXME("not implemented\n");
1954
1955     return NotImplemented;
1956 }
1957
1958 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1959 {
1960     static int calls;
1961
1962     if(!(calls++))
1963         FIXME("not implemented\n");
1964
1965     return NotImplemented;
1966 }
1967
1968 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1969                                         GDIPCONST CLSID *clsidEncoder,
1970                                         GDIPCONST EncoderParameters *encoderParams)
1971 {
1972     GpStatus stat;
1973     IStream *stream;
1974
1975     TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1976
1977     if (!image || !filename|| !clsidEncoder)
1978         return InvalidParameter;
1979
1980     stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1981     if (stat != Ok)
1982         return GenericError;
1983
1984     stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1985
1986     IStream_Release(stream);
1987     return stat;
1988 }
1989
1990 /*************************************************************************
1991  * Encoding functions -
1992  *   These functions encode an image in different image file formats.
1993  */
1994 #define BITMAP_FORMAT_BMP   0x4d42 /* "BM" */
1995 #define BITMAP_FORMAT_JPEG  0xd8ff
1996 #define BITMAP_FORMAT_GIF   0x4947
1997 #define BITMAP_FORMAT_PNG   0x5089
1998 #define BITMAP_FORMAT_APM   0xcdd7
1999
2000 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
2001     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2002 {
2003     GpStatus stat;
2004     GpBitmap *bitmap;
2005     IWICBitmapEncoder *encoder;
2006     IWICBitmapFrameEncode *frameencode;
2007     IPropertyBag2 *encoderoptions;
2008     HRESULT hr;
2009     UINT width, height;
2010     PixelFormat gdipformat=0;
2011     WICPixelFormatGUID wicformat;
2012     GpRect rc;
2013     BitmapData lockeddata;
2014     HRESULT initresult;
2015     UINT i;
2016
2017     if (image->type != ImageTypeBitmap)
2018         return GenericError;
2019
2020     bitmap = (GpBitmap*)image;
2021
2022     GdipGetImageWidth(image, &width);
2023     GdipGetImageHeight(image, &height);
2024
2025     rc.X = 0;
2026     rc.Y = 0;
2027     rc.Width = width;
2028     rc.Height = height;
2029
2030     initresult = CoInitialize(NULL);
2031
2032     hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2033         &IID_IWICBitmapEncoder, (void**)&encoder);
2034     if (FAILED(hr))
2035     {
2036         if (SUCCEEDED(initresult)) CoUninitialize();
2037         return hresult_to_status(hr);
2038     }
2039
2040     hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2041
2042     if (SUCCEEDED(hr))
2043     {
2044         hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
2045     }
2046
2047     if (SUCCEEDED(hr)) /* created frame */
2048     {
2049         hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
2050
2051         if (SUCCEEDED(hr))
2052             hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
2053
2054         if (SUCCEEDED(hr))
2055             /* FIXME: use the resolution from the image */
2056             hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
2057
2058         if (SUCCEEDED(hr))
2059         {
2060             for (i=0; wic_pixel_formats[i]; i++)
2061             {
2062                 if (wic_gdip_formats[i] == bitmap->format)
2063                     break;
2064             }
2065             if (wic_pixel_formats[i])
2066                 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
2067             else
2068                 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
2069
2070             hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
2071
2072             for (i=0; wic_pixel_formats[i]; i++)
2073             {
2074                 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
2075                     break;
2076             }
2077             if (wic_pixel_formats[i])
2078                 gdipformat = wic_gdip_formats[i];
2079             else
2080             {
2081                 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
2082                 hr = E_FAIL;
2083             }
2084         }
2085
2086         if (SUCCEEDED(hr))
2087         {
2088             stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
2089                 &lockeddata);
2090
2091             if (stat == Ok)
2092             {
2093                 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
2094                 BYTE *row;
2095
2096                 /* write one row at a time in case stride is negative */
2097                 row = lockeddata.Scan0;
2098                 for (i=0; i<lockeddata.Height; i++)
2099                 {
2100                     hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
2101                     if (FAILED(hr)) break;
2102                     row += lockeddata.Stride;
2103                 }
2104
2105                 GdipBitmapUnlockBits(bitmap, &lockeddata);
2106             }
2107             else
2108                 hr = E_FAIL;
2109         }
2110
2111         if (SUCCEEDED(hr))
2112             hr = IWICBitmapFrameEncode_Commit(frameencode);
2113
2114         IWICBitmapFrameEncode_Release(frameencode);
2115         IPropertyBag2_Release(encoderoptions);
2116     }
2117
2118     if (SUCCEEDED(hr))
2119         hr = IWICBitmapEncoder_Commit(encoder);
2120
2121     IWICBitmapEncoder_Release(encoder);
2122
2123     if (SUCCEEDED(initresult)) CoUninitialize();
2124
2125     return hresult_to_status(hr);
2126 }
2127
2128 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
2129     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2130 {
2131     return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
2132 }
2133
2134 static GpStatus encode_image_png(GpImage *image, IStream* stream,
2135     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2136 {
2137     return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
2138 }
2139
2140 /*****************************************************************************
2141  * GdipSaveImageToStream [GDIPLUS.@]
2142  */
2143 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
2144     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2145 {
2146     GpStatus stat;
2147     encode_image_func encode_image;
2148     int i;
2149
2150     TRACE("%p %p %p %p\n", image, stream, clsid, params);
2151
2152     if(!image || !stream)
2153         return InvalidParameter;
2154
2155     /* select correct encoder */
2156     encode_image = NULL;
2157     for (i = 0; i < NUM_CODECS; i++) {
2158         if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
2159             IsEqualCLSID(clsid, &codecs[i].info.Clsid))
2160             encode_image = codecs[i].encode_func;
2161     }
2162     if (encode_image == NULL)
2163         return UnknownImageFormat;
2164
2165     stat = encode_image(image, stream, clsid, params);
2166
2167     return stat;
2168 }
2169
2170 /*****************************************************************************
2171  * GdipGetImagePalette [GDIPLUS.@]
2172  */
2173 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
2174 {
2175     static int calls = 0;
2176
2177     if(!image)
2178         return InvalidParameter;
2179
2180     if(!(calls++))
2181         FIXME("not implemented\n");
2182
2183     return NotImplemented;
2184 }
2185
2186 /*****************************************************************************
2187  * GdipSetImagePalette [GDIPLUS.@]
2188  */
2189 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
2190     GDIPCONST ColorPalette *palette)
2191 {
2192     static int calls;
2193
2194     if(!image || !palette)
2195         return InvalidParameter;
2196
2197     if(!(calls++))
2198         FIXME("not implemented\n");
2199
2200     return NotImplemented;
2201 }
2202
2203 /*************************************************************************
2204  * Encoders -
2205  *   Structures that represent which formats we support for encoding.
2206  */
2207
2208 /* ImageCodecInfo creation routines taken from libgdiplus */
2209 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
2210 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
2211 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
2212 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
2213 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
2214 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
2215
2216 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
2217 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
2218 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
2219 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
2220 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
2221 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
2222
2223 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
2224 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
2225 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
2226 static const WCHAR gif_format[] = {'G','I','F',0};
2227 static const BYTE gif_sig_pattern[4] = "GIF8";
2228 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2229
2230 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
2231 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
2232 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
2233 static const WCHAR emf_format[] = {'E','M','F',0};
2234 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
2235 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2236
2237 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
2238 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
2239 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
2240 static const WCHAR wmf_format[] = {'W','M','F',0};
2241 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
2242 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
2243
2244 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
2245 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
2246 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
2247 static const WCHAR png_format[] = {'P','N','G',0};
2248 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
2249 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2250
2251 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
2252 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
2253 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
2254 static const WCHAR ico_format[] = {'I','C','O',0};
2255 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
2256 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2257
2258 static const struct image_codec codecs[NUM_CODECS] = {
2259     {
2260         { /* BMP */
2261             /* Clsid */              { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2262             /* FormatID */           { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2263             /* CodecName */          bmp_codecname,
2264             /* DllName */            NULL,
2265             /* FormatDescription */  bmp_format,
2266             /* FilenameExtension */  bmp_extension,
2267             /* MimeType */           bmp_mimetype,
2268             /* Flags */              ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2269             /* Version */            1,
2270             /* SigCount */           1,
2271             /* SigSize */            2,
2272             /* SigPattern */         bmp_sig_pattern,
2273             /* SigMask */            bmp_sig_mask,
2274         },
2275         encode_image_BMP,
2276         decode_image_bmp
2277     },
2278     {
2279         { /* JPEG */
2280             /* Clsid */              { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2281             /* FormatID */           { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2282             /* CodecName */          jpeg_codecname,
2283             /* DllName */            NULL,
2284             /* FormatDescription */  jpeg_format,
2285             /* FilenameExtension */  jpeg_extension,
2286             /* MimeType */           jpeg_mimetype,
2287             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2288             /* Version */            1,
2289             /* SigCount */           1,
2290             /* SigSize */            2,
2291             /* SigPattern */         jpeg_sig_pattern,
2292             /* SigMask */            jpeg_sig_mask,
2293         },
2294         NULL,
2295         decode_image_jpeg
2296     },
2297     {
2298         { /* GIF */
2299             /* Clsid */              { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2300             /* FormatID */           { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2301             /* CodecName */          gif_codecname,
2302             /* DllName */            NULL,
2303             /* FormatDescription */  gif_format,
2304             /* FilenameExtension */  gif_extension,
2305             /* MimeType */           gif_mimetype,
2306             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2307             /* Version */            1,
2308             /* SigCount */           1,
2309             /* SigSize */            4,
2310             /* SigPattern */         gif_sig_pattern,
2311             /* SigMask */            gif_sig_mask,
2312         },
2313         NULL,
2314         decode_image_gif
2315     },
2316     {
2317         { /* EMF */
2318             /* Clsid */              { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2319             /* FormatID */           { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2320             /* CodecName */          emf_codecname,
2321             /* DllName */            NULL,
2322             /* FormatDescription */  emf_format,
2323             /* FilenameExtension */  emf_extension,
2324             /* MimeType */           emf_mimetype,
2325             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2326             /* Version */            1,
2327             /* SigCount */           1,
2328             /* SigSize */            4,
2329             /* SigPattern */         emf_sig_pattern,
2330             /* SigMask */            emf_sig_mask,
2331         },
2332         NULL,
2333         decode_image_olepicture_metafile
2334     },
2335     {
2336         { /* WMF */
2337             /* Clsid */              { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2338             /* FormatID */           { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2339             /* CodecName */          wmf_codecname,
2340             /* DllName */            NULL,
2341             /* FormatDescription */  wmf_format,
2342             /* FilenameExtension */  wmf_extension,
2343             /* MimeType */           wmf_mimetype,
2344             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2345             /* Version */            1,
2346             /* SigCount */           1,
2347             /* SigSize */            2,
2348             /* SigPattern */         wmf_sig_pattern,
2349             /* SigMask */            wmf_sig_mask,
2350         },
2351         NULL,
2352         decode_image_olepicture_metafile
2353     },
2354     {
2355         { /* PNG */
2356             /* Clsid */              { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2357             /* FormatID */           { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2358             /* CodecName */          png_codecname,
2359             /* DllName */            NULL,
2360             /* FormatDescription */  png_format,
2361             /* FilenameExtension */  png_extension,
2362             /* MimeType */           png_mimetype,
2363             /* Flags */              ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2364             /* Version */            1,
2365             /* SigCount */           1,
2366             /* SigSize */            8,
2367             /* SigPattern */         png_sig_pattern,
2368             /* SigMask */            png_sig_mask,
2369         },
2370         encode_image_png,
2371         decode_image_png
2372     },
2373     {
2374         { /* ICO */
2375             /* Clsid */              { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2376             /* FormatID */           { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2377             /* CodecName */          ico_codecname,
2378             /* DllName */            NULL,
2379             /* FormatDescription */  ico_format,
2380             /* FilenameExtension */  ico_extension,
2381             /* MimeType */           ico_mimetype,
2382             /* Flags */              ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2383             /* Version */            1,
2384             /* SigCount */           1,
2385             /* SigSize */            4,
2386             /* SigPattern */         ico_sig_pattern,
2387             /* SigMask */            ico_sig_mask,
2388         },
2389         NULL,
2390         decode_image_icon
2391     },
2392 };
2393
2394 /*****************************************************************************
2395  * GdipGetImageDecodersSize [GDIPLUS.@]
2396  */
2397 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
2398 {
2399     int decoder_count=0;
2400     int i;
2401     TRACE("%p %p\n", numDecoders, size);
2402
2403     if (!numDecoders || !size)
2404         return InvalidParameter;
2405
2406     for (i=0; i<NUM_CODECS; i++)
2407     {
2408         if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2409             decoder_count++;
2410     }
2411
2412     *numDecoders = decoder_count;
2413     *size = decoder_count * sizeof(ImageCodecInfo);
2414
2415     return Ok;
2416 }
2417
2418 /*****************************************************************************
2419  * GdipGetImageDecoders [GDIPLUS.@]
2420  */
2421 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
2422 {
2423     int i, decoder_count=0;
2424     TRACE("%u %u %p\n", numDecoders, size, decoders);
2425
2426     if (!decoders ||
2427         size != numDecoders * sizeof(ImageCodecInfo))
2428         return GenericError;
2429
2430     for (i=0; i<NUM_CODECS; i++)
2431     {
2432         if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2433         {
2434             if (decoder_count == numDecoders) return GenericError;
2435             memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2436             decoder_count++;
2437         }
2438     }
2439
2440     if (decoder_count < numDecoders) return GenericError;
2441
2442     return Ok;
2443 }
2444
2445 /*****************************************************************************
2446  * GdipGetImageEncodersSize [GDIPLUS.@]
2447  */
2448 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
2449 {
2450     int encoder_count=0;
2451     int i;
2452     TRACE("%p %p\n", numEncoders, size);
2453
2454     if (!numEncoders || !size)
2455         return InvalidParameter;
2456
2457     for (i=0; i<NUM_CODECS; i++)
2458     {
2459         if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2460             encoder_count++;
2461     }
2462
2463     *numEncoders = encoder_count;
2464     *size = encoder_count * sizeof(ImageCodecInfo);
2465
2466     return Ok;
2467 }
2468
2469 /*****************************************************************************
2470  * GdipGetImageEncoders [GDIPLUS.@]
2471  */
2472 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
2473 {
2474     int i, encoder_count=0;
2475     TRACE("%u %u %p\n", numEncoders, size, encoders);
2476
2477     if (!encoders ||
2478         size != numEncoders * sizeof(ImageCodecInfo))
2479         return GenericError;
2480
2481     for (i=0; i<NUM_CODECS; i++)
2482     {
2483         if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2484         {
2485             if (encoder_count == numEncoders) return GenericError;
2486             memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2487             encoder_count++;
2488         }
2489     }
2490
2491     if (encoder_count < numEncoders) return GenericError;
2492
2493     return Ok;
2494 }
2495
2496 /*****************************************************************************
2497  * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
2498  */
2499 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
2500 {
2501     BITMAP bm;
2502     GpStatus retval;
2503     PixelFormat format;
2504     BYTE* bits;
2505
2506     TRACE("%p %p %p\n", hbm, hpal, bitmap);
2507
2508     if(!hbm || !bitmap)
2509         return InvalidParameter;
2510
2511     /* TODO: Support for device-dependent bitmaps */
2512     if(hpal){
2513         FIXME("no support for device-dependent bitmaps\n");
2514         return NotImplemented;
2515     }
2516
2517     if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
2518             return InvalidParameter;
2519
2520     /* TODO: Figure out the correct format for 16, 32, 64 bpp */
2521     switch(bm.bmBitsPixel) {
2522         case 1:
2523             format = PixelFormat1bppIndexed;
2524             break;
2525         case 4:
2526             format = PixelFormat4bppIndexed;
2527             break;
2528         case 8:
2529             format = PixelFormat8bppIndexed;
2530             break;
2531         case 24:
2532             format = PixelFormat24bppRGB;
2533             break;
2534         case 32:
2535             format = PixelFormat32bppRGB;
2536             break;
2537         case 48:
2538             format = PixelFormat48bppRGB;
2539             break;
2540         default:
2541             FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
2542             return InvalidParameter;
2543     }
2544
2545     if (bm.bmBits)
2546         bits = (BYTE*)bm.bmBits + (bm.bmHeight - 1) * bm.bmWidthBytes;
2547     else
2548     {
2549         FIXME("can only get image data from DIB sections\n");
2550         bits = NULL;
2551     }
2552
2553     retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, -bm.bmWidthBytes,
2554         format, bits, bitmap);
2555
2556     return retval;
2557 }
2558
2559 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
2560 {
2561     FIXME("(%p): stub\n", effect);
2562     /* note: According to Jose Roca's GDI+ Docs, this is not implemented
2563      * in Windows's gdiplus */
2564     return NotImplemented;
2565 }
2566
2567 /*****************************************************************************
2568  * GdipSetEffectParameters [GDIPLUS.@]
2569  */
2570 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
2571     const VOID *params, const UINT size)
2572 {
2573     static int calls;
2574
2575     if(!(calls++))
2576         FIXME("not implemented\n");
2577
2578     return NotImplemented;
2579 }
2580
2581 /*****************************************************************************
2582  * GdipGetImageFlags [GDIPLUS.@]
2583  */
2584 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
2585 {
2586     TRACE("%p %p\n", image, flags);
2587
2588     if(!image || !flags)
2589         return InvalidParameter;
2590
2591     *flags = image->flags;
2592
2593     return Ok;
2594 }
2595
2596 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
2597 {
2598     TRACE("(%d, %p)\n", control, param);
2599
2600     switch(control){
2601         case TestControlForceBilinear:
2602             if(param)
2603                 FIXME("TestControlForceBilinear not handled\n");
2604             break;
2605         case TestControlNoICM:
2606             if(param)
2607                 FIXME("TestControlNoICM not handled\n");
2608             break;
2609         case TestControlGetBuildNumber:
2610             *((DWORD*)param) = 3102;
2611             break;
2612     }
2613
2614     return Ok;
2615 }
2616
2617 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
2618                             HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
2619                             MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
2620                             GpMetafile **metafile)
2621 {
2622     FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2623                                  frameUnit, debugstr_w(desc), metafile);
2624
2625     return NotImplemented;
2626 }
2627
2628 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
2629                             GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
2630                             GDIPCONST WCHAR *desc, GpMetafile **metafile)
2631 {
2632     FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2633                                  frameUnit, debugstr_w(desc), metafile);
2634
2635     return NotImplemented;
2636 }
2637
2638 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
2639 {
2640     FIXME("%p\n", image);
2641
2642     return Ok;
2643 }
2644
2645 /*****************************************************************************
2646  * GdipGetImageThumbnail [GDIPLUS.@]
2647  */
2648 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
2649                             GpImage **ret_image, GetThumbnailImageAbort cb,
2650                             VOID * cb_data)
2651 {
2652     FIXME("(%p %u %u %p %p %p) stub\n",
2653         image, width, height, ret_image, cb, cb_data);
2654     return NotImplemented;
2655 }
2656
2657 /*****************************************************************************
2658  * GdipImageRotateFlip [GDIPLUS.@]
2659  */
2660 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
2661 {
2662     FIXME("(%p %u) stub\n", image, type);
2663     return NotImplemented;
2664 }