2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "gdiplus_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus
);
37 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
39 static INT
ipicture_pixel_height(IPicture
*pic
)
44 IPicture_get_Height(pic
, &y
);
48 y
= MulDiv(y
, GetDeviceCaps(hdcref
, LOGPIXELSY
), INCH_HIMETRIC
);
54 static INT
ipicture_pixel_width(IPicture
*pic
)
59 IPicture_get_Width(pic
, &x
);
63 x
= MulDiv(x
, GetDeviceCaps(hdcref
, LOGPIXELSX
), INCH_HIMETRIC
);
70 GpStatus WINGDIPAPI
GdipBitmapGetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
74 TRACE("%p %d %d %p\n", bitmap
, x
, y
, color
);
77 return InvalidParameter
;
80 FIXME("not implemented\n");
84 return NotImplemented
;
87 /* This function returns a pointer to an array of pixels that represents the
88 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
89 * flags. It is correct behavior that a user who calls this function with write
90 * privileges can write to the whole bitmap (not just the area in rect).
92 * FIXME: only used portion of format is bits per pixel. */
93 GpStatus WINGDIPAPI
GdipBitmapLockBits(GpBitmap
* bitmap
, GDIPCONST GpRect
* rect
,
94 UINT flags
, PixelFormat format
, BitmapData
* lockeddata
)
97 INT stride
, bitspp
= PIXELFORMATBPP(format
);
104 GpRect act_rect
; /* actual rect to be used */
106 TRACE("%p %p %d %d %p\n", bitmap
, rect
, flags
, format
, lockeddata
);
108 if(!lockeddata
|| !bitmap
)
109 return InvalidParameter
;
112 if(rect
->X
< 0 || rect
->Y
< 0 || (rect
->X
+ rect
->Width
> bitmap
->width
) ||
113 (rect
->Y
+ rect
->Height
> bitmap
->height
) || !flags
)
114 return InvalidParameter
;
119 act_rect
.X
= act_rect
.Y
= 0;
120 act_rect
.Width
= bitmap
->width
;
121 act_rect
.Height
= bitmap
->height
;
124 if(flags
& ImageLockModeUserInputBuf
)
125 return NotImplemented
;
130 IPicture_get_Handle(bitmap
->image
.picture
, &hbm
);
131 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
132 bm_is_selected
= (hdc
!= 0);
134 bmi
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
135 bmi
.bmiHeader
.biBitCount
= 0;
138 hdc
= CreateCompatibleDC(0);
139 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
143 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
145 abs_height
= abs(bmi
.bmiHeader
.biHeight
);
146 stride
= bmi
.bmiHeader
.biWidth
* bitspp
/ 8;
147 stride
= (stride
+ 3) & ~3;
149 buff
= GdipAlloc(stride
* abs_height
);
151 bmi
.bmiHeader
.biBitCount
= bitspp
;
154 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, abs_height
, buff
, &bmi
, DIB_RGB_COLORS
);
157 SelectObject(hdc
, old
);
164 lockeddata
->Width
= act_rect
.Width
;
165 lockeddata
->Height
= act_rect
.Height
;
166 lockeddata
->PixelFormat
= format
;
167 lockeddata
->Reserved
= flags
;
169 if(bmi
.bmiHeader
.biHeight
> 0){
170 lockeddata
->Stride
= -stride
;
171 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * act_rect
.X
+
172 stride
* (abs_height
- 1 - act_rect
.Y
);
175 lockeddata
->Stride
= stride
;
176 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * act_rect
.X
+ stride
* act_rect
.Y
;
179 bitmap
->lockmode
= flags
;
182 bitmap
->bitmapbits
= buff
;
187 GpStatus WINGDIPAPI
GdipBitmapUnlockBits(GpBitmap
* bitmap
,
188 BitmapData
* lockeddata
)
196 if(!bitmap
|| !lockeddata
)
197 return InvalidParameter
;
199 if(!bitmap
->lockmode
)
202 if(lockeddata
->Reserved
& ImageLockModeUserInputBuf
)
203 return NotImplemented
;
205 if(lockeddata
->Reserved
& ImageLockModeRead
){
206 if(!(--bitmap
->numlocks
))
207 bitmap
->lockmode
= 0;
209 GdipFree(bitmap
->bitmapbits
);
210 bitmap
->bitmapbits
= NULL
;
214 IPicture_get_Handle(bitmap
->image
.picture
, &hbm
);
215 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
216 bm_is_selected
= (hdc
!= 0);
218 bmi
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
219 bmi
.bmiHeader
.biBitCount
= 0;
222 hdc
= CreateCompatibleDC(0);
223 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
226 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
227 bmi
.bmiHeader
.biBitCount
= PIXELFORMATBPP(lockeddata
->PixelFormat
);
228 SetDIBits(hdc
, (HBITMAP
)hbm
, 0, abs(bmi
.bmiHeader
.biHeight
),
229 bitmap
->bitmapbits
, &bmi
, DIB_RGB_COLORS
);
232 SelectObject(hdc
, old
);
236 GdipFree(bitmap
->bitmapbits
);
237 bitmap
->bitmapbits
= NULL
;
238 bitmap
->lockmode
= 0;
243 GpStatus WINGDIPAPI
GdipCloneImage(GpImage
*image
, GpImage
**cloneImage
)
249 TRACE("%p, %p\n", image
, cloneImage
);
251 if (!image
|| !cloneImage
)
252 return InvalidParameter
;
254 hr
= CreateStreamOnHGlobal(0, TRUE
, &stream
);
258 *cloneImage
= GdipAlloc(sizeof(GpImage
));
261 IStream_Release(stream
);
264 (*cloneImage
)->type
= image
->type
;
265 (*cloneImage
)->flags
= image
->flags
;
267 hr
= IPicture_SaveAsFile(image
->picture
, stream
, FALSE
, &size
);
270 WARN("Failed to save image on stream\n");
274 hr
= OleLoadPicture(stream
, size
, FALSE
, &IID_IPicture
,
275 (LPVOID
*) &(*cloneImage
)->picture
);
278 WARN("Failed to load image from stream\n");
282 IStream_Release(stream
);
285 IStream_Release(stream
);
286 GdipFree(*cloneImage
);
291 GpStatus WINGDIPAPI
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
297 if(!filename
|| !bitmap
)
298 return InvalidParameter
;
300 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
305 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
307 IStream_Release(stream
);
312 GpStatus WINGDIPAPI
GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO
* info
,
313 VOID
*bits
, GpBitmap
**bitmap
)
315 DWORD height
, stride
;
318 FIXME("(%p, %p, %p) - partially implemented\n", info
, bits
, bitmap
);
320 height
= abs(info
->bmiHeader
.biHeight
);
321 stride
= ((info
->bmiHeader
.biWidth
* info
->bmiHeader
.biBitCount
+ 31) >> 3) & ~3;
323 if(info
->bmiHeader
.biHeight
> 0) /* bottom-up */
325 bits
= (BYTE
*)bits
+ (height
- 1) * stride
;
329 switch(info
->bmiHeader
.biBitCount
) {
331 format
= PixelFormat1bppIndexed
;
334 format
= PixelFormat4bppIndexed
;
337 format
= PixelFormat8bppIndexed
;
340 format
= PixelFormat24bppRGB
;
343 FIXME("don't know how to handle %d bpp\n", info
->bmiHeader
.biBitCount
);
345 return InvalidParameter
;
348 return GdipCreateBitmapFromScan0(info
->bmiHeader
.biWidth
, height
, stride
, format
,
354 GpStatus WINGDIPAPI
GdipCreateBitmapFromFileICM(GDIPCONST WCHAR
* filename
,
357 return GdipCreateBitmapFromFile(filename
, bitmap
);
360 GpStatus WINGDIPAPI
GdipCreateBitmapFromResource(HINSTANCE hInstance
,
361 GDIPCONST WCHAR
* lpBitmapName
, GpBitmap
** bitmap
)
364 GpStatus stat
= InvalidParameter
;
366 if(!lpBitmapName
|| !bitmap
)
367 return InvalidParameter
;
370 hbm
= (HBITMAP
)LoadImageW(hInstance
,lpBitmapName
,IMAGE_BITMAP
,0,0,LR_CREATEDIBSECTION
);
373 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, bitmap
);
380 GpStatus WINGDIPAPI
GdipCreateHBITMAPFromBitmap(GpBitmap
* bitmap
,
381 HBITMAP
* hbmReturn
, ARGB background
)
387 return NotImplemented
;
390 GpStatus WINGDIPAPI
GdipConvertToEmfPlus(const GpGraphics
* ref
,
391 GpMetafile
* metafile
, BOOL
* succ
, EmfType emfType
,
392 const WCHAR
* description
, GpMetafile
** out_metafile
)
396 if(!ref
|| !metafile
|| !out_metafile
)
397 return InvalidParameter
;
400 *out_metafile
= NULL
;
403 FIXME("not implemented\n");
405 return NotImplemented
;
408 /* FIXME: this should create a bitmap in the given size with the attributes
409 * (resolution etc.) of the graphics object */
410 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
411 GpGraphics
* target
, GpBitmap
** bitmap
)
416 if(!target
|| !bitmap
)
417 return InvalidParameter
;
420 FIXME("hacked stub\n");
422 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat24bppRGB
,
428 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
429 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
431 BITMAPFILEHEADER
*bmfh
;
432 BITMAPINFOHEADER
*bmih
;
437 TRACE("%d %d %d %d %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
439 if(!bitmap
|| width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
441 return InvalidParameter
;
445 return InvalidParameter
;
447 /* FIXME: windows allows negative stride (reads backwards from scan0) */
449 FIXME("negative stride\n");
450 return InvalidParameter
;
453 *bitmap
= GdipAlloc(sizeof(GpBitmap
));
454 if(!*bitmap
) return OutOfMemory
;
457 stride
= width
* (PIXELFORMATBPP(format
) / 8);
458 stride
= (stride
+ 3) & ~3;
461 datalen
= abs(stride
* height
);
462 size
= sizeof(BITMAPFILEHEADER
) + sizeof(BITMAPINFOHEADER
) + datalen
;
463 buff
= GdipAlloc(size
);
469 bmfh
= (BITMAPFILEHEADER
*) buff
;
470 bmih
= (BITMAPINFOHEADER
*) (bmfh
+ 1);
472 bmfh
->bfType
= (((WORD
)'M') << 8) + (WORD
)'B';
474 bmfh
->bfOffBits
= size
- datalen
;
476 bmih
->biSize
= sizeof(BITMAPINFOHEADER
);
477 bmih
->biWidth
= width
;
478 bmih
->biHeight
= -height
;
479 /* FIXME: use the rest of the data from format */
480 bmih
->biBitCount
= PIXELFORMATBPP(format
);
481 bmih
->biCompression
= BI_RGB
;
482 bmih
->biSizeImage
= datalen
;
485 memcpy(bmih
+ 1, scan0
, datalen
);
487 memset(bmih
+ 1, 0, datalen
);
489 if(CreateStreamOnHGlobal(buff
, TRUE
, &stream
) != S_OK
){
490 ERR("could not make stream\n");
496 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
497 (LPVOID
*) &((*bitmap
)->image
.picture
)) != S_OK
){
498 TRACE("Could not load picture\n");
499 IStream_Release(stream
);
505 (*bitmap
)->image
.type
= ImageTypeBitmap
;
506 (*bitmap
)->image
.flags
= ImageFlagsNone
;
507 (*bitmap
)->width
= width
;
508 (*bitmap
)->height
= height
;
509 (*bitmap
)->format
= format
;
514 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
519 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
524 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
525 IPicture_Release((*bitmap
)->image
.picture
);
527 return GenericError
; /* FIXME: what error to return? */
534 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
537 return GdipCreateBitmapFromStream(stream
, bitmap
);
540 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
545 return InvalidParameter
;
547 IPicture_get_CurDC(image
->picture
, &hdc
);
549 IPicture_Release(image
->picture
);
550 if (image
->type
== ImageTypeBitmap
)
551 GdipFree(((GpBitmap
*)image
)->bitmapbits
);
557 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
560 return InvalidParameter
;
562 return NotImplemented
;
565 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
568 if(!image
|| !srcRect
|| !srcUnit
)
569 return InvalidParameter
;
570 if(image
->type
== ImageTypeMetafile
){
571 *srcRect
= ((GpMetafile
*)image
)->bounds
;
572 *srcUnit
= ((GpMetafile
*)image
)->unit
;
574 else if(image
->type
== ImageTypeBitmap
){
575 srcRect
->X
= srcRect
->Y
= 0.0;
576 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
577 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
578 *srcUnit
= UnitPixel
;
581 srcRect
->X
= srcRect
->Y
= 0.0;
582 srcRect
->Width
= ipicture_pixel_width(image
->picture
);
583 srcRect
->Height
= ipicture_pixel_height(image
->picture
);
584 *srcUnit
= UnitPixel
;
587 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
588 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
593 GpStatus WINGDIPAPI
GdipGetImageDimension(GpImage
*image
, REAL
*width
,
596 if(!image
|| !height
|| !width
)
597 return InvalidParameter
;
599 if(image
->type
== ImageTypeMetafile
){
602 *height
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
603 ((GpMetafile
*)image
)->bounds
.Height
;
605 *width
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
606 ((GpMetafile
*)image
)->bounds
.Width
;
611 else if(image
->type
== ImageTypeBitmap
){
612 *height
= ((GpBitmap
*)image
)->height
;
613 *width
= ((GpBitmap
*)image
)->width
;
616 *height
= ipicture_pixel_height(image
->picture
);
617 *width
= ipicture_pixel_width(image
->picture
);
620 TRACE("returning (%f, %f)\n", *height
, *width
);
624 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
625 GpGraphics
**graphics
)
629 if(!image
|| !graphics
)
630 return InvalidParameter
;
632 if(image
->type
!= ImageTypeBitmap
){
633 FIXME("not implemented for image type %d\n", image
->type
);
634 return NotImplemented
;
637 IPicture_get_CurDC(image
->picture
, &hdc
);
640 hdc
= CreateCompatibleDC(0);
641 IPicture_SelectPicture(image
->picture
, hdc
, NULL
, NULL
);
644 return GdipCreateFromHDC(hdc
, graphics
);
647 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
649 if(!image
|| !height
)
650 return InvalidParameter
;
652 if(image
->type
== ImageTypeMetafile
){
655 *height
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
656 ((GpMetafile
*)image
)->bounds
.Height
);
660 else if(image
->type
== ImageTypeBitmap
)
661 *height
= ((GpBitmap
*)image
)->height
;
663 *height
= ipicture_pixel_height(image
->picture
);
665 TRACE("returning %d\n", *height
);
670 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
675 return InvalidParameter
;
678 FIXME("not implemented\n");
680 return NotImplemented
;
683 /* FIXME: test this function for non-bitmap types */
684 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
686 if(!image
|| !format
)
687 return InvalidParameter
;
689 if(image
->type
!= ImageTypeBitmap
)
690 *format
= PixelFormat24bppRGB
;
692 *format
= ((GpBitmap
*) image
)->format
;
697 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
701 if(!image
|| !format
)
702 return InvalidParameter
;
705 FIXME("not implemented\n");
707 return NotImplemented
;
710 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
713 return InvalidParameter
;
720 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
725 return InvalidParameter
;
728 FIXME("not implemented\n");
730 return NotImplemented
;
733 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
736 return InvalidParameter
;
738 if(image
->type
== ImageTypeMetafile
){
741 *width
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
742 ((GpMetafile
*)image
)->bounds
.Width
);
746 else if(image
->type
== ImageTypeBitmap
)
747 *width
= ((GpBitmap
*)image
)->width
;
749 *width
= ipicture_pixel_width(image
->picture
);
751 TRACE("returning %d\n", *width
);
756 GpStatus WINGDIPAPI
GdipGetMetafileHeaderFromMetafile(GpMetafile
* metafile
,
757 MetafileHeader
* header
)
761 if(!metafile
|| !header
)
762 return InvalidParameter
;
765 FIXME("not implemented\n");
770 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID pid
,
775 TRACE("%p %x %p\n", image
, pid
, size
);
778 return InvalidParameter
;
781 FIXME("not implemented\n");
783 return NotImplemented
;
786 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
787 GDIPCONST GUID
* dimensionID
, UINT
* count
)
791 if(!image
|| !dimensionID
|| !count
)
792 return InvalidParameter
;
795 FIXME("not implemented\n");
797 return NotImplemented
;
800 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsCount(GpImage
*image
,
804 return InvalidParameter
;
813 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
814 GUID
* dimensionIDs
, UINT count
)
818 if(!image
|| !dimensionIDs
)
819 return InvalidParameter
;
822 FIXME("not implemented\n");
827 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
,
828 GDIPCONST GUID
* dimensionID
, UINT frameidx
)
832 if(!image
|| !dimensionID
)
833 return InvalidParameter
;
836 FIXME("not implemented\n");
841 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
847 if (!filename
|| !image
)
848 return InvalidParameter
;
850 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
855 stat
= GdipLoadImageFromStream(stream
, image
);
857 IStream_Release(stream
);
862 /* FIXME: no icm handling */
863 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
865 return GdipLoadImageFromFile(filename
, image
);
868 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
* stream
, GpImage
**image
)
873 if(!stream
|| !image
)
874 return InvalidParameter
;
876 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
877 (LPVOID
*) &pic
) != S_OK
){
878 TRACE("Could not load picture\n");
882 IPicture_get_Type(pic
, &type
);
884 if(type
== PICTYPE_BITMAP
){
886 BITMAPCOREHEADER
* bmch
;
890 *image
= GdipAlloc(sizeof(GpBitmap
));
891 if(!*image
) return OutOfMemory
;
892 (*image
)->type
= ImageTypeBitmap
;
894 (*((GpBitmap
**) image
))->width
= ipicture_pixel_width(pic
);
895 (*((GpBitmap
**) image
))->height
= ipicture_pixel_height(pic
);
897 /* get the pixel format */
898 IPicture_get_Handle(pic
, &hbm
);
899 IPicture_get_CurDC(pic
, &hdc
);
901 ZeroMemory(&bmi
, sizeof(bmi
));
902 bmch
= (BITMAPCOREHEADER
*) (&bmi
.bmiHeader
);
903 bmch
->bcSize
= sizeof(BITMAPCOREHEADER
);
907 hdc
= CreateCompatibleDC(0);
908 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
909 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
910 SelectObject(hdc
, old
);
914 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
916 (*((GpBitmap
**) image
))->format
= (bmch
->bcBitCount
<< 8) | PixelFormatGDI
;
918 else if(type
== PICTYPE_METAFILE
|| type
== PICTYPE_ENHMETAFILE
){
919 /* FIXME: missing initialization code */
920 *image
= GdipAlloc(sizeof(GpMetafile
));
921 if(!*image
) return OutOfMemory
;
922 (*image
)->type
= ImageTypeMetafile
;
925 *image
= GdipAlloc(sizeof(GpImage
));
926 if(!*image
) return OutOfMemory
;
927 (*image
)->type
= ImageTypeUnknown
;
930 (*image
)->picture
= pic
;
931 (*image
)->flags
= ImageFlagsNone
;
937 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
939 return GdipLoadImageFromStream(stream
, image
);
942 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
947 return InvalidParameter
;
950 FIXME("not implemented\n");
952 return NotImplemented
;
955 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
956 GDIPCONST CLSID
*clsidEncoder
,
957 GDIPCONST EncoderParameters
*encoderParams
)
962 if (!image
|| !filename
|| !clsidEncoder
)
963 return InvalidParameter
;
965 if (!(image
->picture
))
966 return InvalidParameter
;
968 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
972 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
974 IStream_Release(stream
);
978 /*************************************************************************
979 * Encoding functions -
980 * These functions encode an image in different image file formats.
982 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
983 #define BITMAP_FORMAT_JPEG 0xd8ff
984 #define BITMAP_FORMAT_GIF 0x4947
985 #define BITMAP_FORMAT_PNG 0x5089
986 #define BITMAP_FORMAT_APM 0xcdd7
988 static GpStatus
encode_image_BMP(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
989 void **output
, unsigned int *output_size
)
991 int num_palette_entries
;
992 BITMAPFILEHEADER
*bmp_file_hdr
;
993 BITMAPINFO
*bmp_info_hdr
;
995 if (bitmap_info
->bmiHeader
.biClrUsed
) {
996 num_palette_entries
= bitmap_info
->bmiHeader
.biClrUsed
;
997 if (num_palette_entries
> 256) num_palette_entries
= 256;
999 if (bitmap_info
->bmiHeader
.biBitCount
<= 8)
1000 num_palette_entries
= 1 << bitmap_info
->bmiHeader
.biBitCount
;
1002 num_palette_entries
= 0;
1006 sizeof(BITMAPFILEHEADER
) +
1007 sizeof(BITMAPINFOHEADER
) +
1008 num_palette_entries
* sizeof(RGBQUAD
) +
1009 bitmap_info
->bmiHeader
.biSizeImage
;
1011 *output
= GdipAlloc(*output_size
);
1013 bmp_file_hdr
= (BITMAPFILEHEADER
*) *output
;
1014 bmp_file_hdr
->bfType
= BITMAP_FORMAT_BMP
;
1015 bmp_file_hdr
->bfSize
= *output_size
;
1016 bmp_file_hdr
->bfOffBits
=
1017 sizeof(BITMAPFILEHEADER
) +
1018 sizeof(BITMAPINFOHEADER
) +
1019 num_palette_entries
* sizeof (RGBQUAD
);
1021 bmp_info_hdr
= (BITMAPINFO
*) ((unsigned char*)(*output
) + sizeof(BITMAPFILEHEADER
));
1022 memcpy(bmp_info_hdr
, bitmap_info
, sizeof(BITMAPINFOHEADER
) + num_palette_entries
* sizeof(RGBQUAD
));
1023 memcpy((unsigned char *)(*output
) +
1024 sizeof(BITMAPFILEHEADER
) +
1025 sizeof(BITMAPINFOHEADER
) +
1026 num_palette_entries
* sizeof(RGBQUAD
),
1027 bitmap_bits
, bitmap_info
->bmiHeader
.biSizeImage
);
1032 typedef GpStatus
encode_image_func(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
1033 void **output
, unsigned int *output_size
);
1037 NUM_ENCODERS_SUPPORTED
1040 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
];
1041 static encode_image_func
*const encode_image_funcs
[NUM_ENCODERS_SUPPORTED
] = {
1045 /*****************************************************************************
1046 * GdipSaveImageToStream [GDIPLUS.@]
1048 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
1049 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
1058 BITMAPINFO bmp_info
;
1060 encode_image_func
* encode_image
;
1062 unsigned int output_size
;
1070 if(!image
|| !stream
)
1071 return InvalidParameter
;
1073 if (!image
->picture
)
1074 return GenericError
;
1076 hr
= IPicture_get_Type(image
->picture
, &type
);
1077 if (FAILED(hr
) || type
!= PICTYPE_BITMAP
)
1078 return GenericError
;
1080 /* select correct encoder */
1081 encode_image
= NULL
;
1082 for (i
= 0; i
< NUM_ENCODERS_SUPPORTED
; i
++) {
1083 if (IsEqualCLSID(clsid
, &codecs
[i
].Clsid
))
1084 encode_image
= encode_image_funcs
[i
];
1086 if (encode_image
== NULL
)
1087 return UnknownImageFormat
;
1089 /* extract underlying hbitmap representation from the IPicture */
1090 hr
= IPicture_get_Handle(image
->picture
, (OLE_HANDLE
*)&hbmp
);
1091 if (FAILED(hr
) || !hbmp
)
1092 return GenericError
;
1093 hr
= IPicture_get_CurDC(image
->picture
, &hdc
);
1095 return GenericError
;
1096 bm_is_selected
= (hdc
!= 0);
1097 if (!bm_is_selected
) {
1098 hdc
= CreateCompatibleDC(0);
1099 old_hbmp
= SelectObject(hdc
, hbmp
);
1102 /* get bits from HBITMAP */
1103 bmp_info
.bmiHeader
.biSize
= sizeof(bmp_info
.bmiHeader
);
1104 bmp_info
.bmiHeader
.biBitCount
= 0;
1105 GetDIBits(hdc
, hbmp
, 0, 0, NULL
, &bmp_info
, DIB_RGB_COLORS
);
1107 bmp_bits
= GdipAlloc(bmp_info
.bmiHeader
.biSizeImage
);
1110 GetDIBits(hdc
, hbmp
, 0, abs(bmp_info
.bmiHeader
.biHeight
), bmp_bits
, &bmp_info
, DIB_RGB_COLORS
);
1112 if (!bm_is_selected
) {
1113 SelectObject(hdc
, old_hbmp
);
1120 stat
= encode_image(bmp_bits
, &bmp_info
, &output
, &output_size
);
1122 IStream_Write(stream
, output
, output_size
, &dummy
);
1130 /*****************************************************************************
1131 * GdipSetImagePalette [GDIPLUS.@]
1133 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
1134 GDIPCONST ColorPalette
*palette
)
1138 if(!image
|| !palette
)
1139 return InvalidParameter
;
1142 FIXME("not implemented\n");
1144 return NotImplemented
;
1147 /*************************************************************************
1149 * Structures that represent which formats we support for encoding.
1152 /* ImageCodecInfo creation routines taken from libgdiplus */
1153 static const WCHAR bmp_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1154 static const WCHAR bmp_extension
[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1155 static const WCHAR bmp_mimetype
[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1156 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0}; /* BMP */
1157 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
1158 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
1160 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
] =
1163 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1164 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1165 /* CodecName */ bmp_codecname
,
1167 /* FormatDescription */ bmp_format
,
1168 /* FilenameExtension */ bmp_extension
,
1169 /* MimeType */ bmp_mimetype
,
1170 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
1174 /* SigPattern */ bmp_sig_pattern
,
1175 /* SigMask */ bmp_sig_mask
,
1179 /*****************************************************************************
1180 * GdipGetImageEncodersSize [GDIPLUS.@]
1182 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
1184 if (!numEncoders
|| !size
)
1185 return InvalidParameter
;
1187 *numEncoders
= NUM_ENCODERS_SUPPORTED
;
1188 *size
= sizeof (codecs
);
1193 /*****************************************************************************
1194 * GdipGetImageEncoders [GDIPLUS.@]
1196 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
1199 (numEncoders
!= NUM_ENCODERS_SUPPORTED
) ||
1200 (size
!= sizeof (codecs
)))
1201 return GenericError
;
1203 memcpy(encoders
, codecs
, sizeof (codecs
));
1208 /*****************************************************************************
1209 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1211 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
1218 return InvalidParameter
;
1220 /* TODO: Support for device-dependent bitmaps */
1222 FIXME("no support for device-dependent bitmaps\n");
1223 return NotImplemented
;
1226 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
1227 return InvalidParameter
;
1229 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1230 switch(bm
.bmBitsPixel
) {
1232 format
= PixelFormat1bppIndexed
;
1235 format
= PixelFormat4bppIndexed
;
1238 format
= PixelFormat8bppIndexed
;
1241 format
= PixelFormat24bppRGB
;
1244 format
= PixelFormat48bppRGB
;
1247 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
1248 return InvalidParameter
;
1251 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, bm
.bmWidthBytes
,
1252 format
, bm
.bmBits
, bitmap
);
1257 /*****************************************************************************
1258 * GdipSetEffectParameters [GDIPLUS.@]
1260 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
1261 const VOID
*params
, const UINT size
)
1266 FIXME("not implemented\n");
1268 return NotImplemented
;
1271 /*****************************************************************************
1272 * GdipGetImageFlags [GDIPLUS.@]
1274 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
1276 if(!image
|| !flags
)
1277 return InvalidParameter
;
1279 *flags
= image
->flags
;