d3dxof: Do not expect a separator when there is no element.
[wine/multimedia.git] / dlls / gdiplus / image.c
blob4fd593fcb639c410ce18e2ba267be3c1ba334185
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_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
99 *index = (row[x/8]>>(7-x%8)) & 1;
102 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
104 if (x & 1)
105 *index = row[x/2]&0xf;
106 else
107 *index = row[x/2]>>4;
110 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
112 *index = row[x];
115 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
116 const BYTE *row, UINT x)
118 *r = *g = *b = row[x*2+1];
119 *a = 255;
122 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
123 const BYTE *row, UINT x)
125 WORD pixel = *((WORD*)(row)+x);
126 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
127 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
128 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
129 *a = 255;
132 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
133 const BYTE *row, UINT x)
135 WORD pixel = *((WORD*)(row)+x);
136 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
137 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
138 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
139 *a = 255;
142 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
143 const BYTE *row, UINT x)
145 WORD pixel = *((WORD*)(row)+x);
146 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
147 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
148 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
149 if ((pixel&0x8000) == 0x8000)
150 *a = 255;
151 else
152 *a = 0;
155 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
156 const BYTE *row, UINT x)
158 *r = row[x*3+2];
159 *g = row[x*3+1];
160 *b = row[x*3];
161 *a = 255;
164 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
165 const BYTE *row, UINT x)
167 *r = row[x*4+2];
168 *g = row[x*4+1];
169 *b = row[x*4];
170 *a = 255;
173 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
174 const BYTE *row, UINT x)
176 *r = row[x*4+2];
177 *g = row[x*4+1];
178 *b = row[x*4];
179 *a = row[x*4+3];
182 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
183 const BYTE *row, UINT x)
185 *a = row[x*4+3];
186 if (*a == 0)
187 *r = *g = *b = 0;
188 else
190 *r = row[x*4+2] * 255 / *a;
191 *g = row[x*4+1] * 255 / *a;
192 *b = row[x*4] * 255 / *a;
196 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
197 const BYTE *row, UINT x)
199 *r = row[x*6+5];
200 *g = row[x*6+3];
201 *b = row[x*6+1];
202 *a = 255;
205 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
206 const BYTE *row, UINT x)
208 *r = row[x*8+5];
209 *g = row[x*8+3];
210 *b = row[x*8+1];
211 *a = row[x*8+7];
214 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
215 const BYTE *row, UINT x)
217 *a = row[x*8+7];
218 if (*a == 0)
219 *r = *g = *b = 0;
220 else
222 *r = row[x*8+5] * 255 / *a;
223 *g = row[x*8+3] * 255 / *a;
224 *b = row[x*8+1] * 255 / *a;
228 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
229 ARGB *color)
231 BYTE r, g, b, a;
232 BYTE index;
233 BYTE *row;
234 TRACE("%p %d %d %p\n", bitmap, x, y, color);
236 if(!bitmap || !color ||
237 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
238 return InvalidParameter;
240 row = bitmap->bits+bitmap->stride*y;
242 switch (bitmap->format)
244 case PixelFormat1bppIndexed:
245 getpixel_1bppIndexed(&index,row,x);
246 break;
247 case PixelFormat4bppIndexed:
248 getpixel_4bppIndexed(&index,row,x);
249 break;
250 case PixelFormat8bppIndexed:
251 getpixel_8bppIndexed(&index,row,x);
252 break;
253 case PixelFormat16bppGrayScale:
254 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
255 break;
256 case PixelFormat16bppRGB555:
257 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
258 break;
259 case PixelFormat16bppRGB565:
260 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
261 break;
262 case PixelFormat16bppARGB1555:
263 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
264 break;
265 case PixelFormat24bppRGB:
266 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
267 break;
268 case PixelFormat32bppRGB:
269 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
270 break;
271 case PixelFormat32bppARGB:
272 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
273 break;
274 case PixelFormat32bppPARGB:
275 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
276 break;
277 case PixelFormat48bppRGB:
278 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
279 break;
280 case PixelFormat64bppARGB:
281 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
282 break;
283 case PixelFormat64bppPARGB:
284 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
285 break;
286 default:
287 FIXME("not implemented for format 0x%x\n", bitmap->format);
288 return NotImplemented;
291 if (bitmap->format & PixelFormatIndexed)
292 *color = bitmap->image.palette_entries[index];
293 else
294 *color = a<<24|r<<16|g<<8|b;
296 return Ok;
299 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
300 BYTE *row, UINT x)
302 *((WORD*)(row)+x) = (r+g+b)*85;
305 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
306 BYTE *row, UINT x)
308 *((WORD*)(row)+x) = (r<<7&0x7c00)|
309 (g<<2&0x03e0)|
310 (b>>3&0x001f);
313 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
314 BYTE *row, UINT x)
316 *((WORD*)(row)+x) = (r<<8&0xf800)|
317 (g<<3&0x07e0)|
318 (b>>3&0x001f);
321 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
322 BYTE *row, UINT x)
324 *((WORD*)(row)+x) = (a<<8&0x8000)|
325 (r<<7&0x7c00)|
326 (g<<2&0x03e0)|
327 (b>>3&0x001f);
330 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
331 BYTE *row, UINT x)
333 row[x*3+2] = r;
334 row[x*3+1] = g;
335 row[x*3] = b;
338 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
339 BYTE *row, UINT x)
341 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
344 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
345 BYTE *row, UINT x)
347 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
350 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
351 BYTE *row, UINT x)
353 r = r * a / 255;
354 g = g * a / 255;
355 b = b * a / 255;
356 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
359 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
360 BYTE *row, UINT x)
362 row[x*6+5] = row[x*6+4] = r;
363 row[x*6+3] = row[x*6+2] = g;
364 row[x*6+1] = row[x*6] = b;
367 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
368 BYTE *row, UINT x)
370 UINT64 a64=a, r64=r, g64=g, b64=b;
371 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
374 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
375 BYTE *row, UINT x)
377 UINT64 a64, r64, g64, b64;
378 a64 = a * 257;
379 r64 = r * a / 255;
380 g64 = g * a / 255;
381 b64 = b * a / 255;
382 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
385 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
386 ARGB color)
388 BYTE a, r, g, b;
389 BYTE *row;
390 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
392 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
393 return InvalidParameter;
395 a = color>>24;
396 r = color>>16;
397 g = color>>8;
398 b = color;
400 row = bitmap->bits + bitmap->stride * y;
402 switch (bitmap->format)
404 case PixelFormat16bppGrayScale:
405 setpixel_16bppGrayScale(r,g,b,a,row,x);
406 break;
407 case PixelFormat16bppRGB555:
408 setpixel_16bppRGB555(r,g,b,a,row,x);
409 break;
410 case PixelFormat16bppRGB565:
411 setpixel_16bppRGB565(r,g,b,a,row,x);
412 break;
413 case PixelFormat16bppARGB1555:
414 setpixel_16bppARGB1555(r,g,b,a,row,x);
415 break;
416 case PixelFormat24bppRGB:
417 setpixel_24bppRGB(r,g,b,a,row,x);
418 break;
419 case PixelFormat32bppRGB:
420 setpixel_32bppRGB(r,g,b,a,row,x);
421 break;
422 case PixelFormat32bppARGB:
423 setpixel_32bppARGB(r,g,b,a,row,x);
424 break;
425 case PixelFormat32bppPARGB:
426 setpixel_32bppPARGB(r,g,b,a,row,x);
427 break;
428 case PixelFormat48bppRGB:
429 setpixel_48bppRGB(r,g,b,a,row,x);
430 break;
431 case PixelFormat64bppARGB:
432 setpixel_64bppARGB(r,g,b,a,row,x);
433 break;
434 case PixelFormat64bppPARGB:
435 setpixel_64bppPARGB(r,g,b,a,row,x);
436 break;
437 default:
438 FIXME("not implemented for format 0x%x\n", bitmap->format);
439 return NotImplemented;
442 return Ok;
445 /* This function returns a pointer to an array of pixels that represents the
446 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
447 * flags. It is correct behavior that a user who calls this function with write
448 * privileges can write to the whole bitmap (not just the area in rect).
450 * FIXME: only used portion of format is bits per pixel. */
451 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
452 UINT flags, PixelFormat format, BitmapData* lockeddata)
454 BOOL bm_is_selected;
455 INT stride, bitspp = PIXELFORMATBPP(format);
456 HDC hdc;
457 HBITMAP hbm, old = NULL;
458 BITMAPINFO *pbmi;
459 BYTE *buff = NULL;
460 UINT abs_height;
461 GpRect act_rect; /* actual rect to be used */
463 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
465 if(!lockeddata || !bitmap)
466 return InvalidParameter;
468 if(rect){
469 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
470 (rect->Y + rect->Height > bitmap->height) || !flags)
471 return InvalidParameter;
473 act_rect = *rect;
475 else{
476 act_rect.X = act_rect.Y = 0;
477 act_rect.Width = bitmap->width;
478 act_rect.Height = bitmap->height;
481 if(flags & ImageLockModeUserInputBuf)
482 return NotImplemented;
484 if(bitmap->lockmode)
485 return WrongState;
487 if (bitmap->bits && bitmap->format == format)
489 /* no conversion is necessary; just use the bits directly */
490 lockeddata->Width = act_rect.Width;
491 lockeddata->Height = act_rect.Height;
492 lockeddata->PixelFormat = format;
493 lockeddata->Reserved = flags;
494 lockeddata->Stride = bitmap->stride;
495 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
496 bitmap->stride * act_rect.Y;
498 bitmap->lockmode = flags;
499 bitmap->numlocks++;
501 return Ok;
504 hbm = bitmap->hbitmap;
505 hdc = bitmap->hdc;
506 bm_is_selected = (hdc != 0);
508 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
509 if (!pbmi)
510 return OutOfMemory;
511 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
512 pbmi->bmiHeader.biBitCount = 0;
514 if(!bm_is_selected){
515 hdc = CreateCompatibleDC(0);
516 old = SelectObject(hdc, hbm);
519 /* fill out bmi */
520 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
522 abs_height = abs(pbmi->bmiHeader.biHeight);
523 stride = pbmi->bmiHeader.biWidth * bitspp / 8;
524 stride = (stride + 3) & ~3;
526 buff = GdipAlloc(stride * abs_height);
528 pbmi->bmiHeader.biBitCount = bitspp;
530 if(buff)
531 GetDIBits(hdc, hbm, 0, abs_height, buff, pbmi, DIB_RGB_COLORS);
533 if(!bm_is_selected){
534 SelectObject(hdc, old);
535 DeleteDC(hdc);
538 if(!buff){
539 GdipFree(pbmi);
540 return OutOfMemory;
543 lockeddata->Width = act_rect.Width;
544 lockeddata->Height = act_rect.Height;
545 lockeddata->PixelFormat = format;
546 lockeddata->Reserved = flags;
548 if(pbmi->bmiHeader.biHeight > 0){
549 lockeddata->Stride = -stride;
550 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
551 stride * (abs_height - 1 - act_rect.Y);
553 else{
554 lockeddata->Stride = stride;
555 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
558 bitmap->lockmode = flags;
559 bitmap->numlocks++;
561 bitmap->bitmapbits = buff;
563 GdipFree(pbmi);
564 return Ok;
567 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
569 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
571 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
572 return InvalidParameter;
574 bitmap->image.xres = xdpi;
575 bitmap->image.yres = ydpi;
577 return Ok;
580 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
581 BitmapData* lockeddata)
583 HDC hdc;
584 HBITMAP hbm, old = NULL;
585 BOOL bm_is_selected;
586 BITMAPINFO *pbmi;
588 TRACE("(%p,%p)\n", bitmap, lockeddata);
590 if(!bitmap || !lockeddata)
591 return InvalidParameter;
593 if(!bitmap->lockmode)
594 return WrongState;
596 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
597 return NotImplemented;
599 if(lockeddata->Reserved & ImageLockModeRead){
600 if(!(--bitmap->numlocks))
601 bitmap->lockmode = 0;
603 GdipFree(bitmap->bitmapbits);
604 bitmap->bitmapbits = NULL;
605 return Ok;
608 if (!bitmap->bitmapbits)
610 /* we passed a direct reference; no need to do anything */
611 bitmap->lockmode = 0;
612 bitmap->numlocks = 0;
613 return Ok;
616 hbm = bitmap->hbitmap;
617 hdc = bitmap->hdc;
618 bm_is_selected = (hdc != 0);
620 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
621 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
622 pbmi->bmiHeader.biBitCount = 0;
624 if(!bm_is_selected){
625 hdc = CreateCompatibleDC(0);
626 old = SelectObject(hdc, hbm);
629 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
630 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
631 SetDIBits(hdc, hbm, 0, abs(pbmi->bmiHeader.biHeight),
632 bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
634 if(!bm_is_selected){
635 SelectObject(hdc, old);
636 DeleteDC(hdc);
639 GdipFree(pbmi);
640 GdipFree(bitmap->bitmapbits);
641 bitmap->bitmapbits = NULL;
642 bitmap->lockmode = 0;
643 bitmap->numlocks = 0;
645 return Ok;
648 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
649 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
651 BitmapData lockeddata_src, lockeddata_dst;
652 int i;
653 UINT row_size;
654 Rect area;
655 GpStatus stat;
657 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
659 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
660 x < 0 || y < 0 ||
661 x + width > srcBitmap->width || y + height > srcBitmap->height)
663 TRACE("<-- InvalidParameter\n");
664 return InvalidParameter;
667 if (format == PixelFormatDontCare)
668 format = srcBitmap->format;
670 area.X = roundr(x);
671 area.Y = roundr(y);
672 area.Width = roundr(width);
673 area.Height = roundr(height);
675 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
676 &lockeddata_src);
677 if (stat != Ok) return stat;
679 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
680 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
681 if (stat == Ok)
683 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
684 lockeddata_src.PixelFormat, &lockeddata_dst);
686 if (stat == Ok)
688 /* copy the image data */
689 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
690 for (i=0; i<lockeddata_src.Height; i++)
691 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
692 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
693 row_size);
695 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
698 if (stat != Ok)
699 GdipDisposeImage((GpImage*)*dstBitmap);
702 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
704 if (stat != Ok)
706 *dstBitmap = NULL;
709 return stat;
712 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
713 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
715 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
717 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
720 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
722 GpStatus stat = GenericError;
724 TRACE("%p, %p\n", image, cloneImage);
726 if (!image || !cloneImage)
727 return InvalidParameter;
729 if (image->picture)
731 IStream* stream;
732 HRESULT hr;
733 INT size;
734 LARGE_INTEGER move;
736 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
737 if (FAILED(hr))
738 return GenericError;
740 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
741 if(FAILED(hr))
743 WARN("Failed to save image on stream\n");
744 goto out;
747 /* Set seek pointer back to the beginning of the picture */
748 move.QuadPart = 0;
749 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
750 if (FAILED(hr))
751 goto out;
753 stat = GdipLoadImageFromStream(stream, cloneImage);
754 if (stat != Ok) WARN("Failed to load image from stream\n");
756 out:
757 IStream_Release(stream);
758 return stat;
760 else if (image->type == ImageTypeBitmap)
762 GpBitmap *bitmap = (GpBitmap*)image;
763 BitmapData lockeddata_src, lockeddata_dst;
764 int i;
765 UINT row_size;
767 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
768 &lockeddata_src);
769 if (stat != Ok) return stat;
771 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
772 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
773 if (stat == Ok)
775 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
776 lockeddata_src.PixelFormat, &lockeddata_dst);
778 if (stat == Ok)
780 /* copy the image data */
781 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
782 for (i=0; i<lockeddata_src.Height; i++)
783 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
784 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
785 row_size);
787 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
790 if (stat != Ok)
791 GdipDisposeImage(*cloneImage);
794 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
796 if (stat != Ok)
798 *cloneImage = NULL;
800 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
802 return stat;
804 else
806 ERR("GpImage with no IPicture or bitmap?!\n");
807 return NotImplemented;
811 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
812 GpBitmap **bitmap)
814 GpStatus stat;
815 IStream *stream;
817 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
819 if(!filename || !bitmap)
820 return InvalidParameter;
822 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
824 if(stat != Ok)
825 return stat;
827 stat = GdipCreateBitmapFromStream(stream, bitmap);
829 IStream_Release(stream);
831 return stat;
834 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
835 VOID *bits, GpBitmap **bitmap)
837 DWORD height, stride;
838 PixelFormat format;
840 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
842 height = abs(info->bmiHeader.biHeight);
843 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
845 if(info->bmiHeader.biHeight > 0) /* bottom-up */
847 bits = (BYTE*)bits + (height - 1) * stride;
848 stride = -stride;
851 switch(info->bmiHeader.biBitCount) {
852 case 1:
853 format = PixelFormat1bppIndexed;
854 break;
855 case 4:
856 format = PixelFormat4bppIndexed;
857 break;
858 case 8:
859 format = PixelFormat8bppIndexed;
860 break;
861 case 24:
862 format = PixelFormat24bppRGB;
863 break;
864 default:
865 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
866 *bitmap = NULL;
867 return InvalidParameter;
870 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
871 bits, bitmap);
875 /* FIXME: no icm */
876 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
877 GpBitmap **bitmap)
879 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
881 return GdipCreateBitmapFromFile(filename, bitmap);
884 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
885 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
887 HBITMAP hbm;
888 GpStatus stat = InvalidParameter;
890 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
892 if(!lpBitmapName || !bitmap)
893 return InvalidParameter;
895 /* load DIB */
896 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
897 LR_CREATEDIBSECTION);
899 if(hbm){
900 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
901 DeleteObject(hbm);
904 return stat;
907 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
908 HBITMAP* hbmReturn, ARGB background)
910 GpStatus stat;
911 HBITMAP result, oldbitmap;
912 UINT width, height;
913 HDC hdc;
914 GpGraphics *graphics;
915 BITMAPINFOHEADER bih;
916 void *bits;
917 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
919 if (!bitmap || !hbmReturn) return InvalidParameter;
921 GdipGetImageWidth((GpImage*)bitmap, &width);
922 GdipGetImageHeight((GpImage*)bitmap, &height);
924 bih.biSize = sizeof(bih);
925 bih.biWidth = width;
926 bih.biHeight = height;
927 bih.biPlanes = 1;
928 bih.biBitCount = 32;
929 bih.biCompression = BI_RGB;
930 bih.biSizeImage = 0;
931 bih.biXPelsPerMeter = 0;
932 bih.biYPelsPerMeter = 0;
933 bih.biClrUsed = 0;
934 bih.biClrImportant = 0;
936 hdc = CreateCompatibleDC(NULL);
937 if (!hdc) return GenericError;
939 result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
940 NULL, 0);
942 if (result)
944 oldbitmap = SelectObject(hdc, result);
946 stat = GdipCreateFromHDC(hdc, &graphics);
947 if (stat == Ok)
949 stat = GdipGraphicsClear(graphics, background);
951 if (stat == Ok)
952 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
954 GdipDeleteGraphics(graphics);
957 SelectObject(hdc, oldbitmap);
959 else
960 stat = GenericError;
962 DeleteDC(hdc);
964 if (stat != Ok && result)
966 DeleteObject(result);
967 result = NULL;
970 *hbmReturn = result;
972 return stat;
975 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
976 GpMetafile* metafile, BOOL* succ, EmfType emfType,
977 const WCHAR* description, GpMetafile** out_metafile)
979 static int calls;
981 TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
982 debugstr_w(description), out_metafile);
984 if(!ref || !metafile || !out_metafile)
985 return InvalidParameter;
987 *succ = FALSE;
988 *out_metafile = NULL;
990 if(!(calls++))
991 FIXME("not implemented\n");
993 return NotImplemented;
996 /* FIXME: this should create a bitmap in the given size with the attributes
997 * (resolution etc.) of the graphics object */
998 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
999 GpGraphics* target, GpBitmap** bitmap)
1001 static int calls;
1002 GpStatus ret;
1004 if(!target || !bitmap)
1005 return InvalidParameter;
1007 if(!(calls++))
1008 FIXME("hacked stub\n");
1010 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1011 NULL, bitmap);
1013 return ret;
1016 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1018 GpStatus stat;
1019 ICONINFO iinfo;
1020 BITMAP bm;
1021 int ret;
1022 UINT width, height;
1023 GpRect rect;
1024 BitmapData lockeddata;
1025 HDC screendc;
1026 BOOL has_alpha;
1027 int x, y;
1028 BYTE *bits;
1029 BITMAPINFOHEADER bih;
1030 DWORD *src;
1031 BYTE *dst_row;
1032 DWORD *dst;
1034 TRACE("%p, %p\n", hicon, bitmap);
1036 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1037 return InvalidParameter;
1039 /* get the size of the icon */
1040 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1041 if (ret == 0) {
1042 DeleteObject(iinfo.hbmColor);
1043 DeleteObject(iinfo.hbmMask);
1044 return GenericError;
1047 width = bm.bmWidth;
1049 if (iinfo.hbmColor)
1050 height = abs(bm.bmHeight);
1051 else /* combined bitmap + mask */
1052 height = abs(bm.bmHeight) / 2;
1054 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1055 if (!bits) {
1056 DeleteObject(iinfo.hbmColor);
1057 DeleteObject(iinfo.hbmMask);
1058 return OutOfMemory;
1061 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1062 if (stat != Ok) {
1063 DeleteObject(iinfo.hbmColor);
1064 DeleteObject(iinfo.hbmMask);
1065 HeapFree(GetProcessHeap(), 0, bits);
1066 return stat;
1069 rect.X = 0;
1070 rect.Y = 0;
1071 rect.Width = width;
1072 rect.Height = height;
1074 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1075 if (stat != Ok) {
1076 DeleteObject(iinfo.hbmColor);
1077 DeleteObject(iinfo.hbmMask);
1078 HeapFree(GetProcessHeap(), 0, bits);
1079 GdipDisposeImage((GpImage*)*bitmap);
1080 return stat;
1083 bih.biSize = sizeof(bih);
1084 bih.biWidth = width;
1085 bih.biHeight = -height;
1086 bih.biPlanes = 1;
1087 bih.biBitCount = 32;
1088 bih.biCompression = BI_RGB;
1089 bih.biSizeImage = 0;
1090 bih.biXPelsPerMeter = 0;
1091 bih.biYPelsPerMeter = 0;
1092 bih.biClrUsed = 0;
1093 bih.biClrImportant = 0;
1095 screendc = GetDC(0);
1096 if (iinfo.hbmColor)
1098 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1100 if (bm.bmBitsPixel == 32)
1102 has_alpha = FALSE;
1104 /* If any pixel has a non-zero alpha, ignore hbmMask */
1105 src = (DWORD*)bits;
1106 for (x=0; x<width && !has_alpha; x++)
1107 for (y=0; y<height && !has_alpha; y++)
1108 if ((*src++ & 0xff000000) != 0)
1109 has_alpha = TRUE;
1111 else has_alpha = FALSE;
1113 else
1115 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1116 has_alpha = FALSE;
1119 /* copy the image data to the Bitmap */
1120 src = (DWORD*)bits;
1121 dst_row = lockeddata.Scan0;
1122 for (y=0; y<height; y++)
1124 memcpy(dst_row, src, width*4);
1125 src += width;
1126 dst_row += lockeddata.Stride;
1129 if (!has_alpha)
1131 if (iinfo.hbmMask)
1133 /* read alpha data from the mask */
1134 if (iinfo.hbmColor)
1135 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1136 else
1137 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1139 src = (DWORD*)bits;
1140 dst_row = lockeddata.Scan0;
1141 for (y=0; y<height; y++)
1143 dst = (DWORD*)dst_row;
1144 for (x=0; x<height; x++)
1146 DWORD src_value = *src++;
1147 if (src_value)
1148 *dst++ = 0;
1149 else
1150 *dst++ |= 0xff000000;
1152 dst_row += lockeddata.Stride;
1155 else
1157 /* set constant alpha of 255 */
1158 dst_row = bits;
1159 for (y=0; y<height; y++)
1161 dst = (DWORD*)dst_row;
1162 for (x=0; x<height; x++)
1163 *dst++ |= 0xff000000;
1164 dst_row += lockeddata.Stride;
1169 ReleaseDC(0, screendc);
1171 DeleteObject(iinfo.hbmColor);
1172 DeleteObject(iinfo.hbmMask);
1174 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1176 HeapFree(GetProcessHeap(), 0, bits);
1178 return Ok;
1181 static void generate_halftone_palette(ARGB *entries, UINT count)
1183 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1184 UINT i;
1186 for (i=0; i<8 && i<count; i++)
1188 entries[i] = 0xff000000;
1189 if (i&1) entries[i] |= 0x800000;
1190 if (i&2) entries[i] |= 0x8000;
1191 if (i&4) entries[i] |= 0x80;
1194 if (8 < count)
1195 entries[i] = 0xffc0c0c0;
1197 for (i=9; i<16 && i<count; i++)
1199 entries[i] = 0xff000000;
1200 if (i&1) entries[i] |= 0xff0000;
1201 if (i&2) entries[i] |= 0xff00;
1202 if (i&4) entries[i] |= 0xff;
1205 for (i=16; i<40 && i<count; i++)
1207 entries[i] = 0;
1210 for (i=40; i<256 && i<count; i++)
1212 entries[i] = 0xff000000;
1213 entries[i] |= halftone_values[(i-40)%6];
1214 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1215 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1219 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1221 HDC screendc = GetDC(0);
1223 if (!screendc) return GenericError;
1225 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1226 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1228 ReleaseDC(0, screendc);
1230 return Ok;
1233 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1234 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1236 BITMAPINFOHEADER bmih;
1237 HBITMAP hbitmap;
1238 INT row_size, dib_stride;
1239 HDC hdc;
1240 BYTE *bits;
1241 int i;
1242 REAL xres, yres;
1243 GpStatus stat;
1245 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1247 if (!bitmap) return InvalidParameter;
1249 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1250 *bitmap = NULL;
1251 return InvalidParameter;
1254 if(scan0 && !stride)
1255 return InvalidParameter;
1257 stat = get_screen_resolution(&xres, &yres);
1258 if (stat != Ok) return stat;
1260 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1261 dib_stride = (row_size + 3) & ~3;
1263 if(stride == 0)
1264 stride = dib_stride;
1266 bmih.biSize = sizeof(BITMAPINFOHEADER);
1267 bmih.biWidth = width;
1268 bmih.biHeight = -height;
1269 bmih.biPlanes = 1;
1270 /* FIXME: use the rest of the data from format */
1271 bmih.biBitCount = PIXELFORMATBPP(format);
1272 bmih.biCompression = BI_RGB;
1273 bmih.biSizeImage = 0;
1274 bmih.biXPelsPerMeter = 0;
1275 bmih.biYPelsPerMeter = 0;
1276 bmih.biClrUsed = 0;
1277 bmih.biClrImportant = 0;
1279 hdc = CreateCompatibleDC(NULL);
1280 if (!hdc) return GenericError;
1282 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, (void**)&bits,
1283 NULL, 0);
1285 DeleteDC(hdc);
1287 if (!hbitmap) return GenericError;
1289 /* copy bits to the dib if necessary */
1290 /* FIXME: should reference the bits instead of copying them */
1291 if (scan0)
1292 for (i=0; i<height; i++)
1293 memcpy(bits+i*dib_stride, scan0+i*stride, row_size);
1295 *bitmap = GdipAlloc(sizeof(GpBitmap));
1296 if(!*bitmap)
1298 DeleteObject(hbitmap);
1299 return OutOfMemory;
1302 (*bitmap)->image.type = ImageTypeBitmap;
1303 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1304 (*bitmap)->image.flags = ImageFlagsNone;
1305 (*bitmap)->image.palette_flags = 0;
1306 (*bitmap)->image.palette_count = 0;
1307 (*bitmap)->image.palette_size = 0;
1308 (*bitmap)->image.palette_entries = NULL;
1309 (*bitmap)->image.xres = xres;
1310 (*bitmap)->image.yres = yres;
1311 (*bitmap)->width = width;
1312 (*bitmap)->height = height;
1313 (*bitmap)->format = format;
1314 (*bitmap)->image.picture = NULL;
1315 (*bitmap)->hbitmap = hbitmap;
1316 (*bitmap)->hdc = NULL;
1317 (*bitmap)->bits = bits;
1318 (*bitmap)->stride = dib_stride;
1320 if (format == PixelFormat1bppIndexed ||
1321 format == PixelFormat4bppIndexed ||
1322 format == PixelFormat8bppIndexed)
1324 (*bitmap)->image.palette_size = (*bitmap)->image.palette_count = 1 << PIXELFORMATBPP(format);
1325 (*bitmap)->image.palette_entries = GdipAlloc(sizeof(ARGB) * ((*bitmap)->image.palette_size));
1327 if (!(*bitmap)->image.palette_entries)
1329 GdipDisposeImage(&(*bitmap)->image);
1330 *bitmap = NULL;
1331 return OutOfMemory;
1334 if (format == PixelFormat1bppIndexed)
1336 (*bitmap)->image.palette_flags = PaletteFlagsGrayScale;
1337 (*bitmap)->image.palette_entries[0] = 0xff000000;
1338 (*bitmap)->image.palette_entries[1] = 0xffffffff;
1340 else
1342 if (format == PixelFormat8bppIndexed)
1343 (*bitmap)->image.palette_flags = PaletteFlagsHalftone;
1345 generate_halftone_palette((*bitmap)->image.palette_entries,
1346 (*bitmap)->image.palette_count);
1350 TRACE("<-- %p\n", *bitmap);
1352 return Ok;
1355 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1356 GpBitmap **bitmap)
1358 GpStatus stat;
1360 TRACE("%p %p\n", stream, bitmap);
1362 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1364 if(stat != Ok)
1365 return stat;
1367 if((*bitmap)->image.type != ImageTypeBitmap){
1368 GdipDisposeImage(&(*bitmap)->image);
1369 *bitmap = NULL;
1370 return GenericError; /* FIXME: what error to return? */
1373 return Ok;
1376 /* FIXME: no icm */
1377 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1378 GpBitmap **bitmap)
1380 TRACE("%p %p\n", stream, bitmap);
1382 return GdipCreateBitmapFromStream(stream, bitmap);
1385 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1386 GpCachedBitmap **cachedbmp)
1388 GpStatus stat;
1390 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1392 if(!bitmap || !graphics || !cachedbmp)
1393 return InvalidParameter;
1395 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1396 if(!*cachedbmp)
1397 return OutOfMemory;
1399 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1400 if(stat != Ok){
1401 GdipFree(*cachedbmp);
1402 return stat;
1405 return Ok;
1408 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1410 FIXME("(%p, %p)\n", bitmap, hicon);
1412 return NotImplemented;
1415 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1417 TRACE("%p\n", cachedbmp);
1419 if(!cachedbmp)
1420 return InvalidParameter;
1422 GdipDisposeImage(cachedbmp->image);
1423 GdipFree(cachedbmp);
1425 return Ok;
1428 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1429 GpCachedBitmap *cachedbmp, INT x, INT y)
1431 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1433 if(!graphics || !cachedbmp)
1434 return InvalidParameter;
1436 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1439 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1440 LPBYTE pData16, INT iMapMode, INT eFlags)
1442 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1443 return NotImplemented;
1446 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
1448 TRACE("%p\n", image);
1450 if(!image)
1451 return InvalidParameter;
1453 if (image->picture)
1454 IPicture_Release(image->picture);
1455 if (image->type == ImageTypeBitmap)
1457 GdipFree(((GpBitmap*)image)->bitmapbits);
1458 DeleteDC(((GpBitmap*)image)->hdc);
1460 GdipFree(image->palette_entries);
1461 GdipFree(image);
1463 return Ok;
1466 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
1468 static int calls;
1470 TRACE("(%p,%p)\n", image, item);
1472 if(!image || !item)
1473 return InvalidParameter;
1475 if (!(calls++))
1476 FIXME("not implemented\n");
1478 return NotImplemented;
1481 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
1482 GpUnit *srcUnit)
1484 TRACE("%p %p %p\n", image, srcRect, srcUnit);
1486 if(!image || !srcRect || !srcUnit)
1487 return InvalidParameter;
1488 if(image->type == ImageTypeMetafile){
1489 *srcRect = ((GpMetafile*)image)->bounds;
1490 *srcUnit = ((GpMetafile*)image)->unit;
1492 else if(image->type == ImageTypeBitmap){
1493 srcRect->X = srcRect->Y = 0.0;
1494 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
1495 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
1496 *srcUnit = UnitPixel;
1498 else{
1499 srcRect->X = srcRect->Y = 0.0;
1500 srcRect->Width = ipicture_pixel_width(image->picture);
1501 srcRect->Height = ipicture_pixel_height(image->picture);
1502 *srcUnit = UnitPixel;
1505 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
1506 srcRect->Width, srcRect->Height, *srcUnit);
1508 return Ok;
1511 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
1512 REAL *height)
1514 TRACE("%p %p %p\n", image, width, height);
1516 if(!image || !height || !width)
1517 return InvalidParameter;
1519 if(image->type == ImageTypeMetafile){
1520 HDC hdc = GetDC(0);
1522 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1523 ((GpMetafile*)image)->bounds.Height;
1525 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1526 ((GpMetafile*)image)->bounds.Width;
1528 ReleaseDC(0, hdc);
1531 else if(image->type == ImageTypeBitmap){
1532 *height = ((GpBitmap*)image)->height;
1533 *width = ((GpBitmap*)image)->width;
1535 else{
1536 *height = ipicture_pixel_height(image->picture);
1537 *width = ipicture_pixel_width(image->picture);
1540 TRACE("returning (%f, %f)\n", *height, *width);
1541 return Ok;
1544 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
1545 GpGraphics **graphics)
1547 HDC hdc;
1549 TRACE("%p %p\n", image, graphics);
1551 if(!image || !graphics)
1552 return InvalidParameter;
1554 if(image->type != ImageTypeBitmap){
1555 FIXME("not implemented for image type %d\n", image->type);
1556 return NotImplemented;
1559 hdc = ((GpBitmap*)image)->hdc;
1561 if(!hdc){
1562 hdc = CreateCompatibleDC(0);
1563 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
1564 ((GpBitmap*)image)->hdc = hdc;
1567 return GdipCreateFromHDC(hdc, graphics);
1570 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
1572 TRACE("%p %p\n", image, height);
1574 if(!image || !height)
1575 return InvalidParameter;
1577 if(image->type == ImageTypeMetafile){
1578 HDC hdc = GetDC(0);
1580 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1581 ((GpMetafile*)image)->bounds.Height);
1583 ReleaseDC(0, hdc);
1585 else if(image->type == ImageTypeBitmap)
1586 *height = ((GpBitmap*)image)->height;
1587 else
1588 *height = ipicture_pixel_height(image->picture);
1590 TRACE("returning %d\n", *height);
1592 return Ok;
1595 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
1597 if(!image || !res)
1598 return InvalidParameter;
1600 *res = image->xres;
1602 TRACE("(%p) <-- %0.2f\n", image, *res);
1604 return Ok;
1607 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
1609 TRACE("%p %p\n", image, size);
1611 if(!image || !size)
1612 return InvalidParameter;
1614 if (image->palette_count == 0)
1615 *size = sizeof(ColorPalette);
1616 else
1617 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette_count;
1619 TRACE("<-- %u\n", *size);
1621 return Ok;
1624 /* FIXME: test this function for non-bitmap types */
1625 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
1627 TRACE("%p %p\n", image, format);
1629 if(!image || !format)
1630 return InvalidParameter;
1632 if(image->type != ImageTypeBitmap)
1633 *format = PixelFormat24bppRGB;
1634 else
1635 *format = ((GpBitmap*) image)->format;
1637 return Ok;
1640 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
1642 if(!image || !format)
1643 return InvalidParameter;
1645 memcpy(format, &image->format, sizeof(GUID));
1647 return Ok;
1650 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
1652 TRACE("%p %p\n", image, type);
1654 if(!image || !type)
1655 return InvalidParameter;
1657 *type = image->type;
1659 return Ok;
1662 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
1664 if(!image || !res)
1665 return InvalidParameter;
1667 *res = image->yres;
1669 TRACE("(%p) <-- %0.2f\n", image, *res);
1671 return Ok;
1674 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
1676 TRACE("%p %p\n", image, width);
1678 if(!image || !width)
1679 return InvalidParameter;
1681 if(image->type == ImageTypeMetafile){
1682 HDC hdc = GetDC(0);
1684 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1685 ((GpMetafile*)image)->bounds.Width);
1687 ReleaseDC(0, hdc);
1689 else if(image->type == ImageTypeBitmap)
1690 *width = ((GpBitmap*)image)->width;
1691 else
1692 *width = ipicture_pixel_width(image->picture);
1694 TRACE("returning %d\n", *width);
1696 return Ok;
1699 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
1700 MetafileHeader * header)
1702 static int calls;
1704 if(!metafile || !header)
1705 return InvalidParameter;
1707 if(!(calls++))
1708 FIXME("not implemented\n");
1710 return Ok;
1713 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
1714 UINT num, PropertyItem* items)
1716 static int calls;
1718 if(!(calls++))
1719 FIXME("not implemented\n");
1721 return InvalidParameter;
1724 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
1726 static int calls;
1728 if(!(calls++))
1729 FIXME("not implemented\n");
1731 return InvalidParameter;
1734 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
1736 static int calls;
1738 if(!(calls++))
1739 FIXME("not implemented\n");
1741 return InvalidParameter;
1744 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
1745 PropertyItem* buffer)
1747 static int calls;
1749 if(!(calls++))
1750 FIXME("not implemented\n");
1752 return InvalidParameter;
1755 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
1756 UINT* size)
1758 static int calls;
1760 TRACE("%p %x %p\n", image, pid, size);
1762 if(!size || !image)
1763 return InvalidParameter;
1765 if(!(calls++))
1766 FIXME("not implemented\n");
1768 return NotImplemented;
1771 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
1773 static int calls;
1775 TRACE("(%p,%p,%p)\n", image, size, num);
1777 if(!(calls++))
1778 FIXME("not implemented\n");
1780 return InvalidParameter;
1783 struct image_format_dimension
1785 const GUID *format;
1786 const GUID *dimension;
1789 struct image_format_dimension image_format_dimensions[] =
1791 {&ImageFormatGIF, &FrameDimensionTime},
1792 {&ImageFormatIcon, &FrameDimensionResolution},
1793 {NULL}
1796 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
1797 GDIPCONST GUID* dimensionID, UINT* count)
1799 static int calls;
1801 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
1803 if(!image || !dimensionID || !count)
1804 return InvalidParameter;
1806 if(!(calls++))
1807 FIXME("not implemented\n");
1809 return NotImplemented;
1812 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
1813 UINT* count)
1815 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
1817 if(!image || !count)
1818 return InvalidParameter;
1820 *count = 1;
1822 return Ok;
1825 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
1826 GUID* dimensionIDs, UINT count)
1828 int i;
1829 const GUID *result=NULL;
1831 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
1833 if(!image || !dimensionIDs || count != 1)
1834 return InvalidParameter;
1836 for (i=0; image_format_dimensions[i].format; i++)
1838 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
1840 result = image_format_dimensions[i].dimension;
1841 break;
1845 if (!result)
1846 result = &FrameDimensionPage;
1848 memcpy(dimensionIDs, result, sizeof(GUID));
1850 return Ok;
1853 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
1854 GDIPCONST GUID* dimensionID, UINT frameidx)
1856 static int calls;
1858 if(!image || !dimensionID)
1859 return InvalidParameter;
1861 if(!(calls++))
1862 FIXME("not implemented\n");
1864 return Ok;
1867 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
1868 GpImage **image)
1870 GpStatus stat;
1871 IStream *stream;
1873 TRACE("(%s) %p\n", debugstr_w(filename), image);
1875 if (!filename || !image)
1876 return InvalidParameter;
1878 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1880 if (stat != Ok)
1881 return stat;
1883 stat = GdipLoadImageFromStream(stream, image);
1885 IStream_Release(stream);
1887 return stat;
1890 /* FIXME: no icm handling */
1891 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
1893 TRACE("(%s) %p\n", debugstr_w(filename), image);
1895 return GdipLoadImageFromFile(filename, image);
1898 static const WICPixelFormatGUID *wic_pixel_formats[] = {
1899 &GUID_WICPixelFormat16bppBGR555,
1900 &GUID_WICPixelFormat24bppBGR,
1901 &GUID_WICPixelFormat32bppBGR,
1902 &GUID_WICPixelFormat32bppBGRA,
1903 &GUID_WICPixelFormat32bppPBGRA,
1904 NULL
1907 static const PixelFormat wic_gdip_formats[] = {
1908 PixelFormat16bppRGB555,
1909 PixelFormat24bppRGB,
1910 PixelFormat32bppRGB,
1911 PixelFormat32bppARGB,
1912 PixelFormat32bppPARGB,
1915 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
1917 GpStatus status=Ok;
1918 GpBitmap *bitmap;
1919 HRESULT hr;
1920 IWICBitmapDecoder *decoder;
1921 IWICBitmapFrameDecode *frame;
1922 IWICBitmapSource *source=NULL;
1923 WICPixelFormatGUID wic_format;
1924 PixelFormat gdip_format=0;
1925 int i;
1926 UINT width, height;
1927 BitmapData lockeddata;
1928 WICRect wrc;
1929 HRESULT initresult;
1931 initresult = CoInitialize(NULL);
1933 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
1934 &IID_IWICBitmapDecoder, (void**)&decoder);
1935 if (FAILED(hr)) goto end;
1937 hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
1938 if (SUCCEEDED(hr))
1939 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
1941 if (SUCCEEDED(hr)) /* got frame */
1943 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
1945 if (SUCCEEDED(hr))
1947 for (i=0; wic_pixel_formats[i]; i++)
1949 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
1951 source = (IWICBitmapSource*)frame;
1952 IWICBitmapSource_AddRef(source);
1953 gdip_format = wic_gdip_formats[i];
1954 break;
1957 if (!source)
1959 /* unknown format; fall back on 32bppARGB */
1960 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
1961 gdip_format = PixelFormat32bppARGB;
1965 if (SUCCEEDED(hr)) /* got source */
1967 hr = IWICBitmapSource_GetSize(source, &width, &height);
1969 if (SUCCEEDED(hr))
1970 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
1971 NULL, &bitmap);
1973 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
1975 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
1976 gdip_format, &lockeddata);
1977 if (status == Ok) /* locked bitmap */
1979 wrc.X = 0;
1980 wrc.Width = width;
1981 wrc.Height = 1;
1982 for (i=0; i<height; i++)
1984 wrc.Y = i;
1985 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
1986 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
1987 if (FAILED(hr)) break;
1990 GdipBitmapUnlockBits(bitmap, &lockeddata);
1993 if (SUCCEEDED(hr) && status == Ok)
1994 *image = (GpImage*)bitmap;
1995 else
1997 *image = NULL;
1998 GdipDisposeImage((GpImage*)bitmap);
2002 IWICBitmapSource_Release(source);
2005 IWICBitmapFrameDecode_Release(frame);
2008 IWICBitmapDecoder_Release(decoder);
2010 end:
2011 if (SUCCEEDED(initresult)) CoUninitialize();
2013 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
2015 return status;
2018 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
2020 return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
2023 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, GpImage **image)
2025 GpStatus status;
2026 GpBitmap* bitmap;
2028 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, image);
2030 bitmap = (GpBitmap*)*image;
2032 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
2034 /* WIC supports bmp files with alpha, but gdiplus does not */
2035 bitmap->format = PixelFormat32bppRGB;
2038 return status;
2041 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
2043 return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
2046 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, GpImage **image)
2048 return decode_image_wic(stream, &CLSID_WICPngDecoder, image);
2051 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
2053 return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
2056 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
2058 IPicture *pic;
2060 TRACE("%p %p\n", stream, image);
2062 if(!stream || !image)
2063 return InvalidParameter;
2065 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2066 (LPVOID*) &pic) != S_OK){
2067 TRACE("Could not load picture\n");
2068 return GenericError;
2071 /* FIXME: missing initialization code */
2072 *image = GdipAlloc(sizeof(GpMetafile));
2073 if(!*image) return OutOfMemory;
2074 (*image)->type = ImageTypeMetafile;
2075 (*image)->picture = pic;
2076 (*image)->flags = ImageFlagsNone;
2077 (*image)->palette_flags = 0;
2078 (*image)->palette_count = 0;
2079 (*image)->palette_size = 0;
2080 (*image)->palette_entries = NULL;
2082 TRACE("<-- %p\n", *image);
2084 return Ok;
2087 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
2088 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
2090 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
2092 typedef struct image_codec {
2093 ImageCodecInfo info;
2094 encode_image_func encode_func;
2095 decode_image_func decode_func;
2096 } image_codec;
2098 typedef enum {
2099 BMP,
2100 JPEG,
2101 GIF,
2102 EMF,
2103 WMF,
2104 PNG,
2105 ICO,
2106 NUM_CODECS
2107 } ImageFormat;
2109 static const struct image_codec codecs[NUM_CODECS];
2111 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
2113 BYTE signature[8];
2114 LARGE_INTEGER seek;
2115 HRESULT hr;
2116 UINT bytesread;
2117 int i, j;
2119 /* seek to the start of the stream */
2120 seek.QuadPart = 0;
2121 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2122 if (FAILED(hr)) return hresult_to_status(hr);
2124 /* read the first 8 bytes */
2125 /* FIXME: This assumes all codecs have one signature <= 8 bytes in length */
2126 hr = IStream_Read(stream, signature, 8, &bytesread);
2127 if (FAILED(hr)) return hresult_to_status(hr);
2128 if (hr == S_FALSE || bytesread == 0) return GenericError;
2130 for (i = 0; i < NUM_CODECS; i++) {
2131 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
2132 bytesread >= codecs[i].info.SigSize)
2134 for (j=0; j<codecs[i].info.SigSize; j++)
2135 if ((signature[j] & codecs[i].info.SigMask[j]) != codecs[i].info.SigPattern[j])
2136 break;
2137 if (j == codecs[i].info.SigSize)
2139 *result = &codecs[i];
2140 return Ok;
2145 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
2146 signature[0],signature[1],signature[2],signature[3],
2147 signature[4],signature[5],signature[6],signature[7]);
2149 return GenericError;
2152 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
2154 GpStatus stat;
2155 LARGE_INTEGER seek;
2156 HRESULT hr;
2157 const struct image_codec *codec=NULL;
2159 /* choose an appropriate image decoder */
2160 stat = get_decoder_info(stream, &codec);
2161 if (stat != Ok) return stat;
2163 /* seek to the start of the stream */
2164 seek.QuadPart = 0;
2165 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2166 if (FAILED(hr)) return hresult_to_status(hr);
2168 /* call on the image decoder to do the real work */
2169 stat = codec->decode_func(stream, &codec->info.Clsid, image);
2171 /* take note of the original data format */
2172 if (stat == Ok)
2174 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
2177 return stat;
2180 /* FIXME: no ICM */
2181 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
2183 TRACE("%p %p\n", stream, image);
2185 return GdipLoadImageFromStream(stream, image);
2188 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
2190 static int calls;
2192 TRACE("(%p,%u)\n", image, propId);
2194 if(!image)
2195 return InvalidParameter;
2197 if(!(calls++))
2198 FIXME("not implemented\n");
2200 return NotImplemented;
2203 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
2205 static int calls;
2207 TRACE("(%p,%p)\n", image, item);
2209 if(!(calls++))
2210 FIXME("not implemented\n");
2212 return NotImplemented;
2215 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
2216 GDIPCONST CLSID *clsidEncoder,
2217 GDIPCONST EncoderParameters *encoderParams)
2219 GpStatus stat;
2220 IStream *stream;
2222 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
2224 if (!image || !filename|| !clsidEncoder)
2225 return InvalidParameter;
2227 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
2228 if (stat != Ok)
2229 return GenericError;
2231 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
2233 IStream_Release(stream);
2234 return stat;
2237 /*************************************************************************
2238 * Encoding functions -
2239 * These functions encode an image in different image file formats.
2241 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
2242 #define BITMAP_FORMAT_JPEG 0xd8ff
2243 #define BITMAP_FORMAT_GIF 0x4947
2244 #define BITMAP_FORMAT_PNG 0x5089
2245 #define BITMAP_FORMAT_APM 0xcdd7
2247 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
2248 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2250 GpStatus stat;
2251 GpBitmap *bitmap;
2252 IWICBitmapEncoder *encoder;
2253 IWICBitmapFrameEncode *frameencode;
2254 IPropertyBag2 *encoderoptions;
2255 HRESULT hr;
2256 UINT width, height;
2257 PixelFormat gdipformat=0;
2258 WICPixelFormatGUID wicformat;
2259 GpRect rc;
2260 BitmapData lockeddata;
2261 HRESULT initresult;
2262 UINT i;
2264 if (image->type != ImageTypeBitmap)
2265 return GenericError;
2267 bitmap = (GpBitmap*)image;
2269 GdipGetImageWidth(image, &width);
2270 GdipGetImageHeight(image, &height);
2272 rc.X = 0;
2273 rc.Y = 0;
2274 rc.Width = width;
2275 rc.Height = height;
2277 initresult = CoInitialize(NULL);
2279 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2280 &IID_IWICBitmapEncoder, (void**)&encoder);
2281 if (FAILED(hr))
2283 if (SUCCEEDED(initresult)) CoUninitialize();
2284 return hresult_to_status(hr);
2287 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2289 if (SUCCEEDED(hr))
2291 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
2294 if (SUCCEEDED(hr)) /* created frame */
2296 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
2298 if (SUCCEEDED(hr))
2299 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
2301 if (SUCCEEDED(hr))
2302 /* FIXME: use the resolution from the image */
2303 hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
2305 if (SUCCEEDED(hr))
2307 for (i=0; wic_pixel_formats[i]; i++)
2309 if (wic_gdip_formats[i] == bitmap->format)
2310 break;
2312 if (wic_pixel_formats[i])
2313 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
2314 else
2315 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
2317 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
2319 for (i=0; wic_pixel_formats[i]; i++)
2321 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
2322 break;
2324 if (wic_pixel_formats[i])
2325 gdipformat = wic_gdip_formats[i];
2326 else
2328 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
2329 hr = E_FAIL;
2333 if (SUCCEEDED(hr))
2335 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
2336 &lockeddata);
2338 if (stat == Ok)
2340 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
2341 BYTE *row;
2343 /* write one row at a time in case stride is negative */
2344 row = lockeddata.Scan0;
2345 for (i=0; i<lockeddata.Height; i++)
2347 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
2348 if (FAILED(hr)) break;
2349 row += lockeddata.Stride;
2352 GdipBitmapUnlockBits(bitmap, &lockeddata);
2354 else
2355 hr = E_FAIL;
2358 if (SUCCEEDED(hr))
2359 hr = IWICBitmapFrameEncode_Commit(frameencode);
2361 IWICBitmapFrameEncode_Release(frameencode);
2362 IPropertyBag2_Release(encoderoptions);
2365 if (SUCCEEDED(hr))
2366 hr = IWICBitmapEncoder_Commit(encoder);
2368 IWICBitmapEncoder_Release(encoder);
2370 if (SUCCEEDED(initresult)) CoUninitialize();
2372 return hresult_to_status(hr);
2375 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
2376 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2378 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
2381 static GpStatus encode_image_png(GpImage *image, IStream* stream,
2382 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2384 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
2387 /*****************************************************************************
2388 * GdipSaveImageToStream [GDIPLUS.@]
2390 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
2391 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2393 GpStatus stat;
2394 encode_image_func encode_image;
2395 int i;
2397 TRACE("%p %p %p %p\n", image, stream, clsid, params);
2399 if(!image || !stream)
2400 return InvalidParameter;
2402 /* select correct encoder */
2403 encode_image = NULL;
2404 for (i = 0; i < NUM_CODECS; i++) {
2405 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
2406 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
2407 encode_image = codecs[i].encode_func;
2409 if (encode_image == NULL)
2410 return UnknownImageFormat;
2412 stat = encode_image(image, stream, clsid, params);
2414 return stat;
2417 /*****************************************************************************
2418 * GdipGetImagePalette [GDIPLUS.@]
2420 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
2422 TRACE("(%p,%p,%i)\n", image, palette, size);
2424 if (!image || !palette)
2425 return InvalidParameter;
2427 if (size < (sizeof(UINT)*2+sizeof(ARGB)*image->palette_count))
2429 TRACE("<-- InsufficientBuffer\n");
2430 return InsufficientBuffer;
2433 palette->Flags = image->palette_flags;
2434 palette->Count = image->palette_count;
2435 memcpy(palette->Entries, image->palette_entries, sizeof(ARGB)*image->palette_count);
2437 return Ok;
2440 /*****************************************************************************
2441 * GdipSetImagePalette [GDIPLUS.@]
2443 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
2444 GDIPCONST ColorPalette *palette)
2446 TRACE("(%p,%p)\n", image, palette);
2448 if(!image || !palette || palette->Count > 256)
2449 return InvalidParameter;
2451 if (palette->Count > image->palette_size)
2453 ARGB *new_palette;
2455 new_palette = GdipAlloc(sizeof(ARGB) * palette->Count);
2456 if (!new_palette) return OutOfMemory;
2458 GdipFree(image->palette_entries);
2459 image->palette_entries = new_palette;
2460 image->palette_size = palette->Count;
2463 image->palette_flags = palette->Flags;
2464 image->palette_count = palette->Count;
2465 memcpy(image->palette_entries, palette->Entries, sizeof(ARGB)*palette->Count);
2467 return Ok;
2470 /*************************************************************************
2471 * Encoders -
2472 * Structures that represent which formats we support for encoding.
2475 /* ImageCodecInfo creation routines taken from libgdiplus */
2476 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
2477 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
2478 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
2479 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
2480 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
2481 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
2483 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
2484 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
2485 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
2486 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
2487 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
2488 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
2490 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
2491 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
2492 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
2493 static const WCHAR gif_format[] = {'G','I','F',0};
2494 static const BYTE gif_sig_pattern[4] = "GIF8";
2495 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2497 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
2498 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
2499 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
2500 static const WCHAR emf_format[] = {'E','M','F',0};
2501 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
2502 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2504 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
2505 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
2506 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
2507 static const WCHAR wmf_format[] = {'W','M','F',0};
2508 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
2509 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
2511 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
2512 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
2513 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
2514 static const WCHAR png_format[] = {'P','N','G',0};
2515 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
2516 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2518 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
2519 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
2520 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
2521 static const WCHAR ico_format[] = {'I','C','O',0};
2522 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
2523 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2525 static const struct image_codec codecs[NUM_CODECS] = {
2527 { /* BMP */
2528 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2529 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2530 /* CodecName */ bmp_codecname,
2531 /* DllName */ NULL,
2532 /* FormatDescription */ bmp_format,
2533 /* FilenameExtension */ bmp_extension,
2534 /* MimeType */ bmp_mimetype,
2535 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2536 /* Version */ 1,
2537 /* SigCount */ 1,
2538 /* SigSize */ 2,
2539 /* SigPattern */ bmp_sig_pattern,
2540 /* SigMask */ bmp_sig_mask,
2542 encode_image_BMP,
2543 decode_image_bmp
2546 { /* JPEG */
2547 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2548 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2549 /* CodecName */ jpeg_codecname,
2550 /* DllName */ NULL,
2551 /* FormatDescription */ jpeg_format,
2552 /* FilenameExtension */ jpeg_extension,
2553 /* MimeType */ jpeg_mimetype,
2554 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2555 /* Version */ 1,
2556 /* SigCount */ 1,
2557 /* SigSize */ 2,
2558 /* SigPattern */ jpeg_sig_pattern,
2559 /* SigMask */ jpeg_sig_mask,
2561 NULL,
2562 decode_image_jpeg
2565 { /* GIF */
2566 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2567 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2568 /* CodecName */ gif_codecname,
2569 /* DllName */ NULL,
2570 /* FormatDescription */ gif_format,
2571 /* FilenameExtension */ gif_extension,
2572 /* MimeType */ gif_mimetype,
2573 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2574 /* Version */ 1,
2575 /* SigCount */ 1,
2576 /* SigSize */ 4,
2577 /* SigPattern */ gif_sig_pattern,
2578 /* SigMask */ gif_sig_mask,
2580 NULL,
2581 decode_image_gif
2584 { /* EMF */
2585 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2586 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2587 /* CodecName */ emf_codecname,
2588 /* DllName */ NULL,
2589 /* FormatDescription */ emf_format,
2590 /* FilenameExtension */ emf_extension,
2591 /* MimeType */ emf_mimetype,
2592 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2593 /* Version */ 1,
2594 /* SigCount */ 1,
2595 /* SigSize */ 4,
2596 /* SigPattern */ emf_sig_pattern,
2597 /* SigMask */ emf_sig_mask,
2599 NULL,
2600 decode_image_olepicture_metafile
2603 { /* WMF */
2604 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2605 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2606 /* CodecName */ wmf_codecname,
2607 /* DllName */ NULL,
2608 /* FormatDescription */ wmf_format,
2609 /* FilenameExtension */ wmf_extension,
2610 /* MimeType */ wmf_mimetype,
2611 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2612 /* Version */ 1,
2613 /* SigCount */ 1,
2614 /* SigSize */ 2,
2615 /* SigPattern */ wmf_sig_pattern,
2616 /* SigMask */ wmf_sig_mask,
2618 NULL,
2619 decode_image_olepicture_metafile
2622 { /* PNG */
2623 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2624 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2625 /* CodecName */ png_codecname,
2626 /* DllName */ NULL,
2627 /* FormatDescription */ png_format,
2628 /* FilenameExtension */ png_extension,
2629 /* MimeType */ png_mimetype,
2630 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2631 /* Version */ 1,
2632 /* SigCount */ 1,
2633 /* SigSize */ 8,
2634 /* SigPattern */ png_sig_pattern,
2635 /* SigMask */ png_sig_mask,
2637 encode_image_png,
2638 decode_image_png
2641 { /* ICO */
2642 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2643 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2644 /* CodecName */ ico_codecname,
2645 /* DllName */ NULL,
2646 /* FormatDescription */ ico_format,
2647 /* FilenameExtension */ ico_extension,
2648 /* MimeType */ ico_mimetype,
2649 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2650 /* Version */ 1,
2651 /* SigCount */ 1,
2652 /* SigSize */ 4,
2653 /* SigPattern */ ico_sig_pattern,
2654 /* SigMask */ ico_sig_mask,
2656 NULL,
2657 decode_image_icon
2661 /*****************************************************************************
2662 * GdipGetImageDecodersSize [GDIPLUS.@]
2664 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
2666 int decoder_count=0;
2667 int i;
2668 TRACE("%p %p\n", numDecoders, size);
2670 if (!numDecoders || !size)
2671 return InvalidParameter;
2673 for (i=0; i<NUM_CODECS; i++)
2675 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2676 decoder_count++;
2679 *numDecoders = decoder_count;
2680 *size = decoder_count * sizeof(ImageCodecInfo);
2682 return Ok;
2685 /*****************************************************************************
2686 * GdipGetImageDecoders [GDIPLUS.@]
2688 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
2690 int i, decoder_count=0;
2691 TRACE("%u %u %p\n", numDecoders, size, decoders);
2693 if (!decoders ||
2694 size != numDecoders * sizeof(ImageCodecInfo))
2695 return GenericError;
2697 for (i=0; i<NUM_CODECS; i++)
2699 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2701 if (decoder_count == numDecoders) return GenericError;
2702 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2703 decoder_count++;
2707 if (decoder_count < numDecoders) return GenericError;
2709 return Ok;
2712 /*****************************************************************************
2713 * GdipGetImageEncodersSize [GDIPLUS.@]
2715 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
2717 int encoder_count=0;
2718 int i;
2719 TRACE("%p %p\n", numEncoders, size);
2721 if (!numEncoders || !size)
2722 return InvalidParameter;
2724 for (i=0; i<NUM_CODECS; i++)
2726 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2727 encoder_count++;
2730 *numEncoders = encoder_count;
2731 *size = encoder_count * sizeof(ImageCodecInfo);
2733 return Ok;
2736 /*****************************************************************************
2737 * GdipGetImageEncoders [GDIPLUS.@]
2739 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
2741 int i, encoder_count=0;
2742 TRACE("%u %u %p\n", numEncoders, size, encoders);
2744 if (!encoders ||
2745 size != numEncoders * sizeof(ImageCodecInfo))
2746 return GenericError;
2748 for (i=0; i<NUM_CODECS; i++)
2750 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2752 if (encoder_count == numEncoders) return GenericError;
2753 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2754 encoder_count++;
2758 if (encoder_count < numEncoders) return GenericError;
2760 return Ok;
2763 /*****************************************************************************
2764 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
2766 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
2768 BITMAP bm;
2769 GpStatus retval;
2770 PixelFormat format;
2771 BitmapData lockeddata;
2772 INT y;
2774 TRACE("%p %p %p\n", hbm, hpal, bitmap);
2776 if(!hbm || !bitmap)
2777 return InvalidParameter;
2779 /* TODO: Support for device-dependent bitmaps */
2780 if(hpal){
2781 FIXME("no support for device-dependent bitmaps\n");
2782 return NotImplemented;
2785 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
2786 return InvalidParameter;
2788 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
2789 switch(bm.bmBitsPixel) {
2790 case 1:
2791 format = PixelFormat1bppIndexed;
2792 break;
2793 case 4:
2794 format = PixelFormat4bppIndexed;
2795 break;
2796 case 8:
2797 format = PixelFormat8bppIndexed;
2798 break;
2799 case 24:
2800 format = PixelFormat24bppRGB;
2801 break;
2802 case 32:
2803 format = PixelFormat32bppRGB;
2804 break;
2805 case 48:
2806 format = PixelFormat48bppRGB;
2807 break;
2808 default:
2809 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
2810 return InvalidParameter;
2813 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
2814 format, NULL, bitmap);
2816 if (retval == Ok)
2818 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
2819 format, &lockeddata);
2820 if (retval == Ok)
2822 if (bm.bmBits)
2824 for (y=0; y<bm.bmHeight; y++)
2826 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
2827 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
2828 bm.bmWidthBytes);
2831 else
2833 HDC hdc;
2834 HBITMAP oldhbm;
2835 BITMAPINFO *pbmi;
2836 INT src_height, dst_stride;
2837 BYTE *dst_bits;
2839 hdc = CreateCompatibleDC(NULL);
2840 oldhbm = SelectObject(hdc, hbm);
2842 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
2844 if (pbmi)
2846 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2847 pbmi->bmiHeader.biBitCount = 0;
2849 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
2851 src_height = abs(pbmi->bmiHeader.biHeight);
2853 if (pbmi->bmiHeader.biHeight > 0)
2855 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
2856 dst_stride = -lockeddata.Stride;
2858 else
2860 dst_bits = lockeddata.Scan0;
2861 dst_stride = lockeddata.Stride;
2864 for (y=0; y<src_height; y++)
2866 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
2867 pbmi, DIB_RGB_COLORS);
2870 GdipFree(pbmi);
2872 else
2873 retval = OutOfMemory;
2875 SelectObject(hdc, oldhbm);
2876 DeleteDC(hdc);
2879 GdipBitmapUnlockBits(*bitmap, &lockeddata);
2883 return retval;
2886 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
2888 FIXME("(%p): stub\n", effect);
2889 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
2890 * in Windows's gdiplus */
2891 return NotImplemented;
2894 /*****************************************************************************
2895 * GdipSetEffectParameters [GDIPLUS.@]
2897 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
2898 const VOID *params, const UINT size)
2900 static int calls;
2902 TRACE("(%p,%p,%u)\n", effect, params, size);
2904 if(!(calls++))
2905 FIXME("not implemented\n");
2907 return NotImplemented;
2910 /*****************************************************************************
2911 * GdipGetImageFlags [GDIPLUS.@]
2913 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
2915 TRACE("%p %p\n", image, flags);
2917 if(!image || !flags)
2918 return InvalidParameter;
2920 *flags = image->flags;
2922 return Ok;
2925 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
2927 TRACE("(%d, %p)\n", control, param);
2929 switch(control){
2930 case TestControlForceBilinear:
2931 if(param)
2932 FIXME("TestControlForceBilinear not handled\n");
2933 break;
2934 case TestControlNoICM:
2935 if(param)
2936 FIXME("TestControlNoICM not handled\n");
2937 break;
2938 case TestControlGetBuildNumber:
2939 *((DWORD*)param) = 3102;
2940 break;
2943 return Ok;
2946 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
2947 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
2948 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
2949 GpMetafile **metafile)
2951 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2952 frameUnit, debugstr_w(desc), metafile);
2954 return NotImplemented;
2957 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
2958 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
2959 GDIPCONST WCHAR *desc, GpMetafile **metafile)
2961 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2962 frameUnit, debugstr_w(desc), metafile);
2964 return NotImplemented;
2967 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
2969 FIXME("%p\n", image);
2971 return Ok;
2974 /*****************************************************************************
2975 * GdipGetImageThumbnail [GDIPLUS.@]
2977 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
2978 GpImage **ret_image, GetThumbnailImageAbort cb,
2979 VOID * cb_data)
2981 FIXME("(%p %u %u %p %p %p) stub\n",
2982 image, width, height, ret_image, cb, cb_data);
2983 return NotImplemented;
2986 /*****************************************************************************
2987 * GdipImageRotateFlip [GDIPLUS.@]
2989 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
2991 FIXME("(%p %u) stub\n", image, type);
2992 return NotImplemented;