wine.inf: Open xml files using winebrowser.
[wine/multimedia.git] / dlls / gdiplus / image.c
blob72bf7f509bf86d105fee67647b0ae2fd67af35e9
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;
105 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
107 if(!lockeddata || !bitmap || !rect)
108 return InvalidParameter;
110 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
111 (rect->Y + rect->Height > bitmap->height) || !flags)
112 return InvalidParameter;
114 if(flags & ImageLockModeUserInputBuf)
115 return NotImplemented;
117 if(bitmap->lockmode)
118 return WrongState;
120 IPicture_get_Handle(bitmap->image.picture, &hbm);
121 IPicture_get_CurDC(bitmap->image.picture, &hdc);
122 bm_is_selected = (hdc != 0);
124 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
125 bmi.bmiHeader.biBitCount = 0;
127 if(!bm_is_selected){
128 hdc = CreateCompatibleDC(0);
129 old = SelectObject(hdc, (HBITMAP)hbm);
132 /* fill out bmi */
133 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
135 abs_height = abs(bmi.bmiHeader.biHeight);
136 stride = bmi.bmiHeader.biWidth * bitspp / 8;
137 stride = (stride + 3) & ~3;
139 buff = GdipAlloc(stride * abs_height);
141 bmi.bmiHeader.biBitCount = bitspp;
143 if(buff)
144 GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, &bmi, DIB_RGB_COLORS);
146 if(!bm_is_selected){
147 SelectObject(hdc, old);
148 DeleteDC(hdc);
151 if(!buff)
152 return OutOfMemory;
154 lockeddata->Width = rect->Width;
155 lockeddata->Height = rect->Height;
156 lockeddata->PixelFormat = format;
157 lockeddata->Reserved = flags;
159 if(bmi.bmiHeader.biHeight > 0){
160 lockeddata->Stride = -stride;
161 lockeddata->Scan0 = buff + (bitspp / 8) * rect->X +
162 stride * (abs_height - 1 - rect->Y);
164 else{
165 lockeddata->Stride = stride;
166 lockeddata->Scan0 = buff + (bitspp / 8) * rect->X + stride * rect->Y;
169 bitmap->lockmode = flags;
170 bitmap->numlocks++;
172 bitmap->bitmapbits = buff;
174 return Ok;
177 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
178 BitmapData* lockeddata)
180 OLE_HANDLE hbm;
181 HDC hdc;
182 HBITMAP old = NULL;
183 BOOL bm_is_selected;
184 BITMAPINFO bmi;
186 if(!bitmap || !lockeddata)
187 return InvalidParameter;
189 if(!bitmap->lockmode)
190 return WrongState;
192 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
193 return NotImplemented;
195 if(lockeddata->Reserved & ImageLockModeRead){
196 if(!(--bitmap->numlocks))
197 bitmap->lockmode = 0;
199 GdipFree(bitmap->bitmapbits);
200 bitmap->bitmapbits = NULL;
201 return Ok;
204 IPicture_get_Handle(bitmap->image.picture, &hbm);
205 IPicture_get_CurDC(bitmap->image.picture, &hdc);
206 bm_is_selected = (hdc != 0);
208 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
209 bmi.bmiHeader.biBitCount = 0;
211 if(!bm_is_selected){
212 hdc = CreateCompatibleDC(0);
213 old = SelectObject(hdc, (HBITMAP)hbm);
216 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
217 bmi.bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
218 SetDIBits(hdc, (HBITMAP)hbm, 0, abs(bmi.bmiHeader.biHeight),
219 bitmap->bitmapbits, &bmi, DIB_RGB_COLORS);
221 if(!bm_is_selected){
222 SelectObject(hdc, old);
223 DeleteDC(hdc);
226 GdipFree(bitmap->bitmapbits);
227 bitmap->bitmapbits = NULL;
228 bitmap->lockmode = 0;
230 return Ok;
233 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
234 GpBitmap **bitmap)
236 GpStatus stat;
237 IStream *stream;
239 if(!filename || !bitmap)
240 return InvalidParameter;
242 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
244 if(stat != Ok)
245 return stat;
247 stat = GdipCreateBitmapFromStream(stream, bitmap);
249 IStream_Release(stream);
251 return stat;
254 /* FIXME: no icm */
255 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
256 GpBitmap **bitmap)
258 return GdipCreateBitmapFromFile(filename, bitmap);
261 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
262 HBITMAP* hbmReturn, ARGB background)
264 FIXME("stub\n");
266 hbmReturn = NULL;
268 return NotImplemented;
271 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
272 GpMetafile* metafile, BOOL* succ, EmfType emfType,
273 const WCHAR* description, GpMetafile** out_metafile)
275 static int calls;
277 if(!ref || !metafile || !out_metafile)
278 return InvalidParameter;
280 *succ = FALSE;
281 *out_metafile = NULL;
283 if(!(calls++))
284 FIXME("not implemented\n");
286 return NotImplemented;
289 /* FIXME: this should create a bitmap in the given size with the attributes
290 * (resolution etc.) of the graphics object */
291 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
292 GpGraphics* target, GpBitmap** bitmap)
294 static int calls;
295 GpStatus ret;
297 if(!target || !bitmap)
298 return InvalidParameter;
300 if(!(calls++))
301 FIXME("hacked stub\n");
303 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
304 NULL, bitmap);
306 return ret;
309 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
310 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
312 BITMAPFILEHEADER *bmfh;
313 BITMAPINFOHEADER *bmih;
314 BYTE *buff;
315 INT datalen, size;
316 IStream *stream;
318 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
320 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
321 *bitmap = NULL;
322 return InvalidParameter;
325 if(scan0 && !stride)
326 return InvalidParameter;
328 /* FIXME: windows allows negative stride (reads backwards from scan0) */
329 if(stride < 0){
330 FIXME("negative stride\n");
331 return InvalidParameter;
334 *bitmap = GdipAlloc(sizeof(GpBitmap));
335 if(!*bitmap) return OutOfMemory;
337 if(stride == 0){
338 stride = width * (PIXELFORMATBPP(format) / 8);
339 stride = (stride + 3) & ~3;
342 datalen = abs(stride * height);
343 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
344 buff = GdipAlloc(size);
345 if(!buff){
346 GdipFree(*bitmap);
347 return OutOfMemory;
350 bmfh = (BITMAPFILEHEADER*) buff;
351 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
353 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
354 bmfh->bfSize = size;
355 bmfh->bfOffBits = size - datalen;
357 bmih->biSize = sizeof(BITMAPINFOHEADER);
358 bmih->biWidth = width;
359 bmih->biHeight = -height;
360 /* FIXME: use the rest of the data from format */
361 bmih->biBitCount = PIXELFORMATBPP(format);
362 bmih->biCompression = BI_RGB;
363 bmih->biSizeImage = datalen;
365 if(scan0)
366 memcpy(bmih + 1, scan0, datalen);
367 else
368 memset(bmih + 1, 0, datalen);
370 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
371 ERR("could not make stream\n");
372 GdipFree(*bitmap);
373 GdipFree(buff);
374 return GenericError;
377 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
378 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
379 TRACE("Could not load picture\n");
380 IStream_Release(stream);
381 GdipFree(*bitmap);
382 GdipFree(buff);
383 return GenericError;
386 (*bitmap)->image.type = ImageTypeBitmap;
387 (*bitmap)->image.flags = ImageFlagsNone;
388 (*bitmap)->width = width;
389 (*bitmap)->height = height;
390 (*bitmap)->format = format;
392 return Ok;
395 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
396 GpBitmap **bitmap)
398 GpStatus stat;
400 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
402 if(stat != Ok)
403 return stat;
405 if((*bitmap)->image.type != ImageTypeBitmap){
406 IPicture_Release((*bitmap)->image.picture);
407 GdipFree(bitmap);
408 return GenericError; /* FIXME: what error to return? */
411 return Ok;
414 /* FIXME: no icm */
415 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
416 GpBitmap **bitmap)
418 return GdipCreateBitmapFromStream(stream, bitmap);
421 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
423 HDC hdc;
425 if(!image)
426 return InvalidParameter;
428 IPicture_get_CurDC(image->picture, &hdc);
429 DeleteDC(hdc);
430 IPicture_Release(image->picture);
431 if (image->type == ImageTypeBitmap)
432 GdipFree(((GpBitmap*)image)->bitmapbits);
433 GdipFree(image);
435 return Ok;
438 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
440 if(!image || !item)
441 return InvalidParameter;
443 return NotImplemented;
446 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
447 GpUnit *srcUnit)
449 if(!image || !srcRect || !srcUnit)
450 return InvalidParameter;
451 if(image->type == ImageTypeMetafile){
452 *srcRect = ((GpMetafile*)image)->bounds;
453 *srcUnit = ((GpMetafile*)image)->unit;
455 else if(image->type == ImageTypeBitmap){
456 srcRect->X = srcRect->Y = 0.0;
457 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
458 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
459 *srcUnit = UnitPixel;
461 else{
462 srcRect->X = srcRect->Y = 0.0;
463 srcRect->Width = ipicture_pixel_width(image->picture);
464 srcRect->Height = ipicture_pixel_height(image->picture);
465 *srcUnit = UnitPixel;
468 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
469 srcRect->Width, srcRect->Height, *srcUnit);
471 return Ok;
474 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
475 REAL *height)
477 if(!image || !height || !width)
478 return InvalidParameter;
480 if(image->type == ImageTypeMetafile){
481 HDC hdc = GetDC(0);
483 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
484 ((GpMetafile*)image)->bounds.Height;
486 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
487 ((GpMetafile*)image)->bounds.Width;
489 ReleaseDC(0, hdc);
492 else if(image->type == ImageTypeBitmap){
493 *height = ((GpBitmap*)image)->height;
494 *width = ((GpBitmap*)image)->width;
496 else{
497 *height = ipicture_pixel_height(image->picture);
498 *width = ipicture_pixel_width(image->picture);
501 TRACE("returning (%f, %f)\n", *height, *width);
502 return Ok;
505 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
506 GpGraphics **graphics)
508 HDC hdc;
510 if(!image || !graphics)
511 return InvalidParameter;
513 if(image->type != ImageTypeBitmap){
514 FIXME("not implemented for image type %d\n", image->type);
515 return NotImplemented;
518 IPicture_get_CurDC(image->picture, &hdc);
520 if(!hdc){
521 hdc = CreateCompatibleDC(0);
522 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
525 return GdipCreateFromHDC(hdc, graphics);
528 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
530 if(!image || !height)
531 return InvalidParameter;
533 if(image->type == ImageTypeMetafile){
534 HDC hdc = GetDC(0);
536 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
537 ((GpMetafile*)image)->bounds.Height);
539 ReleaseDC(0, hdc);
541 else if(image->type == ImageTypeBitmap)
542 *height = ((GpBitmap*)image)->height;
543 else
544 *height = ipicture_pixel_height(image->picture);
546 TRACE("returning %d\n", *height);
548 return Ok;
551 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
553 static int calls;
555 if(!image || !res)
556 return InvalidParameter;
558 if(!(calls++))
559 FIXME("not implemented\n");
561 return NotImplemented;
564 /* FIXME: test this function for non-bitmap types */
565 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
567 if(!image || !format)
568 return InvalidParameter;
570 if(image->type != ImageTypeBitmap)
571 *format = PixelFormat24bppRGB;
572 else
573 *format = ((GpBitmap*) image)->format;
575 return Ok;
578 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
580 static int calls;
582 if(!image || !format)
583 return InvalidParameter;
585 if(!(calls++))
586 FIXME("not implemented\n");
588 return NotImplemented;
591 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
593 if(!image || !type)
594 return InvalidParameter;
596 *type = image->type;
598 return Ok;
601 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
603 static int calls;
605 if(!image || !res)
606 return InvalidParameter;
608 if(!(calls++))
609 FIXME("not implemented\n");
611 return NotImplemented;
614 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
616 if(!image || !width)
617 return InvalidParameter;
619 if(image->type == ImageTypeMetafile){
620 HDC hdc = GetDC(0);
622 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
623 ((GpMetafile*)image)->bounds.Width);
625 ReleaseDC(0, hdc);
627 else if(image->type == ImageTypeBitmap)
628 *width = ((GpBitmap*)image)->width;
629 else
630 *width = ipicture_pixel_width(image->picture);
632 TRACE("returning %d\n", *width);
634 return Ok;
637 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
638 MetafileHeader * header)
640 static int calls;
642 if(!metafile || !header)
643 return InvalidParameter;
645 if(!(calls++))
646 FIXME("not implemented\n");
648 return Ok;
651 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
652 UINT* size)
654 static int calls;
656 TRACE("%p %x %p\n", image, pid, size);
658 if(!size || !image)
659 return InvalidParameter;
661 if(!(calls++))
662 FIXME("not implemented\n");
664 return NotImplemented;
667 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
668 GDIPCONST GUID* dimensionID, UINT* count)
670 static int calls;
672 if(!image || !dimensionID || !count)
673 return InvalidParameter;
675 if(!(calls++))
676 FIXME("not implemented\n");
678 return NotImplemented;
681 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
682 GUID* dimensionIDs, UINT count)
684 static int calls;
686 if(!image || !dimensionIDs)
687 return InvalidParameter;
689 if(!(calls++))
690 FIXME("not implemented\n");
692 return Ok;
695 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
696 GDIPCONST GUID* dimensionID, UINT frameidx)
698 static int calls;
700 if(!image || !dimensionID)
701 return InvalidParameter;
703 if(!(calls++))
704 FIXME("not implemented\n");
706 return Ok;
709 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
710 GpImage **image)
712 GpStatus stat;
713 IStream *stream;
715 if (!filename || !image)
716 return InvalidParameter;
718 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
720 if (stat != Ok)
721 return stat;
723 stat = GdipLoadImageFromStream(stream, image);
725 IStream_Release(stream);
727 return stat;
730 /* FIXME: no icm handling */
731 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
733 return GdipLoadImageFromFile(filename, image);
736 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
738 IPicture *pic;
739 short type;
741 if(!stream || !image)
742 return InvalidParameter;
744 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
745 (LPVOID*) &pic) != S_OK){
746 TRACE("Could not load picture\n");
747 return GenericError;
750 IPicture_get_Type(pic, &type);
752 if(type == PICTYPE_BITMAP){
753 BITMAPINFO bmi;
754 BITMAPCOREHEADER* bmch;
755 OLE_HANDLE hbm;
756 HDC hdc;
758 *image = GdipAlloc(sizeof(GpBitmap));
759 if(!*image) return OutOfMemory;
760 (*image)->type = ImageTypeBitmap;
762 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
763 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
765 /* get the pixel format */
766 IPicture_get_Handle(pic, &hbm);
767 IPicture_get_CurDC(pic, &hdc);
769 ZeroMemory(&bmi, sizeof(bmi));
770 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
771 bmch->bcSize = sizeof(BITMAPCOREHEADER);
773 if(!hdc){
774 HBITMAP old;
775 hdc = CreateCompatibleDC(0);
776 old = SelectObject(hdc, (HBITMAP)hbm);
777 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
778 SelectObject(hdc, old);
779 DeleteDC(hdc);
781 else
782 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
784 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
786 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
787 /* FIXME: missing initialization code */
788 *image = GdipAlloc(sizeof(GpMetafile));
789 if(!*image) return OutOfMemory;
790 (*image)->type = ImageTypeMetafile;
792 else{
793 *image = GdipAlloc(sizeof(GpImage));
794 if(!*image) return OutOfMemory;
795 (*image)->type = ImageTypeUnknown;
798 (*image)->picture = pic;
799 (*image)->flags = ImageFlagsNone;
801 return Ok;
804 /* FIXME: no ICM */
805 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
807 return GdipLoadImageFromStream(stream, image);
810 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
812 static int calls;
814 if(!image)
815 return InvalidParameter;
817 if(!(calls++))
818 FIXME("not implemented\n");
820 return NotImplemented;
823 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
824 GDIPCONST CLSID *clsidEncoder,
825 GDIPCONST EncoderParameters *encoderParams)
827 GpStatus stat;
828 IStream *stream;
830 if (!image || !filename|| !clsidEncoder)
831 return InvalidParameter;
833 if (!(image->picture))
834 return InvalidParameter;
836 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
837 if (stat != Ok)
838 return GenericError;
840 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
842 IStream_Release(stream);
843 return stat;
846 /*************************************************************************
847 * Encoding functions -
848 * These functions encode an image in different image file formats.
850 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
851 #define BITMAP_FORMAT_JPEG 0xd8ff
852 #define BITMAP_FORMAT_GIF 0x4947
853 #define BITMAP_FORMAT_PNG 0x5089
854 #define BITMAP_FORMAT_APM 0xcdd7
856 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
857 void **output, unsigned int *output_size)
859 int num_palette_entries;
860 BITMAPFILEHEADER *bmp_file_hdr;
861 BITMAPINFO *bmp_info_hdr;
863 if (bitmap_info->bmiHeader.biClrUsed) {
864 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
865 if (num_palette_entries > 256) num_palette_entries = 256;
866 } else {
867 if (bitmap_info->bmiHeader.biBitCount <= 8)
868 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
869 else
870 num_palette_entries = 0;
873 *output_size =
874 sizeof(BITMAPFILEHEADER) +
875 sizeof(BITMAPINFOHEADER) +
876 num_palette_entries * sizeof(RGBQUAD) +
877 bitmap_info->bmiHeader.biSizeImage;
879 *output = GdipAlloc(*output_size);
881 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
882 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
883 bmp_file_hdr->bfSize = *output_size;
884 bmp_file_hdr->bfOffBits =
885 sizeof(BITMAPFILEHEADER) +
886 sizeof(BITMAPINFOHEADER) +
887 num_palette_entries * sizeof (RGBQUAD);
889 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
890 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
891 memcpy((unsigned char *)(*output) +
892 sizeof(BITMAPFILEHEADER) +
893 sizeof(BITMAPINFOHEADER) +
894 num_palette_entries * sizeof(RGBQUAD),
895 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
897 return Ok;
900 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
901 void **output, unsigned int *output_size);
903 typedef enum {
904 BMP,
905 NUM_ENCODERS_SUPPORTED
906 } ImageFormat;
908 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
909 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
910 encode_image_BMP,
913 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
914 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
916 GpStatus stat;
917 HRESULT hr;
918 short type;
919 HBITMAP hbmp;
920 HBITMAP old_hbmp;
921 HDC hdc;
922 int bm_is_selected;
923 BITMAPINFO bmp_info;
924 LPVOID bmp_bits;
925 encode_image_func* encode_image;
926 LPVOID output;
927 unsigned int output_size;
928 unsigned int dummy;
929 int i;
931 old_hbmp = 0;
932 output = NULL;
933 output_size = 0;
935 if(!image || !stream)
936 return InvalidParameter;
938 if (!image->picture)
939 return GenericError;
941 hr = IPicture_get_Type(image->picture, &type);
942 if (FAILED(hr) || type != PICTYPE_BITMAP)
943 return GenericError;
945 /* select correct encoder */
946 encode_image = NULL;
947 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
948 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
949 encode_image = encode_image_funcs[i];
951 if (encode_image == NULL)
952 return UnknownImageFormat;
954 /* extract underlying hbitmap representation from the IPicture */
955 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
956 if (FAILED(hr) || !hbmp)
957 return GenericError;
958 hr = IPicture_get_CurDC(image->picture, &hdc);
959 if (FAILED(hr))
960 return GenericError;
961 bm_is_selected = (hdc != 0);
962 if (!bm_is_selected) {
963 hdc = CreateCompatibleDC(0);
964 old_hbmp = SelectObject(hdc, hbmp);
967 /* get bits from HBITMAP */
968 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
969 bmp_info.bmiHeader.biBitCount = 0;
970 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
972 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
974 if (bmp_bits)
975 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
977 if (!bm_is_selected) {
978 SelectObject(hdc, old_hbmp);
979 DeleteDC(hdc);
982 if (!bmp_bits)
983 return OutOfMemory;
985 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
986 if (stat == Ok)
987 IStream_Write(stream, output, output_size, &dummy);
989 GdipFree(output);
990 GdipFree(bmp_bits);
992 return stat;
995 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
996 GDIPCONST ColorPalette *palette)
998 static int calls;
1000 if(!image || !palette)
1001 return InvalidParameter;
1003 if(!(calls++))
1004 FIXME("not implemented\n");
1006 return NotImplemented;
1009 /*************************************************************************
1010 * Encoders -
1011 * Structures that represent which formats we support for encoding.
1014 /* ImageCodecInfo creation routines taken from libgdiplus */
1015 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1016 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1017 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1018 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1019 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1020 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1022 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1024 { /* BMP */
1025 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1026 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1027 /* CodecName */ bmp_codecname,
1028 /* DllName */ NULL,
1029 /* FormatDescription */ bmp_format,
1030 /* FilenameExtension */ bmp_extension,
1031 /* MimeType */ bmp_mimetype,
1032 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1033 /* Version */ 1,
1034 /* SigCount */ 1,
1035 /* SigSize */ 2,
1036 /* SigPattern */ bmp_sig_pattern,
1037 /* SigMask */ bmp_sig_mask,
1041 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1043 if (!numEncoders || !size)
1044 return InvalidParameter;
1046 *numEncoders = NUM_ENCODERS_SUPPORTED;
1047 *size = sizeof (codecs);
1049 return Ok;
1052 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1054 if (!encoders ||
1055 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1056 (size != sizeof (codecs)))
1057 return GenericError;
1059 memcpy(encoders, codecs, sizeof (codecs));
1061 return Ok;
1063 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1065 BITMAP bm;
1066 GpStatus retval;
1067 PixelFormat format;
1069 if(!hbm || !bitmap)
1070 return InvalidParameter;
1072 /* TODO: Support for device-dependent bitmaps */
1073 if(hpal){
1074 FIXME("no support for device-dependent bitmaps\n");
1075 return NotImplemented;
1078 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1079 return InvalidParameter;
1081 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1082 switch(bm.bmBitsPixel) {
1083 case 1:
1084 format = PixelFormat1bppIndexed;
1085 break;
1086 case 4:
1087 format = PixelFormat4bppIndexed;
1088 break;
1089 case 8:
1090 format = PixelFormat8bppIndexed;
1091 break;
1092 case 24:
1093 format = PixelFormat24bppRGB;
1094 break;
1095 case 48:
1096 format = PixelFormat48bppRGB;
1097 break;
1098 default:
1099 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1100 return InvalidParameter;
1103 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1104 format, bm.bmBits, bitmap);
1106 return retval;
1109 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1110 const VOID *params, const UINT size)
1112 static int calls;
1114 if(!(calls++))
1115 FIXME("not implemented\n");
1117 return NotImplemented;
1120 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1122 if(!image || !flags)
1123 return InvalidParameter;
1125 *flags = image->flags;
1127 return Ok;