gdiplus: Replace GetDC(0) with CreateCompatibleDC(0).
[wine.git] / dlls / gdiplus / image.c
bloba6cf6719e94e1955c6baa2cb7d69861c6664b5d7
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 = CreateCompatibleDC(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 DeleteDC(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 = 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);
222 if(!bm_is_selected){
223 SelectObject(hdc, old);
224 DeleteDC(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 /* 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)
259 static int calls;
260 GpStatus ret;
262 if(!target || !bitmap)
263 return InvalidParameter;
265 if(!(calls++))
266 FIXME("hacked stub\n");
268 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
269 NULL, bitmap);
271 return ret;
274 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
275 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
277 BITMAPFILEHEADER *bmfh;
278 BITMAPINFOHEADER *bmih;
279 BYTE *buff;
280 INT datalen, size;
281 IStream *stream;
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))){
286 *bitmap = NULL;
287 return InvalidParameter;
290 if(scan0 && !stride)
291 return InvalidParameter;
293 /* FIXME: windows allows negative stride (reads backwards from scan0) */
294 if(stride < 0){
295 FIXME("negative stride\n");
296 return InvalidParameter;
299 *bitmap = GdipAlloc(sizeof(GpBitmap));
300 if(!*bitmap) return OutOfMemory;
302 if(stride == 0){
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);
310 if(!buff){
311 GdipFree(*bitmap);
312 return OutOfMemory;
315 bmfh = (BITMAPFILEHEADER*) buff;
316 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
318 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
319 bmfh->bfSize = size;
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;
330 if(scan0)
331 memcpy(bmih + 1, scan0, datalen);
332 else
333 memset(bmih + 1, 0, datalen);
335 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
336 ERR("could not make stream\n");
337 GdipFree(*bitmap);
338 GdipFree(buff);
339 return GenericError;
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);
346 GdipFree(*bitmap);
347 GdipFree(buff);
348 return GenericError;
351 (*bitmap)->image.type = ImageTypeBitmap;
352 (*bitmap)->width = width;
353 (*bitmap)->height = height;
354 (*bitmap)->format = format;
356 return Ok;
359 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
360 GpBitmap **bitmap)
362 GpStatus stat;
364 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
366 if(stat != Ok)
367 return stat;
369 if((*bitmap)->image.type != ImageTypeBitmap){
370 IPicture_Release((*bitmap)->image.picture);
371 GdipFree(bitmap);
372 return GenericError; /* FIXME: what error to return? */
375 return Ok;
378 /* FIXME: no icm */
379 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
380 GpBitmap **bitmap)
382 return GdipCreateBitmapFromStream(stream, bitmap);
385 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
387 HDC hdc;
389 if(!image)
390 return InvalidParameter;
392 IPicture_get_CurDC(image->picture, &hdc);
393 DeleteDC(hdc);
394 IPicture_Release(image->picture);
395 GdipFree(image);
397 return Ok;
400 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
402 if(!image || !item)
403 return InvalidParameter;
405 return NotImplemented;
408 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
409 GpUnit *srcUnit)
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;
423 else{
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);
433 return Ok;
436 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
437 GpGraphics **graphics)
439 HDC hdc;
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);
451 if(!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){
465 HDC hdc = GetDC(0);
467 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
468 ((GpMetafile*)image)->bounds.Height);
470 ReleaseDC(0, hdc);
472 else if(image->type == ImageTypeBitmap)
473 *height = ((GpBitmap*)image)->height;
474 else
475 *height = ipicture_pixel_height(image->picture);
477 TRACE("returning %d\n", *height);
479 return Ok;
482 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
484 static int calls;
486 if(!image || !res)
487 return InvalidParameter;
489 if(!(calls++))
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;
503 else
504 *format = ((GpBitmap*) image)->format;
506 return Ok;
509 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
511 static int calls;
513 if(!image || !format)
514 return InvalidParameter;
516 if(!(calls++))
517 FIXME("not implemented\n");
519 return NotImplemented;
522 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
524 if(!image || !type)
525 return InvalidParameter;
527 *type = image->type;
529 return Ok;
532 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
534 static int calls;
536 if(!image || !res)
537 return InvalidParameter;
539 if(!(calls++))
540 FIXME("not implemented\n");
542 return NotImplemented;
545 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
547 if(!image || !width)
548 return InvalidParameter;
550 if(image->type == ImageTypeMetafile){
551 HDC hdc = GetDC(0);
553 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
554 ((GpMetafile*)image)->bounds.Width);
556 ReleaseDC(0, hdc);
558 else if(image->type == ImageTypeBitmap)
559 *width = ((GpBitmap*)image)->width;
560 else
561 *width = ipicture_pixel_width(image->picture);
563 TRACE("returning %d\n", *width);
565 return Ok;
568 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
569 MetafileHeader * header)
571 static int calls;
573 if(!metafile || !header)
574 return InvalidParameter;
576 if(!(calls++))
577 FIXME("not implemented\n");
579 return Ok;
582 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
583 UINT* size)
585 static int calls;
587 TRACE("%p %x %p\n", image, pid, size);
589 if(!size || !image)
590 return InvalidParameter;
592 if(!(calls++))
593 FIXME("not implemented\n");
595 return NotImplemented;
598 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
599 GDIPCONST GUID* dimensionID, UINT* count)
601 static int calls;
603 if(!image || !dimensionID || !count)
604 return InvalidParameter;
606 if(!(calls++))
607 FIXME("not implemented\n");
609 return NotImplemented;
612 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
613 GUID* dimensionIDs, UINT count)
615 static int calls;
617 if(!image || !dimensionIDs)
618 return InvalidParameter;
620 if(!(calls++))
621 FIXME("not implemented\n");
623 return Ok;
626 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
627 GDIPCONST GUID* dimensionID, UINT frameidx)
629 static int calls;
631 if(!image || !dimensionID)
632 return InvalidParameter;
634 if(!(calls++))
635 FIXME("not implemented\n");
637 return Ok;
640 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
642 IPicture *pic;
643 short type;
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");
651 return GenericError;
654 IStream_AddRef(stream);
656 IPicture_get_Type(pic, &type);
658 if(type == PICTYPE_BITMAP){
659 BITMAPINFO bmi;
660 BITMAPCOREHEADER* bmch;
661 OLE_HANDLE hbm;
662 HDC hdc;
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);
678 if(!hdc){
679 HBITMAP old;
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);
684 DeleteDC(hdc);
686 else
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;
697 else{
698 *image = GdipAlloc(sizeof(GpImage));
699 if(!*image) return OutOfMemory;
700 (*image)->type = ImageTypeUnknown;
703 (*image)->picture = pic;
705 return Ok;
708 /* FIXME: no ICM */
709 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
711 return GdipLoadImageFromStream(stream, image);
714 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
716 static int calls;
718 if(!image)
719 return InvalidParameter;
721 if(!(calls++))
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);
737 return Ok;
740 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
741 GDIPCONST ColorPalette *palette)
743 static int calls;
745 if(!image || !palette)
746 return InvalidParameter;
748 if(!(calls++))
749 FIXME("not implemented\n");
751 return NotImplemented;