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
33 #include "gdiplus_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
38 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
40 static INT ipicture_pixel_height(IPicture *pic)
45 IPicture_get_Height(pic, &y);
49 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
55 static INT ipicture_pixel_width(IPicture *pic)
60 IPicture_get_Width(pic, &x);
64 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
71 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
75 TRACE("%p %d %d %p\n", bitmap, x, y, color);
78 return InvalidParameter;
81 FIXME("not implemented\n");
85 return NotImplemented;
88 /* This function returns a pointer to an array of pixels that represents the
89 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
90 * flags. It is correct behavior that a user who calls this function with write
91 * privileges can write to the whole bitmap (not just the area in rect).
93 * FIXME: only used portion of format is bits per pixel. */
94 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
95 UINT flags, PixelFormat format, BitmapData* lockeddata)
98 INT stride, bitspp = PIXELFORMATBPP(format);
105 GpRect act_rect; /* actual rect to be used */
107 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
109 if(!lockeddata || !bitmap)
110 return InvalidParameter;
113 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
114 (rect->Y + rect->Height > bitmap->height) || !flags)
115 return InvalidParameter;
120 act_rect.X = act_rect.Y = 0;
121 act_rect.Width = bitmap->width;
122 act_rect.Height = bitmap->height;
125 if(flags & ImageLockModeUserInputBuf)
126 return NotImplemented;
131 IPicture_get_Handle(bitmap->image.picture, &hbm);
132 IPicture_get_CurDC(bitmap->image.picture, &hdc);
133 bm_is_selected = (hdc != 0);
135 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
138 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
139 pbmi->bmiHeader.biBitCount = 0;
142 hdc = CreateCompatibleDC(0);
143 old = SelectObject(hdc, (HBITMAP)hbm);
147 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
149 abs_height = abs(pbmi->bmiHeader.biHeight);
150 stride = pbmi->bmiHeader.biWidth * bitspp / 8;
151 stride = (stride + 3) & ~3;
153 buff = GdipAlloc(stride * abs_height);
155 pbmi->bmiHeader.biBitCount = bitspp;
158 GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, pbmi, DIB_RGB_COLORS);
161 SelectObject(hdc, old);
170 lockeddata->Width = act_rect.Width;
171 lockeddata->Height = act_rect.Height;
172 lockeddata->PixelFormat = format;
173 lockeddata->Reserved = flags;
175 if(pbmi->bmiHeader.biHeight > 0){
176 lockeddata->Stride = -stride;
177 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
178 stride * (abs_height - 1 - act_rect.Y);
181 lockeddata->Stride = stride;
182 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
185 bitmap->lockmode = flags;
188 bitmap->bitmapbits = buff;
194 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
195 BitmapData* lockeddata)
203 if(!bitmap || !lockeddata)
204 return InvalidParameter;
206 if(!bitmap->lockmode)
209 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
210 return NotImplemented;
212 if(lockeddata->Reserved & ImageLockModeRead){
213 if(!(--bitmap->numlocks))
214 bitmap->lockmode = 0;
216 GdipFree(bitmap->bitmapbits);
217 bitmap->bitmapbits = NULL;
221 IPicture_get_Handle(bitmap->image.picture, &hbm);
222 IPicture_get_CurDC(bitmap->image.picture, &hdc);
223 bm_is_selected = (hdc != 0);
225 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
226 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
227 pbmi->bmiHeader.biBitCount = 0;
230 hdc = CreateCompatibleDC(0);
231 old = SelectObject(hdc, (HBITMAP)hbm);
234 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
235 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
236 SetDIBits(hdc, (HBITMAP)hbm, 0, abs(pbmi->bmiHeader.biHeight),
237 bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
240 SelectObject(hdc, old);
245 GdipFree(bitmap->bitmapbits);
246 bitmap->bitmapbits = NULL;
247 bitmap->lockmode = 0;
252 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
258 GpStatus stat = GenericError;
260 TRACE("%p, %p\n", image, cloneImage);
262 if (!image || !cloneImage)
263 return InvalidParameter;
265 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
269 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
272 WARN("Failed to save image on stream\n");
276 /* Set seek pointer back to the beginning of the picture */
278 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
282 stat = GdipLoadImageFromStream(stream, cloneImage);
283 if (stat != Ok) WARN("Failed to load image from stream\n");
286 IStream_Release(stream);
290 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
296 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
298 if(!filename || !bitmap)
299 return InvalidParameter;
301 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
306 stat = GdipCreateBitmapFromStream(stream, bitmap);
308 IStream_Release(stream);
313 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
314 VOID *bits, GpBitmap **bitmap)
316 DWORD height, stride;
319 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
321 height = abs(info->bmiHeader.biHeight);
322 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
324 if(info->bmiHeader.biHeight > 0) /* bottom-up */
326 bits = (BYTE*)bits + (height - 1) * stride;
330 switch(info->bmiHeader.biBitCount) {
332 format = PixelFormat1bppIndexed;
335 format = PixelFormat4bppIndexed;
338 format = PixelFormat8bppIndexed;
341 format = PixelFormat24bppRGB;
344 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
346 return InvalidParameter;
349 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
355 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
358 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
360 return GdipCreateBitmapFromFile(filename, bitmap);
363 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
364 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
367 GpStatus stat = InvalidParameter;
369 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
371 if(!lpBitmapName || !bitmap)
372 return InvalidParameter;
375 hbm = (HBITMAP)LoadImageW(hInstance,lpBitmapName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
378 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
385 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
386 HBITMAP* hbmReturn, ARGB background)
392 return NotImplemented;
395 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
396 GpMetafile* metafile, BOOL* succ, EmfType emfType,
397 const WCHAR* description, GpMetafile** out_metafile)
401 if(!ref || !metafile || !out_metafile)
402 return InvalidParameter;
405 *out_metafile = NULL;
408 FIXME("not implemented\n");
410 return NotImplemented;
413 /* FIXME: this should create a bitmap in the given size with the attributes
414 * (resolution etc.) of the graphics object */
415 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
416 GpGraphics* target, GpBitmap** bitmap)
421 if(!target || !bitmap)
422 return InvalidParameter;
425 FIXME("hacked stub\n");
427 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
433 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
434 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
436 BITMAPFILEHEADER *bmfh;
437 BITMAPINFOHEADER *bmih;
442 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
444 if (!bitmap) return InvalidParameter;
446 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
448 return InvalidParameter;
452 return InvalidParameter;
454 /* FIXME: windows allows negative stride (reads backwards from scan0) */
456 FIXME("negative stride\n");
457 return InvalidParameter;
460 *bitmap = GdipAlloc(sizeof(GpBitmap));
461 if(!*bitmap) return OutOfMemory;
464 stride = width * (PIXELFORMATBPP(format) / 8);
465 stride = (stride + 3) & ~3;
468 datalen = abs(stride * height);
469 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
470 buff = GdipAlloc(size);
476 bmfh = (BITMAPFILEHEADER*) buff;
477 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
479 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
481 bmfh->bfOffBits = size - datalen;
483 bmih->biSize = sizeof(BITMAPINFOHEADER);
484 bmih->biWidth = width;
485 bmih->biHeight = -height;
486 /* FIXME: use the rest of the data from format */
487 bmih->biBitCount = PIXELFORMATBPP(format);
488 bmih->biCompression = BI_RGB;
489 bmih->biSizeImage = datalen;
492 memcpy(bmih + 1, scan0, datalen);
494 memset(bmih + 1, 0, datalen);
496 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
497 ERR("could not make stream\n");
503 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
504 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
505 TRACE("Could not load picture\n");
506 IStream_Release(stream);
512 (*bitmap)->image.type = ImageTypeBitmap;
513 (*bitmap)->image.flags = ImageFlagsNone;
514 (*bitmap)->width = width;
515 (*bitmap)->height = height;
516 (*bitmap)->format = format;
521 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
526 TRACE("%p %p\n", stream, bitmap);
528 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
533 if((*bitmap)->image.type != ImageTypeBitmap){
534 IPicture_Release((*bitmap)->image.picture);
536 return GenericError; /* FIXME: what error to return? */
543 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
546 TRACE("%p %p\n", stream, bitmap);
548 return GdipCreateBitmapFromStream(stream, bitmap);
551 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
552 GpCachedBitmap **cachedbmp)
556 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
558 if(!bitmap || !graphics || !cachedbmp)
559 return InvalidParameter;
561 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
565 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
567 GdipFree(*cachedbmp);
574 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
576 TRACE("%p\n", cachedbmp);
579 return InvalidParameter;
581 GdipDisposeImage(cachedbmp->image);
587 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
588 GpCachedBitmap *cachedbmp, INT x, INT y)
590 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
592 if(!graphics || !cachedbmp)
593 return InvalidParameter;
595 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
598 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
602 TRACE("%p\n", image);
605 return InvalidParameter;
607 IPicture_get_CurDC(image->picture, &hdc);
609 IPicture_Release(image->picture);
610 if (image->type == ImageTypeBitmap)
611 GdipFree(((GpBitmap*)image)->bitmapbits);
617 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
620 return InvalidParameter;
622 return NotImplemented;
625 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
628 TRACE("%p %p %p\n", image, srcRect, srcUnit);
630 if(!image || !srcRect || !srcUnit)
631 return InvalidParameter;
632 if(image->type == ImageTypeMetafile){
633 *srcRect = ((GpMetafile*)image)->bounds;
634 *srcUnit = ((GpMetafile*)image)->unit;
636 else if(image->type == ImageTypeBitmap){
637 srcRect->X = srcRect->Y = 0.0;
638 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
639 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
640 *srcUnit = UnitPixel;
643 srcRect->X = srcRect->Y = 0.0;
644 srcRect->Width = ipicture_pixel_width(image->picture);
645 srcRect->Height = ipicture_pixel_height(image->picture);
646 *srcUnit = UnitPixel;
649 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
650 srcRect->Width, srcRect->Height, *srcUnit);
655 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
658 TRACE("%p %p %p\n", image, width, height);
660 if(!image || !height || !width)
661 return InvalidParameter;
663 if(image->type == ImageTypeMetafile){
666 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
667 ((GpMetafile*)image)->bounds.Height;
669 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
670 ((GpMetafile*)image)->bounds.Width;
675 else if(image->type == ImageTypeBitmap){
676 *height = ((GpBitmap*)image)->height;
677 *width = ((GpBitmap*)image)->width;
680 *height = ipicture_pixel_height(image->picture);
681 *width = ipicture_pixel_width(image->picture);
684 TRACE("returning (%f, %f)\n", *height, *width);
688 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
689 GpGraphics **graphics)
693 TRACE("%p %p\n", image, graphics);
695 if(!image || !graphics)
696 return InvalidParameter;
698 if(image->type != ImageTypeBitmap){
699 FIXME("not implemented for image type %d\n", image->type);
700 return NotImplemented;
703 IPicture_get_CurDC(image->picture, &hdc);
706 hdc = CreateCompatibleDC(0);
707 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
710 return GdipCreateFromHDC(hdc, graphics);
713 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
715 TRACE("%p %p\n", image, height);
717 if(!image || !height)
718 return InvalidParameter;
720 if(image->type == ImageTypeMetafile){
723 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
724 ((GpMetafile*)image)->bounds.Height);
728 else if(image->type == ImageTypeBitmap)
729 *height = ((GpBitmap*)image)->height;
731 *height = ipicture_pixel_height(image->picture);
733 TRACE("returning %d\n", *height);
738 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
743 return InvalidParameter;
746 FIXME("not implemented\n");
748 return NotImplemented;
751 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
753 FIXME("%p %p\n", image, size);
756 return InvalidParameter;
758 return NotImplemented;
761 /* FIXME: test this function for non-bitmap types */
762 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
764 TRACE("%p %p\n", image, format);
766 if(!image || !format)
767 return InvalidParameter;
769 if(image->type != ImageTypeBitmap)
770 *format = PixelFormat24bppRGB;
772 *format = ((GpBitmap*) image)->format;
777 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
781 if(!image || !format)
782 return InvalidParameter;
787 /* FIXME: should be detected from embedded picture or stored separately */
790 case ImageTypeBitmap: *format = ImageFormatBMP; break;
791 case ImageTypeMetafile: *format = ImageFormatEMF; break;
793 WARN("unknown type %u\n", image->type);
794 *format = ImageFormatUndefined;
799 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
801 TRACE("%p %p\n", image, type);
804 return InvalidParameter;
811 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
816 return InvalidParameter;
819 FIXME("not implemented\n");
821 return NotImplemented;
824 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
826 TRACE("%p %p\n", image, width);
829 return InvalidParameter;
831 if(image->type == ImageTypeMetafile){
834 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
835 ((GpMetafile*)image)->bounds.Width);
839 else if(image->type == ImageTypeBitmap)
840 *width = ((GpBitmap*)image)->width;
842 *width = ipicture_pixel_width(image->picture);
844 TRACE("returning %d\n", *width);
849 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
850 MetafileHeader * header)
854 if(!metafile || !header)
855 return InvalidParameter;
858 FIXME("not implemented\n");
863 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
864 UINT num, PropertyItem* items)
869 FIXME("not implemented\n");
871 return InvalidParameter;
874 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
879 FIXME("not implemented\n");
881 return InvalidParameter;
884 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
889 FIXME("not implemented\n");
891 return InvalidParameter;
894 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
895 PropertyItem* buffer)
900 FIXME("not implemented\n");
902 return InvalidParameter;
905 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
910 TRACE("%p %x %p\n", image, pid, size);
913 return InvalidParameter;
916 FIXME("not implemented\n");
918 return NotImplemented;
921 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
926 FIXME("not implemented\n");
928 return InvalidParameter;
931 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
932 GDIPCONST GUID* dimensionID, UINT* count)
936 if(!image || !dimensionID || !count)
937 return InvalidParameter;
940 FIXME("not implemented\n");
942 return NotImplemented;
945 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
949 return InvalidParameter;
958 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
959 GUID* dimensionIDs, UINT count)
963 if(!image || !dimensionIDs)
964 return InvalidParameter;
967 FIXME("not implemented\n");
972 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
973 GDIPCONST GUID* dimensionID, UINT frameidx)
977 if(!image || !dimensionID)
978 return InvalidParameter;
981 FIXME("not implemented\n");
986 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
992 TRACE("(%s) %p\n", debugstr_w(filename), image);
994 if (!filename || !image)
995 return InvalidParameter;
997 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1002 stat = GdipLoadImageFromStream(stream, image);
1004 IStream_Release(stream);
1009 /* FIXME: no icm handling */
1010 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
1012 TRACE("(%s) %p\n", debugstr_w(filename), image);
1014 return GdipLoadImageFromFile(filename, image);
1017 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
1022 TRACE("%p %p\n", stream, image);
1024 if(!stream || !image)
1025 return InvalidParameter;
1027 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1028 (LPVOID*) &pic) != S_OK){
1029 TRACE("Could not load picture\n");
1030 return GenericError;
1033 IPicture_get_Type(pic, &type);
1035 if(type == PICTYPE_BITMAP){
1037 BITMAPCOREHEADER* bmch;
1041 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1044 *image = GdipAlloc(sizeof(GpBitmap));
1049 (*image)->type = ImageTypeBitmap;
1051 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
1052 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
1054 /* get the pixel format */
1055 IPicture_get_Handle(pic, &hbm);
1056 IPicture_get_CurDC(pic, &hdc);
1058 bmch = (BITMAPCOREHEADER*) (&pbmi->bmiHeader);
1059 bmch->bcSize = sizeof(BITMAPCOREHEADER);
1063 hdc = CreateCompatibleDC(0);
1064 old = SelectObject(hdc, (HBITMAP)hbm);
1065 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1066 SelectObject(hdc, old);
1070 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1072 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
1075 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
1076 /* FIXME: missing initialization code */
1077 *image = GdipAlloc(sizeof(GpMetafile));
1078 if(!*image) return OutOfMemory;
1079 (*image)->type = ImageTypeMetafile;
1082 *image = GdipAlloc(sizeof(GpImage));
1083 if(!*image) return OutOfMemory;
1084 (*image)->type = ImageTypeUnknown;
1087 (*image)->picture = pic;
1088 (*image)->flags = ImageFlagsNone;
1094 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1096 TRACE("%p %p\n", stream, image);
1098 return GdipLoadImageFromStream(stream, image);
1101 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1106 return InvalidParameter;
1109 FIXME("not implemented\n");
1111 return NotImplemented;
1114 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1119 FIXME("not implemented\n");
1121 return NotImplemented;
1124 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1125 GDIPCONST CLSID *clsidEncoder,
1126 GDIPCONST EncoderParameters *encoderParams)
1131 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1133 if (!image || !filename|| !clsidEncoder)
1134 return InvalidParameter;
1136 if (!(image->picture))
1137 return InvalidParameter;
1139 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1141 return GenericError;
1143 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1145 IStream_Release(stream);
1149 /*************************************************************************
1150 * Encoding functions -
1151 * These functions encode an image in different image file formats.
1153 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1154 #define BITMAP_FORMAT_JPEG 0xd8ff
1155 #define BITMAP_FORMAT_GIF 0x4947
1156 #define BITMAP_FORMAT_PNG 0x5089
1157 #define BITMAP_FORMAT_APM 0xcdd7
1159 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1160 void **output, unsigned int *output_size)
1162 int num_palette_entries;
1163 BITMAPFILEHEADER *bmp_file_hdr;
1164 BITMAPINFO *bmp_info_hdr;
1166 if (bitmap_info->bmiHeader.biClrUsed) {
1167 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
1168 if (num_palette_entries > 256) num_palette_entries = 256;
1170 if (bitmap_info->bmiHeader.biBitCount <= 8)
1171 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1173 num_palette_entries = 0;
1177 sizeof(BITMAPFILEHEADER) +
1178 sizeof(BITMAPINFOHEADER) +
1179 num_palette_entries * sizeof(RGBQUAD) +
1180 bitmap_info->bmiHeader.biSizeImage;
1182 *output = GdipAlloc(*output_size);
1184 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
1185 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1186 bmp_file_hdr->bfSize = *output_size;
1187 bmp_file_hdr->bfOffBits =
1188 sizeof(BITMAPFILEHEADER) +
1189 sizeof(BITMAPINFOHEADER) +
1190 num_palette_entries * sizeof (RGBQUAD);
1192 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1193 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1194 memcpy((unsigned char *)(*output) +
1195 sizeof(BITMAPFILEHEADER) +
1196 sizeof(BITMAPINFOHEADER) +
1197 num_palette_entries * sizeof(RGBQUAD),
1198 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1203 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1204 void **output, unsigned int *output_size);
1208 NUM_ENCODERS_SUPPORTED
1211 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1212 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1216 /*****************************************************************************
1217 * GdipSaveImageToStream [GDIPLUS.@]
1219 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1220 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1229 BITMAPINFO bmp_info;
1231 encode_image_func* encode_image;
1233 unsigned int output_size;
1241 TRACE("%p %p %p %p\n", image, stream, clsid, params);
1243 if(!image || !stream)
1244 return InvalidParameter;
1246 if (!image->picture)
1247 return GenericError;
1249 hr = IPicture_get_Type(image->picture, &type);
1250 if (FAILED(hr) || type != PICTYPE_BITMAP)
1251 return GenericError;
1253 /* select correct encoder */
1254 encode_image = NULL;
1255 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1256 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1257 encode_image = encode_image_funcs[i];
1259 if (encode_image == NULL)
1260 return UnknownImageFormat;
1262 /* extract underlying hbitmap representation from the IPicture */
1263 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1264 if (FAILED(hr) || !hbmp)
1265 return GenericError;
1266 hr = IPicture_get_CurDC(image->picture, &hdc);
1268 return GenericError;
1269 bm_is_selected = (hdc != 0);
1270 if (!bm_is_selected) {
1271 hdc = CreateCompatibleDC(0);
1272 old_hbmp = SelectObject(hdc, hbmp);
1275 /* get bits from HBITMAP */
1276 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1277 bmp_info.bmiHeader.biBitCount = 0;
1278 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1280 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1283 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1285 if (!bm_is_selected) {
1286 SelectObject(hdc, old_hbmp);
1293 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1295 IStream_Write(stream, output, output_size, &dummy);
1303 /*****************************************************************************
1304 * GdipSetImagePalette [GDIPLUS.@]
1306 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1307 GDIPCONST ColorPalette *palette)
1311 if(!image || !palette)
1312 return InvalidParameter;
1315 FIXME("not implemented\n");
1317 return NotImplemented;
1320 /*************************************************************************
1322 * Structures that represent which formats we support for encoding.
1325 /* ImageCodecInfo creation routines taken from libgdiplus */
1326 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1327 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1328 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1329 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1330 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1331 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1333 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1336 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1337 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1338 /* CodecName */ bmp_codecname,
1340 /* FormatDescription */ bmp_format,
1341 /* FilenameExtension */ bmp_extension,
1342 /* MimeType */ bmp_mimetype,
1343 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1347 /* SigPattern */ bmp_sig_pattern,
1348 /* SigMask */ bmp_sig_mask,
1352 /*****************************************************************************
1353 * GdipGetImageDecodersSize [GDIPLUS.@]
1355 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
1357 FIXME("%p %p stub!\n", numDecoders, size);
1359 if (!numDecoders || !size)
1360 return InvalidParameter;
1368 /*****************************************************************************
1369 * GdipGetImageDecoders [GDIPLUS.@]
1371 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
1373 FIXME("%u %u %p stub!\n", numDecoders, size, decoders);
1376 return GenericError;
1378 return NotImplemented;
1381 /*****************************************************************************
1382 * GdipGetImageEncodersSize [GDIPLUS.@]
1384 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1386 TRACE("%p %p\n", numEncoders, size);
1388 if (!numEncoders || !size)
1389 return InvalidParameter;
1391 *numEncoders = NUM_ENCODERS_SUPPORTED;
1392 *size = sizeof (codecs);
1397 /*****************************************************************************
1398 * GdipGetImageEncoders [GDIPLUS.@]
1400 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1402 TRACE("%u %u %p\n", numEncoders, size, encoders);
1405 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1406 (size != sizeof (codecs)))
1407 return GenericError;
1409 memcpy(encoders, codecs, sizeof (codecs));
1414 /*****************************************************************************
1415 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1417 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1423 TRACE("%p %p %p\n", hbm, hpal, bitmap);
1426 return InvalidParameter;
1428 /* TODO: Support for device-dependent bitmaps */
1430 FIXME("no support for device-dependent bitmaps\n");
1431 return NotImplemented;
1434 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1435 return InvalidParameter;
1437 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1438 switch(bm.bmBitsPixel) {
1440 format = PixelFormat1bppIndexed;
1443 format = PixelFormat4bppIndexed;
1446 format = PixelFormat8bppIndexed;
1449 format = PixelFormat24bppRGB;
1452 format = PixelFormat32bppRGB;
1455 format = PixelFormat48bppRGB;
1458 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1459 return InvalidParameter;
1462 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1463 format, bm.bmBits, bitmap);
1468 /*****************************************************************************
1469 * GdipSetEffectParameters [GDIPLUS.@]
1471 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1472 const VOID *params, const UINT size)
1477 FIXME("not implemented\n");
1479 return NotImplemented;
1482 /*****************************************************************************
1483 * GdipGetImageFlags [GDIPLUS.@]
1485 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1487 TRACE("%p %p\n", image, flags);
1489 if(!image || !flags)
1490 return InvalidParameter;
1492 *flags = image->flags;
1497 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
1499 TRACE("(%d, %p)\n", control, param);
1502 case TestControlForceBilinear:
1504 FIXME("TestControlForceBilinear not handled\n");
1506 case TestControlNoICM:
1508 FIXME("TestControlNoICM not handled\n");
1510 case TestControlGetBuildNumber:
1511 *((DWORD*)param) = 3102;
1518 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
1519 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
1520 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
1521 GpMetafile **metafile)
1523 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1524 frameUnit, debugstr_w(desc), metafile);
1526 return NotImplemented;
1529 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
1530 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
1531 GDIPCONST WCHAR *desc, GpMetafile **metafile)
1533 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1534 frameUnit, debugstr_w(desc), metafile);
1536 return NotImplemented;
1539 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
1541 FIXME("%p\n", image);