push 7f8c39dca3a5819e8ef115eebf7abed537de3a22
[wine/hacks.git] / dlls / gdiplus / image.c
blob330e54bfb032fe88a01dcfefead499e9977eb14d
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
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.
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.
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
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "wingdi.h"
26 #define COBJMACROS
27 #include "objbase.h"
28 #include "olectl.h"
29 #include "ole2.h"
31 #include "gdiplus.h"
32 #include "gdiplus_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
37 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
39 static INT ipicture_pixel_height(IPicture *pic)
41 HDC hdcref;
42 OLE_YSIZE_HIMETRIC y;
44 IPicture_get_Height(pic, &y);
46 hdcref = GetDC(0);
48 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
49 ReleaseDC(0, hdcref);
51 return y;
54 static INT ipicture_pixel_width(IPicture *pic)
56 HDC hdcref;
57 OLE_XSIZE_HIMETRIC x;
59 IPicture_get_Width(pic, &x);
61 hdcref = GetDC(0);
63 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
65 ReleaseDC(0, hdcref);
67 return x;
70 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
71 ARGB *color)
73 static int calls;
74 TRACE("%p %d %d %p\n", bitmap, x, y, color);
76 if(!bitmap || !color)
77 return InvalidParameter;
79 if(!(calls++))
80 FIXME("not implemented\n");
82 *color = 0xdeadbeef;
84 return NotImplemented;
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).
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)
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 */
106 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
108 if(!lockeddata || !bitmap)
109 return InvalidParameter;
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;
116 act_rect = *rect;
118 else{
119 act_rect.X = act_rect.Y = 0;
120 act_rect.Width = bitmap->width;
121 act_rect.Height = bitmap->height;
124 if(flags & ImageLockModeUserInputBuf)
125 return NotImplemented;
127 if(bitmap->lockmode)
128 return WrongState;
130 IPicture_get_Handle(bitmap->image.picture, &hbm);
131 IPicture_get_CurDC(bitmap->image.picture, &hdc);
132 bm_is_selected = (hdc != 0);
134 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
135 bmi.bmiHeader.biBitCount = 0;
137 if(!bm_is_selected){
138 hdc = CreateCompatibleDC(0);
139 old = SelectObject(hdc, (HBITMAP)hbm);
142 /* fill out bmi */
143 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
145 abs_height = abs(bmi.bmiHeader.biHeight);
146 stride = bmi.bmiHeader.biWidth * bitspp / 8;
147 stride = (stride + 3) & ~3;
149 buff = GdipAlloc(stride * abs_height);
151 bmi.bmiHeader.biBitCount = bitspp;
153 if(buff)
154 GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, &bmi, DIB_RGB_COLORS);
156 if(!bm_is_selected){
157 SelectObject(hdc, old);
158 DeleteDC(hdc);
161 if(!buff)
162 return OutOfMemory;
164 lockeddata->Width = act_rect.Width;
165 lockeddata->Height = act_rect.Height;
166 lockeddata->PixelFormat = format;
167 lockeddata->Reserved = flags;
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);
174 else{
175 lockeddata->Stride = stride;
176 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
179 bitmap->lockmode = flags;
180 bitmap->numlocks++;
182 bitmap->bitmapbits = buff;
184 return Ok;
187 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
188 BitmapData* lockeddata)
190 OLE_HANDLE hbm;
191 HDC hdc;
192 HBITMAP old = NULL;
193 BOOL bm_is_selected;
194 BITMAPINFO bmi;
196 if(!bitmap || !lockeddata)
197 return InvalidParameter;
199 if(!bitmap->lockmode)
200 return WrongState;
202 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
203 return NotImplemented;
205 if(lockeddata->Reserved & ImageLockModeRead){
206 if(!(--bitmap->numlocks))
207 bitmap->lockmode = 0;
209 GdipFree(bitmap->bitmapbits);
210 bitmap->bitmapbits = NULL;
211 return Ok;
214 IPicture_get_Handle(bitmap->image.picture, &hbm);
215 IPicture_get_CurDC(bitmap->image.picture, &hdc);
216 bm_is_selected = (hdc != 0);
218 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
219 bmi.bmiHeader.biBitCount = 0;
221 if(!bm_is_selected){
222 hdc = CreateCompatibleDC(0);
223 old = SelectObject(hdc, (HBITMAP)hbm);
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);
231 if(!bm_is_selected){
232 SelectObject(hdc, old);
233 DeleteDC(hdc);
236 GdipFree(bitmap->bitmapbits);
237 bitmap->bitmapbits = NULL;
238 bitmap->lockmode = 0;
240 return Ok;
243 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
244 GpBitmap **bitmap)
246 GpStatus stat;
247 IStream *stream;
249 if(!filename || !bitmap)
250 return InvalidParameter;
252 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
254 if(stat != Ok)
255 return stat;
257 stat = GdipCreateBitmapFromStream(stream, bitmap);
259 IStream_Release(stream);
261 return stat;
264 /* FIXME: no icm */
265 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
266 GpBitmap **bitmap)
268 return GdipCreateBitmapFromFile(filename, bitmap);
271 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
272 HBITMAP* hbmReturn, ARGB background)
274 FIXME("stub\n");
276 hbmReturn = NULL;
278 return NotImplemented;
281 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
282 GpMetafile* metafile, BOOL* succ, EmfType emfType,
283 const WCHAR* description, GpMetafile** out_metafile)
285 static int calls;
287 if(!ref || !metafile || !out_metafile)
288 return InvalidParameter;
290 *succ = FALSE;
291 *out_metafile = NULL;
293 if(!(calls++))
294 FIXME("not implemented\n");
296 return NotImplemented;
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)
304 static int calls;
305 GpStatus ret;
307 if(!target || !bitmap)
308 return InvalidParameter;
310 if(!(calls++))
311 FIXME("hacked stub\n");
313 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
314 NULL, bitmap);
316 return ret;
319 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
320 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
322 BITMAPFILEHEADER *bmfh;
323 BITMAPINFOHEADER *bmih;
324 BYTE *buff;
325 INT datalen, size;
326 IStream *stream;
328 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
330 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
331 *bitmap = NULL;
332 return InvalidParameter;
335 if(scan0 && !stride)
336 return InvalidParameter;
338 /* FIXME: windows allows negative stride (reads backwards from scan0) */
339 if(stride < 0){
340 FIXME("negative stride\n");
341 return InvalidParameter;
344 *bitmap = GdipAlloc(sizeof(GpBitmap));
345 if(!*bitmap) return OutOfMemory;
347 if(stride == 0){
348 stride = width * (PIXELFORMATBPP(format) / 8);
349 stride = (stride + 3) & ~3;
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;
360 bmfh = (BITMAPFILEHEADER*) buff;
361 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
363 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
364 bmfh->bfSize = size;
365 bmfh->bfOffBits = size - datalen;
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;
375 if(scan0)
376 memcpy(bmih + 1, scan0, datalen);
377 else
378 memset(bmih + 1, 0, datalen);
380 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
381 ERR("could not make stream\n");
382 GdipFree(*bitmap);
383 GdipFree(buff);
384 return GenericError;
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;
396 (*bitmap)->image.type = ImageTypeBitmap;
397 (*bitmap)->image.flags = ImageFlagsNone;
398 (*bitmap)->width = width;
399 (*bitmap)->height = height;
400 (*bitmap)->format = format;
402 return Ok;
405 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
406 GpBitmap **bitmap)
408 GpStatus stat;
410 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
412 if(stat != Ok)
413 return stat;
415 if((*bitmap)->image.type != ImageTypeBitmap){
416 IPicture_Release((*bitmap)->image.picture);
417 GdipFree(bitmap);
418 return GenericError; /* FIXME: what error to return? */
421 return Ok;
424 /* FIXME: no icm */
425 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
426 GpBitmap **bitmap)
428 return GdipCreateBitmapFromStream(stream, bitmap);
431 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
433 HDC hdc;
435 if(!image)
436 return InvalidParameter;
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);
445 return Ok;
448 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
450 if(!image || !item)
451 return InvalidParameter;
453 return NotImplemented;
456 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
457 GpUnit *srcUnit)
459 if(!image || !srcRect || !srcUnit)
460 return InvalidParameter;
461 if(image->type == ImageTypeMetafile){
462 *srcRect = ((GpMetafile*)image)->bounds;
463 *srcUnit = ((GpMetafile*)image)->unit;
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;
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;
478 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
479 srcRect->Width, srcRect->Height, *srcUnit);
481 return Ok;
484 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
485 REAL *height)
487 if(!image || !height || !width)
488 return InvalidParameter;
490 if(image->type == ImageTypeMetafile){
491 HDC hdc = GetDC(0);
493 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
494 ((GpMetafile*)image)->bounds.Height;
496 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
497 ((GpMetafile*)image)->bounds.Width;
499 ReleaseDC(0, hdc);
502 else if(image->type == ImageTypeBitmap){
503 *height = ((GpBitmap*)image)->height;
504 *width = ((GpBitmap*)image)->width;
506 else{
507 *height = ipicture_pixel_height(image->picture);
508 *width = ipicture_pixel_width(image->picture);
511 TRACE("returning (%f, %f)\n", *height, *width);
512 return Ok;
515 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
516 GpGraphics **graphics)
518 HDC hdc;
520 if(!image || !graphics)
521 return InvalidParameter;
523 if(image->type != ImageTypeBitmap){
524 FIXME("not implemented for image type %d\n", image->type);
525 return NotImplemented;
528 IPicture_get_CurDC(image->picture, &hdc);
530 if(!hdc){
531 hdc = CreateCompatibleDC(0);
532 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
535 return GdipCreateFromHDC(hdc, graphics);
538 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
540 if(!image || !height)
541 return InvalidParameter;
543 if(image->type == ImageTypeMetafile){
544 HDC hdc = GetDC(0);
546 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
547 ((GpMetafile*)image)->bounds.Height);
549 ReleaseDC(0, hdc);
551 else if(image->type == ImageTypeBitmap)
552 *height = ((GpBitmap*)image)->height;
553 else
554 *height = ipicture_pixel_height(image->picture);
556 TRACE("returning %d\n", *height);
558 return Ok;
561 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
563 static int calls;
565 if(!image || !res)
566 return InvalidParameter;
568 if(!(calls++))
569 FIXME("not implemented\n");
571 return NotImplemented;
574 /* FIXME: test this function for non-bitmap types */
575 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
577 if(!image || !format)
578 return InvalidParameter;
580 if(image->type != ImageTypeBitmap)
581 *format = PixelFormat24bppRGB;
582 else
583 *format = ((GpBitmap*) image)->format;
585 return Ok;
588 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
590 static int calls;
592 if(!image || !format)
593 return InvalidParameter;
595 if(!(calls++))
596 FIXME("not implemented\n");
598 return NotImplemented;
601 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
603 if(!image || !type)
604 return InvalidParameter;
606 *type = image->type;
608 return Ok;
611 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
613 static int calls;
615 if(!image || !res)
616 return InvalidParameter;
618 if(!(calls++))
619 FIXME("not implemented\n");
621 return NotImplemented;
624 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
626 if(!image || !width)
627 return InvalidParameter;
629 if(image->type == ImageTypeMetafile){
630 HDC hdc = GetDC(0);
632 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
633 ((GpMetafile*)image)->bounds.Width);
635 ReleaseDC(0, hdc);
637 else if(image->type == ImageTypeBitmap)
638 *width = ((GpBitmap*)image)->width;
639 else
640 *width = ipicture_pixel_width(image->picture);
642 TRACE("returning %d\n", *width);
644 return Ok;
647 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
648 MetafileHeader * header)
650 static int calls;
652 if(!metafile || !header)
653 return InvalidParameter;
655 if(!(calls++))
656 FIXME("not implemented\n");
658 return Ok;
661 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
662 UINT* size)
664 static int calls;
666 TRACE("%p %x %p\n", image, pid, size);
668 if(!size || !image)
669 return InvalidParameter;
671 if(!(calls++))
672 FIXME("not implemented\n");
674 return NotImplemented;
677 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
678 GDIPCONST GUID* dimensionID, UINT* count)
680 static int calls;
682 if(!image || !dimensionID || !count)
683 return InvalidParameter;
685 if(!(calls++))
686 FIXME("not implemented\n");
688 return NotImplemented;
691 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
692 UINT* count)
694 if(!image || !count)
695 return InvalidParameter;
697 *count = 1;
699 FIXME("stub\n");
701 return Ok;
704 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
705 GUID* dimensionIDs, UINT count)
707 static int calls;
709 if(!image || !dimensionIDs)
710 return InvalidParameter;
712 if(!(calls++))
713 FIXME("not implemented\n");
715 return Ok;
718 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
719 GDIPCONST GUID* dimensionID, UINT frameidx)
721 static int calls;
723 if(!image || !dimensionID)
724 return InvalidParameter;
726 if(!(calls++))
727 FIXME("not implemented\n");
729 return Ok;
732 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
733 GpImage **image)
735 GpStatus stat;
736 IStream *stream;
738 if (!filename || !image)
739 return InvalidParameter;
741 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
743 if (stat != Ok)
744 return stat;
746 stat = GdipLoadImageFromStream(stream, image);
748 IStream_Release(stream);
750 return stat;
753 /* FIXME: no icm handling */
754 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
756 return GdipLoadImageFromFile(filename, image);
759 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
761 IPicture *pic;
762 short type;
764 if(!stream || !image)
765 return InvalidParameter;
767 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
768 (LPVOID*) &pic) != S_OK){
769 TRACE("Could not load picture\n");
770 return GenericError;
773 IPicture_get_Type(pic, &type);
775 if(type == PICTYPE_BITMAP){
776 BITMAPINFO bmi;
777 BITMAPCOREHEADER* bmch;
778 OLE_HANDLE hbm;
779 HDC hdc;
781 *image = GdipAlloc(sizeof(GpBitmap));
782 if(!*image) return OutOfMemory;
783 (*image)->type = ImageTypeBitmap;
785 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
786 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
788 /* get the pixel format */
789 IPicture_get_Handle(pic, &hbm);
790 IPicture_get_CurDC(pic, &hdc);
792 ZeroMemory(&bmi, sizeof(bmi));
793 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
794 bmch->bcSize = sizeof(BITMAPCOREHEADER);
796 if(!hdc){
797 HBITMAP old;
798 hdc = CreateCompatibleDC(0);
799 old = SelectObject(hdc, (HBITMAP)hbm);
800 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
801 SelectObject(hdc, old);
802 DeleteDC(hdc);
804 else
805 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
807 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
809 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
810 /* FIXME: missing initialization code */
811 *image = GdipAlloc(sizeof(GpMetafile));
812 if(!*image) return OutOfMemory;
813 (*image)->type = ImageTypeMetafile;
815 else{
816 *image = GdipAlloc(sizeof(GpImage));
817 if(!*image) return OutOfMemory;
818 (*image)->type = ImageTypeUnknown;
821 (*image)->picture = pic;
822 (*image)->flags = ImageFlagsNone;
824 return Ok;
827 /* FIXME: no ICM */
828 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
830 return GdipLoadImageFromStream(stream, image);
833 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
835 static int calls;
837 if(!image)
838 return InvalidParameter;
840 if(!(calls++))
841 FIXME("not implemented\n");
843 return NotImplemented;
846 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
847 GDIPCONST CLSID *clsidEncoder,
848 GDIPCONST EncoderParameters *encoderParams)
850 GpStatus stat;
851 IStream *stream;
853 if (!image || !filename|| !clsidEncoder)
854 return InvalidParameter;
856 if (!(image->picture))
857 return InvalidParameter;
859 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
860 if (stat != Ok)
861 return GenericError;
863 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
865 IStream_Release(stream);
866 return stat;
869 /*************************************************************************
870 * Encoding functions -
871 * These functions encode an image in different image file formats.
873 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
874 #define BITMAP_FORMAT_JPEG 0xd8ff
875 #define BITMAP_FORMAT_GIF 0x4947
876 #define BITMAP_FORMAT_PNG 0x5089
877 #define BITMAP_FORMAT_APM 0xcdd7
879 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
880 void **output, unsigned int *output_size)
882 int num_palette_entries;
883 BITMAPFILEHEADER *bmp_file_hdr;
884 BITMAPINFO *bmp_info_hdr;
886 if (bitmap_info->bmiHeader.biClrUsed) {
887 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
888 if (num_palette_entries > 256) num_palette_entries = 256;
889 } else {
890 if (bitmap_info->bmiHeader.biBitCount <= 8)
891 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
892 else
893 num_palette_entries = 0;
896 *output_size =
897 sizeof(BITMAPFILEHEADER) +
898 sizeof(BITMAPINFOHEADER) +
899 num_palette_entries * sizeof(RGBQUAD) +
900 bitmap_info->bmiHeader.biSizeImage;
902 *output = GdipAlloc(*output_size);
904 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
905 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
906 bmp_file_hdr->bfSize = *output_size;
907 bmp_file_hdr->bfOffBits =
908 sizeof(BITMAPFILEHEADER) +
909 sizeof(BITMAPINFOHEADER) +
910 num_palette_entries * sizeof (RGBQUAD);
912 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
913 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
914 memcpy((unsigned char *)(*output) +
915 sizeof(BITMAPFILEHEADER) +
916 sizeof(BITMAPINFOHEADER) +
917 num_palette_entries * sizeof(RGBQUAD),
918 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
920 return Ok;
923 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
924 void **output, unsigned int *output_size);
926 typedef enum {
927 BMP,
928 NUM_ENCODERS_SUPPORTED
929 } ImageFormat;
931 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
932 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
933 encode_image_BMP,
936 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
937 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
939 GpStatus stat;
940 HRESULT hr;
941 short type;
942 HBITMAP hbmp;
943 HBITMAP old_hbmp;
944 HDC hdc;
945 int bm_is_selected;
946 BITMAPINFO bmp_info;
947 LPVOID bmp_bits;
948 encode_image_func* encode_image;
949 LPVOID output;
950 unsigned int output_size;
951 unsigned int dummy;
952 int i;
954 old_hbmp = 0;
955 output = NULL;
956 output_size = 0;
958 if(!image || !stream)
959 return InvalidParameter;
961 if (!image->picture)
962 return GenericError;
964 hr = IPicture_get_Type(image->picture, &type);
965 if (FAILED(hr) || type != PICTYPE_BITMAP)
966 return GenericError;
968 /* select correct encoder */
969 encode_image = NULL;
970 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
971 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
972 encode_image = encode_image_funcs[i];
974 if (encode_image == NULL)
975 return UnknownImageFormat;
977 /* extract underlying hbitmap representation from the IPicture */
978 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
979 if (FAILED(hr) || !hbmp)
980 return GenericError;
981 hr = IPicture_get_CurDC(image->picture, &hdc);
982 if (FAILED(hr))
983 return GenericError;
984 bm_is_selected = (hdc != 0);
985 if (!bm_is_selected) {
986 hdc = CreateCompatibleDC(0);
987 old_hbmp = SelectObject(hdc, hbmp);
990 /* get bits from HBITMAP */
991 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
992 bmp_info.bmiHeader.biBitCount = 0;
993 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
995 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
997 if (bmp_bits)
998 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1000 if (!bm_is_selected) {
1001 SelectObject(hdc, old_hbmp);
1002 DeleteDC(hdc);
1005 if (!bmp_bits)
1006 return OutOfMemory;
1008 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1009 if (stat == Ok)
1010 IStream_Write(stream, output, output_size, &dummy);
1012 GdipFree(output);
1013 GdipFree(bmp_bits);
1015 return stat;
1018 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1019 GDIPCONST ColorPalette *palette)
1021 static int calls;
1023 if(!image || !palette)
1024 return InvalidParameter;
1026 if(!(calls++))
1027 FIXME("not implemented\n");
1029 return NotImplemented;
1032 /*************************************************************************
1033 * Encoders -
1034 * Structures that represent which formats we support for encoding.
1037 /* ImageCodecInfo creation routines taken from libgdiplus */
1038 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1039 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1040 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1041 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1042 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1043 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1045 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1047 { /* BMP */
1048 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1049 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1050 /* CodecName */ bmp_codecname,
1051 /* DllName */ NULL,
1052 /* FormatDescription */ bmp_format,
1053 /* FilenameExtension */ bmp_extension,
1054 /* MimeType */ bmp_mimetype,
1055 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1056 /* Version */ 1,
1057 /* SigCount */ 1,
1058 /* SigSize */ 2,
1059 /* SigPattern */ bmp_sig_pattern,
1060 /* SigMask */ bmp_sig_mask,
1064 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1066 if (!numEncoders || !size)
1067 return InvalidParameter;
1069 *numEncoders = NUM_ENCODERS_SUPPORTED;
1070 *size = sizeof (codecs);
1072 return Ok;
1075 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1077 if (!encoders ||
1078 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1079 (size != sizeof (codecs)))
1080 return GenericError;
1082 memcpy(encoders, codecs, sizeof (codecs));
1084 return Ok;
1086 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1088 BITMAP bm;
1089 GpStatus retval;
1090 PixelFormat format;
1092 if(!hbm || !bitmap)
1093 return InvalidParameter;
1095 /* TODO: Support for device-dependent bitmaps */
1096 if(hpal){
1097 FIXME("no support for device-dependent bitmaps\n");
1098 return NotImplemented;
1101 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1102 return InvalidParameter;
1104 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1105 switch(bm.bmBitsPixel) {
1106 case 1:
1107 format = PixelFormat1bppIndexed;
1108 break;
1109 case 4:
1110 format = PixelFormat4bppIndexed;
1111 break;
1112 case 8:
1113 format = PixelFormat8bppIndexed;
1114 break;
1115 case 24:
1116 format = PixelFormat24bppRGB;
1117 break;
1118 case 48:
1119 format = PixelFormat48bppRGB;
1120 break;
1121 default:
1122 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1123 return InvalidParameter;
1126 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1127 format, bm.bmBits, bitmap);
1129 return retval;
1132 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1133 const VOID *params, const UINT size)
1135 static int calls;
1137 if(!(calls++))
1138 FIXME("not implemented\n");
1140 return NotImplemented;
1143 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1145 if(!image || !flags)
1146 return InvalidParameter;
1148 *flags = image->flags;
1150 return Ok;