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;
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;
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 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
255 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
257 BITMAPFILEHEADER *bmfh;
258 BITMAPINFOHEADER *bmih;
260 INT datalen = stride * height, size;
263 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
265 if(!scan0 || !bitmap)
266 return InvalidParameter;
268 *bitmap = GdipAlloc(sizeof(GpBitmap));
269 if(!*bitmap) return OutOfMemory;
271 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
272 buff = GdipAlloc(size);
278 bmfh = (BITMAPFILEHEADER*) buff;
279 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
281 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
283 bmfh->bfOffBits = size - datalen;
285 bmih->biSize = sizeof(BITMAPINFOHEADER);
286 bmih->biWidth = width;
287 bmih->biHeight = height;
288 /* FIXME: use the rest of the data from format */
289 bmih->biBitCount = PIXELFORMATBPP(format);
290 bmih->biCompression = BI_RGB;
292 memcpy(bmih + 1, scan0, datalen);
294 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
295 ERR("could not make stream\n");
301 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
302 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
303 TRACE("Could not load picture\n");
304 IStream_Release(stream);
310 (*bitmap)->image.type = ImageTypeBitmap;
311 (*bitmap)->width = width;
312 (*bitmap)->height = height;
313 (*bitmap)->format = format;
318 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
322 BITMAPCOREHEADER* bmch;
327 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
332 /* FIXME: make sure it's actually a bitmap */
333 (*bitmap)->image.type = ImageTypeBitmap;
334 (*bitmap)->width = ipicture_pixel_width((*bitmap)->image.picture);
335 (*bitmap)->height = ipicture_pixel_height((*bitmap)->image.picture);
337 /* get the pixel format */
338 IPicture_get_Handle((*bitmap)->image.picture, &hbm);
339 IPicture_get_CurDC((*bitmap)->image.picture, &hdc);
341 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
342 bmch->bcSize = sizeof(BITMAPCOREHEADER);
347 old = SelectObject(hdc, (HBITMAP)hbm);
348 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
349 SelectObject(hdc, old);
353 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
355 (*bitmap)->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
361 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
364 return GdipCreateBitmapFromStream(stream, bitmap);
367 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
370 return InvalidParameter;
372 IPicture_Release(image->picture);
378 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
381 return InvalidParameter;
383 return NotImplemented;
386 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
389 if(!image || !srcRect || !srcUnit)
390 return InvalidParameter;
391 if(image->type == ImageTypeMetafile){
392 memcpy(srcRect, &((GpMetafile*)image)->bounds, sizeof(GpRectF));
393 *srcUnit = ((GpMetafile*)image)->unit;
395 else if(image->type == ImageTypeBitmap){
396 srcRect->X = srcRect->Y = 0.0;
397 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
398 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
399 *srcUnit = UnitPixel;
402 srcRect->X = srcRect->Y = 0.0;
403 srcRect->Width = ipicture_pixel_width(image->picture);
404 srcRect->Height = ipicture_pixel_height(image->picture);
405 *srcUnit = UnitPixel;
408 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
409 srcRect->Width, srcRect->Height, *srcUnit);
414 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
416 if(!image || !height)
417 return InvalidParameter;
419 if(image->type == ImageTypeMetafile){
420 FIXME("not implemented for metafiles\n");
421 return NotImplemented;
423 else if(image->type == ImageTypeBitmap)
424 *height = ((GpBitmap*)image)->height;
426 *height = ipicture_pixel_height(image->picture);
428 TRACE("returning %d\n", *height);
433 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
438 return InvalidParameter;
441 FIXME("not implemented\n");
443 return NotImplemented;
446 /* FIXME: test this function for non-bitmap types */
447 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
449 if(!image || !format)
450 return InvalidParameter;
452 if(image->type != ImageTypeBitmap)
453 *format = PixelFormat24bppRGB;
455 *format = ((GpBitmap*) image)->format;
460 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
464 if(!image || !format)
465 return InvalidParameter;
468 FIXME("not implemented\n");
470 return NotImplemented;
473 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
476 return InvalidParameter;
483 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
488 return InvalidParameter;
491 FIXME("not implemented\n");
493 return NotImplemented;
496 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
499 return InvalidParameter;
501 if(image->type == ImageTypeMetafile){
502 FIXME("not implemented for metafiles\n");
503 return NotImplemented;
505 else if(image->type == ImageTypeBitmap)
506 *width = ((GpBitmap*)image)->width;
508 *width = ipicture_pixel_width(image->picture);
510 TRACE("returning %d\n", *width);
515 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
516 MetafileHeader * header)
520 if(!metafile || !header)
521 return InvalidParameter;
524 FIXME("not implemented\n");
526 return NotImplemented;
529 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
534 TRACE("%p %x %p\n", image, pid, size);
537 return InvalidParameter;
540 FIXME("not implemented\n");
542 return NotImplemented;
545 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
546 GDIPCONST GUID* dimensionID, UINT* count)
550 if(!image || !dimensionID || !count)
551 return InvalidParameter;
554 FIXME("not implemented\n");
556 return NotImplemented;
559 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
560 GUID* dimensionIDs, UINT count)
564 if(!image || !dimensionIDs)
565 return InvalidParameter;
568 FIXME("not implemented\n");
573 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
574 GDIPCONST GUID* dimensionID, UINT frameidx)
578 if(!image || !dimensionID)
579 return InvalidParameter;
582 FIXME("not implemented\n");
587 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
589 if(!stream || !image)
590 return InvalidParameter;
592 *image = GdipAlloc(sizeof(GpImage));
593 if(!*image) return OutOfMemory;
595 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
596 (LPVOID*) &((*image)->picture)) != S_OK){
597 TRACE("Could not load picture\n");
602 /* FIXME: use IPicture_get_Type to get image type? */
603 (*image)->type = ImageTypeUnknown;
609 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
611 return GdipLoadImageFromStream(stream, image);
614 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
619 return InvalidParameter;
622 FIXME("not implemented\n");
624 return NotImplemented;
627 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
628 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
630 if(!image || !stream)
631 return InvalidParameter;
633 /* FIXME: CLSID, EncoderParameters not used */
635 IPicture_SaveAsFile(image->picture, stream, FALSE, NULL);