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 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
540 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
541 return InvalidParameter;
543 bitmap->image.xres = xdpi;
544 bitmap->image.yres = ydpi;
549 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
550 BitmapData* lockeddata)
553 HBITMAP hbm, old = NULL;
557 if(!bitmap || !lockeddata)
558 return InvalidParameter;
560 if(!bitmap->lockmode)
563 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
564 return NotImplemented;
566 if(lockeddata->Reserved & ImageLockModeRead){
567 if(!(--bitmap->numlocks))
568 bitmap->lockmode = 0;
570 GdipFree(bitmap->bitmapbits);
571 bitmap->bitmapbits = NULL;
575 if (!bitmap->bitmapbits)
577 /* we passed a direct reference; no need to do anything */
578 bitmap->lockmode = 0;
582 hbm = bitmap->hbitmap;
584 bm_is_selected = (hdc != 0);
586 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
587 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
588 pbmi->bmiHeader.biBitCount = 0;
591 hdc = CreateCompatibleDC(0);
592 old = SelectObject(hdc, hbm);
595 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
596 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
597 SetDIBits(hdc, hbm, 0, abs(pbmi->bmiHeader.biHeight),
598 bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
601 SelectObject(hdc, old);
606 GdipFree(bitmap->bitmapbits);
607 bitmap->bitmapbits = NULL;
608 bitmap->lockmode = 0;
613 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
614 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
616 BitmapData lockeddata_src, lockeddata_dst;
622 TRACE("(%f,%f,%f,%f,%i,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
624 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
626 x + width > srcBitmap->width || y + height > srcBitmap->height)
628 TRACE("<-- InvalidParameter\n");
629 return InvalidParameter;
632 if (format == PixelFormatDontCare)
633 format = srcBitmap->format;
637 area.Width = roundr(width);
638 area.Height = roundr(height);
640 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
642 if (stat != Ok) return stat;
644 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
645 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
648 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
649 lockeddata_src.PixelFormat, &lockeddata_dst);
653 /* copy the image data */
654 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
655 for (i=0; i<lockeddata_src.Height; i++)
656 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
657 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
660 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
664 GdipDisposeImage((GpImage*)*dstBitmap);
667 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
677 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
678 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
680 TRACE("(%i,%i,%i,%i,%i,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
682 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
685 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
687 GpStatus stat = GenericError;
689 TRACE("%p, %p\n", image, cloneImage);
691 if (!image || !cloneImage)
692 return InvalidParameter;
701 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
705 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
708 WARN("Failed to save image on stream\n");
712 /* Set seek pointer back to the beginning of the picture */
714 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
718 stat = GdipLoadImageFromStream(stream, cloneImage);
719 if (stat != Ok) WARN("Failed to load image from stream\n");
722 IStream_Release(stream);
725 else if (image->type == ImageTypeBitmap)
727 GpBitmap *bitmap = (GpBitmap*)image;
728 BitmapData lockeddata_src, lockeddata_dst;
732 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
734 if (stat != Ok) return stat;
736 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
737 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
740 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
741 lockeddata_src.PixelFormat, &lockeddata_dst);
745 /* copy the image data */
746 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
747 for (i=0; i<lockeddata_src.Height; i++)
748 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
749 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
752 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
756 GdipDisposeImage(*cloneImage);
759 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
765 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
771 ERR("GpImage with no IPicture or bitmap?!\n");
772 return NotImplemented;
776 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
782 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
784 if(!filename || !bitmap)
785 return InvalidParameter;
787 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
792 stat = GdipCreateBitmapFromStream(stream, bitmap);
794 IStream_Release(stream);
799 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
800 VOID *bits, GpBitmap **bitmap)
802 DWORD height, stride;
805 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
807 height = abs(info->bmiHeader.biHeight);
808 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
810 if(info->bmiHeader.biHeight > 0) /* bottom-up */
812 bits = (BYTE*)bits + (height - 1) * stride;
816 switch(info->bmiHeader.biBitCount) {
818 format = PixelFormat1bppIndexed;
821 format = PixelFormat4bppIndexed;
824 format = PixelFormat8bppIndexed;
827 format = PixelFormat24bppRGB;
830 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
832 return InvalidParameter;
835 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
841 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
844 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
846 return GdipCreateBitmapFromFile(filename, bitmap);
849 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
850 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
853 GpStatus stat = InvalidParameter;
855 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
857 if(!lpBitmapName || !bitmap)
858 return InvalidParameter;
861 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
862 LR_CREATEDIBSECTION);
865 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
872 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
873 HBITMAP* hbmReturn, ARGB background)
876 HBITMAP result, oldbitmap;
879 GpGraphics *graphics;
880 BITMAPINFOHEADER bih;
882 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
884 if (!bitmap || !hbmReturn) return InvalidParameter;
886 GdipGetImageWidth((GpImage*)bitmap, &width);
887 GdipGetImageHeight((GpImage*)bitmap, &height);
889 bih.biSize = sizeof(bih);
891 bih.biHeight = height;
894 bih.biCompression = BI_RGB;
896 bih.biXPelsPerMeter = 0;
897 bih.biYPelsPerMeter = 0;
899 bih.biClrImportant = 0;
901 hdc = CreateCompatibleDC(NULL);
902 if (!hdc) return GenericError;
904 result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
909 oldbitmap = SelectObject(hdc, result);
911 stat = GdipCreateFromHDC(hdc, &graphics);
914 stat = GdipGraphicsClear(graphics, background);
917 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
919 GdipDeleteGraphics(graphics);
922 SelectObject(hdc, oldbitmap);
929 if (stat != Ok && result)
931 DeleteObject(result);
940 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
941 GpMetafile* metafile, BOOL* succ, EmfType emfType,
942 const WCHAR* description, GpMetafile** out_metafile)
946 if(!ref || !metafile || !out_metafile)
947 return InvalidParameter;
950 *out_metafile = NULL;
953 FIXME("not implemented\n");
955 return NotImplemented;
958 /* FIXME: this should create a bitmap in the given size with the attributes
959 * (resolution etc.) of the graphics object */
960 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
961 GpGraphics* target, GpBitmap** bitmap)
966 if(!target || !bitmap)
967 return InvalidParameter;
970 FIXME("hacked stub\n");
972 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
978 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
986 BitmapData lockeddata;
991 BITMAPINFOHEADER bih;
996 TRACE("%p, %p\n", hicon, bitmap);
998 if(!bitmap || !GetIconInfo(hicon, &iinfo))
999 return InvalidParameter;
1001 /* get the size of the icon */
1002 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1004 DeleteObject(iinfo.hbmColor);
1005 DeleteObject(iinfo.hbmMask);
1006 return GenericError;
1012 height = abs(bm.bmHeight);
1013 else /* combined bitmap + mask */
1014 height = abs(bm.bmHeight) / 2;
1016 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1018 DeleteObject(iinfo.hbmColor);
1019 DeleteObject(iinfo.hbmMask);
1023 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1025 DeleteObject(iinfo.hbmColor);
1026 DeleteObject(iinfo.hbmMask);
1027 HeapFree(GetProcessHeap(), 0, bits);
1034 rect.Height = height;
1036 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1038 DeleteObject(iinfo.hbmColor);
1039 DeleteObject(iinfo.hbmMask);
1040 HeapFree(GetProcessHeap(), 0, bits);
1041 GdipDisposeImage((GpImage*)*bitmap);
1045 bih.biSize = sizeof(bih);
1046 bih.biWidth = width;
1047 bih.biHeight = -height;
1049 bih.biBitCount = 32;
1050 bih.biCompression = BI_RGB;
1051 bih.biSizeImage = 0;
1052 bih.biXPelsPerMeter = 0;
1053 bih.biYPelsPerMeter = 0;
1055 bih.biClrImportant = 0;
1057 screendc = GetDC(0);
1060 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1062 if (bm.bmBitsPixel == 32)
1066 /* If any pixel has a non-zero alpha, ignore hbmMask */
1068 for (x=0; x<width && !has_alpha; x++)
1069 for (y=0; y<height && !has_alpha; y++)
1070 if ((*src++ & 0xff000000) != 0)
1073 else has_alpha = FALSE;
1077 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1081 /* copy the image data to the Bitmap */
1083 dst_row = lockeddata.Scan0;
1084 for (y=0; y<height; y++)
1086 memcpy(dst_row, src, width*4);
1088 dst_row += lockeddata.Stride;
1095 /* read alpha data from the mask */
1097 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1099 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1102 dst_row = lockeddata.Scan0;
1103 for (y=0; y<height; y++)
1105 dst = (DWORD*)dst_row;
1106 for (x=0; x<height; x++)
1108 DWORD src_value = *src++;
1112 *dst++ |= 0xff000000;
1114 dst_row += lockeddata.Stride;
1119 /* set constant alpha of 255 */
1121 for (y=0; y<height; y++)
1123 dst = (DWORD*)dst_row;
1124 for (x=0; x<height; x++)
1125 *dst++ |= 0xff000000;
1126 dst_row += lockeddata.Stride;
1131 ReleaseDC(0, screendc);
1133 DeleteObject(iinfo.hbmColor);
1134 DeleteObject(iinfo.hbmMask);
1136 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1138 HeapFree(GetProcessHeap(), 0, bits);
1143 static void generate_halftone_palette(ARGB *entries, UINT count)
1145 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1148 for (i=0; i<8 && i<count; i++)
1150 entries[i] = 0xff000000;
1151 if (i&1) entries[i] |= 0x800000;
1152 if (i&2) entries[i] |= 0x8000;
1153 if (i&4) entries[i] |= 0x80;
1157 entries[i] = 0xffc0c0c0;
1159 for (i=9; i<16 && i<count; i++)
1161 entries[i] = 0xff000000;
1162 if (i&1) entries[i] |= 0xff0000;
1163 if (i&2) entries[i] |= 0xff00;
1164 if (i&4) entries[i] |= 0xff;
1167 for (i=16; i<40 && i<count; i++)
1172 for (i=40; i<256 && i<count; i++)
1174 entries[i] = 0xff000000;
1175 entries[i] |= halftone_values[(i-40)%6];
1176 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1177 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1181 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1183 HDC screendc = GetDC(0);
1185 if (!screendc) return GenericError;
1187 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1188 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1190 ReleaseDC(0, screendc);
1195 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1196 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1198 BITMAPINFOHEADER bmih;
1200 INT row_size, dib_stride;
1207 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
1209 if (!bitmap) return InvalidParameter;
1211 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1213 return InvalidParameter;
1216 if(scan0 && !stride)
1217 return InvalidParameter;
1219 stat = get_screen_resolution(&xres, &yres);
1220 if (stat != Ok) return stat;
1222 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1223 dib_stride = (row_size + 3) & ~3;
1226 stride = dib_stride;
1228 bmih.biSize = sizeof(BITMAPINFOHEADER);
1229 bmih.biWidth = width;
1230 bmih.biHeight = -height;
1232 /* FIXME: use the rest of the data from format */
1233 bmih.biBitCount = PIXELFORMATBPP(format);
1234 bmih.biCompression = BI_RGB;
1235 bmih.biSizeImage = 0;
1236 bmih.biXPelsPerMeter = 0;
1237 bmih.biYPelsPerMeter = 0;
1239 bmih.biClrImportant = 0;
1241 hdc = CreateCompatibleDC(NULL);
1242 if (!hdc) return GenericError;
1244 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, (void**)&bits,
1249 if (!hbitmap) return GenericError;
1251 /* copy bits to the dib if necessary */
1252 /* FIXME: should reference the bits instead of copying them */
1254 for (i=0; i<height; i++)
1255 memcpy(bits+i*dib_stride, scan0+i*stride, row_size);
1257 *bitmap = GdipAlloc(sizeof(GpBitmap));
1260 DeleteObject(hbitmap);
1264 (*bitmap)->image.type = ImageTypeBitmap;
1265 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1266 (*bitmap)->image.flags = ImageFlagsNone;
1267 (*bitmap)->image.palette_flags = 0;
1268 (*bitmap)->image.palette_count = 0;
1269 (*bitmap)->image.palette_size = 0;
1270 (*bitmap)->image.palette_entries = NULL;
1271 (*bitmap)->image.xres = xres;
1272 (*bitmap)->image.yres = yres;
1273 (*bitmap)->width = width;
1274 (*bitmap)->height = height;
1275 (*bitmap)->format = format;
1276 (*bitmap)->image.picture = NULL;
1277 (*bitmap)->hbitmap = hbitmap;
1278 (*bitmap)->hdc = NULL;
1279 (*bitmap)->bits = bits;
1280 (*bitmap)->stride = dib_stride;
1282 if (format == PixelFormat1bppIndexed ||
1283 format == PixelFormat4bppIndexed ||
1284 format == PixelFormat8bppIndexed)
1286 (*bitmap)->image.palette_size = (*bitmap)->image.palette_count = 1 << PIXELFORMATBPP(format);
1287 (*bitmap)->image.palette_entries = GdipAlloc(sizeof(ARGB) * ((*bitmap)->image.palette_size));
1289 if (!(*bitmap)->image.palette_entries)
1291 GdipDisposeImage(&(*bitmap)->image);
1296 if (format == PixelFormat1bppIndexed)
1298 (*bitmap)->image.palette_flags = PaletteFlagsGrayScale;
1299 (*bitmap)->image.palette_entries[0] = 0xff000000;
1300 (*bitmap)->image.palette_entries[1] = 0xffffffff;
1304 if (format == PixelFormat8bppIndexed)
1305 (*bitmap)->image.palette_flags = PaletteFlagsHalftone;
1307 generate_halftone_palette((*bitmap)->image.palette_entries,
1308 (*bitmap)->image.palette_count);
1312 TRACE("<-- %p\n", *bitmap);
1317 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1322 TRACE("%p %p\n", stream, bitmap);
1324 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1329 if((*bitmap)->image.type != ImageTypeBitmap){
1330 GdipDisposeImage(&(*bitmap)->image);
1332 return GenericError; /* FIXME: what error to return? */
1339 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1342 TRACE("%p %p\n", stream, bitmap);
1344 return GdipCreateBitmapFromStream(stream, bitmap);
1347 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1348 GpCachedBitmap **cachedbmp)
1352 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1354 if(!bitmap || !graphics || !cachedbmp)
1355 return InvalidParameter;
1357 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1361 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1363 GdipFree(*cachedbmp);
1370 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1372 FIXME("(%p, %p)\n", bitmap, hicon);
1374 return NotImplemented;
1377 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1379 TRACE("%p\n", cachedbmp);
1382 return InvalidParameter;
1384 GdipDisposeImage(cachedbmp->image);
1385 GdipFree(cachedbmp);
1390 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1391 GpCachedBitmap *cachedbmp, INT x, INT y)
1393 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1395 if(!graphics || !cachedbmp)
1396 return InvalidParameter;
1398 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1401 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1402 LPBYTE pData16, INT iMapMode, INT eFlags)
1404 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1405 return NotImplemented;
1408 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
1410 TRACE("%p\n", image);
1413 return InvalidParameter;
1416 IPicture_Release(image->picture);
1417 if (image->type == ImageTypeBitmap)
1419 GdipFree(((GpBitmap*)image)->bitmapbits);
1420 DeleteDC(((GpBitmap*)image)->hdc);
1422 GdipFree(image->palette_entries);
1428 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
1431 return InvalidParameter;
1433 return NotImplemented;
1436 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
1439 TRACE("%p %p %p\n", image, srcRect, srcUnit);
1441 if(!image || !srcRect || !srcUnit)
1442 return InvalidParameter;
1443 if(image->type == ImageTypeMetafile){
1444 *srcRect = ((GpMetafile*)image)->bounds;
1445 *srcUnit = ((GpMetafile*)image)->unit;
1447 else if(image->type == ImageTypeBitmap){
1448 srcRect->X = srcRect->Y = 0.0;
1449 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
1450 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
1451 *srcUnit = UnitPixel;
1454 srcRect->X = srcRect->Y = 0.0;
1455 srcRect->Width = ipicture_pixel_width(image->picture);
1456 srcRect->Height = ipicture_pixel_height(image->picture);
1457 *srcUnit = UnitPixel;
1460 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
1461 srcRect->Width, srcRect->Height, *srcUnit);
1466 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
1469 TRACE("%p %p %p\n", image, width, height);
1471 if(!image || !height || !width)
1472 return InvalidParameter;
1474 if(image->type == ImageTypeMetafile){
1477 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1478 ((GpMetafile*)image)->bounds.Height;
1480 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1481 ((GpMetafile*)image)->bounds.Width;
1486 else if(image->type == ImageTypeBitmap){
1487 *height = ((GpBitmap*)image)->height;
1488 *width = ((GpBitmap*)image)->width;
1491 *height = ipicture_pixel_height(image->picture);
1492 *width = ipicture_pixel_width(image->picture);
1495 TRACE("returning (%f, %f)\n", *height, *width);
1499 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
1500 GpGraphics **graphics)
1504 TRACE("%p %p\n", image, graphics);
1506 if(!image || !graphics)
1507 return InvalidParameter;
1509 if(image->type != ImageTypeBitmap){
1510 FIXME("not implemented for image type %d\n", image->type);
1511 return NotImplemented;
1514 hdc = ((GpBitmap*)image)->hdc;
1517 hdc = CreateCompatibleDC(0);
1518 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
1519 ((GpBitmap*)image)->hdc = hdc;
1522 return GdipCreateFromHDC(hdc, graphics);
1525 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
1527 TRACE("%p %p\n", image, height);
1529 if(!image || !height)
1530 return InvalidParameter;
1532 if(image->type == ImageTypeMetafile){
1535 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1536 ((GpMetafile*)image)->bounds.Height);
1540 else if(image->type == ImageTypeBitmap)
1541 *height = ((GpBitmap*)image)->height;
1543 *height = ipicture_pixel_height(image->picture);
1545 TRACE("returning %d\n", *height);
1550 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
1553 return InvalidParameter;
1557 TRACE("(%p) <-- %0.2f\n", image, *res);
1562 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
1564 TRACE("%p %p\n", image, size);
1567 return InvalidParameter;
1569 if (image->palette_count == 0)
1570 *size = sizeof(ColorPalette);
1572 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette_count;
1574 TRACE("<-- %u\n", *size);
1579 /* FIXME: test this function for non-bitmap types */
1580 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
1582 TRACE("%p %p\n", image, format);
1584 if(!image || !format)
1585 return InvalidParameter;
1587 if(image->type != ImageTypeBitmap)
1588 *format = PixelFormat24bppRGB;
1590 *format = ((GpBitmap*) image)->format;
1595 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
1597 if(!image || !format)
1598 return InvalidParameter;
1600 memcpy(format, &image->format, sizeof(GUID));
1605 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
1607 TRACE("%p %p\n", image, type);
1610 return InvalidParameter;
1612 *type = image->type;
1617 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
1620 return InvalidParameter;
1624 TRACE("(%p) <-- %0.2f\n", image, *res);
1629 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
1631 TRACE("%p %p\n", image, width);
1633 if(!image || !width)
1634 return InvalidParameter;
1636 if(image->type == ImageTypeMetafile){
1639 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1640 ((GpMetafile*)image)->bounds.Width);
1644 else if(image->type == ImageTypeBitmap)
1645 *width = ((GpBitmap*)image)->width;
1647 *width = ipicture_pixel_width(image->picture);
1649 TRACE("returning %d\n", *width);
1654 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
1655 MetafileHeader * header)
1659 if(!metafile || !header)
1660 return InvalidParameter;
1663 FIXME("not implemented\n");
1668 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
1669 UINT num, PropertyItem* items)
1674 FIXME("not implemented\n");
1676 return InvalidParameter;
1679 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
1684 FIXME("not implemented\n");
1686 return InvalidParameter;
1689 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
1694 FIXME("not implemented\n");
1696 return InvalidParameter;
1699 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
1700 PropertyItem* buffer)
1705 FIXME("not implemented\n");
1707 return InvalidParameter;
1710 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
1715 TRACE("%p %x %p\n", image, pid, size);
1718 return InvalidParameter;
1721 FIXME("not implemented\n");
1723 return NotImplemented;
1726 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
1731 FIXME("not implemented\n");
1733 return InvalidParameter;
1736 struct image_format_dimension
1739 const GUID *dimension;
1742 struct image_format_dimension image_format_dimensions[] =
1744 {&ImageFormatGIF, &FrameDimensionTime},
1745 {&ImageFormatIcon, &FrameDimensionResolution},
1749 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
1750 GDIPCONST GUID* dimensionID, UINT* count)
1754 if(!image || !dimensionID || !count)
1755 return InvalidParameter;
1758 FIXME("not implemented\n");
1760 return NotImplemented;
1763 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
1766 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
1768 if(!image || !count)
1769 return InvalidParameter;
1776 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
1777 GUID* dimensionIDs, UINT count)
1780 const GUID *result=NULL;
1782 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
1784 if(!image || !dimensionIDs || count != 1)
1785 return InvalidParameter;
1787 for (i=0; image_format_dimensions[i].format; i++)
1789 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
1791 result = image_format_dimensions[i].dimension;
1797 result = &FrameDimensionPage;
1799 memcpy(dimensionIDs, result, sizeof(GUID));
1804 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
1805 GDIPCONST GUID* dimensionID, UINT frameidx)
1809 if(!image || !dimensionID)
1810 return InvalidParameter;
1813 FIXME("not implemented\n");
1818 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
1824 TRACE("(%s) %p\n", debugstr_w(filename), image);
1826 if (!filename || !image)
1827 return InvalidParameter;
1829 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1834 stat = GdipLoadImageFromStream(stream, image);
1836 IStream_Release(stream);
1841 /* FIXME: no icm handling */
1842 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
1844 TRACE("(%s) %p\n", debugstr_w(filename), image);
1846 return GdipLoadImageFromFile(filename, image);
1849 static const WICPixelFormatGUID *wic_pixel_formats[] = {
1850 &GUID_WICPixelFormat16bppBGR555,
1851 &GUID_WICPixelFormat24bppBGR,
1852 &GUID_WICPixelFormat32bppBGR,
1853 &GUID_WICPixelFormat32bppBGRA,
1854 &GUID_WICPixelFormat32bppPBGRA,
1858 static const PixelFormat wic_gdip_formats[] = {
1859 PixelFormat16bppRGB555,
1860 PixelFormat24bppRGB,
1861 PixelFormat32bppRGB,
1862 PixelFormat32bppARGB,
1863 PixelFormat32bppPARGB,
1866 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
1871 IWICBitmapDecoder *decoder;
1872 IWICBitmapFrameDecode *frame;
1873 IWICBitmapSource *source=NULL;
1874 WICPixelFormatGUID wic_format;
1875 PixelFormat gdip_format=0;
1878 BitmapData lockeddata;
1882 initresult = CoInitialize(NULL);
1884 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
1885 &IID_IWICBitmapDecoder, (void**)&decoder);
1886 if (FAILED(hr)) goto end;
1888 hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
1890 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
1892 if (SUCCEEDED(hr)) /* got frame */
1894 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
1898 for (i=0; wic_pixel_formats[i]; i++)
1900 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
1902 source = (IWICBitmapSource*)frame;
1903 IWICBitmapSource_AddRef(source);
1904 gdip_format = wic_gdip_formats[i];
1910 /* unknown format; fall back on 32bppARGB */
1911 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
1912 gdip_format = PixelFormat32bppARGB;
1916 if (SUCCEEDED(hr)) /* got source */
1918 hr = IWICBitmapSource_GetSize(source, &width, &height);
1921 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
1924 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
1926 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
1927 gdip_format, &lockeddata);
1928 if (status == Ok) /* locked bitmap */
1933 for (i=0; i<height; i++)
1936 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
1937 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
1938 if (FAILED(hr)) break;
1941 GdipBitmapUnlockBits(bitmap, &lockeddata);
1944 if (SUCCEEDED(hr) && status == Ok)
1945 *image = (GpImage*)bitmap;
1949 GdipDisposeImage((GpImage*)bitmap);
1953 IWICBitmapSource_Release(source);
1956 IWICBitmapFrameDecode_Release(frame);
1959 IWICBitmapDecoder_Release(decoder);
1962 if (SUCCEEDED(initresult)) CoUninitialize();
1964 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
1969 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
1971 return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
1974 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, GpImage **image)
1979 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, image);
1981 bitmap = (GpBitmap*)*image;
1983 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
1985 /* WIC supports bmp files with alpha, but gdiplus does not */
1986 bitmap->format = PixelFormat32bppRGB;
1992 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
1994 return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
1997 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, GpImage **image)
1999 return decode_image_wic(stream, &CLSID_WICPngDecoder, image);
2002 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
2004 return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
2007 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
2011 TRACE("%p %p\n", stream, image);
2013 if(!stream || !image)
2014 return InvalidParameter;
2016 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2017 (LPVOID*) &pic) != S_OK){
2018 TRACE("Could not load picture\n");
2019 return GenericError;
2022 /* FIXME: missing initialization code */
2023 *image = GdipAlloc(sizeof(GpMetafile));
2024 if(!*image) return OutOfMemory;
2025 (*image)->type = ImageTypeMetafile;
2026 (*image)->picture = pic;
2027 (*image)->flags = ImageFlagsNone;
2028 (*image)->palette_flags = 0;
2029 (*image)->palette_count = 0;
2030 (*image)->palette_size = 0;
2031 (*image)->palette_entries = NULL;
2033 TRACE("<-- %p\n", *image);
2038 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
2039 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
2041 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
2043 typedef struct image_codec {
2044 ImageCodecInfo info;
2045 encode_image_func encode_func;
2046 decode_image_func decode_func;
2060 static const struct image_codec codecs[NUM_CODECS];
2062 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
2070 /* seek to the start of the stream */
2072 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2073 if (FAILED(hr)) return hresult_to_status(hr);
2075 /* read the first 8 bytes */
2076 /* FIXME: This assumes all codecs have one signature <= 8 bytes in length */
2077 hr = IStream_Read(stream, signature, 8, &bytesread);
2078 if (FAILED(hr)) return hresult_to_status(hr);
2079 if (hr == S_FALSE || bytesread == 0) return GenericError;
2081 for (i = 0; i < NUM_CODECS; i++) {
2082 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
2083 bytesread >= codecs[i].info.SigSize)
2085 for (j=0; j<codecs[i].info.SigSize; j++)
2086 if ((signature[j] & codecs[i].info.SigMask[j]) != codecs[i].info.SigPattern[j])
2088 if (j == codecs[i].info.SigSize)
2090 *result = &codecs[i];
2096 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
2097 signature[0],signature[1],signature[2],signature[3],
2098 signature[4],signature[5],signature[6],signature[7]);
2100 return GenericError;
2103 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
2108 const struct image_codec *codec=NULL;
2110 /* choose an appropriate image decoder */
2111 stat = get_decoder_info(stream, &codec);
2112 if (stat != Ok) return stat;
2114 /* seek to the start of the stream */
2116 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2117 if (FAILED(hr)) return hresult_to_status(hr);
2119 /* call on the image decoder to do the real work */
2120 stat = codec->decode_func(stream, &codec->info.Clsid, image);
2122 /* take note of the original data format */
2125 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
2132 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
2134 TRACE("%p %p\n", stream, image);
2136 return GdipLoadImageFromStream(stream, image);
2139 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
2144 return InvalidParameter;
2147 FIXME("not implemented\n");
2149 return NotImplemented;
2152 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
2157 FIXME("not implemented\n");
2159 return NotImplemented;
2162 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
2163 GDIPCONST CLSID *clsidEncoder,
2164 GDIPCONST EncoderParameters *encoderParams)
2169 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
2171 if (!image || !filename|| !clsidEncoder)
2172 return InvalidParameter;
2174 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
2176 return GenericError;
2178 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
2180 IStream_Release(stream);
2184 /*************************************************************************
2185 * Encoding functions -
2186 * These functions encode an image in different image file formats.
2188 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
2189 #define BITMAP_FORMAT_JPEG 0xd8ff
2190 #define BITMAP_FORMAT_GIF 0x4947
2191 #define BITMAP_FORMAT_PNG 0x5089
2192 #define BITMAP_FORMAT_APM 0xcdd7
2194 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
2195 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2199 IWICBitmapEncoder *encoder;
2200 IWICBitmapFrameEncode *frameencode;
2201 IPropertyBag2 *encoderoptions;
2204 PixelFormat gdipformat=0;
2205 WICPixelFormatGUID wicformat;
2207 BitmapData lockeddata;
2211 if (image->type != ImageTypeBitmap)
2212 return GenericError;
2214 bitmap = (GpBitmap*)image;
2216 GdipGetImageWidth(image, &width);
2217 GdipGetImageHeight(image, &height);
2224 initresult = CoInitialize(NULL);
2226 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2227 &IID_IWICBitmapEncoder, (void**)&encoder);
2230 if (SUCCEEDED(initresult)) CoUninitialize();
2231 return hresult_to_status(hr);
2234 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2238 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
2241 if (SUCCEEDED(hr)) /* created frame */
2243 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
2246 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
2249 /* FIXME: use the resolution from the image */
2250 hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
2254 for (i=0; wic_pixel_formats[i]; i++)
2256 if (wic_gdip_formats[i] == bitmap->format)
2259 if (wic_pixel_formats[i])
2260 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
2262 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
2264 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
2266 for (i=0; wic_pixel_formats[i]; i++)
2268 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
2271 if (wic_pixel_formats[i])
2272 gdipformat = wic_gdip_formats[i];
2275 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
2282 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
2287 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
2290 /* write one row at a time in case stride is negative */
2291 row = lockeddata.Scan0;
2292 for (i=0; i<lockeddata.Height; i++)
2294 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
2295 if (FAILED(hr)) break;
2296 row += lockeddata.Stride;
2299 GdipBitmapUnlockBits(bitmap, &lockeddata);
2306 hr = IWICBitmapFrameEncode_Commit(frameencode);
2308 IWICBitmapFrameEncode_Release(frameencode);
2309 IPropertyBag2_Release(encoderoptions);
2313 hr = IWICBitmapEncoder_Commit(encoder);
2315 IWICBitmapEncoder_Release(encoder);
2317 if (SUCCEEDED(initresult)) CoUninitialize();
2319 return hresult_to_status(hr);
2322 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
2323 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2325 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
2328 static GpStatus encode_image_png(GpImage *image, IStream* stream,
2329 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2331 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
2334 /*****************************************************************************
2335 * GdipSaveImageToStream [GDIPLUS.@]
2337 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
2338 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2341 encode_image_func encode_image;
2344 TRACE("%p %p %p %p\n", image, stream, clsid, params);
2346 if(!image || !stream)
2347 return InvalidParameter;
2349 /* select correct encoder */
2350 encode_image = NULL;
2351 for (i = 0; i < NUM_CODECS; i++) {
2352 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
2353 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
2354 encode_image = codecs[i].encode_func;
2356 if (encode_image == NULL)
2357 return UnknownImageFormat;
2359 stat = encode_image(image, stream, clsid, params);
2364 /*****************************************************************************
2365 * GdipGetImagePalette [GDIPLUS.@]
2367 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
2369 TRACE("(%p,%p,%i)\n", image, palette, size);
2371 if (!image || !palette)
2372 return InvalidParameter;
2374 if (size < (sizeof(UINT)*2+sizeof(ARGB)*image->palette_count))
2376 TRACE("<-- InsufficientBuffer\n");
2377 return InsufficientBuffer;
2380 palette->Flags = image->palette_flags;
2381 palette->Count = image->palette_count;
2382 memcpy(palette->Entries, image->palette_entries, sizeof(ARGB)*image->palette_count);
2387 /*****************************************************************************
2388 * GdipSetImagePalette [GDIPLUS.@]
2390 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
2391 GDIPCONST ColorPalette *palette)
2393 TRACE("(%p,%p)\n", image, palette);
2395 if(!image || !palette || palette->Count > 256)
2396 return InvalidParameter;
2398 if (palette->Count > image->palette_size)
2402 new_palette = GdipAlloc(sizeof(ARGB) * palette->Count);
2403 if (!new_palette) return OutOfMemory;
2405 GdipFree(image->palette_entries);
2406 image->palette_entries = new_palette;
2407 image->palette_size = palette->Count;
2410 image->palette_flags = palette->Flags;
2411 image->palette_count = palette->Count;
2412 memcpy(image->palette_entries, palette->Entries, sizeof(ARGB)*palette->Count);
2417 /*************************************************************************
2419 * Structures that represent which formats we support for encoding.
2422 /* ImageCodecInfo creation routines taken from libgdiplus */
2423 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
2424 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
2425 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
2426 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
2427 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
2428 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
2430 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
2431 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
2432 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
2433 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
2434 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
2435 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
2437 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
2438 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
2439 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
2440 static const WCHAR gif_format[] = {'G','I','F',0};
2441 static const BYTE gif_sig_pattern[4] = "GIF8";
2442 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2444 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
2445 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
2446 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
2447 static const WCHAR emf_format[] = {'E','M','F',0};
2448 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
2449 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2451 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
2452 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
2453 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
2454 static const WCHAR wmf_format[] = {'W','M','F',0};
2455 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
2456 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
2458 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
2459 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
2460 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
2461 static const WCHAR png_format[] = {'P','N','G',0};
2462 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
2463 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2465 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
2466 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
2467 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
2468 static const WCHAR ico_format[] = {'I','C','O',0};
2469 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
2470 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2472 static const struct image_codec codecs[NUM_CODECS] = {
2475 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2476 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2477 /* CodecName */ bmp_codecname,
2479 /* FormatDescription */ bmp_format,
2480 /* FilenameExtension */ bmp_extension,
2481 /* MimeType */ bmp_mimetype,
2482 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2486 /* SigPattern */ bmp_sig_pattern,
2487 /* SigMask */ bmp_sig_mask,
2494 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2495 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2496 /* CodecName */ jpeg_codecname,
2498 /* FormatDescription */ jpeg_format,
2499 /* FilenameExtension */ jpeg_extension,
2500 /* MimeType */ jpeg_mimetype,
2501 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2505 /* SigPattern */ jpeg_sig_pattern,
2506 /* SigMask */ jpeg_sig_mask,
2513 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2514 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2515 /* CodecName */ gif_codecname,
2517 /* FormatDescription */ gif_format,
2518 /* FilenameExtension */ gif_extension,
2519 /* MimeType */ gif_mimetype,
2520 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2524 /* SigPattern */ gif_sig_pattern,
2525 /* SigMask */ gif_sig_mask,
2532 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2533 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2534 /* CodecName */ emf_codecname,
2536 /* FormatDescription */ emf_format,
2537 /* FilenameExtension */ emf_extension,
2538 /* MimeType */ emf_mimetype,
2539 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2543 /* SigPattern */ emf_sig_pattern,
2544 /* SigMask */ emf_sig_mask,
2547 decode_image_olepicture_metafile
2551 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2552 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2553 /* CodecName */ wmf_codecname,
2555 /* FormatDescription */ wmf_format,
2556 /* FilenameExtension */ wmf_extension,
2557 /* MimeType */ wmf_mimetype,
2558 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2562 /* SigPattern */ wmf_sig_pattern,
2563 /* SigMask */ wmf_sig_mask,
2566 decode_image_olepicture_metafile
2570 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2571 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2572 /* CodecName */ png_codecname,
2574 /* FormatDescription */ png_format,
2575 /* FilenameExtension */ png_extension,
2576 /* MimeType */ png_mimetype,
2577 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2581 /* SigPattern */ png_sig_pattern,
2582 /* SigMask */ png_sig_mask,
2589 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2590 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2591 /* CodecName */ ico_codecname,
2593 /* FormatDescription */ ico_format,
2594 /* FilenameExtension */ ico_extension,
2595 /* MimeType */ ico_mimetype,
2596 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2600 /* SigPattern */ ico_sig_pattern,
2601 /* SigMask */ ico_sig_mask,
2608 /*****************************************************************************
2609 * GdipGetImageDecodersSize [GDIPLUS.@]
2611 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
2613 int decoder_count=0;
2615 TRACE("%p %p\n", numDecoders, size);
2617 if (!numDecoders || !size)
2618 return InvalidParameter;
2620 for (i=0; i<NUM_CODECS; i++)
2622 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2626 *numDecoders = decoder_count;
2627 *size = decoder_count * sizeof(ImageCodecInfo);
2632 /*****************************************************************************
2633 * GdipGetImageDecoders [GDIPLUS.@]
2635 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
2637 int i, decoder_count=0;
2638 TRACE("%u %u %p\n", numDecoders, size, decoders);
2641 size != numDecoders * sizeof(ImageCodecInfo))
2642 return GenericError;
2644 for (i=0; i<NUM_CODECS; i++)
2646 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2648 if (decoder_count == numDecoders) return GenericError;
2649 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2654 if (decoder_count < numDecoders) return GenericError;
2659 /*****************************************************************************
2660 * GdipGetImageEncodersSize [GDIPLUS.@]
2662 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
2664 int encoder_count=0;
2666 TRACE("%p %p\n", numEncoders, size);
2668 if (!numEncoders || !size)
2669 return InvalidParameter;
2671 for (i=0; i<NUM_CODECS; i++)
2673 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2677 *numEncoders = encoder_count;
2678 *size = encoder_count * sizeof(ImageCodecInfo);
2683 /*****************************************************************************
2684 * GdipGetImageEncoders [GDIPLUS.@]
2686 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
2688 int i, encoder_count=0;
2689 TRACE("%u %u %p\n", numEncoders, size, encoders);
2692 size != numEncoders * sizeof(ImageCodecInfo))
2693 return GenericError;
2695 for (i=0; i<NUM_CODECS; i++)
2697 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2699 if (encoder_count == numEncoders) return GenericError;
2700 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2705 if (encoder_count < numEncoders) return GenericError;
2710 /*****************************************************************************
2711 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
2713 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
2718 BitmapData lockeddata;
2721 TRACE("%p %p %p\n", hbm, hpal, bitmap);
2724 return InvalidParameter;
2726 /* TODO: Support for device-dependent bitmaps */
2728 FIXME("no support for device-dependent bitmaps\n");
2729 return NotImplemented;
2732 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
2733 return InvalidParameter;
2735 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
2736 switch(bm.bmBitsPixel) {
2738 format = PixelFormat1bppIndexed;
2741 format = PixelFormat4bppIndexed;
2744 format = PixelFormat8bppIndexed;
2747 format = PixelFormat24bppRGB;
2750 format = PixelFormat32bppRGB;
2753 format = PixelFormat48bppRGB;
2756 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
2757 return InvalidParameter;
2760 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
2761 format, NULL, bitmap);
2765 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
2766 format, &lockeddata);
2771 for (y=0; y<bm.bmHeight; y++)
2773 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
2774 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
2783 INT src_height, dst_stride;
2786 hdc = CreateCompatibleDC(NULL);
2787 oldhbm = SelectObject(hdc, hbm);
2789 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2793 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2794 pbmi->bmiHeader.biBitCount = 0;
2796 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
2798 src_height = abs(pbmi->bmiHeader.biHeight);
2800 if (pbmi->bmiHeader.biHeight > 0)
2802 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
2803 dst_stride = -lockeddata.Stride;
2807 dst_bits = lockeddata.Scan0;
2808 dst_stride = lockeddata.Stride;
2811 for (y=0; y<src_height; y++)
2813 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
2814 pbmi, DIB_RGB_COLORS);
2820 retval = OutOfMemory;
2822 SelectObject(hdc, oldhbm);
2826 GdipBitmapUnlockBits(*bitmap, &lockeddata);
2833 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
2835 FIXME("(%p): stub\n", effect);
2836 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
2837 * in Windows's gdiplus */
2838 return NotImplemented;
2841 /*****************************************************************************
2842 * GdipSetEffectParameters [GDIPLUS.@]
2844 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
2845 const VOID *params, const UINT size)
2850 FIXME("not implemented\n");
2852 return NotImplemented;
2855 /*****************************************************************************
2856 * GdipGetImageFlags [GDIPLUS.@]
2858 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
2860 TRACE("%p %p\n", image, flags);
2862 if(!image || !flags)
2863 return InvalidParameter;
2865 *flags = image->flags;
2870 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
2872 TRACE("(%d, %p)\n", control, param);
2875 case TestControlForceBilinear:
2877 FIXME("TestControlForceBilinear not handled\n");
2879 case TestControlNoICM:
2881 FIXME("TestControlNoICM not handled\n");
2883 case TestControlGetBuildNumber:
2884 *((DWORD*)param) = 3102;
2891 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
2892 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
2893 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
2894 GpMetafile **metafile)
2896 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2897 frameUnit, debugstr_w(desc), metafile);
2899 return NotImplemented;
2902 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
2903 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
2904 GDIPCONST WCHAR *desc, GpMetafile **metafile)
2906 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2907 frameUnit, debugstr_w(desc), metafile);
2909 return NotImplemented;
2912 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
2914 FIXME("%p\n", image);
2919 /*****************************************************************************
2920 * GdipGetImageThumbnail [GDIPLUS.@]
2922 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
2923 GpImage **ret_image, GetThumbnailImageAbort cb,
2926 FIXME("(%p %u %u %p %p %p) stub\n",
2927 image, width, height, ret_image, cb, cb_data);
2928 return NotImplemented;
2931 /*****************************************************************************
2932 * GdipImageRotateFlip [GDIPLUS.@]
2934 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
2936 FIXME("(%p %u) stub\n", image, type);
2937 return NotImplemented;