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 || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
455 return InvalidParameter;
459 return InvalidParameter;
461 /* FIXME: windows allows negative stride (reads backwards from scan0) */
463 FIXME("negative stride\n");
464 return InvalidParameter;
467 *bitmap = GdipAlloc(sizeof(GpBitmap));
468 if(!*bitmap) return OutOfMemory;
471 stride = width * (PIXELFORMATBPP(format) / 8);
472 stride = (stride + 3) & ~3;
475 datalen = abs(stride * height);
476 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
477 buff = GdipAlloc(size);
483 bmfh = (BITMAPFILEHEADER*) buff;
484 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
486 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
488 bmfh->bfOffBits = size - datalen;
490 bmih->biSize = sizeof(BITMAPINFOHEADER);
491 bmih->biWidth = width;
492 bmih->biHeight = -height;
493 /* FIXME: use the rest of the data from format */
494 bmih->biBitCount = PIXELFORMATBPP(format);
495 bmih->biCompression = BI_RGB;
496 bmih->biSizeImage = datalen;
499 memcpy(bmih + 1, scan0, datalen);
501 memset(bmih + 1, 0, datalen);
503 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
504 ERR("could not make stream\n");
510 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
511 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
512 TRACE("Could not load picture\n");
513 IStream_Release(stream);
519 (*bitmap)->image.type = ImageTypeBitmap;
520 (*bitmap)->image.flags = ImageFlagsNone;
521 (*bitmap)->width = width;
522 (*bitmap)->height = height;
523 (*bitmap)->format = format;
528 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
533 TRACE("%p %p\n", stream, bitmap);
535 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
540 if((*bitmap)->image.type != ImageTypeBitmap){
541 IPicture_Release((*bitmap)->image.picture);
543 return GenericError; /* FIXME: what error to return? */
550 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
553 TRACE("%p %p\n", stream, bitmap);
555 return GdipCreateBitmapFromStream(stream, bitmap);
558 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
562 TRACE("%p\n", image);
565 return InvalidParameter;
567 IPicture_get_CurDC(image->picture, &hdc);
569 IPicture_Release(image->picture);
570 if (image->type == ImageTypeBitmap)
571 GdipFree(((GpBitmap*)image)->bitmapbits);
577 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
580 return InvalidParameter;
582 return NotImplemented;
585 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
588 TRACE("%p %p %p\n", image, srcRect, srcUnit);
590 if(!image || !srcRect || !srcUnit)
591 return InvalidParameter;
592 if(image->type == ImageTypeMetafile){
593 *srcRect = ((GpMetafile*)image)->bounds;
594 *srcUnit = ((GpMetafile*)image)->unit;
596 else if(image->type == ImageTypeBitmap){
597 srcRect->X = srcRect->Y = 0.0;
598 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
599 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
600 *srcUnit = UnitPixel;
603 srcRect->X = srcRect->Y = 0.0;
604 srcRect->Width = ipicture_pixel_width(image->picture);
605 srcRect->Height = ipicture_pixel_height(image->picture);
606 *srcUnit = UnitPixel;
609 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
610 srcRect->Width, srcRect->Height, *srcUnit);
615 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
618 TRACE("%p %p %p\n", image, width, height);
620 if(!image || !height || !width)
621 return InvalidParameter;
623 if(image->type == ImageTypeMetafile){
626 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
627 ((GpMetafile*)image)->bounds.Height;
629 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
630 ((GpMetafile*)image)->bounds.Width;
635 else if(image->type == ImageTypeBitmap){
636 *height = ((GpBitmap*)image)->height;
637 *width = ((GpBitmap*)image)->width;
640 *height = ipicture_pixel_height(image->picture);
641 *width = ipicture_pixel_width(image->picture);
644 TRACE("returning (%f, %f)\n", *height, *width);
648 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
649 GpGraphics **graphics)
653 TRACE("%p %p\n", image, graphics);
655 if(!image || !graphics)
656 return InvalidParameter;
658 if(image->type != ImageTypeBitmap){
659 FIXME("not implemented for image type %d\n", image->type);
660 return NotImplemented;
663 IPicture_get_CurDC(image->picture, &hdc);
666 hdc = CreateCompatibleDC(0);
667 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
670 return GdipCreateFromHDC(hdc, graphics);
673 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
675 TRACE("%p %p\n", image, height);
677 if(!image || !height)
678 return InvalidParameter;
680 if(image->type == ImageTypeMetafile){
683 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
684 ((GpMetafile*)image)->bounds.Height);
688 else if(image->type == ImageTypeBitmap)
689 *height = ((GpBitmap*)image)->height;
691 *height = ipicture_pixel_height(image->picture);
693 TRACE("returning %d\n", *height);
698 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
703 return InvalidParameter;
706 FIXME("not implemented\n");
708 return NotImplemented;
711 /* FIXME: test this function for non-bitmap types */
712 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
714 TRACE("%p %p\n", image, format);
716 if(!image || !format)
717 return InvalidParameter;
719 if(image->type != ImageTypeBitmap)
720 *format = PixelFormat24bppRGB;
722 *format = ((GpBitmap*) image)->format;
727 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
731 if(!image || !format)
732 return InvalidParameter;
735 FIXME("not implemented\n");
737 return NotImplemented;
740 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
742 TRACE("%p %p\n", image, type);
745 return InvalidParameter;
752 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
757 return InvalidParameter;
760 FIXME("not implemented\n");
762 return NotImplemented;
765 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
767 TRACE("%p %p\n", image, width);
770 return InvalidParameter;
772 if(image->type == ImageTypeMetafile){
775 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
776 ((GpMetafile*)image)->bounds.Width);
780 else if(image->type == ImageTypeBitmap)
781 *width = ((GpBitmap*)image)->width;
783 *width = ipicture_pixel_width(image->picture);
785 TRACE("returning %d\n", *width);
790 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
791 MetafileHeader * header)
795 if(!metafile || !header)
796 return InvalidParameter;
799 FIXME("not implemented\n");
804 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
805 UINT num, PropertyItem* items)
810 FIXME("not implemented\n");
812 return InvalidParameter;
815 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
820 FIXME("not implemented\n");
822 return InvalidParameter;
825 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
830 FIXME("not implemented\n");
832 return InvalidParameter;
835 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
836 PropertyItem* buffer)
841 FIXME("not implemented\n");
843 return InvalidParameter;
846 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
851 TRACE("%p %x %p\n", image, pid, size);
854 return InvalidParameter;
857 FIXME("not implemented\n");
859 return NotImplemented;
862 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
867 FIXME("not implemented\n");
869 return InvalidParameter;
872 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
873 GDIPCONST GUID* dimensionID, UINT* count)
877 if(!image || !dimensionID || !count)
878 return InvalidParameter;
881 FIXME("not implemented\n");
883 return NotImplemented;
886 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
890 return InvalidParameter;
899 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
900 GUID* dimensionIDs, UINT count)
904 if(!image || !dimensionIDs)
905 return InvalidParameter;
908 FIXME("not implemented\n");
913 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
914 GDIPCONST GUID* dimensionID, UINT frameidx)
918 if(!image || !dimensionID)
919 return InvalidParameter;
922 FIXME("not implemented\n");
927 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
933 TRACE("(%s) %p\n", debugstr_w(filename), image);
935 if (!filename || !image)
936 return InvalidParameter;
938 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
943 stat = GdipLoadImageFromStream(stream, image);
945 IStream_Release(stream);
950 /* FIXME: no icm handling */
951 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
953 TRACE("(%s) %p\n", debugstr_w(filename), image);
955 return GdipLoadImageFromFile(filename, image);
958 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
963 TRACE("%p %p\n", stream, image);
965 if(!stream || !image)
966 return InvalidParameter;
968 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
969 (LPVOID*) &pic) != S_OK){
970 TRACE("Could not load picture\n");
974 IPicture_get_Type(pic, &type);
976 if(type == PICTYPE_BITMAP){
978 BITMAPCOREHEADER* bmch;
982 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
985 *image = GdipAlloc(sizeof(GpBitmap));
990 (*image)->type = ImageTypeBitmap;
992 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
993 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
995 /* get the pixel format */
996 IPicture_get_Handle(pic, &hbm);
997 IPicture_get_CurDC(pic, &hdc);
999 bmch = (BITMAPCOREHEADER*) (&pbmi->bmiHeader);
1000 bmch->bcSize = sizeof(BITMAPCOREHEADER);
1004 hdc = CreateCompatibleDC(0);
1005 old = SelectObject(hdc, (HBITMAP)hbm);
1006 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1007 SelectObject(hdc, old);
1011 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1013 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
1016 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
1017 /* FIXME: missing initialization code */
1018 *image = GdipAlloc(sizeof(GpMetafile));
1019 if(!*image) return OutOfMemory;
1020 (*image)->type = ImageTypeMetafile;
1023 *image = GdipAlloc(sizeof(GpImage));
1024 if(!*image) return OutOfMemory;
1025 (*image)->type = ImageTypeUnknown;
1028 (*image)->picture = pic;
1029 (*image)->flags = ImageFlagsNone;
1035 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1037 TRACE("%p %p\n", stream, image);
1039 return GdipLoadImageFromStream(stream, image);
1042 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1047 return InvalidParameter;
1050 FIXME("not implemented\n");
1052 return NotImplemented;
1055 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1060 FIXME("not implemented\n");
1062 return NotImplemented;
1065 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1066 GDIPCONST CLSID *clsidEncoder,
1067 GDIPCONST EncoderParameters *encoderParams)
1072 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1074 if (!image || !filename|| !clsidEncoder)
1075 return InvalidParameter;
1077 if (!(image->picture))
1078 return InvalidParameter;
1080 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1082 return GenericError;
1084 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1086 IStream_Release(stream);
1090 /*************************************************************************
1091 * Encoding functions -
1092 * These functions encode an image in different image file formats.
1094 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1095 #define BITMAP_FORMAT_JPEG 0xd8ff
1096 #define BITMAP_FORMAT_GIF 0x4947
1097 #define BITMAP_FORMAT_PNG 0x5089
1098 #define BITMAP_FORMAT_APM 0xcdd7
1100 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1101 void **output, unsigned int *output_size)
1103 int num_palette_entries;
1104 BITMAPFILEHEADER *bmp_file_hdr;
1105 BITMAPINFO *bmp_info_hdr;
1107 if (bitmap_info->bmiHeader.biClrUsed) {
1108 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
1109 if (num_palette_entries > 256) num_palette_entries = 256;
1111 if (bitmap_info->bmiHeader.biBitCount <= 8)
1112 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1114 num_palette_entries = 0;
1118 sizeof(BITMAPFILEHEADER) +
1119 sizeof(BITMAPINFOHEADER) +
1120 num_palette_entries * sizeof(RGBQUAD) +
1121 bitmap_info->bmiHeader.biSizeImage;
1123 *output = GdipAlloc(*output_size);
1125 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
1126 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1127 bmp_file_hdr->bfSize = *output_size;
1128 bmp_file_hdr->bfOffBits =
1129 sizeof(BITMAPFILEHEADER) +
1130 sizeof(BITMAPINFOHEADER) +
1131 num_palette_entries * sizeof (RGBQUAD);
1133 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1134 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1135 memcpy((unsigned char *)(*output) +
1136 sizeof(BITMAPFILEHEADER) +
1137 sizeof(BITMAPINFOHEADER) +
1138 num_palette_entries * sizeof(RGBQUAD),
1139 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1144 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1145 void **output, unsigned int *output_size);
1149 NUM_ENCODERS_SUPPORTED
1152 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1153 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1157 /*****************************************************************************
1158 * GdipSaveImageToStream [GDIPLUS.@]
1160 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1161 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1170 BITMAPINFO bmp_info;
1172 encode_image_func* encode_image;
1174 unsigned int output_size;
1182 TRACE("%p %p %p %p\n", image, stream, clsid, params);
1184 if(!image || !stream)
1185 return InvalidParameter;
1187 if (!image->picture)
1188 return GenericError;
1190 hr = IPicture_get_Type(image->picture, &type);
1191 if (FAILED(hr) || type != PICTYPE_BITMAP)
1192 return GenericError;
1194 /* select correct encoder */
1195 encode_image = NULL;
1196 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1197 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1198 encode_image = encode_image_funcs[i];
1200 if (encode_image == NULL)
1201 return UnknownImageFormat;
1203 /* extract underlying hbitmap representation from the IPicture */
1204 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1205 if (FAILED(hr) || !hbmp)
1206 return GenericError;
1207 hr = IPicture_get_CurDC(image->picture, &hdc);
1209 return GenericError;
1210 bm_is_selected = (hdc != 0);
1211 if (!bm_is_selected) {
1212 hdc = CreateCompatibleDC(0);
1213 old_hbmp = SelectObject(hdc, hbmp);
1216 /* get bits from HBITMAP */
1217 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1218 bmp_info.bmiHeader.biBitCount = 0;
1219 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1221 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1224 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1226 if (!bm_is_selected) {
1227 SelectObject(hdc, old_hbmp);
1234 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1236 IStream_Write(stream, output, output_size, &dummy);
1244 /*****************************************************************************
1245 * GdipSetImagePalette [GDIPLUS.@]
1247 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1248 GDIPCONST ColorPalette *palette)
1252 if(!image || !palette)
1253 return InvalidParameter;
1256 FIXME("not implemented\n");
1258 return NotImplemented;
1261 /*************************************************************************
1263 * Structures that represent which formats we support for encoding.
1266 /* ImageCodecInfo creation routines taken from libgdiplus */
1267 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1268 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1269 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1270 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1271 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1272 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1274 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1277 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1278 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1279 /* CodecName */ bmp_codecname,
1281 /* FormatDescription */ bmp_format,
1282 /* FilenameExtension */ bmp_extension,
1283 /* MimeType */ bmp_mimetype,
1284 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1288 /* SigPattern */ bmp_sig_pattern,
1289 /* SigMask */ bmp_sig_mask,
1293 /*****************************************************************************
1294 * GdipGetImageDecodersSize [GDIPLUS.@]
1296 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
1298 FIXME("%p %p stub!\n", numDecoders, size);
1300 if (!numDecoders || !size)
1301 return InvalidParameter;
1309 /*****************************************************************************
1310 * GdipGetImageDecoders [GDIPLUS.@]
1312 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
1314 FIXME("%u %u %p stub!\n", numDecoders, size, decoders);
1317 return GenericError;
1319 return NotImplemented;
1322 /*****************************************************************************
1323 * GdipGetImageEncodersSize [GDIPLUS.@]
1325 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1327 TRACE("%p %p\n", numEncoders, size);
1329 if (!numEncoders || !size)
1330 return InvalidParameter;
1332 *numEncoders = NUM_ENCODERS_SUPPORTED;
1333 *size = sizeof (codecs);
1338 /*****************************************************************************
1339 * GdipGetImageEncoders [GDIPLUS.@]
1341 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1343 TRACE("%u %u %p\n", numEncoders, size, encoders);
1346 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1347 (size != sizeof (codecs)))
1348 return GenericError;
1350 memcpy(encoders, codecs, sizeof (codecs));
1355 /*****************************************************************************
1356 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1358 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1364 TRACE("%p %p %p\n", hbm, hpal, bitmap);
1367 return InvalidParameter;
1369 /* TODO: Support for device-dependent bitmaps */
1371 FIXME("no support for device-dependent bitmaps\n");
1372 return NotImplemented;
1375 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1376 return InvalidParameter;
1378 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1379 switch(bm.bmBitsPixel) {
1381 format = PixelFormat1bppIndexed;
1384 format = PixelFormat4bppIndexed;
1387 format = PixelFormat8bppIndexed;
1390 format = PixelFormat24bppRGB;
1393 format = PixelFormat32bppRGB;
1396 format = PixelFormat48bppRGB;
1399 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1400 return InvalidParameter;
1403 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1404 format, bm.bmBits, bitmap);
1409 /*****************************************************************************
1410 * GdipSetEffectParameters [GDIPLUS.@]
1412 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1413 const VOID *params, const UINT size)
1418 FIXME("not implemented\n");
1420 return NotImplemented;
1423 /*****************************************************************************
1424 * GdipGetImageFlags [GDIPLUS.@]
1426 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1428 TRACE("%p %p\n", image, flags);
1430 if(!image || !flags)
1431 return InvalidParameter;
1433 *flags = image->flags;
1438 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
1440 TRACE("(%d, %p)\n", control, param);
1443 case TestControlForceBilinear:
1445 FIXME("TestControlForceBilinear not handled\n");
1447 case TestControlNoICM:
1449 FIXME("TestControlNoICM not handled\n");
1451 case TestControlGetBuildNumber:
1452 *((DWORD*)param) = 3102;
1459 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
1460 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
1461 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
1462 GpMetafile **metafile)
1464 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1465 frameUnit, debugstr_w(desc), metafile);
1467 return NotImplemented;
1470 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
1471 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
1472 GDIPCONST WCHAR *desc, GpMetafile **metafile)
1474 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1475 frameUnit, debugstr_w(desc), metafile);
1477 return NotImplemented;