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
21 #define NONAMELESSUNION
36 #include "gdiplus_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
41 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
43 static INT ipicture_pixel_height(IPicture *pic)
48 IPicture_get_Height(pic, &y);
52 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
58 static INT ipicture_pixel_width(IPicture *pic)
63 IPicture_get_Width(pic, &x);
67 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
74 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
75 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
77 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
79 * Note: According to Jose Roca's GDI+ docs, this function is not
80 * implemented in Windows's GDI+.
82 return NotImplemented;
85 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
86 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
87 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
89 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
91 * Note: According to Jose Roca's GDI+ docs, this function is not
92 * implemented in Windows's GDI+.
94 return NotImplemented;
97 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
98 const BYTE *row, UINT x)
100 *r = *g = *b = row[x*2+1];
104 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
105 const BYTE *row, UINT x)
107 WORD pixel = *((WORD*)(row)+x);
108 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
109 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
110 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
114 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
115 const BYTE *row, UINT x)
117 WORD pixel = *((WORD*)(row)+x);
118 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
119 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
120 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
124 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
125 const BYTE *row, UINT x)
127 WORD pixel = *((WORD*)(row)+x);
128 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
129 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
130 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
131 if ((pixel&0x8000) == 0x8000)
137 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
138 const BYTE *row, UINT x)
146 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
147 const BYTE *row, UINT x)
155 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
156 const BYTE *row, UINT x)
164 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
165 const BYTE *row, UINT x)
172 *r = row[x*4+2] * 255 / *a;
173 *g = row[x*4+1] * 255 / *a;
174 *b = row[x*4] * 255 / *a;
178 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
179 const BYTE *row, UINT x)
187 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
188 const BYTE *row, UINT x)
196 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
197 const BYTE *row, UINT x)
204 *r = row[x*8+5] * 255 / *a;
205 *g = row[x*8+3] * 255 / *a;
206 *b = row[x*8+1] * 255 / *a;
210 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
215 TRACE("%p %d %d %p\n", bitmap, x, y, color);
217 if(!bitmap || !color ||
218 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
219 return InvalidParameter;
221 row = bitmap->bits+bitmap->stride*y;
223 switch (bitmap->format)
225 case PixelFormat16bppGrayScale:
226 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
228 case PixelFormat16bppRGB555:
229 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
231 case PixelFormat16bppRGB565:
232 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
234 case PixelFormat16bppARGB1555:
235 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
237 case PixelFormat24bppRGB:
238 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
240 case PixelFormat32bppRGB:
241 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
243 case PixelFormat32bppARGB:
244 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
246 case PixelFormat32bppPARGB:
247 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
249 case PixelFormat48bppRGB:
250 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
252 case PixelFormat64bppARGB:
253 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
255 case PixelFormat64bppPARGB:
256 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
259 FIXME("not implemented for format 0x%x\n", bitmap->format);
260 return NotImplemented;
263 *color = a<<24|r<<16|g<<8|b;
268 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
271 *((WORD*)(row)+x) = (r+g+b)*85;
274 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
277 *((WORD*)(row)+x) = (r<<7&0x7c00)|
282 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
285 *((WORD*)(row)+x) = (r<<8&0xf800)|
290 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
293 *((WORD*)(row)+x) = (a<<8&0x8000)|
299 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
307 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
310 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
313 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
316 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
319 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
325 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
328 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
331 row[x*6+5] = row[x*6+4] = r;
332 row[x*6+3] = row[x*6+2] = g;
333 row[x*6+1] = row[x*6] = b;
336 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
339 UINT64 a64=a, r64=r, g64=g, b64=b;
340 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
343 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
346 UINT64 a64, r64, g64, b64;
351 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
354 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
359 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
361 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
362 return InvalidParameter;
369 row = bitmap->bits + bitmap->stride * y;
371 switch (bitmap->format)
373 case PixelFormat16bppGrayScale:
374 setpixel_16bppGrayScale(r,g,b,a,row,x);
376 case PixelFormat16bppRGB555:
377 setpixel_16bppRGB555(r,g,b,a,row,x);
379 case PixelFormat16bppRGB565:
380 setpixel_16bppRGB565(r,g,b,a,row,x);
382 case PixelFormat16bppARGB1555:
383 setpixel_16bppARGB1555(r,g,b,a,row,x);
385 case PixelFormat24bppRGB:
386 setpixel_24bppRGB(r,g,b,a,row,x);
388 case PixelFormat32bppRGB:
389 setpixel_32bppRGB(r,g,b,a,row,x);
391 case PixelFormat32bppARGB:
392 setpixel_32bppARGB(r,g,b,a,row,x);
394 case PixelFormat32bppPARGB:
395 setpixel_32bppPARGB(r,g,b,a,row,x);
397 case PixelFormat48bppRGB:
398 setpixel_48bppRGB(r,g,b,a,row,x);
400 case PixelFormat64bppARGB:
401 setpixel_64bppARGB(r,g,b,a,row,x);
403 case PixelFormat64bppPARGB:
404 setpixel_64bppPARGB(r,g,b,a,row,x);
407 FIXME("not implemented for format 0x%x\n", bitmap->format);
408 return NotImplemented;
414 /* This function returns a pointer to an array of pixels that represents the
415 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
416 * flags. It is correct behavior that a user who calls this function with write
417 * privileges can write to the whole bitmap (not just the area in rect).
419 * FIXME: only used portion of format is bits per pixel. */
420 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
421 UINT flags, PixelFormat format, BitmapData* lockeddata)
424 INT stride, bitspp = PIXELFORMATBPP(format);
426 HBITMAP hbm, old = NULL;
430 GpRect act_rect; /* actual rect to be used */
432 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
434 if(!lockeddata || !bitmap)
435 return InvalidParameter;
438 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
439 (rect->Y + rect->Height > bitmap->height) || !flags)
440 return InvalidParameter;
445 act_rect.X = act_rect.Y = 0;
446 act_rect.Width = bitmap->width;
447 act_rect.Height = bitmap->height;
450 if(flags & ImageLockModeUserInputBuf)
451 return NotImplemented;
456 if (bitmap->bits && bitmap->format == format)
458 /* no conversion is necessary; just use the bits directly */
459 lockeddata->Width = act_rect.Width;
460 lockeddata->Height = act_rect.Height;
461 lockeddata->PixelFormat = format;
462 lockeddata->Reserved = flags;
463 lockeddata->Stride = bitmap->stride;
464 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
465 bitmap->stride * act_rect.Y;
467 bitmap->lockmode = flags;
473 hbm = bitmap->hbitmap;
475 bm_is_selected = (hdc != 0);
477 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
480 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
481 pbmi->bmiHeader.biBitCount = 0;
484 hdc = CreateCompatibleDC(0);
485 old = SelectObject(hdc, hbm);
489 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
491 abs_height = abs(pbmi->bmiHeader.biHeight);
492 stride = pbmi->bmiHeader.biWidth * bitspp / 8;
493 stride = (stride + 3) & ~3;
495 buff = GdipAlloc(stride * abs_height);
497 pbmi->bmiHeader.biBitCount = bitspp;
500 GetDIBits(hdc, hbm, 0, abs_height, buff, pbmi, DIB_RGB_COLORS);
503 SelectObject(hdc, old);
512 lockeddata->Width = act_rect.Width;
513 lockeddata->Height = act_rect.Height;
514 lockeddata->PixelFormat = format;
515 lockeddata->Reserved = flags;
517 if(pbmi->bmiHeader.biHeight > 0){
518 lockeddata->Stride = -stride;
519 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
520 stride * (abs_height - 1 - act_rect.Y);
523 lockeddata->Stride = stride;
524 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
527 bitmap->lockmode = flags;
530 bitmap->bitmapbits = buff;
536 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
538 FIXME("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
540 return NotImplemented;
543 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
544 BitmapData* lockeddata)
547 HBITMAP hbm, old = NULL;
551 if(!bitmap || !lockeddata)
552 return InvalidParameter;
554 if(!bitmap->lockmode)
557 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
558 return NotImplemented;
560 if(lockeddata->Reserved & ImageLockModeRead){
561 if(!(--bitmap->numlocks))
562 bitmap->lockmode = 0;
564 GdipFree(bitmap->bitmapbits);
565 bitmap->bitmapbits = NULL;
569 if (!bitmap->bitmapbits)
571 /* we passed a direct reference; no need to do anything */
572 bitmap->lockmode = 0;
576 hbm = bitmap->hbitmap;
578 bm_is_selected = (hdc != 0);
580 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
581 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
582 pbmi->bmiHeader.biBitCount = 0;
585 hdc = CreateCompatibleDC(0);
586 old = SelectObject(hdc, hbm);
589 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
590 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
591 SetDIBits(hdc, hbm, 0, abs(pbmi->bmiHeader.biHeight),
592 bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
595 SelectObject(hdc, old);
600 GdipFree(bitmap->bitmapbits);
601 bitmap->bitmapbits = NULL;
602 bitmap->lockmode = 0;
607 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
608 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
610 BitmapData lockeddata_src, lockeddata_dst;
616 TRACE("(%f,%f,%f,%f,%i,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
618 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
620 x + width > srcBitmap->width || y + height > srcBitmap->height)
622 TRACE("<-- InvalidParameter\n");
623 return InvalidParameter;
626 if (format == PixelFormatDontCare)
627 format = srcBitmap->format;
631 area.Width = roundr(width);
632 area.Height = roundr(height);
634 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
636 if (stat != Ok) return stat;
638 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
639 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
642 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
643 lockeddata_src.PixelFormat, &lockeddata_dst);
647 /* copy the image data */
648 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
649 for (i=0; i<lockeddata_src.Height; i++)
650 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
651 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
654 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
658 GdipDisposeImage((GpImage*)*dstBitmap);
661 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
671 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
672 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
674 TRACE("(%i,%i,%i,%i,%i,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
676 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
679 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
681 GpStatus stat = GenericError;
683 TRACE("%p, %p\n", image, cloneImage);
685 if (!image || !cloneImage)
686 return InvalidParameter;
695 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
699 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
702 WARN("Failed to save image on stream\n");
706 /* Set seek pointer back to the beginning of the picture */
708 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
712 stat = GdipLoadImageFromStream(stream, cloneImage);
713 if (stat != Ok) WARN("Failed to load image from stream\n");
716 IStream_Release(stream);
719 else if (image->type == ImageTypeBitmap)
721 GpBitmap *bitmap = (GpBitmap*)image;
722 BitmapData lockeddata_src, lockeddata_dst;
726 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
728 if (stat != Ok) return stat;
730 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
731 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
734 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
735 lockeddata_src.PixelFormat, &lockeddata_dst);
739 /* copy the image data */
740 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
741 for (i=0; i<lockeddata_src.Height; i++)
742 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
743 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
746 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
750 GdipDisposeImage(*cloneImage);
753 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
759 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
765 ERR("GpImage with no IPicture or bitmap?!\n");
766 return NotImplemented;
770 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
776 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
778 if(!filename || !bitmap)
779 return InvalidParameter;
781 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
786 stat = GdipCreateBitmapFromStream(stream, bitmap);
788 IStream_Release(stream);
793 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
794 VOID *bits, GpBitmap **bitmap)
796 DWORD height, stride;
799 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
801 height = abs(info->bmiHeader.biHeight);
802 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
804 if(info->bmiHeader.biHeight > 0) /* bottom-up */
806 bits = (BYTE*)bits + (height - 1) * stride;
810 switch(info->bmiHeader.biBitCount) {
812 format = PixelFormat1bppIndexed;
815 format = PixelFormat4bppIndexed;
818 format = PixelFormat8bppIndexed;
821 format = PixelFormat24bppRGB;
824 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
826 return InvalidParameter;
829 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
835 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
838 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
840 return GdipCreateBitmapFromFile(filename, bitmap);
843 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
844 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
847 GpStatus stat = InvalidParameter;
849 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
851 if(!lpBitmapName || !bitmap)
852 return InvalidParameter;
855 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
856 LR_CREATEDIBSECTION);
859 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
866 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
867 HBITMAP* hbmReturn, ARGB background)
870 HBITMAP result, oldbitmap;
873 GpGraphics *graphics;
874 BITMAPINFOHEADER bih;
876 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
878 if (!bitmap || !hbmReturn) return InvalidParameter;
880 GdipGetImageWidth((GpImage*)bitmap, &width);
881 GdipGetImageHeight((GpImage*)bitmap, &height);
883 bih.biSize = sizeof(bih);
885 bih.biHeight = height;
888 bih.biCompression = BI_RGB;
890 bih.biXPelsPerMeter = 0;
891 bih.biYPelsPerMeter = 0;
893 bih.biClrImportant = 0;
895 hdc = CreateCompatibleDC(NULL);
896 if (!hdc) return GenericError;
898 result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
903 oldbitmap = SelectObject(hdc, result);
905 stat = GdipCreateFromHDC(hdc, &graphics);
908 stat = GdipGraphicsClear(graphics, background);
911 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
913 GdipDeleteGraphics(graphics);
916 SelectObject(hdc, oldbitmap);
923 if (stat != Ok && result)
925 DeleteObject(result);
934 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
935 GpMetafile* metafile, BOOL* succ, EmfType emfType,
936 const WCHAR* description, GpMetafile** out_metafile)
940 if(!ref || !metafile || !out_metafile)
941 return InvalidParameter;
944 *out_metafile = NULL;
947 FIXME("not implemented\n");
949 return NotImplemented;
952 /* FIXME: this should create a bitmap in the given size with the attributes
953 * (resolution etc.) of the graphics object */
954 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
955 GpGraphics* target, GpBitmap** bitmap)
960 if(!target || !bitmap)
961 return InvalidParameter;
964 FIXME("hacked stub\n");
966 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
972 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
980 BitmapData lockeddata;
985 BITMAPINFOHEADER bih;
990 TRACE("%p, %p\n", hicon, bitmap);
992 if(!bitmap || !GetIconInfo(hicon, &iinfo))
993 return InvalidParameter;
995 /* get the size of the icon */
996 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
998 DeleteObject(iinfo.hbmColor);
999 DeleteObject(iinfo.hbmMask);
1000 return GenericError;
1006 height = abs(bm.bmHeight);
1007 else /* combined bitmap + mask */
1008 height = abs(bm.bmHeight) / 2;
1010 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1012 DeleteObject(iinfo.hbmColor);
1013 DeleteObject(iinfo.hbmMask);
1017 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1019 DeleteObject(iinfo.hbmColor);
1020 DeleteObject(iinfo.hbmMask);
1021 HeapFree(GetProcessHeap(), 0, bits);
1028 rect.Height = height;
1030 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1032 DeleteObject(iinfo.hbmColor);
1033 DeleteObject(iinfo.hbmMask);
1034 HeapFree(GetProcessHeap(), 0, bits);
1035 GdipDisposeImage((GpImage*)*bitmap);
1039 bih.biSize = sizeof(bih);
1040 bih.biWidth = width;
1041 bih.biHeight = -height;
1043 bih.biBitCount = 32;
1044 bih.biCompression = BI_RGB;
1045 bih.biSizeImage = 0;
1046 bih.biXPelsPerMeter = 0;
1047 bih.biYPelsPerMeter = 0;
1049 bih.biClrImportant = 0;
1051 screendc = GetDC(0);
1054 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1056 if (bm.bmBitsPixel == 32)
1060 /* If any pixel has a non-zero alpha, ignore hbmMask */
1062 for (x=0; x<width && !has_alpha; x++)
1063 for (y=0; y<height && !has_alpha; y++)
1064 if ((*src++ & 0xff000000) != 0)
1067 else has_alpha = FALSE;
1071 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1075 /* copy the image data to the Bitmap */
1077 dst_row = lockeddata.Scan0;
1078 for (y=0; y<height; y++)
1080 memcpy(dst_row, src, width*4);
1082 dst_row += lockeddata.Stride;
1089 /* read alpha data from the mask */
1091 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1093 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1096 dst_row = lockeddata.Scan0;
1097 for (y=0; y<height; y++)
1099 dst = (DWORD*)dst_row;
1100 for (x=0; x<height; x++)
1102 DWORD src_value = *src++;
1106 *dst++ |= 0xff000000;
1108 dst_row += lockeddata.Stride;
1113 /* set constant alpha of 255 */
1115 for (y=0; y<height; y++)
1117 dst = (DWORD*)dst_row;
1118 for (x=0; x<height; x++)
1119 *dst++ |= 0xff000000;
1120 dst_row += lockeddata.Stride;
1125 ReleaseDC(0, screendc);
1127 DeleteObject(iinfo.hbmColor);
1128 DeleteObject(iinfo.hbmMask);
1130 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1132 HeapFree(GetProcessHeap(), 0, bits);
1137 static void generate_halftone_palette(ARGB *entries, UINT count)
1139 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1142 for (i=0; i<8 && i<count; i++)
1144 entries[i] = 0xff000000;
1145 if (i&1) entries[i] |= 0x800000;
1146 if (i&2) entries[i] |= 0x8000;
1147 if (i&4) entries[i] |= 0x80;
1151 entries[i] = 0xffc0c0c0;
1153 for (i=9; i<16 && i<count; i++)
1155 entries[i] = 0xff000000;
1156 if (i&1) entries[i] |= 0xff0000;
1157 if (i&2) entries[i] |= 0xff00;
1158 if (i&4) entries[i] |= 0xff;
1161 for (i=16; i<40 && i<count; i++)
1166 for (i=40; i<256 && i<count; i++)
1168 entries[i] = 0xff000000;
1169 entries[i] |= halftone_values[(i-40)%6];
1170 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1171 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1175 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1176 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1178 BITMAPINFOHEADER bmih;
1180 INT row_size, dib_stride;
1185 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
1187 if (!bitmap) return InvalidParameter;
1189 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1191 return InvalidParameter;
1194 if(scan0 && !stride)
1195 return InvalidParameter;
1197 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1198 dib_stride = (row_size + 3) & ~3;
1201 stride = dib_stride;
1203 bmih.biSize = sizeof(BITMAPINFOHEADER);
1204 bmih.biWidth = width;
1205 bmih.biHeight = -height;
1207 /* FIXME: use the rest of the data from format */
1208 bmih.biBitCount = PIXELFORMATBPP(format);
1209 bmih.biCompression = BI_RGB;
1210 bmih.biSizeImage = 0;
1211 bmih.biXPelsPerMeter = 0;
1212 bmih.biYPelsPerMeter = 0;
1214 bmih.biClrImportant = 0;
1216 hdc = CreateCompatibleDC(NULL);
1217 if (!hdc) return GenericError;
1219 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, (void**)&bits,
1224 if (!hbitmap) return GenericError;
1226 /* copy bits to the dib if necessary */
1227 /* FIXME: should reference the bits instead of copying them */
1229 for (i=0; i<height; i++)
1230 memcpy(bits+i*dib_stride, scan0+i*stride, row_size);
1232 *bitmap = GdipAlloc(sizeof(GpBitmap));
1235 DeleteObject(hbitmap);
1239 (*bitmap)->image.type = ImageTypeBitmap;
1240 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1241 (*bitmap)->image.flags = ImageFlagsNone;
1242 (*bitmap)->image.palette_flags = 0;
1243 (*bitmap)->image.palette_count = 0;
1244 (*bitmap)->image.palette_size = 0;
1245 (*bitmap)->image.palette_entries = NULL;
1246 (*bitmap)->width = width;
1247 (*bitmap)->height = height;
1248 (*bitmap)->format = format;
1249 (*bitmap)->image.picture = NULL;
1250 (*bitmap)->hbitmap = hbitmap;
1251 (*bitmap)->hdc = NULL;
1252 (*bitmap)->bits = bits;
1253 (*bitmap)->stride = dib_stride;
1255 if (format == PixelFormat1bppIndexed ||
1256 format == PixelFormat4bppIndexed ||
1257 format == PixelFormat8bppIndexed)
1259 (*bitmap)->image.palette_size = (*bitmap)->image.palette_count = 1 << PIXELFORMATBPP(format);
1260 (*bitmap)->image.palette_entries = GdipAlloc(sizeof(ARGB) * ((*bitmap)->image.palette_size));
1262 if (!(*bitmap)->image.palette_entries)
1264 GdipDisposeImage(&(*bitmap)->image);
1269 if (format == PixelFormat1bppIndexed)
1271 (*bitmap)->image.palette_flags = PaletteFlagsGrayScale;
1272 (*bitmap)->image.palette_entries[0] = 0xff000000;
1273 (*bitmap)->image.palette_entries[1] = 0xffffffff;
1277 if (format == PixelFormat8bppIndexed)
1278 (*bitmap)->image.palette_flags = PaletteFlagsHalftone;
1280 generate_halftone_palette((*bitmap)->image.palette_entries,
1281 (*bitmap)->image.palette_count);
1285 TRACE("<-- %p\n", *bitmap);
1290 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1295 TRACE("%p %p\n", stream, bitmap);
1297 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1302 if((*bitmap)->image.type != ImageTypeBitmap){
1303 GdipDisposeImage(&(*bitmap)->image);
1305 return GenericError; /* FIXME: what error to return? */
1312 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1315 TRACE("%p %p\n", stream, bitmap);
1317 return GdipCreateBitmapFromStream(stream, bitmap);
1320 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1321 GpCachedBitmap **cachedbmp)
1325 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1327 if(!bitmap || !graphics || !cachedbmp)
1328 return InvalidParameter;
1330 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1334 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1336 GdipFree(*cachedbmp);
1343 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1345 FIXME("(%p, %p)\n", bitmap, hicon);
1347 return NotImplemented;
1350 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1352 TRACE("%p\n", cachedbmp);
1355 return InvalidParameter;
1357 GdipDisposeImage(cachedbmp->image);
1358 GdipFree(cachedbmp);
1363 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1364 GpCachedBitmap *cachedbmp, INT x, INT y)
1366 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1368 if(!graphics || !cachedbmp)
1369 return InvalidParameter;
1371 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1374 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1375 LPBYTE pData16, INT iMapMode, INT eFlags)
1377 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1378 return NotImplemented;
1381 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
1383 TRACE("%p\n", image);
1386 return InvalidParameter;
1389 IPicture_Release(image->picture);
1390 if (image->type == ImageTypeBitmap)
1392 GdipFree(((GpBitmap*)image)->bitmapbits);
1393 DeleteDC(((GpBitmap*)image)->hdc);
1395 GdipFree(image->palette_entries);
1401 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
1404 return InvalidParameter;
1406 return NotImplemented;
1409 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
1412 TRACE("%p %p %p\n", image, srcRect, srcUnit);
1414 if(!image || !srcRect || !srcUnit)
1415 return InvalidParameter;
1416 if(image->type == ImageTypeMetafile){
1417 *srcRect = ((GpMetafile*)image)->bounds;
1418 *srcUnit = ((GpMetafile*)image)->unit;
1420 else if(image->type == ImageTypeBitmap){
1421 srcRect->X = srcRect->Y = 0.0;
1422 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
1423 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
1424 *srcUnit = UnitPixel;
1427 srcRect->X = srcRect->Y = 0.0;
1428 srcRect->Width = ipicture_pixel_width(image->picture);
1429 srcRect->Height = ipicture_pixel_height(image->picture);
1430 *srcUnit = UnitPixel;
1433 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
1434 srcRect->Width, srcRect->Height, *srcUnit);
1439 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
1442 TRACE("%p %p %p\n", image, width, height);
1444 if(!image || !height || !width)
1445 return InvalidParameter;
1447 if(image->type == ImageTypeMetafile){
1450 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1451 ((GpMetafile*)image)->bounds.Height;
1453 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1454 ((GpMetafile*)image)->bounds.Width;
1459 else if(image->type == ImageTypeBitmap){
1460 *height = ((GpBitmap*)image)->height;
1461 *width = ((GpBitmap*)image)->width;
1464 *height = ipicture_pixel_height(image->picture);
1465 *width = ipicture_pixel_width(image->picture);
1468 TRACE("returning (%f, %f)\n", *height, *width);
1472 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
1473 GpGraphics **graphics)
1477 TRACE("%p %p\n", image, graphics);
1479 if(!image || !graphics)
1480 return InvalidParameter;
1482 if(image->type != ImageTypeBitmap){
1483 FIXME("not implemented for image type %d\n", image->type);
1484 return NotImplemented;
1487 hdc = ((GpBitmap*)image)->hdc;
1490 hdc = CreateCompatibleDC(0);
1491 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
1492 ((GpBitmap*)image)->hdc = hdc;
1495 return GdipCreateFromHDC(hdc, graphics);
1498 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
1500 TRACE("%p %p\n", image, height);
1502 if(!image || !height)
1503 return InvalidParameter;
1505 if(image->type == ImageTypeMetafile){
1508 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1509 ((GpMetafile*)image)->bounds.Height);
1513 else if(image->type == ImageTypeBitmap)
1514 *height = ((GpBitmap*)image)->height;
1516 *height = ipicture_pixel_height(image->picture);
1518 TRACE("returning %d\n", *height);
1523 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
1528 return InvalidParameter;
1531 FIXME("not implemented\n");
1533 return NotImplemented;
1536 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
1538 TRACE("%p %p\n", image, size);
1541 return InvalidParameter;
1543 if (image->palette_count == 0)
1544 *size = sizeof(ColorPalette);
1546 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette_count;
1548 TRACE("<-- %u\n", *size);
1553 /* FIXME: test this function for non-bitmap types */
1554 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
1556 TRACE("%p %p\n", image, format);
1558 if(!image || !format)
1559 return InvalidParameter;
1561 if(image->type != ImageTypeBitmap)
1562 *format = PixelFormat24bppRGB;
1564 *format = ((GpBitmap*) image)->format;
1569 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
1571 if(!image || !format)
1572 return InvalidParameter;
1574 memcpy(format, &image->format, sizeof(GUID));
1579 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
1581 TRACE("%p %p\n", image, type);
1584 return InvalidParameter;
1586 *type = image->type;
1591 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
1596 return InvalidParameter;
1599 FIXME("not implemented\n");
1601 return NotImplemented;
1604 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
1606 TRACE("%p %p\n", image, width);
1608 if(!image || !width)
1609 return InvalidParameter;
1611 if(image->type == ImageTypeMetafile){
1614 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1615 ((GpMetafile*)image)->bounds.Width);
1619 else if(image->type == ImageTypeBitmap)
1620 *width = ((GpBitmap*)image)->width;
1622 *width = ipicture_pixel_width(image->picture);
1624 TRACE("returning %d\n", *width);
1629 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
1630 MetafileHeader * header)
1634 if(!metafile || !header)
1635 return InvalidParameter;
1638 FIXME("not implemented\n");
1643 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
1644 UINT num, PropertyItem* items)
1649 FIXME("not implemented\n");
1651 return InvalidParameter;
1654 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
1659 FIXME("not implemented\n");
1661 return InvalidParameter;
1664 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
1669 FIXME("not implemented\n");
1671 return InvalidParameter;
1674 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
1675 PropertyItem* buffer)
1680 FIXME("not implemented\n");
1682 return InvalidParameter;
1685 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
1690 TRACE("%p %x %p\n", image, pid, size);
1693 return InvalidParameter;
1696 FIXME("not implemented\n");
1698 return NotImplemented;
1701 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
1706 FIXME("not implemented\n");
1708 return InvalidParameter;
1711 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
1712 GDIPCONST GUID* dimensionID, UINT* count)
1716 if(!image || !dimensionID || !count)
1717 return InvalidParameter;
1720 FIXME("not implemented\n");
1722 return NotImplemented;
1725 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
1728 if(!image || !count)
1729 return InvalidParameter;
1738 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
1739 GUID* dimensionIDs, UINT count)
1743 if(!image || !dimensionIDs)
1744 return InvalidParameter;
1747 FIXME("not implemented\n");
1752 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
1753 GDIPCONST GUID* dimensionID, UINT frameidx)
1757 if(!image || !dimensionID)
1758 return InvalidParameter;
1761 FIXME("not implemented\n");
1766 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
1772 TRACE("(%s) %p\n", debugstr_w(filename), image);
1774 if (!filename || !image)
1775 return InvalidParameter;
1777 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1782 stat = GdipLoadImageFromStream(stream, image);
1784 IStream_Release(stream);
1789 /* FIXME: no icm handling */
1790 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
1792 TRACE("(%s) %p\n", debugstr_w(filename), image);
1794 return GdipLoadImageFromFile(filename, image);
1797 static const WICPixelFormatGUID *wic_pixel_formats[] = {
1798 &GUID_WICPixelFormat16bppBGR555,
1799 &GUID_WICPixelFormat24bppBGR,
1800 &GUID_WICPixelFormat32bppBGR,
1801 &GUID_WICPixelFormat32bppBGRA,
1802 &GUID_WICPixelFormat32bppPBGRA,
1806 static const PixelFormat wic_gdip_formats[] = {
1807 PixelFormat16bppRGB555,
1808 PixelFormat24bppRGB,
1809 PixelFormat32bppRGB,
1810 PixelFormat32bppARGB,
1811 PixelFormat32bppPARGB,
1814 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
1819 IWICBitmapDecoder *decoder;
1820 IWICBitmapFrameDecode *frame;
1821 IWICBitmapSource *source=NULL;
1822 WICPixelFormatGUID wic_format;
1823 PixelFormat gdip_format=0;
1826 BitmapData lockeddata;
1830 initresult = CoInitialize(NULL);
1832 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
1833 &IID_IWICBitmapDecoder, (void**)&decoder);
1834 if (FAILED(hr)) goto end;
1836 hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
1838 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
1840 if (SUCCEEDED(hr)) /* got frame */
1842 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
1846 for (i=0; wic_pixel_formats[i]; i++)
1848 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
1850 source = (IWICBitmapSource*)frame;
1851 IWICBitmapSource_AddRef(source);
1852 gdip_format = wic_gdip_formats[i];
1858 /* unknown format; fall back on 32bppARGB */
1859 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
1860 gdip_format = PixelFormat32bppARGB;
1864 if (SUCCEEDED(hr)) /* got source */
1866 hr = IWICBitmapSource_GetSize(source, &width, &height);
1869 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
1872 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
1874 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
1875 gdip_format, &lockeddata);
1876 if (status == Ok) /* locked bitmap */
1881 for (i=0; i<height; i++)
1884 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
1885 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
1886 if (FAILED(hr)) break;
1889 GdipBitmapUnlockBits(bitmap, &lockeddata);
1892 if (SUCCEEDED(hr) && status == Ok)
1893 *image = (GpImage*)bitmap;
1897 GdipDisposeImage((GpImage*)bitmap);
1901 IWICBitmapSource_Release(source);
1904 IWICBitmapFrameDecode_Release(frame);
1907 IWICBitmapDecoder_Release(decoder);
1910 if (SUCCEEDED(initresult)) CoUninitialize();
1912 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
1917 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
1919 return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
1922 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, GpImage **image)
1927 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, image);
1929 bitmap = (GpBitmap*)*image;
1931 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
1933 /* WIC supports bmp files with alpha, but gdiplus does not */
1934 bitmap->format = PixelFormat32bppRGB;
1940 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
1942 return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
1945 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, GpImage **image)
1947 return decode_image_wic(stream, &CLSID_WICPngDecoder, image);
1950 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
1952 return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
1955 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
1959 TRACE("%p %p\n", stream, image);
1961 if(!stream || !image)
1962 return InvalidParameter;
1964 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1965 (LPVOID*) &pic) != S_OK){
1966 TRACE("Could not load picture\n");
1967 return GenericError;
1970 /* FIXME: missing initialization code */
1971 *image = GdipAlloc(sizeof(GpMetafile));
1972 if(!*image) return OutOfMemory;
1973 (*image)->type = ImageTypeMetafile;
1974 (*image)->picture = pic;
1975 (*image)->flags = ImageFlagsNone;
1976 (*image)->palette_flags = 0;
1977 (*image)->palette_count = 0;
1978 (*image)->palette_size = 0;
1979 (*image)->palette_entries = NULL;
1981 TRACE("<-- %p\n", *image);
1986 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
1987 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
1989 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
1991 typedef struct image_codec {
1992 ImageCodecInfo info;
1993 encode_image_func encode_func;
1994 decode_image_func decode_func;
2008 static const struct image_codec codecs[NUM_CODECS];
2010 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
2018 /* seek to the start of the stream */
2020 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2021 if (FAILED(hr)) return hresult_to_status(hr);
2023 /* read the first 8 bytes */
2024 /* FIXME: This assumes all codecs have one signature <= 8 bytes in length */
2025 hr = IStream_Read(stream, signature, 8, &bytesread);
2026 if (FAILED(hr)) return hresult_to_status(hr);
2027 if (hr == S_FALSE || bytesread == 0) return GenericError;
2029 for (i = 0; i < NUM_CODECS; i++) {
2030 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
2031 bytesread >= codecs[i].info.SigSize)
2033 for (j=0; j<codecs[i].info.SigSize; j++)
2034 if ((signature[j] & codecs[i].info.SigMask[j]) != codecs[i].info.SigPattern[j])
2036 if (j == codecs[i].info.SigSize)
2038 *result = &codecs[i];
2044 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
2045 signature[0],signature[1],signature[2],signature[3],
2046 signature[4],signature[5],signature[6],signature[7]);
2048 return GenericError;
2051 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
2056 const struct image_codec *codec=NULL;
2058 /* choose an appropriate image decoder */
2059 stat = get_decoder_info(stream, &codec);
2060 if (stat != Ok) return stat;
2062 /* seek to the start of the stream */
2064 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2065 if (FAILED(hr)) return hresult_to_status(hr);
2067 /* call on the image decoder to do the real work */
2068 stat = codec->decode_func(stream, &codec->info.Clsid, image);
2070 /* take note of the original data format */
2073 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
2080 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
2082 TRACE("%p %p\n", stream, image);
2084 return GdipLoadImageFromStream(stream, image);
2087 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
2092 return InvalidParameter;
2095 FIXME("not implemented\n");
2097 return NotImplemented;
2100 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
2105 FIXME("not implemented\n");
2107 return NotImplemented;
2110 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
2111 GDIPCONST CLSID *clsidEncoder,
2112 GDIPCONST EncoderParameters *encoderParams)
2117 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
2119 if (!image || !filename|| !clsidEncoder)
2120 return InvalidParameter;
2122 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
2124 return GenericError;
2126 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
2128 IStream_Release(stream);
2132 /*************************************************************************
2133 * Encoding functions -
2134 * These functions encode an image in different image file formats.
2136 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
2137 #define BITMAP_FORMAT_JPEG 0xd8ff
2138 #define BITMAP_FORMAT_GIF 0x4947
2139 #define BITMAP_FORMAT_PNG 0x5089
2140 #define BITMAP_FORMAT_APM 0xcdd7
2142 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
2143 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2147 IWICBitmapEncoder *encoder;
2148 IWICBitmapFrameEncode *frameencode;
2149 IPropertyBag2 *encoderoptions;
2152 PixelFormat gdipformat=0;
2153 WICPixelFormatGUID wicformat;
2155 BitmapData lockeddata;
2159 if (image->type != ImageTypeBitmap)
2160 return GenericError;
2162 bitmap = (GpBitmap*)image;
2164 GdipGetImageWidth(image, &width);
2165 GdipGetImageHeight(image, &height);
2172 initresult = CoInitialize(NULL);
2174 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2175 &IID_IWICBitmapEncoder, (void**)&encoder);
2178 if (SUCCEEDED(initresult)) CoUninitialize();
2179 return hresult_to_status(hr);
2182 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2186 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
2189 if (SUCCEEDED(hr)) /* created frame */
2191 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
2194 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
2197 /* FIXME: use the resolution from the image */
2198 hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
2202 for (i=0; wic_pixel_formats[i]; i++)
2204 if (wic_gdip_formats[i] == bitmap->format)
2207 if (wic_pixel_formats[i])
2208 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
2210 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
2212 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
2214 for (i=0; wic_pixel_formats[i]; i++)
2216 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
2219 if (wic_pixel_formats[i])
2220 gdipformat = wic_gdip_formats[i];
2223 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
2230 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
2235 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
2238 /* write one row at a time in case stride is negative */
2239 row = lockeddata.Scan0;
2240 for (i=0; i<lockeddata.Height; i++)
2242 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
2243 if (FAILED(hr)) break;
2244 row += lockeddata.Stride;
2247 GdipBitmapUnlockBits(bitmap, &lockeddata);
2254 hr = IWICBitmapFrameEncode_Commit(frameencode);
2256 IWICBitmapFrameEncode_Release(frameencode);
2257 IPropertyBag2_Release(encoderoptions);
2261 hr = IWICBitmapEncoder_Commit(encoder);
2263 IWICBitmapEncoder_Release(encoder);
2265 if (SUCCEEDED(initresult)) CoUninitialize();
2267 return hresult_to_status(hr);
2270 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
2271 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2273 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
2276 static GpStatus encode_image_png(GpImage *image, IStream* stream,
2277 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2279 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
2282 /*****************************************************************************
2283 * GdipSaveImageToStream [GDIPLUS.@]
2285 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
2286 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2289 encode_image_func encode_image;
2292 TRACE("%p %p %p %p\n", image, stream, clsid, params);
2294 if(!image || !stream)
2295 return InvalidParameter;
2297 /* select correct encoder */
2298 encode_image = NULL;
2299 for (i = 0; i < NUM_CODECS; i++) {
2300 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
2301 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
2302 encode_image = codecs[i].encode_func;
2304 if (encode_image == NULL)
2305 return UnknownImageFormat;
2307 stat = encode_image(image, stream, clsid, params);
2312 /*****************************************************************************
2313 * GdipGetImagePalette [GDIPLUS.@]
2315 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
2317 TRACE("(%p,%p,%i)\n", image, palette, size);
2319 if (!image || !palette)
2320 return InvalidParameter;
2322 if (size < (sizeof(UINT)*2+sizeof(ARGB)*image->palette_count))
2324 TRACE("<-- InsufficientBuffer\n");
2325 return InsufficientBuffer;
2328 palette->Flags = image->palette_flags;
2329 palette->Count = image->palette_count;
2330 memcpy(palette->Entries, image->palette_entries, sizeof(ARGB)*image->palette_count);
2335 /*****************************************************************************
2336 * GdipSetImagePalette [GDIPLUS.@]
2338 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
2339 GDIPCONST ColorPalette *palette)
2341 TRACE("(%p,%p)\n", image, palette);
2343 if(!image || !palette || palette->Count > 256)
2344 return InvalidParameter;
2346 if (palette->Count > image->palette_size)
2350 new_palette = GdipAlloc(sizeof(ARGB) * palette->Count);
2351 if (!new_palette) return OutOfMemory;
2353 GdipFree(image->palette_entries);
2354 image->palette_entries = new_palette;
2355 image->palette_size = palette->Count;
2358 image->palette_flags = palette->Flags;
2359 image->palette_count = palette->Count;
2360 memcpy(image->palette_entries, palette->Entries, sizeof(ARGB)*palette->Count);
2365 /*************************************************************************
2367 * Structures that represent which formats we support for encoding.
2370 /* ImageCodecInfo creation routines taken from libgdiplus */
2371 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
2372 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
2373 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
2374 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
2375 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
2376 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
2378 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
2379 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
2380 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
2381 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
2382 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
2383 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
2385 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
2386 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
2387 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
2388 static const WCHAR gif_format[] = {'G','I','F',0};
2389 static const BYTE gif_sig_pattern[4] = "GIF8";
2390 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2392 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
2393 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
2394 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
2395 static const WCHAR emf_format[] = {'E','M','F',0};
2396 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
2397 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2399 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
2400 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
2401 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
2402 static const WCHAR wmf_format[] = {'W','M','F',0};
2403 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
2404 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
2406 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
2407 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
2408 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
2409 static const WCHAR png_format[] = {'P','N','G',0};
2410 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
2411 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2413 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
2414 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
2415 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
2416 static const WCHAR ico_format[] = {'I','C','O',0};
2417 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
2418 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2420 static const struct image_codec codecs[NUM_CODECS] = {
2423 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2424 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2425 /* CodecName */ bmp_codecname,
2427 /* FormatDescription */ bmp_format,
2428 /* FilenameExtension */ bmp_extension,
2429 /* MimeType */ bmp_mimetype,
2430 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2434 /* SigPattern */ bmp_sig_pattern,
2435 /* SigMask */ bmp_sig_mask,
2442 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2443 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2444 /* CodecName */ jpeg_codecname,
2446 /* FormatDescription */ jpeg_format,
2447 /* FilenameExtension */ jpeg_extension,
2448 /* MimeType */ jpeg_mimetype,
2449 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2453 /* SigPattern */ jpeg_sig_pattern,
2454 /* SigMask */ jpeg_sig_mask,
2461 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2462 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2463 /* CodecName */ gif_codecname,
2465 /* FormatDescription */ gif_format,
2466 /* FilenameExtension */ gif_extension,
2467 /* MimeType */ gif_mimetype,
2468 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2472 /* SigPattern */ gif_sig_pattern,
2473 /* SigMask */ gif_sig_mask,
2480 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2481 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2482 /* CodecName */ emf_codecname,
2484 /* FormatDescription */ emf_format,
2485 /* FilenameExtension */ emf_extension,
2486 /* MimeType */ emf_mimetype,
2487 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2491 /* SigPattern */ emf_sig_pattern,
2492 /* SigMask */ emf_sig_mask,
2495 decode_image_olepicture_metafile
2499 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2500 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2501 /* CodecName */ wmf_codecname,
2503 /* FormatDescription */ wmf_format,
2504 /* FilenameExtension */ wmf_extension,
2505 /* MimeType */ wmf_mimetype,
2506 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2510 /* SigPattern */ wmf_sig_pattern,
2511 /* SigMask */ wmf_sig_mask,
2514 decode_image_olepicture_metafile
2518 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2519 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2520 /* CodecName */ png_codecname,
2522 /* FormatDescription */ png_format,
2523 /* FilenameExtension */ png_extension,
2524 /* MimeType */ png_mimetype,
2525 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2529 /* SigPattern */ png_sig_pattern,
2530 /* SigMask */ png_sig_mask,
2537 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2538 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2539 /* CodecName */ ico_codecname,
2541 /* FormatDescription */ ico_format,
2542 /* FilenameExtension */ ico_extension,
2543 /* MimeType */ ico_mimetype,
2544 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2548 /* SigPattern */ ico_sig_pattern,
2549 /* SigMask */ ico_sig_mask,
2556 /*****************************************************************************
2557 * GdipGetImageDecodersSize [GDIPLUS.@]
2559 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
2561 int decoder_count=0;
2563 TRACE("%p %p\n", numDecoders, size);
2565 if (!numDecoders || !size)
2566 return InvalidParameter;
2568 for (i=0; i<NUM_CODECS; i++)
2570 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2574 *numDecoders = decoder_count;
2575 *size = decoder_count * sizeof(ImageCodecInfo);
2580 /*****************************************************************************
2581 * GdipGetImageDecoders [GDIPLUS.@]
2583 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
2585 int i, decoder_count=0;
2586 TRACE("%u %u %p\n", numDecoders, size, decoders);
2589 size != numDecoders * sizeof(ImageCodecInfo))
2590 return GenericError;
2592 for (i=0; i<NUM_CODECS; i++)
2594 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2596 if (decoder_count == numDecoders) return GenericError;
2597 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2602 if (decoder_count < numDecoders) return GenericError;
2607 /*****************************************************************************
2608 * GdipGetImageEncodersSize [GDIPLUS.@]
2610 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
2612 int encoder_count=0;
2614 TRACE("%p %p\n", numEncoders, size);
2616 if (!numEncoders || !size)
2617 return InvalidParameter;
2619 for (i=0; i<NUM_CODECS; i++)
2621 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2625 *numEncoders = encoder_count;
2626 *size = encoder_count * sizeof(ImageCodecInfo);
2631 /*****************************************************************************
2632 * GdipGetImageEncoders [GDIPLUS.@]
2634 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
2636 int i, encoder_count=0;
2637 TRACE("%u %u %p\n", numEncoders, size, encoders);
2640 size != numEncoders * sizeof(ImageCodecInfo))
2641 return GenericError;
2643 for (i=0; i<NUM_CODECS; i++)
2645 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2647 if (encoder_count == numEncoders) return GenericError;
2648 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2653 if (encoder_count < numEncoders) return GenericError;
2658 /*****************************************************************************
2659 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
2661 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
2666 BitmapData lockeddata;
2669 TRACE("%p %p %p\n", hbm, hpal, bitmap);
2672 return InvalidParameter;
2674 /* TODO: Support for device-dependent bitmaps */
2676 FIXME("no support for device-dependent bitmaps\n");
2677 return NotImplemented;
2680 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
2681 return InvalidParameter;
2683 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
2684 switch(bm.bmBitsPixel) {
2686 format = PixelFormat1bppIndexed;
2689 format = PixelFormat4bppIndexed;
2692 format = PixelFormat8bppIndexed;
2695 format = PixelFormat24bppRGB;
2698 format = PixelFormat32bppRGB;
2701 format = PixelFormat48bppRGB;
2704 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
2705 return InvalidParameter;
2708 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
2709 format, NULL, bitmap);
2713 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
2714 format, &lockeddata);
2719 for (y=0; y<bm.bmHeight; y++)
2721 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
2722 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
2731 INT src_height, dst_stride;
2734 hdc = CreateCompatibleDC(NULL);
2735 oldhbm = SelectObject(hdc, hbm);
2737 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2741 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2742 pbmi->bmiHeader.biBitCount = 0;
2744 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
2746 src_height = abs(pbmi->bmiHeader.biHeight);
2748 if (pbmi->bmiHeader.biHeight > 0)
2750 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
2751 dst_stride = -lockeddata.Stride;
2755 dst_bits = lockeddata.Scan0;
2756 dst_stride = lockeddata.Stride;
2759 for (y=0; y<src_height; y++)
2761 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
2762 pbmi, DIB_RGB_COLORS);
2768 retval = OutOfMemory;
2770 SelectObject(hdc, oldhbm);
2774 GdipBitmapUnlockBits(*bitmap, &lockeddata);
2781 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
2783 FIXME("(%p): stub\n", effect);
2784 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
2785 * in Windows's gdiplus */
2786 return NotImplemented;
2789 /*****************************************************************************
2790 * GdipSetEffectParameters [GDIPLUS.@]
2792 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
2793 const VOID *params, const UINT size)
2798 FIXME("not implemented\n");
2800 return NotImplemented;
2803 /*****************************************************************************
2804 * GdipGetImageFlags [GDIPLUS.@]
2806 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
2808 TRACE("%p %p\n", image, flags);
2810 if(!image || !flags)
2811 return InvalidParameter;
2813 *flags = image->flags;
2818 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
2820 TRACE("(%d, %p)\n", control, param);
2823 case TestControlForceBilinear:
2825 FIXME("TestControlForceBilinear not handled\n");
2827 case TestControlNoICM:
2829 FIXME("TestControlNoICM not handled\n");
2831 case TestControlGetBuildNumber:
2832 *((DWORD*)param) = 3102;
2839 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
2840 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
2841 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
2842 GpMetafile **metafile)
2844 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2845 frameUnit, debugstr_w(desc), metafile);
2847 return NotImplemented;
2850 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
2851 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
2852 GDIPCONST WCHAR *desc, GpMetafile **metafile)
2854 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2855 frameUnit, debugstr_w(desc), metafile);
2857 return NotImplemented;
2860 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
2862 FIXME("%p\n", image);
2867 /*****************************************************************************
2868 * GdipGetImageThumbnail [GDIPLUS.@]
2870 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
2871 GpImage **ret_image, GetThumbnailImageAbort cb,
2874 FIXME("(%p %u %u %p %p %p) stub\n",
2875 image, width, height, ret_image, cb, cb_data);
2876 return NotImplemented;
2879 /*****************************************************************************
2880 * GdipImageRotateFlip [GDIPLUS.@]
2882 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
2884 FIXME("(%p %u) stub\n", image, type);
2885 return NotImplemented;