user32/tests: Fix the listbox delete test on NT4.
[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 "gdiplus.h"
35 #include "gdiplus_private.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
39
40 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
41
42 static INT ipicture_pixel_height(IPicture *pic)
43 {
44     HDC hdcref;
45     OLE_YSIZE_HIMETRIC y;
46
47     IPicture_get_Height(pic, &y);
48
49     hdcref = GetDC(0);
50
51     y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
52     ReleaseDC(0, hdcref);
53
54     return y;
55 }
56
57 static INT ipicture_pixel_width(IPicture *pic)
58 {
59     HDC hdcref;
60     OLE_XSIZE_HIMETRIC x;
61
62     IPicture_get_Width(pic, &x);
63
64     hdcref = GetDC(0);
65
66     x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
67
68     ReleaseDC(0, hdcref);
69
70     return x;
71 }
72
73 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
74     ARGB *color)
75 {
76     static int calls;
77     TRACE("%p %d %d %p\n", bitmap, x, y, color);
78
79     if(!bitmap || !color)
80         return InvalidParameter;
81
82     if(!(calls++))
83         FIXME("not implemented\n");
84
85     *color = 0xdeadbeef;
86
87     return NotImplemented;
88 }
89
90 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
91     ARGB color)
92 {
93     static int calls;
94     TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
95
96     if(!bitmap)
97         return InvalidParameter;
98
99     if(!(calls++))
100         FIXME("not implemented\n");
101
102     return NotImplemented;
103 }
104
105 /* This function returns a pointer to an array of pixels that represents the
106  * bitmap. The *entire* bitmap is locked according to the lock mode specified by
107  * flags.  It is correct behavior that a user who calls this function with write
108  * privileges can write to the whole bitmap (not just the area in rect).
109  *
110  * FIXME: only used portion of format is bits per pixel. */
111 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
112     UINT flags, PixelFormat format, BitmapData* lockeddata)
113 {
114     BOOL bm_is_selected;
115     INT stride, bitspp = PIXELFORMATBPP(format);
116     HDC hdc;
117     HBITMAP hbm, old = NULL;
118     BITMAPINFO *pbmi;
119     BYTE *buff = NULL;
120     UINT abs_height;
121     GpRect act_rect; /* actual rect to be used */
122
123     TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
124
125     if(!lockeddata || !bitmap)
126         return InvalidParameter;
127
128     if(rect){
129         if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
130           (rect->Y + rect->Height > bitmap->height) || !flags)
131             return InvalidParameter;
132
133         act_rect = *rect;
134     }
135     else{
136         act_rect.X = act_rect.Y = 0;
137         act_rect.Width  = bitmap->width;
138         act_rect.Height = bitmap->height;
139     }
140
141     if(flags & ImageLockModeUserInputBuf)
142         return NotImplemented;
143
144     if(bitmap->lockmode)
145         return WrongState;
146
147     IPicture_get_Handle(bitmap->image.picture, (OLE_HANDLE*)&hbm);
148     IPicture_get_CurDC(bitmap->image.picture, &hdc);
149     bm_is_selected = (hdc != 0);
150
151     pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
152     if (!pbmi)
153         return OutOfMemory;
154     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
155     pbmi->bmiHeader.biBitCount = 0;
156
157     if(!bm_is_selected){
158         hdc = CreateCompatibleDC(0);
159         old = SelectObject(hdc, hbm);
160     }
161
162     /* fill out bmi */
163     GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
164
165     abs_height = abs(pbmi->bmiHeader.biHeight);
166     stride = pbmi->bmiHeader.biWidth * bitspp / 8;
167     stride = (stride + 3) & ~3;
168
169     buff = GdipAlloc(stride * abs_height);
170
171     pbmi->bmiHeader.biBitCount = bitspp;
172
173     if(buff)
174         GetDIBits(hdc, hbm, 0, abs_height, buff, pbmi, DIB_RGB_COLORS);
175
176     if(!bm_is_selected){
177         SelectObject(hdc, old);
178         DeleteDC(hdc);
179     }
180
181     if(!buff){
182         GdipFree(pbmi);
183         return OutOfMemory;
184     }
185
186     lockeddata->Width  = act_rect.Width;
187     lockeddata->Height = act_rect.Height;
188     lockeddata->PixelFormat = format;
189     lockeddata->Reserved = flags;
190
191     if(pbmi->bmiHeader.biHeight > 0){
192         lockeddata->Stride = -stride;
193         lockeddata->Scan0  = buff + (bitspp / 8) * act_rect.X +
194                              stride * (abs_height - 1 - act_rect.Y);
195     }
196     else{
197         lockeddata->Stride = stride;
198         lockeddata->Scan0  = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
199     }
200
201     bitmap->lockmode = flags;
202     bitmap->numlocks++;
203
204     bitmap->bitmapbits = buff;
205
206     GdipFree(pbmi);
207     return Ok;
208 }
209
210 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
211 {
212     FIXME("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
213
214     return NotImplemented;
215 }
216
217 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
218     BitmapData* lockeddata)
219 {
220     HDC hdc;
221     HBITMAP hbm, old = NULL;
222     BOOL bm_is_selected;
223     BITMAPINFO *pbmi;
224
225     if(!bitmap || !lockeddata)
226         return InvalidParameter;
227
228     if(!bitmap->lockmode)
229         return WrongState;
230
231     if(lockeddata->Reserved & ImageLockModeUserInputBuf)
232         return NotImplemented;
233
234     if(lockeddata->Reserved & ImageLockModeRead){
235         if(!(--bitmap->numlocks))
236             bitmap->lockmode = 0;
237
238         GdipFree(bitmap->bitmapbits);
239         bitmap->bitmapbits = NULL;
240         return Ok;
241     }
242
243     IPicture_get_Handle(bitmap->image.picture, (OLE_HANDLE*)&hbm);
244     IPicture_get_CurDC(bitmap->image.picture, &hdc);
245     bm_is_selected = (hdc != 0);
246
247     pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
248     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
249     pbmi->bmiHeader.biBitCount = 0;
250
251     if(!bm_is_selected){
252         hdc = CreateCompatibleDC(0);
253         old = SelectObject(hdc, hbm);
254     }
255
256     GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
257     pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
258     SetDIBits(hdc, hbm, 0, abs(pbmi->bmiHeader.biHeight),
259               bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
260
261     if(!bm_is_selected){
262         SelectObject(hdc, old);
263         DeleteDC(hdc);
264     }
265
266     GdipFree(pbmi);
267     GdipFree(bitmap->bitmapbits);
268     bitmap->bitmapbits = NULL;
269     bitmap->lockmode = 0;
270
271     return Ok;
272 }
273
274 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
275     PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
276 {
277     FIXME("(%f,%f,%f,%f,%i,%p,%p): stub\n", x, y, width, height, format, srcBitmap, dstBitmap);
278
279     return NotImplemented;
280 }
281
282 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
283     PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
284 {
285     FIXME("(%i,%i,%i,%i,%i,%p,%p): stub\n", x, y, width, height, format, srcBitmap, dstBitmap);
286
287     return NotImplemented;
288 }
289
290 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
291 {
292     IStream* stream;
293     HRESULT hr;
294     INT size;
295     LARGE_INTEGER move;
296     GpStatus stat = GenericError;
297
298     TRACE("%p, %p\n", image, cloneImage);
299
300     if (!image || !cloneImage)
301         return InvalidParameter;
302
303     hr = CreateStreamOnHGlobal(0, TRUE, &stream);
304     if (FAILED(hr))
305         return GenericError;
306
307     hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
308     if(FAILED(hr))
309     {
310         WARN("Failed to save image on stream\n");
311         goto out;
312     }
313
314     /* Set seek pointer back to the beginning of the picture */
315     move.QuadPart = 0;
316     hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
317     if (FAILED(hr))
318         goto out;
319
320     stat = GdipLoadImageFromStream(stream, cloneImage);
321     if (stat != Ok) WARN("Failed to load image from stream\n");
322
323 out:
324     IStream_Release(stream);
325     return stat;
326 }
327
328 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
329     GpBitmap **bitmap)
330 {
331     GpStatus stat;
332     IStream *stream;
333
334     TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
335
336     if(!filename || !bitmap)
337         return InvalidParameter;
338
339     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
340
341     if(stat != Ok)
342         return stat;
343
344     stat = GdipCreateBitmapFromStream(stream, bitmap);
345
346     IStream_Release(stream);
347
348     return stat;
349 }
350
351 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
352                                                VOID *bits, GpBitmap **bitmap)
353 {
354     DWORD height, stride;
355     PixelFormat format;
356
357     FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
358
359     height = abs(info->bmiHeader.biHeight);
360     stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
361
362     if(info->bmiHeader.biHeight > 0) /* bottom-up */
363     {
364         bits = (BYTE*)bits + (height - 1) * stride;
365         stride = -stride;
366     }
367
368     switch(info->bmiHeader.biBitCount) {
369     case 1:
370         format = PixelFormat1bppIndexed;
371         break;
372     case 4:
373         format = PixelFormat4bppIndexed;
374         break;
375     case 8:
376         format = PixelFormat8bppIndexed;
377         break;
378     case 24:
379         format = PixelFormat24bppRGB;
380         break;
381     default:
382         FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
383         *bitmap = NULL;
384         return InvalidParameter;
385     }
386
387     return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
388                                      bits, bitmap);
389
390 }
391
392 /* FIXME: no icm */
393 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
394     GpBitmap **bitmap)
395 {
396     TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
397
398     return GdipCreateBitmapFromFile(filename, bitmap);
399 }
400
401 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
402     GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
403 {
404     HBITMAP hbm;
405     GpStatus stat = InvalidParameter;
406
407     TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
408
409     if(!lpBitmapName || !bitmap)
410         return InvalidParameter;
411
412     /* load DIB */
413     hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
414                      LR_CREATEDIBSECTION);
415
416     if(hbm){
417         stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
418         DeleteObject(hbm);
419     }
420
421     return stat;
422 }
423
424 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
425     HBITMAP* hbmReturn, ARGB background)
426 {
427     FIXME("stub\n");
428
429     hbmReturn = NULL;
430
431     return NotImplemented;
432 }
433
434 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
435     GpMetafile* metafile, BOOL* succ, EmfType emfType,
436     const WCHAR* description, GpMetafile** out_metafile)
437 {
438     static int calls;
439
440     if(!ref || !metafile || !out_metafile)
441         return InvalidParameter;
442
443     *succ = FALSE;
444     *out_metafile = NULL;
445
446     if(!(calls++))
447         FIXME("not implemented\n");
448
449     return NotImplemented;
450 }
451
452 /* FIXME: this should create a bitmap in the given size with the attributes
453  * (resolution etc.) of the graphics object */
454 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
455     GpGraphics* target, GpBitmap** bitmap)
456 {
457     static int calls;
458     GpStatus ret;
459
460     if(!target || !bitmap)
461         return InvalidParameter;
462
463     if(!(calls++))
464         FIXME("hacked stub\n");
465
466     ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
467                                     NULL, bitmap);
468
469     return ret;
470 }
471
472 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
473 {
474     HICON icon_copy;
475     ICONINFO iinfo;
476     PICTDESC desc;
477
478     TRACE("%p, %p\n", hicon, bitmap);
479
480     if(!bitmap || !GetIconInfo(hicon, &iinfo))
481         return InvalidParameter;
482
483     *bitmap = GdipAlloc(sizeof(GpBitmap));
484     if(!*bitmap)    return OutOfMemory;
485
486     icon_copy = CreateIconIndirect(&iinfo);
487
488     if(!icon_copy){
489         GdipFree(*bitmap);
490         return InvalidParameter;
491     }
492
493     desc.cbSizeofstruct = sizeof(PICTDESC);
494     desc.picType = PICTYPE_ICON;
495     desc.u.icon.hicon = icon_copy;
496
497     if(OleCreatePictureIndirect(&desc, &IID_IPicture, TRUE,
498                                 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
499         DestroyIcon(icon_copy);
500         GdipFree(*bitmap);
501         return GenericError;
502     }
503
504     (*bitmap)->format = PixelFormat32bppARGB;
505     (*bitmap)->image.type  = ImageTypeBitmap;
506     (*bitmap)->image.flags = ImageFlagsNone;
507     (*bitmap)->width  = ipicture_pixel_width((*bitmap)->image.picture);
508     (*bitmap)->height = ipicture_pixel_height((*bitmap)->image.picture);
509
510     DeleteObject(iinfo.hbmColor);
511     DeleteObject(iinfo.hbmMask);
512
513     return Ok;
514 }
515
516 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
517     PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
518 {
519     BITMAPFILEHEADER *bmfh;
520     BITMAPINFOHEADER *bmih;
521     BYTE *buff;
522     INT datalen, size;
523     IStream *stream;
524
525     TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
526
527     if (!bitmap) return InvalidParameter;
528
529     if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
530         *bitmap = NULL;
531         return InvalidParameter;
532     }
533
534     if(scan0 && !stride)
535         return InvalidParameter;
536
537     *bitmap = GdipAlloc(sizeof(GpBitmap));
538     if(!*bitmap)    return OutOfMemory;
539
540     if(stride == 0){
541         stride = width * (PIXELFORMATBPP(format) / 8);
542         stride = (stride + 3) & ~3;
543     }
544
545     datalen = abs(stride * height);
546     size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
547     buff = GdipAlloc(size);
548     if(!buff){
549         GdipFree(*bitmap);
550         return OutOfMemory;
551     }
552
553     bmfh = (BITMAPFILEHEADER*) buff;
554     bmih = (BITMAPINFOHEADER*) (bmfh + 1);
555
556     bmfh->bfType    = (((WORD)'M') << 8) + (WORD)'B';
557     bmfh->bfSize    = size;
558     bmfh->bfOffBits = size - datalen;
559
560     bmih->biSize            = sizeof(BITMAPINFOHEADER);
561     bmih->biWidth           = width;
562     /* FIXME: use the rest of the data from format */
563     bmih->biBitCount        = PIXELFORMATBPP(format);
564     bmih->biCompression     = BI_RGB;
565     bmih->biSizeImage       = datalen;
566
567     if (scan0)
568     {
569         if (stride > 0)
570         {
571             bmih->biHeight = -height;
572             memcpy(bmih + 1, scan0, datalen);
573         }
574         else
575         {
576             bmih->biHeight = height;
577             memcpy(bmih + 1, scan0 + stride * (height - 1), datalen);
578         }
579     }
580     else
581     {
582         bmih->biHeight = height;
583         memset(bmih + 1, 0, datalen);
584     }
585
586     if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
587         ERR("could not make stream\n");
588         GdipFree(*bitmap);
589         GdipFree(buff);
590         *bitmap = NULL;
591         return GenericError;
592     }
593
594     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
595         (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
596         TRACE("Could not load picture\n");
597         IStream_Release(stream);
598         GdipFree(*bitmap);
599         GdipFree(buff);
600         *bitmap = NULL;
601         return GenericError;
602     }
603
604     (*bitmap)->image.type = ImageTypeBitmap;
605     (*bitmap)->image.flags = ImageFlagsNone;
606     (*bitmap)->width = width;
607     (*bitmap)->height = height;
608     (*bitmap)->format = format;
609
610     return Ok;
611 }
612
613 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
614     GpBitmap **bitmap)
615 {
616     GpStatus stat;
617
618     TRACE("%p %p\n", stream, bitmap);
619
620     stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
621
622     if(stat != Ok)
623         return stat;
624
625     if((*bitmap)->image.type != ImageTypeBitmap){
626         IPicture_Release((*bitmap)->image.picture);
627         GdipFree(bitmap);
628         return GenericError; /* FIXME: what error to return? */
629     }
630
631     return Ok;
632 }
633
634 /* FIXME: no icm */
635 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
636     GpBitmap **bitmap)
637 {
638     TRACE("%p %p\n", stream, bitmap);
639
640     return GdipCreateBitmapFromStream(stream, bitmap);
641 }
642
643 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
644     GpCachedBitmap **cachedbmp)
645 {
646     GpStatus stat;
647
648     TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
649
650     if(!bitmap || !graphics || !cachedbmp)
651         return InvalidParameter;
652
653     *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
654     if(!*cachedbmp)
655         return OutOfMemory;
656
657     stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
658     if(stat != Ok){
659         GdipFree(*cachedbmp);
660         return stat;
661     }
662
663     return Ok;
664 }
665
666 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
667 {
668     FIXME("(%p, %p)\n", bitmap, hicon);
669
670     return NotImplemented;
671 }
672
673 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
674 {
675     TRACE("%p\n", cachedbmp);
676
677     if(!cachedbmp)
678         return InvalidParameter;
679
680     GdipDisposeImage(cachedbmp->image);
681     GdipFree(cachedbmp);
682
683     return Ok;
684 }
685
686 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
687     GpCachedBitmap *cachedbmp, INT x, INT y)
688 {
689     TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
690
691     if(!graphics || !cachedbmp)
692         return InvalidParameter;
693
694     return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
695 }
696
697 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
698 {
699     HDC hdc;
700
701     TRACE("%p\n", image);
702
703     if(!image)
704         return InvalidParameter;
705
706     IPicture_get_CurDC(image->picture, &hdc);
707     DeleteDC(hdc);
708     IPicture_Release(image->picture);
709     if (image->type == ImageTypeBitmap)
710         GdipFree(((GpBitmap*)image)->bitmapbits);
711     GdipFree(image);
712
713     return Ok;
714 }
715
716 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
717 {
718     if(!image || !item)
719         return InvalidParameter;
720
721     return NotImplemented;
722 }
723
724 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
725     GpUnit *srcUnit)
726 {
727     TRACE("%p %p %p\n", image, srcRect, srcUnit);
728
729     if(!image || !srcRect || !srcUnit)
730         return InvalidParameter;
731     if(image->type == ImageTypeMetafile){
732         *srcRect = ((GpMetafile*)image)->bounds;
733         *srcUnit = ((GpMetafile*)image)->unit;
734     }
735     else if(image->type == ImageTypeBitmap){
736         srcRect->X = srcRect->Y = 0.0;
737         srcRect->Width = (REAL) ((GpBitmap*)image)->width;
738         srcRect->Height = (REAL) ((GpBitmap*)image)->height;
739         *srcUnit = UnitPixel;
740     }
741     else{
742         srcRect->X = srcRect->Y = 0.0;
743         srcRect->Width = ipicture_pixel_width(image->picture);
744         srcRect->Height = ipicture_pixel_height(image->picture);
745         *srcUnit = UnitPixel;
746     }
747
748     TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
749           srcRect->Width, srcRect->Height, *srcUnit);
750
751     return Ok;
752 }
753
754 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
755     REAL *height)
756 {
757     TRACE("%p %p %p\n", image, width, height);
758
759     if(!image || !height || !width)
760         return InvalidParameter;
761
762     if(image->type == ImageTypeMetafile){
763         HDC hdc = GetDC(0);
764
765         *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
766                         ((GpMetafile*)image)->bounds.Height;
767
768         *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
769                         ((GpMetafile*)image)->bounds.Width;
770
771         ReleaseDC(0, hdc);
772     }
773
774     else if(image->type == ImageTypeBitmap){
775         *height = ((GpBitmap*)image)->height;
776         *width = ((GpBitmap*)image)->width;
777     }
778     else{
779         *height = ipicture_pixel_height(image->picture);
780         *width = ipicture_pixel_width(image->picture);
781     }
782
783     TRACE("returning (%f, %f)\n", *height, *width);
784     return Ok;
785 }
786
787 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
788     GpGraphics **graphics)
789 {
790     HDC hdc;
791
792     TRACE("%p %p\n", image, graphics);
793
794     if(!image || !graphics)
795         return InvalidParameter;
796
797     if(image->type != ImageTypeBitmap){
798         FIXME("not implemented for image type %d\n", image->type);
799         return NotImplemented;
800     }
801
802     IPicture_get_CurDC(image->picture, &hdc);
803
804     if(!hdc){
805         hdc = CreateCompatibleDC(0);
806         IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
807     }
808
809     return GdipCreateFromHDC(hdc, graphics);
810 }
811
812 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
813 {
814     TRACE("%p %p\n", image, height);
815
816     if(!image || !height)
817         return InvalidParameter;
818
819     if(image->type == ImageTypeMetafile){
820         HDC hdc = GetDC(0);
821
822         *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
823                         ((GpMetafile*)image)->bounds.Height);
824
825         ReleaseDC(0, hdc);
826     }
827     else if(image->type == ImageTypeBitmap)
828         *height = ((GpBitmap*)image)->height;
829     else
830         *height = ipicture_pixel_height(image->picture);
831
832     TRACE("returning %d\n", *height);
833
834     return Ok;
835 }
836
837 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
838 {
839     static int calls;
840
841     if(!image || !res)
842         return InvalidParameter;
843
844     if(!(calls++))
845         FIXME("not implemented\n");
846
847     return NotImplemented;
848 }
849
850 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
851 {
852     FIXME("%p %p\n", image, size);
853
854     if(!image || !size)
855         return InvalidParameter;
856
857     return NotImplemented;
858 }
859
860 /* FIXME: test this function for non-bitmap types */
861 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
862 {
863     TRACE("%p %p\n", image, format);
864
865     if(!image || !format)
866         return InvalidParameter;
867
868     if(image->type != ImageTypeBitmap)
869         *format = PixelFormat24bppRGB;
870     else
871         *format = ((GpBitmap*) image)->format;
872
873     return Ok;
874 }
875
876 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
877 {
878     static int calls;
879
880     if(!image || !format)
881         return InvalidParameter;
882
883     if(!(calls++))
884         FIXME("stub\n");
885
886     /* FIXME: should be detected from embedded picture or stored separately */
887     switch (image->type)
888     {
889     case ImageTypeBitmap:   *format = ImageFormatBMP; break;
890     case ImageTypeMetafile: *format = ImageFormatEMF; break;
891     default:
892         WARN("unknown type %u\n", image->type);
893         *format = ImageFormatUndefined;
894     }
895     return Ok;
896 }
897
898 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
899 {
900     TRACE("%p %p\n", image, type);
901
902     if(!image || !type)
903         return InvalidParameter;
904
905     *type = image->type;
906
907     return Ok;
908 }
909
910 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
911 {
912     static int calls;
913
914     if(!image || !res)
915         return InvalidParameter;
916
917     if(!(calls++))
918         FIXME("not implemented\n");
919
920     return NotImplemented;
921 }
922
923 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
924 {
925     TRACE("%p %p\n", image, width);
926
927     if(!image || !width)
928         return InvalidParameter;
929
930     if(image->type == ImageTypeMetafile){
931         HDC hdc = GetDC(0);
932
933         *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
934                         ((GpMetafile*)image)->bounds.Width);
935
936         ReleaseDC(0, hdc);
937     }
938     else if(image->type == ImageTypeBitmap)
939         *width = ((GpBitmap*)image)->width;
940     else
941         *width = ipicture_pixel_width(image->picture);
942
943     TRACE("returning %d\n", *width);
944
945     return Ok;
946 }
947
948 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
949     MetafileHeader * header)
950 {
951     static int calls;
952
953     if(!metafile || !header)
954         return InvalidParameter;
955
956     if(!(calls++))
957         FIXME("not implemented\n");
958
959     return Ok;
960 }
961
962 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
963     UINT num, PropertyItem* items)
964 {
965     static int calls;
966
967     if(!(calls++))
968         FIXME("not implemented\n");
969
970     return InvalidParameter;
971 }
972
973 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
974 {
975     static int calls;
976
977     if(!(calls++))
978         FIXME("not implemented\n");
979
980     return InvalidParameter;
981 }
982
983 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
984 {
985     static int calls;
986
987     if(!(calls++))
988         FIXME("not implemented\n");
989
990     return InvalidParameter;
991 }
992
993 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
994     PropertyItem* buffer)
995 {
996     static int calls;
997
998     if(!(calls++))
999         FIXME("not implemented\n");
1000
1001     return InvalidParameter;
1002 }
1003
1004 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
1005     UINT* size)
1006 {
1007     static int calls;
1008
1009     TRACE("%p %x %p\n", image, pid, size);
1010
1011     if(!size || !image)
1012         return InvalidParameter;
1013
1014     if(!(calls++))
1015         FIXME("not implemented\n");
1016
1017     return NotImplemented;
1018 }
1019
1020 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
1021 {
1022     static int calls;
1023
1024     if(!(calls++))
1025         FIXME("not implemented\n");
1026
1027     return InvalidParameter;
1028 }
1029
1030 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
1031     GDIPCONST GUID* dimensionID, UINT* count)
1032 {
1033     static int calls;
1034
1035     if(!image || !dimensionID || !count)
1036         return InvalidParameter;
1037
1038     if(!(calls++))
1039         FIXME("not implemented\n");
1040
1041     return NotImplemented;
1042 }
1043
1044 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
1045     UINT* count)
1046 {
1047     if(!image || !count)
1048         return InvalidParameter;
1049
1050     *count = 1;
1051
1052     FIXME("stub\n");
1053
1054     return Ok;
1055 }
1056
1057 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
1058     GUID* dimensionIDs, UINT count)
1059 {
1060     static int calls;
1061
1062     if(!image || !dimensionIDs)
1063         return InvalidParameter;
1064
1065     if(!(calls++))
1066         FIXME("not implemented\n");
1067
1068     return Ok;
1069 }
1070
1071 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
1072     GDIPCONST GUID* dimensionID, UINT frameidx)
1073 {
1074     static int calls;
1075
1076     if(!image || !dimensionID)
1077         return InvalidParameter;
1078
1079     if(!(calls++))
1080         FIXME("not implemented\n");
1081
1082     return Ok;
1083 }
1084
1085 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
1086                                           GpImage **image)
1087 {
1088     GpStatus stat;
1089     IStream *stream;
1090
1091     TRACE("(%s) %p\n", debugstr_w(filename), image);
1092
1093     if (!filename || !image)
1094         return InvalidParameter;
1095
1096     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1097
1098     if (stat != Ok)
1099         return stat;
1100
1101     stat = GdipLoadImageFromStream(stream, image);
1102
1103     IStream_Release(stream);
1104
1105     return stat;
1106 }
1107
1108 /* FIXME: no icm handling */
1109 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
1110 {
1111     TRACE("(%s) %p\n", debugstr_w(filename), image);
1112
1113     return GdipLoadImageFromFile(filename, image);
1114 }
1115
1116 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
1117 {
1118     IPicture *pic;
1119     short type;
1120
1121     TRACE("%p %p\n", stream, image);
1122
1123     if(!stream || !image)
1124         return InvalidParameter;
1125
1126     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1127         (LPVOID*) &pic) != S_OK){
1128         TRACE("Could not load picture\n");
1129         return GenericError;
1130     }
1131
1132     IPicture_get_Type(pic, &type);
1133
1134     if(type == PICTYPE_BITMAP){
1135         BITMAPINFO *pbmi;
1136         BITMAPCOREHEADER* bmch;
1137         HBITMAP hbm;
1138         HDC hdc;
1139
1140         pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1141         if (!pbmi)
1142             return OutOfMemory;
1143         *image = GdipAlloc(sizeof(GpBitmap));
1144         if(!*image){
1145             GdipFree(pbmi);
1146             return OutOfMemory;
1147         }
1148         (*image)->type = ImageTypeBitmap;
1149
1150         (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
1151         (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
1152
1153         /* get the pixel format */
1154         IPicture_get_Handle(pic, (OLE_HANDLE*)&hbm);
1155         IPicture_get_CurDC(pic, &hdc);
1156
1157         bmch = (BITMAPCOREHEADER*) (&pbmi->bmiHeader);
1158         bmch->bcSize = sizeof(BITMAPCOREHEADER);
1159
1160         if(!hdc){
1161             HBITMAP old;
1162             hdc = CreateCompatibleDC(0);
1163             old = SelectObject(hdc, hbm);
1164             GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1165             SelectObject(hdc, old);
1166             DeleteDC(hdc);
1167         }
1168         else
1169             GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1170
1171         switch(bmch->bcBitCount)
1172         {
1173             case 1:
1174                 (*((GpBitmap**) image))->format = PixelFormat1bppIndexed;
1175                 break;
1176             case 4:
1177                 (*((GpBitmap**) image))->format = PixelFormat4bppIndexed;
1178                 break;
1179             case 8:
1180                 (*((GpBitmap**) image))->format = PixelFormat8bppIndexed;
1181                 break;
1182             case 16:
1183                 (*((GpBitmap**) image))->format = PixelFormat16bppRGB565;
1184                 break;
1185             case 24:
1186                 (*((GpBitmap**) image))->format = PixelFormat24bppRGB;
1187                 break;
1188             case 32:
1189                 (*((GpBitmap**) image))->format = PixelFormat32bppRGB;
1190                 break;
1191             case 48:
1192                 (*((GpBitmap**) image))->format = PixelFormat48bppRGB;
1193                 break;
1194             default:
1195                 FIXME("Bit depth %d is not fully supported yet\n", bmch->bcBitCount);
1196                 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
1197                 break;
1198         }
1199
1200         GdipFree(pbmi);
1201     }
1202     else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
1203         /* FIXME: missing initialization code */
1204         *image = GdipAlloc(sizeof(GpMetafile));
1205         if(!*image) return OutOfMemory;
1206         (*image)->type = ImageTypeMetafile;
1207     }
1208     else{
1209         *image = GdipAlloc(sizeof(GpImage));
1210         if(!*image) return OutOfMemory;
1211         (*image)->type = ImageTypeUnknown;
1212     }
1213
1214     (*image)->picture = pic;
1215     (*image)->flags   = ImageFlagsNone;
1216
1217     return Ok;
1218 }
1219
1220 /* FIXME: no ICM */
1221 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1222 {
1223     TRACE("%p %p\n", stream, image);
1224
1225     return GdipLoadImageFromStream(stream, image);
1226 }
1227
1228 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1229 {
1230     static int calls;
1231
1232     if(!image)
1233         return InvalidParameter;
1234
1235     if(!(calls++))
1236         FIXME("not implemented\n");
1237
1238     return NotImplemented;
1239 }
1240
1241 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1242 {
1243     static int calls;
1244
1245     if(!(calls++))
1246         FIXME("not implemented\n");
1247
1248     return NotImplemented;
1249 }
1250
1251 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1252                                         GDIPCONST CLSID *clsidEncoder,
1253                                         GDIPCONST EncoderParameters *encoderParams)
1254 {
1255     GpStatus stat;
1256     IStream *stream;
1257
1258     TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1259
1260     if (!image || !filename|| !clsidEncoder)
1261         return InvalidParameter;
1262
1263     if (!(image->picture))
1264         return InvalidParameter;
1265
1266     stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1267     if (stat != Ok)
1268         return GenericError;
1269
1270     stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1271
1272     IStream_Release(stream);
1273     return stat;
1274 }
1275
1276 /*************************************************************************
1277  * Encoding functions -
1278  *   These functions encode an image in different image file formats.
1279  */
1280 #define BITMAP_FORMAT_BMP   0x4d42 /* "BM" */
1281 #define BITMAP_FORMAT_JPEG  0xd8ff
1282 #define BITMAP_FORMAT_GIF   0x4947
1283 #define BITMAP_FORMAT_PNG   0x5089
1284 #define BITMAP_FORMAT_APM   0xcdd7
1285
1286 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1287                                  void **output, unsigned int *output_size)
1288 {
1289     int num_palette_entries;
1290     BITMAPFILEHEADER *bmp_file_hdr;
1291     BITMAPINFO *bmp_info_hdr;
1292
1293     if (bitmap_info->bmiHeader.biClrUsed) {
1294         num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
1295         if (num_palette_entries > 256) num_palette_entries = 256;
1296     } else {
1297         if (bitmap_info->bmiHeader.biBitCount <= 8)
1298             num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1299         else
1300             num_palette_entries = 0;
1301     }
1302
1303     *output_size =
1304         sizeof(BITMAPFILEHEADER) +
1305         sizeof(BITMAPINFOHEADER) +
1306         num_palette_entries * sizeof(RGBQUAD) +
1307         bitmap_info->bmiHeader.biSizeImage;
1308
1309     *output = GdipAlloc(*output_size);
1310
1311     bmp_file_hdr = *output;
1312     bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1313     bmp_file_hdr->bfSize = *output_size;
1314     bmp_file_hdr->bfOffBits =
1315         sizeof(BITMAPFILEHEADER) +
1316         sizeof(BITMAPINFOHEADER) +
1317         num_palette_entries * sizeof (RGBQUAD);
1318
1319     bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1320     memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1321     memcpy((unsigned char *)(*output) +
1322            sizeof(BITMAPFILEHEADER) +
1323            sizeof(BITMAPINFOHEADER) +
1324            num_palette_entries * sizeof(RGBQUAD),
1325            bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1326
1327     return Ok;
1328 }
1329
1330 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1331                                    void **output, unsigned int *output_size);
1332
1333 typedef enum {
1334     BMP,
1335     NUM_ENCODERS_SUPPORTED
1336 } ImageFormat;
1337
1338 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1339 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1340     encode_image_BMP,
1341 };
1342
1343 /*****************************************************************************
1344  * GdipSaveImageToStream [GDIPLUS.@]
1345  */
1346 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1347     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1348 {
1349     GpStatus stat;
1350     HRESULT hr;
1351     short type;
1352     HBITMAP hbmp;
1353     HBITMAP old_hbmp;
1354     HDC hdc;
1355     int bm_is_selected;
1356     BITMAPINFO bmp_info;
1357     LPVOID bmp_bits;
1358     encode_image_func* encode_image;
1359     LPVOID output;
1360     unsigned int output_size;
1361     unsigned int dummy;
1362     int i;
1363
1364     old_hbmp = 0;
1365     output = NULL;
1366     output_size = 0;
1367
1368     TRACE("%p %p %p %p\n", image, stream, clsid, params);
1369
1370     if(!image || !stream)
1371         return InvalidParameter;
1372
1373     if (!image->picture)
1374         return GenericError;
1375
1376     hr = IPicture_get_Type(image->picture, &type);
1377     if (FAILED(hr) || type != PICTYPE_BITMAP)
1378         return GenericError;
1379
1380     /* select correct encoder */
1381     encode_image = NULL;
1382     for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1383         if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1384             encode_image = encode_image_funcs[i];
1385     }
1386     if (encode_image == NULL)
1387         return UnknownImageFormat;
1388
1389     /* extract underlying hbitmap representation from the IPicture */
1390     hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1391     if (FAILED(hr) || !hbmp)
1392         return GenericError;
1393     hr = IPicture_get_CurDC(image->picture, &hdc);
1394     if (FAILED(hr))
1395         return GenericError;
1396     bm_is_selected = (hdc != 0);
1397     if (!bm_is_selected) {
1398         hdc = CreateCompatibleDC(0);
1399         old_hbmp = SelectObject(hdc, hbmp);
1400     }
1401
1402     /* get bits from HBITMAP */
1403     bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1404     bmp_info.bmiHeader.biBitCount = 0;
1405     GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1406
1407     bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1408
1409     if (bmp_bits)
1410         GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1411
1412     if (!bm_is_selected) {
1413         SelectObject(hdc, old_hbmp);
1414         DeleteDC(hdc);
1415     }
1416
1417     if (!bmp_bits)
1418         return OutOfMemory;
1419
1420     stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1421     if (stat == Ok)
1422         IStream_Write(stream, output, output_size, &dummy);
1423
1424     GdipFree(output);
1425     GdipFree(bmp_bits);
1426
1427     return stat;
1428 }
1429
1430 /*****************************************************************************
1431  * GdipSetImagePalette [GDIPLUS.@]
1432  */
1433 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1434     GDIPCONST ColorPalette *palette)
1435 {
1436     static int calls;
1437
1438     if(!image || !palette)
1439         return InvalidParameter;
1440
1441     if(!(calls++))
1442         FIXME("not implemented\n");
1443
1444     return NotImplemented;
1445 }
1446
1447 /*************************************************************************
1448  * Encoders -
1449  *   Structures that represent which formats we support for encoding.
1450  */
1451
1452 /* ImageCodecInfo creation routines taken from libgdiplus */
1453 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1454 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1455 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1456 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1457 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1458 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1459
1460 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1461     {
1462         { /* BMP */
1463             /* Clsid */              { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1464             /* FormatID */           { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1465             /* CodecName */          bmp_codecname,
1466             /* DllName */            NULL,
1467             /* FormatDescription */  bmp_format,
1468             /* FilenameExtension */  bmp_extension,
1469             /* MimeType */           bmp_mimetype,
1470             /* Flags */              ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1471             /* Version */            1,
1472             /* SigCount */           1,
1473             /* SigSize */            2,
1474             /* SigPattern */         bmp_sig_pattern,
1475             /* SigMask */            bmp_sig_mask,
1476         },
1477     };
1478
1479 /*****************************************************************************
1480  * GdipGetImageDecodersSize [GDIPLUS.@]
1481  */
1482 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
1483 {
1484     FIXME("%p %p stub!\n", numDecoders, size);
1485
1486     if (!numDecoders || !size)
1487         return InvalidParameter;
1488
1489     *numDecoders = 0;
1490     *size = 0;
1491
1492     return Ok;
1493 }
1494
1495 /*****************************************************************************
1496  * GdipGetImageDecoders [GDIPLUS.@]
1497  */
1498 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
1499 {
1500     FIXME("%u %u %p stub!\n", numDecoders, size, decoders);
1501
1502     if (!decoders)
1503         return GenericError;
1504
1505     return NotImplemented;
1506 }
1507
1508 /*****************************************************************************
1509  * GdipGetImageEncodersSize [GDIPLUS.@]
1510  */
1511 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1512 {
1513     TRACE("%p %p\n", numEncoders, size);
1514
1515     if (!numEncoders || !size)
1516         return InvalidParameter;
1517
1518     *numEncoders = NUM_ENCODERS_SUPPORTED;
1519     *size = sizeof (codecs);
1520
1521     return Ok;
1522 }
1523
1524 /*****************************************************************************
1525  * GdipGetImageEncoders [GDIPLUS.@]
1526  */
1527 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1528 {
1529     TRACE("%u %u %p\n", numEncoders, size, encoders);
1530
1531     if (!encoders ||
1532         (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1533         (size != sizeof (codecs)))
1534         return GenericError;
1535
1536     memcpy(encoders, codecs, sizeof (codecs));
1537
1538     return Ok;
1539 }
1540
1541 /*****************************************************************************
1542  * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1543  */
1544 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1545 {
1546     BITMAP bm;
1547     GpStatus retval;
1548     PixelFormat format;
1549     BYTE* bits;
1550
1551     TRACE("%p %p %p\n", hbm, hpal, bitmap);
1552
1553     if(!hbm || !bitmap)
1554         return InvalidParameter;
1555
1556     /* TODO: Support for device-dependent bitmaps */
1557     if(hpal){
1558         FIXME("no support for device-dependent bitmaps\n");
1559         return NotImplemented;
1560     }
1561
1562     if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1563             return InvalidParameter;
1564
1565     /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1566     switch(bm.bmBitsPixel) {
1567         case 1:
1568             format = PixelFormat1bppIndexed;
1569             break;
1570         case 4:
1571             format = PixelFormat4bppIndexed;
1572             break;
1573         case 8:
1574             format = PixelFormat8bppIndexed;
1575             break;
1576         case 24:
1577             format = PixelFormat24bppRGB;
1578             break;
1579         case 32:
1580             format = PixelFormat32bppRGB;
1581             break;
1582         case 48:
1583             format = PixelFormat48bppRGB;
1584             break;
1585         default:
1586             FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1587             return InvalidParameter;
1588     }
1589
1590     if (bm.bmBits)
1591         bits = (BYTE*)bm.bmBits + (bm.bmHeight - 1) * bm.bmWidthBytes;
1592     else
1593     {
1594         FIXME("can only get image data from DIB sections\n");
1595         bits = NULL;
1596     }
1597
1598     retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, -bm.bmWidthBytes,
1599         format, bits, bitmap);
1600
1601     return retval;
1602 }
1603
1604 /*****************************************************************************
1605  * GdipSetEffectParameters [GDIPLUS.@]
1606  */
1607 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1608     const VOID *params, const UINT size)
1609 {
1610     static int calls;
1611
1612     if(!(calls++))
1613         FIXME("not implemented\n");
1614
1615     return NotImplemented;
1616 }
1617
1618 /*****************************************************************************
1619  * GdipGetImageFlags [GDIPLUS.@]
1620  */
1621 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1622 {
1623     TRACE("%p %p\n", image, flags);
1624
1625     if(!image || !flags)
1626         return InvalidParameter;
1627
1628     *flags = image->flags;
1629
1630     return Ok;
1631 }
1632
1633 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
1634 {
1635     TRACE("(%d, %p)\n", control, param);
1636
1637     switch(control){
1638         case TestControlForceBilinear:
1639             if(param)
1640                 FIXME("TestControlForceBilinear not handled\n");
1641             break;
1642         case TestControlNoICM:
1643             if(param)
1644                 FIXME("TestControlNoICM not handled\n");
1645             break;
1646         case TestControlGetBuildNumber:
1647             *((DWORD*)param) = 3102;
1648             break;
1649     }
1650
1651     return Ok;
1652 }
1653
1654 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
1655                             HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
1656                             MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
1657                             GpMetafile **metafile)
1658 {
1659     FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1660                                  frameUnit, debugstr_w(desc), metafile);
1661
1662     return NotImplemented;
1663 }
1664
1665 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
1666                             GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
1667                             GDIPCONST WCHAR *desc, GpMetafile **metafile)
1668 {
1669     FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1670                                  frameUnit, debugstr_w(desc), metafile);
1671
1672     return NotImplemented;
1673 }
1674
1675 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
1676 {
1677     FIXME("%p\n", image);
1678
1679     return Ok;
1680 }
1681
1682 /*****************************************************************************
1683  * GdipGetImageThumbnail [GDIPLUS.@]
1684  */
1685 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
1686                             GpImage **ret_image, GetThumbnailImageAbort cb,
1687                             VOID * cb_data)
1688 {
1689     FIXME("(%p %u %u %p %p %p) stub\n",
1690         image, width, height, ret_image, cb, cb_data);
1691     return NotImplemented;
1692 }
1693
1694 /*****************************************************************************
1695  * GdipImageRotateFlip [GDIPLUS.@]
1696  */
1697 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
1698 {
1699     FIXME("(%p %u) stub\n", image, type);
1700     return NotImplemented;
1701 }