2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
43 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
47 const WICPixelFormatGUID *wic_format;
48 PixelFormat gdip_format;
49 /* predefined palette type to use for pixel format conversions */
50 WICBitmapPaletteType palette_type;
53 { &GUID_WICPixelFormatBlackWhite, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
54 { &GUID_WICPixelFormat1bppIndexed, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
55 { &GUID_WICPixelFormat8bppGray, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedGray256 },
56 { &GUID_WICPixelFormat8bppIndexed, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedHalftone256 },
57 { &GUID_WICPixelFormat16bppBGR555, PixelFormat16bppRGB555, WICBitmapPaletteTypeFixedHalftone256 },
58 { &GUID_WICPixelFormat24bppBGR, PixelFormat24bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
59 { &GUID_WICPixelFormat32bppBGR, PixelFormat32bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
60 { &GUID_WICPixelFormat32bppBGRA, PixelFormat32bppARGB, WICBitmapPaletteTypeFixedHalftone256 },
61 { &GUID_WICPixelFormat32bppPBGRA, PixelFormat32bppPARGB, WICBitmapPaletteTypeFixedHalftone256 },
65 static ColorPalette *get_palette(IWICBitmapFrameDecode *frame, WICBitmapPaletteType palette_type)
68 IWICImagingFactory *factory;
69 IWICPalette *wic_palette;
70 ColorPalette *palette = NULL;
72 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
73 &IID_IWICImagingFactory, (void **)&factory);
74 if (hr != S_OK) return NULL;
76 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
79 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
81 hr = IWICBitmapFrameDecode_CopyPalette(frame, wic_palette);
84 TRACE("using predefined palette %#x\n", palette_type);
85 hr = IWICPalette_InitializePredefined(wic_palette, palette_type, FALSE);
92 IWICPalette_IsBlackWhite(wic_palette, &mono);
93 IWICPalette_IsGrayscale(wic_palette, &gray);
95 IWICPalette_GetColorCount(wic_palette, &count);
96 palette = HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(UINT) + count * sizeof(ARGB));
97 IWICPalette_GetColors(wic_palette, count, palette->Entries, &palette->Count);
102 palette->Flags = PaletteFlagsGrayScale;
104 palette->Flags = PaletteFlagsHalftone;
106 IWICPalette_Release(wic_palette);
108 IWICImagingFactory_Release(factory);
112 static INT ipicture_pixel_height(IPicture *pic)
115 OLE_YSIZE_HIMETRIC y;
117 IPicture_get_Height(pic, &y);
121 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
122 ReleaseDC(0, hdcref);
127 static INT ipicture_pixel_width(IPicture *pic)
130 OLE_XSIZE_HIMETRIC x;
132 IPicture_get_Width(pic, &x);
136 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
138 ReleaseDC(0, hdcref);
143 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
144 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
146 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
148 * Note: According to Jose Roca's GDI+ docs, this function is not
149 * implemented in Windows's GDI+.
151 return NotImplemented;
154 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
155 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
156 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
158 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
160 * Note: According to Jose Roca's GDI+ docs, this function is not
161 * implemented in Windows's GDI+.
163 return NotImplemented;
166 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
168 *index = (row[x/8]>>(7-x%8)) & 1;
171 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
174 *index = row[x/2]&0xf;
176 *index = row[x/2]>>4;
179 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
184 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
185 const BYTE *row, UINT x)
187 *r = *g = *b = row[x*2+1];
191 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
192 const BYTE *row, UINT x)
194 WORD pixel = *((const WORD*)(row)+x);
195 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
196 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
197 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
201 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
202 const BYTE *row, UINT x)
204 WORD pixel = *((const WORD*)(row)+x);
205 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
206 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
207 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
211 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
212 const BYTE *row, UINT x)
214 WORD pixel = *((const WORD*)(row)+x);
215 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
216 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
217 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
218 if ((pixel&0x8000) == 0x8000)
224 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
225 const BYTE *row, UINT x)
233 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
234 const BYTE *row, UINT x)
242 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
243 const BYTE *row, UINT x)
251 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
252 const BYTE *row, UINT x)
259 *r = row[x*4+2] * 255 / *a;
260 *g = row[x*4+1] * 255 / *a;
261 *b = row[x*4] * 255 / *a;
265 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
266 const BYTE *row, UINT x)
274 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
275 const BYTE *row, UINT x)
283 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
284 const BYTE *row, UINT x)
291 *r = row[x*8+5] * 255 / *a;
292 *g = row[x*8+3] * 255 / *a;
293 *b = row[x*8+1] * 255 / *a;
297 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
303 TRACE("%p %d %d %p\n", bitmap, x, y, color);
305 if(!bitmap || !color ||
306 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
307 return InvalidParameter;
309 row = bitmap->bits+bitmap->stride*y;
311 switch (bitmap->format)
313 case PixelFormat1bppIndexed:
314 getpixel_1bppIndexed(&index,row,x);
316 case PixelFormat4bppIndexed:
317 getpixel_4bppIndexed(&index,row,x);
319 case PixelFormat8bppIndexed:
320 getpixel_8bppIndexed(&index,row,x);
322 case PixelFormat16bppGrayScale:
323 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
325 case PixelFormat16bppRGB555:
326 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
328 case PixelFormat16bppRGB565:
329 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
331 case PixelFormat16bppARGB1555:
332 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
334 case PixelFormat24bppRGB:
335 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
337 case PixelFormat32bppRGB:
338 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
340 case PixelFormat32bppARGB:
341 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
343 case PixelFormat32bppPARGB:
344 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
346 case PixelFormat48bppRGB:
347 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
349 case PixelFormat64bppARGB:
350 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
352 case PixelFormat64bppPARGB:
353 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
356 FIXME("not implemented for format 0x%x\n", bitmap->format);
357 return NotImplemented;
360 if (bitmap->format & PixelFormatIndexed)
361 *color = bitmap->image.palette->Entries[index];
363 *color = a<<24|r<<16|g<<8|b;
368 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
371 int best_distance = 0x7fff;
375 if (!palette) return 0;
376 /* This algorithm scans entire palette,
377 computes difference from desired color (all color components have equal weight)
378 and returns the index of color with least difference.
380 Note: Maybe it could be replaced with a better algorithm for better image quality
381 and performance, though better algorithm would probably need some pre-built lookup
382 tables and thus may actually be slower if this method is called only few times per
385 for(i=0;i<palette->Count;i++) {
386 ARGB color=palette->Entries[i];
387 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
388 if (distance<best_distance) {
389 best_distance=distance;
396 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
397 BYTE *row, UINT x, ColorPalette *palette)
399 BYTE index = get_palette_index(r,g,b,a,palette);
403 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
404 BYTE *row, UINT x, ColorPalette *palette)
406 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
409 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
410 BYTE *row, UINT x, ColorPalette *palette)
413 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
415 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
418 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
421 *((WORD*)(row)+x) = (r+g+b)*85;
424 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
427 *((WORD*)(row)+x) = (r<<7&0x7c00)|
432 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
435 *((WORD*)(row)+x) = (r<<8&0xf800)|
440 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
443 *((WORD*)(row)+x) = (a<<8&0x8000)|
449 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
457 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
460 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
463 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
466 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
469 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
475 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
478 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
481 row[x*6+5] = row[x*6+4] = r;
482 row[x*6+3] = row[x*6+2] = g;
483 row[x*6+1] = row[x*6] = b;
486 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
489 UINT64 a64=a, r64=r, g64=g, b64=b;
490 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
493 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
496 UINT64 a64, r64, g64, b64;
501 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
504 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
509 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
511 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
512 return InvalidParameter;
519 row = bitmap->bits + bitmap->stride * y;
521 switch (bitmap->format)
523 case PixelFormat16bppGrayScale:
524 setpixel_16bppGrayScale(r,g,b,a,row,x);
526 case PixelFormat16bppRGB555:
527 setpixel_16bppRGB555(r,g,b,a,row,x);
529 case PixelFormat16bppRGB565:
530 setpixel_16bppRGB565(r,g,b,a,row,x);
532 case PixelFormat16bppARGB1555:
533 setpixel_16bppARGB1555(r,g,b,a,row,x);
535 case PixelFormat24bppRGB:
536 setpixel_24bppRGB(r,g,b,a,row,x);
538 case PixelFormat32bppRGB:
539 setpixel_32bppRGB(r,g,b,a,row,x);
541 case PixelFormat32bppARGB:
542 setpixel_32bppARGB(r,g,b,a,row,x);
544 case PixelFormat32bppPARGB:
545 setpixel_32bppPARGB(r,g,b,a,row,x);
547 case PixelFormat48bppRGB:
548 setpixel_48bppRGB(r,g,b,a,row,x);
550 case PixelFormat64bppARGB:
551 setpixel_64bppARGB(r,g,b,a,row,x);
553 case PixelFormat64bppPARGB:
554 setpixel_64bppPARGB(r,g,b,a,row,x);
556 case PixelFormat8bppIndexed:
557 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
559 case PixelFormat4bppIndexed:
560 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
562 case PixelFormat1bppIndexed:
563 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
566 FIXME("not implemented for format 0x%x\n", bitmap->format);
567 return NotImplemented;
573 GpStatus convert_pixels(INT width, INT height,
574 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
575 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
576 ColorPalette *palette)
580 if (src_format == dst_format ||
581 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
583 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
584 for (y=0; y<height; y++)
585 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
589 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
590 for (x=0; x<width; x++) \
591 for (y=0; y<height; y++) { \
594 BYTE *color = (BYTE *)&argb; \
595 getpixel_function(&index, src_bits+src_stride*y, x); \
596 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
597 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
602 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
603 for (x=0; x<width; x++) \
604 for (y=0; y<height; y++) { \
606 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
607 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
612 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
613 for (x=0; x<width; x++) \
614 for (y=0; y<height; y++) { \
616 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
617 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
624 case PixelFormat1bppIndexed:
627 case PixelFormat16bppGrayScale:
628 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
629 case PixelFormat16bppRGB555:
630 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
631 case PixelFormat16bppRGB565:
632 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
633 case PixelFormat16bppARGB1555:
634 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
635 case PixelFormat24bppRGB:
636 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
637 case PixelFormat32bppRGB:
638 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
639 case PixelFormat32bppARGB:
640 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
641 case PixelFormat32bppPARGB:
642 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
643 case PixelFormat48bppRGB:
644 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
645 case PixelFormat64bppARGB:
646 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
651 case PixelFormat4bppIndexed:
654 case PixelFormat16bppGrayScale:
655 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
656 case PixelFormat16bppRGB555:
657 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
658 case PixelFormat16bppRGB565:
659 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
660 case PixelFormat16bppARGB1555:
661 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
662 case PixelFormat24bppRGB:
663 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
664 case PixelFormat32bppRGB:
665 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
666 case PixelFormat32bppARGB:
667 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
668 case PixelFormat32bppPARGB:
669 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
670 case PixelFormat48bppRGB:
671 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
672 case PixelFormat64bppARGB:
673 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
678 case PixelFormat8bppIndexed:
681 case PixelFormat16bppGrayScale:
682 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
683 case PixelFormat16bppRGB555:
684 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
685 case PixelFormat16bppRGB565:
686 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
687 case PixelFormat16bppARGB1555:
688 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
689 case PixelFormat24bppRGB:
690 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
691 case PixelFormat32bppRGB:
692 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
693 case PixelFormat32bppARGB:
694 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
695 case PixelFormat32bppPARGB:
696 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
697 case PixelFormat48bppRGB:
698 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
699 case PixelFormat64bppARGB:
700 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
705 case PixelFormat16bppGrayScale:
708 case PixelFormat1bppIndexed:
709 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
710 case PixelFormat8bppIndexed:
711 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
712 case PixelFormat16bppRGB555:
713 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
714 case PixelFormat16bppRGB565:
715 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
716 case PixelFormat16bppARGB1555:
717 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
718 case PixelFormat24bppRGB:
719 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
720 case PixelFormat32bppRGB:
721 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
722 case PixelFormat32bppARGB:
723 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
724 case PixelFormat32bppPARGB:
725 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
726 case PixelFormat48bppRGB:
727 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
728 case PixelFormat64bppARGB:
729 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
734 case PixelFormat16bppRGB555:
737 case PixelFormat1bppIndexed:
738 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
739 case PixelFormat8bppIndexed:
740 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
741 case PixelFormat16bppGrayScale:
742 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
743 case PixelFormat16bppRGB565:
744 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
745 case PixelFormat16bppARGB1555:
746 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
747 case PixelFormat24bppRGB:
748 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
749 case PixelFormat32bppRGB:
750 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
751 case PixelFormat32bppARGB:
752 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
753 case PixelFormat32bppPARGB:
754 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
755 case PixelFormat48bppRGB:
756 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
757 case PixelFormat64bppARGB:
758 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
763 case PixelFormat16bppRGB565:
766 case PixelFormat1bppIndexed:
767 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
768 case PixelFormat8bppIndexed:
769 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
770 case PixelFormat16bppGrayScale:
771 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
772 case PixelFormat16bppRGB555:
773 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
774 case PixelFormat16bppARGB1555:
775 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
776 case PixelFormat24bppRGB:
777 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
778 case PixelFormat32bppRGB:
779 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
780 case PixelFormat32bppARGB:
781 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
782 case PixelFormat32bppPARGB:
783 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
784 case PixelFormat48bppRGB:
785 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
786 case PixelFormat64bppARGB:
787 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
792 case PixelFormat16bppARGB1555:
795 case PixelFormat1bppIndexed:
796 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
797 case PixelFormat8bppIndexed:
798 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
799 case PixelFormat16bppGrayScale:
800 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
801 case PixelFormat16bppRGB555:
802 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
803 case PixelFormat16bppRGB565:
804 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
805 case PixelFormat24bppRGB:
806 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
807 case PixelFormat32bppRGB:
808 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
809 case PixelFormat32bppARGB:
810 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
811 case PixelFormat32bppPARGB:
812 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
813 case PixelFormat48bppRGB:
814 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
815 case PixelFormat64bppARGB:
816 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
821 case PixelFormat24bppRGB:
824 case PixelFormat1bppIndexed:
825 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
826 case PixelFormat8bppIndexed:
827 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
828 case PixelFormat16bppGrayScale:
829 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
830 case PixelFormat16bppRGB555:
831 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
832 case PixelFormat16bppRGB565:
833 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
834 case PixelFormat16bppARGB1555:
835 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
836 case PixelFormat32bppRGB:
837 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
838 case PixelFormat32bppARGB:
839 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
840 case PixelFormat32bppPARGB:
841 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
842 case PixelFormat48bppRGB:
843 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
844 case PixelFormat64bppARGB:
845 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
850 case PixelFormat32bppRGB:
853 case PixelFormat1bppIndexed:
854 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
855 case PixelFormat8bppIndexed:
856 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
857 case PixelFormat16bppGrayScale:
858 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
859 case PixelFormat16bppRGB555:
860 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
861 case PixelFormat16bppRGB565:
862 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
863 case PixelFormat16bppARGB1555:
864 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
865 case PixelFormat24bppRGB:
866 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
867 case PixelFormat32bppARGB:
868 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
869 case PixelFormat32bppPARGB:
870 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
871 case PixelFormat48bppRGB:
872 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
873 case PixelFormat64bppARGB:
874 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
879 case PixelFormat32bppARGB:
882 case PixelFormat1bppIndexed:
883 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
884 case PixelFormat8bppIndexed:
885 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
886 case PixelFormat16bppGrayScale:
887 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
888 case PixelFormat16bppRGB555:
889 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
890 case PixelFormat16bppRGB565:
891 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
892 case PixelFormat16bppARGB1555:
893 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
894 case PixelFormat24bppRGB:
895 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
896 case PixelFormat32bppPARGB:
897 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
899 case PixelFormat48bppRGB:
900 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
901 case PixelFormat64bppARGB:
902 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
907 case PixelFormat32bppPARGB:
910 case PixelFormat1bppIndexed:
911 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
912 case PixelFormat8bppIndexed:
913 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
914 case PixelFormat16bppGrayScale:
915 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
916 case PixelFormat16bppRGB555:
917 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
918 case PixelFormat16bppRGB565:
919 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
920 case PixelFormat16bppARGB1555:
921 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
922 case PixelFormat24bppRGB:
923 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
924 case PixelFormat32bppRGB:
925 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
926 case PixelFormat32bppARGB:
927 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
928 case PixelFormat48bppRGB:
929 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
930 case PixelFormat64bppARGB:
931 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
936 case PixelFormat48bppRGB:
939 case PixelFormat1bppIndexed:
940 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
941 case PixelFormat8bppIndexed:
942 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
943 case PixelFormat16bppGrayScale:
944 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
945 case PixelFormat16bppRGB555:
946 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
947 case PixelFormat16bppRGB565:
948 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
949 case PixelFormat16bppARGB1555:
950 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
951 case PixelFormat24bppRGB:
952 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
953 case PixelFormat32bppRGB:
954 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
955 case PixelFormat32bppARGB:
956 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
957 case PixelFormat32bppPARGB:
958 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
959 case PixelFormat64bppARGB:
960 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
965 case PixelFormat64bppARGB:
968 case PixelFormat1bppIndexed:
969 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
970 case PixelFormat8bppIndexed:
971 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
972 case PixelFormat16bppGrayScale:
973 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
974 case PixelFormat16bppRGB555:
975 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
976 case PixelFormat16bppRGB565:
977 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
978 case PixelFormat16bppARGB1555:
979 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
980 case PixelFormat24bppRGB:
981 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
982 case PixelFormat32bppRGB:
983 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
984 case PixelFormat32bppARGB:
985 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
986 case PixelFormat32bppPARGB:
987 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
988 case PixelFormat48bppRGB:
989 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
994 case PixelFormat64bppPARGB:
997 case PixelFormat1bppIndexed:
998 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
999 case PixelFormat8bppIndexed:
1000 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
1001 case PixelFormat16bppGrayScale:
1002 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
1003 case PixelFormat16bppRGB555:
1004 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
1005 case PixelFormat16bppRGB565:
1006 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
1007 case PixelFormat16bppARGB1555:
1008 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
1009 case PixelFormat24bppRGB:
1010 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
1011 case PixelFormat32bppRGB:
1012 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
1013 case PixelFormat32bppARGB:
1014 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
1015 case PixelFormat32bppPARGB:
1016 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1017 case PixelFormat48bppRGB:
1018 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1019 case PixelFormat64bppARGB:
1020 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1029 #undef convert_indexed_to_rgb
1030 #undef convert_rgb_to_rgb
1032 return NotImplemented;
1035 /* This function returns a pointer to an array of pixels that represents the
1036 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1037 * flags. It is correct behavior that a user who calls this function with write
1038 * privileges can write to the whole bitmap (not just the area in rect).
1040 * FIXME: only used portion of format is bits per pixel. */
1041 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1042 UINT flags, PixelFormat format, BitmapData* lockeddata)
1044 INT bitspp = PIXELFORMATBPP(format);
1045 GpRect act_rect; /* actual rect to be used */
1048 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1050 if(!lockeddata || !bitmap)
1051 return InvalidParameter;
1054 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1055 (rect->Y + rect->Height > bitmap->height) || !flags)
1056 return InvalidParameter;
1061 act_rect.X = act_rect.Y = 0;
1062 act_rect.Width = bitmap->width;
1063 act_rect.Height = bitmap->height;
1066 if(bitmap->lockmode)
1068 WARN("bitmap is already locked and cannot be locked again\n");
1072 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1074 /* no conversion is necessary; just use the bits directly */
1075 lockeddata->Width = act_rect.Width;
1076 lockeddata->Height = act_rect.Height;
1077 lockeddata->PixelFormat = format;
1078 lockeddata->Reserved = flags;
1079 lockeddata->Stride = bitmap->stride;
1080 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1081 bitmap->stride * act_rect.Y;
1083 bitmap->lockmode = flags | ImageLockModeRead;
1089 /* Make sure we can convert to the requested format. */
1090 if (flags & ImageLockModeRead)
1092 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
1093 if (stat == NotImplemented)
1095 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1096 return NotImplemented;
1100 /* If we're opening for writing, make sure we'll be able to write back in
1101 * the original format. */
1102 if (flags & ImageLockModeWrite)
1104 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
1105 if (stat == NotImplemented)
1107 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1108 return NotImplemented;
1112 lockeddata->Width = act_rect.Width;
1113 lockeddata->Height = act_rect.Height;
1114 lockeddata->PixelFormat = format;
1115 lockeddata->Reserved = flags;
1117 if(!(flags & ImageLockModeUserInputBuf))
1119 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1121 bitmap->bitmapbits = GdipAlloc(lockeddata->Stride * act_rect.Height);
1123 if (!bitmap->bitmapbits) return OutOfMemory;
1125 lockeddata->Scan0 = bitmap->bitmapbits;
1128 if (flags & ImageLockModeRead)
1132 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1134 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1138 stat = convert_pixels(act_rect.Width, act_rect.Height,
1139 lockeddata->Stride, lockeddata->Scan0, format,
1141 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1142 bitmap->format, bitmap->image.palette);
1146 GdipFree(bitmap->bitmapbits);
1147 bitmap->bitmapbits = NULL;
1152 bitmap->lockmode = flags | ImageLockModeRead;
1154 bitmap->lockx = act_rect.X;
1155 bitmap->locky = act_rect.Y;
1160 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1162 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1164 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1165 return InvalidParameter;
1167 bitmap->image.xres = xdpi;
1168 bitmap->image.yres = ydpi;
1173 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1174 BitmapData* lockeddata)
1179 TRACE("(%p,%p)\n", bitmap, lockeddata);
1181 if(!bitmap || !lockeddata)
1182 return InvalidParameter;
1184 if(!bitmap->lockmode)
1187 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1188 if(!(--bitmap->numlocks))
1189 bitmap->lockmode = 0;
1191 GdipFree(bitmap->bitmapbits);
1192 bitmap->bitmapbits = NULL;
1196 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1198 /* we passed a direct reference; no need to do anything */
1199 bitmap->lockmode = 0;
1200 bitmap->numlocks = 0;
1204 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1206 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1210 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1212 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1214 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1218 ERR("failed to convert pixels; this should never happen\n");
1221 GdipFree(bitmap->bitmapbits);
1222 bitmap->bitmapbits = NULL;
1223 bitmap->lockmode = 0;
1224 bitmap->numlocks = 0;
1229 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1230 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1232 BitmapData lockeddata_src, lockeddata_dst;
1238 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1240 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1242 x + width > srcBitmap->width || y + height > srcBitmap->height)
1244 TRACE("<-- InvalidParameter\n");
1245 return InvalidParameter;
1248 if (format == PixelFormatDontCare)
1249 format = srcBitmap->format;
1251 area.X = gdip_round(x);
1252 area.Y = gdip_round(y);
1253 area.Width = gdip_round(width);
1254 area.Height = gdip_round(height);
1256 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
1258 if (stat != Ok) return stat;
1260 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1261 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
1264 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
1265 lockeddata_src.PixelFormat, &lockeddata_dst);
1269 /* copy the image data */
1270 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1271 for (i=0; i<lockeddata_src.Height; i++)
1272 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1273 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1276 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
1280 GdipDisposeImage((GpImage*)*dstBitmap);
1283 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
1293 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1294 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1296 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1298 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1301 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1303 GpStatus stat = GenericError;
1305 TRACE("%p, %p\n", image, cloneImage);
1307 if (!image || !cloneImage)
1308 return InvalidParameter;
1317 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1319 return GenericError;
1321 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1324 WARN("Failed to save image on stream\n");
1328 /* Set seek pointer back to the beginning of the picture */
1330 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1334 stat = GdipLoadImageFromStream(stream, cloneImage);
1335 if (stat != Ok) WARN("Failed to load image from stream\n");
1338 IStream_Release(stream);
1341 else if (image->type == ImageTypeBitmap)
1343 GpBitmap *bitmap = (GpBitmap*)image;
1344 BitmapData lockeddata_src, lockeddata_dst;
1348 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
1350 if (stat != Ok) return stat;
1352 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1353 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
1356 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
1357 lockeddata_src.PixelFormat, &lockeddata_dst);
1361 /* copy the image data */
1362 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1363 for (i=0; i<lockeddata_src.Height; i++)
1364 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1365 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1368 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
1372 GdipDisposeImage(*cloneImage);
1375 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
1381 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
1385 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
1387 GpMetafile *result, *metafile;
1389 metafile = (GpMetafile*)image;
1391 result = GdipAlloc(sizeof(*result));
1395 result->image.type = ImageTypeMetafile;
1396 result->image.format = image->format;
1397 result->image.flags = image->flags;
1398 result->image.frame_count = 1;
1399 result->image.xres = image->xres;
1400 result->image.yres = image->yres;
1401 result->bounds = metafile->bounds;
1402 result->unit = metafile->unit;
1403 result->metafile_type = metafile->metafile_type;
1404 result->hemf = CopyEnhMetaFileW(metafile->hemf, NULL);
1412 *cloneImage = &result->image;
1417 WARN("GpImage with no image data (metafile in wrong state?)\n");
1418 return InvalidParameter;
1422 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1428 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1430 if(!filename || !bitmap)
1431 return InvalidParameter;
1433 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1438 stat = GdipCreateBitmapFromStream(stream, bitmap);
1440 IStream_Release(stream);
1445 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1446 VOID *bits, GpBitmap **bitmap)
1448 DWORD height, stride;
1451 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1453 if (!info || !bits || !bitmap)
1454 return InvalidParameter;
1456 height = abs(info->bmiHeader.biHeight);
1457 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1459 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1461 bits = (BYTE*)bits + (height - 1) * stride;
1465 switch(info->bmiHeader.biBitCount) {
1467 format = PixelFormat1bppIndexed;
1470 format = PixelFormat4bppIndexed;
1473 format = PixelFormat8bppIndexed;
1476 format = PixelFormat16bppRGB555;
1479 format = PixelFormat24bppRGB;
1482 format = PixelFormat32bppRGB;
1485 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1487 return InvalidParameter;
1490 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1496 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1499 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1501 return GdipCreateBitmapFromFile(filename, bitmap);
1504 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1505 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1508 GpStatus stat = InvalidParameter;
1510 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1512 if(!lpBitmapName || !bitmap)
1513 return InvalidParameter;
1516 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1517 LR_CREATEDIBSECTION);
1520 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1527 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1528 HBITMAP* hbmReturn, ARGB background)
1533 BITMAPINFOHEADER bih;
1535 BitmapData lockeddata;
1536 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1538 if (!bitmap || !hbmReturn) return InvalidParameter;
1540 GdipGetImageWidth((GpImage*)bitmap, &width);
1541 GdipGetImageHeight((GpImage*)bitmap, &height);
1543 bih.biSize = sizeof(bih);
1544 bih.biWidth = width;
1545 bih.biHeight = height;
1547 bih.biBitCount = 32;
1548 bih.biCompression = BI_RGB;
1549 bih.biSizeImage = 0;
1550 bih.biXPelsPerMeter = 0;
1551 bih.biYPelsPerMeter = 0;
1553 bih.biClrImportant = 0;
1555 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1559 lockeddata.Stride = -width * 4;
1560 lockeddata.Scan0 = bits + (width * 4 * (height - 1));
1562 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead|ImageLockModeUserInputBuf,
1563 PixelFormat32bppPARGB, &lockeddata);
1566 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1569 stat = GenericError;
1571 if (stat != Ok && result)
1573 DeleteObject(result);
1577 *hbmReturn = result;
1582 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1583 GpMetafile* metafile, BOOL* succ, EmfType emfType,
1584 const WCHAR* description, GpMetafile** out_metafile)
1588 TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1589 debugstr_w(description), out_metafile);
1591 if(!ref || !metafile || !out_metafile)
1592 return InvalidParameter;
1595 *out_metafile = NULL;
1598 FIXME("not implemented\n");
1600 return NotImplemented;
1603 /* FIXME: this should create a bitmap in the given size with the attributes
1604 * (resolution etc.) of the graphics object */
1605 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1606 GpGraphics* target, GpBitmap** bitmap)
1611 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1613 if(!target || !bitmap)
1614 return InvalidParameter;
1617 FIXME("hacked stub\n");
1619 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1625 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1631 UINT width, height, stride;
1633 BitmapData lockeddata;
1637 BITMAPINFOHEADER bih;
1642 TRACE("%p, %p\n", hicon, bitmap);
1644 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1645 return InvalidParameter;
1647 /* get the size of the icon */
1648 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1650 DeleteObject(iinfo.hbmColor);
1651 DeleteObject(iinfo.hbmMask);
1652 return GenericError;
1656 height = iinfo.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
1659 stat = GdipCreateBitmapFromScan0(width, height, stride, PixelFormat32bppARGB, NULL, bitmap);
1661 DeleteObject(iinfo.hbmColor);
1662 DeleteObject(iinfo.hbmMask);
1669 rect.Height = height;
1671 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1673 DeleteObject(iinfo.hbmColor);
1674 DeleteObject(iinfo.hbmMask);
1675 GdipDisposeImage((GpImage*)*bitmap);
1679 bih.biSize = sizeof(bih);
1680 bih.biWidth = width;
1681 bih.biHeight = iinfo.hbmColor ? -height: -height * 2;
1683 bih.biBitCount = 32;
1684 bih.biCompression = BI_RGB;
1685 bih.biSizeImage = 0;
1686 bih.biXPelsPerMeter = 0;
1687 bih.biYPelsPerMeter = 0;
1689 bih.biClrImportant = 0;
1691 screendc = GetDC(0);
1694 GetDIBits(screendc, iinfo.hbmColor, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1696 if (bm.bmBitsPixel == 32)
1700 /* If any pixel has a non-zero alpha, ignore hbmMask */
1701 src = (DWORD*)lockeddata.Scan0;
1702 for (x=0; x<width && !has_alpha; x++)
1703 for (y=0; y<height && !has_alpha; y++)
1704 if ((*src++ & 0xff000000) != 0)
1707 else has_alpha = FALSE;
1711 GetDIBits(screendc, iinfo.hbmMask, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1719 BYTE *bits = HeapAlloc(GetProcessHeap(), 0, height * stride);
1721 /* read alpha data from the mask */
1723 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1725 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1728 dst_row = lockeddata.Scan0;
1729 for (y=0; y<height; y++)
1731 dst = (DWORD*)dst_row;
1732 for (x=0; x<height; x++)
1734 DWORD src_value = *src++;
1738 *dst++ |= 0xff000000;
1740 dst_row += lockeddata.Stride;
1743 HeapFree(GetProcessHeap(), 0, bits);
1747 /* set constant alpha of 255 */
1748 dst_row = lockeddata.Scan0;
1749 for (y=0; y<height; y++)
1751 dst = (DWORD*)dst_row;
1752 for (x=0; x<height; x++)
1753 *dst++ |= 0xff000000;
1754 dst_row += lockeddata.Stride;
1759 ReleaseDC(0, screendc);
1761 DeleteObject(iinfo.hbmColor);
1762 DeleteObject(iinfo.hbmMask);
1764 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1769 static void generate_halftone_palette(ARGB *entries, UINT count)
1771 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1774 for (i=0; i<8 && i<count; i++)
1776 entries[i] = 0xff000000;
1777 if (i&1) entries[i] |= 0x800000;
1778 if (i&2) entries[i] |= 0x8000;
1779 if (i&4) entries[i] |= 0x80;
1783 entries[i] = 0xffc0c0c0;
1785 for (i=9; i<16 && i<count; i++)
1787 entries[i] = 0xff000000;
1788 if (i&1) entries[i] |= 0xff0000;
1789 if (i&2) entries[i] |= 0xff00;
1790 if (i&4) entries[i] |= 0xff;
1793 for (i=16; i<40 && i<count; i++)
1798 for (i=40; i<256 && i<count; i++)
1800 entries[i] = 0xff000000;
1801 entries[i] |= halftone_values[(i-40)%6];
1802 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1803 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1807 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1809 HDC screendc = GetDC(0);
1811 if (!screendc) return GenericError;
1813 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1814 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1816 ReleaseDC(0, screendc);
1821 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1822 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1825 HBITMAP hbitmap=NULL;
1826 INT row_size, dib_stride;
1827 BYTE *bits=NULL, *own_bits=NULL;
1831 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1833 if (!bitmap) return InvalidParameter;
1835 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1837 return InvalidParameter;
1840 if(scan0 && !stride)
1841 return InvalidParameter;
1843 stat = get_screen_resolution(&xres, &yres);
1844 if (stat != Ok) return stat;
1846 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1847 dib_stride = (row_size + 3) & ~3;
1850 stride = dib_stride;
1852 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1854 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1858 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1859 pbmi->bmiHeader.biWidth = width;
1860 pbmi->bmiHeader.biHeight = -height;
1861 pbmi->bmiHeader.biPlanes = 1;
1862 /* FIXME: use the rest of the data from format */
1863 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1864 pbmi->bmiHeader.biCompression = BI_RGB;
1865 pbmi->bmiHeader.biSizeImage = 0;
1866 pbmi->bmiHeader.biXPelsPerMeter = 0;
1867 pbmi->bmiHeader.biYPelsPerMeter = 0;
1868 pbmi->bmiHeader.biClrUsed = 0;
1869 pbmi->bmiHeader.biClrImportant = 0;
1871 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1875 if (!hbitmap) return GenericError;
1877 stride = dib_stride;
1881 /* Not a GDI format; don't try to make an HBITMAP. */
1886 INT size = abs(stride) * height;
1888 own_bits = bits = GdipAlloc(size);
1889 if (!own_bits) return OutOfMemory;
1892 bits += stride * (1 - height);
1896 *bitmap = GdipAlloc(sizeof(GpBitmap));
1899 DeleteObject(hbitmap);
1904 (*bitmap)->image.type = ImageTypeBitmap;
1905 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1906 (*bitmap)->image.flags = ImageFlagsNone;
1907 (*bitmap)->image.frame_count = 1;
1908 (*bitmap)->image.current_frame = 0;
1909 (*bitmap)->image.palette = NULL;
1910 (*bitmap)->image.xres = xres;
1911 (*bitmap)->image.yres = yres;
1912 (*bitmap)->width = width;
1913 (*bitmap)->height = height;
1914 (*bitmap)->format = format;
1915 (*bitmap)->image.picture = NULL;
1916 (*bitmap)->image.stream = NULL;
1917 (*bitmap)->hbitmap = hbitmap;
1918 (*bitmap)->hdc = NULL;
1919 (*bitmap)->bits = bits;
1920 (*bitmap)->stride = stride;
1921 (*bitmap)->own_bits = own_bits;
1922 (*bitmap)->metadata_reader = NULL;
1923 (*bitmap)->prop_count = 0;
1924 (*bitmap)->prop_item = NULL;
1926 /* set format-related flags */
1927 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1928 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1930 if (format == PixelFormat1bppIndexed ||
1931 format == PixelFormat4bppIndexed ||
1932 format == PixelFormat8bppIndexed)
1934 (*bitmap)->image.palette = GdipAlloc(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1936 if (!(*bitmap)->image.palette)
1938 GdipDisposeImage(&(*bitmap)->image);
1943 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1945 if (format == PixelFormat1bppIndexed)
1947 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1948 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1949 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1953 if (format == PixelFormat8bppIndexed)
1954 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1956 generate_halftone_palette((*bitmap)->image.palette->Entries,
1957 (*bitmap)->image.palette->Count);
1961 TRACE("<-- %p\n", *bitmap);
1966 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1971 TRACE("%p %p\n", stream, bitmap);
1973 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1978 if((*bitmap)->image.type != ImageTypeBitmap){
1979 GdipDisposeImage(&(*bitmap)->image);
1981 return GenericError; /* FIXME: what error to return? */
1988 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1991 TRACE("%p %p\n", stream, bitmap);
1993 return GdipCreateBitmapFromStream(stream, bitmap);
1996 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1997 GpCachedBitmap **cachedbmp)
2001 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
2003 if(!bitmap || !graphics || !cachedbmp)
2004 return InvalidParameter;
2006 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
2010 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
2012 GdipFree(*cachedbmp);
2019 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
2022 BitmapData lockeddata;
2023 ULONG andstride, xorstride, bitssize;
2024 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
2027 TRACE("(%p, %p)\n", bitmap, hicon);
2029 if (!bitmap || !hicon)
2030 return InvalidParameter;
2032 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
2033 PixelFormat32bppPARGB, &lockeddata);
2036 andstride = ((lockeddata.Width+31)/32)*4;
2037 xorstride = lockeddata.Width*4;
2038 bitssize = (andstride + xorstride) * lockeddata.Height;
2040 andbits = GdipAlloc(bitssize);
2044 xorbits = andbits + andstride * lockeddata.Height;
2046 for (y=0; y<lockeddata.Height; y++)
2048 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
2050 androw = andbits + andstride * y;
2051 for (x=0; x<lockeddata.Width; x++)
2052 if (srcrow[3+4*x] >= 128)
2053 androw[x/8] |= 1 << (7-x%8);
2055 xorrow = xorbits + xorstride * y;
2056 memcpy(xorrow, srcrow, xorstride);
2059 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
2067 GdipBitmapUnlockBits(bitmap, &lockeddata);
2073 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
2075 TRACE("%p\n", cachedbmp);
2078 return InvalidParameter;
2080 GdipDisposeImage(cachedbmp->image);
2081 GdipFree(cachedbmp);
2086 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2087 GpCachedBitmap *cachedbmp, INT x, INT y)
2089 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2091 if(!graphics || !cachedbmp)
2092 return InvalidParameter;
2094 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2097 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
2098 LPBYTE pData16, INT iMapMode, INT eFlags)
2100 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
2101 return NotImplemented;
2104 /* Internal utility function: Replace the image data of dst with that of src,
2106 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2108 assert(src->image.type == ImageTypeBitmap);
2109 assert(dst->image.type == ImageTypeBitmap);
2111 GdipFree(dst->bitmapbits);
2112 GdipFree(dst->own_bits);
2114 DeleteObject(dst->hbitmap);
2116 if (clobber_palette)
2118 GdipFree(dst->image.palette);
2119 dst->image.palette = src->image.palette;
2122 GdipFree(src->image.palette);
2124 dst->image.xres = src->image.xres;
2125 dst->image.yres = src->image.yres;
2126 dst->width = src->width;
2127 dst->height = src->height;
2128 dst->format = src->format;
2129 dst->hbitmap = src->hbitmap;
2130 dst->hdc = src->hdc;
2131 dst->bits = src->bits;
2132 dst->stride = src->stride;
2133 dst->own_bits = src->own_bits;
2134 if (dst->metadata_reader)
2135 IWICMetadataReader_Release(dst->metadata_reader);
2136 dst->metadata_reader = src->metadata_reader;
2137 GdipFree(dst->prop_item);
2138 dst->prop_item = src->prop_item;
2139 dst->prop_count = src->prop_count;
2140 if (dst->image.stream)
2141 IStream_Release(dst->image.stream);
2142 dst->image.stream = src->image.stream;
2143 dst->image.frame_count = src->image.frame_count;
2144 dst->image.current_frame = src->image.current_frame;
2145 dst->image.format = src->image.format;
2147 src->image.type = ~0;
2151 static GpStatus free_image_data(GpImage *image)
2154 return InvalidParameter;
2156 if (image->type == ImageTypeBitmap)
2158 GdipFree(((GpBitmap*)image)->bitmapbits);
2159 GdipFree(((GpBitmap*)image)->own_bits);
2160 DeleteDC(((GpBitmap*)image)->hdc);
2161 DeleteObject(((GpBitmap*)image)->hbitmap);
2162 if (((GpBitmap*)image)->metadata_reader)
2163 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2164 GdipFree(((GpBitmap*)image)->prop_item);
2166 else if (image->type == ImageTypeMetafile)
2168 GpMetafile *metafile = (GpMetafile*)image;
2169 GdipFree(metafile->comment_data);
2170 DeleteEnhMetaFile(CloseEnhMetaFile(metafile->record_dc));
2171 if (!metafile->preserve_hemf)
2172 DeleteEnhMetaFile(metafile->hemf);
2173 if (metafile->record_graphics)
2175 WARN("metafile closed while recording\n");
2176 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2177 metafile->record_graphics->image = NULL;
2178 metafile->record_graphics->busy = TRUE;
2183 WARN("invalid image: %p\n", image);
2187 IPicture_Release(image->picture);
2189 IStream_Release(image->stream);
2190 GdipFree(image->palette);
2195 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2199 TRACE("%p\n", image);
2201 status = free_image_data(image);
2202 if (status != Ok) return status;
2209 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2213 TRACE("(%p,%p)\n", image, item);
2216 return InvalidParameter;
2219 FIXME("not implemented\n");
2221 return NotImplemented;
2224 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2228 TRACE("(%p,%p)\n", image, item);
2231 FIXME("not implemented\n");
2233 return NotImplemented;
2236 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2239 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2241 if(!image || !srcRect || !srcUnit)
2242 return InvalidParameter;
2243 if(image->type == ImageTypeMetafile){
2244 *srcRect = ((GpMetafile*)image)->bounds;
2245 *srcUnit = ((GpMetafile*)image)->unit;
2247 else if(image->type == ImageTypeBitmap){
2248 srcRect->X = srcRect->Y = 0.0;
2249 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2250 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2251 *srcUnit = UnitPixel;
2254 srcRect->X = srcRect->Y = 0.0;
2255 srcRect->Width = ipicture_pixel_width(image->picture);
2256 srcRect->Height = ipicture_pixel_height(image->picture);
2257 *srcUnit = UnitPixel;
2260 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2261 srcRect->Width, srcRect->Height, *srcUnit);
2266 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2269 TRACE("%p %p %p\n", image, width, height);
2271 if(!image || !height || !width)
2272 return InvalidParameter;
2274 if(image->type == ImageTypeMetafile){
2275 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2276 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2278 else if(image->type == ImageTypeBitmap){
2279 *height = ((GpBitmap*)image)->height;
2280 *width = ((GpBitmap*)image)->width;
2283 *height = ipicture_pixel_height(image->picture);
2284 *width = ipicture_pixel_width(image->picture);
2287 TRACE("returning (%f, %f)\n", *height, *width);
2291 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2292 GpGraphics **graphics)
2297 TRACE("%p %p\n", image, graphics);
2299 if(!image || !graphics)
2300 return InvalidParameter;
2302 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2304 hdc = ((GpBitmap*)image)->hdc;
2307 hdc = CreateCompatibleDC(0);
2308 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2309 ((GpBitmap*)image)->hdc = hdc;
2312 stat = GdipCreateFromHDC(hdc, graphics);
2316 (*graphics)->image = image;
2317 (*graphics)->xres = image->xres;
2318 (*graphics)->yres = image->yres;
2321 else if (image->type == ImageTypeMetafile)
2322 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2324 stat = graphics_from_image(image, graphics);
2329 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2331 TRACE("%p %p\n", image, height);
2333 if(!image || !height)
2334 return InvalidParameter;
2336 if(image->type == ImageTypeMetafile)
2337 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2338 else if(image->type == ImageTypeBitmap)
2339 *height = ((GpBitmap*)image)->height;
2341 *height = ipicture_pixel_height(image->picture);
2343 TRACE("returning %d\n", *height);
2348 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2351 return InvalidParameter;
2355 TRACE("(%p) <-- %0.2f\n", image, *res);
2360 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2362 TRACE("%p %p\n", image, size);
2365 return InvalidParameter;
2367 if (!image->palette || image->palette->Count == 0)
2368 *size = sizeof(ColorPalette);
2370 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2372 TRACE("<-- %u\n", *size);
2377 /* FIXME: test this function for non-bitmap types */
2378 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2380 TRACE("%p %p\n", image, format);
2382 if(!image || !format)
2383 return InvalidParameter;
2385 if(image->type != ImageTypeBitmap)
2386 *format = PixelFormat24bppRGB;
2388 *format = ((GpBitmap*) image)->format;
2393 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2395 TRACE("(%p, %p)\n", image, format);
2397 if(!image || !format)
2398 return InvalidParameter;
2400 memcpy(format, &image->format, sizeof(GUID));
2405 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2407 TRACE("%p %p\n", image, type);
2410 return InvalidParameter;
2412 *type = image->type;
2417 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2420 return InvalidParameter;
2424 TRACE("(%p) <-- %0.2f\n", image, *res);
2429 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2431 TRACE("%p %p\n", image, width);
2433 if(!image || !width)
2434 return InvalidParameter;
2436 if(image->type == ImageTypeMetafile)
2437 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2438 else if(image->type == ImageTypeBitmap)
2439 *width = ((GpBitmap*)image)->width;
2441 *width = ipicture_pixel_width(image->picture);
2443 TRACE("returning %d\n", *width);
2448 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2449 MetafileHeader * header)
2453 TRACE("(%p, %p)\n", metafile, header);
2455 if(!metafile || !header)
2456 return InvalidParameter;
2459 FIXME("not implemented\n");
2461 memset(header, 0, sizeof(MetafileHeader));
2466 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromEmf(HENHMETAFILE hEmf,
2467 MetafileHeader *header)
2471 if(!hEmf || !header)
2472 return InvalidParameter;
2475 FIXME("not implemented\n");
2477 memset(header, 0, sizeof(MetafileHeader));
2482 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromFile(GDIPCONST WCHAR *filename,
2483 MetafileHeader *header)
2487 TRACE("(%s,%p)\n", debugstr_w(filename), header);
2489 if(!filename || !header)
2490 return InvalidParameter;
2493 FIXME("not implemented\n");
2495 memset(header, 0, sizeof(MetafileHeader));
2500 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromStream(IStream *stream,
2501 MetafileHeader *header)
2505 TRACE("(%p,%p)\n", stream, header);
2507 if(!stream || !header)
2508 return InvalidParameter;
2511 FIXME("not implemented\n");
2513 memset(header, 0, sizeof(MetafileHeader));
2518 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2520 TRACE("(%p, %p)\n", image, num);
2522 if (!image || !num) return InvalidParameter;
2526 if (image->type == ImageTypeBitmap)
2528 if (((GpBitmap *)image)->prop_item)
2530 *num = ((GpBitmap *)image)->prop_count;
2534 if (((GpBitmap *)image)->metadata_reader)
2535 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2541 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2544 IWICMetadataReader *reader;
2545 IWICEnumMetadataItem *enumerator;
2546 UINT prop_count, i, items_returned;
2548 TRACE("(%p, %u, %p)\n", image, num, list);
2550 if (!image || !list) return InvalidParameter;
2552 if (image->type != ImageTypeBitmap)
2554 FIXME("Not implemented for type %d\n", image->type);
2555 return NotImplemented;
2558 if (((GpBitmap *)image)->prop_item)
2560 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2562 for (i = 0; i < num; i++)
2564 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2570 reader = ((GpBitmap *)image)->metadata_reader;
2573 if (num != 0) return InvalidParameter;
2577 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2578 if (FAILED(hr)) return hresult_to_status(hr);
2580 if (num != prop_count) return InvalidParameter;
2582 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2583 if (FAILED(hr)) return hresult_to_status(hr);
2585 IWICEnumMetadataItem_Reset(enumerator);
2587 for (i = 0; i < num; i++)
2591 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2592 if (hr != S_OK) break;
2594 if (id.vt != VT_UI2)
2596 FIXME("not supported propvariant type for id: %u\n", id.vt);
2600 list[i] = id.u.uiVal;
2603 IWICEnumMetadataItem_Release(enumerator);
2605 return hr == S_OK ? Ok : hresult_to_status(hr);
2608 static UINT propvariant_size(PROPVARIANT *value)
2610 switch (value->vt & ~VT_VECTOR)
2616 if (!(value->vt & VT_VECTOR)) return 1;
2617 return value->u.caub.cElems;
2620 if (!(value->vt & VT_VECTOR)) return 2;
2621 return value->u.caui.cElems * 2;
2625 if (!(value->vt & VT_VECTOR)) return 4;
2626 return value->u.caul.cElems * 4;
2630 if (!(value->vt & VT_VECTOR)) return 8;
2631 return value->u.cauh.cElems * 8;
2633 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2635 return value->u.blob.cbSize;
2637 FIXME("not supported variant type %d\n", value->vt);
2642 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2645 IWICMetadataReader *reader;
2646 PROPVARIANT id, value;
2648 TRACE("(%p,%#x,%p)\n", image, propid, size);
2650 if (!size || !image) return InvalidParameter;
2652 if (image->type != ImageTypeBitmap)
2654 FIXME("Not implemented for type %d\n", image->type);
2655 return NotImplemented;
2658 if (((GpBitmap *)image)->prop_item)
2662 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2664 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2666 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2671 return PropertyNotFound;
2674 reader = ((GpBitmap *)image)->metadata_reader;
2675 if (!reader) return PropertyNotFound;
2678 id.u.uiVal = propid;
2679 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2680 if (FAILED(hr)) return PropertyNotFound;
2682 *size = propvariant_size(&value);
2683 if (*size) *size += sizeof(PropertyItem);
2684 PropVariantClear(&value);
2689 #ifndef PropertyTagTypeSByte
2690 #define PropertyTagTypeSByte 6
2691 #define PropertyTagTypeSShort 8
2692 #define PropertyTagTypeFloat 11
2693 #define PropertyTagTypeDouble 12
2696 static UINT vt_to_itemtype(UINT vt)
2703 { VT_I1, PropertyTagTypeSByte },
2704 { VT_UI1, PropertyTagTypeByte },
2705 { VT_I2, PropertyTagTypeSShort },
2706 { VT_UI2, PropertyTagTypeShort },
2707 { VT_I4, PropertyTagTypeSLONG },
2708 { VT_UI4, PropertyTagTypeLong },
2709 { VT_I8, PropertyTagTypeSRational },
2710 { VT_UI8, PropertyTagTypeRational },
2711 { VT_R4, PropertyTagTypeFloat },
2712 { VT_R8, PropertyTagTypeDouble },
2713 { VT_LPSTR, PropertyTagTypeASCII },
2714 { VT_BLOB, PropertyTagTypeUndefined }
2717 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2719 if (vt2type[i].vt == vt) return vt2type[i].type;
2721 FIXME("not supported variant type %u\n", vt);
2725 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2726 UINT size, PROPID id)
2728 UINT item_size, item_type;
2730 item_size = propvariant_size(value);
2731 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2733 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2734 if (!item_type) return InvalidParameter;
2736 item->value = item + 1;
2738 switch (value->vt & ~VT_VECTOR)
2742 if (!(value->vt & VT_VECTOR))
2743 *(BYTE *)item->value = value->u.bVal;
2745 memcpy(item->value, value->u.caub.pElems, item_size);
2749 if (!(value->vt & VT_VECTOR))
2750 *(USHORT *)item->value = value->u.uiVal;
2752 memcpy(item->value, value->u.caui.pElems, item_size);
2757 if (!(value->vt & VT_VECTOR))
2758 *(ULONG *)item->value = value->u.ulVal;
2760 memcpy(item->value, value->u.caul.pElems, item_size);
2765 if (!(value->vt & VT_VECTOR))
2766 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2768 memcpy(item->value, value->u.cauh.pElems, item_size);
2771 memcpy(item->value, value->u.pszVal, item_size);
2774 memcpy(item->value, value->u.blob.pBlobData, item_size);
2777 FIXME("not supported variant type %d\n", value->vt);
2778 return InvalidParameter;
2781 item->length = item_size;
2782 item->type = item_type;
2788 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2789 PropertyItem *buffer)
2793 IWICMetadataReader *reader;
2794 PROPVARIANT id, value;
2796 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2798 if (!image || !buffer) return InvalidParameter;
2800 if (image->type != ImageTypeBitmap)
2802 FIXME("Not implemented for type %d\n", image->type);
2803 return NotImplemented;
2806 if (((GpBitmap *)image)->prop_item)
2810 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2812 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2814 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2815 return InvalidParameter;
2817 *buffer = ((GpBitmap *)image)->prop_item[i];
2818 buffer->value = buffer + 1;
2819 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2824 return PropertyNotFound;
2827 reader = ((GpBitmap *)image)->metadata_reader;
2828 if (!reader) return PropertyNotFound;
2831 id.u.uiVal = propid;
2832 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2833 if (FAILED(hr)) return PropertyNotFound;
2835 stat = propvariant_to_item(&value, buffer, size, propid);
2836 PropVariantClear(&value);
2841 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2844 IWICMetadataReader *reader;
2845 IWICEnumMetadataItem *enumerator;
2846 UINT prop_count, prop_size, i;
2847 PROPVARIANT id, value;
2849 TRACE("(%p,%p,%p)\n", image, size, count);
2851 if (!image || !size || !count) return InvalidParameter;
2853 if (image->type != ImageTypeBitmap)
2855 FIXME("Not implemented for type %d\n", image->type);
2856 return NotImplemented;
2859 if (((GpBitmap *)image)->prop_item)
2861 *count = ((GpBitmap *)image)->prop_count;
2864 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2866 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2872 reader = ((GpBitmap *)image)->metadata_reader;
2873 if (!reader) return PropertyNotFound;
2875 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2876 if (FAILED(hr)) return hresult_to_status(hr);
2878 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2879 if (FAILED(hr)) return hresult_to_status(hr);
2881 IWICEnumMetadataItem_Reset(enumerator);
2885 PropVariantInit(&id);
2886 PropVariantInit(&value);
2888 for (i = 0; i < prop_count; i++)
2890 UINT items_returned, item_size;
2892 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2893 if (hr != S_OK) break;
2895 item_size = propvariant_size(&value);
2896 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2898 PropVariantClear(&id);
2899 PropVariantClear(&value);
2902 IWICEnumMetadataItem_Release(enumerator);
2904 if (hr != S_OK) return PropertyNotFound;
2906 *count = prop_count;
2911 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2912 UINT count, PropertyItem *buf)
2916 IWICMetadataReader *reader;
2917 IWICEnumMetadataItem *enumerator;
2918 UINT prop_count, prop_size, i;
2919 PROPVARIANT id, value;
2922 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2924 if (!image || !buf) return InvalidParameter;
2926 if (image->type != ImageTypeBitmap)
2928 FIXME("Not implemented for type %d\n", image->type);
2929 return NotImplemented;
2932 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2933 if (status != Ok) return status;
2935 if (prop_count != count || prop_size != size) return InvalidParameter;
2937 if (((GpBitmap *)image)->prop_item)
2939 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2941 item_value = (char *)(buf + prop_count);
2943 for (i = 0; i < prop_count; i++)
2945 buf[i].value = item_value;
2946 item_value += buf[i].length;
2952 reader = ((GpBitmap *)image)->metadata_reader;
2953 if (!reader) return PropertyNotFound;
2955 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2956 if (FAILED(hr)) return hresult_to_status(hr);
2958 IWICEnumMetadataItem_Reset(enumerator);
2960 item_value = (char *)(buf + prop_count);
2962 PropVariantInit(&id);
2963 PropVariantInit(&value);
2965 for (i = 0; i < prop_count; i++)
2968 UINT items_returned, item_size;
2970 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2971 if (hr != S_OK) break;
2973 if (id.vt != VT_UI2)
2975 FIXME("not supported propvariant type for id: %u\n", id.vt);
2979 item_size = propvariant_size(&value);
2982 item = HeapAlloc(GetProcessHeap(), 0, item_size + sizeof(*item));
2984 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2985 buf[i].id = item->id;
2986 buf[i].type = item->type;
2987 buf[i].length = item_size;
2988 buf[i].value = item_value;
2989 memcpy(item_value, item->value, item_size);
2990 item_value += item_size;
2992 HeapFree(GetProcessHeap(), 0, item);
2995 PropVariantClear(&id);
2996 PropVariantClear(&value);
2999 IWICEnumMetadataItem_Release(enumerator);
3001 if (hr != S_OK) return PropertyNotFound;
3006 struct image_format_dimension
3009 const GUID *dimension;
3012 static const struct image_format_dimension image_format_dimensions[] =
3014 {&ImageFormatGIF, &FrameDimensionTime},
3015 {&ImageFormatIcon, &FrameDimensionResolution},
3019 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
3020 GDIPCONST GUID* dimensionID, UINT* count)
3022 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
3024 if(!image || !count)
3025 return InvalidParameter;
3028 IsEqualGUID(dimensionID, &image->format) ||
3029 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
3030 IsEqualGUID(dimensionID, &FrameDimensionTime))
3032 *count = image->frame_count;
3036 return InvalidParameter;
3039 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
3042 TRACE("(%p, %p)\n", image, count);
3044 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
3046 if(!image || !count)
3047 return InvalidParameter;
3054 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
3055 GUID* dimensionIDs, UINT count)
3058 const GUID *result=NULL;
3060 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
3062 if(!image || !dimensionIDs || count != 1)
3063 return InvalidParameter;
3065 for (i=0; image_format_dimensions[i].format; i++)
3067 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
3069 result = image_format_dimensions[i].dimension;
3075 result = &FrameDimensionPage;
3077 memcpy(dimensionIDs, result, sizeof(GUID));
3082 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
3088 TRACE("(%s) %p\n", debugstr_w(filename), image);
3090 if (!filename || !image)
3091 return InvalidParameter;
3093 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
3098 stat = GdipLoadImageFromStream(stream, image);
3100 IStream_Release(stream);
3105 /* FIXME: no icm handling */
3106 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
3108 TRACE("(%s) %p\n", debugstr_w(filename), image);
3110 return GdipLoadImageFromFile(filename, image);
3113 static void add_property(GpBitmap *bitmap, PropertyItem *item)
3115 UINT prop_size, prop_count;
3116 PropertyItem *prop_item;
3118 if (bitmap->prop_item == NULL)
3120 prop_size = prop_count = 0;
3121 prop_item = GdipAlloc(item->length + sizeof(PropertyItem));
3122 if (!prop_item) return;
3129 GdipGetPropertySize((GpImage *)bitmap, &prop_size, &prop_count);
3131 prop_item = GdipAlloc(prop_size + item->length + sizeof(PropertyItem));
3132 if (!prop_item) return;
3133 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
3134 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
3135 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
3137 item_value = (char *)(prop_item + prop_count + 1);
3139 for (i = 0; i < prop_count; i++)
3141 prop_item[i].value = item_value;
3142 item_value += prop_item[i].length;
3146 prop_item[prop_count].id = item->id;
3147 prop_item[prop_count].type = item->type;
3148 prop_item[prop_count].length = item->length;
3149 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3150 memcpy(prop_item[prop_count].value, item->value, item->length);
3152 GdipFree(bitmap->prop_item);
3153 bitmap->prop_item = prop_item;
3154 bitmap->prop_count++;
3157 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3161 PROPVARIANT id, value;
3164 IWICMetadataReader_GetMetadataFormat(reader, &format);
3165 if (!IsEqualGUID(&format, guid)) return FALSE;
3167 PropVariantInit(&id);
3168 PropVariantInit(&value);
3171 id.u.pwszVal = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3172 if (!id.u.pwszVal) return FALSE;
3173 lstrcpyW(id.u.pwszVal, prop_name);
3174 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3175 if (hr == S_OK && value.vt == VT_BOOL)
3176 ret = value.u.boolVal;
3178 PropVariantClear(&id);
3179 PropVariantClear(&value);
3184 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3188 PROPVARIANT id, value;
3189 PropertyItem *item = NULL;
3191 IWICMetadataReader_GetMetadataFormat(reader, &format);
3192 if (!IsEqualGUID(&format, guid)) return NULL;
3194 PropVariantInit(&id);
3195 PropVariantInit(&value);
3198 id.u.pwszVal = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3199 if (!id.u.pwszVal) return NULL;
3200 lstrcpyW(id.u.pwszVal, prop_name);
3201 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3204 UINT item_size = propvariant_size(&value);
3207 item_size += sizeof(*item);
3208 item = GdipAlloc(item_size);
3209 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3217 PropVariantClear(&id);
3218 PropVariantClear(&value);
3223 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3225 static const WCHAR textentryW[] = { 'T','e','x','t','E','n','t','r','y',0 };
3226 PropertyItem *comment;
3228 comment = get_property(reader, &GUID_MetadataFormatGifComment, textentryW);
3230 comment->id = PropertyTagExifUserComment;
3235 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3237 static const WCHAR applicationW[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3238 static const WCHAR dataW[] = { 'D','a','t','a',0 };
3239 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3241 appext = get_property(reader, &GUID_MetadataFormatAPE, applicationW);
3244 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3245 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3247 appdata = get_property(reader, &GUID_MetadataFormatAPE, dataW);
3250 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3252 BYTE *data = appdata->value;
3253 if (data[0] == 3 && data[1] == 1)
3255 loop = GdipAlloc(sizeof(*loop) + sizeof(SHORT));
3258 loop->type = PropertyTagTypeShort;
3259 loop->id = PropertyTagLoopCount;
3260 loop->length = sizeof(SHORT);
3261 loop->value = loop + 1;
3262 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3276 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3278 static const WCHAR backgroundW[] = { 'B','a','c','k','g','r','o','u','n','d','C','o','l','o','r','I','n','d','e','x',0 };
3279 PropertyItem *background;
3281 background = get_property(reader, &GUID_MetadataFormatLSD, backgroundW);
3283 background->id = PropertyTagIndexBackground;
3288 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3290 static const WCHAR global_flagW[] = { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 };
3292 IWICImagingFactory *factory;
3293 IWICPalette *palette;
3295 WICColor colors[256];
3297 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, global_flagW))
3300 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
3301 &IID_IWICImagingFactory, (void **)&factory);
3302 if (hr != S_OK) return NULL;
3304 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3307 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3309 IWICPalette_GetColors(palette, 256, colors, &count);
3311 IWICPalette_Release(palette);
3314 IWICImagingFactory_Release(factory);
3322 pal = GdipAlloc(sizeof(*pal) + count * 3);
3323 if (!pal) return NULL;
3324 pal->type = PropertyTagTypeByte;
3325 pal->id = PropertyTagGlobalPalette;
3326 pal->value = pal + 1;
3327 pal->length = count * 3;
3331 for (i = 0; i < count; i++)
3333 rgb[i*3] = (colors[i] >> 16) & 0xff;
3334 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3335 rgb[i*3 + 2] = colors[i] & 0xff;
3344 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3346 static const WCHAR transparency_flagW[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3347 static const WCHAR colorW[] = { 'T','r','a','n','s','p','a','r','e','n','t','C','o','l','o','r','I','n','d','e','x',0 };
3348 PropertyItem *index = NULL;
3350 if (get_bool_property(reader, &GUID_MetadataFormatGCE, transparency_flagW))
3352 index = get_property(reader, &GUID_MetadataFormatGCE, colorW);
3354 index->id = PropertyTagIndexTransparent;
3359 static LONG get_gif_frame_delay(IWICBitmapFrameDecode *frame)
3361 static const WCHAR delayW[] = { 'D','e','l','a','y',0 };
3363 IWICMetadataBlockReader *block_reader;
3364 IWICMetadataReader *reader;
3365 UINT block_count, i;
3366 PropertyItem *delay;
3369 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3372 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3375 for (i = 0; i < block_count; i++)
3377 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3380 delay = get_property(reader, &GUID_MetadataFormatGCE, delayW);
3383 if (delay->type == PropertyTagTypeShort && delay->length == 2)
3384 value = *(SHORT *)delay->value;
3388 IWICMetadataReader_Release(reader);
3392 IWICMetadataBlockReader_Release(block_reader);
3398 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3401 IWICBitmapFrameDecode *frame;
3402 IWICMetadataBlockReader *block_reader;
3403 IWICMetadataReader *reader;
3404 UINT frame_count, block_count, i;
3405 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3406 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3408 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3409 if (frame_count > 1)
3411 delay = GdipAlloc(sizeof(*delay) + frame_count * sizeof(LONG));
3416 delay->type = PropertyTagTypeLong;
3417 delay->id = PropertyTagFrameDelay;
3418 delay->length = frame_count * sizeof(LONG);
3419 delay->value = delay + 1;
3421 value = delay->value;
3423 for (i = 0; i < frame_count; i++)
3425 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3428 value[i] = get_gif_frame_delay(frame);
3429 IWICBitmapFrameDecode_Release(frame);
3436 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3439 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3442 for (i = 0; i < block_count; i++)
3444 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3448 comment = get_gif_comment(reader);
3450 if (frame_count > 1 && !loop)
3451 loop = get_gif_loopcount(reader);
3454 background = get_gif_background(reader);
3457 palette = get_gif_palette(decoder, reader);
3459 IWICMetadataReader_Release(reader);
3463 IWICMetadataBlockReader_Release(block_reader);
3466 if (frame_count > 1 && !loop)
3468 loop = GdipAlloc(sizeof(*loop) + sizeof(SHORT));
3471 loop->type = PropertyTagTypeShort;
3472 loop->id = PropertyTagLoopCount;
3473 loop->length = sizeof(SHORT);
3474 loop->value = loop + 1;
3475 *(SHORT *)loop->value = 1;
3479 if (delay) add_property(bitmap, delay);
3480 if (comment) add_property(bitmap, comment);
3481 if (loop) add_property(bitmap, loop);
3482 if (palette) add_property(bitmap, palette);
3483 if (background) add_property(bitmap, background);
3489 GdipFree(background);
3491 /* Win7 gdiplus always returns transparent color index from frame 0 */
3492 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3493 if (hr != S_OK) return;
3495 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3498 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3501 for (i = 0; i < block_count; i++)
3503 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3506 if (!transparent_idx)
3507 transparent_idx = get_gif_transparent_idx(reader);
3509 IWICMetadataReader_Release(reader);
3513 IWICMetadataBlockReader_Release(block_reader);
3516 if (transparent_idx) add_property(bitmap, transparent_idx);
3517 GdipFree(transparent_idx);
3519 IWICBitmapFrameDecode_Release(frame);
3522 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3524 static GpStatus decode_image_wic(IStream *stream, GDIPCONST CLSID *clsid,
3525 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3530 IWICBitmapDecoder *decoder;
3531 IWICBitmapFrameDecode *frame;
3532 IWICBitmapSource *source=NULL;
3533 IWICMetadataBlockReader *block_reader;
3534 WICPixelFormatGUID wic_format;
3535 PixelFormat gdip_format=0;
3536 ColorPalette *palette = NULL;
3537 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3539 UINT width, height, frame_count;
3540 BitmapData lockeddata;
3544 TRACE("%p,%s,%u,%p\n", stream, wine_dbgstr_guid(clsid), active_frame, image);
3546 initresult = CoInitialize(NULL);
3548 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3549 &IID_IWICBitmapDecoder, (void**)&decoder);
3550 if (FAILED(hr)) goto end;
3552 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
3555 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3556 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3559 if (SUCCEEDED(hr)) /* got frame */
3561 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3565 IWICBitmapSource *bmp_source;
3566 IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICBitmapSource, (void **)&bmp_source);
3568 for (i=0; pixel_formats[i].wic_format; i++)
3570 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3572 source = bmp_source;
3573 gdip_format = pixel_formats[i].gdip_format;
3574 palette_type = pixel_formats[i].palette_type;
3580 /* unknown format; fall back on 32bppARGB */
3581 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, bmp_source, &source);
3582 gdip_format = PixelFormat32bppARGB;
3583 IWICBitmapSource_Release(bmp_source);
3585 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3588 if (SUCCEEDED(hr)) /* got source */
3590 hr = IWICBitmapSource_GetSize(source, &width, &height);
3593 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3596 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3598 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3599 gdip_format, &lockeddata);
3600 if (status == Ok) /* locked bitmap */
3605 for (i=0; i<height; i++)
3608 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3609 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3610 if (FAILED(hr)) break;
3613 GdipBitmapUnlockBits(bitmap, &lockeddata);
3616 if (SUCCEEDED(hr) && status == Ok)
3617 *image = (GpImage*)bitmap;
3621 GdipDisposeImage((GpImage*)bitmap);
3624 if (SUCCEEDED(hr) && status == Ok)
3627 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3630 bitmap->image.xres = dpix;
3631 bitmap->image.yres = dpiy;
3637 IWICBitmapSource_Release(source);
3640 if (SUCCEEDED(hr)) {
3641 bitmap->metadata_reader = NULL;
3643 if (metadata_reader)
3644 metadata_reader(bitmap, decoder, active_frame);
3645 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3647 UINT block_count = 0;
3648 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3649 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3650 IWICMetadataBlockReader_Release(block_reader);
3653 palette = get_palette(frame, palette_type);
3654 IWICBitmapFrameDecode_Release(frame);
3658 IWICBitmapDecoder_Release(decoder);
3661 if (SUCCEEDED(initresult)) CoUninitialize();
3663 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3667 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3668 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3669 bitmap->image.frame_count = frame_count;
3670 bitmap->image.current_frame = active_frame;
3671 bitmap->image.stream = stream;
3674 GdipFree(bitmap->image.palette);
3675 bitmap->image.palette = palette;
3679 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3680 bitmap->image.palette->Flags = 0;
3682 /* Pin the source stream */
3683 IStream_AddRef(stream);
3684 TRACE("=> %p\n", *image);
3690 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3692 return decode_image_wic(stream, &CLSID_WICIcoDecoder, active_frame, NULL, image);
3695 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3700 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, active_frame, NULL, image);
3702 bitmap = (GpBitmap*)*image;
3704 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3706 /* WIC supports bmp files with alpha, but gdiplus does not */
3707 bitmap->format = PixelFormat32bppRGB;
3713 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3715 return decode_image_wic(stream, &CLSID_WICJpegDecoder, active_frame, NULL, image);
3718 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3720 return decode_image_wic(stream, &CLSID_WICPngDecoder, active_frame, NULL, image);
3723 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3725 return decode_image_wic(stream, &CLSID_WICGifDecoder, active_frame, gif_metadata_reader, image);
3728 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3730 return decode_image_wic(stream, &CLSID_WICTiffDecoder, active_frame, NULL, image);
3733 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3737 TRACE("%p %p\n", stream, image);
3739 if(!stream || !image)
3740 return InvalidParameter;
3742 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3743 (LPVOID*) &pic) != S_OK){
3744 TRACE("Could not load picture\n");
3745 return GenericError;
3748 /* FIXME: missing initialization code */
3749 *image = GdipAlloc(sizeof(GpMetafile));
3750 if(!*image) return OutOfMemory;
3751 (*image)->type = ImageTypeMetafile;
3752 (*image)->stream = NULL;
3753 (*image)->picture = pic;
3754 (*image)->flags = ImageFlagsNone;
3755 (*image)->frame_count = 1;
3756 (*image)->current_frame = 0;
3757 (*image)->palette = NULL;
3759 TRACE("<-- %p\n", *image);
3764 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
3765 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
3767 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, UINT active_frame, GpImage **image);
3769 typedef struct image_codec {
3770 ImageCodecInfo info;
3771 encode_image_func encode_func;
3772 decode_image_func decode_func;
3787 static const struct image_codec codecs[NUM_CODECS];
3789 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
3792 const BYTE *pattern, *mask;
3798 /* seek to the start of the stream */
3800 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3801 if (FAILED(hr)) return hresult_to_status(hr);
3803 /* read the first 8 bytes */
3804 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
3805 hr = IStream_Read(stream, signature, 8, &bytesread);
3806 if (FAILED(hr)) return hresult_to_status(hr);
3807 if (hr == S_FALSE || bytesread == 0) return GenericError;
3809 for (i = 0; i < NUM_CODECS; i++) {
3810 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
3811 bytesread >= codecs[i].info.SigSize)
3813 for (sig=0; sig<codecs[i].info.SigCount; sig++)
3815 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
3816 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
3817 for (j=0; j<codecs[i].info.SigSize; j++)
3818 if ((signature[j] & mask[j]) != pattern[j])
3820 if (j == codecs[i].info.SigSize)
3822 *result = &codecs[i];
3829 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
3830 signature[0],signature[1],signature[2],signature[3],
3831 signature[4],signature[5],signature[6],signature[7]);
3833 return GenericError;
3836 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
3842 const struct image_codec *codec = NULL;
3845 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
3847 if (!image || !dimensionID)
3848 return InvalidParameter;
3850 if (frame >= image->frame_count)
3852 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
3853 return InvalidParameter;
3856 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
3858 WARN("invalid image type %d\n", image->type);
3859 return InvalidParameter;
3862 if (image->current_frame == frame)
3867 TRACE("image doesn't have an associated stream\n");
3871 /* choose an appropriate image decoder */
3872 stat = get_decoder_info(image->stream, &codec);
3875 WARN("can't find decoder info\n");
3879 /* seek to the start of the stream */
3881 hr = IStream_Seek(image->stream, seek, STREAM_SEEK_SET, NULL);
3883 return hresult_to_status(hr);
3885 /* call on the image decoder to do the real work */
3886 stat = codec->decode_func(image->stream, &codec->info.Clsid, frame, &new_image);
3890 memcpy(&new_image->format, &codec->info.FormatID, sizeof(GUID));
3891 free_image_data(image);
3892 if (image->type == ImageTypeBitmap)
3893 *(GpBitmap *)image = *(GpBitmap *)new_image;
3894 else if (image->type == ImageTypeMetafile)
3895 *(GpMetafile *)image = *(GpMetafile *)new_image;
3896 new_image->type = ~0;
3897 GdipFree(new_image);
3904 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
3909 const struct image_codec *codec=NULL;
3911 /* choose an appropriate image decoder */
3912 stat = get_decoder_info(stream, &codec);
3913 if (stat != Ok) return stat;
3915 /* seek to the start of the stream */
3917 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3918 if (FAILED(hr)) return hresult_to_status(hr);
3920 /* call on the image decoder to do the real work */
3921 stat = codec->decode_func(stream, &codec->info.Clsid, 0, image);
3923 /* take note of the original data format */
3926 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
3934 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
3936 TRACE("%p %p\n", stream, image);
3938 return GdipLoadImageFromStream(stream, image);
3941 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
3945 TRACE("(%p,%u)\n", image, propId);
3948 return InvalidParameter;
3951 FIXME("not implemented\n");
3953 return NotImplemented;
3956 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
3960 if (!image || !item) return InvalidParameter;
3962 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
3965 FIXME("not implemented\n");
3970 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
3971 GDIPCONST CLSID *clsidEncoder,
3972 GDIPCONST EncoderParameters *encoderParams)
3977 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
3979 if (!image || !filename|| !clsidEncoder)
3980 return InvalidParameter;
3982 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
3984 return GenericError;
3986 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
3988 IStream_Release(stream);
3992 /*************************************************************************
3993 * Encoding functions -
3994 * These functions encode an image in different image file formats.
3996 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
3997 #define BITMAP_FORMAT_JPEG 0xd8ff
3998 #define BITMAP_FORMAT_GIF 0x4947
3999 #define BITMAP_FORMAT_PNG 0x5089
4000 #define BITMAP_FORMAT_APM 0xcdd7
4002 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
4003 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4007 IWICBitmapEncoder *encoder;
4008 IWICBitmapFrameEncode *frameencode;
4009 IPropertyBag2 *encoderoptions;
4012 PixelFormat gdipformat=0;
4013 WICPixelFormatGUID wicformat;
4015 BitmapData lockeddata;
4019 if (image->type != ImageTypeBitmap)
4020 return GenericError;
4022 bitmap = (GpBitmap*)image;
4024 GdipGetImageWidth(image, &width);
4025 GdipGetImageHeight(image, &height);
4032 initresult = CoInitialize(NULL);
4034 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
4035 &IID_IWICBitmapEncoder, (void**)&encoder);
4038 if (SUCCEEDED(initresult)) CoUninitialize();
4039 return hresult_to_status(hr);
4042 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
4046 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4049 if (SUCCEEDED(hr)) /* created frame */
4051 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4054 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4057 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4061 for (i=0; pixel_formats[i].wic_format; i++)
4063 if (pixel_formats[i].gdip_format == bitmap->format)
4065 memcpy(&wicformat, pixel_formats[i].wic_format, sizeof(GUID));
4066 gdipformat = bitmap->format;
4072 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
4073 gdipformat = PixelFormat32bppARGB;
4076 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4081 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4086 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4089 /* write one row at a time in case stride is negative */
4090 row = lockeddata.Scan0;
4091 for (i=0; i<lockeddata.Height; i++)
4093 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4094 if (FAILED(hr)) break;
4095 row += lockeddata.Stride;
4098 GdipBitmapUnlockBits(bitmap, &lockeddata);
4105 hr = IWICBitmapFrameEncode_Commit(frameencode);
4107 IWICBitmapFrameEncode_Release(frameencode);
4108 IPropertyBag2_Release(encoderoptions);
4112 hr = IWICBitmapEncoder_Commit(encoder);
4114 IWICBitmapEncoder_Release(encoder);
4116 if (SUCCEEDED(initresult)) CoUninitialize();
4118 return hresult_to_status(hr);
4121 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4122 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4124 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
4127 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4128 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4130 return encode_image_WIC(image, stream, &CLSID_WICTiffEncoder, params);
4133 static GpStatus encode_image_png(GpImage *image, IStream* stream,
4134 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4136 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
4139 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4140 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4142 return encode_image_WIC(image, stream, &CLSID_WICJpegEncoder, params);
4145 /*****************************************************************************
4146 * GdipSaveImageToStream [GDIPLUS.@]
4148 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4149 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4152 encode_image_func encode_image;
4155 TRACE("%p %p %p %p\n", image, stream, clsid, params);
4157 if(!image || !stream)
4158 return InvalidParameter;
4160 /* select correct encoder */
4161 encode_image = NULL;
4162 for (i = 0; i < NUM_CODECS; i++) {
4163 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4164 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4165 encode_image = codecs[i].encode_func;
4167 if (encode_image == NULL)
4168 return UnknownImageFormat;
4170 stat = encode_image(image, stream, clsid, params);
4175 /*****************************************************************************
4176 * GdipSaveAdd [GDIPLUS.@]
4178 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4180 FIXME("(%p,%p): stub\n", image, params);
4184 /*****************************************************************************
4185 * GdipGetImagePalette [GDIPLUS.@]
4187 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4191 TRACE("(%p,%p,%i)\n", image, palette, size);
4193 if (!image || !palette)
4194 return InvalidParameter;
4196 count = image->palette ? image->palette->Count : 0;
4198 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4200 TRACE("<-- InsufficientBuffer\n");
4201 return InsufficientBuffer;
4206 palette->Flags = image->palette->Flags;
4207 palette->Count = image->palette->Count;
4208 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4218 /*****************************************************************************
4219 * GdipSetImagePalette [GDIPLUS.@]
4221 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4222 GDIPCONST ColorPalette *palette)
4224 ColorPalette *new_palette;
4226 TRACE("(%p,%p)\n", image, palette);
4228 if(!image || !palette || palette->Count > 256)
4229 return InvalidParameter;
4231 new_palette = GdipAlloc(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4232 if (!new_palette) return OutOfMemory;
4234 GdipFree(image->palette);
4235 image->palette = new_palette;
4236 image->palette->Flags = palette->Flags;
4237 image->palette->Count = palette->Count;
4238 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4243 /*************************************************************************
4245 * Structures that represent which formats we support for encoding.
4248 /* ImageCodecInfo creation routines taken from libgdiplus */
4249 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4250 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4251 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4252 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4253 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4254 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4256 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4257 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4258 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4259 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4260 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4261 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4263 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4264 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4265 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4266 static const WCHAR gif_format[] = {'G','I','F',0};
4267 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4268 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4270 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4271 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4272 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4273 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4274 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4275 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4277 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4278 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4279 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4280 static const WCHAR emf_format[] = {'E','M','F',0};
4281 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4282 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4284 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4285 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4286 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4287 static const WCHAR wmf_format[] = {'W','M','F',0};
4288 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4289 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4291 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4292 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4293 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4294 static const WCHAR png_format[] = {'P','N','G',0};
4295 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4296 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4298 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4299 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4300 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4301 static const WCHAR ico_format[] = {'I','C','O',0};
4302 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4303 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4305 static const struct image_codec codecs[NUM_CODECS] = {
4308 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4309 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4310 /* CodecName */ bmp_codecname,
4312 /* FormatDescription */ bmp_format,
4313 /* FilenameExtension */ bmp_extension,
4314 /* MimeType */ bmp_mimetype,
4315 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4319 /* SigPattern */ bmp_sig_pattern,
4320 /* SigMask */ bmp_sig_mask,
4327 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4328 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4329 /* CodecName */ jpeg_codecname,
4331 /* FormatDescription */ jpeg_format,
4332 /* FilenameExtension */ jpeg_extension,
4333 /* MimeType */ jpeg_mimetype,
4334 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4338 /* SigPattern */ jpeg_sig_pattern,
4339 /* SigMask */ jpeg_sig_mask,
4346 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4347 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4348 /* CodecName */ gif_codecname,
4350 /* FormatDescription */ gif_format,
4351 /* FilenameExtension */ gif_extension,
4352 /* MimeType */ gif_mimetype,
4353 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4357 /* SigPattern */ gif_sig_pattern,
4358 /* SigMask */ gif_sig_mask,
4365 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4366 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4367 /* CodecName */ tiff_codecname,
4369 /* FormatDescription */ tiff_format,
4370 /* FilenameExtension */ tiff_extension,
4371 /* MimeType */ tiff_mimetype,
4372 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4376 /* SigPattern */ tiff_sig_pattern,
4377 /* SigMask */ tiff_sig_mask,
4384 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4385 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4386 /* CodecName */ emf_codecname,
4388 /* FormatDescription */ emf_format,
4389 /* FilenameExtension */ emf_extension,
4390 /* MimeType */ emf_mimetype,
4391 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4395 /* SigPattern */ emf_sig_pattern,
4396 /* SigMask */ emf_sig_mask,
4399 decode_image_olepicture_metafile
4403 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4404 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4405 /* CodecName */ wmf_codecname,
4407 /* FormatDescription */ wmf_format,
4408 /* FilenameExtension */ wmf_extension,
4409 /* MimeType */ wmf_mimetype,
4410 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4414 /* SigPattern */ wmf_sig_pattern,
4415 /* SigMask */ wmf_sig_mask,
4418 decode_image_olepicture_metafile
4422 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4423 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4424 /* CodecName */ png_codecname,
4426 /* FormatDescription */ png_format,
4427 /* FilenameExtension */ png_extension,
4428 /* MimeType */ png_mimetype,
4429 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4433 /* SigPattern */ png_sig_pattern,
4434 /* SigMask */ png_sig_mask,
4441 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4442 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4443 /* CodecName */ ico_codecname,
4445 /* FormatDescription */ ico_format,
4446 /* FilenameExtension */ ico_extension,
4447 /* MimeType */ ico_mimetype,
4448 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4452 /* SigPattern */ ico_sig_pattern,
4453 /* SigMask */ ico_sig_mask,
4460 /*****************************************************************************
4461 * GdipGetImageDecodersSize [GDIPLUS.@]
4463 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
4465 int decoder_count=0;
4467 TRACE("%p %p\n", numDecoders, size);
4469 if (!numDecoders || !size)
4470 return InvalidParameter;
4472 for (i=0; i<NUM_CODECS; i++)
4474 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4478 *numDecoders = decoder_count;
4479 *size = decoder_count * sizeof(ImageCodecInfo);
4484 /*****************************************************************************
4485 * GdipGetImageDecoders [GDIPLUS.@]
4487 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
4489 int i, decoder_count=0;
4490 TRACE("%u %u %p\n", numDecoders, size, decoders);
4493 size != numDecoders * sizeof(ImageCodecInfo))
4494 return GenericError;
4496 for (i=0; i<NUM_CODECS; i++)
4498 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4500 if (decoder_count == numDecoders) return GenericError;
4501 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4506 if (decoder_count < numDecoders) return GenericError;
4511 /*****************************************************************************
4512 * GdipGetImageEncodersSize [GDIPLUS.@]
4514 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4516 int encoder_count=0;
4518 TRACE("%p %p\n", numEncoders, size);
4520 if (!numEncoders || !size)
4521 return InvalidParameter;
4523 for (i=0; i<NUM_CODECS; i++)
4525 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4529 *numEncoders = encoder_count;
4530 *size = encoder_count * sizeof(ImageCodecInfo);
4535 /*****************************************************************************
4536 * GdipGetImageEncoders [GDIPLUS.@]
4538 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4540 int i, encoder_count=0;
4541 TRACE("%u %u %p\n", numEncoders, size, encoders);
4544 size != numEncoders * sizeof(ImageCodecInfo))
4545 return GenericError;
4547 for (i=0; i<NUM_CODECS; i++)
4549 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4551 if (encoder_count == numEncoders) return GenericError;
4552 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4557 if (encoder_count < numEncoders) return GenericError;
4562 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
4563 GDIPCONST CLSID* clsidEncoder, UINT *size)
4567 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
4570 FIXME("not implemented\n");
4574 return NotImplemented;
4577 static PixelFormat get_16bpp_format(HBITMAP hbm)
4583 hdc = CreateCompatibleDC(NULL);
4585 memset(&bmh, 0, sizeof(bmh));
4586 bmh.bV4Size = sizeof(bmh);
4589 bmh.bV4V4Compression = BI_BITFIELDS;
4590 bmh.bV4BitCount = 16;
4592 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4594 if (bmh.bV4RedMask == 0x7c00 &&
4595 bmh.bV4GreenMask == 0x3e0 &&
4596 bmh.bV4BlueMask == 0x1f)
4598 result = PixelFormat16bppRGB555;
4600 else if (bmh.bV4RedMask == 0xf800 &&
4601 bmh.bV4GreenMask == 0x7e0 &&
4602 bmh.bV4BlueMask == 0x1f)
4604 result = PixelFormat16bppRGB565;
4608 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4609 bmh.bV4GreenMask, bmh.bV4BlueMask);
4610 result = PixelFormatUndefined;
4618 /*****************************************************************************
4619 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4621 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4626 BitmapData lockeddata;
4628 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4631 return InvalidParameter;
4633 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4634 return InvalidParameter;
4636 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4637 switch(bm.bmBitsPixel) {
4639 format = PixelFormat1bppIndexed;
4642 format = PixelFormat4bppIndexed;
4645 format = PixelFormat8bppIndexed;
4648 format = get_16bpp_format(hbm);
4649 if (format == PixelFormatUndefined)
4650 return InvalidParameter;
4653 format = PixelFormat24bppRGB;
4656 format = PixelFormat32bppRGB;
4659 format = PixelFormat48bppRGB;
4662 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4663 return InvalidParameter;
4666 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4667 format, NULL, bitmap);
4671 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4672 format, &lockeddata);
4676 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
4677 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
4680 hdc = CreateCompatibleDC(NULL);
4682 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4683 pbmi->bmiHeader.biBitCount = 0;
4685 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4687 src_height = abs(pbmi->bmiHeader.biHeight);
4688 pbmi->bmiHeader.biHeight = -src_height;
4690 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
4694 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4697 if (retval == Ok && hpal)
4699 PALETTEENTRY entry[256];
4700 ColorPalette *palette=NULL;
4701 int i, num_palette_entries;
4703 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
4704 if (!num_palette_entries)
4705 retval = GenericError;
4707 palette = GdipAlloc(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4709 retval = OutOfMemory;
4714 palette->Count = num_palette_entries;
4716 for (i=0; i<num_palette_entries; i++)
4718 palette->Entries[i] = 0xff000000 | entry[i].peRed << 16 |
4719 entry[i].peGreen << 8 | entry[i].peBlue;
4722 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
4730 GdipDisposeImage((GpImage*)*bitmap);
4738 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
4740 FIXME("(%p): stub\n", effect);
4741 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
4742 * in Windows's gdiplus */
4743 return NotImplemented;
4746 /*****************************************************************************
4747 * GdipSetEffectParameters [GDIPLUS.@]
4749 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
4750 const VOID *params, const UINT size)
4754 TRACE("(%p,%p,%u)\n", effect, params, size);
4757 FIXME("not implemented\n");
4759 return NotImplemented;
4762 /*****************************************************************************
4763 * GdipGetImageFlags [GDIPLUS.@]
4765 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
4767 TRACE("%p %p\n", image, flags);
4769 if(!image || !flags)
4770 return InvalidParameter;
4772 *flags = image->flags;
4777 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
4779 TRACE("(%d, %p)\n", control, param);
4782 case TestControlForceBilinear:
4784 FIXME("TestControlForceBilinear not handled\n");
4786 case TestControlNoICM:
4788 FIXME("TestControlNoICM not handled\n");
4790 case TestControlGetBuildNumber:
4791 *((DWORD*)param) = 3102;
4798 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
4799 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
4800 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
4801 GpMetafile **metafile)
4803 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4804 frameUnit, debugstr_w(desc), metafile);
4806 return NotImplemented;
4809 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
4810 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
4811 GDIPCONST WCHAR *desc, GpMetafile **metafile)
4813 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4814 frameUnit, debugstr_w(desc), metafile);
4816 return NotImplemented;
4819 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
4821 TRACE("%p\n", image);
4826 /*****************************************************************************
4827 * GdipGetImageThumbnail [GDIPLUS.@]
4829 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
4830 GpImage **ret_image, GetThumbnailImageAbort cb,
4834 GpGraphics *graphics;
4835 UINT srcwidth, srcheight;
4837 TRACE("(%p %u %u %p %p %p)\n",
4838 image, width, height, ret_image, cb, cb_data);
4840 if (!image || !ret_image)
4841 return InvalidParameter;
4843 if (!width) width = 120;
4844 if (!height) height = 120;
4846 GdipGetImageWidth(image, &srcwidth);
4847 GdipGetImageHeight(image, &srcheight);
4849 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
4850 NULL, (GpBitmap**)ret_image);
4854 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
4858 stat = GdipDrawImageRectRectI(graphics, image,
4859 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
4862 GdipDeleteGraphics(graphics);
4867 GdipDisposeImage(*ret_image);
4875 /*****************************************************************************
4876 * GdipImageRotateFlip [GDIPLUS.@]
4878 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
4880 GpBitmap *new_bitmap;
4882 int bpp, bytesperpixel;
4883 int rotate_90, flip_x, flip_y;
4884 int src_x_offset, src_y_offset;
4886 UINT x, y, width, height;
4887 BitmapData src_lock, dst_lock;
4890 TRACE("(%p, %u)\n", image, type);
4893 return InvalidParameter;
4896 flip_x = (type&6) == 2 || (type&6) == 4;
4897 flip_y = (type&3) == 1 || (type&3) == 2;
4899 if (image->type != ImageTypeBitmap)
4901 FIXME("Not implemented for type %i\n", image->type);
4902 return NotImplemented;
4905 bitmap = (GpBitmap*)image;
4906 bpp = PIXELFORMATBPP(bitmap->format);
4910 FIXME("Not implemented for %i bit images\n", bpp);
4911 return NotImplemented;
4916 width = bitmap->height;
4917 height = bitmap->width;
4921 width = bitmap->width;
4922 height = bitmap->height;
4925 bytesperpixel = bpp/8;
4927 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
4932 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
4936 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
4940 LPBYTE src_row, src_pixel;
4941 LPBYTE dst_row, dst_pixel;
4943 src_origin = src_lock.Scan0;
4944 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
4945 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
4949 if (flip_y) src_x_offset = -src_lock.Stride;
4950 else src_x_offset = src_lock.Stride;
4951 if (flip_x) src_y_offset = -bytesperpixel;
4952 else src_y_offset = bytesperpixel;
4956 if (flip_x) src_x_offset = -bytesperpixel;
4957 else src_x_offset = bytesperpixel;
4958 if (flip_y) src_y_offset = -src_lock.Stride;
4959 else src_y_offset = src_lock.Stride;
4962 src_row = src_origin;
4963 dst_row = dst_lock.Scan0;
4964 for (y=0; y<height; y++)
4966 src_pixel = src_row;
4967 dst_pixel = dst_row;
4968 for (x=0; x<width; x++)
4970 /* FIXME: This could probably be faster without memcpy. */
4971 memcpy(dst_pixel, src_pixel, bytesperpixel);
4972 dst_pixel += bytesperpixel;
4973 src_pixel += src_x_offset;
4975 src_row += src_y_offset;
4976 dst_row += dst_lock.Stride;
4979 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
4982 GdipBitmapUnlockBits(bitmap, &src_lock);
4986 move_bitmap(bitmap, new_bitmap, FALSE);
4988 GdipDisposeImage((GpImage*)new_bitmap);
4993 /*****************************************************************************
4994 * GdipConvertToEmfPlusToFile [GDIPLUS.@]
4997 GpStatus WINGDIPAPI GdipConvertToEmfPlusToFile(const GpGraphics* refGraphics,
4998 GpMetafile* metafile, BOOL* conversionSuccess,
4999 const WCHAR* filename, EmfType emfType,
5000 const WCHAR* description, GpMetafile** out_metafile)
5002 FIXME("stub: %p, %p, %p, %p, %u, %p, %p\n", refGraphics, metafile, conversionSuccess, filename, emfType, description, out_metafile);
5003 return NotImplemented;