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 typedef void ImageItemData
;
39 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
41 static INT
ipicture_pixel_height(IPicture
*pic
)
46 IPicture_get_Height(pic
, &y
);
50 y
= (UINT
)(((REAL
)y
) * ((REAL
)GetDeviceCaps(hdcref
, LOGPIXELSY
)) /
51 ((REAL
)INCH_HIMETRIC
));
57 static INT
ipicture_pixel_width(IPicture
*pic
)
62 IPicture_get_Width(pic
, &x
);
66 x
= (UINT
)(((REAL
)x
) * ((REAL
)GetDeviceCaps(hdcref
, LOGPIXELSX
)) /
67 ((REAL
)INCH_HIMETRIC
));
74 GpStatus WINGDIPAPI
GdipBitmapGetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
78 TRACE("%p %d %d %p\n", bitmap
, x
, y
, color
);
81 return InvalidParameter
;
84 FIXME("not implemented\n");
88 return NotImplemented
;
91 /* This function returns a pointer to an array of pixels that represents the
92 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
93 * flags. It is correct behavior that a user who calls this function with write
94 * privileges can write to the whole bitmap (not just the area in rect).
96 * FIXME: only used portion of format is bits per pixel. */
97 GpStatus WINGDIPAPI
GdipBitmapLockBits(GpBitmap
* bitmap
, GDIPCONST GpRect
* rect
,
98 UINT flags
, PixelFormat format
, BitmapData
* lockeddata
)
101 INT stride
, bitspp
= PIXELFORMATBPP(format
);
109 TRACE("%p %p %d %d %p\n", bitmap
, rect
, flags
, format
, lockeddata
);
111 if(!lockeddata
|| !bitmap
|| !rect
)
112 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
;
118 if(flags
& ImageLockModeUserInputBuf
)
119 return NotImplemented
;
121 if((bitmap
->lockmode
& ImageLockModeWrite
) || (bitmap
->lockmode
&&
122 (flags
& ImageLockModeWrite
)))
125 IPicture_get_Handle(bitmap
->image
.picture
, &hbm
);
126 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
127 bm_is_selected
= (hdc
!= 0);
129 bmi
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
130 bmi
.bmiHeader
.biBitCount
= 0;
133 hdc
= CreateCompatibleDC(0);
134 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
138 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
140 abs_height
= abs(bmi
.bmiHeader
.biHeight
);
141 stride
= bmi
.bmiHeader
.biWidth
* bitspp
/ 8;
142 stride
= (stride
+ 3) & ~3;
144 buff
= GdipAlloc(stride
* abs_height
);
145 if(!buff
) return OutOfMemory
;
147 bmi
.bmiHeader
.biBitCount
= bitspp
;
148 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, abs_height
, buff
, &bmi
, DIB_RGB_COLORS
);
151 SelectObject(hdc
, old
);
155 lockeddata
->Width
= rect
->Width
;
156 lockeddata
->Height
= rect
->Height
;
157 lockeddata
->PixelFormat
= format
;
158 lockeddata
->Reserved
= flags
;
160 if(bmi
.bmiHeader
.biHeight
> 0){
161 lockeddata
->Stride
= -stride
;
162 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * rect
->X
+
163 stride
* (abs_height
- 1 - rect
->Y
);
166 lockeddata
->Stride
= stride
;
167 lockeddata
->Scan0
= buff
+ (bitspp
/ 8) * rect
->X
+ stride
* rect
->Y
;
170 bitmap
->lockmode
= flags
;
173 if(flags
& ImageLockModeWrite
)
174 bitmap
->bitmapbits
= buff
;
179 GpStatus WINGDIPAPI
GdipBitmapUnlockBits(GpBitmap
* bitmap
,
180 BitmapData
* lockeddata
)
188 if(!bitmap
|| !lockeddata
)
189 return InvalidParameter
;
191 if(!bitmap
->lockmode
)
194 if(lockeddata
->Reserved
& ImageLockModeUserInputBuf
)
195 return NotImplemented
;
197 if(lockeddata
->Reserved
& ImageLockModeRead
){
198 if(!(--bitmap
->numlocks
))
199 bitmap
->lockmode
= 0;
201 GdipFree(lockeddata
->Scan0
);
205 IPicture_get_Handle(bitmap
->image
.picture
, &hbm
);
206 IPicture_get_CurDC(bitmap
->image
.picture
, &hdc
);
207 bm_is_selected
= (hdc
!= 0);
209 bmi
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
210 bmi
.bmiHeader
.biBitCount
= 0;
213 hdc
= CreateCompatibleDC(0);
214 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
217 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
218 bmi
.bmiHeader
.biBitCount
= PIXELFORMATBPP(lockeddata
->PixelFormat
);
219 SetDIBits(hdc
, (HBITMAP
)hbm
, 0, abs(bmi
.bmiHeader
.biHeight
),
220 bitmap
->bitmapbits
, &bmi
, DIB_RGB_COLORS
);
223 SelectObject(hdc
, old
);
227 GdipFree(bitmap
->bitmapbits
);
232 GpStatus WINGDIPAPI
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
238 if(!filename
|| !bitmap
)
239 return InvalidParameter
;
241 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
246 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
249 IStream_Release(stream
);
254 /* FIXME: this should create a bitmap in the given size with the attributes
255 * (resolution etc.) of the graphics object */
256 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
257 GpGraphics
* target
, GpBitmap
** bitmap
)
262 if(!target
|| !bitmap
)
263 return InvalidParameter
;
266 FIXME("hacked stub\n");
268 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat24bppRGB
,
274 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
275 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
277 BITMAPFILEHEADER
*bmfh
;
278 BITMAPINFOHEADER
*bmih
;
283 TRACE("%d %d %d %d %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
285 if(!bitmap
|| width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
287 return InvalidParameter
;
291 return InvalidParameter
;
293 /* FIXME: windows allows negative stride (reads backwards from scan0) */
295 FIXME("negative stride\n");
296 return InvalidParameter
;
299 *bitmap
= GdipAlloc(sizeof(GpBitmap
));
300 if(!*bitmap
) return OutOfMemory
;
303 stride
= width
* (PIXELFORMATBPP(format
) / 8);
304 stride
= (stride
+ 3) & ~3;
307 datalen
= abs(stride
* height
);
308 size
= sizeof(BITMAPFILEHEADER
) + sizeof(BITMAPINFOHEADER
) + datalen
;
309 buff
= GdipAlloc(size
);
315 bmfh
= (BITMAPFILEHEADER
*) buff
;
316 bmih
= (BITMAPINFOHEADER
*) (bmfh
+ 1);
318 bmfh
->bfType
= (((WORD
)'M') << 8) + (WORD
)'B';
320 bmfh
->bfOffBits
= size
- datalen
;
322 bmih
->biSize
= sizeof(BITMAPINFOHEADER
);
323 bmih
->biWidth
= width
;
324 bmih
->biHeight
= -height
;
325 /* FIXME: use the rest of the data from format */
326 bmih
->biBitCount
= PIXELFORMATBPP(format
);
327 bmih
->biCompression
= BI_RGB
;
328 bmih
->biSizeImage
= datalen
;
331 memcpy(bmih
+ 1, scan0
, datalen
);
333 memset(bmih
+ 1, 0, datalen
);
335 if(CreateStreamOnHGlobal(buff
, TRUE
, &stream
) != S_OK
){
336 ERR("could not make stream\n");
342 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
343 (LPVOID
*) &((*bitmap
)->image
.picture
)) != S_OK
){
344 TRACE("Could not load picture\n");
345 IStream_Release(stream
);
351 (*bitmap
)->image
.type
= ImageTypeBitmap
;
352 (*bitmap
)->width
= width
;
353 (*bitmap
)->height
= height
;
354 (*bitmap
)->format
= format
;
359 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
364 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
369 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
370 IPicture_Release((*bitmap
)->image
.picture
);
372 return GenericError
; /* FIXME: what error to return? */
379 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
382 return GdipCreateBitmapFromStream(stream
, bitmap
);
385 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
390 return InvalidParameter
;
392 IPicture_get_CurDC(image
->picture
, &hdc
);
394 IPicture_Release(image
->picture
);
400 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
403 return InvalidParameter
;
405 return NotImplemented
;
408 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
411 if(!image
|| !srcRect
|| !srcUnit
)
412 return InvalidParameter
;
413 if(image
->type
== ImageTypeMetafile
){
414 memcpy(srcRect
, &((GpMetafile
*)image
)->bounds
, sizeof(GpRectF
));
415 *srcUnit
= ((GpMetafile
*)image
)->unit
;
417 else if(image
->type
== ImageTypeBitmap
){
418 srcRect
->X
= srcRect
->Y
= 0.0;
419 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
420 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
421 *srcUnit
= UnitPixel
;
424 srcRect
->X
= srcRect
->Y
= 0.0;
425 srcRect
->Width
= ipicture_pixel_width(image
->picture
);
426 srcRect
->Height
= ipicture_pixel_height(image
->picture
);
427 *srcUnit
= UnitPixel
;
430 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
431 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
436 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
437 GpGraphics
**graphics
)
441 if(!image
|| !graphics
)
442 return InvalidParameter
;
444 if(image
->type
!= ImageTypeBitmap
){
445 FIXME("not implemented for image type %d\n", image
->type
);
446 return NotImplemented
;
449 IPicture_get_CurDC(image
->picture
, &hdc
);
452 hdc
= CreateCompatibleDC(0);
453 IPicture_SelectPicture(image
->picture
, hdc
, NULL
, NULL
);
456 return GdipCreateFromHDC(hdc
, graphics
);
459 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
461 if(!image
|| !height
)
462 return InvalidParameter
;
464 if(image
->type
== ImageTypeMetafile
){
467 *height
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
468 ((GpMetafile
*)image
)->bounds
.Height
);
472 else if(image
->type
== ImageTypeBitmap
)
473 *height
= ((GpBitmap
*)image
)->height
;
475 *height
= ipicture_pixel_height(image
->picture
);
477 TRACE("returning %d\n", *height
);
482 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
487 return InvalidParameter
;
490 FIXME("not implemented\n");
492 return NotImplemented
;
495 /* FIXME: test this function for non-bitmap types */
496 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
498 if(!image
|| !format
)
499 return InvalidParameter
;
501 if(image
->type
!= ImageTypeBitmap
)
502 *format
= PixelFormat24bppRGB
;
504 *format
= ((GpBitmap
*) image
)->format
;
509 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
513 if(!image
|| !format
)
514 return InvalidParameter
;
517 FIXME("not implemented\n");
519 return NotImplemented
;
522 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
525 return InvalidParameter
;
532 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
537 return InvalidParameter
;
540 FIXME("not implemented\n");
542 return NotImplemented
;
545 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
548 return InvalidParameter
;
550 if(image
->type
== ImageTypeMetafile
){
553 *width
= roundr(convert_unit(hdc
, ((GpMetafile
*)image
)->unit
) *
554 ((GpMetafile
*)image
)->bounds
.Width
);
558 else if(image
->type
== ImageTypeBitmap
)
559 *width
= ((GpBitmap
*)image
)->width
;
561 *width
= ipicture_pixel_width(image
->picture
);
563 TRACE("returning %d\n", *width
);
568 GpStatus WINGDIPAPI
GdipGetMetafileHeaderFromMetafile(GpMetafile
* metafile
,
569 MetafileHeader
* header
)
573 if(!metafile
|| !header
)
574 return InvalidParameter
;
577 FIXME("not implemented\n");
582 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID pid
,
587 TRACE("%p %x %p\n", image
, pid
, size
);
590 return InvalidParameter
;
593 FIXME("not implemented\n");
595 return NotImplemented
;
598 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
599 GDIPCONST GUID
* dimensionID
, UINT
* count
)
603 if(!image
|| !dimensionID
|| !count
)
604 return InvalidParameter
;
607 FIXME("not implemented\n");
609 return NotImplemented
;
612 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
613 GUID
* dimensionIDs
, UINT count
)
617 if(!image
|| !dimensionIDs
)
618 return InvalidParameter
;
621 FIXME("not implemented\n");
626 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
,
627 GDIPCONST GUID
* dimensionID
, UINT frameidx
)
631 if(!image
|| !dimensionID
)
632 return InvalidParameter
;
635 FIXME("not implemented\n");
640 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
* stream
, GpImage
**image
)
645 if(!stream
|| !image
)
646 return InvalidParameter
;
648 if(OleLoadPicture(stream
, 0, FALSE
, &IID_IPicture
,
649 (LPVOID
*) &pic
) != S_OK
){
650 TRACE("Could not load picture\n");
654 IStream_AddRef(stream
);
656 IPicture_get_Type(pic
, &type
);
658 if(type
== PICTYPE_BITMAP
){
660 BITMAPCOREHEADER
* bmch
;
664 *image
= GdipAlloc(sizeof(GpBitmap
));
665 if(!*image
) return OutOfMemory
;
666 (*image
)->type
= ImageTypeBitmap
;
668 (*((GpBitmap
**) image
))->width
= ipicture_pixel_width(pic
);
669 (*((GpBitmap
**) image
))->height
= ipicture_pixel_height(pic
);
671 /* get the pixel format */
672 IPicture_get_Handle(pic
, &hbm
);
673 IPicture_get_CurDC(pic
, &hdc
);
675 bmch
= (BITMAPCOREHEADER
*) (&bmi
.bmiHeader
);
676 bmch
->bcSize
= sizeof(BITMAPCOREHEADER
);
680 hdc
= CreateCompatibleDC(0);
681 old
= SelectObject(hdc
, (HBITMAP
)hbm
);
682 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
683 SelectObject(hdc
, old
);
687 GetDIBits(hdc
, (HBITMAP
)hbm
, 0, 0, NULL
, &bmi
, DIB_RGB_COLORS
);
689 (*((GpBitmap
**) image
))->format
= (bmch
->bcBitCount
<< 8) | PixelFormatGDI
;
691 else if(type
== PICTYPE_METAFILE
|| type
== PICTYPE_ENHMETAFILE
){
692 /* FIXME: missing initialization code */
693 *image
= GdipAlloc(sizeof(GpMetafile
));
694 if(!*image
) return OutOfMemory
;
695 (*image
)->type
= ImageTypeMetafile
;
698 *image
= GdipAlloc(sizeof(GpImage
));
699 if(!*image
) return OutOfMemory
;
700 (*image
)->type
= ImageTypeUnknown
;
703 (*image
)->picture
= pic
;
709 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
711 return GdipLoadImageFromStream(stream
, image
);
714 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
719 return InvalidParameter
;
722 FIXME("not implemented\n");
724 return NotImplemented
;
727 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
728 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
730 if(!image
|| !stream
)
731 return InvalidParameter
;
733 /* FIXME: CLSID, EncoderParameters not used */
735 IPicture_SaveAsFile(image
->picture
, stream
, FALSE
, NULL
);
740 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
741 GDIPCONST ColorPalette
*palette
)
745 if(!image
|| !palette
)
746 return InvalidParameter
;
749 FIXME("not implemented\n");
751 return NotImplemented
;