2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define NONAMELESSUNION
35 #include "gdiplus_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus
);
40 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
42 static INT
ipicture_pixel_height(IPicture
*pic
)
47 IPicture_get_Height(pic
, &y
);
51 y
= MulDiv(y
, GetDeviceCaps(hdcref
, LOGPIXELSY
), INCH_HIMETRIC
);
57 static INT
ipicture_pixel_width(IPicture
*pic
)
62 IPicture_get_Width(pic
, &x
);
66 x
= MulDiv(x
, GetDeviceCaps(hdcref
, LOGPIXELSX
), INCH_HIMETRIC
);
73 GpStatus WINGDIPAPI
GdipBitmapGetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
77 TRACE("%p %d %d %p\n", bitmap
, x
, y
, color
);
80 return InvalidParameter
;
83 FIXME("not implemented\n");
87 return NotImplemented
;
90 GpStatus WINGDIPAPI
GdipBitmapSetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
94 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap
, x
, y
, color
);
97 return InvalidParameter
;
100 FIXME("not implemented\n");
102 return NotImplemented
;
105 /* This function returns a pointer to an array of pixels that represents the
106 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
107 * flags. It is correct behavior that a user who calls this function with write
108 * privileges can write to the whole bitmap (not just the area in rect).
110 * FIXME: only used portion of format is bits per pixel. */
111 GpStatus WINGDIPAPI
GdipBitmapLockBits(GpBitmap
* bitmap
, GDIPCONST GpRect
* rect
,
112 UINT flags
, PixelFormat format
, BitmapData
* lockeddata
)
115 INT stride
, bitspp
= PIXELFORMATBPP(format
);
117 HBITMAP hbm
, old
= NULL
;
121 GpRect act_rect
; /* actual rect to be used */
123 TRACE("%p %p %d %d %p\n", bitmap
, rect
, flags
, format
, lockeddata
);
125 if(!lockeddata
|| !bitmap
)
126 return InvalidParameter
;
129 if(rect
->X
< 0 || rect
->Y
< 0 || (rect
->X
+ rect
->Width
> bitmap
->width
) ||
130 (rect
->Y
+ rect
->Height
> bitmap
->height
) || !flags
)
131 return InvalidParameter
;
136 act_rect
.X
= act_rect
.Y
= 0;
137 act_rect
.Width
= bitmap
->width
;
138 act_rect
.Height
= bitmap
->height
;
141 if(flags
& ImageLockModeUserInputBuf
)
142 return NotImplemented
;
147 IPicture_get_Handle(bitmap
->image
.picture
, (OLE_HANDLE
*)&hbm
);
148 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
149 bm_is_selected
= (hdc
!= 0);
151 pbmi
= GdipAlloc(sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
154 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
155 pbmi
->bmiHeader
.biBitCount
= 0;
158 hdc
= CreateCompatibleDC(0);
159 old
= SelectObject(hdc
, hbm
);
163 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
165 abs_height
= abs(pbmi
->bmiHeader
.biHeight
);
166 stride
= pbmi
->bmiHeader
.biWidth
* bitspp
/ 8;
167 stride
= (stride
+ 3) & ~3;
169 buff
= GdipAlloc(stride
* abs_height
);
171 pbmi
->bmiHeader
.biBitCount
= bitspp
;
174 GetDIBits(hdc
, hbm
, 0, abs_height
, buff
, pbmi
, DIB_RGB_COLORS
);
177 SelectObject(hdc
, old
);
186 lockeddata
->Width
= act_rect
.Width
;
187 lockeddata
->Height
= act_rect
.Height
;
188 lockeddata
->PixelFormat
= format
;
189 lockeddata
->Reserved
= flags
;
191 if(pbmi
->bmiHeader
.biHeight
> 0){
192 lockeddata
->Stride
= -stride
;
193 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * act_rect
.X
+
194 stride
* (abs_height
- 1 - act_rect
.Y
);
197 lockeddata
->Stride
= stride
;
198 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * act_rect
.X
+ stride
* act_rect
.Y
;
201 bitmap
->lockmode
= flags
;
204 bitmap
->bitmapbits
= buff
;
210 GpStatus WINGDIPAPI
GdipBitmapSetResolution(GpBitmap
* bitmap
, REAL xdpi
, REAL ydpi
)
212 FIXME("(%p, %.2f, %.2f)\n", bitmap
, xdpi
, ydpi
);
214 return NotImplemented
;
217 GpStatus WINGDIPAPI
GdipBitmapUnlockBits(GpBitmap
* bitmap
,
218 BitmapData
* lockeddata
)
221 HBITMAP hbm
, old
= NULL
;
225 if(!bitmap
|| !lockeddata
)
226 return InvalidParameter
;
228 if(!bitmap
->lockmode
)
231 if(lockeddata
->Reserved
& ImageLockModeUserInputBuf
)
232 return NotImplemented
;
234 if(lockeddata
->Reserved
& ImageLockModeRead
){
235 if(!(--bitmap
->numlocks
))
236 bitmap
->lockmode
= 0;
238 GdipFree(bitmap
->bitmapbits
);
239 bitmap
->bitmapbits
= NULL
;
243 IPicture_get_Handle(bitmap
->image
.picture
, (OLE_HANDLE
*)&hbm
);
244 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
245 bm_is_selected
= (hdc
!= 0);
247 pbmi
= GdipAlloc(sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
248 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
249 pbmi
->bmiHeader
.biBitCount
= 0;
252 hdc
= CreateCompatibleDC(0);
253 old
= SelectObject(hdc
, hbm
);
256 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
257 pbmi
->bmiHeader
.biBitCount
= PIXELFORMATBPP(lockeddata
->PixelFormat
);
258 SetDIBits(hdc
, hbm
, 0, abs(pbmi
->bmiHeader
.biHeight
),
259 bitmap
->bitmapbits
, pbmi
, DIB_RGB_COLORS
);
262 SelectObject(hdc
, old
);
267 GdipFree(bitmap
->bitmapbits
);
268 bitmap
->bitmapbits
= NULL
;
269 bitmap
->lockmode
= 0;
274 GpStatus WINGDIPAPI
GdipCloneBitmapAreaI(INT x
, INT y
, INT width
, INT height
,
275 PixelFormat format
, GpBitmap
* srcBitmap
, GpBitmap
** dstBitmap
)
277 FIXME("(%i,%i,%i,%i,%i,%p,%p)\n", x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
279 return NotImplemented
;
282 GpStatus WINGDIPAPI
GdipCloneImage(GpImage
*image
, GpImage
**cloneImage
)
288 GpStatus stat
= GenericError
;
290 TRACE("%p, %p\n", image
, cloneImage
);
292 if (!image
|| !cloneImage
)
293 return InvalidParameter
;
295 hr
= CreateStreamOnHGlobal(0, TRUE
, &stream
);
299 hr
= IPicture_SaveAsFile(image
->picture
, stream
, FALSE
, &size
);
302 WARN("Failed to save image on stream\n");
306 /* Set seek pointer back to the beginning of the picture */
308 hr
= IStream_Seek(stream
, move
, STREAM_SEEK_SET
, NULL
);
312 stat
= GdipLoadImageFromStream(stream
, cloneImage
);
313 if (stat
!= Ok
) WARN("Failed to load image from stream\n");
316 IStream_Release(stream
);
320 GpStatus WINGDIPAPI
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
326 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
328 if(!filename
|| !bitmap
)
329 return InvalidParameter
;
331 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
336 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
338 IStream_Release(stream
);
343 GpStatus WINGDIPAPI
GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO
* info
,
344 VOID
*bits
, GpBitmap
**bitmap
)
346 DWORD height
, stride
;
349 FIXME("(%p, %p, %p) - partially implemented\n", info
, bits
, bitmap
);
351 height
= abs(info
->bmiHeader
.biHeight
);
352 stride
= ((info
->bmiHeader
.biWidth
* info
->bmiHeader
.biBitCount
+ 31) >> 3) & ~3;
354 if(info
->bmiHeader
.biHeight
> 0) /* bottom-up */
356 bits
= (BYTE
*)bits
+ (height
- 1) * stride
;
360 switch(info
->bmiHeader
.biBitCount
) {
362 format
= PixelFormat1bppIndexed
;
365 format
= PixelFormat4bppIndexed
;
368 format
= PixelFormat8bppIndexed
;
371 format
= PixelFormat24bppRGB
;
374 FIXME("don't know how to handle %d bpp\n", info
->bmiHeader
.biBitCount
);
376 return InvalidParameter
;
379 return GdipCreateBitmapFromScan0(info
->bmiHeader
.biWidth
, height
, stride
, format
,
385 GpStatus WINGDIPAPI
GdipCreateBitmapFromFileICM(GDIPCONST WCHAR
* filename
,
388 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
390 return GdipCreateBitmapFromFile(filename
, bitmap
);
393 GpStatus WINGDIPAPI
GdipCreateBitmapFromResource(HINSTANCE hInstance
,
394 GDIPCONST WCHAR
* lpBitmapName
, GpBitmap
** bitmap
)
397 GpStatus stat
= InvalidParameter
;
399 TRACE("%p (%s) %p\n", hInstance
, debugstr_w(lpBitmapName
), bitmap
);
401 if(!lpBitmapName
|| !bitmap
)
402 return InvalidParameter
;
405 hbm
= LoadImageW(hInstance
, lpBitmapName
, IMAGE_BITMAP
, 0, 0,
406 LR_CREATEDIBSECTION
);
409 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, bitmap
);
416 GpStatus WINGDIPAPI
GdipCreateHBITMAPFromBitmap(GpBitmap
* bitmap
,
417 HBITMAP
* hbmReturn
, ARGB background
)
423 return NotImplemented
;
426 GpStatus WINGDIPAPI
GdipConvertToEmfPlus(const GpGraphics
* ref
,
427 GpMetafile
* metafile
, BOOL
* succ
, EmfType emfType
,
428 const WCHAR
* description
, GpMetafile
** out_metafile
)
432 if(!ref
|| !metafile
|| !out_metafile
)
433 return InvalidParameter
;
436 *out_metafile
= NULL
;
439 FIXME("not implemented\n");
441 return NotImplemented
;
444 /* FIXME: this should create a bitmap in the given size with the attributes
445 * (resolution etc.) of the graphics object */
446 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
447 GpGraphics
* target
, GpBitmap
** bitmap
)
452 if(!target
|| !bitmap
)
453 return InvalidParameter
;
456 FIXME("hacked stub\n");
458 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat24bppRGB
,
464 GpStatus WINGDIPAPI
GdipCreateBitmapFromHICON(HICON hicon
, GpBitmap
** bitmap
)
470 TRACE("%p, %p\n", hicon
, bitmap
);
472 if(!bitmap
|| !GetIconInfo(hicon
, &iinfo
))
473 return InvalidParameter
;
475 *bitmap
= GdipAlloc(sizeof(GpBitmap
));
476 if(!*bitmap
) return OutOfMemory
;
478 icon_copy
= CreateIconIndirect(&iinfo
);
482 return InvalidParameter
;
485 desc
.cbSizeofstruct
= sizeof(PICTDESC
);
486 desc
.picType
= PICTYPE_ICON
;
487 desc
.u
.icon
.hicon
= icon_copy
;
489 if(OleCreatePictureIndirect(&desc
, &IID_IPicture
, TRUE
,
490 (LPVOID
*) &((*bitmap
)->image
.picture
)) != S_OK
){
491 DestroyIcon(icon_copy
);
496 (*bitmap
)->format
= PixelFormat32bppARGB
;
497 (*bitmap
)->image
.type
= ImageTypeBitmap
;
498 (*bitmap
)->image
.flags
= ImageFlagsNone
;
499 (*bitmap
)->width
= ipicture_pixel_width((*bitmap
)->image
.picture
);
500 (*bitmap
)->height
= ipicture_pixel_height((*bitmap
)->image
.picture
);
502 DeleteObject(iinfo
.hbmColor
);
503 DeleteObject(iinfo
.hbmMask
);
508 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
509 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
511 BITMAPFILEHEADER
*bmfh
;
512 BITMAPINFOHEADER
*bmih
;
517 TRACE("%d %d %d %d %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
519 if (!bitmap
) return InvalidParameter
;
521 if(width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
523 return InvalidParameter
;
527 return InvalidParameter
;
529 *bitmap
= GdipAlloc(sizeof(GpBitmap
));
530 if(!*bitmap
) return OutOfMemory
;
533 stride
= width
* (PIXELFORMATBPP(format
) / 8);
534 stride
= (stride
+ 3) & ~3;
537 datalen
= abs(stride
* height
);
538 size
= sizeof(BITMAPFILEHEADER
) + sizeof(BITMAPINFOHEADER
) + datalen
;
539 buff
= GdipAlloc(size
);
545 bmfh
= (BITMAPFILEHEADER
*) buff
;
546 bmih
= (BITMAPINFOHEADER
*) (bmfh
+ 1);
548 bmfh
->bfType
= (((WORD
)'M') << 8) + (WORD
)'B';
550 bmfh
->bfOffBits
= size
- datalen
;
552 bmih
->biSize
= sizeof(BITMAPINFOHEADER
);
553 bmih
->biWidth
= width
;
554 /* FIXME: use the rest of the data from format */
555 bmih
->biBitCount
= PIXELFORMATBPP(format
);
556 bmih
->biCompression
= BI_RGB
;
557 bmih
->biSizeImage
= datalen
;
563 bmih
->biHeight
= -height
;
564 memcpy(bmih
+ 1, scan0
, datalen
);
568 bmih
->biHeight
= height
;
569 memcpy(bmih
+ 1, scan0
+ stride
* (height
- 1), datalen
);
574 bmih
->biHeight
= height
;
575 memset(bmih
+ 1, 0, datalen
);
578 if(CreateStreamOnHGlobal(buff
, TRUE
, &stream
) != S_OK
){
579 ERR("could not make stream\n");
586 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
587 (LPVOID
*) &((*bitmap
)->image
.picture
)) != S_OK
){
588 TRACE("Could not load picture\n");
589 IStream_Release(stream
);
596 (*bitmap
)->image
.type
= ImageTypeBitmap
;
597 (*bitmap
)->image
.flags
= ImageFlagsNone
;
598 (*bitmap
)->width
= width
;
599 (*bitmap
)->height
= height
;
600 (*bitmap
)->format
= format
;
605 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
610 TRACE("%p %p\n", stream
, bitmap
);
612 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
617 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
618 IPicture_Release((*bitmap
)->image
.picture
);
620 return GenericError
; /* FIXME: what error to return? */
627 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
630 TRACE("%p %p\n", stream
, bitmap
);
632 return GdipCreateBitmapFromStream(stream
, bitmap
);
635 GpStatus WINGDIPAPI
GdipCreateCachedBitmap(GpBitmap
*bitmap
, GpGraphics
*graphics
,
636 GpCachedBitmap
**cachedbmp
)
640 TRACE("%p %p %p\n", bitmap
, graphics
, cachedbmp
);
642 if(!bitmap
|| !graphics
|| !cachedbmp
)
643 return InvalidParameter
;
645 *cachedbmp
= GdipAlloc(sizeof(GpCachedBitmap
));
649 stat
= GdipCloneImage(&(bitmap
->image
), &(*cachedbmp
)->image
);
651 GdipFree(*cachedbmp
);
658 GpStatus WINGDIPAPI
GdipDeleteCachedBitmap(GpCachedBitmap
*cachedbmp
)
660 TRACE("%p\n", cachedbmp
);
663 return InvalidParameter
;
665 GdipDisposeImage(cachedbmp
->image
);
671 GpStatus WINGDIPAPI
GdipDrawCachedBitmap(GpGraphics
*graphics
,
672 GpCachedBitmap
*cachedbmp
, INT x
, INT y
)
674 TRACE("%p %p %d %d\n", graphics
, cachedbmp
, x
, y
);
676 if(!graphics
|| !cachedbmp
)
677 return InvalidParameter
;
679 return GdipDrawImage(graphics
, cachedbmp
->image
, (REAL
)x
, (REAL
)y
);
682 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
686 TRACE("%p\n", image
);
689 return InvalidParameter
;
691 IPicture_get_CurDC(image
->picture
, &hdc
);
693 IPicture_Release(image
->picture
);
694 if (image
->type
== ImageTypeBitmap
)
695 GdipFree(((GpBitmap
*)image
)->bitmapbits
);
701 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
704 return InvalidParameter
;
706 return NotImplemented
;
709 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
712 TRACE("%p %p %p\n", image
, srcRect
, srcUnit
);
714 if(!image
|| !srcRect
|| !srcUnit
)
715 return InvalidParameter
;
716 if(image
->type
== ImageTypeMetafile
){
717 *srcRect
= ((GpMetafile
*)image
)->bounds
;
718 *srcUnit
= ((GpMetafile
*)image
)->unit
;
720 else if(image
->type
== ImageTypeBitmap
){
721 srcRect
->X
= srcRect
->Y
= 0.0;
722 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
723 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
724 *srcUnit
= UnitPixel
;
727 srcRect
->X
= srcRect
->Y
= 0.0;
728 srcRect
->Width
= ipicture_pixel_width(image
->picture
);
729 srcRect
->Height
= ipicture_pixel_height(image
->picture
);
730 *srcUnit
= UnitPixel
;
733 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
734 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
739 GpStatus WINGDIPAPI
GdipGetImageDimension(GpImage
*image
, REAL
*width
,
742 TRACE("%p %p %p\n", image
, width
, height
);
744 if(!image
|| !height
|| !width
)
745 return InvalidParameter
;
747 if(image
->type
== ImageTypeMetafile
){
750 *height
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
751 ((GpMetafile
*)image
)->bounds
.Height
;
753 *width
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
754 ((GpMetafile
*)image
)->bounds
.Width
;
759 else if(image
->type
== ImageTypeBitmap
){
760 *height
= ((GpBitmap
*)image
)->height
;
761 *width
= ((GpBitmap
*)image
)->width
;
764 *height
= ipicture_pixel_height(image
->picture
);
765 *width
= ipicture_pixel_width(image
->picture
);
768 TRACE("returning (%f, %f)\n", *height
, *width
);
772 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
773 GpGraphics
**graphics
)
777 TRACE("%p %p\n", image
, graphics
);
779 if(!image
|| !graphics
)
780 return InvalidParameter
;
782 if(image
->type
!= ImageTypeBitmap
){
783 FIXME("not implemented for image type %d\n", image
->type
);
784 return NotImplemented
;
787 IPicture_get_CurDC(image
->picture
, &hdc
);
790 hdc
= CreateCompatibleDC(0);
791 IPicture_SelectPicture(image
->picture
, hdc
, NULL
, NULL
);
794 return GdipCreateFromHDC(hdc
, graphics
);
797 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
799 TRACE("%p %p\n", image
, height
);
801 if(!image
|| !height
)
802 return InvalidParameter
;
804 if(image
->type
== ImageTypeMetafile
){
807 *height
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
808 ((GpMetafile
*)image
)->bounds
.Height
);
812 else if(image
->type
== ImageTypeBitmap
)
813 *height
= ((GpBitmap
*)image
)->height
;
815 *height
= ipicture_pixel_height(image
->picture
);
817 TRACE("returning %d\n", *height
);
822 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
827 return InvalidParameter
;
830 FIXME("not implemented\n");
832 return NotImplemented
;
835 GpStatus WINGDIPAPI
GdipGetImagePaletteSize(GpImage
*image
, INT
*size
)
837 FIXME("%p %p\n", image
, size
);
840 return InvalidParameter
;
842 return NotImplemented
;
845 /* FIXME: test this function for non-bitmap types */
846 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
848 TRACE("%p %p\n", image
, format
);
850 if(!image
|| !format
)
851 return InvalidParameter
;
853 if(image
->type
!= ImageTypeBitmap
)
854 *format
= PixelFormat24bppRGB
;
856 *format
= ((GpBitmap
*) image
)->format
;
861 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
865 if(!image
|| !format
)
866 return InvalidParameter
;
871 /* FIXME: should be detected from embedded picture or stored separately */
874 case ImageTypeBitmap
: *format
= ImageFormatBMP
; break;
875 case ImageTypeMetafile
: *format
= ImageFormatEMF
; break;
877 WARN("unknown type %u\n", image
->type
);
878 *format
= ImageFormatUndefined
;
883 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
885 TRACE("%p %p\n", image
, type
);
888 return InvalidParameter
;
895 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
900 return InvalidParameter
;
903 FIXME("not implemented\n");
905 return NotImplemented
;
908 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
910 TRACE("%p %p\n", image
, width
);
913 return InvalidParameter
;
915 if(image
->type
== ImageTypeMetafile
){
918 *width
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
919 ((GpMetafile
*)image
)->bounds
.Width
);
923 else if(image
->type
== ImageTypeBitmap
)
924 *width
= ((GpBitmap
*)image
)->width
;
926 *width
= ipicture_pixel_width(image
->picture
);
928 TRACE("returning %d\n", *width
);
933 GpStatus WINGDIPAPI
GdipGetMetafileHeaderFromMetafile(GpMetafile
* metafile
,
934 MetafileHeader
* header
)
938 if(!metafile
|| !header
)
939 return InvalidParameter
;
942 FIXME("not implemented\n");
947 GpStatus WINGDIPAPI
GdipGetAllPropertyItems(GpImage
*image
, UINT size
,
948 UINT num
, PropertyItem
* items
)
953 FIXME("not implemented\n");
955 return InvalidParameter
;
958 GpStatus WINGDIPAPI
GdipGetPropertyCount(GpImage
*image
, UINT
* num
)
963 FIXME("not implemented\n");
965 return InvalidParameter
;
968 GpStatus WINGDIPAPI
GdipGetPropertyIdList(GpImage
*image
, UINT num
, PROPID
* list
)
973 FIXME("not implemented\n");
975 return InvalidParameter
;
978 GpStatus WINGDIPAPI
GdipGetPropertyItem(GpImage
*image
, PROPID id
, UINT size
,
979 PropertyItem
* buffer
)
984 FIXME("not implemented\n");
986 return InvalidParameter
;
989 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID pid
,
994 TRACE("%p %x %p\n", image
, pid
, size
);
997 return InvalidParameter
;
1000 FIXME("not implemented\n");
1002 return NotImplemented
;
1005 GpStatus WINGDIPAPI
GdipGetPropertySize(GpImage
*image
, UINT
* size
, UINT
* num
)
1010 FIXME("not implemented\n");
1012 return InvalidParameter
;
1015 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
1016 GDIPCONST GUID
* dimensionID
, UINT
* count
)
1020 if(!image
|| !dimensionID
|| !count
)
1021 return InvalidParameter
;
1024 FIXME("not implemented\n");
1026 return NotImplemented
;
1029 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsCount(GpImage
*image
,
1032 if(!image
|| !count
)
1033 return InvalidParameter
;
1042 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
1043 GUID
* dimensionIDs
, UINT count
)
1047 if(!image
|| !dimensionIDs
)
1048 return InvalidParameter
;
1051 FIXME("not implemented\n");
1056 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
,
1057 GDIPCONST GUID
* dimensionID
, UINT frameidx
)
1061 if(!image
|| !dimensionID
)
1062 return InvalidParameter
;
1065 FIXME("not implemented\n");
1070 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
1076 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
1078 if (!filename
|| !image
)
1079 return InvalidParameter
;
1081 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
1086 stat
= GdipLoadImageFromStream(stream
, image
);
1088 IStream_Release(stream
);
1093 /* FIXME: no icm handling */
1094 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
1096 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
1098 return GdipLoadImageFromFile(filename
, image
);
1101 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
* stream
, GpImage
**image
)
1106 TRACE("%p %p\n", stream
, image
);
1108 if(!stream
|| !image
)
1109 return InvalidParameter
;
1111 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
1112 (LPVOID
*) &pic
) != S_OK
){
1113 TRACE("Could not load picture\n");
1114 return GenericError
;
1117 IPicture_get_Type(pic
, &type
);
1119 if(type
== PICTYPE_BITMAP
){
1121 BITMAPCOREHEADER
* bmch
;
1125 pbmi
= GdipAlloc(sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
1128 *image
= GdipAlloc(sizeof(GpBitmap
));
1133 (*image
)->type
= ImageTypeBitmap
;
1135 (*((GpBitmap
**) image
))->width
= ipicture_pixel_width(pic
);
1136 (*((GpBitmap
**) image
))->height
= ipicture_pixel_height(pic
);
1138 /* get the pixel format */
1139 IPicture_get_Handle(pic
, (OLE_HANDLE
*)&hbm
);
1140 IPicture_get_CurDC(pic
, &hdc
);
1142 bmch
= (BITMAPCOREHEADER
*) (&pbmi
->bmiHeader
);
1143 bmch
->bcSize
= sizeof(BITMAPCOREHEADER
);
1147 hdc
= CreateCompatibleDC(0);
1148 old
= SelectObject(hdc
, hbm
);
1149 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
1150 SelectObject(hdc
, old
);
1154 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
1156 switch(bmch
->bcBitCount
)
1159 (*((GpBitmap
**) image
))->format
= PixelFormat1bppIndexed
;
1162 (*((GpBitmap
**) image
))->format
= PixelFormat4bppIndexed
;
1165 (*((GpBitmap
**) image
))->format
= PixelFormat8bppIndexed
;
1168 (*((GpBitmap
**) image
))->format
= PixelFormat16bppRGB565
;
1171 (*((GpBitmap
**) image
))->format
= PixelFormat24bppRGB
;
1174 (*((GpBitmap
**) image
))->format
= PixelFormat32bppRGB
;
1177 (*((GpBitmap
**) image
))->format
= PixelFormat48bppRGB
;
1180 FIXME("Bit depth %d is not fully supported yet\n", bmch
->bcBitCount
);
1181 (*((GpBitmap
**) image
))->format
= (bmch
->bcBitCount
<< 8) | PixelFormatGDI
;
1187 else if(type
== PICTYPE_METAFILE
|| type
== PICTYPE_ENHMETAFILE
){
1188 /* FIXME: missing initialization code */
1189 *image
= GdipAlloc(sizeof(GpMetafile
));
1190 if(!*image
) return OutOfMemory
;
1191 (*image
)->type
= ImageTypeMetafile
;
1194 *image
= GdipAlloc(sizeof(GpImage
));
1195 if(!*image
) return OutOfMemory
;
1196 (*image
)->type
= ImageTypeUnknown
;
1199 (*image
)->picture
= pic
;
1200 (*image
)->flags
= ImageFlagsNone
;
1206 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
1208 TRACE("%p %p\n", stream
, image
);
1210 return GdipLoadImageFromStream(stream
, image
);
1213 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
1218 return InvalidParameter
;
1221 FIXME("not implemented\n");
1223 return NotImplemented
;
1226 GpStatus WINGDIPAPI
GdipSetPropertyItem(GpImage
*image
, GDIPCONST PropertyItem
* item
)
1231 FIXME("not implemented\n");
1233 return NotImplemented
;
1236 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
1237 GDIPCONST CLSID
*clsidEncoder
,
1238 GDIPCONST EncoderParameters
*encoderParams
)
1243 TRACE("%p (%s) %p %p\n", image
, debugstr_w(filename
), clsidEncoder
, encoderParams
);
1245 if (!image
|| !filename
|| !clsidEncoder
)
1246 return InvalidParameter
;
1248 if (!(image
->picture
))
1249 return InvalidParameter
;
1251 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
1253 return GenericError
;
1255 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
1257 IStream_Release(stream
);
1261 /*************************************************************************
1262 * Encoding functions -
1263 * These functions encode an image in different image file formats.
1265 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1266 #define BITMAP_FORMAT_JPEG 0xd8ff
1267 #define BITMAP_FORMAT_GIF 0x4947
1268 #define BITMAP_FORMAT_PNG 0x5089
1269 #define BITMAP_FORMAT_APM 0xcdd7
1271 static GpStatus
encode_image_BMP(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
1272 void **output
, unsigned int *output_size
)
1274 int num_palette_entries
;
1275 BITMAPFILEHEADER
*bmp_file_hdr
;
1276 BITMAPINFO
*bmp_info_hdr
;
1278 if (bitmap_info
->bmiHeader
.biClrUsed
) {
1279 num_palette_entries
= bitmap_info
->bmiHeader
.biClrUsed
;
1280 if (num_palette_entries
> 256) num_palette_entries
= 256;
1282 if (bitmap_info
->bmiHeader
.biBitCount
<= 8)
1283 num_palette_entries
= 1 << bitmap_info
->bmiHeader
.biBitCount
;
1285 num_palette_entries
= 0;
1289 sizeof(BITMAPFILEHEADER
) +
1290 sizeof(BITMAPINFOHEADER
) +
1291 num_palette_entries
* sizeof(RGBQUAD
) +
1292 bitmap_info
->bmiHeader
.biSizeImage
;
1294 *output
= GdipAlloc(*output_size
);
1296 bmp_file_hdr
= *output
;
1297 bmp_file_hdr
->bfType
= BITMAP_FORMAT_BMP
;
1298 bmp_file_hdr
->bfSize
= *output_size
;
1299 bmp_file_hdr
->bfOffBits
=
1300 sizeof(BITMAPFILEHEADER
) +
1301 sizeof(BITMAPINFOHEADER
) +
1302 num_palette_entries
* sizeof (RGBQUAD
);
1304 bmp_info_hdr
= (BITMAPINFO
*) ((unsigned char*)(*output
) + sizeof(BITMAPFILEHEADER
));
1305 memcpy(bmp_info_hdr
, bitmap_info
, sizeof(BITMAPINFOHEADER
) + num_palette_entries
* sizeof(RGBQUAD
));
1306 memcpy((unsigned char *)(*output
) +
1307 sizeof(BITMAPFILEHEADER
) +
1308 sizeof(BITMAPINFOHEADER
) +
1309 num_palette_entries
* sizeof(RGBQUAD
),
1310 bitmap_bits
, bitmap_info
->bmiHeader
.biSizeImage
);
1315 typedef GpStatus
encode_image_func(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
1316 void **output
, unsigned int *output_size
);
1320 NUM_ENCODERS_SUPPORTED
1323 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
];
1324 static encode_image_func
*const encode_image_funcs
[NUM_ENCODERS_SUPPORTED
] = {
1328 /*****************************************************************************
1329 * GdipSaveImageToStream [GDIPLUS.@]
1331 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
1332 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
1341 BITMAPINFO bmp_info
;
1343 encode_image_func
* encode_image
;
1345 unsigned int output_size
;
1353 TRACE("%p %p %p %p\n", image
, stream
, clsid
, params
);
1355 if(!image
|| !stream
)
1356 return InvalidParameter
;
1358 if (!image
->picture
)
1359 return GenericError
;
1361 hr
= IPicture_get_Type(image
->picture
, &type
);
1362 if (FAILED(hr
) || type
!= PICTYPE_BITMAP
)
1363 return GenericError
;
1365 /* select correct encoder */
1366 encode_image
= NULL
;
1367 for (i
= 0; i
< NUM_ENCODERS_SUPPORTED
; i
++) {
1368 if (IsEqualCLSID(clsid
, &codecs
[i
].Clsid
))
1369 encode_image
= encode_image_funcs
[i
];
1371 if (encode_image
== NULL
)
1372 return UnknownImageFormat
;
1374 /* extract underlying hbitmap representation from the IPicture */
1375 hr
= IPicture_get_Handle(image
->picture
, (OLE_HANDLE
*)&hbmp
);
1376 if (FAILED(hr
) || !hbmp
)
1377 return GenericError
;
1378 hr
= IPicture_get_CurDC(image
->picture
, &hdc
);
1380 return GenericError
;
1381 bm_is_selected
= (hdc
!= 0);
1382 if (!bm_is_selected
) {
1383 hdc
= CreateCompatibleDC(0);
1384 old_hbmp
= SelectObject(hdc
, hbmp
);
1387 /* get bits from HBITMAP */
1388 bmp_info
.bmiHeader
.biSize
= sizeof(bmp_info
.bmiHeader
);
1389 bmp_info
.bmiHeader
.biBitCount
= 0;
1390 GetDIBits(hdc
, hbmp
, 0, 0, NULL
, &bmp_info
, DIB_RGB_COLORS
);
1392 bmp_bits
= GdipAlloc(bmp_info
.bmiHeader
.biSizeImage
);
1395 GetDIBits(hdc
, hbmp
, 0, abs(bmp_info
.bmiHeader
.biHeight
), bmp_bits
, &bmp_info
, DIB_RGB_COLORS
);
1397 if (!bm_is_selected
) {
1398 SelectObject(hdc
, old_hbmp
);
1405 stat
= encode_image(bmp_bits
, &bmp_info
, &output
, &output_size
);
1407 IStream_Write(stream
, output
, output_size
, &dummy
);
1415 /*****************************************************************************
1416 * GdipSetImagePalette [GDIPLUS.@]
1418 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
1419 GDIPCONST ColorPalette
*palette
)
1423 if(!image
|| !palette
)
1424 return InvalidParameter
;
1427 FIXME("not implemented\n");
1429 return NotImplemented
;
1432 /*************************************************************************
1434 * Structures that represent which formats we support for encoding.
1437 /* ImageCodecInfo creation routines taken from libgdiplus */
1438 static const WCHAR bmp_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1439 static const WCHAR bmp_extension
[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1440 static const WCHAR bmp_mimetype
[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1441 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0}; /* BMP */
1442 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
1443 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
1445 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
] =
1448 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1449 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1450 /* CodecName */ bmp_codecname
,
1452 /* FormatDescription */ bmp_format
,
1453 /* FilenameExtension */ bmp_extension
,
1454 /* MimeType */ bmp_mimetype
,
1455 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
1459 /* SigPattern */ bmp_sig_pattern
,
1460 /* SigMask */ bmp_sig_mask
,
1464 /*****************************************************************************
1465 * GdipGetImageDecodersSize [GDIPLUS.@]
1467 GpStatus WINGDIPAPI
GdipGetImageDecodersSize(UINT
*numDecoders
, UINT
*size
)
1469 FIXME("%p %p stub!\n", numDecoders
, size
);
1471 if (!numDecoders
|| !size
)
1472 return InvalidParameter
;
1480 /*****************************************************************************
1481 * GdipGetImageDecoders [GDIPLUS.@]
1483 GpStatus WINGDIPAPI
GdipGetImageDecoders(UINT numDecoders
, UINT size
, ImageCodecInfo
*decoders
)
1485 FIXME("%u %u %p stub!\n", numDecoders
, size
, decoders
);
1488 return GenericError
;
1490 return NotImplemented
;
1493 /*****************************************************************************
1494 * GdipGetImageEncodersSize [GDIPLUS.@]
1496 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
1498 TRACE("%p %p\n", numEncoders
, size
);
1500 if (!numEncoders
|| !size
)
1501 return InvalidParameter
;
1503 *numEncoders
= NUM_ENCODERS_SUPPORTED
;
1504 *size
= sizeof (codecs
);
1509 /*****************************************************************************
1510 * GdipGetImageEncoders [GDIPLUS.@]
1512 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
1514 TRACE("%u %u %p\n", numEncoders
, size
, encoders
);
1517 (numEncoders
!= NUM_ENCODERS_SUPPORTED
) ||
1518 (size
!= sizeof (codecs
)))
1519 return GenericError
;
1521 memcpy(encoders
, codecs
, sizeof (codecs
));
1526 /*****************************************************************************
1527 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1529 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
1536 TRACE("%p %p %p\n", hbm
, hpal
, bitmap
);
1539 return InvalidParameter
;
1541 /* TODO: Support for device-dependent bitmaps */
1543 FIXME("no support for device-dependent bitmaps\n");
1544 return NotImplemented
;
1547 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
1548 return InvalidParameter
;
1550 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1551 switch(bm
.bmBitsPixel
) {
1553 format
= PixelFormat1bppIndexed
;
1556 format
= PixelFormat4bppIndexed
;
1559 format
= PixelFormat8bppIndexed
;
1562 format
= PixelFormat24bppRGB
;
1565 format
= PixelFormat32bppRGB
;
1568 format
= PixelFormat48bppRGB
;
1571 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
1572 return InvalidParameter
;
1576 bits
= (BYTE
*)bm
.bmBits
+ (bm
.bmHeight
- 1) * bm
.bmWidthBytes
;
1579 FIXME("can only get image data from DIB sections\n");
1583 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, -bm
.bmWidthBytes
,
1584 format
, bits
, bitmap
);
1589 /*****************************************************************************
1590 * GdipSetEffectParameters [GDIPLUS.@]
1592 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
1593 const VOID
*params
, const UINT size
)
1598 FIXME("not implemented\n");
1600 return NotImplemented
;
1603 /*****************************************************************************
1604 * GdipGetImageFlags [GDIPLUS.@]
1606 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
1608 TRACE("%p %p\n", image
, flags
);
1610 if(!image
|| !flags
)
1611 return InvalidParameter
;
1613 *flags
= image
->flags
;
1618 GpStatus WINGDIPAPI
GdipTestControl(GpTestControlEnum control
, void *param
)
1620 TRACE("(%d, %p)\n", control
, param
);
1623 case TestControlForceBilinear
:
1625 FIXME("TestControlForceBilinear not handled\n");
1627 case TestControlNoICM
:
1629 FIXME("TestControlNoICM not handled\n");
1631 case TestControlGetBuildNumber
:
1632 *((DWORD
*)param
) = 3102;
1639 GpStatus WINGDIPAPI
GdipRecordMetafileFileName(GDIPCONST WCHAR
* fileName
,
1640 HDC hdc
, EmfType type
, GDIPCONST GpRectF
*pFrameRect
,
1641 MetafileFrameUnit frameUnit
, GDIPCONST WCHAR
*desc
,
1642 GpMetafile
**metafile
)
1644 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName
), hdc
, type
, pFrameRect
,
1645 frameUnit
, debugstr_w(desc
), metafile
);
1647 return NotImplemented
;
1650 GpStatus WINGDIPAPI
GdipRecordMetafileFileNameI(GDIPCONST WCHAR
* fileName
, HDC hdc
, EmfType type
,
1651 GDIPCONST GpRect
*pFrameRect
, MetafileFrameUnit frameUnit
,
1652 GDIPCONST WCHAR
*desc
, GpMetafile
**metafile
)
1654 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName
), hdc
, type
, pFrameRect
,
1655 frameUnit
, debugstr_w(desc
), metafile
);
1657 return NotImplemented
;
1660 GpStatus WINGDIPAPI
GdipImageForceValidation(GpImage
*image
)
1662 FIXME("%p\n", image
);