d3d9/tests: Don't fail d3d9ex if the window manager restores focus too soon.
[wine.git] / dlls / gdiplus / image.c
blobc6aa2ce5dd5baa6f02106921a496a0a054cece69
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 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
28 #define COBJMACROS
29 #include "objbase.h"
30 #include "olectl.h"
31 #include "ole2.h"
33 #include "initguid.h"
34 #include "wincodec.h"
35 #include "gdiplus.h"
36 #include "gdiplus_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
41 HRESULT WINAPI WICCreateImagingFactory_Proxy(UINT, IWICImagingFactory**);
43 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
44 #define WMF_PLACEABLE_KEY 0x9ac6cdd7
46 static const struct
48 const WICPixelFormatGUID *wic_format;
49 PixelFormat gdip_format;
50 /* predefined palette type to use for pixel format conversions */
51 WICBitmapPaletteType palette_type;
52 } pixel_formats[] =
54 { &GUID_WICPixelFormat1bppIndexed, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
55 { &GUID_WICPixelFormatBlackWhite, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
56 { &GUID_WICPixelFormat4bppIndexed, PixelFormat4bppIndexed, WICBitmapPaletteTypeFixedHalftone8 },
57 { &GUID_WICPixelFormat8bppIndexed, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedHalftone256 },
58 { &GUID_WICPixelFormat8bppGray, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedGray256 },
59 { &GUID_WICPixelFormat16bppBGR555, PixelFormat16bppRGB555, 0 },
60 { &GUID_WICPixelFormat24bppBGR, PixelFormat24bppRGB, 0 },
61 { &GUID_WICPixelFormat32bppBGR, PixelFormat32bppRGB, 0 },
62 { &GUID_WICPixelFormat32bppBGRA, PixelFormat32bppARGB, 0 },
63 { &GUID_WICPixelFormat32bppPBGRA, PixelFormat32bppPARGB, 0 },
64 { NULL }
67 static ColorPalette *get_palette(IWICBitmapFrameDecode *frame, WICBitmapPaletteType palette_type)
69 HRESULT hr;
70 IWICImagingFactory *factory;
71 IWICPalette *wic_palette;
72 ColorPalette *palette = NULL;
74 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
75 if (hr != S_OK) return NULL;
77 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
78 if (hr == S_OK)
80 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
81 if (frame)
82 hr = IWICBitmapFrameDecode_CopyPalette(frame, wic_palette);
83 if (hr != S_OK && palette_type != 0)
85 TRACE("using predefined palette %#x\n", palette_type);
86 hr = IWICPalette_InitializePredefined(wic_palette, palette_type, FALSE);
88 if (hr == S_OK)
90 WICBitmapPaletteType type;
91 BOOL alpha;
92 UINT count;
94 IWICPalette_GetColorCount(wic_palette, &count);
95 palette = heap_alloc(2 * sizeof(UINT) + count * sizeof(ARGB));
96 IWICPalette_GetColors(wic_palette, count, (UINT *)palette->Entries, &palette->Count);
98 IWICPalette_GetType(wic_palette, &type);
99 switch(type) {
100 case WICBitmapPaletteTypeFixedGray4:
101 case WICBitmapPaletteTypeFixedGray16:
102 case WICBitmapPaletteTypeFixedGray256:
103 palette->Flags = PaletteFlagsGrayScale;
104 break;
105 case WICBitmapPaletteTypeFixedHalftone8:
106 case WICBitmapPaletteTypeFixedHalftone27:
107 case WICBitmapPaletteTypeFixedHalftone64:
108 case WICBitmapPaletteTypeFixedHalftone125:
109 case WICBitmapPaletteTypeFixedHalftone216:
110 case WICBitmapPaletteTypeFixedHalftone252:
111 case WICBitmapPaletteTypeFixedHalftone256:
112 palette->Flags = PaletteFlagsHalftone;
113 break;
114 default:
115 palette->Flags = 0;
117 IWICPalette_HasAlpha(wic_palette, &alpha);
118 if(alpha)
119 palette->Flags |= PaletteFlagsHasAlpha;
121 IWICPalette_Release(wic_palette);
123 IWICImagingFactory_Release(factory);
124 return palette;
127 static HRESULT set_palette(IWICBitmapFrameEncode *frame, ColorPalette *palette)
129 HRESULT hr;
130 IWICImagingFactory *factory;
131 IWICPalette *wic_palette;
133 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
134 if (FAILED(hr))
135 return hr;
137 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
138 IWICImagingFactory_Release(factory);
139 if (SUCCEEDED(hr))
141 hr = IWICPalette_InitializeCustom(wic_palette, (UINT *)palette->Entries, palette->Count);
143 if (SUCCEEDED(hr))
144 hr = IWICBitmapFrameEncode_SetPalette(frame, wic_palette);
146 IWICPalette_Release(wic_palette);
149 return hr;
152 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
153 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
155 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
157 * Note: According to Jose Roca's GDI+ docs, this function is not
158 * implemented in Windows's GDI+.
160 return NotImplemented;
163 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
164 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
165 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
167 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
169 * Note: According to Jose Roca's GDI+ docs, this function is not
170 * implemented in Windows's GDI+.
172 return NotImplemented;
175 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
177 *index = (row[x/8]>>(7-x%8)) & 1;
180 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
182 if (x & 1)
183 *index = row[x/2]&0xf;
184 else
185 *index = row[x/2]>>4;
188 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
190 *index = row[x];
193 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
194 const BYTE *row, UINT x)
196 *r = *g = *b = row[x*2+1];
197 *a = 255;
200 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
201 const BYTE *row, UINT x)
203 WORD pixel = *((const WORD*)(row)+x);
204 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
205 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
206 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
207 *a = 255;
210 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
211 const BYTE *row, UINT x)
213 WORD pixel = *((const WORD*)(row)+x);
214 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
215 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
216 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
217 *a = 255;
220 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
221 const BYTE *row, UINT x)
223 WORD pixel = *((const WORD*)(row)+x);
224 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
225 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
226 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
227 if ((pixel&0x8000) == 0x8000)
228 *a = 255;
229 else
230 *a = 0;
233 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
234 const BYTE *row, UINT x)
236 *r = row[x*3+2];
237 *g = row[x*3+1];
238 *b = row[x*3];
239 *a = 255;
242 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
243 const BYTE *row, UINT x)
245 *r = row[x*4+2];
246 *g = row[x*4+1];
247 *b = row[x*4];
248 *a = 255;
251 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
252 const BYTE *row, UINT x)
254 *r = row[x*4+2];
255 *g = row[x*4+1];
256 *b = row[x*4];
257 *a = row[x*4+3];
260 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
261 const BYTE *row, UINT x)
263 *a = row[x*4+3];
264 if (*a == 0)
266 *r = row[x*4+2];
267 *g = row[x*4+1];
268 *b = row[x*4];
270 else
272 DWORD scaled_q = (255 << 15) / *a;
273 *r = (row[x*4+2] > *a) ? 0xff : (row[x*4+2] * scaled_q) >> 15;
274 *g = (row[x*4+1] > *a) ? 0xff : (row[x*4+1] * scaled_q) >> 15;
275 *b = (row[x*4] > *a) ? 0xff : (row[x*4] * scaled_q) >> 15;
279 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
280 const BYTE *row, UINT x)
282 *r = row[x*6+5];
283 *g = row[x*6+3];
284 *b = row[x*6+1];
285 *a = 255;
288 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
289 const BYTE *row, UINT x)
291 *r = row[x*8+5];
292 *g = row[x*8+3];
293 *b = row[x*8+1];
294 *a = row[x*8+7];
297 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
298 const BYTE *row, UINT x)
300 *a = row[x*8+7];
301 if (*a == 0)
302 *r = *g = *b = 0;
303 else
305 *r = row[x*8+5] * 255 / *a;
306 *g = row[x*8+3] * 255 / *a;
307 *b = row[x*8+1] * 255 / *a;
311 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
312 ARGB *color)
314 BYTE r, g, b, a;
315 BYTE index;
316 BYTE *row;
318 if(!bitmap || !color ||
319 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
320 return InvalidParameter;
322 row = bitmap->bits+bitmap->stride*y;
324 switch (bitmap->format)
326 case PixelFormat1bppIndexed:
327 getpixel_1bppIndexed(&index,row,x);
328 break;
329 case PixelFormat4bppIndexed:
330 getpixel_4bppIndexed(&index,row,x);
331 break;
332 case PixelFormat8bppIndexed:
333 getpixel_8bppIndexed(&index,row,x);
334 break;
335 case PixelFormat16bppGrayScale:
336 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
337 break;
338 case PixelFormat16bppRGB555:
339 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
340 break;
341 case PixelFormat16bppRGB565:
342 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
343 break;
344 case PixelFormat16bppARGB1555:
345 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
346 break;
347 case PixelFormat24bppRGB:
348 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
349 break;
350 case PixelFormat32bppRGB:
351 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
352 break;
353 case PixelFormat32bppARGB:
354 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
355 break;
356 case PixelFormat32bppPARGB:
357 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
358 break;
359 case PixelFormat48bppRGB:
360 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
361 break;
362 case PixelFormat64bppARGB:
363 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
364 break;
365 case PixelFormat64bppPARGB:
366 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
367 break;
368 default:
369 FIXME("not implemented for format 0x%x\n", bitmap->format);
370 return NotImplemented;
373 if (bitmap->format & PixelFormatIndexed)
374 *color = bitmap->image.palette->Entries[index];
375 else
376 *color = a<<24|r<<16|g<<8|b;
378 return Ok;
381 static unsigned int absdiff(unsigned int x, unsigned int y)
383 return x > y ? x - y : y - x;
386 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
388 BYTE index = 0;
389 int best_distance = 0x7fff;
390 int distance;
391 UINT i;
393 if (!palette) return 0;
394 /* This algorithm scans entire palette,
395 computes difference from desired color (all color components have equal weight)
396 and returns the index of color with least difference.
398 Note: Maybe it could be replaced with a better algorithm for better image quality
399 and performance, though better algorithm would probably need some pre-built lookup
400 tables and thus may actually be slower if this method is called only few times per
401 every image.
403 for(i=0;i<palette->Count;i++) {
404 ARGB color=palette->Entries[i];
405 distance=absdiff(b, color & 0xff) + absdiff(g, color>>8 & 0xff) + absdiff(r, color>>16 & 0xff) + absdiff(a, color>>24 & 0xff);
406 if (distance<best_distance) {
407 best_distance=distance;
408 index=i;
411 return index;
414 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
415 BYTE *row, UINT x, ColorPalette *palette)
417 BYTE index = get_palette_index(r,g,b,a,palette);
418 row[x]=index;
421 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
422 BYTE *row, UINT x, ColorPalette *palette)
424 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
427 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
428 BYTE *row, UINT x, ColorPalette *palette)
430 if (x & 1)
431 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
432 else
433 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
436 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
437 BYTE *row, UINT x)
439 *((WORD*)(row)+x) = (r+g+b)*85;
442 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
443 BYTE *row, UINT x)
445 *((WORD*)(row)+x) = (r<<7&0x7c00)|
446 (g<<2&0x03e0)|
447 (b>>3&0x001f);
450 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
451 BYTE *row, UINT x)
453 *((WORD*)(row)+x) = (r<<8&0xf800)|
454 (g<<3&0x07e0)|
455 (b>>3&0x001f);
458 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
459 BYTE *row, UINT x)
461 *((WORD*)(row)+x) = (a<<8&0x8000)|
462 (r<<7&0x7c00)|
463 (g<<2&0x03e0)|
464 (b>>3&0x001f);
467 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
468 BYTE *row, UINT x)
470 row[x*3+2] = r;
471 row[x*3+1] = g;
472 row[x*3] = b;
475 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
476 BYTE *row, UINT x)
478 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
481 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
482 BYTE *row, UINT x)
484 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
487 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
488 BYTE *row, UINT x)
490 r = (r * a + 127) / 255;
491 g = (g * a + 127) / 255;
492 b = (b * a + 127) / 255;
493 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
496 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
497 BYTE *row, UINT x)
499 row[x*6+5] = row[x*6+4] = r;
500 row[x*6+3] = row[x*6+2] = g;
501 row[x*6+1] = row[x*6] = b;
504 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
505 BYTE *row, UINT x)
507 UINT64 a64=a, r64=r, g64=g, b64=b;
508 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
511 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
512 BYTE *row, UINT x)
514 UINT64 a64, r64, g64, b64;
515 a64 = a * 257;
516 r64 = r * a / 255;
517 g64 = g * a / 255;
518 b64 = b * a / 255;
519 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
522 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
523 ARGB color)
525 BYTE a, r, g, b;
526 BYTE *row;
528 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
529 return InvalidParameter;
531 a = color>>24;
532 r = color>>16;
533 g = color>>8;
534 b = color;
536 row = bitmap->bits + bitmap->stride * y;
538 switch (bitmap->format)
540 case PixelFormat16bppGrayScale:
541 setpixel_16bppGrayScale(r,g,b,a,row,x);
542 break;
543 case PixelFormat16bppRGB555:
544 setpixel_16bppRGB555(r,g,b,a,row,x);
545 break;
546 case PixelFormat16bppRGB565:
547 setpixel_16bppRGB565(r,g,b,a,row,x);
548 break;
549 case PixelFormat16bppARGB1555:
550 setpixel_16bppARGB1555(r,g,b,a,row,x);
551 break;
552 case PixelFormat24bppRGB:
553 setpixel_24bppRGB(r,g,b,a,row,x);
554 break;
555 case PixelFormat32bppRGB:
556 setpixel_32bppRGB(r,g,b,a,row,x);
557 break;
558 case PixelFormat32bppARGB:
559 setpixel_32bppARGB(r,g,b,a,row,x);
560 break;
561 case PixelFormat32bppPARGB:
562 setpixel_32bppPARGB(r,g,b,a,row,x);
563 break;
564 case PixelFormat48bppRGB:
565 setpixel_48bppRGB(r,g,b,a,row,x);
566 break;
567 case PixelFormat64bppARGB:
568 setpixel_64bppARGB(r,g,b,a,row,x);
569 break;
570 case PixelFormat64bppPARGB:
571 setpixel_64bppPARGB(r,g,b,a,row,x);
572 break;
573 case PixelFormat8bppIndexed:
574 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
575 break;
576 case PixelFormat4bppIndexed:
577 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
578 break;
579 case PixelFormat1bppIndexed:
580 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
581 break;
582 default:
583 FIXME("not implemented for format 0x%x\n", bitmap->format);
584 return NotImplemented;
587 return Ok;
590 GpStatus convert_pixels(INT width, INT height,
591 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
592 ColorPalette *dst_palette,
593 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
594 ColorPalette *src_palette)
596 INT x, y;
598 if (src_format == dst_format ||
599 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
601 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
602 for (y=0; y<height; y++)
603 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
604 return Ok;
607 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
608 for (y=0; y<height; y++) \
609 for (x=0; x<width; x++) { \
610 BYTE index; \
611 ARGB argb; \
612 BYTE *color = (BYTE *)&argb; \
613 getpixel_function(&index, src_bits+src_stride*y, x); \
614 argb = (src_palette && index < src_palette->Count) ? src_palette->Entries[index] : 0; \
615 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
617 return Ok; \
618 } while (0);
620 #define convert_indexed_to_indexed(getpixel_function, setpixel_function) do { \
621 for (y=0; y<height; y++) \
622 for (x=0; x<width; x++) { \
623 BYTE index; \
624 ARGB argb; \
625 BYTE *color = (BYTE *)&argb; \
626 getpixel_function(&index, src_bits+src_stride*y, x); \
627 argb = (src_palette && index < src_palette->Count) ? src_palette->Entries[index] : 0; \
628 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x, dst_palette); \
630 return Ok; \
631 } while (0);
633 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
634 for (y=0; y<height; y++) \
635 for (x=0; x<width; x++) { \
636 BYTE r, g, b, a; \
637 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
638 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
640 return Ok; \
641 } while (0);
643 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
644 for (y=0; y<height; y++) \
645 for (x=0; x<width; x++) { \
646 BYTE r, g, b, a; \
647 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
648 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, dst_palette); \
650 return Ok; \
651 } while (0);
653 switch (src_format)
655 case PixelFormat1bppIndexed:
656 switch (dst_format)
658 case PixelFormat4bppIndexed:
659 convert_indexed_to_indexed(getpixel_1bppIndexed, setpixel_4bppIndexed);
660 case PixelFormat8bppIndexed:
661 convert_indexed_to_indexed(getpixel_1bppIndexed, setpixel_8bppIndexed);
662 case PixelFormat16bppGrayScale:
663 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
664 case PixelFormat16bppRGB555:
665 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
666 case PixelFormat16bppRGB565:
667 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
668 case PixelFormat16bppARGB1555:
669 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
670 case PixelFormat24bppRGB:
671 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
672 case PixelFormat32bppRGB:
673 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
674 case PixelFormat32bppARGB:
675 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
676 case PixelFormat32bppPARGB:
677 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
678 case PixelFormat48bppRGB:
679 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
680 case PixelFormat64bppARGB:
681 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
682 default:
683 break;
685 break;
686 case PixelFormat4bppIndexed:
687 switch (dst_format)
689 case PixelFormat1bppIndexed:
690 convert_indexed_to_indexed(getpixel_4bppIndexed, setpixel_1bppIndexed);
691 case PixelFormat8bppIndexed:
692 convert_indexed_to_indexed(getpixel_4bppIndexed, setpixel_8bppIndexed);
693 case PixelFormat16bppGrayScale:
694 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
695 case PixelFormat16bppRGB555:
696 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
697 case PixelFormat16bppRGB565:
698 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
699 case PixelFormat16bppARGB1555:
700 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
701 case PixelFormat24bppRGB:
702 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
703 case PixelFormat32bppRGB:
704 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
705 case PixelFormat32bppARGB:
706 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
707 case PixelFormat32bppPARGB:
708 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
709 case PixelFormat48bppRGB:
710 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
711 case PixelFormat64bppARGB:
712 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
713 default:
714 break;
716 break;
717 case PixelFormat8bppIndexed:
718 switch (dst_format)
720 case PixelFormat1bppIndexed:
721 convert_indexed_to_indexed(getpixel_8bppIndexed, setpixel_1bppIndexed);
722 case PixelFormat4bppIndexed:
723 convert_indexed_to_indexed(getpixel_8bppIndexed, setpixel_4bppIndexed);
724 case PixelFormat16bppGrayScale:
725 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
726 case PixelFormat16bppRGB555:
727 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
728 case PixelFormat16bppRGB565:
729 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
730 case PixelFormat16bppARGB1555:
731 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
732 case PixelFormat24bppRGB:
733 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
734 case PixelFormat32bppRGB:
735 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
736 case PixelFormat32bppARGB:
737 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
738 case PixelFormat32bppPARGB:
739 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
740 case PixelFormat48bppRGB:
741 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
742 case PixelFormat64bppARGB:
743 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
744 default:
745 break;
747 break;
748 case PixelFormat16bppGrayScale:
749 switch (dst_format)
751 case PixelFormat1bppIndexed:
752 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
753 case PixelFormat4bppIndexed:
754 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_4bppIndexed);
755 case PixelFormat8bppIndexed:
756 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
757 case PixelFormat16bppRGB555:
758 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
759 case PixelFormat16bppRGB565:
760 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
761 case PixelFormat16bppARGB1555:
762 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
763 case PixelFormat24bppRGB:
764 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
765 case PixelFormat32bppRGB:
766 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
767 case PixelFormat32bppARGB:
768 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
769 case PixelFormat32bppPARGB:
770 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
771 case PixelFormat48bppRGB:
772 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
773 case PixelFormat64bppARGB:
774 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
775 default:
776 break;
778 break;
779 case PixelFormat16bppRGB555:
780 switch (dst_format)
782 case PixelFormat1bppIndexed:
783 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
784 case PixelFormat4bppIndexed:
785 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_4bppIndexed);
786 case PixelFormat8bppIndexed:
787 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
788 case PixelFormat16bppGrayScale:
789 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
790 case PixelFormat16bppRGB565:
791 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
792 case PixelFormat16bppARGB1555:
793 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
794 case PixelFormat24bppRGB:
795 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
796 case PixelFormat32bppRGB:
797 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
798 case PixelFormat32bppARGB:
799 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
800 case PixelFormat32bppPARGB:
801 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
802 case PixelFormat48bppRGB:
803 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
804 case PixelFormat64bppARGB:
805 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
806 default:
807 break;
809 break;
810 case PixelFormat16bppRGB565:
811 switch (dst_format)
813 case PixelFormat1bppIndexed:
814 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
815 case PixelFormat4bppIndexed:
816 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_4bppIndexed);
817 case PixelFormat8bppIndexed:
818 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
819 case PixelFormat16bppGrayScale:
820 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
821 case PixelFormat16bppRGB555:
822 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
823 case PixelFormat16bppARGB1555:
824 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
825 case PixelFormat24bppRGB:
826 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
827 case PixelFormat32bppRGB:
828 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
829 case PixelFormat32bppARGB:
830 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
831 case PixelFormat32bppPARGB:
832 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
833 case PixelFormat48bppRGB:
834 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
835 case PixelFormat64bppARGB:
836 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
837 default:
838 break;
840 break;
841 case PixelFormat16bppARGB1555:
842 switch (dst_format)
844 case PixelFormat1bppIndexed:
845 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
846 case PixelFormat4bppIndexed:
847 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_4bppIndexed);
848 case PixelFormat8bppIndexed:
849 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
850 case PixelFormat16bppGrayScale:
851 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
852 case PixelFormat16bppRGB555:
853 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
854 case PixelFormat16bppRGB565:
855 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
856 case PixelFormat24bppRGB:
857 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
858 case PixelFormat32bppRGB:
859 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
860 case PixelFormat32bppARGB:
861 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
862 case PixelFormat32bppPARGB:
863 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
864 case PixelFormat48bppRGB:
865 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
866 case PixelFormat64bppARGB:
867 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
868 default:
869 break;
871 break;
872 case PixelFormat24bppRGB:
873 switch (dst_format)
875 case PixelFormat1bppIndexed:
876 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
877 case PixelFormat4bppIndexed:
878 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_4bppIndexed);
879 case PixelFormat8bppIndexed:
880 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
881 case PixelFormat16bppGrayScale:
882 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
883 case PixelFormat16bppRGB555:
884 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
885 case PixelFormat16bppRGB565:
886 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
887 case PixelFormat16bppARGB1555:
888 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
889 case PixelFormat32bppRGB:
890 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
891 case PixelFormat32bppARGB:
892 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
893 case PixelFormat32bppPARGB:
894 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
895 case PixelFormat48bppRGB:
896 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
897 case PixelFormat64bppARGB:
898 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
899 default:
900 break;
902 break;
903 case PixelFormat32bppRGB:
904 switch (dst_format)
906 case PixelFormat1bppIndexed:
907 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
908 case PixelFormat4bppIndexed:
909 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_4bppIndexed);
910 case PixelFormat8bppIndexed:
911 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
912 case PixelFormat16bppGrayScale:
913 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
914 case PixelFormat16bppRGB555:
915 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
916 case PixelFormat16bppRGB565:
917 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
918 case PixelFormat16bppARGB1555:
919 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
920 case PixelFormat24bppRGB:
921 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
922 case PixelFormat32bppARGB:
923 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
924 case PixelFormat32bppPARGB:
925 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
926 case PixelFormat48bppRGB:
927 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
928 case PixelFormat64bppARGB:
929 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
930 default:
931 break;
933 break;
934 case PixelFormat32bppARGB:
935 switch (dst_format)
937 case PixelFormat1bppIndexed:
938 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
939 case PixelFormat4bppIndexed:
940 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_4bppIndexed);
941 case PixelFormat8bppIndexed:
942 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
943 case PixelFormat16bppGrayScale:
944 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
945 case PixelFormat16bppRGB555:
946 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
947 case PixelFormat16bppRGB565:
948 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
949 case PixelFormat16bppARGB1555:
950 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
951 case PixelFormat24bppRGB:
952 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
953 case PixelFormat32bppPARGB:
954 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
955 return Ok;
956 case PixelFormat48bppRGB:
957 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
958 case PixelFormat64bppARGB:
959 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
960 default:
961 break;
963 break;
964 case PixelFormat32bppPARGB:
965 switch (dst_format)
967 case PixelFormat1bppIndexed:
968 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
969 case PixelFormat4bppIndexed:
970 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_4bppIndexed);
971 case PixelFormat8bppIndexed:
972 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
973 case PixelFormat16bppGrayScale:
974 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
975 case PixelFormat16bppRGB555:
976 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
977 case PixelFormat16bppRGB565:
978 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
979 case PixelFormat16bppARGB1555:
980 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
981 case PixelFormat24bppRGB:
982 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
983 case PixelFormat32bppRGB:
984 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
985 case PixelFormat32bppARGB:
986 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
987 case PixelFormat48bppRGB:
988 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
989 case PixelFormat64bppARGB:
990 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
991 default:
992 break;
994 break;
995 case PixelFormat48bppRGB:
996 switch (dst_format)
998 case PixelFormat1bppIndexed:
999 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
1000 case PixelFormat4bppIndexed:
1001 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_4bppIndexed);
1002 case PixelFormat8bppIndexed:
1003 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
1004 case PixelFormat16bppGrayScale:
1005 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
1006 case PixelFormat16bppRGB555:
1007 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
1008 case PixelFormat16bppRGB565:
1009 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
1010 case PixelFormat16bppARGB1555:
1011 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
1012 case PixelFormat24bppRGB:
1013 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
1014 case PixelFormat32bppRGB:
1015 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
1016 case PixelFormat32bppARGB:
1017 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
1018 case PixelFormat32bppPARGB:
1019 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
1020 case PixelFormat64bppARGB:
1021 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
1022 default:
1023 break;
1025 break;
1026 case PixelFormat64bppARGB:
1027 switch (dst_format)
1029 case PixelFormat1bppIndexed:
1030 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
1031 case PixelFormat4bppIndexed:
1032 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_4bppIndexed);
1033 case PixelFormat8bppIndexed:
1034 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
1035 case PixelFormat16bppGrayScale:
1036 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
1037 case PixelFormat16bppRGB555:
1038 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
1039 case PixelFormat16bppRGB565:
1040 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
1041 case PixelFormat16bppARGB1555:
1042 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
1043 case PixelFormat24bppRGB:
1044 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
1045 case PixelFormat32bppRGB:
1046 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
1047 case PixelFormat32bppARGB:
1048 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
1049 case PixelFormat32bppPARGB:
1050 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
1051 case PixelFormat48bppRGB:
1052 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
1053 default:
1054 break;
1056 break;
1057 case PixelFormat64bppPARGB:
1058 switch (dst_format)
1060 case PixelFormat1bppIndexed:
1061 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
1062 case PixelFormat4bppIndexed:
1063 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_4bppIndexed);
1064 case PixelFormat8bppIndexed:
1065 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
1066 case PixelFormat16bppGrayScale:
1067 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
1068 case PixelFormat16bppRGB555:
1069 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
1070 case PixelFormat16bppRGB565:
1071 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
1072 case PixelFormat16bppARGB1555:
1073 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
1074 case PixelFormat24bppRGB:
1075 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
1076 case PixelFormat32bppRGB:
1077 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
1078 case PixelFormat32bppARGB:
1079 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
1080 case PixelFormat32bppPARGB:
1081 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1082 case PixelFormat48bppRGB:
1083 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1084 case PixelFormat64bppARGB:
1085 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1086 default:
1087 break;
1089 break;
1090 default:
1091 break;
1094 #undef convert_indexed_to_rgb
1095 #undef convert_indexed_to_indexed
1096 #undef convert_rgb_to_rgb
1097 #undef convert_rgb_to_indexed
1099 return NotImplemented;
1102 /* This function returns a pointer to an array of pixels that represents the
1103 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1104 * flags. It is correct behavior that a user who calls this function with write
1105 * privileges can write to the whole bitmap (not just the area in rect).
1107 * FIXME: only used portion of format is bits per pixel. */
1108 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1109 UINT flags, PixelFormat format, BitmapData* lockeddata)
1111 INT bitspp = PIXELFORMATBPP(format);
1112 GpRect act_rect; /* actual rect to be used */
1113 GpStatus stat;
1115 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1117 if(!lockeddata || !bitmap)
1118 return InvalidParameter;
1119 if(!image_lock(&bitmap->image))
1120 return ObjectBusy;
1122 if(rect){
1123 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1124 (rect->Y + rect->Height > bitmap->height) || !flags)
1126 image_unlock(&bitmap->image);
1127 return InvalidParameter;
1130 act_rect = *rect;
1132 else{
1133 act_rect.X = act_rect.Y = 0;
1134 act_rect.Width = bitmap->width;
1135 act_rect.Height = bitmap->height;
1138 if(bitmap->lockmode)
1140 WARN("bitmap is already locked and cannot be locked again\n");
1141 image_unlock(&bitmap->image);
1142 return WrongState;
1145 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1147 /* no conversion is necessary; just use the bits directly */
1148 lockeddata->Width = act_rect.Width;
1149 lockeddata->Height = act_rect.Height;
1150 lockeddata->PixelFormat = format;
1151 lockeddata->Reserved = flags;
1152 lockeddata->Stride = bitmap->stride;
1153 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1154 bitmap->stride * act_rect.Y;
1156 bitmap->lockmode = flags | ImageLockModeRead;
1158 image_unlock(&bitmap->image);
1159 return Ok;
1162 /* Make sure we can convert to the requested format. */
1163 if (flags & ImageLockModeRead)
1165 stat = convert_pixels(0, 0, 0, NULL, format, NULL, 0, NULL, bitmap->format, NULL);
1166 if (stat == NotImplemented)
1168 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1169 image_unlock(&bitmap->image);
1170 return NotImplemented;
1174 /* If we're opening for writing, make sure we'll be able to write back in
1175 * the original format. */
1176 if (flags & ImageLockModeWrite)
1178 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, NULL, 0, NULL, format, NULL);
1179 if (stat == NotImplemented)
1181 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1182 image_unlock(&bitmap->image);
1183 return NotImplemented;
1187 lockeddata->Width = act_rect.Width;
1188 lockeddata->Height = act_rect.Height;
1189 lockeddata->PixelFormat = format;
1190 lockeddata->Reserved = flags;
1192 if(!(flags & ImageLockModeUserInputBuf))
1194 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1196 bitmap->bitmapbits = heap_alloc_zero(lockeddata->Stride * act_rect.Height);
1198 if (!bitmap->bitmapbits)
1200 image_unlock(&bitmap->image);
1201 return OutOfMemory;
1204 lockeddata->Scan0 = bitmap->bitmapbits;
1207 if (flags & ImageLockModeRead)
1209 static BOOL fixme = FALSE;
1211 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1213 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1214 fixme = TRUE;
1217 stat = convert_pixels(act_rect.Width, act_rect.Height,
1218 lockeddata->Stride, lockeddata->Scan0, format, bitmap->image.palette,
1219 bitmap->stride,
1220 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1221 bitmap->format, bitmap->image.palette);
1223 if (stat != Ok)
1225 heap_free(bitmap->bitmapbits);
1226 bitmap->bitmapbits = NULL;
1227 image_unlock(&bitmap->image);
1228 return stat;
1232 bitmap->lockmode = flags | ImageLockModeRead;
1233 bitmap->lockx = act_rect.X;
1234 bitmap->locky = act_rect.Y;
1236 image_unlock(&bitmap->image);
1237 return Ok;
1240 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1242 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1244 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1245 return InvalidParameter;
1247 bitmap->image.xres = xdpi;
1248 bitmap->image.yres = ydpi;
1250 return Ok;
1253 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1254 BitmapData* lockeddata)
1256 GpStatus stat;
1257 static BOOL fixme = FALSE;
1259 TRACE("(%p,%p)\n", bitmap, lockeddata);
1261 if(!bitmap || !lockeddata)
1262 return InvalidParameter;
1263 if(!image_lock(&bitmap->image))
1264 return ObjectBusy;
1266 if(!bitmap->lockmode)
1268 image_unlock(&bitmap->image);
1269 return WrongState;
1272 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1273 bitmap->lockmode = 0;
1274 heap_free(bitmap->bitmapbits);
1275 bitmap->bitmapbits = NULL;
1276 image_unlock(&bitmap->image);
1277 return Ok;
1280 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1282 /* we passed a direct reference; no need to do anything */
1283 bitmap->lockmode = 0;
1284 image_unlock(&bitmap->image);
1285 return Ok;
1288 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1290 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1291 fixme = TRUE;
1294 /* FIXME: Pass src_palette generated from lockeddata->PixelFormat. */
1295 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1296 bitmap->stride,
1297 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1298 bitmap->format, bitmap->image.palette,
1299 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1301 if (stat != Ok)
1303 ERR("failed to convert pixels; this should never happen\n");
1306 heap_free(bitmap->bitmapbits);
1307 bitmap->bitmapbits = NULL;
1308 bitmap->lockmode = 0;
1310 image_unlock(&bitmap->image);
1311 return stat;
1314 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1315 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1317 Rect area;
1318 GpStatus stat;
1320 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1322 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1323 x < 0 || y < 0 ||
1324 x + width > srcBitmap->width || y + height > srcBitmap->height)
1326 TRACE("<-- InvalidParameter\n");
1327 return InvalidParameter;
1330 if (format == PixelFormatDontCare)
1331 format = srcBitmap->format;
1333 area.X = gdip_round(x);
1334 area.Y = gdip_round(y);
1335 area.Width = gdip_round(width);
1336 area.Height = gdip_round(height);
1338 stat = GdipCreateBitmapFromScan0(area.Width, area.Height, 0, format, NULL, dstBitmap);
1339 if (stat == Ok)
1341 stat = convert_pixels(area.Width, area.Height, (*dstBitmap)->stride, (*dstBitmap)->bits, (*dstBitmap)->format,
1342 (*dstBitmap)->image.palette, srcBitmap->stride,
1343 srcBitmap->bits + srcBitmap->stride * area.Y + PIXELFORMATBPP(srcBitmap->format) * area.X / 8,
1344 srcBitmap->format, srcBitmap->image.palette);
1346 if (stat == Ok && srcBitmap->image.palette)
1348 ColorPalette *src_palette, *dst_palette;
1350 src_palette = srcBitmap->image.palette;
1352 dst_palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * src_palette->Count);
1354 if (dst_palette)
1356 dst_palette->Flags = src_palette->Flags;
1357 dst_palette->Count = src_palette->Count;
1358 memcpy(dst_palette->Entries, src_palette->Entries, sizeof(ARGB) * src_palette->Count);
1360 heap_free((*dstBitmap)->image.palette);
1361 (*dstBitmap)->image.palette = dst_palette;
1363 else
1364 stat = OutOfMemory;
1367 if (stat != Ok)
1368 GdipDisposeImage(&(*dstBitmap)->image);
1371 if (stat != Ok)
1372 *dstBitmap = NULL;
1374 return stat;
1377 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1378 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1380 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1382 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1385 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1387 TRACE("%p, %p\n", image, cloneImage);
1389 if (!image || !cloneImage)
1390 return InvalidParameter;
1392 if (image->type == ImageTypeBitmap)
1394 GpBitmap *bitmap = (GpBitmap *)image;
1396 return GdipCloneBitmapAreaI(0, 0, bitmap->width, bitmap->height,
1397 bitmap->format, bitmap, (GpBitmap **)cloneImage);
1399 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
1401 GpMetafile *result, *metafile;
1403 metafile = (GpMetafile*)image;
1405 result = heap_alloc_zero(sizeof(*result));
1406 if (!result)
1407 return OutOfMemory;
1409 result->image.type = ImageTypeMetafile;
1410 result->image.format = image->format;
1411 result->image.flags = image->flags;
1412 result->image.frame_count = 1;
1413 result->image.xres = image->xres;
1414 result->image.yres = image->yres;
1415 result->bounds = metafile->bounds;
1416 result->unit = metafile->unit;
1417 result->metafile_type = metafile->metafile_type;
1418 result->hemf = CopyEnhMetaFileW(metafile->hemf, NULL);
1419 list_init(&result->containers);
1421 if (!result->hemf)
1423 heap_free(result);
1424 return OutOfMemory;
1427 *cloneImage = &result->image;
1428 return Ok;
1430 else
1432 WARN("GpImage with no image data (metafile in wrong state?)\n");
1433 return InvalidParameter;
1437 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1438 GpBitmap **bitmap)
1440 GpStatus stat;
1441 IStream *stream;
1443 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1445 if(!filename || !bitmap)
1446 return InvalidParameter;
1448 *bitmap = NULL;
1450 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1452 if(stat != Ok)
1453 return stat;
1455 stat = GdipCreateBitmapFromStream(stream, bitmap);
1457 IStream_Release(stream);
1459 return stat;
1462 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1463 VOID *bits, GpBitmap **bitmap)
1465 DWORD height, stride;
1466 HBITMAP hbm;
1467 void *bmbits;
1468 GpStatus status;
1470 TRACE("(%p, %p, %p)\n", info, bits, bitmap);
1472 if (!info || !bits || !bitmap)
1473 return InvalidParameter;
1475 hbm = CreateDIBSection(0, info, DIB_RGB_COLORS, &bmbits, NULL, 0);
1476 if (!hbm)
1477 return InvalidParameter;
1479 height = abs(info->bmiHeader.biHeight);
1480 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1481 TRACE("height %lu, stride %lu, image size %lu\n", height, stride, height * stride);
1483 memcpy(bmbits, bits, height * stride);
1485 status = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1486 DeleteObject(hbm);
1488 return status;
1491 /* FIXME: no icm */
1492 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1493 GpBitmap **bitmap)
1495 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1497 return GdipCreateBitmapFromFile(filename, bitmap);
1500 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1501 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1503 HBITMAP hbm;
1504 GpStatus stat = InvalidParameter;
1506 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1508 if(!lpBitmapName || !bitmap)
1509 return InvalidParameter;
1511 /* load DIB */
1512 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1513 LR_CREATEDIBSECTION);
1515 if(hbm){
1516 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1517 DeleteObject(hbm);
1520 return stat;
1523 static inline DWORD blend_argb_no_bkgnd_alpha(DWORD src, DWORD bkgnd)
1525 BYTE b = (BYTE)src;
1526 BYTE g = (BYTE)(src >> 8);
1527 BYTE r = (BYTE)(src >> 16);
1528 DWORD alpha = (BYTE)(src >> 24);
1529 return ((b + ((BYTE)bkgnd * (255 - alpha) + 127) / 255) |
1530 (g + ((BYTE)(bkgnd >> 8) * (255 - alpha) + 127) / 255) << 8 |
1531 (r + ((BYTE)(bkgnd >> 16) * (255 - alpha) + 127) / 255) << 16 |
1532 (alpha << 24));
1535 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1536 HBITMAP* hbmReturn, ARGB background)
1538 GpStatus stat;
1539 HBITMAP result;
1540 UINT width, height;
1541 BITMAPINFOHEADER bih;
1542 LPBYTE bits;
1544 TRACE("(%p,%p,%lx)\n", bitmap, hbmReturn, background);
1546 if (!bitmap || !hbmReturn) return InvalidParameter;
1547 if (!image_lock(&bitmap->image)) return ObjectBusy;
1549 GdipGetImageWidth(&bitmap->image, &width);
1550 GdipGetImageHeight(&bitmap->image, &height);
1552 bih.biSize = sizeof(bih);
1553 bih.biWidth = width;
1554 bih.biHeight = height;
1555 bih.biPlanes = 1;
1556 bih.biBitCount = 32;
1557 bih.biCompression = BI_RGB;
1558 bih.biSizeImage = 0;
1559 bih.biXPelsPerMeter = 0;
1560 bih.biYPelsPerMeter = 0;
1561 bih.biClrUsed = 0;
1562 bih.biClrImportant = 0;
1564 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1565 if (!result)
1567 image_unlock(&bitmap->image);
1568 return GenericError;
1571 stat = convert_pixels(width, height, -width*4,
1572 bits + (width * 4 * (height - 1)), PixelFormat32bppPARGB, bitmap->image.palette,
1573 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette);
1574 if (stat != Ok)
1576 DeleteObject(result);
1577 image_unlock(&bitmap->image);
1578 return stat;
1581 if (background & 0xffffff)
1583 DWORD *ptr;
1584 UINT i;
1585 for (ptr = (DWORD*)bits, i = 0; i < width * height; ptr++, i++)
1587 if ((*ptr & 0xff000000) == 0xff000000) continue;
1588 *ptr = blend_argb_no_bkgnd_alpha(*ptr, background);
1592 *hbmReturn = result;
1593 image_unlock(&bitmap->image);
1594 return Ok;
1597 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1598 GpGraphics* target, GpBitmap** bitmap)
1600 GpStatus ret;
1602 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1604 if(!target || !bitmap)
1605 return InvalidParameter;
1607 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
1608 NULL, bitmap);
1610 if (ret == Ok)
1612 GdipGetDpiX(target, &(*bitmap)->image.xres);
1613 GdipGetDpiY(target, &(*bitmap)->image.yres);
1616 return ret;
1619 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1621 GpStatus stat;
1622 ICONINFO iinfo;
1623 BITMAP bm;
1624 int ret;
1625 UINT width, height, stride;
1626 GpRect rect;
1627 BitmapData lockeddata;
1628 HDC screendc;
1629 int x, y;
1630 BITMAPINFOHEADER bih;
1631 DWORD *src;
1632 BYTE *dst_row;
1633 DWORD *dst;
1634 BYTE *bits;
1635 int mask_scanlines = 0, color_scanlines = 0;
1637 TRACE("%p, %p\n", hicon, bitmap);
1639 if(!bitmap || !GetIconInfo(hicon, &iinfo) || !iinfo.hbmColor || !iinfo.fIcon)
1640 return InvalidParameter;
1642 /* get the size of the icon */
1643 ret = GetObjectA(iinfo.hbmColor, sizeof(bm), &bm);
1644 if (ret == 0) {
1645 DeleteObject(iinfo.hbmColor);
1646 DeleteObject(iinfo.hbmMask);
1647 return GenericError;
1650 width = bm.bmWidth;
1651 height = abs(bm.bmHeight);
1652 stride = width * 4;
1654 stat = GdipCreateBitmapFromScan0(width, height, stride, PixelFormat32bppARGB, NULL, bitmap);
1655 if (stat != Ok) {
1656 DeleteObject(iinfo.hbmColor);
1657 DeleteObject(iinfo.hbmMask);
1658 return stat;
1661 rect.X = 0;
1662 rect.Y = 0;
1663 rect.Width = width;
1664 rect.Height = height;
1666 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1667 if (stat != Ok) {
1668 DeleteObject(iinfo.hbmColor);
1669 DeleteObject(iinfo.hbmMask);
1670 GdipDisposeImage(&(*bitmap)->image);
1671 return stat;
1674 bih.biSize = sizeof(bih);
1675 bih.biWidth = width;
1676 bih.biHeight = -height;
1677 bih.biPlanes = 1;
1678 bih.biBitCount = 32;
1679 bih.biCompression = BI_RGB;
1680 bih.biSizeImage = 0;
1681 bih.biXPelsPerMeter = 0;
1682 bih.biYPelsPerMeter = 0;
1683 bih.biClrUsed = 0;
1684 bih.biClrImportant = 0;
1686 bits = heap_alloc(height * stride);
1687 if (!bits)
1689 DeleteObject(iinfo.hbmColor);
1690 DeleteObject(iinfo.hbmMask);
1691 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1692 GdipDisposeImage(&(*bitmap)->image);
1693 return OutOfMemory;
1696 screendc = CreateCompatibleDC(0);
1697 if (screendc)
1699 color_scanlines = GetDIBits(screendc, iinfo.hbmColor, 0, height, lockeddata.Scan0,
1700 (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1701 mask_scanlines = GetDIBits(screendc, iinfo.hbmMask, 0, height, bits,
1702 (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1703 DeleteDC(screendc);
1706 DeleteObject(iinfo.hbmColor);
1707 DeleteObject(iinfo.hbmMask);
1709 if (!screendc || ((color_scanlines == 0 || mask_scanlines == 0) &&
1710 GetLastError() == ERROR_INVALID_PARAMETER))
1712 heap_free(bits);
1713 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1714 GdipDisposeImage(&(*bitmap)->image);
1715 return GenericError;
1718 src = (DWORD*)bits;
1719 dst_row = lockeddata.Scan0;
1720 for (y=0; y<height; y++)
1722 dst = (DWORD*)dst_row;
1723 for (x=0; x<width; x++)
1725 DWORD src_value = *src++;
1726 if (src_value)
1727 *dst++ = 0;
1728 else
1729 *dst++ |= 0xff000000;
1731 dst_row += lockeddata.Stride;
1734 heap_free(bits);
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)
2770 *count = 0;
2771 *size = 0;
2772 return Ok;
2775 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2776 if (FAILED(hr)) return hresult_to_status(hr);
2778 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2779 if (FAILED(hr)) return hresult_to_status(hr);
2781 IWICEnumMetadataItem_Reset(enumerator);
2783 prop_size = 0;
2785 PropVariantInit(&id);
2786 PropVariantInit(&value);
2788 for (i = 0; i < prop_count; i++)
2790 ULONG items_returned;
2791 UINT item_size;
2793 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2794 if (hr != S_OK) break;
2796 item_size = propvariant_size(&value);
2797 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2799 PropVariantClear(&id);
2800 PropVariantClear(&value);
2803 IWICEnumMetadataItem_Release(enumerator);
2805 if (hr != S_OK) return PropertyNotFound;
2807 *count = prop_count;
2808 *size = prop_size;
2809 return Ok;
2812 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2813 UINT count, PropertyItem *buf)
2815 GpStatus status;
2816 HRESULT hr;
2817 IWICMetadataReader *reader;
2818 IWICEnumMetadataItem *enumerator;
2819 UINT prop_count, prop_size, i;
2820 PROPVARIANT id, value;
2821 char *item_value;
2823 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2825 if (!image || !buf) return InvalidParameter;
2827 if (image->type != ImageTypeBitmap)
2829 FIXME("Not implemented for type %d\n", image->type);
2830 return NotImplemented;
2833 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2834 if (status != Ok) return status;
2836 if (prop_count != count || prop_size != size) return InvalidParameter;
2838 if (((GpBitmap *)image)->prop_item)
2840 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2842 item_value = (char *)(buf + prop_count);
2844 for (i = 0; i < prop_count; i++)
2846 buf[i].value = item_value;
2847 item_value += buf[i].length;
2850 return Ok;
2853 reader = ((GpBitmap *)image)->metadata_reader;
2854 if (!reader) return GenericError;
2856 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2857 if (FAILED(hr)) return hresult_to_status(hr);
2859 IWICEnumMetadataItem_Reset(enumerator);
2861 item_value = (char *)(buf + prop_count);
2863 PropVariantInit(&id);
2864 PropVariantInit(&value);
2866 for (i = 0; i < prop_count; i++)
2868 PropertyItem *item;
2869 ULONG items_returned;
2870 UINT item_size;
2872 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2873 if (hr != S_OK) break;
2875 if (id.vt != VT_UI2)
2877 FIXME("not supported propvariant type for id: %u\n", id.vt);
2878 continue;
2881 item_size = propvariant_size(&value);
2882 if (item_size)
2884 item = heap_alloc(item_size + sizeof(*item));
2886 propvariant_to_item(&value, item, item_size + sizeof(*item), id.uiVal);
2887 buf[i].id = item->id;
2888 buf[i].type = item->type;
2889 buf[i].length = item_size;
2890 buf[i].value = item_value;
2891 memcpy(item_value, item->value, item_size);
2892 item_value += item_size;
2894 heap_free(item);
2897 PropVariantClear(&id);
2898 PropVariantClear(&value);
2901 IWICEnumMetadataItem_Release(enumerator);
2903 if (hr != S_OK) return PropertyNotFound;
2905 return Ok;
2908 struct image_format_dimension
2910 const GUID *format;
2911 const GUID *dimension;
2914 static const struct image_format_dimension image_format_dimensions[] =
2916 {&ImageFormatGIF, &FrameDimensionTime},
2917 {&ImageFormatIcon, &FrameDimensionResolution},
2918 {NULL}
2921 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2922 GDIPCONST GUID* dimensionID, UINT* count)
2924 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2926 if(!image || !count)
2927 return InvalidParameter;
2929 if (!dimensionID ||
2930 IsEqualGUID(dimensionID, &image->format) ||
2931 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2932 IsEqualGUID(dimensionID, &FrameDimensionTime))
2934 *count = image->frame_count;
2935 return Ok;
2938 return InvalidParameter;
2941 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2942 UINT* count)
2944 TRACE("(%p, %p)\n", image, count);
2946 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2948 if(!image || !count)
2949 return InvalidParameter;
2951 *count = 1;
2953 return Ok;
2956 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2957 GUID* dimensionIDs, UINT count)
2959 int i;
2960 const GUID *result=NULL;
2962 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2964 if(!image || !dimensionIDs || count != 1)
2965 return InvalidParameter;
2967 for (i=0; image_format_dimensions[i].format; i++)
2969 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2971 result = image_format_dimensions[i].dimension;
2972 break;
2976 if (!result)
2977 result = &FrameDimensionPage;
2979 memcpy(dimensionIDs, result, sizeof(GUID));
2981 return Ok;
2984 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2985 GpImage **image)
2987 GpStatus stat;
2988 IStream *stream;
2990 TRACE("(%s) %p\n", debugstr_w(filename), image);
2992 if (!filename || !image)
2993 return InvalidParameter;
2995 *image = NULL;
2997 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2999 if (stat != Ok)
3000 return stat;
3002 stat = GdipLoadImageFromStream(stream, image);
3004 IStream_Release(stream);
3006 return stat;
3009 /* FIXME: no icm handling */
3010 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
3012 TRACE("(%s) %p\n", debugstr_w(filename), image);
3014 return GdipLoadImageFromFile(filename, image);
3017 static void add_property(GpBitmap *bitmap, PropertyItem *item)
3019 UINT prop_size, prop_count;
3020 PropertyItem *prop_item;
3022 if (bitmap->prop_item == NULL)
3024 prop_size = prop_count = 0;
3025 prop_item = heap_alloc_zero(item->length + sizeof(PropertyItem));
3026 if (!prop_item) return;
3028 else
3030 UINT i;
3031 char *item_value;
3033 GdipGetPropertySize(&bitmap->image, &prop_size, &prop_count);
3035 prop_item = heap_alloc_zero(prop_size + item->length + sizeof(PropertyItem));
3036 if (!prop_item) return;
3037 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
3038 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
3039 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
3041 item_value = (char *)(prop_item + prop_count + 1);
3043 for (i = 0; i < prop_count; i++)
3045 prop_item[i].value = item_value;
3046 item_value += prop_item[i].length;
3050 prop_item[prop_count].id = item->id;
3051 prop_item[prop_count].type = item->type;
3052 prop_item[prop_count].length = item->length;
3053 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3054 memcpy(prop_item[prop_count].value, item->value, item->length);
3056 heap_free(bitmap->prop_item);
3057 bitmap->prop_item = prop_item;
3058 bitmap->prop_count++;
3061 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3063 HRESULT hr;
3064 GUID format;
3065 PROPVARIANT id, value;
3066 BOOL ret = FALSE;
3068 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3069 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return FALSE;
3071 PropVariantInit(&id);
3072 PropVariantInit(&value);
3074 id.vt = VT_LPWSTR;
3075 id.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3076 if (!id.pwszVal) return FALSE;
3077 lstrcpyW(id.pwszVal, prop_name);
3078 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3079 if (hr == S_OK && value.vt == VT_BOOL)
3080 ret = value.boolVal;
3082 PropVariantClear(&id);
3083 PropVariantClear(&value);
3085 return ret;
3088 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3090 HRESULT hr;
3091 GUID format;
3092 PROPVARIANT id, value;
3093 PropertyItem *item = NULL;
3095 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3096 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return NULL;
3098 PropVariantInit(&id);
3099 PropVariantInit(&value);
3101 id.vt = VT_LPWSTR;
3102 id.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3103 if (!id.pwszVal) return NULL;
3104 lstrcpyW(id.pwszVal, prop_name);
3105 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3106 if (hr == S_OK)
3108 UINT item_size = propvariant_size(&value);
3109 if (item_size)
3111 item_size += sizeof(*item);
3112 item = heap_alloc_zero(item_size);
3113 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3115 heap_free(item);
3116 item = NULL;
3121 PropVariantClear(&id);
3122 PropVariantClear(&value);
3124 return item;
3127 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3129 PropertyItem *comment;
3131 comment = get_property(reader, &GUID_MetadataFormatGifComment, L"TextEntry");
3132 if (comment)
3133 comment->id = PropertyTagExifUserComment;
3135 return comment;
3138 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3140 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3142 appext = get_property(reader, &GUID_MetadataFormatAPE, L"Application");
3143 if (appext)
3145 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3146 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3148 appdata = get_property(reader, &GUID_MetadataFormatAPE, L"Data");
3149 if (appdata)
3151 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3153 BYTE *data = appdata->value;
3154 if (data[0] == 3 && data[1] == 1)
3156 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3157 if (loop)
3159 loop->type = PropertyTagTypeShort;
3160 loop->id = PropertyTagLoopCount;
3161 loop->length = sizeof(SHORT);
3162 loop->value = loop + 1;
3163 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3171 heap_free(appext);
3172 heap_free(appdata);
3174 return loop;
3177 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3179 PropertyItem *background = NULL;
3181 if (get_bool_property(reader, &GUID_MetadataFormatLSD, L"GlobalColorTableFlag"))
3183 background = get_property(reader, &GUID_MetadataFormatLSD, L"BackgroundColorIndex");
3184 if (background)
3185 background->id = PropertyTagIndexBackground;
3188 return background;
3191 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3193 HRESULT hr;
3194 IWICImagingFactory *factory;
3195 IWICPalette *palette;
3196 UINT count = 0;
3197 WICColor colors[256];
3199 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, L"GlobalColorTableFlag"))
3200 return NULL;
3202 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3203 if (hr != S_OK) return NULL;
3205 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3206 if (hr == S_OK)
3208 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3209 if (hr == S_OK)
3210 IWICPalette_GetColors(palette, 256, colors, &count);
3212 IWICPalette_Release(palette);
3215 IWICImagingFactory_Release(factory);
3217 if (count)
3219 PropertyItem *pal;
3220 UINT i;
3221 BYTE *rgb;
3223 pal = heap_alloc_zero(sizeof(*pal) + count * 3);
3224 if (!pal) return NULL;
3225 pal->type = PropertyTagTypeByte;
3226 pal->id = PropertyTagGlobalPalette;
3227 pal->value = pal + 1;
3228 pal->length = count * 3;
3230 rgb = pal->value;
3232 for (i = 0; i < count; i++)
3234 rgb[i*3] = (colors[i] >> 16) & 0xff;
3235 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3236 rgb[i*3 + 2] = colors[i] & 0xff;
3239 return pal;
3242 return NULL;
3245 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3247 PropertyItem *index = NULL;
3249 if (get_bool_property(reader, &GUID_MetadataFormatGCE, L"TransparencyFlag"))
3251 index = get_property(reader, &GUID_MetadataFormatGCE, L"TransparentColorIndex");
3252 if (index)
3253 index->id = PropertyTagIndexTransparent;
3255 return index;
3258 static void get_gif_frame_property(IWICBitmapFrameDecode *frame, const GUID *format, const WCHAR *property, LONG *value)
3260 HRESULT hr;
3261 IWICMetadataBlockReader *block_reader;
3262 IWICMetadataReader *reader;
3263 UINT block_count, i;
3264 PropertyItem *prop;
3266 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3267 if (hr == S_OK)
3269 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3270 if (hr == S_OK)
3272 for (i = 0; i < block_count; i++)
3274 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3275 if (hr == S_OK)
3277 prop = get_property(reader, format, property);
3278 if (prop)
3280 if (prop->type == PropertyTagTypeByte && prop->length == 1)
3281 *value = *(BYTE *)prop->value;
3282 else if (prop->type == PropertyTagTypeShort && prop->length == 2)
3283 *value = *(SHORT *)prop->value;
3285 heap_free(prop);
3287 IWICMetadataReader_Release(reader);
3291 IWICMetadataBlockReader_Release(block_reader);
3295 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3297 HRESULT hr;
3298 IWICBitmapFrameDecode *frame;
3299 IWICMetadataBlockReader *block_reader;
3300 IWICMetadataReader *reader;
3301 UINT frame_count, block_count, i;
3302 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3303 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3305 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3306 delay = heap_alloc_zero(sizeof(*delay) + frame_count * sizeof(LONG));
3307 if (delay)
3309 LONG *value;
3310 LONG frame_delay = 0;
3312 delay->type = PropertyTagTypeLong;
3313 delay->id = PropertyTagFrameDelay;
3314 delay->length = frame_count * sizeof(LONG);
3315 delay->value = delay + 1;
3317 value = delay->value;
3319 for (i = 0; i < frame_count; i++)
3321 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3322 if (hr == S_OK)
3324 get_gif_frame_property(frame, &GUID_MetadataFormatGCE, L"Delay", &frame_delay);
3325 IWICBitmapFrameDecode_Release(frame);
3327 value[i] = frame_delay;
3331 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3332 if (hr == S_OK)
3334 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3335 if (hr == S_OK)
3337 for (i = 0; i < block_count; i++)
3339 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3340 if (hr == S_OK)
3342 if (!comment)
3343 comment = get_gif_comment(reader);
3345 if (!loop)
3346 loop = get_gif_loopcount(reader);
3348 if (!background)
3349 background = get_gif_background(reader);
3351 if (!palette)
3352 palette = get_gif_palette(decoder, reader);
3354 IWICMetadataReader_Release(reader);
3358 IWICMetadataBlockReader_Release(block_reader);
3361 if (!loop)
3363 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3364 if (loop)
3366 loop->type = PropertyTagTypeShort;
3367 loop->id = PropertyTagLoopCount;
3368 loop->length = sizeof(SHORT);
3369 loop->value = loop + 1;
3370 *(SHORT *)loop->value = 1;
3374 if (delay) add_property(bitmap, delay);
3375 if (comment) add_property(bitmap, comment);
3376 if (loop) add_property(bitmap, loop);
3377 if (palette) add_property(bitmap, palette);
3378 if (background) add_property(bitmap, background);
3380 heap_free(delay);
3381 heap_free(comment);
3382 heap_free(loop);
3383 heap_free(palette);
3384 heap_free(background);
3386 /* Win7 gdiplus always returns transparent color index from frame 0 */
3387 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3388 if (hr != S_OK) return;
3390 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3391 if (hr == S_OK)
3393 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3394 if (hr == S_OK)
3396 for (i = 0; i < block_count; i++)
3398 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3399 if (hr == S_OK)
3401 if (!transparent_idx)
3402 transparent_idx = get_gif_transparent_idx(reader);
3404 IWICMetadataReader_Release(reader);
3408 IWICMetadataBlockReader_Release(block_reader);
3411 if (transparent_idx) add_property(bitmap, transparent_idx);
3412 heap_free(transparent_idx);
3414 IWICBitmapFrameDecode_Release(frame);
3417 static PropertyItem* create_prop(PROPID propid, PROPVARIANT* value)
3419 PropertyItem *item = NULL;
3420 UINT item_size = propvariant_size(value);
3422 if (item_size)
3424 item_size += sizeof(*item);
3425 item = heap_alloc_zero(item_size);
3426 if (propvariant_to_item(value, item, item_size, propid) != Ok)
3428 heap_free(item);
3429 item = NULL;
3433 return item;
3436 static ULONG get_ulong_by_index(IWICMetadataReader* reader, ULONG index)
3438 PROPVARIANT value;
3439 HRESULT hr;
3440 ULONG result=0;
3442 hr = IWICMetadataReader_GetValueByIndex(reader, index, NULL, NULL, &value);
3443 if (SUCCEEDED(hr))
3445 switch (value.vt)
3447 case VT_UI4:
3448 result = value.ulVal;
3449 break;
3450 default:
3451 ERR("unhandled case %u\n", value.vt);
3452 break;
3454 PropVariantClear(&value);
3456 return result;
3459 static HRESULT png_read_text(IWICMetadataReader *reader, GpBitmap *bitmap, BOOL **seen_text)
3461 HRESULT hr;
3462 UINT i;
3463 PROPVARIANT name, value;
3464 PropertyItem* item;
3465 static const struct keyword_info {
3466 const char* name;
3467 PROPID propid;
3468 } keywords[] = {
3469 { "Title", PropertyTagImageTitle },
3470 { "Author", PropertyTagArtist },
3471 { "Description", PropertyTagImageDescription },
3472 { "Copyright", PropertyTagCopyright },
3473 { "Software", PropertyTagSoftwareUsed },
3474 { "Source", PropertyTagEquipModel },
3475 { "Comment", PropertyTagExifUserComment },
3478 if (*seen_text == NULL)
3479 *seen_text = heap_alloc_zero(sizeof(BOOL) * ARRAY_SIZE(keywords));
3480 if (*seen_text == NULL)
3481 return E_OUTOFMEMORY;
3483 hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, &name, &value);
3484 if (FAILED(hr))
3485 return hr;
3487 if (name.vt == VT_LPSTR)
3489 for (i = 0; i < ARRAY_SIZE(keywords); i++)
3491 if (!strcmp(keywords[i].name, name.pszVal))
3492 break;
3494 if (i < ARRAY_SIZE(keywords) && !(*seen_text)[i])
3496 (*seen_text)[i] = TRUE;
3497 item = create_prop(keywords[i].propid, &value);
3498 if (item)
3499 add_property(bitmap, item);
3500 heap_free(item);
3504 PropVariantClear(&name);
3505 PropVariantClear(&value);
3507 return S_OK;
3510 static HRESULT png_read_gamma(IWICMetadataReader *reader, GpBitmap *bitmap)
3512 PropertyItem* item;
3513 ULONG *rational;
3515 item = heap_alloc_zero(sizeof(*item) + sizeof(ULONG) * 2);
3516 if (!item)
3517 return E_OUTOFMEMORY;
3519 item->length = sizeof(ULONG) * 2;
3520 item->type = PropertyTagTypeRational;
3521 item->id = PropertyTagGamma;
3522 rational = item->value = item + 1;
3523 rational[0] = 100000;
3524 rational[1] = get_ulong_by_index(reader, 0);
3525 add_property(bitmap, item);
3526 heap_free(item);
3528 return S_OK;
3531 static HRESULT png_read_whitepoint(IWICMetadataReader *reader, GpBitmap *bitmap)
3533 PropertyItem* item;
3534 ULONG *rational;
3536 item = heap_alloc_zero(sizeof(*item) + sizeof(ULONG) * 4);
3537 if (!item)
3538 return E_OUTOFMEMORY;
3540 item->length = sizeof(ULONG) * 4;
3541 item->type = PropertyTagTypeRational;
3542 item->id = PropertyTagWhitePoint;
3543 rational = item->value = item + 1;
3544 rational[0] = get_ulong_by_index(reader, 0);
3545 rational[1] = 100000;
3546 rational[2] = get_ulong_by_index(reader, 1);
3547 rational[3] = 100000;
3548 add_property(bitmap, item);
3549 heap_free(item);
3551 return S_OK;
3554 static HRESULT png_read_chromaticity(IWICMetadataReader *reader, GpBitmap *bitmap)
3556 PropertyItem* item;
3557 ULONG *rational;
3559 item = heap_alloc_zero(sizeof(*item) + sizeof(ULONG) * 12);
3560 if (!item)
3561 return E_OUTOFMEMORY;
3563 item->length = sizeof(ULONG) * 12;
3564 item->type = PropertyTagTypeRational;
3565 item->id = PropertyTagPrimaryChromaticities;
3566 rational = item->value = item + 1;
3567 rational[0] = get_ulong_by_index(reader, 2);
3568 rational[1] = 100000;
3569 rational[2] = get_ulong_by_index(reader, 3);
3570 rational[3] = 100000;
3571 rational[4] = get_ulong_by_index(reader, 4);
3572 rational[5] = 100000;
3573 rational[6] = get_ulong_by_index(reader, 5);
3574 rational[7] = 100000;
3575 rational[8] = get_ulong_by_index(reader, 6);
3576 rational[9] = 100000;
3577 rational[10] = get_ulong_by_index(reader, 7);
3578 rational[11] = 100000;
3579 add_property(bitmap, item);
3580 heap_free(item);
3582 return S_OK;
3585 static HRESULT png_read_time(IWICMetadataReader *reader, GpBitmap *bitmap)
3587 HRESULT hr;
3588 UINT item_size, i;
3589 PropertyItem* item;
3590 PROPVARIANT value;
3591 USHORT datetime[6];
3593 for (i = 0; i < 6; i++)
3595 hr = IWICMetadataReader_GetValueByIndex(reader, i, NULL, NULL, &value);
3596 if (FAILED(hr))
3597 return hr;
3598 if (i == 0 && value.vt == VT_UI2)
3599 datetime[0] = value.uiVal;
3600 else if (i > 0 && value.vt == VT_UI1)
3601 datetime[i] = value.bVal;
3602 else
3604 PropVariantClear(&value);
3605 return E_FAIL;
3607 PropVariantClear(&value);
3610 item_size = 20;
3611 item = heap_alloc_zero(sizeof(*item) + item_size);
3612 if (!item)
3613 return E_OUTOFMEMORY;
3615 item->id = PropertyTagDateTime;
3616 item->type = PropertyTagTypeASCII;
3617 item->length = item_size;
3618 item->value = item + 1;
3619 snprintf(item->value, item_size, "%04u:%02u:%02u %02u:%02u:%02u",
3620 datetime[0], datetime[1], datetime[2], datetime[3], datetime[4], datetime[5]);
3622 add_property(bitmap, item);
3623 heap_free(item);
3625 return S_OK;
3628 static HRESULT png_read_histogram(IWICMetadataReader *reader, GpBitmap *bitmap)
3630 HRESULT hr;
3631 PropertyItem* item;
3632 PROPVARIANT value;
3634 hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, NULL, &value);
3635 if (FAILED(hr))
3636 return hr;
3638 item = create_prop(PropertyTagPaletteHistogram, &value);
3639 if (item)
3640 add_property(bitmap, item);
3641 heap_free(item);
3643 PropVariantClear(&value);
3645 return S_OK;
3648 static HRESULT png_add_unit_properties(IWICBitmapFrameDecode *frame, GpBitmap *bitmap)
3650 HRESULT hr;
3651 double dpiX, dpiY;
3652 PropertyItem *unit, *unitX, *unitY;
3654 hr = IWICBitmapFrameDecode_GetResolution(frame, &dpiX, &dpiY);
3655 if (FAILED(hr))
3656 return hr;
3658 unit = heap_alloc_zero(sizeof(*unit) + 1);
3659 unitX = heap_alloc_zero(sizeof(*unitX) + 4);
3660 unitY = heap_alloc_zero(sizeof(*unitY) + 4);
3662 if (!unit || !unitX || !unitY)
3664 heap_free(unit);
3665 heap_free(unitX);
3666 heap_free(unitY);
3667 return E_OUTOFMEMORY;
3670 unit->type = PropertyTagTypeByte;
3671 unit->id = PropertyTagPixelUnit;
3672 unit->length = 1;
3673 unit->value = unit + 1;
3674 *(BYTE *)unit->value = 1;
3675 add_property(bitmap, unit);
3676 heap_free(unit);
3678 unitX->type = PropertyTagTypeLong;
3679 unitX->id = PropertyTagPixelPerUnitX;
3680 unitX->length = 4;
3681 unitX->value = unitX + 1;
3682 *(ULONG *)unitX->value = (dpiX == 96.0) ? 0 : gdip_round(dpiX / 0.0254);
3683 add_property(bitmap, unitX);
3684 heap_free(unitX);
3686 unitY->type = PropertyTagTypeLong;
3687 unitY->id = PropertyTagPixelPerUnitY;
3688 unitY->length = 4;
3689 unitY->value = unitY + 1;
3690 *(ULONG *)unitY->value = (dpiY == 96.0) ? 0 : gdip_round(dpiY / 0.0254);
3691 add_property(bitmap, unitY);
3692 heap_free(unitY);
3694 return S_OK;
3697 static void png_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3699 HRESULT hr;
3700 IWICBitmapFrameDecode *frame;
3701 IWICMetadataBlockReader *block_reader;
3702 UINT block_count, i;
3703 BOOL seen_gamma=FALSE, seen_whitepoint=FALSE, seen_chrm=FALSE, seen_time=FALSE, seen_histogram=FALSE;
3704 BOOL *seen_text = NULL;
3706 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3707 if (hr != S_OK)
3708 return;
3710 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3711 if (hr != S_OK)
3713 IWICBitmapFrameDecode_Release(frame);
3714 return;
3717 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3718 if (hr != S_OK)
3720 IWICMetadataBlockReader_Release(block_reader);
3721 IWICBitmapFrameDecode_Release(frame);
3722 return;
3725 for (i = 0; i < block_count; i++)
3727 IWICMetadataReader *reader;
3728 GUID format;
3730 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3731 if (hr != S_OK)
3732 continue;
3734 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3735 if (FAILED(hr))
3737 IWICMetadataReader_Release(reader);
3738 continue;
3741 if (IsEqualGUID(&GUID_MetadataFormatChunktEXt, &format))
3742 png_read_text(reader, bitmap, &seen_text);
3743 else if (IsEqualGUID(&GUID_MetadataFormatChunkgAMA, &format))
3745 if (!seen_gamma)
3747 hr = png_read_gamma(reader, bitmap);
3748 seen_gamma = SUCCEEDED(hr);
3751 else if (IsEqualGUID(&GUID_MetadataFormatChunkcHRM, &format))
3753 if (!seen_whitepoint)
3755 hr = png_read_whitepoint(reader, bitmap);
3756 seen_whitepoint = SUCCEEDED(hr);
3758 if (!seen_chrm)
3760 hr = png_read_chromaticity(reader, bitmap);
3761 seen_chrm = SUCCEEDED(hr);
3764 else if (IsEqualGUID(&GUID_MetadataFormatChunktIME, &format))
3766 if (!seen_time)
3768 hr = png_read_time(reader, bitmap);
3769 seen_time = SUCCEEDED(hr);
3772 else if (IsEqualGUID(&GUID_MetadataFormatChunkhIST, &format))
3774 if (!seen_histogram)
3776 hr = png_read_histogram(reader, bitmap);
3777 seen_histogram = SUCCEEDED(hr);
3781 IWICMetadataReader_Release(reader);
3784 if (seen_text)
3785 heap_free(seen_text);
3787 png_add_unit_properties(frame, bitmap);
3789 IWICMetadataBlockReader_Release(block_reader);
3791 IWICBitmapFrameDecode_Release(frame);
3794 static GpStatus initialize_decoder_wic(IStream *stream, REFGUID container, IWICBitmapDecoder **decoder)
3796 IWICImagingFactory *factory;
3797 HRESULT hr;
3799 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
3801 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3802 if (FAILED(hr)) return hresult_to_status(hr);
3803 hr = IWICImagingFactory_CreateDecoder(factory, container, NULL, decoder);
3804 IWICImagingFactory_Release(factory);
3805 if (FAILED(hr)) return hresult_to_status(hr);
3807 hr = IWICBitmapDecoder_Initialize(*decoder, stream, WICDecodeMetadataCacheOnLoad);
3808 if (FAILED(hr))
3810 IWICBitmapDecoder_Release(*decoder);
3811 return hresult_to_status(hr);
3813 return Ok;
3816 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3818 static GpStatus decode_frame_wic(IWICBitmapDecoder *decoder, BOOL force_conversion,
3819 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3821 GpStatus status=Ok;
3822 GpBitmap *bitmap;
3823 HRESULT hr;
3824 IWICBitmapFrameDecode *frame;
3825 IWICBitmapSource *source=NULL;
3826 IWICMetadataBlockReader *block_reader;
3827 WICPixelFormatGUID wic_format;
3828 PixelFormat gdip_format=0;
3829 ColorPalette *palette = NULL;
3830 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3831 int i;
3832 UINT width, height, frame_count;
3833 BitmapData lockeddata;
3834 WICRect wrc;
3836 TRACE("%p,%u,%p\n", decoder, active_frame, image);
3838 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3839 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3840 if (SUCCEEDED(hr)) /* got frame */
3842 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3844 if (SUCCEEDED(hr))
3846 if (!force_conversion)
3848 for (i=0; pixel_formats[i].wic_format; i++)
3850 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3852 source = (IWICBitmapSource*)frame;
3853 IWICBitmapSource_AddRef(source);
3854 gdip_format = pixel_formats[i].gdip_format;
3855 palette_type = pixel_formats[i].palette_type;
3856 break;
3860 if (!source)
3862 /* unknown format; fall back on 32bppARGB */
3863 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3864 gdip_format = PixelFormat32bppARGB;
3866 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3869 if (SUCCEEDED(hr)) /* got source */
3871 hr = IWICBitmapSource_GetSize(source, &width, &height);
3873 if (SUCCEEDED(hr))
3874 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3875 NULL, &bitmap);
3877 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3879 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3880 gdip_format, &lockeddata);
3881 if (status == Ok) /* locked bitmap */
3883 wrc.X = 0;
3884 wrc.Width = width;
3885 wrc.Height = 1;
3886 for (i=0; i<height; i++)
3888 wrc.Y = i;
3889 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3890 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3891 if (FAILED(hr)) break;
3894 GdipBitmapUnlockBits(bitmap, &lockeddata);
3897 if (SUCCEEDED(hr) && status == Ok)
3898 *image = &bitmap->image;
3899 else
3901 *image = NULL;
3902 GdipDisposeImage(&bitmap->image);
3905 if (SUCCEEDED(hr) && status == Ok)
3907 double dpix, dpiy;
3908 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3909 if (SUCCEEDED(hr))
3911 bitmap->image.xres = dpix;
3912 bitmap->image.yres = dpiy;
3914 hr = S_OK;
3918 IWICBitmapSource_Release(source);
3921 if (SUCCEEDED(hr) && status == Ok) {
3922 bitmap->metadata_reader = NULL;
3924 if (metadata_reader)
3925 metadata_reader(bitmap, decoder, active_frame);
3926 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3928 UINT block_count = 0;
3929 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3930 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3931 IWICMetadataBlockReader_Release(block_reader);
3934 palette = get_palette(frame, palette_type);
3935 IWICBitmapFrameDecode_Release(frame);
3939 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3941 if (status == Ok)
3943 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3944 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI;
3945 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormat2bppGray) ||
3946 IsEqualGUID(&wic_format, &GUID_WICPixelFormat4bppGray) ||
3947 IsEqualGUID(&wic_format, &GUID_WICPixelFormat8bppGray) ||
3948 IsEqualGUID(&wic_format, &GUID_WICPixelFormat16bppGray))
3949 bitmap->image.flags |= ImageFlagsColorSpaceGRAY;
3950 else
3951 bitmap->image.flags |= ImageFlagsColorSpaceRGB;
3952 bitmap->image.frame_count = frame_count;
3953 bitmap->image.current_frame = active_frame;
3954 bitmap->image.decoder = decoder;
3955 IWICBitmapDecoder_AddRef(decoder);
3956 if (palette)
3958 heap_free(bitmap->image.palette);
3959 bitmap->image.palette = palette;
3961 else
3963 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3964 bitmap->image.palette->Flags = 0;
3966 TRACE("=> %p\n", *image);
3969 return status;
3972 static GpStatus decode_image_wic(IStream *stream, REFGUID container,
3973 metadata_reader_func metadata_reader, GpImage **image)
3975 IWICBitmapDecoder *decoder;
3976 GpStatus status;
3978 status = initialize_decoder_wic(stream, container, &decoder);
3979 if(status != Ok)
3980 return status;
3982 status = decode_frame_wic(decoder, FALSE, 0, metadata_reader, image);
3983 IWICBitmapDecoder_Release(decoder);
3984 return status;
3987 static GpStatus select_frame_wic(GpImage *image, UINT active_frame)
3989 size_t obj_size, body_offset;
3990 GpImage *new_image;
3991 GpStatus status;
3993 status = decode_frame_wic(image->decoder, FALSE, active_frame, NULL, &new_image);
3994 if(status != Ok)
3995 return status;
3997 if (image->type == ImageTypeBitmap)
3998 obj_size = sizeof(GpBitmap);
3999 else if (image->type == ImageTypeMetafile)
4000 obj_size = sizeof(GpMetafile);
4001 else
4003 ERR("unknown image type: %d\n", image->type);
4004 GdipDisposeImage(new_image);
4005 return GenericError;
4008 memcpy(&new_image->format, &image->format, sizeof(GUID));
4009 new_image->encoder = image->encoder;
4010 image->encoder = NULL;
4011 free_image_data(image);
4012 memcpy(image, new_image, FIELD_OFFSET(GpImage, lock));
4013 body_offset = RTL_SIZEOF_THROUGH_FIELD(GpImage, lock);
4014 memcpy((char *)image + body_offset, (char *)new_image + body_offset, obj_size - body_offset);
4015 new_image->type = ~0;
4016 heap_free(new_image);
4017 return Ok;
4020 static HRESULT get_gif_frame_rect(IWICBitmapFrameDecode *frame,
4021 UINT *left, UINT *top, UINT *width, UINT *height)
4023 LONG frame_left = 0, frame_top = 0;
4024 get_gif_frame_property(frame, &GUID_MetadataFormatIMD, L"Left", &frame_left);
4025 get_gif_frame_property(frame, &GUID_MetadataFormatIMD, L"Top", &frame_top);
4026 *left = frame_left;
4027 *top = frame_top;
4029 return IWICBitmapFrameDecode_GetSize(frame, width, height);
4032 static HRESULT blit_gif_frame(GpBitmap *bitmap, IWICBitmapFrameDecode *frame, BOOL first_frame)
4034 UINT i, j, left, top, width, height;
4035 IWICBitmapSource *source;
4036 BYTE *new_bits;
4037 HRESULT hr;
4039 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
4040 if(FAILED(hr))
4041 return hr;
4043 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
4044 if(FAILED(hr))
4045 return hr;
4047 new_bits = heap_alloc_zero(width*height*4);
4048 if(!new_bits)
4049 return E_OUTOFMEMORY;
4051 hr = IWICBitmapSource_CopyPixels(source, NULL, width*4, width*height*4, new_bits);
4052 IWICBitmapSource_Release(source);
4053 if(FAILED(hr)) {
4054 heap_free(new_bits);
4055 return hr;
4058 for(i=0; i<height && i+top<bitmap->height; i++) {
4059 for(j=0; j<width && j+left<bitmap->width; j++) {
4060 DWORD *src = (DWORD*)(new_bits+i*width*4+j*4);
4061 DWORD *dst = (DWORD*)(bitmap->bits+(i+top)*bitmap->stride+(j+left)*4);
4063 if(first_frame || *src>>24 != 0)
4064 *dst = *src;
4067 heap_free(new_bits);
4068 return hr;
4071 static DWORD get_gif_background_color(GpBitmap *bitmap)
4073 BYTE bgcolor_idx = 0;
4074 UINT i;
4076 for(i=0; i<bitmap->prop_count; i++) {
4077 if(bitmap->prop_item[i].id == PropertyTagIndexBackground)
4078 bgcolor_idx = *(BYTE*)bitmap->prop_item[i].value;
4079 else if(bitmap->prop_item[i].id == PropertyTagIndexTransparent)
4080 return 0;
4083 for(i=0; i<bitmap->prop_count; i++) {
4084 if(bitmap->prop_item[i].id == PropertyTagGlobalPalette) {
4085 if(bitmap->prop_item[i].length/3 > bgcolor_idx) {
4086 BYTE *color = ((BYTE*)bitmap->prop_item[i].value)+bgcolor_idx*3;
4087 return color[2] + (color[1]<<8) + (color[0]<<16) + (0xffu<<24);
4089 break;
4093 FIXME("can't get gif background color\n");
4094 return 0xffffffff;
4097 static GpStatus select_frame_gif(GpImage* image, UINT active_frame)
4099 GpBitmap *bitmap = (GpBitmap*)image;
4100 IWICBitmapFrameDecode *frame;
4101 int cur_frame=0;
4102 LONG disposal;
4103 BOOL bgcolor_set = FALSE;
4104 DWORD bgcolor = 0;
4105 HRESULT hr;
4107 if(active_frame > image->current_frame) {
4108 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, image->current_frame, &frame);
4109 if(FAILED(hr))
4110 return hresult_to_status(hr);
4111 disposal = 0;
4112 get_gif_frame_property(frame, &GUID_MetadataFormatGCE, L"Disposal", &disposal);
4113 IWICBitmapFrameDecode_Release(frame);
4115 if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND)
4116 cur_frame = image->current_frame;
4117 else if(disposal != GIF_DISPOSE_RESTORE_TO_PREV)
4118 cur_frame = image->current_frame+1;
4121 while(cur_frame != active_frame) {
4122 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, cur_frame, &frame);
4123 if(FAILED(hr))
4124 return hresult_to_status(hr);
4125 disposal = 0;
4126 get_gif_frame_property(frame, &GUID_MetadataFormatGCE, L"Disposal", &disposal);
4128 if(disposal==GIF_DISPOSE_UNSPECIFIED || disposal==GIF_DISPOSE_DO_NOT_DISPOSE) {
4129 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
4130 if(FAILED(hr))
4131 return hresult_to_status(hr);
4132 }else if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND) {
4133 UINT left, top, width, height, i, j;
4135 if(!bgcolor_set) {
4136 bgcolor = get_gif_background_color(bitmap);
4137 bgcolor_set = TRUE;
4140 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
4141 if(FAILED(hr))
4142 return hresult_to_status(hr);
4143 for(i=top; i<top+height && i<bitmap->height; i++) {
4144 DWORD *bits = (DWORD*)(bitmap->bits+i*bitmap->stride);
4145 for(j=left; j<left+width && j<bitmap->width; j++)
4146 bits[j] = bgcolor;
4150 IWICBitmapFrameDecode_Release(frame);
4151 cur_frame++;
4154 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, active_frame, &frame);
4155 if(FAILED(hr))
4156 return hresult_to_status(hr);
4158 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
4159 IWICBitmapFrameDecode_Release(frame);
4160 if(FAILED(hr))
4161 return hresult_to_status(hr);
4163 image->current_frame = active_frame;
4164 return Ok;
4167 static GpStatus decode_image_icon(IStream* stream, GpImage **image)
4169 return decode_image_wic(stream, &GUID_ContainerFormatIco, NULL, image);
4172 static GpStatus decode_image_bmp(IStream* stream, GpImage **image)
4174 GpStatus status;
4175 GpBitmap* bitmap;
4177 status = decode_image_wic(stream, &GUID_ContainerFormatBmp, NULL, image);
4179 bitmap = (GpBitmap*)*image;
4181 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
4183 /* WIC supports bmp files with alpha, but gdiplus does not */
4184 bitmap->format = PixelFormat32bppRGB;
4187 return status;
4190 static GpStatus decode_image_jpeg(IStream* stream, GpImage **image)
4192 return decode_image_wic(stream, &GUID_ContainerFormatJpeg, NULL, image);
4195 static BOOL has_png_transparency_chunk(IStream *pIStream)
4197 LARGE_INTEGER seek;
4198 BOOL has_tRNS = FALSE;
4199 HRESULT hr;
4200 BYTE header[8];
4202 seek.QuadPart = 8;
4205 ULARGE_INTEGER chunk_start;
4206 ULONG bytesread, chunk_size;
4208 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, &chunk_start);
4209 if (FAILED(hr)) break;
4211 hr = IStream_Read(pIStream, header, 8, &bytesread);
4212 if (FAILED(hr) || bytesread < 8) break;
4214 chunk_size = (header[0] << 24) | (header[1] << 16) | (header[2] << 8) | header[3];
4215 if (!memcmp(&header[4], "tRNS", 4))
4217 has_tRNS = TRUE;
4218 break;
4221 seek.QuadPart = chunk_start.QuadPart + chunk_size + 12; /* skip data and CRC */
4222 } while (memcmp(&header[4], "IDAT", 4) && memcmp(&header[4], "IEND", 4));
4224 TRACE("has_tRNS = %d\n", has_tRNS);
4225 return has_tRNS;
4228 static GpStatus decode_image_png(IStream* stream, GpImage **image)
4230 IWICBitmapDecoder *decoder;
4231 IWICBitmapFrameDecode *frame;
4232 GpStatus status;
4233 HRESULT hr;
4234 GUID format;
4235 BOOL force_conversion = FALSE;
4237 status = initialize_decoder_wic(stream, &GUID_ContainerFormatPng, &decoder);
4238 if (status != Ok)
4239 return status;
4241 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
4242 if (hr == S_OK)
4244 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
4245 if (hr == S_OK)
4247 if (IsEqualGUID(&format, &GUID_WICPixelFormat8bppGray))
4248 force_conversion = TRUE;
4249 else if ((IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed) ||
4250 IsEqualGUID(&format, &GUID_WICPixelFormat4bppIndexed) ||
4251 IsEqualGUID(&format, &GUID_WICPixelFormat2bppIndexed) ||
4252 IsEqualGUID(&format, &GUID_WICPixelFormat1bppIndexed) ||
4253 IsEqualGUID(&format, &GUID_WICPixelFormat24bppBGR)) &&
4254 has_png_transparency_chunk(stream))
4255 force_conversion = TRUE;
4257 status = decode_frame_wic(decoder, force_conversion, 0, png_metadata_reader, image);
4259 else
4260 status = hresult_to_status(hr);
4262 IWICBitmapFrameDecode_Release(frame);
4264 else
4265 status = hresult_to_status(hr);
4267 IWICBitmapDecoder_Release(decoder);
4268 return status;
4271 static GpStatus decode_image_gif(IStream* stream, GpImage **image)
4273 IWICBitmapDecoder *decoder;
4274 UINT frame_count;
4275 GpStatus status;
4276 HRESULT hr;
4278 status = initialize_decoder_wic(stream, &GUID_ContainerFormatGif, &decoder);
4279 if(status != Ok)
4280 return status;
4282 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
4283 if(FAILED(hr))
4284 return hresult_to_status(hr);
4286 status = decode_frame_wic(decoder, frame_count > 1, 0, gif_metadata_reader, image);
4287 IWICBitmapDecoder_Release(decoder);
4288 if(status != Ok)
4289 return status;
4291 if(frame_count > 1) {
4292 heap_free((*image)->palette);
4293 (*image)->palette = NULL;
4295 return Ok;
4298 static GpStatus decode_image_tiff(IStream* stream, GpImage **image)
4300 return decode_image_wic(stream, &GUID_ContainerFormatTiff, NULL, image);
4303 C_ASSERT(offsetof(WmfPlaceableFileHeader, Key) == 0);
4305 static GpStatus load_wmf(IStream *stream, GpMetafile **metafile)
4307 WmfPlaceableFileHeader pfh;
4308 BOOL is_placeable = FALSE;
4309 LARGE_INTEGER seek;
4310 GpStatus status;
4311 METAHEADER mh;
4312 HMETAFILE hmf;
4313 HRESULT hr;
4314 ULONG size;
4315 void *buf;
4317 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4318 if (hr != S_OK || size != sizeof(mh))
4319 return GenericError;
4321 /* detect whether stream starts with a WmfPlaceablefileheader */
4322 if (*(UINT32 *)&mh == WMF_PLACEABLE_KEY)
4324 seek.QuadPart = 0;
4325 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4326 if (FAILED(hr)) return hresult_to_status(hr);
4328 hr = IStream_Read(stream, &pfh, sizeof(pfh), &size);
4329 if (hr != S_OK || size != sizeof(pfh))
4330 return GenericError;
4332 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4333 if (hr != S_OK || size != sizeof(mh))
4334 return GenericError;
4336 is_placeable = TRUE;
4339 seek.QuadPart = is_placeable ? sizeof(pfh) : 0;
4340 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4341 if (FAILED(hr)) return hresult_to_status(hr);
4343 buf = heap_alloc(mh.mtSize * 2);
4344 if (!buf) return OutOfMemory;
4346 hr = IStream_Read(stream, buf, mh.mtSize * 2, &size);
4347 if (hr != S_OK || size != mh.mtSize * 2)
4349 heap_free(buf);
4350 return GenericError;
4353 hmf = SetMetaFileBitsEx(mh.mtSize * 2, buf);
4354 heap_free(buf);
4355 if (!hmf)
4356 return GenericError;
4358 status = GdipCreateMetafileFromWmf(hmf, TRUE, is_placeable ? &pfh : NULL, metafile);
4359 if (status != Ok)
4360 DeleteMetaFile(hmf);
4361 return status;
4364 static GpStatus decode_image_wmf(IStream *stream, GpImage **image)
4366 GpMetafile *metafile;
4367 GpStatus status;
4369 TRACE("%p %p\n", stream, image);
4371 if (!stream || !image)
4372 return InvalidParameter;
4374 status = load_wmf(stream, &metafile);
4375 if (status != Ok)
4377 TRACE("Could not load metafile\n");
4378 return status;
4381 *image = (GpImage *)metafile;
4382 TRACE("<-- %p\n", *image);
4384 return Ok;
4387 static GpStatus load_emf(IStream *stream, GpMetafile **metafile)
4389 LARGE_INTEGER seek;
4390 ENHMETAHEADER emh;
4391 HENHMETAFILE hemf;
4392 GpStatus status;
4393 HRESULT hr;
4394 ULONG size;
4395 void *buf;
4397 hr = IStream_Read(stream, &emh, sizeof(emh), &size);
4398 if (hr != S_OK || size != sizeof(emh) || emh.dSignature != ENHMETA_SIGNATURE)
4399 return GenericError;
4401 seek.QuadPart = 0;
4402 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4403 if (FAILED(hr)) return hresult_to_status(hr);
4405 buf = heap_alloc(emh.nBytes);
4406 if (!buf) return OutOfMemory;
4408 hr = IStream_Read(stream, buf, emh.nBytes, &size);
4409 if (hr != S_OK || size != emh.nBytes)
4411 heap_free(buf);
4412 return GenericError;
4415 hemf = SetEnhMetaFileBits(emh.nBytes, buf);
4416 heap_free(buf);
4417 if (!hemf)
4418 return GenericError;
4420 status = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
4421 if (status != Ok)
4422 DeleteEnhMetaFile(hemf);
4423 return status;
4426 static GpStatus decode_image_emf(IStream *stream, GpImage **image)
4428 GpMetafile *metafile;
4429 GpStatus status;
4431 TRACE("%p %p\n", stream, image);
4433 if (!stream || !image)
4434 return InvalidParameter;
4436 status = load_emf(stream, &metafile);
4437 if (status != Ok)
4439 TRACE("Could not load metafile\n");
4440 return status;
4443 *image = (GpImage *)metafile;
4444 TRACE("<-- %p\n", *image);
4446 return Ok;
4449 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
4450 GDIPCONST EncoderParameters* params);
4452 typedef GpStatus (*decode_image_func)(IStream *stream, GpImage **image);
4454 typedef GpStatus (*select_image_func)(GpImage *image, UINT active_frame);
4456 typedef struct image_codec {
4457 ImageCodecInfo info;
4458 encode_image_func encode_func;
4459 decode_image_func decode_func;
4460 select_image_func select_func;
4461 } image_codec;
4463 typedef enum {
4464 BMP,
4465 JPEG,
4466 GIF,
4467 TIFF,
4468 EMF,
4469 WMF,
4470 PNG,
4471 ICO,
4472 NUM_CODECS
4473 } ImageFormat;
4475 static const struct image_codec codecs[NUM_CODECS];
4477 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
4479 BYTE signature[8];
4480 const BYTE *pattern, *mask;
4481 LARGE_INTEGER seek;
4482 HRESULT hr;
4483 ULONG bytesread;
4484 int i;
4485 DWORD j, sig;
4487 /* seek to the start of the stream */
4488 seek.QuadPart = 0;
4489 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4490 if (FAILED(hr)) return hresult_to_status(hr);
4492 /* read the first 8 bytes */
4493 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4494 hr = IStream_Read(stream, signature, 8, &bytesread);
4495 if (FAILED(hr)) return hresult_to_status(hr);
4496 if (hr == S_FALSE || bytesread == 0) return GenericError;
4498 for (i = 0; i < NUM_CODECS; i++) {
4499 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4500 bytesread >= codecs[i].info.SigSize)
4502 for (sig=0; sig<codecs[i].info.SigCount; sig++)
4504 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
4505 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
4506 for (j=0; j<codecs[i].info.SigSize; j++)
4507 if ((signature[j] & mask[j]) != pattern[j])
4508 break;
4509 if (j == codecs[i].info.SigSize)
4511 *result = &codecs[i];
4512 return Ok;
4518 TRACE("no match for %lu byte signature %x %x %x %x %x %x %x %x\n", bytesread,
4519 signature[0],signature[1],signature[2],signature[3],
4520 signature[4],signature[5],signature[6],signature[7]);
4522 return GenericError;
4525 static GpStatus get_decoder_info_from_image(GpImage *image, const struct image_codec **result)
4527 int i;
4529 for (i = 0; i < NUM_CODECS; i++) {
4530 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4531 IsEqualIID(&codecs[i].info.FormatID, &image->format))
4533 *result = &codecs[i];
4534 return Ok;
4538 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image->format));
4539 return GenericError;
4542 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
4543 UINT frame)
4545 GpStatus stat;
4546 const struct image_codec *codec = NULL;
4548 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
4550 if (!image || !dimensionID)
4551 return InvalidParameter;
4552 if(!image_lock(image))
4553 return ObjectBusy;
4555 if (frame >= image->frame_count)
4556 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
4557 /* rely on codec->select_func() to fail */
4559 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
4561 WARN("invalid image type %d\n", image->type);
4562 image_unlock(image);
4563 return InvalidParameter;
4566 if (image->current_frame == frame)
4568 image_unlock(image);
4569 return Ok;
4572 if (!image->decoder)
4574 TRACE("image doesn't have an associated decoder\n");
4575 image_unlock(image);
4576 return Ok;
4579 /* choose an appropriate image decoder */
4580 stat = get_decoder_info_from_image(image, &codec);
4581 if (stat != Ok)
4583 WARN("can't find decoder info\n");
4584 image_unlock(image);
4585 return stat;
4588 stat = codec->select_func(image, frame);
4589 image_unlock(image);
4590 return stat;
4593 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
4595 GpStatus stat;
4596 LARGE_INTEGER seek;
4597 HRESULT hr;
4598 const struct image_codec *codec=NULL;
4600 TRACE("%p %p\n", stream, image);
4602 if (!stream || !image)
4603 return InvalidParameter;
4605 /* choose an appropriate image decoder */
4606 stat = get_decoder_info(stream, &codec);
4607 if (stat != Ok) return stat;
4609 /* seek to the start of the stream */
4610 seek.QuadPart = 0;
4611 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4612 if (FAILED(hr)) return hresult_to_status(hr);
4614 /* call on the image decoder to do the real work */
4615 stat = codec->decode_func(stream, image);
4617 /* take note of the original data format */
4618 if (stat == Ok)
4620 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
4621 return Ok;
4624 return stat;
4627 /* FIXME: no ICM */
4628 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
4630 TRACE("%p %p\n", stream, image);
4632 return GdipLoadImageFromStream(stream, image);
4635 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
4637 static int calls;
4639 TRACE("(%p,%lu)\n", image, propId);
4641 if(!image)
4642 return InvalidParameter;
4644 if(!(calls++))
4645 FIXME("not implemented\n");
4647 return NotImplemented;
4650 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
4652 static int calls;
4654 if (!image || !item) return InvalidParameter;
4656 TRACE("(%p,%p:%#lx,%u,%lu,%p)\n", image, item, item->id, item->type, item->length, item->value);
4658 if(!(calls++))
4659 FIXME("not implemented\n");
4661 return Ok;
4664 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
4665 GDIPCONST CLSID *clsidEncoder,
4666 GDIPCONST EncoderParameters *encoderParams)
4668 GpStatus stat;
4669 IStream *stream;
4671 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
4673 if (!image || !filename|| !clsidEncoder)
4674 return InvalidParameter;
4676 /* this might release an old file stream held by the encoder so we can re-create it below */
4677 terminate_encoder_wic(image);
4679 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4680 if (stat != Ok)
4681 return GenericError;
4683 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4685 IStream_Release(stream);
4686 return stat;
4689 /*************************************************************************
4690 * Encoding functions -
4691 * These functions encode an image in different image file formats.
4694 static GpStatus initialize_encoder_wic(IStream *stream, REFGUID container, GpImage *image)
4696 IWICImagingFactory *factory;
4697 HRESULT hr;
4699 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
4701 terminate_encoder_wic(image); /* terminate previous encoder if it exists */
4703 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
4704 if (FAILED(hr)) return hresult_to_status(hr);
4705 hr = IWICImagingFactory_CreateEncoder(factory, container, NULL, &image->encoder);
4706 IWICImagingFactory_Release(factory);
4707 if (FAILED(hr))
4709 image->encoder = NULL;
4710 return hresult_to_status(hr);
4713 hr = IWICBitmapEncoder_Initialize(image->encoder, stream, WICBitmapEncoderNoCache);
4714 if (FAILED(hr))
4716 IWICBitmapEncoder_Release(image->encoder);
4717 image->encoder = NULL;
4718 return hresult_to_status(hr);
4720 return Ok;
4723 GpStatus terminate_encoder_wic(GpImage *image)
4725 if (!image->encoder)
4726 return Ok;
4727 else
4729 HRESULT hr = IWICBitmapEncoder_Commit(image->encoder);
4730 IWICBitmapEncoder_Release(image->encoder);
4731 image->encoder = NULL;
4732 return hresult_to_status(hr);
4736 static GpStatus encode_frame_wic(IWICBitmapEncoder *encoder, GpImage *image)
4738 GpStatus stat;
4739 GpBitmap *bitmap;
4740 IWICBitmapFrameEncode *frameencode;
4741 IPropertyBag2 *encoderoptions;
4742 GUID container_format;
4743 HRESULT hr;
4744 UINT width, height;
4745 PixelFormat gdipformat=0;
4746 const WICPixelFormatGUID *desired_wicformat;
4747 WICPixelFormatGUID wicformat;
4748 GpRect rc;
4749 BitmapData lockeddata;
4750 UINT i;
4752 if (image->type != ImageTypeBitmap)
4753 return GenericError;
4755 bitmap = (GpBitmap*)image;
4757 GdipGetImageWidth(image, &width);
4758 GdipGetImageHeight(image, &height);
4760 rc.X = 0;
4761 rc.Y = 0;
4762 rc.Width = width;
4763 rc.Height = height;
4765 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4767 if (SUCCEEDED(hr)) /* created frame */
4769 hr = IWICBitmapEncoder_GetContainerFormat(encoder, &container_format);
4770 if (SUCCEEDED(hr) && IsEqualGUID(&container_format, &GUID_ContainerFormatPng))
4772 /* disable PNG filters for faster encoding */
4773 PROPBAG2 filter_option = { .pstrName = (LPOLESTR) L"FilterOption" };
4774 VARIANT filter_value;
4775 VariantInit(&filter_value);
4776 V_VT(&filter_value) = VT_UI1;
4777 V_UI1(&filter_value) = WICPngFilterNone;
4778 hr = IPropertyBag2_Write(encoderoptions, 1, &filter_option, &filter_value);
4781 if (SUCCEEDED(hr))
4782 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4784 if (SUCCEEDED(hr))
4785 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4787 if (SUCCEEDED(hr))
4788 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4790 if (SUCCEEDED(hr))
4792 for (i=0; pixel_formats[i].wic_format; i++)
4794 if (pixel_formats[i].gdip_format == bitmap->format)
4796 desired_wicformat = pixel_formats[i].wic_format;
4797 gdipformat = bitmap->format;
4798 break;
4801 if (!gdipformat)
4803 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
4804 gdipformat = PixelFormat32bppARGB;
4807 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
4808 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4811 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
4813 /* Encoder doesn't support this bitmap's format. */
4814 gdipformat = 0;
4815 for (i=0; pixel_formats[i].wic_format; i++)
4817 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
4819 gdipformat = pixel_formats[i].gdip_format;
4820 break;
4823 if (!gdipformat)
4825 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
4826 hr = E_FAIL;
4830 if (SUCCEEDED(hr) && IsIndexedPixelFormat(gdipformat) && image->palette)
4831 hr = set_palette(frameencode, image->palette);
4833 if (SUCCEEDED(hr))
4835 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4836 &lockeddata);
4838 if (stat == Ok)
4840 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4841 BYTE *row;
4843 /* write one row at a time in case stride is negative */
4844 row = lockeddata.Scan0;
4845 for (i=0; i<lockeddata.Height; i++)
4847 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4848 if (FAILED(hr)) break;
4849 row += lockeddata.Stride;
4852 GdipBitmapUnlockBits(bitmap, &lockeddata);
4854 else
4855 hr = E_FAIL;
4858 if (SUCCEEDED(hr))
4859 hr = IWICBitmapFrameEncode_Commit(frameencode);
4861 IWICBitmapFrameEncode_Release(frameencode);
4862 IPropertyBag2_Release(encoderoptions);
4865 return hresult_to_status(hr);
4868 static BOOL has_encoder_param_long(GDIPCONST EncoderParameters *params, GUID param_guid, ULONG val)
4870 int param_idx, value_idx;
4872 if (!params)
4873 return FALSE;
4875 for (param_idx = 0; param_idx < params->Count; param_idx++)
4877 EncoderParameter param = params->Parameter[param_idx];
4878 if (param.Type == EncoderParameterValueTypeLong && IsEqualCLSID(&param.Guid, &param_guid))
4880 ULONG *value_array = (ULONG*) param.Value;
4881 for (value_idx = 0; value_idx < param.NumberOfValues; value_idx++)
4883 if (value_array[value_idx] == val)
4884 return TRUE;
4888 return FALSE;
4891 static GpStatus encode_image_wic(GpImage *image, IStream *stream,
4892 REFGUID container, GDIPCONST EncoderParameters *params)
4894 GpStatus status, terminate_status;
4896 if (image->type != ImageTypeBitmap)
4897 return GenericError;
4899 status = initialize_encoder_wic(stream, container, image);
4901 if (status == Ok)
4902 status = encode_frame_wic(image->encoder, image);
4904 if (!has_encoder_param_long(params, EncoderSaveFlag, EncoderValueMultiFrame))
4906 /* always try to terminate, but if something already failed earlier, keep the old status. */
4907 terminate_status = terminate_encoder_wic(image);
4908 if (status == Ok)
4909 status = terminate_status;
4912 return status;
4915 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4916 GDIPCONST EncoderParameters* params)
4918 return encode_image_wic(image, stream, &GUID_ContainerFormatBmp, params);
4921 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4922 GDIPCONST EncoderParameters* params)
4924 return encode_image_wic(image, stream, &GUID_ContainerFormatTiff, params);
4927 GpStatus encode_image_png(GpImage *image, IStream* stream,
4928 GDIPCONST EncoderParameters* params)
4930 return encode_image_wic(image, stream, &GUID_ContainerFormatPng, params);
4933 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4934 GDIPCONST EncoderParameters* params)
4936 return encode_image_wic(image, stream, &GUID_ContainerFormatJpeg, params);
4939 static GpStatus encode_image_gif(GpImage *image, IStream* stream,
4940 GDIPCONST EncoderParameters* params)
4942 return encode_image_wic(image, stream, &GUID_ContainerFormatGif, params);
4945 /*****************************************************************************
4946 * GdipSaveImageToStream [GDIPLUS.@]
4948 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4949 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4951 GpStatus stat;
4952 encode_image_func encode_image;
4953 int i;
4955 TRACE("%p, %p, %s, %p\n", image, stream, wine_dbgstr_guid(clsid), params);
4957 if(!image || !stream)
4958 return InvalidParameter;
4960 /* select correct encoder */
4961 encode_image = NULL;
4962 for (i = 0; i < NUM_CODECS; i++) {
4963 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4964 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4965 encode_image = codecs[i].encode_func;
4967 if (encode_image == NULL)
4968 return UnknownImageFormat;
4970 stat = encode_image(image, stream, params);
4972 return stat;
4975 /*****************************************************************************
4976 * GdipSaveAdd [GDIPLUS.@]
4978 * Like GdipSaveAddImage(), but encode the currently active frame of the given image into the file
4979 * or stream that is currently being encoded.
4981 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4983 return GdipSaveAddImage(image, image, params);
4986 /*****************************************************************************
4987 * GdipSaveAddImage [GDIPLUS.@]
4989 * Encode the currently active frame of additional_image into the file or stream that is currently
4990 * being encoded by the image given in the image parameter. The first frame of a multi-frame image
4991 * must be encoded using the normal GdipSaveImageToStream() or GdipSaveImageToFile() functions,
4992 * but with the "MultiFrame" encoding parameter set. The multi-frame encoding process must be
4993 * finished after adding the last frame by calling GdipSaveAdd() with the "Flush" encoding parameter
4994 * set.
4996 GpStatus WINGDIPAPI GdipSaveAddImage(GpImage *image, GpImage *additional_image,
4997 GDIPCONST EncoderParameters *params)
4999 TRACE("%p, %p, %p\n", image, additional_image, params);
5001 if (!image || !additional_image || !params)
5002 return InvalidParameter;
5004 if (!image->encoder)
5005 return Win32Error;
5007 if (has_encoder_param_long(params, EncoderSaveFlag, EncoderValueFlush))
5008 return terminate_encoder_wic(image);
5009 else if (has_encoder_param_long(params, EncoderSaveFlag, EncoderValueFrameDimensionPage))
5010 return encode_frame_wic(image->encoder, additional_image);
5011 else
5012 return InvalidParameter;
5015 /*****************************************************************************
5016 * GdipGetImagePalette [GDIPLUS.@]
5018 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
5020 INT count;
5022 TRACE("(%p,%p,%i)\n", image, palette, size);
5024 if (!image || !palette)
5025 return InvalidParameter;
5027 count = image->palette ? image->palette->Count : 0;
5029 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
5031 TRACE("<-- InsufficientBuffer\n");
5032 return InsufficientBuffer;
5035 if (image->palette)
5037 palette->Flags = image->palette->Flags;
5038 palette->Count = image->palette->Count;
5039 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
5041 else
5043 palette->Flags = 0;
5044 palette->Count = 0;
5046 return Ok;
5049 /*****************************************************************************
5050 * GdipSetImagePalette [GDIPLUS.@]
5052 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
5053 GDIPCONST ColorPalette *palette)
5055 ColorPalette *new_palette;
5057 TRACE("(%p,%p)\n", image, palette);
5059 if(!image || !palette || palette->Count > 256)
5060 return InvalidParameter;
5062 new_palette = heap_alloc_zero(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
5063 if (!new_palette) return OutOfMemory;
5065 heap_free(image->palette);
5066 image->palette = new_palette;
5067 image->palette->Flags = palette->Flags;
5068 image->palette->Count = palette->Count;
5069 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
5071 return Ok;
5074 /*************************************************************************
5075 * Encoders -
5076 * Structures that represent which formats we support for encoding.
5079 /* ImageCodecInfo creation routines taken from libgdiplus */
5080 static const WCHAR bmp_codecname[] = L"Built-in BMP";
5081 static const WCHAR bmp_extension[] = L"*.BMP;*.DIB;*.RLE";
5082 static const WCHAR bmp_mimetype[] = L"image/bmp";
5083 static const WCHAR bmp_format[] = L"BMP";
5084 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
5085 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
5087 static const WCHAR jpeg_codecname[] = L"Built-in JPEG";
5088 static const WCHAR jpeg_extension[] = L"*.JPG;*.JPEG;*.JPE;*.JFIF";
5089 static const WCHAR jpeg_mimetype[] = L"image/jpeg";
5090 static const WCHAR jpeg_format[] = L"JPEG";
5091 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
5092 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
5094 static const WCHAR gif_codecname[] = L"Built-in GIF";
5095 static const WCHAR gif_extension[] = L"*.GIF";
5096 static const WCHAR gif_mimetype[] = L"image/gif";
5097 static const WCHAR gif_format[] = L"GIF";
5098 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
5099 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5101 static const WCHAR tiff_codecname[] = L"Built-in TIFF";
5102 static const WCHAR tiff_extension[] = L"*.TIFF;*.TIF";
5103 static const WCHAR tiff_mimetype[] = L"image/tiff";
5104 static const WCHAR tiff_format[] = L"TIFF";
5105 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
5106 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5108 static const WCHAR emf_codecname[] = L"Built-in EMF";
5109 static const WCHAR emf_extension[] = L"*.EMF";
5110 static const WCHAR emf_mimetype[] = L"image/x-emf";
5111 static const WCHAR emf_format[] = L"EMF";
5112 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
5113 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
5115 static const WCHAR wmf_codecname[] = L"Built-in WMF";
5116 static const WCHAR wmf_extension[] = L"*.WMF";
5117 static const WCHAR wmf_mimetype[] = L"image/x-wmf";
5118 static const WCHAR wmf_format[] = L"WMF";
5119 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
5120 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
5122 static const WCHAR png_codecname[] = L"Built-in PNG";
5123 static const WCHAR png_extension[] = L"*.PNG";
5124 static const WCHAR png_mimetype[] = L"image/png";
5125 static const WCHAR png_format[] = L"PNG";
5126 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
5127 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5129 static const WCHAR ico_codecname[] = L"Built-in ICO";
5130 static const WCHAR ico_extension[] = L"*.ICO";
5131 static const WCHAR ico_mimetype[] = L"image/x-icon";
5132 static const WCHAR ico_format[] = L"ICO";
5133 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
5134 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
5136 static const struct image_codec codecs[NUM_CODECS] = {
5138 { /* BMP */
5139 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5140 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5141 /* CodecName */ bmp_codecname,
5142 /* DllName */ NULL,
5143 /* FormatDescription */ bmp_format,
5144 /* FilenameExtension */ bmp_extension,
5145 /* MimeType */ bmp_mimetype,
5146 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5147 /* Version */ 1,
5148 /* SigCount */ 1,
5149 /* SigSize */ 2,
5150 /* SigPattern */ bmp_sig_pattern,
5151 /* SigMask */ bmp_sig_mask,
5153 encode_image_BMP,
5154 decode_image_bmp,
5155 select_frame_wic
5158 { /* JPEG */
5159 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5160 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5161 /* CodecName */ jpeg_codecname,
5162 /* DllName */ NULL,
5163 /* FormatDescription */ jpeg_format,
5164 /* FilenameExtension */ jpeg_extension,
5165 /* MimeType */ jpeg_mimetype,
5166 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5167 /* Version */ 1,
5168 /* SigCount */ 1,
5169 /* SigSize */ 2,
5170 /* SigPattern */ jpeg_sig_pattern,
5171 /* SigMask */ jpeg_sig_mask,
5173 encode_image_jpeg,
5174 decode_image_jpeg,
5175 select_frame_wic
5178 { /* GIF */
5179 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5180 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5181 /* CodecName */ gif_codecname,
5182 /* DllName */ NULL,
5183 /* FormatDescription */ gif_format,
5184 /* FilenameExtension */ gif_extension,
5185 /* MimeType */ gif_mimetype,
5186 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5187 /* Version */ 1,
5188 /* SigCount */ 2,
5189 /* SigSize */ 6,
5190 /* SigPattern */ gif_sig_pattern,
5191 /* SigMask */ gif_sig_mask,
5193 encode_image_gif,
5194 decode_image_gif,
5195 select_frame_gif
5198 { /* TIFF */
5199 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5200 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5201 /* CodecName */ tiff_codecname,
5202 /* DllName */ NULL,
5203 /* FormatDescription */ tiff_format,
5204 /* FilenameExtension */ tiff_extension,
5205 /* MimeType */ tiff_mimetype,
5206 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5207 /* Version */ 1,
5208 /* SigCount */ 2,
5209 /* SigSize */ 4,
5210 /* SigPattern */ tiff_sig_pattern,
5211 /* SigMask */ tiff_sig_mask,
5213 encode_image_tiff,
5214 decode_image_tiff,
5215 select_frame_wic
5218 { /* EMF */
5219 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5220 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5221 /* CodecName */ emf_codecname,
5222 /* DllName */ NULL,
5223 /* FormatDescription */ emf_format,
5224 /* FilenameExtension */ emf_extension,
5225 /* MimeType */ emf_mimetype,
5226 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
5227 /* Version */ 1,
5228 /* SigCount */ 1,
5229 /* SigSize */ 4,
5230 /* SigPattern */ emf_sig_pattern,
5231 /* SigMask */ emf_sig_mask,
5233 NULL,
5234 decode_image_emf,
5235 NULL
5238 { /* WMF */
5239 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5240 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5241 /* CodecName */ wmf_codecname,
5242 /* DllName */ NULL,
5243 /* FormatDescription */ wmf_format,
5244 /* FilenameExtension */ wmf_extension,
5245 /* MimeType */ wmf_mimetype,
5246 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
5247 /* Version */ 1,
5248 /* SigCount */ 1,
5249 /* SigSize */ 2,
5250 /* SigPattern */ wmf_sig_pattern,
5251 /* SigMask */ wmf_sig_mask,
5253 NULL,
5254 decode_image_wmf,
5255 NULL
5258 { /* PNG */
5259 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5260 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5261 /* CodecName */ png_codecname,
5262 /* DllName */ NULL,
5263 /* FormatDescription */ png_format,
5264 /* FilenameExtension */ png_extension,
5265 /* MimeType */ png_mimetype,
5266 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5267 /* Version */ 1,
5268 /* SigCount */ 1,
5269 /* SigSize */ 8,
5270 /* SigPattern */ png_sig_pattern,
5271 /* SigMask */ png_sig_mask,
5273 encode_image_png,
5274 decode_image_png,
5275 select_frame_wic
5278 { /* ICO */
5279 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5280 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5281 /* CodecName */ ico_codecname,
5282 /* DllName */ NULL,
5283 /* FormatDescription */ ico_format,
5284 /* FilenameExtension */ ico_extension,
5285 /* MimeType */ ico_mimetype,
5286 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5287 /* Version */ 1,
5288 /* SigCount */ 1,
5289 /* SigSize */ 4,
5290 /* SigPattern */ ico_sig_pattern,
5291 /* SigMask */ ico_sig_mask,
5293 NULL,
5294 decode_image_icon,
5295 select_frame_wic
5299 /*****************************************************************************
5300 * GdipGetImageDecodersSize [GDIPLUS.@]
5302 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
5304 int decoder_count=0;
5305 int i;
5306 TRACE("%p %p\n", numDecoders, size);
5308 if (!numDecoders || !size)
5309 return InvalidParameter;
5311 for (i=0; i<NUM_CODECS; i++)
5313 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
5314 decoder_count++;
5317 *numDecoders = decoder_count;
5318 *size = decoder_count * sizeof(ImageCodecInfo);
5320 return Ok;
5323 /*****************************************************************************
5324 * GdipGetImageDecoders [GDIPLUS.@]
5326 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
5328 int i, decoder_count=0;
5329 TRACE("%u %u %p\n", numDecoders, size, decoders);
5331 if (!decoders ||
5332 size != numDecoders * sizeof(ImageCodecInfo))
5333 return GenericError;
5335 for (i=0; i<NUM_CODECS; i++)
5337 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
5339 if (decoder_count == numDecoders) return GenericError;
5340 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
5341 decoder_count++;
5345 if (decoder_count < numDecoders) return GenericError;
5347 return Ok;
5350 /*****************************************************************************
5351 * GdipGetImageEncodersSize [GDIPLUS.@]
5353 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
5355 int encoder_count=0;
5356 int i;
5357 TRACE("%p %p\n", numEncoders, size);
5359 if (!numEncoders || !size)
5360 return InvalidParameter;
5362 for (i=0; i<NUM_CODECS; i++)
5364 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
5365 encoder_count++;
5368 *numEncoders = encoder_count;
5369 *size = encoder_count * sizeof(ImageCodecInfo);
5371 return Ok;
5374 /*****************************************************************************
5375 * GdipGetImageEncoders [GDIPLUS.@]
5377 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
5379 int i, encoder_count=0;
5380 TRACE("%u %u %p\n", numEncoders, size, encoders);
5382 if (!encoders ||
5383 size != numEncoders * sizeof(ImageCodecInfo))
5384 return GenericError;
5386 for (i=0; i<NUM_CODECS; i++)
5388 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
5390 if (encoder_count == numEncoders) return GenericError;
5391 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
5392 encoder_count++;
5396 if (encoder_count < numEncoders) return GenericError;
5398 return Ok;
5401 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
5402 GDIPCONST CLSID* clsidEncoder, UINT *size)
5404 static int calls;
5406 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
5408 if(!(calls++))
5409 FIXME("not implemented\n");
5411 *size = 0;
5413 return NotImplemented;
5416 static PixelFormat get_16bpp_format(HBITMAP hbm)
5418 BITMAPV4HEADER bmh;
5419 HDC hdc;
5420 PixelFormat result;
5422 hdc = CreateCompatibleDC(NULL);
5424 memset(&bmh, 0, sizeof(bmh));
5425 bmh.bV4Size = sizeof(bmh);
5426 bmh.bV4Width = 1;
5427 bmh.bV4Height = 1;
5428 bmh.bV4V4Compression = BI_BITFIELDS;
5429 bmh.bV4BitCount = 16;
5431 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
5433 if (bmh.bV4RedMask == 0x7c00 &&
5434 bmh.bV4GreenMask == 0x3e0 &&
5435 bmh.bV4BlueMask == 0x1f)
5437 result = PixelFormat16bppRGB555;
5439 else if (bmh.bV4RedMask == 0xf800 &&
5440 bmh.bV4GreenMask == 0x7e0 &&
5441 bmh.bV4BlueMask == 0x1f)
5443 result = PixelFormat16bppRGB565;
5445 else
5447 FIXME("unrecognized bitfields %lx,%lx,%lx\n", bmh.bV4RedMask,
5448 bmh.bV4GreenMask, bmh.bV4BlueMask);
5449 result = PixelFormatUndefined;
5452 DeleteDC(hdc);
5454 return result;
5457 /*****************************************************************************
5458 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
5460 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
5462 BITMAP bm;
5463 GpStatus retval;
5464 PixelFormat format;
5465 BitmapData lockeddata;
5466 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
5467 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
5469 TRACE("%p %p %p\n", hbm, hpal, bitmap);
5471 if(!hbm || !bitmap)
5472 return InvalidParameter;
5474 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
5475 return InvalidParameter;
5477 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
5478 switch(bm.bmBitsPixel) {
5479 case 1:
5480 format = PixelFormat1bppIndexed;
5481 break;
5482 case 4:
5483 format = PixelFormat4bppIndexed;
5484 break;
5485 case 8:
5486 format = PixelFormat8bppIndexed;
5487 break;
5488 case 16:
5489 format = get_16bpp_format(hbm);
5490 if (format == PixelFormatUndefined)
5491 return InvalidParameter;
5492 break;
5493 case 24:
5494 format = PixelFormat24bppRGB;
5495 break;
5496 case 32:
5497 format = PixelFormat32bppRGB;
5498 break;
5499 case 48:
5500 format = PixelFormat48bppRGB;
5501 break;
5502 default:
5503 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
5504 return InvalidParameter;
5507 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
5508 format, NULL, bitmap);
5510 if (retval == Ok)
5512 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
5513 format, &lockeddata);
5514 if (retval == Ok)
5516 HDC hdc;
5517 INT src_height;
5519 hdc = CreateCompatibleDC(NULL);
5521 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
5522 pbmi->bmiHeader.biBitCount = 0;
5524 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
5526 src_height = abs(pbmi->bmiHeader.biHeight);
5527 pbmi->bmiHeader.biHeight = -src_height;
5529 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
5531 DeleteDC(hdc);
5533 GdipBitmapUnlockBits(*bitmap, &lockeddata);
5536 /* According to the tests hpal is ignored */
5537 if (retval == Ok && pbmi->bmiHeader.biBitCount <= 8)
5539 ColorPalette *palette;
5540 int i, num_palette_entries;
5542 num_palette_entries = pbmi->bmiHeader.biClrUsed;
5543 if (!num_palette_entries)
5544 num_palette_entries = 1 << pbmi->bmiHeader.biBitCount;
5546 palette = heap_alloc_zero(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
5547 if (!palette)
5548 retval = OutOfMemory;
5549 else
5551 palette->Flags = 0;
5552 palette->Count = num_palette_entries;
5554 for (i=0; i<num_palette_entries; i++)
5556 palette->Entries[i] = 0xff000000 | pbmi->bmiColors[i].rgbRed << 16 |
5557 pbmi->bmiColors[i].rgbGreen << 8 | pbmi->bmiColors[i].rgbBlue;
5560 retval = GdipSetImagePalette(&(*bitmap)->image, palette);
5563 heap_free(palette);
5566 if (retval != Ok)
5568 GdipDisposeImage(&(*bitmap)->image);
5569 *bitmap = NULL;
5573 return retval;
5576 /*****************************************************************************
5577 * GdipCreateEffect [GDIPLUS.@]
5579 GpStatus WINGDIPAPI GdipCreateEffect(const GUID guid, CGpEffect **effect)
5581 FIXME("(%s, %p): stub\n", debugstr_guid(&guid), effect);
5583 if(!effect)
5584 return InvalidParameter;
5586 *effect = NULL;
5588 return NotImplemented;
5591 /*****************************************************************************
5592 * GdipDeleteEffect [GDIPLUS.@]
5594 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
5596 FIXME("(%p): stub\n", effect);
5597 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5598 * in Windows's gdiplus */
5599 return NotImplemented;
5602 /*****************************************************************************
5603 * GdipSetEffectParameters [GDIPLUS.@]
5605 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
5606 const VOID *params, const UINT size)
5608 static int calls;
5610 TRACE("(%p,%p,%u)\n", effect, params, size);
5612 if(!(calls++))
5613 FIXME("not implemented\n");
5615 return NotImplemented;
5618 /*****************************************************************************
5619 * GdipGetImageFlags [GDIPLUS.@]
5621 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
5623 TRACE("%p %p\n", image, flags);
5625 if(!image || !flags)
5626 return InvalidParameter;
5628 *flags = image->flags;
5630 return Ok;
5633 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
5635 TRACE("(%d, %p)\n", control, param);
5637 switch(control){
5638 case TestControlForceBilinear:
5639 if(param)
5640 FIXME("TestControlForceBilinear not handled\n");
5641 break;
5642 case TestControlNoICM:
5643 if(param)
5644 FIXME("TestControlNoICM not handled\n");
5645 break;
5646 case TestControlGetBuildNumber:
5647 *((DWORD*)param) = 3102;
5648 break;
5651 return Ok;
5654 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
5656 TRACE("%p\n", image);
5658 return Ok;
5661 /*****************************************************************************
5662 * GdipGetImageThumbnail [GDIPLUS.@]
5664 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
5665 GpImage **ret_image, GetThumbnailImageAbort cb,
5666 VOID * cb_data)
5668 GpStatus stat;
5669 GpGraphics *graphics;
5670 UINT srcwidth, srcheight;
5672 TRACE("(%p %u %u %p %p %p)\n",
5673 image, width, height, ret_image, cb, cb_data);
5675 if (!image || !ret_image)
5676 return InvalidParameter;
5678 if (!width) width = 120;
5679 if (!height) height = 120;
5681 GdipGetImageWidth(image, &srcwidth);
5682 GdipGetImageHeight(image, &srcheight);
5684 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
5685 NULL, (GpBitmap**)ret_image);
5687 if (stat == Ok)
5689 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
5691 if (stat == Ok)
5693 stat = GdipDrawImageRectRectI(graphics, image,
5694 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
5695 NULL, NULL, NULL);
5697 GdipDeleteGraphics(graphics);
5700 if (stat != Ok)
5702 GdipDisposeImage(*ret_image);
5703 *ret_image = NULL;
5707 return stat;
5710 /*****************************************************************************
5711 * GdipImageRotateFlip [GDIPLUS.@]
5713 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
5715 GpBitmap *new_bitmap;
5716 GpBitmap *bitmap;
5717 int bpp, bytesperpixel;
5718 BOOL rotate_90, flip_x, flip_y;
5719 int src_x_offset, src_y_offset;
5720 LPBYTE src_origin;
5721 UINT x, y, width, height;
5722 BitmapData dst_lock;
5723 GpStatus stat;
5725 TRACE("(%p, %u)\n", image, type);
5727 if (!image)
5728 return InvalidParameter;
5729 if (!image_lock(image))
5730 return ObjectBusy;
5732 rotate_90 = type&1;
5733 flip_x = (type&6) == 2 || (type&6) == 4;
5734 flip_y = (type&3) == 1 || (type&3) == 2;
5736 if (image->type != ImageTypeBitmap)
5738 FIXME("Not implemented for type %i\n", image->type);
5739 image_unlock(image);
5740 return NotImplemented;
5743 bitmap = (GpBitmap*)image;
5744 bpp = PIXELFORMATBPP(bitmap->format);
5746 if (bpp < 8)
5748 FIXME("Not implemented for %i bit images\n", bpp);
5749 image_unlock(image);
5750 return NotImplemented;
5753 if (rotate_90)
5755 width = bitmap->height;
5756 height = bitmap->width;
5758 else
5760 width = bitmap->width;
5761 height = bitmap->height;
5764 bytesperpixel = bpp/8;
5766 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5768 if (stat == Ok)
5770 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5772 if (stat == Ok)
5774 LPBYTE src_row, src_pixel;
5775 LPBYTE dst_row, dst_pixel;
5777 src_origin = bitmap->bits;
5778 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5779 if (flip_y) src_origin += bitmap->stride * (bitmap->height - 1);
5781 if (rotate_90)
5783 if (flip_y) src_x_offset = -bitmap->stride;
5784 else src_x_offset = bitmap->stride;
5785 if (flip_x) src_y_offset = -bytesperpixel;
5786 else src_y_offset = bytesperpixel;
5788 else
5790 if (flip_x) src_x_offset = -bytesperpixel;
5791 else src_x_offset = bytesperpixel;
5792 if (flip_y) src_y_offset = -bitmap->stride;
5793 else src_y_offset = bitmap->stride;
5796 src_row = src_origin;
5797 dst_row = dst_lock.Scan0;
5798 for (y=0; y<height; y++)
5800 src_pixel = src_row;
5801 dst_pixel = dst_row;
5802 for (x=0; x<width; x++)
5804 /* FIXME: This could probably be faster without memcpy. */
5805 memcpy(dst_pixel, src_pixel, bytesperpixel);
5806 dst_pixel += bytesperpixel;
5807 src_pixel += src_x_offset;
5809 src_row += src_y_offset;
5810 dst_row += dst_lock.Stride;
5813 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5814 move_bitmap(bitmap, new_bitmap, FALSE);
5816 else GdipDisposeImage(&new_bitmap->image);
5819 image_unlock(image);
5820 return stat;
5823 /*****************************************************************************
5824 * GdipImageSetAbort [GDIPLUS.@]
5826 GpStatus WINGDIPAPI GdipImageSetAbort(GpImage *image, GdiplusAbort *pabort)
5828 TRACE("(%p, %p)\n", image, pabort);
5830 if (!image)
5831 return InvalidParameter;
5833 if (pabort)
5834 FIXME("Abort callback is not supported.\n");
5836 return Ok;
5839 /*****************************************************************************
5840 * GdipBitmapConvertFormat [GDIPLUS.@]
5842 GpStatus WINGDIPAPI GdipBitmapConvertFormat(GpBitmap *bitmap, PixelFormat format, DitherType dithertype,
5843 PaletteType palettetype, ColorPalette *palette, REAL alphathreshold)
5845 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap, format, dithertype, palettetype, palette, alphathreshold);
5846 return NotImplemented;
5849 static void set_histogram_point_argb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5851 ch0[ color >> 24 ]++;
5852 ch1[(color >> 16) & 0xff]++;
5853 ch2[(color >> 8) & 0xff]++;
5854 ch3[ color & 0xff]++;
5857 static void set_histogram_point_pargb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5859 BYTE alpha = color >> 24;
5861 ch0[alpha]++;
5862 ch1[(((color >> 16) & 0xff) * alpha) / 0xff]++;
5863 ch2[(((color >> 8) & 0xff) * alpha) / 0xff]++;
5864 ch3[(( color & 0xff) * alpha) / 0xff]++;
5867 static void set_histogram_point_rgb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5869 ch0[(color >> 16) & 0xff]++;
5870 ch1[(color >> 8) & 0xff]++;
5871 ch2[ color & 0xff]++;
5874 static void set_histogram_point_gray(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5876 ch0[(76 * ((color >> 16) & 0xff) + 150 * ((color >> 8) & 0xff) + 29 * (color & 0xff)) / 0xff]++;
5879 static void set_histogram_point_b(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5881 ch0[color & 0xff]++;
5884 static void set_histogram_point_g(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5886 ch0[(color >> 8) & 0xff]++;
5889 static void set_histogram_point_r(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5891 ch0[(color >> 16) & 0xff]++;
5894 static void set_histogram_point_a(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5896 ch0[(color >> 24) & 0xff]++;
5899 /*****************************************************************************
5900 * GdipBitmapGetHistogram [GDIPLUS.@]
5902 GpStatus WINGDIPAPI GdipBitmapGetHistogram(GpBitmap *bitmap, HistogramFormat format, UINT num_of_entries,
5903 UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5905 static void (* const set_histogram_point[])(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3) =
5907 set_histogram_point_argb,
5908 set_histogram_point_pargb,
5909 set_histogram_point_rgb,
5910 set_histogram_point_gray,
5911 set_histogram_point_b,
5912 set_histogram_point_g,
5913 set_histogram_point_r,
5914 set_histogram_point_a,
5916 UINT width, height, x, y;
5918 TRACE("(%p, %d, %u, %p, %p, %p, %p)\n", bitmap, format, num_of_entries,
5919 ch0, ch1, ch2, ch3);
5921 if (!bitmap || num_of_entries != 256)
5922 return InvalidParameter;
5924 /* Make sure passed channel pointers match requested format */
5925 switch (format)
5927 case HistogramFormatARGB:
5928 case HistogramFormatPARGB:
5929 if (!ch0 || !ch1 || !ch2 || !ch3)
5930 return InvalidParameter;
5931 memset(ch0, 0, num_of_entries * sizeof(UINT));
5932 memset(ch1, 0, num_of_entries * sizeof(UINT));
5933 memset(ch2, 0, num_of_entries * sizeof(UINT));
5934 memset(ch3, 0, num_of_entries * sizeof(UINT));
5935 break;
5936 case HistogramFormatRGB:
5937 if (!ch0 || !ch1 || !ch2 || ch3)
5938 return InvalidParameter;
5939 memset(ch0, 0, num_of_entries * sizeof(UINT));
5940 memset(ch1, 0, num_of_entries * sizeof(UINT));
5941 memset(ch2, 0, num_of_entries * sizeof(UINT));
5942 break;
5943 case HistogramFormatGray:
5944 case HistogramFormatB:
5945 case HistogramFormatG:
5946 case HistogramFormatR:
5947 case HistogramFormatA:
5948 if (!ch0 || ch1 || ch2 || ch3)
5949 return InvalidParameter;
5950 memset(ch0, 0, num_of_entries * sizeof(UINT));
5951 break;
5952 default:
5953 WARN("Invalid histogram format requested, %d\n", format);
5954 return InvalidParameter;
5957 GdipGetImageWidth(&bitmap->image, &width);
5958 GdipGetImageHeight(&bitmap->image, &height);
5960 for (y = 0; y < height; y++)
5961 for (x = 0; x < width; x++)
5963 ARGB color;
5965 GdipBitmapGetPixel(bitmap, x, y, &color);
5966 set_histogram_point[format](color, ch0, ch1, ch2, ch3);
5969 return Ok;
5972 /*****************************************************************************
5973 * GdipBitmapGetHistogramSize [GDIPLUS.@]
5975 GpStatus WINGDIPAPI GdipBitmapGetHistogramSize(HistogramFormat format, UINT *num_of_entries)
5977 TRACE("(%d, %p)\n", format, num_of_entries);
5979 if (!num_of_entries)
5980 return InvalidParameter;
5982 *num_of_entries = 256;
5983 return Ok;
5986 static GpStatus create_optimal_palette(ColorPalette *palette, INT desired,
5987 BOOL transparent, GpBitmap *bitmap)
5989 GpStatus status;
5990 BitmapData data;
5991 HRESULT hr;
5992 IWICImagingFactory *factory;
5993 IWICPalette *wic_palette;
5995 if (!bitmap) return InvalidParameter;
5996 if (palette->Count < desired) return GenericError;
5998 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, PixelFormat24bppRGB, &data);
5999 if (status != Ok) return status;
6001 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
6002 if (hr != S_OK)
6004 GdipBitmapUnlockBits(bitmap, &data);
6005 return hresult_to_status(hr);
6008 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
6009 if (hr == S_OK)
6011 IWICBitmap *bitmap;
6013 /* PixelFormat24bppRGB actually stores the bitmap bits as BGR. */
6014 hr = IWICImagingFactory_CreateBitmapFromMemory(factory, data.Width, data.Height,
6015 &GUID_WICPixelFormat24bppBGR, data.Stride, data.Stride * data.Width, data.Scan0, &bitmap);
6016 if (hr == S_OK)
6018 hr = IWICPalette_InitializeFromBitmap(wic_palette, (IWICBitmapSource *)bitmap, desired, transparent);
6019 if (hr == S_OK)
6021 palette->Flags = 0;
6022 IWICPalette_GetColorCount(wic_palette, &palette->Count);
6023 IWICPalette_GetColors(wic_palette, palette->Count, (UINT *)palette->Entries, &palette->Count);
6026 IWICBitmap_Release(bitmap);
6029 IWICPalette_Release(wic_palette);
6032 IWICImagingFactory_Release(factory);
6033 GdipBitmapUnlockBits(bitmap, &data);
6035 return hresult_to_status(hr);
6038 /*****************************************************************************
6039 * GdipInitializePalette [GDIPLUS.@]
6041 GpStatus WINGDIPAPI GdipInitializePalette(ColorPalette *palette,
6042 PaletteType type, INT desired, BOOL transparent, GpBitmap *bitmap)
6044 TRACE("(%p,%d,%d,%d,%p)\n", palette, type, desired, transparent, bitmap);
6046 if (!palette) return InvalidParameter;
6048 switch (type)
6050 case PaletteTypeCustom:
6051 return Ok;
6053 case PaletteTypeOptimal:
6054 return create_optimal_palette(palette, desired, transparent, bitmap);
6056 /* WIC palette type enumeration matches these gdiplus enums */
6057 case PaletteTypeFixedBW:
6058 case PaletteTypeFixedHalftone8:
6059 case PaletteTypeFixedHalftone27:
6060 case PaletteTypeFixedHalftone64:
6061 case PaletteTypeFixedHalftone125:
6062 case PaletteTypeFixedHalftone216:
6063 case PaletteTypeFixedHalftone252:
6064 case PaletteTypeFixedHalftone256:
6066 ColorPalette *wic_palette;
6067 GpStatus status = Ok;
6069 wic_palette = get_palette(NULL, type);
6070 if (!wic_palette) return OutOfMemory;
6072 if (palette->Count >= wic_palette->Count)
6074 palette->Flags = wic_palette->Flags;
6075 palette->Count = wic_palette->Count;
6076 memcpy(palette->Entries, wic_palette->Entries, wic_palette->Count * sizeof(wic_palette->Entries[0]));
6078 else
6079 status = GenericError;
6081 heap_free(wic_palette);
6083 return status;
6086 default:
6087 FIXME("unknown palette type %d\n", type);
6088 break;
6091 return InvalidParameter;