gdiplus: Stub GdipCloneImage.
[wine/wine-kai.git] / dlls / gdiplus / image.c
blobbf2ac35b14fafa0f92a145fcd98c39e179ee88ee
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 GdipCloneImage(GpImage *image, GpImage **cloneImage)
245 if (!(image && cloneImage)) return InvalidParameter;
247 FIXME("stub: %p, %p", image, cloneImage);
249 return NotImplemented;
252 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
253 GpBitmap **bitmap)
255 GpStatus stat;
256 IStream *stream;
258 if(!filename || !bitmap)
259 return InvalidParameter;
261 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
263 if(stat != Ok)
264 return stat;
266 stat = GdipCreateBitmapFromStream(stream, bitmap);
268 IStream_Release(stream);
270 return stat;
273 /* FIXME: no icm */
274 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
275 GpBitmap **bitmap)
277 return GdipCreateBitmapFromFile(filename, bitmap);
280 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
281 HBITMAP* hbmReturn, ARGB background)
283 FIXME("stub\n");
285 hbmReturn = NULL;
287 return NotImplemented;
290 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
291 GpMetafile* metafile, BOOL* succ, EmfType emfType,
292 const WCHAR* description, GpMetafile** out_metafile)
294 static int calls;
296 if(!ref || !metafile || !out_metafile)
297 return InvalidParameter;
299 *succ = FALSE;
300 *out_metafile = NULL;
302 if(!(calls++))
303 FIXME("not implemented\n");
305 return NotImplemented;
308 /* FIXME: this should create a bitmap in the given size with the attributes
309 * (resolution etc.) of the graphics object */
310 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
311 GpGraphics* target, GpBitmap** bitmap)
313 static int calls;
314 GpStatus ret;
316 if(!target || !bitmap)
317 return InvalidParameter;
319 if(!(calls++))
320 FIXME("hacked stub\n");
322 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
323 NULL, bitmap);
325 return ret;
328 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
329 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
331 BITMAPFILEHEADER *bmfh;
332 BITMAPINFOHEADER *bmih;
333 BYTE *buff;
334 INT datalen, size;
335 IStream *stream;
337 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
339 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
340 *bitmap = NULL;
341 return InvalidParameter;
344 if(scan0 && !stride)
345 return InvalidParameter;
347 /* FIXME: windows allows negative stride (reads backwards from scan0) */
348 if(stride < 0){
349 FIXME("negative stride\n");
350 return InvalidParameter;
353 *bitmap = GdipAlloc(sizeof(GpBitmap));
354 if(!*bitmap) return OutOfMemory;
356 if(stride == 0){
357 stride = width * (PIXELFORMATBPP(format) / 8);
358 stride = (stride + 3) & ~3;
361 datalen = abs(stride * height);
362 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
363 buff = GdipAlloc(size);
364 if(!buff){
365 GdipFree(*bitmap);
366 return OutOfMemory;
369 bmfh = (BITMAPFILEHEADER*) buff;
370 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
372 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
373 bmfh->bfSize = size;
374 bmfh->bfOffBits = size - datalen;
376 bmih->biSize = sizeof(BITMAPINFOHEADER);
377 bmih->biWidth = width;
378 bmih->biHeight = -height;
379 /* FIXME: use the rest of the data from format */
380 bmih->biBitCount = PIXELFORMATBPP(format);
381 bmih->biCompression = BI_RGB;
382 bmih->biSizeImage = datalen;
384 if(scan0)
385 memcpy(bmih + 1, scan0, datalen);
386 else
387 memset(bmih + 1, 0, datalen);
389 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
390 ERR("could not make stream\n");
391 GdipFree(*bitmap);
392 GdipFree(buff);
393 return GenericError;
396 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
397 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
398 TRACE("Could not load picture\n");
399 IStream_Release(stream);
400 GdipFree(*bitmap);
401 GdipFree(buff);
402 return GenericError;
405 (*bitmap)->image.type = ImageTypeBitmap;
406 (*bitmap)->image.flags = ImageFlagsNone;
407 (*bitmap)->width = width;
408 (*bitmap)->height = height;
409 (*bitmap)->format = format;
411 return Ok;
414 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
415 GpBitmap **bitmap)
417 GpStatus stat;
419 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
421 if(stat != Ok)
422 return stat;
424 if((*bitmap)->image.type != ImageTypeBitmap){
425 IPicture_Release((*bitmap)->image.picture);
426 GdipFree(bitmap);
427 return GenericError; /* FIXME: what error to return? */
430 return Ok;
433 /* FIXME: no icm */
434 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
435 GpBitmap **bitmap)
437 return GdipCreateBitmapFromStream(stream, bitmap);
440 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
442 HDC hdc;
444 if(!image)
445 return InvalidParameter;
447 IPicture_get_CurDC(image->picture, &hdc);
448 DeleteDC(hdc);
449 IPicture_Release(image->picture);
450 if (image->type == ImageTypeBitmap)
451 GdipFree(((GpBitmap*)image)->bitmapbits);
452 GdipFree(image);
454 return Ok;
457 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
459 if(!image || !item)
460 return InvalidParameter;
462 return NotImplemented;
465 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
466 GpUnit *srcUnit)
468 if(!image || !srcRect || !srcUnit)
469 return InvalidParameter;
470 if(image->type == ImageTypeMetafile){
471 *srcRect = ((GpMetafile*)image)->bounds;
472 *srcUnit = ((GpMetafile*)image)->unit;
474 else if(image->type == ImageTypeBitmap){
475 srcRect->X = srcRect->Y = 0.0;
476 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
477 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
478 *srcUnit = UnitPixel;
480 else{
481 srcRect->X = srcRect->Y = 0.0;
482 srcRect->Width = ipicture_pixel_width(image->picture);
483 srcRect->Height = ipicture_pixel_height(image->picture);
484 *srcUnit = UnitPixel;
487 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
488 srcRect->Width, srcRect->Height, *srcUnit);
490 return Ok;
493 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
494 REAL *height)
496 if(!image || !height || !width)
497 return InvalidParameter;
499 if(image->type == ImageTypeMetafile){
500 HDC hdc = GetDC(0);
502 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
503 ((GpMetafile*)image)->bounds.Height;
505 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
506 ((GpMetafile*)image)->bounds.Width;
508 ReleaseDC(0, hdc);
511 else if(image->type == ImageTypeBitmap){
512 *height = ((GpBitmap*)image)->height;
513 *width = ((GpBitmap*)image)->width;
515 else{
516 *height = ipicture_pixel_height(image->picture);
517 *width = ipicture_pixel_width(image->picture);
520 TRACE("returning (%f, %f)\n", *height, *width);
521 return Ok;
524 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
525 GpGraphics **graphics)
527 HDC hdc;
529 if(!image || !graphics)
530 return InvalidParameter;
532 if(image->type != ImageTypeBitmap){
533 FIXME("not implemented for image type %d\n", image->type);
534 return NotImplemented;
537 IPicture_get_CurDC(image->picture, &hdc);
539 if(!hdc){
540 hdc = CreateCompatibleDC(0);
541 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
544 return GdipCreateFromHDC(hdc, graphics);
547 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
549 if(!image || !height)
550 return InvalidParameter;
552 if(image->type == ImageTypeMetafile){
553 HDC hdc = GetDC(0);
555 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
556 ((GpMetafile*)image)->bounds.Height);
558 ReleaseDC(0, hdc);
560 else if(image->type == ImageTypeBitmap)
561 *height = ((GpBitmap*)image)->height;
562 else
563 *height = ipicture_pixel_height(image->picture);
565 TRACE("returning %d\n", *height);
567 return Ok;
570 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
572 static int calls;
574 if(!image || !res)
575 return InvalidParameter;
577 if(!(calls++))
578 FIXME("not implemented\n");
580 return NotImplemented;
583 /* FIXME: test this function for non-bitmap types */
584 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
586 if(!image || !format)
587 return InvalidParameter;
589 if(image->type != ImageTypeBitmap)
590 *format = PixelFormat24bppRGB;
591 else
592 *format = ((GpBitmap*) image)->format;
594 return Ok;
597 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
599 static int calls;
601 if(!image || !format)
602 return InvalidParameter;
604 if(!(calls++))
605 FIXME("not implemented\n");
607 return NotImplemented;
610 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
612 if(!image || !type)
613 return InvalidParameter;
615 *type = image->type;
617 return Ok;
620 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
622 static int calls;
624 if(!image || !res)
625 return InvalidParameter;
627 if(!(calls++))
628 FIXME("not implemented\n");
630 return NotImplemented;
633 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
635 if(!image || !width)
636 return InvalidParameter;
638 if(image->type == ImageTypeMetafile){
639 HDC hdc = GetDC(0);
641 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
642 ((GpMetafile*)image)->bounds.Width);
644 ReleaseDC(0, hdc);
646 else if(image->type == ImageTypeBitmap)
647 *width = ((GpBitmap*)image)->width;
648 else
649 *width = ipicture_pixel_width(image->picture);
651 TRACE("returning %d\n", *width);
653 return Ok;
656 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
657 MetafileHeader * header)
659 static int calls;
661 if(!metafile || !header)
662 return InvalidParameter;
664 if(!(calls++))
665 FIXME("not implemented\n");
667 return Ok;
670 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
671 UINT* size)
673 static int calls;
675 TRACE("%p %x %p\n", image, pid, size);
677 if(!size || !image)
678 return InvalidParameter;
680 if(!(calls++))
681 FIXME("not implemented\n");
683 return NotImplemented;
686 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
687 GDIPCONST GUID* dimensionID, UINT* count)
689 static int calls;
691 if(!image || !dimensionID || !count)
692 return InvalidParameter;
694 if(!(calls++))
695 FIXME("not implemented\n");
697 return NotImplemented;
700 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
701 UINT* count)
703 if(!image || !count)
704 return InvalidParameter;
706 *count = 1;
708 FIXME("stub\n");
710 return Ok;
713 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
714 GUID* dimensionIDs, UINT count)
716 static int calls;
718 if(!image || !dimensionIDs)
719 return InvalidParameter;
721 if(!(calls++))
722 FIXME("not implemented\n");
724 return Ok;
727 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
728 GDIPCONST GUID* dimensionID, UINT frameidx)
730 static int calls;
732 if(!image || !dimensionID)
733 return InvalidParameter;
735 if(!(calls++))
736 FIXME("not implemented\n");
738 return Ok;
741 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
742 GpImage **image)
744 GpStatus stat;
745 IStream *stream;
747 if (!filename || !image)
748 return InvalidParameter;
750 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
752 if (stat != Ok)
753 return stat;
755 stat = GdipLoadImageFromStream(stream, image);
757 IStream_Release(stream);
759 return stat;
762 /* FIXME: no icm handling */
763 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
765 return GdipLoadImageFromFile(filename, image);
768 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
770 IPicture *pic;
771 short type;
773 if(!stream || !image)
774 return InvalidParameter;
776 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
777 (LPVOID*) &pic) != S_OK){
778 TRACE("Could not load picture\n");
779 return GenericError;
782 IPicture_get_Type(pic, &type);
784 if(type == PICTYPE_BITMAP){
785 BITMAPINFO bmi;
786 BITMAPCOREHEADER* bmch;
787 OLE_HANDLE hbm;
788 HDC hdc;
790 *image = GdipAlloc(sizeof(GpBitmap));
791 if(!*image) return OutOfMemory;
792 (*image)->type = ImageTypeBitmap;
794 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
795 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
797 /* get the pixel format */
798 IPicture_get_Handle(pic, &hbm);
799 IPicture_get_CurDC(pic, &hdc);
801 ZeroMemory(&bmi, sizeof(bmi));
802 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
803 bmch->bcSize = sizeof(BITMAPCOREHEADER);
805 if(!hdc){
806 HBITMAP old;
807 hdc = CreateCompatibleDC(0);
808 old = SelectObject(hdc, (HBITMAP)hbm);
809 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
810 SelectObject(hdc, old);
811 DeleteDC(hdc);
813 else
814 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
816 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
818 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
819 /* FIXME: missing initialization code */
820 *image = GdipAlloc(sizeof(GpMetafile));
821 if(!*image) return OutOfMemory;
822 (*image)->type = ImageTypeMetafile;
824 else{
825 *image = GdipAlloc(sizeof(GpImage));
826 if(!*image) return OutOfMemory;
827 (*image)->type = ImageTypeUnknown;
830 (*image)->picture = pic;
831 (*image)->flags = ImageFlagsNone;
833 return Ok;
836 /* FIXME: no ICM */
837 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
839 return GdipLoadImageFromStream(stream, image);
842 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
844 static int calls;
846 if(!image)
847 return InvalidParameter;
849 if(!(calls++))
850 FIXME("not implemented\n");
852 return NotImplemented;
855 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
856 GDIPCONST CLSID *clsidEncoder,
857 GDIPCONST EncoderParameters *encoderParams)
859 GpStatus stat;
860 IStream *stream;
862 if (!image || !filename|| !clsidEncoder)
863 return InvalidParameter;
865 if (!(image->picture))
866 return InvalidParameter;
868 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
869 if (stat != Ok)
870 return GenericError;
872 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
874 IStream_Release(stream);
875 return stat;
878 /*************************************************************************
879 * Encoding functions -
880 * These functions encode an image in different image file formats.
882 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
883 #define BITMAP_FORMAT_JPEG 0xd8ff
884 #define BITMAP_FORMAT_GIF 0x4947
885 #define BITMAP_FORMAT_PNG 0x5089
886 #define BITMAP_FORMAT_APM 0xcdd7
888 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
889 void **output, unsigned int *output_size)
891 int num_palette_entries;
892 BITMAPFILEHEADER *bmp_file_hdr;
893 BITMAPINFO *bmp_info_hdr;
895 if (bitmap_info->bmiHeader.biClrUsed) {
896 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
897 if (num_palette_entries > 256) num_palette_entries = 256;
898 } else {
899 if (bitmap_info->bmiHeader.biBitCount <= 8)
900 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
901 else
902 num_palette_entries = 0;
905 *output_size =
906 sizeof(BITMAPFILEHEADER) +
907 sizeof(BITMAPINFOHEADER) +
908 num_palette_entries * sizeof(RGBQUAD) +
909 bitmap_info->bmiHeader.biSizeImage;
911 *output = GdipAlloc(*output_size);
913 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
914 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
915 bmp_file_hdr->bfSize = *output_size;
916 bmp_file_hdr->bfOffBits =
917 sizeof(BITMAPFILEHEADER) +
918 sizeof(BITMAPINFOHEADER) +
919 num_palette_entries * sizeof (RGBQUAD);
921 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
922 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
923 memcpy((unsigned char *)(*output) +
924 sizeof(BITMAPFILEHEADER) +
925 sizeof(BITMAPINFOHEADER) +
926 num_palette_entries * sizeof(RGBQUAD),
927 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
929 return Ok;
932 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
933 void **output, unsigned int *output_size);
935 typedef enum {
936 BMP,
937 NUM_ENCODERS_SUPPORTED
938 } ImageFormat;
940 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
941 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
942 encode_image_BMP,
945 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
946 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
948 GpStatus stat;
949 HRESULT hr;
950 short type;
951 HBITMAP hbmp;
952 HBITMAP old_hbmp;
953 HDC hdc;
954 int bm_is_selected;
955 BITMAPINFO bmp_info;
956 LPVOID bmp_bits;
957 encode_image_func* encode_image;
958 LPVOID output;
959 unsigned int output_size;
960 unsigned int dummy;
961 int i;
963 old_hbmp = 0;
964 output = NULL;
965 output_size = 0;
967 if(!image || !stream)
968 return InvalidParameter;
970 if (!image->picture)
971 return GenericError;
973 hr = IPicture_get_Type(image->picture, &type);
974 if (FAILED(hr) || type != PICTYPE_BITMAP)
975 return GenericError;
977 /* select correct encoder */
978 encode_image = NULL;
979 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
980 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
981 encode_image = encode_image_funcs[i];
983 if (encode_image == NULL)
984 return UnknownImageFormat;
986 /* extract underlying hbitmap representation from the IPicture */
987 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
988 if (FAILED(hr) || !hbmp)
989 return GenericError;
990 hr = IPicture_get_CurDC(image->picture, &hdc);
991 if (FAILED(hr))
992 return GenericError;
993 bm_is_selected = (hdc != 0);
994 if (!bm_is_selected) {
995 hdc = CreateCompatibleDC(0);
996 old_hbmp = SelectObject(hdc, hbmp);
999 /* get bits from HBITMAP */
1000 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1001 bmp_info.bmiHeader.biBitCount = 0;
1002 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1004 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1006 if (bmp_bits)
1007 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1009 if (!bm_is_selected) {
1010 SelectObject(hdc, old_hbmp);
1011 DeleteDC(hdc);
1014 if (!bmp_bits)
1015 return OutOfMemory;
1017 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1018 if (stat == Ok)
1019 IStream_Write(stream, output, output_size, &dummy);
1021 GdipFree(output);
1022 GdipFree(bmp_bits);
1024 return stat;
1027 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1028 GDIPCONST ColorPalette *palette)
1030 static int calls;
1032 if(!image || !palette)
1033 return InvalidParameter;
1035 if(!(calls++))
1036 FIXME("not implemented\n");
1038 return NotImplemented;
1041 /*************************************************************************
1042 * Encoders -
1043 * Structures that represent which formats we support for encoding.
1046 /* ImageCodecInfo creation routines taken from libgdiplus */
1047 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1048 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1049 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1050 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1051 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1052 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1054 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1056 { /* BMP */
1057 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1058 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1059 /* CodecName */ bmp_codecname,
1060 /* DllName */ NULL,
1061 /* FormatDescription */ bmp_format,
1062 /* FilenameExtension */ bmp_extension,
1063 /* MimeType */ bmp_mimetype,
1064 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1065 /* Version */ 1,
1066 /* SigCount */ 1,
1067 /* SigSize */ 2,
1068 /* SigPattern */ bmp_sig_pattern,
1069 /* SigMask */ bmp_sig_mask,
1073 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1075 if (!numEncoders || !size)
1076 return InvalidParameter;
1078 *numEncoders = NUM_ENCODERS_SUPPORTED;
1079 *size = sizeof (codecs);
1081 return Ok;
1084 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1086 if (!encoders ||
1087 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1088 (size != sizeof (codecs)))
1089 return GenericError;
1091 memcpy(encoders, codecs, sizeof (codecs));
1093 return Ok;
1095 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1097 BITMAP bm;
1098 GpStatus retval;
1099 PixelFormat format;
1101 if(!hbm || !bitmap)
1102 return InvalidParameter;
1104 /* TODO: Support for device-dependent bitmaps */
1105 if(hpal){
1106 FIXME("no support for device-dependent bitmaps\n");
1107 return NotImplemented;
1110 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1111 return InvalidParameter;
1113 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1114 switch(bm.bmBitsPixel) {
1115 case 1:
1116 format = PixelFormat1bppIndexed;
1117 break;
1118 case 4:
1119 format = PixelFormat4bppIndexed;
1120 break;
1121 case 8:
1122 format = PixelFormat8bppIndexed;
1123 break;
1124 case 24:
1125 format = PixelFormat24bppRGB;
1126 break;
1127 case 48:
1128 format = PixelFormat48bppRGB;
1129 break;
1130 default:
1131 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1132 return InvalidParameter;
1135 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1136 format, bm.bmBits, bitmap);
1138 return retval;
1141 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1142 const VOID *params, const UINT size)
1144 static int calls;
1146 if(!(calls++))
1147 FIXME("not implemented\n");
1149 return NotImplemented;
1152 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1154 if(!image || !flags)
1155 return InvalidParameter;
1157 *flags = image->flags;
1159 return Ok;