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 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
135 bmi.bmiHeader.biBitCount = 0;
138 hdc = CreateCompatibleDC(0);
139 old = SelectObject(hdc, (HBITMAP)hbm);
143 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
145 abs_height = abs(bmi.bmiHeader.biHeight);
146 stride = bmi.bmiHeader.biWidth * bitspp / 8;
147 stride = (stride + 3) & ~3;
149 buff = GdipAlloc(stride * abs_height);
151 bmi.bmiHeader.biBitCount = bitspp;
154 GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, &bmi, DIB_RGB_COLORS);
157 SelectObject(hdc, old);
164 lockeddata->Width = act_rect.Width;
165 lockeddata->Height = act_rect.Height;
166 lockeddata->PixelFormat = format;
167 lockeddata->Reserved = flags;
169 if(bmi.bmiHeader.biHeight > 0){
170 lockeddata->Stride = -stride;
171 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
172 stride * (abs_height - 1 - act_rect.Y);
175 lockeddata->Stride = stride;
176 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
179 bitmap->lockmode = flags;
182 bitmap->bitmapbits = buff;
187 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
188 BitmapData* lockeddata)
196 if(!bitmap || !lockeddata)
197 return InvalidParameter;
199 if(!bitmap->lockmode)
202 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
203 return NotImplemented;
205 if(lockeddata->Reserved & ImageLockModeRead){
206 if(!(--bitmap->numlocks))
207 bitmap->lockmode = 0;
209 GdipFree(bitmap->bitmapbits);
210 bitmap->bitmapbits = NULL;
214 IPicture_get_Handle(bitmap->image.picture, &hbm);
215 IPicture_get_CurDC(bitmap->image.picture, &hdc);
216 bm_is_selected = (hdc != 0);
218 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
219 bmi.bmiHeader.biBitCount = 0;
222 hdc = CreateCompatibleDC(0);
223 old = SelectObject(hdc, (HBITMAP)hbm);
226 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
227 bmi.bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
228 SetDIBits(hdc, (HBITMAP)hbm, 0, abs(bmi.bmiHeader.biHeight),
229 bitmap->bitmapbits, &bmi, DIB_RGB_COLORS);
232 SelectObject(hdc, old);
236 GdipFree(bitmap->bitmapbits);
237 bitmap->bitmapbits = NULL;
238 bitmap->lockmode = 0;
243 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
249 TRACE("%p, %p\n", image, cloneImage);
251 if (!image || !cloneImage)
252 return InvalidParameter;
254 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
258 *cloneImage = GdipAlloc(sizeof(GpImage));
261 IStream_Release(stream);
264 (*cloneImage)->type = image->type;
265 (*cloneImage)->flags = image->flags;
267 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
270 WARN("Failed to save image on stream\n");
274 hr = OleLoadPicture(stream, size, FALSE, &IID_IPicture,
275 (LPVOID*) &(*cloneImage)->picture);
278 WARN("Failed to load image from stream\n");
282 IStream_Release(stream);
285 IStream_Release(stream);
286 GdipFree(*cloneImage);
291 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
297 if(!filename || !bitmap)
298 return InvalidParameter;
300 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
305 stat = GdipCreateBitmapFromStream(stream, bitmap);
307 IStream_Release(stream);
312 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
313 VOID *bits, GpBitmap **bitmap)
315 DWORD height, stride;
318 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
320 height = abs(info->bmiHeader.biHeight);
321 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
323 if(info->bmiHeader.biHeight > 0) /* bottom-up */
325 bits = (BYTE*)bits + (height - 1) * stride;
329 switch(info->bmiHeader.biBitCount) {
331 format = PixelFormat1bppIndexed;
334 format = PixelFormat4bppIndexed;
337 format = PixelFormat8bppIndexed;
340 format = PixelFormat24bppRGB;
343 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
345 return InvalidParameter;
348 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
354 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
357 return GdipCreateBitmapFromFile(filename, bitmap);
360 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
361 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
364 GpStatus stat = InvalidParameter;
366 if(!lpBitmapName || !bitmap)
367 return InvalidParameter;
370 hbm = (HBITMAP)LoadImageW(hInstance,lpBitmapName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
373 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
380 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
381 HBITMAP* hbmReturn, ARGB background)
387 return NotImplemented;
390 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
391 GpMetafile* metafile, BOOL* succ, EmfType emfType,
392 const WCHAR* description, GpMetafile** out_metafile)
396 if(!ref || !metafile || !out_metafile)
397 return InvalidParameter;
400 *out_metafile = NULL;
403 FIXME("not implemented\n");
405 return NotImplemented;
408 /* FIXME: this should create a bitmap in the given size with the attributes
409 * (resolution etc.) of the graphics object */
410 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
411 GpGraphics* target, GpBitmap** bitmap)
416 if(!target || !bitmap)
417 return InvalidParameter;
420 FIXME("hacked stub\n");
422 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
428 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
429 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
431 BITMAPFILEHEADER *bmfh;
432 BITMAPINFOHEADER *bmih;
437 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
439 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
441 return InvalidParameter;
445 return InvalidParameter;
447 /* FIXME: windows allows negative stride (reads backwards from scan0) */
449 FIXME("negative stride\n");
450 return InvalidParameter;
453 *bitmap = GdipAlloc(sizeof(GpBitmap));
454 if(!*bitmap) return OutOfMemory;
457 stride = width * (PIXELFORMATBPP(format) / 8);
458 stride = (stride + 3) & ~3;
461 datalen = abs(stride * height);
462 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
463 buff = GdipAlloc(size);
469 bmfh = (BITMAPFILEHEADER*) buff;
470 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
472 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
474 bmfh->bfOffBits = size - datalen;
476 bmih->biSize = sizeof(BITMAPINFOHEADER);
477 bmih->biWidth = width;
478 bmih->biHeight = -height;
479 /* FIXME: use the rest of the data from format */
480 bmih->biBitCount = PIXELFORMATBPP(format);
481 bmih->biCompression = BI_RGB;
482 bmih->biSizeImage = datalen;
485 memcpy(bmih + 1, scan0, datalen);
487 memset(bmih + 1, 0, datalen);
489 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
490 ERR("could not make stream\n");
496 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
497 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
498 TRACE("Could not load picture\n");
499 IStream_Release(stream);
505 (*bitmap)->image.type = ImageTypeBitmap;
506 (*bitmap)->image.flags = ImageFlagsNone;
507 (*bitmap)->width = width;
508 (*bitmap)->height = height;
509 (*bitmap)->format = format;
514 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
519 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
524 if((*bitmap)->image.type != ImageTypeBitmap){
525 IPicture_Release((*bitmap)->image.picture);
527 return GenericError; /* FIXME: what error to return? */
534 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
537 return GdipCreateBitmapFromStream(stream, bitmap);
540 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
545 return InvalidParameter;
547 IPicture_get_CurDC(image->picture, &hdc);
549 IPicture_Release(image->picture);
550 if (image->type == ImageTypeBitmap)
551 GdipFree(((GpBitmap*)image)->bitmapbits);
557 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
560 return InvalidParameter;
562 return NotImplemented;
565 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
568 if(!image || !srcRect || !srcUnit)
569 return InvalidParameter;
570 if(image->type == ImageTypeMetafile){
571 *srcRect = ((GpMetafile*)image)->bounds;
572 *srcUnit = ((GpMetafile*)image)->unit;
574 else if(image->type == ImageTypeBitmap){
575 srcRect->X = srcRect->Y = 0.0;
576 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
577 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
578 *srcUnit = UnitPixel;
581 srcRect->X = srcRect->Y = 0.0;
582 srcRect->Width = ipicture_pixel_width(image->picture);
583 srcRect->Height = ipicture_pixel_height(image->picture);
584 *srcUnit = UnitPixel;
587 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
588 srcRect->Width, srcRect->Height, *srcUnit);
593 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
596 if(!image || !height || !width)
597 return InvalidParameter;
599 if(image->type == ImageTypeMetafile){
602 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
603 ((GpMetafile*)image)->bounds.Height;
605 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
606 ((GpMetafile*)image)->bounds.Width;
611 else if(image->type == ImageTypeBitmap){
612 *height = ((GpBitmap*)image)->height;
613 *width = ((GpBitmap*)image)->width;
616 *height = ipicture_pixel_height(image->picture);
617 *width = ipicture_pixel_width(image->picture);
620 TRACE("returning (%f, %f)\n", *height, *width);
624 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
625 GpGraphics **graphics)
629 if(!image || !graphics)
630 return InvalidParameter;
632 if(image->type != ImageTypeBitmap){
633 FIXME("not implemented for image type %d\n", image->type);
634 return NotImplemented;
637 IPicture_get_CurDC(image->picture, &hdc);
640 hdc = CreateCompatibleDC(0);
641 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
644 return GdipCreateFromHDC(hdc, graphics);
647 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
649 if(!image || !height)
650 return InvalidParameter;
652 if(image->type == ImageTypeMetafile){
655 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
656 ((GpMetafile*)image)->bounds.Height);
660 else if(image->type == ImageTypeBitmap)
661 *height = ((GpBitmap*)image)->height;
663 *height = ipicture_pixel_height(image->picture);
665 TRACE("returning %d\n", *height);
670 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
675 return InvalidParameter;
678 FIXME("not implemented\n");
680 return NotImplemented;
683 /* FIXME: test this function for non-bitmap types */
684 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
686 if(!image || !format)
687 return InvalidParameter;
689 if(image->type != ImageTypeBitmap)
690 *format = PixelFormat24bppRGB;
692 *format = ((GpBitmap*) image)->format;
697 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
701 if(!image || !format)
702 return InvalidParameter;
705 FIXME("not implemented\n");
707 return NotImplemented;
710 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
713 return InvalidParameter;
720 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
725 return InvalidParameter;
728 FIXME("not implemented\n");
730 return NotImplemented;
733 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
736 return InvalidParameter;
738 if(image->type == ImageTypeMetafile){
741 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
742 ((GpMetafile*)image)->bounds.Width);
746 else if(image->type == ImageTypeBitmap)
747 *width = ((GpBitmap*)image)->width;
749 *width = ipicture_pixel_width(image->picture);
751 TRACE("returning %d\n", *width);
756 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
757 MetafileHeader * header)
761 if(!metafile || !header)
762 return InvalidParameter;
765 FIXME("not implemented\n");
770 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
771 UINT num, PropertyItem* items)
776 FIXME("not implemented\n");
778 return InvalidParameter;
781 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
786 FIXME("not implemented\n");
788 return InvalidParameter;
791 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
796 FIXME("not implemented\n");
798 return InvalidParameter;
801 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
802 PropertyItem* buffer)
807 FIXME("not implemented\n");
809 return InvalidParameter;
812 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
817 TRACE("%p %x %p\n", image, pid, size);
820 return InvalidParameter;
823 FIXME("not implemented\n");
825 return NotImplemented;
828 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
833 FIXME("not implemented\n");
835 return InvalidParameter;
838 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
839 GDIPCONST GUID* dimensionID, UINT* count)
843 if(!image || !dimensionID || !count)
844 return InvalidParameter;
847 FIXME("not implemented\n");
849 return NotImplemented;
852 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
856 return InvalidParameter;
865 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
866 GUID* dimensionIDs, UINT count)
870 if(!image || !dimensionIDs)
871 return InvalidParameter;
874 FIXME("not implemented\n");
879 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
880 GDIPCONST GUID* dimensionID, UINT frameidx)
884 if(!image || !dimensionID)
885 return InvalidParameter;
888 FIXME("not implemented\n");
893 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
899 if (!filename || !image)
900 return InvalidParameter;
902 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
907 stat = GdipLoadImageFromStream(stream, image);
909 IStream_Release(stream);
914 /* FIXME: no icm handling */
915 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
917 return GdipLoadImageFromFile(filename, image);
920 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
925 if(!stream || !image)
926 return InvalidParameter;
928 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
929 (LPVOID*) &pic) != S_OK){
930 TRACE("Could not load picture\n");
934 IPicture_get_Type(pic, &type);
936 if(type == PICTYPE_BITMAP){
938 BITMAPCOREHEADER* bmch;
942 *image = GdipAlloc(sizeof(GpBitmap));
943 if(!*image) return OutOfMemory;
944 (*image)->type = ImageTypeBitmap;
946 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
947 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
949 /* get the pixel format */
950 IPicture_get_Handle(pic, &hbm);
951 IPicture_get_CurDC(pic, &hdc);
953 ZeroMemory(&bmi, sizeof(bmi));
954 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
955 bmch->bcSize = sizeof(BITMAPCOREHEADER);
959 hdc = CreateCompatibleDC(0);
960 old = SelectObject(hdc, (HBITMAP)hbm);
961 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
962 SelectObject(hdc, old);
966 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
968 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
970 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
971 /* FIXME: missing initialization code */
972 *image = GdipAlloc(sizeof(GpMetafile));
973 if(!*image) return OutOfMemory;
974 (*image)->type = ImageTypeMetafile;
977 *image = GdipAlloc(sizeof(GpImage));
978 if(!*image) return OutOfMemory;
979 (*image)->type = ImageTypeUnknown;
982 (*image)->picture = pic;
983 (*image)->flags = ImageFlagsNone;
989 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
991 return GdipLoadImageFromStream(stream, image);
994 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
999 return InvalidParameter;
1002 FIXME("not implemented\n");
1004 return NotImplemented;
1007 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1012 FIXME("not implemented\n");
1014 return NotImplemented;
1017 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1018 GDIPCONST CLSID *clsidEncoder,
1019 GDIPCONST EncoderParameters *encoderParams)
1024 if (!image || !filename|| !clsidEncoder)
1025 return InvalidParameter;
1027 if (!(image->picture))
1028 return InvalidParameter;
1030 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1032 return GenericError;
1034 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1036 IStream_Release(stream);
1040 /*************************************************************************
1041 * Encoding functions -
1042 * These functions encode an image in different image file formats.
1044 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1045 #define BITMAP_FORMAT_JPEG 0xd8ff
1046 #define BITMAP_FORMAT_GIF 0x4947
1047 #define BITMAP_FORMAT_PNG 0x5089
1048 #define BITMAP_FORMAT_APM 0xcdd7
1050 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1051 void **output, unsigned int *output_size)
1053 int num_palette_entries;
1054 BITMAPFILEHEADER *bmp_file_hdr;
1055 BITMAPINFO *bmp_info_hdr;
1057 if (bitmap_info->bmiHeader.biClrUsed) {
1058 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
1059 if (num_palette_entries > 256) num_palette_entries = 256;
1061 if (bitmap_info->bmiHeader.biBitCount <= 8)
1062 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1064 num_palette_entries = 0;
1068 sizeof(BITMAPFILEHEADER) +
1069 sizeof(BITMAPINFOHEADER) +
1070 num_palette_entries * sizeof(RGBQUAD) +
1071 bitmap_info->bmiHeader.biSizeImage;
1073 *output = GdipAlloc(*output_size);
1075 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
1076 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1077 bmp_file_hdr->bfSize = *output_size;
1078 bmp_file_hdr->bfOffBits =
1079 sizeof(BITMAPFILEHEADER) +
1080 sizeof(BITMAPINFOHEADER) +
1081 num_palette_entries * sizeof (RGBQUAD);
1083 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1084 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1085 memcpy((unsigned char *)(*output) +
1086 sizeof(BITMAPFILEHEADER) +
1087 sizeof(BITMAPINFOHEADER) +
1088 num_palette_entries * sizeof(RGBQUAD),
1089 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1094 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1095 void **output, unsigned int *output_size);
1099 NUM_ENCODERS_SUPPORTED
1102 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1103 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1107 /*****************************************************************************
1108 * GdipSaveImageToStream [GDIPLUS.@]
1110 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1111 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1120 BITMAPINFO bmp_info;
1122 encode_image_func* encode_image;
1124 unsigned int output_size;
1132 if(!image || !stream)
1133 return InvalidParameter;
1135 if (!image->picture)
1136 return GenericError;
1138 hr = IPicture_get_Type(image->picture, &type);
1139 if (FAILED(hr) || type != PICTYPE_BITMAP)
1140 return GenericError;
1142 /* select correct encoder */
1143 encode_image = NULL;
1144 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1145 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1146 encode_image = encode_image_funcs[i];
1148 if (encode_image == NULL)
1149 return UnknownImageFormat;
1151 /* extract underlying hbitmap representation from the IPicture */
1152 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1153 if (FAILED(hr) || !hbmp)
1154 return GenericError;
1155 hr = IPicture_get_CurDC(image->picture, &hdc);
1157 return GenericError;
1158 bm_is_selected = (hdc != 0);
1159 if (!bm_is_selected) {
1160 hdc = CreateCompatibleDC(0);
1161 old_hbmp = SelectObject(hdc, hbmp);
1164 /* get bits from HBITMAP */
1165 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1166 bmp_info.bmiHeader.biBitCount = 0;
1167 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1169 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1172 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1174 if (!bm_is_selected) {
1175 SelectObject(hdc, old_hbmp);
1182 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1184 IStream_Write(stream, output, output_size, &dummy);
1192 /*****************************************************************************
1193 * GdipSetImagePalette [GDIPLUS.@]
1195 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1196 GDIPCONST ColorPalette *palette)
1200 if(!image || !palette)
1201 return InvalidParameter;
1204 FIXME("not implemented\n");
1206 return NotImplemented;
1209 /*************************************************************************
1211 * Structures that represent which formats we support for encoding.
1214 /* ImageCodecInfo creation routines taken from libgdiplus */
1215 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1216 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1217 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1218 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1219 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1220 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1222 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1225 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1226 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1227 /* CodecName */ bmp_codecname,
1229 /* FormatDescription */ bmp_format,
1230 /* FilenameExtension */ bmp_extension,
1231 /* MimeType */ bmp_mimetype,
1232 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1236 /* SigPattern */ bmp_sig_pattern,
1237 /* SigMask */ bmp_sig_mask,
1241 /*****************************************************************************
1242 * GdipGetImageEncodersSize [GDIPLUS.@]
1244 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1246 if (!numEncoders || !size)
1247 return InvalidParameter;
1249 *numEncoders = NUM_ENCODERS_SUPPORTED;
1250 *size = sizeof (codecs);
1255 /*****************************************************************************
1256 * GdipGetImageEncoders [GDIPLUS.@]
1258 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1261 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1262 (size != sizeof (codecs)))
1263 return GenericError;
1265 memcpy(encoders, codecs, sizeof (codecs));
1270 /*****************************************************************************
1271 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1273 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1280 return InvalidParameter;
1282 /* TODO: Support for device-dependent bitmaps */
1284 FIXME("no support for device-dependent bitmaps\n");
1285 return NotImplemented;
1288 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1289 return InvalidParameter;
1291 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1292 switch(bm.bmBitsPixel) {
1294 format = PixelFormat1bppIndexed;
1297 format = PixelFormat4bppIndexed;
1300 format = PixelFormat8bppIndexed;
1303 format = PixelFormat24bppRGB;
1306 format = PixelFormat48bppRGB;
1309 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1310 return InvalidParameter;
1313 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1314 format, bm.bmBits, bitmap);
1319 /*****************************************************************************
1320 * GdipSetEffectParameters [GDIPLUS.@]
1322 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1323 const VOID *params, const UINT size)
1328 FIXME("not implemented\n");
1330 return NotImplemented;
1333 /*****************************************************************************
1334 * GdipGetImageFlags [GDIPLUS.@]
1336 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1338 if(!image || !flags)
1339 return InvalidParameter;
1341 *flags = image->flags;
1346 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
1348 TRACE("(%d, %p)\n", control, param);
1351 case TestControlForceBilinear:
1353 FIXME("TestControlForceBilinear not handled\n");
1355 case TestControlNoICM:
1357 FIXME("TestControlNoICM not handled\n");
1359 case TestControlGetBuildNumber:
1360 *((DWORD*)param) = 3102;