gdiplus: Implement GdipBitmapGetPixel.
[wine/multimedia.git] / dlls / gdiplus / image.c
blobdc9f9547e809bfd1e491b47e92a5933c7fbe43b3
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #define NONAMELESSUNION
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
28 #define COBJMACROS
29 #include "objbase.h"
30 #include "olectl.h"
31 #include "ole2.h"
33 #include "initguid.h"
34 #include "wincodec.h"
35 #include "gdiplus.h"
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)
45 HDC hdcref;
46 OLE_YSIZE_HIMETRIC y;
48 IPicture_get_Height(pic, &y);
50 hdcref = GetDC(0);
52 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
53 ReleaseDC(0, hdcref);
55 return y;
58 static INT ipicture_pixel_width(IPicture *pic)
60 HDC hdcref;
61 OLE_XSIZE_HIMETRIC x;
63 IPicture_get_Width(pic, &x);
65 hdcref = GetDC(0);
67 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
69 ReleaseDC(0, hdcref);
71 return x;
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];
101 *a = 255;
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);
111 *a = 255;
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);
121 *a = 255;
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)
132 *a = 255;
133 else
134 *a = 0;
137 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
138 const BYTE *row, UINT x)
140 *r = row[x*3+2];
141 *g = row[x*3+1];
142 *b = row[x*3];
143 *a = 255;
146 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
147 const BYTE *row, UINT x)
149 *r = row[x*4+2];
150 *g = row[x*4+1];
151 *b = row[x*4];
152 *a = 255;
155 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
156 const BYTE *row, UINT x)
158 *r = row[x*4+2];
159 *g = row[x*4+1];
160 *b = row[x*4];
161 *a = row[x*4+3];
164 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
165 const BYTE *row, UINT x)
167 *a = row[x*4+3];
168 if (*a == 0)
169 *r = *g = *b = 0;
170 else
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)
181 *r = row[x*6+5];
182 *g = row[x*6+3];
183 *b = row[x*6+1];
184 *a = 255;
187 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
188 const BYTE *row, UINT x)
190 *r = row[x*8+5];
191 *g = row[x*8+3];
192 *b = row[x*8+1];
193 *a = row[x*8+7];
196 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
197 const BYTE *row, UINT x)
199 *a = row[x*8+7];
200 if (*a == 0)
201 *r = *g = *b = 0;
202 else
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,
211 ARGB *color)
213 BYTE r, g, b, a;
214 BYTE *row;
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);
227 break;
228 case PixelFormat16bppRGB555:
229 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
230 break;
231 case PixelFormat16bppRGB565:
232 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
233 break;
234 case PixelFormat16bppARGB1555:
235 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
236 break;
237 case PixelFormat24bppRGB:
238 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
239 break;
240 case PixelFormat32bppRGB:
241 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
242 break;
243 case PixelFormat32bppARGB:
244 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
245 break;
246 case PixelFormat32bppPARGB:
247 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
248 break;
249 case PixelFormat48bppRGB:
250 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
251 break;
252 case PixelFormat64bppARGB:
253 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
254 break;
255 case PixelFormat64bppPARGB:
256 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
257 break;
258 default:
259 FIXME("not implemented for format 0x%x\n", bitmap->format);
260 return NotImplemented;
263 *color = a<<24|r<<16|g<<8|b;
265 return Ok;
268 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
269 ARGB color)
271 static int calls;
272 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
274 if(!bitmap)
275 return InvalidParameter;
277 if(!(calls++))
278 FIXME("not implemented\n");
280 return NotImplemented;
283 /* This function returns a pointer to an array of pixels that represents the
284 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
285 * flags. It is correct behavior that a user who calls this function with write
286 * privileges can write to the whole bitmap (not just the area in rect).
288 * FIXME: only used portion of format is bits per pixel. */
289 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
290 UINT flags, PixelFormat format, BitmapData* lockeddata)
292 BOOL bm_is_selected;
293 INT stride, bitspp = PIXELFORMATBPP(format);
294 HDC hdc;
295 HBITMAP hbm, old = NULL;
296 BITMAPINFO *pbmi;
297 BYTE *buff = NULL;
298 UINT abs_height;
299 GpRect act_rect; /* actual rect to be used */
301 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
303 if(!lockeddata || !bitmap)
304 return InvalidParameter;
306 if(rect){
307 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
308 (rect->Y + rect->Height > bitmap->height) || !flags)
309 return InvalidParameter;
311 act_rect = *rect;
313 else{
314 act_rect.X = act_rect.Y = 0;
315 act_rect.Width = bitmap->width;
316 act_rect.Height = bitmap->height;
319 if(flags & ImageLockModeUserInputBuf)
320 return NotImplemented;
322 if(bitmap->lockmode)
323 return WrongState;
325 if (bitmap->bits && bitmap->format == format)
327 /* no conversion is necessary; just use the bits directly */
328 lockeddata->Width = act_rect.Width;
329 lockeddata->Height = act_rect.Height;
330 lockeddata->PixelFormat = format;
331 lockeddata->Reserved = flags;
332 lockeddata->Stride = bitmap->stride;
333 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
334 bitmap->stride * act_rect.Y;
336 bitmap->lockmode = flags;
337 bitmap->numlocks++;
339 return Ok;
342 hbm = bitmap->hbitmap;
343 hdc = bitmap->hdc;
344 bm_is_selected = (hdc != 0);
346 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
347 if (!pbmi)
348 return OutOfMemory;
349 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
350 pbmi->bmiHeader.biBitCount = 0;
352 if(!bm_is_selected){
353 hdc = CreateCompatibleDC(0);
354 old = SelectObject(hdc, hbm);
357 /* fill out bmi */
358 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
360 abs_height = abs(pbmi->bmiHeader.biHeight);
361 stride = pbmi->bmiHeader.biWidth * bitspp / 8;
362 stride = (stride + 3) & ~3;
364 buff = GdipAlloc(stride * abs_height);
366 pbmi->bmiHeader.biBitCount = bitspp;
368 if(buff)
369 GetDIBits(hdc, hbm, 0, abs_height, buff, pbmi, DIB_RGB_COLORS);
371 if(!bm_is_selected){
372 SelectObject(hdc, old);
373 DeleteDC(hdc);
376 if(!buff){
377 GdipFree(pbmi);
378 return OutOfMemory;
381 lockeddata->Width = act_rect.Width;
382 lockeddata->Height = act_rect.Height;
383 lockeddata->PixelFormat = format;
384 lockeddata->Reserved = flags;
386 if(pbmi->bmiHeader.biHeight > 0){
387 lockeddata->Stride = -stride;
388 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
389 stride * (abs_height - 1 - act_rect.Y);
391 else{
392 lockeddata->Stride = stride;
393 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
396 bitmap->lockmode = flags;
397 bitmap->numlocks++;
399 bitmap->bitmapbits = buff;
401 GdipFree(pbmi);
402 return Ok;
405 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
407 FIXME("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
409 return NotImplemented;
412 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
413 BitmapData* lockeddata)
415 HDC hdc;
416 HBITMAP hbm, old = NULL;
417 BOOL bm_is_selected;
418 BITMAPINFO *pbmi;
420 if(!bitmap || !lockeddata)
421 return InvalidParameter;
423 if(!bitmap->lockmode)
424 return WrongState;
426 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
427 return NotImplemented;
429 if(lockeddata->Reserved & ImageLockModeRead){
430 if(!(--bitmap->numlocks))
431 bitmap->lockmode = 0;
433 GdipFree(bitmap->bitmapbits);
434 bitmap->bitmapbits = NULL;
435 return Ok;
438 if (!bitmap->bitmapbits)
440 /* we passed a direct reference; no need to do anything */
441 bitmap->lockmode = 0;
442 return Ok;
445 hbm = bitmap->hbitmap;
446 hdc = bitmap->hdc;
447 bm_is_selected = (hdc != 0);
449 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
450 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
451 pbmi->bmiHeader.biBitCount = 0;
453 if(!bm_is_selected){
454 hdc = CreateCompatibleDC(0);
455 old = SelectObject(hdc, hbm);
458 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
459 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
460 SetDIBits(hdc, hbm, 0, abs(pbmi->bmiHeader.biHeight),
461 bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
463 if(!bm_is_selected){
464 SelectObject(hdc, old);
465 DeleteDC(hdc);
468 GdipFree(pbmi);
469 GdipFree(bitmap->bitmapbits);
470 bitmap->bitmapbits = NULL;
471 bitmap->lockmode = 0;
473 return Ok;
476 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
477 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
479 FIXME("(%f,%f,%f,%f,%i,%p,%p): stub\n", x, y, width, height, format, srcBitmap, dstBitmap);
481 return NotImplemented;
484 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
485 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
487 FIXME("(%i,%i,%i,%i,%i,%p,%p): stub\n", x, y, width, height, format, srcBitmap, dstBitmap);
489 return NotImplemented;
492 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
494 GpStatus stat = GenericError;
496 TRACE("%p, %p\n", image, cloneImage);
498 if (!image || !cloneImage)
499 return InvalidParameter;
501 if (image->picture)
503 IStream* stream;
504 HRESULT hr;
505 INT size;
506 LARGE_INTEGER move;
508 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
509 if (FAILED(hr))
510 return GenericError;
512 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
513 if(FAILED(hr))
515 WARN("Failed to save image on stream\n");
516 goto out;
519 /* Set seek pointer back to the beginning of the picture */
520 move.QuadPart = 0;
521 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
522 if (FAILED(hr))
523 goto out;
525 stat = GdipLoadImageFromStream(stream, cloneImage);
526 if (stat != Ok) WARN("Failed to load image from stream\n");
528 out:
529 IStream_Release(stream);
530 return stat;
532 else if (image->type == ImageTypeBitmap)
534 GpBitmap *bitmap = (GpBitmap*)image;
535 BitmapData lockeddata_src, lockeddata_dst;
536 int i;
537 UINT row_size;
539 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
540 &lockeddata_src);
541 if (stat != Ok) return stat;
543 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
544 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
545 if (stat == Ok)
547 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
548 lockeddata_src.PixelFormat, &lockeddata_dst);
550 if (stat == Ok)
552 /* copy the image data */
553 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
554 for (i=0; i<lockeddata_src.Height; i++)
555 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
556 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
557 row_size);
559 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
562 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
565 if (stat != Ok)
567 GdipDisposeImage(*cloneImage);
568 *cloneImage = NULL;
570 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
572 return stat;
574 else
576 ERR("GpImage with no IPicture or bitmap?!\n");
577 return NotImplemented;
581 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
582 GpBitmap **bitmap)
584 GpStatus stat;
585 IStream *stream;
587 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
589 if(!filename || !bitmap)
590 return InvalidParameter;
592 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
594 if(stat != Ok)
595 return stat;
597 stat = GdipCreateBitmapFromStream(stream, bitmap);
599 IStream_Release(stream);
601 return stat;
604 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
605 VOID *bits, GpBitmap **bitmap)
607 DWORD height, stride;
608 PixelFormat format;
610 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
612 height = abs(info->bmiHeader.biHeight);
613 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
615 if(info->bmiHeader.biHeight > 0) /* bottom-up */
617 bits = (BYTE*)bits + (height - 1) * stride;
618 stride = -stride;
621 switch(info->bmiHeader.biBitCount) {
622 case 1:
623 format = PixelFormat1bppIndexed;
624 break;
625 case 4:
626 format = PixelFormat4bppIndexed;
627 break;
628 case 8:
629 format = PixelFormat8bppIndexed;
630 break;
631 case 24:
632 format = PixelFormat24bppRGB;
633 break;
634 default:
635 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
636 *bitmap = NULL;
637 return InvalidParameter;
640 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
641 bits, bitmap);
645 /* FIXME: no icm */
646 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
647 GpBitmap **bitmap)
649 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
651 return GdipCreateBitmapFromFile(filename, bitmap);
654 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
655 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
657 HBITMAP hbm;
658 GpStatus stat = InvalidParameter;
660 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
662 if(!lpBitmapName || !bitmap)
663 return InvalidParameter;
665 /* load DIB */
666 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
667 LR_CREATEDIBSECTION);
669 if(hbm){
670 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
671 DeleteObject(hbm);
674 return stat;
677 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
678 HBITMAP* hbmReturn, ARGB background)
680 GpStatus stat;
681 HBITMAP result, oldbitmap;
682 UINT width, height;
683 HDC hdc;
684 GpGraphics *graphics;
685 BITMAPINFOHEADER bih;
686 void *bits;
687 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
689 if (!bitmap || !hbmReturn) return InvalidParameter;
691 GdipGetImageWidth((GpImage*)bitmap, &width);
692 GdipGetImageHeight((GpImage*)bitmap, &height);
694 bih.biSize = sizeof(bih);
695 bih.biWidth = width;
696 bih.biHeight = height;
697 bih.biPlanes = 1;
698 bih.biBitCount = 32;
699 bih.biCompression = BI_RGB;
700 bih.biSizeImage = 0;
701 bih.biXPelsPerMeter = 0;
702 bih.biYPelsPerMeter = 0;
703 bih.biClrUsed = 0;
704 bih.biClrImportant = 0;
706 hdc = CreateCompatibleDC(NULL);
707 if (!hdc) return GenericError;
709 result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
710 NULL, 0);
712 if (result)
714 oldbitmap = SelectObject(hdc, result);
716 stat = GdipCreateFromHDC(hdc, &graphics);
717 if (stat == Ok)
719 stat = GdipGraphicsClear(graphics, background);
721 if (stat == Ok)
722 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
724 GdipDeleteGraphics(graphics);
727 SelectObject(hdc, oldbitmap);
729 else
730 stat = GenericError;
732 DeleteDC(hdc);
734 if (stat != Ok && result)
736 DeleteObject(result);
737 result = NULL;
740 *hbmReturn = result;
742 return stat;
745 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
746 GpMetafile* metafile, BOOL* succ, EmfType emfType,
747 const WCHAR* description, GpMetafile** out_metafile)
749 static int calls;
751 if(!ref || !metafile || !out_metafile)
752 return InvalidParameter;
754 *succ = FALSE;
755 *out_metafile = NULL;
757 if(!(calls++))
758 FIXME("not implemented\n");
760 return NotImplemented;
763 /* FIXME: this should create a bitmap in the given size with the attributes
764 * (resolution etc.) of the graphics object */
765 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
766 GpGraphics* target, GpBitmap** bitmap)
768 static int calls;
769 GpStatus ret;
771 if(!target || !bitmap)
772 return InvalidParameter;
774 if(!(calls++))
775 FIXME("hacked stub\n");
777 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
778 NULL, bitmap);
780 return ret;
783 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
785 GpStatus stat;
786 ICONINFO iinfo;
787 BITMAP bm;
788 int ret;
789 UINT width, height;
790 GpRect rect;
791 BitmapData lockeddata;
792 HDC screendc;
793 BOOL has_alpha;
794 int x, y;
795 BYTE *bits;
796 BITMAPINFOHEADER bih;
797 DWORD *src;
798 BYTE *dst_row;
799 DWORD *dst;
801 TRACE("%p, %p\n", hicon, bitmap);
803 if(!bitmap || !GetIconInfo(hicon, &iinfo))
805 DeleteObject(iinfo.hbmColor);
806 DeleteObject(iinfo.hbmMask);
807 return InvalidParameter;
810 /* get the size of the icon */
811 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
812 if (ret == 0) {
813 DeleteObject(iinfo.hbmColor);
814 DeleteObject(iinfo.hbmMask);
815 return GenericError;
818 width = bm.bmWidth;
820 if (iinfo.hbmColor)
821 height = abs(bm.bmHeight);
822 else /* combined bitmap + mask */
823 height = abs(bm.bmHeight) / 2;
825 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
826 if (!bits) {
827 DeleteObject(iinfo.hbmColor);
828 DeleteObject(iinfo.hbmMask);
829 return OutOfMemory;
832 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
833 if (stat != Ok) {
834 DeleteObject(iinfo.hbmColor);
835 DeleteObject(iinfo.hbmMask);
836 HeapFree(GetProcessHeap(), 0, bits);
837 return stat;
840 rect.X = 0;
841 rect.Y = 0;
842 rect.Width = width;
843 rect.Height = height;
845 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
846 if (stat != Ok) {
847 DeleteObject(iinfo.hbmColor);
848 DeleteObject(iinfo.hbmMask);
849 HeapFree(GetProcessHeap(), 0, bits);
850 GdipDisposeImage((GpImage*)*bitmap);
851 return stat;
854 bih.biSize = sizeof(bih);
855 bih.biWidth = width;
856 bih.biHeight = -height;
857 bih.biPlanes = 1;
858 bih.biBitCount = 32;
859 bih.biCompression = BI_RGB;
860 bih.biSizeImage = 0;
861 bih.biXPelsPerMeter = 0;
862 bih.biYPelsPerMeter = 0;
863 bih.biClrUsed = 0;
864 bih.biClrImportant = 0;
866 screendc = GetDC(0);
867 if (iinfo.hbmColor)
869 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
871 if (bm.bmBitsPixel == 32)
873 has_alpha = FALSE;
875 /* If any pixel has a non-zero alpha, ignore hbmMask */
876 src = (DWORD*)bits;
877 for (x=0; x<width && !has_alpha; x++)
878 for (y=0; y<height && !has_alpha; y++)
879 if ((*src++ & 0xff000000) != 0)
880 has_alpha = TRUE;
882 else has_alpha = FALSE;
884 else
886 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
887 has_alpha = FALSE;
890 /* copy the image data to the Bitmap */
891 src = (DWORD*)bits;
892 dst_row = lockeddata.Scan0;
893 for (y=0; y<height; y++)
895 memcpy(dst_row, src, width*4);
896 src += width;
897 dst_row += lockeddata.Stride;
900 if (!has_alpha)
902 if (iinfo.hbmMask)
904 /* read alpha data from the mask */
905 if (iinfo.hbmColor)
906 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
907 else
908 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
910 src = (DWORD*)bits;
911 dst_row = lockeddata.Scan0;
912 for (y=0; y<height; y++)
914 dst = (DWORD*)dst_row;
915 for (x=0; x<height; x++)
917 DWORD src_value = *src++;
918 if (src_value)
919 *dst++ = 0;
920 else
921 *dst++ |= 0xff000000;
923 dst_row += lockeddata.Stride;
926 else
928 /* set constant alpha of 255 */
929 dst_row = bits;
930 for (y=0; y<height; y++)
932 dst = (DWORD*)dst_row;
933 for (x=0; x<height; x++)
934 *dst++ |= 0xff000000;
935 dst_row += lockeddata.Stride;
940 ReleaseDC(0, screendc);
942 DeleteObject(iinfo.hbmColor);
943 DeleteObject(iinfo.hbmMask);
945 GdipBitmapUnlockBits(*bitmap, &lockeddata);
947 HeapFree(GetProcessHeap(), 0, bits);
949 return Ok;
952 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
953 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
955 BITMAPINFOHEADER bmih;
956 HBITMAP hbitmap;
957 INT row_size, dib_stride;
958 HDC hdc;
959 BYTE *bits;
960 int i;
962 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
964 if (!bitmap) return InvalidParameter;
966 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
967 *bitmap = NULL;
968 return InvalidParameter;
971 if(scan0 && !stride)
972 return InvalidParameter;
974 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
975 dib_stride = (row_size + 3) & ~3;
977 if(stride == 0)
978 stride = dib_stride;
980 bmih.biSize = sizeof(BITMAPINFOHEADER);
981 bmih.biWidth = width;
982 bmih.biHeight = -height;
983 bmih.biPlanes = 1;
984 /* FIXME: use the rest of the data from format */
985 bmih.biBitCount = PIXELFORMATBPP(format);
986 bmih.biCompression = BI_RGB;
987 bmih.biSizeImage = 0;
988 bmih.biXPelsPerMeter = 0;
989 bmih.biYPelsPerMeter = 0;
990 bmih.biClrUsed = 0;
991 bmih.biClrImportant = 0;
993 hdc = CreateCompatibleDC(NULL);
994 if (!hdc) return GenericError;
996 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, (void**)&bits,
997 NULL, 0);
999 DeleteDC(hdc);
1001 if (!hbitmap) return GenericError;
1003 /* copy bits to the dib if necessary */
1004 /* FIXME: should reference the bits instead of copying them */
1005 if (scan0)
1006 for (i=0; i<height; i++)
1007 memcpy(bits+i*dib_stride, scan0+i*stride, row_size);
1009 *bitmap = GdipAlloc(sizeof(GpBitmap));
1010 if(!*bitmap)
1012 DeleteObject(hbitmap);
1013 return OutOfMemory;
1016 (*bitmap)->image.type = ImageTypeBitmap;
1017 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1018 (*bitmap)->image.flags = ImageFlagsNone;
1019 (*bitmap)->width = width;
1020 (*bitmap)->height = height;
1021 (*bitmap)->format = format;
1022 (*bitmap)->image.picture = NULL;
1023 (*bitmap)->hbitmap = hbitmap;
1024 (*bitmap)->hdc = NULL;
1025 (*bitmap)->bits = bits;
1026 (*bitmap)->stride = dib_stride;
1028 return Ok;
1031 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1032 GpBitmap **bitmap)
1034 GpStatus stat;
1036 TRACE("%p %p\n", stream, bitmap);
1038 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1040 if(stat != Ok)
1041 return stat;
1043 if((*bitmap)->image.type != ImageTypeBitmap){
1044 GdipDisposeImage(&(*bitmap)->image);
1045 *bitmap = NULL;
1046 return GenericError; /* FIXME: what error to return? */
1049 return Ok;
1052 /* FIXME: no icm */
1053 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1054 GpBitmap **bitmap)
1056 TRACE("%p %p\n", stream, bitmap);
1058 return GdipCreateBitmapFromStream(stream, bitmap);
1061 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1062 GpCachedBitmap **cachedbmp)
1064 GpStatus stat;
1066 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1068 if(!bitmap || !graphics || !cachedbmp)
1069 return InvalidParameter;
1071 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1072 if(!*cachedbmp)
1073 return OutOfMemory;
1075 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1076 if(stat != Ok){
1077 GdipFree(*cachedbmp);
1078 return stat;
1081 return Ok;
1084 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1086 FIXME("(%p, %p)\n", bitmap, hicon);
1088 return NotImplemented;
1091 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1093 TRACE("%p\n", cachedbmp);
1095 if(!cachedbmp)
1096 return InvalidParameter;
1098 GdipDisposeImage(cachedbmp->image);
1099 GdipFree(cachedbmp);
1101 return Ok;
1104 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1105 GpCachedBitmap *cachedbmp, INT x, INT y)
1107 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1109 if(!graphics || !cachedbmp)
1110 return InvalidParameter;
1112 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1115 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1116 LPBYTE pData16, INT iMapMode, INT eFlags)
1118 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1119 return NotImplemented;
1122 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
1124 TRACE("%p\n", image);
1126 if(!image)
1127 return InvalidParameter;
1129 if (image->picture)
1130 IPicture_Release(image->picture);
1131 if (image->type == ImageTypeBitmap)
1133 GdipFree(((GpBitmap*)image)->bitmapbits);
1134 DeleteDC(((GpBitmap*)image)->hdc);
1136 GdipFree(image);
1138 return Ok;
1141 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
1143 if(!image || !item)
1144 return InvalidParameter;
1146 return NotImplemented;
1149 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
1150 GpUnit *srcUnit)
1152 TRACE("%p %p %p\n", image, srcRect, srcUnit);
1154 if(!image || !srcRect || !srcUnit)
1155 return InvalidParameter;
1156 if(image->type == ImageTypeMetafile){
1157 *srcRect = ((GpMetafile*)image)->bounds;
1158 *srcUnit = ((GpMetafile*)image)->unit;
1160 else if(image->type == ImageTypeBitmap){
1161 srcRect->X = srcRect->Y = 0.0;
1162 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
1163 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
1164 *srcUnit = UnitPixel;
1166 else{
1167 srcRect->X = srcRect->Y = 0.0;
1168 srcRect->Width = ipicture_pixel_width(image->picture);
1169 srcRect->Height = ipicture_pixel_height(image->picture);
1170 *srcUnit = UnitPixel;
1173 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
1174 srcRect->Width, srcRect->Height, *srcUnit);
1176 return Ok;
1179 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
1180 REAL *height)
1182 TRACE("%p %p %p\n", image, width, height);
1184 if(!image || !height || !width)
1185 return InvalidParameter;
1187 if(image->type == ImageTypeMetafile){
1188 HDC hdc = GetDC(0);
1190 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1191 ((GpMetafile*)image)->bounds.Height;
1193 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1194 ((GpMetafile*)image)->bounds.Width;
1196 ReleaseDC(0, hdc);
1199 else if(image->type == ImageTypeBitmap){
1200 *height = ((GpBitmap*)image)->height;
1201 *width = ((GpBitmap*)image)->width;
1203 else{
1204 *height = ipicture_pixel_height(image->picture);
1205 *width = ipicture_pixel_width(image->picture);
1208 TRACE("returning (%f, %f)\n", *height, *width);
1209 return Ok;
1212 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
1213 GpGraphics **graphics)
1215 HDC hdc;
1217 TRACE("%p %p\n", image, graphics);
1219 if(!image || !graphics)
1220 return InvalidParameter;
1222 if(image->type != ImageTypeBitmap){
1223 FIXME("not implemented for image type %d\n", image->type);
1224 return NotImplemented;
1227 hdc = ((GpBitmap*)image)->hdc;
1229 if(!hdc){
1230 hdc = CreateCompatibleDC(0);
1231 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
1232 ((GpBitmap*)image)->hdc = hdc;
1235 return GdipCreateFromHDC(hdc, graphics);
1238 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
1240 TRACE("%p %p\n", image, height);
1242 if(!image || !height)
1243 return InvalidParameter;
1245 if(image->type == ImageTypeMetafile){
1246 HDC hdc = GetDC(0);
1248 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1249 ((GpMetafile*)image)->bounds.Height);
1251 ReleaseDC(0, hdc);
1253 else if(image->type == ImageTypeBitmap)
1254 *height = ((GpBitmap*)image)->height;
1255 else
1256 *height = ipicture_pixel_height(image->picture);
1258 TRACE("returning %d\n", *height);
1260 return Ok;
1263 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
1265 static int calls;
1267 if(!image || !res)
1268 return InvalidParameter;
1270 if(!(calls++))
1271 FIXME("not implemented\n");
1273 return NotImplemented;
1276 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
1278 FIXME("%p %p\n", image, size);
1280 if(!image || !size)
1281 return InvalidParameter;
1283 return NotImplemented;
1286 /* FIXME: test this function for non-bitmap types */
1287 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
1289 TRACE("%p %p\n", image, format);
1291 if(!image || !format)
1292 return InvalidParameter;
1294 if(image->type != ImageTypeBitmap)
1295 *format = PixelFormat24bppRGB;
1296 else
1297 *format = ((GpBitmap*) image)->format;
1299 return Ok;
1302 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
1304 if(!image || !format)
1305 return InvalidParameter;
1307 memcpy(format, &image->format, sizeof(GUID));
1309 return Ok;
1312 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
1314 TRACE("%p %p\n", image, type);
1316 if(!image || !type)
1317 return InvalidParameter;
1319 *type = image->type;
1321 return Ok;
1324 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
1326 static int calls;
1328 if(!image || !res)
1329 return InvalidParameter;
1331 if(!(calls++))
1332 FIXME("not implemented\n");
1334 return NotImplemented;
1337 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
1339 TRACE("%p %p\n", image, width);
1341 if(!image || !width)
1342 return InvalidParameter;
1344 if(image->type == ImageTypeMetafile){
1345 HDC hdc = GetDC(0);
1347 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1348 ((GpMetafile*)image)->bounds.Width);
1350 ReleaseDC(0, hdc);
1352 else if(image->type == ImageTypeBitmap)
1353 *width = ((GpBitmap*)image)->width;
1354 else
1355 *width = ipicture_pixel_width(image->picture);
1357 TRACE("returning %d\n", *width);
1359 return Ok;
1362 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
1363 MetafileHeader * header)
1365 static int calls;
1367 if(!metafile || !header)
1368 return InvalidParameter;
1370 if(!(calls++))
1371 FIXME("not implemented\n");
1373 return Ok;
1376 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
1377 UINT num, PropertyItem* items)
1379 static int calls;
1381 if(!(calls++))
1382 FIXME("not implemented\n");
1384 return InvalidParameter;
1387 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
1389 static int calls;
1391 if(!(calls++))
1392 FIXME("not implemented\n");
1394 return InvalidParameter;
1397 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
1399 static int calls;
1401 if(!(calls++))
1402 FIXME("not implemented\n");
1404 return InvalidParameter;
1407 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
1408 PropertyItem* buffer)
1410 static int calls;
1412 if(!(calls++))
1413 FIXME("not implemented\n");
1415 return InvalidParameter;
1418 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
1419 UINT* size)
1421 static int calls;
1423 TRACE("%p %x %p\n", image, pid, size);
1425 if(!size || !image)
1426 return InvalidParameter;
1428 if(!(calls++))
1429 FIXME("not implemented\n");
1431 return NotImplemented;
1434 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
1436 static int calls;
1438 if(!(calls++))
1439 FIXME("not implemented\n");
1441 return InvalidParameter;
1444 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
1445 GDIPCONST GUID* dimensionID, UINT* count)
1447 static int calls;
1449 if(!image || !dimensionID || !count)
1450 return InvalidParameter;
1452 if(!(calls++))
1453 FIXME("not implemented\n");
1455 return NotImplemented;
1458 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
1459 UINT* count)
1461 if(!image || !count)
1462 return InvalidParameter;
1464 *count = 1;
1466 FIXME("stub\n");
1468 return Ok;
1471 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
1472 GUID* dimensionIDs, UINT count)
1474 static int calls;
1476 if(!image || !dimensionIDs)
1477 return InvalidParameter;
1479 if(!(calls++))
1480 FIXME("not implemented\n");
1482 return Ok;
1485 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
1486 GDIPCONST GUID* dimensionID, UINT frameidx)
1488 static int calls;
1490 if(!image || !dimensionID)
1491 return InvalidParameter;
1493 if(!(calls++))
1494 FIXME("not implemented\n");
1496 return Ok;
1499 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
1500 GpImage **image)
1502 GpStatus stat;
1503 IStream *stream;
1505 TRACE("(%s) %p\n", debugstr_w(filename), image);
1507 if (!filename || !image)
1508 return InvalidParameter;
1510 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1512 if (stat != Ok)
1513 return stat;
1515 stat = GdipLoadImageFromStream(stream, image);
1517 IStream_Release(stream);
1519 return stat;
1522 /* FIXME: no icm handling */
1523 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
1525 TRACE("(%s) %p\n", debugstr_w(filename), image);
1527 return GdipLoadImageFromFile(filename, image);
1530 static const WICPixelFormatGUID *wic_pixel_formats[] = {
1531 &GUID_WICPixelFormat16bppBGR555,
1532 &GUID_WICPixelFormat24bppBGR,
1533 &GUID_WICPixelFormat32bppBGR,
1534 &GUID_WICPixelFormat32bppBGRA,
1535 &GUID_WICPixelFormat32bppPBGRA,
1536 NULL
1539 static const PixelFormat wic_gdip_formats[] = {
1540 PixelFormat16bppRGB555,
1541 PixelFormat24bppRGB,
1542 PixelFormat32bppRGB,
1543 PixelFormat32bppARGB,
1544 PixelFormat32bppPARGB,
1547 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
1549 GpStatus status=Ok;
1550 GpBitmap *bitmap;
1551 HRESULT hr;
1552 IWICBitmapDecoder *decoder;
1553 IWICBitmapFrameDecode *frame;
1554 IWICBitmapSource *source=NULL;
1555 WICPixelFormatGUID wic_format;
1556 PixelFormat gdip_format=0;
1557 int i;
1558 UINT width, height;
1559 BitmapData lockeddata;
1560 WICRect wrc;
1561 HRESULT initresult;
1563 initresult = CoInitialize(NULL);
1565 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
1566 &IID_IWICBitmapDecoder, (void**)&decoder);
1567 if (FAILED(hr)) goto end;
1569 hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
1570 if (SUCCEEDED(hr))
1571 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
1573 if (SUCCEEDED(hr)) /* got frame */
1575 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
1577 if (SUCCEEDED(hr))
1579 for (i=0; wic_pixel_formats[i]; i++)
1581 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
1583 source = (IWICBitmapSource*)frame;
1584 IWICBitmapSource_AddRef(source);
1585 gdip_format = wic_gdip_formats[i];
1586 break;
1589 if (!source)
1591 /* unknown format; fall back on 32bppARGB */
1592 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
1593 gdip_format = PixelFormat32bppARGB;
1597 if (SUCCEEDED(hr)) /* got source */
1599 hr = IWICBitmapSource_GetSize(source, &width, &height);
1601 if (SUCCEEDED(hr))
1602 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
1603 NULL, &bitmap);
1605 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
1607 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
1608 gdip_format, &lockeddata);
1609 if (status == Ok) /* locked bitmap */
1611 wrc.X = 0;
1612 wrc.Width = width;
1613 wrc.Height = 1;
1614 for (i=0; i<height; i++)
1616 wrc.Y = i;
1617 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
1618 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
1619 if (FAILED(hr)) break;
1622 GdipBitmapUnlockBits(bitmap, &lockeddata);
1625 if (SUCCEEDED(hr) && status == Ok)
1626 *image = (GpImage*)bitmap;
1627 else
1629 *image = NULL;
1630 GdipDisposeImage((GpImage*)bitmap);
1634 IWICBitmapSource_Release(source);
1637 IWICBitmapFrameDecode_Release(frame);
1640 IWICBitmapDecoder_Release(decoder);
1642 end:
1643 if (SUCCEEDED(initresult)) CoUninitialize();
1645 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
1647 return status;
1650 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
1652 return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
1655 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, GpImage **image)
1657 GpStatus status;
1658 GpBitmap* bitmap;
1660 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, image);
1662 bitmap = (GpBitmap*)*image;
1664 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
1666 /* WIC supports bmp files with alpha, but gdiplus does not */
1667 bitmap->format = PixelFormat32bppRGB;
1670 return status;
1673 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
1675 return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
1678 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, GpImage **image)
1680 return decode_image_wic(stream, &CLSID_WICPngDecoder, image);
1683 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
1685 return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
1688 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
1690 IPicture *pic;
1692 TRACE("%p %p\n", stream, image);
1694 if(!stream || !image)
1695 return InvalidParameter;
1697 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1698 (LPVOID*) &pic) != S_OK){
1699 TRACE("Could not load picture\n");
1700 return GenericError;
1703 /* FIXME: missing initialization code */
1704 *image = GdipAlloc(sizeof(GpMetafile));
1705 if(!*image) return OutOfMemory;
1706 (*image)->type = ImageTypeMetafile;
1707 (*image)->picture = pic;
1708 (*image)->flags = ImageFlagsNone;
1710 return Ok;
1713 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
1714 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
1716 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
1718 typedef struct image_codec {
1719 ImageCodecInfo info;
1720 encode_image_func encode_func;
1721 decode_image_func decode_func;
1722 } image_codec;
1724 typedef enum {
1725 BMP,
1726 JPEG,
1727 GIF,
1728 EMF,
1729 WMF,
1730 PNG,
1731 ICO,
1732 NUM_CODECS
1733 } ImageFormat;
1735 static const struct image_codec codecs[NUM_CODECS];
1737 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
1739 BYTE signature[8];
1740 LARGE_INTEGER seek;
1741 HRESULT hr;
1742 UINT bytesread;
1743 int i, j;
1745 /* seek to the start of the stream */
1746 seek.QuadPart = 0;
1747 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
1748 if (FAILED(hr)) return hresult_to_status(hr);
1750 /* read the first 8 bytes */
1751 /* FIXME: This assumes all codecs have one signature <= 8 bytes in length */
1752 hr = IStream_Read(stream, signature, 8, &bytesread);
1753 if (FAILED(hr)) return hresult_to_status(hr);
1754 if (hr == S_FALSE || bytesread == 0) return GenericError;
1756 for (i = 0; i < NUM_CODECS; i++) {
1757 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
1758 bytesread >= codecs[i].info.SigSize)
1760 for (j=0; j<codecs[i].info.SigSize; j++)
1761 if ((signature[j] & codecs[i].info.SigMask[j]) != codecs[i].info.SigPattern[j])
1762 break;
1763 if (j == codecs[i].info.SigSize)
1765 *result = &codecs[i];
1766 return Ok;
1771 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
1772 signature[0],signature[1],signature[2],signature[3],
1773 signature[4],signature[5],signature[6],signature[7]);
1775 return GenericError;
1778 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
1780 GpStatus stat;
1781 LARGE_INTEGER seek;
1782 HRESULT hr;
1783 const struct image_codec *codec=NULL;
1785 /* choose an appropriate image decoder */
1786 stat = get_decoder_info(stream, &codec);
1787 if (stat != Ok) return stat;
1789 /* seek to the start of the stream */
1790 seek.QuadPart = 0;
1791 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
1792 if (FAILED(hr)) return hresult_to_status(hr);
1794 /* call on the image decoder to do the real work */
1795 stat = codec->decode_func(stream, &codec->info.Clsid, image);
1797 /* take note of the original data format */
1798 if (stat == Ok)
1800 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
1803 return stat;
1806 /* FIXME: no ICM */
1807 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1809 TRACE("%p %p\n", stream, image);
1811 return GdipLoadImageFromStream(stream, image);
1814 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1816 static int calls;
1818 if(!image)
1819 return InvalidParameter;
1821 if(!(calls++))
1822 FIXME("not implemented\n");
1824 return NotImplemented;
1827 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1829 static int calls;
1831 if(!(calls++))
1832 FIXME("not implemented\n");
1834 return NotImplemented;
1837 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1838 GDIPCONST CLSID *clsidEncoder,
1839 GDIPCONST EncoderParameters *encoderParams)
1841 GpStatus stat;
1842 IStream *stream;
1844 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1846 if (!image || !filename|| !clsidEncoder)
1847 return InvalidParameter;
1849 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1850 if (stat != Ok)
1851 return GenericError;
1853 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1855 IStream_Release(stream);
1856 return stat;
1859 /*************************************************************************
1860 * Encoding functions -
1861 * These functions encode an image in different image file formats.
1863 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1864 #define BITMAP_FORMAT_JPEG 0xd8ff
1865 #define BITMAP_FORMAT_GIF 0x4947
1866 #define BITMAP_FORMAT_PNG 0x5089
1867 #define BITMAP_FORMAT_APM 0xcdd7
1869 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
1870 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1872 GpStatus stat;
1873 GpBitmap *bitmap;
1874 IWICBitmapEncoder *encoder;
1875 IWICBitmapFrameEncode *frameencode;
1876 IPropertyBag2 *encoderoptions;
1877 HRESULT hr;
1878 UINT width, height;
1879 PixelFormat gdipformat=0;
1880 WICPixelFormatGUID wicformat;
1881 GpRect rc;
1882 BitmapData lockeddata;
1883 HRESULT initresult;
1884 UINT i;
1886 if (image->type != ImageTypeBitmap)
1887 return GenericError;
1889 bitmap = (GpBitmap*)image;
1891 GdipGetImageWidth(image, &width);
1892 GdipGetImageHeight(image, &height);
1894 rc.X = 0;
1895 rc.Y = 0;
1896 rc.Width = width;
1897 rc.Height = height;
1899 initresult = CoInitialize(NULL);
1901 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
1902 &IID_IWICBitmapEncoder, (void**)&encoder);
1903 if (FAILED(hr))
1905 if (SUCCEEDED(initresult)) CoUninitialize();
1906 return hresult_to_status(hr);
1909 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
1911 if (SUCCEEDED(hr))
1913 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
1916 if (SUCCEEDED(hr)) /* created frame */
1918 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
1920 if (SUCCEEDED(hr))
1921 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
1923 if (SUCCEEDED(hr))
1924 /* FIXME: use the resolution from the image */
1925 hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
1927 if (SUCCEEDED(hr))
1929 for (i=0; wic_pixel_formats[i]; i++)
1931 if (wic_gdip_formats[i] == bitmap->format)
1932 break;
1934 if (wic_pixel_formats[i])
1935 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
1936 else
1937 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
1939 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
1941 for (i=0; wic_pixel_formats[i]; i++)
1943 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
1944 break;
1946 if (wic_pixel_formats[i])
1947 gdipformat = wic_gdip_formats[i];
1948 else
1950 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
1951 hr = E_FAIL;
1955 if (SUCCEEDED(hr))
1957 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
1958 &lockeddata);
1960 if (stat == Ok)
1962 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
1963 BYTE *row;
1965 /* write one row at a time in case stride is negative */
1966 row = lockeddata.Scan0;
1967 for (i=0; i<lockeddata.Height; i++)
1969 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
1970 if (FAILED(hr)) break;
1971 row += lockeddata.Stride;
1974 GdipBitmapUnlockBits(bitmap, &lockeddata);
1976 else
1977 hr = E_FAIL;
1980 if (SUCCEEDED(hr))
1981 hr = IWICBitmapFrameEncode_Commit(frameencode);
1983 IWICBitmapFrameEncode_Release(frameencode);
1984 IPropertyBag2_Release(encoderoptions);
1987 if (SUCCEEDED(hr))
1988 hr = IWICBitmapEncoder_Commit(encoder);
1990 IWICBitmapEncoder_Release(encoder);
1992 if (SUCCEEDED(initresult)) CoUninitialize();
1994 return hresult_to_status(hr);
1997 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
1998 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2000 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
2003 /*****************************************************************************
2004 * GdipSaveImageToStream [GDIPLUS.@]
2006 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
2007 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2009 GpStatus stat;
2010 encode_image_func encode_image;
2011 int i;
2013 TRACE("%p %p %p %p\n", image, stream, clsid, params);
2015 if(!image || !stream)
2016 return InvalidParameter;
2018 /* select correct encoder */
2019 encode_image = NULL;
2020 for (i = 0; i < NUM_CODECS; i++) {
2021 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
2022 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
2023 encode_image = codecs[i].encode_func;
2025 if (encode_image == NULL)
2026 return UnknownImageFormat;
2028 stat = encode_image(image, stream, clsid, params);
2030 return stat;
2033 /*****************************************************************************
2034 * GdipGetImagePalette [GDIPLUS.@]
2036 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
2038 static int calls = 0;
2040 if(!image)
2041 return InvalidParameter;
2043 if(!(calls++))
2044 FIXME("not implemented\n");
2046 return NotImplemented;
2049 /*****************************************************************************
2050 * GdipSetImagePalette [GDIPLUS.@]
2052 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
2053 GDIPCONST ColorPalette *palette)
2055 static int calls;
2057 if(!image || !palette)
2058 return InvalidParameter;
2060 if(!(calls++))
2061 FIXME("not implemented\n");
2063 return NotImplemented;
2066 /*************************************************************************
2067 * Encoders -
2068 * Structures that represent which formats we support for encoding.
2071 /* ImageCodecInfo creation routines taken from libgdiplus */
2072 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
2073 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
2074 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
2075 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
2076 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
2077 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
2079 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
2080 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
2081 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
2082 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
2083 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
2084 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
2086 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
2087 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
2088 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
2089 static const WCHAR gif_format[] = {'G','I','F',0};
2090 static const BYTE gif_sig_pattern[4] = "GIF8";
2091 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2093 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
2094 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
2095 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
2096 static const WCHAR emf_format[] = {'E','M','F',0};
2097 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
2098 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2100 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
2101 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
2102 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
2103 static const WCHAR wmf_format[] = {'W','M','F',0};
2104 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
2105 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
2107 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
2108 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
2109 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
2110 static const WCHAR png_format[] = {'P','N','G',0};
2111 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
2112 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2114 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
2115 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
2116 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
2117 static const WCHAR ico_format[] = {'I','C','O',0};
2118 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
2119 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2121 static const struct image_codec codecs[NUM_CODECS] = {
2123 { /* BMP */
2124 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2125 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2126 /* CodecName */ bmp_codecname,
2127 /* DllName */ NULL,
2128 /* FormatDescription */ bmp_format,
2129 /* FilenameExtension */ bmp_extension,
2130 /* MimeType */ bmp_mimetype,
2131 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2132 /* Version */ 1,
2133 /* SigCount */ 1,
2134 /* SigSize */ 2,
2135 /* SigPattern */ bmp_sig_pattern,
2136 /* SigMask */ bmp_sig_mask,
2138 encode_image_BMP,
2139 decode_image_bmp
2142 { /* JPEG */
2143 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2144 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2145 /* CodecName */ jpeg_codecname,
2146 /* DllName */ NULL,
2147 /* FormatDescription */ jpeg_format,
2148 /* FilenameExtension */ jpeg_extension,
2149 /* MimeType */ jpeg_mimetype,
2150 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2151 /* Version */ 1,
2152 /* SigCount */ 1,
2153 /* SigSize */ 2,
2154 /* SigPattern */ jpeg_sig_pattern,
2155 /* SigMask */ jpeg_sig_mask,
2157 NULL,
2158 decode_image_jpeg
2161 { /* GIF */
2162 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2163 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2164 /* CodecName */ gif_codecname,
2165 /* DllName */ NULL,
2166 /* FormatDescription */ gif_format,
2167 /* FilenameExtension */ gif_extension,
2168 /* MimeType */ gif_mimetype,
2169 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2170 /* Version */ 1,
2171 /* SigCount */ 1,
2172 /* SigSize */ 4,
2173 /* SigPattern */ gif_sig_pattern,
2174 /* SigMask */ gif_sig_mask,
2176 NULL,
2177 decode_image_gif
2180 { /* EMF */
2181 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2182 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2183 /* CodecName */ emf_codecname,
2184 /* DllName */ NULL,
2185 /* FormatDescription */ emf_format,
2186 /* FilenameExtension */ emf_extension,
2187 /* MimeType */ emf_mimetype,
2188 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2189 /* Version */ 1,
2190 /* SigCount */ 1,
2191 /* SigSize */ 4,
2192 /* SigPattern */ emf_sig_pattern,
2193 /* SigMask */ emf_sig_mask,
2195 NULL,
2196 decode_image_olepicture_metafile
2199 { /* WMF */
2200 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2201 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2202 /* CodecName */ wmf_codecname,
2203 /* DllName */ NULL,
2204 /* FormatDescription */ wmf_format,
2205 /* FilenameExtension */ wmf_extension,
2206 /* MimeType */ wmf_mimetype,
2207 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2208 /* Version */ 1,
2209 /* SigCount */ 1,
2210 /* SigSize */ 2,
2211 /* SigPattern */ wmf_sig_pattern,
2212 /* SigMask */ wmf_sig_mask,
2214 NULL,
2215 decode_image_olepicture_metafile
2218 { /* PNG */
2219 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2220 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2221 /* CodecName */ png_codecname,
2222 /* DllName */ NULL,
2223 /* FormatDescription */ png_format,
2224 /* FilenameExtension */ png_extension,
2225 /* MimeType */ png_mimetype,
2226 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2227 /* Version */ 1,
2228 /* SigCount */ 1,
2229 /* SigSize */ 8,
2230 /* SigPattern */ png_sig_pattern,
2231 /* SigMask */ png_sig_mask,
2233 NULL,
2234 decode_image_png
2237 { /* ICO */
2238 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2239 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2240 /* CodecName */ ico_codecname,
2241 /* DllName */ NULL,
2242 /* FormatDescription */ ico_format,
2243 /* FilenameExtension */ ico_extension,
2244 /* MimeType */ ico_mimetype,
2245 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2246 /* Version */ 1,
2247 /* SigCount */ 1,
2248 /* SigSize */ 4,
2249 /* SigPattern */ ico_sig_pattern,
2250 /* SigMask */ ico_sig_mask,
2252 NULL,
2253 decode_image_icon
2257 /*****************************************************************************
2258 * GdipGetImageDecodersSize [GDIPLUS.@]
2260 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
2262 int decoder_count=0;
2263 int i;
2264 TRACE("%p %p\n", numDecoders, size);
2266 if (!numDecoders || !size)
2267 return InvalidParameter;
2269 for (i=0; i<NUM_CODECS; i++)
2271 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2272 decoder_count++;
2275 *numDecoders = decoder_count;
2276 *size = decoder_count * sizeof(ImageCodecInfo);
2278 return Ok;
2281 /*****************************************************************************
2282 * GdipGetImageDecoders [GDIPLUS.@]
2284 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
2286 int i, decoder_count=0;
2287 TRACE("%u %u %p\n", numDecoders, size, decoders);
2289 if (!decoders ||
2290 size != numDecoders * sizeof(ImageCodecInfo))
2291 return GenericError;
2293 for (i=0; i<NUM_CODECS; i++)
2295 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2297 if (decoder_count == numDecoders) return GenericError;
2298 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2299 decoder_count++;
2303 if (decoder_count < numDecoders) return GenericError;
2305 return Ok;
2308 /*****************************************************************************
2309 * GdipGetImageEncodersSize [GDIPLUS.@]
2311 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
2313 int encoder_count=0;
2314 int i;
2315 TRACE("%p %p\n", numEncoders, size);
2317 if (!numEncoders || !size)
2318 return InvalidParameter;
2320 for (i=0; i<NUM_CODECS; i++)
2322 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2323 encoder_count++;
2326 *numEncoders = encoder_count;
2327 *size = encoder_count * sizeof(ImageCodecInfo);
2329 return Ok;
2332 /*****************************************************************************
2333 * GdipGetImageEncoders [GDIPLUS.@]
2335 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
2337 int i, encoder_count=0;
2338 TRACE("%u %u %p\n", numEncoders, size, encoders);
2340 if (!encoders ||
2341 size != numEncoders * sizeof(ImageCodecInfo))
2342 return GenericError;
2344 for (i=0; i<NUM_CODECS; i++)
2346 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2348 if (encoder_count == numEncoders) return GenericError;
2349 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2350 encoder_count++;
2354 if (encoder_count < numEncoders) return GenericError;
2356 return Ok;
2359 /*****************************************************************************
2360 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
2362 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
2364 BITMAP bm;
2365 GpStatus retval;
2366 PixelFormat format;
2367 BYTE* bits;
2369 TRACE("%p %p %p\n", hbm, hpal, bitmap);
2371 if(!hbm || !bitmap)
2372 return InvalidParameter;
2374 /* TODO: Support for device-dependent bitmaps */
2375 if(hpal){
2376 FIXME("no support for device-dependent bitmaps\n");
2377 return NotImplemented;
2380 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
2381 return InvalidParameter;
2383 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
2384 switch(bm.bmBitsPixel) {
2385 case 1:
2386 format = PixelFormat1bppIndexed;
2387 break;
2388 case 4:
2389 format = PixelFormat4bppIndexed;
2390 break;
2391 case 8:
2392 format = PixelFormat8bppIndexed;
2393 break;
2394 case 24:
2395 format = PixelFormat24bppRGB;
2396 break;
2397 case 32:
2398 format = PixelFormat32bppRGB;
2399 break;
2400 case 48:
2401 format = PixelFormat48bppRGB;
2402 break;
2403 default:
2404 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
2405 return InvalidParameter;
2408 if (bm.bmBits)
2409 bits = (BYTE*)bm.bmBits + (bm.bmHeight - 1) * bm.bmWidthBytes;
2410 else
2412 FIXME("can only get image data from DIB sections\n");
2413 bits = NULL;
2416 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, -bm.bmWidthBytes,
2417 format, bits, bitmap);
2419 return retval;
2422 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
2424 FIXME("(%p): stub\n", effect);
2425 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
2426 * in Windows's gdiplus */
2427 return NotImplemented;
2430 /*****************************************************************************
2431 * GdipSetEffectParameters [GDIPLUS.@]
2433 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
2434 const VOID *params, const UINT size)
2436 static int calls;
2438 if(!(calls++))
2439 FIXME("not implemented\n");
2441 return NotImplemented;
2444 /*****************************************************************************
2445 * GdipGetImageFlags [GDIPLUS.@]
2447 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
2449 TRACE("%p %p\n", image, flags);
2451 if(!image || !flags)
2452 return InvalidParameter;
2454 *flags = image->flags;
2456 return Ok;
2459 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
2461 TRACE("(%d, %p)\n", control, param);
2463 switch(control){
2464 case TestControlForceBilinear:
2465 if(param)
2466 FIXME("TestControlForceBilinear not handled\n");
2467 break;
2468 case TestControlNoICM:
2469 if(param)
2470 FIXME("TestControlNoICM not handled\n");
2471 break;
2472 case TestControlGetBuildNumber:
2473 *((DWORD*)param) = 3102;
2474 break;
2477 return Ok;
2480 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
2481 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
2482 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
2483 GpMetafile **metafile)
2485 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2486 frameUnit, debugstr_w(desc), metafile);
2488 return NotImplemented;
2491 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
2492 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
2493 GDIPCONST WCHAR *desc, GpMetafile **metafile)
2495 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2496 frameUnit, debugstr_w(desc), metafile);
2498 return NotImplemented;
2501 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
2503 FIXME("%p\n", image);
2505 return Ok;
2508 /*****************************************************************************
2509 * GdipGetImageThumbnail [GDIPLUS.@]
2511 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
2512 GpImage **ret_image, GetThumbnailImageAbort cb,
2513 VOID * cb_data)
2515 FIXME("(%p %u %u %p %p %p) stub\n",
2516 image, width, height, ret_image, cb, cb_data);
2517 return NotImplemented;
2520 /*****************************************************************************
2521 * GdipImageRotateFlip [GDIPLUS.@]
2523 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
2525 FIXME("(%p %u) stub\n", image, type);
2526 return NotImplemented;