riched20: Fix the name of the lpUsedDefChar field of GETTEXTEX.
[wine/hacks.git] / dlls / gdiplus / image.c
blobb08c5e50b4bfa131a083799d4c30b3885d8e6a21
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 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
39 static INT ipicture_pixel_height(IPicture *pic)
41 HDC hdcref;
42 OLE_YSIZE_HIMETRIC y;
44 IPicture_get_Height(pic, &y);
46 hdcref = GetDC(0);
48 y = (UINT)(((REAL)y) * ((REAL)GetDeviceCaps(hdcref, LOGPIXELSY)) /
49 ((REAL)INCH_HIMETRIC));
50 ReleaseDC(0, hdcref);
52 return y;
55 static INT ipicture_pixel_width(IPicture *pic)
57 HDC hdcref;
58 OLE_XSIZE_HIMETRIC x;
60 IPicture_get_Width(pic, &x);
62 hdcref = GetDC(0);
64 x = (UINT)(((REAL)x) * ((REAL)GetDeviceCaps(hdcref, LOGPIXELSX)) /
65 ((REAL)INCH_HIMETRIC));
67 ReleaseDC(0, hdcref);
69 return x;
72 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
73 ARGB *color)
75 static int calls;
76 TRACE("%p %d %d %p\n", bitmap, x, y, color);
78 if(!bitmap || !color)
79 return InvalidParameter;
81 if(!(calls++))
82 FIXME("not implemented\n");
84 *color = 0xdeadbeef;
86 return NotImplemented;
89 /* This function returns a pointer to an array of pixels that represents the
90 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
91 * flags. It is correct behavior that a user who calls this function with write
92 * privileges can write to the whole bitmap (not just the area in rect).
94 * FIXME: only used portion of format is bits per pixel. */
95 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
96 UINT flags, PixelFormat format, BitmapData* lockeddata)
98 BOOL bm_is_selected;
99 INT stride, bitspp = PIXELFORMATBPP(format);
100 OLE_HANDLE hbm;
101 HDC hdc;
102 HBITMAP old = NULL;
103 BITMAPINFO bmi;
104 BYTE *buff = NULL;
105 UINT abs_height;
107 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
109 if(!lockeddata || !bitmap || !rect)
110 return InvalidParameter;
112 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
113 (rect->Y + rect->Height > bitmap->height) || !flags)
114 return InvalidParameter;
116 if(flags & ImageLockModeUserInputBuf)
117 return NotImplemented;
119 if((bitmap->lockmode & ImageLockModeWrite) || (bitmap->lockmode &&
120 (flags & ImageLockModeWrite)))
121 return WrongState;
123 IPicture_get_Handle(bitmap->image.picture, &hbm);
124 IPicture_get_CurDC(bitmap->image.picture, &hdc);
125 bm_is_selected = (hdc != 0);
127 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
128 bmi.bmiHeader.biBitCount = 0;
130 if(!bm_is_selected){
131 hdc = CreateCompatibleDC(0);
132 old = SelectObject(hdc, (HBITMAP)hbm);
135 /* fill out bmi */
136 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
138 abs_height = abs(bmi.bmiHeader.biHeight);
139 stride = bmi.bmiHeader.biWidth * bitspp / 8;
140 stride = (stride + 3) & ~3;
142 buff = GdipAlloc(stride * abs_height);
144 bmi.bmiHeader.biBitCount = bitspp;
146 if(buff)
147 GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, &bmi, DIB_RGB_COLORS);
149 if(!bm_is_selected){
150 SelectObject(hdc, old);
151 DeleteDC(hdc);
154 if(!buff)
155 return OutOfMemory;
157 lockeddata->Width = rect->Width;
158 lockeddata->Height = rect->Height;
159 lockeddata->PixelFormat = format;
160 lockeddata->Reserved = flags;
162 if(bmi.bmiHeader.biHeight > 0){
163 lockeddata->Stride = -stride;
164 lockeddata->Scan0 = buff + (bitspp / 8) * rect->X +
165 stride * (abs_height - 1 - rect->Y);
167 else{
168 lockeddata->Stride = stride;
169 lockeddata->Scan0 = buff + (bitspp / 8) * rect->X + stride * rect->Y;
172 bitmap->lockmode = flags;
173 bitmap->numlocks++;
175 if(flags & ImageLockModeWrite)
176 bitmap->bitmapbits = buff;
178 return Ok;
181 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
182 BitmapData* lockeddata)
184 OLE_HANDLE hbm;
185 HDC hdc;
186 HBITMAP old = NULL;
187 BOOL bm_is_selected;
188 BITMAPINFO bmi;
190 if(!bitmap || !lockeddata)
191 return InvalidParameter;
193 if(!bitmap->lockmode)
194 return WrongState;
196 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
197 return NotImplemented;
199 if(lockeddata->Reserved & ImageLockModeRead){
200 if(!(--bitmap->numlocks))
201 bitmap->lockmode = 0;
203 GdipFree(lockeddata->Scan0);
204 return Ok;
207 IPicture_get_Handle(bitmap->image.picture, &hbm);
208 IPicture_get_CurDC(bitmap->image.picture, &hdc);
209 bm_is_selected = (hdc != 0);
211 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
212 bmi.bmiHeader.biBitCount = 0;
214 if(!bm_is_selected){
215 hdc = CreateCompatibleDC(0);
216 old = SelectObject(hdc, (HBITMAP)hbm);
219 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
220 bmi.bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
221 SetDIBits(hdc, (HBITMAP)hbm, 0, abs(bmi.bmiHeader.biHeight),
222 bitmap->bitmapbits, &bmi, DIB_RGB_COLORS);
224 if(!bm_is_selected){
225 SelectObject(hdc, old);
226 DeleteDC(hdc);
229 GdipFree(bitmap->bitmapbits);
231 return Ok;
234 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
235 GpBitmap **bitmap)
237 GpStatus stat;
238 IStream *stream;
240 if(!filename || !bitmap)
241 return InvalidParameter;
243 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
245 if(stat != Ok)
246 return stat;
248 stat = GdipCreateBitmapFromStream(stream, bitmap);
250 if(!stat)
251 IStream_Release(stream);
253 return stat;
256 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
257 HBITMAP* hbmReturn, ARGB background)
259 FIXME("stub\n");
261 hbmReturn = NULL;
263 return NotImplemented;
266 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
267 GpMetafile* metafile, BOOL* succ, EmfType emfType,
268 const WCHAR* description, GpMetafile** out_metafile)
270 static int calls;
272 if(!ref || !metafile || !out_metafile)
273 return InvalidParameter;
275 *succ = FALSE;
276 *out_metafile = NULL;
278 if(!(calls++))
279 FIXME("not implemented\n");
281 return NotImplemented;
284 /* FIXME: this should create a bitmap in the given size with the attributes
285 * (resolution etc.) of the graphics object */
286 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
287 GpGraphics* target, GpBitmap** bitmap)
289 static int calls;
290 GpStatus ret;
292 if(!target || !bitmap)
293 return InvalidParameter;
295 if(!(calls++))
296 FIXME("hacked stub\n");
298 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
299 NULL, bitmap);
301 return ret;
304 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
305 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
307 BITMAPFILEHEADER *bmfh;
308 BITMAPINFOHEADER *bmih;
309 BYTE *buff;
310 INT datalen, size;
311 IStream *stream;
313 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
315 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
316 *bitmap = NULL;
317 return InvalidParameter;
320 if(scan0 && !stride)
321 return InvalidParameter;
323 /* FIXME: windows allows negative stride (reads backwards from scan0) */
324 if(stride < 0){
325 FIXME("negative stride\n");
326 return InvalidParameter;
329 *bitmap = GdipAlloc(sizeof(GpBitmap));
330 if(!*bitmap) return OutOfMemory;
332 if(stride == 0){
333 stride = width * (PIXELFORMATBPP(format) / 8);
334 stride = (stride + 3) & ~3;
337 datalen = abs(stride * height);
338 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
339 buff = GdipAlloc(size);
340 if(!buff){
341 GdipFree(*bitmap);
342 return OutOfMemory;
345 bmfh = (BITMAPFILEHEADER*) buff;
346 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
348 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
349 bmfh->bfSize = size;
350 bmfh->bfOffBits = size - datalen;
352 bmih->biSize = sizeof(BITMAPINFOHEADER);
353 bmih->biWidth = width;
354 bmih->biHeight = -height;
355 /* FIXME: use the rest of the data from format */
356 bmih->biBitCount = PIXELFORMATBPP(format);
357 bmih->biCompression = BI_RGB;
358 bmih->biSizeImage = datalen;
360 if(scan0)
361 memcpy(bmih + 1, scan0, datalen);
362 else
363 memset(bmih + 1, 0, datalen);
365 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
366 ERR("could not make stream\n");
367 GdipFree(*bitmap);
368 GdipFree(buff);
369 return GenericError;
372 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
373 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
374 TRACE("Could not load picture\n");
375 IStream_Release(stream);
376 GdipFree(*bitmap);
377 GdipFree(buff);
378 return GenericError;
381 (*bitmap)->image.type = ImageTypeBitmap;
382 (*bitmap)->width = width;
383 (*bitmap)->height = height;
384 (*bitmap)->format = format;
386 return Ok;
389 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
390 GpBitmap **bitmap)
392 GpStatus stat;
394 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
396 if(stat != Ok)
397 return stat;
399 if((*bitmap)->image.type != ImageTypeBitmap){
400 IPicture_Release((*bitmap)->image.picture);
401 GdipFree(bitmap);
402 return GenericError; /* FIXME: what error to return? */
405 return Ok;
408 /* FIXME: no icm */
409 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
410 GpBitmap **bitmap)
412 return GdipCreateBitmapFromStream(stream, bitmap);
415 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
417 HDC hdc;
419 if(!image)
420 return InvalidParameter;
422 IPicture_get_CurDC(image->picture, &hdc);
423 DeleteDC(hdc);
424 IPicture_Release(image->picture);
425 GdipFree(image);
427 return Ok;
430 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
432 if(!image || !item)
433 return InvalidParameter;
435 return NotImplemented;
438 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
439 GpUnit *srcUnit)
441 if(!image || !srcRect || !srcUnit)
442 return InvalidParameter;
443 if(image->type == ImageTypeMetafile){
444 memcpy(srcRect, &((GpMetafile*)image)->bounds, sizeof(GpRectF));
445 *srcUnit = ((GpMetafile*)image)->unit;
447 else if(image->type == ImageTypeBitmap){
448 srcRect->X = srcRect->Y = 0.0;
449 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
450 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
451 *srcUnit = UnitPixel;
453 else{
454 srcRect->X = srcRect->Y = 0.0;
455 srcRect->Width = ipicture_pixel_width(image->picture);
456 srcRect->Height = ipicture_pixel_height(image->picture);
457 *srcUnit = UnitPixel;
460 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
461 srcRect->Width, srcRect->Height, *srcUnit);
463 return Ok;
466 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
467 GpGraphics **graphics)
469 HDC hdc;
471 if(!image || !graphics)
472 return InvalidParameter;
474 if(image->type != ImageTypeBitmap){
475 FIXME("not implemented for image type %d\n", image->type);
476 return NotImplemented;
479 IPicture_get_CurDC(image->picture, &hdc);
481 if(!hdc){
482 hdc = CreateCompatibleDC(0);
483 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
486 return GdipCreateFromHDC(hdc, graphics);
489 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
491 if(!image || !height)
492 return InvalidParameter;
494 if(image->type == ImageTypeMetafile){
495 HDC hdc = GetDC(0);
497 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
498 ((GpMetafile*)image)->bounds.Height);
500 ReleaseDC(0, hdc);
502 else if(image->type == ImageTypeBitmap)
503 *height = ((GpBitmap*)image)->height;
504 else
505 *height = ipicture_pixel_height(image->picture);
507 TRACE("returning %d\n", *height);
509 return Ok;
512 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
514 static int calls;
516 if(!image || !res)
517 return InvalidParameter;
519 if(!(calls++))
520 FIXME("not implemented\n");
522 return NotImplemented;
525 /* FIXME: test this function for non-bitmap types */
526 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
528 if(!image || !format)
529 return InvalidParameter;
531 if(image->type != ImageTypeBitmap)
532 *format = PixelFormat24bppRGB;
533 else
534 *format = ((GpBitmap*) image)->format;
536 return Ok;
539 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
541 static int calls;
543 if(!image || !format)
544 return InvalidParameter;
546 if(!(calls++))
547 FIXME("not implemented\n");
549 return NotImplemented;
552 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
554 if(!image || !type)
555 return InvalidParameter;
557 *type = image->type;
559 return Ok;
562 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
564 static int calls;
566 if(!image || !res)
567 return InvalidParameter;
569 if(!(calls++))
570 FIXME("not implemented\n");
572 return NotImplemented;
575 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
577 if(!image || !width)
578 return InvalidParameter;
580 if(image->type == ImageTypeMetafile){
581 HDC hdc = GetDC(0);
583 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
584 ((GpMetafile*)image)->bounds.Width);
586 ReleaseDC(0, hdc);
588 else if(image->type == ImageTypeBitmap)
589 *width = ((GpBitmap*)image)->width;
590 else
591 *width = ipicture_pixel_width(image->picture);
593 TRACE("returning %d\n", *width);
595 return Ok;
598 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
599 MetafileHeader * header)
601 static int calls;
603 if(!metafile || !header)
604 return InvalidParameter;
606 if(!(calls++))
607 FIXME("not implemented\n");
609 return Ok;
612 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
613 UINT* size)
615 static int calls;
617 TRACE("%p %x %p\n", image, pid, size);
619 if(!size || !image)
620 return InvalidParameter;
622 if(!(calls++))
623 FIXME("not implemented\n");
625 return NotImplemented;
628 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
629 GDIPCONST GUID* dimensionID, UINT* count)
631 static int calls;
633 if(!image || !dimensionID || !count)
634 return InvalidParameter;
636 if(!(calls++))
637 FIXME("not implemented\n");
639 return NotImplemented;
642 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
643 GUID* dimensionIDs, UINT count)
645 static int calls;
647 if(!image || !dimensionIDs)
648 return InvalidParameter;
650 if(!(calls++))
651 FIXME("not implemented\n");
653 return Ok;
656 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
657 GDIPCONST GUID* dimensionID, UINT frameidx)
659 static int calls;
661 if(!image || !dimensionID)
662 return InvalidParameter;
664 if(!(calls++))
665 FIXME("not implemented\n");
667 return Ok;
670 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
672 IPicture *pic;
673 short type;
675 if(!stream || !image)
676 return InvalidParameter;
678 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
679 (LPVOID*) &pic) != S_OK){
680 TRACE("Could not load picture\n");
681 return GenericError;
684 IStream_AddRef(stream);
686 IPicture_get_Type(pic, &type);
688 if(type == PICTYPE_BITMAP){
689 BITMAPINFO bmi;
690 BITMAPCOREHEADER* bmch;
691 OLE_HANDLE hbm;
692 HDC hdc;
694 *image = GdipAlloc(sizeof(GpBitmap));
695 if(!*image) return OutOfMemory;
696 (*image)->type = ImageTypeBitmap;
698 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
699 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
701 /* get the pixel format */
702 IPicture_get_Handle(pic, &hbm);
703 IPicture_get_CurDC(pic, &hdc);
705 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
706 bmch->bcSize = sizeof(BITMAPCOREHEADER);
708 if(!hdc){
709 HBITMAP old;
710 hdc = CreateCompatibleDC(0);
711 old = SelectObject(hdc, (HBITMAP)hbm);
712 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
713 SelectObject(hdc, old);
714 DeleteDC(hdc);
716 else
717 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
719 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
721 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
722 /* FIXME: missing initialization code */
723 *image = GdipAlloc(sizeof(GpMetafile));
724 if(!*image) return OutOfMemory;
725 (*image)->type = ImageTypeMetafile;
727 else{
728 *image = GdipAlloc(sizeof(GpImage));
729 if(!*image) return OutOfMemory;
730 (*image)->type = ImageTypeUnknown;
733 (*image)->picture = pic;
735 return Ok;
738 /* FIXME: no ICM */
739 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
741 return GdipLoadImageFromStream(stream, image);
744 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
746 static int calls;
748 if(!image)
749 return InvalidParameter;
751 if(!(calls++))
752 FIXME("not implemented\n");
754 return NotImplemented;
757 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
758 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
760 if(!image || !stream)
761 return InvalidParameter;
763 /* FIXME: CLSID, EncoderParameters not used */
765 IPicture_SaveAsFile(image->picture, stream, FALSE, NULL);
767 return Ok;
770 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
771 GDIPCONST ColorPalette *palette)
773 static int calls;
775 if(!image || !palette)
776 return InvalidParameter;
778 if(!(calls++))
779 FIXME("not implemented\n");
781 return NotImplemented;