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 /* FIXME: test this function for non-bitmap types */
752 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
754 TRACE("%p %p\n", image, format);
756 if(!image || !format)
757 return InvalidParameter;
759 if(image->type != ImageTypeBitmap)
760 *format = PixelFormat24bppRGB;
762 *format = ((GpBitmap*) image)->format;
767 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
771 if(!image || !format)
772 return InvalidParameter;
777 /* FIXME: should be detected from embedded picture or stored separately */
780 case ImageTypeBitmap: *format = ImageFormatBMP; break;
781 case ImageTypeMetafile: *format = ImageFormatEMF; break;
783 WARN("unknown type %u\n", image->type);
784 *format = ImageFormatUndefined;
789 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
791 TRACE("%p %p\n", image, type);
794 return InvalidParameter;
801 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
806 return InvalidParameter;
809 FIXME("not implemented\n");
811 return NotImplemented;
814 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
816 TRACE("%p %p\n", image, width);
819 return InvalidParameter;
821 if(image->type == ImageTypeMetafile){
824 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
825 ((GpMetafile*)image)->bounds.Width);
829 else if(image->type == ImageTypeBitmap)
830 *width = ((GpBitmap*)image)->width;
832 *width = ipicture_pixel_width(image->picture);
834 TRACE("returning %d\n", *width);
839 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
840 MetafileHeader * header)
844 if(!metafile || !header)
845 return InvalidParameter;
848 FIXME("not implemented\n");
853 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
854 UINT num, PropertyItem* items)
859 FIXME("not implemented\n");
861 return InvalidParameter;
864 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
869 FIXME("not implemented\n");
871 return InvalidParameter;
874 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
879 FIXME("not implemented\n");
881 return InvalidParameter;
884 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
885 PropertyItem* buffer)
890 FIXME("not implemented\n");
892 return InvalidParameter;
895 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
900 TRACE("%p %x %p\n", image, pid, size);
903 return InvalidParameter;
906 FIXME("not implemented\n");
908 return NotImplemented;
911 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
916 FIXME("not implemented\n");
918 return InvalidParameter;
921 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
922 GDIPCONST GUID* dimensionID, UINT* count)
926 if(!image || !dimensionID || !count)
927 return InvalidParameter;
930 FIXME("not implemented\n");
932 return NotImplemented;
935 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
939 return InvalidParameter;
948 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
949 GUID* dimensionIDs, UINT count)
953 if(!image || !dimensionIDs)
954 return InvalidParameter;
957 FIXME("not implemented\n");
962 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
963 GDIPCONST GUID* dimensionID, UINT frameidx)
967 if(!image || !dimensionID)
968 return InvalidParameter;
971 FIXME("not implemented\n");
976 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
982 TRACE("(%s) %p\n", debugstr_w(filename), image);
984 if (!filename || !image)
985 return InvalidParameter;
987 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
992 stat = GdipLoadImageFromStream(stream, image);
994 IStream_Release(stream);
999 /* FIXME: no icm handling */
1000 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
1002 TRACE("(%s) %p\n", debugstr_w(filename), image);
1004 return GdipLoadImageFromFile(filename, image);
1007 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
1012 TRACE("%p %p\n", stream, image);
1014 if(!stream || !image)
1015 return InvalidParameter;
1017 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1018 (LPVOID*) &pic) != S_OK){
1019 TRACE("Could not load picture\n");
1020 return GenericError;
1023 IPicture_get_Type(pic, &type);
1025 if(type == PICTYPE_BITMAP){
1027 BITMAPCOREHEADER* bmch;
1031 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1034 *image = GdipAlloc(sizeof(GpBitmap));
1039 (*image)->type = ImageTypeBitmap;
1041 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
1042 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
1044 /* get the pixel format */
1045 IPicture_get_Handle(pic, &hbm);
1046 IPicture_get_CurDC(pic, &hdc);
1048 bmch = (BITMAPCOREHEADER*) (&pbmi->bmiHeader);
1049 bmch->bcSize = sizeof(BITMAPCOREHEADER);
1053 hdc = CreateCompatibleDC(0);
1054 old = SelectObject(hdc, (HBITMAP)hbm);
1055 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1056 SelectObject(hdc, old);
1060 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1062 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
1065 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
1066 /* FIXME: missing initialization code */
1067 *image = GdipAlloc(sizeof(GpMetafile));
1068 if(!*image) return OutOfMemory;
1069 (*image)->type = ImageTypeMetafile;
1072 *image = GdipAlloc(sizeof(GpImage));
1073 if(!*image) return OutOfMemory;
1074 (*image)->type = ImageTypeUnknown;
1077 (*image)->picture = pic;
1078 (*image)->flags = ImageFlagsNone;
1084 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1086 TRACE("%p %p\n", stream, image);
1088 return GdipLoadImageFromStream(stream, image);
1091 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1096 return InvalidParameter;
1099 FIXME("not implemented\n");
1101 return NotImplemented;
1104 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1109 FIXME("not implemented\n");
1111 return NotImplemented;
1114 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1115 GDIPCONST CLSID *clsidEncoder,
1116 GDIPCONST EncoderParameters *encoderParams)
1121 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1123 if (!image || !filename|| !clsidEncoder)
1124 return InvalidParameter;
1126 if (!(image->picture))
1127 return InvalidParameter;
1129 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1131 return GenericError;
1133 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1135 IStream_Release(stream);
1139 /*************************************************************************
1140 * Encoding functions -
1141 * These functions encode an image in different image file formats.
1143 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1144 #define BITMAP_FORMAT_JPEG 0xd8ff
1145 #define BITMAP_FORMAT_GIF 0x4947
1146 #define BITMAP_FORMAT_PNG 0x5089
1147 #define BITMAP_FORMAT_APM 0xcdd7
1149 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1150 void **output, unsigned int *output_size)
1152 int num_palette_entries;
1153 BITMAPFILEHEADER *bmp_file_hdr;
1154 BITMAPINFO *bmp_info_hdr;
1156 if (bitmap_info->bmiHeader.biClrUsed) {
1157 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
1158 if (num_palette_entries > 256) num_palette_entries = 256;
1160 if (bitmap_info->bmiHeader.biBitCount <= 8)
1161 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1163 num_palette_entries = 0;
1167 sizeof(BITMAPFILEHEADER) +
1168 sizeof(BITMAPINFOHEADER) +
1169 num_palette_entries * sizeof(RGBQUAD) +
1170 bitmap_info->bmiHeader.biSizeImage;
1172 *output = GdipAlloc(*output_size);
1174 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
1175 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1176 bmp_file_hdr->bfSize = *output_size;
1177 bmp_file_hdr->bfOffBits =
1178 sizeof(BITMAPFILEHEADER) +
1179 sizeof(BITMAPINFOHEADER) +
1180 num_palette_entries * sizeof (RGBQUAD);
1182 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1183 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1184 memcpy((unsigned char *)(*output) +
1185 sizeof(BITMAPFILEHEADER) +
1186 sizeof(BITMAPINFOHEADER) +
1187 num_palette_entries * sizeof(RGBQUAD),
1188 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1193 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1194 void **output, unsigned int *output_size);
1198 NUM_ENCODERS_SUPPORTED
1201 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1202 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1206 /*****************************************************************************
1207 * GdipSaveImageToStream [GDIPLUS.@]
1209 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1210 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1219 BITMAPINFO bmp_info;
1221 encode_image_func* encode_image;
1223 unsigned int output_size;
1231 TRACE("%p %p %p %p\n", image, stream, clsid, params);
1233 if(!image || !stream)
1234 return InvalidParameter;
1236 if (!image->picture)
1237 return GenericError;
1239 hr = IPicture_get_Type(image->picture, &type);
1240 if (FAILED(hr) || type != PICTYPE_BITMAP)
1241 return GenericError;
1243 /* select correct encoder */
1244 encode_image = NULL;
1245 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1246 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1247 encode_image = encode_image_funcs[i];
1249 if (encode_image == NULL)
1250 return UnknownImageFormat;
1252 /* extract underlying hbitmap representation from the IPicture */
1253 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1254 if (FAILED(hr) || !hbmp)
1255 return GenericError;
1256 hr = IPicture_get_CurDC(image->picture, &hdc);
1258 return GenericError;
1259 bm_is_selected = (hdc != 0);
1260 if (!bm_is_selected) {
1261 hdc = CreateCompatibleDC(0);
1262 old_hbmp = SelectObject(hdc, hbmp);
1265 /* get bits from HBITMAP */
1266 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1267 bmp_info.bmiHeader.biBitCount = 0;
1268 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1270 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1273 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1275 if (!bm_is_selected) {
1276 SelectObject(hdc, old_hbmp);
1283 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1285 IStream_Write(stream, output, output_size, &dummy);
1293 /*****************************************************************************
1294 * GdipSetImagePalette [GDIPLUS.@]
1296 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1297 GDIPCONST ColorPalette *palette)
1301 if(!image || !palette)
1302 return InvalidParameter;
1305 FIXME("not implemented\n");
1307 return NotImplemented;
1310 /*************************************************************************
1312 * Structures that represent which formats we support for encoding.
1315 /* ImageCodecInfo creation routines taken from libgdiplus */
1316 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1317 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1318 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1319 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1320 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1321 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1323 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1326 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1327 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1328 /* CodecName */ bmp_codecname,
1330 /* FormatDescription */ bmp_format,
1331 /* FilenameExtension */ bmp_extension,
1332 /* MimeType */ bmp_mimetype,
1333 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1337 /* SigPattern */ bmp_sig_pattern,
1338 /* SigMask */ bmp_sig_mask,
1342 /*****************************************************************************
1343 * GdipGetImageDecodersSize [GDIPLUS.@]
1345 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
1347 FIXME("%p %p stub!\n", numDecoders, size);
1349 if (!numDecoders || !size)
1350 return InvalidParameter;
1358 /*****************************************************************************
1359 * GdipGetImageDecoders [GDIPLUS.@]
1361 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
1363 FIXME("%u %u %p stub!\n", numDecoders, size, decoders);
1366 return GenericError;
1368 return NotImplemented;
1371 /*****************************************************************************
1372 * GdipGetImageEncodersSize [GDIPLUS.@]
1374 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1376 TRACE("%p %p\n", numEncoders, size);
1378 if (!numEncoders || !size)
1379 return InvalidParameter;
1381 *numEncoders = NUM_ENCODERS_SUPPORTED;
1382 *size = sizeof (codecs);
1387 /*****************************************************************************
1388 * GdipGetImageEncoders [GDIPLUS.@]
1390 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1392 TRACE("%u %u %p\n", numEncoders, size, encoders);
1395 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1396 (size != sizeof (codecs)))
1397 return GenericError;
1399 memcpy(encoders, codecs, sizeof (codecs));
1404 /*****************************************************************************
1405 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1407 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1413 TRACE("%p %p %p\n", hbm, hpal, bitmap);
1416 return InvalidParameter;
1418 /* TODO: Support for device-dependent bitmaps */
1420 FIXME("no support for device-dependent bitmaps\n");
1421 return NotImplemented;
1424 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1425 return InvalidParameter;
1427 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1428 switch(bm.bmBitsPixel) {
1430 format = PixelFormat1bppIndexed;
1433 format = PixelFormat4bppIndexed;
1436 format = PixelFormat8bppIndexed;
1439 format = PixelFormat24bppRGB;
1442 format = PixelFormat32bppRGB;
1445 format = PixelFormat48bppRGB;
1448 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1449 return InvalidParameter;
1452 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1453 format, bm.bmBits, bitmap);
1458 /*****************************************************************************
1459 * GdipSetEffectParameters [GDIPLUS.@]
1461 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1462 const VOID *params, const UINT size)
1467 FIXME("not implemented\n");
1469 return NotImplemented;
1472 /*****************************************************************************
1473 * GdipGetImageFlags [GDIPLUS.@]
1475 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1477 TRACE("%p %p\n", image, flags);
1479 if(!image || !flags)
1480 return InvalidParameter;
1482 *flags = image->flags;
1487 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
1489 TRACE("(%d, %p)\n", control, param);
1492 case TestControlForceBilinear:
1494 FIXME("TestControlForceBilinear not handled\n");
1496 case TestControlNoICM:
1498 FIXME("TestControlNoICM not handled\n");
1500 case TestControlGetBuildNumber:
1501 *((DWORD*)param) = 3102;
1508 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
1509 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
1510 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
1511 GpMetafile **metafile)
1513 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1514 frameUnit, debugstr_w(desc), metafile);
1516 return NotImplemented;
1519 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
1520 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
1521 GDIPCONST WCHAR *desc, 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 GdipImageForceValidation(GpImage *image)
1531 FIXME("%p\n", image);