mshtml: Add tests for get_scrollLeft.
[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 GdipCloneImage(GpImage *image, GpImage **cloneImage)
244 {
245     IStream* stream;
246     HRESULT hr;
247     INT size;
248
249     TRACE("%p, %p\n", image, cloneImage);
250
251     if (!image || !cloneImage)
252         return InvalidParameter;
253
254     hr = CreateStreamOnHGlobal(0, TRUE, &stream);
255     if (FAILED(hr))
256         return GenericError;
257
258     *cloneImage = GdipAlloc(sizeof(GpImage));
259     if (!*cloneImage)
260     {
261         IStream_Release(stream);
262         return OutOfMemory;
263     }
264     (*cloneImage)->type = image->type;
265     (*cloneImage)->flags = image->flags;
266
267     hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
268     if(FAILED(hr))
269     {
270         WARN("Failed to save image on stream\n");
271         goto out;
272     }
273
274     hr = OleLoadPicture(stream, size, FALSE, &IID_IPicture,
275             (LPVOID*) &(*cloneImage)->picture);
276     if (FAILED(hr))
277     {
278         WARN("Failed to load image from stream\n");
279         goto out;
280     }
281
282     IStream_Release(stream);
283     return Ok;
284 out:
285     IStream_Release(stream);
286     GdipFree(*cloneImage);
287     *cloneImage = NULL;
288     return GenericError;
289 }
290
291 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
292     GpBitmap **bitmap)
293 {
294     GpStatus stat;
295     IStream *stream;
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     return GdipCreateBitmapFromFile(filename, bitmap);
358 }
359
360 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
361     GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
362 {
363     HBITMAP hbm;
364     GpStatus stat = InvalidParameter;
365
366     if(!lpBitmapName || !bitmap)
367         return InvalidParameter;
368
369     /* load DIB */
370     hbm = (HBITMAP)LoadImageW(hInstance,lpBitmapName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
371
372     if(hbm){
373         stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
374         DeleteObject(hbm);
375     }
376
377     return stat;
378 }
379
380 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
381     HBITMAP* hbmReturn, ARGB background)
382 {
383     FIXME("stub\n");
384
385     hbmReturn = NULL;
386
387     return NotImplemented;
388 }
389
390 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
391     GpMetafile* metafile, BOOL* succ, EmfType emfType,
392     const WCHAR* description, GpMetafile** out_metafile)
393 {
394     static int calls;
395
396     if(!ref || !metafile || !out_metafile)
397         return InvalidParameter;
398
399     *succ = FALSE;
400     *out_metafile = NULL;
401
402     if(!(calls++))
403         FIXME("not implemented\n");
404
405     return NotImplemented;
406 }
407
408 /* FIXME: this should create a bitmap in the given size with the attributes
409  * (resolution etc.) of the graphics object */
410 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
411     GpGraphics* target, GpBitmap** bitmap)
412 {
413     static int calls;
414     GpStatus ret;
415
416     if(!target || !bitmap)
417         return InvalidParameter;
418
419     if(!(calls++))
420         FIXME("hacked stub\n");
421
422     ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
423                                     NULL, bitmap);
424
425     return ret;
426 }
427
428 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
429     PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
430 {
431     BITMAPFILEHEADER *bmfh;
432     BITMAPINFOHEADER *bmih;
433     BYTE *buff;
434     INT datalen, size;
435     IStream *stream;
436
437     TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
438
439     if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
440         *bitmap = NULL;
441         return InvalidParameter;
442     }
443
444     if(scan0 && !stride)
445         return InvalidParameter;
446
447     /* FIXME: windows allows negative stride (reads backwards from scan0) */
448     if(stride < 0){
449         FIXME("negative stride\n");
450         return InvalidParameter;
451     }
452
453     *bitmap = GdipAlloc(sizeof(GpBitmap));
454     if(!*bitmap)    return OutOfMemory;
455
456     if(stride == 0){
457         stride = width * (PIXELFORMATBPP(format) / 8);
458         stride = (stride + 3) & ~3;
459     }
460
461     datalen = abs(stride * height);
462     size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
463     buff = GdipAlloc(size);
464     if(!buff){
465         GdipFree(*bitmap);
466         return OutOfMemory;
467     }
468
469     bmfh = (BITMAPFILEHEADER*) buff;
470     bmih = (BITMAPINFOHEADER*) (bmfh + 1);
471
472     bmfh->bfType    = (((WORD)'M') << 8) + (WORD)'B';
473     bmfh->bfSize    = size;
474     bmfh->bfOffBits = size - datalen;
475
476     bmih->biSize            = sizeof(BITMAPINFOHEADER);
477     bmih->biWidth           = width;
478     bmih->biHeight          = -height;
479     /* FIXME: use the rest of the data from format */
480     bmih->biBitCount        = PIXELFORMATBPP(format);
481     bmih->biCompression     = BI_RGB;
482     bmih->biSizeImage       = datalen;
483
484     if(scan0)
485         memcpy(bmih + 1, scan0, datalen);
486     else
487         memset(bmih + 1, 0, datalen);
488
489     if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
490         ERR("could not make stream\n");
491         GdipFree(*bitmap);
492         GdipFree(buff);
493         return GenericError;
494     }
495
496     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
497         (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
498         TRACE("Could not load picture\n");
499         IStream_Release(stream);
500         GdipFree(*bitmap);
501         GdipFree(buff);
502         return GenericError;
503     }
504
505     (*bitmap)->image.type = ImageTypeBitmap;
506     (*bitmap)->image.flags = ImageFlagsNone;
507     (*bitmap)->width = width;
508     (*bitmap)->height = height;
509     (*bitmap)->format = format;
510
511     return Ok;
512 }
513
514 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
515     GpBitmap **bitmap)
516 {
517     GpStatus stat;
518
519     stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
520
521     if(stat != Ok)
522         return stat;
523
524     if((*bitmap)->image.type != ImageTypeBitmap){
525         IPicture_Release((*bitmap)->image.picture);
526         GdipFree(bitmap);
527         return GenericError; /* FIXME: what error to return? */
528     }
529
530     return Ok;
531 }
532
533 /* FIXME: no icm */
534 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
535     GpBitmap **bitmap)
536 {
537     return GdipCreateBitmapFromStream(stream, bitmap);
538 }
539
540 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
541 {
542     HDC hdc;
543
544     if(!image)
545         return InvalidParameter;
546
547     IPicture_get_CurDC(image->picture, &hdc);
548     DeleteDC(hdc);
549     IPicture_Release(image->picture);
550     if (image->type == ImageTypeBitmap)
551         GdipFree(((GpBitmap*)image)->bitmapbits);
552     GdipFree(image);
553
554     return Ok;
555 }
556
557 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
558 {
559     if(!image || !item)
560         return InvalidParameter;
561
562     return NotImplemented;
563 }
564
565 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
566     GpUnit *srcUnit)
567 {
568     if(!image || !srcRect || !srcUnit)
569         return InvalidParameter;
570     if(image->type == ImageTypeMetafile){
571         *srcRect = ((GpMetafile*)image)->bounds;
572         *srcUnit = ((GpMetafile*)image)->unit;
573     }
574     else if(image->type == ImageTypeBitmap){
575         srcRect->X = srcRect->Y = 0.0;
576         srcRect->Width = (REAL) ((GpBitmap*)image)->width;
577         srcRect->Height = (REAL) ((GpBitmap*)image)->height;
578         *srcUnit = UnitPixel;
579     }
580     else{
581         srcRect->X = srcRect->Y = 0.0;
582         srcRect->Width = ipicture_pixel_width(image->picture);
583         srcRect->Height = ipicture_pixel_height(image->picture);
584         *srcUnit = UnitPixel;
585     }
586
587     TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
588           srcRect->Width, srcRect->Height, *srcUnit);
589
590     return Ok;
591 }
592
593 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
594     REAL *height)
595 {
596     if(!image || !height || !width)
597         return InvalidParameter;
598
599     if(image->type == ImageTypeMetafile){
600         HDC hdc = GetDC(0);
601
602         *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
603                         ((GpMetafile*)image)->bounds.Height;
604
605         *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
606                         ((GpMetafile*)image)->bounds.Width;
607
608         ReleaseDC(0, hdc);
609     }
610
611     else if(image->type == ImageTypeBitmap){
612         *height = ((GpBitmap*)image)->height;
613         *width = ((GpBitmap*)image)->width;
614     }
615     else{
616         *height = ipicture_pixel_height(image->picture);
617         *width = ipicture_pixel_width(image->picture);
618     }
619
620     TRACE("returning (%f, %f)\n", *height, *width);
621     return Ok;
622 }
623
624 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
625     GpGraphics **graphics)
626 {
627     HDC hdc;
628
629     if(!image || !graphics)
630         return InvalidParameter;
631
632     if(image->type != ImageTypeBitmap){
633         FIXME("not implemented for image type %d\n", image->type);
634         return NotImplemented;
635     }
636
637     IPicture_get_CurDC(image->picture, &hdc);
638
639     if(!hdc){
640         hdc = CreateCompatibleDC(0);
641         IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
642     }
643
644     return GdipCreateFromHDC(hdc, graphics);
645 }
646
647 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
648 {
649     if(!image || !height)
650         return InvalidParameter;
651
652     if(image->type == ImageTypeMetafile){
653         HDC hdc = GetDC(0);
654
655         *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
656                         ((GpMetafile*)image)->bounds.Height);
657
658         ReleaseDC(0, hdc);
659     }
660     else if(image->type == ImageTypeBitmap)
661         *height = ((GpBitmap*)image)->height;
662     else
663         *height = ipicture_pixel_height(image->picture);
664
665     TRACE("returning %d\n", *height);
666
667     return Ok;
668 }
669
670 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
671 {
672     static int calls;
673
674     if(!image || !res)
675         return InvalidParameter;
676
677     if(!(calls++))
678         FIXME("not implemented\n");
679
680     return NotImplemented;
681 }
682
683 /* FIXME: test this function for non-bitmap types */
684 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
685 {
686     if(!image || !format)
687         return InvalidParameter;
688
689     if(image->type != ImageTypeBitmap)
690         *format = PixelFormat24bppRGB;
691     else
692         *format = ((GpBitmap*) image)->format;
693
694     return Ok;
695 }
696
697 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
698 {
699     static int calls;
700
701     if(!image || !format)
702         return InvalidParameter;
703
704     if(!(calls++))
705         FIXME("not implemented\n");
706
707     return NotImplemented;
708 }
709
710 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
711 {
712     if(!image || !type)
713         return InvalidParameter;
714
715     *type = image->type;
716
717     return Ok;
718 }
719
720 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
721 {
722     static int calls;
723
724     if(!image || !res)
725         return InvalidParameter;
726
727     if(!(calls++))
728         FIXME("not implemented\n");
729
730     return NotImplemented;
731 }
732
733 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
734 {
735     if(!image || !width)
736         return InvalidParameter;
737
738     if(image->type == ImageTypeMetafile){
739         HDC hdc = GetDC(0);
740
741         *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
742                         ((GpMetafile*)image)->bounds.Width);
743
744         ReleaseDC(0, hdc);
745     }
746     else if(image->type == ImageTypeBitmap)
747         *width = ((GpBitmap*)image)->width;
748     else
749         *width = ipicture_pixel_width(image->picture);
750
751     TRACE("returning %d\n", *width);
752
753     return Ok;
754 }
755
756 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
757     MetafileHeader * header)
758 {
759     static int calls;
760
761     if(!metafile || !header)
762         return InvalidParameter;
763
764     if(!(calls++))
765         FIXME("not implemented\n");
766
767     return Ok;
768 }
769
770 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
771     UINT num, PropertyItem* items)
772 {
773     static int calls;
774
775     if(!(calls++))
776         FIXME("not implemented\n");
777
778     return InvalidParameter;
779 }
780
781 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
782 {
783     static int calls;
784
785     if(!(calls++))
786         FIXME("not implemented\n");
787
788     return InvalidParameter;
789 }
790
791 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
792 {
793     static int calls;
794
795     if(!(calls++))
796         FIXME("not implemented\n");
797
798     return InvalidParameter;
799 }
800
801 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
802     PropertyItem* buffer)
803 {
804     static int calls;
805
806     if(!(calls++))
807         FIXME("not implemented\n");
808
809     return InvalidParameter;
810 }
811
812 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
813     UINT* size)
814 {
815     static int calls;
816
817     TRACE("%p %x %p\n", image, pid, size);
818
819     if(!size || !image)
820         return InvalidParameter;
821
822     if(!(calls++))
823         FIXME("not implemented\n");
824
825     return NotImplemented;
826 }
827
828 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
829 {
830     static int calls;
831
832     if(!(calls++))
833         FIXME("not implemented\n");
834
835     return InvalidParameter;
836 }
837
838 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
839     GDIPCONST GUID* dimensionID, UINT* count)
840 {
841     static int calls;
842
843     if(!image || !dimensionID || !count)
844         return InvalidParameter;
845
846     if(!(calls++))
847         FIXME("not implemented\n");
848
849     return NotImplemented;
850 }
851
852 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
853     UINT* count)
854 {
855     if(!image || !count)
856         return InvalidParameter;
857
858     *count = 1;
859
860     FIXME("stub\n");
861
862     return Ok;
863 }
864
865 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
866     GUID* dimensionIDs, UINT count)
867 {
868     static int calls;
869
870     if(!image || !dimensionIDs)
871         return InvalidParameter;
872
873     if(!(calls++))
874         FIXME("not implemented\n");
875
876     return Ok;
877 }
878
879 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
880     GDIPCONST GUID* dimensionID, UINT frameidx)
881 {
882     static int calls;
883
884     if(!image || !dimensionID)
885         return InvalidParameter;
886
887     if(!(calls++))
888         FIXME("not implemented\n");
889
890     return Ok;
891 }
892
893 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
894                                           GpImage **image)
895 {
896     GpStatus stat;
897     IStream *stream;
898
899     if (!filename || !image)
900         return InvalidParameter;
901
902     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
903
904     if (stat != Ok)
905         return stat;
906
907     stat = GdipLoadImageFromStream(stream, image);
908
909     IStream_Release(stream);
910
911     return stat;
912 }
913
914 /* FIXME: no icm handling */
915 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
916 {
917     return GdipLoadImageFromFile(filename, image);
918 }
919
920 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
921 {
922     IPicture *pic;
923     short type;
924
925     if(!stream || !image)
926         return InvalidParameter;
927
928     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
929         (LPVOID*) &pic) != S_OK){
930         TRACE("Could not load picture\n");
931         return GenericError;
932     }
933
934     IPicture_get_Type(pic, &type);
935
936     if(type == PICTYPE_BITMAP){
937         BITMAPINFO bmi;
938         BITMAPCOREHEADER* bmch;
939         OLE_HANDLE hbm;
940         HDC hdc;
941
942         *image = GdipAlloc(sizeof(GpBitmap));
943         if(!*image) return OutOfMemory;
944         (*image)->type = ImageTypeBitmap;
945
946         (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
947         (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
948
949         /* get the pixel format */
950         IPicture_get_Handle(pic, &hbm);
951         IPicture_get_CurDC(pic, &hdc);
952
953         ZeroMemory(&bmi, sizeof(bmi));
954         bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
955         bmch->bcSize = sizeof(BITMAPCOREHEADER);
956
957         if(!hdc){
958             HBITMAP old;
959             hdc = CreateCompatibleDC(0);
960             old = SelectObject(hdc, (HBITMAP)hbm);
961             GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
962             SelectObject(hdc, old);
963             DeleteDC(hdc);
964         }
965         else
966             GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
967
968         (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
969     }
970     else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
971         /* FIXME: missing initialization code */
972         *image = GdipAlloc(sizeof(GpMetafile));
973         if(!*image) return OutOfMemory;
974         (*image)->type = ImageTypeMetafile;
975     }
976     else{
977         *image = GdipAlloc(sizeof(GpImage));
978         if(!*image) return OutOfMemory;
979         (*image)->type = ImageTypeUnknown;
980     }
981
982     (*image)->picture = pic;
983     (*image)->flags   = ImageFlagsNone;
984
985     return Ok;
986 }
987
988 /* FIXME: no ICM */
989 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
990 {
991     return GdipLoadImageFromStream(stream, image);
992 }
993
994 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
995 {
996     static int calls;
997
998     if(!image)
999         return InvalidParameter;
1000
1001     if(!(calls++))
1002         FIXME("not implemented\n");
1003
1004     return NotImplemented;
1005 }
1006
1007 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1008 {
1009     static int calls;
1010
1011     if(!(calls++))
1012         FIXME("not implemented\n");
1013
1014     return NotImplemented;
1015 }
1016
1017 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1018                                         GDIPCONST CLSID *clsidEncoder,
1019                                         GDIPCONST EncoderParameters *encoderParams)
1020 {
1021     GpStatus stat;
1022     IStream *stream;
1023
1024     if (!image || !filename|| !clsidEncoder)
1025         return InvalidParameter;
1026
1027     if (!(image->picture))
1028         return InvalidParameter;
1029
1030     stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1031     if (stat != Ok)
1032         return GenericError;
1033
1034     stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1035
1036     IStream_Release(stream);
1037     return stat;
1038 }
1039
1040 /*************************************************************************
1041  * Encoding functions -
1042  *   These functions encode an image in different image file formats.
1043  */
1044 #define BITMAP_FORMAT_BMP   0x4d42 /* "BM" */
1045 #define BITMAP_FORMAT_JPEG  0xd8ff
1046 #define BITMAP_FORMAT_GIF   0x4947
1047 #define BITMAP_FORMAT_PNG   0x5089
1048 #define BITMAP_FORMAT_APM   0xcdd7
1049
1050 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1051                                  void **output, unsigned int *output_size)
1052 {
1053     int num_palette_entries;
1054     BITMAPFILEHEADER *bmp_file_hdr;
1055     BITMAPINFO *bmp_info_hdr;
1056
1057     if (bitmap_info->bmiHeader.biClrUsed) {
1058         num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
1059         if (num_palette_entries > 256) num_palette_entries = 256;
1060     } else {
1061         if (bitmap_info->bmiHeader.biBitCount <= 8)
1062             num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1063         else
1064             num_palette_entries = 0;
1065     }
1066
1067     *output_size =
1068         sizeof(BITMAPFILEHEADER) +
1069         sizeof(BITMAPINFOHEADER) +
1070         num_palette_entries * sizeof(RGBQUAD) +
1071         bitmap_info->bmiHeader.biSizeImage;
1072
1073     *output = GdipAlloc(*output_size);
1074
1075     bmp_file_hdr = (BITMAPFILEHEADER*) *output;
1076     bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1077     bmp_file_hdr->bfSize = *output_size;
1078     bmp_file_hdr->bfOffBits =
1079         sizeof(BITMAPFILEHEADER) +
1080         sizeof(BITMAPINFOHEADER) +
1081         num_palette_entries * sizeof (RGBQUAD);
1082
1083     bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1084     memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1085     memcpy((unsigned char *)(*output) +
1086            sizeof(BITMAPFILEHEADER) +
1087            sizeof(BITMAPINFOHEADER) +
1088            num_palette_entries * sizeof(RGBQUAD),
1089            bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1090
1091     return Ok;
1092 }
1093
1094 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1095                                    void **output, unsigned int *output_size);
1096
1097 typedef enum {
1098     BMP,
1099     NUM_ENCODERS_SUPPORTED
1100 } ImageFormat;
1101
1102 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1103 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1104     encode_image_BMP,
1105 };
1106
1107 /*****************************************************************************
1108  * GdipSaveImageToStream [GDIPLUS.@]
1109  */
1110 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1111     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1112 {
1113     GpStatus stat;
1114     HRESULT hr;
1115     short type;
1116     HBITMAP hbmp;
1117     HBITMAP old_hbmp;
1118     HDC hdc;
1119     int bm_is_selected;
1120     BITMAPINFO bmp_info;
1121     LPVOID bmp_bits;
1122     encode_image_func* encode_image;
1123     LPVOID output;
1124     unsigned int output_size;
1125     unsigned int dummy;
1126     int i;
1127
1128     old_hbmp = 0;
1129     output = NULL;
1130     output_size = 0;
1131
1132     if(!image || !stream)
1133         return InvalidParameter;
1134
1135     if (!image->picture)
1136         return GenericError;
1137
1138     hr = IPicture_get_Type(image->picture, &type);
1139     if (FAILED(hr) || type != PICTYPE_BITMAP)
1140         return GenericError;
1141
1142     /* select correct encoder */
1143     encode_image = NULL;
1144     for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1145         if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1146             encode_image = encode_image_funcs[i];
1147     }
1148     if (encode_image == NULL)
1149         return UnknownImageFormat;
1150
1151     /* extract underlying hbitmap representation from the IPicture */
1152     hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1153     if (FAILED(hr) || !hbmp)
1154         return GenericError;
1155     hr = IPicture_get_CurDC(image->picture, &hdc);
1156     if (FAILED(hr))
1157         return GenericError;
1158     bm_is_selected = (hdc != 0);
1159     if (!bm_is_selected) {
1160         hdc = CreateCompatibleDC(0);
1161         old_hbmp = SelectObject(hdc, hbmp);
1162     }
1163
1164     /* get bits from HBITMAP */
1165     bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1166     bmp_info.bmiHeader.biBitCount = 0;
1167     GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1168
1169     bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1170
1171     if (bmp_bits)
1172         GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1173
1174     if (!bm_is_selected) {
1175         SelectObject(hdc, old_hbmp);
1176         DeleteDC(hdc);
1177     }
1178
1179     if (!bmp_bits)
1180         return OutOfMemory;
1181
1182     stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1183     if (stat == Ok)
1184         IStream_Write(stream, output, output_size, &dummy);
1185
1186     GdipFree(output);
1187     GdipFree(bmp_bits);
1188
1189     return stat;
1190 }
1191
1192 /*****************************************************************************
1193  * GdipSetImagePalette [GDIPLUS.@]
1194  */
1195 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1196     GDIPCONST ColorPalette *palette)
1197 {
1198     static int calls;
1199
1200     if(!image || !palette)
1201         return InvalidParameter;
1202
1203     if(!(calls++))
1204         FIXME("not implemented\n");
1205
1206     return NotImplemented;
1207 }
1208
1209 /*************************************************************************
1210  * Encoders -
1211  *   Structures that represent which formats we support for encoding.
1212  */
1213
1214 /* ImageCodecInfo creation routines taken from libgdiplus */
1215 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1216 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1217 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1218 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1219 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1220 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1221
1222 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1223     {
1224         { /* BMP */
1225             /* Clsid */              { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1226             /* FormatID */           { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1227             /* CodecName */          bmp_codecname,
1228             /* DllName */            NULL,
1229             /* FormatDescription */  bmp_format,
1230             /* FilenameExtension */  bmp_extension,
1231             /* MimeType */           bmp_mimetype,
1232             /* Flags */              ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1233             /* Version */            1,
1234             /* SigCount */           1,
1235             /* SigSize */            2,
1236             /* SigPattern */         bmp_sig_pattern,
1237             /* SigMask */            bmp_sig_mask,
1238         },
1239     };
1240
1241 /*****************************************************************************
1242  * GdipGetImageEncodersSize [GDIPLUS.@]
1243  */
1244 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1245 {
1246     if (!numEncoders || !size)
1247         return InvalidParameter;
1248
1249     *numEncoders = NUM_ENCODERS_SUPPORTED;
1250     *size = sizeof (codecs);
1251
1252     return Ok;
1253 }
1254
1255 /*****************************************************************************
1256  * GdipGetImageEncoders [GDIPLUS.@]
1257  */
1258 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1259 {
1260     if (!encoders ||
1261         (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1262         (size != sizeof (codecs)))
1263         return GenericError;
1264
1265     memcpy(encoders, codecs, sizeof (codecs));
1266
1267     return Ok;
1268 }
1269
1270 /*****************************************************************************
1271  * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1272  */
1273 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1274 {
1275     BITMAP bm;
1276     GpStatus retval;
1277     PixelFormat format;
1278
1279     if(!hbm || !bitmap)
1280         return InvalidParameter;
1281
1282     /* TODO: Support for device-dependent bitmaps */
1283     if(hpal){
1284         FIXME("no support for device-dependent bitmaps\n");
1285         return NotImplemented;
1286     }
1287
1288     if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1289             return InvalidParameter;
1290
1291     /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1292     switch(bm.bmBitsPixel) {
1293         case 1:
1294             format = PixelFormat1bppIndexed;
1295             break;
1296         case 4:
1297             format = PixelFormat4bppIndexed;
1298             break;
1299         case 8:
1300             format = PixelFormat8bppIndexed;
1301             break;
1302         case 24:
1303             format = PixelFormat24bppRGB;
1304             break;
1305         case 48:
1306             format = PixelFormat48bppRGB;
1307             break;
1308         default:
1309             FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1310             return InvalidParameter;
1311     }
1312
1313     retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1314         format, bm.bmBits, bitmap);
1315
1316     return retval;
1317 }
1318
1319 /*****************************************************************************
1320  * GdipSetEffectParameters [GDIPLUS.@]
1321  */
1322 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1323     const VOID *params, const UINT size)
1324 {
1325     static int calls;
1326
1327     if(!(calls++))
1328         FIXME("not implemented\n");
1329
1330     return NotImplemented;
1331 }
1332
1333 /*****************************************************************************
1334  * GdipGetImageFlags [GDIPLUS.@]
1335  */
1336 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1337 {
1338     if(!image || !flags)
1339         return InvalidParameter;
1340
1341     *flags = image->flags;
1342
1343     return Ok;
1344 }
1345
1346 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
1347 {
1348     TRACE("(%d, %p)\n", control, param);
1349
1350     switch(control){
1351         case TestControlForceBilinear:
1352             if(param)
1353                 FIXME("TestControlForceBilinear not handled\n");
1354             break;
1355         case TestControlNoICM:
1356             if(param)
1357                 FIXME("TestControlNoICM not handled\n");
1358             break;
1359         case TestControlGetBuildNumber:
1360             *((DWORD*)param) = 3102;
1361             break;
1362     }
1363
1364     return Ok;
1365 }