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
);
105 TRACE("%p %p %d %d %p\n", bitmap
, rect
, flags
, format
, lockeddata
);
107 if(!lockeddata
|| !bitmap
|| !rect
)
108 return InvalidParameter
;
110 if(rect
->X
< 0 || rect
->Y
< 0 || (rect
->X
+ rect
->Width
> bitmap
->width
) ||
111 (rect
->Y
+ rect
->Height
> bitmap
->height
) || !flags
)
112 return InvalidParameter
;
114 if(flags
& ImageLockModeUserInputBuf
)
115 return NotImplemented
;
120 IPicture_get_Handle(bitmap
->image
.picture
, &hbm
);
121 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
122 bm_is_selected
= (hdc
!= 0);
124 bmi
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
125 bmi
.bmiHeader
.biBitCount
= 0;
128 hdc
= CreateCompatibleDC(0);
129 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
133 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
135 abs_height
= abs(bmi
.bmiHeader
.biHeight
);
136 stride
= bmi
.bmiHeader
.biWidth
* bitspp
/ 8;
137 stride
= (stride
+ 3) & ~3;
139 buff
= GdipAlloc(stride
* abs_height
);
141 bmi
.bmiHeader
.biBitCount
= bitspp
;
144 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, abs_height
, buff
, &bmi
, DIB_RGB_COLORS
);
147 SelectObject(hdc
, old
);
154 lockeddata
->Width
= rect
->Width
;
155 lockeddata
->Height
= rect
->Height
;
156 lockeddata
->PixelFormat
= format
;
157 lockeddata
->Reserved
= flags
;
159 if(bmi
.bmiHeader
.biHeight
> 0){
160 lockeddata
->Stride
= -stride
;
161 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * rect
->X
+
162 stride
* (abs_height
- 1 - rect
->Y
);
165 lockeddata
->Stride
= stride
;
166 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * rect
->X
+ stride
* rect
->Y
;
169 bitmap
->lockmode
= flags
;
172 bitmap
->bitmapbits
= buff
;
177 GpStatus WINGDIPAPI
GdipBitmapUnlockBits(GpBitmap
* bitmap
,
178 BitmapData
* lockeddata
)
186 if(!bitmap
|| !lockeddata
)
187 return InvalidParameter
;
189 if(!bitmap
->lockmode
)
192 if(lockeddata
->Reserved
& ImageLockModeUserInputBuf
)
193 return NotImplemented
;
195 if(lockeddata
->Reserved
& ImageLockModeRead
){
196 if(!(--bitmap
->numlocks
))
197 bitmap
->lockmode
= 0;
199 GdipFree(bitmap
->bitmapbits
);
200 bitmap
->bitmapbits
= NULL
;
204 IPicture_get_Handle(bitmap
->image
.picture
, &hbm
);
205 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
206 bm_is_selected
= (hdc
!= 0);
208 bmi
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
209 bmi
.bmiHeader
.biBitCount
= 0;
212 hdc
= CreateCompatibleDC(0);
213 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
216 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
217 bmi
.bmiHeader
.biBitCount
= PIXELFORMATBPP(lockeddata
->PixelFormat
);
218 SetDIBits(hdc
, (HBITMAP
)hbm
, 0, abs(bmi
.bmiHeader
.biHeight
),
219 bitmap
->bitmapbits
, &bmi
, DIB_RGB_COLORS
);
222 SelectObject(hdc
, old
);
226 GdipFree(bitmap
->bitmapbits
);
227 bitmap
->bitmapbits
= NULL
;
228 bitmap
->lockmode
= 0;
233 GpStatus WINGDIPAPI
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
239 if(!filename
|| !bitmap
)
240 return InvalidParameter
;
242 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
247 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
249 IStream_Release(stream
);
255 GpStatus WINGDIPAPI
GdipCreateBitmapFromFileICM(GDIPCONST WCHAR
* filename
,
258 return GdipCreateBitmapFromFile(filename
, bitmap
);
261 GpStatus WINGDIPAPI
GdipCreateHBITMAPFromBitmap(GpBitmap
* bitmap
,
262 HBITMAP
* hbmReturn
, ARGB background
)
268 return NotImplemented
;
271 GpStatus WINGDIPAPI
GdipConvertToEmfPlus(const GpGraphics
* ref
,
272 GpMetafile
* metafile
, BOOL
* succ
, EmfType emfType
,
273 const WCHAR
* description
, GpMetafile
** out_metafile
)
277 if(!ref
|| !metafile
|| !out_metafile
)
278 return InvalidParameter
;
281 *out_metafile
= NULL
;
284 FIXME("not implemented\n");
286 return NotImplemented
;
289 /* FIXME: this should create a bitmap in the given size with the attributes
290 * (resolution etc.) of the graphics object */
291 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
292 GpGraphics
* target
, GpBitmap
** bitmap
)
297 if(!target
|| !bitmap
)
298 return InvalidParameter
;
301 FIXME("hacked stub\n");
303 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat24bppRGB
,
309 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
310 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
312 BITMAPFILEHEADER
*bmfh
;
313 BITMAPINFOHEADER
*bmih
;
318 TRACE("%d %d %d %d %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
320 if(!bitmap
|| width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
322 return InvalidParameter
;
326 return InvalidParameter
;
328 /* FIXME: windows allows negative stride (reads backwards from scan0) */
330 FIXME("negative stride\n");
331 return InvalidParameter
;
334 *bitmap
= GdipAlloc(sizeof(GpBitmap
));
335 if(!*bitmap
) return OutOfMemory
;
338 stride
= width
* (PIXELFORMATBPP(format
) / 8);
339 stride
= (stride
+ 3) & ~3;
342 datalen
= abs(stride
* height
);
343 size
= sizeof(BITMAPFILEHEADER
) + sizeof(BITMAPINFOHEADER
) + datalen
;
344 buff
= GdipAlloc(size
);
350 bmfh
= (BITMAPFILEHEADER
*) buff
;
351 bmih
= (BITMAPINFOHEADER
*) (bmfh
+ 1);
353 bmfh
->bfType
= (((WORD
)'M') << 8) + (WORD
)'B';
355 bmfh
->bfOffBits
= size
- datalen
;
357 bmih
->biSize
= sizeof(BITMAPINFOHEADER
);
358 bmih
->biWidth
= width
;
359 bmih
->biHeight
= -height
;
360 /* FIXME: use the rest of the data from format */
361 bmih
->biBitCount
= PIXELFORMATBPP(format
);
362 bmih
->biCompression
= BI_RGB
;
363 bmih
->biSizeImage
= datalen
;
366 memcpy(bmih
+ 1, scan0
, datalen
);
368 memset(bmih
+ 1, 0, datalen
);
370 if(CreateStreamOnHGlobal(buff
, TRUE
, &stream
) != S_OK
){
371 ERR("could not make stream\n");
377 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
378 (LPVOID
*) &((*bitmap
)->image
.picture
)) != S_OK
){
379 TRACE("Could not load picture\n");
380 IStream_Release(stream
);
386 (*bitmap
)->image
.type
= ImageTypeBitmap
;
387 (*bitmap
)->image
.flags
= ImageFlagsNone
;
388 (*bitmap
)->width
= width
;
389 (*bitmap
)->height
= height
;
390 (*bitmap
)->format
= format
;
395 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
400 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
405 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
406 IPicture_Release((*bitmap
)->image
.picture
);
408 return GenericError
; /* FIXME: what error to return? */
415 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
418 return GdipCreateBitmapFromStream(stream
, bitmap
);
421 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
426 return InvalidParameter
;
428 IPicture_get_CurDC(image
->picture
, &hdc
);
430 IPicture_Release(image
->picture
);
431 if (image
->type
== ImageTypeBitmap
)
432 GdipFree(((GpBitmap
*)image
)->bitmapbits
);
438 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
441 return InvalidParameter
;
443 return NotImplemented
;
446 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
449 if(!image
|| !srcRect
|| !srcUnit
)
450 return InvalidParameter
;
451 if(image
->type
== ImageTypeMetafile
){
452 *srcRect
= ((GpMetafile
*)image
)->bounds
;
453 *srcUnit
= ((GpMetafile
*)image
)->unit
;
455 else if(image
->type
== ImageTypeBitmap
){
456 srcRect
->X
= srcRect
->Y
= 0.0;
457 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
458 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
459 *srcUnit
= UnitPixel
;
462 srcRect
->X
= srcRect
->Y
= 0.0;
463 srcRect
->Width
= ipicture_pixel_width(image
->picture
);
464 srcRect
->Height
= ipicture_pixel_height(image
->picture
);
465 *srcUnit
= UnitPixel
;
468 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
469 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
474 GpStatus WINGDIPAPI
GdipGetImageDimension(GpImage
*image
, REAL
*width
,
477 if(!image
|| !height
|| !width
)
478 return InvalidParameter
;
480 if(image
->type
== ImageTypeMetafile
){
483 *height
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
484 ((GpMetafile
*)image
)->bounds
.Height
;
486 *width
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
487 ((GpMetafile
*)image
)->bounds
.Width
;
492 else if(image
->type
== ImageTypeBitmap
){
493 *height
= ((GpBitmap
*)image
)->height
;
494 *width
= ((GpBitmap
*)image
)->width
;
497 *height
= ipicture_pixel_height(image
->picture
);
498 *width
= ipicture_pixel_width(image
->picture
);
501 TRACE("returning (%f, %f)\n", *height
, *width
);
505 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
506 GpGraphics
**graphics
)
510 if(!image
|| !graphics
)
511 return InvalidParameter
;
513 if(image
->type
!= ImageTypeBitmap
){
514 FIXME("not implemented for image type %d\n", image
->type
);
515 return NotImplemented
;
518 IPicture_get_CurDC(image
->picture
, &hdc
);
521 hdc
= CreateCompatibleDC(0);
522 IPicture_SelectPicture(image
->picture
, hdc
, NULL
, NULL
);
525 return GdipCreateFromHDC(hdc
, graphics
);
528 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
530 if(!image
|| !height
)
531 return InvalidParameter
;
533 if(image
->type
== ImageTypeMetafile
){
536 *height
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
537 ((GpMetafile
*)image
)->bounds
.Height
);
541 else if(image
->type
== ImageTypeBitmap
)
542 *height
= ((GpBitmap
*)image
)->height
;
544 *height
= ipicture_pixel_height(image
->picture
);
546 TRACE("returning %d\n", *height
);
551 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
556 return InvalidParameter
;
559 FIXME("not implemented\n");
561 return NotImplemented
;
564 /* FIXME: test this function for non-bitmap types */
565 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
567 if(!image
|| !format
)
568 return InvalidParameter
;
570 if(image
->type
!= ImageTypeBitmap
)
571 *format
= PixelFormat24bppRGB
;
573 *format
= ((GpBitmap
*) image
)->format
;
578 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
582 if(!image
|| !format
)
583 return InvalidParameter
;
586 FIXME("not implemented\n");
588 return NotImplemented
;
591 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
594 return InvalidParameter
;
601 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
606 return InvalidParameter
;
609 FIXME("not implemented\n");
611 return NotImplemented
;
614 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
617 return InvalidParameter
;
619 if(image
->type
== ImageTypeMetafile
){
622 *width
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
623 ((GpMetafile
*)image
)->bounds
.Width
);
627 else if(image
->type
== ImageTypeBitmap
)
628 *width
= ((GpBitmap
*)image
)->width
;
630 *width
= ipicture_pixel_width(image
->picture
);
632 TRACE("returning %d\n", *width
);
637 GpStatus WINGDIPAPI
GdipGetMetafileHeaderFromMetafile(GpMetafile
* metafile
,
638 MetafileHeader
* header
)
642 if(!metafile
|| !header
)
643 return InvalidParameter
;
646 FIXME("not implemented\n");
651 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID pid
,
656 TRACE("%p %x %p\n", image
, pid
, size
);
659 return InvalidParameter
;
662 FIXME("not implemented\n");
664 return NotImplemented
;
667 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
668 GDIPCONST GUID
* dimensionID
, UINT
* count
)
672 if(!image
|| !dimensionID
|| !count
)
673 return InvalidParameter
;
676 FIXME("not implemented\n");
678 return NotImplemented
;
681 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
682 GUID
* dimensionIDs
, UINT count
)
686 if(!image
|| !dimensionIDs
)
687 return InvalidParameter
;
690 FIXME("not implemented\n");
695 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
,
696 GDIPCONST GUID
* dimensionID
, UINT frameidx
)
700 if(!image
|| !dimensionID
)
701 return InvalidParameter
;
704 FIXME("not implemented\n");
709 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
715 if (!filename
|| !image
)
716 return InvalidParameter
;
718 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
723 stat
= GdipLoadImageFromStream(stream
, image
);
725 IStream_Release(stream
);
730 /* FIXME: no icm handling */
731 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
733 return GdipLoadImageFromFile(filename
, image
);
736 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
* stream
, GpImage
**image
)
741 if(!stream
|| !image
)
742 return InvalidParameter
;
744 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
745 (LPVOID
*) &pic
) != S_OK
){
746 TRACE("Could not load picture\n");
750 IPicture_get_Type(pic
, &type
);
752 if(type
== PICTYPE_BITMAP
){
754 BITMAPCOREHEADER
* bmch
;
758 *image
= GdipAlloc(sizeof(GpBitmap
));
759 if(!*image
) return OutOfMemory
;
760 (*image
)->type
= ImageTypeBitmap
;
762 (*((GpBitmap
**) image
))->width
= ipicture_pixel_width(pic
);
763 (*((GpBitmap
**) image
))->height
= ipicture_pixel_height(pic
);
765 /* get the pixel format */
766 IPicture_get_Handle(pic
, &hbm
);
767 IPicture_get_CurDC(pic
, &hdc
);
769 ZeroMemory(&bmi
, sizeof(bmi
));
770 bmch
= (BITMAPCOREHEADER
*) (&bmi
.bmiHeader
);
771 bmch
->bcSize
= sizeof(BITMAPCOREHEADER
);
775 hdc
= CreateCompatibleDC(0);
776 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
777 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
778 SelectObject(hdc
, old
);
782 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
784 (*((GpBitmap
**) image
))->format
= (bmch
->bcBitCount
<< 8) | PixelFormatGDI
;
786 else if(type
== PICTYPE_METAFILE
|| type
== PICTYPE_ENHMETAFILE
){
787 /* FIXME: missing initialization code */
788 *image
= GdipAlloc(sizeof(GpMetafile
));
789 if(!*image
) return OutOfMemory
;
790 (*image
)->type
= ImageTypeMetafile
;
793 *image
= GdipAlloc(sizeof(GpImage
));
794 if(!*image
) return OutOfMemory
;
795 (*image
)->type
= ImageTypeUnknown
;
798 (*image
)->picture
= pic
;
799 (*image
)->flags
= ImageFlagsNone
;
805 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
807 return GdipLoadImageFromStream(stream
, image
);
810 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
815 return InvalidParameter
;
818 FIXME("not implemented\n");
820 return NotImplemented
;
823 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
824 GDIPCONST CLSID
*clsidEncoder
,
825 GDIPCONST EncoderParameters
*encoderParams
)
830 if (!image
|| !filename
|| !clsidEncoder
)
831 return InvalidParameter
;
833 if (!(image
->picture
))
834 return InvalidParameter
;
836 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
840 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
842 IStream_Release(stream
);
846 /*************************************************************************
847 * Encoding functions -
848 * These functions encode an image in different image file formats.
850 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
851 #define BITMAP_FORMAT_JPEG 0xd8ff
852 #define BITMAP_FORMAT_GIF 0x4947
853 #define BITMAP_FORMAT_PNG 0x5089
854 #define BITMAP_FORMAT_APM 0xcdd7
856 static GpStatus
encode_image_BMP(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
857 void **output
, unsigned int *output_size
)
859 int num_palette_entries
;
860 BITMAPFILEHEADER
*bmp_file_hdr
;
861 BITMAPINFO
*bmp_info_hdr
;
863 if (bitmap_info
->bmiHeader
.biClrUsed
) {
864 num_palette_entries
= bitmap_info
->bmiHeader
.biClrUsed
;
865 if (num_palette_entries
> 256) num_palette_entries
= 256;
867 if (bitmap_info
->bmiHeader
.biBitCount
<= 8)
868 num_palette_entries
= 1 << bitmap_info
->bmiHeader
.biBitCount
;
870 num_palette_entries
= 0;
874 sizeof(BITMAPFILEHEADER
) +
875 sizeof(BITMAPINFOHEADER
) +
876 num_palette_entries
* sizeof(RGBQUAD
) +
877 bitmap_info
->bmiHeader
.biSizeImage
;
879 *output
= GdipAlloc(*output_size
);
881 bmp_file_hdr
= (BITMAPFILEHEADER
*) *output
;
882 bmp_file_hdr
->bfType
= BITMAP_FORMAT_BMP
;
883 bmp_file_hdr
->bfSize
= *output_size
;
884 bmp_file_hdr
->bfOffBits
=
885 sizeof(BITMAPFILEHEADER
) +
886 sizeof(BITMAPINFOHEADER
) +
887 num_palette_entries
* sizeof (RGBQUAD
);
889 bmp_info_hdr
= (BITMAPINFO
*) ((unsigned char*)(*output
) + sizeof(BITMAPFILEHEADER
));
890 memcpy(bmp_info_hdr
, bitmap_info
, sizeof(BITMAPINFOHEADER
) + num_palette_entries
* sizeof(RGBQUAD
));
891 memcpy((unsigned char *)(*output
) +
892 sizeof(BITMAPFILEHEADER
) +
893 sizeof(BITMAPINFOHEADER
) +
894 num_palette_entries
* sizeof(RGBQUAD
),
895 bitmap_bits
, bitmap_info
->bmiHeader
.biSizeImage
);
900 typedef GpStatus
encode_image_func(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
901 void **output
, unsigned int *output_size
);
905 NUM_ENCODERS_SUPPORTED
908 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
];
909 static encode_image_func
*const encode_image_funcs
[NUM_ENCODERS_SUPPORTED
] = {
913 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
914 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
925 encode_image_func
* encode_image
;
927 unsigned int output_size
;
935 if(!image
|| !stream
)
936 return InvalidParameter
;
941 hr
= IPicture_get_Type(image
->picture
, &type
);
942 if (FAILED(hr
) || type
!= PICTYPE_BITMAP
)
945 /* select correct encoder */
947 for (i
= 0; i
< NUM_ENCODERS_SUPPORTED
; i
++) {
948 if (IsEqualCLSID(clsid
, &codecs
[i
].Clsid
))
949 encode_image
= encode_image_funcs
[i
];
951 if (encode_image
== NULL
)
952 return UnknownImageFormat
;
954 /* extract underlying hbitmap representation from the IPicture */
955 hr
= IPicture_get_Handle(image
->picture
, (OLE_HANDLE
*)&hbmp
);
956 if (FAILED(hr
) || !hbmp
)
958 hr
= IPicture_get_CurDC(image
->picture
, &hdc
);
961 bm_is_selected
= (hdc
!= 0);
962 if (!bm_is_selected
) {
963 hdc
= CreateCompatibleDC(0);
964 old_hbmp
= SelectObject(hdc
, hbmp
);
967 /* get bits from HBITMAP */
968 bmp_info
.bmiHeader
.biSize
= sizeof(bmp_info
.bmiHeader
);
969 bmp_info
.bmiHeader
.biBitCount
= 0;
970 GetDIBits(hdc
, hbmp
, 0, 0, NULL
, &bmp_info
, DIB_RGB_COLORS
);
972 bmp_bits
= GdipAlloc(bmp_info
.bmiHeader
.biSizeImage
);
975 GetDIBits(hdc
, hbmp
, 0, abs(bmp_info
.bmiHeader
.biHeight
), bmp_bits
, &bmp_info
, DIB_RGB_COLORS
);
977 if (!bm_is_selected
) {
978 SelectObject(hdc
, old_hbmp
);
985 stat
= encode_image(bmp_bits
, &bmp_info
, &output
, &output_size
);
987 IStream_Write(stream
, output
, output_size
, &dummy
);
995 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
996 GDIPCONST ColorPalette
*palette
)
1000 if(!image
|| !palette
)
1001 return InvalidParameter
;
1004 FIXME("not implemented\n");
1006 return NotImplemented
;
1009 /*************************************************************************
1011 * Structures that represent which formats we support for encoding.
1014 /* ImageCodecInfo creation routines taken from libgdiplus */
1015 static const WCHAR bmp_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1016 static const WCHAR bmp_extension
[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1017 static const WCHAR bmp_mimetype
[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1018 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0}; /* BMP */
1019 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
1020 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
1022 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
] =
1025 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1026 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1027 /* CodecName */ bmp_codecname
,
1029 /* FormatDescription */ bmp_format
,
1030 /* FilenameExtension */ bmp_extension
,
1031 /* MimeType */ bmp_mimetype
,
1032 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
1036 /* SigPattern */ bmp_sig_pattern
,
1037 /* SigMask */ bmp_sig_mask
,
1041 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
1043 if (!numEncoders
|| !size
)
1044 return InvalidParameter
;
1046 *numEncoders
= NUM_ENCODERS_SUPPORTED
;
1047 *size
= sizeof (codecs
);
1052 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
1055 (numEncoders
!= NUM_ENCODERS_SUPPORTED
) ||
1056 (size
!= sizeof (codecs
)))
1057 return GenericError
;
1059 memcpy(encoders
, codecs
, sizeof (codecs
));
1063 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
1070 return InvalidParameter
;
1072 /* TODO: Support for device-dependent bitmaps */
1074 FIXME("no support for device-dependent bitmaps\n");
1075 return NotImplemented
;
1078 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
1079 return InvalidParameter
;
1081 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1082 switch(bm
.bmBitsPixel
) {
1084 format
= PixelFormat1bppIndexed
;
1087 format
= PixelFormat4bppIndexed
;
1090 format
= PixelFormat8bppIndexed
;
1093 format
= PixelFormat24bppRGB
;
1096 format
= PixelFormat48bppRGB
;
1099 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
1100 return InvalidParameter
;
1103 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, bm
.bmWidthBytes
,
1104 format
, bm
.bmBits
, bitmap
);
1109 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
1110 const VOID
*params
, const UINT size
)
1115 FIXME("not implemented\n");
1117 return NotImplemented
;
1120 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
1122 if(!image
|| !flags
)
1123 return InvalidParameter
;
1125 *flags
= image
->flags
;