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
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)
44 IPicture_get_Height(pic, &y);
48 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
54 static INT ipicture_pixel_width(IPicture *pic)
59 IPicture_get_Width(pic, &x);
63 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
70 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
74 TRACE("%p %d %d %p\n", bitmap, x, y, color);
77 return InvalidParameter;
80 FIXME("not implemented\n");
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)
97 INT stride, bitspp = PIXELFORMATBPP(format);
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;
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;
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;
130 IPicture_get_Handle(bitmap->image.picture, &hbm);
131 IPicture_get_CurDC(bitmap->image.picture, &hdc);
132 bm_is_selected = (hdc != 0);
134 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
137 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
138 pbmi->bmiHeader.biBitCount = 0;
141 hdc = CreateCompatibleDC(0);
142 old = SelectObject(hdc, (HBITMAP)hbm);
146 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
148 abs_height = abs(pbmi->bmiHeader.biHeight);
149 stride = pbmi->bmiHeader.biWidth * bitspp / 8;
150 stride = (stride + 3) & ~3;
152 buff = GdipAlloc(stride * abs_height);
154 pbmi->bmiHeader.biBitCount = bitspp;
157 GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, pbmi, DIB_RGB_COLORS);
160 SelectObject(hdc, old);
169 lockeddata->Width = act_rect.Width;
170 lockeddata->Height = act_rect.Height;
171 lockeddata->PixelFormat = format;
172 lockeddata->Reserved = flags;
174 if(pbmi->bmiHeader.biHeight > 0){
175 lockeddata->Stride = -stride;
176 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
177 stride * (abs_height - 1 - act_rect.Y);
180 lockeddata->Stride = stride;
181 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
184 bitmap->lockmode = flags;
187 bitmap->bitmapbits = buff;
193 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
194 BitmapData* lockeddata)
202 if(!bitmap || !lockeddata)
203 return InvalidParameter;
205 if(!bitmap->lockmode)
208 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
209 return NotImplemented;
211 if(lockeddata->Reserved & ImageLockModeRead){
212 if(!(--bitmap->numlocks))
213 bitmap->lockmode = 0;
215 GdipFree(bitmap->bitmapbits);
216 bitmap->bitmapbits = NULL;
220 IPicture_get_Handle(bitmap->image.picture, &hbm);
221 IPicture_get_CurDC(bitmap->image.picture, &hdc);
222 bm_is_selected = (hdc != 0);
224 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
225 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
226 pbmi->bmiHeader.biBitCount = 0;
229 hdc = CreateCompatibleDC(0);
230 old = SelectObject(hdc, (HBITMAP)hbm);
233 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
234 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
235 SetDIBits(hdc, (HBITMAP)hbm, 0, abs(pbmi->bmiHeader.biHeight),
236 bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
239 SelectObject(hdc, old);
244 GdipFree(bitmap->bitmapbits);
245 bitmap->bitmapbits = NULL;
246 bitmap->lockmode = 0;
251 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
257 TRACE("%p, %p\n", image, cloneImage);
259 if (!image || !cloneImage)
260 return InvalidParameter;
262 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
266 *cloneImage = GdipAlloc(sizeof(GpImage));
269 IStream_Release(stream);
272 (*cloneImage)->type = image->type;
273 (*cloneImage)->flags = image->flags;
275 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
278 WARN("Failed to save image on stream\n");
282 hr = OleLoadPicture(stream, size, FALSE, &IID_IPicture,
283 (LPVOID*) &(*cloneImage)->picture);
286 WARN("Failed to load image from stream\n");
290 IStream_Release(stream);
293 IStream_Release(stream);
294 GdipFree(*cloneImage);
299 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
305 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
307 if(!filename || !bitmap)
308 return InvalidParameter;
310 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
315 stat = GdipCreateBitmapFromStream(stream, bitmap);
317 IStream_Release(stream);
322 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
323 VOID *bits, GpBitmap **bitmap)
325 DWORD height, stride;
328 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
330 height = abs(info->bmiHeader.biHeight);
331 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
333 if(info->bmiHeader.biHeight > 0) /* bottom-up */
335 bits = (BYTE*)bits + (height - 1) * stride;
339 switch(info->bmiHeader.biBitCount) {
341 format = PixelFormat1bppIndexed;
344 format = PixelFormat4bppIndexed;
347 format = PixelFormat8bppIndexed;
350 format = PixelFormat24bppRGB;
353 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
355 return InvalidParameter;
358 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
364 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
367 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
369 return GdipCreateBitmapFromFile(filename, bitmap);
372 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
373 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
376 GpStatus stat = InvalidParameter;
378 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
380 if(!lpBitmapName || !bitmap)
381 return InvalidParameter;
384 hbm = (HBITMAP)LoadImageW(hInstance,lpBitmapName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
387 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
394 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
395 HBITMAP* hbmReturn, ARGB background)
401 return NotImplemented;
404 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
405 GpMetafile* metafile, BOOL* succ, EmfType emfType,
406 const WCHAR* description, GpMetafile** out_metafile)
410 if(!ref || !metafile || !out_metafile)
411 return InvalidParameter;
414 *out_metafile = NULL;
417 FIXME("not implemented\n");
419 return NotImplemented;
422 /* FIXME: this should create a bitmap in the given size with the attributes
423 * (resolution etc.) of the graphics object */
424 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
425 GpGraphics* target, GpBitmap** bitmap)
430 if(!target || !bitmap)
431 return InvalidParameter;
434 FIXME("hacked stub\n");
436 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
442 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
443 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
445 BITMAPFILEHEADER *bmfh;
446 BITMAPINFOHEADER *bmih;
451 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
453 if (!bitmap) return InvalidParameter;
455 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
457 return InvalidParameter;
461 return InvalidParameter;
463 /* FIXME: windows allows negative stride (reads backwards from scan0) */
465 FIXME("negative stride\n");
466 return InvalidParameter;
469 *bitmap = GdipAlloc(sizeof(GpBitmap));
470 if(!*bitmap) return OutOfMemory;
473 stride = width * (PIXELFORMATBPP(format) / 8);
474 stride = (stride + 3) & ~3;
477 datalen = abs(stride * height);
478 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
479 buff = GdipAlloc(size);
485 bmfh = (BITMAPFILEHEADER*) buff;
486 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
488 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
490 bmfh->bfOffBits = size - datalen;
492 bmih->biSize = sizeof(BITMAPINFOHEADER);
493 bmih->biWidth = width;
494 bmih->biHeight = -height;
495 /* FIXME: use the rest of the data from format */
496 bmih->biBitCount = PIXELFORMATBPP(format);
497 bmih->biCompression = BI_RGB;
498 bmih->biSizeImage = datalen;
501 memcpy(bmih + 1, scan0, datalen);
503 memset(bmih + 1, 0, datalen);
505 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
506 ERR("could not make stream\n");
512 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
513 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
514 TRACE("Could not load picture\n");
515 IStream_Release(stream);
521 (*bitmap)->image.type = ImageTypeBitmap;
522 (*bitmap)->image.flags = ImageFlagsNone;
523 (*bitmap)->width = width;
524 (*bitmap)->height = height;
525 (*bitmap)->format = format;
530 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
535 TRACE("%p %p\n", stream, bitmap);
537 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
542 if((*bitmap)->image.type != ImageTypeBitmap){
543 IPicture_Release((*bitmap)->image.picture);
545 return GenericError; /* FIXME: what error to return? */
552 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
555 TRACE("%p %p\n", stream, bitmap);
557 return GdipCreateBitmapFromStream(stream, bitmap);
560 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
564 TRACE("%p\n", image);
567 return InvalidParameter;
569 IPicture_get_CurDC(image->picture, &hdc);
571 IPicture_Release(image->picture);
572 if (image->type == ImageTypeBitmap)
573 GdipFree(((GpBitmap*)image)->bitmapbits);
579 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
582 return InvalidParameter;
584 return NotImplemented;
587 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
590 TRACE("%p %p %p\n", image, srcRect, srcUnit);
592 if(!image || !srcRect || !srcUnit)
593 return InvalidParameter;
594 if(image->type == ImageTypeMetafile){
595 *srcRect = ((GpMetafile*)image)->bounds;
596 *srcUnit = ((GpMetafile*)image)->unit;
598 else if(image->type == ImageTypeBitmap){
599 srcRect->X = srcRect->Y = 0.0;
600 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
601 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
602 *srcUnit = UnitPixel;
605 srcRect->X = srcRect->Y = 0.0;
606 srcRect->Width = ipicture_pixel_width(image->picture);
607 srcRect->Height = ipicture_pixel_height(image->picture);
608 *srcUnit = UnitPixel;
611 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
612 srcRect->Width, srcRect->Height, *srcUnit);
617 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
620 TRACE("%p %p %p\n", image, width, height);
622 if(!image || !height || !width)
623 return InvalidParameter;
625 if(image->type == ImageTypeMetafile){
628 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
629 ((GpMetafile*)image)->bounds.Height;
631 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
632 ((GpMetafile*)image)->bounds.Width;
637 else if(image->type == ImageTypeBitmap){
638 *height = ((GpBitmap*)image)->height;
639 *width = ((GpBitmap*)image)->width;
642 *height = ipicture_pixel_height(image->picture);
643 *width = ipicture_pixel_width(image->picture);
646 TRACE("returning (%f, %f)\n", *height, *width);
650 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
651 GpGraphics **graphics)
655 TRACE("%p %p\n", image, graphics);
657 if(!image || !graphics)
658 return InvalidParameter;
660 if(image->type != ImageTypeBitmap){
661 FIXME("not implemented for image type %d\n", image->type);
662 return NotImplemented;
665 IPicture_get_CurDC(image->picture, &hdc);
668 hdc = CreateCompatibleDC(0);
669 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
672 return GdipCreateFromHDC(hdc, graphics);
675 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
677 TRACE("%p %p\n", image, height);
679 if(!image || !height)
680 return InvalidParameter;
682 if(image->type == ImageTypeMetafile){
685 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
686 ((GpMetafile*)image)->bounds.Height);
690 else if(image->type == ImageTypeBitmap)
691 *height = ((GpBitmap*)image)->height;
693 *height = ipicture_pixel_height(image->picture);
695 TRACE("returning %d\n", *height);
700 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
705 return InvalidParameter;
708 FIXME("not implemented\n");
710 return NotImplemented;
713 /* FIXME: test this function for non-bitmap types */
714 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
716 TRACE("%p %p\n", image, format);
718 if(!image || !format)
719 return InvalidParameter;
721 if(image->type != ImageTypeBitmap)
722 *format = PixelFormat24bppRGB;
724 *format = ((GpBitmap*) image)->format;
729 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
733 if(!image || !format)
734 return InvalidParameter;
737 FIXME("not implemented\n");
739 return NotImplemented;
742 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
744 TRACE("%p %p\n", image, type);
747 return InvalidParameter;
754 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
759 return InvalidParameter;
762 FIXME("not implemented\n");
764 return NotImplemented;
767 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
769 TRACE("%p %p\n", image, width);
772 return InvalidParameter;
774 if(image->type == ImageTypeMetafile){
777 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
778 ((GpMetafile*)image)->bounds.Width);
782 else if(image->type == ImageTypeBitmap)
783 *width = ((GpBitmap*)image)->width;
785 *width = ipicture_pixel_width(image->picture);
787 TRACE("returning %d\n", *width);
792 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
793 MetafileHeader * header)
797 if(!metafile || !header)
798 return InvalidParameter;
801 FIXME("not implemented\n");
806 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
807 UINT num, PropertyItem* items)
812 FIXME("not implemented\n");
814 return InvalidParameter;
817 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
822 FIXME("not implemented\n");
824 return InvalidParameter;
827 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
832 FIXME("not implemented\n");
834 return InvalidParameter;
837 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
838 PropertyItem* buffer)
843 FIXME("not implemented\n");
845 return InvalidParameter;
848 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
853 TRACE("%p %x %p\n", image, pid, size);
856 return InvalidParameter;
859 FIXME("not implemented\n");
861 return NotImplemented;
864 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
869 FIXME("not implemented\n");
871 return InvalidParameter;
874 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
875 GDIPCONST GUID* dimensionID, UINT* count)
879 if(!image || !dimensionID || !count)
880 return InvalidParameter;
883 FIXME("not implemented\n");
885 return NotImplemented;
888 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
892 return InvalidParameter;
901 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
902 GUID* dimensionIDs, UINT count)
906 if(!image || !dimensionIDs)
907 return InvalidParameter;
910 FIXME("not implemented\n");
915 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
916 GDIPCONST GUID* dimensionID, UINT frameidx)
920 if(!image || !dimensionID)
921 return InvalidParameter;
924 FIXME("not implemented\n");
929 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
935 TRACE("(%s) %p\n", debugstr_w(filename), image);
937 if (!filename || !image)
938 return InvalidParameter;
940 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
945 stat = GdipLoadImageFromStream(stream, image);
947 IStream_Release(stream);
952 /* FIXME: no icm handling */
953 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
955 TRACE("(%s) %p\n", debugstr_w(filename), image);
957 return GdipLoadImageFromFile(filename, image);
960 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
965 TRACE("%p %p\n", stream, image);
967 if(!stream || !image)
968 return InvalidParameter;
970 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
971 (LPVOID*) &pic) != S_OK){
972 TRACE("Could not load picture\n");
976 IPicture_get_Type(pic, &type);
978 if(type == PICTYPE_BITMAP){
980 BITMAPCOREHEADER* bmch;
984 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
987 *image = GdipAlloc(sizeof(GpBitmap));
992 (*image)->type = ImageTypeBitmap;
994 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
995 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
997 /* get the pixel format */
998 IPicture_get_Handle(pic, &hbm);
999 IPicture_get_CurDC(pic, &hdc);
1001 bmch = (BITMAPCOREHEADER*) (&pbmi->bmiHeader);
1002 bmch->bcSize = sizeof(BITMAPCOREHEADER);
1006 hdc = CreateCompatibleDC(0);
1007 old = SelectObject(hdc, (HBITMAP)hbm);
1008 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1009 SelectObject(hdc, old);
1013 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1015 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
1018 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
1019 /* FIXME: missing initialization code */
1020 *image = GdipAlloc(sizeof(GpMetafile));
1021 if(!*image) return OutOfMemory;
1022 (*image)->type = ImageTypeMetafile;
1025 *image = GdipAlloc(sizeof(GpImage));
1026 if(!*image) return OutOfMemory;
1027 (*image)->type = ImageTypeUnknown;
1030 (*image)->picture = pic;
1031 (*image)->flags = ImageFlagsNone;
1037 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1039 TRACE("%p %p\n", stream, image);
1041 return GdipLoadImageFromStream(stream, image);
1044 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1049 return InvalidParameter;
1052 FIXME("not implemented\n");
1054 return NotImplemented;
1057 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1062 FIXME("not implemented\n");
1064 return NotImplemented;
1067 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1068 GDIPCONST CLSID *clsidEncoder,
1069 GDIPCONST EncoderParameters *encoderParams)
1074 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1076 if (!image || !filename|| !clsidEncoder)
1077 return InvalidParameter;
1079 if (!(image->picture))
1080 return InvalidParameter;
1082 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1084 return GenericError;
1086 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1088 IStream_Release(stream);
1092 /*************************************************************************
1093 * Encoding functions -
1094 * These functions encode an image in different image file formats.
1096 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1097 #define BITMAP_FORMAT_JPEG 0xd8ff
1098 #define BITMAP_FORMAT_GIF 0x4947
1099 #define BITMAP_FORMAT_PNG 0x5089
1100 #define BITMAP_FORMAT_APM 0xcdd7
1102 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1103 void **output, unsigned int *output_size)
1105 int num_palette_entries;
1106 BITMAPFILEHEADER *bmp_file_hdr;
1107 BITMAPINFO *bmp_info_hdr;
1109 if (bitmap_info->bmiHeader.biClrUsed) {
1110 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
1111 if (num_palette_entries > 256) num_palette_entries = 256;
1113 if (bitmap_info->bmiHeader.biBitCount <= 8)
1114 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1116 num_palette_entries = 0;
1120 sizeof(BITMAPFILEHEADER) +
1121 sizeof(BITMAPINFOHEADER) +
1122 num_palette_entries * sizeof(RGBQUAD) +
1123 bitmap_info->bmiHeader.biSizeImage;
1125 *output = GdipAlloc(*output_size);
1127 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
1128 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1129 bmp_file_hdr->bfSize = *output_size;
1130 bmp_file_hdr->bfOffBits =
1131 sizeof(BITMAPFILEHEADER) +
1132 sizeof(BITMAPINFOHEADER) +
1133 num_palette_entries * sizeof (RGBQUAD);
1135 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1136 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1137 memcpy((unsigned char *)(*output) +
1138 sizeof(BITMAPFILEHEADER) +
1139 sizeof(BITMAPINFOHEADER) +
1140 num_palette_entries * sizeof(RGBQUAD),
1141 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1146 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1147 void **output, unsigned int *output_size);
1151 NUM_ENCODERS_SUPPORTED
1154 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1155 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1159 /*****************************************************************************
1160 * GdipSaveImageToStream [GDIPLUS.@]
1162 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1163 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1172 BITMAPINFO bmp_info;
1174 encode_image_func* encode_image;
1176 unsigned int output_size;
1184 TRACE("%p %p %p %p\n", image, stream, clsid, params);
1186 if(!image || !stream)
1187 return InvalidParameter;
1189 if (!image->picture)
1190 return GenericError;
1192 hr = IPicture_get_Type(image->picture, &type);
1193 if (FAILED(hr) || type != PICTYPE_BITMAP)
1194 return GenericError;
1196 /* select correct encoder */
1197 encode_image = NULL;
1198 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1199 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1200 encode_image = encode_image_funcs[i];
1202 if (encode_image == NULL)
1203 return UnknownImageFormat;
1205 /* extract underlying hbitmap representation from the IPicture */
1206 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1207 if (FAILED(hr) || !hbmp)
1208 return GenericError;
1209 hr = IPicture_get_CurDC(image->picture, &hdc);
1211 return GenericError;
1212 bm_is_selected = (hdc != 0);
1213 if (!bm_is_selected) {
1214 hdc = CreateCompatibleDC(0);
1215 old_hbmp = SelectObject(hdc, hbmp);
1218 /* get bits from HBITMAP */
1219 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1220 bmp_info.bmiHeader.biBitCount = 0;
1221 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1223 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1226 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1228 if (!bm_is_selected) {
1229 SelectObject(hdc, old_hbmp);
1236 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1238 IStream_Write(stream, output, output_size, &dummy);
1246 /*****************************************************************************
1247 * GdipSetImagePalette [GDIPLUS.@]
1249 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1250 GDIPCONST ColorPalette *palette)
1254 if(!image || !palette)
1255 return InvalidParameter;
1258 FIXME("not implemented\n");
1260 return NotImplemented;
1263 /*************************************************************************
1265 * Structures that represent which formats we support for encoding.
1268 /* ImageCodecInfo creation routines taken from libgdiplus */
1269 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1270 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1271 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1272 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1273 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1274 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1276 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1279 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1280 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1281 /* CodecName */ bmp_codecname,
1283 /* FormatDescription */ bmp_format,
1284 /* FilenameExtension */ bmp_extension,
1285 /* MimeType */ bmp_mimetype,
1286 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1290 /* SigPattern */ bmp_sig_pattern,
1291 /* SigMask */ bmp_sig_mask,
1295 /*****************************************************************************
1296 * GdipGetImageDecodersSize [GDIPLUS.@]
1298 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
1300 FIXME("%p %p stub!\n", numDecoders, size);
1302 if (!numDecoders || !size)
1303 return InvalidParameter;
1311 /*****************************************************************************
1312 * GdipGetImageDecoders [GDIPLUS.@]
1314 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
1316 FIXME("%u %u %p stub!\n", numDecoders, size, decoders);
1319 return GenericError;
1321 return NotImplemented;
1324 /*****************************************************************************
1325 * GdipGetImageEncodersSize [GDIPLUS.@]
1327 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1329 TRACE("%p %p\n", numEncoders, size);
1331 if (!numEncoders || !size)
1332 return InvalidParameter;
1334 *numEncoders = NUM_ENCODERS_SUPPORTED;
1335 *size = sizeof (codecs);
1340 /*****************************************************************************
1341 * GdipGetImageEncoders [GDIPLUS.@]
1343 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1345 TRACE("%u %u %p\n", numEncoders, size, encoders);
1348 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1349 (size != sizeof (codecs)))
1350 return GenericError;
1352 memcpy(encoders, codecs, sizeof (codecs));
1357 /*****************************************************************************
1358 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1360 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1366 TRACE("%p %p %p\n", hbm, hpal, bitmap);
1369 return InvalidParameter;
1371 /* TODO: Support for device-dependent bitmaps */
1373 FIXME("no support for device-dependent bitmaps\n");
1374 return NotImplemented;
1377 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1378 return InvalidParameter;
1380 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1381 switch(bm.bmBitsPixel) {
1383 format = PixelFormat1bppIndexed;
1386 format = PixelFormat4bppIndexed;
1389 format = PixelFormat8bppIndexed;
1392 format = PixelFormat24bppRGB;
1395 format = PixelFormat32bppRGB;
1398 format = PixelFormat48bppRGB;
1401 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1402 return InvalidParameter;
1405 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1406 format, bm.bmBits, bitmap);
1411 /*****************************************************************************
1412 * GdipSetEffectParameters [GDIPLUS.@]
1414 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1415 const VOID *params, const UINT size)
1420 FIXME("not implemented\n");
1422 return NotImplemented;
1425 /*****************************************************************************
1426 * GdipGetImageFlags [GDIPLUS.@]
1428 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1430 TRACE("%p %p\n", image, flags);
1432 if(!image || !flags)
1433 return InvalidParameter;
1435 *flags = image->flags;
1440 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
1442 TRACE("(%d, %p)\n", control, param);
1445 case TestControlForceBilinear:
1447 FIXME("TestControlForceBilinear not handled\n");
1449 case TestControlNoICM:
1451 FIXME("TestControlNoICM not handled\n");
1453 case TestControlGetBuildNumber:
1454 *((DWORD*)param) = 3102;
1461 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
1462 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
1463 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
1464 GpMetafile **metafile)
1466 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1467 frameUnit, debugstr_w(desc), metafile);
1469 return NotImplemented;
1472 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
1473 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
1474 GDIPCONST WCHAR *desc, GpMetafile **metafile)
1476 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1477 frameUnit, debugstr_w(desc), metafile);
1479 return NotImplemented;