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
22 #define NONAMELESSUNION
37 #include "gdiplus_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
42 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
44 static INT ipicture_pixel_height(IPicture *pic)
49 IPicture_get_Height(pic, &y);
53 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
59 static INT ipicture_pixel_width(IPicture *pic)
64 IPicture_get_Width(pic, &x);
68 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
75 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
76 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
78 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
80 * Note: According to Jose Roca's GDI+ docs, this function is not
81 * implemented in Windows's GDI+.
83 return NotImplemented;
86 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
87 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
88 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
90 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
92 * Note: According to Jose Roca's GDI+ docs, this function is not
93 * implemented in Windows's GDI+.
95 return NotImplemented;
98 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
100 *index = (row[x/8]>>(7-x%8)) & 1;
103 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
106 *index = row[x/2]&0xf;
108 *index = row[x/2]>>4;
111 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
116 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
117 const BYTE *row, UINT x)
119 *r = *g = *b = row[x*2+1];
123 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
124 const BYTE *row, UINT x)
126 WORD pixel = *((const WORD*)(row)+x);
127 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
128 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
129 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
133 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
134 const BYTE *row, UINT x)
136 WORD pixel = *((const WORD*)(row)+x);
137 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
138 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
139 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
143 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
144 const BYTE *row, UINT x)
146 WORD pixel = *((const WORD*)(row)+x);
147 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
148 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
149 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
150 if ((pixel&0x8000) == 0x8000)
156 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
157 const BYTE *row, UINT x)
165 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
166 const BYTE *row, UINT x)
174 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
175 const BYTE *row, UINT x)
183 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
184 const BYTE *row, UINT x)
191 *r = row[x*4+2] * 255 / *a;
192 *g = row[x*4+1] * 255 / *a;
193 *b = row[x*4] * 255 / *a;
197 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
198 const BYTE *row, UINT x)
206 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
207 const BYTE *row, UINT x)
215 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
216 const BYTE *row, UINT x)
223 *r = row[x*8+5] * 255 / *a;
224 *g = row[x*8+3] * 255 / *a;
225 *b = row[x*8+1] * 255 / *a;
229 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
235 TRACE("%p %d %d %p\n", bitmap, x, y, color);
237 if(!bitmap || !color ||
238 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
239 return InvalidParameter;
241 row = bitmap->bits+bitmap->stride*y;
243 switch (bitmap->format)
245 case PixelFormat1bppIndexed:
246 getpixel_1bppIndexed(&index,row,x);
248 case PixelFormat4bppIndexed:
249 getpixel_4bppIndexed(&index,row,x);
251 case PixelFormat8bppIndexed:
252 getpixel_8bppIndexed(&index,row,x);
254 case PixelFormat16bppGrayScale:
255 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
257 case PixelFormat16bppRGB555:
258 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
260 case PixelFormat16bppRGB565:
261 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
263 case PixelFormat16bppARGB1555:
264 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
266 case PixelFormat24bppRGB:
267 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
269 case PixelFormat32bppRGB:
270 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
272 case PixelFormat32bppARGB:
273 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
275 case PixelFormat32bppPARGB:
276 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
278 case PixelFormat48bppRGB:
279 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
281 case PixelFormat64bppARGB:
282 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
284 case PixelFormat64bppPARGB:
285 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
288 FIXME("not implemented for format 0x%x\n", bitmap->format);
289 return NotImplemented;
292 if (bitmap->format & PixelFormatIndexed)
293 *color = bitmap->image.palette->Entries[index];
295 *color = a<<24|r<<16|g<<8|b;
300 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
303 int best_distance = 0x7fff;
307 if (!palette) return 0;
308 /* This algorithm scans entire palette,
309 computes difference from desired color (all color components have equal weight)
310 and returns the index of color with least difference.
312 Note: Maybe it could be replaced with a better algorithm for better image quality
313 and performance, though better algorithm would probably need some pre-built lookup
314 tables and thus may actually be slower if this method is called only few times per
317 for(i=0;i<palette->Count;i++) {
318 ARGB color=palette->Entries[i];
319 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
320 if (distance<best_distance) {
321 best_distance=distance;
328 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
329 BYTE *row, UINT x, ColorPalette *palette)
331 BYTE index = get_palette_index(r,g,b,a,palette);
335 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
336 BYTE *row, UINT x, ColorPalette *palette)
338 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
341 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
342 BYTE *row, UINT x, ColorPalette *palette)
345 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
347 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
350 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
353 *((WORD*)(row)+x) = (r+g+b)*85;
356 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
359 *((WORD*)(row)+x) = (r<<7&0x7c00)|
364 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
367 *((WORD*)(row)+x) = (r<<8&0xf800)|
372 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
375 *((WORD*)(row)+x) = (a<<8&0x8000)|
381 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
389 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
392 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
395 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
398 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
401 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
407 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
410 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
413 row[x*6+5] = row[x*6+4] = r;
414 row[x*6+3] = row[x*6+2] = g;
415 row[x*6+1] = row[x*6] = b;
418 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
421 UINT64 a64=a, r64=r, g64=g, b64=b;
422 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
425 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
428 UINT64 a64, r64, g64, b64;
433 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
436 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
441 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
443 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
444 return InvalidParameter;
451 row = bitmap->bits + bitmap->stride * y;
453 switch (bitmap->format)
455 case PixelFormat16bppGrayScale:
456 setpixel_16bppGrayScale(r,g,b,a,row,x);
458 case PixelFormat16bppRGB555:
459 setpixel_16bppRGB555(r,g,b,a,row,x);
461 case PixelFormat16bppRGB565:
462 setpixel_16bppRGB565(r,g,b,a,row,x);
464 case PixelFormat16bppARGB1555:
465 setpixel_16bppARGB1555(r,g,b,a,row,x);
467 case PixelFormat24bppRGB:
468 setpixel_24bppRGB(r,g,b,a,row,x);
470 case PixelFormat32bppRGB:
471 setpixel_32bppRGB(r,g,b,a,row,x);
473 case PixelFormat32bppARGB:
474 setpixel_32bppARGB(r,g,b,a,row,x);
476 case PixelFormat32bppPARGB:
477 setpixel_32bppPARGB(r,g,b,a,row,x);
479 case PixelFormat48bppRGB:
480 setpixel_48bppRGB(r,g,b,a,row,x);
482 case PixelFormat64bppARGB:
483 setpixel_64bppARGB(r,g,b,a,row,x);
485 case PixelFormat64bppPARGB:
486 setpixel_64bppPARGB(r,g,b,a,row,x);
488 case PixelFormat8bppIndexed:
489 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
491 case PixelFormat4bppIndexed:
492 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
494 case PixelFormat1bppIndexed:
495 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
498 FIXME("not implemented for format 0x%x\n", bitmap->format);
499 return NotImplemented;
505 GpStatus convert_pixels(INT width, INT height,
506 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
507 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
508 ColorPalette *palette)
512 if (src_format == dst_format ||
513 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
515 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
516 for (y=0; y<height; y++)
517 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
521 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
522 for (x=0; x<width; x++) \
523 for (y=0; y<height; y++) { \
526 BYTE *color = (BYTE *)&argb; \
527 getpixel_function(&index, src_bits+src_stride*y, x); \
528 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
529 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
534 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
535 for (x=0; x<width; x++) \
536 for (y=0; y<height; y++) { \
538 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
539 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
546 case PixelFormat1bppIndexed:
549 case PixelFormat16bppGrayScale:
550 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
551 case PixelFormat16bppRGB555:
552 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
553 case PixelFormat16bppRGB565:
554 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
555 case PixelFormat16bppARGB1555:
556 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
557 case PixelFormat24bppRGB:
558 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
559 case PixelFormat32bppRGB:
560 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
561 case PixelFormat32bppARGB:
562 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
563 case PixelFormat32bppPARGB:
564 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
565 case PixelFormat48bppRGB:
566 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
567 case PixelFormat64bppARGB:
568 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
573 case PixelFormat4bppIndexed:
576 case PixelFormat16bppGrayScale:
577 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
578 case PixelFormat16bppRGB555:
579 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
580 case PixelFormat16bppRGB565:
581 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
582 case PixelFormat16bppARGB1555:
583 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
584 case PixelFormat24bppRGB:
585 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
586 case PixelFormat32bppRGB:
587 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
588 case PixelFormat32bppARGB:
589 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
590 case PixelFormat32bppPARGB:
591 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
592 case PixelFormat48bppRGB:
593 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
594 case PixelFormat64bppARGB:
595 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
600 case PixelFormat8bppIndexed:
603 case PixelFormat16bppGrayScale:
604 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
605 case PixelFormat16bppRGB555:
606 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
607 case PixelFormat16bppRGB565:
608 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
609 case PixelFormat16bppARGB1555:
610 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
611 case PixelFormat24bppRGB:
612 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
613 case PixelFormat32bppRGB:
614 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
615 case PixelFormat32bppARGB:
616 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
617 case PixelFormat32bppPARGB:
618 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
619 case PixelFormat48bppRGB:
620 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
621 case PixelFormat64bppARGB:
622 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
627 case PixelFormat16bppGrayScale:
630 case PixelFormat16bppRGB555:
631 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
632 case PixelFormat16bppRGB565:
633 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
634 case PixelFormat16bppARGB1555:
635 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
636 case PixelFormat24bppRGB:
637 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
638 case PixelFormat32bppRGB:
639 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
640 case PixelFormat32bppARGB:
641 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
642 case PixelFormat32bppPARGB:
643 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
644 case PixelFormat48bppRGB:
645 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
646 case PixelFormat64bppARGB:
647 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
652 case PixelFormat16bppRGB555:
655 case PixelFormat16bppGrayScale:
656 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
657 case PixelFormat16bppRGB565:
658 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
659 case PixelFormat16bppARGB1555:
660 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
661 case PixelFormat24bppRGB:
662 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
663 case PixelFormat32bppRGB:
664 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
665 case PixelFormat32bppARGB:
666 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
667 case PixelFormat32bppPARGB:
668 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
669 case PixelFormat48bppRGB:
670 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
671 case PixelFormat64bppARGB:
672 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
677 case PixelFormat16bppRGB565:
680 case PixelFormat16bppGrayScale:
681 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
682 case PixelFormat16bppRGB555:
683 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
684 case PixelFormat16bppARGB1555:
685 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
686 case PixelFormat24bppRGB:
687 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
688 case PixelFormat32bppRGB:
689 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
690 case PixelFormat32bppARGB:
691 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
692 case PixelFormat32bppPARGB:
693 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
694 case PixelFormat48bppRGB:
695 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
696 case PixelFormat64bppARGB:
697 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
702 case PixelFormat16bppARGB1555:
705 case PixelFormat16bppGrayScale:
706 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
707 case PixelFormat16bppRGB555:
708 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
709 case PixelFormat16bppRGB565:
710 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
711 case PixelFormat24bppRGB:
712 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
713 case PixelFormat32bppRGB:
714 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
715 case PixelFormat32bppARGB:
716 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
717 case PixelFormat32bppPARGB:
718 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
719 case PixelFormat48bppRGB:
720 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
721 case PixelFormat64bppARGB:
722 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
727 case PixelFormat24bppRGB:
730 case PixelFormat16bppGrayScale:
731 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
732 case PixelFormat16bppRGB555:
733 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
734 case PixelFormat16bppRGB565:
735 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
736 case PixelFormat16bppARGB1555:
737 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
738 case PixelFormat32bppRGB:
739 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
740 case PixelFormat32bppARGB:
741 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
742 case PixelFormat32bppPARGB:
743 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
744 case PixelFormat48bppRGB:
745 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
746 case PixelFormat64bppARGB:
747 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
752 case PixelFormat32bppRGB:
755 case PixelFormat16bppGrayScale:
756 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
757 case PixelFormat16bppRGB555:
758 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
759 case PixelFormat16bppRGB565:
760 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
761 case PixelFormat16bppARGB1555:
762 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
763 case PixelFormat24bppRGB:
764 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
765 case PixelFormat32bppARGB:
766 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
767 case PixelFormat32bppPARGB:
768 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
769 case PixelFormat48bppRGB:
770 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
771 case PixelFormat64bppARGB:
772 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
777 case PixelFormat32bppARGB:
780 case PixelFormat16bppGrayScale:
781 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
782 case PixelFormat16bppRGB555:
783 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
784 case PixelFormat16bppRGB565:
785 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
786 case PixelFormat16bppARGB1555:
787 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
788 case PixelFormat24bppRGB:
789 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
790 case PixelFormat32bppPARGB:
791 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
793 case PixelFormat48bppRGB:
794 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
795 case PixelFormat64bppARGB:
796 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
801 case PixelFormat32bppPARGB:
804 case PixelFormat16bppGrayScale:
805 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
806 case PixelFormat16bppRGB555:
807 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
808 case PixelFormat16bppRGB565:
809 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
810 case PixelFormat16bppARGB1555:
811 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
812 case PixelFormat24bppRGB:
813 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
814 case PixelFormat32bppRGB:
815 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
816 case PixelFormat32bppARGB:
817 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
818 case PixelFormat48bppRGB:
819 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
820 case PixelFormat64bppARGB:
821 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
826 case PixelFormat48bppRGB:
829 case PixelFormat16bppGrayScale:
830 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
831 case PixelFormat16bppRGB555:
832 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
833 case PixelFormat16bppRGB565:
834 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
835 case PixelFormat16bppARGB1555:
836 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
837 case PixelFormat24bppRGB:
838 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
839 case PixelFormat32bppRGB:
840 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
841 case PixelFormat32bppARGB:
842 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
843 case PixelFormat32bppPARGB:
844 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
845 case PixelFormat64bppARGB:
846 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
851 case PixelFormat64bppARGB:
854 case PixelFormat16bppGrayScale:
855 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
856 case PixelFormat16bppRGB555:
857 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
858 case PixelFormat16bppRGB565:
859 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
860 case PixelFormat16bppARGB1555:
861 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
862 case PixelFormat24bppRGB:
863 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
864 case PixelFormat32bppRGB:
865 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
866 case PixelFormat32bppARGB:
867 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
868 case PixelFormat32bppPARGB:
869 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
870 case PixelFormat48bppRGB:
871 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
876 case PixelFormat64bppPARGB:
879 case PixelFormat16bppGrayScale:
880 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
881 case PixelFormat16bppRGB555:
882 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
883 case PixelFormat16bppRGB565:
884 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
885 case PixelFormat16bppARGB1555:
886 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
887 case PixelFormat24bppRGB:
888 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
889 case PixelFormat32bppRGB:
890 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
891 case PixelFormat32bppARGB:
892 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
893 case PixelFormat32bppPARGB:
894 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
895 case PixelFormat48bppRGB:
896 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
897 case PixelFormat64bppARGB:
898 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
907 #undef convert_indexed_to_rgb
908 #undef convert_rgb_to_rgb
910 return NotImplemented;
913 /* This function returns a pointer to an array of pixels that represents the
914 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
915 * flags. It is correct behavior that a user who calls this function with write
916 * privileges can write to the whole bitmap (not just the area in rect).
918 * FIXME: only used portion of format is bits per pixel. */
919 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
920 UINT flags, PixelFormat format, BitmapData* lockeddata)
922 INT bitspp = PIXELFORMATBPP(format);
923 GpRect act_rect; /* actual rect to be used */
926 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
928 if(!lockeddata || !bitmap)
929 return InvalidParameter;
932 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
933 (rect->Y + rect->Height > bitmap->height) || !flags)
934 return InvalidParameter;
939 act_rect.X = act_rect.Y = 0;
940 act_rect.Width = bitmap->width;
941 act_rect.Height = bitmap->height;
946 WARN("bitmap is already locked and cannot be locked again\n");
950 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
952 /* no conversion is necessary; just use the bits directly */
953 lockeddata->Width = act_rect.Width;
954 lockeddata->Height = act_rect.Height;
955 lockeddata->PixelFormat = format;
956 lockeddata->Reserved = flags;
957 lockeddata->Stride = bitmap->stride;
958 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
959 bitmap->stride * act_rect.Y;
961 bitmap->lockmode = flags | ImageLockModeRead;
967 /* Make sure we can convert to the requested format. */
968 if (flags & ImageLockModeRead)
970 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
971 if (stat == NotImplemented)
973 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
974 return NotImplemented;
978 /* If we're opening for writing, make sure we'll be able to write back in
979 * the original format. */
980 if (flags & ImageLockModeWrite)
982 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
983 if (stat == NotImplemented)
985 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
986 return NotImplemented;
990 lockeddata->Width = act_rect.Width;
991 lockeddata->Height = act_rect.Height;
992 lockeddata->PixelFormat = format;
993 lockeddata->Reserved = flags;
995 if(!(flags & ImageLockModeUserInputBuf))
997 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
999 bitmap->bitmapbits = GdipAlloc(lockeddata->Stride * act_rect.Height);
1001 if (!bitmap->bitmapbits) return OutOfMemory;
1003 lockeddata->Scan0 = bitmap->bitmapbits;
1006 if (flags & ImageLockModeRead)
1010 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1012 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1016 stat = convert_pixels(act_rect.Width, act_rect.Height,
1017 lockeddata->Stride, lockeddata->Scan0, format,
1019 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1020 bitmap->format, bitmap->image.palette);
1024 GdipFree(bitmap->bitmapbits);
1025 bitmap->bitmapbits = NULL;
1030 bitmap->lockmode = flags | ImageLockModeRead;
1032 bitmap->lockx = act_rect.X;
1033 bitmap->locky = act_rect.Y;
1038 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1040 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1042 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1043 return InvalidParameter;
1045 bitmap->image.xres = xdpi;
1046 bitmap->image.yres = ydpi;
1051 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1052 BitmapData* lockeddata)
1057 TRACE("(%p,%p)\n", bitmap, lockeddata);
1059 if(!bitmap || !lockeddata)
1060 return InvalidParameter;
1062 if(!bitmap->lockmode)
1065 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1066 if(!(--bitmap->numlocks))
1067 bitmap->lockmode = 0;
1069 GdipFree(bitmap->bitmapbits);
1070 bitmap->bitmapbits = NULL;
1074 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1076 /* we passed a direct reference; no need to do anything */
1077 bitmap->lockmode = 0;
1078 bitmap->numlocks = 0;
1082 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1084 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1088 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1090 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1092 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1096 ERR("failed to convert pixels; this should never happen\n");
1099 GdipFree(bitmap->bitmapbits);
1100 bitmap->bitmapbits = NULL;
1101 bitmap->lockmode = 0;
1102 bitmap->numlocks = 0;
1107 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1108 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1110 BitmapData lockeddata_src, lockeddata_dst;
1116 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1118 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1120 x + width > srcBitmap->width || y + height > srcBitmap->height)
1122 TRACE("<-- InvalidParameter\n");
1123 return InvalidParameter;
1126 if (format == PixelFormatDontCare)
1127 format = srcBitmap->format;
1131 area.Width = roundr(width);
1132 area.Height = roundr(height);
1134 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
1136 if (stat != Ok) return stat;
1138 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1139 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
1142 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
1143 lockeddata_src.PixelFormat, &lockeddata_dst);
1147 /* copy the image data */
1148 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1149 for (i=0; i<lockeddata_src.Height; i++)
1150 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1151 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1154 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
1158 GdipDisposeImage((GpImage*)*dstBitmap);
1161 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
1171 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1172 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1174 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1176 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1179 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1181 GpStatus stat = GenericError;
1183 TRACE("%p, %p\n", image, cloneImage);
1185 if (!image || !cloneImage)
1186 return InvalidParameter;
1195 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1197 return GenericError;
1199 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1202 WARN("Failed to save image on stream\n");
1206 /* Set seek pointer back to the beginning of the picture */
1208 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1212 stat = GdipLoadImageFromStream(stream, cloneImage);
1213 if (stat != Ok) WARN("Failed to load image from stream\n");
1216 IStream_Release(stream);
1219 else if (image->type == ImageTypeBitmap)
1221 GpBitmap *bitmap = (GpBitmap*)image;
1222 BitmapData lockeddata_src, lockeddata_dst;
1226 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
1228 if (stat != Ok) return stat;
1230 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1231 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
1234 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
1235 lockeddata_src.PixelFormat, &lockeddata_dst);
1239 /* copy the image data */
1240 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1241 for (i=0; i<lockeddata_src.Height; i++)
1242 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1243 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1246 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
1250 GdipDisposeImage(*cloneImage);
1253 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
1259 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
1265 ERR("GpImage with no IPicture or bitmap?!\n");
1266 return NotImplemented;
1270 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1276 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1278 if(!filename || !bitmap)
1279 return InvalidParameter;
1281 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1286 stat = GdipCreateBitmapFromStream(stream, bitmap);
1288 IStream_Release(stream);
1293 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1294 VOID *bits, GpBitmap **bitmap)
1296 DWORD height, stride;
1299 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1301 if (!info || !bits || !bitmap)
1302 return InvalidParameter;
1304 height = abs(info->bmiHeader.biHeight);
1305 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1307 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1309 bits = (BYTE*)bits + (height - 1) * stride;
1313 switch(info->bmiHeader.biBitCount) {
1315 format = PixelFormat1bppIndexed;
1318 format = PixelFormat4bppIndexed;
1321 format = PixelFormat8bppIndexed;
1324 format = PixelFormat16bppRGB555;
1327 format = PixelFormat24bppRGB;
1330 format = PixelFormat32bppRGB;
1333 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1335 return InvalidParameter;
1338 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1344 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1347 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1349 return GdipCreateBitmapFromFile(filename, bitmap);
1352 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1353 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1356 GpStatus stat = InvalidParameter;
1358 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1360 if(!lpBitmapName || !bitmap)
1361 return InvalidParameter;
1364 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1365 LR_CREATEDIBSECTION);
1368 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1375 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1376 HBITMAP* hbmReturn, ARGB background)
1381 BITMAPINFOHEADER bih;
1383 BitmapData lockeddata;
1384 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1386 if (!bitmap || !hbmReturn) return InvalidParameter;
1388 GdipGetImageWidth((GpImage*)bitmap, &width);
1389 GdipGetImageHeight((GpImage*)bitmap, &height);
1391 bih.biSize = sizeof(bih);
1392 bih.biWidth = width;
1393 bih.biHeight = height;
1395 bih.biBitCount = 32;
1396 bih.biCompression = BI_RGB;
1397 bih.biSizeImage = 0;
1398 bih.biXPelsPerMeter = 0;
1399 bih.biYPelsPerMeter = 0;
1401 bih.biClrImportant = 0;
1403 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1407 lockeddata.Stride = -width * 4;
1408 lockeddata.Scan0 = bits + (width * 4 * (height - 1));
1410 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead|ImageLockModeUserInputBuf,
1411 PixelFormat32bppPARGB, &lockeddata);
1414 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1417 stat = GenericError;
1419 if (stat != Ok && result)
1421 DeleteObject(result);
1425 *hbmReturn = result;
1430 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1431 GpMetafile* metafile, BOOL* succ, EmfType emfType,
1432 const WCHAR* description, GpMetafile** out_metafile)
1436 TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1437 debugstr_w(description), out_metafile);
1439 if(!ref || !metafile || !out_metafile)
1440 return InvalidParameter;
1443 *out_metafile = NULL;
1446 FIXME("not implemented\n");
1448 return NotImplemented;
1451 /* FIXME: this should create a bitmap in the given size with the attributes
1452 * (resolution etc.) of the graphics object */
1453 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1454 GpGraphics* target, GpBitmap** bitmap)
1459 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1461 if(!target || !bitmap)
1462 return InvalidParameter;
1465 FIXME("hacked stub\n");
1467 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1473 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1481 BitmapData lockeddata;
1486 BITMAPINFOHEADER bih;
1491 TRACE("%p, %p\n", hicon, bitmap);
1493 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1494 return InvalidParameter;
1496 /* get the size of the icon */
1497 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1499 DeleteObject(iinfo.hbmColor);
1500 DeleteObject(iinfo.hbmMask);
1501 return GenericError;
1507 height = abs(bm.bmHeight);
1508 else /* combined bitmap + mask */
1509 height = abs(bm.bmHeight) / 2;
1511 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1513 DeleteObject(iinfo.hbmColor);
1514 DeleteObject(iinfo.hbmMask);
1518 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1520 DeleteObject(iinfo.hbmColor);
1521 DeleteObject(iinfo.hbmMask);
1522 HeapFree(GetProcessHeap(), 0, bits);
1529 rect.Height = height;
1531 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1533 DeleteObject(iinfo.hbmColor);
1534 DeleteObject(iinfo.hbmMask);
1535 HeapFree(GetProcessHeap(), 0, bits);
1536 GdipDisposeImage((GpImage*)*bitmap);
1540 bih.biSize = sizeof(bih);
1541 bih.biWidth = width;
1542 bih.biHeight = -height;
1544 bih.biBitCount = 32;
1545 bih.biCompression = BI_RGB;
1546 bih.biSizeImage = 0;
1547 bih.biXPelsPerMeter = 0;
1548 bih.biYPelsPerMeter = 0;
1550 bih.biClrImportant = 0;
1552 screendc = GetDC(0);
1555 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1557 if (bm.bmBitsPixel == 32)
1561 /* If any pixel has a non-zero alpha, ignore hbmMask */
1563 for (x=0; x<width && !has_alpha; x++)
1564 for (y=0; y<height && !has_alpha; y++)
1565 if ((*src++ & 0xff000000) != 0)
1568 else has_alpha = FALSE;
1572 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1576 /* copy the image data to the Bitmap */
1578 dst_row = lockeddata.Scan0;
1579 for (y=0; y<height; y++)
1581 memcpy(dst_row, src, width*4);
1583 dst_row += lockeddata.Stride;
1590 /* read alpha data from the mask */
1592 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1594 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1597 dst_row = lockeddata.Scan0;
1598 for (y=0; y<height; y++)
1600 dst = (DWORD*)dst_row;
1601 for (x=0; x<height; x++)
1603 DWORD src_value = *src++;
1607 *dst++ |= 0xff000000;
1609 dst_row += lockeddata.Stride;
1614 /* set constant alpha of 255 */
1616 for (y=0; y<height; y++)
1618 dst = (DWORD*)dst_row;
1619 for (x=0; x<height; x++)
1620 *dst++ |= 0xff000000;
1621 dst_row += lockeddata.Stride;
1626 ReleaseDC(0, screendc);
1628 DeleteObject(iinfo.hbmColor);
1629 DeleteObject(iinfo.hbmMask);
1631 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1633 HeapFree(GetProcessHeap(), 0, bits);
1638 static void generate_halftone_palette(ARGB *entries, UINT count)
1640 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1643 for (i=0; i<8 && i<count; i++)
1645 entries[i] = 0xff000000;
1646 if (i&1) entries[i] |= 0x800000;
1647 if (i&2) entries[i] |= 0x8000;
1648 if (i&4) entries[i] |= 0x80;
1652 entries[i] = 0xffc0c0c0;
1654 for (i=9; i<16 && i<count; i++)
1656 entries[i] = 0xff000000;
1657 if (i&1) entries[i] |= 0xff0000;
1658 if (i&2) entries[i] |= 0xff00;
1659 if (i&4) entries[i] |= 0xff;
1662 for (i=16; i<40 && i<count; i++)
1667 for (i=40; i<256 && i<count; i++)
1669 entries[i] = 0xff000000;
1670 entries[i] |= halftone_values[(i-40)%6];
1671 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1672 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1676 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1678 HDC screendc = GetDC(0);
1680 if (!screendc) return GenericError;
1682 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1683 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1685 ReleaseDC(0, screendc);
1690 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1691 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1694 HBITMAP hbitmap=NULL;
1695 INT row_size, dib_stride;
1696 BYTE *bits=NULL, *own_bits=NULL;
1700 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1702 if (!bitmap) return InvalidParameter;
1704 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1706 return InvalidParameter;
1709 if(scan0 && !stride)
1710 return InvalidParameter;
1712 stat = get_screen_resolution(&xres, &yres);
1713 if (stat != Ok) return stat;
1715 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1716 dib_stride = (row_size + 3) & ~3;
1719 stride = dib_stride;
1721 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1723 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1727 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1728 pbmi->bmiHeader.biWidth = width;
1729 pbmi->bmiHeader.biHeight = -height;
1730 pbmi->bmiHeader.biPlanes = 1;
1731 /* FIXME: use the rest of the data from format */
1732 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1733 pbmi->bmiHeader.biCompression = BI_RGB;
1734 pbmi->bmiHeader.biSizeImage = 0;
1735 pbmi->bmiHeader.biXPelsPerMeter = 0;
1736 pbmi->bmiHeader.biYPelsPerMeter = 0;
1737 pbmi->bmiHeader.biClrUsed = 0;
1738 pbmi->bmiHeader.biClrImportant = 0;
1740 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1744 if (!hbitmap) return GenericError;
1746 stride = dib_stride;
1750 /* Not a GDI format; don't try to make an HBITMAP. */
1755 INT size = abs(stride) * height;
1757 own_bits = bits = GdipAlloc(size);
1758 if (!own_bits) return OutOfMemory;
1761 bits += stride * (1 - height);
1765 *bitmap = GdipAlloc(sizeof(GpBitmap));
1768 DeleteObject(hbitmap);
1773 (*bitmap)->image.type = ImageTypeBitmap;
1774 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1775 (*bitmap)->image.flags = ImageFlagsNone;
1776 (*bitmap)->image.frame_count = 1;
1777 (*bitmap)->image.current_frame = 0;
1778 (*bitmap)->image.palette = NULL;
1779 (*bitmap)->image.xres = xres;
1780 (*bitmap)->image.yres = yres;
1781 (*bitmap)->width = width;
1782 (*bitmap)->height = height;
1783 (*bitmap)->format = format;
1784 (*bitmap)->image.picture = NULL;
1785 (*bitmap)->image.stream = NULL;
1786 (*bitmap)->hbitmap = hbitmap;
1787 (*bitmap)->hdc = NULL;
1788 (*bitmap)->bits = bits;
1789 (*bitmap)->stride = stride;
1790 (*bitmap)->own_bits = own_bits;
1791 (*bitmap)->metadata_reader = NULL;
1793 /* set format-related flags */
1794 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1795 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1797 if (format == PixelFormat1bppIndexed ||
1798 format == PixelFormat4bppIndexed ||
1799 format == PixelFormat8bppIndexed)
1801 (*bitmap)->image.palette = GdipAlloc(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1803 if (!(*bitmap)->image.palette)
1805 GdipDisposeImage(&(*bitmap)->image);
1810 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1812 if (format == PixelFormat1bppIndexed)
1814 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1815 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1816 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1820 if (format == PixelFormat8bppIndexed)
1821 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1823 generate_halftone_palette((*bitmap)->image.palette->Entries,
1824 (*bitmap)->image.palette->Count);
1828 TRACE("<-- %p\n", *bitmap);
1833 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1838 TRACE("%p %p\n", stream, bitmap);
1840 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1845 if((*bitmap)->image.type != ImageTypeBitmap){
1846 GdipDisposeImage(&(*bitmap)->image);
1848 return GenericError; /* FIXME: what error to return? */
1855 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1858 TRACE("%p %p\n", stream, bitmap);
1860 return GdipCreateBitmapFromStream(stream, bitmap);
1863 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1864 GpCachedBitmap **cachedbmp)
1868 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1870 if(!bitmap || !graphics || !cachedbmp)
1871 return InvalidParameter;
1873 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1877 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1879 GdipFree(*cachedbmp);
1886 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1889 BitmapData lockeddata;
1890 ULONG andstride, xorstride, bitssize;
1891 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1894 TRACE("(%p, %p)\n", bitmap, hicon);
1896 if (!bitmap || !hicon)
1897 return InvalidParameter;
1899 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1900 PixelFormat32bppPARGB, &lockeddata);
1903 andstride = ((lockeddata.Width+31)/32)*4;
1904 xorstride = lockeddata.Width*4;
1905 bitssize = (andstride + xorstride) * lockeddata.Height;
1907 andbits = GdipAlloc(bitssize);
1911 xorbits = andbits + andstride * lockeddata.Height;
1913 for (y=0; y<lockeddata.Height; y++)
1915 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
1917 androw = andbits + andstride * y;
1918 for (x=0; x<lockeddata.Width; x++)
1919 if (srcrow[3+4*x] >= 128)
1920 androw[x/8] |= 1 << (7-x%8);
1922 xorrow = xorbits + xorstride * y;
1923 memcpy(xorrow, srcrow, xorstride);
1926 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
1934 GdipBitmapUnlockBits(bitmap, &lockeddata);
1940 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1942 TRACE("%p\n", cachedbmp);
1945 return InvalidParameter;
1947 GdipDisposeImage(cachedbmp->image);
1948 GdipFree(cachedbmp);
1953 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1954 GpCachedBitmap *cachedbmp, INT x, INT y)
1956 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1958 if(!graphics || !cachedbmp)
1959 return InvalidParameter;
1961 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1964 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1965 LPBYTE pData16, INT iMapMode, INT eFlags)
1967 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1968 return NotImplemented;
1971 /* Internal utility function: Replace the image data of dst with that of src,
1973 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
1975 assert(src->image.type == ImageTypeBitmap);
1976 assert(dst->image.type == ImageTypeBitmap);
1978 GdipFree(dst->bitmapbits);
1979 GdipFree(dst->own_bits);
1981 DeleteObject(dst->hbitmap);
1983 if (clobber_palette)
1985 GdipFree(dst->image.palette);
1986 dst->image.palette = src->image.palette;
1989 GdipFree(src->image.palette);
1991 dst->image.xres = src->image.xres;
1992 dst->image.yres = src->image.yres;
1993 dst->width = src->width;
1994 dst->height = src->height;
1995 dst->format = src->format;
1996 dst->hbitmap = src->hbitmap;
1997 dst->hdc = src->hdc;
1998 dst->bits = src->bits;
1999 dst->stride = src->stride;
2000 dst->own_bits = src->own_bits;
2001 if (dst->metadata_reader)
2002 IWICMetadataReader_Release(dst->metadata_reader);
2003 dst->metadata_reader = src->metadata_reader;
2004 if (dst->image.stream)
2005 IStream_Release(dst->image.stream);
2006 dst->image.stream = src->image.stream;
2007 dst->image.frame_count = src->image.frame_count;
2008 dst->image.current_frame = src->image.current_frame;
2009 dst->image.format = src->image.format;
2011 src->image.type = ~0;
2015 static GpStatus free_image_data(GpImage *image)
2018 return InvalidParameter;
2020 if (image->type == ImageTypeBitmap)
2022 GdipFree(((GpBitmap*)image)->bitmapbits);
2023 GdipFree(((GpBitmap*)image)->own_bits);
2024 DeleteDC(((GpBitmap*)image)->hdc);
2025 DeleteObject(((GpBitmap*)image)->hbitmap);
2026 if (((GpBitmap*)image)->metadata_reader)
2027 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2029 else if (image->type == ImageTypeMetafile)
2031 GpMetafile *metafile = (GpMetafile*)image;
2032 GdipFree(metafile->comment_data);
2033 DeleteEnhMetaFile(CloseEnhMetaFile(metafile->record_dc));
2034 DeleteEnhMetaFile(metafile->hemf);
2035 if (metafile->record_graphics)
2037 WARN("metafile closed while recording\n");
2038 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2039 metafile->record_graphics->image = NULL;
2040 metafile->record_graphics->busy = TRUE;
2045 WARN("invalid image: %p\n", image);
2049 IPicture_Release(image->picture);
2051 IStream_Release(image->stream);
2052 GdipFree(image->palette);
2057 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2061 TRACE("%p\n", image);
2063 status = free_image_data(image);
2064 if (status != Ok) return status;
2071 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2075 TRACE("(%p,%p)\n", image, item);
2078 return InvalidParameter;
2081 FIXME("not implemented\n");
2083 return NotImplemented;
2086 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2090 TRACE("(%p,%p)\n", image, item);
2093 FIXME("not implemented\n");
2095 return NotImplemented;
2098 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2101 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2103 if(!image || !srcRect || !srcUnit)
2104 return InvalidParameter;
2105 if(image->type == ImageTypeMetafile){
2106 *srcRect = ((GpMetafile*)image)->bounds;
2107 *srcUnit = ((GpMetafile*)image)->unit;
2109 else if(image->type == ImageTypeBitmap){
2110 srcRect->X = srcRect->Y = 0.0;
2111 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2112 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2113 *srcUnit = UnitPixel;
2116 srcRect->X = srcRect->Y = 0.0;
2117 srcRect->Width = ipicture_pixel_width(image->picture);
2118 srcRect->Height = ipicture_pixel_height(image->picture);
2119 *srcUnit = UnitPixel;
2122 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2123 srcRect->Width, srcRect->Height, *srcUnit);
2128 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2131 TRACE("%p %p %p\n", image, width, height);
2133 if(!image || !height || !width)
2134 return InvalidParameter;
2136 if(image->type == ImageTypeMetafile){
2138 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2142 *height = convert_unit(res, ((GpMetafile*)image)->unit) *
2143 ((GpMetafile*)image)->bounds.Height;
2145 *width = convert_unit(res, ((GpMetafile*)image)->unit) *
2146 ((GpMetafile*)image)->bounds.Width;
2149 else if(image->type == ImageTypeBitmap){
2150 *height = ((GpBitmap*)image)->height;
2151 *width = ((GpBitmap*)image)->width;
2154 *height = ipicture_pixel_height(image->picture);
2155 *width = ipicture_pixel_width(image->picture);
2158 TRACE("returning (%f, %f)\n", *height, *width);
2162 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2163 GpGraphics **graphics)
2168 TRACE("%p %p\n", image, graphics);
2170 if(!image || !graphics)
2171 return InvalidParameter;
2173 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2175 hdc = ((GpBitmap*)image)->hdc;
2178 hdc = CreateCompatibleDC(0);
2179 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2180 ((GpBitmap*)image)->hdc = hdc;
2183 stat = GdipCreateFromHDC(hdc, graphics);
2186 (*graphics)->image = image;
2188 else if (image->type == ImageTypeMetafile)
2189 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2191 stat = graphics_from_image(image, graphics);
2196 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2198 TRACE("%p %p\n", image, height);
2200 if(!image || !height)
2201 return InvalidParameter;
2203 if(image->type == ImageTypeMetafile){
2205 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2209 *height = roundr(convert_unit(res, ((GpMetafile*)image)->unit) *
2210 ((GpMetafile*)image)->bounds.Height);
2212 else if(image->type == ImageTypeBitmap)
2213 *height = ((GpBitmap*)image)->height;
2215 *height = ipicture_pixel_height(image->picture);
2217 TRACE("returning %d\n", *height);
2222 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2225 return InvalidParameter;
2229 TRACE("(%p) <-- %0.2f\n", image, *res);
2234 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2236 TRACE("%p %p\n", image, size);
2239 return InvalidParameter;
2241 if (!image->palette || image->palette->Count == 0)
2242 *size = sizeof(ColorPalette);
2244 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2246 TRACE("<-- %u\n", *size);
2251 /* FIXME: test this function for non-bitmap types */
2252 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2254 TRACE("%p %p\n", image, format);
2256 if(!image || !format)
2257 return InvalidParameter;
2259 if(image->type != ImageTypeBitmap)
2260 *format = PixelFormat24bppRGB;
2262 *format = ((GpBitmap*) image)->format;
2267 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2269 TRACE("(%p, %p)\n", image, format);
2271 if(!image || !format)
2272 return InvalidParameter;
2274 memcpy(format, &image->format, sizeof(GUID));
2279 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2281 TRACE("%p %p\n", image, type);
2284 return InvalidParameter;
2286 *type = image->type;
2291 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2294 return InvalidParameter;
2298 TRACE("(%p) <-- %0.2f\n", image, *res);
2303 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2305 TRACE("%p %p\n", image, width);
2307 if(!image || !width)
2308 return InvalidParameter;
2310 if(image->type == ImageTypeMetafile){
2312 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2316 *width = roundr(convert_unit(res, ((GpMetafile*)image)->unit) *
2317 ((GpMetafile*)image)->bounds.Width);
2319 else if(image->type == ImageTypeBitmap)
2320 *width = ((GpBitmap*)image)->width;
2322 *width = ipicture_pixel_width(image->picture);
2324 TRACE("returning %d\n", *width);
2329 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2330 MetafileHeader * header)
2334 TRACE("(%p, %p)\n", metafile, header);
2336 if(!metafile || !header)
2337 return InvalidParameter;
2340 FIXME("not implemented\n");
2342 memset(header, 0, sizeof(MetafileHeader));
2347 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromEmf(HENHMETAFILE hEmf,
2348 MetafileHeader *header)
2352 if(!hEmf || !header)
2353 return InvalidParameter;
2356 FIXME("not implemented\n");
2358 memset(header, 0, sizeof(MetafileHeader));
2363 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromFile(GDIPCONST WCHAR *filename,
2364 MetafileHeader *header)
2368 TRACE("(%s,%p)\n", debugstr_w(filename), header);
2370 if(!filename || !header)
2371 return InvalidParameter;
2374 FIXME("not implemented\n");
2376 memset(header, 0, sizeof(MetafileHeader));
2381 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromStream(IStream *stream,
2382 MetafileHeader *header)
2386 TRACE("(%p,%p)\n", stream, header);
2388 if(!stream || !header)
2389 return InvalidParameter;
2392 FIXME("not implemented\n");
2394 memset(header, 0, sizeof(MetafileHeader));
2399 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2401 TRACE("(%p, %p)\n", image, num);
2403 if (!image || !num) return InvalidParameter;
2407 if (image->type == ImageTypeBitmap)
2409 if (((GpBitmap *)image)->metadata_reader)
2410 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2416 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2419 IWICMetadataReader *reader;
2420 IWICEnumMetadataItem *enumerator;
2421 UINT prop_count, i, items_returned;
2423 TRACE("(%p, %u, %p)\n", image, num, list);
2425 if (!image || !list) return InvalidParameter;
2427 if (image->type != ImageTypeBitmap)
2429 FIXME("Not implemented for type %d\n", image->type);
2430 return NotImplemented;
2433 reader = ((GpBitmap *)image)->metadata_reader;
2436 if (num != 0) return InvalidParameter;
2440 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2441 if (FAILED(hr)) return hresult_to_status(hr);
2443 if (num != prop_count) return InvalidParameter;
2445 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2446 if (FAILED(hr)) return hresult_to_status(hr);
2448 IWICEnumMetadataItem_Reset(enumerator);
2450 for (i = 0; i < num; i++)
2454 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2455 if (hr != S_OK) break;
2457 if (id.vt != VT_UI2)
2459 FIXME("not supported propvariant type for id: %u\n", id.vt);
2463 list[i] = id.u.uiVal;
2466 IWICEnumMetadataItem_Release(enumerator);
2468 return hr == S_OK ? Ok : hresult_to_status(hr);
2471 static UINT propvariant_size(PROPVARIANT *value)
2473 switch (value->vt & ~VT_VECTOR)
2479 if (!(value->vt & VT_VECTOR)) return 1;
2480 return value->u.caub.cElems;
2483 if (!(value->vt & VT_VECTOR)) return 2;
2484 return value->u.caui.cElems * 2;
2488 if (!(value->vt & VT_VECTOR)) return 4;
2489 return value->u.caul.cElems * 4;
2493 if (!(value->vt & VT_VECTOR)) return 8;
2494 return value->u.cauh.cElems * 8;
2496 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2498 return value->u.blob.cbSize;
2500 FIXME("not supported variant type %d\n", value->vt);
2505 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2508 IWICMetadataReader *reader;
2509 PROPVARIANT id, value;
2511 TRACE("(%p,%#x,%p)\n", image, propid, size);
2513 if (!size || !image) return InvalidParameter;
2515 if (image->type != ImageTypeBitmap)
2517 FIXME("Not implemented for type %d\n", image->type);
2518 return NotImplemented;
2521 reader = ((GpBitmap *)image)->metadata_reader;
2522 if (!reader) return PropertyNotFound;
2525 id.u.uiVal = propid;
2526 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2527 if (FAILED(hr)) return PropertyNotFound;
2529 *size = propvariant_size(&value);
2530 if (*size) *size += sizeof(PropertyItem);
2531 PropVariantClear(&value);
2536 #ifndef PropertyTagTypeSByte
2537 #define PropertyTagTypeSByte 6
2538 #define PropertyTagTypeSShort 8
2539 #define PropertyTagTypeFloat 11
2540 #define PropertyTagTypeDouble 12
2543 static UINT vt_to_itemtype(UINT vt)
2550 { VT_I1, PropertyTagTypeSByte },
2551 { VT_UI1, PropertyTagTypeByte },
2552 { VT_I2, PropertyTagTypeSShort },
2553 { VT_UI2, PropertyTagTypeShort },
2554 { VT_I4, PropertyTagTypeSLONG },
2555 { VT_UI4, PropertyTagTypeLong },
2556 { VT_I8, PropertyTagTypeSRational },
2557 { VT_UI8, PropertyTagTypeRational },
2558 { VT_R4, PropertyTagTypeFloat },
2559 { VT_R8, PropertyTagTypeDouble },
2560 { VT_LPSTR, PropertyTagTypeASCII },
2561 { VT_BLOB, PropertyTagTypeUndefined }
2564 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2566 if (vt2type[i].vt == vt) return vt2type[i].type;
2568 FIXME("not supported variant type %u\n", vt);
2572 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2573 UINT size, PROPID id)
2575 UINT item_size, item_type;
2577 item_size = propvariant_size(value);
2578 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2580 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2581 if (!item_type) return InvalidParameter;
2583 item->value = item + 1;
2585 switch (value->vt & ~VT_VECTOR)
2589 if (!(value->vt & VT_VECTOR))
2590 *(BYTE *)item->value = value->u.bVal;
2592 memcpy(item->value, value->u.caub.pElems, item_size);
2596 if (!(value->vt & VT_VECTOR))
2597 *(USHORT *)item->value = value->u.uiVal;
2599 memcpy(item->value, value->u.caui.pElems, item_size);
2604 if (!(value->vt & VT_VECTOR))
2605 *(ULONG *)item->value = value->u.ulVal;
2607 memcpy(item->value, value->u.caul.pElems, item_size);
2612 if (!(value->vt & VT_VECTOR))
2613 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2615 memcpy(item->value, value->u.cauh.pElems, item_size);
2618 memcpy(item->value, value->u.pszVal, item_size);
2621 memcpy(item->value, value->u.blob.pBlobData, item_size);
2624 FIXME("not supported variant type %d\n", value->vt);
2625 return InvalidParameter;
2628 item->length = item_size;
2629 item->type = item_type;
2635 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2636 PropertyItem *buffer)
2640 IWICMetadataReader *reader;
2641 PROPVARIANT id, value;
2643 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2645 if (!image || !buffer) return InvalidParameter;
2647 if (image->type != ImageTypeBitmap)
2649 FIXME("Not implemented for type %d\n", image->type);
2650 return NotImplemented;
2653 reader = ((GpBitmap *)image)->metadata_reader;
2654 if (!reader) return PropertyNotFound;
2657 id.u.uiVal = propid;
2658 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2659 if (FAILED(hr)) return PropertyNotFound;
2661 stat = propvariant_to_item(&value, buffer, size, propid);
2662 PropVariantClear(&value);
2667 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2670 IWICMetadataReader *reader;
2671 IWICEnumMetadataItem *enumerator;
2672 UINT prop_count, prop_size, i;
2673 PROPVARIANT id, value;
2675 TRACE("(%p,%p,%p)\n", image, size, count);
2677 if (!image || !size || !count) return InvalidParameter;
2679 if (image->type != ImageTypeBitmap)
2681 FIXME("Not implemented for type %d\n", image->type);
2682 return NotImplemented;
2685 reader = ((GpBitmap *)image)->metadata_reader;
2686 if (!reader) return PropertyNotFound;
2688 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2689 if (FAILED(hr)) return hresult_to_status(hr);
2691 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2692 if (FAILED(hr)) return hresult_to_status(hr);
2694 IWICEnumMetadataItem_Reset(enumerator);
2698 PropVariantInit(&id);
2699 PropVariantInit(&value);
2701 for (i = 0; i < prop_count; i++)
2703 UINT items_returned, item_size;
2705 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2706 if (hr != S_OK) break;
2708 item_size = propvariant_size(&value);
2709 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2711 PropVariantClear(&id);
2712 PropVariantClear(&value);
2715 IWICEnumMetadataItem_Release(enumerator);
2717 if (hr != S_OK) return PropertyNotFound;
2719 *count = prop_count;
2724 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2725 UINT count, PropertyItem *buf)
2729 IWICMetadataReader *reader;
2730 IWICEnumMetadataItem *enumerator;
2731 UINT prop_count, prop_size, i;
2732 PROPVARIANT id, value;
2735 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2737 if (!image || !buf) return InvalidParameter;
2739 if (image->type != ImageTypeBitmap)
2741 FIXME("Not implemented for type %d\n", image->type);
2742 return NotImplemented;
2745 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2746 if (status != Ok) return status;
2748 if (prop_count != count || prop_size != size) return InvalidParameter;
2750 reader = ((GpBitmap *)image)->metadata_reader;
2751 if (!reader) return PropertyNotFound;
2753 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2754 if (FAILED(hr)) return hresult_to_status(hr);
2756 IWICEnumMetadataItem_Reset(enumerator);
2758 item_value = (char *)(buf + prop_count);
2760 PropVariantInit(&id);
2761 PropVariantInit(&value);
2763 for (i = 0; i < prop_count; i++)
2766 UINT items_returned, item_size;
2768 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2769 if (hr != S_OK) break;
2771 if (id.vt != VT_UI2)
2773 FIXME("not supported propvariant type for id: %u\n", id.vt);
2777 item_size = propvariant_size(&value);
2780 item = HeapAlloc(GetProcessHeap(), 0, item_size + sizeof(*item));
2782 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2783 buf[i].id = item->id;
2784 buf[i].type = item->type;
2785 buf[i].length = item_size;
2786 buf[i].value = item_value;
2787 memcpy(item_value, item->value, item_size);
2788 item_value += item_size;
2790 HeapFree(GetProcessHeap(), 0, item);
2793 PropVariantClear(&id);
2794 PropVariantClear(&value);
2797 IWICEnumMetadataItem_Release(enumerator);
2799 if (hr != S_OK) return PropertyNotFound;
2804 struct image_format_dimension
2807 const GUID *dimension;
2810 static const struct image_format_dimension image_format_dimensions[] =
2812 {&ImageFormatGIF, &FrameDimensionTime},
2813 {&ImageFormatIcon, &FrameDimensionResolution},
2817 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2818 GDIPCONST GUID* dimensionID, UINT* count)
2820 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2822 if(!image || !count)
2823 return InvalidParameter;
2826 IsEqualGUID(dimensionID, &image->format) ||
2827 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2828 IsEqualGUID(dimensionID, &FrameDimensionTime))
2830 *count = image->frame_count;
2834 return InvalidParameter;
2837 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2840 TRACE("(%p, %p)\n", image, count);
2842 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2844 if(!image || !count)
2845 return InvalidParameter;
2852 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2853 GUID* dimensionIDs, UINT count)
2856 const GUID *result=NULL;
2858 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2860 if(!image || !dimensionIDs || count != 1)
2861 return InvalidParameter;
2863 for (i=0; image_format_dimensions[i].format; i++)
2865 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2867 result = image_format_dimensions[i].dimension;
2873 result = &FrameDimensionPage;
2875 memcpy(dimensionIDs, result, sizeof(GUID));
2880 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2886 TRACE("(%s) %p\n", debugstr_w(filename), image);
2888 if (!filename || !image)
2889 return InvalidParameter;
2891 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2896 stat = GdipLoadImageFromStream(stream, image);
2898 IStream_Release(stream);
2903 /* FIXME: no icm handling */
2904 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2906 TRACE("(%s) %p\n", debugstr_w(filename), image);
2908 return GdipLoadImageFromFile(filename, image);
2911 static const WICPixelFormatGUID * const wic_pixel_formats[] = {
2912 &GUID_WICPixelFormatBlackWhite,
2913 &GUID_WICPixelFormat1bppIndexed,
2914 &GUID_WICPixelFormat16bppBGR555,
2915 &GUID_WICPixelFormat24bppBGR,
2916 &GUID_WICPixelFormat32bppBGR,
2917 &GUID_WICPixelFormat32bppBGRA,
2918 &GUID_WICPixelFormat32bppPBGRA,
2922 static const PixelFormat wic_gdip_formats[] = {
2923 PixelFormat1bppIndexed,
2924 PixelFormat1bppIndexed,
2925 PixelFormat16bppRGB555,
2926 PixelFormat24bppRGB,
2927 PixelFormat32bppRGB,
2928 PixelFormat32bppARGB,
2929 PixelFormat32bppPARGB,
2932 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
2937 IWICBitmapDecoder *decoder;
2938 IWICBitmapFrameDecode *frame;
2939 IWICBitmapSource *source=NULL;
2940 IWICMetadataBlockReader *block_reader;
2941 WICPixelFormatGUID wic_format;
2942 PixelFormat gdip_format=0;
2944 UINT width, height, frame_count;
2945 BitmapData lockeddata;
2949 initresult = CoInitialize(NULL);
2951 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2952 &IID_IWICBitmapDecoder, (void**)&decoder);
2953 if (FAILED(hr)) goto end;
2955 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
2958 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
2959 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
2962 if (SUCCEEDED(hr)) /* got frame */
2964 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
2968 IWICBitmapSource *bmp_source;
2969 IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICBitmapSource, (void **)&bmp_source);
2971 for (i=0; wic_pixel_formats[i]; i++)
2973 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
2975 source = bmp_source;
2976 gdip_format = wic_gdip_formats[i];
2982 /* unknown format; fall back on 32bppARGB */
2983 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, bmp_source, &source);
2984 gdip_format = PixelFormat32bppARGB;
2985 IWICBitmapSource_Release(bmp_source);
2987 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
2990 if (SUCCEEDED(hr)) /* got source */
2992 hr = IWICBitmapSource_GetSize(source, &width, &height);
2995 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
2998 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3000 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3001 gdip_format, &lockeddata);
3002 if (status == Ok) /* locked bitmap */
3007 for (i=0; i<height; i++)
3010 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3011 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3012 if (FAILED(hr)) break;
3015 GdipBitmapUnlockBits(bitmap, &lockeddata);
3018 if (SUCCEEDED(hr) && status == Ok)
3019 *image = (GpImage*)bitmap;
3023 GdipDisposeImage((GpImage*)bitmap);
3026 if (SUCCEEDED(hr) && status == Ok)
3029 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3032 bitmap->image.xres = dpix;
3033 bitmap->image.yres = dpiy;
3039 IWICBitmapSource_Release(source);
3042 bitmap->metadata_reader = NULL;
3043 if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3045 UINT block_count = 0;
3046 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3047 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3048 IWICMetadataBlockReader_Release(block_reader);
3051 IWICBitmapFrameDecode_Release(frame);
3054 IWICBitmapDecoder_Release(decoder);
3057 if (SUCCEEDED(initresult)) CoUninitialize();
3059 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3063 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3064 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3065 bitmap->image.frame_count = frame_count;
3066 bitmap->image.current_frame = active_frame;
3067 bitmap->image.stream = stream;
3068 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3069 bitmap->image.palette->Flags = 0;
3070 /* Pin the source stream */
3071 IStream_AddRef(stream);
3077 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3079 return decode_image_wic(stream, &CLSID_WICIcoDecoder, active_frame, image);
3082 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3087 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, active_frame, image);
3089 bitmap = (GpBitmap*)*image;
3091 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3093 /* WIC supports bmp files with alpha, but gdiplus does not */
3094 bitmap->format = PixelFormat32bppRGB;
3100 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3102 return decode_image_wic(stream, &CLSID_WICJpegDecoder, active_frame, image);
3105 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3107 return decode_image_wic(stream, &CLSID_WICPngDecoder, active_frame, image);
3110 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3112 return decode_image_wic(stream, &CLSID_WICGifDecoder, active_frame, image);
3115 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3117 return decode_image_wic(stream, &CLSID_WICTiffDecoder, active_frame, image);
3120 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3124 TRACE("%p %p\n", stream, image);
3126 if(!stream || !image)
3127 return InvalidParameter;
3129 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3130 (LPVOID*) &pic) != S_OK){
3131 TRACE("Could not load picture\n");
3132 return GenericError;
3135 /* FIXME: missing initialization code */
3136 *image = GdipAlloc(sizeof(GpMetafile));
3137 if(!*image) return OutOfMemory;
3138 (*image)->type = ImageTypeMetafile;
3139 (*image)->stream = NULL;
3140 (*image)->picture = pic;
3141 (*image)->flags = ImageFlagsNone;
3142 (*image)->frame_count = 1;
3143 (*image)->current_frame = 0;
3144 (*image)->palette = NULL;
3146 TRACE("<-- %p\n", *image);
3151 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
3152 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
3154 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, UINT active_frame, GpImage **image);
3156 typedef struct image_codec {
3157 ImageCodecInfo info;
3158 encode_image_func encode_func;
3159 decode_image_func decode_func;
3174 static const struct image_codec codecs[NUM_CODECS];
3176 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
3179 const BYTE *pattern, *mask;
3185 /* seek to the start of the stream */
3187 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3188 if (FAILED(hr)) return hresult_to_status(hr);
3190 /* read the first 8 bytes */
3191 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
3192 hr = IStream_Read(stream, signature, 8, &bytesread);
3193 if (FAILED(hr)) return hresult_to_status(hr);
3194 if (hr == S_FALSE || bytesread == 0) return GenericError;
3196 for (i = 0; i < NUM_CODECS; i++) {
3197 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
3198 bytesread >= codecs[i].info.SigSize)
3200 for (sig=0; sig<codecs[i].info.SigCount; sig++)
3202 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
3203 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
3204 for (j=0; j<codecs[i].info.SigSize; j++)
3205 if ((signature[j] & mask[j]) != pattern[j])
3207 if (j == codecs[i].info.SigSize)
3209 *result = &codecs[i];
3216 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
3217 signature[0],signature[1],signature[2],signature[3],
3218 signature[4],signature[5],signature[6],signature[7]);
3220 return GenericError;
3223 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
3229 const struct image_codec *codec = NULL;
3233 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
3235 if (!image || !dimensionID)
3236 return InvalidParameter;
3238 if (frame >= image->frame_count)
3239 return InvalidParameter;
3241 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
3243 WARN("invalid image type %d\n", image->type);
3244 return InvalidParameter;
3247 if (image->current_frame == frame)
3252 TRACE("image doesn't have an associated stream\n");
3256 hr = IStream_Clone(image->stream, &stream);
3261 hr = IStream_Stat(image->stream, &statstg, STATFLAG_NOOPEN);
3262 if (FAILED(hr)) return hresult_to_status(hr);
3264 stat = GdipCreateStreamOnFile(statstg.pwcsName, GENERIC_READ, &stream);
3265 if (stat != Ok) return stat;
3268 /* choose an appropriate image decoder */
3269 stat = get_decoder_info(stream, &codec);
3272 IStream_Release(stream);
3276 /* seek to the start of the stream */
3278 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3281 IStream_Release(stream);
3282 return hresult_to_status(hr);
3285 /* call on the image decoder to do the real work */
3286 stat = codec->decode_func(stream, &codec->info.Clsid, frame, &new_image);
3290 memcpy(&new_image->format, &codec->info.FormatID, sizeof(GUID));
3291 free_image_data(image);
3292 if (image->type == ImageTypeBitmap)
3293 *(GpBitmap *)image = *(GpBitmap *)new_image;
3294 else if (image->type == ImageTypeMetafile)
3295 *(GpMetafile *)image = *(GpMetafile *)new_image;
3296 new_image->type = ~0;
3297 GdipFree(new_image);
3301 IStream_Release(stream);
3305 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
3310 const struct image_codec *codec=NULL;
3312 /* choose an appropriate image decoder */
3313 stat = get_decoder_info(stream, &codec);
3314 if (stat != Ok) return stat;
3316 /* seek to the start of the stream */
3318 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3319 if (FAILED(hr)) return hresult_to_status(hr);
3321 /* call on the image decoder to do the real work */
3322 stat = codec->decode_func(stream, &codec->info.Clsid, 0, image);
3324 /* take note of the original data format */
3327 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
3335 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
3337 TRACE("%p %p\n", stream, image);
3339 return GdipLoadImageFromStream(stream, image);
3342 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
3346 TRACE("(%p,%u)\n", image, propId);
3349 return InvalidParameter;
3352 FIXME("not implemented\n");
3354 return NotImplemented;
3357 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
3361 if (!image || !item) return InvalidParameter;
3363 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
3366 FIXME("not implemented\n");
3368 return NotImplemented;
3371 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
3372 GDIPCONST CLSID *clsidEncoder,
3373 GDIPCONST EncoderParameters *encoderParams)
3378 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
3380 if (!image || !filename|| !clsidEncoder)
3381 return InvalidParameter;
3383 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
3385 return GenericError;
3387 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
3389 IStream_Release(stream);
3393 /*************************************************************************
3394 * Encoding functions -
3395 * These functions encode an image in different image file formats.
3397 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
3398 #define BITMAP_FORMAT_JPEG 0xd8ff
3399 #define BITMAP_FORMAT_GIF 0x4947
3400 #define BITMAP_FORMAT_PNG 0x5089
3401 #define BITMAP_FORMAT_APM 0xcdd7
3403 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
3404 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3408 IWICBitmapEncoder *encoder;
3409 IWICBitmapFrameEncode *frameencode;
3410 IPropertyBag2 *encoderoptions;
3413 PixelFormat gdipformat=0;
3414 WICPixelFormatGUID wicformat;
3416 BitmapData lockeddata;
3420 if (image->type != ImageTypeBitmap)
3421 return GenericError;
3423 bitmap = (GpBitmap*)image;
3425 GdipGetImageWidth(image, &width);
3426 GdipGetImageHeight(image, &height);
3433 initresult = CoInitialize(NULL);
3435 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3436 &IID_IWICBitmapEncoder, (void**)&encoder);
3439 if (SUCCEEDED(initresult)) CoUninitialize();
3440 return hresult_to_status(hr);
3443 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
3447 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
3450 if (SUCCEEDED(hr)) /* created frame */
3452 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
3455 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
3458 /* FIXME: use the resolution from the image */
3459 hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
3463 for (i=0; wic_pixel_formats[i]; i++)
3465 if (wic_gdip_formats[i] == bitmap->format)
3468 if (wic_pixel_formats[i])
3469 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
3471 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
3473 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
3475 for (i=0; wic_pixel_formats[i]; i++)
3477 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
3480 if (wic_pixel_formats[i])
3481 gdipformat = wic_gdip_formats[i];
3484 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
3491 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
3496 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
3499 /* write one row at a time in case stride is negative */
3500 row = lockeddata.Scan0;
3501 for (i=0; i<lockeddata.Height; i++)
3503 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
3504 if (FAILED(hr)) break;
3505 row += lockeddata.Stride;
3508 GdipBitmapUnlockBits(bitmap, &lockeddata);
3515 hr = IWICBitmapFrameEncode_Commit(frameencode);
3517 IWICBitmapFrameEncode_Release(frameencode);
3518 IPropertyBag2_Release(encoderoptions);
3522 hr = IWICBitmapEncoder_Commit(encoder);
3524 IWICBitmapEncoder_Release(encoder);
3526 if (SUCCEEDED(initresult)) CoUninitialize();
3528 return hresult_to_status(hr);
3531 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
3532 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3534 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
3537 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
3538 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3540 return encode_image_WIC(image, stream, &CLSID_WICTiffEncoder, params);
3543 static GpStatus encode_image_png(GpImage *image, IStream* stream,
3544 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3546 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
3549 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
3550 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3552 return encode_image_WIC(image, stream, &CLSID_WICJpegEncoder, params);
3555 /*****************************************************************************
3556 * GdipSaveImageToStream [GDIPLUS.@]
3558 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
3559 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3562 encode_image_func encode_image;
3565 TRACE("%p %p %p %p\n", image, stream, clsid, params);
3567 if(!image || !stream)
3568 return InvalidParameter;
3570 /* select correct encoder */
3571 encode_image = NULL;
3572 for (i = 0; i < NUM_CODECS; i++) {
3573 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
3574 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
3575 encode_image = codecs[i].encode_func;
3577 if (encode_image == NULL)
3578 return UnknownImageFormat;
3580 stat = encode_image(image, stream, clsid, params);
3585 /*****************************************************************************
3586 * GdipSaveAdd [GDIPLUS.@]
3588 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
3590 FIXME("(%p,%p): stub\n", image, params);
3594 /*****************************************************************************
3595 * GdipGetImagePalette [GDIPLUS.@]
3597 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
3601 TRACE("(%p,%p,%i)\n", image, palette, size);
3603 if (!image || !palette)
3604 return InvalidParameter;
3606 count = image->palette ? image->palette->Count : 0;
3608 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
3610 TRACE("<-- InsufficientBuffer\n");
3611 return InsufficientBuffer;
3616 palette->Flags = image->palette->Flags;
3617 palette->Count = image->palette->Count;
3618 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
3628 /*****************************************************************************
3629 * GdipSetImagePalette [GDIPLUS.@]
3631 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
3632 GDIPCONST ColorPalette *palette)
3634 ColorPalette *new_palette;
3636 TRACE("(%p,%p)\n", image, palette);
3638 if(!image || !palette || palette->Count > 256)
3639 return InvalidParameter;
3641 new_palette = GdipAlloc(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
3642 if (!new_palette) return OutOfMemory;
3644 GdipFree(image->palette);
3645 image->palette = new_palette;
3646 image->palette->Flags = palette->Flags;
3647 image->palette->Count = palette->Count;
3648 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
3653 /*************************************************************************
3655 * Structures that represent which formats we support for encoding.
3658 /* ImageCodecInfo creation routines taken from libgdiplus */
3659 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
3660 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
3661 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
3662 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
3663 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
3664 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
3666 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
3667 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
3668 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
3669 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
3670 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
3671 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
3673 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
3674 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
3675 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
3676 static const WCHAR gif_format[] = {'G','I','F',0};
3677 static const BYTE gif_sig_pattern[4] = "GIF8";
3678 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3680 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
3681 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
3682 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
3683 static const WCHAR tiff_format[] = {'T','I','F','F',0};
3684 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
3685 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3687 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
3688 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
3689 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
3690 static const WCHAR emf_format[] = {'E','M','F',0};
3691 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
3692 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3694 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
3695 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
3696 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
3697 static const WCHAR wmf_format[] = {'W','M','F',0};
3698 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
3699 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
3701 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
3702 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
3703 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
3704 static const WCHAR png_format[] = {'P','N','G',0};
3705 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
3706 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3708 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
3709 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
3710 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
3711 static const WCHAR ico_format[] = {'I','C','O',0};
3712 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
3713 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3715 static const struct image_codec codecs[NUM_CODECS] = {
3718 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3719 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3720 /* CodecName */ bmp_codecname,
3722 /* FormatDescription */ bmp_format,
3723 /* FilenameExtension */ bmp_extension,
3724 /* MimeType */ bmp_mimetype,
3725 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3729 /* SigPattern */ bmp_sig_pattern,
3730 /* SigMask */ bmp_sig_mask,
3737 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3738 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3739 /* CodecName */ jpeg_codecname,
3741 /* FormatDescription */ jpeg_format,
3742 /* FilenameExtension */ jpeg_extension,
3743 /* MimeType */ jpeg_mimetype,
3744 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3748 /* SigPattern */ jpeg_sig_pattern,
3749 /* SigMask */ jpeg_sig_mask,
3756 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3757 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3758 /* CodecName */ gif_codecname,
3760 /* FormatDescription */ gif_format,
3761 /* FilenameExtension */ gif_extension,
3762 /* MimeType */ gif_mimetype,
3763 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3767 /* SigPattern */ gif_sig_pattern,
3768 /* SigMask */ gif_sig_mask,
3775 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3776 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3777 /* CodecName */ tiff_codecname,
3779 /* FormatDescription */ tiff_format,
3780 /* FilenameExtension */ tiff_extension,
3781 /* MimeType */ tiff_mimetype,
3782 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3786 /* SigPattern */ tiff_sig_pattern,
3787 /* SigMask */ tiff_sig_mask,
3794 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3795 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3796 /* CodecName */ emf_codecname,
3798 /* FormatDescription */ emf_format,
3799 /* FilenameExtension */ emf_extension,
3800 /* MimeType */ emf_mimetype,
3801 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3805 /* SigPattern */ emf_sig_pattern,
3806 /* SigMask */ emf_sig_mask,
3809 decode_image_olepicture_metafile
3813 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3814 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3815 /* CodecName */ wmf_codecname,
3817 /* FormatDescription */ wmf_format,
3818 /* FilenameExtension */ wmf_extension,
3819 /* MimeType */ wmf_mimetype,
3820 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3824 /* SigPattern */ wmf_sig_pattern,
3825 /* SigMask */ wmf_sig_mask,
3828 decode_image_olepicture_metafile
3832 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3833 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3834 /* CodecName */ png_codecname,
3836 /* FormatDescription */ png_format,
3837 /* FilenameExtension */ png_extension,
3838 /* MimeType */ png_mimetype,
3839 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3843 /* SigPattern */ png_sig_pattern,
3844 /* SigMask */ png_sig_mask,
3851 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3852 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3853 /* CodecName */ ico_codecname,
3855 /* FormatDescription */ ico_format,
3856 /* FilenameExtension */ ico_extension,
3857 /* MimeType */ ico_mimetype,
3858 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3862 /* SigPattern */ ico_sig_pattern,
3863 /* SigMask */ ico_sig_mask,
3870 /*****************************************************************************
3871 * GdipGetImageDecodersSize [GDIPLUS.@]
3873 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
3875 int decoder_count=0;
3877 TRACE("%p %p\n", numDecoders, size);
3879 if (!numDecoders || !size)
3880 return InvalidParameter;
3882 for (i=0; i<NUM_CODECS; i++)
3884 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3888 *numDecoders = decoder_count;
3889 *size = decoder_count * sizeof(ImageCodecInfo);
3894 /*****************************************************************************
3895 * GdipGetImageDecoders [GDIPLUS.@]
3897 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
3899 int i, decoder_count=0;
3900 TRACE("%u %u %p\n", numDecoders, size, decoders);
3903 size != numDecoders * sizeof(ImageCodecInfo))
3904 return GenericError;
3906 for (i=0; i<NUM_CODECS; i++)
3908 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3910 if (decoder_count == numDecoders) return GenericError;
3911 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3916 if (decoder_count < numDecoders) return GenericError;
3921 /*****************************************************************************
3922 * GdipGetImageEncodersSize [GDIPLUS.@]
3924 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
3926 int encoder_count=0;
3928 TRACE("%p %p\n", numEncoders, size);
3930 if (!numEncoders || !size)
3931 return InvalidParameter;
3933 for (i=0; i<NUM_CODECS; i++)
3935 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3939 *numEncoders = encoder_count;
3940 *size = encoder_count * sizeof(ImageCodecInfo);
3945 /*****************************************************************************
3946 * GdipGetImageEncoders [GDIPLUS.@]
3948 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
3950 int i, encoder_count=0;
3951 TRACE("%u %u %p\n", numEncoders, size, encoders);
3954 size != numEncoders * sizeof(ImageCodecInfo))
3955 return GenericError;
3957 for (i=0; i<NUM_CODECS; i++)
3959 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3961 if (encoder_count == numEncoders) return GenericError;
3962 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3967 if (encoder_count < numEncoders) return GenericError;
3972 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
3973 GDIPCONST CLSID* clsidEncoder, UINT *size)
3977 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
3980 FIXME("not implemented\n");
3984 return NotImplemented;
3987 static PixelFormat get_16bpp_format(HBITMAP hbm)
3993 hdc = CreateCompatibleDC(NULL);
3995 memset(&bmh, 0, sizeof(bmh));
3996 bmh.bV4Size = sizeof(bmh);
3999 bmh.bV4V4Compression = BI_BITFIELDS;
4000 bmh.bV4BitCount = 16;
4002 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4004 if (bmh.bV4RedMask == 0x7c00 &&
4005 bmh.bV4GreenMask == 0x3e0 &&
4006 bmh.bV4BlueMask == 0x1f)
4008 result = PixelFormat16bppRGB555;
4010 else if (bmh.bV4RedMask == 0xf800 &&
4011 bmh.bV4GreenMask == 0x7e0 &&
4012 bmh.bV4BlueMask == 0x1f)
4014 result = PixelFormat16bppRGB565;
4018 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4019 bmh.bV4GreenMask, bmh.bV4BlueMask);
4020 result = PixelFormatUndefined;
4028 /*****************************************************************************
4029 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4031 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4036 BitmapData lockeddata;
4039 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4042 return InvalidParameter;
4044 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4045 return InvalidParameter;
4047 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4048 switch(bm.bmBitsPixel) {
4050 format = PixelFormat1bppIndexed;
4053 format = PixelFormat4bppIndexed;
4056 format = PixelFormat8bppIndexed;
4059 format = get_16bpp_format(hbm);
4060 if (format == PixelFormatUndefined)
4061 return InvalidParameter;
4064 format = PixelFormat24bppRGB;
4067 format = PixelFormat32bppRGB;
4070 format = PixelFormat48bppRGB;
4073 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4074 return InvalidParameter;
4077 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4078 format, NULL, bitmap);
4082 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4083 format, &lockeddata);
4088 for (y=0; y<bm.bmHeight; y++)
4090 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
4091 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
4100 INT src_height, dst_stride;
4103 hdc = CreateCompatibleDC(NULL);
4104 oldhbm = SelectObject(hdc, hbm);
4106 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
4110 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4111 pbmi->bmiHeader.biBitCount = 0;
4113 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4115 src_height = abs(pbmi->bmiHeader.biHeight);
4117 if (pbmi->bmiHeader.biHeight > 0)
4119 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
4120 dst_stride = -lockeddata.Stride;
4124 dst_bits = lockeddata.Scan0;
4125 dst_stride = lockeddata.Stride;
4128 for (y=0; y<src_height; y++)
4130 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
4131 pbmi, DIB_RGB_COLORS);
4137 retval = OutOfMemory;
4139 SelectObject(hdc, oldhbm);
4143 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4146 if (retval == Ok && hpal)
4148 WORD num_palette_entries;
4149 PALETTEENTRY *palette_entries=NULL;
4150 ColorPalette *palette=NULL;
4153 if (!GetObjectW(hpal, sizeof(num_palette_entries), &num_palette_entries))
4154 retval = GenericError;
4158 palette_entries = GdipAlloc(sizeof(PALETTEENTRY) * num_palette_entries);
4159 palette = GdipAlloc(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4161 if (!palette_entries || !palette)
4162 retval = OutOfMemory;
4167 if (!GetPaletteEntries(hpal, 0, num_palette_entries, palette_entries))
4168 retval = GenericError;
4174 palette->Count = num_palette_entries;
4176 for (i=0; i<num_palette_entries; i++)
4178 PALETTEENTRY * entry = &palette_entries[i];
4179 palette->Entries[i] = 0xff000000 | entry->peRed << 16 |
4180 entry->peGreen << 8 | entry->peBlue;
4183 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
4186 GdipFree(palette_entries);
4192 GdipDisposeImage((GpImage*)*bitmap);
4200 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
4202 FIXME("(%p): stub\n", effect);
4203 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
4204 * in Windows's gdiplus */
4205 return NotImplemented;
4208 /*****************************************************************************
4209 * GdipSetEffectParameters [GDIPLUS.@]
4211 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
4212 const VOID *params, const UINT size)
4216 TRACE("(%p,%p,%u)\n", effect, params, size);
4219 FIXME("not implemented\n");
4221 return NotImplemented;
4224 /*****************************************************************************
4225 * GdipGetImageFlags [GDIPLUS.@]
4227 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
4229 TRACE("%p %p\n", image, flags);
4231 if(!image || !flags)
4232 return InvalidParameter;
4234 *flags = image->flags;
4239 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
4241 TRACE("(%d, %p)\n", control, param);
4244 case TestControlForceBilinear:
4246 FIXME("TestControlForceBilinear not handled\n");
4248 case TestControlNoICM:
4250 FIXME("TestControlNoICM not handled\n");
4252 case TestControlGetBuildNumber:
4253 *((DWORD*)param) = 3102;
4260 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
4261 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
4262 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
4263 GpMetafile **metafile)
4265 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4266 frameUnit, debugstr_w(desc), metafile);
4268 return NotImplemented;
4271 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
4272 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
4273 GDIPCONST WCHAR *desc, GpMetafile **metafile)
4275 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4276 frameUnit, debugstr_w(desc), metafile);
4278 return NotImplemented;
4281 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
4283 TRACE("%p\n", image);
4288 /*****************************************************************************
4289 * GdipGetImageThumbnail [GDIPLUS.@]
4291 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
4292 GpImage **ret_image, GetThumbnailImageAbort cb,
4296 GpGraphics *graphics;
4297 UINT srcwidth, srcheight;
4299 TRACE("(%p %u %u %p %p %p)\n",
4300 image, width, height, ret_image, cb, cb_data);
4302 if (!image || !ret_image)
4303 return InvalidParameter;
4305 if (!width) width = 120;
4306 if (!height) height = 120;
4308 GdipGetImageWidth(image, &srcwidth);
4309 GdipGetImageHeight(image, &srcheight);
4311 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB,
4312 NULL, (GpBitmap**)ret_image);
4316 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
4320 stat = GdipDrawImageRectRectI(graphics, image,
4321 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
4324 GdipDeleteGraphics(graphics);
4329 GdipDisposeImage(*ret_image);
4337 /*****************************************************************************
4338 * GdipImageRotateFlip [GDIPLUS.@]
4340 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
4342 GpBitmap *new_bitmap;
4344 int bpp, bytesperpixel;
4345 int rotate_90, flip_x, flip_y;
4346 int src_x_offset, src_y_offset;
4348 UINT x, y, width, height;
4349 BitmapData src_lock, dst_lock;
4352 TRACE("(%p, %u)\n", image, type);
4355 return InvalidParameter;
4358 flip_x = (type&6) == 2 || (type&6) == 4;
4359 flip_y = (type&3) == 1 || (type&3) == 2;
4361 if (image->type != ImageTypeBitmap)
4363 FIXME("Not implemented for type %i\n", image->type);
4364 return NotImplemented;
4367 bitmap = (GpBitmap*)image;
4368 bpp = PIXELFORMATBPP(bitmap->format);
4372 FIXME("Not implemented for %i bit images\n", bpp);
4373 return NotImplemented;
4378 width = bitmap->height;
4379 height = bitmap->width;
4383 width = bitmap->width;
4384 height = bitmap->height;
4387 bytesperpixel = bpp/8;
4389 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
4394 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
4398 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
4402 LPBYTE src_row, src_pixel;
4403 LPBYTE dst_row, dst_pixel;
4405 src_origin = src_lock.Scan0;
4406 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
4407 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
4411 if (flip_y) src_x_offset = -src_lock.Stride;
4412 else src_x_offset = src_lock.Stride;
4413 if (flip_x) src_y_offset = -bytesperpixel;
4414 else src_y_offset = bytesperpixel;
4418 if (flip_x) src_x_offset = -bytesperpixel;
4419 else src_x_offset = bytesperpixel;
4420 if (flip_y) src_y_offset = -src_lock.Stride;
4421 else src_y_offset = src_lock.Stride;
4424 src_row = src_origin;
4425 dst_row = dst_lock.Scan0;
4426 for (y=0; y<height; y++)
4428 src_pixel = src_row;
4429 dst_pixel = dst_row;
4430 for (x=0; x<width; x++)
4432 /* FIXME: This could probably be faster without memcpy. */
4433 memcpy(dst_pixel, src_pixel, bytesperpixel);
4434 dst_pixel += bytesperpixel;
4435 src_pixel += src_x_offset;
4437 src_row += src_y_offset;
4438 dst_row += dst_lock.Stride;
4441 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
4444 GdipBitmapUnlockBits(bitmap, &src_lock);
4448 move_bitmap(bitmap, new_bitmap, FALSE);
4450 GdipDisposeImage((GpImage*)new_bitmap);
4455 /*****************************************************************************
4456 * GdipConvertToEmfPlusToFile [GDIPLUS.@]
4459 GpStatus WINGDIPAPI GdipConvertToEmfPlusToFile(const GpGraphics* refGraphics,
4460 GpMetafile* metafile, BOOL* conversionSuccess,
4461 const WCHAR* filename, EmfType emfType,
4462 const WCHAR* description, GpMetafile** out_metafile)
4464 FIXME("stub: %p, %p, %p, %p, %u, %p, %p\n", refGraphics, metafile, conversionSuccess, filename, emfType, description, out_metafile);
4465 return NotImplemented;