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