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)
245 if (!(image && cloneImage)) return InvalidParameter;
247 FIXME("stub: %p, %p\n", image, cloneImage);
249 return NotImplemented;
252 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
258 if(!filename || !bitmap)
259 return InvalidParameter;
261 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
266 stat = GdipCreateBitmapFromStream(stream, bitmap);
268 IStream_Release(stream);
273 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
274 VOID *bits, GpBitmap **bitmap)
276 DWORD height, stride;
279 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
281 height = abs(info->bmiHeader.biHeight);
282 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
284 if(info->bmiHeader.biHeight > 0) /* bottom-up */
286 bits = (BYTE*)bits + (height - 1) * stride;
290 switch(info->bmiHeader.biBitCount) {
292 format = PixelFormat1bppIndexed;
295 format = PixelFormat4bppIndexed;
298 format = PixelFormat8bppIndexed;
301 format = PixelFormat24bppRGB;
304 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
306 return InvalidParameter;
309 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
315 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
318 return GdipCreateBitmapFromFile(filename, bitmap);
321 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
322 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
325 GpStatus stat = InvalidParameter;
327 if(!lpBitmapName || !bitmap)
328 return InvalidParameter;
331 hbm = (HBITMAP)LoadImageW(hInstance,lpBitmapName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
334 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
341 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
342 HBITMAP* hbmReturn, ARGB background)
348 return NotImplemented;
351 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
352 GpMetafile* metafile, BOOL* succ, EmfType emfType,
353 const WCHAR* description, GpMetafile** out_metafile)
357 if(!ref || !metafile || !out_metafile)
358 return InvalidParameter;
361 *out_metafile = NULL;
364 FIXME("not implemented\n");
366 return NotImplemented;
369 /* FIXME: this should create a bitmap in the given size with the attributes
370 * (resolution etc.) of the graphics object */
371 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
372 GpGraphics* target, GpBitmap** bitmap)
377 if(!target || !bitmap)
378 return InvalidParameter;
381 FIXME("hacked stub\n");
383 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
389 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
390 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
392 BITMAPFILEHEADER *bmfh;
393 BITMAPINFOHEADER *bmih;
398 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
400 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
402 return InvalidParameter;
406 return InvalidParameter;
408 /* FIXME: windows allows negative stride (reads backwards from scan0) */
410 FIXME("negative stride\n");
411 return InvalidParameter;
414 *bitmap = GdipAlloc(sizeof(GpBitmap));
415 if(!*bitmap) return OutOfMemory;
418 stride = width * (PIXELFORMATBPP(format) / 8);
419 stride = (stride + 3) & ~3;
422 datalen = abs(stride * height);
423 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
424 buff = GdipAlloc(size);
430 bmfh = (BITMAPFILEHEADER*) buff;
431 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
433 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
435 bmfh->bfOffBits = size - datalen;
437 bmih->biSize = sizeof(BITMAPINFOHEADER);
438 bmih->biWidth = width;
439 bmih->biHeight = -height;
440 /* FIXME: use the rest of the data from format */
441 bmih->biBitCount = PIXELFORMATBPP(format);
442 bmih->biCompression = BI_RGB;
443 bmih->biSizeImage = datalen;
446 memcpy(bmih + 1, scan0, datalen);
448 memset(bmih + 1, 0, datalen);
450 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
451 ERR("could not make stream\n");
457 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
458 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
459 TRACE("Could not load picture\n");
460 IStream_Release(stream);
466 (*bitmap)->image.type = ImageTypeBitmap;
467 (*bitmap)->image.flags = ImageFlagsNone;
468 (*bitmap)->width = width;
469 (*bitmap)->height = height;
470 (*bitmap)->format = format;
475 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
480 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
485 if((*bitmap)->image.type != ImageTypeBitmap){
486 IPicture_Release((*bitmap)->image.picture);
488 return GenericError; /* FIXME: what error to return? */
495 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
498 return GdipCreateBitmapFromStream(stream, bitmap);
501 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
506 return InvalidParameter;
508 IPicture_get_CurDC(image->picture, &hdc);
510 IPicture_Release(image->picture);
511 if (image->type == ImageTypeBitmap)
512 GdipFree(((GpBitmap*)image)->bitmapbits);
518 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
521 return InvalidParameter;
523 return NotImplemented;
526 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
529 if(!image || !srcRect || !srcUnit)
530 return InvalidParameter;
531 if(image->type == ImageTypeMetafile){
532 *srcRect = ((GpMetafile*)image)->bounds;
533 *srcUnit = ((GpMetafile*)image)->unit;
535 else if(image->type == ImageTypeBitmap){
536 srcRect->X = srcRect->Y = 0.0;
537 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
538 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
539 *srcUnit = UnitPixel;
542 srcRect->X = srcRect->Y = 0.0;
543 srcRect->Width = ipicture_pixel_width(image->picture);
544 srcRect->Height = ipicture_pixel_height(image->picture);
545 *srcUnit = UnitPixel;
548 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
549 srcRect->Width, srcRect->Height, *srcUnit);
554 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
557 if(!image || !height || !width)
558 return InvalidParameter;
560 if(image->type == ImageTypeMetafile){
563 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
564 ((GpMetafile*)image)->bounds.Height;
566 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
567 ((GpMetafile*)image)->bounds.Width;
572 else if(image->type == ImageTypeBitmap){
573 *height = ((GpBitmap*)image)->height;
574 *width = ((GpBitmap*)image)->width;
577 *height = ipicture_pixel_height(image->picture);
578 *width = ipicture_pixel_width(image->picture);
581 TRACE("returning (%f, %f)\n", *height, *width);
585 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
586 GpGraphics **graphics)
590 if(!image || !graphics)
591 return InvalidParameter;
593 if(image->type != ImageTypeBitmap){
594 FIXME("not implemented for image type %d\n", image->type);
595 return NotImplemented;
598 IPicture_get_CurDC(image->picture, &hdc);
601 hdc = CreateCompatibleDC(0);
602 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
605 return GdipCreateFromHDC(hdc, graphics);
608 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
610 if(!image || !height)
611 return InvalidParameter;
613 if(image->type == ImageTypeMetafile){
616 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
617 ((GpMetafile*)image)->bounds.Height);
621 else if(image->type == ImageTypeBitmap)
622 *height = ((GpBitmap*)image)->height;
624 *height = ipicture_pixel_height(image->picture);
626 TRACE("returning %d\n", *height);
631 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
636 return InvalidParameter;
639 FIXME("not implemented\n");
641 return NotImplemented;
644 /* FIXME: test this function for non-bitmap types */
645 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
647 if(!image || !format)
648 return InvalidParameter;
650 if(image->type != ImageTypeBitmap)
651 *format = PixelFormat24bppRGB;
653 *format = ((GpBitmap*) image)->format;
658 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
662 if(!image || !format)
663 return InvalidParameter;
666 FIXME("not implemented\n");
668 return NotImplemented;
671 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
674 return InvalidParameter;
681 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
686 return InvalidParameter;
689 FIXME("not implemented\n");
691 return NotImplemented;
694 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
697 return InvalidParameter;
699 if(image->type == ImageTypeMetafile){
702 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
703 ((GpMetafile*)image)->bounds.Width);
707 else if(image->type == ImageTypeBitmap)
708 *width = ((GpBitmap*)image)->width;
710 *width = ipicture_pixel_width(image->picture);
712 TRACE("returning %d\n", *width);
717 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
718 MetafileHeader * header)
722 if(!metafile || !header)
723 return InvalidParameter;
726 FIXME("not implemented\n");
731 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
736 TRACE("%p %x %p\n", image, pid, size);
739 return InvalidParameter;
742 FIXME("not implemented\n");
744 return NotImplemented;
747 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
748 GDIPCONST GUID* dimensionID, UINT* count)
752 if(!image || !dimensionID || !count)
753 return InvalidParameter;
756 FIXME("not implemented\n");
758 return NotImplemented;
761 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
765 return InvalidParameter;
774 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
775 GUID* dimensionIDs, UINT count)
779 if(!image || !dimensionIDs)
780 return InvalidParameter;
783 FIXME("not implemented\n");
788 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
789 GDIPCONST GUID* dimensionID, UINT frameidx)
793 if(!image || !dimensionID)
794 return InvalidParameter;
797 FIXME("not implemented\n");
802 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
808 if (!filename || !image)
809 return InvalidParameter;
811 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
816 stat = GdipLoadImageFromStream(stream, image);
818 IStream_Release(stream);
823 /* FIXME: no icm handling */
824 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
826 return GdipLoadImageFromFile(filename, image);
829 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
834 if(!stream || !image)
835 return InvalidParameter;
837 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
838 (LPVOID*) &pic) != S_OK){
839 TRACE("Could not load picture\n");
843 IPicture_get_Type(pic, &type);
845 if(type == PICTYPE_BITMAP){
847 BITMAPCOREHEADER* bmch;
851 *image = GdipAlloc(sizeof(GpBitmap));
852 if(!*image) return OutOfMemory;
853 (*image)->type = ImageTypeBitmap;
855 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
856 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
858 /* get the pixel format */
859 IPicture_get_Handle(pic, &hbm);
860 IPicture_get_CurDC(pic, &hdc);
862 ZeroMemory(&bmi, sizeof(bmi));
863 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
864 bmch->bcSize = sizeof(BITMAPCOREHEADER);
868 hdc = CreateCompatibleDC(0);
869 old = SelectObject(hdc, (HBITMAP)hbm);
870 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
871 SelectObject(hdc, old);
875 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
877 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
879 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
880 /* FIXME: missing initialization code */
881 *image = GdipAlloc(sizeof(GpMetafile));
882 if(!*image) return OutOfMemory;
883 (*image)->type = ImageTypeMetafile;
886 *image = GdipAlloc(sizeof(GpImage));
887 if(!*image) return OutOfMemory;
888 (*image)->type = ImageTypeUnknown;
891 (*image)->picture = pic;
892 (*image)->flags = ImageFlagsNone;
898 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
900 return GdipLoadImageFromStream(stream, image);
903 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
908 return InvalidParameter;
911 FIXME("not implemented\n");
913 return NotImplemented;
916 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
917 GDIPCONST CLSID *clsidEncoder,
918 GDIPCONST EncoderParameters *encoderParams)
923 if (!image || !filename|| !clsidEncoder)
924 return InvalidParameter;
926 if (!(image->picture))
927 return InvalidParameter;
929 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
933 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
935 IStream_Release(stream);
939 /*************************************************************************
940 * Encoding functions -
941 * These functions encode an image in different image file formats.
943 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
944 #define BITMAP_FORMAT_JPEG 0xd8ff
945 #define BITMAP_FORMAT_GIF 0x4947
946 #define BITMAP_FORMAT_PNG 0x5089
947 #define BITMAP_FORMAT_APM 0xcdd7
949 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
950 void **output, unsigned int *output_size)
952 int num_palette_entries;
953 BITMAPFILEHEADER *bmp_file_hdr;
954 BITMAPINFO *bmp_info_hdr;
956 if (bitmap_info->bmiHeader.biClrUsed) {
957 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
958 if (num_palette_entries > 256) num_palette_entries = 256;
960 if (bitmap_info->bmiHeader.biBitCount <= 8)
961 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
963 num_palette_entries = 0;
967 sizeof(BITMAPFILEHEADER) +
968 sizeof(BITMAPINFOHEADER) +
969 num_palette_entries * sizeof(RGBQUAD) +
970 bitmap_info->bmiHeader.biSizeImage;
972 *output = GdipAlloc(*output_size);
974 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
975 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
976 bmp_file_hdr->bfSize = *output_size;
977 bmp_file_hdr->bfOffBits =
978 sizeof(BITMAPFILEHEADER) +
979 sizeof(BITMAPINFOHEADER) +
980 num_palette_entries * sizeof (RGBQUAD);
982 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
983 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
984 memcpy((unsigned char *)(*output) +
985 sizeof(BITMAPFILEHEADER) +
986 sizeof(BITMAPINFOHEADER) +
987 num_palette_entries * sizeof(RGBQUAD),
988 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
993 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
994 void **output, unsigned int *output_size);
998 NUM_ENCODERS_SUPPORTED
1001 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1002 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1006 /*****************************************************************************
1007 * GdipSaveImageToStream [GDIPLUS.@]
1009 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1010 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1019 BITMAPINFO bmp_info;
1021 encode_image_func* encode_image;
1023 unsigned int output_size;
1031 if(!image || !stream)
1032 return InvalidParameter;
1034 if (!image->picture)
1035 return GenericError;
1037 hr = IPicture_get_Type(image->picture, &type);
1038 if (FAILED(hr) || type != PICTYPE_BITMAP)
1039 return GenericError;
1041 /* select correct encoder */
1042 encode_image = NULL;
1043 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1044 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1045 encode_image = encode_image_funcs[i];
1047 if (encode_image == NULL)
1048 return UnknownImageFormat;
1050 /* extract underlying hbitmap representation from the IPicture */
1051 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1052 if (FAILED(hr) || !hbmp)
1053 return GenericError;
1054 hr = IPicture_get_CurDC(image->picture, &hdc);
1056 return GenericError;
1057 bm_is_selected = (hdc != 0);
1058 if (!bm_is_selected) {
1059 hdc = CreateCompatibleDC(0);
1060 old_hbmp = SelectObject(hdc, hbmp);
1063 /* get bits from HBITMAP */
1064 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1065 bmp_info.bmiHeader.biBitCount = 0;
1066 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1068 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1071 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1073 if (!bm_is_selected) {
1074 SelectObject(hdc, old_hbmp);
1081 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1083 IStream_Write(stream, output, output_size, &dummy);
1091 /*****************************************************************************
1092 * GdipSetImagePalette [GDIPLUS.@]
1094 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1095 GDIPCONST ColorPalette *palette)
1099 if(!image || !palette)
1100 return InvalidParameter;
1103 FIXME("not implemented\n");
1105 return NotImplemented;
1108 /*************************************************************************
1110 * Structures that represent which formats we support for encoding.
1113 /* ImageCodecInfo creation routines taken from libgdiplus */
1114 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1115 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1116 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1117 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1118 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1119 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1121 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1124 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1125 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1126 /* CodecName */ bmp_codecname,
1128 /* FormatDescription */ bmp_format,
1129 /* FilenameExtension */ bmp_extension,
1130 /* MimeType */ bmp_mimetype,
1131 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1135 /* SigPattern */ bmp_sig_pattern,
1136 /* SigMask */ bmp_sig_mask,
1140 /*****************************************************************************
1141 * GdipGetImageEncodersSize [GDIPLUS.@]
1143 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1145 if (!numEncoders || !size)
1146 return InvalidParameter;
1148 *numEncoders = NUM_ENCODERS_SUPPORTED;
1149 *size = sizeof (codecs);
1154 /*****************************************************************************
1155 * GdipGetImageEncoders [GDIPLUS.@]
1157 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1160 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1161 (size != sizeof (codecs)))
1162 return GenericError;
1164 memcpy(encoders, codecs, sizeof (codecs));
1169 /*****************************************************************************
1170 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1172 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1179 return InvalidParameter;
1181 /* TODO: Support for device-dependent bitmaps */
1183 FIXME("no support for device-dependent bitmaps\n");
1184 return NotImplemented;
1187 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1188 return InvalidParameter;
1190 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1191 switch(bm.bmBitsPixel) {
1193 format = PixelFormat1bppIndexed;
1196 format = PixelFormat4bppIndexed;
1199 format = PixelFormat8bppIndexed;
1202 format = PixelFormat24bppRGB;
1205 format = PixelFormat48bppRGB;
1208 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1209 return InvalidParameter;
1212 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1213 format, bm.bmBits, bitmap);
1218 /*****************************************************************************
1219 * GdipSetEffectParameters [GDIPLUS.@]
1221 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1222 const VOID *params, const UINT size)
1227 FIXME("not implemented\n");
1229 return NotImplemented;
1232 /*****************************************************************************
1233 * GdipGetImageFlags [GDIPLUS.@]
1235 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1237 if(!image || !flags)
1238 return InvalidParameter;
1240 *flags = image->flags;