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
GdipImageGetFrameDimensionsCount(GpImage
*image
,
695 return InvalidParameter
;
704 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
705 GUID
* dimensionIDs
, UINT count
)
709 if(!image
|| !dimensionIDs
)
710 return InvalidParameter
;
713 FIXME("not implemented\n");
718 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
,
719 GDIPCONST GUID
* dimensionID
, UINT frameidx
)
723 if(!image
|| !dimensionID
)
724 return InvalidParameter
;
727 FIXME("not implemented\n");
732 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
738 if (!filename
|| !image
)
739 return InvalidParameter
;
741 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
746 stat
= GdipLoadImageFromStream(stream
, image
);
748 IStream_Release(stream
);
753 /* FIXME: no icm handling */
754 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
756 return GdipLoadImageFromFile(filename
, image
);
759 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
* stream
, GpImage
**image
)
764 if(!stream
|| !image
)
765 return InvalidParameter
;
767 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
768 (LPVOID
*) &pic
) != S_OK
){
769 TRACE("Could not load picture\n");
773 IPicture_get_Type(pic
, &type
);
775 if(type
== PICTYPE_BITMAP
){
777 BITMAPCOREHEADER
* bmch
;
781 *image
= GdipAlloc(sizeof(GpBitmap
));
782 if(!*image
) return OutOfMemory
;
783 (*image
)->type
= ImageTypeBitmap
;
785 (*((GpBitmap
**) image
))->width
= ipicture_pixel_width(pic
);
786 (*((GpBitmap
**) image
))->height
= ipicture_pixel_height(pic
);
788 /* get the pixel format */
789 IPicture_get_Handle(pic
, &hbm
);
790 IPicture_get_CurDC(pic
, &hdc
);
792 ZeroMemory(&bmi
, sizeof(bmi
));
793 bmch
= (BITMAPCOREHEADER
*) (&bmi
.bmiHeader
);
794 bmch
->bcSize
= sizeof(BITMAPCOREHEADER
);
798 hdc
= CreateCompatibleDC(0);
799 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
800 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
801 SelectObject(hdc
, old
);
805 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
807 (*((GpBitmap
**) image
))->format
= (bmch
->bcBitCount
<< 8) | PixelFormatGDI
;
809 else if(type
== PICTYPE_METAFILE
|| type
== PICTYPE_ENHMETAFILE
){
810 /* FIXME: missing initialization code */
811 *image
= GdipAlloc(sizeof(GpMetafile
));
812 if(!*image
) return OutOfMemory
;
813 (*image
)->type
= ImageTypeMetafile
;
816 *image
= GdipAlloc(sizeof(GpImage
));
817 if(!*image
) return OutOfMemory
;
818 (*image
)->type
= ImageTypeUnknown
;
821 (*image
)->picture
= pic
;
822 (*image
)->flags
= ImageFlagsNone
;
828 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
830 return GdipLoadImageFromStream(stream
, image
);
833 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
838 return InvalidParameter
;
841 FIXME("not implemented\n");
843 return NotImplemented
;
846 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
847 GDIPCONST CLSID
*clsidEncoder
,
848 GDIPCONST EncoderParameters
*encoderParams
)
853 if (!image
|| !filename
|| !clsidEncoder
)
854 return InvalidParameter
;
856 if (!(image
->picture
))
857 return InvalidParameter
;
859 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
863 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
865 IStream_Release(stream
);
869 /*************************************************************************
870 * Encoding functions -
871 * These functions encode an image in different image file formats.
873 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
874 #define BITMAP_FORMAT_JPEG 0xd8ff
875 #define BITMAP_FORMAT_GIF 0x4947
876 #define BITMAP_FORMAT_PNG 0x5089
877 #define BITMAP_FORMAT_APM 0xcdd7
879 static GpStatus
encode_image_BMP(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
880 void **output
, unsigned int *output_size
)
882 int num_palette_entries
;
883 BITMAPFILEHEADER
*bmp_file_hdr
;
884 BITMAPINFO
*bmp_info_hdr
;
886 if (bitmap_info
->bmiHeader
.biClrUsed
) {
887 num_palette_entries
= bitmap_info
->bmiHeader
.biClrUsed
;
888 if (num_palette_entries
> 256) num_palette_entries
= 256;
890 if (bitmap_info
->bmiHeader
.biBitCount
<= 8)
891 num_palette_entries
= 1 << bitmap_info
->bmiHeader
.biBitCount
;
893 num_palette_entries
= 0;
897 sizeof(BITMAPFILEHEADER
) +
898 sizeof(BITMAPINFOHEADER
) +
899 num_palette_entries
* sizeof(RGBQUAD
) +
900 bitmap_info
->bmiHeader
.biSizeImage
;
902 *output
= GdipAlloc(*output_size
);
904 bmp_file_hdr
= (BITMAPFILEHEADER
*) *output
;
905 bmp_file_hdr
->bfType
= BITMAP_FORMAT_BMP
;
906 bmp_file_hdr
->bfSize
= *output_size
;
907 bmp_file_hdr
->bfOffBits
=
908 sizeof(BITMAPFILEHEADER
) +
909 sizeof(BITMAPINFOHEADER
) +
910 num_palette_entries
* sizeof (RGBQUAD
);
912 bmp_info_hdr
= (BITMAPINFO
*) ((unsigned char*)(*output
) + sizeof(BITMAPFILEHEADER
));
913 memcpy(bmp_info_hdr
, bitmap_info
, sizeof(BITMAPINFOHEADER
) + num_palette_entries
* sizeof(RGBQUAD
));
914 memcpy((unsigned char *)(*output
) +
915 sizeof(BITMAPFILEHEADER
) +
916 sizeof(BITMAPINFOHEADER
) +
917 num_palette_entries
* sizeof(RGBQUAD
),
918 bitmap_bits
, bitmap_info
->bmiHeader
.biSizeImage
);
923 typedef GpStatus
encode_image_func(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
924 void **output
, unsigned int *output_size
);
928 NUM_ENCODERS_SUPPORTED
931 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
];
932 static encode_image_func
*const encode_image_funcs
[NUM_ENCODERS_SUPPORTED
] = {
936 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
937 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
948 encode_image_func
* encode_image
;
950 unsigned int output_size
;
958 if(!image
|| !stream
)
959 return InvalidParameter
;
964 hr
= IPicture_get_Type(image
->picture
, &type
);
965 if (FAILED(hr
) || type
!= PICTYPE_BITMAP
)
968 /* select correct encoder */
970 for (i
= 0; i
< NUM_ENCODERS_SUPPORTED
; i
++) {
971 if (IsEqualCLSID(clsid
, &codecs
[i
].Clsid
))
972 encode_image
= encode_image_funcs
[i
];
974 if (encode_image
== NULL
)
975 return UnknownImageFormat
;
977 /* extract underlying hbitmap representation from the IPicture */
978 hr
= IPicture_get_Handle(image
->picture
, (OLE_HANDLE
*)&hbmp
);
979 if (FAILED(hr
) || !hbmp
)
981 hr
= IPicture_get_CurDC(image
->picture
, &hdc
);
984 bm_is_selected
= (hdc
!= 0);
985 if (!bm_is_selected
) {
986 hdc
= CreateCompatibleDC(0);
987 old_hbmp
= SelectObject(hdc
, hbmp
);
990 /* get bits from HBITMAP */
991 bmp_info
.bmiHeader
.biSize
= sizeof(bmp_info
.bmiHeader
);
992 bmp_info
.bmiHeader
.biBitCount
= 0;
993 GetDIBits(hdc
, hbmp
, 0, 0, NULL
, &bmp_info
, DIB_RGB_COLORS
);
995 bmp_bits
= GdipAlloc(bmp_info
.bmiHeader
.biSizeImage
);
998 GetDIBits(hdc
, hbmp
, 0, abs(bmp_info
.bmiHeader
.biHeight
), bmp_bits
, &bmp_info
, DIB_RGB_COLORS
);
1000 if (!bm_is_selected
) {
1001 SelectObject(hdc
, old_hbmp
);
1008 stat
= encode_image(bmp_bits
, &bmp_info
, &output
, &output_size
);
1010 IStream_Write(stream
, output
, output_size
, &dummy
);
1018 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
1019 GDIPCONST ColorPalette
*palette
)
1023 if(!image
|| !palette
)
1024 return InvalidParameter
;
1027 FIXME("not implemented\n");
1029 return NotImplemented
;
1032 /*************************************************************************
1034 * Structures that represent which formats we support for encoding.
1037 /* ImageCodecInfo creation routines taken from libgdiplus */
1038 static const WCHAR bmp_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1039 static const WCHAR bmp_extension
[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1040 static const WCHAR bmp_mimetype
[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1041 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0}; /* BMP */
1042 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
1043 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
1045 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
] =
1048 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1049 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1050 /* CodecName */ bmp_codecname
,
1052 /* FormatDescription */ bmp_format
,
1053 /* FilenameExtension */ bmp_extension
,
1054 /* MimeType */ bmp_mimetype
,
1055 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
1059 /* SigPattern */ bmp_sig_pattern
,
1060 /* SigMask */ bmp_sig_mask
,
1064 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
1066 if (!numEncoders
|| !size
)
1067 return InvalidParameter
;
1069 *numEncoders
= NUM_ENCODERS_SUPPORTED
;
1070 *size
= sizeof (codecs
);
1075 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
1078 (numEncoders
!= NUM_ENCODERS_SUPPORTED
) ||
1079 (size
!= sizeof (codecs
)))
1080 return GenericError
;
1082 memcpy(encoders
, codecs
, sizeof (codecs
));
1086 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
1093 return InvalidParameter
;
1095 /* TODO: Support for device-dependent bitmaps */
1097 FIXME("no support for device-dependent bitmaps\n");
1098 return NotImplemented
;
1101 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
1102 return InvalidParameter
;
1104 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1105 switch(bm
.bmBitsPixel
) {
1107 format
= PixelFormat1bppIndexed
;
1110 format
= PixelFormat4bppIndexed
;
1113 format
= PixelFormat8bppIndexed
;
1116 format
= PixelFormat24bppRGB
;
1119 format
= PixelFormat48bppRGB
;
1122 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
1123 return InvalidParameter
;
1126 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, bm
.bmWidthBytes
,
1127 format
, bm
.bmBits
, bitmap
);
1132 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
1133 const VOID
*params
, const UINT size
)
1138 FIXME("not implemented\n");
1140 return NotImplemented
;
1143 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
1145 if(!image
|| !flags
)
1146 return InvalidParameter
;
1148 *flags
= image
->flags
;