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
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
249 if(!filename
|| !bitmap
)
250 return InvalidParameter
;
252 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
257 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
259 IStream_Release(stream
);
265 GpStatus WINGDIPAPI
GdipCreateBitmapFromFileICM(GDIPCONST WCHAR
* filename
,
268 return GdipCreateBitmapFromFile(filename
, bitmap
);
271 GpStatus WINGDIPAPI
GdipCreateHBITMAPFromBitmap(GpBitmap
* bitmap
,
272 HBITMAP
* hbmReturn
, ARGB background
)
278 return NotImplemented
;
281 GpStatus WINGDIPAPI
GdipConvertToEmfPlus(const GpGraphics
* ref
,
282 GpMetafile
* metafile
, BOOL
* succ
, EmfType emfType
,
283 const WCHAR
* description
, GpMetafile
** out_metafile
)
287 if(!ref
|| !metafile
|| !out_metafile
)
288 return InvalidParameter
;
291 *out_metafile
= NULL
;
294 FIXME("not implemented\n");
296 return NotImplemented
;
299 /* FIXME: this should create a bitmap in the given size with the attributes
300 * (resolution etc.) of the graphics object */
301 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
302 GpGraphics
* target
, GpBitmap
** bitmap
)
307 if(!target
|| !bitmap
)
308 return InvalidParameter
;
311 FIXME("hacked stub\n");
313 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat24bppRGB
,
319 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
320 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
322 BITMAPFILEHEADER
*bmfh
;
323 BITMAPINFOHEADER
*bmih
;
328 TRACE("%d %d %d %d %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
330 if(!bitmap
|| width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
332 return InvalidParameter
;
336 return InvalidParameter
;
338 /* FIXME: windows allows negative stride (reads backwards from scan0) */
340 FIXME("negative stride\n");
341 return InvalidParameter
;
344 *bitmap
= GdipAlloc(sizeof(GpBitmap
));
345 if(!*bitmap
) return OutOfMemory
;
348 stride
= width
* (PIXELFORMATBPP(format
) / 8);
349 stride
= (stride
+ 3) & ~3;
352 datalen
= abs(stride
* height
);
353 size
= sizeof(BITMAPFILEHEADER
) + sizeof(BITMAPINFOHEADER
) + datalen
;
354 buff
= GdipAlloc(size
);
360 bmfh
= (BITMAPFILEHEADER
*) buff
;
361 bmih
= (BITMAPINFOHEADER
*) (bmfh
+ 1);
363 bmfh
->bfType
= (((WORD
)'M') << 8) + (WORD
)'B';
365 bmfh
->bfOffBits
= size
- datalen
;
367 bmih
->biSize
= sizeof(BITMAPINFOHEADER
);
368 bmih
->biWidth
= width
;
369 bmih
->biHeight
= -height
;
370 /* FIXME: use the rest of the data from format */
371 bmih
->biBitCount
= PIXELFORMATBPP(format
);
372 bmih
->biCompression
= BI_RGB
;
373 bmih
->biSizeImage
= datalen
;
376 memcpy(bmih
+ 1, scan0
, datalen
);
378 memset(bmih
+ 1, 0, datalen
);
380 if(CreateStreamOnHGlobal(buff
, TRUE
, &stream
) != S_OK
){
381 ERR("could not make stream\n");
387 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
388 (LPVOID
*) &((*bitmap
)->image
.picture
)) != S_OK
){
389 TRACE("Could not load picture\n");
390 IStream_Release(stream
);
396 (*bitmap
)->image
.type
= ImageTypeBitmap
;
397 (*bitmap
)->image
.flags
= ImageFlagsNone
;
398 (*bitmap
)->width
= width
;
399 (*bitmap
)->height
= height
;
400 (*bitmap
)->format
= format
;
405 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
410 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
415 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
416 IPicture_Release((*bitmap
)->image
.picture
);
418 return GenericError
; /* FIXME: what error to return? */
425 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
428 return GdipCreateBitmapFromStream(stream
, bitmap
);
431 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
436 return InvalidParameter
;
438 IPicture_get_CurDC(image
->picture
, &hdc
);
440 IPicture_Release(image
->picture
);
441 if (image
->type
== ImageTypeBitmap
)
442 GdipFree(((GpBitmap
*)image
)->bitmapbits
);
448 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
451 return InvalidParameter
;
453 return NotImplemented
;
456 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
459 if(!image
|| !srcRect
|| !srcUnit
)
460 return InvalidParameter
;
461 if(image
->type
== ImageTypeMetafile
){
462 *srcRect
= ((GpMetafile
*)image
)->bounds
;
463 *srcUnit
= ((GpMetafile
*)image
)->unit
;
465 else if(image
->type
== ImageTypeBitmap
){
466 srcRect
->X
= srcRect
->Y
= 0.0;
467 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
468 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
469 *srcUnit
= UnitPixel
;
472 srcRect
->X
= srcRect
->Y
= 0.0;
473 srcRect
->Width
= ipicture_pixel_width(image
->picture
);
474 srcRect
->Height
= ipicture_pixel_height(image
->picture
);
475 *srcUnit
= UnitPixel
;
478 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
479 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
484 GpStatus WINGDIPAPI
GdipGetImageDimension(GpImage
*image
, REAL
*width
,
487 if(!image
|| !height
|| !width
)
488 return InvalidParameter
;
490 if(image
->type
== ImageTypeMetafile
){
493 *height
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
494 ((GpMetafile
*)image
)->bounds
.Height
;
496 *width
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
497 ((GpMetafile
*)image
)->bounds
.Width
;
502 else if(image
->type
== ImageTypeBitmap
){
503 *height
= ((GpBitmap
*)image
)->height
;
504 *width
= ((GpBitmap
*)image
)->width
;
507 *height
= ipicture_pixel_height(image
->picture
);
508 *width
= ipicture_pixel_width(image
->picture
);
511 TRACE("returning (%f, %f)\n", *height
, *width
);
515 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
516 GpGraphics
**graphics
)
520 if(!image
|| !graphics
)
521 return InvalidParameter
;
523 if(image
->type
!= ImageTypeBitmap
){
524 FIXME("not implemented for image type %d\n", image
->type
);
525 return NotImplemented
;
528 IPicture_get_CurDC(image
->picture
, &hdc
);
531 hdc
= CreateCompatibleDC(0);
532 IPicture_SelectPicture(image
->picture
, hdc
, NULL
, NULL
);
535 return GdipCreateFromHDC(hdc
, graphics
);
538 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
540 if(!image
|| !height
)
541 return InvalidParameter
;
543 if(image
->type
== ImageTypeMetafile
){
546 *height
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
547 ((GpMetafile
*)image
)->bounds
.Height
);
551 else if(image
->type
== ImageTypeBitmap
)
552 *height
= ((GpBitmap
*)image
)->height
;
554 *height
= ipicture_pixel_height(image
->picture
);
556 TRACE("returning %d\n", *height
);
561 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
566 return InvalidParameter
;
569 FIXME("not implemented\n");
571 return NotImplemented
;
574 /* FIXME: test this function for non-bitmap types */
575 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
577 if(!image
|| !format
)
578 return InvalidParameter
;
580 if(image
->type
!= ImageTypeBitmap
)
581 *format
= PixelFormat24bppRGB
;
583 *format
= ((GpBitmap
*) image
)->format
;
588 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
592 if(!image
|| !format
)
593 return InvalidParameter
;
596 FIXME("not implemented\n");
598 return NotImplemented
;
601 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
604 return InvalidParameter
;
611 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
616 return InvalidParameter
;
619 FIXME("not implemented\n");
621 return NotImplemented
;
624 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
627 return InvalidParameter
;
629 if(image
->type
== ImageTypeMetafile
){
632 *width
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
633 ((GpMetafile
*)image
)->bounds
.Width
);
637 else if(image
->type
== ImageTypeBitmap
)
638 *width
= ((GpBitmap
*)image
)->width
;
640 *width
= ipicture_pixel_width(image
->picture
);
642 TRACE("returning %d\n", *width
);
647 GpStatus WINGDIPAPI
GdipGetMetafileHeaderFromMetafile(GpMetafile
* metafile
,
648 MetafileHeader
* header
)
652 if(!metafile
|| !header
)
653 return InvalidParameter
;
656 FIXME("not implemented\n");
661 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID pid
,
666 TRACE("%p %x %p\n", image
, pid
, size
);
669 return InvalidParameter
;
672 FIXME("not implemented\n");
674 return NotImplemented
;
677 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
678 GDIPCONST GUID
* dimensionID
, UINT
* count
)
682 if(!image
|| !dimensionID
|| !count
)
683 return InvalidParameter
;
686 FIXME("not implemented\n");
688 return NotImplemented
;
691 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
692 GUID
* dimensionIDs
, UINT count
)
696 if(!image
|| !dimensionIDs
)
697 return InvalidParameter
;
700 FIXME("not implemented\n");
705 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
,
706 GDIPCONST GUID
* dimensionID
, UINT frameidx
)
710 if(!image
|| !dimensionID
)
711 return InvalidParameter
;
714 FIXME("not implemented\n");
719 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
725 if (!filename
|| !image
)
726 return InvalidParameter
;
728 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
733 stat
= GdipLoadImageFromStream(stream
, image
);
735 IStream_Release(stream
);
740 /* FIXME: no icm handling */
741 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
743 return GdipLoadImageFromFile(filename
, image
);
746 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
* stream
, GpImage
**image
)
751 if(!stream
|| !image
)
752 return InvalidParameter
;
754 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
755 (LPVOID
*) &pic
) != S_OK
){
756 TRACE("Could not load picture\n");
760 IPicture_get_Type(pic
, &type
);
762 if(type
== PICTYPE_BITMAP
){
764 BITMAPCOREHEADER
* bmch
;
768 *image
= GdipAlloc(sizeof(GpBitmap
));
769 if(!*image
) return OutOfMemory
;
770 (*image
)->type
= ImageTypeBitmap
;
772 (*((GpBitmap
**) image
))->width
= ipicture_pixel_width(pic
);
773 (*((GpBitmap
**) image
))->height
= ipicture_pixel_height(pic
);
775 /* get the pixel format */
776 IPicture_get_Handle(pic
, &hbm
);
777 IPicture_get_CurDC(pic
, &hdc
);
779 ZeroMemory(&bmi
, sizeof(bmi
));
780 bmch
= (BITMAPCOREHEADER
*) (&bmi
.bmiHeader
);
781 bmch
->bcSize
= sizeof(BITMAPCOREHEADER
);
785 hdc
= CreateCompatibleDC(0);
786 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
787 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
788 SelectObject(hdc
, old
);
792 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
794 (*((GpBitmap
**) image
))->format
= (bmch
->bcBitCount
<< 8) | PixelFormatGDI
;
796 else if(type
== PICTYPE_METAFILE
|| type
== PICTYPE_ENHMETAFILE
){
797 /* FIXME: missing initialization code */
798 *image
= GdipAlloc(sizeof(GpMetafile
));
799 if(!*image
) return OutOfMemory
;
800 (*image
)->type
= ImageTypeMetafile
;
803 *image
= GdipAlloc(sizeof(GpImage
));
804 if(!*image
) return OutOfMemory
;
805 (*image
)->type
= ImageTypeUnknown
;
808 (*image
)->picture
= pic
;
809 (*image
)->flags
= ImageFlagsNone
;
815 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
817 return GdipLoadImageFromStream(stream
, image
);
820 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
825 return InvalidParameter
;
828 FIXME("not implemented\n");
830 return NotImplemented
;
833 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
834 GDIPCONST CLSID
*clsidEncoder
,
835 GDIPCONST EncoderParameters
*encoderParams
)
840 if (!image
|| !filename
|| !clsidEncoder
)
841 return InvalidParameter
;
843 if (!(image
->picture
))
844 return InvalidParameter
;
846 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
850 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
852 IStream_Release(stream
);
856 /*************************************************************************
857 * Encoding functions -
858 * These functions encode an image in different image file formats.
860 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
861 #define BITMAP_FORMAT_JPEG 0xd8ff
862 #define BITMAP_FORMAT_GIF 0x4947
863 #define BITMAP_FORMAT_PNG 0x5089
864 #define BITMAP_FORMAT_APM 0xcdd7
866 static GpStatus
encode_image_BMP(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
867 void **output
, unsigned int *output_size
)
869 int num_palette_entries
;
870 BITMAPFILEHEADER
*bmp_file_hdr
;
871 BITMAPINFO
*bmp_info_hdr
;
873 if (bitmap_info
->bmiHeader
.biClrUsed
) {
874 num_palette_entries
= bitmap_info
->bmiHeader
.biClrUsed
;
875 if (num_palette_entries
> 256) num_palette_entries
= 256;
877 if (bitmap_info
->bmiHeader
.biBitCount
<= 8)
878 num_palette_entries
= 1 << bitmap_info
->bmiHeader
.biBitCount
;
880 num_palette_entries
= 0;
884 sizeof(BITMAPFILEHEADER
) +
885 sizeof(BITMAPINFOHEADER
) +
886 num_palette_entries
* sizeof(RGBQUAD
) +
887 bitmap_info
->bmiHeader
.biSizeImage
;
889 *output
= GdipAlloc(*output_size
);
891 bmp_file_hdr
= (BITMAPFILEHEADER
*) *output
;
892 bmp_file_hdr
->bfType
= BITMAP_FORMAT_BMP
;
893 bmp_file_hdr
->bfSize
= *output_size
;
894 bmp_file_hdr
->bfOffBits
=
895 sizeof(BITMAPFILEHEADER
) +
896 sizeof(BITMAPINFOHEADER
) +
897 num_palette_entries
* sizeof (RGBQUAD
);
899 bmp_info_hdr
= (BITMAPINFO
*) ((unsigned char*)(*output
) + sizeof(BITMAPFILEHEADER
));
900 memcpy(bmp_info_hdr
, bitmap_info
, sizeof(BITMAPINFOHEADER
) + num_palette_entries
* sizeof(RGBQUAD
));
901 memcpy((unsigned char *)(*output
) +
902 sizeof(BITMAPFILEHEADER
) +
903 sizeof(BITMAPINFOHEADER
) +
904 num_palette_entries
* sizeof(RGBQUAD
),
905 bitmap_bits
, bitmap_info
->bmiHeader
.biSizeImage
);
910 typedef GpStatus
encode_image_func(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
911 void **output
, unsigned int *output_size
);
915 NUM_ENCODERS_SUPPORTED
918 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
];
919 static encode_image_func
*const encode_image_funcs
[NUM_ENCODERS_SUPPORTED
] = {
923 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
924 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
935 encode_image_func
* encode_image
;
937 unsigned int output_size
;
945 if(!image
|| !stream
)
946 return InvalidParameter
;
951 hr
= IPicture_get_Type(image
->picture
, &type
);
952 if (FAILED(hr
) || type
!= PICTYPE_BITMAP
)
955 /* select correct encoder */
957 for (i
= 0; i
< NUM_ENCODERS_SUPPORTED
; i
++) {
958 if (IsEqualCLSID(clsid
, &codecs
[i
].Clsid
))
959 encode_image
= encode_image_funcs
[i
];
961 if (encode_image
== NULL
)
962 return UnknownImageFormat
;
964 /* extract underlying hbitmap representation from the IPicture */
965 hr
= IPicture_get_Handle(image
->picture
, (OLE_HANDLE
*)&hbmp
);
966 if (FAILED(hr
) || !hbmp
)
968 hr
= IPicture_get_CurDC(image
->picture
, &hdc
);
971 bm_is_selected
= (hdc
!= 0);
972 if (!bm_is_selected
) {
973 hdc
= CreateCompatibleDC(0);
974 old_hbmp
= SelectObject(hdc
, hbmp
);
977 /* get bits from HBITMAP */
978 bmp_info
.bmiHeader
.biSize
= sizeof(bmp_info
.bmiHeader
);
979 bmp_info
.bmiHeader
.biBitCount
= 0;
980 GetDIBits(hdc
, hbmp
, 0, 0, NULL
, &bmp_info
, DIB_RGB_COLORS
);
982 bmp_bits
= GdipAlloc(bmp_info
.bmiHeader
.biSizeImage
);
985 GetDIBits(hdc
, hbmp
, 0, abs(bmp_info
.bmiHeader
.biHeight
), bmp_bits
, &bmp_info
, DIB_RGB_COLORS
);
987 if (!bm_is_selected
) {
988 SelectObject(hdc
, old_hbmp
);
995 stat
= encode_image(bmp_bits
, &bmp_info
, &output
, &output_size
);
997 IStream_Write(stream
, output
, output_size
, &dummy
);
1005 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
1006 GDIPCONST ColorPalette
*palette
)
1010 if(!image
|| !palette
)
1011 return InvalidParameter
;
1014 FIXME("not implemented\n");
1016 return NotImplemented
;
1019 /*************************************************************************
1021 * Structures that represent which formats we support for encoding.
1024 /* ImageCodecInfo creation routines taken from libgdiplus */
1025 static const WCHAR bmp_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1026 static const WCHAR bmp_extension
[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1027 static const WCHAR bmp_mimetype
[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1028 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0}; /* BMP */
1029 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
1030 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
1032 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
] =
1035 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1036 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1037 /* CodecName */ bmp_codecname
,
1039 /* FormatDescription */ bmp_format
,
1040 /* FilenameExtension */ bmp_extension
,
1041 /* MimeType */ bmp_mimetype
,
1042 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
1046 /* SigPattern */ bmp_sig_pattern
,
1047 /* SigMask */ bmp_sig_mask
,
1051 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
1053 if (!numEncoders
|| !size
)
1054 return InvalidParameter
;
1056 *numEncoders
= NUM_ENCODERS_SUPPORTED
;
1057 *size
= sizeof (codecs
);
1062 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
1065 (numEncoders
!= NUM_ENCODERS_SUPPORTED
) ||
1066 (size
!= sizeof (codecs
)))
1067 return GenericError
;
1069 memcpy(encoders
, codecs
, sizeof (codecs
));
1073 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
1080 return InvalidParameter
;
1082 /* TODO: Support for device-dependent bitmaps */
1084 FIXME("no support for device-dependent bitmaps\n");
1085 return NotImplemented
;
1088 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
1089 return InvalidParameter
;
1091 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1092 switch(bm
.bmBitsPixel
) {
1094 format
= PixelFormat1bppIndexed
;
1097 format
= PixelFormat4bppIndexed
;
1100 format
= PixelFormat8bppIndexed
;
1103 format
= PixelFormat24bppRGB
;
1106 format
= PixelFormat48bppRGB
;
1109 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
1110 return InvalidParameter
;
1113 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, bm
.bmWidthBytes
,
1114 format
, bm
.bmBits
, bitmap
);
1119 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
1120 const VOID
*params
, const UINT size
)
1125 FIXME("not implemented\n");
1127 return NotImplemented
;
1130 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
1132 if(!image
|| !flags
)
1133 return InvalidParameter
;
1135 *flags
= image
->flags
;