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", 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);
274 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
277 return GdipCreateBitmapFromFile(filename, bitmap);
280 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
281 HBITMAP* hbmReturn, ARGB background)
287 return NotImplemented;
290 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
291 GpMetafile* metafile, BOOL* succ, EmfType emfType,
292 const WCHAR* description, GpMetafile** out_metafile)
296 if(!ref || !metafile || !out_metafile)
297 return InvalidParameter;
300 *out_metafile = NULL;
303 FIXME("not implemented\n");
305 return NotImplemented;
308 /* FIXME: this should create a bitmap in the given size with the attributes
309 * (resolution etc.) of the graphics object */
310 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
311 GpGraphics* target, GpBitmap** bitmap)
316 if(!target || !bitmap)
317 return InvalidParameter;
320 FIXME("hacked stub\n");
322 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
328 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
329 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
331 BITMAPFILEHEADER *bmfh;
332 BITMAPINFOHEADER *bmih;
337 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
339 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
341 return InvalidParameter;
345 return InvalidParameter;
347 /* FIXME: windows allows negative stride (reads backwards from scan0) */
349 FIXME("negative stride\n");
350 return InvalidParameter;
353 *bitmap = GdipAlloc(sizeof(GpBitmap));
354 if(!*bitmap) return OutOfMemory;
357 stride = width * (PIXELFORMATBPP(format) / 8);
358 stride = (stride + 3) & ~3;
361 datalen = abs(stride * height);
362 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
363 buff = GdipAlloc(size);
369 bmfh = (BITMAPFILEHEADER*) buff;
370 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
372 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
374 bmfh->bfOffBits = size - datalen;
376 bmih->biSize = sizeof(BITMAPINFOHEADER);
377 bmih->biWidth = width;
378 bmih->biHeight = -height;
379 /* FIXME: use the rest of the data from format */
380 bmih->biBitCount = PIXELFORMATBPP(format);
381 bmih->biCompression = BI_RGB;
382 bmih->biSizeImage = datalen;
385 memcpy(bmih + 1, scan0, datalen);
387 memset(bmih + 1, 0, datalen);
389 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
390 ERR("could not make stream\n");
396 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
397 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
398 TRACE("Could not load picture\n");
399 IStream_Release(stream);
405 (*bitmap)->image.type = ImageTypeBitmap;
406 (*bitmap)->image.flags = ImageFlagsNone;
407 (*bitmap)->width = width;
408 (*bitmap)->height = height;
409 (*bitmap)->format = format;
414 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
419 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
424 if((*bitmap)->image.type != ImageTypeBitmap){
425 IPicture_Release((*bitmap)->image.picture);
427 return GenericError; /* FIXME: what error to return? */
434 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
437 return GdipCreateBitmapFromStream(stream, bitmap);
440 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
445 return InvalidParameter;
447 IPicture_get_CurDC(image->picture, &hdc);
449 IPicture_Release(image->picture);
450 if (image->type == ImageTypeBitmap)
451 GdipFree(((GpBitmap*)image)->bitmapbits);
457 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
460 return InvalidParameter;
462 return NotImplemented;
465 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
468 if(!image || !srcRect || !srcUnit)
469 return InvalidParameter;
470 if(image->type == ImageTypeMetafile){
471 *srcRect = ((GpMetafile*)image)->bounds;
472 *srcUnit = ((GpMetafile*)image)->unit;
474 else if(image->type == ImageTypeBitmap){
475 srcRect->X = srcRect->Y = 0.0;
476 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
477 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
478 *srcUnit = UnitPixel;
481 srcRect->X = srcRect->Y = 0.0;
482 srcRect->Width = ipicture_pixel_width(image->picture);
483 srcRect->Height = ipicture_pixel_height(image->picture);
484 *srcUnit = UnitPixel;
487 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
488 srcRect->Width, srcRect->Height, *srcUnit);
493 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
496 if(!image || !height || !width)
497 return InvalidParameter;
499 if(image->type == ImageTypeMetafile){
502 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
503 ((GpMetafile*)image)->bounds.Height;
505 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
506 ((GpMetafile*)image)->bounds.Width;
511 else if(image->type == ImageTypeBitmap){
512 *height = ((GpBitmap*)image)->height;
513 *width = ((GpBitmap*)image)->width;
516 *height = ipicture_pixel_height(image->picture);
517 *width = ipicture_pixel_width(image->picture);
520 TRACE("returning (%f, %f)\n", *height, *width);
524 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
525 GpGraphics **graphics)
529 if(!image || !graphics)
530 return InvalidParameter;
532 if(image->type != ImageTypeBitmap){
533 FIXME("not implemented for image type %d\n", image->type);
534 return NotImplemented;
537 IPicture_get_CurDC(image->picture, &hdc);
540 hdc = CreateCompatibleDC(0);
541 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
544 return GdipCreateFromHDC(hdc, graphics);
547 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
549 if(!image || !height)
550 return InvalidParameter;
552 if(image->type == ImageTypeMetafile){
555 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
556 ((GpMetafile*)image)->bounds.Height);
560 else if(image->type == ImageTypeBitmap)
561 *height = ((GpBitmap*)image)->height;
563 *height = ipicture_pixel_height(image->picture);
565 TRACE("returning %d\n", *height);
570 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
575 return InvalidParameter;
578 FIXME("not implemented\n");
580 return NotImplemented;
583 /* FIXME: test this function for non-bitmap types */
584 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
586 if(!image || !format)
587 return InvalidParameter;
589 if(image->type != ImageTypeBitmap)
590 *format = PixelFormat24bppRGB;
592 *format = ((GpBitmap*) image)->format;
597 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
601 if(!image || !format)
602 return InvalidParameter;
605 FIXME("not implemented\n");
607 return NotImplemented;
610 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
613 return InvalidParameter;
620 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
625 return InvalidParameter;
628 FIXME("not implemented\n");
630 return NotImplemented;
633 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
636 return InvalidParameter;
638 if(image->type == ImageTypeMetafile){
641 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
642 ((GpMetafile*)image)->bounds.Width);
646 else if(image->type == ImageTypeBitmap)
647 *width = ((GpBitmap*)image)->width;
649 *width = ipicture_pixel_width(image->picture);
651 TRACE("returning %d\n", *width);
656 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
657 MetafileHeader * header)
661 if(!metafile || !header)
662 return InvalidParameter;
665 FIXME("not implemented\n");
670 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
675 TRACE("%p %x %p\n", image, pid, size);
678 return InvalidParameter;
681 FIXME("not implemented\n");
683 return NotImplemented;
686 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
687 GDIPCONST GUID* dimensionID, UINT* count)
691 if(!image || !dimensionID || !count)
692 return InvalidParameter;
695 FIXME("not implemented\n");
697 return NotImplemented;
700 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
704 return InvalidParameter;
713 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
714 GUID* dimensionIDs, UINT count)
718 if(!image || !dimensionIDs)
719 return InvalidParameter;
722 FIXME("not implemented\n");
727 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
728 GDIPCONST GUID* dimensionID, UINT frameidx)
732 if(!image || !dimensionID)
733 return InvalidParameter;
736 FIXME("not implemented\n");
741 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
747 if (!filename || !image)
748 return InvalidParameter;
750 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
755 stat = GdipLoadImageFromStream(stream, image);
757 IStream_Release(stream);
762 /* FIXME: no icm handling */
763 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
765 return GdipLoadImageFromFile(filename, image);
768 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
773 if(!stream || !image)
774 return InvalidParameter;
776 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
777 (LPVOID*) &pic) != S_OK){
778 TRACE("Could not load picture\n");
782 IPicture_get_Type(pic, &type);
784 if(type == PICTYPE_BITMAP){
786 BITMAPCOREHEADER* bmch;
790 *image = GdipAlloc(sizeof(GpBitmap));
791 if(!*image) return OutOfMemory;
792 (*image)->type = ImageTypeBitmap;
794 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
795 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
797 /* get the pixel format */
798 IPicture_get_Handle(pic, &hbm);
799 IPicture_get_CurDC(pic, &hdc);
801 ZeroMemory(&bmi, sizeof(bmi));
802 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
803 bmch->bcSize = sizeof(BITMAPCOREHEADER);
807 hdc = CreateCompatibleDC(0);
808 old = SelectObject(hdc, (HBITMAP)hbm);
809 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
810 SelectObject(hdc, old);
814 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
816 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
818 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
819 /* FIXME: missing initialization code */
820 *image = GdipAlloc(sizeof(GpMetafile));
821 if(!*image) return OutOfMemory;
822 (*image)->type = ImageTypeMetafile;
825 *image = GdipAlloc(sizeof(GpImage));
826 if(!*image) return OutOfMemory;
827 (*image)->type = ImageTypeUnknown;
830 (*image)->picture = pic;
831 (*image)->flags = ImageFlagsNone;
837 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
839 return GdipLoadImageFromStream(stream, image);
842 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
847 return InvalidParameter;
850 FIXME("not implemented\n");
852 return NotImplemented;
855 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
856 GDIPCONST CLSID *clsidEncoder,
857 GDIPCONST EncoderParameters *encoderParams)
862 if (!image || !filename|| !clsidEncoder)
863 return InvalidParameter;
865 if (!(image->picture))
866 return InvalidParameter;
868 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
872 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
874 IStream_Release(stream);
878 /*************************************************************************
879 * Encoding functions -
880 * These functions encode an image in different image file formats.
882 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
883 #define BITMAP_FORMAT_JPEG 0xd8ff
884 #define BITMAP_FORMAT_GIF 0x4947
885 #define BITMAP_FORMAT_PNG 0x5089
886 #define BITMAP_FORMAT_APM 0xcdd7
888 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
889 void **output, unsigned int *output_size)
891 int num_palette_entries;
892 BITMAPFILEHEADER *bmp_file_hdr;
893 BITMAPINFO *bmp_info_hdr;
895 if (bitmap_info->bmiHeader.biClrUsed) {
896 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
897 if (num_palette_entries > 256) num_palette_entries = 256;
899 if (bitmap_info->bmiHeader.biBitCount <= 8)
900 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
902 num_palette_entries = 0;
906 sizeof(BITMAPFILEHEADER) +
907 sizeof(BITMAPINFOHEADER) +
908 num_palette_entries * sizeof(RGBQUAD) +
909 bitmap_info->bmiHeader.biSizeImage;
911 *output = GdipAlloc(*output_size);
913 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
914 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
915 bmp_file_hdr->bfSize = *output_size;
916 bmp_file_hdr->bfOffBits =
917 sizeof(BITMAPFILEHEADER) +
918 sizeof(BITMAPINFOHEADER) +
919 num_palette_entries * sizeof (RGBQUAD);
921 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
922 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
923 memcpy((unsigned char *)(*output) +
924 sizeof(BITMAPFILEHEADER) +
925 sizeof(BITMAPINFOHEADER) +
926 num_palette_entries * sizeof(RGBQUAD),
927 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
932 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
933 void **output, unsigned int *output_size);
937 NUM_ENCODERS_SUPPORTED
940 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
941 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
945 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
946 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
957 encode_image_func* encode_image;
959 unsigned int output_size;
967 if(!image || !stream)
968 return InvalidParameter;
973 hr = IPicture_get_Type(image->picture, &type);
974 if (FAILED(hr) || type != PICTYPE_BITMAP)
977 /* select correct encoder */
979 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
980 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
981 encode_image = encode_image_funcs[i];
983 if (encode_image == NULL)
984 return UnknownImageFormat;
986 /* extract underlying hbitmap representation from the IPicture */
987 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
988 if (FAILED(hr) || !hbmp)
990 hr = IPicture_get_CurDC(image->picture, &hdc);
993 bm_is_selected = (hdc != 0);
994 if (!bm_is_selected) {
995 hdc = CreateCompatibleDC(0);
996 old_hbmp = SelectObject(hdc, hbmp);
999 /* get bits from HBITMAP */
1000 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1001 bmp_info.bmiHeader.biBitCount = 0;
1002 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1004 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1007 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1009 if (!bm_is_selected) {
1010 SelectObject(hdc, old_hbmp);
1017 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1019 IStream_Write(stream, output, output_size, &dummy);
1027 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1028 GDIPCONST ColorPalette *palette)
1032 if(!image || !palette)
1033 return InvalidParameter;
1036 FIXME("not implemented\n");
1038 return NotImplemented;
1041 /*************************************************************************
1043 * Structures that represent which formats we support for encoding.
1046 /* ImageCodecInfo creation routines taken from libgdiplus */
1047 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1048 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1049 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1050 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1051 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1052 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1054 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1057 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1058 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1059 /* CodecName */ bmp_codecname,
1061 /* FormatDescription */ bmp_format,
1062 /* FilenameExtension */ bmp_extension,
1063 /* MimeType */ bmp_mimetype,
1064 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1068 /* SigPattern */ bmp_sig_pattern,
1069 /* SigMask */ bmp_sig_mask,
1073 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1075 if (!numEncoders || !size)
1076 return InvalidParameter;
1078 *numEncoders = NUM_ENCODERS_SUPPORTED;
1079 *size = sizeof (codecs);
1084 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1087 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1088 (size != sizeof (codecs)))
1089 return GenericError;
1091 memcpy(encoders, codecs, sizeof (codecs));
1095 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1102 return InvalidParameter;
1104 /* TODO: Support for device-dependent bitmaps */
1106 FIXME("no support for device-dependent bitmaps\n");
1107 return NotImplemented;
1110 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1111 return InvalidParameter;
1113 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1114 switch(bm.bmBitsPixel) {
1116 format = PixelFormat1bppIndexed;
1119 format = PixelFormat4bppIndexed;
1122 format = PixelFormat8bppIndexed;
1125 format = PixelFormat24bppRGB;
1128 format = PixelFormat48bppRGB;
1131 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1132 return InvalidParameter;
1135 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1136 format, bm.bmBits, bitmap);
1141 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1142 const VOID *params, const UINT size)
1147 FIXME("not implemented\n");
1149 return NotImplemented;
1152 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1154 if(!image || !flags)
1155 return InvalidParameter;
1157 *flags = image->flags;