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
21 #define NONAMELESSUNION
35 #include "gdiplus_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
40 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
42 static INT ipicture_pixel_height(IPicture *pic)
47 IPicture_get_Height(pic, &y);
51 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
57 static INT ipicture_pixel_width(IPicture *pic)
62 IPicture_get_Width(pic, &x);
66 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
73 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
77 TRACE("%p %d %d %p\n", bitmap, x, y, color);
80 return InvalidParameter;
83 FIXME("not implemented\n");
87 return NotImplemented;
90 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
94 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
97 return InvalidParameter;
100 FIXME("not implemented\n");
102 return NotImplemented;
105 /* This function returns a pointer to an array of pixels that represents the
106 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
107 * flags. It is correct behavior that a user who calls this function with write
108 * privileges can write to the whole bitmap (not just the area in rect).
110 * FIXME: only used portion of format is bits per pixel. */
111 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
112 UINT flags, PixelFormat format, BitmapData* lockeddata)
115 INT stride, bitspp = PIXELFORMATBPP(format);
117 HBITMAP hbm, old = NULL;
121 GpRect act_rect; /* actual rect to be used */
123 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
125 if(!lockeddata || !bitmap)
126 return InvalidParameter;
129 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
130 (rect->Y + rect->Height > bitmap->height) || !flags)
131 return InvalidParameter;
136 act_rect.X = act_rect.Y = 0;
137 act_rect.Width = bitmap->width;
138 act_rect.Height = bitmap->height;
141 if(flags & ImageLockModeUserInputBuf)
142 return NotImplemented;
147 IPicture_get_Handle(bitmap->image.picture, (OLE_HANDLE*)&hbm);
148 IPicture_get_CurDC(bitmap->image.picture, &hdc);
149 bm_is_selected = (hdc != 0);
151 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
154 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
155 pbmi->bmiHeader.biBitCount = 0;
158 hdc = CreateCompatibleDC(0);
159 old = SelectObject(hdc, hbm);
163 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
165 abs_height = abs(pbmi->bmiHeader.biHeight);
166 stride = pbmi->bmiHeader.biWidth * bitspp / 8;
167 stride = (stride + 3) & ~3;
169 buff = GdipAlloc(stride * abs_height);
171 pbmi->bmiHeader.biBitCount = bitspp;
174 GetDIBits(hdc, hbm, 0, abs_height, buff, pbmi, DIB_RGB_COLORS);
177 SelectObject(hdc, old);
186 lockeddata->Width = act_rect.Width;
187 lockeddata->Height = act_rect.Height;
188 lockeddata->PixelFormat = format;
189 lockeddata->Reserved = flags;
191 if(pbmi->bmiHeader.biHeight > 0){
192 lockeddata->Stride = -stride;
193 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
194 stride * (abs_height - 1 - act_rect.Y);
197 lockeddata->Stride = stride;
198 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
201 bitmap->lockmode = flags;
204 bitmap->bitmapbits = buff;
210 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
212 FIXME("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
214 return NotImplemented;
217 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
218 BitmapData* lockeddata)
221 HBITMAP hbm, old = NULL;
225 if(!bitmap || !lockeddata)
226 return InvalidParameter;
228 if(!bitmap->lockmode)
231 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
232 return NotImplemented;
234 if(lockeddata->Reserved & ImageLockModeRead){
235 if(!(--bitmap->numlocks))
236 bitmap->lockmode = 0;
238 GdipFree(bitmap->bitmapbits);
239 bitmap->bitmapbits = NULL;
243 IPicture_get_Handle(bitmap->image.picture, (OLE_HANDLE*)&hbm);
244 IPicture_get_CurDC(bitmap->image.picture, &hdc);
245 bm_is_selected = (hdc != 0);
247 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
248 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
249 pbmi->bmiHeader.biBitCount = 0;
252 hdc = CreateCompatibleDC(0);
253 old = SelectObject(hdc, hbm);
256 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
257 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
258 SetDIBits(hdc, hbm, 0, abs(pbmi->bmiHeader.biHeight),
259 bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
262 SelectObject(hdc, old);
267 GdipFree(bitmap->bitmapbits);
268 bitmap->bitmapbits = NULL;
269 bitmap->lockmode = 0;
274 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
275 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
277 FIXME("(%f,%f,%f,%f,%i,%p,%p): stub\n", x, y, width, height, format, srcBitmap, dstBitmap);
279 return NotImplemented;
282 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
283 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
285 FIXME("(%i,%i,%i,%i,%i,%p,%p): stub\n", x, y, width, height, format, srcBitmap, dstBitmap);
287 return NotImplemented;
290 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
296 GpStatus stat = GenericError;
298 TRACE("%p, %p\n", image, cloneImage);
300 if (!image || !cloneImage)
301 return InvalidParameter;
303 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
307 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
310 WARN("Failed to save image on stream\n");
314 /* Set seek pointer back to the beginning of the picture */
316 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
320 stat = GdipLoadImageFromStream(stream, cloneImage);
321 if (stat != Ok) WARN("Failed to load image from stream\n");
324 IStream_Release(stream);
328 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
334 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
336 if(!filename || !bitmap)
337 return InvalidParameter;
339 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
344 stat = GdipCreateBitmapFromStream(stream, bitmap);
346 IStream_Release(stream);
351 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
352 VOID *bits, GpBitmap **bitmap)
354 DWORD height, stride;
357 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
359 height = abs(info->bmiHeader.biHeight);
360 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
362 if(info->bmiHeader.biHeight > 0) /* bottom-up */
364 bits = (BYTE*)bits + (height - 1) * stride;
368 switch(info->bmiHeader.biBitCount) {
370 format = PixelFormat1bppIndexed;
373 format = PixelFormat4bppIndexed;
376 format = PixelFormat8bppIndexed;
379 format = PixelFormat24bppRGB;
382 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
384 return InvalidParameter;
387 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
393 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
396 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
398 return GdipCreateBitmapFromFile(filename, bitmap);
401 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
402 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
405 GpStatus stat = InvalidParameter;
407 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
409 if(!lpBitmapName || !bitmap)
410 return InvalidParameter;
413 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
414 LR_CREATEDIBSECTION);
417 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
424 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
425 HBITMAP* hbmReturn, ARGB background)
431 return NotImplemented;
434 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
435 GpMetafile* metafile, BOOL* succ, EmfType emfType,
436 const WCHAR* description, GpMetafile** out_metafile)
440 if(!ref || !metafile || !out_metafile)
441 return InvalidParameter;
444 *out_metafile = NULL;
447 FIXME("not implemented\n");
449 return NotImplemented;
452 /* FIXME: this should create a bitmap in the given size with the attributes
453 * (resolution etc.) of the graphics object */
454 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
455 GpGraphics* target, GpBitmap** bitmap)
460 if(!target || !bitmap)
461 return InvalidParameter;
464 FIXME("hacked stub\n");
466 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
472 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
478 TRACE("%p, %p\n", hicon, bitmap);
480 if(!bitmap || !GetIconInfo(hicon, &iinfo))
481 return InvalidParameter;
483 *bitmap = GdipAlloc(sizeof(GpBitmap));
484 if(!*bitmap) return OutOfMemory;
486 icon_copy = CreateIconIndirect(&iinfo);
490 return InvalidParameter;
493 desc.cbSizeofstruct = sizeof(PICTDESC);
494 desc.picType = PICTYPE_ICON;
495 desc.u.icon.hicon = icon_copy;
497 if(OleCreatePictureIndirect(&desc, &IID_IPicture, TRUE,
498 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
499 DestroyIcon(icon_copy);
504 (*bitmap)->format = PixelFormat32bppARGB;
505 (*bitmap)->image.type = ImageTypeBitmap;
506 (*bitmap)->image.flags = ImageFlagsNone;
507 (*bitmap)->width = ipicture_pixel_width((*bitmap)->image.picture);
508 (*bitmap)->height = ipicture_pixel_height((*bitmap)->image.picture);
510 DeleteObject(iinfo.hbmColor);
511 DeleteObject(iinfo.hbmMask);
516 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
517 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
519 BITMAPFILEHEADER *bmfh;
520 BITMAPINFOHEADER *bmih;
525 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
527 if (!bitmap) return InvalidParameter;
529 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
531 return InvalidParameter;
535 return InvalidParameter;
537 *bitmap = GdipAlloc(sizeof(GpBitmap));
538 if(!*bitmap) return OutOfMemory;
541 stride = width * (PIXELFORMATBPP(format) / 8);
542 stride = (stride + 3) & ~3;
545 datalen = abs(stride * height);
546 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
547 buff = GdipAlloc(size);
553 bmfh = (BITMAPFILEHEADER*) buff;
554 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
556 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
558 bmfh->bfOffBits = size - datalen;
560 bmih->biSize = sizeof(BITMAPINFOHEADER);
561 bmih->biWidth = width;
562 /* FIXME: use the rest of the data from format */
563 bmih->biBitCount = PIXELFORMATBPP(format);
564 bmih->biCompression = BI_RGB;
565 bmih->biSizeImage = datalen;
571 bmih->biHeight = -height;
572 memcpy(bmih + 1, scan0, datalen);
576 bmih->biHeight = height;
577 memcpy(bmih + 1, scan0 + stride * (height - 1), datalen);
582 bmih->biHeight = height;
583 memset(bmih + 1, 0, datalen);
586 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
587 ERR("could not make stream\n");
594 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
595 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
596 TRACE("Could not load picture\n");
597 IStream_Release(stream);
604 (*bitmap)->image.type = ImageTypeBitmap;
605 (*bitmap)->image.flags = ImageFlagsNone;
606 (*bitmap)->width = width;
607 (*bitmap)->height = height;
608 (*bitmap)->format = format;
613 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
618 TRACE("%p %p\n", stream, bitmap);
620 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
625 if((*bitmap)->image.type != ImageTypeBitmap){
626 IPicture_Release((*bitmap)->image.picture);
628 return GenericError; /* FIXME: what error to return? */
635 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
638 TRACE("%p %p\n", stream, bitmap);
640 return GdipCreateBitmapFromStream(stream, bitmap);
643 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
644 GpCachedBitmap **cachedbmp)
648 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
650 if(!bitmap || !graphics || !cachedbmp)
651 return InvalidParameter;
653 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
657 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
659 GdipFree(*cachedbmp);
666 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
668 FIXME("(%p, %p)\n", bitmap, hicon);
670 return NotImplemented;
673 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
675 TRACE("%p\n", cachedbmp);
678 return InvalidParameter;
680 GdipDisposeImage(cachedbmp->image);
686 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
687 GpCachedBitmap *cachedbmp, INT x, INT y)
689 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
691 if(!graphics || !cachedbmp)
692 return InvalidParameter;
694 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
697 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
701 TRACE("%p\n", image);
704 return InvalidParameter;
706 IPicture_get_CurDC(image->picture, &hdc);
708 IPicture_Release(image->picture);
709 if (image->type == ImageTypeBitmap)
710 GdipFree(((GpBitmap*)image)->bitmapbits);
716 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
719 return InvalidParameter;
721 return NotImplemented;
724 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
727 TRACE("%p %p %p\n", image, srcRect, srcUnit);
729 if(!image || !srcRect || !srcUnit)
730 return InvalidParameter;
731 if(image->type == ImageTypeMetafile){
732 *srcRect = ((GpMetafile*)image)->bounds;
733 *srcUnit = ((GpMetafile*)image)->unit;
735 else if(image->type == ImageTypeBitmap){
736 srcRect->X = srcRect->Y = 0.0;
737 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
738 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
739 *srcUnit = UnitPixel;
742 srcRect->X = srcRect->Y = 0.0;
743 srcRect->Width = ipicture_pixel_width(image->picture);
744 srcRect->Height = ipicture_pixel_height(image->picture);
745 *srcUnit = UnitPixel;
748 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
749 srcRect->Width, srcRect->Height, *srcUnit);
754 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
757 TRACE("%p %p %p\n", image, width, height);
759 if(!image || !height || !width)
760 return InvalidParameter;
762 if(image->type == ImageTypeMetafile){
765 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
766 ((GpMetafile*)image)->bounds.Height;
768 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
769 ((GpMetafile*)image)->bounds.Width;
774 else if(image->type == ImageTypeBitmap){
775 *height = ((GpBitmap*)image)->height;
776 *width = ((GpBitmap*)image)->width;
779 *height = ipicture_pixel_height(image->picture);
780 *width = ipicture_pixel_width(image->picture);
783 TRACE("returning (%f, %f)\n", *height, *width);
787 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
788 GpGraphics **graphics)
792 TRACE("%p %p\n", image, graphics);
794 if(!image || !graphics)
795 return InvalidParameter;
797 if(image->type != ImageTypeBitmap){
798 FIXME("not implemented for image type %d\n", image->type);
799 return NotImplemented;
802 IPicture_get_CurDC(image->picture, &hdc);
805 hdc = CreateCompatibleDC(0);
806 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
809 return GdipCreateFromHDC(hdc, graphics);
812 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
814 TRACE("%p %p\n", image, height);
816 if(!image || !height)
817 return InvalidParameter;
819 if(image->type == ImageTypeMetafile){
822 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
823 ((GpMetafile*)image)->bounds.Height);
827 else if(image->type == ImageTypeBitmap)
828 *height = ((GpBitmap*)image)->height;
830 *height = ipicture_pixel_height(image->picture);
832 TRACE("returning %d\n", *height);
837 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
842 return InvalidParameter;
845 FIXME("not implemented\n");
847 return NotImplemented;
850 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
852 FIXME("%p %p\n", image, size);
855 return InvalidParameter;
857 return NotImplemented;
860 /* FIXME: test this function for non-bitmap types */
861 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
863 TRACE("%p %p\n", image, format);
865 if(!image || !format)
866 return InvalidParameter;
868 if(image->type != ImageTypeBitmap)
869 *format = PixelFormat24bppRGB;
871 *format = ((GpBitmap*) image)->format;
876 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
880 if(!image || !format)
881 return InvalidParameter;
886 /* FIXME: should be detected from embedded picture or stored separately */
889 case ImageTypeBitmap: *format = ImageFormatBMP; break;
890 case ImageTypeMetafile: *format = ImageFormatEMF; break;
892 WARN("unknown type %u\n", image->type);
893 *format = ImageFormatUndefined;
898 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
900 TRACE("%p %p\n", image, type);
903 return InvalidParameter;
910 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
915 return InvalidParameter;
918 FIXME("not implemented\n");
920 return NotImplemented;
923 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
925 TRACE("%p %p\n", image, width);
928 return InvalidParameter;
930 if(image->type == ImageTypeMetafile){
933 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
934 ((GpMetafile*)image)->bounds.Width);
938 else if(image->type == ImageTypeBitmap)
939 *width = ((GpBitmap*)image)->width;
941 *width = ipicture_pixel_width(image->picture);
943 TRACE("returning %d\n", *width);
948 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
949 MetafileHeader * header)
953 if(!metafile || !header)
954 return InvalidParameter;
957 FIXME("not implemented\n");
962 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
963 UINT num, PropertyItem* items)
968 FIXME("not implemented\n");
970 return InvalidParameter;
973 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
978 FIXME("not implemented\n");
980 return InvalidParameter;
983 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
988 FIXME("not implemented\n");
990 return InvalidParameter;
993 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
994 PropertyItem* buffer)
999 FIXME("not implemented\n");
1001 return InvalidParameter;
1004 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
1009 TRACE("%p %x %p\n", image, pid, size);
1012 return InvalidParameter;
1015 FIXME("not implemented\n");
1017 return NotImplemented;
1020 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
1025 FIXME("not implemented\n");
1027 return InvalidParameter;
1030 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
1031 GDIPCONST GUID* dimensionID, UINT* count)
1035 if(!image || !dimensionID || !count)
1036 return InvalidParameter;
1039 FIXME("not implemented\n");
1041 return NotImplemented;
1044 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
1047 if(!image || !count)
1048 return InvalidParameter;
1057 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
1058 GUID* dimensionIDs, UINT count)
1062 if(!image || !dimensionIDs)
1063 return InvalidParameter;
1066 FIXME("not implemented\n");
1071 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
1072 GDIPCONST GUID* dimensionID, UINT frameidx)
1076 if(!image || !dimensionID)
1077 return InvalidParameter;
1080 FIXME("not implemented\n");
1085 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
1091 TRACE("(%s) %p\n", debugstr_w(filename), image);
1093 if (!filename || !image)
1094 return InvalidParameter;
1096 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1101 stat = GdipLoadImageFromStream(stream, image);
1103 IStream_Release(stream);
1108 /* FIXME: no icm handling */
1109 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
1111 TRACE("(%s) %p\n", debugstr_w(filename), image);
1113 return GdipLoadImageFromFile(filename, image);
1116 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
1121 TRACE("%p %p\n", stream, image);
1123 if(!stream || !image)
1124 return InvalidParameter;
1126 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1127 (LPVOID*) &pic) != S_OK){
1128 TRACE("Could not load picture\n");
1129 return GenericError;
1132 IPicture_get_Type(pic, &type);
1134 if(type == PICTYPE_BITMAP){
1136 BITMAPCOREHEADER* bmch;
1140 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1143 *image = GdipAlloc(sizeof(GpBitmap));
1148 (*image)->type = ImageTypeBitmap;
1150 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
1151 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
1153 /* get the pixel format */
1154 IPicture_get_Handle(pic, (OLE_HANDLE*)&hbm);
1155 IPicture_get_CurDC(pic, &hdc);
1157 bmch = (BITMAPCOREHEADER*) (&pbmi->bmiHeader);
1158 bmch->bcSize = sizeof(BITMAPCOREHEADER);
1162 hdc = CreateCompatibleDC(0);
1163 old = SelectObject(hdc, hbm);
1164 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1165 SelectObject(hdc, old);
1169 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1171 switch(bmch->bcBitCount)
1174 (*((GpBitmap**) image))->format = PixelFormat1bppIndexed;
1177 (*((GpBitmap**) image))->format = PixelFormat4bppIndexed;
1180 (*((GpBitmap**) image))->format = PixelFormat8bppIndexed;
1183 (*((GpBitmap**) image))->format = PixelFormat16bppRGB565;
1186 (*((GpBitmap**) image))->format = PixelFormat24bppRGB;
1189 (*((GpBitmap**) image))->format = PixelFormat32bppRGB;
1192 (*((GpBitmap**) image))->format = PixelFormat48bppRGB;
1195 FIXME("Bit depth %d is not fully supported yet\n", bmch->bcBitCount);
1196 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
1202 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
1203 /* FIXME: missing initialization code */
1204 *image = GdipAlloc(sizeof(GpMetafile));
1205 if(!*image) return OutOfMemory;
1206 (*image)->type = ImageTypeMetafile;
1209 *image = GdipAlloc(sizeof(GpImage));
1210 if(!*image) return OutOfMemory;
1211 (*image)->type = ImageTypeUnknown;
1214 (*image)->picture = pic;
1215 (*image)->flags = ImageFlagsNone;
1221 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1223 TRACE("%p %p\n", stream, image);
1225 return GdipLoadImageFromStream(stream, image);
1228 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1233 return InvalidParameter;
1236 FIXME("not implemented\n");
1238 return NotImplemented;
1241 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1246 FIXME("not implemented\n");
1248 return NotImplemented;
1251 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1252 GDIPCONST CLSID *clsidEncoder,
1253 GDIPCONST EncoderParameters *encoderParams)
1258 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1260 if (!image || !filename|| !clsidEncoder)
1261 return InvalidParameter;
1263 if (!(image->picture))
1264 return InvalidParameter;
1266 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1268 return GenericError;
1270 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1272 IStream_Release(stream);
1276 /*************************************************************************
1277 * Encoding functions -
1278 * These functions encode an image in different image file formats.
1280 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1281 #define BITMAP_FORMAT_JPEG 0xd8ff
1282 #define BITMAP_FORMAT_GIF 0x4947
1283 #define BITMAP_FORMAT_PNG 0x5089
1284 #define BITMAP_FORMAT_APM 0xcdd7
1286 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1287 void **output, unsigned int *output_size)
1289 int num_palette_entries;
1290 BITMAPFILEHEADER *bmp_file_hdr;
1291 BITMAPINFO *bmp_info_hdr;
1293 if (bitmap_info->bmiHeader.biClrUsed) {
1294 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
1295 if (num_palette_entries > 256) num_palette_entries = 256;
1297 if (bitmap_info->bmiHeader.biBitCount <= 8)
1298 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1300 num_palette_entries = 0;
1304 sizeof(BITMAPFILEHEADER) +
1305 sizeof(BITMAPINFOHEADER) +
1306 num_palette_entries * sizeof(RGBQUAD) +
1307 bitmap_info->bmiHeader.biSizeImage;
1309 *output = GdipAlloc(*output_size);
1311 bmp_file_hdr = *output;
1312 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1313 bmp_file_hdr->bfSize = *output_size;
1314 bmp_file_hdr->bfOffBits =
1315 sizeof(BITMAPFILEHEADER) +
1316 sizeof(BITMAPINFOHEADER) +
1317 num_palette_entries * sizeof (RGBQUAD);
1319 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1320 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1321 memcpy((unsigned char *)(*output) +
1322 sizeof(BITMAPFILEHEADER) +
1323 sizeof(BITMAPINFOHEADER) +
1324 num_palette_entries * sizeof(RGBQUAD),
1325 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1330 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1331 void **output, unsigned int *output_size);
1335 NUM_ENCODERS_SUPPORTED
1338 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1339 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1343 /*****************************************************************************
1344 * GdipSaveImageToStream [GDIPLUS.@]
1346 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1347 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1356 BITMAPINFO bmp_info;
1358 encode_image_func* encode_image;
1360 unsigned int output_size;
1368 TRACE("%p %p %p %p\n", image, stream, clsid, params);
1370 if(!image || !stream)
1371 return InvalidParameter;
1373 if (!image->picture)
1374 return GenericError;
1376 hr = IPicture_get_Type(image->picture, &type);
1377 if (FAILED(hr) || type != PICTYPE_BITMAP)
1378 return GenericError;
1380 /* select correct encoder */
1381 encode_image = NULL;
1382 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1383 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1384 encode_image = encode_image_funcs[i];
1386 if (encode_image == NULL)
1387 return UnknownImageFormat;
1389 /* extract underlying hbitmap representation from the IPicture */
1390 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1391 if (FAILED(hr) || !hbmp)
1392 return GenericError;
1393 hr = IPicture_get_CurDC(image->picture, &hdc);
1395 return GenericError;
1396 bm_is_selected = (hdc != 0);
1397 if (!bm_is_selected) {
1398 hdc = CreateCompatibleDC(0);
1399 old_hbmp = SelectObject(hdc, hbmp);
1402 /* get bits from HBITMAP */
1403 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1404 bmp_info.bmiHeader.biBitCount = 0;
1405 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1407 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1410 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1412 if (!bm_is_selected) {
1413 SelectObject(hdc, old_hbmp);
1420 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1422 IStream_Write(stream, output, output_size, &dummy);
1430 /*****************************************************************************
1431 * GdipSetImagePalette [GDIPLUS.@]
1433 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1434 GDIPCONST ColorPalette *palette)
1438 if(!image || !palette)
1439 return InvalidParameter;
1442 FIXME("not implemented\n");
1444 return NotImplemented;
1447 /*************************************************************************
1449 * Structures that represent which formats we support for encoding.
1452 /* ImageCodecInfo creation routines taken from libgdiplus */
1453 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1454 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1455 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1456 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1457 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1458 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1460 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1463 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1464 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1465 /* CodecName */ bmp_codecname,
1467 /* FormatDescription */ bmp_format,
1468 /* FilenameExtension */ bmp_extension,
1469 /* MimeType */ bmp_mimetype,
1470 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1474 /* SigPattern */ bmp_sig_pattern,
1475 /* SigMask */ bmp_sig_mask,
1479 /*****************************************************************************
1480 * GdipGetImageDecodersSize [GDIPLUS.@]
1482 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
1484 FIXME("%p %p stub!\n", numDecoders, size);
1486 if (!numDecoders || !size)
1487 return InvalidParameter;
1495 /*****************************************************************************
1496 * GdipGetImageDecoders [GDIPLUS.@]
1498 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
1500 FIXME("%u %u %p stub!\n", numDecoders, size, decoders);
1503 return GenericError;
1505 return NotImplemented;
1508 /*****************************************************************************
1509 * GdipGetImageEncodersSize [GDIPLUS.@]
1511 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1513 TRACE("%p %p\n", numEncoders, size);
1515 if (!numEncoders || !size)
1516 return InvalidParameter;
1518 *numEncoders = NUM_ENCODERS_SUPPORTED;
1519 *size = sizeof (codecs);
1524 /*****************************************************************************
1525 * GdipGetImageEncoders [GDIPLUS.@]
1527 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1529 TRACE("%u %u %p\n", numEncoders, size, encoders);
1532 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1533 (size != sizeof (codecs)))
1534 return GenericError;
1536 memcpy(encoders, codecs, sizeof (codecs));
1541 /*****************************************************************************
1542 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1544 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1551 TRACE("%p %p %p\n", hbm, hpal, bitmap);
1554 return InvalidParameter;
1556 /* TODO: Support for device-dependent bitmaps */
1558 FIXME("no support for device-dependent bitmaps\n");
1559 return NotImplemented;
1562 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1563 return InvalidParameter;
1565 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1566 switch(bm.bmBitsPixel) {
1568 format = PixelFormat1bppIndexed;
1571 format = PixelFormat4bppIndexed;
1574 format = PixelFormat8bppIndexed;
1577 format = PixelFormat24bppRGB;
1580 format = PixelFormat32bppRGB;
1583 format = PixelFormat48bppRGB;
1586 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1587 return InvalidParameter;
1591 bits = (BYTE*)bm.bmBits + (bm.bmHeight - 1) * bm.bmWidthBytes;
1594 FIXME("can only get image data from DIB sections\n");
1598 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, -bm.bmWidthBytes,
1599 format, bits, bitmap);
1604 /*****************************************************************************
1605 * GdipSetEffectParameters [GDIPLUS.@]
1607 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1608 const VOID *params, const UINT size)
1613 FIXME("not implemented\n");
1615 return NotImplemented;
1618 /*****************************************************************************
1619 * GdipGetImageFlags [GDIPLUS.@]
1621 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1623 TRACE("%p %p\n", image, flags);
1625 if(!image || !flags)
1626 return InvalidParameter;
1628 *flags = image->flags;
1633 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
1635 TRACE("(%d, %p)\n", control, param);
1638 case TestControlForceBilinear:
1640 FIXME("TestControlForceBilinear not handled\n");
1642 case TestControlNoICM:
1644 FIXME("TestControlNoICM not handled\n");
1646 case TestControlGetBuildNumber:
1647 *((DWORD*)param) = 3102;
1654 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
1655 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
1656 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
1657 GpMetafile **metafile)
1659 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1660 frameUnit, debugstr_w(desc), metafile);
1662 return NotImplemented;
1665 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
1666 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
1667 GDIPCONST WCHAR *desc, GpMetafile **metafile)
1669 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1670 frameUnit, debugstr_w(desc), metafile);
1672 return NotImplemented;
1675 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
1677 FIXME("%p\n", image);
1682 /*****************************************************************************
1683 * GdipGetImageThumbnail [GDIPLUS.@]
1685 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
1686 GpImage **ret_image, GetThumbnailImageAbort cb,
1689 FIXME("(%p %u %u %p %p %p) stub\n",
1690 image, width, height, ret_image, cb, cb_data);
1691 return NotImplemented;
1694 /*****************************************************************************
1695 * GdipImageRotateFlip [GDIPLUS.@]
1697 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
1699 FIXME("(%p %u) stub\n", image, type);
1700 return NotImplemented;