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 /* This function returns a pointer to an array of pixels that represents the
91 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
92 * flags. It is correct behavior that a user who calls this function with write
93 * privileges can write to the whole bitmap (not just the area in rect).
95 * FIXME: only used portion of format is bits per pixel. */
96 GpStatus WINGDIPAPI
GdipBitmapLockBits(GpBitmap
* bitmap
, GDIPCONST GpRect
* rect
,
97 UINT flags
, PixelFormat format
, BitmapData
* lockeddata
)
100 INT stride
, bitspp
= PIXELFORMATBPP(format
);
102 HBITMAP hbm
, old
= NULL
;
106 GpRect act_rect
; /* actual rect to be used */
108 TRACE("%p %p %d %d %p\n", bitmap
, rect
, flags
, format
, lockeddata
);
110 if(!lockeddata
|| !bitmap
)
111 return InvalidParameter
;
114 if(rect
->X
< 0 || rect
->Y
< 0 || (rect
->X
+ rect
->Width
> bitmap
->width
) ||
115 (rect
->Y
+ rect
->Height
> bitmap
->height
) || !flags
)
116 return InvalidParameter
;
121 act_rect
.X
= act_rect
.Y
= 0;
122 act_rect
.Width
= bitmap
->width
;
123 act_rect
.Height
= bitmap
->height
;
126 if(flags
& ImageLockModeUserInputBuf
)
127 return NotImplemented
;
132 IPicture_get_Handle(bitmap
->image
.picture
, (OLE_HANDLE
*)&hbm
);
133 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
134 bm_is_selected
= (hdc
!= 0);
136 pbmi
= GdipAlloc(sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
139 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
140 pbmi
->bmiHeader
.biBitCount
= 0;
143 hdc
= CreateCompatibleDC(0);
144 old
= SelectObject(hdc
, hbm
);
148 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
150 abs_height
= abs(pbmi
->bmiHeader
.biHeight
);
151 stride
= pbmi
->bmiHeader
.biWidth
* bitspp
/ 8;
152 stride
= (stride
+ 3) & ~3;
154 buff
= GdipAlloc(stride
* abs_height
);
156 pbmi
->bmiHeader
.biBitCount
= bitspp
;
159 GetDIBits(hdc
, hbm
, 0, abs_height
, buff
, pbmi
, DIB_RGB_COLORS
);
162 SelectObject(hdc
, old
);
171 lockeddata
->Width
= act_rect
.Width
;
172 lockeddata
->Height
= act_rect
.Height
;
173 lockeddata
->PixelFormat
= format
;
174 lockeddata
->Reserved
= flags
;
176 if(pbmi
->bmiHeader
.biHeight
> 0){
177 lockeddata
->Stride
= -stride
;
178 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * act_rect
.X
+
179 stride
* (abs_height
- 1 - act_rect
.Y
);
182 lockeddata
->Stride
= stride
;
183 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * act_rect
.X
+ stride
* act_rect
.Y
;
186 bitmap
->lockmode
= flags
;
189 bitmap
->bitmapbits
= buff
;
195 GpStatus WINGDIPAPI
GdipBitmapUnlockBits(GpBitmap
* bitmap
,
196 BitmapData
* lockeddata
)
199 HBITMAP hbm
, old
= NULL
;
203 if(!bitmap
|| !lockeddata
)
204 return InvalidParameter
;
206 if(!bitmap
->lockmode
)
209 if(lockeddata
->Reserved
& ImageLockModeUserInputBuf
)
210 return NotImplemented
;
212 if(lockeddata
->Reserved
& ImageLockModeRead
){
213 if(!(--bitmap
->numlocks
))
214 bitmap
->lockmode
= 0;
216 GdipFree(bitmap
->bitmapbits
);
217 bitmap
->bitmapbits
= NULL
;
221 IPicture_get_Handle(bitmap
->image
.picture
, (OLE_HANDLE
*)&hbm
);
222 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
223 bm_is_selected
= (hdc
!= 0);
225 pbmi
= GdipAlloc(sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
226 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
227 pbmi
->bmiHeader
.biBitCount
= 0;
230 hdc
= CreateCompatibleDC(0);
231 old
= SelectObject(hdc
, hbm
);
234 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
235 pbmi
->bmiHeader
.biBitCount
= PIXELFORMATBPP(lockeddata
->PixelFormat
);
236 SetDIBits(hdc
, hbm
, 0, abs(pbmi
->bmiHeader
.biHeight
),
237 bitmap
->bitmapbits
, pbmi
, DIB_RGB_COLORS
);
240 SelectObject(hdc
, old
);
245 GdipFree(bitmap
->bitmapbits
);
246 bitmap
->bitmapbits
= NULL
;
247 bitmap
->lockmode
= 0;
252 GpStatus WINGDIPAPI
GdipCloneImage(GpImage
*image
, GpImage
**cloneImage
)
258 GpStatus stat
= GenericError
;
260 TRACE("%p, %p\n", image
, cloneImage
);
262 if (!image
|| !cloneImage
)
263 return InvalidParameter
;
265 hr
= CreateStreamOnHGlobal(0, TRUE
, &stream
);
269 hr
= IPicture_SaveAsFile(image
->picture
, stream
, FALSE
, &size
);
272 WARN("Failed to save image on stream\n");
276 /* Set seek pointer back to the beginning of the picture */
278 hr
= IStream_Seek(stream
, move
, STREAM_SEEK_SET
, NULL
);
282 stat
= GdipLoadImageFromStream(stream
, cloneImage
);
283 if (stat
!= Ok
) WARN("Failed to load image from stream\n");
286 IStream_Release(stream
);
290 GpStatus WINGDIPAPI
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
296 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
298 if(!filename
|| !bitmap
)
299 return InvalidParameter
;
301 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
306 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
308 IStream_Release(stream
);
313 GpStatus WINGDIPAPI
GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO
* info
,
314 VOID
*bits
, GpBitmap
**bitmap
)
316 DWORD height
, stride
;
319 FIXME("(%p, %p, %p) - partially implemented\n", info
, bits
, bitmap
);
321 height
= abs(info
->bmiHeader
.biHeight
);
322 stride
= ((info
->bmiHeader
.biWidth
* info
->bmiHeader
.biBitCount
+ 31) >> 3) & ~3;
324 if(info
->bmiHeader
.biHeight
> 0) /* bottom-up */
326 bits
= (BYTE
*)bits
+ (height
- 1) * stride
;
330 switch(info
->bmiHeader
.biBitCount
) {
332 format
= PixelFormat1bppIndexed
;
335 format
= PixelFormat4bppIndexed
;
338 format
= PixelFormat8bppIndexed
;
341 format
= PixelFormat24bppRGB
;
344 FIXME("don't know how to handle %d bpp\n", info
->bmiHeader
.biBitCount
);
346 return InvalidParameter
;
349 return GdipCreateBitmapFromScan0(info
->bmiHeader
.biWidth
, height
, stride
, format
,
355 GpStatus WINGDIPAPI
GdipCreateBitmapFromFileICM(GDIPCONST WCHAR
* filename
,
358 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
360 return GdipCreateBitmapFromFile(filename
, bitmap
);
363 GpStatus WINGDIPAPI
GdipCreateBitmapFromResource(HINSTANCE hInstance
,
364 GDIPCONST WCHAR
* lpBitmapName
, GpBitmap
** bitmap
)
367 GpStatus stat
= InvalidParameter
;
369 TRACE("%p (%s) %p\n", hInstance
, debugstr_w(lpBitmapName
), bitmap
);
371 if(!lpBitmapName
|| !bitmap
)
372 return InvalidParameter
;
375 hbm
= LoadImageW(hInstance
, lpBitmapName
, IMAGE_BITMAP
, 0, 0,
376 LR_CREATEDIBSECTION
);
379 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, bitmap
);
386 GpStatus WINGDIPAPI
GdipCreateHBITMAPFromBitmap(GpBitmap
* bitmap
,
387 HBITMAP
* hbmReturn
, ARGB background
)
393 return NotImplemented
;
396 GpStatus WINGDIPAPI
GdipConvertToEmfPlus(const GpGraphics
* ref
,
397 GpMetafile
* metafile
, BOOL
* succ
, EmfType emfType
,
398 const WCHAR
* description
, GpMetafile
** out_metafile
)
402 if(!ref
|| !metafile
|| !out_metafile
)
403 return InvalidParameter
;
406 *out_metafile
= NULL
;
409 FIXME("not implemented\n");
411 return NotImplemented
;
414 /* FIXME: this should create a bitmap in the given size with the attributes
415 * (resolution etc.) of the graphics object */
416 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
417 GpGraphics
* target
, GpBitmap
** bitmap
)
422 if(!target
|| !bitmap
)
423 return InvalidParameter
;
426 FIXME("hacked stub\n");
428 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat24bppRGB
,
434 GpStatus WINGDIPAPI
GdipCreateBitmapFromHICON(HICON hicon
, GpBitmap
** bitmap
)
440 TRACE("%p, %p\n", hicon
, bitmap
);
442 if(!bitmap
|| !GetIconInfo(hicon
, &iinfo
))
443 return InvalidParameter
;
445 *bitmap
= GdipAlloc(sizeof(GpBitmap
));
446 if(!*bitmap
) return OutOfMemory
;
448 icon_copy
= CreateIconIndirect(&iinfo
);
452 return InvalidParameter
;
455 desc
.cbSizeofstruct
= sizeof(PICTDESC
);
456 desc
.picType
= PICTYPE_ICON
;
457 desc
.u
.icon
.hicon
= icon_copy
;
459 if(OleCreatePictureIndirect(&desc
, &IID_IPicture
, TRUE
,
460 (LPVOID
*) &((*bitmap
)->image
.picture
)) != S_OK
){
461 DestroyIcon(icon_copy
);
466 (*bitmap
)->format
= PixelFormat32bppARGB
;
467 (*bitmap
)->image
.type
= ImageTypeBitmap
;
468 (*bitmap
)->image
.flags
= ImageFlagsNone
;
469 (*bitmap
)->width
= ipicture_pixel_width((*bitmap
)->image
.picture
);
470 (*bitmap
)->height
= ipicture_pixel_height((*bitmap
)->image
.picture
);
472 DeleteObject(iinfo
.hbmColor
);
473 DeleteObject(iinfo
.hbmMask
);
478 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
479 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
481 BITMAPFILEHEADER
*bmfh
;
482 BITMAPINFOHEADER
*bmih
;
487 TRACE("%d %d %d %d %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
489 if (!bitmap
) return InvalidParameter
;
491 if(width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
493 return InvalidParameter
;
497 return InvalidParameter
;
499 /* FIXME: windows allows negative stride (reads backwards from scan0) */
501 FIXME("negative stride\n");
502 return InvalidParameter
;
505 *bitmap
= GdipAlloc(sizeof(GpBitmap
));
506 if(!*bitmap
) return OutOfMemory
;
509 stride
= width
* (PIXELFORMATBPP(format
) / 8);
510 stride
= (stride
+ 3) & ~3;
513 datalen
= abs(stride
* height
);
514 size
= sizeof(BITMAPFILEHEADER
) + sizeof(BITMAPINFOHEADER
) + datalen
;
515 buff
= GdipAlloc(size
);
521 bmfh
= (BITMAPFILEHEADER
*) buff
;
522 bmih
= (BITMAPINFOHEADER
*) (bmfh
+ 1);
524 bmfh
->bfType
= (((WORD
)'M') << 8) + (WORD
)'B';
526 bmfh
->bfOffBits
= size
- datalen
;
528 bmih
->biSize
= sizeof(BITMAPINFOHEADER
);
529 bmih
->biWidth
= width
;
530 bmih
->biHeight
= -height
;
531 /* FIXME: use the rest of the data from format */
532 bmih
->biBitCount
= PIXELFORMATBPP(format
);
533 bmih
->biCompression
= BI_RGB
;
534 bmih
->biSizeImage
= datalen
;
537 memcpy(bmih
+ 1, scan0
, datalen
);
539 memset(bmih
+ 1, 0, datalen
);
541 if(CreateStreamOnHGlobal(buff
, TRUE
, &stream
) != S_OK
){
542 ERR("could not make stream\n");
548 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
549 (LPVOID
*) &((*bitmap
)->image
.picture
)) != S_OK
){
550 TRACE("Could not load picture\n");
551 IStream_Release(stream
);
557 (*bitmap
)->image
.type
= ImageTypeBitmap
;
558 (*bitmap
)->image
.flags
= ImageFlagsNone
;
559 (*bitmap
)->width
= width
;
560 (*bitmap
)->height
= height
;
561 (*bitmap
)->format
= format
;
566 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
571 TRACE("%p %p\n", stream
, bitmap
);
573 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
578 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
579 IPicture_Release((*bitmap
)->image
.picture
);
581 return GenericError
; /* FIXME: what error to return? */
588 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
591 TRACE("%p %p\n", stream
, bitmap
);
593 return GdipCreateBitmapFromStream(stream
, bitmap
);
596 GpStatus WINGDIPAPI
GdipCreateCachedBitmap(GpBitmap
*bitmap
, GpGraphics
*graphics
,
597 GpCachedBitmap
**cachedbmp
)
601 TRACE("%p %p %p\n", bitmap
, graphics
, cachedbmp
);
603 if(!bitmap
|| !graphics
|| !cachedbmp
)
604 return InvalidParameter
;
606 *cachedbmp
= GdipAlloc(sizeof(GpCachedBitmap
));
610 stat
= GdipCloneImage(&(bitmap
->image
), &(*cachedbmp
)->image
);
612 GdipFree(*cachedbmp
);
619 GpStatus WINGDIPAPI
GdipDeleteCachedBitmap(GpCachedBitmap
*cachedbmp
)
621 TRACE("%p\n", cachedbmp
);
624 return InvalidParameter
;
626 GdipDisposeImage(cachedbmp
->image
);
632 GpStatus WINGDIPAPI
GdipDrawCachedBitmap(GpGraphics
*graphics
,
633 GpCachedBitmap
*cachedbmp
, INT x
, INT y
)
635 TRACE("%p %p %d %d\n", graphics
, cachedbmp
, x
, y
);
637 if(!graphics
|| !cachedbmp
)
638 return InvalidParameter
;
640 return GdipDrawImage(graphics
, cachedbmp
->image
, (REAL
)x
, (REAL
)y
);
643 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
647 TRACE("%p\n", image
);
650 return InvalidParameter
;
652 IPicture_get_CurDC(image
->picture
, &hdc
);
654 IPicture_Release(image
->picture
);
655 if (image
->type
== ImageTypeBitmap
)
656 GdipFree(((GpBitmap
*)image
)->bitmapbits
);
662 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
665 return InvalidParameter
;
667 return NotImplemented
;
670 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
673 TRACE("%p %p %p\n", image
, srcRect
, srcUnit
);
675 if(!image
|| !srcRect
|| !srcUnit
)
676 return InvalidParameter
;
677 if(image
->type
== ImageTypeMetafile
){
678 *srcRect
= ((GpMetafile
*)image
)->bounds
;
679 *srcUnit
= ((GpMetafile
*)image
)->unit
;
681 else if(image
->type
== ImageTypeBitmap
){
682 srcRect
->X
= srcRect
->Y
= 0.0;
683 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
684 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
685 *srcUnit
= UnitPixel
;
688 srcRect
->X
= srcRect
->Y
= 0.0;
689 srcRect
->Width
= ipicture_pixel_width(image
->picture
);
690 srcRect
->Height
= ipicture_pixel_height(image
->picture
);
691 *srcUnit
= UnitPixel
;
694 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
695 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
700 GpStatus WINGDIPAPI
GdipGetImageDimension(GpImage
*image
, REAL
*width
,
703 TRACE("%p %p %p\n", image
, width
, height
);
705 if(!image
|| !height
|| !width
)
706 return InvalidParameter
;
708 if(image
->type
== ImageTypeMetafile
){
711 *height
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
712 ((GpMetafile
*)image
)->bounds
.Height
;
714 *width
= convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
715 ((GpMetafile
*)image
)->bounds
.Width
;
720 else if(image
->type
== ImageTypeBitmap
){
721 *height
= ((GpBitmap
*)image
)->height
;
722 *width
= ((GpBitmap
*)image
)->width
;
725 *height
= ipicture_pixel_height(image
->picture
);
726 *width
= ipicture_pixel_width(image
->picture
);
729 TRACE("returning (%f, %f)\n", *height
, *width
);
733 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
734 GpGraphics
**graphics
)
738 TRACE("%p %p\n", image
, graphics
);
740 if(!image
|| !graphics
)
741 return InvalidParameter
;
743 if(image
->type
!= ImageTypeBitmap
){
744 FIXME("not implemented for image type %d\n", image
->type
);
745 return NotImplemented
;
748 IPicture_get_CurDC(image
->picture
, &hdc
);
751 hdc
= CreateCompatibleDC(0);
752 IPicture_SelectPicture(image
->picture
, hdc
, NULL
, NULL
);
755 return GdipCreateFromHDC(hdc
, graphics
);
758 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
760 TRACE("%p %p\n", image
, height
);
762 if(!image
|| !height
)
763 return InvalidParameter
;
765 if(image
->type
== ImageTypeMetafile
){
768 *height
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
769 ((GpMetafile
*)image
)->bounds
.Height
);
773 else if(image
->type
== ImageTypeBitmap
)
774 *height
= ((GpBitmap
*)image
)->height
;
776 *height
= ipicture_pixel_height(image
->picture
);
778 TRACE("returning %d\n", *height
);
783 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
788 return InvalidParameter
;
791 FIXME("not implemented\n");
793 return NotImplemented
;
796 GpStatus WINGDIPAPI
GdipGetImagePaletteSize(GpImage
*image
, INT
*size
)
798 FIXME("%p %p\n", image
, size
);
801 return InvalidParameter
;
803 return NotImplemented
;
806 /* FIXME: test this function for non-bitmap types */
807 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
809 TRACE("%p %p\n", image
, format
);
811 if(!image
|| !format
)
812 return InvalidParameter
;
814 if(image
->type
!= ImageTypeBitmap
)
815 *format
= PixelFormat24bppRGB
;
817 *format
= ((GpBitmap
*) image
)->format
;
822 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
826 if(!image
|| !format
)
827 return InvalidParameter
;
832 /* FIXME: should be detected from embedded picture or stored separately */
835 case ImageTypeBitmap
: *format
= ImageFormatBMP
; break;
836 case ImageTypeMetafile
: *format
= ImageFormatEMF
; break;
838 WARN("unknown type %u\n", image
->type
);
839 *format
= ImageFormatUndefined
;
844 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
846 TRACE("%p %p\n", image
, type
);
849 return InvalidParameter
;
856 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
861 return InvalidParameter
;
864 FIXME("not implemented\n");
866 return NotImplemented
;
869 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
871 TRACE("%p %p\n", image
, width
);
874 return InvalidParameter
;
876 if(image
->type
== ImageTypeMetafile
){
879 *width
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
880 ((GpMetafile
*)image
)->bounds
.Width
);
884 else if(image
->type
== ImageTypeBitmap
)
885 *width
= ((GpBitmap
*)image
)->width
;
887 *width
= ipicture_pixel_width(image
->picture
);
889 TRACE("returning %d\n", *width
);
894 GpStatus WINGDIPAPI
GdipGetMetafileHeaderFromMetafile(GpMetafile
* metafile
,
895 MetafileHeader
* header
)
899 if(!metafile
|| !header
)
900 return InvalidParameter
;
903 FIXME("not implemented\n");
908 GpStatus WINGDIPAPI
GdipGetAllPropertyItems(GpImage
*image
, UINT size
,
909 UINT num
, PropertyItem
* items
)
914 FIXME("not implemented\n");
916 return InvalidParameter
;
919 GpStatus WINGDIPAPI
GdipGetPropertyCount(GpImage
*image
, UINT
* num
)
924 FIXME("not implemented\n");
926 return InvalidParameter
;
929 GpStatus WINGDIPAPI
GdipGetPropertyIdList(GpImage
*image
, UINT num
, PROPID
* list
)
934 FIXME("not implemented\n");
936 return InvalidParameter
;
939 GpStatus WINGDIPAPI
GdipGetPropertyItem(GpImage
*image
, PROPID id
, UINT size
,
940 PropertyItem
* buffer
)
945 FIXME("not implemented\n");
947 return InvalidParameter
;
950 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID pid
,
955 TRACE("%p %x %p\n", image
, pid
, size
);
958 return InvalidParameter
;
961 FIXME("not implemented\n");
963 return NotImplemented
;
966 GpStatus WINGDIPAPI
GdipGetPropertySize(GpImage
*image
, UINT
* size
, UINT
* num
)
971 FIXME("not implemented\n");
973 return InvalidParameter
;
976 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
977 GDIPCONST GUID
* dimensionID
, UINT
* count
)
981 if(!image
|| !dimensionID
|| !count
)
982 return InvalidParameter
;
985 FIXME("not implemented\n");
987 return NotImplemented
;
990 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsCount(GpImage
*image
,
994 return InvalidParameter
;
1003 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
1004 GUID
* dimensionIDs
, UINT count
)
1008 if(!image
|| !dimensionIDs
)
1009 return InvalidParameter
;
1012 FIXME("not implemented\n");
1017 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
,
1018 GDIPCONST GUID
* dimensionID
, UINT frameidx
)
1022 if(!image
|| !dimensionID
)
1023 return InvalidParameter
;
1026 FIXME("not implemented\n");
1031 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
1037 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
1039 if (!filename
|| !image
)
1040 return InvalidParameter
;
1042 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
1047 stat
= GdipLoadImageFromStream(stream
, image
);
1049 IStream_Release(stream
);
1054 /* FIXME: no icm handling */
1055 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
1057 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
1059 return GdipLoadImageFromFile(filename
, image
);
1062 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
* stream
, GpImage
**image
)
1067 TRACE("%p %p\n", stream
, image
);
1069 if(!stream
|| !image
)
1070 return InvalidParameter
;
1072 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
1073 (LPVOID
*) &pic
) != S_OK
){
1074 TRACE("Could not load picture\n");
1075 return GenericError
;
1078 IPicture_get_Type(pic
, &type
);
1080 if(type
== PICTYPE_BITMAP
){
1082 BITMAPCOREHEADER
* bmch
;
1086 pbmi
= GdipAlloc(sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
1089 *image
= GdipAlloc(sizeof(GpBitmap
));
1094 (*image
)->type
= ImageTypeBitmap
;
1096 (*((GpBitmap
**) image
))->width
= ipicture_pixel_width(pic
);
1097 (*((GpBitmap
**) image
))->height
= ipicture_pixel_height(pic
);
1099 /* get the pixel format */
1100 IPicture_get_Handle(pic
, (OLE_HANDLE
*)&hbm
);
1101 IPicture_get_CurDC(pic
, &hdc
);
1103 bmch
= (BITMAPCOREHEADER
*) (&pbmi
->bmiHeader
);
1104 bmch
->bcSize
= sizeof(BITMAPCOREHEADER
);
1108 hdc
= CreateCompatibleDC(0);
1109 old
= SelectObject(hdc
, hbm
);
1110 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
1111 SelectObject(hdc
, old
);
1115 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
1117 switch(bmch
->bcBitCount
)
1120 (*((GpBitmap
**) image
))->format
= PixelFormat1bppIndexed
;
1123 (*((GpBitmap
**) image
))->format
= PixelFormat4bppIndexed
;
1126 (*((GpBitmap
**) image
))->format
= PixelFormat8bppIndexed
;
1129 (*((GpBitmap
**) image
))->format
= PixelFormat16bppRGB565
;
1132 (*((GpBitmap
**) image
))->format
= PixelFormat24bppRGB
;
1135 (*((GpBitmap
**) image
))->format
= PixelFormat32bppRGB
;
1138 (*((GpBitmap
**) image
))->format
= PixelFormat48bppRGB
;
1141 FIXME("Bit depth %d is not fully supported yet\n", bmch
->bcBitCount
);
1142 (*((GpBitmap
**) image
))->format
= (bmch
->bcBitCount
<< 8) | PixelFormatGDI
;
1148 else if(type
== PICTYPE_METAFILE
|| type
== PICTYPE_ENHMETAFILE
){
1149 /* FIXME: missing initialization code */
1150 *image
= GdipAlloc(sizeof(GpMetafile
));
1151 if(!*image
) return OutOfMemory
;
1152 (*image
)->type
= ImageTypeMetafile
;
1155 *image
= GdipAlloc(sizeof(GpImage
));
1156 if(!*image
) return OutOfMemory
;
1157 (*image
)->type
= ImageTypeUnknown
;
1160 (*image
)->picture
= pic
;
1161 (*image
)->flags
= ImageFlagsNone
;
1167 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
1169 TRACE("%p %p\n", stream
, image
);
1171 return GdipLoadImageFromStream(stream
, image
);
1174 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
1179 return InvalidParameter
;
1182 FIXME("not implemented\n");
1184 return NotImplemented
;
1187 GpStatus WINGDIPAPI
GdipSetPropertyItem(GpImage
*image
, GDIPCONST PropertyItem
* item
)
1192 FIXME("not implemented\n");
1194 return NotImplemented
;
1197 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
1198 GDIPCONST CLSID
*clsidEncoder
,
1199 GDIPCONST EncoderParameters
*encoderParams
)
1204 TRACE("%p (%s) %p %p\n", image
, debugstr_w(filename
), clsidEncoder
, encoderParams
);
1206 if (!image
|| !filename
|| !clsidEncoder
)
1207 return InvalidParameter
;
1209 if (!(image
->picture
))
1210 return InvalidParameter
;
1212 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
1214 return GenericError
;
1216 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
1218 IStream_Release(stream
);
1222 /*************************************************************************
1223 * Encoding functions -
1224 * These functions encode an image in different image file formats.
1226 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1227 #define BITMAP_FORMAT_JPEG 0xd8ff
1228 #define BITMAP_FORMAT_GIF 0x4947
1229 #define BITMAP_FORMAT_PNG 0x5089
1230 #define BITMAP_FORMAT_APM 0xcdd7
1232 static GpStatus
encode_image_BMP(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
1233 void **output
, unsigned int *output_size
)
1235 int num_palette_entries
;
1236 BITMAPFILEHEADER
*bmp_file_hdr
;
1237 BITMAPINFO
*bmp_info_hdr
;
1239 if (bitmap_info
->bmiHeader
.biClrUsed
) {
1240 num_palette_entries
= bitmap_info
->bmiHeader
.biClrUsed
;
1241 if (num_palette_entries
> 256) num_palette_entries
= 256;
1243 if (bitmap_info
->bmiHeader
.biBitCount
<= 8)
1244 num_palette_entries
= 1 << bitmap_info
->bmiHeader
.biBitCount
;
1246 num_palette_entries
= 0;
1250 sizeof(BITMAPFILEHEADER
) +
1251 sizeof(BITMAPINFOHEADER
) +
1252 num_palette_entries
* sizeof(RGBQUAD
) +
1253 bitmap_info
->bmiHeader
.biSizeImage
;
1255 *output
= GdipAlloc(*output_size
);
1257 bmp_file_hdr
= *output
;
1258 bmp_file_hdr
->bfType
= BITMAP_FORMAT_BMP
;
1259 bmp_file_hdr
->bfSize
= *output_size
;
1260 bmp_file_hdr
->bfOffBits
=
1261 sizeof(BITMAPFILEHEADER
) +
1262 sizeof(BITMAPINFOHEADER
) +
1263 num_palette_entries
* sizeof (RGBQUAD
);
1265 bmp_info_hdr
= (BITMAPINFO
*) ((unsigned char*)(*output
) + sizeof(BITMAPFILEHEADER
));
1266 memcpy(bmp_info_hdr
, bitmap_info
, sizeof(BITMAPINFOHEADER
) + num_palette_entries
* sizeof(RGBQUAD
));
1267 memcpy((unsigned char *)(*output
) +
1268 sizeof(BITMAPFILEHEADER
) +
1269 sizeof(BITMAPINFOHEADER
) +
1270 num_palette_entries
* sizeof(RGBQUAD
),
1271 bitmap_bits
, bitmap_info
->bmiHeader
.biSizeImage
);
1276 typedef GpStatus
encode_image_func(LPVOID bitmap_bits
, LPBITMAPINFO bitmap_info
,
1277 void **output
, unsigned int *output_size
);
1281 NUM_ENCODERS_SUPPORTED
1284 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
];
1285 static encode_image_func
*const encode_image_funcs
[NUM_ENCODERS_SUPPORTED
] = {
1289 /*****************************************************************************
1290 * GdipSaveImageToStream [GDIPLUS.@]
1292 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
1293 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
1302 BITMAPINFO bmp_info
;
1304 encode_image_func
* encode_image
;
1306 unsigned int output_size
;
1314 TRACE("%p %p %p %p\n", image
, stream
, clsid
, params
);
1316 if(!image
|| !stream
)
1317 return InvalidParameter
;
1319 if (!image
->picture
)
1320 return GenericError
;
1322 hr
= IPicture_get_Type(image
->picture
, &type
);
1323 if (FAILED(hr
) || type
!= PICTYPE_BITMAP
)
1324 return GenericError
;
1326 /* select correct encoder */
1327 encode_image
= NULL
;
1328 for (i
= 0; i
< NUM_ENCODERS_SUPPORTED
; i
++) {
1329 if (IsEqualCLSID(clsid
, &codecs
[i
].Clsid
))
1330 encode_image
= encode_image_funcs
[i
];
1332 if (encode_image
== NULL
)
1333 return UnknownImageFormat
;
1335 /* extract underlying hbitmap representation from the IPicture */
1336 hr
= IPicture_get_Handle(image
->picture
, (OLE_HANDLE
*)&hbmp
);
1337 if (FAILED(hr
) || !hbmp
)
1338 return GenericError
;
1339 hr
= IPicture_get_CurDC(image
->picture
, &hdc
);
1341 return GenericError
;
1342 bm_is_selected
= (hdc
!= 0);
1343 if (!bm_is_selected
) {
1344 hdc
= CreateCompatibleDC(0);
1345 old_hbmp
= SelectObject(hdc
, hbmp
);
1348 /* get bits from HBITMAP */
1349 bmp_info
.bmiHeader
.biSize
= sizeof(bmp_info
.bmiHeader
);
1350 bmp_info
.bmiHeader
.biBitCount
= 0;
1351 GetDIBits(hdc
, hbmp
, 0, 0, NULL
, &bmp_info
, DIB_RGB_COLORS
);
1353 bmp_bits
= GdipAlloc(bmp_info
.bmiHeader
.biSizeImage
);
1356 GetDIBits(hdc
, hbmp
, 0, abs(bmp_info
.bmiHeader
.biHeight
), bmp_bits
, &bmp_info
, DIB_RGB_COLORS
);
1358 if (!bm_is_selected
) {
1359 SelectObject(hdc
, old_hbmp
);
1366 stat
= encode_image(bmp_bits
, &bmp_info
, &output
, &output_size
);
1368 IStream_Write(stream
, output
, output_size
, &dummy
);
1376 /*****************************************************************************
1377 * GdipSetImagePalette [GDIPLUS.@]
1379 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
1380 GDIPCONST ColorPalette
*palette
)
1384 if(!image
|| !palette
)
1385 return InvalidParameter
;
1388 FIXME("not implemented\n");
1390 return NotImplemented
;
1393 /*************************************************************************
1395 * Structures that represent which formats we support for encoding.
1398 /* ImageCodecInfo creation routines taken from libgdiplus */
1399 static const WCHAR bmp_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1400 static const WCHAR bmp_extension
[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1401 static const WCHAR bmp_mimetype
[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1402 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0}; /* BMP */
1403 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
1404 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
1406 static const ImageCodecInfo codecs
[NUM_ENCODERS_SUPPORTED
] =
1409 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1410 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1411 /* CodecName */ bmp_codecname
,
1413 /* FormatDescription */ bmp_format
,
1414 /* FilenameExtension */ bmp_extension
,
1415 /* MimeType */ bmp_mimetype
,
1416 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
1420 /* SigPattern */ bmp_sig_pattern
,
1421 /* SigMask */ bmp_sig_mask
,
1425 /*****************************************************************************
1426 * GdipGetImageDecodersSize [GDIPLUS.@]
1428 GpStatus WINGDIPAPI
GdipGetImageDecodersSize(UINT
*numDecoders
, UINT
*size
)
1430 FIXME("%p %p stub!\n", numDecoders
, size
);
1432 if (!numDecoders
|| !size
)
1433 return InvalidParameter
;
1441 /*****************************************************************************
1442 * GdipGetImageDecoders [GDIPLUS.@]
1444 GpStatus WINGDIPAPI
GdipGetImageDecoders(UINT numDecoders
, UINT size
, ImageCodecInfo
*decoders
)
1446 FIXME("%u %u %p stub!\n", numDecoders
, size
, decoders
);
1449 return GenericError
;
1451 return NotImplemented
;
1454 /*****************************************************************************
1455 * GdipGetImageEncodersSize [GDIPLUS.@]
1457 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
1459 TRACE("%p %p\n", numEncoders
, size
);
1461 if (!numEncoders
|| !size
)
1462 return InvalidParameter
;
1464 *numEncoders
= NUM_ENCODERS_SUPPORTED
;
1465 *size
= sizeof (codecs
);
1470 /*****************************************************************************
1471 * GdipGetImageEncoders [GDIPLUS.@]
1473 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
1475 TRACE("%u %u %p\n", numEncoders
, size
, encoders
);
1478 (numEncoders
!= NUM_ENCODERS_SUPPORTED
) ||
1479 (size
!= sizeof (codecs
)))
1480 return GenericError
;
1482 memcpy(encoders
, codecs
, sizeof (codecs
));
1487 /*****************************************************************************
1488 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1490 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
1496 TRACE("%p %p %p\n", hbm
, hpal
, bitmap
);
1499 return InvalidParameter
;
1501 /* TODO: Support for device-dependent bitmaps */
1503 FIXME("no support for device-dependent bitmaps\n");
1504 return NotImplemented
;
1507 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
1508 return InvalidParameter
;
1510 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1511 switch(bm
.bmBitsPixel
) {
1513 format
= PixelFormat1bppIndexed
;
1516 format
= PixelFormat4bppIndexed
;
1519 format
= PixelFormat8bppIndexed
;
1522 format
= PixelFormat24bppRGB
;
1525 format
= PixelFormat32bppRGB
;
1528 format
= PixelFormat48bppRGB
;
1531 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
1532 return InvalidParameter
;
1535 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, bm
.bmWidthBytes
,
1536 format
, bm
.bmBits
, bitmap
);
1541 /*****************************************************************************
1542 * GdipSetEffectParameters [GDIPLUS.@]
1544 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
1545 const VOID
*params
, const UINT size
)
1550 FIXME("not implemented\n");
1552 return NotImplemented
;
1555 /*****************************************************************************
1556 * GdipGetImageFlags [GDIPLUS.@]
1558 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
1560 TRACE("%p %p\n", image
, flags
);
1562 if(!image
|| !flags
)
1563 return InvalidParameter
;
1565 *flags
= image
->flags
;
1570 GpStatus WINGDIPAPI
GdipTestControl(GpTestControlEnum control
, void *param
)
1572 TRACE("(%d, %p)\n", control
, param
);
1575 case TestControlForceBilinear
:
1577 FIXME("TestControlForceBilinear not handled\n");
1579 case TestControlNoICM
:
1581 FIXME("TestControlNoICM not handled\n");
1583 case TestControlGetBuildNumber
:
1584 *((DWORD
*)param
) = 3102;
1591 GpStatus WINGDIPAPI
GdipRecordMetafileFileName(GDIPCONST WCHAR
* fileName
,
1592 HDC hdc
, EmfType type
, GDIPCONST GpRectF
*pFrameRect
,
1593 MetafileFrameUnit frameUnit
, GDIPCONST WCHAR
*desc
,
1594 GpMetafile
**metafile
)
1596 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName
), hdc
, type
, pFrameRect
,
1597 frameUnit
, debugstr_w(desc
), metafile
);
1599 return NotImplemented
;
1602 GpStatus WINGDIPAPI
GdipRecordMetafileFileNameI(GDIPCONST WCHAR
* fileName
, HDC hdc
, EmfType type
,
1603 GDIPCONST GpRect
*pFrameRect
, MetafileFrameUnit frameUnit
,
1604 GDIPCONST WCHAR
*desc
, GpMetafile
**metafile
)
1606 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName
), hdc
, type
, pFrameRect
,
1607 frameUnit
, debugstr_w(desc
), metafile
);
1609 return NotImplemented
;
1612 GpStatus WINGDIPAPI
GdipImageForceValidation(GpImage
*image
)
1614 FIXME("%p\n", image
);