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)
258 TRACE("%p, %p\n", image, cloneImage);
260 if (!image || !cloneImage)
261 return InvalidParameter;
263 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
267 *cloneImage = GdipAlloc(sizeof(GpImage));
270 IStream_Release(stream);
273 (*cloneImage)->type = image->type;
274 (*cloneImage)->flags = image->flags;
276 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
279 WARN("Failed to save image on stream\n");
283 /* Set seek pointer back to the beginning of the picture */
285 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
289 hr = OleLoadPicture(stream, size, FALSE, &IID_IPicture,
290 (LPVOID*) &(*cloneImage)->picture);
293 WARN("Failed to load image from stream\n");
297 IStream_Release(stream);
300 IStream_Release(stream);
301 GdipFree(*cloneImage);
306 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
312 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
314 if(!filename || !bitmap)
315 return InvalidParameter;
317 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
322 stat = GdipCreateBitmapFromStream(stream, bitmap);
324 IStream_Release(stream);
329 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
330 VOID *bits, GpBitmap **bitmap)
332 DWORD height, stride;
335 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
337 height = abs(info->bmiHeader.biHeight);
338 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
340 if(info->bmiHeader.biHeight > 0) /* bottom-up */
342 bits = (BYTE*)bits + (height - 1) * stride;
346 switch(info->bmiHeader.biBitCount) {
348 format = PixelFormat1bppIndexed;
351 format = PixelFormat4bppIndexed;
354 format = PixelFormat8bppIndexed;
357 format = PixelFormat24bppRGB;
360 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
362 return InvalidParameter;
365 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
371 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
374 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
376 return GdipCreateBitmapFromFile(filename, bitmap);
379 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
380 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
383 GpStatus stat = InvalidParameter;
385 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
387 if(!lpBitmapName || !bitmap)
388 return InvalidParameter;
391 hbm = (HBITMAP)LoadImageW(hInstance,lpBitmapName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
394 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
401 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
402 HBITMAP* hbmReturn, ARGB background)
408 return NotImplemented;
411 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
412 GpMetafile* metafile, BOOL* succ, EmfType emfType,
413 const WCHAR* description, GpMetafile** out_metafile)
417 if(!ref || !metafile || !out_metafile)
418 return InvalidParameter;
421 *out_metafile = NULL;
424 FIXME("not implemented\n");
426 return NotImplemented;
429 /* FIXME: this should create a bitmap in the given size with the attributes
430 * (resolution etc.) of the graphics object */
431 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
432 GpGraphics* target, GpBitmap** bitmap)
437 if(!target || !bitmap)
438 return InvalidParameter;
441 FIXME("hacked stub\n");
443 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
449 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
450 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
452 BITMAPFILEHEADER *bmfh;
453 BITMAPINFOHEADER *bmih;
458 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
460 if (!bitmap) return InvalidParameter;
462 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
464 return InvalidParameter;
468 return InvalidParameter;
470 /* FIXME: windows allows negative stride (reads backwards from scan0) */
472 FIXME("negative stride\n");
473 return InvalidParameter;
476 *bitmap = GdipAlloc(sizeof(GpBitmap));
477 if(!*bitmap) return OutOfMemory;
480 stride = width * (PIXELFORMATBPP(format) / 8);
481 stride = (stride + 3) & ~3;
484 datalen = abs(stride * height);
485 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
486 buff = GdipAlloc(size);
492 bmfh = (BITMAPFILEHEADER*) buff;
493 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
495 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
497 bmfh->bfOffBits = size - datalen;
499 bmih->biSize = sizeof(BITMAPINFOHEADER);
500 bmih->biWidth = width;
501 bmih->biHeight = -height;
502 /* FIXME: use the rest of the data from format */
503 bmih->biBitCount = PIXELFORMATBPP(format);
504 bmih->biCompression = BI_RGB;
505 bmih->biSizeImage = datalen;
508 memcpy(bmih + 1, scan0, datalen);
510 memset(bmih + 1, 0, datalen);
512 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
513 ERR("could not make stream\n");
519 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
520 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
521 TRACE("Could not load picture\n");
522 IStream_Release(stream);
528 (*bitmap)->image.type = ImageTypeBitmap;
529 (*bitmap)->image.flags = ImageFlagsNone;
530 (*bitmap)->width = width;
531 (*bitmap)->height = height;
532 (*bitmap)->format = format;
537 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
542 TRACE("%p %p\n", stream, bitmap);
544 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
549 if((*bitmap)->image.type != ImageTypeBitmap){
550 IPicture_Release((*bitmap)->image.picture);
552 return GenericError; /* FIXME: what error to return? */
559 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
562 TRACE("%p %p\n", stream, bitmap);
564 return GdipCreateBitmapFromStream(stream, bitmap);
567 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
571 TRACE("%p\n", image);
574 return InvalidParameter;
576 IPicture_get_CurDC(image->picture, &hdc);
578 IPicture_Release(image->picture);
579 if (image->type == ImageTypeBitmap)
580 GdipFree(((GpBitmap*)image)->bitmapbits);
586 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
589 return InvalidParameter;
591 return NotImplemented;
594 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
597 TRACE("%p %p %p\n", image, srcRect, srcUnit);
599 if(!image || !srcRect || !srcUnit)
600 return InvalidParameter;
601 if(image->type == ImageTypeMetafile){
602 *srcRect = ((GpMetafile*)image)->bounds;
603 *srcUnit = ((GpMetafile*)image)->unit;
605 else if(image->type == ImageTypeBitmap){
606 srcRect->X = srcRect->Y = 0.0;
607 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
608 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
609 *srcUnit = UnitPixel;
612 srcRect->X = srcRect->Y = 0.0;
613 srcRect->Width = ipicture_pixel_width(image->picture);
614 srcRect->Height = ipicture_pixel_height(image->picture);
615 *srcUnit = UnitPixel;
618 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
619 srcRect->Width, srcRect->Height, *srcUnit);
624 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
627 TRACE("%p %p %p\n", image, width, height);
629 if(!image || !height || !width)
630 return InvalidParameter;
632 if(image->type == ImageTypeMetafile){
635 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
636 ((GpMetafile*)image)->bounds.Height;
638 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
639 ((GpMetafile*)image)->bounds.Width;
644 else if(image->type == ImageTypeBitmap){
645 *height = ((GpBitmap*)image)->height;
646 *width = ((GpBitmap*)image)->width;
649 *height = ipicture_pixel_height(image->picture);
650 *width = ipicture_pixel_width(image->picture);
653 TRACE("returning (%f, %f)\n", *height, *width);
657 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
658 GpGraphics **graphics)
662 TRACE("%p %p\n", image, graphics);
664 if(!image || !graphics)
665 return InvalidParameter;
667 if(image->type != ImageTypeBitmap){
668 FIXME("not implemented for image type %d\n", image->type);
669 return NotImplemented;
672 IPicture_get_CurDC(image->picture, &hdc);
675 hdc = CreateCompatibleDC(0);
676 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
679 return GdipCreateFromHDC(hdc, graphics);
682 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
684 TRACE("%p %p\n", image, height);
686 if(!image || !height)
687 return InvalidParameter;
689 if(image->type == ImageTypeMetafile){
692 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
693 ((GpMetafile*)image)->bounds.Height);
697 else if(image->type == ImageTypeBitmap)
698 *height = ((GpBitmap*)image)->height;
700 *height = ipicture_pixel_height(image->picture);
702 TRACE("returning %d\n", *height);
707 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
712 return InvalidParameter;
715 FIXME("not implemented\n");
717 return NotImplemented;
720 /* FIXME: test this function for non-bitmap types */
721 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
723 TRACE("%p %p\n", image, format);
725 if(!image || !format)
726 return InvalidParameter;
728 if(image->type != ImageTypeBitmap)
729 *format = PixelFormat24bppRGB;
731 *format = ((GpBitmap*) image)->format;
736 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
740 if(!image || !format)
741 return InvalidParameter;
744 FIXME("not implemented\n");
746 return NotImplemented;
749 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
751 TRACE("%p %p\n", image, type);
754 return InvalidParameter;
761 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
766 return InvalidParameter;
769 FIXME("not implemented\n");
771 return NotImplemented;
774 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
776 TRACE("%p %p\n", image, width);
779 return InvalidParameter;
781 if(image->type == ImageTypeMetafile){
784 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
785 ((GpMetafile*)image)->bounds.Width);
789 else if(image->type == ImageTypeBitmap)
790 *width = ((GpBitmap*)image)->width;
792 *width = ipicture_pixel_width(image->picture);
794 TRACE("returning %d\n", *width);
799 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
800 MetafileHeader * header)
804 if(!metafile || !header)
805 return InvalidParameter;
808 FIXME("not implemented\n");
813 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
814 UINT num, PropertyItem* items)
819 FIXME("not implemented\n");
821 return InvalidParameter;
824 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
829 FIXME("not implemented\n");
831 return InvalidParameter;
834 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
839 FIXME("not implemented\n");
841 return InvalidParameter;
844 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
845 PropertyItem* buffer)
850 FIXME("not implemented\n");
852 return InvalidParameter;
855 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
860 TRACE("%p %x %p\n", image, pid, size);
863 return InvalidParameter;
866 FIXME("not implemented\n");
868 return NotImplemented;
871 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
876 FIXME("not implemented\n");
878 return InvalidParameter;
881 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
882 GDIPCONST GUID* dimensionID, UINT* count)
886 if(!image || !dimensionID || !count)
887 return InvalidParameter;
890 FIXME("not implemented\n");
892 return NotImplemented;
895 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
899 return InvalidParameter;
908 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
909 GUID* dimensionIDs, UINT count)
913 if(!image || !dimensionIDs)
914 return InvalidParameter;
917 FIXME("not implemented\n");
922 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
923 GDIPCONST GUID* dimensionID, UINT frameidx)
927 if(!image || !dimensionID)
928 return InvalidParameter;
931 FIXME("not implemented\n");
936 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
942 TRACE("(%s) %p\n", debugstr_w(filename), image);
944 if (!filename || !image)
945 return InvalidParameter;
947 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
952 stat = GdipLoadImageFromStream(stream, image);
954 IStream_Release(stream);
959 /* FIXME: no icm handling */
960 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
962 TRACE("(%s) %p\n", debugstr_w(filename), image);
964 return GdipLoadImageFromFile(filename, image);
967 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
972 TRACE("%p %p\n", stream, image);
974 if(!stream || !image)
975 return InvalidParameter;
977 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
978 (LPVOID*) &pic) != S_OK){
979 TRACE("Could not load picture\n");
983 IPicture_get_Type(pic, &type);
985 if(type == PICTYPE_BITMAP){
987 BITMAPCOREHEADER* bmch;
991 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
994 *image = GdipAlloc(sizeof(GpBitmap));
999 (*image)->type = ImageTypeBitmap;
1001 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
1002 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
1004 /* get the pixel format */
1005 IPicture_get_Handle(pic, &hbm);
1006 IPicture_get_CurDC(pic, &hdc);
1008 bmch = (BITMAPCOREHEADER*) (&pbmi->bmiHeader);
1009 bmch->bcSize = sizeof(BITMAPCOREHEADER);
1013 hdc = CreateCompatibleDC(0);
1014 old = SelectObject(hdc, (HBITMAP)hbm);
1015 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1016 SelectObject(hdc, old);
1020 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1022 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
1025 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
1026 /* FIXME: missing initialization code */
1027 *image = GdipAlloc(sizeof(GpMetafile));
1028 if(!*image) return OutOfMemory;
1029 (*image)->type = ImageTypeMetafile;
1032 *image = GdipAlloc(sizeof(GpImage));
1033 if(!*image) return OutOfMemory;
1034 (*image)->type = ImageTypeUnknown;
1037 (*image)->picture = pic;
1038 (*image)->flags = ImageFlagsNone;
1044 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1046 TRACE("%p %p\n", stream, image);
1048 return GdipLoadImageFromStream(stream, image);
1051 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1056 return InvalidParameter;
1059 FIXME("not implemented\n");
1061 return NotImplemented;
1064 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1069 FIXME("not implemented\n");
1071 return NotImplemented;
1074 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1075 GDIPCONST CLSID *clsidEncoder,
1076 GDIPCONST EncoderParameters *encoderParams)
1081 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1083 if (!image || !filename|| !clsidEncoder)
1084 return InvalidParameter;
1086 if (!(image->picture))
1087 return InvalidParameter;
1089 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1091 return GenericError;
1093 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1095 IStream_Release(stream);
1099 /*************************************************************************
1100 * Encoding functions -
1101 * These functions encode an image in different image file formats.
1103 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1104 #define BITMAP_FORMAT_JPEG 0xd8ff
1105 #define BITMAP_FORMAT_GIF 0x4947
1106 #define BITMAP_FORMAT_PNG 0x5089
1107 #define BITMAP_FORMAT_APM 0xcdd7
1109 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1110 void **output, unsigned int *output_size)
1112 int num_palette_entries;
1113 BITMAPFILEHEADER *bmp_file_hdr;
1114 BITMAPINFO *bmp_info_hdr;
1116 if (bitmap_info->bmiHeader.biClrUsed) {
1117 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
1118 if (num_palette_entries > 256) num_palette_entries = 256;
1120 if (bitmap_info->bmiHeader.biBitCount <= 8)
1121 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1123 num_palette_entries = 0;
1127 sizeof(BITMAPFILEHEADER) +
1128 sizeof(BITMAPINFOHEADER) +
1129 num_palette_entries * sizeof(RGBQUAD) +
1130 bitmap_info->bmiHeader.biSizeImage;
1132 *output = GdipAlloc(*output_size);
1134 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
1135 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1136 bmp_file_hdr->bfSize = *output_size;
1137 bmp_file_hdr->bfOffBits =
1138 sizeof(BITMAPFILEHEADER) +
1139 sizeof(BITMAPINFOHEADER) +
1140 num_palette_entries * sizeof (RGBQUAD);
1142 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1143 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1144 memcpy((unsigned char *)(*output) +
1145 sizeof(BITMAPFILEHEADER) +
1146 sizeof(BITMAPINFOHEADER) +
1147 num_palette_entries * sizeof(RGBQUAD),
1148 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1153 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1154 void **output, unsigned int *output_size);
1158 NUM_ENCODERS_SUPPORTED
1161 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1162 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1166 /*****************************************************************************
1167 * GdipSaveImageToStream [GDIPLUS.@]
1169 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1170 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1179 BITMAPINFO bmp_info;
1181 encode_image_func* encode_image;
1183 unsigned int output_size;
1191 TRACE("%p %p %p %p\n", image, stream, clsid, params);
1193 if(!image || !stream)
1194 return InvalidParameter;
1196 if (!image->picture)
1197 return GenericError;
1199 hr = IPicture_get_Type(image->picture, &type);
1200 if (FAILED(hr) || type != PICTYPE_BITMAP)
1201 return GenericError;
1203 /* select correct encoder */
1204 encode_image = NULL;
1205 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1206 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1207 encode_image = encode_image_funcs[i];
1209 if (encode_image == NULL)
1210 return UnknownImageFormat;
1212 /* extract underlying hbitmap representation from the IPicture */
1213 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1214 if (FAILED(hr) || !hbmp)
1215 return GenericError;
1216 hr = IPicture_get_CurDC(image->picture, &hdc);
1218 return GenericError;
1219 bm_is_selected = (hdc != 0);
1220 if (!bm_is_selected) {
1221 hdc = CreateCompatibleDC(0);
1222 old_hbmp = SelectObject(hdc, hbmp);
1225 /* get bits from HBITMAP */
1226 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1227 bmp_info.bmiHeader.biBitCount = 0;
1228 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1230 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1233 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1235 if (!bm_is_selected) {
1236 SelectObject(hdc, old_hbmp);
1243 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1245 IStream_Write(stream, output, output_size, &dummy);
1253 /*****************************************************************************
1254 * GdipSetImagePalette [GDIPLUS.@]
1256 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1257 GDIPCONST ColorPalette *palette)
1261 if(!image || !palette)
1262 return InvalidParameter;
1265 FIXME("not implemented\n");
1267 return NotImplemented;
1270 /*************************************************************************
1272 * Structures that represent which formats we support for encoding.
1275 /* ImageCodecInfo creation routines taken from libgdiplus */
1276 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1277 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1278 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1279 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1280 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1281 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1283 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1286 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1287 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1288 /* CodecName */ bmp_codecname,
1290 /* FormatDescription */ bmp_format,
1291 /* FilenameExtension */ bmp_extension,
1292 /* MimeType */ bmp_mimetype,
1293 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1297 /* SigPattern */ bmp_sig_pattern,
1298 /* SigMask */ bmp_sig_mask,
1302 /*****************************************************************************
1303 * GdipGetImageDecodersSize [GDIPLUS.@]
1305 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
1307 FIXME("%p %p stub!\n", numDecoders, size);
1309 if (!numDecoders || !size)
1310 return InvalidParameter;
1318 /*****************************************************************************
1319 * GdipGetImageDecoders [GDIPLUS.@]
1321 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
1323 FIXME("%u %u %p stub!\n", numDecoders, size, decoders);
1326 return GenericError;
1328 return NotImplemented;
1331 /*****************************************************************************
1332 * GdipGetImageEncodersSize [GDIPLUS.@]
1334 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1336 TRACE("%p %p\n", numEncoders, size);
1338 if (!numEncoders || !size)
1339 return InvalidParameter;
1341 *numEncoders = NUM_ENCODERS_SUPPORTED;
1342 *size = sizeof (codecs);
1347 /*****************************************************************************
1348 * GdipGetImageEncoders [GDIPLUS.@]
1350 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1352 TRACE("%u %u %p\n", numEncoders, size, encoders);
1355 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1356 (size != sizeof (codecs)))
1357 return GenericError;
1359 memcpy(encoders, codecs, sizeof (codecs));
1364 /*****************************************************************************
1365 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1367 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1373 TRACE("%p %p %p\n", hbm, hpal, bitmap);
1376 return InvalidParameter;
1378 /* TODO: Support for device-dependent bitmaps */
1380 FIXME("no support for device-dependent bitmaps\n");
1381 return NotImplemented;
1384 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1385 return InvalidParameter;
1387 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1388 switch(bm.bmBitsPixel) {
1390 format = PixelFormat1bppIndexed;
1393 format = PixelFormat4bppIndexed;
1396 format = PixelFormat8bppIndexed;
1399 format = PixelFormat24bppRGB;
1402 format = PixelFormat32bppRGB;
1405 format = PixelFormat48bppRGB;
1408 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1409 return InvalidParameter;
1412 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1413 format, bm.bmBits, bitmap);
1418 /*****************************************************************************
1419 * GdipSetEffectParameters [GDIPLUS.@]
1421 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1422 const VOID *params, const UINT size)
1427 FIXME("not implemented\n");
1429 return NotImplemented;
1432 /*****************************************************************************
1433 * GdipGetImageFlags [GDIPLUS.@]
1435 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1437 TRACE("%p %p\n", image, flags);
1439 if(!image || !flags)
1440 return InvalidParameter;
1442 *flags = image->flags;
1447 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
1449 TRACE("(%d, %p)\n", control, param);
1452 case TestControlForceBilinear:
1454 FIXME("TestControlForceBilinear not handled\n");
1456 case TestControlNoICM:
1458 FIXME("TestControlNoICM not handled\n");
1460 case TestControlGetBuildNumber:
1461 *((DWORD*)param) = 3102;
1468 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
1469 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
1470 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
1471 GpMetafile **metafile)
1473 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1474 frameUnit, debugstr_w(desc), metafile);
1476 return NotImplemented;
1479 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
1480 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
1481 GDIPCONST WCHAR *desc, GpMetafile **metafile)
1483 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1484 frameUnit, debugstr_w(desc), metafile);
1486 return NotImplemented;