wined3d: Destroy the Vulkan command pool after cleaning up resources.
[wine.git] / dlls / gdiplus / image.c
blobb899619e1d3e0b9179cc61828c96f17bb3bad66c
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012,2016 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
20 #include <stdarg.h>
21 #include <assert.h>
23 #define NONAMELESSUNION
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "wingdi.h"
30 #define COBJMACROS
31 #include "objbase.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "initguid.h"
36 #include "wincodec.h"
37 #include "gdiplus.h"
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
43 HRESULT WINAPI WICCreateImagingFactory_Proxy(UINT, IWICImagingFactory**);
45 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
46 #define WMF_PLACEABLE_KEY 0x9ac6cdd7
48 static const struct
50 const WICPixelFormatGUID *wic_format;
51 PixelFormat gdip_format;
52 /* predefined palette type to use for pixel format conversions */
53 WICBitmapPaletteType palette_type;
54 } pixel_formats[] =
56 { &GUID_WICPixelFormat1bppIndexed, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
57 { &GUID_WICPixelFormatBlackWhite, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
58 { &GUID_WICPixelFormat4bppIndexed, PixelFormat4bppIndexed, WICBitmapPaletteTypeFixedHalftone8 },
59 { &GUID_WICPixelFormat8bppIndexed, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedHalftone256 },
60 { &GUID_WICPixelFormat8bppGray, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedGray256 },
61 { &GUID_WICPixelFormat16bppBGR555, PixelFormat16bppRGB555, 0 },
62 { &GUID_WICPixelFormat24bppBGR, PixelFormat24bppRGB, 0 },
63 { &GUID_WICPixelFormat32bppBGR, PixelFormat32bppRGB, 0 },
64 { &GUID_WICPixelFormat32bppBGRA, PixelFormat32bppARGB, 0 },
65 { &GUID_WICPixelFormat32bppPBGRA, PixelFormat32bppPARGB, 0 },
66 { NULL }
69 static ColorPalette *get_palette(IWICBitmapFrameDecode *frame, WICBitmapPaletteType palette_type)
71 HRESULT hr;
72 IWICImagingFactory *factory;
73 IWICPalette *wic_palette;
74 ColorPalette *palette = NULL;
76 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
77 if (hr != S_OK) return NULL;
79 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
80 if (hr == S_OK)
82 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
83 if (frame)
84 hr = IWICBitmapFrameDecode_CopyPalette(frame, wic_palette);
85 if (hr != S_OK && palette_type != 0)
87 TRACE("using predefined palette %#x\n", palette_type);
88 hr = IWICPalette_InitializePredefined(wic_palette, palette_type, FALSE);
90 if (hr == S_OK)
92 WICBitmapPaletteType type;
93 BOOL alpha;
94 UINT count;
96 IWICPalette_GetColorCount(wic_palette, &count);
97 palette = heap_alloc(2 * sizeof(UINT) + count * sizeof(ARGB));
98 IWICPalette_GetColors(wic_palette, count, (UINT *)palette->Entries, &palette->Count);
100 IWICPalette_GetType(wic_palette, &type);
101 switch(type) {
102 case WICBitmapPaletteTypeFixedGray4:
103 case WICBitmapPaletteTypeFixedGray16:
104 case WICBitmapPaletteTypeFixedGray256:
105 palette->Flags = PaletteFlagsGrayScale;
106 break;
107 case WICBitmapPaletteTypeFixedHalftone8:
108 case WICBitmapPaletteTypeFixedHalftone27:
109 case WICBitmapPaletteTypeFixedHalftone64:
110 case WICBitmapPaletteTypeFixedHalftone125:
111 case WICBitmapPaletteTypeFixedHalftone216:
112 case WICBitmapPaletteTypeFixedHalftone252:
113 case WICBitmapPaletteTypeFixedHalftone256:
114 palette->Flags = PaletteFlagsHalftone;
115 break;
116 default:
117 palette->Flags = 0;
119 IWICPalette_HasAlpha(wic_palette, &alpha);
120 if(alpha)
121 palette->Flags |= PaletteFlagsHasAlpha;
123 IWICPalette_Release(wic_palette);
125 IWICImagingFactory_Release(factory);
126 return palette;
129 static HRESULT set_palette(IWICBitmapFrameEncode *frame, ColorPalette *palette)
131 HRESULT hr;
132 IWICImagingFactory *factory;
133 IWICPalette *wic_palette;
135 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
136 if (FAILED(hr))
137 return hr;
139 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
140 IWICImagingFactory_Release(factory);
141 if (SUCCEEDED(hr))
143 hr = IWICPalette_InitializeCustom(wic_palette, (UINT *)palette->Entries, palette->Count);
145 if (SUCCEEDED(hr))
146 hr = IWICBitmapFrameEncode_SetPalette(frame, wic_palette);
148 IWICPalette_Release(wic_palette);
151 return hr;
154 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
155 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
157 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
159 * Note: According to Jose Roca's GDI+ docs, this function is not
160 * implemented in Windows's GDI+.
162 return NotImplemented;
165 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
166 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
167 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
169 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
171 * Note: According to Jose Roca's GDI+ docs, this function is not
172 * implemented in Windows's GDI+.
174 return NotImplemented;
177 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
179 *index = (row[x/8]>>(7-x%8)) & 1;
182 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
184 if (x & 1)
185 *index = row[x/2]&0xf;
186 else
187 *index = row[x/2]>>4;
190 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
192 *index = row[x];
195 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
196 const BYTE *row, UINT x)
198 *r = *g = *b = row[x*2+1];
199 *a = 255;
202 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
203 const BYTE *row, UINT x)
205 WORD pixel = *((const WORD*)(row)+x);
206 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
207 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
208 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
209 *a = 255;
212 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
213 const BYTE *row, UINT x)
215 WORD pixel = *((const WORD*)(row)+x);
216 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
217 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
218 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
219 *a = 255;
222 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
223 const BYTE *row, UINT x)
225 WORD pixel = *((const WORD*)(row)+x);
226 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
227 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
228 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
229 if ((pixel&0x8000) == 0x8000)
230 *a = 255;
231 else
232 *a = 0;
235 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
236 const BYTE *row, UINT x)
238 *r = row[x*3+2];
239 *g = row[x*3+1];
240 *b = row[x*3];
241 *a = 255;
244 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
245 const BYTE *row, UINT x)
247 *r = row[x*4+2];
248 *g = row[x*4+1];
249 *b = row[x*4];
250 *a = 255;
253 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
254 const BYTE *row, UINT x)
256 *r = row[x*4+2];
257 *g = row[x*4+1];
258 *b = row[x*4];
259 *a = row[x*4+3];
262 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
263 const BYTE *row, UINT x)
265 *a = row[x*4+3];
266 if (*a == 0)
268 *r = row[x*4+2];
269 *g = row[x*4+1];
270 *b = row[x*4];
272 else
274 DWORD scaled_q = (255 << 15) / *a;
275 *r = (row[x*4+2] > *a) ? 0xff : (row[x*4+2] * scaled_q) >> 15;
276 *g = (row[x*4+1] > *a) ? 0xff : (row[x*4+1] * scaled_q) >> 15;
277 *b = (row[x*4] > *a) ? 0xff : (row[x*4] * scaled_q) >> 15;
281 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
282 const BYTE *row, UINT x)
284 *r = row[x*6+5];
285 *g = row[x*6+3];
286 *b = row[x*6+1];
287 *a = 255;
290 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
291 const BYTE *row, UINT x)
293 *r = row[x*8+5];
294 *g = row[x*8+3];
295 *b = row[x*8+1];
296 *a = row[x*8+7];
299 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
300 const BYTE *row, UINT x)
302 *a = row[x*8+7];
303 if (*a == 0)
304 *r = *g = *b = 0;
305 else
307 *r = row[x*8+5] * 255 / *a;
308 *g = row[x*8+3] * 255 / *a;
309 *b = row[x*8+1] * 255 / *a;
313 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
314 ARGB *color)
316 BYTE r, g, b, a;
317 BYTE index;
318 BYTE *row;
320 if(!bitmap || !color ||
321 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
322 return InvalidParameter;
324 row = bitmap->bits+bitmap->stride*y;
326 switch (bitmap->format)
328 case PixelFormat1bppIndexed:
329 getpixel_1bppIndexed(&index,row,x);
330 break;
331 case PixelFormat4bppIndexed:
332 getpixel_4bppIndexed(&index,row,x);
333 break;
334 case PixelFormat8bppIndexed:
335 getpixel_8bppIndexed(&index,row,x);
336 break;
337 case PixelFormat16bppGrayScale:
338 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
339 break;
340 case PixelFormat16bppRGB555:
341 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
342 break;
343 case PixelFormat16bppRGB565:
344 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
345 break;
346 case PixelFormat16bppARGB1555:
347 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
348 break;
349 case PixelFormat24bppRGB:
350 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
351 break;
352 case PixelFormat32bppRGB:
353 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
354 break;
355 case PixelFormat32bppARGB:
356 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
357 break;
358 case PixelFormat32bppPARGB:
359 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
360 break;
361 case PixelFormat48bppRGB:
362 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
363 break;
364 case PixelFormat64bppARGB:
365 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
366 break;
367 case PixelFormat64bppPARGB:
368 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
369 break;
370 default:
371 FIXME("not implemented for format 0x%x\n", bitmap->format);
372 return NotImplemented;
375 if (bitmap->format & PixelFormatIndexed)
376 *color = bitmap->image.palette->Entries[index];
377 else
378 *color = a<<24|r<<16|g<<8|b;
380 return Ok;
383 static unsigned int absdiff(unsigned int x, unsigned int y)
385 return x > y ? x - y : y - x;
388 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
390 BYTE index = 0;
391 int best_distance = 0x7fff;
392 int distance;
393 UINT i;
395 if (!palette) return 0;
396 /* This algorithm scans entire palette,
397 computes difference from desired color (all color components have equal weight)
398 and returns the index of color with least difference.
400 Note: Maybe it could be replaced with a better algorithm for better image quality
401 and performance, though better algorithm would probably need some pre-built lookup
402 tables and thus may actually be slower if this method is called only few times per
403 every image.
405 for(i=0;i<palette->Count;i++) {
406 ARGB color=palette->Entries[i];
407 distance=absdiff(b, color & 0xff) + absdiff(g, color>>8 & 0xff) + absdiff(r, color>>16 & 0xff) + absdiff(a, color>>24 & 0xff);
408 if (distance<best_distance) {
409 best_distance=distance;
410 index=i;
413 return index;
416 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
417 BYTE *row, UINT x, ColorPalette *palette)
419 BYTE index = get_palette_index(r,g,b,a,palette);
420 row[x]=index;
423 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
424 BYTE *row, UINT x, ColorPalette *palette)
426 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
429 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
430 BYTE *row, UINT x, ColorPalette *palette)
432 if (x & 1)
433 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
434 else
435 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
438 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
439 BYTE *row, UINT x)
441 *((WORD*)(row)+x) = (r+g+b)*85;
444 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
445 BYTE *row, UINT x)
447 *((WORD*)(row)+x) = (r<<7&0x7c00)|
448 (g<<2&0x03e0)|
449 (b>>3&0x001f);
452 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
453 BYTE *row, UINT x)
455 *((WORD*)(row)+x) = (r<<8&0xf800)|
456 (g<<3&0x07e0)|
457 (b>>3&0x001f);
460 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
461 BYTE *row, UINT x)
463 *((WORD*)(row)+x) = (a<<8&0x8000)|
464 (r<<7&0x7c00)|
465 (g<<2&0x03e0)|
466 (b>>3&0x001f);
469 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
470 BYTE *row, UINT x)
472 row[x*3+2] = r;
473 row[x*3+1] = g;
474 row[x*3] = b;
477 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
478 BYTE *row, UINT x)
480 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
483 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
484 BYTE *row, UINT x)
486 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
489 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
490 BYTE *row, UINT x)
492 r = (r * a + 127) / 255;
493 g = (g * a + 127) / 255;
494 b = (b * a + 127) / 255;
495 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
498 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
499 BYTE *row, UINT x)
501 row[x*6+5] = row[x*6+4] = r;
502 row[x*6+3] = row[x*6+2] = g;
503 row[x*6+1] = row[x*6] = b;
506 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
507 BYTE *row, UINT x)
509 UINT64 a64=a, r64=r, g64=g, b64=b;
510 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
513 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
514 BYTE *row, UINT x)
516 UINT64 a64, r64, g64, b64;
517 a64 = a * 257;
518 r64 = r * a / 255;
519 g64 = g * a / 255;
520 b64 = b * a / 255;
521 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
524 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
525 ARGB color)
527 BYTE a, r, g, b;
528 BYTE *row;
530 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
531 return InvalidParameter;
533 a = color>>24;
534 r = color>>16;
535 g = color>>8;
536 b = color;
538 row = bitmap->bits + bitmap->stride * y;
540 switch (bitmap->format)
542 case PixelFormat16bppGrayScale:
543 setpixel_16bppGrayScale(r,g,b,a,row,x);
544 break;
545 case PixelFormat16bppRGB555:
546 setpixel_16bppRGB555(r,g,b,a,row,x);
547 break;
548 case PixelFormat16bppRGB565:
549 setpixel_16bppRGB565(r,g,b,a,row,x);
550 break;
551 case PixelFormat16bppARGB1555:
552 setpixel_16bppARGB1555(r,g,b,a,row,x);
553 break;
554 case PixelFormat24bppRGB:
555 setpixel_24bppRGB(r,g,b,a,row,x);
556 break;
557 case PixelFormat32bppRGB:
558 setpixel_32bppRGB(r,g,b,a,row,x);
559 break;
560 case PixelFormat32bppARGB:
561 setpixel_32bppARGB(r,g,b,a,row,x);
562 break;
563 case PixelFormat32bppPARGB:
564 setpixel_32bppPARGB(r,g,b,a,row,x);
565 break;
566 case PixelFormat48bppRGB:
567 setpixel_48bppRGB(r,g,b,a,row,x);
568 break;
569 case PixelFormat64bppARGB:
570 setpixel_64bppARGB(r,g,b,a,row,x);
571 break;
572 case PixelFormat64bppPARGB:
573 setpixel_64bppPARGB(r,g,b,a,row,x);
574 break;
575 case PixelFormat8bppIndexed:
576 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
577 break;
578 case PixelFormat4bppIndexed:
579 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
580 break;
581 case PixelFormat1bppIndexed:
582 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
583 break;
584 default:
585 FIXME("not implemented for format 0x%x\n", bitmap->format);
586 return NotImplemented;
589 return Ok;
592 GpStatus convert_pixels(INT width, INT height,
593 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
594 ColorPalette *dst_palette,
595 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
596 ColorPalette *src_palette)
598 INT x, y;
600 if (src_format == dst_format ||
601 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
603 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
604 for (y=0; y<height; y++)
605 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
606 return Ok;
609 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
610 for (y=0; y<height; y++) \
611 for (x=0; x<width; x++) { \
612 BYTE index; \
613 ARGB argb; \
614 BYTE *color = (BYTE *)&argb; \
615 getpixel_function(&index, src_bits+src_stride*y, x); \
616 argb = (src_palette && index < src_palette->Count) ? src_palette->Entries[index] : 0; \
617 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
619 return Ok; \
620 } while (0);
622 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
623 for (y=0; y<height; y++) \
624 for (x=0; x<width; x++) { \
625 BYTE r, g, b, a; \
626 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
627 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
629 return Ok; \
630 } while (0);
632 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
633 for (y=0; y<height; y++) \
634 for (x=0; x<width; x++) { \
635 BYTE r, g, b, a; \
636 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
637 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, dst_palette); \
639 return Ok; \
640 } while (0);
642 switch (src_format)
644 case PixelFormat1bppIndexed:
645 switch (dst_format)
647 case PixelFormat16bppGrayScale:
648 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
649 case PixelFormat16bppRGB555:
650 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
651 case PixelFormat16bppRGB565:
652 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
653 case PixelFormat16bppARGB1555:
654 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
655 case PixelFormat24bppRGB:
656 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
657 case PixelFormat32bppRGB:
658 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
659 case PixelFormat32bppARGB:
660 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
661 case PixelFormat32bppPARGB:
662 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
663 case PixelFormat48bppRGB:
664 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
665 case PixelFormat64bppARGB:
666 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
667 default:
668 break;
670 break;
671 case PixelFormat4bppIndexed:
672 switch (dst_format)
674 case PixelFormat16bppGrayScale:
675 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
676 case PixelFormat16bppRGB555:
677 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
678 case PixelFormat16bppRGB565:
679 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
680 case PixelFormat16bppARGB1555:
681 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
682 case PixelFormat24bppRGB:
683 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
684 case PixelFormat32bppRGB:
685 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
686 case PixelFormat32bppARGB:
687 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
688 case PixelFormat32bppPARGB:
689 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
690 case PixelFormat48bppRGB:
691 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
692 case PixelFormat64bppARGB:
693 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
694 default:
695 break;
697 break;
698 case PixelFormat8bppIndexed:
699 switch (dst_format)
701 case PixelFormat16bppGrayScale:
702 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
703 case PixelFormat16bppRGB555:
704 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
705 case PixelFormat16bppRGB565:
706 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
707 case PixelFormat16bppARGB1555:
708 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
709 case PixelFormat24bppRGB:
710 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
711 case PixelFormat32bppRGB:
712 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
713 case PixelFormat32bppARGB:
714 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
715 case PixelFormat32bppPARGB:
716 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
717 case PixelFormat48bppRGB:
718 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
719 case PixelFormat64bppARGB:
720 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
721 default:
722 break;
724 break;
725 case PixelFormat16bppGrayScale:
726 switch (dst_format)
728 case PixelFormat1bppIndexed:
729 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
730 case PixelFormat4bppIndexed:
731 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_4bppIndexed);
732 case PixelFormat8bppIndexed:
733 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
734 case PixelFormat16bppRGB555:
735 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
736 case PixelFormat16bppRGB565:
737 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
738 case PixelFormat16bppARGB1555:
739 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
740 case PixelFormat24bppRGB:
741 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
742 case PixelFormat32bppRGB:
743 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
744 case PixelFormat32bppARGB:
745 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
746 case PixelFormat32bppPARGB:
747 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
748 case PixelFormat48bppRGB:
749 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
750 case PixelFormat64bppARGB:
751 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
752 default:
753 break;
755 break;
756 case PixelFormat16bppRGB555:
757 switch (dst_format)
759 case PixelFormat1bppIndexed:
760 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
761 case PixelFormat4bppIndexed:
762 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_4bppIndexed);
763 case PixelFormat8bppIndexed:
764 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
765 case PixelFormat16bppGrayScale:
766 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
767 case PixelFormat16bppRGB565:
768 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
769 case PixelFormat16bppARGB1555:
770 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
771 case PixelFormat24bppRGB:
772 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
773 case PixelFormat32bppRGB:
774 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
775 case PixelFormat32bppARGB:
776 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
777 case PixelFormat32bppPARGB:
778 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
779 case PixelFormat48bppRGB:
780 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
781 case PixelFormat64bppARGB:
782 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
783 default:
784 break;
786 break;
787 case PixelFormat16bppRGB565:
788 switch (dst_format)
790 case PixelFormat1bppIndexed:
791 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
792 case PixelFormat4bppIndexed:
793 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_4bppIndexed);
794 case PixelFormat8bppIndexed:
795 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
796 case PixelFormat16bppGrayScale:
797 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
798 case PixelFormat16bppRGB555:
799 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
800 case PixelFormat16bppARGB1555:
801 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
802 case PixelFormat24bppRGB:
803 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
804 case PixelFormat32bppRGB:
805 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
806 case PixelFormat32bppARGB:
807 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
808 case PixelFormat32bppPARGB:
809 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
810 case PixelFormat48bppRGB:
811 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
812 case PixelFormat64bppARGB:
813 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
814 default:
815 break;
817 break;
818 case PixelFormat16bppARGB1555:
819 switch (dst_format)
821 case PixelFormat1bppIndexed:
822 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
823 case PixelFormat4bppIndexed:
824 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_4bppIndexed);
825 case PixelFormat8bppIndexed:
826 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
827 case PixelFormat16bppGrayScale:
828 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
829 case PixelFormat16bppRGB555:
830 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
831 case PixelFormat16bppRGB565:
832 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
833 case PixelFormat24bppRGB:
834 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
835 case PixelFormat32bppRGB:
836 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
837 case PixelFormat32bppARGB:
838 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
839 case PixelFormat32bppPARGB:
840 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
841 case PixelFormat48bppRGB:
842 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
843 case PixelFormat64bppARGB:
844 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
845 default:
846 break;
848 break;
849 case PixelFormat24bppRGB:
850 switch (dst_format)
852 case PixelFormat1bppIndexed:
853 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
854 case PixelFormat4bppIndexed:
855 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_4bppIndexed);
856 case PixelFormat8bppIndexed:
857 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
858 case PixelFormat16bppGrayScale:
859 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
860 case PixelFormat16bppRGB555:
861 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
862 case PixelFormat16bppRGB565:
863 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
864 case PixelFormat16bppARGB1555:
865 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
866 case PixelFormat32bppRGB:
867 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
868 case PixelFormat32bppARGB:
869 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
870 case PixelFormat32bppPARGB:
871 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
872 case PixelFormat48bppRGB:
873 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
874 case PixelFormat64bppARGB:
875 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
876 default:
877 break;
879 break;
880 case PixelFormat32bppRGB:
881 switch (dst_format)
883 case PixelFormat1bppIndexed:
884 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
885 case PixelFormat4bppIndexed:
886 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_4bppIndexed);
887 case PixelFormat8bppIndexed:
888 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
889 case PixelFormat16bppGrayScale:
890 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
891 case PixelFormat16bppRGB555:
892 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
893 case PixelFormat16bppRGB565:
894 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
895 case PixelFormat16bppARGB1555:
896 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
897 case PixelFormat24bppRGB:
898 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
899 case PixelFormat32bppARGB:
900 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
901 case PixelFormat32bppPARGB:
902 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
903 case PixelFormat48bppRGB:
904 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
905 case PixelFormat64bppARGB:
906 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
907 default:
908 break;
910 break;
911 case PixelFormat32bppARGB:
912 switch (dst_format)
914 case PixelFormat1bppIndexed:
915 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
916 case PixelFormat4bppIndexed:
917 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_4bppIndexed);
918 case PixelFormat8bppIndexed:
919 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
920 case PixelFormat16bppGrayScale:
921 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
922 case PixelFormat16bppRGB555:
923 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
924 case PixelFormat16bppRGB565:
925 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
926 case PixelFormat16bppARGB1555:
927 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
928 case PixelFormat24bppRGB:
929 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
930 case PixelFormat32bppPARGB:
931 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
932 return Ok;
933 case PixelFormat48bppRGB:
934 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
935 case PixelFormat64bppARGB:
936 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
937 default:
938 break;
940 break;
941 case PixelFormat32bppPARGB:
942 switch (dst_format)
944 case PixelFormat1bppIndexed:
945 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
946 case PixelFormat4bppIndexed:
947 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_4bppIndexed);
948 case PixelFormat8bppIndexed:
949 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
950 case PixelFormat16bppGrayScale:
951 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
952 case PixelFormat16bppRGB555:
953 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
954 case PixelFormat16bppRGB565:
955 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
956 case PixelFormat16bppARGB1555:
957 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
958 case PixelFormat24bppRGB:
959 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
960 case PixelFormat32bppRGB:
961 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
962 case PixelFormat32bppARGB:
963 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
964 case PixelFormat48bppRGB:
965 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
966 case PixelFormat64bppARGB:
967 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
968 default:
969 break;
971 break;
972 case PixelFormat48bppRGB:
973 switch (dst_format)
975 case PixelFormat1bppIndexed:
976 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
977 case PixelFormat4bppIndexed:
978 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_4bppIndexed);
979 case PixelFormat8bppIndexed:
980 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
981 case PixelFormat16bppGrayScale:
982 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
983 case PixelFormat16bppRGB555:
984 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
985 case PixelFormat16bppRGB565:
986 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
987 case PixelFormat16bppARGB1555:
988 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
989 case PixelFormat24bppRGB:
990 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
991 case PixelFormat32bppRGB:
992 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
993 case PixelFormat32bppARGB:
994 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
995 case PixelFormat32bppPARGB:
996 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
997 case PixelFormat64bppARGB:
998 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
999 default:
1000 break;
1002 break;
1003 case PixelFormat64bppARGB:
1004 switch (dst_format)
1006 case PixelFormat1bppIndexed:
1007 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
1008 case PixelFormat4bppIndexed:
1009 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_4bppIndexed);
1010 case PixelFormat8bppIndexed:
1011 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
1012 case PixelFormat16bppGrayScale:
1013 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
1014 case PixelFormat16bppRGB555:
1015 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
1016 case PixelFormat16bppRGB565:
1017 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
1018 case PixelFormat16bppARGB1555:
1019 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
1020 case PixelFormat24bppRGB:
1021 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
1022 case PixelFormat32bppRGB:
1023 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
1024 case PixelFormat32bppARGB:
1025 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
1026 case PixelFormat32bppPARGB:
1027 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
1028 case PixelFormat48bppRGB:
1029 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
1030 default:
1031 break;
1033 break;
1034 case PixelFormat64bppPARGB:
1035 switch (dst_format)
1037 case PixelFormat1bppIndexed:
1038 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
1039 case PixelFormat4bppIndexed:
1040 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_4bppIndexed);
1041 case PixelFormat8bppIndexed:
1042 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
1043 case PixelFormat16bppGrayScale:
1044 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
1045 case PixelFormat16bppRGB555:
1046 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
1047 case PixelFormat16bppRGB565:
1048 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
1049 case PixelFormat16bppARGB1555:
1050 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
1051 case PixelFormat24bppRGB:
1052 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
1053 case PixelFormat32bppRGB:
1054 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
1055 case PixelFormat32bppARGB:
1056 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
1057 case PixelFormat32bppPARGB:
1058 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1059 case PixelFormat48bppRGB:
1060 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1061 case PixelFormat64bppARGB:
1062 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1063 default:
1064 break;
1066 break;
1067 default:
1068 break;
1071 #undef convert_indexed_to_rgb
1072 #undef convert_rgb_to_rgb
1074 return NotImplemented;
1077 /* This function returns a pointer to an array of pixels that represents the
1078 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1079 * flags. It is correct behavior that a user who calls this function with write
1080 * privileges can write to the whole bitmap (not just the area in rect).
1082 * FIXME: only used portion of format is bits per pixel. */
1083 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1084 UINT flags, PixelFormat format, BitmapData* lockeddata)
1086 INT bitspp = PIXELFORMATBPP(format);
1087 GpRect act_rect; /* actual rect to be used */
1088 GpStatus stat;
1089 BOOL unlock;
1091 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1093 if(!lockeddata || !bitmap)
1094 return InvalidParameter;
1095 if(!image_lock(&bitmap->image, &unlock))
1096 return ObjectBusy;
1098 if(rect){
1099 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1100 (rect->Y + rect->Height > bitmap->height) || !flags)
1102 image_unlock(&bitmap->image, unlock);
1103 return InvalidParameter;
1106 act_rect = *rect;
1108 else{
1109 act_rect.X = act_rect.Y = 0;
1110 act_rect.Width = bitmap->width;
1111 act_rect.Height = bitmap->height;
1114 if(bitmap->lockmode)
1116 WARN("bitmap is already locked and cannot be locked again\n");
1117 image_unlock(&bitmap->image, unlock);
1118 return WrongState;
1121 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1123 /* no conversion is necessary; just use the bits directly */
1124 lockeddata->Width = act_rect.Width;
1125 lockeddata->Height = act_rect.Height;
1126 lockeddata->PixelFormat = format;
1127 lockeddata->Reserved = flags;
1128 lockeddata->Stride = bitmap->stride;
1129 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1130 bitmap->stride * act_rect.Y;
1132 bitmap->lockmode = flags | ImageLockModeRead;
1134 image_unlock(&bitmap->image, unlock);
1135 return Ok;
1138 /* Make sure we can convert to the requested format. */
1139 if (flags & ImageLockModeRead)
1141 stat = convert_pixels(0, 0, 0, NULL, format, NULL, 0, NULL, bitmap->format, NULL);
1142 if (stat == NotImplemented)
1144 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1145 image_unlock(&bitmap->image, unlock);
1146 return NotImplemented;
1150 /* If we're opening for writing, make sure we'll be able to write back in
1151 * the original format. */
1152 if (flags & ImageLockModeWrite)
1154 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, NULL, 0, NULL, format, NULL);
1155 if (stat == NotImplemented)
1157 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1158 image_unlock(&bitmap->image, unlock);
1159 return NotImplemented;
1163 lockeddata->Width = act_rect.Width;
1164 lockeddata->Height = act_rect.Height;
1165 lockeddata->PixelFormat = format;
1166 lockeddata->Reserved = flags;
1168 if(!(flags & ImageLockModeUserInputBuf))
1170 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1172 bitmap->bitmapbits = heap_alloc_zero(lockeddata->Stride * act_rect.Height);
1174 if (!bitmap->bitmapbits)
1176 image_unlock(&bitmap->image, unlock);
1177 return OutOfMemory;
1180 lockeddata->Scan0 = bitmap->bitmapbits;
1183 if (flags & ImageLockModeRead)
1185 static BOOL fixme = FALSE;
1187 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1189 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1190 fixme = TRUE;
1193 stat = convert_pixels(act_rect.Width, act_rect.Height,
1194 lockeddata->Stride, lockeddata->Scan0, format, bitmap->image.palette,
1195 bitmap->stride,
1196 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1197 bitmap->format, bitmap->image.palette);
1199 if (stat != Ok)
1201 heap_free(bitmap->bitmapbits);
1202 bitmap->bitmapbits = NULL;
1203 image_unlock(&bitmap->image, unlock);
1204 return stat;
1208 bitmap->lockmode = flags | ImageLockModeRead;
1209 bitmap->lockx = act_rect.X;
1210 bitmap->locky = act_rect.Y;
1212 image_unlock(&bitmap->image, unlock);
1213 return Ok;
1216 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1218 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1220 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1221 return InvalidParameter;
1223 bitmap->image.xres = xdpi;
1224 bitmap->image.yres = ydpi;
1226 return Ok;
1229 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1230 BitmapData* lockeddata)
1232 GpStatus stat;
1233 static BOOL fixme = FALSE;
1234 BOOL unlock;
1236 TRACE("(%p,%p)\n", bitmap, lockeddata);
1238 if(!bitmap || !lockeddata)
1239 return InvalidParameter;
1240 if(!image_lock(&bitmap->image, &unlock))
1241 return ObjectBusy;
1243 if(!bitmap->lockmode)
1245 image_unlock(&bitmap->image, unlock);
1246 return WrongState;
1249 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1250 bitmap->lockmode = 0;
1251 heap_free(bitmap->bitmapbits);
1252 bitmap->bitmapbits = NULL;
1253 image_unlock(&bitmap->image, unlock);
1254 return Ok;
1257 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1259 /* we passed a direct reference; no need to do anything */
1260 bitmap->lockmode = 0;
1261 image_unlock(&bitmap->image, unlock);
1262 return Ok;
1265 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1267 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1268 fixme = TRUE;
1271 /* FIXME: Pass src_palette generated from lockeddata->PixelFormat. */
1272 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1273 bitmap->stride,
1274 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1275 bitmap->format, bitmap->image.palette,
1276 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1278 if (stat != Ok)
1280 ERR("failed to convert pixels; this should never happen\n");
1283 heap_free(bitmap->bitmapbits);
1284 bitmap->bitmapbits = NULL;
1285 bitmap->lockmode = 0;
1287 image_unlock(&bitmap->image, unlock);
1288 return stat;
1291 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1292 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1294 Rect area;
1295 GpStatus stat;
1297 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1299 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1300 x < 0 || y < 0 ||
1301 x + width > srcBitmap->width || y + height > srcBitmap->height)
1303 TRACE("<-- InvalidParameter\n");
1304 return InvalidParameter;
1307 if (format == PixelFormatDontCare)
1308 format = srcBitmap->format;
1310 area.X = gdip_round(x);
1311 area.Y = gdip_round(y);
1312 area.Width = gdip_round(width);
1313 area.Height = gdip_round(height);
1315 stat = GdipCreateBitmapFromScan0(area.Width, area.Height, 0, format, NULL, dstBitmap);
1316 if (stat == Ok)
1318 stat = convert_pixels(area.Width, area.Height, (*dstBitmap)->stride, (*dstBitmap)->bits, (*dstBitmap)->format,
1319 (*dstBitmap)->image.palette, srcBitmap->stride,
1320 srcBitmap->bits + srcBitmap->stride * area.Y + PIXELFORMATBPP(srcBitmap->format) * area.X / 8,
1321 srcBitmap->format, srcBitmap->image.palette);
1323 if (stat == Ok && srcBitmap->image.palette)
1325 ColorPalette *src_palette, *dst_palette;
1327 src_palette = srcBitmap->image.palette;
1329 dst_palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * src_palette->Count);
1331 if (dst_palette)
1333 dst_palette->Flags = src_palette->Flags;
1334 dst_palette->Count = src_palette->Count;
1335 memcpy(dst_palette->Entries, src_palette->Entries, sizeof(ARGB) * src_palette->Count);
1337 heap_free((*dstBitmap)->image.palette);
1338 (*dstBitmap)->image.palette = dst_palette;
1340 else
1341 stat = OutOfMemory;
1344 if (stat != Ok)
1345 GdipDisposeImage(&(*dstBitmap)->image);
1348 if (stat != Ok)
1349 *dstBitmap = NULL;
1351 return stat;
1354 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1355 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1357 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1359 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1362 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1364 TRACE("%p, %p\n", image, cloneImage);
1366 if (!image || !cloneImage)
1367 return InvalidParameter;
1369 if (image->type == ImageTypeBitmap)
1371 GpBitmap *bitmap = (GpBitmap *)image;
1373 return GdipCloneBitmapAreaI(0, 0, bitmap->width, bitmap->height,
1374 bitmap->format, bitmap, (GpBitmap **)cloneImage);
1376 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
1378 GpMetafile *result, *metafile;
1380 metafile = (GpMetafile*)image;
1382 result = heap_alloc_zero(sizeof(*result));
1383 if (!result)
1384 return OutOfMemory;
1386 result->image.type = ImageTypeMetafile;
1387 result->image.format = image->format;
1388 result->image.flags = image->flags;
1389 result->image.frame_count = 1;
1390 result->image.xres = image->xres;
1391 result->image.yres = image->yres;
1392 result->bounds = metafile->bounds;
1393 result->unit = metafile->unit;
1394 result->metafile_type = metafile->metafile_type;
1395 result->hemf = CopyEnhMetaFileW(metafile->hemf, NULL);
1396 list_init(&result->containers);
1398 if (!result->hemf)
1400 heap_free(result);
1401 return OutOfMemory;
1404 *cloneImage = &result->image;
1405 return Ok;
1407 else
1409 WARN("GpImage with no image data (metafile in wrong state?)\n");
1410 return InvalidParameter;
1414 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1415 GpBitmap **bitmap)
1417 GpStatus stat;
1418 IStream *stream;
1420 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1422 if(!filename || !bitmap)
1423 return InvalidParameter;
1425 *bitmap = NULL;
1427 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1429 if(stat != Ok)
1430 return stat;
1432 stat = GdipCreateBitmapFromStream(stream, bitmap);
1434 IStream_Release(stream);
1436 return stat;
1439 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1440 VOID *bits, GpBitmap **bitmap)
1442 DWORD height, stride;
1443 HBITMAP hbm;
1444 void *bmbits;
1445 GpStatus status;
1447 TRACE("(%p, %p, %p)\n", info, bits, bitmap);
1449 if (!info || !bits || !bitmap)
1450 return InvalidParameter;
1452 hbm = CreateDIBSection(0, info, DIB_RGB_COLORS, &bmbits, NULL, 0);
1453 if (!hbm)
1454 return InvalidParameter;
1456 height = abs(info->bmiHeader.biHeight);
1457 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1458 TRACE("height %lu, stride %lu, image size %lu\n", height, stride, height * stride);
1460 memcpy(bmbits, bits, height * stride);
1462 status = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1463 DeleteObject(hbm);
1465 return status;
1468 /* FIXME: no icm */
1469 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1470 GpBitmap **bitmap)
1472 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1474 return GdipCreateBitmapFromFile(filename, bitmap);
1477 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1478 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1480 HBITMAP hbm;
1481 GpStatus stat = InvalidParameter;
1483 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1485 if(!lpBitmapName || !bitmap)
1486 return InvalidParameter;
1488 /* load DIB */
1489 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1490 LR_CREATEDIBSECTION);
1492 if(hbm){
1493 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1494 DeleteObject(hbm);
1497 return stat;
1500 static inline DWORD blend_argb_no_bkgnd_alpha(DWORD src, DWORD bkgnd)
1502 BYTE b = (BYTE)src;
1503 BYTE g = (BYTE)(src >> 8);
1504 BYTE r = (BYTE)(src >> 16);
1505 DWORD alpha = (BYTE)(src >> 24);
1506 return ((b + ((BYTE)bkgnd * (255 - alpha) + 127) / 255) |
1507 (g + ((BYTE)(bkgnd >> 8) * (255 - alpha) + 127) / 255) << 8 |
1508 (r + ((BYTE)(bkgnd >> 16) * (255 - alpha) + 127) / 255) << 16 |
1509 (alpha << 24));
1512 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1513 HBITMAP* hbmReturn, ARGB background)
1515 GpStatus stat;
1516 HBITMAP result;
1517 UINT width, height;
1518 BITMAPINFOHEADER bih;
1519 LPBYTE bits;
1520 BOOL unlock;
1522 TRACE("(%p,%p,%lx)\n", bitmap, hbmReturn, background);
1524 if (!bitmap || !hbmReturn) return InvalidParameter;
1525 if (!image_lock(&bitmap->image, &unlock)) return ObjectBusy;
1527 GdipGetImageWidth(&bitmap->image, &width);
1528 GdipGetImageHeight(&bitmap->image, &height);
1530 bih.biSize = sizeof(bih);
1531 bih.biWidth = width;
1532 bih.biHeight = height;
1533 bih.biPlanes = 1;
1534 bih.biBitCount = 32;
1535 bih.biCompression = BI_RGB;
1536 bih.biSizeImage = 0;
1537 bih.biXPelsPerMeter = 0;
1538 bih.biYPelsPerMeter = 0;
1539 bih.biClrUsed = 0;
1540 bih.biClrImportant = 0;
1542 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1543 if (!result)
1545 image_unlock(&bitmap->image, unlock);
1546 return GenericError;
1549 stat = convert_pixels(width, height, -width*4,
1550 bits + (width * 4 * (height - 1)), PixelFormat32bppPARGB, bitmap->image.palette,
1551 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette);
1552 if (stat != Ok)
1554 DeleteObject(result);
1555 image_unlock(&bitmap->image, unlock);
1556 return stat;
1559 if (background & 0xffffff)
1561 DWORD *ptr;
1562 UINT i;
1563 for (ptr = (DWORD*)bits, i = 0; i < width * height; ptr++, i++)
1565 if ((*ptr & 0xff000000) == 0xff000000) continue;
1566 *ptr = blend_argb_no_bkgnd_alpha(*ptr, background);
1570 *hbmReturn = result;
1571 image_unlock(&bitmap->image, unlock);
1572 return Ok;
1575 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1576 GpGraphics* target, GpBitmap** bitmap)
1578 GpStatus ret;
1580 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1582 if(!target || !bitmap)
1583 return InvalidParameter;
1585 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
1586 NULL, bitmap);
1588 if (ret == Ok)
1590 GdipGetDpiX(target, &(*bitmap)->image.xres);
1591 GdipGetDpiY(target, &(*bitmap)->image.yres);
1594 return ret;
1597 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1599 GpStatus stat;
1600 ICONINFO iinfo;
1601 BITMAP bm;
1602 int ret;
1603 UINT width, height, stride;
1604 GpRect rect;
1605 BitmapData lockeddata;
1606 HDC screendc;
1607 BOOL has_alpha;
1608 int x, y;
1609 BITMAPINFOHEADER bih;
1610 DWORD *src;
1611 BYTE *dst_row;
1612 DWORD *dst;
1614 TRACE("%p, %p\n", hicon, bitmap);
1616 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1617 return InvalidParameter;
1619 /* get the size of the icon */
1620 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1621 if (ret == 0) {
1622 DeleteObject(iinfo.hbmColor);
1623 DeleteObject(iinfo.hbmMask);
1624 return GenericError;
1627 width = bm.bmWidth;
1628 height = iinfo.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
1629 stride = width * 4;
1631 stat = GdipCreateBitmapFromScan0(width, height, stride, PixelFormat32bppARGB, NULL, bitmap);
1632 if (stat != Ok) {
1633 DeleteObject(iinfo.hbmColor);
1634 DeleteObject(iinfo.hbmMask);
1635 return stat;
1638 rect.X = 0;
1639 rect.Y = 0;
1640 rect.Width = width;
1641 rect.Height = height;
1643 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1644 if (stat != Ok) {
1645 DeleteObject(iinfo.hbmColor);
1646 DeleteObject(iinfo.hbmMask);
1647 GdipDisposeImage(&(*bitmap)->image);
1648 return stat;
1651 bih.biSize = sizeof(bih);
1652 bih.biWidth = width;
1653 bih.biHeight = iinfo.hbmColor ? -height: -height * 2;
1654 bih.biPlanes = 1;
1655 bih.biBitCount = 32;
1656 bih.biCompression = BI_RGB;
1657 bih.biSizeImage = 0;
1658 bih.biXPelsPerMeter = 0;
1659 bih.biYPelsPerMeter = 0;
1660 bih.biClrUsed = 0;
1661 bih.biClrImportant = 0;
1663 screendc = CreateCompatibleDC(0);
1664 if (iinfo.hbmColor)
1666 GetDIBits(screendc, iinfo.hbmColor, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1668 if (bm.bmBitsPixel == 32)
1670 has_alpha = FALSE;
1672 /* If any pixel has a non-zero alpha, ignore hbmMask */
1673 src = (DWORD*)lockeddata.Scan0;
1674 for (x=0; x<width && !has_alpha; x++)
1675 for (y=0; y<height && !has_alpha; y++)
1676 if ((*src++ & 0xff000000) != 0)
1677 has_alpha = TRUE;
1679 else has_alpha = FALSE;
1681 else
1683 GetDIBits(screendc, iinfo.hbmMask, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1684 has_alpha = FALSE;
1687 if (!has_alpha)
1689 if (iinfo.hbmMask)
1691 BYTE *bits = heap_alloc(height * stride);
1693 /* read alpha data from the mask */
1694 if (iinfo.hbmColor)
1695 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1696 else
1697 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1699 src = (DWORD*)bits;
1700 dst_row = lockeddata.Scan0;
1701 for (y=0; y<height; y++)
1703 dst = (DWORD*)dst_row;
1704 for (x=0; x<height; x++)
1706 DWORD src_value = *src++;
1707 if (src_value)
1708 *dst++ = 0;
1709 else
1710 *dst++ |= 0xff000000;
1712 dst_row += lockeddata.Stride;
1715 heap_free(bits);
1717 else
1719 /* set constant alpha of 255 */
1720 dst_row = lockeddata.Scan0;
1721 for (y=0; y<height; y++)
1723 dst = (DWORD*)dst_row;
1724 for (x=0; x<height; x++)
1725 *dst++ |= 0xff000000;
1726 dst_row += lockeddata.Stride;
1731 DeleteDC(screendc);
1733 DeleteObject(iinfo.hbmColor);
1734 DeleteObject(iinfo.hbmMask);
1736 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1738 return Ok;
1741 static void generate_halftone_palette(ARGB *entries, UINT count)
1743 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1744 UINT i;
1746 for (i=0; i<8 && i<count; i++)
1748 entries[i] = 0xff000000;
1749 if (i&1) entries[i] |= 0x800000;
1750 if (i&2) entries[i] |= 0x8000;
1751 if (i&4) entries[i] |= 0x80;
1754 if (8 < count)
1755 entries[i] = 0xffc0c0c0;
1757 for (i=9; i<16 && i<count; i++)
1759 entries[i] = 0xff000000;
1760 if (i&1) entries[i] |= 0xff0000;
1761 if (i&2) entries[i] |= 0xff00;
1762 if (i&4) entries[i] |= 0xff;
1765 for (i=16; i<40 && i<count; i++)
1767 entries[i] = 0;
1770 for (i=40; i<256 && i<count; i++)
1772 entries[i] = 0xff000000;
1773 entries[i] |= halftone_values[(i-40)%6];
1774 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1775 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1779 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1781 HDC screendc = CreateCompatibleDC(0);
1783 if (!screendc) return GenericError;
1785 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1786 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1788 DeleteDC(screendc);
1790 return Ok;
1793 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1794 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1796 HBITMAP hbitmap=NULL;
1797 INT row_size, dib_stride;
1798 BYTE *bits=NULL, *own_bits=NULL;
1799 REAL xres, yres;
1800 GpStatus stat;
1802 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1804 if (!bitmap) return InvalidParameter;
1806 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1807 *bitmap = NULL;
1808 return InvalidParameter;
1811 if(scan0 && !stride)
1812 return InvalidParameter;
1814 stat = get_screen_resolution(&xres, &yres);
1815 if (stat != Ok) return stat;
1817 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1818 dib_stride = (row_size + 3) & ~3;
1820 if(stride == 0)
1821 stride = dib_stride;
1823 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1825 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
1826 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
1828 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1829 pbmi->bmiHeader.biWidth = width;
1830 pbmi->bmiHeader.biHeight = -height;
1831 pbmi->bmiHeader.biPlanes = 1;
1832 /* FIXME: use the rest of the data from format */
1833 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1834 pbmi->bmiHeader.biCompression = BI_RGB;
1835 pbmi->bmiHeader.biSizeImage = 0;
1836 pbmi->bmiHeader.biXPelsPerMeter = 0;
1837 pbmi->bmiHeader.biYPelsPerMeter = 0;
1838 pbmi->bmiHeader.biClrUsed = 0;
1839 pbmi->bmiHeader.biClrImportant = 0;
1841 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1843 if (!hbitmap) return GenericError;
1845 stride = dib_stride;
1847 else
1849 /* Not a GDI format; don't try to make an HBITMAP. */
1850 if (scan0)
1851 bits = scan0;
1852 else
1854 INT size = abs(stride) * height;
1856 own_bits = bits = heap_alloc_zero(size);
1857 if (!own_bits) return OutOfMemory;
1859 if (stride < 0)
1860 bits += stride * (1 - height);
1864 *bitmap = heap_alloc_zero(sizeof(GpBitmap));
1865 if(!*bitmap)
1867 DeleteObject(hbitmap);
1868 heap_free(own_bits);
1869 return OutOfMemory;
1872 (*bitmap)->image.type = ImageTypeBitmap;
1873 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1874 (*bitmap)->image.flags = ImageFlagsNone;
1875 (*bitmap)->image.frame_count = 1;
1876 (*bitmap)->image.current_frame = 0;
1877 (*bitmap)->image.palette = NULL;
1878 (*bitmap)->image.xres = xres;
1879 (*bitmap)->image.yres = yres;
1880 (*bitmap)->width = width;
1881 (*bitmap)->height = height;
1882 (*bitmap)->format = format;
1883 (*bitmap)->image.decoder = NULL;
1884 (*bitmap)->image.encoder = NULL;
1885 (*bitmap)->hbitmap = hbitmap;
1886 (*bitmap)->hdc = NULL;
1887 (*bitmap)->bits = bits;
1888 (*bitmap)->stride = stride;
1889 (*bitmap)->own_bits = own_bits;
1890 (*bitmap)->metadata_reader = NULL;
1891 (*bitmap)->prop_count = 0;
1892 (*bitmap)->prop_item = NULL;
1894 /* set format-related flags */
1895 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1896 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1898 if (format == PixelFormat1bppIndexed ||
1899 format == PixelFormat4bppIndexed ||
1900 format == PixelFormat8bppIndexed)
1902 (*bitmap)->image.palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1904 if (!(*bitmap)->image.palette)
1906 GdipDisposeImage(&(*bitmap)->image);
1907 *bitmap = NULL;
1908 return OutOfMemory;
1911 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1913 if (format == PixelFormat1bppIndexed)
1915 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1916 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1917 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1919 else
1921 if (format == PixelFormat8bppIndexed)
1922 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1924 generate_halftone_palette((*bitmap)->image.palette->Entries,
1925 (*bitmap)->image.palette->Count);
1929 TRACE("<-- %p\n", *bitmap);
1931 return Ok;
1934 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1935 GpBitmap **bitmap)
1937 GpStatus stat;
1939 TRACE("%p %p\n", stream, bitmap);
1941 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1943 if(stat != Ok)
1944 return stat;
1946 if((*bitmap)->image.type != ImageTypeBitmap){
1947 GdipDisposeImage(&(*bitmap)->image);
1948 *bitmap = NULL;
1949 return GenericError; /* FIXME: what error to return? */
1952 return Ok;
1955 /* FIXME: no icm */
1956 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1957 GpBitmap **bitmap)
1959 TRACE("%p %p\n", stream, bitmap);
1961 return GdipCreateBitmapFromStream(stream, bitmap);
1964 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1965 GpCachedBitmap **cachedbmp)
1967 GpStatus stat;
1969 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1971 if(!bitmap || !graphics || !cachedbmp)
1972 return InvalidParameter;
1974 *cachedbmp = heap_alloc_zero(sizeof(GpCachedBitmap));
1975 if(!*cachedbmp)
1976 return OutOfMemory;
1978 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1979 if(stat != Ok){
1980 heap_free(*cachedbmp);
1981 return stat;
1984 return Ok;
1987 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1989 GpStatus stat;
1990 BitmapData lockeddata;
1991 ULONG andstride, xorstride, bitssize;
1992 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1993 UINT x, y;
1995 TRACE("(%p, %p)\n", bitmap, hicon);
1997 if (!bitmap || !hicon)
1998 return InvalidParameter;
2000 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
2001 PixelFormat32bppPARGB, &lockeddata);
2002 if (stat == Ok)
2004 andstride = ((lockeddata.Width+31)/32)*4;
2005 xorstride = lockeddata.Width*4;
2006 bitssize = (andstride + xorstride) * lockeddata.Height;
2008 andbits = heap_alloc_zero(bitssize);
2010 if (andbits)
2012 xorbits = andbits + andstride * lockeddata.Height;
2014 for (y=0; y<lockeddata.Height; y++)
2016 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
2018 androw = andbits + andstride * y;
2019 for (x=0; x<lockeddata.Width; x++)
2020 if (srcrow[3+4*x] >= 128)
2021 androw[x/8] |= 1 << (7-x%8);
2023 xorrow = xorbits + xorstride * y;
2024 memcpy(xorrow, srcrow, xorstride);
2027 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
2028 andbits, xorbits);
2030 heap_free(andbits);
2032 else
2033 stat = OutOfMemory;
2035 GdipBitmapUnlockBits(bitmap, &lockeddata);
2038 return stat;
2041 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
2043 TRACE("%p\n", cachedbmp);
2045 if(!cachedbmp)
2046 return InvalidParameter;
2048 GdipDisposeImage(cachedbmp->image);
2049 heap_free(cachedbmp);
2051 return Ok;
2054 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2055 GpCachedBitmap *cachedbmp, INT x, INT y)
2057 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2059 if(!graphics || !cachedbmp)
2060 return InvalidParameter;
2062 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2065 /* Internal utility function: Replace the image data of dst with that of src,
2066 * and free src. */
2067 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2069 assert(src->image.type == ImageTypeBitmap);
2070 assert(dst->image.type == ImageTypeBitmap);
2072 heap_free(dst->bitmapbits);
2073 heap_free(dst->own_bits);
2074 DeleteDC(dst->hdc);
2075 DeleteObject(dst->hbitmap);
2077 if (clobber_palette)
2079 heap_free(dst->image.palette);
2080 dst->image.palette = src->image.palette;
2082 else
2083 heap_free(src->image.palette);
2085 dst->image.xres = src->image.xres;
2086 dst->image.yres = src->image.yres;
2087 dst->width = src->width;
2088 dst->height = src->height;
2089 dst->format = src->format;
2090 dst->hbitmap = src->hbitmap;
2091 dst->hdc = src->hdc;
2092 dst->bits = src->bits;
2093 dst->stride = src->stride;
2094 dst->own_bits = src->own_bits;
2095 if (dst->metadata_reader)
2096 IWICMetadataReader_Release(dst->metadata_reader);
2097 dst->metadata_reader = src->metadata_reader;
2098 heap_free(dst->prop_item);
2099 dst->prop_item = src->prop_item;
2100 dst->prop_count = src->prop_count;
2101 if (dst->image.decoder)
2102 IWICBitmapDecoder_Release(dst->image.decoder);
2103 dst->image.decoder = src->image.decoder;
2104 terminate_encoder_wic(&dst->image); /* terminate active encoder before overwriting with src */
2105 dst->image.encoder = src->image.encoder;
2106 dst->image.frame_count = src->image.frame_count;
2107 dst->image.current_frame = src->image.current_frame;
2108 dst->image.format = src->image.format;
2110 src->image.type = ~0;
2111 heap_free(src);
2114 static GpStatus free_image_data(GpImage *image)
2116 if(!image)
2117 return InvalidParameter;
2119 if (image->type == ImageTypeBitmap)
2121 heap_free(((GpBitmap*)image)->bitmapbits);
2122 heap_free(((GpBitmap*)image)->own_bits);
2123 DeleteDC(((GpBitmap*)image)->hdc);
2124 DeleteObject(((GpBitmap*)image)->hbitmap);
2125 if (((GpBitmap*)image)->metadata_reader)
2126 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2127 heap_free(((GpBitmap*)image)->prop_item);
2129 else if (image->type == ImageTypeMetafile)
2130 METAFILE_Free((GpMetafile *)image);
2131 else
2133 WARN("invalid image: %p\n", image);
2134 return ObjectBusy;
2136 if (image->decoder)
2137 IWICBitmapDecoder_Release(image->decoder);
2138 terminate_encoder_wic(image);
2139 heap_free(image->palette);
2141 return Ok;
2144 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2146 GpStatus status;
2148 TRACE("%p\n", image);
2150 status = free_image_data(image);
2151 if (status != Ok) return status;
2152 image->type = ~0;
2153 heap_free(image);
2155 return Ok;
2158 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2160 static int calls;
2162 TRACE("(%p,%p)\n", image, item);
2164 if(!image || !item)
2165 return InvalidParameter;
2167 if (!(calls++))
2168 FIXME("not implemented\n");
2170 return NotImplemented;
2173 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2175 static int calls;
2177 TRACE("(%p,%p)\n", image, item);
2179 if (!(calls++))
2180 FIXME("not implemented\n");
2182 return NotImplemented;
2185 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2186 GpUnit *srcUnit)
2188 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2190 if(!image || !srcRect || !srcUnit)
2191 return InvalidParameter;
2192 if(image->type == ImageTypeMetafile){
2193 *srcRect = ((GpMetafile*)image)->bounds;
2194 *srcUnit = ((GpMetafile*)image)->unit;
2196 else if(image->type == ImageTypeBitmap){
2197 srcRect->X = srcRect->Y = 0.0;
2198 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2199 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2200 *srcUnit = UnitPixel;
2202 else{
2203 WARN("GpImage with no image data\n");
2204 return InvalidParameter;
2207 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2208 srcRect->Width, srcRect->Height, *srcUnit);
2210 return Ok;
2213 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2214 REAL *height)
2216 TRACE("%p %p %p\n", image, width, height);
2218 if(!image || !height || !width)
2219 return InvalidParameter;
2221 if(image->type == ImageTypeMetafile){
2222 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit,
2223 image->yres, ((GpMetafile*)image)->printer_display);
2224 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit,
2225 image->xres, ((GpMetafile*)image)->printer_display);
2227 else if(image->type == ImageTypeBitmap){
2228 *height = ((GpBitmap*)image)->height;
2229 *width = ((GpBitmap*)image)->width;
2231 else{
2232 WARN("GpImage with no image data\n");
2233 return InvalidParameter;
2236 TRACE("returning (%f, %f)\n", *height, *width);
2237 return Ok;
2240 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2241 GpGraphics **graphics)
2243 HDC hdc;
2244 GpStatus stat;
2246 TRACE("%p %p\n", image, graphics);
2248 if(!image || !graphics)
2249 return InvalidParameter;
2251 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2253 hdc = ((GpBitmap*)image)->hdc;
2255 if(!hdc){
2256 hdc = CreateCompatibleDC(0);
2257 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2258 ((GpBitmap*)image)->hdc = hdc;
2261 stat = GdipCreateFromHDC(hdc, graphics);
2263 if (stat == Ok)
2265 (*graphics)->image = image;
2266 (*graphics)->image_type = image->type;
2267 (*graphics)->xres = image->xres;
2268 (*graphics)->yres = image->yres;
2271 else if (image->type == ImageTypeMetafile)
2272 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2273 else
2274 stat = graphics_from_image(image, graphics);
2276 return stat;
2279 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2281 TRACE("%p %p\n", image, height);
2283 if(!image || !height)
2284 return InvalidParameter;
2286 if(image->type == ImageTypeMetafile)
2287 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit,
2288 image->yres, ((GpMetafile*)image)->printer_display);
2289 else if(image->type == ImageTypeBitmap)
2290 *height = ((GpBitmap*)image)->height;
2291 else
2293 WARN("GpImage with no image data\n");
2294 return InvalidParameter;
2297 TRACE("returning %d\n", *height);
2299 return Ok;
2302 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2304 if(!image || !res)
2305 return InvalidParameter;
2307 *res = image->xres;
2309 TRACE("(%p) <-- %0.2f\n", image, *res);
2311 return Ok;
2314 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2316 TRACE("%p %p\n", image, size);
2318 if(!image || !size)
2319 return InvalidParameter;
2321 if (image->type == ImageTypeMetafile)
2323 *size = 0;
2324 return GenericError;
2327 if (!image->palette || image->palette->Count == 0)
2328 *size = sizeof(ColorPalette);
2329 else
2330 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2332 TRACE("<-- %u\n", *size);
2334 return Ok;
2337 /* FIXME: test this function for non-bitmap types */
2338 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2340 TRACE("%p %p\n", image, format);
2342 if(!image || !format)
2343 return InvalidParameter;
2345 if(image->type != ImageTypeBitmap)
2346 *format = PixelFormat24bppRGB;
2347 else
2348 *format = ((GpBitmap*) image)->format;
2350 return Ok;
2353 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2355 TRACE("(%p, %p)\n", image, format);
2357 if(!image || !format)
2358 return InvalidParameter;
2360 memcpy(format, &image->format, sizeof(GUID));
2362 return Ok;
2365 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2367 TRACE("%p %p\n", image, type);
2369 if(!image || !type)
2370 return InvalidParameter;
2372 *type = image->type;
2374 return Ok;
2377 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2379 if(!image || !res)
2380 return InvalidParameter;
2382 *res = image->yres;
2384 TRACE("(%p) <-- %0.2f\n", image, *res);
2386 return Ok;
2389 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2391 TRACE("%p %p\n", image, width);
2393 if(!image || !width)
2394 return InvalidParameter;
2396 if(image->type == ImageTypeMetafile)
2397 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit,
2398 image->xres, ((GpMetafile*)image)->printer_display);
2399 else if(image->type == ImageTypeBitmap)
2400 *width = ((GpBitmap*)image)->width;
2401 else
2403 WARN("GpImage with no image data\n");
2404 return InvalidParameter;
2407 TRACE("returning %d\n", *width);
2409 return Ok;
2412 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2414 TRACE("(%p, %p)\n", image, num);
2416 if (!image || !num) return InvalidParameter;
2418 *num = 0;
2420 if (image->type == ImageTypeBitmap)
2422 if (((GpBitmap *)image)->prop_item)
2424 *num = ((GpBitmap *)image)->prop_count;
2425 return Ok;
2428 if (((GpBitmap *)image)->metadata_reader)
2429 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2432 return Ok;
2435 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2437 HRESULT hr;
2438 IWICMetadataReader *reader;
2439 IWICEnumMetadataItem *enumerator;
2440 UINT prop_count, i;
2441 ULONG items_returned;
2443 TRACE("(%p, %u, %p)\n", image, num, list);
2445 if (!image || !list) return InvalidParameter;
2447 if (image->type != ImageTypeBitmap)
2449 FIXME("Not implemented for type %d\n", image->type);
2450 return NotImplemented;
2453 if (((GpBitmap *)image)->prop_item)
2455 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2457 for (i = 0; i < num; i++)
2459 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2462 return Ok;
2465 reader = ((GpBitmap *)image)->metadata_reader;
2466 if (!reader)
2468 if (num != 0) return InvalidParameter;
2469 return Ok;
2472 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2473 if (FAILED(hr)) return hresult_to_status(hr);
2475 if (num != prop_count) return InvalidParameter;
2477 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2478 if (FAILED(hr)) return hresult_to_status(hr);
2480 IWICEnumMetadataItem_Reset(enumerator);
2482 for (i = 0; i < num; i++)
2484 PROPVARIANT id;
2486 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2487 if (hr != S_OK) break;
2489 if (id.vt != VT_UI2)
2491 FIXME("not supported propvariant type for id: %u\n", id.vt);
2492 list[i] = 0;
2493 continue;
2495 list[i] = id.uiVal;
2498 IWICEnumMetadataItem_Release(enumerator);
2500 return hr == S_OK ? Ok : hresult_to_status(hr);
2503 static UINT propvariant_size(PROPVARIANT *value)
2505 switch (value->vt & ~VT_VECTOR)
2507 case VT_EMPTY:
2508 return 0;
2509 case VT_I1:
2510 case VT_UI1:
2511 if (!(value->vt & VT_VECTOR)) return 1;
2512 return value->caub.cElems;
2513 case VT_I2:
2514 case VT_UI2:
2515 if (!(value->vt & VT_VECTOR)) return 2;
2516 return value->caui.cElems * 2;
2517 case VT_I4:
2518 case VT_UI4:
2519 case VT_R4:
2520 if (!(value->vt & VT_VECTOR)) return 4;
2521 return value->caul.cElems * 4;
2522 case VT_I8:
2523 case VT_UI8:
2524 case VT_R8:
2525 if (!(value->vt & VT_VECTOR)) return 8;
2526 return value->cauh.cElems * 8;
2527 case VT_LPSTR:
2528 return value->pszVal ? strlen(value->pszVal) + 1 : 0;
2529 case VT_BLOB:
2530 return value->blob.cbSize;
2531 default:
2532 FIXME("not supported variant type %d\n", value->vt);
2533 return 0;
2537 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2539 HRESULT hr;
2540 IWICMetadataReader *reader;
2541 PROPVARIANT id, value;
2543 TRACE("(%p,%#lx,%p)\n", image, propid, size);
2545 if (!size || !image) return InvalidParameter;
2547 if (image->type != ImageTypeBitmap)
2549 FIXME("Not implemented for type %d\n", image->type);
2550 return NotImplemented;
2553 if (((GpBitmap *)image)->prop_item)
2555 UINT i;
2557 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2559 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2561 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2562 return Ok;
2566 return PropertyNotFound;
2569 reader = ((GpBitmap *)image)->metadata_reader;
2570 if (!reader) return PropertyNotFound;
2572 id.vt = VT_UI2;
2573 id.uiVal = propid;
2574 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2575 if (FAILED(hr)) return PropertyNotFound;
2577 *size = propvariant_size(&value);
2578 if (*size) *size += sizeof(PropertyItem);
2579 PropVariantClear(&value);
2581 return Ok;
2584 #ifndef PropertyTagTypeSByte
2585 #define PropertyTagTypeSByte 6
2586 #define PropertyTagTypeSShort 8
2587 #define PropertyTagTypeFloat 11
2588 #define PropertyTagTypeDouble 12
2589 #endif
2591 static UINT vt_to_itemtype(UINT vt)
2593 static const struct
2595 UINT vt, type;
2596 } vt2type[] =
2598 { VT_I1, PropertyTagTypeSByte },
2599 { VT_UI1, PropertyTagTypeByte },
2600 { VT_I2, PropertyTagTypeSShort },
2601 { VT_UI2, PropertyTagTypeShort },
2602 { VT_I4, PropertyTagTypeSLONG },
2603 { VT_UI4, PropertyTagTypeLong },
2604 { VT_I8, PropertyTagTypeSRational },
2605 { VT_UI8, PropertyTagTypeRational },
2606 { VT_R4, PropertyTagTypeFloat },
2607 { VT_R8, PropertyTagTypeDouble },
2608 { VT_LPSTR, PropertyTagTypeASCII },
2609 { VT_BLOB, PropertyTagTypeUndefined }
2611 UINT i;
2612 for (i = 0; i < ARRAY_SIZE(vt2type); i++)
2614 if (vt2type[i].vt == vt) return vt2type[i].type;
2616 FIXME("not supported variant type %u\n", vt);
2617 return 0;
2620 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2621 UINT size, PROPID id)
2623 UINT item_size, item_type;
2625 item_size = propvariant_size(value);
2626 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2628 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2629 if (!item_type) return InvalidParameter;
2631 item->value = item + 1;
2633 switch (value->vt & ~VT_VECTOR)
2635 case VT_I1:
2636 case VT_UI1:
2637 if (!(value->vt & VT_VECTOR))
2638 *(BYTE *)item->value = value->bVal;
2639 else
2640 memcpy(item->value, value->caub.pElems, item_size);
2641 break;
2642 case VT_I2:
2643 case VT_UI2:
2644 if (!(value->vt & VT_VECTOR))
2645 *(USHORT *)item->value = value->uiVal;
2646 else
2647 memcpy(item->value, value->caui.pElems, item_size);
2648 break;
2649 case VT_I4:
2650 case VT_UI4:
2651 case VT_R4:
2652 if (!(value->vt & VT_VECTOR))
2653 *(ULONG *)item->value = value->ulVal;
2654 else
2655 memcpy(item->value, value->caul.pElems, item_size);
2656 break;
2657 case VT_I8:
2658 case VT_UI8:
2659 case VT_R8:
2660 if (!(value->vt & VT_VECTOR))
2661 *(ULONGLONG *)item->value = value->uhVal.QuadPart;
2662 else
2663 memcpy(item->value, value->cauh.pElems, item_size);
2664 break;
2665 case VT_LPSTR:
2666 memcpy(item->value, value->pszVal, item_size);
2667 break;
2668 case VT_BLOB:
2669 memcpy(item->value, value->blob.pBlobData, item_size);
2670 break;
2671 default:
2672 FIXME("not supported variant type %d\n", value->vt);
2673 return InvalidParameter;
2676 item->length = item_size;
2677 item->type = item_type;
2678 item->id = id;
2680 return Ok;
2683 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2684 PropertyItem *buffer)
2686 GpStatus stat;
2687 HRESULT hr;
2688 IWICMetadataReader *reader;
2689 PROPVARIANT id, value;
2691 TRACE("(%p,%#lx,%u,%p)\n", image, propid, size, buffer);
2693 if (!image || !buffer) return InvalidParameter;
2695 if (image->type != ImageTypeBitmap)
2697 FIXME("Not implemented for type %d\n", image->type);
2698 return NotImplemented;
2701 if (((GpBitmap *)image)->prop_item)
2703 UINT i;
2705 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2707 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2709 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2710 return InvalidParameter;
2712 *buffer = ((GpBitmap *)image)->prop_item[i];
2713 buffer->value = buffer + 1;
2714 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2715 return Ok;
2719 return PropertyNotFound;
2722 reader = ((GpBitmap *)image)->metadata_reader;
2723 if (!reader) return PropertyNotFound;
2725 id.vt = VT_UI2;
2726 id.uiVal = propid;
2727 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2728 if (FAILED(hr)) return PropertyNotFound;
2730 stat = propvariant_to_item(&value, buffer, size, propid);
2731 PropVariantClear(&value);
2733 return stat;
2736 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2738 HRESULT hr;
2739 IWICMetadataReader *reader;
2740 IWICEnumMetadataItem *enumerator;
2741 UINT prop_count, prop_size, i;
2742 PROPVARIANT id, value;
2744 TRACE("(%p,%p,%p)\n", image, size, count);
2746 if (!image || !size || !count) return InvalidParameter;
2748 if (image->type != ImageTypeBitmap)
2750 FIXME("Not implemented for type %d\n", image->type);
2751 return NotImplemented;
2754 if (((GpBitmap *)image)->prop_item)
2756 *count = ((GpBitmap *)image)->prop_count;
2757 *size = 0;
2759 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2761 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2764 return Ok;
2767 reader = ((GpBitmap *)image)->metadata_reader;
2768 if (!reader) return PropertyNotFound;
2770 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2771 if (FAILED(hr)) return hresult_to_status(hr);
2773 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2774 if (FAILED(hr)) return hresult_to_status(hr);
2776 IWICEnumMetadataItem_Reset(enumerator);
2778 prop_size = 0;
2780 PropVariantInit(&id);
2781 PropVariantInit(&value);
2783 for (i = 0; i < prop_count; i++)
2785 ULONG items_returned;
2786 UINT item_size;
2788 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2789 if (hr != S_OK) break;
2791 item_size = propvariant_size(&value);
2792 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2794 PropVariantClear(&id);
2795 PropVariantClear(&value);
2798 IWICEnumMetadataItem_Release(enumerator);
2800 if (hr != S_OK) return PropertyNotFound;
2802 *count = prop_count;
2803 *size = prop_size;
2804 return Ok;
2807 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2808 UINT count, PropertyItem *buf)
2810 GpStatus status;
2811 HRESULT hr;
2812 IWICMetadataReader *reader;
2813 IWICEnumMetadataItem *enumerator;
2814 UINT prop_count, prop_size, i;
2815 PROPVARIANT id, value;
2816 char *item_value;
2818 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2820 if (!image || !buf) return InvalidParameter;
2822 if (image->type != ImageTypeBitmap)
2824 FIXME("Not implemented for type %d\n", image->type);
2825 return NotImplemented;
2828 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2829 if (status != Ok) return status;
2831 if (prop_count != count || prop_size != size) return InvalidParameter;
2833 if (((GpBitmap *)image)->prop_item)
2835 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2837 item_value = (char *)(buf + prop_count);
2839 for (i = 0; i < prop_count; i++)
2841 buf[i].value = item_value;
2842 item_value += buf[i].length;
2845 return Ok;
2848 reader = ((GpBitmap *)image)->metadata_reader;
2849 if (!reader) return PropertyNotFound;
2851 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2852 if (FAILED(hr)) return hresult_to_status(hr);
2854 IWICEnumMetadataItem_Reset(enumerator);
2856 item_value = (char *)(buf + prop_count);
2858 PropVariantInit(&id);
2859 PropVariantInit(&value);
2861 for (i = 0; i < prop_count; i++)
2863 PropertyItem *item;
2864 ULONG items_returned;
2865 UINT item_size;
2867 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2868 if (hr != S_OK) break;
2870 if (id.vt != VT_UI2)
2872 FIXME("not supported propvariant type for id: %u\n", id.vt);
2873 continue;
2876 item_size = propvariant_size(&value);
2877 if (item_size)
2879 item = heap_alloc(item_size + sizeof(*item));
2881 propvariant_to_item(&value, item, item_size + sizeof(*item), id.uiVal);
2882 buf[i].id = item->id;
2883 buf[i].type = item->type;
2884 buf[i].length = item_size;
2885 buf[i].value = item_value;
2886 memcpy(item_value, item->value, item_size);
2887 item_value += item_size;
2889 heap_free(item);
2892 PropVariantClear(&id);
2893 PropVariantClear(&value);
2896 IWICEnumMetadataItem_Release(enumerator);
2898 if (hr != S_OK) return PropertyNotFound;
2900 return Ok;
2903 struct image_format_dimension
2905 const GUID *format;
2906 const GUID *dimension;
2909 static const struct image_format_dimension image_format_dimensions[] =
2911 {&ImageFormatGIF, &FrameDimensionTime},
2912 {&ImageFormatIcon, &FrameDimensionResolution},
2913 {NULL}
2916 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2917 GDIPCONST GUID* dimensionID, UINT* count)
2919 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2921 if(!image || !count)
2922 return InvalidParameter;
2924 if (!dimensionID ||
2925 IsEqualGUID(dimensionID, &image->format) ||
2926 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2927 IsEqualGUID(dimensionID, &FrameDimensionTime))
2929 *count = image->frame_count;
2930 return Ok;
2933 return InvalidParameter;
2936 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2937 UINT* count)
2939 TRACE("(%p, %p)\n", image, count);
2941 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2943 if(!image || !count)
2944 return InvalidParameter;
2946 *count = 1;
2948 return Ok;
2951 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2952 GUID* dimensionIDs, UINT count)
2954 int i;
2955 const GUID *result=NULL;
2957 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2959 if(!image || !dimensionIDs || count != 1)
2960 return InvalidParameter;
2962 for (i=0; image_format_dimensions[i].format; i++)
2964 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2966 result = image_format_dimensions[i].dimension;
2967 break;
2971 if (!result)
2972 result = &FrameDimensionPage;
2974 memcpy(dimensionIDs, result, sizeof(GUID));
2976 return Ok;
2979 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2980 GpImage **image)
2982 GpStatus stat;
2983 IStream *stream;
2985 TRACE("(%s) %p\n", debugstr_w(filename), image);
2987 if (!filename || !image)
2988 return InvalidParameter;
2990 *image = NULL;
2992 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2994 if (stat != Ok)
2995 return stat;
2997 stat = GdipLoadImageFromStream(stream, image);
2999 IStream_Release(stream);
3001 return stat;
3004 /* FIXME: no icm handling */
3005 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
3007 TRACE("(%s) %p\n", debugstr_w(filename), image);
3009 return GdipLoadImageFromFile(filename, image);
3012 static void add_property(GpBitmap *bitmap, PropertyItem *item)
3014 UINT prop_size, prop_count;
3015 PropertyItem *prop_item;
3017 if (bitmap->prop_item == NULL)
3019 prop_size = prop_count = 0;
3020 prop_item = heap_alloc_zero(item->length + sizeof(PropertyItem));
3021 if (!prop_item) return;
3023 else
3025 UINT i;
3026 char *item_value;
3028 GdipGetPropertySize(&bitmap->image, &prop_size, &prop_count);
3030 prop_item = heap_alloc_zero(prop_size + item->length + sizeof(PropertyItem));
3031 if (!prop_item) return;
3032 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
3033 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
3034 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
3036 item_value = (char *)(prop_item + prop_count + 1);
3038 for (i = 0; i < prop_count; i++)
3040 prop_item[i].value = item_value;
3041 item_value += prop_item[i].length;
3045 prop_item[prop_count].id = item->id;
3046 prop_item[prop_count].type = item->type;
3047 prop_item[prop_count].length = item->length;
3048 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3049 memcpy(prop_item[prop_count].value, item->value, item->length);
3051 heap_free(bitmap->prop_item);
3052 bitmap->prop_item = prop_item;
3053 bitmap->prop_count++;
3056 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3058 HRESULT hr;
3059 GUID format;
3060 PROPVARIANT id, value;
3061 BOOL ret = FALSE;
3063 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3064 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return FALSE;
3066 PropVariantInit(&id);
3067 PropVariantInit(&value);
3069 id.vt = VT_LPWSTR;
3070 id.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3071 if (!id.pwszVal) return FALSE;
3072 lstrcpyW(id.pwszVal, prop_name);
3073 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3074 if (hr == S_OK && value.vt == VT_BOOL)
3075 ret = value.boolVal;
3077 PropVariantClear(&id);
3078 PropVariantClear(&value);
3080 return ret;
3083 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3085 HRESULT hr;
3086 GUID format;
3087 PROPVARIANT id, value;
3088 PropertyItem *item = NULL;
3090 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3091 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return NULL;
3093 PropVariantInit(&id);
3094 PropVariantInit(&value);
3096 id.vt = VT_LPWSTR;
3097 id.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3098 if (!id.pwszVal) return NULL;
3099 lstrcpyW(id.pwszVal, prop_name);
3100 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3101 if (hr == S_OK)
3103 UINT item_size = propvariant_size(&value);
3104 if (item_size)
3106 item_size += sizeof(*item);
3107 item = heap_alloc_zero(item_size);
3108 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3110 heap_free(item);
3111 item = NULL;
3116 PropVariantClear(&id);
3117 PropVariantClear(&value);
3119 return item;
3122 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3124 PropertyItem *comment;
3126 comment = get_property(reader, &GUID_MetadataFormatGifComment, L"TextEntry");
3127 if (comment)
3128 comment->id = PropertyTagExifUserComment;
3130 return comment;
3133 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3135 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3137 appext = get_property(reader, &GUID_MetadataFormatAPE, L"Application");
3138 if (appext)
3140 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3141 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3143 appdata = get_property(reader, &GUID_MetadataFormatAPE, L"Data");
3144 if (appdata)
3146 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3148 BYTE *data = appdata->value;
3149 if (data[0] == 3 && data[1] == 1)
3151 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3152 if (loop)
3154 loop->type = PropertyTagTypeShort;
3155 loop->id = PropertyTagLoopCount;
3156 loop->length = sizeof(SHORT);
3157 loop->value = loop + 1;
3158 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3166 heap_free(appext);
3167 heap_free(appdata);
3169 return loop;
3172 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3174 PropertyItem *background;
3176 background = get_property(reader, &GUID_MetadataFormatLSD, L"BackgroundColorIndex");
3177 if (background)
3178 background->id = PropertyTagIndexBackground;
3180 return background;
3183 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3185 HRESULT hr;
3186 IWICImagingFactory *factory;
3187 IWICPalette *palette;
3188 UINT count = 0;
3189 WICColor colors[256];
3191 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, L"GlobalColorTableFlag"))
3192 return NULL;
3194 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3195 if (hr != S_OK) return NULL;
3197 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3198 if (hr == S_OK)
3200 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3201 if (hr == S_OK)
3202 IWICPalette_GetColors(palette, 256, colors, &count);
3204 IWICPalette_Release(palette);
3207 IWICImagingFactory_Release(factory);
3209 if (count)
3211 PropertyItem *pal;
3212 UINT i;
3213 BYTE *rgb;
3215 pal = heap_alloc_zero(sizeof(*pal) + count * 3);
3216 if (!pal) return NULL;
3217 pal->type = PropertyTagTypeByte;
3218 pal->id = PropertyTagGlobalPalette;
3219 pal->value = pal + 1;
3220 pal->length = count * 3;
3222 rgb = pal->value;
3224 for (i = 0; i < count; i++)
3226 rgb[i*3] = (colors[i] >> 16) & 0xff;
3227 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3228 rgb[i*3 + 2] = colors[i] & 0xff;
3231 return pal;
3234 return NULL;
3237 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3239 PropertyItem *index = NULL;
3241 if (get_bool_property(reader, &GUID_MetadataFormatGCE, L"TransparencyFlag"))
3243 index = get_property(reader, &GUID_MetadataFormatGCE, L"TransparentColorIndex");
3244 if (index)
3245 index->id = PropertyTagIndexTransparent;
3247 return index;
3250 static LONG get_gif_frame_property(IWICBitmapFrameDecode *frame, const GUID *format, const WCHAR *property)
3252 HRESULT hr;
3253 IWICMetadataBlockReader *block_reader;
3254 IWICMetadataReader *reader;
3255 UINT block_count, i;
3256 PropertyItem *prop;
3257 LONG value = 0;
3259 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3260 if (hr == S_OK)
3262 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3263 if (hr == S_OK)
3265 for (i = 0; i < block_count; i++)
3267 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3268 if (hr == S_OK)
3270 prop = get_property(reader, format, property);
3271 if (prop)
3273 if (prop->type == PropertyTagTypeByte && prop->length == 1)
3274 value = *(BYTE *)prop->value;
3275 else if (prop->type == PropertyTagTypeShort && prop->length == 2)
3276 value = *(SHORT *)prop->value;
3278 heap_free(prop);
3280 IWICMetadataReader_Release(reader);
3284 IWICMetadataBlockReader_Release(block_reader);
3287 return value;
3290 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3292 HRESULT hr;
3293 IWICBitmapFrameDecode *frame;
3294 IWICMetadataBlockReader *block_reader;
3295 IWICMetadataReader *reader;
3296 UINT frame_count, block_count, i;
3297 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3298 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3300 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3301 if (frame_count > 1)
3303 delay = heap_alloc_zero(sizeof(*delay) + frame_count * sizeof(LONG));
3304 if (delay)
3306 LONG *value;
3308 delay->type = PropertyTagTypeLong;
3309 delay->id = PropertyTagFrameDelay;
3310 delay->length = frame_count * sizeof(LONG);
3311 delay->value = delay + 1;
3313 value = delay->value;
3315 for (i = 0; i < frame_count; i++)
3317 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3318 if (hr == S_OK)
3320 value[i] = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, L"Delay");
3321 IWICBitmapFrameDecode_Release(frame);
3323 else value[i] = 0;
3328 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3329 if (hr == S_OK)
3331 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3332 if (hr == S_OK)
3334 for (i = 0; i < block_count; i++)
3336 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3337 if (hr == S_OK)
3339 if (!comment)
3340 comment = get_gif_comment(reader);
3342 if (frame_count > 1 && !loop)
3343 loop = get_gif_loopcount(reader);
3345 if (!background)
3346 background = get_gif_background(reader);
3348 if (!palette)
3349 palette = get_gif_palette(decoder, reader);
3351 IWICMetadataReader_Release(reader);
3355 IWICMetadataBlockReader_Release(block_reader);
3358 if (frame_count > 1 && !loop)
3360 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3361 if (loop)
3363 loop->type = PropertyTagTypeShort;
3364 loop->id = PropertyTagLoopCount;
3365 loop->length = sizeof(SHORT);
3366 loop->value = loop + 1;
3367 *(SHORT *)loop->value = 1;
3371 if (delay) add_property(bitmap, delay);
3372 if (comment) add_property(bitmap, comment);
3373 if (loop) add_property(bitmap, loop);
3374 if (palette) add_property(bitmap, palette);
3375 if (background) add_property(bitmap, background);
3377 heap_free(delay);
3378 heap_free(comment);
3379 heap_free(loop);
3380 heap_free(palette);
3381 heap_free(background);
3383 /* Win7 gdiplus always returns transparent color index from frame 0 */
3384 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3385 if (hr != S_OK) return;
3387 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3388 if (hr == S_OK)
3390 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3391 if (hr == S_OK)
3393 for (i = 0; i < block_count; i++)
3395 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3396 if (hr == S_OK)
3398 if (!transparent_idx)
3399 transparent_idx = get_gif_transparent_idx(reader);
3401 IWICMetadataReader_Release(reader);
3405 IWICMetadataBlockReader_Release(block_reader);
3408 if (transparent_idx) add_property(bitmap, transparent_idx);
3409 heap_free(transparent_idx);
3411 IWICBitmapFrameDecode_Release(frame);
3414 static PropertyItem* create_prop(PROPID propid, PROPVARIANT* value)
3416 PropertyItem *item = NULL;
3417 UINT item_size = propvariant_size(value);
3419 if (item_size)
3421 item_size += sizeof(*item);
3422 item = heap_alloc_zero(item_size);
3423 if (propvariant_to_item(value, item, item_size, propid) != Ok)
3425 heap_free(item);
3426 item = NULL;
3430 return item;
3433 static ULONG get_ulong_by_index(IWICMetadataReader* reader, ULONG index)
3435 PROPVARIANT value;
3436 HRESULT hr;
3437 ULONG result=0;
3439 hr = IWICMetadataReader_GetValueByIndex(reader, index, NULL, NULL, &value);
3440 if (SUCCEEDED(hr))
3442 switch (value.vt)
3444 case VT_UI4:
3445 result = value.ulVal;
3446 break;
3447 default:
3448 ERR("unhandled case %u\n", value.vt);
3449 break;
3451 PropVariantClear(&value);
3453 return result;
3456 static void png_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3458 HRESULT hr;
3459 IWICBitmapFrameDecode *frame;
3460 IWICMetadataBlockReader *block_reader;
3461 IWICMetadataReader *reader;
3462 UINT block_count, i, j;
3463 struct keyword_info {
3464 const char* name;
3465 PROPID propid;
3466 BOOL seen;
3467 } keywords[] = {
3468 { "Title", PropertyTagImageTitle },
3469 { "Author", PropertyTagArtist },
3470 { "Description", PropertyTagImageDescription },
3471 { "Copyright", PropertyTagCopyright },
3472 { "Software", PropertyTagSoftwareUsed },
3473 { "Source", PropertyTagEquipModel },
3474 { "Comment", PropertyTagExifUserComment },
3476 BOOL seen_gamma=FALSE, seen_whitepoint=FALSE, seen_chrm=FALSE;
3478 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3479 if (hr != S_OK) return;
3481 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3482 if (hr == S_OK)
3484 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3485 if (hr == S_OK)
3487 for (i = 0; i < block_count; i++)
3489 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3490 if (hr == S_OK)
3492 GUID format;
3494 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3495 if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunktEXt, &format))
3497 PROPVARIANT name, value;
3498 PropertyItem* item;
3500 hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, &name, &value);
3502 if (SUCCEEDED(hr))
3504 if (name.vt == VT_LPSTR)
3506 for (j = 0; j < ARRAY_SIZE(keywords); j++)
3507 if (!strcmp(keywords[j].name, name.pszVal))
3508 break;
3509 if (j < ARRAY_SIZE(keywords) && !keywords[j].seen)
3511 keywords[j].seen = TRUE;
3512 item = create_prop(keywords[j].propid, &value);
3513 if (item)
3514 add_property(bitmap, item);
3515 heap_free(item);
3519 PropVariantClear(&name);
3520 PropVariantClear(&value);
3523 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA, &format))
3525 PropertyItem* item;
3527 if (!seen_gamma)
3529 item = heap_alloc_zero(sizeof(PropertyItem) + sizeof(ULONG) * 2);
3530 if (item)
3532 ULONG *rational;
3533 item->length = sizeof(ULONG) * 2;
3534 item->type = PropertyTagTypeRational;
3535 item->id = PropertyTagGamma;
3536 rational = item->value = item + 1;
3537 rational[0] = 100000;
3538 rational[1] = get_ulong_by_index(reader, 0);
3539 add_property(bitmap, item);
3540 seen_gamma = TRUE;
3541 heap_free(item);
3545 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkcHRM, &format))
3547 PropertyItem* item;
3549 if (!seen_whitepoint)
3551 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 4);
3552 if (item)
3554 ULONG *rational;
3555 item->length = sizeof(ULONG) * 4;
3556 item->type = PropertyTagTypeRational;
3557 item->id = PropertyTagWhitePoint;
3558 rational = item->value = item + 1;
3559 rational[0] = get_ulong_by_index(reader, 0);
3560 rational[1] = 100000;
3561 rational[2] = get_ulong_by_index(reader, 1);
3562 rational[3] = 100000;
3563 add_property(bitmap, item);
3564 seen_whitepoint = TRUE;
3565 GdipFree(item);
3568 if (!seen_chrm)
3570 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 12);
3571 if (item)
3573 ULONG *rational;
3574 item->length = sizeof(ULONG) * 12;
3575 item->type = PropertyTagTypeRational;
3576 item->id = PropertyTagPrimaryChromaticities;
3577 rational = item->value = item + 1;
3578 rational[0] = get_ulong_by_index(reader, 2);
3579 rational[1] = 100000;
3580 rational[2] = get_ulong_by_index(reader, 3);
3581 rational[3] = 100000;
3582 rational[4] = get_ulong_by_index(reader, 4);
3583 rational[5] = 100000;
3584 rational[6] = get_ulong_by_index(reader, 5);
3585 rational[7] = 100000;
3586 rational[8] = get_ulong_by_index(reader, 6);
3587 rational[9] = 100000;
3588 rational[10] = get_ulong_by_index(reader, 7);
3589 rational[11] = 100000;
3590 add_property(bitmap, item);
3591 seen_chrm = TRUE;
3592 GdipFree(item);
3597 IWICMetadataReader_Release(reader);
3601 IWICMetadataBlockReader_Release(block_reader);
3604 IWICBitmapFrameDecode_Release(frame);
3607 static GpStatus initialize_decoder_wic(IStream *stream, REFGUID container, IWICBitmapDecoder **decoder)
3609 IWICImagingFactory *factory;
3610 HRESULT hr;
3612 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
3614 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3615 if (FAILED(hr)) return hresult_to_status(hr);
3616 hr = IWICImagingFactory_CreateDecoder(factory, container, NULL, decoder);
3617 IWICImagingFactory_Release(factory);
3618 if (FAILED(hr)) return hresult_to_status(hr);
3620 hr = IWICBitmapDecoder_Initialize(*decoder, stream, WICDecodeMetadataCacheOnLoad);
3621 if (FAILED(hr))
3623 IWICBitmapDecoder_Release(*decoder);
3624 return hresult_to_status(hr);
3626 return Ok;
3629 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3631 static GpStatus decode_frame_wic(IWICBitmapDecoder *decoder, BOOL force_conversion,
3632 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3634 GpStatus status=Ok;
3635 GpBitmap *bitmap;
3636 HRESULT hr;
3637 IWICBitmapFrameDecode *frame;
3638 IWICBitmapSource *source=NULL;
3639 IWICMetadataBlockReader *block_reader;
3640 WICPixelFormatGUID wic_format;
3641 PixelFormat gdip_format=0;
3642 ColorPalette *palette = NULL;
3643 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3644 int i;
3645 UINT width, height, frame_count;
3646 BitmapData lockeddata;
3647 WICRect wrc;
3649 TRACE("%p,%u,%p\n", decoder, active_frame, image);
3651 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3652 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3653 if (SUCCEEDED(hr)) /* got frame */
3655 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3657 if (SUCCEEDED(hr))
3659 if (!force_conversion)
3661 for (i=0; pixel_formats[i].wic_format; i++)
3663 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3665 source = (IWICBitmapSource*)frame;
3666 IWICBitmapSource_AddRef(source);
3667 gdip_format = pixel_formats[i].gdip_format;
3668 palette_type = pixel_formats[i].palette_type;
3669 break;
3673 if (!source)
3675 /* unknown format; fall back on 32bppARGB */
3676 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3677 gdip_format = PixelFormat32bppARGB;
3679 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3682 if (SUCCEEDED(hr)) /* got source */
3684 hr = IWICBitmapSource_GetSize(source, &width, &height);
3686 if (SUCCEEDED(hr))
3687 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3688 NULL, &bitmap);
3690 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3692 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3693 gdip_format, &lockeddata);
3694 if (status == Ok) /* locked bitmap */
3696 wrc.X = 0;
3697 wrc.Width = width;
3698 wrc.Height = 1;
3699 for (i=0; i<height; i++)
3701 wrc.Y = i;
3702 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3703 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3704 if (FAILED(hr)) break;
3707 GdipBitmapUnlockBits(bitmap, &lockeddata);
3710 if (SUCCEEDED(hr) && status == Ok)
3711 *image = &bitmap->image;
3712 else
3714 *image = NULL;
3715 GdipDisposeImage(&bitmap->image);
3718 if (SUCCEEDED(hr) && status == Ok)
3720 double dpix, dpiy;
3721 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3722 if (SUCCEEDED(hr))
3724 bitmap->image.xres = dpix;
3725 bitmap->image.yres = dpiy;
3727 hr = S_OK;
3731 IWICBitmapSource_Release(source);
3734 if (SUCCEEDED(hr) && status == Ok) {
3735 bitmap->metadata_reader = NULL;
3737 if (metadata_reader)
3738 metadata_reader(bitmap, decoder, active_frame);
3739 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3741 UINT block_count = 0;
3742 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3743 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3744 IWICMetadataBlockReader_Release(block_reader);
3747 palette = get_palette(frame, palette_type);
3748 IWICBitmapFrameDecode_Release(frame);
3752 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3754 if (status == Ok)
3756 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3757 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI;
3758 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormat2bppGray) ||
3759 IsEqualGUID(&wic_format, &GUID_WICPixelFormat4bppGray) ||
3760 IsEqualGUID(&wic_format, &GUID_WICPixelFormat8bppGray) ||
3761 IsEqualGUID(&wic_format, &GUID_WICPixelFormat16bppGray))
3762 bitmap->image.flags |= ImageFlagsColorSpaceGRAY;
3763 else
3764 bitmap->image.flags |= ImageFlagsColorSpaceRGB;
3765 bitmap->image.frame_count = frame_count;
3766 bitmap->image.current_frame = active_frame;
3767 bitmap->image.decoder = decoder;
3768 IWICBitmapDecoder_AddRef(decoder);
3769 if (palette)
3771 heap_free(bitmap->image.palette);
3772 bitmap->image.palette = palette;
3774 else
3776 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3777 bitmap->image.palette->Flags = 0;
3779 TRACE("=> %p\n", *image);
3782 return status;
3785 static GpStatus decode_image_wic(IStream *stream, REFGUID container,
3786 metadata_reader_func metadata_reader, GpImage **image)
3788 IWICBitmapDecoder *decoder;
3789 GpStatus status;
3791 status = initialize_decoder_wic(stream, container, &decoder);
3792 if(status != Ok)
3793 return status;
3795 status = decode_frame_wic(decoder, FALSE, 0, metadata_reader, image);
3796 IWICBitmapDecoder_Release(decoder);
3797 return status;
3800 static GpStatus select_frame_wic(GpImage *image, UINT active_frame)
3802 GpImage *new_image;
3803 GpStatus status;
3805 status = decode_frame_wic(image->decoder, FALSE, active_frame, NULL, &new_image);
3806 if(status != Ok)
3807 return status;
3809 new_image->busy = image->busy;
3810 memcpy(&new_image->format, &image->format, sizeof(GUID));
3811 new_image->encoder = image->encoder;
3812 image->encoder = NULL;
3813 free_image_data(image);
3814 if (image->type == ImageTypeBitmap)
3815 *(GpBitmap *)image = *(GpBitmap *)new_image;
3816 else if (image->type == ImageTypeMetafile)
3817 *(GpMetafile *)image = *(GpMetafile *)new_image;
3818 new_image->type = ~0;
3819 heap_free(new_image);
3820 return Ok;
3823 static HRESULT get_gif_frame_rect(IWICBitmapFrameDecode *frame,
3824 UINT *left, UINT *top, UINT *width, UINT *height)
3826 *left = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, L"Left");
3827 *top = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, L"Top");
3829 return IWICBitmapFrameDecode_GetSize(frame, width, height);
3832 static HRESULT blit_gif_frame(GpBitmap *bitmap, IWICBitmapFrameDecode *frame, BOOL first_frame)
3834 UINT i, j, left, top, width, height;
3835 IWICBitmapSource *source;
3836 BYTE *new_bits;
3837 HRESULT hr;
3839 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3840 if(FAILED(hr))
3841 return hr;
3843 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3844 if(FAILED(hr))
3845 return hr;
3847 new_bits = heap_alloc_zero(width*height*4);
3848 if(!new_bits)
3849 return E_OUTOFMEMORY;
3851 hr = IWICBitmapSource_CopyPixels(source, NULL, width*4, width*height*4, new_bits);
3852 IWICBitmapSource_Release(source);
3853 if(FAILED(hr)) {
3854 heap_free(new_bits);
3855 return hr;
3858 for(i=0; i<height && i+top<bitmap->height; i++) {
3859 for(j=0; j<width && j+left<bitmap->width; j++) {
3860 DWORD *src = (DWORD*)(new_bits+i*width*4+j*4);
3861 DWORD *dst = (DWORD*)(bitmap->bits+(i+top)*bitmap->stride+(j+left)*4);
3863 if(first_frame || *src>>24 != 0)
3864 *dst = *src;
3867 heap_free(new_bits);
3868 return hr;
3871 static DWORD get_gif_background_color(GpBitmap *bitmap)
3873 BYTE bgcolor_idx = 0;
3874 UINT i;
3876 for(i=0; i<bitmap->prop_count; i++) {
3877 if(bitmap->prop_item[i].id == PropertyTagIndexBackground) {
3878 bgcolor_idx = *(BYTE*)bitmap->prop_item[i].value;
3879 break;
3883 for(i=0; i<bitmap->prop_count; i++) {
3884 if(bitmap->prop_item[i].id == PropertyTagIndexTransparent) {
3885 BYTE transparent_idx;
3886 transparent_idx = *(BYTE*)bitmap->prop_item[i].value;
3888 if(transparent_idx == bgcolor_idx)
3889 return 0;
3893 for(i=0; i<bitmap->prop_count; i++) {
3894 if(bitmap->prop_item[i].id == PropertyTagGlobalPalette) {
3895 if(bitmap->prop_item[i].length/3 > bgcolor_idx) {
3896 BYTE *color = ((BYTE*)bitmap->prop_item[i].value)+bgcolor_idx*3;
3897 return color[2] + (color[1]<<8) + (color[0]<<16) + (0xffu<<24);
3899 break;
3903 FIXME("can't get gif background color\n");
3904 return 0xffffffff;
3907 static GpStatus select_frame_gif(GpImage* image, UINT active_frame)
3909 GpBitmap *bitmap = (GpBitmap*)image;
3910 IWICBitmapFrameDecode *frame;
3911 int cur_frame=0, disposal;
3912 BOOL bgcolor_set = FALSE;
3913 DWORD bgcolor = 0;
3914 HRESULT hr;
3916 if(active_frame > image->current_frame) {
3917 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, image->current_frame, &frame);
3918 if(FAILED(hr))
3919 return hresult_to_status(hr);
3920 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, L"Disposal");
3921 IWICBitmapFrameDecode_Release(frame);
3923 if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND)
3924 cur_frame = image->current_frame;
3925 else if(disposal != GIF_DISPOSE_RESTORE_TO_PREV)
3926 cur_frame = image->current_frame+1;
3929 while(cur_frame != active_frame) {
3930 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, cur_frame, &frame);
3931 if(FAILED(hr))
3932 return hresult_to_status(hr);
3933 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, L"Disposal");
3935 if(disposal==GIF_DISPOSE_UNSPECIFIED || disposal==GIF_DISPOSE_DO_NOT_DISPOSE) {
3936 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3937 if(FAILED(hr))
3938 return hresult_to_status(hr);
3939 }else if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND) {
3940 UINT left, top, width, height, i, j;
3942 if(!bgcolor_set) {
3943 bgcolor = get_gif_background_color(bitmap);
3944 bgcolor_set = TRUE;
3947 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3948 if(FAILED(hr))
3949 return hresult_to_status(hr);
3950 for(i=top; i<top+height && i<bitmap->height; i++) {
3951 DWORD *bits = (DWORD*)(bitmap->bits+i*bitmap->stride);
3952 for(j=left; j<left+width && j<bitmap->width; j++)
3953 bits[j] = bgcolor;
3957 IWICBitmapFrameDecode_Release(frame);
3958 cur_frame++;
3961 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, active_frame, &frame);
3962 if(FAILED(hr))
3963 return hresult_to_status(hr);
3965 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3966 IWICBitmapFrameDecode_Release(frame);
3967 if(FAILED(hr))
3968 return hresult_to_status(hr);
3970 image->current_frame = active_frame;
3971 return Ok;
3974 static GpStatus decode_image_icon(IStream* stream, GpImage **image)
3976 return decode_image_wic(stream, &GUID_ContainerFormatIco, NULL, image);
3979 static GpStatus decode_image_bmp(IStream* stream, GpImage **image)
3981 GpStatus status;
3982 GpBitmap* bitmap;
3984 status = decode_image_wic(stream, &GUID_ContainerFormatBmp, NULL, image);
3986 bitmap = (GpBitmap*)*image;
3988 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3990 /* WIC supports bmp files with alpha, but gdiplus does not */
3991 bitmap->format = PixelFormat32bppRGB;
3994 return status;
3997 static GpStatus decode_image_jpeg(IStream* stream, GpImage **image)
3999 return decode_image_wic(stream, &GUID_ContainerFormatJpeg, NULL, image);
4002 static BOOL has_png_transparency_chunk(IStream *pIStream)
4004 LARGE_INTEGER seek;
4005 BOOL has_tRNS = FALSE;
4006 HRESULT hr;
4007 BYTE header[8];
4009 seek.QuadPart = 8;
4012 ULARGE_INTEGER chunk_start;
4013 ULONG bytesread, chunk_size;
4015 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, &chunk_start);
4016 if (FAILED(hr)) break;
4018 hr = IStream_Read(pIStream, header, 8, &bytesread);
4019 if (FAILED(hr) || bytesread < 8) break;
4021 chunk_size = (header[0] << 24) | (header[1] << 16) | (header[2] << 8) | header[3];
4022 if (!memcmp(&header[4], "tRNS", 4))
4024 has_tRNS = TRUE;
4025 break;
4028 seek.QuadPart = chunk_start.QuadPart + chunk_size + 12; /* skip data and CRC */
4029 } while (memcmp(&header[4], "IDAT", 4) && memcmp(&header[4], "IEND", 4));
4031 TRACE("has_tRNS = %d\n", has_tRNS);
4032 return has_tRNS;
4035 static GpStatus decode_image_png(IStream* stream, GpImage **image)
4037 IWICBitmapDecoder *decoder;
4038 IWICBitmapFrameDecode *frame;
4039 GpStatus status;
4040 HRESULT hr;
4041 GUID format;
4042 BOOL force_conversion = FALSE;
4044 status = initialize_decoder_wic(stream, &GUID_ContainerFormatPng, &decoder);
4045 if (status != Ok)
4046 return status;
4048 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
4049 if (hr == S_OK)
4051 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
4052 if (hr == S_OK)
4054 if (IsEqualGUID(&format, &GUID_WICPixelFormat8bppGray))
4055 force_conversion = TRUE;
4056 else if ((IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed) ||
4057 IsEqualGUID(&format, &GUID_WICPixelFormat4bppIndexed) ||
4058 IsEqualGUID(&format, &GUID_WICPixelFormat2bppIndexed) ||
4059 IsEqualGUID(&format, &GUID_WICPixelFormat1bppIndexed) ||
4060 IsEqualGUID(&format, &GUID_WICPixelFormat24bppBGR)) &&
4061 has_png_transparency_chunk(stream))
4062 force_conversion = TRUE;
4064 status = decode_frame_wic(decoder, force_conversion, 0, png_metadata_reader, image);
4066 else
4067 status = hresult_to_status(hr);
4069 IWICBitmapFrameDecode_Release(frame);
4071 else
4072 status = hresult_to_status(hr);
4074 IWICBitmapDecoder_Release(decoder);
4075 return status;
4078 static GpStatus decode_image_gif(IStream* stream, GpImage **image)
4080 IWICBitmapDecoder *decoder;
4081 UINT frame_count;
4082 GpStatus status;
4083 HRESULT hr;
4085 status = initialize_decoder_wic(stream, &GUID_ContainerFormatGif, &decoder);
4086 if(status != Ok)
4087 return status;
4089 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
4090 if(FAILED(hr))
4091 return hresult_to_status(hr);
4093 status = decode_frame_wic(decoder, frame_count > 1, 0, gif_metadata_reader, image);
4094 IWICBitmapDecoder_Release(decoder);
4095 if(status != Ok)
4096 return status;
4098 if(frame_count > 1) {
4099 heap_free((*image)->palette);
4100 (*image)->palette = NULL;
4102 return Ok;
4105 static GpStatus decode_image_tiff(IStream* stream, GpImage **image)
4107 return decode_image_wic(stream, &GUID_ContainerFormatTiff, NULL, image);
4110 C_ASSERT(offsetof(WmfPlaceableFileHeader, Key) == 0);
4112 static GpStatus load_wmf(IStream *stream, GpMetafile **metafile)
4114 WmfPlaceableFileHeader pfh;
4115 BOOL is_placeable = FALSE;
4116 LARGE_INTEGER seek;
4117 GpStatus status;
4118 METAHEADER mh;
4119 HMETAFILE hmf;
4120 HRESULT hr;
4121 ULONG size;
4122 void *buf;
4124 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4125 if (hr != S_OK || size != sizeof(mh))
4126 return GenericError;
4128 /* detect whether stream starts with a WmfPlaceablefileheader */
4129 if (*(UINT32 *)&mh == WMF_PLACEABLE_KEY)
4131 seek.QuadPart = 0;
4132 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4133 if (FAILED(hr)) return hresult_to_status(hr);
4135 hr = IStream_Read(stream, &pfh, sizeof(pfh), &size);
4136 if (hr != S_OK || size != sizeof(pfh))
4137 return GenericError;
4139 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4140 if (hr != S_OK || size != sizeof(mh))
4141 return GenericError;
4143 is_placeable = TRUE;
4146 seek.QuadPart = is_placeable ? sizeof(pfh) : 0;
4147 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4148 if (FAILED(hr)) return hresult_to_status(hr);
4150 buf = heap_alloc(mh.mtSize * 2);
4151 if (!buf) return OutOfMemory;
4153 hr = IStream_Read(stream, buf, mh.mtSize * 2, &size);
4154 if (hr != S_OK || size != mh.mtSize * 2)
4156 heap_free(buf);
4157 return GenericError;
4160 hmf = SetMetaFileBitsEx(mh.mtSize * 2, buf);
4161 heap_free(buf);
4162 if (!hmf)
4163 return GenericError;
4165 status = GdipCreateMetafileFromWmf(hmf, TRUE, is_placeable ? &pfh : NULL, metafile);
4166 if (status != Ok)
4167 DeleteMetaFile(hmf);
4168 return status;
4171 static GpStatus decode_image_wmf(IStream *stream, GpImage **image)
4173 GpMetafile *metafile;
4174 GpStatus status;
4176 TRACE("%p %p\n", stream, image);
4178 if (!stream || !image)
4179 return InvalidParameter;
4181 status = load_wmf(stream, &metafile);
4182 if (status != Ok)
4184 TRACE("Could not load metafile\n");
4185 return status;
4188 *image = (GpImage *)metafile;
4189 TRACE("<-- %p\n", *image);
4191 return Ok;
4194 static GpStatus load_emf(IStream *stream, GpMetafile **metafile)
4196 LARGE_INTEGER seek;
4197 ENHMETAHEADER emh;
4198 HENHMETAFILE hemf;
4199 GpStatus status;
4200 HRESULT hr;
4201 ULONG size;
4202 void *buf;
4204 hr = IStream_Read(stream, &emh, sizeof(emh), &size);
4205 if (hr != S_OK || size != sizeof(emh) || emh.dSignature != ENHMETA_SIGNATURE)
4206 return GenericError;
4208 seek.QuadPart = 0;
4209 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4210 if (FAILED(hr)) return hresult_to_status(hr);
4212 buf = heap_alloc(emh.nBytes);
4213 if (!buf) return OutOfMemory;
4215 hr = IStream_Read(stream, buf, emh.nBytes, &size);
4216 if (hr != S_OK || size != emh.nBytes)
4218 heap_free(buf);
4219 return GenericError;
4222 hemf = SetEnhMetaFileBits(emh.nBytes, buf);
4223 heap_free(buf);
4224 if (!hemf)
4225 return GenericError;
4227 status = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
4228 if (status != Ok)
4229 DeleteEnhMetaFile(hemf);
4230 return status;
4233 static GpStatus decode_image_emf(IStream *stream, GpImage **image)
4235 GpMetafile *metafile;
4236 GpStatus status;
4238 TRACE("%p %p\n", stream, image);
4240 if (!stream || !image)
4241 return InvalidParameter;
4243 status = load_emf(stream, &metafile);
4244 if (status != Ok)
4246 TRACE("Could not load metafile\n");
4247 return status;
4250 *image = (GpImage *)metafile;
4251 TRACE("<-- %p\n", *image);
4253 return Ok;
4256 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
4257 GDIPCONST EncoderParameters* params);
4259 typedef GpStatus (*decode_image_func)(IStream *stream, GpImage **image);
4261 typedef GpStatus (*select_image_func)(GpImage *image, UINT active_frame);
4263 typedef struct image_codec {
4264 ImageCodecInfo info;
4265 encode_image_func encode_func;
4266 decode_image_func decode_func;
4267 select_image_func select_func;
4268 } image_codec;
4270 typedef enum {
4271 BMP,
4272 JPEG,
4273 GIF,
4274 TIFF,
4275 EMF,
4276 WMF,
4277 PNG,
4278 ICO,
4279 NUM_CODECS
4280 } ImageFormat;
4282 static const struct image_codec codecs[NUM_CODECS];
4284 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
4286 BYTE signature[8];
4287 const BYTE *pattern, *mask;
4288 LARGE_INTEGER seek;
4289 HRESULT hr;
4290 ULONG bytesread;
4291 int i;
4292 DWORD j, sig;
4294 /* seek to the start of the stream */
4295 seek.QuadPart = 0;
4296 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4297 if (FAILED(hr)) return hresult_to_status(hr);
4299 /* read the first 8 bytes */
4300 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4301 hr = IStream_Read(stream, signature, 8, &bytesread);
4302 if (FAILED(hr)) return hresult_to_status(hr);
4303 if (hr == S_FALSE || bytesread == 0) return GenericError;
4305 for (i = 0; i < NUM_CODECS; i++) {
4306 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4307 bytesread >= codecs[i].info.SigSize)
4309 for (sig=0; sig<codecs[i].info.SigCount; sig++)
4311 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
4312 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
4313 for (j=0; j<codecs[i].info.SigSize; j++)
4314 if ((signature[j] & mask[j]) != pattern[j])
4315 break;
4316 if (j == codecs[i].info.SigSize)
4318 *result = &codecs[i];
4319 return Ok;
4325 TRACE("no match for %lu byte signature %x %x %x %x %x %x %x %x\n", bytesread,
4326 signature[0],signature[1],signature[2],signature[3],
4327 signature[4],signature[5],signature[6],signature[7]);
4329 return GenericError;
4332 static GpStatus get_decoder_info_from_image(GpImage *image, const struct image_codec **result)
4334 int i;
4336 for (i = 0; i < NUM_CODECS; i++) {
4337 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4338 IsEqualIID(&codecs[i].info.FormatID, &image->format))
4340 *result = &codecs[i];
4341 return Ok;
4345 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image->format));
4346 return GenericError;
4349 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
4350 UINT frame)
4352 GpStatus stat;
4353 const struct image_codec *codec = NULL;
4354 BOOL unlock;
4356 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
4358 if (!image || !dimensionID)
4359 return InvalidParameter;
4360 if(!image_lock(image, &unlock))
4361 return ObjectBusy;
4363 if (frame >= image->frame_count)
4365 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
4366 image_unlock(image, unlock);
4367 return InvalidParameter;
4370 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
4372 WARN("invalid image type %d\n", image->type);
4373 image_unlock(image, unlock);
4374 return InvalidParameter;
4377 if (image->current_frame == frame)
4379 image_unlock(image, unlock);
4380 return Ok;
4383 if (!image->decoder)
4385 TRACE("image doesn't have an associated decoder\n");
4386 image_unlock(image, unlock);
4387 return Ok;
4390 /* choose an appropriate image decoder */
4391 stat = get_decoder_info_from_image(image, &codec);
4392 if (stat != Ok)
4394 WARN("can't find decoder info\n");
4395 image_unlock(image, unlock);
4396 return stat;
4399 stat = codec->select_func(image, frame);
4400 image_unlock(image, unlock);
4401 return stat;
4404 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
4406 GpStatus stat;
4407 LARGE_INTEGER seek;
4408 HRESULT hr;
4409 const struct image_codec *codec=NULL;
4411 TRACE("%p %p\n", stream, image);
4413 if (!stream || !image)
4414 return InvalidParameter;
4416 /* choose an appropriate image decoder */
4417 stat = get_decoder_info(stream, &codec);
4418 if (stat != Ok) return stat;
4420 /* seek to the start of the stream */
4421 seek.QuadPart = 0;
4422 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4423 if (FAILED(hr)) return hresult_to_status(hr);
4425 /* call on the image decoder to do the real work */
4426 stat = codec->decode_func(stream, image);
4428 /* take note of the original data format */
4429 if (stat == Ok)
4431 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
4432 return Ok;
4435 return stat;
4438 /* FIXME: no ICM */
4439 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
4441 TRACE("%p %p\n", stream, image);
4443 return GdipLoadImageFromStream(stream, image);
4446 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
4448 static int calls;
4450 TRACE("(%p,%lu)\n", image, propId);
4452 if(!image)
4453 return InvalidParameter;
4455 if(!(calls++))
4456 FIXME("not implemented\n");
4458 return NotImplemented;
4461 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
4463 static int calls;
4465 if (!image || !item) return InvalidParameter;
4467 TRACE("(%p,%p:%#lx,%u,%lu,%p)\n", image, item, item->id, item->type, item->length, item->value);
4469 if(!(calls++))
4470 FIXME("not implemented\n");
4472 return Ok;
4475 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
4476 GDIPCONST CLSID *clsidEncoder,
4477 GDIPCONST EncoderParameters *encoderParams)
4479 GpStatus stat;
4480 IStream *stream;
4482 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
4484 if (!image || !filename|| !clsidEncoder)
4485 return InvalidParameter;
4487 /* this might release an old file stream held by the encoder so we can re-create it below */
4488 terminate_encoder_wic(image);
4490 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4491 if (stat != Ok)
4492 return GenericError;
4494 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4496 IStream_Release(stream);
4497 return stat;
4500 /*************************************************************************
4501 * Encoding functions -
4502 * These functions encode an image in different image file formats.
4505 static GpStatus initialize_encoder_wic(IStream *stream, REFGUID container, GpImage *image)
4507 IWICImagingFactory *factory;
4508 HRESULT hr;
4510 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
4512 terminate_encoder_wic(image); /* terminate previous encoder if it exists */
4514 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
4515 if (FAILED(hr)) return hresult_to_status(hr);
4516 hr = IWICImagingFactory_CreateEncoder(factory, container, NULL, &image->encoder);
4517 IWICImagingFactory_Release(factory);
4518 if (FAILED(hr))
4520 image->encoder = NULL;
4521 return hresult_to_status(hr);
4524 hr = IWICBitmapEncoder_Initialize(image->encoder, stream, WICBitmapEncoderNoCache);
4525 if (FAILED(hr))
4527 IWICBitmapEncoder_Release(image->encoder);
4528 image->encoder = NULL;
4529 return hresult_to_status(hr);
4531 return Ok;
4534 GpStatus terminate_encoder_wic(GpImage *image)
4536 if (!image->encoder)
4537 return Ok;
4538 else
4540 HRESULT hr = IWICBitmapEncoder_Commit(image->encoder);
4541 IWICBitmapEncoder_Release(image->encoder);
4542 image->encoder = NULL;
4543 return hresult_to_status(hr);
4547 static GpStatus encode_frame_wic(IWICBitmapEncoder *encoder, GpImage *image)
4549 GpStatus stat;
4550 GpBitmap *bitmap;
4551 IWICBitmapFrameEncode *frameencode;
4552 IPropertyBag2 *encoderoptions;
4553 GUID container_format;
4554 HRESULT hr;
4555 UINT width, height;
4556 PixelFormat gdipformat=0;
4557 const WICPixelFormatGUID *desired_wicformat;
4558 WICPixelFormatGUID wicformat;
4559 GpRect rc;
4560 BitmapData lockeddata;
4561 UINT i;
4563 if (image->type != ImageTypeBitmap)
4564 return GenericError;
4566 bitmap = (GpBitmap*)image;
4568 GdipGetImageWidth(image, &width);
4569 GdipGetImageHeight(image, &height);
4571 rc.X = 0;
4572 rc.Y = 0;
4573 rc.Width = width;
4574 rc.Height = height;
4576 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4578 if (SUCCEEDED(hr)) /* created frame */
4580 hr = IWICBitmapEncoder_GetContainerFormat(encoder, &container_format);
4581 if (SUCCEEDED(hr) && IsEqualGUID(&container_format, &GUID_ContainerFormatPng))
4583 /* disable PNG filters for faster encoding */
4584 PROPBAG2 filter_option = { .pstrName = (LPOLESTR) L"FilterOption" };
4585 VARIANT filter_value;
4586 VariantInit(&filter_value);
4587 V_VT(&filter_value) = VT_UI1;
4588 V_UI1(&filter_value) = WICPngFilterNone;
4589 hr = IPropertyBag2_Write(encoderoptions, 1, &filter_option, &filter_value);
4592 if (SUCCEEDED(hr))
4593 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4595 if (SUCCEEDED(hr))
4596 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4598 if (SUCCEEDED(hr))
4599 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4601 if (SUCCEEDED(hr))
4603 for (i=0; pixel_formats[i].wic_format; i++)
4605 if (pixel_formats[i].gdip_format == bitmap->format)
4607 desired_wicformat = pixel_formats[i].wic_format;
4608 gdipformat = bitmap->format;
4609 break;
4612 if (!gdipformat)
4614 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
4615 gdipformat = PixelFormat32bppARGB;
4618 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
4619 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4622 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
4624 /* Encoder doesn't support this bitmap's format. */
4625 gdipformat = 0;
4626 for (i=0; pixel_formats[i].wic_format; i++)
4628 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
4630 gdipformat = pixel_formats[i].gdip_format;
4631 break;
4634 if (!gdipformat)
4636 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
4637 hr = E_FAIL;
4641 if (SUCCEEDED(hr) && IsIndexedPixelFormat(gdipformat) && image->palette)
4642 hr = set_palette(frameencode, image->palette);
4644 if (SUCCEEDED(hr))
4646 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4647 &lockeddata);
4649 if (stat == Ok)
4651 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4652 BYTE *row;
4654 /* write one row at a time in case stride is negative */
4655 row = lockeddata.Scan0;
4656 for (i=0; i<lockeddata.Height; i++)
4658 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4659 if (FAILED(hr)) break;
4660 row += lockeddata.Stride;
4663 GdipBitmapUnlockBits(bitmap, &lockeddata);
4665 else
4666 hr = E_FAIL;
4669 if (SUCCEEDED(hr))
4670 hr = IWICBitmapFrameEncode_Commit(frameencode);
4672 IWICBitmapFrameEncode_Release(frameencode);
4673 IPropertyBag2_Release(encoderoptions);
4676 return hresult_to_status(hr);
4679 static BOOL has_encoder_param_long(GDIPCONST EncoderParameters *params, GUID param_guid, ULONG val)
4681 int param_idx, value_idx;
4683 if (!params)
4684 return FALSE;
4686 for (param_idx = 0; param_idx < params->Count; param_idx++)
4688 EncoderParameter param = params->Parameter[param_idx];
4689 if (param.Type == EncoderParameterValueTypeLong && IsEqualCLSID(&param.Guid, &param_guid))
4691 ULONG *value_array = (ULONG*) param.Value;
4692 for (value_idx = 0; value_idx < param.NumberOfValues; value_idx++)
4694 if (value_array[value_idx] == val)
4695 return TRUE;
4699 return FALSE;
4702 static GpStatus encode_image_wic(GpImage *image, IStream *stream,
4703 REFGUID container, GDIPCONST EncoderParameters *params)
4705 GpStatus status, terminate_status;
4707 if (image->type != ImageTypeBitmap)
4708 return GenericError;
4710 status = initialize_encoder_wic(stream, container, image);
4712 if (status == Ok)
4713 status = encode_frame_wic(image->encoder, image);
4715 if (!has_encoder_param_long(params, EncoderSaveFlag, EncoderValueMultiFrame))
4717 /* always try to terminate, but if something already failed earlier, keep the old status. */
4718 terminate_status = terminate_encoder_wic(image);
4719 if (status == Ok)
4720 status = terminate_status;
4723 return status;
4726 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4727 GDIPCONST EncoderParameters* params)
4729 return encode_image_wic(image, stream, &GUID_ContainerFormatBmp, params);
4732 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4733 GDIPCONST EncoderParameters* params)
4735 return encode_image_wic(image, stream, &GUID_ContainerFormatTiff, params);
4738 GpStatus encode_image_png(GpImage *image, IStream* stream,
4739 GDIPCONST EncoderParameters* params)
4741 return encode_image_wic(image, stream, &GUID_ContainerFormatPng, params);
4744 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4745 GDIPCONST EncoderParameters* params)
4747 return encode_image_wic(image, stream, &GUID_ContainerFormatJpeg, params);
4750 static GpStatus encode_image_gif(GpImage *image, IStream* stream,
4751 GDIPCONST EncoderParameters* params)
4753 return encode_image_wic(image, stream, &GUID_ContainerFormatGif, params);
4756 /*****************************************************************************
4757 * GdipSaveImageToStream [GDIPLUS.@]
4759 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4760 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4762 GpStatus stat;
4763 encode_image_func encode_image;
4764 int i;
4766 TRACE("%p, %p, %s, %p\n", image, stream, wine_dbgstr_guid(clsid), params);
4768 if(!image || !stream)
4769 return InvalidParameter;
4771 /* select correct encoder */
4772 encode_image = NULL;
4773 for (i = 0; i < NUM_CODECS; i++) {
4774 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4775 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4776 encode_image = codecs[i].encode_func;
4778 if (encode_image == NULL)
4779 return UnknownImageFormat;
4781 stat = encode_image(image, stream, params);
4783 return stat;
4786 /*****************************************************************************
4787 * GdipSaveAdd [GDIPLUS.@]
4789 * Like GdipSaveAddImage(), but encode the currently active frame of the given image into the file
4790 * or stream that is currently being encoded.
4792 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4794 return GdipSaveAddImage(image, image, params);
4797 /*****************************************************************************
4798 * GdipSaveAddImage [GDIPLUS.@]
4800 * Encode the currently active frame of additional_image into the file or stream that is currently
4801 * being encoded by the image given in the image parameter. The first frame of a multi-frame image
4802 * must be encoded using the normal GdipSaveImageToStream() or GdipSaveImageToFile() functions,
4803 * but with the "MultiFrame" encoding parameter set. The multi-frame encoding process must be
4804 * finished after adding the last frame by calling GdipSaveAdd() with the "Flush" encoding parameter
4805 * set.
4807 GpStatus WINGDIPAPI GdipSaveAddImage(GpImage *image, GpImage *additional_image,
4808 GDIPCONST EncoderParameters *params)
4810 TRACE("%p, %p, %p\n", image, additional_image, params);
4812 if (!image || !additional_image || !params)
4813 return InvalidParameter;
4815 if (!image->encoder)
4816 return Win32Error;
4818 if (has_encoder_param_long(params, EncoderSaveFlag, EncoderValueFlush))
4819 return terminate_encoder_wic(image);
4820 else if (has_encoder_param_long(params, EncoderSaveFlag, EncoderValueFrameDimensionPage))
4821 return encode_frame_wic(image->encoder, additional_image);
4822 else
4823 return InvalidParameter;
4826 /*****************************************************************************
4827 * GdipGetImagePalette [GDIPLUS.@]
4829 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4831 INT count;
4833 TRACE("(%p,%p,%i)\n", image, palette, size);
4835 if (!image || !palette)
4836 return InvalidParameter;
4838 count = image->palette ? image->palette->Count : 0;
4840 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4842 TRACE("<-- InsufficientBuffer\n");
4843 return InsufficientBuffer;
4846 if (image->palette)
4848 palette->Flags = image->palette->Flags;
4849 palette->Count = image->palette->Count;
4850 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4852 else
4854 palette->Flags = 0;
4855 palette->Count = 0;
4857 return Ok;
4860 /*****************************************************************************
4861 * GdipSetImagePalette [GDIPLUS.@]
4863 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4864 GDIPCONST ColorPalette *palette)
4866 ColorPalette *new_palette;
4868 TRACE("(%p,%p)\n", image, palette);
4870 if(!image || !palette || palette->Count > 256)
4871 return InvalidParameter;
4873 new_palette = heap_alloc_zero(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4874 if (!new_palette) return OutOfMemory;
4876 heap_free(image->palette);
4877 image->palette = new_palette;
4878 image->palette->Flags = palette->Flags;
4879 image->palette->Count = palette->Count;
4880 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4882 return Ok;
4885 /*************************************************************************
4886 * Encoders -
4887 * Structures that represent which formats we support for encoding.
4890 /* ImageCodecInfo creation routines taken from libgdiplus */
4891 static const WCHAR bmp_codecname[] = L"Built-in BMP";
4892 static const WCHAR bmp_extension[] = L"*.BMP;*.DIB;*.RLE";
4893 static const WCHAR bmp_mimetype[] = L"image/bmp";
4894 static const WCHAR bmp_format[] = L"BMP";
4895 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4896 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4898 static const WCHAR jpeg_codecname[] = L"Built-in JPEG";
4899 static const WCHAR jpeg_extension[] = L"*.JPG;*.JPEG;*.JPE;*.JFIF";
4900 static const WCHAR jpeg_mimetype[] = L"image/jpeg";
4901 static const WCHAR jpeg_format[] = L"JPEG";
4902 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4903 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4905 static const WCHAR gif_codecname[] = L"Built-in GIF";
4906 static const WCHAR gif_extension[] = L"*.GIF";
4907 static const WCHAR gif_mimetype[] = L"image/gif";
4908 static const WCHAR gif_format[] = L"GIF";
4909 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4910 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4912 static const WCHAR tiff_codecname[] = L"Built-in TIFF";
4913 static const WCHAR tiff_extension[] = L"*.TIFF;*.TIF";
4914 static const WCHAR tiff_mimetype[] = L"image/tiff";
4915 static const WCHAR tiff_format[] = L"TIFF";
4916 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4917 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4919 static const WCHAR emf_codecname[] = L"Built-in EMF";
4920 static const WCHAR emf_extension[] = L"*.EMF";
4921 static const WCHAR emf_mimetype[] = L"image/x-emf";
4922 static const WCHAR emf_format[] = L"EMF";
4923 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4924 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4926 static const WCHAR wmf_codecname[] = L"Built-in WMF";
4927 static const WCHAR wmf_extension[] = L"*.WMF";
4928 static const WCHAR wmf_mimetype[] = L"image/x-wmf";
4929 static const WCHAR wmf_format[] = L"WMF";
4930 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4931 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4933 static const WCHAR png_codecname[] = L"Built-in PNG";
4934 static const WCHAR png_extension[] = L"*.PNG";
4935 static const WCHAR png_mimetype[] = L"image/png";
4936 static const WCHAR png_format[] = L"PNG";
4937 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4938 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4940 static const WCHAR ico_codecname[] = L"Built-in ICO";
4941 static const WCHAR ico_extension[] = L"*.ICO";
4942 static const WCHAR ico_mimetype[] = L"image/x-icon";
4943 static const WCHAR ico_format[] = L"ICO";
4944 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4945 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4947 static const struct image_codec codecs[NUM_CODECS] = {
4949 { /* BMP */
4950 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4951 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4952 /* CodecName */ bmp_codecname,
4953 /* DllName */ NULL,
4954 /* FormatDescription */ bmp_format,
4955 /* FilenameExtension */ bmp_extension,
4956 /* MimeType */ bmp_mimetype,
4957 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4958 /* Version */ 1,
4959 /* SigCount */ 1,
4960 /* SigSize */ 2,
4961 /* SigPattern */ bmp_sig_pattern,
4962 /* SigMask */ bmp_sig_mask,
4964 encode_image_BMP,
4965 decode_image_bmp,
4966 select_frame_wic
4969 { /* JPEG */
4970 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4971 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4972 /* CodecName */ jpeg_codecname,
4973 /* DllName */ NULL,
4974 /* FormatDescription */ jpeg_format,
4975 /* FilenameExtension */ jpeg_extension,
4976 /* MimeType */ jpeg_mimetype,
4977 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4978 /* Version */ 1,
4979 /* SigCount */ 1,
4980 /* SigSize */ 2,
4981 /* SigPattern */ jpeg_sig_pattern,
4982 /* SigMask */ jpeg_sig_mask,
4984 encode_image_jpeg,
4985 decode_image_jpeg,
4986 select_frame_wic
4989 { /* GIF */
4990 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4991 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4992 /* CodecName */ gif_codecname,
4993 /* DllName */ NULL,
4994 /* FormatDescription */ gif_format,
4995 /* FilenameExtension */ gif_extension,
4996 /* MimeType */ gif_mimetype,
4997 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4998 /* Version */ 1,
4999 /* SigCount */ 2,
5000 /* SigSize */ 6,
5001 /* SigPattern */ gif_sig_pattern,
5002 /* SigMask */ gif_sig_mask,
5004 encode_image_gif,
5005 decode_image_gif,
5006 select_frame_gif
5009 { /* TIFF */
5010 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5011 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5012 /* CodecName */ tiff_codecname,
5013 /* DllName */ NULL,
5014 /* FormatDescription */ tiff_format,
5015 /* FilenameExtension */ tiff_extension,
5016 /* MimeType */ tiff_mimetype,
5017 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5018 /* Version */ 1,
5019 /* SigCount */ 2,
5020 /* SigSize */ 4,
5021 /* SigPattern */ tiff_sig_pattern,
5022 /* SigMask */ tiff_sig_mask,
5024 encode_image_tiff,
5025 decode_image_tiff,
5026 select_frame_wic
5029 { /* EMF */
5030 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5031 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5032 /* CodecName */ emf_codecname,
5033 /* DllName */ NULL,
5034 /* FormatDescription */ emf_format,
5035 /* FilenameExtension */ emf_extension,
5036 /* MimeType */ emf_mimetype,
5037 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
5038 /* Version */ 1,
5039 /* SigCount */ 1,
5040 /* SigSize */ 4,
5041 /* SigPattern */ emf_sig_pattern,
5042 /* SigMask */ emf_sig_mask,
5044 NULL,
5045 decode_image_emf,
5046 NULL
5049 { /* WMF */
5050 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5051 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5052 /* CodecName */ wmf_codecname,
5053 /* DllName */ NULL,
5054 /* FormatDescription */ wmf_format,
5055 /* FilenameExtension */ wmf_extension,
5056 /* MimeType */ wmf_mimetype,
5057 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
5058 /* Version */ 1,
5059 /* SigCount */ 1,
5060 /* SigSize */ 2,
5061 /* SigPattern */ wmf_sig_pattern,
5062 /* SigMask */ wmf_sig_mask,
5064 NULL,
5065 decode_image_wmf,
5066 NULL
5069 { /* PNG */
5070 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5071 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5072 /* CodecName */ png_codecname,
5073 /* DllName */ NULL,
5074 /* FormatDescription */ png_format,
5075 /* FilenameExtension */ png_extension,
5076 /* MimeType */ png_mimetype,
5077 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5078 /* Version */ 1,
5079 /* SigCount */ 1,
5080 /* SigSize */ 8,
5081 /* SigPattern */ png_sig_pattern,
5082 /* SigMask */ png_sig_mask,
5084 encode_image_png,
5085 decode_image_png,
5086 select_frame_wic
5089 { /* ICO */
5090 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5091 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5092 /* CodecName */ ico_codecname,
5093 /* DllName */ NULL,
5094 /* FormatDescription */ ico_format,
5095 /* FilenameExtension */ ico_extension,
5096 /* MimeType */ ico_mimetype,
5097 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5098 /* Version */ 1,
5099 /* SigCount */ 1,
5100 /* SigSize */ 4,
5101 /* SigPattern */ ico_sig_pattern,
5102 /* SigMask */ ico_sig_mask,
5104 NULL,
5105 decode_image_icon,
5106 select_frame_wic
5110 /*****************************************************************************
5111 * GdipGetImageDecodersSize [GDIPLUS.@]
5113 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
5115 int decoder_count=0;
5116 int i;
5117 TRACE("%p %p\n", numDecoders, size);
5119 if (!numDecoders || !size)
5120 return InvalidParameter;
5122 for (i=0; i<NUM_CODECS; i++)
5124 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
5125 decoder_count++;
5128 *numDecoders = decoder_count;
5129 *size = decoder_count * sizeof(ImageCodecInfo);
5131 return Ok;
5134 /*****************************************************************************
5135 * GdipGetImageDecoders [GDIPLUS.@]
5137 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
5139 int i, decoder_count=0;
5140 TRACE("%u %u %p\n", numDecoders, size, decoders);
5142 if (!decoders ||
5143 size != numDecoders * sizeof(ImageCodecInfo))
5144 return GenericError;
5146 for (i=0; i<NUM_CODECS; i++)
5148 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
5150 if (decoder_count == numDecoders) return GenericError;
5151 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
5152 decoder_count++;
5156 if (decoder_count < numDecoders) return GenericError;
5158 return Ok;
5161 /*****************************************************************************
5162 * GdipGetImageEncodersSize [GDIPLUS.@]
5164 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
5166 int encoder_count=0;
5167 int i;
5168 TRACE("%p %p\n", numEncoders, size);
5170 if (!numEncoders || !size)
5171 return InvalidParameter;
5173 for (i=0; i<NUM_CODECS; i++)
5175 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
5176 encoder_count++;
5179 *numEncoders = encoder_count;
5180 *size = encoder_count * sizeof(ImageCodecInfo);
5182 return Ok;
5185 /*****************************************************************************
5186 * GdipGetImageEncoders [GDIPLUS.@]
5188 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
5190 int i, encoder_count=0;
5191 TRACE("%u %u %p\n", numEncoders, size, encoders);
5193 if (!encoders ||
5194 size != numEncoders * sizeof(ImageCodecInfo))
5195 return GenericError;
5197 for (i=0; i<NUM_CODECS; i++)
5199 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
5201 if (encoder_count == numEncoders) return GenericError;
5202 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
5203 encoder_count++;
5207 if (encoder_count < numEncoders) return GenericError;
5209 return Ok;
5212 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
5213 GDIPCONST CLSID* clsidEncoder, UINT *size)
5215 static int calls;
5217 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
5219 if(!(calls++))
5220 FIXME("not implemented\n");
5222 *size = 0;
5224 return NotImplemented;
5227 static PixelFormat get_16bpp_format(HBITMAP hbm)
5229 BITMAPV4HEADER bmh;
5230 HDC hdc;
5231 PixelFormat result;
5233 hdc = CreateCompatibleDC(NULL);
5235 memset(&bmh, 0, sizeof(bmh));
5236 bmh.bV4Size = sizeof(bmh);
5237 bmh.bV4Width = 1;
5238 bmh.bV4Height = 1;
5239 bmh.bV4V4Compression = BI_BITFIELDS;
5240 bmh.bV4BitCount = 16;
5242 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
5244 if (bmh.bV4RedMask == 0x7c00 &&
5245 bmh.bV4GreenMask == 0x3e0 &&
5246 bmh.bV4BlueMask == 0x1f)
5248 result = PixelFormat16bppRGB555;
5250 else if (bmh.bV4RedMask == 0xf800 &&
5251 bmh.bV4GreenMask == 0x7e0 &&
5252 bmh.bV4BlueMask == 0x1f)
5254 result = PixelFormat16bppRGB565;
5256 else
5258 FIXME("unrecognized bitfields %lx,%lx,%lx\n", bmh.bV4RedMask,
5259 bmh.bV4GreenMask, bmh.bV4BlueMask);
5260 result = PixelFormatUndefined;
5263 DeleteDC(hdc);
5265 return result;
5268 /*****************************************************************************
5269 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
5271 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
5273 BITMAP bm;
5274 GpStatus retval;
5275 PixelFormat format;
5276 BitmapData lockeddata;
5277 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
5278 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
5280 TRACE("%p %p %p\n", hbm, hpal, bitmap);
5282 if(!hbm || !bitmap)
5283 return InvalidParameter;
5285 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
5286 return InvalidParameter;
5288 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
5289 switch(bm.bmBitsPixel) {
5290 case 1:
5291 format = PixelFormat1bppIndexed;
5292 break;
5293 case 4:
5294 format = PixelFormat4bppIndexed;
5295 break;
5296 case 8:
5297 format = PixelFormat8bppIndexed;
5298 break;
5299 case 16:
5300 format = get_16bpp_format(hbm);
5301 if (format == PixelFormatUndefined)
5302 return InvalidParameter;
5303 break;
5304 case 24:
5305 format = PixelFormat24bppRGB;
5306 break;
5307 case 32:
5308 format = PixelFormat32bppRGB;
5309 break;
5310 case 48:
5311 format = PixelFormat48bppRGB;
5312 break;
5313 default:
5314 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
5315 return InvalidParameter;
5318 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
5319 format, NULL, bitmap);
5321 if (retval == Ok)
5323 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
5324 format, &lockeddata);
5325 if (retval == Ok)
5327 HDC hdc;
5328 INT src_height;
5330 hdc = CreateCompatibleDC(NULL);
5332 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
5333 pbmi->bmiHeader.biBitCount = 0;
5335 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
5337 src_height = abs(pbmi->bmiHeader.biHeight);
5338 pbmi->bmiHeader.biHeight = -src_height;
5340 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
5342 DeleteDC(hdc);
5344 GdipBitmapUnlockBits(*bitmap, &lockeddata);
5347 /* According to the tests hpal is ignored */
5348 if (retval == Ok && pbmi->bmiHeader.biBitCount <= 8)
5350 ColorPalette *palette;
5351 int i, num_palette_entries;
5353 num_palette_entries = pbmi->bmiHeader.biClrUsed;
5354 if (!num_palette_entries)
5355 num_palette_entries = 1 << pbmi->bmiHeader.biBitCount;
5357 palette = heap_alloc_zero(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
5358 if (!palette)
5359 retval = OutOfMemory;
5360 else
5362 palette->Flags = 0;
5363 palette->Count = num_palette_entries;
5365 for (i=0; i<num_palette_entries; i++)
5367 palette->Entries[i] = 0xff000000 | pbmi->bmiColors[i].rgbRed << 16 |
5368 pbmi->bmiColors[i].rgbGreen << 8 | pbmi->bmiColors[i].rgbBlue;
5371 retval = GdipSetImagePalette(&(*bitmap)->image, palette);
5374 heap_free(palette);
5377 if (retval != Ok)
5379 GdipDisposeImage(&(*bitmap)->image);
5380 *bitmap = NULL;
5384 return retval;
5387 /*****************************************************************************
5388 * GdipCreateEffect [GDIPLUS.@]
5390 GpStatus WINGDIPAPI GdipCreateEffect(const GUID guid, CGpEffect **effect)
5392 FIXME("(%s, %p): stub\n", debugstr_guid(&guid), effect);
5394 if(!effect)
5395 return InvalidParameter;
5397 *effect = NULL;
5399 return NotImplemented;
5402 /*****************************************************************************
5403 * GdipDeleteEffect [GDIPLUS.@]
5405 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
5407 FIXME("(%p): stub\n", effect);
5408 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5409 * in Windows's gdiplus */
5410 return NotImplemented;
5413 /*****************************************************************************
5414 * GdipSetEffectParameters [GDIPLUS.@]
5416 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
5417 const VOID *params, const UINT size)
5419 static int calls;
5421 TRACE("(%p,%p,%u)\n", effect, params, size);
5423 if(!(calls++))
5424 FIXME("not implemented\n");
5426 return NotImplemented;
5429 /*****************************************************************************
5430 * GdipGetImageFlags [GDIPLUS.@]
5432 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
5434 TRACE("%p %p\n", image, flags);
5436 if(!image || !flags)
5437 return InvalidParameter;
5439 *flags = image->flags;
5441 return Ok;
5444 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
5446 TRACE("(%d, %p)\n", control, param);
5448 switch(control){
5449 case TestControlForceBilinear:
5450 if(param)
5451 FIXME("TestControlForceBilinear not handled\n");
5452 break;
5453 case TestControlNoICM:
5454 if(param)
5455 FIXME("TestControlNoICM not handled\n");
5456 break;
5457 case TestControlGetBuildNumber:
5458 *((DWORD*)param) = 3102;
5459 break;
5462 return Ok;
5465 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
5467 TRACE("%p\n", image);
5469 return Ok;
5472 /*****************************************************************************
5473 * GdipGetImageThumbnail [GDIPLUS.@]
5475 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
5476 GpImage **ret_image, GetThumbnailImageAbort cb,
5477 VOID * cb_data)
5479 GpStatus stat;
5480 GpGraphics *graphics;
5481 UINT srcwidth, srcheight;
5483 TRACE("(%p %u %u %p %p %p)\n",
5484 image, width, height, ret_image, cb, cb_data);
5486 if (!image || !ret_image)
5487 return InvalidParameter;
5489 if (!width) width = 120;
5490 if (!height) height = 120;
5492 GdipGetImageWidth(image, &srcwidth);
5493 GdipGetImageHeight(image, &srcheight);
5495 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
5496 NULL, (GpBitmap**)ret_image);
5498 if (stat == Ok)
5500 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
5502 if (stat == Ok)
5504 stat = GdipDrawImageRectRectI(graphics, image,
5505 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
5506 NULL, NULL, NULL);
5508 GdipDeleteGraphics(graphics);
5511 if (stat != Ok)
5513 GdipDisposeImage(*ret_image);
5514 *ret_image = NULL;
5518 return stat;
5521 /*****************************************************************************
5522 * GdipImageRotateFlip [GDIPLUS.@]
5524 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
5526 GpBitmap *new_bitmap;
5527 GpBitmap *bitmap;
5528 int bpp, bytesperpixel;
5529 BOOL rotate_90, flip_x, flip_y;
5530 int src_x_offset, src_y_offset;
5531 LPBYTE src_origin;
5532 UINT x, y, width, height;
5533 BitmapData src_lock, dst_lock;
5534 GpStatus stat;
5535 BOOL unlock;
5537 TRACE("(%p, %u)\n", image, type);
5539 if (!image)
5540 return InvalidParameter;
5541 if (!image_lock(image, &unlock))
5542 return ObjectBusy;
5544 rotate_90 = type&1;
5545 flip_x = (type&6) == 2 || (type&6) == 4;
5546 flip_y = (type&3) == 1 || (type&3) == 2;
5548 if (image->type != ImageTypeBitmap)
5550 FIXME("Not implemented for type %i\n", image->type);
5551 image_unlock(image, unlock);
5552 return NotImplemented;
5555 bitmap = (GpBitmap*)image;
5556 bpp = PIXELFORMATBPP(bitmap->format);
5558 if (bpp < 8)
5560 FIXME("Not implemented for %i bit images\n", bpp);
5561 image_unlock(image, unlock);
5562 return NotImplemented;
5565 if (rotate_90)
5567 width = bitmap->height;
5568 height = bitmap->width;
5570 else
5572 width = bitmap->width;
5573 height = bitmap->height;
5576 bytesperpixel = bpp/8;
5578 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5580 if (stat != Ok)
5582 image_unlock(image, unlock);
5583 return stat;
5586 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
5588 if (stat == Ok)
5590 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5592 if (stat == Ok)
5594 LPBYTE src_row, src_pixel;
5595 LPBYTE dst_row, dst_pixel;
5597 src_origin = src_lock.Scan0;
5598 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5599 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
5601 if (rotate_90)
5603 if (flip_y) src_x_offset = -src_lock.Stride;
5604 else src_x_offset = src_lock.Stride;
5605 if (flip_x) src_y_offset = -bytesperpixel;
5606 else src_y_offset = bytesperpixel;
5608 else
5610 if (flip_x) src_x_offset = -bytesperpixel;
5611 else src_x_offset = bytesperpixel;
5612 if (flip_y) src_y_offset = -src_lock.Stride;
5613 else src_y_offset = src_lock.Stride;
5616 src_row = src_origin;
5617 dst_row = dst_lock.Scan0;
5618 for (y=0; y<height; y++)
5620 src_pixel = src_row;
5621 dst_pixel = dst_row;
5622 for (x=0; x<width; x++)
5624 /* FIXME: This could probably be faster without memcpy. */
5625 memcpy(dst_pixel, src_pixel, bytesperpixel);
5626 dst_pixel += bytesperpixel;
5627 src_pixel += src_x_offset;
5629 src_row += src_y_offset;
5630 dst_row += dst_lock.Stride;
5633 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5636 GdipBitmapUnlockBits(bitmap, &src_lock);
5639 if (stat == Ok)
5640 move_bitmap(bitmap, new_bitmap, FALSE);
5641 else
5642 GdipDisposeImage(&new_bitmap->image);
5644 image_unlock(image, unlock);
5645 return stat;
5648 /*****************************************************************************
5649 * GdipImageSetAbort [GDIPLUS.@]
5651 GpStatus WINGDIPAPI GdipImageSetAbort(GpImage *image, GdiplusAbort *pabort)
5653 TRACE("(%p, %p)\n", image, pabort);
5655 if (!image)
5656 return InvalidParameter;
5658 if (pabort)
5659 FIXME("Abort callback is not supported.\n");
5661 return Ok;
5664 /*****************************************************************************
5665 * GdipBitmapConvertFormat [GDIPLUS.@]
5667 GpStatus WINGDIPAPI GdipBitmapConvertFormat(GpBitmap *bitmap, PixelFormat format, DitherType dithertype,
5668 PaletteType palettetype, ColorPalette *palette, REAL alphathreshold)
5670 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap, format, dithertype, palettetype, palette, alphathreshold);
5671 return NotImplemented;
5674 static void set_histogram_point_argb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5676 ch0[ color >> 24 ]++;
5677 ch1[(color >> 16) & 0xff]++;
5678 ch2[(color >> 8) & 0xff]++;
5679 ch3[ color & 0xff]++;
5682 static void set_histogram_point_pargb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5684 BYTE alpha = color >> 24;
5686 ch0[alpha]++;
5687 ch1[(((color >> 16) & 0xff) * alpha) / 0xff]++;
5688 ch2[(((color >> 8) & 0xff) * alpha) / 0xff]++;
5689 ch3[(( color & 0xff) * alpha) / 0xff]++;
5692 static void set_histogram_point_rgb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5694 ch0[(color >> 16) & 0xff]++;
5695 ch1[(color >> 8) & 0xff]++;
5696 ch2[ color & 0xff]++;
5699 static void set_histogram_point_gray(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5701 ch0[(76 * ((color >> 16) & 0xff) + 150 * ((color >> 8) & 0xff) + 29 * (color & 0xff)) / 0xff]++;
5704 static void set_histogram_point_b(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5706 ch0[color & 0xff]++;
5709 static void set_histogram_point_g(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5711 ch0[(color >> 8) & 0xff]++;
5714 static void set_histogram_point_r(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5716 ch0[(color >> 16) & 0xff]++;
5719 static void set_histogram_point_a(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5721 ch0[(color >> 24) & 0xff]++;
5724 /*****************************************************************************
5725 * GdipBitmapGetHistogram [GDIPLUS.@]
5727 GpStatus WINGDIPAPI GdipBitmapGetHistogram(GpBitmap *bitmap, HistogramFormat format, UINT num_of_entries,
5728 UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5730 static void (* const set_histogram_point[])(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3) =
5732 set_histogram_point_argb,
5733 set_histogram_point_pargb,
5734 set_histogram_point_rgb,
5735 set_histogram_point_gray,
5736 set_histogram_point_b,
5737 set_histogram_point_g,
5738 set_histogram_point_r,
5739 set_histogram_point_a,
5741 UINT width, height, x, y;
5743 TRACE("(%p, %d, %u, %p, %p, %p, %p)\n", bitmap, format, num_of_entries,
5744 ch0, ch1, ch2, ch3);
5746 if (!bitmap || num_of_entries != 256)
5747 return InvalidParameter;
5749 /* Make sure passed channel pointers match requested format */
5750 switch (format)
5752 case HistogramFormatARGB:
5753 case HistogramFormatPARGB:
5754 if (!ch0 || !ch1 || !ch2 || !ch3)
5755 return InvalidParameter;
5756 memset(ch0, 0, num_of_entries * sizeof(UINT));
5757 memset(ch1, 0, num_of_entries * sizeof(UINT));
5758 memset(ch2, 0, num_of_entries * sizeof(UINT));
5759 memset(ch3, 0, num_of_entries * sizeof(UINT));
5760 break;
5761 case HistogramFormatRGB:
5762 if (!ch0 || !ch1 || !ch2 || ch3)
5763 return InvalidParameter;
5764 memset(ch0, 0, num_of_entries * sizeof(UINT));
5765 memset(ch1, 0, num_of_entries * sizeof(UINT));
5766 memset(ch2, 0, num_of_entries * sizeof(UINT));
5767 break;
5768 case HistogramFormatGray:
5769 case HistogramFormatB:
5770 case HistogramFormatG:
5771 case HistogramFormatR:
5772 case HistogramFormatA:
5773 if (!ch0 || ch1 || ch2 || ch3)
5774 return InvalidParameter;
5775 memset(ch0, 0, num_of_entries * sizeof(UINT));
5776 break;
5777 default:
5778 WARN("Invalid histogram format requested, %d\n", format);
5779 return InvalidParameter;
5782 GdipGetImageWidth(&bitmap->image, &width);
5783 GdipGetImageHeight(&bitmap->image, &height);
5785 for (y = 0; y < height; y++)
5786 for (x = 0; x < width; x++)
5788 ARGB color;
5790 GdipBitmapGetPixel(bitmap, x, y, &color);
5791 set_histogram_point[format](color, ch0, ch1, ch2, ch3);
5794 return Ok;
5797 /*****************************************************************************
5798 * GdipBitmapGetHistogramSize [GDIPLUS.@]
5800 GpStatus WINGDIPAPI GdipBitmapGetHistogramSize(HistogramFormat format, UINT *num_of_entries)
5802 TRACE("(%d, %p)\n", format, num_of_entries);
5804 if (!num_of_entries)
5805 return InvalidParameter;
5807 *num_of_entries = 256;
5808 return Ok;
5811 static GpStatus create_optimal_palette(ColorPalette *palette, INT desired,
5812 BOOL transparent, GpBitmap *bitmap)
5814 GpStatus status;
5815 BitmapData data;
5816 HRESULT hr;
5817 IWICImagingFactory *factory;
5818 IWICPalette *wic_palette;
5820 if (!bitmap) return InvalidParameter;
5821 if (palette->Count < desired) return GenericError;
5823 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, PixelFormat24bppRGB, &data);
5824 if (status != Ok) return status;
5826 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
5827 if (hr != S_OK)
5829 GdipBitmapUnlockBits(bitmap, &data);
5830 return hresult_to_status(hr);
5833 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
5834 if (hr == S_OK)
5836 IWICBitmap *bitmap;
5838 /* PixelFormat24bppRGB actually stores the bitmap bits as BGR. */
5839 hr = IWICImagingFactory_CreateBitmapFromMemory(factory, data.Width, data.Height,
5840 &GUID_WICPixelFormat24bppBGR, data.Stride, data.Stride * data.Width, data.Scan0, &bitmap);
5841 if (hr == S_OK)
5843 hr = IWICPalette_InitializeFromBitmap(wic_palette, (IWICBitmapSource *)bitmap, desired, transparent);
5844 if (hr == S_OK)
5846 palette->Flags = 0;
5847 IWICPalette_GetColorCount(wic_palette, &palette->Count);
5848 IWICPalette_GetColors(wic_palette, palette->Count, (UINT *)palette->Entries, &palette->Count);
5851 IWICBitmap_Release(bitmap);
5854 IWICPalette_Release(wic_palette);
5857 IWICImagingFactory_Release(factory);
5858 GdipBitmapUnlockBits(bitmap, &data);
5860 return hresult_to_status(hr);
5863 /*****************************************************************************
5864 * GdipInitializePalette [GDIPLUS.@]
5866 GpStatus WINGDIPAPI GdipInitializePalette(ColorPalette *palette,
5867 PaletteType type, INT desired, BOOL transparent, GpBitmap *bitmap)
5869 TRACE("(%p,%d,%d,%d,%p)\n", palette, type, desired, transparent, bitmap);
5871 if (!palette) return InvalidParameter;
5873 switch (type)
5875 case PaletteTypeCustom:
5876 return Ok;
5878 case PaletteTypeOptimal:
5879 return create_optimal_palette(palette, desired, transparent, bitmap);
5881 /* WIC palette type enumeration matches these gdiplus enums */
5882 case PaletteTypeFixedBW:
5883 case PaletteTypeFixedHalftone8:
5884 case PaletteTypeFixedHalftone27:
5885 case PaletteTypeFixedHalftone64:
5886 case PaletteTypeFixedHalftone125:
5887 case PaletteTypeFixedHalftone216:
5888 case PaletteTypeFixedHalftone252:
5889 case PaletteTypeFixedHalftone256:
5891 ColorPalette *wic_palette;
5892 GpStatus status = Ok;
5894 wic_palette = get_palette(NULL, type);
5895 if (!wic_palette) return OutOfMemory;
5897 if (palette->Count >= wic_palette->Count)
5899 palette->Flags = wic_palette->Flags;
5900 palette->Count = wic_palette->Count;
5901 memcpy(palette->Entries, wic_palette->Entries, wic_palette->Count * sizeof(wic_palette->Entries[0]));
5903 else
5904 status = GenericError;
5906 heap_free(wic_palette);
5908 return status;
5911 default:
5912 FIXME("unknown palette type %d\n", type);
5913 break;
5916 return InvalidParameter;