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); \
544 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
545 for (x=0; x<width; x++) \
546 for (y=0; y<height; y++) { \
548 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
549 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
556 case PixelFormat1bppIndexed:
559 case PixelFormat16bppGrayScale:
560 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
561 case PixelFormat16bppRGB555:
562 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
563 case PixelFormat16bppRGB565:
564 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
565 case PixelFormat16bppARGB1555:
566 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
567 case PixelFormat24bppRGB:
568 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
569 case PixelFormat32bppRGB:
570 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
571 case PixelFormat32bppARGB:
572 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
573 case PixelFormat32bppPARGB:
574 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
575 case PixelFormat48bppRGB:
576 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
577 case PixelFormat64bppARGB:
578 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
583 case PixelFormat4bppIndexed:
586 case PixelFormat16bppGrayScale:
587 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
588 case PixelFormat16bppRGB555:
589 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
590 case PixelFormat16bppRGB565:
591 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
592 case PixelFormat16bppARGB1555:
593 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
594 case PixelFormat24bppRGB:
595 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
596 case PixelFormat32bppRGB:
597 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
598 case PixelFormat32bppARGB:
599 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
600 case PixelFormat32bppPARGB:
601 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
602 case PixelFormat48bppRGB:
603 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
604 case PixelFormat64bppARGB:
605 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
610 case PixelFormat8bppIndexed:
613 case PixelFormat16bppGrayScale:
614 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
615 case PixelFormat16bppRGB555:
616 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
617 case PixelFormat16bppRGB565:
618 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
619 case PixelFormat16bppARGB1555:
620 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
621 case PixelFormat24bppRGB:
622 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
623 case PixelFormat32bppRGB:
624 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
625 case PixelFormat32bppARGB:
626 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
627 case PixelFormat32bppPARGB:
628 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
629 case PixelFormat48bppRGB:
630 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
631 case PixelFormat64bppARGB:
632 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
637 case PixelFormat16bppGrayScale:
640 case PixelFormat1bppIndexed:
641 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
642 case PixelFormat8bppIndexed:
643 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
644 case PixelFormat16bppRGB555:
645 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
646 case PixelFormat16bppRGB565:
647 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
648 case PixelFormat16bppARGB1555:
649 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
650 case PixelFormat24bppRGB:
651 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
652 case PixelFormat32bppRGB:
653 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
654 case PixelFormat32bppARGB:
655 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
656 case PixelFormat32bppPARGB:
657 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
658 case PixelFormat48bppRGB:
659 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
660 case PixelFormat64bppARGB:
661 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
666 case PixelFormat16bppRGB555:
669 case PixelFormat1bppIndexed:
670 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
671 case PixelFormat8bppIndexed:
672 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
673 case PixelFormat16bppGrayScale:
674 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
675 case PixelFormat16bppRGB565:
676 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
677 case PixelFormat16bppARGB1555:
678 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
679 case PixelFormat24bppRGB:
680 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
681 case PixelFormat32bppRGB:
682 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
683 case PixelFormat32bppARGB:
684 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
685 case PixelFormat32bppPARGB:
686 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
687 case PixelFormat48bppRGB:
688 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
689 case PixelFormat64bppARGB:
690 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
695 case PixelFormat16bppRGB565:
698 case PixelFormat1bppIndexed:
699 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
700 case PixelFormat8bppIndexed:
701 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
702 case PixelFormat16bppGrayScale:
703 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
704 case PixelFormat16bppRGB555:
705 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
706 case PixelFormat16bppARGB1555:
707 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
708 case PixelFormat24bppRGB:
709 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
710 case PixelFormat32bppRGB:
711 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
712 case PixelFormat32bppARGB:
713 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
714 case PixelFormat32bppPARGB:
715 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
716 case PixelFormat48bppRGB:
717 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
718 case PixelFormat64bppARGB:
719 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
724 case PixelFormat16bppARGB1555:
727 case PixelFormat1bppIndexed:
728 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
729 case PixelFormat8bppIndexed:
730 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
731 case PixelFormat16bppGrayScale:
732 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
733 case PixelFormat16bppRGB555:
734 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
735 case PixelFormat16bppRGB565:
736 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
737 case PixelFormat24bppRGB:
738 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
739 case PixelFormat32bppRGB:
740 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
741 case PixelFormat32bppARGB:
742 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
743 case PixelFormat32bppPARGB:
744 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
745 case PixelFormat48bppRGB:
746 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
747 case PixelFormat64bppARGB:
748 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
753 case PixelFormat24bppRGB:
756 case PixelFormat1bppIndexed:
757 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
758 case PixelFormat8bppIndexed:
759 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
760 case PixelFormat16bppGrayScale:
761 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
762 case PixelFormat16bppRGB555:
763 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
764 case PixelFormat16bppRGB565:
765 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
766 case PixelFormat16bppARGB1555:
767 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
768 case PixelFormat32bppRGB:
769 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
770 case PixelFormat32bppARGB:
771 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
772 case PixelFormat32bppPARGB:
773 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
774 case PixelFormat48bppRGB:
775 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
776 case PixelFormat64bppARGB:
777 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
782 case PixelFormat32bppRGB:
785 case PixelFormat1bppIndexed:
786 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
787 case PixelFormat8bppIndexed:
788 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
789 case PixelFormat16bppGrayScale:
790 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
791 case PixelFormat16bppRGB555:
792 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
793 case PixelFormat16bppRGB565:
794 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
795 case PixelFormat16bppARGB1555:
796 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
797 case PixelFormat24bppRGB:
798 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
799 case PixelFormat32bppARGB:
800 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
801 case PixelFormat32bppPARGB:
802 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
803 case PixelFormat48bppRGB:
804 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
805 case PixelFormat64bppARGB:
806 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
811 case PixelFormat32bppARGB:
814 case PixelFormat1bppIndexed:
815 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
816 case PixelFormat8bppIndexed:
817 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
818 case PixelFormat16bppGrayScale:
819 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
820 case PixelFormat16bppRGB555:
821 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
822 case PixelFormat16bppRGB565:
823 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
824 case PixelFormat16bppARGB1555:
825 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
826 case PixelFormat24bppRGB:
827 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
828 case PixelFormat32bppPARGB:
829 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
831 case PixelFormat48bppRGB:
832 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
833 case PixelFormat64bppARGB:
834 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
839 case PixelFormat32bppPARGB:
842 case PixelFormat1bppIndexed:
843 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
844 case PixelFormat8bppIndexed:
845 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
846 case PixelFormat16bppGrayScale:
847 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
848 case PixelFormat16bppRGB555:
849 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
850 case PixelFormat16bppRGB565:
851 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
852 case PixelFormat16bppARGB1555:
853 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
854 case PixelFormat24bppRGB:
855 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
856 case PixelFormat32bppRGB:
857 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
858 case PixelFormat32bppARGB:
859 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
860 case PixelFormat48bppRGB:
861 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
862 case PixelFormat64bppARGB:
863 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
868 case PixelFormat48bppRGB:
871 case PixelFormat1bppIndexed:
872 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
873 case PixelFormat8bppIndexed:
874 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
875 case PixelFormat16bppGrayScale:
876 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
877 case PixelFormat16bppRGB555:
878 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
879 case PixelFormat16bppRGB565:
880 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
881 case PixelFormat16bppARGB1555:
882 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
883 case PixelFormat24bppRGB:
884 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
885 case PixelFormat32bppRGB:
886 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
887 case PixelFormat32bppARGB:
888 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
889 case PixelFormat32bppPARGB:
890 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
891 case PixelFormat64bppARGB:
892 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
897 case PixelFormat64bppARGB:
900 case PixelFormat1bppIndexed:
901 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
902 case PixelFormat8bppIndexed:
903 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
904 case PixelFormat16bppGrayScale:
905 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
906 case PixelFormat16bppRGB555:
907 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
908 case PixelFormat16bppRGB565:
909 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
910 case PixelFormat16bppARGB1555:
911 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
912 case PixelFormat24bppRGB:
913 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
914 case PixelFormat32bppRGB:
915 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
916 case PixelFormat32bppARGB:
917 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
918 case PixelFormat32bppPARGB:
919 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
920 case PixelFormat48bppRGB:
921 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
926 case PixelFormat64bppPARGB:
929 case PixelFormat1bppIndexed:
930 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
931 case PixelFormat8bppIndexed:
932 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
933 case PixelFormat16bppGrayScale:
934 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
935 case PixelFormat16bppRGB555:
936 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
937 case PixelFormat16bppRGB565:
938 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
939 case PixelFormat16bppARGB1555:
940 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
941 case PixelFormat24bppRGB:
942 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
943 case PixelFormat32bppRGB:
944 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
945 case PixelFormat32bppARGB:
946 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
947 case PixelFormat32bppPARGB:
948 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
949 case PixelFormat48bppRGB:
950 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
951 case PixelFormat64bppARGB:
952 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
961 #undef convert_indexed_to_rgb
962 #undef convert_rgb_to_rgb
964 return NotImplemented;
967 /* This function returns a pointer to an array of pixels that represents the
968 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
969 * flags. It is correct behavior that a user who calls this function with write
970 * privileges can write to the whole bitmap (not just the area in rect).
972 * FIXME: only used portion of format is bits per pixel. */
973 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
974 UINT flags, PixelFormat format, BitmapData* lockeddata)
976 INT bitspp = PIXELFORMATBPP(format);
977 GpRect act_rect; /* actual rect to be used */
980 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
982 if(!lockeddata || !bitmap)
983 return InvalidParameter;
986 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
987 (rect->Y + rect->Height > bitmap->height) || !flags)
988 return InvalidParameter;
993 act_rect.X = act_rect.Y = 0;
994 act_rect.Width = bitmap->width;
995 act_rect.Height = bitmap->height;
1000 WARN("bitmap is already locked and cannot be locked again\n");
1004 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1006 /* no conversion is necessary; just use the bits directly */
1007 lockeddata->Width = act_rect.Width;
1008 lockeddata->Height = act_rect.Height;
1009 lockeddata->PixelFormat = format;
1010 lockeddata->Reserved = flags;
1011 lockeddata->Stride = bitmap->stride;
1012 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1013 bitmap->stride * act_rect.Y;
1015 bitmap->lockmode = flags | ImageLockModeRead;
1021 /* Make sure we can convert to the requested format. */
1022 if (flags & ImageLockModeRead)
1024 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
1025 if (stat == NotImplemented)
1027 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1028 return NotImplemented;
1032 /* If we're opening for writing, make sure we'll be able to write back in
1033 * the original format. */
1034 if (flags & ImageLockModeWrite)
1036 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
1037 if (stat == NotImplemented)
1039 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1040 return NotImplemented;
1044 lockeddata->Width = act_rect.Width;
1045 lockeddata->Height = act_rect.Height;
1046 lockeddata->PixelFormat = format;
1047 lockeddata->Reserved = flags;
1049 if(!(flags & ImageLockModeUserInputBuf))
1051 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1053 bitmap->bitmapbits = GdipAlloc(lockeddata->Stride * act_rect.Height);
1055 if (!bitmap->bitmapbits) return OutOfMemory;
1057 lockeddata->Scan0 = bitmap->bitmapbits;
1060 if (flags & ImageLockModeRead)
1064 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1066 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1070 stat = convert_pixels(act_rect.Width, act_rect.Height,
1071 lockeddata->Stride, lockeddata->Scan0, format,
1073 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1074 bitmap->format, bitmap->image.palette);
1078 GdipFree(bitmap->bitmapbits);
1079 bitmap->bitmapbits = NULL;
1084 bitmap->lockmode = flags | ImageLockModeRead;
1086 bitmap->lockx = act_rect.X;
1087 bitmap->locky = act_rect.Y;
1092 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1094 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1096 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1097 return InvalidParameter;
1099 bitmap->image.xres = xdpi;
1100 bitmap->image.yres = ydpi;
1105 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1106 BitmapData* lockeddata)
1111 TRACE("(%p,%p)\n", bitmap, lockeddata);
1113 if(!bitmap || !lockeddata)
1114 return InvalidParameter;
1116 if(!bitmap->lockmode)
1119 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1120 if(!(--bitmap->numlocks))
1121 bitmap->lockmode = 0;
1123 GdipFree(bitmap->bitmapbits);
1124 bitmap->bitmapbits = NULL;
1128 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1130 /* we passed a direct reference; no need to do anything */
1131 bitmap->lockmode = 0;
1132 bitmap->numlocks = 0;
1136 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1138 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1142 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1144 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1146 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1150 ERR("failed to convert pixels; this should never happen\n");
1153 GdipFree(bitmap->bitmapbits);
1154 bitmap->bitmapbits = NULL;
1155 bitmap->lockmode = 0;
1156 bitmap->numlocks = 0;
1161 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1162 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1164 BitmapData lockeddata_src, lockeddata_dst;
1170 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1172 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1174 x + width > srcBitmap->width || y + height > srcBitmap->height)
1176 TRACE("<-- InvalidParameter\n");
1177 return InvalidParameter;
1180 if (format == PixelFormatDontCare)
1181 format = srcBitmap->format;
1185 area.Width = roundr(width);
1186 area.Height = roundr(height);
1188 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
1190 if (stat != Ok) return stat;
1192 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1193 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
1196 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
1197 lockeddata_src.PixelFormat, &lockeddata_dst);
1201 /* copy the image data */
1202 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1203 for (i=0; i<lockeddata_src.Height; i++)
1204 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1205 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1208 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
1212 GdipDisposeImage((GpImage*)*dstBitmap);
1215 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
1225 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1226 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1228 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1230 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1233 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1235 GpStatus stat = GenericError;
1237 TRACE("%p, %p\n", image, cloneImage);
1239 if (!image || !cloneImage)
1240 return InvalidParameter;
1249 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1251 return GenericError;
1253 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1256 WARN("Failed to save image on stream\n");
1260 /* Set seek pointer back to the beginning of the picture */
1262 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1266 stat = GdipLoadImageFromStream(stream, cloneImage);
1267 if (stat != Ok) WARN("Failed to load image from stream\n");
1270 IStream_Release(stream);
1273 else if (image->type == ImageTypeBitmap)
1275 GpBitmap *bitmap = (GpBitmap*)image;
1276 BitmapData lockeddata_src, lockeddata_dst;
1280 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
1282 if (stat != Ok) return stat;
1284 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1285 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
1288 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
1289 lockeddata_src.PixelFormat, &lockeddata_dst);
1293 /* copy the image data */
1294 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1295 for (i=0; i<lockeddata_src.Height; i++)
1296 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1297 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1300 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
1304 GdipDisposeImage(*cloneImage);
1307 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
1313 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
1319 ERR("GpImage with no IPicture or bitmap?!\n");
1320 return NotImplemented;
1324 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1330 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1332 if(!filename || !bitmap)
1333 return InvalidParameter;
1335 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1340 stat = GdipCreateBitmapFromStream(stream, bitmap);
1342 IStream_Release(stream);
1347 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1348 VOID *bits, GpBitmap **bitmap)
1350 DWORD height, stride;
1353 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1355 if (!info || !bits || !bitmap)
1356 return InvalidParameter;
1358 height = abs(info->bmiHeader.biHeight);
1359 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1361 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1363 bits = (BYTE*)bits + (height - 1) * stride;
1367 switch(info->bmiHeader.biBitCount) {
1369 format = PixelFormat1bppIndexed;
1372 format = PixelFormat4bppIndexed;
1375 format = PixelFormat8bppIndexed;
1378 format = PixelFormat16bppRGB555;
1381 format = PixelFormat24bppRGB;
1384 format = PixelFormat32bppRGB;
1387 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1389 return InvalidParameter;
1392 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1398 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1401 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1403 return GdipCreateBitmapFromFile(filename, bitmap);
1406 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1407 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1410 GpStatus stat = InvalidParameter;
1412 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1414 if(!lpBitmapName || !bitmap)
1415 return InvalidParameter;
1418 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1419 LR_CREATEDIBSECTION);
1422 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1429 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1430 HBITMAP* hbmReturn, ARGB background)
1435 BITMAPINFOHEADER bih;
1437 BitmapData lockeddata;
1438 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1440 if (!bitmap || !hbmReturn) return InvalidParameter;
1442 GdipGetImageWidth((GpImage*)bitmap, &width);
1443 GdipGetImageHeight((GpImage*)bitmap, &height);
1445 bih.biSize = sizeof(bih);
1446 bih.biWidth = width;
1447 bih.biHeight = height;
1449 bih.biBitCount = 32;
1450 bih.biCompression = BI_RGB;
1451 bih.biSizeImage = 0;
1452 bih.biXPelsPerMeter = 0;
1453 bih.biYPelsPerMeter = 0;
1455 bih.biClrImportant = 0;
1457 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1461 lockeddata.Stride = -width * 4;
1462 lockeddata.Scan0 = bits + (width * 4 * (height - 1));
1464 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead|ImageLockModeUserInputBuf,
1465 PixelFormat32bppPARGB, &lockeddata);
1468 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1471 stat = GenericError;
1473 if (stat != Ok && result)
1475 DeleteObject(result);
1479 *hbmReturn = result;
1484 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1485 GpMetafile* metafile, BOOL* succ, EmfType emfType,
1486 const WCHAR* description, GpMetafile** out_metafile)
1490 TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1491 debugstr_w(description), out_metafile);
1493 if(!ref || !metafile || !out_metafile)
1494 return InvalidParameter;
1497 *out_metafile = NULL;
1500 FIXME("not implemented\n");
1502 return NotImplemented;
1505 /* FIXME: this should create a bitmap in the given size with the attributes
1506 * (resolution etc.) of the graphics object */
1507 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1508 GpGraphics* target, GpBitmap** bitmap)
1513 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1515 if(!target || !bitmap)
1516 return InvalidParameter;
1519 FIXME("hacked stub\n");
1521 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1527 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1535 BitmapData lockeddata;
1540 BITMAPINFOHEADER bih;
1545 TRACE("%p, %p\n", hicon, bitmap);
1547 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1548 return InvalidParameter;
1550 /* get the size of the icon */
1551 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1553 DeleteObject(iinfo.hbmColor);
1554 DeleteObject(iinfo.hbmMask);
1555 return GenericError;
1561 height = abs(bm.bmHeight);
1562 else /* combined bitmap + mask */
1563 height = abs(bm.bmHeight) / 2;
1565 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1567 DeleteObject(iinfo.hbmColor);
1568 DeleteObject(iinfo.hbmMask);
1572 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1574 DeleteObject(iinfo.hbmColor);
1575 DeleteObject(iinfo.hbmMask);
1576 HeapFree(GetProcessHeap(), 0, bits);
1583 rect.Height = height;
1585 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1587 DeleteObject(iinfo.hbmColor);
1588 DeleteObject(iinfo.hbmMask);
1589 HeapFree(GetProcessHeap(), 0, bits);
1590 GdipDisposeImage((GpImage*)*bitmap);
1594 bih.biSize = sizeof(bih);
1595 bih.biWidth = width;
1596 bih.biHeight = -height;
1598 bih.biBitCount = 32;
1599 bih.biCompression = BI_RGB;
1600 bih.biSizeImage = 0;
1601 bih.biXPelsPerMeter = 0;
1602 bih.biYPelsPerMeter = 0;
1604 bih.biClrImportant = 0;
1606 screendc = GetDC(0);
1609 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1611 if (bm.bmBitsPixel == 32)
1615 /* If any pixel has a non-zero alpha, ignore hbmMask */
1617 for (x=0; x<width && !has_alpha; x++)
1618 for (y=0; y<height && !has_alpha; y++)
1619 if ((*src++ & 0xff000000) != 0)
1622 else has_alpha = FALSE;
1626 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1630 /* copy the image data to the Bitmap */
1632 dst_row = lockeddata.Scan0;
1633 for (y=0; y<height; y++)
1635 memcpy(dst_row, src, width*4);
1637 dst_row += lockeddata.Stride;
1644 /* read alpha data from the mask */
1646 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1648 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1651 dst_row = lockeddata.Scan0;
1652 for (y=0; y<height; y++)
1654 dst = (DWORD*)dst_row;
1655 for (x=0; x<height; x++)
1657 DWORD src_value = *src++;
1661 *dst++ |= 0xff000000;
1663 dst_row += lockeddata.Stride;
1668 /* set constant alpha of 255 */
1670 for (y=0; y<height; y++)
1672 dst = (DWORD*)dst_row;
1673 for (x=0; x<height; x++)
1674 *dst++ |= 0xff000000;
1675 dst_row += lockeddata.Stride;
1680 ReleaseDC(0, screendc);
1682 DeleteObject(iinfo.hbmColor);
1683 DeleteObject(iinfo.hbmMask);
1685 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1687 HeapFree(GetProcessHeap(), 0, bits);
1692 static void generate_halftone_palette(ARGB *entries, UINT count)
1694 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1697 for (i=0; i<8 && i<count; i++)
1699 entries[i] = 0xff000000;
1700 if (i&1) entries[i] |= 0x800000;
1701 if (i&2) entries[i] |= 0x8000;
1702 if (i&4) entries[i] |= 0x80;
1706 entries[i] = 0xffc0c0c0;
1708 for (i=9; i<16 && i<count; i++)
1710 entries[i] = 0xff000000;
1711 if (i&1) entries[i] |= 0xff0000;
1712 if (i&2) entries[i] |= 0xff00;
1713 if (i&4) entries[i] |= 0xff;
1716 for (i=16; i<40 && i<count; i++)
1721 for (i=40; i<256 && i<count; i++)
1723 entries[i] = 0xff000000;
1724 entries[i] |= halftone_values[(i-40)%6];
1725 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1726 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1730 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1732 HDC screendc = GetDC(0);
1734 if (!screendc) return GenericError;
1736 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1737 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1739 ReleaseDC(0, screendc);
1744 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1745 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1748 HBITMAP hbitmap=NULL;
1749 INT row_size, dib_stride;
1750 BYTE *bits=NULL, *own_bits=NULL;
1754 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1756 if (!bitmap) return InvalidParameter;
1758 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1760 return InvalidParameter;
1763 if(scan0 && !stride)
1764 return InvalidParameter;
1766 stat = get_screen_resolution(&xres, &yres);
1767 if (stat != Ok) return stat;
1769 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1770 dib_stride = (row_size + 3) & ~3;
1773 stride = dib_stride;
1775 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1777 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1781 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1782 pbmi->bmiHeader.biWidth = width;
1783 pbmi->bmiHeader.biHeight = -height;
1784 pbmi->bmiHeader.biPlanes = 1;
1785 /* FIXME: use the rest of the data from format */
1786 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1787 pbmi->bmiHeader.biCompression = BI_RGB;
1788 pbmi->bmiHeader.biSizeImage = 0;
1789 pbmi->bmiHeader.biXPelsPerMeter = 0;
1790 pbmi->bmiHeader.biYPelsPerMeter = 0;
1791 pbmi->bmiHeader.biClrUsed = 0;
1792 pbmi->bmiHeader.biClrImportant = 0;
1794 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1798 if (!hbitmap) return GenericError;
1800 stride = dib_stride;
1804 /* Not a GDI format; don't try to make an HBITMAP. */
1809 INT size = abs(stride) * height;
1811 own_bits = bits = GdipAlloc(size);
1812 if (!own_bits) return OutOfMemory;
1815 bits += stride * (1 - height);
1819 *bitmap = GdipAlloc(sizeof(GpBitmap));
1822 DeleteObject(hbitmap);
1827 (*bitmap)->image.type = ImageTypeBitmap;
1828 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1829 (*bitmap)->image.flags = ImageFlagsNone;
1830 (*bitmap)->image.frame_count = 1;
1831 (*bitmap)->image.current_frame = 0;
1832 (*bitmap)->image.palette = NULL;
1833 (*bitmap)->image.xres = xres;
1834 (*bitmap)->image.yres = yres;
1835 (*bitmap)->width = width;
1836 (*bitmap)->height = height;
1837 (*bitmap)->format = format;
1838 (*bitmap)->image.picture = NULL;
1839 (*bitmap)->image.stream = NULL;
1840 (*bitmap)->hbitmap = hbitmap;
1841 (*bitmap)->hdc = NULL;
1842 (*bitmap)->bits = bits;
1843 (*bitmap)->stride = stride;
1844 (*bitmap)->own_bits = own_bits;
1845 (*bitmap)->metadata_reader = NULL;
1847 /* set format-related flags */
1848 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1849 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1851 if (format == PixelFormat1bppIndexed ||
1852 format == PixelFormat4bppIndexed ||
1853 format == PixelFormat8bppIndexed)
1855 (*bitmap)->image.palette = GdipAlloc(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1857 if (!(*bitmap)->image.palette)
1859 GdipDisposeImage(&(*bitmap)->image);
1864 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1866 if (format == PixelFormat1bppIndexed)
1868 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1869 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1870 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1874 if (format == PixelFormat8bppIndexed)
1875 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1877 generate_halftone_palette((*bitmap)->image.palette->Entries,
1878 (*bitmap)->image.palette->Count);
1882 TRACE("<-- %p\n", *bitmap);
1887 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1892 TRACE("%p %p\n", stream, bitmap);
1894 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1899 if((*bitmap)->image.type != ImageTypeBitmap){
1900 GdipDisposeImage(&(*bitmap)->image);
1902 return GenericError; /* FIXME: what error to return? */
1909 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1912 TRACE("%p %p\n", stream, bitmap);
1914 return GdipCreateBitmapFromStream(stream, bitmap);
1917 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1918 GpCachedBitmap **cachedbmp)
1922 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1924 if(!bitmap || !graphics || !cachedbmp)
1925 return InvalidParameter;
1927 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1931 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1933 GdipFree(*cachedbmp);
1940 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1943 BitmapData lockeddata;
1944 ULONG andstride, xorstride, bitssize;
1945 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1948 TRACE("(%p, %p)\n", bitmap, hicon);
1950 if (!bitmap || !hicon)
1951 return InvalidParameter;
1953 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1954 PixelFormat32bppPARGB, &lockeddata);
1957 andstride = ((lockeddata.Width+31)/32)*4;
1958 xorstride = lockeddata.Width*4;
1959 bitssize = (andstride + xorstride) * lockeddata.Height;
1961 andbits = GdipAlloc(bitssize);
1965 xorbits = andbits + andstride * lockeddata.Height;
1967 for (y=0; y<lockeddata.Height; y++)
1969 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
1971 androw = andbits + andstride * y;
1972 for (x=0; x<lockeddata.Width; x++)
1973 if (srcrow[3+4*x] >= 128)
1974 androw[x/8] |= 1 << (7-x%8);
1976 xorrow = xorbits + xorstride * y;
1977 memcpy(xorrow, srcrow, xorstride);
1980 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
1988 GdipBitmapUnlockBits(bitmap, &lockeddata);
1994 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1996 TRACE("%p\n", cachedbmp);
1999 return InvalidParameter;
2001 GdipDisposeImage(cachedbmp->image);
2002 GdipFree(cachedbmp);
2007 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2008 GpCachedBitmap *cachedbmp, INT x, INT y)
2010 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2012 if(!graphics || !cachedbmp)
2013 return InvalidParameter;
2015 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2018 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
2019 LPBYTE pData16, INT iMapMode, INT eFlags)
2021 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
2022 return NotImplemented;
2025 /* Internal utility function: Replace the image data of dst with that of src,
2027 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2029 assert(src->image.type == ImageTypeBitmap);
2030 assert(dst->image.type == ImageTypeBitmap);
2032 GdipFree(dst->bitmapbits);
2033 GdipFree(dst->own_bits);
2035 DeleteObject(dst->hbitmap);
2037 if (clobber_palette)
2039 GdipFree(dst->image.palette);
2040 dst->image.palette = src->image.palette;
2043 GdipFree(src->image.palette);
2045 dst->image.xres = src->image.xres;
2046 dst->image.yres = src->image.yres;
2047 dst->width = src->width;
2048 dst->height = src->height;
2049 dst->format = src->format;
2050 dst->hbitmap = src->hbitmap;
2051 dst->hdc = src->hdc;
2052 dst->bits = src->bits;
2053 dst->stride = src->stride;
2054 dst->own_bits = src->own_bits;
2055 if (dst->metadata_reader)
2056 IWICMetadataReader_Release(dst->metadata_reader);
2057 dst->metadata_reader = src->metadata_reader;
2058 if (dst->image.stream)
2059 IStream_Release(dst->image.stream);
2060 dst->image.stream = src->image.stream;
2061 dst->image.frame_count = src->image.frame_count;
2062 dst->image.current_frame = src->image.current_frame;
2063 dst->image.format = src->image.format;
2065 src->image.type = ~0;
2069 static GpStatus free_image_data(GpImage *image)
2072 return InvalidParameter;
2074 if (image->type == ImageTypeBitmap)
2076 GdipFree(((GpBitmap*)image)->bitmapbits);
2077 GdipFree(((GpBitmap*)image)->own_bits);
2078 DeleteDC(((GpBitmap*)image)->hdc);
2079 DeleteObject(((GpBitmap*)image)->hbitmap);
2080 if (((GpBitmap*)image)->metadata_reader)
2081 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2083 else if (image->type == ImageTypeMetafile)
2085 GpMetafile *metafile = (GpMetafile*)image;
2086 GdipFree(metafile->comment_data);
2087 DeleteEnhMetaFile(CloseEnhMetaFile(metafile->record_dc));
2088 DeleteEnhMetaFile(metafile->hemf);
2089 if (metafile->record_graphics)
2091 WARN("metafile closed while recording\n");
2092 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2093 metafile->record_graphics->image = NULL;
2094 metafile->record_graphics->busy = TRUE;
2099 WARN("invalid image: %p\n", image);
2103 IPicture_Release(image->picture);
2105 IStream_Release(image->stream);
2106 GdipFree(image->palette);
2111 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2115 TRACE("%p\n", image);
2117 status = free_image_data(image);
2118 if (status != Ok) return status;
2125 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2129 TRACE("(%p,%p)\n", image, item);
2132 return InvalidParameter;
2135 FIXME("not implemented\n");
2137 return NotImplemented;
2140 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2144 TRACE("(%p,%p)\n", image, item);
2147 FIXME("not implemented\n");
2149 return NotImplemented;
2152 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2155 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2157 if(!image || !srcRect || !srcUnit)
2158 return InvalidParameter;
2159 if(image->type == ImageTypeMetafile){
2160 *srcRect = ((GpMetafile*)image)->bounds;
2161 *srcUnit = ((GpMetafile*)image)->unit;
2163 else if(image->type == ImageTypeBitmap){
2164 srcRect->X = srcRect->Y = 0.0;
2165 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2166 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2167 *srcUnit = UnitPixel;
2170 srcRect->X = srcRect->Y = 0.0;
2171 srcRect->Width = ipicture_pixel_width(image->picture);
2172 srcRect->Height = ipicture_pixel_height(image->picture);
2173 *srcUnit = UnitPixel;
2176 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2177 srcRect->Width, srcRect->Height, *srcUnit);
2182 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2185 TRACE("%p %p %p\n", image, width, height);
2187 if(!image || !height || !width)
2188 return InvalidParameter;
2190 if(image->type == ImageTypeMetafile){
2192 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2196 *height = convert_unit(res, ((GpMetafile*)image)->unit) *
2197 ((GpMetafile*)image)->bounds.Height;
2199 *width = convert_unit(res, ((GpMetafile*)image)->unit) *
2200 ((GpMetafile*)image)->bounds.Width;
2203 else if(image->type == ImageTypeBitmap){
2204 *height = ((GpBitmap*)image)->height;
2205 *width = ((GpBitmap*)image)->width;
2208 *height = ipicture_pixel_height(image->picture);
2209 *width = ipicture_pixel_width(image->picture);
2212 TRACE("returning (%f, %f)\n", *height, *width);
2216 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2217 GpGraphics **graphics)
2222 TRACE("%p %p\n", image, graphics);
2224 if(!image || !graphics)
2225 return InvalidParameter;
2227 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2229 hdc = ((GpBitmap*)image)->hdc;
2232 hdc = CreateCompatibleDC(0);
2233 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2234 ((GpBitmap*)image)->hdc = hdc;
2237 stat = GdipCreateFromHDC(hdc, graphics);
2240 (*graphics)->image = image;
2242 else if (image->type == ImageTypeMetafile)
2243 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2245 stat = graphics_from_image(image, graphics);
2250 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2252 TRACE("%p %p\n", image, height);
2254 if(!image || !height)
2255 return InvalidParameter;
2257 if(image->type == ImageTypeMetafile){
2259 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2263 *height = roundr(convert_unit(res, ((GpMetafile*)image)->unit) *
2264 ((GpMetafile*)image)->bounds.Height);
2266 else if(image->type == ImageTypeBitmap)
2267 *height = ((GpBitmap*)image)->height;
2269 *height = ipicture_pixel_height(image->picture);
2271 TRACE("returning %d\n", *height);
2276 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2279 return InvalidParameter;
2283 TRACE("(%p) <-- %0.2f\n", image, *res);
2288 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2290 TRACE("%p %p\n", image, size);
2293 return InvalidParameter;
2295 if (!image->palette || image->palette->Count == 0)
2296 *size = sizeof(ColorPalette);
2298 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2300 TRACE("<-- %u\n", *size);
2305 /* FIXME: test this function for non-bitmap types */
2306 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2308 TRACE("%p %p\n", image, format);
2310 if(!image || !format)
2311 return InvalidParameter;
2313 if(image->type != ImageTypeBitmap)
2314 *format = PixelFormat24bppRGB;
2316 *format = ((GpBitmap*) image)->format;
2321 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2323 TRACE("(%p, %p)\n", image, format);
2325 if(!image || !format)
2326 return InvalidParameter;
2328 memcpy(format, &image->format, sizeof(GUID));
2333 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2335 TRACE("%p %p\n", image, type);
2338 return InvalidParameter;
2340 *type = image->type;
2345 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2348 return InvalidParameter;
2352 TRACE("(%p) <-- %0.2f\n", image, *res);
2357 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2359 TRACE("%p %p\n", image, width);
2361 if(!image || !width)
2362 return InvalidParameter;
2364 if(image->type == ImageTypeMetafile){
2366 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2370 *width = roundr(convert_unit(res, ((GpMetafile*)image)->unit) *
2371 ((GpMetafile*)image)->bounds.Width);
2373 else if(image->type == ImageTypeBitmap)
2374 *width = ((GpBitmap*)image)->width;
2376 *width = ipicture_pixel_width(image->picture);
2378 TRACE("returning %d\n", *width);
2383 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2384 MetafileHeader * header)
2388 TRACE("(%p, %p)\n", metafile, header);
2390 if(!metafile || !header)
2391 return InvalidParameter;
2394 FIXME("not implemented\n");
2396 memset(header, 0, sizeof(MetafileHeader));
2401 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromEmf(HENHMETAFILE hEmf,
2402 MetafileHeader *header)
2406 if(!hEmf || !header)
2407 return InvalidParameter;
2410 FIXME("not implemented\n");
2412 memset(header, 0, sizeof(MetafileHeader));
2417 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromFile(GDIPCONST WCHAR *filename,
2418 MetafileHeader *header)
2422 TRACE("(%s,%p)\n", debugstr_w(filename), header);
2424 if(!filename || !header)
2425 return InvalidParameter;
2428 FIXME("not implemented\n");
2430 memset(header, 0, sizeof(MetafileHeader));
2435 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromStream(IStream *stream,
2436 MetafileHeader *header)
2440 TRACE("(%p,%p)\n", stream, header);
2442 if(!stream || !header)
2443 return InvalidParameter;
2446 FIXME("not implemented\n");
2448 memset(header, 0, sizeof(MetafileHeader));
2453 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2455 TRACE("(%p, %p)\n", image, num);
2457 if (!image || !num) return InvalidParameter;
2461 if (image->type == ImageTypeBitmap)
2463 if (((GpBitmap *)image)->metadata_reader)
2464 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2470 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2473 IWICMetadataReader *reader;
2474 IWICEnumMetadataItem *enumerator;
2475 UINT prop_count, i, items_returned;
2477 TRACE("(%p, %u, %p)\n", image, num, list);
2479 if (!image || !list) return InvalidParameter;
2481 if (image->type != ImageTypeBitmap)
2483 FIXME("Not implemented for type %d\n", image->type);
2484 return NotImplemented;
2487 reader = ((GpBitmap *)image)->metadata_reader;
2490 if (num != 0) return InvalidParameter;
2494 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2495 if (FAILED(hr)) return hresult_to_status(hr);
2497 if (num != prop_count) return InvalidParameter;
2499 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2500 if (FAILED(hr)) return hresult_to_status(hr);
2502 IWICEnumMetadataItem_Reset(enumerator);
2504 for (i = 0; i < num; i++)
2508 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2509 if (hr != S_OK) break;
2511 if (id.vt != VT_UI2)
2513 FIXME("not supported propvariant type for id: %u\n", id.vt);
2517 list[i] = id.u.uiVal;
2520 IWICEnumMetadataItem_Release(enumerator);
2522 return hr == S_OK ? Ok : hresult_to_status(hr);
2525 static UINT propvariant_size(PROPVARIANT *value)
2527 switch (value->vt & ~VT_VECTOR)
2533 if (!(value->vt & VT_VECTOR)) return 1;
2534 return value->u.caub.cElems;
2537 if (!(value->vt & VT_VECTOR)) return 2;
2538 return value->u.caui.cElems * 2;
2542 if (!(value->vt & VT_VECTOR)) return 4;
2543 return value->u.caul.cElems * 4;
2547 if (!(value->vt & VT_VECTOR)) return 8;
2548 return value->u.cauh.cElems * 8;
2550 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2552 return value->u.blob.cbSize;
2554 FIXME("not supported variant type %d\n", value->vt);
2559 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2562 IWICMetadataReader *reader;
2563 PROPVARIANT id, value;
2565 TRACE("(%p,%#x,%p)\n", image, propid, size);
2567 if (!size || !image) return InvalidParameter;
2569 if (image->type != ImageTypeBitmap)
2571 FIXME("Not implemented for type %d\n", image->type);
2572 return NotImplemented;
2575 reader = ((GpBitmap *)image)->metadata_reader;
2576 if (!reader) return PropertyNotFound;
2579 id.u.uiVal = propid;
2580 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2581 if (FAILED(hr)) return PropertyNotFound;
2583 *size = propvariant_size(&value);
2584 if (*size) *size += sizeof(PropertyItem);
2585 PropVariantClear(&value);
2590 #ifndef PropertyTagTypeSByte
2591 #define PropertyTagTypeSByte 6
2592 #define PropertyTagTypeSShort 8
2593 #define PropertyTagTypeFloat 11
2594 #define PropertyTagTypeDouble 12
2597 static UINT vt_to_itemtype(UINT vt)
2604 { VT_I1, PropertyTagTypeSByte },
2605 { VT_UI1, PropertyTagTypeByte },
2606 { VT_I2, PropertyTagTypeSShort },
2607 { VT_UI2, PropertyTagTypeShort },
2608 { VT_I4, PropertyTagTypeSLONG },
2609 { VT_UI4, PropertyTagTypeLong },
2610 { VT_I8, PropertyTagTypeSRational },
2611 { VT_UI8, PropertyTagTypeRational },
2612 { VT_R4, PropertyTagTypeFloat },
2613 { VT_R8, PropertyTagTypeDouble },
2614 { VT_LPSTR, PropertyTagTypeASCII },
2615 { VT_BLOB, PropertyTagTypeUndefined }
2618 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2620 if (vt2type[i].vt == vt) return vt2type[i].type;
2622 FIXME("not supported variant type %u\n", vt);
2626 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2627 UINT size, PROPID id)
2629 UINT item_size, item_type;
2631 item_size = propvariant_size(value);
2632 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2634 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2635 if (!item_type) return InvalidParameter;
2637 item->value = item + 1;
2639 switch (value->vt & ~VT_VECTOR)
2643 if (!(value->vt & VT_VECTOR))
2644 *(BYTE *)item->value = value->u.bVal;
2646 memcpy(item->value, value->u.caub.pElems, item_size);
2650 if (!(value->vt & VT_VECTOR))
2651 *(USHORT *)item->value = value->u.uiVal;
2653 memcpy(item->value, value->u.caui.pElems, item_size);
2658 if (!(value->vt & VT_VECTOR))
2659 *(ULONG *)item->value = value->u.ulVal;
2661 memcpy(item->value, value->u.caul.pElems, item_size);
2666 if (!(value->vt & VT_VECTOR))
2667 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2669 memcpy(item->value, value->u.cauh.pElems, item_size);
2672 memcpy(item->value, value->u.pszVal, item_size);
2675 memcpy(item->value, value->u.blob.pBlobData, item_size);
2678 FIXME("not supported variant type %d\n", value->vt);
2679 return InvalidParameter;
2682 item->length = item_size;
2683 item->type = item_type;
2689 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2690 PropertyItem *buffer)
2694 IWICMetadataReader *reader;
2695 PROPVARIANT id, value;
2697 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2699 if (!image || !buffer) return InvalidParameter;
2701 if (image->type != ImageTypeBitmap)
2703 FIXME("Not implemented for type %d\n", image->type);
2704 return NotImplemented;
2707 reader = ((GpBitmap *)image)->metadata_reader;
2708 if (!reader) return PropertyNotFound;
2711 id.u.uiVal = propid;
2712 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2713 if (FAILED(hr)) return PropertyNotFound;
2715 stat = propvariant_to_item(&value, buffer, size, propid);
2716 PropVariantClear(&value);
2721 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2724 IWICMetadataReader *reader;
2725 IWICEnumMetadataItem *enumerator;
2726 UINT prop_count, prop_size, i;
2727 PROPVARIANT id, value;
2729 TRACE("(%p,%p,%p)\n", image, size, count);
2731 if (!image || !size || !count) return InvalidParameter;
2733 if (image->type != ImageTypeBitmap)
2735 FIXME("Not implemented for type %d\n", image->type);
2736 return NotImplemented;
2739 reader = ((GpBitmap *)image)->metadata_reader;
2740 if (!reader) return PropertyNotFound;
2742 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2743 if (FAILED(hr)) return hresult_to_status(hr);
2745 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2746 if (FAILED(hr)) return hresult_to_status(hr);
2748 IWICEnumMetadataItem_Reset(enumerator);
2752 PropVariantInit(&id);
2753 PropVariantInit(&value);
2755 for (i = 0; i < prop_count; i++)
2757 UINT items_returned, item_size;
2759 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2760 if (hr != S_OK) break;
2762 item_size = propvariant_size(&value);
2763 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2765 PropVariantClear(&id);
2766 PropVariantClear(&value);
2769 IWICEnumMetadataItem_Release(enumerator);
2771 if (hr != S_OK) return PropertyNotFound;
2773 *count = prop_count;
2778 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2779 UINT count, PropertyItem *buf)
2783 IWICMetadataReader *reader;
2784 IWICEnumMetadataItem *enumerator;
2785 UINT prop_count, prop_size, i;
2786 PROPVARIANT id, value;
2789 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2791 if (!image || !buf) return InvalidParameter;
2793 if (image->type != ImageTypeBitmap)
2795 FIXME("Not implemented for type %d\n", image->type);
2796 return NotImplemented;
2799 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2800 if (status != Ok) return status;
2802 if (prop_count != count || prop_size != size) return InvalidParameter;
2804 reader = ((GpBitmap *)image)->metadata_reader;
2805 if (!reader) return PropertyNotFound;
2807 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2808 if (FAILED(hr)) return hresult_to_status(hr);
2810 IWICEnumMetadataItem_Reset(enumerator);
2812 item_value = (char *)(buf + prop_count);
2814 PropVariantInit(&id);
2815 PropVariantInit(&value);
2817 for (i = 0; i < prop_count; i++)
2820 UINT items_returned, item_size;
2822 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2823 if (hr != S_OK) break;
2825 if (id.vt != VT_UI2)
2827 FIXME("not supported propvariant type for id: %u\n", id.vt);
2831 item_size = propvariant_size(&value);
2834 item = HeapAlloc(GetProcessHeap(), 0, item_size + sizeof(*item));
2836 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2837 buf[i].id = item->id;
2838 buf[i].type = item->type;
2839 buf[i].length = item_size;
2840 buf[i].value = item_value;
2841 memcpy(item_value, item->value, item_size);
2842 item_value += item_size;
2844 HeapFree(GetProcessHeap(), 0, item);
2847 PropVariantClear(&id);
2848 PropVariantClear(&value);
2851 IWICEnumMetadataItem_Release(enumerator);
2853 if (hr != S_OK) return PropertyNotFound;
2858 struct image_format_dimension
2861 const GUID *dimension;
2864 static const struct image_format_dimension image_format_dimensions[] =
2866 {&ImageFormatGIF, &FrameDimensionTime},
2867 {&ImageFormatIcon, &FrameDimensionResolution},
2871 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2872 GDIPCONST GUID* dimensionID, UINT* count)
2874 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2876 if(!image || !count)
2877 return InvalidParameter;
2880 IsEqualGUID(dimensionID, &image->format) ||
2881 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2882 IsEqualGUID(dimensionID, &FrameDimensionTime))
2884 *count = image->frame_count;
2888 return InvalidParameter;
2891 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2894 TRACE("(%p, %p)\n", image, count);
2896 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2898 if(!image || !count)
2899 return InvalidParameter;
2906 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2907 GUID* dimensionIDs, UINT count)
2910 const GUID *result=NULL;
2912 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2914 if(!image || !dimensionIDs || count != 1)
2915 return InvalidParameter;
2917 for (i=0; image_format_dimensions[i].format; i++)
2919 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2921 result = image_format_dimensions[i].dimension;
2927 result = &FrameDimensionPage;
2929 memcpy(dimensionIDs, result, sizeof(GUID));
2934 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2940 TRACE("(%s) %p\n", debugstr_w(filename), image);
2942 if (!filename || !image)
2943 return InvalidParameter;
2945 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2950 stat = GdipLoadImageFromStream(stream, image);
2952 IStream_Release(stream);
2957 /* FIXME: no icm handling */
2958 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2960 TRACE("(%s) %p\n", debugstr_w(filename), image);
2962 return GdipLoadImageFromFile(filename, image);
2965 static const WICPixelFormatGUID * const wic_pixel_formats[] = {
2966 &GUID_WICPixelFormatBlackWhite,
2967 &GUID_WICPixelFormat1bppIndexed,
2968 &GUID_WICPixelFormat16bppBGR555,
2969 &GUID_WICPixelFormat24bppBGR,
2970 &GUID_WICPixelFormat32bppBGR,
2971 &GUID_WICPixelFormat32bppBGRA,
2972 &GUID_WICPixelFormat32bppPBGRA,
2976 static const PixelFormat wic_gdip_formats[] = {
2977 PixelFormat1bppIndexed,
2978 PixelFormat1bppIndexed,
2979 PixelFormat16bppRGB555,
2980 PixelFormat24bppRGB,
2981 PixelFormat32bppRGB,
2982 PixelFormat32bppARGB,
2983 PixelFormat32bppPARGB,
2986 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
2991 IWICBitmapDecoder *decoder;
2992 IWICBitmapFrameDecode *frame;
2993 IWICBitmapSource *source=NULL;
2994 IWICMetadataBlockReader *block_reader;
2995 WICPixelFormatGUID wic_format;
2996 PixelFormat gdip_format=0;
2998 UINT width, height, frame_count;
2999 BitmapData lockeddata;
3003 initresult = CoInitialize(NULL);
3005 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3006 &IID_IWICBitmapDecoder, (void**)&decoder);
3007 if (FAILED(hr)) goto end;
3009 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
3012 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3013 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3016 if (SUCCEEDED(hr)) /* got frame */
3018 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3022 IWICBitmapSource *bmp_source;
3023 IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICBitmapSource, (void **)&bmp_source);
3025 for (i=0; wic_pixel_formats[i]; i++)
3027 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
3029 source = bmp_source;
3030 gdip_format = wic_gdip_formats[i];
3036 /* unknown format; fall back on 32bppARGB */
3037 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, bmp_source, &source);
3038 gdip_format = PixelFormat32bppARGB;
3039 IWICBitmapSource_Release(bmp_source);
3041 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3044 if (SUCCEEDED(hr)) /* got source */
3046 hr = IWICBitmapSource_GetSize(source, &width, &height);
3049 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3052 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3054 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3055 gdip_format, &lockeddata);
3056 if (status == Ok) /* locked bitmap */
3061 for (i=0; i<height; i++)
3064 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3065 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3066 if (FAILED(hr)) break;
3069 GdipBitmapUnlockBits(bitmap, &lockeddata);
3072 if (SUCCEEDED(hr) && status == Ok)
3073 *image = (GpImage*)bitmap;
3077 GdipDisposeImage((GpImage*)bitmap);
3080 if (SUCCEEDED(hr) && status == Ok)
3083 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3086 bitmap->image.xres = dpix;
3087 bitmap->image.yres = dpiy;
3093 IWICBitmapSource_Release(source);
3096 bitmap->metadata_reader = NULL;
3097 if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3099 UINT block_count = 0;
3100 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3101 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3102 IWICMetadataBlockReader_Release(block_reader);
3105 IWICBitmapFrameDecode_Release(frame);
3108 IWICBitmapDecoder_Release(decoder);
3111 if (SUCCEEDED(initresult)) CoUninitialize();
3113 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3117 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3118 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3119 bitmap->image.frame_count = frame_count;
3120 bitmap->image.current_frame = active_frame;
3121 bitmap->image.stream = stream;
3122 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3123 bitmap->image.palette->Flags = 0;
3124 /* Pin the source stream */
3125 IStream_AddRef(stream);
3131 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3133 return decode_image_wic(stream, &CLSID_WICIcoDecoder, active_frame, image);
3136 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3141 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, active_frame, image);
3143 bitmap = (GpBitmap*)*image;
3145 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3147 /* WIC supports bmp files with alpha, but gdiplus does not */
3148 bitmap->format = PixelFormat32bppRGB;
3154 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3156 return decode_image_wic(stream, &CLSID_WICJpegDecoder, active_frame, image);
3159 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3161 return decode_image_wic(stream, &CLSID_WICPngDecoder, active_frame, image);
3164 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3166 return decode_image_wic(stream, &CLSID_WICGifDecoder, active_frame, image);
3169 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3171 return decode_image_wic(stream, &CLSID_WICTiffDecoder, active_frame, image);
3174 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3178 TRACE("%p %p\n", stream, image);
3180 if(!stream || !image)
3181 return InvalidParameter;
3183 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3184 (LPVOID*) &pic) != S_OK){
3185 TRACE("Could not load picture\n");
3186 return GenericError;
3189 /* FIXME: missing initialization code */
3190 *image = GdipAlloc(sizeof(GpMetafile));
3191 if(!*image) return OutOfMemory;
3192 (*image)->type = ImageTypeMetafile;
3193 (*image)->stream = NULL;
3194 (*image)->picture = pic;
3195 (*image)->flags = ImageFlagsNone;
3196 (*image)->frame_count = 1;
3197 (*image)->current_frame = 0;
3198 (*image)->palette = NULL;
3200 TRACE("<-- %p\n", *image);
3205 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
3206 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
3208 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, UINT active_frame, GpImage **image);
3210 typedef struct image_codec {
3211 ImageCodecInfo info;
3212 encode_image_func encode_func;
3213 decode_image_func decode_func;
3228 static const struct image_codec codecs[NUM_CODECS];
3230 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
3233 const BYTE *pattern, *mask;
3239 /* seek to the start of the stream */
3241 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3242 if (FAILED(hr)) return hresult_to_status(hr);
3244 /* read the first 8 bytes */
3245 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
3246 hr = IStream_Read(stream, signature, 8, &bytesread);
3247 if (FAILED(hr)) return hresult_to_status(hr);
3248 if (hr == S_FALSE || bytesread == 0) return GenericError;
3250 for (i = 0; i < NUM_CODECS; i++) {
3251 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
3252 bytesread >= codecs[i].info.SigSize)
3254 for (sig=0; sig<codecs[i].info.SigCount; sig++)
3256 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
3257 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
3258 for (j=0; j<codecs[i].info.SigSize; j++)
3259 if ((signature[j] & mask[j]) != pattern[j])
3261 if (j == codecs[i].info.SigSize)
3263 *result = &codecs[i];
3270 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
3271 signature[0],signature[1],signature[2],signature[3],
3272 signature[4],signature[5],signature[6],signature[7]);
3274 return GenericError;
3277 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
3283 const struct image_codec *codec = NULL;
3287 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
3289 if (!image || !dimensionID)
3290 return InvalidParameter;
3292 if (frame >= image->frame_count)
3293 return InvalidParameter;
3295 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
3297 WARN("invalid image type %d\n", image->type);
3298 return InvalidParameter;
3301 if (image->current_frame == frame)
3306 TRACE("image doesn't have an associated stream\n");
3310 hr = IStream_Clone(image->stream, &stream);
3315 hr = IStream_Stat(image->stream, &statstg, STATFLAG_NOOPEN);
3316 if (FAILED(hr)) return hresult_to_status(hr);
3318 stat = GdipCreateStreamOnFile(statstg.pwcsName, GENERIC_READ, &stream);
3319 if (stat != Ok) return stat;
3322 /* choose an appropriate image decoder */
3323 stat = get_decoder_info(stream, &codec);
3326 IStream_Release(stream);
3330 /* seek to the start of the stream */
3332 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3335 IStream_Release(stream);
3336 return hresult_to_status(hr);
3339 /* call on the image decoder to do the real work */
3340 stat = codec->decode_func(stream, &codec->info.Clsid, frame, &new_image);
3344 memcpy(&new_image->format, &codec->info.FormatID, sizeof(GUID));
3345 free_image_data(image);
3346 if (image->type == ImageTypeBitmap)
3347 *(GpBitmap *)image = *(GpBitmap *)new_image;
3348 else if (image->type == ImageTypeMetafile)
3349 *(GpMetafile *)image = *(GpMetafile *)new_image;
3350 new_image->type = ~0;
3351 GdipFree(new_image);
3355 IStream_Release(stream);
3359 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
3364 const struct image_codec *codec=NULL;
3366 /* choose an appropriate image decoder */
3367 stat = get_decoder_info(stream, &codec);
3368 if (stat != Ok) return stat;
3370 /* seek to the start of the stream */
3372 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3373 if (FAILED(hr)) return hresult_to_status(hr);
3375 /* call on the image decoder to do the real work */
3376 stat = codec->decode_func(stream, &codec->info.Clsid, 0, image);
3378 /* take note of the original data format */
3381 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
3389 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
3391 TRACE("%p %p\n", stream, image);
3393 return GdipLoadImageFromStream(stream, image);
3396 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
3400 TRACE("(%p,%u)\n", image, propId);
3403 return InvalidParameter;
3406 FIXME("not implemented\n");
3408 return NotImplemented;
3411 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
3415 if (!image || !item) return InvalidParameter;
3417 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
3420 FIXME("not implemented\n");
3422 return NotImplemented;
3425 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
3426 GDIPCONST CLSID *clsidEncoder,
3427 GDIPCONST EncoderParameters *encoderParams)
3432 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
3434 if (!image || !filename|| !clsidEncoder)
3435 return InvalidParameter;
3437 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
3439 return GenericError;
3441 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
3443 IStream_Release(stream);
3447 /*************************************************************************
3448 * Encoding functions -
3449 * These functions encode an image in different image file formats.
3451 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
3452 #define BITMAP_FORMAT_JPEG 0xd8ff
3453 #define BITMAP_FORMAT_GIF 0x4947
3454 #define BITMAP_FORMAT_PNG 0x5089
3455 #define BITMAP_FORMAT_APM 0xcdd7
3457 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
3458 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3462 IWICBitmapEncoder *encoder;
3463 IWICBitmapFrameEncode *frameencode;
3464 IPropertyBag2 *encoderoptions;
3467 PixelFormat gdipformat=0;
3468 WICPixelFormatGUID wicformat;
3470 BitmapData lockeddata;
3474 if (image->type != ImageTypeBitmap)
3475 return GenericError;
3477 bitmap = (GpBitmap*)image;
3479 GdipGetImageWidth(image, &width);
3480 GdipGetImageHeight(image, &height);
3487 initresult = CoInitialize(NULL);
3489 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3490 &IID_IWICBitmapEncoder, (void**)&encoder);
3493 if (SUCCEEDED(initresult)) CoUninitialize();
3494 return hresult_to_status(hr);
3497 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
3501 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
3504 if (SUCCEEDED(hr)) /* created frame */
3506 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
3509 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
3512 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
3516 for (i=0; wic_pixel_formats[i]; i++)
3518 if (wic_gdip_formats[i] == bitmap->format)
3521 if (wic_pixel_formats[i])
3522 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
3524 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
3526 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
3528 for (i=0; wic_pixel_formats[i]; i++)
3530 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
3533 if (wic_pixel_formats[i])
3534 gdipformat = wic_gdip_formats[i];
3537 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
3544 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
3549 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
3552 /* write one row at a time in case stride is negative */
3553 row = lockeddata.Scan0;
3554 for (i=0; i<lockeddata.Height; i++)
3556 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
3557 if (FAILED(hr)) break;
3558 row += lockeddata.Stride;
3561 GdipBitmapUnlockBits(bitmap, &lockeddata);
3568 hr = IWICBitmapFrameEncode_Commit(frameencode);
3570 IWICBitmapFrameEncode_Release(frameencode);
3571 IPropertyBag2_Release(encoderoptions);
3575 hr = IWICBitmapEncoder_Commit(encoder);
3577 IWICBitmapEncoder_Release(encoder);
3579 if (SUCCEEDED(initresult)) CoUninitialize();
3581 return hresult_to_status(hr);
3584 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
3585 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3587 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
3590 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
3591 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3593 return encode_image_WIC(image, stream, &CLSID_WICTiffEncoder, params);
3596 static GpStatus encode_image_png(GpImage *image, IStream* stream,
3597 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3599 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
3602 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
3603 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3605 return encode_image_WIC(image, stream, &CLSID_WICJpegEncoder, params);
3608 /*****************************************************************************
3609 * GdipSaveImageToStream [GDIPLUS.@]
3611 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
3612 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3615 encode_image_func encode_image;
3618 TRACE("%p %p %p %p\n", image, stream, clsid, params);
3620 if(!image || !stream)
3621 return InvalidParameter;
3623 /* select correct encoder */
3624 encode_image = NULL;
3625 for (i = 0; i < NUM_CODECS; i++) {
3626 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
3627 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
3628 encode_image = codecs[i].encode_func;
3630 if (encode_image == NULL)
3631 return UnknownImageFormat;
3633 stat = encode_image(image, stream, clsid, params);
3638 /*****************************************************************************
3639 * GdipSaveAdd [GDIPLUS.@]
3641 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
3643 FIXME("(%p,%p): stub\n", image, params);
3647 /*****************************************************************************
3648 * GdipGetImagePalette [GDIPLUS.@]
3650 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
3654 TRACE("(%p,%p,%i)\n", image, palette, size);
3656 if (!image || !palette)
3657 return InvalidParameter;
3659 count = image->palette ? image->palette->Count : 0;
3661 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
3663 TRACE("<-- InsufficientBuffer\n");
3664 return InsufficientBuffer;
3669 palette->Flags = image->palette->Flags;
3670 palette->Count = image->palette->Count;
3671 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
3681 /*****************************************************************************
3682 * GdipSetImagePalette [GDIPLUS.@]
3684 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
3685 GDIPCONST ColorPalette *palette)
3687 ColorPalette *new_palette;
3689 TRACE("(%p,%p)\n", image, palette);
3691 if(!image || !palette || palette->Count > 256)
3692 return InvalidParameter;
3694 new_palette = GdipAlloc(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
3695 if (!new_palette) return OutOfMemory;
3697 GdipFree(image->palette);
3698 image->palette = new_palette;
3699 image->palette->Flags = palette->Flags;
3700 image->palette->Count = palette->Count;
3701 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
3706 /*************************************************************************
3708 * Structures that represent which formats we support for encoding.
3711 /* ImageCodecInfo creation routines taken from libgdiplus */
3712 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
3713 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
3714 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
3715 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
3716 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
3717 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
3719 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
3720 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
3721 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
3722 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
3723 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
3724 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
3726 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
3727 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
3728 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
3729 static const WCHAR gif_format[] = {'G','I','F',0};
3730 static const BYTE gif_sig_pattern[4] = "GIF8";
3731 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3733 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
3734 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
3735 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
3736 static const WCHAR tiff_format[] = {'T','I','F','F',0};
3737 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
3738 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3740 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
3741 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
3742 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
3743 static const WCHAR emf_format[] = {'E','M','F',0};
3744 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
3745 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3747 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
3748 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
3749 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
3750 static const WCHAR wmf_format[] = {'W','M','F',0};
3751 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
3752 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
3754 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
3755 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
3756 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
3757 static const WCHAR png_format[] = {'P','N','G',0};
3758 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
3759 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3761 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
3762 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
3763 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
3764 static const WCHAR ico_format[] = {'I','C','O',0};
3765 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
3766 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3768 static const struct image_codec codecs[NUM_CODECS] = {
3771 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3772 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3773 /* CodecName */ bmp_codecname,
3775 /* FormatDescription */ bmp_format,
3776 /* FilenameExtension */ bmp_extension,
3777 /* MimeType */ bmp_mimetype,
3778 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3782 /* SigPattern */ bmp_sig_pattern,
3783 /* SigMask */ bmp_sig_mask,
3790 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3791 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3792 /* CodecName */ jpeg_codecname,
3794 /* FormatDescription */ jpeg_format,
3795 /* FilenameExtension */ jpeg_extension,
3796 /* MimeType */ jpeg_mimetype,
3797 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3801 /* SigPattern */ jpeg_sig_pattern,
3802 /* SigMask */ jpeg_sig_mask,
3809 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3810 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3811 /* CodecName */ gif_codecname,
3813 /* FormatDescription */ gif_format,
3814 /* FilenameExtension */ gif_extension,
3815 /* MimeType */ gif_mimetype,
3816 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3820 /* SigPattern */ gif_sig_pattern,
3821 /* SigMask */ gif_sig_mask,
3828 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3829 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3830 /* CodecName */ tiff_codecname,
3832 /* FormatDescription */ tiff_format,
3833 /* FilenameExtension */ tiff_extension,
3834 /* MimeType */ tiff_mimetype,
3835 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3839 /* SigPattern */ tiff_sig_pattern,
3840 /* SigMask */ tiff_sig_mask,
3847 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3848 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3849 /* CodecName */ emf_codecname,
3851 /* FormatDescription */ emf_format,
3852 /* FilenameExtension */ emf_extension,
3853 /* MimeType */ emf_mimetype,
3854 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3858 /* SigPattern */ emf_sig_pattern,
3859 /* SigMask */ emf_sig_mask,
3862 decode_image_olepicture_metafile
3866 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3867 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3868 /* CodecName */ wmf_codecname,
3870 /* FormatDescription */ wmf_format,
3871 /* FilenameExtension */ wmf_extension,
3872 /* MimeType */ wmf_mimetype,
3873 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3877 /* SigPattern */ wmf_sig_pattern,
3878 /* SigMask */ wmf_sig_mask,
3881 decode_image_olepicture_metafile
3885 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3886 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3887 /* CodecName */ png_codecname,
3889 /* FormatDescription */ png_format,
3890 /* FilenameExtension */ png_extension,
3891 /* MimeType */ png_mimetype,
3892 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3896 /* SigPattern */ png_sig_pattern,
3897 /* SigMask */ png_sig_mask,
3904 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3905 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3906 /* CodecName */ ico_codecname,
3908 /* FormatDescription */ ico_format,
3909 /* FilenameExtension */ ico_extension,
3910 /* MimeType */ ico_mimetype,
3911 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3915 /* SigPattern */ ico_sig_pattern,
3916 /* SigMask */ ico_sig_mask,
3923 /*****************************************************************************
3924 * GdipGetImageDecodersSize [GDIPLUS.@]
3926 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
3928 int decoder_count=0;
3930 TRACE("%p %p\n", numDecoders, size);
3932 if (!numDecoders || !size)
3933 return InvalidParameter;
3935 for (i=0; i<NUM_CODECS; i++)
3937 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3941 *numDecoders = decoder_count;
3942 *size = decoder_count * sizeof(ImageCodecInfo);
3947 /*****************************************************************************
3948 * GdipGetImageDecoders [GDIPLUS.@]
3950 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
3952 int i, decoder_count=0;
3953 TRACE("%u %u %p\n", numDecoders, size, decoders);
3956 size != numDecoders * sizeof(ImageCodecInfo))
3957 return GenericError;
3959 for (i=0; i<NUM_CODECS; i++)
3961 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3963 if (decoder_count == numDecoders) return GenericError;
3964 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3969 if (decoder_count < numDecoders) return GenericError;
3974 /*****************************************************************************
3975 * GdipGetImageEncodersSize [GDIPLUS.@]
3977 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
3979 int encoder_count=0;
3981 TRACE("%p %p\n", numEncoders, size);
3983 if (!numEncoders || !size)
3984 return InvalidParameter;
3986 for (i=0; i<NUM_CODECS; i++)
3988 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3992 *numEncoders = encoder_count;
3993 *size = encoder_count * sizeof(ImageCodecInfo);
3998 /*****************************************************************************
3999 * GdipGetImageEncoders [GDIPLUS.@]
4001 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4003 int i, encoder_count=0;
4004 TRACE("%u %u %p\n", numEncoders, size, encoders);
4007 size != numEncoders * sizeof(ImageCodecInfo))
4008 return GenericError;
4010 for (i=0; i<NUM_CODECS; i++)
4012 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4014 if (encoder_count == numEncoders) return GenericError;
4015 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4020 if (encoder_count < numEncoders) return GenericError;
4025 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
4026 GDIPCONST CLSID* clsidEncoder, UINT *size)
4030 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
4033 FIXME("not implemented\n");
4037 return NotImplemented;
4040 static PixelFormat get_16bpp_format(HBITMAP hbm)
4046 hdc = CreateCompatibleDC(NULL);
4048 memset(&bmh, 0, sizeof(bmh));
4049 bmh.bV4Size = sizeof(bmh);
4052 bmh.bV4V4Compression = BI_BITFIELDS;
4053 bmh.bV4BitCount = 16;
4055 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4057 if (bmh.bV4RedMask == 0x7c00 &&
4058 bmh.bV4GreenMask == 0x3e0 &&
4059 bmh.bV4BlueMask == 0x1f)
4061 result = PixelFormat16bppRGB555;
4063 else if (bmh.bV4RedMask == 0xf800 &&
4064 bmh.bV4GreenMask == 0x7e0 &&
4065 bmh.bV4BlueMask == 0x1f)
4067 result = PixelFormat16bppRGB565;
4071 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4072 bmh.bV4GreenMask, bmh.bV4BlueMask);
4073 result = PixelFormatUndefined;
4081 /*****************************************************************************
4082 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4084 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4089 BitmapData lockeddata;
4092 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4095 return InvalidParameter;
4097 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4098 return InvalidParameter;
4100 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4101 switch(bm.bmBitsPixel) {
4103 format = PixelFormat1bppIndexed;
4106 format = PixelFormat4bppIndexed;
4109 format = PixelFormat8bppIndexed;
4112 format = get_16bpp_format(hbm);
4113 if (format == PixelFormatUndefined)
4114 return InvalidParameter;
4117 format = PixelFormat24bppRGB;
4120 format = PixelFormat32bppRGB;
4123 format = PixelFormat48bppRGB;
4126 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4127 return InvalidParameter;
4130 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4131 format, NULL, bitmap);
4135 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4136 format, &lockeddata);
4141 for (y=0; y<bm.bmHeight; y++)
4143 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
4144 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
4153 INT src_height, dst_stride;
4156 hdc = CreateCompatibleDC(NULL);
4157 oldhbm = SelectObject(hdc, hbm);
4159 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
4163 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4164 pbmi->bmiHeader.biBitCount = 0;
4166 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4168 src_height = abs(pbmi->bmiHeader.biHeight);
4170 if (pbmi->bmiHeader.biHeight > 0)
4172 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
4173 dst_stride = -lockeddata.Stride;
4177 dst_bits = lockeddata.Scan0;
4178 dst_stride = lockeddata.Stride;
4181 for (y=0; y<src_height; y++)
4183 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
4184 pbmi, DIB_RGB_COLORS);
4190 retval = OutOfMemory;
4192 SelectObject(hdc, oldhbm);
4196 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4199 if (retval == Ok && hpal)
4201 WORD num_palette_entries;
4202 PALETTEENTRY *palette_entries=NULL;
4203 ColorPalette *palette=NULL;
4206 if (!GetObjectW(hpal, sizeof(num_palette_entries), &num_palette_entries))
4207 retval = GenericError;
4211 palette_entries = GdipAlloc(sizeof(PALETTEENTRY) * num_palette_entries);
4212 palette = GdipAlloc(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4214 if (!palette_entries || !palette)
4215 retval = OutOfMemory;
4220 if (!GetPaletteEntries(hpal, 0, num_palette_entries, palette_entries))
4221 retval = GenericError;
4227 palette->Count = num_palette_entries;
4229 for (i=0; i<num_palette_entries; i++)
4231 PALETTEENTRY * entry = &palette_entries[i];
4232 palette->Entries[i] = 0xff000000 | entry->peRed << 16 |
4233 entry->peGreen << 8 | entry->peBlue;
4236 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
4239 GdipFree(palette_entries);
4245 GdipDisposeImage((GpImage*)*bitmap);
4253 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
4255 FIXME("(%p): stub\n", effect);
4256 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
4257 * in Windows's gdiplus */
4258 return NotImplemented;
4261 /*****************************************************************************
4262 * GdipSetEffectParameters [GDIPLUS.@]
4264 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
4265 const VOID *params, const UINT size)
4269 TRACE("(%p,%p,%u)\n", effect, params, size);
4272 FIXME("not implemented\n");
4274 return NotImplemented;
4277 /*****************************************************************************
4278 * GdipGetImageFlags [GDIPLUS.@]
4280 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
4282 TRACE("%p %p\n", image, flags);
4284 if(!image || !flags)
4285 return InvalidParameter;
4287 *flags = image->flags;
4292 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
4294 TRACE("(%d, %p)\n", control, param);
4297 case TestControlForceBilinear:
4299 FIXME("TestControlForceBilinear not handled\n");
4301 case TestControlNoICM:
4303 FIXME("TestControlNoICM not handled\n");
4305 case TestControlGetBuildNumber:
4306 *((DWORD*)param) = 3102;
4313 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
4314 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
4315 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
4316 GpMetafile **metafile)
4318 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4319 frameUnit, debugstr_w(desc), metafile);
4321 return NotImplemented;
4324 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
4325 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
4326 GDIPCONST WCHAR *desc, GpMetafile **metafile)
4328 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4329 frameUnit, debugstr_w(desc), metafile);
4331 return NotImplemented;
4334 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
4336 TRACE("%p\n", image);
4341 /*****************************************************************************
4342 * GdipGetImageThumbnail [GDIPLUS.@]
4344 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
4345 GpImage **ret_image, GetThumbnailImageAbort cb,
4349 GpGraphics *graphics;
4350 UINT srcwidth, srcheight;
4352 TRACE("(%p %u %u %p %p %p)\n",
4353 image, width, height, ret_image, cb, cb_data);
4355 if (!image || !ret_image)
4356 return InvalidParameter;
4358 if (!width) width = 120;
4359 if (!height) height = 120;
4361 GdipGetImageWidth(image, &srcwidth);
4362 GdipGetImageHeight(image, &srcheight);
4364 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB,
4365 NULL, (GpBitmap**)ret_image);
4369 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
4373 stat = GdipDrawImageRectRectI(graphics, image,
4374 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
4377 GdipDeleteGraphics(graphics);
4382 GdipDisposeImage(*ret_image);
4390 /*****************************************************************************
4391 * GdipImageRotateFlip [GDIPLUS.@]
4393 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
4395 GpBitmap *new_bitmap;
4397 int bpp, bytesperpixel;
4398 int rotate_90, flip_x, flip_y;
4399 int src_x_offset, src_y_offset;
4401 UINT x, y, width, height;
4402 BitmapData src_lock, dst_lock;
4405 TRACE("(%p, %u)\n", image, type);
4408 return InvalidParameter;
4411 flip_x = (type&6) == 2 || (type&6) == 4;
4412 flip_y = (type&3) == 1 || (type&3) == 2;
4414 if (image->type != ImageTypeBitmap)
4416 FIXME("Not implemented for type %i\n", image->type);
4417 return NotImplemented;
4420 bitmap = (GpBitmap*)image;
4421 bpp = PIXELFORMATBPP(bitmap->format);
4425 FIXME("Not implemented for %i bit images\n", bpp);
4426 return NotImplemented;
4431 width = bitmap->height;
4432 height = bitmap->width;
4436 width = bitmap->width;
4437 height = bitmap->height;
4440 bytesperpixel = bpp/8;
4442 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
4447 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
4451 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
4455 LPBYTE src_row, src_pixel;
4456 LPBYTE dst_row, dst_pixel;
4458 src_origin = src_lock.Scan0;
4459 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
4460 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
4464 if (flip_y) src_x_offset = -src_lock.Stride;
4465 else src_x_offset = src_lock.Stride;
4466 if (flip_x) src_y_offset = -bytesperpixel;
4467 else src_y_offset = bytesperpixel;
4471 if (flip_x) src_x_offset = -bytesperpixel;
4472 else src_x_offset = bytesperpixel;
4473 if (flip_y) src_y_offset = -src_lock.Stride;
4474 else src_y_offset = src_lock.Stride;
4477 src_row = src_origin;
4478 dst_row = dst_lock.Scan0;
4479 for (y=0; y<height; y++)
4481 src_pixel = src_row;
4482 dst_pixel = dst_row;
4483 for (x=0; x<width; x++)
4485 /* FIXME: This could probably be faster without memcpy. */
4486 memcpy(dst_pixel, src_pixel, bytesperpixel);
4487 dst_pixel += bytesperpixel;
4488 src_pixel += src_x_offset;
4490 src_row += src_y_offset;
4491 dst_row += dst_lock.Stride;
4494 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
4497 GdipBitmapUnlockBits(bitmap, &src_lock);
4501 move_bitmap(bitmap, new_bitmap, FALSE);
4503 GdipDisposeImage((GpImage*)new_bitmap);
4508 /*****************************************************************************
4509 * GdipConvertToEmfPlusToFile [GDIPLUS.@]
4512 GpStatus WINGDIPAPI GdipConvertToEmfPlusToFile(const GpGraphics* refGraphics,
4513 GpMetafile* metafile, BOOL* conversionSuccess,
4514 const WCHAR* filename, EmfType emfType,
4515 const WCHAR* description, GpMetafile** out_metafile)
4517 FIXME("stub: %p, %p, %p, %p, %u, %p, %p\n", refGraphics, metafile, conversionSuccess, filename, emfType, description, out_metafile);
4518 return NotImplemented;