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 GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
322 HBITMAP* hbmReturn, ARGB background)
328 return NotImplemented;
331 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
332 GpMetafile* metafile, BOOL* succ, EmfType emfType,
333 const WCHAR* description, GpMetafile** out_metafile)
337 if(!ref || !metafile || !out_metafile)
338 return InvalidParameter;
341 *out_metafile = NULL;
344 FIXME("not implemented\n");
346 return NotImplemented;
349 /* FIXME: this should create a bitmap in the given size with the attributes
350 * (resolution etc.) of the graphics object */
351 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
352 GpGraphics* target, GpBitmap** bitmap)
357 if(!target || !bitmap)
358 return InvalidParameter;
361 FIXME("hacked stub\n");
363 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
369 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
370 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
372 BITMAPFILEHEADER *bmfh;
373 BITMAPINFOHEADER *bmih;
378 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
380 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
382 return InvalidParameter;
386 return InvalidParameter;
388 /* FIXME: windows allows negative stride (reads backwards from scan0) */
390 FIXME("negative stride\n");
391 return InvalidParameter;
394 *bitmap = GdipAlloc(sizeof(GpBitmap));
395 if(!*bitmap) return OutOfMemory;
398 stride = width * (PIXELFORMATBPP(format) / 8);
399 stride = (stride + 3) & ~3;
402 datalen = abs(stride * height);
403 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
404 buff = GdipAlloc(size);
410 bmfh = (BITMAPFILEHEADER*) buff;
411 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
413 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
415 bmfh->bfOffBits = size - datalen;
417 bmih->biSize = sizeof(BITMAPINFOHEADER);
418 bmih->biWidth = width;
419 bmih->biHeight = -height;
420 /* FIXME: use the rest of the data from format */
421 bmih->biBitCount = PIXELFORMATBPP(format);
422 bmih->biCompression = BI_RGB;
423 bmih->biSizeImage = datalen;
426 memcpy(bmih + 1, scan0, datalen);
428 memset(bmih + 1, 0, datalen);
430 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
431 ERR("could not make stream\n");
437 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
438 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
439 TRACE("Could not load picture\n");
440 IStream_Release(stream);
446 (*bitmap)->image.type = ImageTypeBitmap;
447 (*bitmap)->image.flags = ImageFlagsNone;
448 (*bitmap)->width = width;
449 (*bitmap)->height = height;
450 (*bitmap)->format = format;
455 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
460 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
465 if((*bitmap)->image.type != ImageTypeBitmap){
466 IPicture_Release((*bitmap)->image.picture);
468 return GenericError; /* FIXME: what error to return? */
475 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
478 return GdipCreateBitmapFromStream(stream, bitmap);
481 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
486 return InvalidParameter;
488 IPicture_get_CurDC(image->picture, &hdc);
490 IPicture_Release(image->picture);
491 if (image->type == ImageTypeBitmap)
492 GdipFree(((GpBitmap*)image)->bitmapbits);
498 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
501 return InvalidParameter;
503 return NotImplemented;
506 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
509 if(!image || !srcRect || !srcUnit)
510 return InvalidParameter;
511 if(image->type == ImageTypeMetafile){
512 *srcRect = ((GpMetafile*)image)->bounds;
513 *srcUnit = ((GpMetafile*)image)->unit;
515 else if(image->type == ImageTypeBitmap){
516 srcRect->X = srcRect->Y = 0.0;
517 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
518 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
519 *srcUnit = UnitPixel;
522 srcRect->X = srcRect->Y = 0.0;
523 srcRect->Width = ipicture_pixel_width(image->picture);
524 srcRect->Height = ipicture_pixel_height(image->picture);
525 *srcUnit = UnitPixel;
528 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
529 srcRect->Width, srcRect->Height, *srcUnit);
534 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
537 if(!image || !height || !width)
538 return InvalidParameter;
540 if(image->type == ImageTypeMetafile){
543 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
544 ((GpMetafile*)image)->bounds.Height;
546 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
547 ((GpMetafile*)image)->bounds.Width;
552 else if(image->type == ImageTypeBitmap){
553 *height = ((GpBitmap*)image)->height;
554 *width = ((GpBitmap*)image)->width;
557 *height = ipicture_pixel_height(image->picture);
558 *width = ipicture_pixel_width(image->picture);
561 TRACE("returning (%f, %f)\n", *height, *width);
565 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
566 GpGraphics **graphics)
570 if(!image || !graphics)
571 return InvalidParameter;
573 if(image->type != ImageTypeBitmap){
574 FIXME("not implemented for image type %d\n", image->type);
575 return NotImplemented;
578 IPicture_get_CurDC(image->picture, &hdc);
581 hdc = CreateCompatibleDC(0);
582 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
585 return GdipCreateFromHDC(hdc, graphics);
588 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
590 if(!image || !height)
591 return InvalidParameter;
593 if(image->type == ImageTypeMetafile){
596 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
597 ((GpMetafile*)image)->bounds.Height);
601 else if(image->type == ImageTypeBitmap)
602 *height = ((GpBitmap*)image)->height;
604 *height = ipicture_pixel_height(image->picture);
606 TRACE("returning %d\n", *height);
611 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
616 return InvalidParameter;
619 FIXME("not implemented\n");
621 return NotImplemented;
624 /* FIXME: test this function for non-bitmap types */
625 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
627 if(!image || !format)
628 return InvalidParameter;
630 if(image->type != ImageTypeBitmap)
631 *format = PixelFormat24bppRGB;
633 *format = ((GpBitmap*) image)->format;
638 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
642 if(!image || !format)
643 return InvalidParameter;
646 FIXME("not implemented\n");
648 return NotImplemented;
651 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
654 return InvalidParameter;
661 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
666 return InvalidParameter;
669 FIXME("not implemented\n");
671 return NotImplemented;
674 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
677 return InvalidParameter;
679 if(image->type == ImageTypeMetafile){
682 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
683 ((GpMetafile*)image)->bounds.Width);
687 else if(image->type == ImageTypeBitmap)
688 *width = ((GpBitmap*)image)->width;
690 *width = ipicture_pixel_width(image->picture);
692 TRACE("returning %d\n", *width);
697 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
698 MetafileHeader * header)
702 if(!metafile || !header)
703 return InvalidParameter;
706 FIXME("not implemented\n");
711 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
716 TRACE("%p %x %p\n", image, pid, size);
719 return InvalidParameter;
722 FIXME("not implemented\n");
724 return NotImplemented;
727 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
728 GDIPCONST GUID* dimensionID, UINT* count)
732 if(!image || !dimensionID || !count)
733 return InvalidParameter;
736 FIXME("not implemented\n");
738 return NotImplemented;
741 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
745 return InvalidParameter;
754 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
755 GUID* dimensionIDs, UINT count)
759 if(!image || !dimensionIDs)
760 return InvalidParameter;
763 FIXME("not implemented\n");
768 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
769 GDIPCONST GUID* dimensionID, UINT frameidx)
773 if(!image || !dimensionID)
774 return InvalidParameter;
777 FIXME("not implemented\n");
782 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
788 if (!filename || !image)
789 return InvalidParameter;
791 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
796 stat = GdipLoadImageFromStream(stream, image);
798 IStream_Release(stream);
803 /* FIXME: no icm handling */
804 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
806 return GdipLoadImageFromFile(filename, image);
809 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
814 if(!stream || !image)
815 return InvalidParameter;
817 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
818 (LPVOID*) &pic) != S_OK){
819 TRACE("Could not load picture\n");
823 IPicture_get_Type(pic, &type);
825 if(type == PICTYPE_BITMAP){
827 BITMAPCOREHEADER* bmch;
831 *image = GdipAlloc(sizeof(GpBitmap));
832 if(!*image) return OutOfMemory;
833 (*image)->type = ImageTypeBitmap;
835 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
836 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
838 /* get the pixel format */
839 IPicture_get_Handle(pic, &hbm);
840 IPicture_get_CurDC(pic, &hdc);
842 ZeroMemory(&bmi, sizeof(bmi));
843 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
844 bmch->bcSize = sizeof(BITMAPCOREHEADER);
848 hdc = CreateCompatibleDC(0);
849 old = SelectObject(hdc, (HBITMAP)hbm);
850 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
851 SelectObject(hdc, old);
855 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
857 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
859 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
860 /* FIXME: missing initialization code */
861 *image = GdipAlloc(sizeof(GpMetafile));
862 if(!*image) return OutOfMemory;
863 (*image)->type = ImageTypeMetafile;
866 *image = GdipAlloc(sizeof(GpImage));
867 if(!*image) return OutOfMemory;
868 (*image)->type = ImageTypeUnknown;
871 (*image)->picture = pic;
872 (*image)->flags = ImageFlagsNone;
878 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
880 return GdipLoadImageFromStream(stream, image);
883 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
888 return InvalidParameter;
891 FIXME("not implemented\n");
893 return NotImplemented;
896 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
897 GDIPCONST CLSID *clsidEncoder,
898 GDIPCONST EncoderParameters *encoderParams)
903 if (!image || !filename|| !clsidEncoder)
904 return InvalidParameter;
906 if (!(image->picture))
907 return InvalidParameter;
909 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
913 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
915 IStream_Release(stream);
919 /*************************************************************************
920 * Encoding functions -
921 * These functions encode an image in different image file formats.
923 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
924 #define BITMAP_FORMAT_JPEG 0xd8ff
925 #define BITMAP_FORMAT_GIF 0x4947
926 #define BITMAP_FORMAT_PNG 0x5089
927 #define BITMAP_FORMAT_APM 0xcdd7
929 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
930 void **output, unsigned int *output_size)
932 int num_palette_entries;
933 BITMAPFILEHEADER *bmp_file_hdr;
934 BITMAPINFO *bmp_info_hdr;
936 if (bitmap_info->bmiHeader.biClrUsed) {
937 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
938 if (num_palette_entries > 256) num_palette_entries = 256;
940 if (bitmap_info->bmiHeader.biBitCount <= 8)
941 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
943 num_palette_entries = 0;
947 sizeof(BITMAPFILEHEADER) +
948 sizeof(BITMAPINFOHEADER) +
949 num_palette_entries * sizeof(RGBQUAD) +
950 bitmap_info->bmiHeader.biSizeImage;
952 *output = GdipAlloc(*output_size);
954 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
955 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
956 bmp_file_hdr->bfSize = *output_size;
957 bmp_file_hdr->bfOffBits =
958 sizeof(BITMAPFILEHEADER) +
959 sizeof(BITMAPINFOHEADER) +
960 num_palette_entries * sizeof (RGBQUAD);
962 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
963 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
964 memcpy((unsigned char *)(*output) +
965 sizeof(BITMAPFILEHEADER) +
966 sizeof(BITMAPINFOHEADER) +
967 num_palette_entries * sizeof(RGBQUAD),
968 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
973 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
974 void **output, unsigned int *output_size);
978 NUM_ENCODERS_SUPPORTED
981 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
982 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
986 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
987 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
998 encode_image_func* encode_image;
1000 unsigned int output_size;
1008 if(!image || !stream)
1009 return InvalidParameter;
1011 if (!image->picture)
1012 return GenericError;
1014 hr = IPicture_get_Type(image->picture, &type);
1015 if (FAILED(hr) || type != PICTYPE_BITMAP)
1016 return GenericError;
1018 /* select correct encoder */
1019 encode_image = NULL;
1020 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1021 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1022 encode_image = encode_image_funcs[i];
1024 if (encode_image == NULL)
1025 return UnknownImageFormat;
1027 /* extract underlying hbitmap representation from the IPicture */
1028 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1029 if (FAILED(hr) || !hbmp)
1030 return GenericError;
1031 hr = IPicture_get_CurDC(image->picture, &hdc);
1033 return GenericError;
1034 bm_is_selected = (hdc != 0);
1035 if (!bm_is_selected) {
1036 hdc = CreateCompatibleDC(0);
1037 old_hbmp = SelectObject(hdc, hbmp);
1040 /* get bits from HBITMAP */
1041 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1042 bmp_info.bmiHeader.biBitCount = 0;
1043 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1045 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1048 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1050 if (!bm_is_selected) {
1051 SelectObject(hdc, old_hbmp);
1058 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1060 IStream_Write(stream, output, output_size, &dummy);
1068 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1069 GDIPCONST ColorPalette *palette)
1073 if(!image || !palette)
1074 return InvalidParameter;
1077 FIXME("not implemented\n");
1079 return NotImplemented;
1082 /*************************************************************************
1084 * Structures that represent which formats we support for encoding.
1087 /* ImageCodecInfo creation routines taken from libgdiplus */
1088 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1089 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1090 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1091 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1092 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1093 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1095 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1098 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1099 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1100 /* CodecName */ bmp_codecname,
1102 /* FormatDescription */ bmp_format,
1103 /* FilenameExtension */ bmp_extension,
1104 /* MimeType */ bmp_mimetype,
1105 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1109 /* SigPattern */ bmp_sig_pattern,
1110 /* SigMask */ bmp_sig_mask,
1114 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1116 if (!numEncoders || !size)
1117 return InvalidParameter;
1119 *numEncoders = NUM_ENCODERS_SUPPORTED;
1120 *size = sizeof (codecs);
1125 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1128 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1129 (size != sizeof (codecs)))
1130 return GenericError;
1132 memcpy(encoders, codecs, sizeof (codecs));
1136 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1143 return InvalidParameter;
1145 /* TODO: Support for device-dependent bitmaps */
1147 FIXME("no support for device-dependent bitmaps\n");
1148 return NotImplemented;
1151 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1152 return InvalidParameter;
1154 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1155 switch(bm.bmBitsPixel) {
1157 format = PixelFormat1bppIndexed;
1160 format = PixelFormat4bppIndexed;
1163 format = PixelFormat8bppIndexed;
1166 format = PixelFormat24bppRGB;
1169 format = PixelFormat48bppRGB;
1172 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1173 return InvalidParameter;
1176 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1177 format, bm.bmBits, bitmap);
1182 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1183 const VOID *params, const UINT size)
1188 FIXME("not implemented\n");
1190 return NotImplemented;
1193 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1195 if(!image || !flags)
1196 return InvalidParameter;
1198 *flags = image->flags;