gdiplus: Added GdipCreateBitmapFromFile.
[wine/wine64.git] / dlls / gdiplus / image.c
blobda816b85a7b9105f0c34cc830420125ac37659e0
1 /*
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
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "wingdi.h"
26 #define COBJMACROS
27 #include "objbase.h"
28 #include "olectl.h"
29 #include "ole2.h"
31 #include "gdiplus.h"
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)
43 HDC hdcref;
44 OLE_YSIZE_HIMETRIC y;
46 IPicture_get_Height(pic, &y);
48 hdcref = GetDC(0);
50 y = (UINT)(((REAL)y) * ((REAL)GetDeviceCaps(hdcref, LOGPIXELSY)) /
51 ((REAL)INCH_HIMETRIC));
52 ReleaseDC(0, hdcref);
54 return y;
57 static INT ipicture_pixel_width(IPicture *pic)
59 HDC hdcref;
60 OLE_XSIZE_HIMETRIC x;
62 IPicture_get_Width(pic, &x);
64 hdcref = GetDC(0);
66 x = (UINT)(((REAL)x) * ((REAL)GetDeviceCaps(hdcref, LOGPIXELSX)) /
67 ((REAL)INCH_HIMETRIC));
69 ReleaseDC(0, hdcref);
71 return x;
74 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
75 ARGB *color)
77 static int calls;
78 TRACE("%p %d %d %p\n", bitmap, x, y, color);
80 if(!bitmap || !color)
81 return InvalidParameter;
83 if(!(calls++))
84 FIXME("not implemented\n");
86 *color = 0xdeadbeef;
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)
100 BOOL bm_is_selected;
101 INT stride, bitspp = PIXELFORMATBPP(format);
102 OLE_HANDLE hbm;
103 HDC hdc;
104 HBITMAP old = NULL;
105 BITMAPINFO bmi;
106 BYTE *buff = NULL;
107 UINT abs_height;
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)))
123 return WrongState;
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;
132 if(!bm_is_selected){
133 hdc = GetDC(0);
134 old = SelectObject(hdc, (HBITMAP)hbm);
137 /* fill out bmi */
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);
150 if(!bm_is_selected){
151 SelectObject(hdc, old);
152 ReleaseDC(0, hdc);
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);
165 else{
166 lockeddata->Stride = stride;
167 lockeddata->Scan0 = buff + (bitspp / 8) * rect->X + stride * rect->Y;
170 bitmap->lockmode = flags;
171 bitmap->numlocks++;
173 if(flags & ImageLockModeWrite)
174 bitmap->bitmapbits = buff;
176 return Ok;
179 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
180 BitmapData* lockeddata)
182 OLE_HANDLE hbm;
183 HDC hdc;
184 HBITMAP old = NULL;
185 BOOL bm_is_selected;
186 BITMAPINFO bmi;
188 if(!bitmap || !lockeddata)
189 return InvalidParameter;
191 if(!bitmap->lockmode)
192 return WrongState;
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);
202 return Ok;
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;
212 if(!bm_is_selected){
213 hdc = GetDC(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);
222 if(!bm_is_selected){
223 SelectObject(hdc, old);
224 ReleaseDC(0, hdc);
227 GdipFree(bitmap->bitmapbits);
229 return Ok;
232 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
233 GpBitmap **bitmap)
235 GpStatus stat;
236 IStream *stream;
238 if(!filename || !bitmap)
239 return InvalidParameter;
241 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
243 if(stat != Ok)
244 return stat;
246 stat = GdipCreateBitmapFromStream(stream, bitmap);
248 if(!stat)
249 IStream_Release(stream);
251 return stat;
254 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
255 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
257 BITMAPFILEHEADER *bmfh;
258 BITMAPINFOHEADER *bmih;
259 BYTE *buff;
260 INT datalen = stride * height, size;
261 IStream *stream;
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);
273 if(!buff){
274 GdipFree(*bitmap);
275 return OutOfMemory;
278 bmfh = (BITMAPFILEHEADER*) buff;
279 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
281 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
282 bmfh->bfSize = size;
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");
296 GdipFree(*bitmap);
297 GdipFree(buff);
298 return GenericError;
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);
305 GdipFree(*bitmap);
306 GdipFree(buff);
307 return GenericError;
310 (*bitmap)->image.type = ImageTypeBitmap;
311 (*bitmap)->width = width;
312 (*bitmap)->height = height;
313 (*bitmap)->format = format;
315 return Ok;
318 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
319 GpBitmap **bitmap)
321 BITMAPINFO bmi;
322 BITMAPCOREHEADER* bmch;
323 OLE_HANDLE hbm;
324 HDC hdc;
325 GpStatus stat;
327 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
329 if(stat != Ok)
330 return stat;
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);
344 if(!hdc){
345 HBITMAP old;
346 hdc = GetDC(0);
347 old = SelectObject(hdc, (HBITMAP)hbm);
348 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
349 SelectObject(hdc, old);
350 ReleaseDC(0, hdc);
352 else
353 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
355 (*bitmap)->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
357 return Ok;
360 /* FIXME: no icm */
361 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
362 GpBitmap **bitmap)
364 return GdipCreateBitmapFromStream(stream, bitmap);
367 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
369 if(!image)
370 return InvalidParameter;
372 IPicture_Release(image->picture);
373 GdipFree(image);
375 return Ok;
378 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
380 if(!image || !item)
381 return InvalidParameter;
383 return NotImplemented;
386 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
387 GpUnit *srcUnit)
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;
401 else{
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);
411 return Ok;
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;
425 else
426 *height = ipicture_pixel_height(image->picture);
428 TRACE("returning %d\n", *height);
430 return Ok;
433 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
435 static int calls;
437 if(!image || !res)
438 return InvalidParameter;
440 if(!(calls++))
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;
454 else
455 *format = ((GpBitmap*) image)->format;
457 return Ok;
460 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
462 static int calls;
464 if(!image || !format)
465 return InvalidParameter;
467 if(!(calls++))
468 FIXME("not implemented\n");
470 return NotImplemented;
473 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
475 if(!image || !type)
476 return InvalidParameter;
478 *type = image->type;
480 return Ok;
483 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
485 static int calls;
487 if(!image || !res)
488 return InvalidParameter;
490 if(!(calls++))
491 FIXME("not implemented\n");
493 return NotImplemented;
496 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
498 if(!image || !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;
507 else
508 *width = ipicture_pixel_width(image->picture);
510 TRACE("returning %d\n", *width);
512 return Ok;
515 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
516 MetafileHeader * header)
518 static int calls;
520 if(!metafile || !header)
521 return InvalidParameter;
523 if(!(calls++))
524 FIXME("not implemented\n");
526 return NotImplemented;
529 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
530 UINT* size)
532 static int calls;
534 TRACE("%p %x %p\n", image, pid, size);
536 if(!size || !image)
537 return InvalidParameter;
539 if(!(calls++))
540 FIXME("not implemented\n");
542 return NotImplemented;
545 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
546 GDIPCONST GUID* dimensionID, UINT* count)
548 static int calls;
550 if(!image || !dimensionID || !count)
551 return InvalidParameter;
553 if(!(calls++))
554 FIXME("not implemented\n");
556 return NotImplemented;
559 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
560 GUID* dimensionIDs, UINT count)
562 static int calls;
564 if(!image || !dimensionIDs)
565 return InvalidParameter;
567 if(!(calls++))
568 FIXME("not implemented\n");
570 return Ok;
573 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
574 GDIPCONST GUID* dimensionID, UINT frameidx)
576 static int calls;
578 if(!image || !dimensionID)
579 return InvalidParameter;
581 if(!(calls++))
582 FIXME("not implemented\n");
584 return Ok;
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");
598 GdipFree(*image);
599 return GenericError;
602 /* FIXME: use IPicture_get_Type to get image type? */
603 (*image)->type = ImageTypeUnknown;
605 return Ok;
608 /* FIXME: no ICM */
609 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
611 return GdipLoadImageFromStream(stream, image);
614 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
616 static int calls;
618 if(!image)
619 return InvalidParameter;
621 if(!(calls++))
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);
637 return Ok;