mshtml: Fix the Hungarian translation.
[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 bmi;
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     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
135     bmi.bmiHeader.biBitCount = 0;
136
137     if(!bm_is_selected){
138         hdc = CreateCompatibleDC(0);
139         old = SelectObject(hdc, (HBITMAP)hbm);
140     }
141
142     /* fill out bmi */
143     GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
144
145     abs_height = abs(bmi.bmiHeader.biHeight);
146     stride = bmi.bmiHeader.biWidth * bitspp / 8;
147     stride = (stride + 3) & ~3;
148
149     buff = GdipAlloc(stride * abs_height);
150
151     bmi.bmiHeader.biBitCount = bitspp;
152
153     if(buff)
154         GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, &bmi, DIB_RGB_COLORS);
155
156     if(!bm_is_selected){
157         SelectObject(hdc, old);
158         DeleteDC(hdc);
159     }
160
161     if(!buff)
162         return OutOfMemory;
163
164     lockeddata->Width  = act_rect.Width;
165     lockeddata->Height = act_rect.Height;
166     lockeddata->PixelFormat = format;
167     lockeddata->Reserved = flags;
168
169     if(bmi.bmiHeader.biHeight > 0){
170         lockeddata->Stride = -stride;
171         lockeddata->Scan0  = buff + (bitspp / 8) * act_rect.X +
172                              stride * (abs_height - 1 - act_rect.Y);
173     }
174     else{
175         lockeddata->Stride = stride;
176         lockeddata->Scan0  = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
177     }
178
179     bitmap->lockmode = flags;
180     bitmap->numlocks++;
181
182     bitmap->bitmapbits = buff;
183
184     return Ok;
185 }
186
187 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
188     BitmapData* lockeddata)
189 {
190     OLE_HANDLE hbm;
191     HDC hdc;
192     HBITMAP old = NULL;
193     BOOL bm_is_selected;
194     BITMAPINFO bmi;
195
196     if(!bitmap || !lockeddata)
197         return InvalidParameter;
198
199     if(!bitmap->lockmode)
200         return WrongState;
201
202     if(lockeddata->Reserved & ImageLockModeUserInputBuf)
203         return NotImplemented;
204
205     if(lockeddata->Reserved & ImageLockModeRead){
206         if(!(--bitmap->numlocks))
207             bitmap->lockmode = 0;
208
209         GdipFree(bitmap->bitmapbits);
210         bitmap->bitmapbits = NULL;
211         return Ok;
212     }
213
214     IPicture_get_Handle(bitmap->image.picture, &hbm);
215     IPicture_get_CurDC(bitmap->image.picture, &hdc);
216     bm_is_selected = (hdc != 0);
217
218     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
219     bmi.bmiHeader.biBitCount = 0;
220
221     if(!bm_is_selected){
222         hdc = CreateCompatibleDC(0);
223         old = SelectObject(hdc, (HBITMAP)hbm);
224     }
225
226     GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
227     bmi.bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
228     SetDIBits(hdc, (HBITMAP)hbm, 0, abs(bmi.bmiHeader.biHeight),
229               bitmap->bitmapbits, &bmi, DIB_RGB_COLORS);
230
231     if(!bm_is_selected){
232         SelectObject(hdc, old);
233         DeleteDC(hdc);
234     }
235
236     GdipFree(bitmap->bitmapbits);
237     bitmap->bitmapbits = NULL;
238     bitmap->lockmode = 0;
239
240     return Ok;
241 }
242
243 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
244     GpBitmap **bitmap)
245 {
246     GpStatus stat;
247     IStream *stream;
248
249     if(!filename || !bitmap)
250         return InvalidParameter;
251
252     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
253
254     if(stat != Ok)
255         return stat;
256
257     stat = GdipCreateBitmapFromStream(stream, bitmap);
258
259     IStream_Release(stream);
260
261     return stat;
262 }
263
264 /* FIXME: no icm */
265 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
266     GpBitmap **bitmap)
267 {
268     return GdipCreateBitmapFromFile(filename, bitmap);
269 }
270
271 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
272     HBITMAP* hbmReturn, ARGB background)
273 {
274     FIXME("stub\n");
275
276     hbmReturn = NULL;
277
278     return NotImplemented;
279 }
280
281 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
282     GpMetafile* metafile, BOOL* succ, EmfType emfType,
283     const WCHAR* description, GpMetafile** out_metafile)
284 {
285     static int calls;
286
287     if(!ref || !metafile || !out_metafile)
288         return InvalidParameter;
289
290     *succ = FALSE;
291     *out_metafile = NULL;
292
293     if(!(calls++))
294         FIXME("not implemented\n");
295
296     return NotImplemented;
297 }
298
299 /* FIXME: this should create a bitmap in the given size with the attributes
300  * (resolution etc.) of the graphics object */
301 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
302     GpGraphics* target, GpBitmap** bitmap)
303 {
304     static int calls;
305     GpStatus ret;
306
307     if(!target || !bitmap)
308         return InvalidParameter;
309
310     if(!(calls++))
311         FIXME("hacked stub\n");
312
313     ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
314                                     NULL, bitmap);
315
316     return ret;
317 }
318
319 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
320     PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
321 {
322     BITMAPFILEHEADER *bmfh;
323     BITMAPINFOHEADER *bmih;
324     BYTE *buff;
325     INT datalen, size;
326     IStream *stream;
327
328     TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
329
330     if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
331         *bitmap = NULL;
332         return InvalidParameter;
333     }
334
335     if(scan0 && !stride)
336         return InvalidParameter;
337
338     /* FIXME: windows allows negative stride (reads backwards from scan0) */
339     if(stride < 0){
340         FIXME("negative stride\n");
341         return InvalidParameter;
342     }
343
344     *bitmap = GdipAlloc(sizeof(GpBitmap));
345     if(!*bitmap)    return OutOfMemory;
346
347     if(stride == 0){
348         stride = width * (PIXELFORMATBPP(format) / 8);
349         stride = (stride + 3) & ~3;
350     }
351
352     datalen = abs(stride * height);
353     size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
354     buff = GdipAlloc(size);
355     if(!buff){
356         GdipFree(*bitmap);
357         return OutOfMemory;
358     }
359
360     bmfh = (BITMAPFILEHEADER*) buff;
361     bmih = (BITMAPINFOHEADER*) (bmfh + 1);
362
363     bmfh->bfType    = (((WORD)'M') << 8) + (WORD)'B';
364     bmfh->bfSize    = size;
365     bmfh->bfOffBits = size - datalen;
366
367     bmih->biSize            = sizeof(BITMAPINFOHEADER);
368     bmih->biWidth           = width;
369     bmih->biHeight          = -height;
370     /* FIXME: use the rest of the data from format */
371     bmih->biBitCount        = PIXELFORMATBPP(format);
372     bmih->biCompression     = BI_RGB;
373     bmih->biSizeImage       = datalen;
374
375     if(scan0)
376         memcpy(bmih + 1, scan0, datalen);
377     else
378         memset(bmih + 1, 0, datalen);
379
380     if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
381         ERR("could not make stream\n");
382         GdipFree(*bitmap);
383         GdipFree(buff);
384         return GenericError;
385     }
386
387     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
388         (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
389         TRACE("Could not load picture\n");
390         IStream_Release(stream);
391         GdipFree(*bitmap);
392         GdipFree(buff);
393         return GenericError;
394     }
395
396     (*bitmap)->image.type = ImageTypeBitmap;
397     (*bitmap)->image.flags = ImageFlagsNone;
398     (*bitmap)->width = width;
399     (*bitmap)->height = height;
400     (*bitmap)->format = format;
401
402     return Ok;
403 }
404
405 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
406     GpBitmap **bitmap)
407 {
408     GpStatus stat;
409
410     stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
411
412     if(stat != Ok)
413         return stat;
414
415     if((*bitmap)->image.type != ImageTypeBitmap){
416         IPicture_Release((*bitmap)->image.picture);
417         GdipFree(bitmap);
418         return GenericError; /* FIXME: what error to return? */
419     }
420
421     return Ok;
422 }
423
424 /* FIXME: no icm */
425 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
426     GpBitmap **bitmap)
427 {
428     return GdipCreateBitmapFromStream(stream, bitmap);
429 }
430
431 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
432 {
433     HDC hdc;
434
435     if(!image)
436         return InvalidParameter;
437
438     IPicture_get_CurDC(image->picture, &hdc);
439     DeleteDC(hdc);
440     IPicture_Release(image->picture);
441     if (image->type == ImageTypeBitmap)
442         GdipFree(((GpBitmap*)image)->bitmapbits);
443     GdipFree(image);
444
445     return Ok;
446 }
447
448 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
449 {
450     if(!image || !item)
451         return InvalidParameter;
452
453     return NotImplemented;
454 }
455
456 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
457     GpUnit *srcUnit)
458 {
459     if(!image || !srcRect || !srcUnit)
460         return InvalidParameter;
461     if(image->type == ImageTypeMetafile){
462         *srcRect = ((GpMetafile*)image)->bounds;
463         *srcUnit = ((GpMetafile*)image)->unit;
464     }
465     else if(image->type == ImageTypeBitmap){
466         srcRect->X = srcRect->Y = 0.0;
467         srcRect->Width = (REAL) ((GpBitmap*)image)->width;
468         srcRect->Height = (REAL) ((GpBitmap*)image)->height;
469         *srcUnit = UnitPixel;
470     }
471     else{
472         srcRect->X = srcRect->Y = 0.0;
473         srcRect->Width = ipicture_pixel_width(image->picture);
474         srcRect->Height = ipicture_pixel_height(image->picture);
475         *srcUnit = UnitPixel;
476     }
477
478     TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
479           srcRect->Width, srcRect->Height, *srcUnit);
480
481     return Ok;
482 }
483
484 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
485     REAL *height)
486 {
487     if(!image || !height || !width)
488         return InvalidParameter;
489
490     if(image->type == ImageTypeMetafile){
491         HDC hdc = GetDC(0);
492
493         *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
494                         ((GpMetafile*)image)->bounds.Height;
495
496         *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
497                         ((GpMetafile*)image)->bounds.Width;
498
499         ReleaseDC(0, hdc);
500     }
501
502     else if(image->type == ImageTypeBitmap){
503         *height = ((GpBitmap*)image)->height;
504         *width = ((GpBitmap*)image)->width;
505     }
506     else{
507         *height = ipicture_pixel_height(image->picture);
508         *width = ipicture_pixel_width(image->picture);
509     }
510
511     TRACE("returning (%f, %f)\n", *height, *width);
512     return Ok;
513 }
514
515 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
516     GpGraphics **graphics)
517 {
518     HDC hdc;
519
520     if(!image || !graphics)
521         return InvalidParameter;
522
523     if(image->type != ImageTypeBitmap){
524         FIXME("not implemented for image type %d\n", image->type);
525         return NotImplemented;
526     }
527
528     IPicture_get_CurDC(image->picture, &hdc);
529
530     if(!hdc){
531         hdc = CreateCompatibleDC(0);
532         IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
533     }
534
535     return GdipCreateFromHDC(hdc, graphics);
536 }
537
538 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
539 {
540     if(!image || !height)
541         return InvalidParameter;
542
543     if(image->type == ImageTypeMetafile){
544         HDC hdc = GetDC(0);
545
546         *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
547                         ((GpMetafile*)image)->bounds.Height);
548
549         ReleaseDC(0, hdc);
550     }
551     else if(image->type == ImageTypeBitmap)
552         *height = ((GpBitmap*)image)->height;
553     else
554         *height = ipicture_pixel_height(image->picture);
555
556     TRACE("returning %d\n", *height);
557
558     return Ok;
559 }
560
561 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
562 {
563     static int calls;
564
565     if(!image || !res)
566         return InvalidParameter;
567
568     if(!(calls++))
569         FIXME("not implemented\n");
570
571     return NotImplemented;
572 }
573
574 /* FIXME: test this function for non-bitmap types */
575 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
576 {
577     if(!image || !format)
578         return InvalidParameter;
579
580     if(image->type != ImageTypeBitmap)
581         *format = PixelFormat24bppRGB;
582     else
583         *format = ((GpBitmap*) image)->format;
584
585     return Ok;
586 }
587
588 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
589 {
590     static int calls;
591
592     if(!image || !format)
593         return InvalidParameter;
594
595     if(!(calls++))
596         FIXME("not implemented\n");
597
598     return NotImplemented;
599 }
600
601 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
602 {
603     if(!image || !type)
604         return InvalidParameter;
605
606     *type = image->type;
607
608     return Ok;
609 }
610
611 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
612 {
613     static int calls;
614
615     if(!image || !res)
616         return InvalidParameter;
617
618     if(!(calls++))
619         FIXME("not implemented\n");
620
621     return NotImplemented;
622 }
623
624 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
625 {
626     if(!image || !width)
627         return InvalidParameter;
628
629     if(image->type == ImageTypeMetafile){
630         HDC hdc = GetDC(0);
631
632         *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
633                         ((GpMetafile*)image)->bounds.Width);
634
635         ReleaseDC(0, hdc);
636     }
637     else if(image->type == ImageTypeBitmap)
638         *width = ((GpBitmap*)image)->width;
639     else
640         *width = ipicture_pixel_width(image->picture);
641
642     TRACE("returning %d\n", *width);
643
644     return Ok;
645 }
646
647 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
648     MetafileHeader * header)
649 {
650     static int calls;
651
652     if(!metafile || !header)
653         return InvalidParameter;
654
655     if(!(calls++))
656         FIXME("not implemented\n");
657
658     return Ok;
659 }
660
661 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
662     UINT* size)
663 {
664     static int calls;
665
666     TRACE("%p %x %p\n", image, pid, size);
667
668     if(!size || !image)
669         return InvalidParameter;
670
671     if(!(calls++))
672         FIXME("not implemented\n");
673
674     return NotImplemented;
675 }
676
677 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
678     GDIPCONST GUID* dimensionID, UINT* count)
679 {
680     static int calls;
681
682     if(!image || !dimensionID || !count)
683         return InvalidParameter;
684
685     if(!(calls++))
686         FIXME("not implemented\n");
687
688     return NotImplemented;
689 }
690
691 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
692     GUID* dimensionIDs, UINT count)
693 {
694     static int calls;
695
696     if(!image || !dimensionIDs)
697         return InvalidParameter;
698
699     if(!(calls++))
700         FIXME("not implemented\n");
701
702     return Ok;
703 }
704
705 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
706     GDIPCONST GUID* dimensionID, UINT frameidx)
707 {
708     static int calls;
709
710     if(!image || !dimensionID)
711         return InvalidParameter;
712
713     if(!(calls++))
714         FIXME("not implemented\n");
715
716     return Ok;
717 }
718
719 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
720                                           GpImage **image)
721 {
722     GpStatus stat;
723     IStream *stream;
724
725     if (!filename || !image)
726         return InvalidParameter;
727
728     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
729
730     if (stat != Ok)
731         return stat;
732
733     stat = GdipLoadImageFromStream(stream, image);
734
735     IStream_Release(stream);
736
737     return stat;
738 }
739
740 /* FIXME: no icm handling */
741 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
742 {
743     return GdipLoadImageFromFile(filename, image);
744 }
745
746 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
747 {
748     IPicture *pic;
749     short type;
750
751     if(!stream || !image)
752         return InvalidParameter;
753
754     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
755         (LPVOID*) &pic) != S_OK){
756         TRACE("Could not load picture\n");
757         return GenericError;
758     }
759
760     IPicture_get_Type(pic, &type);
761
762     if(type == PICTYPE_BITMAP){
763         BITMAPINFO bmi;
764         BITMAPCOREHEADER* bmch;
765         OLE_HANDLE hbm;
766         HDC hdc;
767
768         *image = GdipAlloc(sizeof(GpBitmap));
769         if(!*image) return OutOfMemory;
770         (*image)->type = ImageTypeBitmap;
771
772         (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
773         (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
774
775         /* get the pixel format */
776         IPicture_get_Handle(pic, &hbm);
777         IPicture_get_CurDC(pic, &hdc);
778
779         ZeroMemory(&bmi, sizeof(bmi));
780         bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
781         bmch->bcSize = sizeof(BITMAPCOREHEADER);
782
783         if(!hdc){
784             HBITMAP old;
785             hdc = CreateCompatibleDC(0);
786             old = SelectObject(hdc, (HBITMAP)hbm);
787             GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
788             SelectObject(hdc, old);
789             DeleteDC(hdc);
790         }
791         else
792             GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
793
794         (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
795     }
796     else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
797         /* FIXME: missing initialization code */
798         *image = GdipAlloc(sizeof(GpMetafile));
799         if(!*image) return OutOfMemory;
800         (*image)->type = ImageTypeMetafile;
801     }
802     else{
803         *image = GdipAlloc(sizeof(GpImage));
804         if(!*image) return OutOfMemory;
805         (*image)->type = ImageTypeUnknown;
806     }
807
808     (*image)->picture = pic;
809     (*image)->flags   = ImageFlagsNone;
810
811     return Ok;
812 }
813
814 /* FIXME: no ICM */
815 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
816 {
817     return GdipLoadImageFromStream(stream, image);
818 }
819
820 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
821 {
822     static int calls;
823
824     if(!image)
825         return InvalidParameter;
826
827     if(!(calls++))
828         FIXME("not implemented\n");
829
830     return NotImplemented;
831 }
832
833 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
834                                         GDIPCONST CLSID *clsidEncoder,
835                                         GDIPCONST EncoderParameters *encoderParams)
836 {
837     GpStatus stat;
838     IStream *stream;
839
840     if (!image || !filename|| !clsidEncoder)
841         return InvalidParameter;
842
843     if (!(image->picture))
844         return InvalidParameter;
845
846     stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
847     if (stat != Ok)
848         return GenericError;
849
850     stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
851
852     IStream_Release(stream);
853     return stat;
854 }
855
856 /*************************************************************************
857  * Encoding functions -
858  *   These functions encode an image in different image file formats.
859  */
860 #define BITMAP_FORMAT_BMP   0x4d42 /* "BM" */
861 #define BITMAP_FORMAT_JPEG  0xd8ff
862 #define BITMAP_FORMAT_GIF   0x4947
863 #define BITMAP_FORMAT_PNG   0x5089
864 #define BITMAP_FORMAT_APM   0xcdd7
865
866 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
867                                  void **output, unsigned int *output_size)
868 {
869     int num_palette_entries;
870     BITMAPFILEHEADER *bmp_file_hdr;
871     BITMAPINFO *bmp_info_hdr;
872
873     if (bitmap_info->bmiHeader.biClrUsed) {
874         num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
875         if (num_palette_entries > 256) num_palette_entries = 256;
876     } else {
877         if (bitmap_info->bmiHeader.biBitCount <= 8)
878             num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
879         else
880             num_palette_entries = 0;
881     }
882
883     *output_size =
884         sizeof(BITMAPFILEHEADER) +
885         sizeof(BITMAPINFOHEADER) +
886         num_palette_entries * sizeof(RGBQUAD) +
887         bitmap_info->bmiHeader.biSizeImage;
888
889     *output = GdipAlloc(*output_size);
890
891     bmp_file_hdr = (BITMAPFILEHEADER*) *output;
892     bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
893     bmp_file_hdr->bfSize = *output_size;
894     bmp_file_hdr->bfOffBits =
895         sizeof(BITMAPFILEHEADER) +
896         sizeof(BITMAPINFOHEADER) +
897         num_palette_entries * sizeof (RGBQUAD);
898
899     bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
900     memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
901     memcpy((unsigned char *)(*output) +
902            sizeof(BITMAPFILEHEADER) +
903            sizeof(BITMAPINFOHEADER) +
904            num_palette_entries * sizeof(RGBQUAD),
905            bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
906
907     return Ok;
908 }
909
910 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
911                                    void **output, unsigned int *output_size);
912
913 typedef enum {
914     BMP,
915     NUM_ENCODERS_SUPPORTED
916 } ImageFormat;
917
918 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
919 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
920     encode_image_BMP,
921 };
922
923 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
924     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
925 {
926     GpStatus stat;
927     HRESULT hr;
928     short type;
929     HBITMAP hbmp;
930     HBITMAP old_hbmp;
931     HDC hdc;
932     int bm_is_selected;
933     BITMAPINFO bmp_info;
934     LPVOID bmp_bits;
935     encode_image_func* encode_image;
936     LPVOID output;
937     unsigned int output_size;
938     unsigned int dummy;
939     int i;
940
941     old_hbmp = 0;
942     output = NULL;
943     output_size = 0;
944
945     if(!image || !stream)
946         return InvalidParameter;
947
948     if (!image->picture)
949         return GenericError;
950
951     hr = IPicture_get_Type(image->picture, &type);
952     if (FAILED(hr) || type != PICTYPE_BITMAP)
953         return GenericError;
954
955     /* select correct encoder */
956     encode_image = NULL;
957     for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
958         if (IsEqualCLSID(clsid, &codecs[i].Clsid))
959             encode_image = encode_image_funcs[i];
960     }
961     if (encode_image == NULL)
962         return UnknownImageFormat;
963
964     /* extract underlying hbitmap representation from the IPicture */
965     hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
966     if (FAILED(hr) || !hbmp)
967         return GenericError;
968     hr = IPicture_get_CurDC(image->picture, &hdc);
969     if (FAILED(hr))
970         return GenericError;
971     bm_is_selected = (hdc != 0);
972     if (!bm_is_selected) {
973         hdc = CreateCompatibleDC(0);
974         old_hbmp = SelectObject(hdc, hbmp);
975     }
976
977     /* get bits from HBITMAP */
978     bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
979     bmp_info.bmiHeader.biBitCount = 0;
980     GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
981
982     bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
983
984     if (bmp_bits)
985         GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
986
987     if (!bm_is_selected) {
988         SelectObject(hdc, old_hbmp);
989         DeleteDC(hdc);
990     }
991
992     if (!bmp_bits)
993         return OutOfMemory;
994
995     stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
996     if (stat == Ok)
997         IStream_Write(stream, output, output_size, &dummy);
998
999     GdipFree(output);
1000     GdipFree(bmp_bits);
1001
1002     return stat;
1003 }
1004
1005 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1006     GDIPCONST ColorPalette *palette)
1007 {
1008     static int calls;
1009
1010     if(!image || !palette)
1011         return InvalidParameter;
1012
1013     if(!(calls++))
1014         FIXME("not implemented\n");
1015
1016     return NotImplemented;
1017 }
1018
1019 /*************************************************************************
1020  * Encoders -
1021  *   Structures that represent which formats we support for encoding.
1022  */
1023
1024 /* ImageCodecInfo creation routines taken from libgdiplus */
1025 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1026 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1027 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1028 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1029 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1030 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1031
1032 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1033     {
1034         { /* BMP */
1035             /* Clsid */              { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1036             /* FormatID */           { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1037             /* CodecName */          bmp_codecname,
1038             /* DllName */            NULL,
1039             /* FormatDescription */  bmp_format,
1040             /* FilenameExtension */  bmp_extension,
1041             /* MimeType */           bmp_mimetype,
1042             /* Flags */              ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1043             /* Version */            1,
1044             /* SigCount */           1,
1045             /* SigSize */            2,
1046             /* SigPattern */         bmp_sig_pattern,
1047             /* SigMask */            bmp_sig_mask,
1048         },
1049     };
1050
1051 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1052 {
1053     if (!numEncoders || !size)
1054         return InvalidParameter;
1055
1056     *numEncoders = NUM_ENCODERS_SUPPORTED;
1057     *size = sizeof (codecs);
1058
1059     return Ok;
1060 }
1061
1062 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1063 {
1064     if (!encoders ||
1065         (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1066         (size != sizeof (codecs)))
1067         return GenericError;
1068
1069     memcpy(encoders, codecs, sizeof (codecs));
1070
1071     return Ok;
1072 }
1073 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1074 {
1075     BITMAP bm;
1076     GpStatus retval;
1077     PixelFormat format;
1078
1079     if(!hbm || !bitmap)
1080         return InvalidParameter;
1081
1082     /* TODO: Support for device-dependent bitmaps */
1083     if(hpal){
1084         FIXME("no support for device-dependent bitmaps\n");
1085         return NotImplemented;
1086     }
1087
1088     if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1089             return InvalidParameter;
1090
1091     /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1092     switch(bm.bmBitsPixel) {
1093         case 1:
1094             format = PixelFormat1bppIndexed;
1095             break;
1096         case 4:
1097             format = PixelFormat4bppIndexed;
1098             break;
1099         case 8:
1100             format = PixelFormat8bppIndexed;
1101             break;
1102         case 24:
1103             format = PixelFormat24bppRGB;
1104             break;
1105         case 48:
1106             format = PixelFormat48bppRGB;
1107             break;
1108         default:
1109             FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1110             return InvalidParameter;
1111     }
1112
1113     retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1114         format, bm.bmBits, bitmap);
1115
1116     return retval;
1117 }
1118
1119 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1120     const VOID *params, const UINT size)
1121 {
1122     static int calls;
1123
1124     if(!(calls++))
1125         FIXME("not implemented\n");
1126
1127     return NotImplemented;
1128 }
1129
1130 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1131 {
1132     if(!image || !flags)
1133         return InvalidParameter;
1134
1135     *flags = image->flags;
1136
1137     return Ok;
1138 }