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 /* This function returns a pointer to an array of pixels that represents the
91 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
92 * flags. It is correct behavior that a user who calls this function with write
93 * privileges can write to the whole bitmap (not just the area in rect).
95 * FIXME: only used portion of format is bits per pixel. */
96 GpStatus WINGDIPAPI
GdipBitmapLockBits(GpBitmap
* bitmap
, GDIPCONST GpRect
* rect
,
97 UINT flags
, PixelFormat format
, BitmapData
* lockeddata
)
100 INT stride
, bitspp
= PIXELFORMATBPP(format
);
102 HBITMAP hbm
, old
= NULL
;
106 GpRect act_rect
; /* actual rect to be used */
108 TRACE("%p %p %d %d %p\n", bitmap
, rect
, flags
, format
, lockeddata
);
110 if(!lockeddata
|| !bitmap
)
111 return InvalidParameter
;
114 if(rect
->X
< 0 || rect
->Y
< 0 || (rect
->X
+ rect
->Width
> bitmap
->width
) ||
115 (rect
->Y
+ rect
->Height
> bitmap
->height
) || !flags
)
116 return InvalidParameter
;
121 act_rect
.X
= act_rect
.Y
= 0;
122 act_rect
.Width
= bitmap
->width
;
123 act_rect
.Height
= bitmap
->height
;
126 if(flags
& ImageLockModeUserInputBuf
)
127 return NotImplemented
;
132 IPicture_get_Handle(bitmap
->image
.picture
, (OLE_HANDLE
*)&hbm
);
133 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
134 bm_is_selected
= (hdc
!= 0);
136 pbmi
= GdipAlloc(sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
139 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
140 pbmi
->bmiHeader
.biBitCount
= 0;
143 hdc
= CreateCompatibleDC(0);
144 old
= SelectObject(hdc
, hbm
);
148 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
150 abs_height
= abs(pbmi
->bmiHeader
.biHeight
);
151 stride
= pbmi
->bmiHeader
.biWidth
* bitspp
/ 8;
152 stride
= (stride
+ 3) & ~3;
154 buff
= GdipAlloc(stride
* abs_height
);
156 pbmi
->bmiHeader
.biBitCount
= bitspp
;
159 GetDIBits(hdc
, hbm
, 0, abs_height
, buff
, pbmi
, DIB_RGB_COLORS
);
162 SelectObject(hdc
, old
);
171 lockeddata
->Width
= act_rect
.Width
;
172 lockeddata
->Height
= act_rect
.Height
;
173 lockeddata
->PixelFormat
= format
;
174 lockeddata
->Reserved
= flags
;
176 if(pbmi
->bmiHeader
.biHeight
> 0){
177 lockeddata
->Stride
= -stride
;
178 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * act_rect
.X
+
179 stride
* (abs_height
- 1 - act_rect
.Y
);
182 lockeddata
->Stride
= stride
;
183 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * act_rect
.X
+ stride
* act_rect
.Y
;
186 bitmap
->lockmode
= flags
;
189 bitmap
->bitmapbits
= buff
;
195 GpStatus WINGDIPAPI
GdipBitmapUnlockBits(GpBitmap
* bitmap
,
196 BitmapData
* lockeddata
)
199 HBITMAP hbm
, old
= NULL
;
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
, (OLE_HANDLE
*)&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
, hbm
);
234 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
235 pbmi
->bmiHeader
.biBitCount
= PIXELFORMATBPP(lockeddata
->PixelFormat
);
236 SetDIBits(hdc
, 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
GdipCloneBitmapAreaI(INT x
, INT y
, INT width
, INT height
,
253 PixelFormat format
, GpBitmap
* srcBitmap
, GpBitmap
** dstBitmap
)
255 FIXME("(%i,%i,%i,%i,%i,%p,%p)\n", x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
257 return NotImplemented
;
260 GpStatus WINGDIPAPI
GdipCloneImage(GpImage
*image
, GpImage
**cloneImage
)
266 GpStatus stat
= GenericError
;
268 TRACE("%p, %p\n", image
, cloneImage
);
270 if (!image
|| !cloneImage
)
271 return InvalidParameter
;
273 hr
= CreateStreamOnHGlobal(0, TRUE
, &stream
);
277 hr
= IPicture_SaveAsFile(image
->picture
, stream
, FALSE
, &size
);
280 WARN("Failed to save image on stream\n");
284 /* Set seek pointer back to the beginning of the picture */
286 hr
= IStream_Seek(stream
, move
, STREAM_SEEK_SET
, NULL
);
290 stat
= GdipLoadImageFromStream(stream
, cloneImage
);
291 if (stat
!= Ok
) WARN("Failed to load image from stream\n");
294 IStream_Release(stream
);
298 GpStatus WINGDIPAPI
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
304 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
306 if(!filename
|| !bitmap
)
307 return InvalidParameter
;
309 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
314 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
316 IStream_Release(stream
);
321 GpStatus WINGDIPAPI
GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO
* info
,
322 VOID
*bits
, GpBitmap
**bitmap
)
324 DWORD height
, stride
;
327 FIXME("(%p, %p, %p) - partially implemented\n", info
, bits
, bitmap
);
329 height
= abs(info
->bmiHeader
.biHeight
);
330 stride
= ((info
->bmiHeader
.biWidth
* info
->bmiHeader
.biBitCount
+ 31) >> 3) & ~3;
332 if(info
->bmiHeader
.biHeight
> 0) /* bottom-up */
334 bits
= (BYTE
*)bits
+ (height
- 1) * stride
;
338 switch(info
->bmiHeader
.biBitCount
) {
340 format
= PixelFormat1bppIndexed
;
343 format
= PixelFormat4bppIndexed
;
346 format
= PixelFormat8bppIndexed
;
349 format
= PixelFormat24bppRGB
;
352 FIXME("don't know how to handle %d bpp\n", info
->bmiHeader
.biBitCount
);
354 return InvalidParameter
;
357 return GdipCreateBitmapFromScan0(info
->bmiHeader
.biWidth
, height
, stride
, format
,
363 GpStatus WINGDIPAPI
GdipCreateBitmapFromFileICM(GDIPCONST WCHAR
* filename
,
366 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
368 return GdipCreateBitmapFromFile(filename
, bitmap
);
371 GpStatus WINGDIPAPI
GdipCreateBitmapFromResource(HINSTANCE hInstance
,
372 GDIPCONST WCHAR
* lpBitmapName
, GpBitmap
** bitmap
)
375 GpStatus stat
= InvalidParameter
;
377 TRACE("%p (%s) %p\n", hInstance
, debugstr_w(lpBitmapName
), bitmap
);
379 if(!lpBitmapName
|| !bitmap
)
380 return InvalidParameter
;
383 hbm
= LoadImageW(hInstance
, lpBitmapName
, IMAGE_BITMAP
, 0, 0,
384 LR_CREATEDIBSECTION
);
387 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, bitmap
);
394 GpStatus WINGDIPAPI
GdipCreateHBITMAPFromBitmap(GpBitmap
* bitmap
,
395 HBITMAP
* hbmReturn
, ARGB background
)
401 return NotImplemented
;
404 GpStatus WINGDIPAPI
GdipConvertToEmfPlus(const GpGraphics
* ref
,
405 GpMetafile
* metafile
, BOOL
* succ
, EmfType emfType
,
406 const WCHAR
* description
, GpMetafile
** out_metafile
)
410 if(!ref
|| !metafile
|| !out_metafile
)
411 return InvalidParameter
;
414 *out_metafile
= NULL
;
417 FIXME("not implemented\n");
419 return NotImplemented
;
422 /* FIXME: this should create a bitmap in the given size with the attributes
423 * (resolution etc.) of the graphics object */
424 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
425 GpGraphics
* target
, GpBitmap
** bitmap
)
430 if(!target
|| !bitmap
)
431 return InvalidParameter
;
434 FIXME("hacked stub\n");
436 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat24bppRGB
,
442 GpStatus WINGDIPAPI
GdipCreateBitmapFromHICON(HICON hicon
, GpBitmap
** bitmap
)
448 TRACE("%p, %p\n", hicon
, bitmap
);
450 if(!bitmap
|| !GetIconInfo(hicon
, &iinfo
))
451 return InvalidParameter
;
453 *bitmap
= GdipAlloc(sizeof(GpBitmap
));
454 if(!*bitmap
) return OutOfMemory
;
456 icon_copy
= CreateIconIndirect(&iinfo
);
460 return InvalidParameter
;
463 desc
.cbSizeofstruct
= sizeof(PICTDESC
);
464 desc
.picType
= PICTYPE_ICON
;
465 desc
.u
.icon
.hicon
= icon_copy
;
467 if(OleCreatePictureIndirect(&desc
, &IID_IPicture
, TRUE
,
468 (LPVOID
*) &((*bitmap
)->image
.picture
)) != S_OK
){
469 DestroyIcon(icon_copy
);
474 (*bitmap
)->format
= PixelFormat32bppARGB
;
475 (*bitmap
)->image
.type
= ImageTypeBitmap
;
476 (*bitmap
)->image
.flags
= ImageFlagsNone
;
477 (*bitmap
)->width
= ipicture_pixel_width((*bitmap
)->image
.picture
);
478 (*bitmap
)->height
= ipicture_pixel_height((*bitmap
)->image
.picture
);
480 DeleteObject(iinfo
.hbmColor
);
481 DeleteObject(iinfo
.hbmMask
);
486 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
487 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
489 BITMAPFILEHEADER
*bmfh
;
490 BITMAPINFOHEADER
*bmih
;
495 TRACE("%d %d %d %d %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
497 if (!bitmap
) return InvalidParameter
;
499 if(width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
501 return InvalidParameter
;
505 return InvalidParameter
;
507 *bitmap
= GdipAlloc(sizeof(GpBitmap
));
508 if(!*bitmap
) return OutOfMemory
;
511 stride
= width
* (PIXELFORMATBPP(format
) / 8);
512 stride
= (stride
+ 3) & ~3;
515 datalen
= abs(stride
* height
);
516 size
= sizeof(BITMAPFILEHEADER
) + sizeof(BITMAPINFOHEADER
) + datalen
;
517 buff
= GdipAlloc(size
);
523 bmfh
= (BITMAPFILEHEADER
*) buff
;
524 bmih
= (BITMAPINFOHEADER
*) (bmfh
+ 1);
526 bmfh
->bfType
= (((WORD
)'M') << 8) + (WORD
)'B';
528 bmfh
->bfOffBits
= size
- datalen
;
530 bmih
->biSize
= sizeof(BITMAPINFOHEADER
);
531 bmih
->biWidth
= width
;
532 /* FIXME: use the rest of the data from format */
533 bmih
->biBitCount
= PIXELFORMATBPP(format
);
534 bmih
->biCompression
= BI_RGB
;
535 bmih
->biSizeImage
= datalen
;
541 bmih
->biHeight
= -height
;
542 memcpy(bmih
+ 1, scan0
, datalen
);
546 bmih
->biHeight
= height
;
547 memcpy(bmih
+ 1, scan0
+ stride
* (height
- 1), datalen
);
552 bmih
->biHeight
= height
;
553 memset(bmih
+ 1, 0, datalen
);
556 if(CreateStreamOnHGlobal(buff
, TRUE
, &stream
) != S_OK
){
557 ERR("could not make stream\n");
564 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
565 (LPVOID
*) &((*bitmap
)->image
.picture
)) != S_OK
){
566 TRACE("Could not load picture\n");
567 IStream_Release(stream
);
574 (*bitmap
)->image
.type
= ImageTypeBitmap
;
575 (*bitmap
)->image
.flags
= ImageFlagsNone
;
576 (*bitmap
)->width
= width
;
577 (*bitmap
)->height
= height
;
578 (*bitmap
)->format
= format
;
583 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
588 TRACE("%p %p\n", stream
, bitmap
);
590 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
595 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
596 IPicture_Release((*bitmap
)->image
.picture
);
598 return GenericError
; /* FIXME: what error to return? */
605 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
608 TRACE("%p %p\n", stream
, bitmap
);
610 return GdipCreateBitmapFromStream(stream
, bitmap
);
613 GpStatus WINGDIPAPI
GdipCreateCachedBitmap(GpBitmap
*bitmap
, GpGraphics
*graphics
,
614 GpCachedBitmap
**cachedbmp
)
618 TRACE("%p %p %p\n", bitmap
, graphics
, cachedbmp
);
620 if(!bitmap
|| !graphics
|| !cachedbmp
)
621 return InvalidParameter
;
623 *cachedbmp
= GdipAlloc(sizeof(GpCachedBitmap
));
627 stat
= GdipCloneImage(&(bitmap
->image
), &(*cachedbmp
)->image
);
629 GdipFree(*cachedbmp
);
636 GpStatus WINGDIPAPI
GdipDeleteCachedBitmap(GpCachedBitmap
*cachedbmp
)
638 TRACE("%p\n", cachedbmp
);
641 return InvalidParameter
;
643 GdipDisposeImage(cachedbmp
->image
);
649 GpStatus WINGDIPAPI
GdipDrawCachedBitmap(GpGraphics
*graphics
,
650 GpCachedBitmap
*cachedbmp
, INT x
, INT y
)
652 TRACE("%p %p %d %d\n", graphics
, cachedbmp
, x
, y
);
654 if(!graphics
|| !cachedbmp
)
655 return InvalidParameter
;
657 return GdipDrawImage(graphics
, cachedbmp
->image
, (REAL
)x
, (REAL
)y
);
660 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
664 TRACE("%p\n", image
);
667 return InvalidParameter
;
669 IPicture_get_CurDC(image
->picture
, &hdc
);
671 IPicture_Release(image
->picture
);
672 if (image
->type
== ImageTypeBitmap
)
673 GdipFree(((GpBitmap
*)image
)->bitmapbits
);
679 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
682 return InvalidParameter
;
684 return NotImplemented
;
687 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
690 TRACE("%p %p %p\n", image
, srcRect
, srcUnit
);
692 if(!image
|| !srcRect
|| !srcUnit
)
693 return InvalidParameter
;
694 if(image
->type
== ImageTypeMetafile
){
695 *srcRect
= ((GpMetafile
*)image
)->bounds
;
696 *srcUnit
= ((GpMetafile
*)image
)->unit
;
698 else if(image
->type
== ImageTypeBitmap
){
699 srcRect
->X
= srcRect
->Y
= 0.0;
700 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
701 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
702 *srcUnit
= UnitPixel
;
705 srcRect
->X
= srcRect
->Y
= 0.0;
706 srcRect
->Width
= ipicture_pixel_width(image
->picture
);
707 srcRect
->Height
= ipicture_pixel_height(image
->picture
);
708 *srcUnit
= UnitPixel
;
711 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
712 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
717 GpStatus WINGDIPAPI
GdipGetImageDimension(GpImage
*image
, REAL
*width
,
720 TRACE("%p %p %p\n", image
, width
, height
);
722 if(!image
|| !height
|| !width
)
723 return InvalidParameter
;
725 if(image
->type
== ImageTypeMetafile
){
728 *height
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
729 ((GpMetafile
*)image
)->bounds
.Height
;
731 *width
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
732 ((GpMetafile
*)image
)->bounds
.Width
;
737 else if(image
->type
== ImageTypeBitmap
){
738 *height
= ((GpBitmap
*)image
)->height
;
739 *width
= ((GpBitmap
*)image
)->width
;
742 *height
= ipicture_pixel_height(image
->picture
);
743 *width
= ipicture_pixel_width(image
->picture
);
746 TRACE("returning (%f, %f)\n", *height
, *width
);
750 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
751 GpGraphics
**graphics
)
755 TRACE("%p %p\n", image
, graphics
);
757 if(!image
|| !graphics
)
758 return InvalidParameter
;
760 if(image
->type
!= ImageTypeBitmap
){
761 FIXME("not implemented for image type %d\n", image
->type
);
762 return NotImplemented
;
765 IPicture_get_CurDC(image
->picture
, &hdc
);
768 hdc
= CreateCompatibleDC(0);
769 IPicture_SelectPicture(image
->picture
, hdc
, NULL
, NULL
);
772 return GdipCreateFromHDC(hdc
, graphics
);
775 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
777 TRACE("%p %p\n", image
, height
);
779 if(!image
|| !height
)
780 return InvalidParameter
;
782 if(image
->type
== ImageTypeMetafile
){
785 *height
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
786 ((GpMetafile
*)image
)->bounds
.Height
);
790 else if(image
->type
== ImageTypeBitmap
)
791 *height
= ((GpBitmap
*)image
)->height
;
793 *height
= ipicture_pixel_height(image
->picture
);
795 TRACE("returning %d\n", *height
);
800 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
805 return InvalidParameter
;
808 FIXME("not implemented\n");
810 return NotImplemented
;
813 GpStatus WINGDIPAPI
GdipGetImagePaletteSize(GpImage
*image
, INT
*size
)
815 FIXME("%p %p\n", image
, size
);
818 return InvalidParameter
;
820 return NotImplemented
;
823 /* FIXME: test this function for non-bitmap types */
824 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
826 TRACE("%p %p\n", image
, format
);
828 if(!image
|| !format
)
829 return InvalidParameter
;
831 if(image
->type
!= ImageTypeBitmap
)
832 *format
= PixelFormat24bppRGB
;
834 *format
= ((GpBitmap
*) image
)->format
;
839 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
843 if(!image
|| !format
)
844 return InvalidParameter
;
849 /* FIXME: should be detected from embedded picture or stored separately */
852 case ImageTypeBitmap
: *format
= ImageFormatBMP
; break;
853 case ImageTypeMetafile
: *format
= ImageFormatEMF
; break;
855 WARN("unknown type %u\n", image
->type
);
856 *format
= ImageFormatUndefined
;
861 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
863 TRACE("%p %p\n", image
, type
);
866 return InvalidParameter
;
873 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
878 return InvalidParameter
;
881 FIXME("not implemented\n");
883 return NotImplemented
;
886 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
888 TRACE("%p %p\n", image
, width
);
891 return InvalidParameter
;
893 if(image
->type
== ImageTypeMetafile
){
896 *width
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
897 ((GpMetafile
*)image
)->bounds
.Width
);
901 else if(image
->type
== ImageTypeBitmap
)
902 *width
= ((GpBitmap
*)image
)->width
;
904 *width
= ipicture_pixel_width(image
->picture
);
906 TRACE("returning %d\n", *width
);
911 GpStatus WINGDIPAPI
GdipGetMetafileHeaderFromMetafile(GpMetafile
* metafile
,
912 MetafileHeader
* header
)
916 if(!metafile
|| !header
)
917 return InvalidParameter
;
920 FIXME("not implemented\n");
925 GpStatus WINGDIPAPI
GdipGetAllPropertyItems(GpImage
*image
, UINT size
,
926 UINT num
, PropertyItem
* items
)
931 FIXME("not implemented\n");
933 return InvalidParameter
;
936 GpStatus WINGDIPAPI
GdipGetPropertyCount(GpImage
*image
, UINT
* num
)
941 FIXME("not implemented\n");
943 return InvalidParameter
;
946 GpStatus WINGDIPAPI
GdipGetPropertyIdList(GpImage
*image
, UINT num
, PROPID
* list
)
951 FIXME("not implemented\n");
953 return InvalidParameter
;
956 GpStatus WINGDIPAPI
GdipGetPropertyItem(GpImage
*image
, PROPID id
, UINT size
,
957 PropertyItem
* buffer
)
962 FIXME("not implemented\n");
964 return InvalidParameter
;
967 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID pid
,
972 TRACE("%p %x %p\n", image
, pid
, size
);
975 return InvalidParameter
;
978 FIXME("not implemented\n");
980 return NotImplemented
;
983 GpStatus WINGDIPAPI
GdipGetPropertySize(GpImage
*image
, UINT
* size
, UINT
* num
)
988 FIXME("not implemented\n");
990 return InvalidParameter
;
993 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
994 GDIPCONST GUID
* dimensionID
, UINT
* count
)
998 if(!image
|| !dimensionID
|| !count
)
999 return InvalidParameter
;
1002 FIXME("not implemented\n");
1004 return NotImplemented
;
1007 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsCount(GpImage
*image
,
1010 if(!image
|| !count
)
1011 return InvalidParameter
;
1020 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
1021 GUID
* dimensionIDs
, UINT count
)
1025 if(!image
|| !dimensionIDs
)
1026 return InvalidParameter
;
1029 FIXME("not implemented\n");
1034 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
,
1035 GDIPCONST GUID
* dimensionID
, UINT frameidx
)
1039 if(!image
|| !dimensionID
)
1040 return InvalidParameter
;
1043 FIXME("not implemented\n");
1048 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
1054 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
1056 if (!filename
|| !image
)
1057 return InvalidParameter
;
1059 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
1064 stat
= GdipLoadImageFromStream(stream
, image
);
1066 IStream_Release(stream
);
1071 /* FIXME: no icm handling */
1072 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
1074 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
1076 return GdipLoadImageFromFile(filename
, image
);
1079 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
* stream
, GpImage
**image
)
1084 TRACE("%p %p\n", stream
, image
);
1086 if(!stream
|| !image
)
1087 return InvalidParameter
;
1089 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
1090 (LPVOID
*) &pic
) != S_OK
){
1091 TRACE("Could not load picture\n");
1092 return GenericError
;
1095 IPicture_get_Type(pic
, &type
);
1097 if(type
== PICTYPE_BITMAP
){
1099 BITMAPCOREHEADER
* bmch
;
1103 pbmi
= GdipAlloc(sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
1106 *image
= GdipAlloc(sizeof(GpBitmap
));
1111 (*image
)->type
= ImageTypeBitmap
;
1113 (*((GpBitmap
**) image
))->width
= ipicture_pixel_width(pic
);
1114 (*((GpBitmap
**) image
))->height
= ipicture_pixel_height(pic
);
1116 /* get the pixel format */
1117 IPicture_get_Handle(pic
, (OLE_HANDLE
*)&hbm
);
1118 IPicture_get_CurDC(pic
, &hdc
);
1120 bmch
= (BITMAPCOREHEADER
*) (&pbmi
->bmiHeader
);
1121 bmch
->bcSize
= sizeof(BITMAPCOREHEADER
);
1125 hdc
= CreateCompatibleDC(0);
1126 old
= SelectObject(hdc
, hbm
);
1127 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
1128 SelectObject(hdc
, old
);
1132 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
1134 switch(bmch
->bcBitCount
)
1137 (*((GpBitmap
**) image
))->format
= PixelFormat1bppIndexed
;
1140 (*((GpBitmap
**) image
))->format
= PixelFormat4bppIndexed
;
1143 (*((GpBitmap
**) image
))->format
= PixelFormat8bppIndexed
;
1146 (*((GpBitmap
**) image
))->format
= PixelFormat16bppRGB565
;
1149 (*((GpBitmap
**) image
))->format
= PixelFormat24bppRGB
;
1152 (*((GpBitmap
**) image
))->format
= PixelFormat32bppRGB
;
1155 (*((GpBitmap
**) image
))->format
= PixelFormat48bppRGB
;
1158 FIXME("Bit depth %d is not fully supported yet\n", bmch
->bcBitCount
);
1159 (*((GpBitmap
**) image
))->format
= (bmch
->bcBitCount
<< 8) | PixelFormatGDI
;
1165 else if(type
== PICTYPE_METAFILE
|| type
== PICTYPE_ENHMETAFILE
){
1166 /* FIXME: missing initialization code */
1167 *image
= GdipAlloc(sizeof(GpMetafile
));
1168 if(!*image
) return OutOfMemory
;
1169 (*image
)->type
= ImageTypeMetafile
;
1172 *image
= GdipAlloc(sizeof(GpImage
));
1173 if(!*image
) return OutOfMemory
;
1174 (*image
)->type
= ImageTypeUnknown
;
1177 (*image
)->picture
= pic
;
1178 (*image
)->flags
= ImageFlagsNone
;
1184 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
1186 TRACE("%p %p\n", stream
, image
);
1188 return GdipLoadImageFromStream(stream
, image
);
1191 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
1196 return InvalidParameter
;
1199 FIXME("not implemented\n");
1201 return NotImplemented
;
1204 GpStatus WINGDIPAPI
GdipSetPropertyItem(GpImage
*image
, GDIPCONST PropertyItem
* item
)
1209 FIXME("not implemented\n");
1211 return NotImplemented
;
1214 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
1215 GDIPCONST CLSID
*clsidEncoder
,
1216 GDIPCONST EncoderParameters
*encoderParams
)
1221 TRACE("%p (%s) %p %p\n", image
, debugstr_w(filename
), clsidEncoder
, encoderParams
);
1223 if (!image
|| !filename
|| !clsidEncoder
)
1224 return InvalidParameter
;
1226 if (!(image
->picture
))
1227 return InvalidParameter
;
1229 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
1231 return GenericError
;
1233 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
1235 IStream_Release(stream
);
1239 /*************************************************************************
1240 * Encoding functions -
1241 * These functions encode an image in different image file formats.
1243 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1244 #define BITMAP_FORMAT_JPEG 0xd8ff
1245 #define BITMAP_FORMAT_GIF 0x4947
1246 #define BITMAP_FORMAT_PNG 0x5089
1247 #define BITMAP_FORMAT_APM 0xcdd7
1249 static GpStatus
encode_image_BMP(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
1250 void **output
, unsigned int *output_size
)
1252 int num_palette_entries
;
1253 BITMAPFILEHEADER
*bmp_file_hdr
;
1254 BITMAPINFO
*bmp_info_hdr
;
1256 if (bitmap_info
->bmiHeader
.biClrUsed
) {
1257 num_palette_entries
= bitmap_info
->bmiHeader
.biClrUsed
;
1258 if (num_palette_entries
> 256) num_palette_entries
= 256;
1260 if (bitmap_info
->bmiHeader
.biBitCount
<= 8)
1261 num_palette_entries
= 1 << bitmap_info
->bmiHeader
.biBitCount
;
1263 num_palette_entries
= 0;
1267 sizeof(BITMAPFILEHEADER
) +
1268 sizeof(BITMAPINFOHEADER
) +
1269 num_palette_entries
* sizeof(RGBQUAD
) +
1270 bitmap_info
->bmiHeader
.biSizeImage
;
1272 *output
= GdipAlloc(*output_size
);
1274 bmp_file_hdr
= *output
;
1275 bmp_file_hdr
->bfType
= BITMAP_FORMAT_BMP
;
1276 bmp_file_hdr
->bfSize
= *output_size
;
1277 bmp_file_hdr
->bfOffBits
=
1278 sizeof(BITMAPFILEHEADER
) +
1279 sizeof(BITMAPINFOHEADER
) +
1280 num_palette_entries
* sizeof (RGBQUAD
);
1282 bmp_info_hdr
= (BITMAPINFO
*) ((unsigned char*)(*output
) + sizeof(BITMAPFILEHEADER
));
1283 memcpy(bmp_info_hdr
, bitmap_info
, sizeof(BITMAPINFOHEADER
) + num_palette_entries
* sizeof(RGBQUAD
));
1284 memcpy((unsigned char *)(*output
) +
1285 sizeof(BITMAPFILEHEADER
) +
1286 sizeof(BITMAPINFOHEADER
) +
1287 num_palette_entries
* sizeof(RGBQUAD
),
1288 bitmap_bits
, bitmap_info
->bmiHeader
.biSizeImage
);
1293 typedef GpStatus
encode_image_func(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
1294 void **output
, unsigned int *output_size
);
1298 NUM_ENCODERS_SUPPORTED
1301 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
];
1302 static encode_image_func
*const encode_image_funcs
[NUM_ENCODERS_SUPPORTED
] = {
1306 /*****************************************************************************
1307 * GdipSaveImageToStream [GDIPLUS.@]
1309 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
1310 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
1319 BITMAPINFO bmp_info
;
1321 encode_image_func
* encode_image
;
1323 unsigned int output_size
;
1331 TRACE("%p %p %p %p\n", image
, stream
, clsid
, params
);
1333 if(!image
|| !stream
)
1334 return InvalidParameter
;
1336 if (!image
->picture
)
1337 return GenericError
;
1339 hr
= IPicture_get_Type(image
->picture
, &type
);
1340 if (FAILED(hr
) || type
!= PICTYPE_BITMAP
)
1341 return GenericError
;
1343 /* select correct encoder */
1344 encode_image
= NULL
;
1345 for (i
= 0; i
< NUM_ENCODERS_SUPPORTED
; i
++) {
1346 if (IsEqualCLSID(clsid
, &codecs
[i
].Clsid
))
1347 encode_image
= encode_image_funcs
[i
];
1349 if (encode_image
== NULL
)
1350 return UnknownImageFormat
;
1352 /* extract underlying hbitmap representation from the IPicture */
1353 hr
= IPicture_get_Handle(image
->picture
, (OLE_HANDLE
*)&hbmp
);
1354 if (FAILED(hr
) || !hbmp
)
1355 return GenericError
;
1356 hr
= IPicture_get_CurDC(image
->picture
, &hdc
);
1358 return GenericError
;
1359 bm_is_selected
= (hdc
!= 0);
1360 if (!bm_is_selected
) {
1361 hdc
= CreateCompatibleDC(0);
1362 old_hbmp
= SelectObject(hdc
, hbmp
);
1365 /* get bits from HBITMAP */
1366 bmp_info
.bmiHeader
.biSize
= sizeof(bmp_info
.bmiHeader
);
1367 bmp_info
.bmiHeader
.biBitCount
= 0;
1368 GetDIBits(hdc
, hbmp
, 0, 0, NULL
, &bmp_info
, DIB_RGB_COLORS
);
1370 bmp_bits
= GdipAlloc(bmp_info
.bmiHeader
.biSizeImage
);
1373 GetDIBits(hdc
, hbmp
, 0, abs(bmp_info
.bmiHeader
.biHeight
), bmp_bits
, &bmp_info
, DIB_RGB_COLORS
);
1375 if (!bm_is_selected
) {
1376 SelectObject(hdc
, old_hbmp
);
1383 stat
= encode_image(bmp_bits
, &bmp_info
, &output
, &output_size
);
1385 IStream_Write(stream
, output
, output_size
, &dummy
);
1393 /*****************************************************************************
1394 * GdipSetImagePalette [GDIPLUS.@]
1396 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
1397 GDIPCONST ColorPalette
*palette
)
1401 if(!image
|| !palette
)
1402 return InvalidParameter
;
1405 FIXME("not implemented\n");
1407 return NotImplemented
;
1410 /*************************************************************************
1412 * Structures that represent which formats we support for encoding.
1415 /* ImageCodecInfo creation routines taken from libgdiplus */
1416 static const WCHAR bmp_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1417 static const WCHAR bmp_extension
[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1418 static const WCHAR bmp_mimetype
[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1419 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0}; /* BMP */
1420 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
1421 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
1423 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
] =
1426 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1427 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1428 /* CodecName */ bmp_codecname
,
1430 /* FormatDescription */ bmp_format
,
1431 /* FilenameExtension */ bmp_extension
,
1432 /* MimeType */ bmp_mimetype
,
1433 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
1437 /* SigPattern */ bmp_sig_pattern
,
1438 /* SigMask */ bmp_sig_mask
,
1442 /*****************************************************************************
1443 * GdipGetImageDecodersSize [GDIPLUS.@]
1445 GpStatus WINGDIPAPI
GdipGetImageDecodersSize(UINT
*numDecoders
, UINT
*size
)
1447 FIXME("%p %p stub!\n", numDecoders
, size
);
1449 if (!numDecoders
|| !size
)
1450 return InvalidParameter
;
1458 /*****************************************************************************
1459 * GdipGetImageDecoders [GDIPLUS.@]
1461 GpStatus WINGDIPAPI
GdipGetImageDecoders(UINT numDecoders
, UINT size
, ImageCodecInfo
*decoders
)
1463 FIXME("%u %u %p stub!\n", numDecoders
, size
, decoders
);
1466 return GenericError
;
1468 return NotImplemented
;
1471 /*****************************************************************************
1472 * GdipGetImageEncodersSize [GDIPLUS.@]
1474 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
1476 TRACE("%p %p\n", numEncoders
, size
);
1478 if (!numEncoders
|| !size
)
1479 return InvalidParameter
;
1481 *numEncoders
= NUM_ENCODERS_SUPPORTED
;
1482 *size
= sizeof (codecs
);
1487 /*****************************************************************************
1488 * GdipGetImageEncoders [GDIPLUS.@]
1490 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
1492 TRACE("%u %u %p\n", numEncoders
, size
, encoders
);
1495 (numEncoders
!= NUM_ENCODERS_SUPPORTED
) ||
1496 (size
!= sizeof (codecs
)))
1497 return GenericError
;
1499 memcpy(encoders
, codecs
, sizeof (codecs
));
1504 /*****************************************************************************
1505 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1507 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
1514 TRACE("%p %p %p\n", hbm
, hpal
, bitmap
);
1517 return InvalidParameter
;
1519 /* TODO: Support for device-dependent bitmaps */
1521 FIXME("no support for device-dependent bitmaps\n");
1522 return NotImplemented
;
1525 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
1526 return InvalidParameter
;
1528 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1529 switch(bm
.bmBitsPixel
) {
1531 format
= PixelFormat1bppIndexed
;
1534 format
= PixelFormat4bppIndexed
;
1537 format
= PixelFormat8bppIndexed
;
1540 format
= PixelFormat24bppRGB
;
1543 format
= PixelFormat32bppRGB
;
1546 format
= PixelFormat48bppRGB
;
1549 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
1550 return InvalidParameter
;
1554 bits
= (BYTE
*)bm
.bmBits
+ (bm
.bmHeight
- 1) * bm
.bmWidthBytes
;
1557 FIXME("can only get image data from DIB sections\n");
1561 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, -bm
.bmWidthBytes
,
1562 format
, bits
, bitmap
);
1567 /*****************************************************************************
1568 * GdipSetEffectParameters [GDIPLUS.@]
1570 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
1571 const VOID
*params
, const UINT size
)
1576 FIXME("not implemented\n");
1578 return NotImplemented
;
1581 /*****************************************************************************
1582 * GdipGetImageFlags [GDIPLUS.@]
1584 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
1586 TRACE("%p %p\n", image
, flags
);
1588 if(!image
|| !flags
)
1589 return InvalidParameter
;
1591 *flags
= image
->flags
;
1596 GpStatus WINGDIPAPI
GdipTestControl(GpTestControlEnum control
, void *param
)
1598 TRACE("(%d, %p)\n", control
, param
);
1601 case TestControlForceBilinear
:
1603 FIXME("TestControlForceBilinear not handled\n");
1605 case TestControlNoICM
:
1607 FIXME("TestControlNoICM not handled\n");
1609 case TestControlGetBuildNumber
:
1610 *((DWORD
*)param
) = 3102;
1617 GpStatus WINGDIPAPI
GdipRecordMetafileFileName(GDIPCONST WCHAR
* fileName
,
1618 HDC hdc
, EmfType type
, GDIPCONST GpRectF
*pFrameRect
,
1619 MetafileFrameUnit frameUnit
, GDIPCONST WCHAR
*desc
,
1620 GpMetafile
**metafile
)
1622 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName
), hdc
, type
, pFrameRect
,
1623 frameUnit
, debugstr_w(desc
), metafile
);
1625 return NotImplemented
;
1628 GpStatus WINGDIPAPI
GdipRecordMetafileFileNameI(GDIPCONST WCHAR
* fileName
, HDC hdc
, EmfType type
,
1629 GDIPCONST GpRect
*pFrameRect
, MetafileFrameUnit frameUnit
,
1630 GDIPCONST WCHAR
*desc
, GpMetafile
**metafile
)
1632 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName
), hdc
, type
, pFrameRect
,
1633 frameUnit
, debugstr_w(desc
), metafile
);
1635 return NotImplemented
;
1638 GpStatus WINGDIPAPI
GdipImageForceValidation(GpImage
*image
)
1640 FIXME("%p\n", image
);