riched20: Constify some variables.
[wine/wine-gecko.git] / dlls / gdiplus / image.c
blobf6dffcfad805ed96097384667cea875cb043cba3
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 = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
49 ReleaseDC(0, hdcref);
51 return y;
54 static INT ipicture_pixel_width(IPicture *pic)
56 HDC hdcref;
57 OLE_XSIZE_HIMETRIC x;
59 IPicture_get_Width(pic, &x);
61 hdcref = GetDC(0);
63 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
65 ReleaseDC(0, hdcref);
67 return x;
70 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
71 ARGB *color)
73 static int calls;
74 TRACE("%p %d %d %p\n", bitmap, x, y, color);
76 if(!bitmap || !color)
77 return InvalidParameter;
79 if(!(calls++))
80 FIXME("not implemented\n");
82 *color = 0xdeadbeef;
84 return NotImplemented;
87 /* This function returns a pointer to an array of pixels that represents the
88 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
89 * flags. It is correct behavior that a user who calls this function with write
90 * privileges can write to the whole bitmap (not just the area in rect).
92 * FIXME: only used portion of format is bits per pixel. */
93 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
94 UINT flags, PixelFormat format, BitmapData* lockeddata)
96 BOOL bm_is_selected;
97 INT stride, bitspp = PIXELFORMATBPP(format);
98 OLE_HANDLE hbm;
99 HDC hdc;
100 HBITMAP old = NULL;
101 BITMAPINFO bmi;
102 BYTE *buff = NULL;
103 UINT abs_height;
104 GpRect act_rect; /* actual rect to be used */
106 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
108 if(!lockeddata || !bitmap)
109 return InvalidParameter;
111 if(rect){
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 act_rect = *rect;
118 else{
119 act_rect.X = act_rect.Y = 0;
120 act_rect.Width = bitmap->width;
121 act_rect.Height = bitmap->height;
124 if(flags & ImageLockModeUserInputBuf)
125 return NotImplemented;
127 if(bitmap->lockmode)
128 return WrongState;
130 IPicture_get_Handle(bitmap->image.picture, &hbm);
131 IPicture_get_CurDC(bitmap->image.picture, &hdc);
132 bm_is_selected = (hdc != 0);
134 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
135 bmi.bmiHeader.biBitCount = 0;
137 if(!bm_is_selected){
138 hdc = CreateCompatibleDC(0);
139 old = SelectObject(hdc, (HBITMAP)hbm);
142 /* fill out bmi */
143 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
145 abs_height = abs(bmi.bmiHeader.biHeight);
146 stride = bmi.bmiHeader.biWidth * bitspp / 8;
147 stride = (stride + 3) & ~3;
149 buff = GdipAlloc(stride * abs_height);
151 bmi.bmiHeader.biBitCount = bitspp;
153 if(buff)
154 GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, &bmi, DIB_RGB_COLORS);
156 if(!bm_is_selected){
157 SelectObject(hdc, old);
158 DeleteDC(hdc);
161 if(!buff)
162 return OutOfMemory;
164 lockeddata->Width = act_rect.Width;
165 lockeddata->Height = act_rect.Height;
166 lockeddata->PixelFormat = format;
167 lockeddata->Reserved = flags;
169 if(bmi.bmiHeader.biHeight > 0){
170 lockeddata->Stride = -stride;
171 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
172 stride * (abs_height - 1 - act_rect.Y);
174 else{
175 lockeddata->Stride = stride;
176 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
179 bitmap->lockmode = flags;
180 bitmap->numlocks++;
182 bitmap->bitmapbits = buff;
184 return Ok;
187 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
188 BitmapData* lockeddata)
190 OLE_HANDLE hbm;
191 HDC hdc;
192 HBITMAP old = NULL;
193 BOOL bm_is_selected;
194 BITMAPINFO bmi;
196 if(!bitmap || !lockeddata)
197 return InvalidParameter;
199 if(!bitmap->lockmode)
200 return WrongState;
202 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
203 return NotImplemented;
205 if(lockeddata->Reserved & ImageLockModeRead){
206 if(!(--bitmap->numlocks))
207 bitmap->lockmode = 0;
209 GdipFree(bitmap->bitmapbits);
210 bitmap->bitmapbits = NULL;
211 return Ok;
214 IPicture_get_Handle(bitmap->image.picture, &hbm);
215 IPicture_get_CurDC(bitmap->image.picture, &hdc);
216 bm_is_selected = (hdc != 0);
218 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
219 bmi.bmiHeader.biBitCount = 0;
221 if(!bm_is_selected){
222 hdc = CreateCompatibleDC(0);
223 old = SelectObject(hdc, (HBITMAP)hbm);
226 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
227 bmi.bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
228 SetDIBits(hdc, (HBITMAP)hbm, 0, abs(bmi.bmiHeader.biHeight),
229 bitmap->bitmapbits, &bmi, DIB_RGB_COLORS);
231 if(!bm_is_selected){
232 SelectObject(hdc, old);
233 DeleteDC(hdc);
236 GdipFree(bitmap->bitmapbits);
237 bitmap->bitmapbits = NULL;
238 bitmap->lockmode = 0;
240 return Ok;
243 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
244 GpBitmap **bitmap)
246 GpStatus stat;
247 IStream *stream;
249 if(!filename || !bitmap)
250 return InvalidParameter;
252 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
254 if(stat != Ok)
255 return stat;
257 stat = GdipCreateBitmapFromStream(stream, bitmap);
259 IStream_Release(stream);
261 return stat;
264 /* FIXME: no icm */
265 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
266 GpBitmap **bitmap)
268 return GdipCreateBitmapFromFile(filename, bitmap);
271 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
272 HBITMAP* hbmReturn, ARGB background)
274 FIXME("stub\n");
276 hbmReturn = NULL;
278 return NotImplemented;
281 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
282 GpMetafile* metafile, BOOL* succ, EmfType emfType,
283 const WCHAR* description, GpMetafile** out_metafile)
285 static int calls;
287 if(!ref || !metafile || !out_metafile)
288 return InvalidParameter;
290 *succ = FALSE;
291 *out_metafile = NULL;
293 if(!(calls++))
294 FIXME("not implemented\n");
296 return NotImplemented;
299 /* FIXME: this should create a bitmap in the given size with the attributes
300 * (resolution etc.) of the graphics object */
301 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
302 GpGraphics* target, GpBitmap** bitmap)
304 static int calls;
305 GpStatus ret;
307 if(!target || !bitmap)
308 return InvalidParameter;
310 if(!(calls++))
311 FIXME("hacked stub\n");
313 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
314 NULL, bitmap);
316 return ret;
319 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
320 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
322 BITMAPFILEHEADER *bmfh;
323 BITMAPINFOHEADER *bmih;
324 BYTE *buff;
325 INT datalen, size;
326 IStream *stream;
328 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
330 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
331 *bitmap = NULL;
332 return InvalidParameter;
335 if(scan0 && !stride)
336 return InvalidParameter;
338 /* FIXME: windows allows negative stride (reads backwards from scan0) */
339 if(stride < 0){
340 FIXME("negative stride\n");
341 return InvalidParameter;
344 *bitmap = GdipAlloc(sizeof(GpBitmap));
345 if(!*bitmap) return OutOfMemory;
347 if(stride == 0){
348 stride = width * (PIXELFORMATBPP(format) / 8);
349 stride = (stride + 3) & ~3;
352 datalen = abs(stride * height);
353 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
354 buff = GdipAlloc(size);
355 if(!buff){
356 GdipFree(*bitmap);
357 return OutOfMemory;
360 bmfh = (BITMAPFILEHEADER*) buff;
361 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
363 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
364 bmfh->bfSize = size;
365 bmfh->bfOffBits = size - datalen;
367 bmih->biSize = sizeof(BITMAPINFOHEADER);
368 bmih->biWidth = width;
369 bmih->biHeight = -height;
370 /* FIXME: use the rest of the data from format */
371 bmih->biBitCount = PIXELFORMATBPP(format);
372 bmih->biCompression = BI_RGB;
373 bmih->biSizeImage = datalen;
375 if(scan0)
376 memcpy(bmih + 1, scan0, datalen);
377 else
378 memset(bmih + 1, 0, datalen);
380 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
381 ERR("could not make stream\n");
382 GdipFree(*bitmap);
383 GdipFree(buff);
384 return GenericError;
387 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
388 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
389 TRACE("Could not load picture\n");
390 IStream_Release(stream);
391 GdipFree(*bitmap);
392 GdipFree(buff);
393 return GenericError;
396 (*bitmap)->image.type = ImageTypeBitmap;
397 (*bitmap)->image.flags = ImageFlagsNone;
398 (*bitmap)->width = width;
399 (*bitmap)->height = height;
400 (*bitmap)->format = format;
402 return Ok;
405 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
406 GpBitmap **bitmap)
408 GpStatus stat;
410 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
412 if(stat != Ok)
413 return stat;
415 if((*bitmap)->image.type != ImageTypeBitmap){
416 IPicture_Release((*bitmap)->image.picture);
417 GdipFree(bitmap);
418 return GenericError; /* FIXME: what error to return? */
421 return Ok;
424 /* FIXME: no icm */
425 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
426 GpBitmap **bitmap)
428 return GdipCreateBitmapFromStream(stream, bitmap);
431 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
433 HDC hdc;
435 if(!image)
436 return InvalidParameter;
438 IPicture_get_CurDC(image->picture, &hdc);
439 DeleteDC(hdc);
440 IPicture_Release(image->picture);
441 if (image->type == ImageTypeBitmap)
442 GdipFree(((GpBitmap*)image)->bitmapbits);
443 GdipFree(image);
445 return Ok;
448 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
450 if(!image || !item)
451 return InvalidParameter;
453 return NotImplemented;
456 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
457 GpUnit *srcUnit)
459 if(!image || !srcRect || !srcUnit)
460 return InvalidParameter;
461 if(image->type == ImageTypeMetafile){
462 *srcRect = ((GpMetafile*)image)->bounds;
463 *srcUnit = ((GpMetafile*)image)->unit;
465 else if(image->type == ImageTypeBitmap){
466 srcRect->X = srcRect->Y = 0.0;
467 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
468 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
469 *srcUnit = UnitPixel;
471 else{
472 srcRect->X = srcRect->Y = 0.0;
473 srcRect->Width = ipicture_pixel_width(image->picture);
474 srcRect->Height = ipicture_pixel_height(image->picture);
475 *srcUnit = UnitPixel;
478 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
479 srcRect->Width, srcRect->Height, *srcUnit);
481 return Ok;
484 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
485 REAL *height)
487 if(!image || !height || !width)
488 return InvalidParameter;
490 if(image->type == ImageTypeMetafile){
491 HDC hdc = GetDC(0);
493 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
494 ((GpMetafile*)image)->bounds.Height;
496 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
497 ((GpMetafile*)image)->bounds.Width;
499 ReleaseDC(0, hdc);
502 else if(image->type == ImageTypeBitmap){
503 *height = ((GpBitmap*)image)->height;
504 *width = ((GpBitmap*)image)->width;
506 else{
507 *height = ipicture_pixel_height(image->picture);
508 *width = ipicture_pixel_width(image->picture);
511 TRACE("returning (%f, %f)\n", *height, *width);
512 return Ok;
515 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
516 GpGraphics **graphics)
518 HDC hdc;
520 if(!image || !graphics)
521 return InvalidParameter;
523 if(image->type != ImageTypeBitmap){
524 FIXME("not implemented for image type %d\n", image->type);
525 return NotImplemented;
528 IPicture_get_CurDC(image->picture, &hdc);
530 if(!hdc){
531 hdc = CreateCompatibleDC(0);
532 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
535 return GdipCreateFromHDC(hdc, graphics);
538 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
540 if(!image || !height)
541 return InvalidParameter;
543 if(image->type == ImageTypeMetafile){
544 HDC hdc = GetDC(0);
546 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
547 ((GpMetafile*)image)->bounds.Height);
549 ReleaseDC(0, hdc);
551 else if(image->type == ImageTypeBitmap)
552 *height = ((GpBitmap*)image)->height;
553 else
554 *height = ipicture_pixel_height(image->picture);
556 TRACE("returning %d\n", *height);
558 return Ok;
561 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
563 static int calls;
565 if(!image || !res)
566 return InvalidParameter;
568 if(!(calls++))
569 FIXME("not implemented\n");
571 return NotImplemented;
574 /* FIXME: test this function for non-bitmap types */
575 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
577 if(!image || !format)
578 return InvalidParameter;
580 if(image->type != ImageTypeBitmap)
581 *format = PixelFormat24bppRGB;
582 else
583 *format = ((GpBitmap*) image)->format;
585 return Ok;
588 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
590 static int calls;
592 if(!image || !format)
593 return InvalidParameter;
595 if(!(calls++))
596 FIXME("not implemented\n");
598 return NotImplemented;
601 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
603 if(!image || !type)
604 return InvalidParameter;
606 *type = image->type;
608 return Ok;
611 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
613 static int calls;
615 if(!image || !res)
616 return InvalidParameter;
618 if(!(calls++))
619 FIXME("not implemented\n");
621 return NotImplemented;
624 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
626 if(!image || !width)
627 return InvalidParameter;
629 if(image->type == ImageTypeMetafile){
630 HDC hdc = GetDC(0);
632 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
633 ((GpMetafile*)image)->bounds.Width);
635 ReleaseDC(0, hdc);
637 else if(image->type == ImageTypeBitmap)
638 *width = ((GpBitmap*)image)->width;
639 else
640 *width = ipicture_pixel_width(image->picture);
642 TRACE("returning %d\n", *width);
644 return Ok;
647 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
648 MetafileHeader * header)
650 static int calls;
652 if(!metafile || !header)
653 return InvalidParameter;
655 if(!(calls++))
656 FIXME("not implemented\n");
658 return Ok;
661 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
662 UINT* size)
664 static int calls;
666 TRACE("%p %x %p\n", image, pid, size);
668 if(!size || !image)
669 return InvalidParameter;
671 if(!(calls++))
672 FIXME("not implemented\n");
674 return NotImplemented;
677 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
678 GDIPCONST GUID* dimensionID, UINT* count)
680 static int calls;
682 if(!image || !dimensionID || !count)
683 return InvalidParameter;
685 if(!(calls++))
686 FIXME("not implemented\n");
688 return NotImplemented;
691 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
692 GUID* dimensionIDs, UINT count)
694 static int calls;
696 if(!image || !dimensionIDs)
697 return InvalidParameter;
699 if(!(calls++))
700 FIXME("not implemented\n");
702 return Ok;
705 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
706 GDIPCONST GUID* dimensionID, UINT frameidx)
708 static int calls;
710 if(!image || !dimensionID)
711 return InvalidParameter;
713 if(!(calls++))
714 FIXME("not implemented\n");
716 return Ok;
719 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
720 GpImage **image)
722 GpStatus stat;
723 IStream *stream;
725 if (!filename || !image)
726 return InvalidParameter;
728 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
730 if (stat != Ok)
731 return stat;
733 stat = GdipLoadImageFromStream(stream, image);
735 IStream_Release(stream);
737 return stat;
740 /* FIXME: no icm handling */
741 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
743 return GdipLoadImageFromFile(filename, image);
746 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
748 IPicture *pic;
749 short type;
751 if(!stream || !image)
752 return InvalidParameter;
754 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
755 (LPVOID*) &pic) != S_OK){
756 TRACE("Could not load picture\n");
757 return GenericError;
760 IPicture_get_Type(pic, &type);
762 if(type == PICTYPE_BITMAP){
763 BITMAPINFO bmi;
764 BITMAPCOREHEADER* bmch;
765 OLE_HANDLE hbm;
766 HDC hdc;
768 *image = GdipAlloc(sizeof(GpBitmap));
769 if(!*image) return OutOfMemory;
770 (*image)->type = ImageTypeBitmap;
772 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
773 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
775 /* get the pixel format */
776 IPicture_get_Handle(pic, &hbm);
777 IPicture_get_CurDC(pic, &hdc);
779 ZeroMemory(&bmi, sizeof(bmi));
780 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
781 bmch->bcSize = sizeof(BITMAPCOREHEADER);
783 if(!hdc){
784 HBITMAP old;
785 hdc = CreateCompatibleDC(0);
786 old = SelectObject(hdc, (HBITMAP)hbm);
787 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
788 SelectObject(hdc, old);
789 DeleteDC(hdc);
791 else
792 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
794 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
796 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
797 /* FIXME: missing initialization code */
798 *image = GdipAlloc(sizeof(GpMetafile));
799 if(!*image) return OutOfMemory;
800 (*image)->type = ImageTypeMetafile;
802 else{
803 *image = GdipAlloc(sizeof(GpImage));
804 if(!*image) return OutOfMemory;
805 (*image)->type = ImageTypeUnknown;
808 (*image)->picture = pic;
809 (*image)->flags = ImageFlagsNone;
811 return Ok;
814 /* FIXME: no ICM */
815 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
817 return GdipLoadImageFromStream(stream, image);
820 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
822 static int calls;
824 if(!image)
825 return InvalidParameter;
827 if(!(calls++))
828 FIXME("not implemented\n");
830 return NotImplemented;
833 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
834 GDIPCONST CLSID *clsidEncoder,
835 GDIPCONST EncoderParameters *encoderParams)
837 GpStatus stat;
838 IStream *stream;
840 if (!image || !filename|| !clsidEncoder)
841 return InvalidParameter;
843 if (!(image->picture))
844 return InvalidParameter;
846 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
847 if (stat != Ok)
848 return GenericError;
850 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
852 IStream_Release(stream);
853 return stat;
856 /*************************************************************************
857 * Encoding functions -
858 * These functions encode an image in different image file formats.
860 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
861 #define BITMAP_FORMAT_JPEG 0xd8ff
862 #define BITMAP_FORMAT_GIF 0x4947
863 #define BITMAP_FORMAT_PNG 0x5089
864 #define BITMAP_FORMAT_APM 0xcdd7
866 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
867 void **output, unsigned int *output_size)
869 int num_palette_entries;
870 BITMAPFILEHEADER *bmp_file_hdr;
871 BITMAPINFO *bmp_info_hdr;
873 if (bitmap_info->bmiHeader.biClrUsed) {
874 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
875 if (num_palette_entries > 256) num_palette_entries = 256;
876 } else {
877 if (bitmap_info->bmiHeader.biBitCount <= 8)
878 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
879 else
880 num_palette_entries = 0;
883 *output_size =
884 sizeof(BITMAPFILEHEADER) +
885 sizeof(BITMAPINFOHEADER) +
886 num_palette_entries * sizeof(RGBQUAD) +
887 bitmap_info->bmiHeader.biSizeImage;
889 *output = GdipAlloc(*output_size);
891 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
892 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
893 bmp_file_hdr->bfSize = *output_size;
894 bmp_file_hdr->bfOffBits =
895 sizeof(BITMAPFILEHEADER) +
896 sizeof(BITMAPINFOHEADER) +
897 num_palette_entries * sizeof (RGBQUAD);
899 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
900 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
901 memcpy((unsigned char *)(*output) +
902 sizeof(BITMAPFILEHEADER) +
903 sizeof(BITMAPINFOHEADER) +
904 num_palette_entries * sizeof(RGBQUAD),
905 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
907 return Ok;
910 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
911 void **output, unsigned int *output_size);
913 typedef enum {
914 BMP,
915 NUM_ENCODERS_SUPPORTED
916 } ImageFormat;
918 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
919 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
920 encode_image_BMP,
923 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
924 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
926 GpStatus stat;
927 HRESULT hr;
928 short type;
929 HBITMAP hbmp;
930 HBITMAP old_hbmp;
931 HDC hdc;
932 int bm_is_selected;
933 BITMAPINFO bmp_info;
934 LPVOID bmp_bits;
935 encode_image_func* encode_image;
936 LPVOID output;
937 unsigned int output_size;
938 unsigned int dummy;
939 int i;
941 old_hbmp = 0;
942 output = NULL;
943 output_size = 0;
945 if(!image || !stream)
946 return InvalidParameter;
948 if (!image->picture)
949 return GenericError;
951 hr = IPicture_get_Type(image->picture, &type);
952 if (FAILED(hr) || type != PICTYPE_BITMAP)
953 return GenericError;
955 /* select correct encoder */
956 encode_image = NULL;
957 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
958 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
959 encode_image = encode_image_funcs[i];
961 if (encode_image == NULL)
962 return UnknownImageFormat;
964 /* extract underlying hbitmap representation from the IPicture */
965 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
966 if (FAILED(hr) || !hbmp)
967 return GenericError;
968 hr = IPicture_get_CurDC(image->picture, &hdc);
969 if (FAILED(hr))
970 return GenericError;
971 bm_is_selected = (hdc != 0);
972 if (!bm_is_selected) {
973 hdc = CreateCompatibleDC(0);
974 old_hbmp = SelectObject(hdc, hbmp);
977 /* get bits from HBITMAP */
978 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
979 bmp_info.bmiHeader.biBitCount = 0;
980 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
982 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
984 if (bmp_bits)
985 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
987 if (!bm_is_selected) {
988 SelectObject(hdc, old_hbmp);
989 DeleteDC(hdc);
992 if (!bmp_bits)
993 return OutOfMemory;
995 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
996 if (stat == Ok)
997 IStream_Write(stream, output, output_size, &dummy);
999 GdipFree(output);
1000 GdipFree(bmp_bits);
1002 return stat;
1005 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1006 GDIPCONST ColorPalette *palette)
1008 static int calls;
1010 if(!image || !palette)
1011 return InvalidParameter;
1013 if(!(calls++))
1014 FIXME("not implemented\n");
1016 return NotImplemented;
1019 /*************************************************************************
1020 * Encoders -
1021 * Structures that represent which formats we support for encoding.
1024 /* ImageCodecInfo creation routines taken from libgdiplus */
1025 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1026 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1027 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1028 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1029 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1030 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1032 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1034 { /* BMP */
1035 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1036 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1037 /* CodecName */ bmp_codecname,
1038 /* DllName */ NULL,
1039 /* FormatDescription */ bmp_format,
1040 /* FilenameExtension */ bmp_extension,
1041 /* MimeType */ bmp_mimetype,
1042 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1043 /* Version */ 1,
1044 /* SigCount */ 1,
1045 /* SigSize */ 2,
1046 /* SigPattern */ bmp_sig_pattern,
1047 /* SigMask */ bmp_sig_mask,
1051 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1053 if (!numEncoders || !size)
1054 return InvalidParameter;
1056 *numEncoders = NUM_ENCODERS_SUPPORTED;
1057 *size = sizeof (codecs);
1059 return Ok;
1062 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1064 if (!encoders ||
1065 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1066 (size != sizeof (codecs)))
1067 return GenericError;
1069 memcpy(encoders, codecs, sizeof (codecs));
1071 return Ok;
1073 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1075 BITMAP bm;
1076 GpStatus retval;
1077 PixelFormat format;
1079 if(!hbm || !bitmap)
1080 return InvalidParameter;
1082 /* TODO: Support for device-dependent bitmaps */
1083 if(hpal){
1084 FIXME("no support for device-dependent bitmaps\n");
1085 return NotImplemented;
1088 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1089 return InvalidParameter;
1091 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1092 switch(bm.bmBitsPixel) {
1093 case 1:
1094 format = PixelFormat1bppIndexed;
1095 break;
1096 case 4:
1097 format = PixelFormat4bppIndexed;
1098 break;
1099 case 8:
1100 format = PixelFormat8bppIndexed;
1101 break;
1102 case 24:
1103 format = PixelFormat24bppRGB;
1104 break;
1105 case 48:
1106 format = PixelFormat48bppRGB;
1107 break;
1108 default:
1109 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1110 return InvalidParameter;
1113 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1114 format, bm.bmBits, bitmap);
1116 return retval;
1119 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1120 const VOID *params, const UINT size)
1122 static int calls;
1124 if(!(calls++))
1125 FIXME("not implemented\n");
1127 return NotImplemented;
1130 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1132 if(!image || !flags)
1133 return InvalidParameter;
1135 *flags = image->flags;
1137 return Ok;