d3d11: Report D3D11_FORMAT_SUPPORT_TYPED_UNORDERED_ACCESS_VIEW in CheckFormatSupport().
[wine.git] / dlls / gdiplus / image.c
blob39f717b5b3d9a1b880be38181cf4172070eee185
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012,2016 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
21 #include <assert.h>
23 #define NONAMELESSUNION
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "wingdi.h"
30 #define COBJMACROS
31 #include "objbase.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "initguid.h"
36 #include "wincodec.h"
37 #include "gdiplus.h"
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
43 HRESULT WINAPI WICCreateImagingFactory_Proxy(UINT, IWICImagingFactory**);
45 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
46 #define WMF_PLACEABLE_KEY 0x9ac6cdd7
48 static const struct
50 const WICPixelFormatGUID *wic_format;
51 PixelFormat gdip_format;
52 /* predefined palette type to use for pixel format conversions */
53 WICBitmapPaletteType palette_type;
54 } pixel_formats[] =
56 { &GUID_WICPixelFormat1bppIndexed, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
57 { &GUID_WICPixelFormatBlackWhite, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
58 { &GUID_WICPixelFormat4bppIndexed, PixelFormat4bppIndexed, WICBitmapPaletteTypeFixedHalftone8 },
59 { &GUID_WICPixelFormat8bppIndexed, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedHalftone256 },
60 { &GUID_WICPixelFormat8bppGray, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedGray256 },
61 { &GUID_WICPixelFormat16bppBGR555, PixelFormat16bppRGB555, 0 },
62 { &GUID_WICPixelFormat24bppBGR, PixelFormat24bppRGB, 0 },
63 { &GUID_WICPixelFormat32bppBGR, PixelFormat32bppRGB, 0 },
64 { &GUID_WICPixelFormat32bppBGRA, PixelFormat32bppARGB, 0 },
65 { &GUID_WICPixelFormat32bppPBGRA, PixelFormat32bppPARGB, 0 },
66 { NULL }
69 static ColorPalette *get_palette(IWICBitmapFrameDecode *frame, WICBitmapPaletteType palette_type)
71 HRESULT hr;
72 IWICImagingFactory *factory;
73 IWICPalette *wic_palette;
74 ColorPalette *palette = NULL;
76 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
77 if (hr != S_OK) return NULL;
79 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
80 if (hr == S_OK)
82 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
83 if (frame)
84 hr = IWICBitmapFrameDecode_CopyPalette(frame, wic_palette);
85 if (hr != S_OK && palette_type != 0)
87 TRACE("using predefined palette %#x\n", palette_type);
88 hr = IWICPalette_InitializePredefined(wic_palette, palette_type, FALSE);
90 if (hr == S_OK)
92 WICBitmapPaletteType type;
93 BOOL alpha;
94 UINT count;
96 IWICPalette_GetColorCount(wic_palette, &count);
97 palette = heap_alloc(2 * sizeof(UINT) + count * sizeof(ARGB));
98 IWICPalette_GetColors(wic_palette, count, palette->Entries, &palette->Count);
100 IWICPalette_GetType(wic_palette, &type);
101 switch(type) {
102 case WICBitmapPaletteTypeFixedGray4:
103 case WICBitmapPaletteTypeFixedGray16:
104 case WICBitmapPaletteTypeFixedGray256:
105 palette->Flags = PaletteFlagsGrayScale;
106 break;
107 case WICBitmapPaletteTypeFixedHalftone8:
108 case WICBitmapPaletteTypeFixedHalftone27:
109 case WICBitmapPaletteTypeFixedHalftone64:
110 case WICBitmapPaletteTypeFixedHalftone125:
111 case WICBitmapPaletteTypeFixedHalftone216:
112 case WICBitmapPaletteTypeFixedHalftone252:
113 case WICBitmapPaletteTypeFixedHalftone256:
114 palette->Flags = PaletteFlagsHalftone;
115 break;
116 default:
117 palette->Flags = 0;
119 IWICPalette_HasAlpha(wic_palette, &alpha);
120 if(alpha)
121 palette->Flags |= PaletteFlagsHasAlpha;
123 IWICPalette_Release(wic_palette);
125 IWICImagingFactory_Release(factory);
126 return palette;
129 static HRESULT set_palette(IWICBitmapFrameEncode *frame, ColorPalette *palette)
131 HRESULT hr;
132 IWICImagingFactory *factory;
133 IWICPalette *wic_palette;
135 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
136 if (FAILED(hr))
137 return hr;
139 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
140 IWICImagingFactory_Release(factory);
141 if (SUCCEEDED(hr))
143 hr = IWICPalette_InitializeCustom(wic_palette, palette->Entries, palette->Count);
145 if (SUCCEEDED(hr))
146 hr = IWICBitmapFrameEncode_SetPalette(frame, wic_palette);
148 IWICPalette_Release(wic_palette);
151 return hr;
154 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
155 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
157 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
159 * Note: According to Jose Roca's GDI+ docs, this function is not
160 * implemented in Windows's GDI+.
162 return NotImplemented;
165 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
166 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
167 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
169 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
171 * Note: According to Jose Roca's GDI+ docs, this function is not
172 * implemented in Windows's GDI+.
174 return NotImplemented;
177 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
179 *index = (row[x/8]>>(7-x%8)) & 1;
182 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
184 if (x & 1)
185 *index = row[x/2]&0xf;
186 else
187 *index = row[x/2]>>4;
190 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
192 *index = row[x];
195 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
196 const BYTE *row, UINT x)
198 *r = *g = *b = row[x*2+1];
199 *a = 255;
202 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
203 const BYTE *row, UINT x)
205 WORD pixel = *((const WORD*)(row)+x);
206 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
207 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
208 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
209 *a = 255;
212 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
213 const BYTE *row, UINT x)
215 WORD pixel = *((const WORD*)(row)+x);
216 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
217 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
218 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
219 *a = 255;
222 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
223 const BYTE *row, UINT x)
225 WORD pixel = *((const WORD*)(row)+x);
226 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
227 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
228 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
229 if ((pixel&0x8000) == 0x8000)
230 *a = 255;
231 else
232 *a = 0;
235 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
236 const BYTE *row, UINT x)
238 *r = row[x*3+2];
239 *g = row[x*3+1];
240 *b = row[x*3];
241 *a = 255;
244 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
245 const BYTE *row, UINT x)
247 *r = row[x*4+2];
248 *g = row[x*4+1];
249 *b = row[x*4];
250 *a = 255;
253 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
254 const BYTE *row, UINT x)
256 *r = row[x*4+2];
257 *g = row[x*4+1];
258 *b = row[x*4];
259 *a = row[x*4+3];
262 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
263 const BYTE *row, UINT x)
265 *a = row[x*4+3];
266 if (*a == 0)
268 *r = row[x*4+2];
269 *g = row[x*4+1];
270 *b = row[x*4];
272 else
274 DWORD scaled_q = (255 << 15) / *a;
275 *r = (row[x*4+2] > *a) ? 0xff : (row[x*4+2] * scaled_q) >> 15;
276 *g = (row[x*4+1] > *a) ? 0xff : (row[x*4+1] * scaled_q) >> 15;
277 *b = (row[x*4] > *a) ? 0xff : (row[x*4] * scaled_q) >> 15;
281 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
282 const BYTE *row, UINT x)
284 *r = row[x*6+5];
285 *g = row[x*6+3];
286 *b = row[x*6+1];
287 *a = 255;
290 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
291 const BYTE *row, UINT x)
293 *r = row[x*8+5];
294 *g = row[x*8+3];
295 *b = row[x*8+1];
296 *a = row[x*8+7];
299 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
300 const BYTE *row, UINT x)
302 *a = row[x*8+7];
303 if (*a == 0)
304 *r = *g = *b = 0;
305 else
307 *r = row[x*8+5] * 255 / *a;
308 *g = row[x*8+3] * 255 / *a;
309 *b = row[x*8+1] * 255 / *a;
313 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
314 ARGB *color)
316 BYTE r, g, b, a;
317 BYTE index;
318 BYTE *row;
320 if(!bitmap || !color ||
321 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
322 return InvalidParameter;
324 row = bitmap->bits+bitmap->stride*y;
326 switch (bitmap->format)
328 case PixelFormat1bppIndexed:
329 getpixel_1bppIndexed(&index,row,x);
330 break;
331 case PixelFormat4bppIndexed:
332 getpixel_4bppIndexed(&index,row,x);
333 break;
334 case PixelFormat8bppIndexed:
335 getpixel_8bppIndexed(&index,row,x);
336 break;
337 case PixelFormat16bppGrayScale:
338 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
339 break;
340 case PixelFormat16bppRGB555:
341 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
342 break;
343 case PixelFormat16bppRGB565:
344 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
345 break;
346 case PixelFormat16bppARGB1555:
347 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
348 break;
349 case PixelFormat24bppRGB:
350 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
351 break;
352 case PixelFormat32bppRGB:
353 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
354 break;
355 case PixelFormat32bppARGB:
356 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
357 break;
358 case PixelFormat32bppPARGB:
359 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
360 break;
361 case PixelFormat48bppRGB:
362 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
363 break;
364 case PixelFormat64bppARGB:
365 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
366 break;
367 case PixelFormat64bppPARGB:
368 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
369 break;
370 default:
371 FIXME("not implemented for format 0x%x\n", bitmap->format);
372 return NotImplemented;
375 if (bitmap->format & PixelFormatIndexed)
376 *color = bitmap->image.palette->Entries[index];
377 else
378 *color = a<<24|r<<16|g<<8|b;
380 return Ok;
383 static unsigned int absdiff(unsigned int x, unsigned int y)
385 return x > y ? x - y : y - x;
388 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
390 BYTE index = 0;
391 int best_distance = 0x7fff;
392 int distance;
393 UINT i;
395 if (!palette) return 0;
396 /* This algorithm scans entire palette,
397 computes difference from desired color (all color components have equal weight)
398 and returns the index of color with least difference.
400 Note: Maybe it could be replaced with a better algorithm for better image quality
401 and performance, though better algorithm would probably need some pre-built lookup
402 tables and thus may actually be slower if this method is called only few times per
403 every image.
405 for(i=0;i<palette->Count;i++) {
406 ARGB color=palette->Entries[i];
407 distance=absdiff(b, color & 0xff) + absdiff(g, color>>8 & 0xff) + absdiff(r, color>>16 & 0xff) + absdiff(a, color>>24 & 0xff);
408 if (distance<best_distance) {
409 best_distance=distance;
410 index=i;
413 return index;
416 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
417 BYTE *row, UINT x, ColorPalette *palette)
419 BYTE index = get_palette_index(r,g,b,a,palette);
420 row[x]=index;
423 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
424 BYTE *row, UINT x, ColorPalette *palette)
426 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
429 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
430 BYTE *row, UINT x, ColorPalette *palette)
432 if (x & 1)
433 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
434 else
435 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
438 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
439 BYTE *row, UINT x)
441 *((WORD*)(row)+x) = (r+g+b)*85;
444 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
445 BYTE *row, UINT x)
447 *((WORD*)(row)+x) = (r<<7&0x7c00)|
448 (g<<2&0x03e0)|
449 (b>>3&0x001f);
452 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
453 BYTE *row, UINT x)
455 *((WORD*)(row)+x) = (r<<8&0xf800)|
456 (g<<3&0x07e0)|
457 (b>>3&0x001f);
460 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
461 BYTE *row, UINT x)
463 *((WORD*)(row)+x) = (a<<8&0x8000)|
464 (r<<7&0x7c00)|
465 (g<<2&0x03e0)|
466 (b>>3&0x001f);
469 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
470 BYTE *row, UINT x)
472 row[x*3+2] = r;
473 row[x*3+1] = g;
474 row[x*3] = b;
477 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
478 BYTE *row, UINT x)
480 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
483 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
484 BYTE *row, UINT x)
486 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
489 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
490 BYTE *row, UINT x)
492 r = (r * a + 127) / 255;
493 g = (g * a + 127) / 255;
494 b = (b * a + 127) / 255;
495 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
498 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
499 BYTE *row, UINT x)
501 row[x*6+5] = row[x*6+4] = r;
502 row[x*6+3] = row[x*6+2] = g;
503 row[x*6+1] = row[x*6] = b;
506 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
507 BYTE *row, UINT x)
509 UINT64 a64=a, r64=r, g64=g, b64=b;
510 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
513 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
514 BYTE *row, UINT x)
516 UINT64 a64, r64, g64, b64;
517 a64 = a * 257;
518 r64 = r * a / 255;
519 g64 = g * a / 255;
520 b64 = b * a / 255;
521 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
524 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
525 ARGB color)
527 BYTE a, r, g, b;
528 BYTE *row;
530 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
531 return InvalidParameter;
533 a = color>>24;
534 r = color>>16;
535 g = color>>8;
536 b = color;
538 row = bitmap->bits + bitmap->stride * y;
540 switch (bitmap->format)
542 case PixelFormat16bppGrayScale:
543 setpixel_16bppGrayScale(r,g,b,a,row,x);
544 break;
545 case PixelFormat16bppRGB555:
546 setpixel_16bppRGB555(r,g,b,a,row,x);
547 break;
548 case PixelFormat16bppRGB565:
549 setpixel_16bppRGB565(r,g,b,a,row,x);
550 break;
551 case PixelFormat16bppARGB1555:
552 setpixel_16bppARGB1555(r,g,b,a,row,x);
553 break;
554 case PixelFormat24bppRGB:
555 setpixel_24bppRGB(r,g,b,a,row,x);
556 break;
557 case PixelFormat32bppRGB:
558 setpixel_32bppRGB(r,g,b,a,row,x);
559 break;
560 case PixelFormat32bppARGB:
561 setpixel_32bppARGB(r,g,b,a,row,x);
562 break;
563 case PixelFormat32bppPARGB:
564 setpixel_32bppPARGB(r,g,b,a,row,x);
565 break;
566 case PixelFormat48bppRGB:
567 setpixel_48bppRGB(r,g,b,a,row,x);
568 break;
569 case PixelFormat64bppARGB:
570 setpixel_64bppARGB(r,g,b,a,row,x);
571 break;
572 case PixelFormat64bppPARGB:
573 setpixel_64bppPARGB(r,g,b,a,row,x);
574 break;
575 case PixelFormat8bppIndexed:
576 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
577 break;
578 case PixelFormat4bppIndexed:
579 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
580 break;
581 case PixelFormat1bppIndexed:
582 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
583 break;
584 default:
585 FIXME("not implemented for format 0x%x\n", bitmap->format);
586 return NotImplemented;
589 return Ok;
592 GpStatus convert_pixels(INT width, INT height,
593 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
594 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
595 ColorPalette *palette)
597 INT x, y;
599 if (src_format == dst_format ||
600 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
602 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
603 for (y=0; y<height; y++)
604 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
605 return Ok;
608 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
609 for (y=0; y<height; y++) \
610 for (x=0; x<width; x++) { \
611 BYTE index; \
612 ARGB argb; \
613 BYTE *color = (BYTE *)&argb; \
614 getpixel_function(&index, src_bits+src_stride*y, x); \
615 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
616 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
618 return Ok; \
619 } while (0);
621 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
622 for (y=0; y<height; y++) \
623 for (x=0; x<width; x++) { \
624 BYTE r, g, b, a; \
625 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
626 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
628 return Ok; \
629 } while (0);
631 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
632 for (y=0; y<height; y++) \
633 for (x=0; x<width; x++) { \
634 BYTE r, g, b, a; \
635 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
636 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
638 return Ok; \
639 } while (0);
641 switch (src_format)
643 case PixelFormat1bppIndexed:
644 switch (dst_format)
646 case PixelFormat16bppGrayScale:
647 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
648 case PixelFormat16bppRGB555:
649 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
650 case PixelFormat16bppRGB565:
651 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
652 case PixelFormat16bppARGB1555:
653 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
654 case PixelFormat24bppRGB:
655 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
656 case PixelFormat32bppRGB:
657 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
658 case PixelFormat32bppARGB:
659 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
660 case PixelFormat32bppPARGB:
661 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
662 case PixelFormat48bppRGB:
663 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
664 case PixelFormat64bppARGB:
665 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
666 default:
667 break;
669 break;
670 case PixelFormat4bppIndexed:
671 switch (dst_format)
673 case PixelFormat16bppGrayScale:
674 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
675 case PixelFormat16bppRGB555:
676 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
677 case PixelFormat16bppRGB565:
678 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
679 case PixelFormat16bppARGB1555:
680 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
681 case PixelFormat24bppRGB:
682 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
683 case PixelFormat32bppRGB:
684 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
685 case PixelFormat32bppARGB:
686 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
687 case PixelFormat32bppPARGB:
688 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
689 case PixelFormat48bppRGB:
690 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
691 case PixelFormat64bppARGB:
692 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
693 default:
694 break;
696 break;
697 case PixelFormat8bppIndexed:
698 switch (dst_format)
700 case PixelFormat16bppGrayScale:
701 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
702 case PixelFormat16bppRGB555:
703 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
704 case PixelFormat16bppRGB565:
705 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
706 case PixelFormat16bppARGB1555:
707 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
708 case PixelFormat24bppRGB:
709 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
710 case PixelFormat32bppRGB:
711 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
712 case PixelFormat32bppARGB:
713 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
714 case PixelFormat32bppPARGB:
715 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
716 case PixelFormat48bppRGB:
717 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
718 case PixelFormat64bppARGB:
719 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
720 default:
721 break;
723 break;
724 case PixelFormat16bppGrayScale:
725 switch (dst_format)
727 case PixelFormat1bppIndexed:
728 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
729 case PixelFormat8bppIndexed:
730 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
731 case PixelFormat16bppRGB555:
732 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
733 case PixelFormat16bppRGB565:
734 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
735 case PixelFormat16bppARGB1555:
736 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
737 case PixelFormat24bppRGB:
738 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
739 case PixelFormat32bppRGB:
740 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
741 case PixelFormat32bppARGB:
742 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
743 case PixelFormat32bppPARGB:
744 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
745 case PixelFormat48bppRGB:
746 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
747 case PixelFormat64bppARGB:
748 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
749 default:
750 break;
752 break;
753 case PixelFormat16bppRGB555:
754 switch (dst_format)
756 case PixelFormat1bppIndexed:
757 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
758 case PixelFormat8bppIndexed:
759 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
760 case PixelFormat16bppGrayScale:
761 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
762 case PixelFormat16bppRGB565:
763 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
764 case PixelFormat16bppARGB1555:
765 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
766 case PixelFormat24bppRGB:
767 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
768 case PixelFormat32bppRGB:
769 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
770 case PixelFormat32bppARGB:
771 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
772 case PixelFormat32bppPARGB:
773 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
774 case PixelFormat48bppRGB:
775 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
776 case PixelFormat64bppARGB:
777 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
778 default:
779 break;
781 break;
782 case PixelFormat16bppRGB565:
783 switch (dst_format)
785 case PixelFormat1bppIndexed:
786 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
787 case PixelFormat8bppIndexed:
788 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
789 case PixelFormat16bppGrayScale:
790 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
791 case PixelFormat16bppRGB555:
792 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
793 case PixelFormat16bppARGB1555:
794 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
795 case PixelFormat24bppRGB:
796 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
797 case PixelFormat32bppRGB:
798 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
799 case PixelFormat32bppARGB:
800 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
801 case PixelFormat32bppPARGB:
802 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
803 case PixelFormat48bppRGB:
804 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
805 case PixelFormat64bppARGB:
806 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
807 default:
808 break;
810 break;
811 case PixelFormat16bppARGB1555:
812 switch (dst_format)
814 case PixelFormat1bppIndexed:
815 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
816 case PixelFormat8bppIndexed:
817 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
818 case PixelFormat16bppGrayScale:
819 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
820 case PixelFormat16bppRGB555:
821 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
822 case PixelFormat16bppRGB565:
823 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
824 case PixelFormat24bppRGB:
825 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
826 case PixelFormat32bppRGB:
827 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
828 case PixelFormat32bppARGB:
829 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
830 case PixelFormat32bppPARGB:
831 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
832 case PixelFormat48bppRGB:
833 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
834 case PixelFormat64bppARGB:
835 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
836 default:
837 break;
839 break;
840 case PixelFormat24bppRGB:
841 switch (dst_format)
843 case PixelFormat1bppIndexed:
844 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
845 case PixelFormat8bppIndexed:
846 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
847 case PixelFormat16bppGrayScale:
848 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
849 case PixelFormat16bppRGB555:
850 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
851 case PixelFormat16bppRGB565:
852 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
853 case PixelFormat16bppARGB1555:
854 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
855 case PixelFormat32bppRGB:
856 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
857 case PixelFormat32bppARGB:
858 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
859 case PixelFormat32bppPARGB:
860 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
861 case PixelFormat48bppRGB:
862 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
863 case PixelFormat64bppARGB:
864 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
865 default:
866 break;
868 break;
869 case PixelFormat32bppRGB:
870 switch (dst_format)
872 case PixelFormat1bppIndexed:
873 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
874 case PixelFormat8bppIndexed:
875 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
876 case PixelFormat16bppGrayScale:
877 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
878 case PixelFormat16bppRGB555:
879 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
880 case PixelFormat16bppRGB565:
881 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
882 case PixelFormat16bppARGB1555:
883 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
884 case PixelFormat24bppRGB:
885 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
886 case PixelFormat32bppARGB:
887 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
888 case PixelFormat32bppPARGB:
889 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
890 case PixelFormat48bppRGB:
891 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
892 case PixelFormat64bppARGB:
893 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
894 default:
895 break;
897 break;
898 case PixelFormat32bppARGB:
899 switch (dst_format)
901 case PixelFormat1bppIndexed:
902 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
903 case PixelFormat8bppIndexed:
904 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
905 case PixelFormat16bppGrayScale:
906 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
907 case PixelFormat16bppRGB555:
908 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
909 case PixelFormat16bppRGB565:
910 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
911 case PixelFormat16bppARGB1555:
912 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
913 case PixelFormat24bppRGB:
914 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
915 case PixelFormat32bppPARGB:
916 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
917 return Ok;
918 case PixelFormat48bppRGB:
919 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
920 case PixelFormat64bppARGB:
921 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
922 default:
923 break;
925 break;
926 case PixelFormat32bppPARGB:
927 switch (dst_format)
929 case PixelFormat1bppIndexed:
930 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
931 case PixelFormat8bppIndexed:
932 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
933 case PixelFormat16bppGrayScale:
934 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
935 case PixelFormat16bppRGB555:
936 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
937 case PixelFormat16bppRGB565:
938 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
939 case PixelFormat16bppARGB1555:
940 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
941 case PixelFormat24bppRGB:
942 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
943 case PixelFormat32bppRGB:
944 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
945 case PixelFormat32bppARGB:
946 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
947 case PixelFormat48bppRGB:
948 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
949 case PixelFormat64bppARGB:
950 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
951 default:
952 break;
954 break;
955 case PixelFormat48bppRGB:
956 switch (dst_format)
958 case PixelFormat1bppIndexed:
959 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
960 case PixelFormat8bppIndexed:
961 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
962 case PixelFormat16bppGrayScale:
963 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
964 case PixelFormat16bppRGB555:
965 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
966 case PixelFormat16bppRGB565:
967 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
968 case PixelFormat16bppARGB1555:
969 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
970 case PixelFormat24bppRGB:
971 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
972 case PixelFormat32bppRGB:
973 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
974 case PixelFormat32bppARGB:
975 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
976 case PixelFormat32bppPARGB:
977 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
978 case PixelFormat64bppARGB:
979 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
980 default:
981 break;
983 break;
984 case PixelFormat64bppARGB:
985 switch (dst_format)
987 case PixelFormat1bppIndexed:
988 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
989 case PixelFormat8bppIndexed:
990 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
991 case PixelFormat16bppGrayScale:
992 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
993 case PixelFormat16bppRGB555:
994 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
995 case PixelFormat16bppRGB565:
996 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
997 case PixelFormat16bppARGB1555:
998 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
999 case PixelFormat24bppRGB:
1000 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
1001 case PixelFormat32bppRGB:
1002 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
1003 case PixelFormat32bppARGB:
1004 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
1005 case PixelFormat32bppPARGB:
1006 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
1007 case PixelFormat48bppRGB:
1008 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
1009 default:
1010 break;
1012 break;
1013 case PixelFormat64bppPARGB:
1014 switch (dst_format)
1016 case PixelFormat1bppIndexed:
1017 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
1018 case PixelFormat8bppIndexed:
1019 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
1020 case PixelFormat16bppGrayScale:
1021 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
1022 case PixelFormat16bppRGB555:
1023 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
1024 case PixelFormat16bppRGB565:
1025 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
1026 case PixelFormat16bppARGB1555:
1027 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
1028 case PixelFormat24bppRGB:
1029 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
1030 case PixelFormat32bppRGB:
1031 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
1032 case PixelFormat32bppARGB:
1033 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
1034 case PixelFormat32bppPARGB:
1035 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1036 case PixelFormat48bppRGB:
1037 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1038 case PixelFormat64bppARGB:
1039 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1040 default:
1041 break;
1043 break;
1044 default:
1045 break;
1048 #undef convert_indexed_to_rgb
1049 #undef convert_rgb_to_rgb
1051 return NotImplemented;
1054 /* This function returns a pointer to an array of pixels that represents the
1055 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1056 * flags. It is correct behavior that a user who calls this function with write
1057 * privileges can write to the whole bitmap (not just the area in rect).
1059 * FIXME: only used portion of format is bits per pixel. */
1060 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1061 UINT flags, PixelFormat format, BitmapData* lockeddata)
1063 INT bitspp = PIXELFORMATBPP(format);
1064 GpRect act_rect; /* actual rect to be used */
1065 GpStatus stat;
1066 BOOL unlock;
1068 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1070 if(!lockeddata || !bitmap)
1071 return InvalidParameter;
1072 if(!image_lock(&bitmap->image, &unlock))
1073 return ObjectBusy;
1075 if(rect){
1076 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1077 (rect->Y + rect->Height > bitmap->height) || !flags)
1079 image_unlock(&bitmap->image, unlock);
1080 return InvalidParameter;
1083 act_rect = *rect;
1085 else{
1086 act_rect.X = act_rect.Y = 0;
1087 act_rect.Width = bitmap->width;
1088 act_rect.Height = bitmap->height;
1091 if(bitmap->lockmode)
1093 WARN("bitmap is already locked and cannot be locked again\n");
1094 image_unlock(&bitmap->image, unlock);
1095 return WrongState;
1098 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1100 /* no conversion is necessary; just use the bits directly */
1101 lockeddata->Width = act_rect.Width;
1102 lockeddata->Height = act_rect.Height;
1103 lockeddata->PixelFormat = format;
1104 lockeddata->Reserved = flags;
1105 lockeddata->Stride = bitmap->stride;
1106 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1107 bitmap->stride * act_rect.Y;
1109 bitmap->lockmode = flags | ImageLockModeRead;
1111 image_unlock(&bitmap->image, unlock);
1112 return Ok;
1115 /* Make sure we can convert to the requested format. */
1116 if (flags & ImageLockModeRead)
1118 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
1119 if (stat == NotImplemented)
1121 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1122 image_unlock(&bitmap->image, unlock);
1123 return NotImplemented;
1127 /* If we're opening for writing, make sure we'll be able to write back in
1128 * the original format. */
1129 if (flags & ImageLockModeWrite)
1131 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
1132 if (stat == NotImplemented)
1134 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1135 image_unlock(&bitmap->image, unlock);
1136 return NotImplemented;
1140 lockeddata->Width = act_rect.Width;
1141 lockeddata->Height = act_rect.Height;
1142 lockeddata->PixelFormat = format;
1143 lockeddata->Reserved = flags;
1145 if(!(flags & ImageLockModeUserInputBuf))
1147 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1149 bitmap->bitmapbits = heap_alloc_zero(lockeddata->Stride * act_rect.Height);
1151 if (!bitmap->bitmapbits)
1153 image_unlock(&bitmap->image, unlock);
1154 return OutOfMemory;
1157 lockeddata->Scan0 = bitmap->bitmapbits;
1160 if (flags & ImageLockModeRead)
1162 static BOOL fixme = FALSE;
1164 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1166 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1167 fixme = TRUE;
1170 stat = convert_pixels(act_rect.Width, act_rect.Height,
1171 lockeddata->Stride, lockeddata->Scan0, format,
1172 bitmap->stride,
1173 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1174 bitmap->format, bitmap->image.palette);
1176 if (stat != Ok)
1178 heap_free(bitmap->bitmapbits);
1179 bitmap->bitmapbits = NULL;
1180 image_unlock(&bitmap->image, unlock);
1181 return stat;
1185 bitmap->lockmode = flags | ImageLockModeRead;
1186 bitmap->lockx = act_rect.X;
1187 bitmap->locky = act_rect.Y;
1189 image_unlock(&bitmap->image, unlock);
1190 return Ok;
1193 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1195 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1197 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1198 return InvalidParameter;
1200 bitmap->image.xres = xdpi;
1201 bitmap->image.yres = ydpi;
1203 return Ok;
1206 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1207 BitmapData* lockeddata)
1209 GpStatus stat;
1210 static BOOL fixme = FALSE;
1211 BOOL unlock;
1213 TRACE("(%p,%p)\n", bitmap, lockeddata);
1215 if(!bitmap || !lockeddata)
1216 return InvalidParameter;
1217 if(!image_lock(&bitmap->image, &unlock))
1218 return ObjectBusy;
1220 if(!bitmap->lockmode)
1222 image_unlock(&bitmap->image, unlock);
1223 return WrongState;
1226 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1227 bitmap->lockmode = 0;
1228 heap_free(bitmap->bitmapbits);
1229 bitmap->bitmapbits = NULL;
1230 image_unlock(&bitmap->image, unlock);
1231 return Ok;
1234 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1236 /* we passed a direct reference; no need to do anything */
1237 bitmap->lockmode = 0;
1238 image_unlock(&bitmap->image, unlock);
1239 return Ok;
1242 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1244 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1245 fixme = TRUE;
1248 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1249 bitmap->stride,
1250 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1251 bitmap->format,
1252 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1254 if (stat != Ok)
1256 ERR("failed to convert pixels; this should never happen\n");
1259 heap_free(bitmap->bitmapbits);
1260 bitmap->bitmapbits = NULL;
1261 bitmap->lockmode = 0;
1263 image_unlock(&bitmap->image, unlock);
1264 return stat;
1267 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1268 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1270 Rect area;
1271 GpStatus stat;
1273 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1275 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1276 x < 0 || y < 0 ||
1277 x + width > srcBitmap->width || y + height > srcBitmap->height)
1279 TRACE("<-- InvalidParameter\n");
1280 return InvalidParameter;
1283 if (format == PixelFormatDontCare)
1284 format = srcBitmap->format;
1286 area.X = gdip_round(x);
1287 area.Y = gdip_round(y);
1288 area.Width = gdip_round(width);
1289 area.Height = gdip_round(height);
1291 stat = GdipCreateBitmapFromScan0(area.Width, area.Height, 0, format, NULL, dstBitmap);
1292 if (stat == Ok)
1294 stat = convert_pixels(area.Width, area.Height, (*dstBitmap)->stride, (*dstBitmap)->bits, (*dstBitmap)->format,
1295 srcBitmap->stride,
1296 srcBitmap->bits + srcBitmap->stride * area.Y + PIXELFORMATBPP(srcBitmap->format) * area.X / 8,
1297 srcBitmap->format, srcBitmap->image.palette);
1299 if (stat == Ok && srcBitmap->image.palette)
1301 ColorPalette *src_palette, *dst_palette;
1303 src_palette = srcBitmap->image.palette;
1305 dst_palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * src_palette->Count);
1307 if (dst_palette)
1309 dst_palette->Flags = src_palette->Flags;
1310 dst_palette->Count = src_palette->Count;
1311 memcpy(dst_palette->Entries, src_palette->Entries, sizeof(ARGB) * src_palette->Count);
1313 heap_free((*dstBitmap)->image.palette);
1314 (*dstBitmap)->image.palette = dst_palette;
1316 else
1317 stat = OutOfMemory;
1320 if (stat != Ok)
1321 GdipDisposeImage(&(*dstBitmap)->image);
1324 if (stat != Ok)
1325 *dstBitmap = NULL;
1327 return stat;
1330 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1331 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1333 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1335 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1338 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1340 TRACE("%p, %p\n", image, cloneImage);
1342 if (!image || !cloneImage)
1343 return InvalidParameter;
1345 if (image->type == ImageTypeBitmap)
1347 GpBitmap *bitmap = (GpBitmap *)image;
1349 return GdipCloneBitmapAreaI(0, 0, bitmap->width, bitmap->height,
1350 bitmap->format, bitmap, (GpBitmap **)cloneImage);
1352 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
1354 GpMetafile *result, *metafile;
1356 metafile = (GpMetafile*)image;
1358 result = heap_alloc_zero(sizeof(*result));
1359 if (!result)
1360 return OutOfMemory;
1362 result->image.type = ImageTypeMetafile;
1363 result->image.format = image->format;
1364 result->image.flags = image->flags;
1365 result->image.frame_count = 1;
1366 result->image.xres = image->xres;
1367 result->image.yres = image->yres;
1368 result->bounds = metafile->bounds;
1369 result->unit = metafile->unit;
1370 result->metafile_type = metafile->metafile_type;
1371 result->hemf = CopyEnhMetaFileW(metafile->hemf, NULL);
1372 list_init(&result->containers);
1374 if (!result->hemf)
1376 heap_free(result);
1377 return OutOfMemory;
1380 *cloneImage = &result->image;
1381 return Ok;
1383 else
1385 WARN("GpImage with no image data (metafile in wrong state?)\n");
1386 return InvalidParameter;
1390 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1391 GpBitmap **bitmap)
1393 GpStatus stat;
1394 IStream *stream;
1396 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1398 if(!filename || !bitmap)
1399 return InvalidParameter;
1401 *bitmap = NULL;
1403 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1405 if(stat != Ok)
1406 return stat;
1408 stat = GdipCreateBitmapFromStream(stream, bitmap);
1410 IStream_Release(stream);
1412 return stat;
1415 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1416 VOID *bits, GpBitmap **bitmap)
1418 DWORD height, stride;
1419 HBITMAP hbm;
1420 void *bmbits;
1421 GpStatus status;
1423 TRACE("(%p, %p, %p)\n", info, bits, bitmap);
1425 if (!info || !bits || !bitmap)
1426 return InvalidParameter;
1428 hbm = CreateDIBSection(0, info, DIB_RGB_COLORS, &bmbits, NULL, 0);
1429 if (!hbm)
1430 return InvalidParameter;
1432 height = abs(info->bmiHeader.biHeight);
1433 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1434 TRACE("height %u, stride %u, image size %u\n", height, stride, height * stride);
1436 memcpy(bmbits, bits, height * stride);
1438 status = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1439 DeleteObject(hbm);
1441 return status;
1444 /* FIXME: no icm */
1445 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1446 GpBitmap **bitmap)
1448 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1450 return GdipCreateBitmapFromFile(filename, bitmap);
1453 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1454 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1456 HBITMAP hbm;
1457 GpStatus stat = InvalidParameter;
1459 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1461 if(!lpBitmapName || !bitmap)
1462 return InvalidParameter;
1464 /* load DIB */
1465 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1466 LR_CREATEDIBSECTION);
1468 if(hbm){
1469 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1470 DeleteObject(hbm);
1473 return stat;
1476 static inline DWORD blend_argb_no_bkgnd_alpha(DWORD src, DWORD bkgnd)
1478 BYTE b = (BYTE)src;
1479 BYTE g = (BYTE)(src >> 8);
1480 BYTE r = (BYTE)(src >> 16);
1481 DWORD alpha = (BYTE)(src >> 24);
1482 return ((b + ((BYTE)bkgnd * (255 - alpha) + 127) / 255) |
1483 (g + ((BYTE)(bkgnd >> 8) * (255 - alpha) + 127) / 255) << 8 |
1484 (r + ((BYTE)(bkgnd >> 16) * (255 - alpha) + 127) / 255) << 16 |
1485 (alpha << 24));
1488 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1489 HBITMAP* hbmReturn, ARGB background)
1491 GpStatus stat;
1492 HBITMAP result;
1493 UINT width, height;
1494 BITMAPINFOHEADER bih;
1495 LPBYTE bits;
1496 BOOL unlock;
1498 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1500 if (!bitmap || !hbmReturn) return InvalidParameter;
1501 if (!image_lock(&bitmap->image, &unlock)) return ObjectBusy;
1503 GdipGetImageWidth(&bitmap->image, &width);
1504 GdipGetImageHeight(&bitmap->image, &height);
1506 bih.biSize = sizeof(bih);
1507 bih.biWidth = width;
1508 bih.biHeight = height;
1509 bih.biPlanes = 1;
1510 bih.biBitCount = 32;
1511 bih.biCompression = BI_RGB;
1512 bih.biSizeImage = 0;
1513 bih.biXPelsPerMeter = 0;
1514 bih.biYPelsPerMeter = 0;
1515 bih.biClrUsed = 0;
1516 bih.biClrImportant = 0;
1518 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1519 if (!result)
1521 image_unlock(&bitmap->image, unlock);
1522 return GenericError;
1525 stat = convert_pixels(width, height, -width*4,
1526 bits + (width * 4 * (height - 1)), PixelFormat32bppPARGB,
1527 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette);
1528 if (stat != Ok)
1530 DeleteObject(result);
1531 image_unlock(&bitmap->image, unlock);
1532 return stat;
1535 if (background & 0xffffff)
1537 DWORD *ptr;
1538 UINT i;
1539 for (ptr = (DWORD*)bits, i = 0; i < width * height; ptr++, i++)
1541 if ((*ptr & 0xff000000) == 0xff000000) continue;
1542 *ptr = blend_argb_no_bkgnd_alpha(*ptr, background);
1546 *hbmReturn = result;
1547 image_unlock(&bitmap->image, unlock);
1548 return Ok;
1551 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1552 GpGraphics* target, GpBitmap** bitmap)
1554 GpStatus ret;
1556 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1558 if(!target || !bitmap)
1559 return InvalidParameter;
1561 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
1562 NULL, bitmap);
1564 if (ret == Ok)
1566 GdipGetDpiX(target, &(*bitmap)->image.xres);
1567 GdipGetDpiY(target, &(*bitmap)->image.yres);
1570 return ret;
1573 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1575 GpStatus stat;
1576 ICONINFO iinfo;
1577 BITMAP bm;
1578 int ret;
1579 UINT width, height, stride;
1580 GpRect rect;
1581 BitmapData lockeddata;
1582 HDC screendc;
1583 BOOL has_alpha;
1584 int x, y;
1585 BITMAPINFOHEADER bih;
1586 DWORD *src;
1587 BYTE *dst_row;
1588 DWORD *dst;
1590 TRACE("%p, %p\n", hicon, bitmap);
1592 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1593 return InvalidParameter;
1595 /* get the size of the icon */
1596 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1597 if (ret == 0) {
1598 DeleteObject(iinfo.hbmColor);
1599 DeleteObject(iinfo.hbmMask);
1600 return GenericError;
1603 width = bm.bmWidth;
1604 height = iinfo.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
1605 stride = width * 4;
1607 stat = GdipCreateBitmapFromScan0(width, height, stride, PixelFormat32bppARGB, NULL, bitmap);
1608 if (stat != Ok) {
1609 DeleteObject(iinfo.hbmColor);
1610 DeleteObject(iinfo.hbmMask);
1611 return stat;
1614 rect.X = 0;
1615 rect.Y = 0;
1616 rect.Width = width;
1617 rect.Height = height;
1619 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1620 if (stat != Ok) {
1621 DeleteObject(iinfo.hbmColor);
1622 DeleteObject(iinfo.hbmMask);
1623 GdipDisposeImage(&(*bitmap)->image);
1624 return stat;
1627 bih.biSize = sizeof(bih);
1628 bih.biWidth = width;
1629 bih.biHeight = iinfo.hbmColor ? -height: -height * 2;
1630 bih.biPlanes = 1;
1631 bih.biBitCount = 32;
1632 bih.biCompression = BI_RGB;
1633 bih.biSizeImage = 0;
1634 bih.biXPelsPerMeter = 0;
1635 bih.biYPelsPerMeter = 0;
1636 bih.biClrUsed = 0;
1637 bih.biClrImportant = 0;
1639 screendc = CreateCompatibleDC(0);
1640 if (iinfo.hbmColor)
1642 GetDIBits(screendc, iinfo.hbmColor, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1644 if (bm.bmBitsPixel == 32)
1646 has_alpha = FALSE;
1648 /* If any pixel has a non-zero alpha, ignore hbmMask */
1649 src = (DWORD*)lockeddata.Scan0;
1650 for (x=0; x<width && !has_alpha; x++)
1651 for (y=0; y<height && !has_alpha; y++)
1652 if ((*src++ & 0xff000000) != 0)
1653 has_alpha = TRUE;
1655 else has_alpha = FALSE;
1657 else
1659 GetDIBits(screendc, iinfo.hbmMask, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1660 has_alpha = FALSE;
1663 if (!has_alpha)
1665 if (iinfo.hbmMask)
1667 BYTE *bits = heap_alloc(height * stride);
1669 /* read alpha data from the mask */
1670 if (iinfo.hbmColor)
1671 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1672 else
1673 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1675 src = (DWORD*)bits;
1676 dst_row = lockeddata.Scan0;
1677 for (y=0; y<height; y++)
1679 dst = (DWORD*)dst_row;
1680 for (x=0; x<height; x++)
1682 DWORD src_value = *src++;
1683 if (src_value)
1684 *dst++ = 0;
1685 else
1686 *dst++ |= 0xff000000;
1688 dst_row += lockeddata.Stride;
1691 heap_free(bits);
1693 else
1695 /* set constant alpha of 255 */
1696 dst_row = lockeddata.Scan0;
1697 for (y=0; y<height; y++)
1699 dst = (DWORD*)dst_row;
1700 for (x=0; x<height; x++)
1701 *dst++ |= 0xff000000;
1702 dst_row += lockeddata.Stride;
1707 DeleteDC(screendc);
1709 DeleteObject(iinfo.hbmColor);
1710 DeleteObject(iinfo.hbmMask);
1712 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1714 return Ok;
1717 static void generate_halftone_palette(ARGB *entries, UINT count)
1719 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1720 UINT i;
1722 for (i=0; i<8 && i<count; i++)
1724 entries[i] = 0xff000000;
1725 if (i&1) entries[i] |= 0x800000;
1726 if (i&2) entries[i] |= 0x8000;
1727 if (i&4) entries[i] |= 0x80;
1730 if (8 < count)
1731 entries[i] = 0xffc0c0c0;
1733 for (i=9; i<16 && i<count; i++)
1735 entries[i] = 0xff000000;
1736 if (i&1) entries[i] |= 0xff0000;
1737 if (i&2) entries[i] |= 0xff00;
1738 if (i&4) entries[i] |= 0xff;
1741 for (i=16; i<40 && i<count; i++)
1743 entries[i] = 0;
1746 for (i=40; i<256 && i<count; i++)
1748 entries[i] = 0xff000000;
1749 entries[i] |= halftone_values[(i-40)%6];
1750 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1751 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1755 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1757 HDC screendc = CreateCompatibleDC(0);
1759 if (!screendc) return GenericError;
1761 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1762 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1764 DeleteDC(screendc);
1766 return Ok;
1769 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1770 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1772 HBITMAP hbitmap=NULL;
1773 INT row_size, dib_stride;
1774 BYTE *bits=NULL, *own_bits=NULL;
1775 REAL xres, yres;
1776 GpStatus stat;
1778 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1780 if (!bitmap) return InvalidParameter;
1782 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1783 *bitmap = NULL;
1784 return InvalidParameter;
1787 if(scan0 && !stride)
1788 return InvalidParameter;
1790 stat = get_screen_resolution(&xres, &yres);
1791 if (stat != Ok) return stat;
1793 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1794 dib_stride = (row_size + 3) & ~3;
1796 if(stride == 0)
1797 stride = dib_stride;
1799 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1801 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
1802 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
1804 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1805 pbmi->bmiHeader.biWidth = width;
1806 pbmi->bmiHeader.biHeight = -height;
1807 pbmi->bmiHeader.biPlanes = 1;
1808 /* FIXME: use the rest of the data from format */
1809 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1810 pbmi->bmiHeader.biCompression = BI_RGB;
1811 pbmi->bmiHeader.biSizeImage = 0;
1812 pbmi->bmiHeader.biXPelsPerMeter = 0;
1813 pbmi->bmiHeader.biYPelsPerMeter = 0;
1814 pbmi->bmiHeader.biClrUsed = 0;
1815 pbmi->bmiHeader.biClrImportant = 0;
1817 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1819 if (!hbitmap) return GenericError;
1821 stride = dib_stride;
1823 else
1825 /* Not a GDI format; don't try to make an HBITMAP. */
1826 if (scan0)
1827 bits = scan0;
1828 else
1830 INT size = abs(stride) * height;
1832 own_bits = bits = heap_alloc_zero(size);
1833 if (!own_bits) return OutOfMemory;
1835 if (stride < 0)
1836 bits += stride * (1 - height);
1840 *bitmap = heap_alloc_zero(sizeof(GpBitmap));
1841 if(!*bitmap)
1843 DeleteObject(hbitmap);
1844 heap_free(own_bits);
1845 return OutOfMemory;
1848 (*bitmap)->image.type = ImageTypeBitmap;
1849 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1850 (*bitmap)->image.flags = ImageFlagsNone;
1851 (*bitmap)->image.frame_count = 1;
1852 (*bitmap)->image.current_frame = 0;
1853 (*bitmap)->image.palette = NULL;
1854 (*bitmap)->image.xres = xres;
1855 (*bitmap)->image.yres = yres;
1856 (*bitmap)->width = width;
1857 (*bitmap)->height = height;
1858 (*bitmap)->format = format;
1859 (*bitmap)->image.decoder = NULL;
1860 (*bitmap)->image.encoder = NULL;
1861 (*bitmap)->hbitmap = hbitmap;
1862 (*bitmap)->hdc = NULL;
1863 (*bitmap)->bits = bits;
1864 (*bitmap)->stride = stride;
1865 (*bitmap)->own_bits = own_bits;
1866 (*bitmap)->metadata_reader = NULL;
1867 (*bitmap)->prop_count = 0;
1868 (*bitmap)->prop_item = NULL;
1870 /* set format-related flags */
1871 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1872 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1874 if (format == PixelFormat1bppIndexed ||
1875 format == PixelFormat4bppIndexed ||
1876 format == PixelFormat8bppIndexed)
1878 (*bitmap)->image.palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1880 if (!(*bitmap)->image.palette)
1882 GdipDisposeImage(&(*bitmap)->image);
1883 *bitmap = NULL;
1884 return OutOfMemory;
1887 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1889 if (format == PixelFormat1bppIndexed)
1891 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1892 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1893 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1895 else
1897 if (format == PixelFormat8bppIndexed)
1898 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1900 generate_halftone_palette((*bitmap)->image.palette->Entries,
1901 (*bitmap)->image.palette->Count);
1905 TRACE("<-- %p\n", *bitmap);
1907 return Ok;
1910 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1911 GpBitmap **bitmap)
1913 GpStatus stat;
1915 TRACE("%p %p\n", stream, bitmap);
1917 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1919 if(stat != Ok)
1920 return stat;
1922 if((*bitmap)->image.type != ImageTypeBitmap){
1923 GdipDisposeImage(&(*bitmap)->image);
1924 *bitmap = NULL;
1925 return GenericError; /* FIXME: what error to return? */
1928 return Ok;
1931 /* FIXME: no icm */
1932 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1933 GpBitmap **bitmap)
1935 TRACE("%p %p\n", stream, bitmap);
1937 return GdipCreateBitmapFromStream(stream, bitmap);
1940 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1941 GpCachedBitmap **cachedbmp)
1943 GpStatus stat;
1945 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1947 if(!bitmap || !graphics || !cachedbmp)
1948 return InvalidParameter;
1950 *cachedbmp = heap_alloc_zero(sizeof(GpCachedBitmap));
1951 if(!*cachedbmp)
1952 return OutOfMemory;
1954 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1955 if(stat != Ok){
1956 heap_free(*cachedbmp);
1957 return stat;
1960 return Ok;
1963 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1965 GpStatus stat;
1966 BitmapData lockeddata;
1967 ULONG andstride, xorstride, bitssize;
1968 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1969 UINT x, y;
1971 TRACE("(%p, %p)\n", bitmap, hicon);
1973 if (!bitmap || !hicon)
1974 return InvalidParameter;
1976 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1977 PixelFormat32bppPARGB, &lockeddata);
1978 if (stat == Ok)
1980 andstride = ((lockeddata.Width+31)/32)*4;
1981 xorstride = lockeddata.Width*4;
1982 bitssize = (andstride + xorstride) * lockeddata.Height;
1984 andbits = heap_alloc_zero(bitssize);
1986 if (andbits)
1988 xorbits = andbits + andstride * lockeddata.Height;
1990 for (y=0; y<lockeddata.Height; y++)
1992 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
1994 androw = andbits + andstride * y;
1995 for (x=0; x<lockeddata.Width; x++)
1996 if (srcrow[3+4*x] >= 128)
1997 androw[x/8] |= 1 << (7-x%8);
1999 xorrow = xorbits + xorstride * y;
2000 memcpy(xorrow, srcrow, xorstride);
2003 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
2004 andbits, xorbits);
2006 heap_free(andbits);
2008 else
2009 stat = OutOfMemory;
2011 GdipBitmapUnlockBits(bitmap, &lockeddata);
2014 return stat;
2017 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
2019 TRACE("%p\n", cachedbmp);
2021 if(!cachedbmp)
2022 return InvalidParameter;
2024 GdipDisposeImage(cachedbmp->image);
2025 heap_free(cachedbmp);
2027 return Ok;
2030 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2031 GpCachedBitmap *cachedbmp, INT x, INT y)
2033 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2035 if(!graphics || !cachedbmp)
2036 return InvalidParameter;
2038 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2041 /* Internal utility function: Replace the image data of dst with that of src,
2042 * and free src. */
2043 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2045 assert(src->image.type == ImageTypeBitmap);
2046 assert(dst->image.type == ImageTypeBitmap);
2048 heap_free(dst->bitmapbits);
2049 heap_free(dst->own_bits);
2050 DeleteDC(dst->hdc);
2051 DeleteObject(dst->hbitmap);
2053 if (clobber_palette)
2055 heap_free(dst->image.palette);
2056 dst->image.palette = src->image.palette;
2058 else
2059 heap_free(src->image.palette);
2061 dst->image.xres = src->image.xres;
2062 dst->image.yres = src->image.yres;
2063 dst->width = src->width;
2064 dst->height = src->height;
2065 dst->format = src->format;
2066 dst->hbitmap = src->hbitmap;
2067 dst->hdc = src->hdc;
2068 dst->bits = src->bits;
2069 dst->stride = src->stride;
2070 dst->own_bits = src->own_bits;
2071 if (dst->metadata_reader)
2072 IWICMetadataReader_Release(dst->metadata_reader);
2073 dst->metadata_reader = src->metadata_reader;
2074 heap_free(dst->prop_item);
2075 dst->prop_item = src->prop_item;
2076 dst->prop_count = src->prop_count;
2077 if (dst->image.decoder)
2078 IWICBitmapDecoder_Release(dst->image.decoder);
2079 dst->image.decoder = src->image.decoder;
2080 terminate_encoder_wic(&dst->image); /* terminate active encoder before overwriting with src */
2081 dst->image.encoder = src->image.encoder;
2082 dst->image.frame_count = src->image.frame_count;
2083 dst->image.current_frame = src->image.current_frame;
2084 dst->image.format = src->image.format;
2086 src->image.type = ~0;
2087 heap_free(src);
2090 static GpStatus free_image_data(GpImage *image)
2092 if(!image)
2093 return InvalidParameter;
2095 if (image->type == ImageTypeBitmap)
2097 heap_free(((GpBitmap*)image)->bitmapbits);
2098 heap_free(((GpBitmap*)image)->own_bits);
2099 DeleteDC(((GpBitmap*)image)->hdc);
2100 DeleteObject(((GpBitmap*)image)->hbitmap);
2101 if (((GpBitmap*)image)->metadata_reader)
2102 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2103 heap_free(((GpBitmap*)image)->prop_item);
2105 else if (image->type == ImageTypeMetafile)
2106 METAFILE_Free((GpMetafile *)image);
2107 else
2109 WARN("invalid image: %p\n", image);
2110 return ObjectBusy;
2112 if (image->decoder)
2113 IWICBitmapDecoder_Release(image->decoder);
2114 terminate_encoder_wic(image);
2115 heap_free(image->palette);
2117 return Ok;
2120 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2122 GpStatus status;
2124 TRACE("%p\n", image);
2126 status = free_image_data(image);
2127 if (status != Ok) return status;
2128 image->type = ~0;
2129 heap_free(image);
2131 return Ok;
2134 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2136 static int calls;
2138 TRACE("(%p,%p)\n", image, item);
2140 if(!image || !item)
2141 return InvalidParameter;
2143 if (!(calls++))
2144 FIXME("not implemented\n");
2146 return NotImplemented;
2149 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2151 static int calls;
2153 TRACE("(%p,%p)\n", image, item);
2155 if (!(calls++))
2156 FIXME("not implemented\n");
2158 return NotImplemented;
2161 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2162 GpUnit *srcUnit)
2164 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2166 if(!image || !srcRect || !srcUnit)
2167 return InvalidParameter;
2168 if(image->type == ImageTypeMetafile){
2169 *srcRect = ((GpMetafile*)image)->bounds;
2170 *srcUnit = ((GpMetafile*)image)->unit;
2172 else if(image->type == ImageTypeBitmap){
2173 srcRect->X = srcRect->Y = 0.0;
2174 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2175 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2176 *srcUnit = UnitPixel;
2178 else{
2179 WARN("GpImage with no image data\n");
2180 return InvalidParameter;
2183 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2184 srcRect->Width, srcRect->Height, *srcUnit);
2186 return Ok;
2189 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2190 REAL *height)
2192 TRACE("%p %p %p\n", image, width, height);
2194 if(!image || !height || !width)
2195 return InvalidParameter;
2197 if(image->type == ImageTypeMetafile){
2198 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2199 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2201 else if(image->type == ImageTypeBitmap){
2202 *height = ((GpBitmap*)image)->height;
2203 *width = ((GpBitmap*)image)->width;
2205 else{
2206 WARN("GpImage with no image data\n");
2207 return InvalidParameter;
2210 TRACE("returning (%f, %f)\n", *height, *width);
2211 return Ok;
2214 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2215 GpGraphics **graphics)
2217 HDC hdc;
2218 GpStatus stat;
2220 TRACE("%p %p\n", image, graphics);
2222 if(!image || !graphics)
2223 return InvalidParameter;
2225 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2227 hdc = ((GpBitmap*)image)->hdc;
2229 if(!hdc){
2230 hdc = CreateCompatibleDC(0);
2231 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2232 ((GpBitmap*)image)->hdc = hdc;
2235 stat = GdipCreateFromHDC(hdc, graphics);
2237 if (stat == Ok)
2239 (*graphics)->image = image;
2240 (*graphics)->xres = image->xres;
2241 (*graphics)->yres = image->yres;
2244 else if (image->type == ImageTypeMetafile)
2245 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2246 else
2247 stat = graphics_from_image(image, graphics);
2249 return stat;
2252 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2254 TRACE("%p %p\n", image, height);
2256 if(!image || !height)
2257 return InvalidParameter;
2259 if(image->type == ImageTypeMetafile)
2260 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2261 else if(image->type == ImageTypeBitmap)
2262 *height = ((GpBitmap*)image)->height;
2263 else
2265 WARN("GpImage with no image data\n");
2266 return InvalidParameter;
2269 TRACE("returning %d\n", *height);
2271 return Ok;
2274 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2276 if(!image || !res)
2277 return InvalidParameter;
2279 *res = image->xres;
2281 TRACE("(%p) <-- %0.2f\n", image, *res);
2283 return Ok;
2286 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2288 TRACE("%p %p\n", image, size);
2290 if(!image || !size)
2291 return InvalidParameter;
2293 if (image->type == ImageTypeMetafile)
2295 *size = 0;
2296 return GenericError;
2299 if (!image->palette || image->palette->Count == 0)
2300 *size = sizeof(ColorPalette);
2301 else
2302 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2304 TRACE("<-- %u\n", *size);
2306 return Ok;
2309 /* FIXME: test this function for non-bitmap types */
2310 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2312 TRACE("%p %p\n", image, format);
2314 if(!image || !format)
2315 return InvalidParameter;
2317 if(image->type != ImageTypeBitmap)
2318 *format = PixelFormat24bppRGB;
2319 else
2320 *format = ((GpBitmap*) image)->format;
2322 return Ok;
2325 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2327 TRACE("(%p, %p)\n", image, format);
2329 if(!image || !format)
2330 return InvalidParameter;
2332 memcpy(format, &image->format, sizeof(GUID));
2334 return Ok;
2337 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2339 TRACE("%p %p\n", image, type);
2341 if(!image || !type)
2342 return InvalidParameter;
2344 *type = image->type;
2346 return Ok;
2349 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2351 if(!image || !res)
2352 return InvalidParameter;
2354 *res = image->yres;
2356 TRACE("(%p) <-- %0.2f\n", image, *res);
2358 return Ok;
2361 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2363 TRACE("%p %p\n", image, width);
2365 if(!image || !width)
2366 return InvalidParameter;
2368 if(image->type == ImageTypeMetafile)
2369 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2370 else if(image->type == ImageTypeBitmap)
2371 *width = ((GpBitmap*)image)->width;
2372 else
2374 WARN("GpImage with no image data\n");
2375 return InvalidParameter;
2378 TRACE("returning %d\n", *width);
2380 return Ok;
2383 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2385 TRACE("(%p, %p)\n", image, num);
2387 if (!image || !num) return InvalidParameter;
2389 *num = 0;
2391 if (image->type == ImageTypeBitmap)
2393 if (((GpBitmap *)image)->prop_item)
2395 *num = ((GpBitmap *)image)->prop_count;
2396 return Ok;
2399 if (((GpBitmap *)image)->metadata_reader)
2400 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2403 return Ok;
2406 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2408 HRESULT hr;
2409 IWICMetadataReader *reader;
2410 IWICEnumMetadataItem *enumerator;
2411 UINT prop_count, i, items_returned;
2413 TRACE("(%p, %u, %p)\n", image, num, list);
2415 if (!image || !list) return InvalidParameter;
2417 if (image->type != ImageTypeBitmap)
2419 FIXME("Not implemented for type %d\n", image->type);
2420 return NotImplemented;
2423 if (((GpBitmap *)image)->prop_item)
2425 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2427 for (i = 0; i < num; i++)
2429 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2432 return Ok;
2435 reader = ((GpBitmap *)image)->metadata_reader;
2436 if (!reader)
2438 if (num != 0) return InvalidParameter;
2439 return Ok;
2442 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2443 if (FAILED(hr)) return hresult_to_status(hr);
2445 if (num != prop_count) return InvalidParameter;
2447 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2448 if (FAILED(hr)) return hresult_to_status(hr);
2450 IWICEnumMetadataItem_Reset(enumerator);
2452 for (i = 0; i < num; i++)
2454 PROPVARIANT id;
2456 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2457 if (hr != S_OK) break;
2459 if (id.vt != VT_UI2)
2461 FIXME("not supported propvariant type for id: %u\n", id.vt);
2462 list[i] = 0;
2463 continue;
2465 list[i] = id.u.uiVal;
2468 IWICEnumMetadataItem_Release(enumerator);
2470 return hr == S_OK ? Ok : hresult_to_status(hr);
2473 static UINT propvariant_size(PROPVARIANT *value)
2475 switch (value->vt & ~VT_VECTOR)
2477 case VT_EMPTY:
2478 return 0;
2479 case VT_I1:
2480 case VT_UI1:
2481 if (!(value->vt & VT_VECTOR)) return 1;
2482 return value->u.caub.cElems;
2483 case VT_I2:
2484 case VT_UI2:
2485 if (!(value->vt & VT_VECTOR)) return 2;
2486 return value->u.caui.cElems * 2;
2487 case VT_I4:
2488 case VT_UI4:
2489 case VT_R4:
2490 if (!(value->vt & VT_VECTOR)) return 4;
2491 return value->u.caul.cElems * 4;
2492 case VT_I8:
2493 case VT_UI8:
2494 case VT_R8:
2495 if (!(value->vt & VT_VECTOR)) return 8;
2496 return value->u.cauh.cElems * 8;
2497 case VT_LPSTR:
2498 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2499 case VT_BLOB:
2500 return value->u.blob.cbSize;
2501 default:
2502 FIXME("not supported variant type %d\n", value->vt);
2503 return 0;
2507 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2509 HRESULT hr;
2510 IWICMetadataReader *reader;
2511 PROPVARIANT id, value;
2513 TRACE("(%p,%#x,%p)\n", image, propid, size);
2515 if (!size || !image) return InvalidParameter;
2517 if (image->type != ImageTypeBitmap)
2519 FIXME("Not implemented for type %d\n", image->type);
2520 return NotImplemented;
2523 if (((GpBitmap *)image)->prop_item)
2525 UINT i;
2527 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2529 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2531 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2532 return Ok;
2536 return PropertyNotFound;
2539 reader = ((GpBitmap *)image)->metadata_reader;
2540 if (!reader) return PropertyNotFound;
2542 id.vt = VT_UI2;
2543 id.u.uiVal = propid;
2544 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2545 if (FAILED(hr)) return PropertyNotFound;
2547 *size = propvariant_size(&value);
2548 if (*size) *size += sizeof(PropertyItem);
2549 PropVariantClear(&value);
2551 return Ok;
2554 #ifndef PropertyTagTypeSByte
2555 #define PropertyTagTypeSByte 6
2556 #define PropertyTagTypeSShort 8
2557 #define PropertyTagTypeFloat 11
2558 #define PropertyTagTypeDouble 12
2559 #endif
2561 static UINT vt_to_itemtype(UINT vt)
2563 static const struct
2565 UINT vt, type;
2566 } vt2type[] =
2568 { VT_I1, PropertyTagTypeSByte },
2569 { VT_UI1, PropertyTagTypeByte },
2570 { VT_I2, PropertyTagTypeSShort },
2571 { VT_UI2, PropertyTagTypeShort },
2572 { VT_I4, PropertyTagTypeSLONG },
2573 { VT_UI4, PropertyTagTypeLong },
2574 { VT_I8, PropertyTagTypeSRational },
2575 { VT_UI8, PropertyTagTypeRational },
2576 { VT_R4, PropertyTagTypeFloat },
2577 { VT_R8, PropertyTagTypeDouble },
2578 { VT_LPSTR, PropertyTagTypeASCII },
2579 { VT_BLOB, PropertyTagTypeUndefined }
2581 UINT i;
2582 for (i = 0; i < ARRAY_SIZE(vt2type); i++)
2584 if (vt2type[i].vt == vt) return vt2type[i].type;
2586 FIXME("not supported variant type %u\n", vt);
2587 return 0;
2590 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2591 UINT size, PROPID id)
2593 UINT item_size, item_type;
2595 item_size = propvariant_size(value);
2596 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2598 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2599 if (!item_type) return InvalidParameter;
2601 item->value = item + 1;
2603 switch (value->vt & ~VT_VECTOR)
2605 case VT_I1:
2606 case VT_UI1:
2607 if (!(value->vt & VT_VECTOR))
2608 *(BYTE *)item->value = value->u.bVal;
2609 else
2610 memcpy(item->value, value->u.caub.pElems, item_size);
2611 break;
2612 case VT_I2:
2613 case VT_UI2:
2614 if (!(value->vt & VT_VECTOR))
2615 *(USHORT *)item->value = value->u.uiVal;
2616 else
2617 memcpy(item->value, value->u.caui.pElems, item_size);
2618 break;
2619 case VT_I4:
2620 case VT_UI4:
2621 case VT_R4:
2622 if (!(value->vt & VT_VECTOR))
2623 *(ULONG *)item->value = value->u.ulVal;
2624 else
2625 memcpy(item->value, value->u.caul.pElems, item_size);
2626 break;
2627 case VT_I8:
2628 case VT_UI8:
2629 case VT_R8:
2630 if (!(value->vt & VT_VECTOR))
2631 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2632 else
2633 memcpy(item->value, value->u.cauh.pElems, item_size);
2634 break;
2635 case VT_LPSTR:
2636 memcpy(item->value, value->u.pszVal, item_size);
2637 break;
2638 case VT_BLOB:
2639 memcpy(item->value, value->u.blob.pBlobData, item_size);
2640 break;
2641 default:
2642 FIXME("not supported variant type %d\n", value->vt);
2643 return InvalidParameter;
2646 item->length = item_size;
2647 item->type = item_type;
2648 item->id = id;
2650 return Ok;
2653 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2654 PropertyItem *buffer)
2656 GpStatus stat;
2657 HRESULT hr;
2658 IWICMetadataReader *reader;
2659 PROPVARIANT id, value;
2661 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2663 if (!image || !buffer) return InvalidParameter;
2665 if (image->type != ImageTypeBitmap)
2667 FIXME("Not implemented for type %d\n", image->type);
2668 return NotImplemented;
2671 if (((GpBitmap *)image)->prop_item)
2673 UINT i;
2675 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2677 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2679 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2680 return InvalidParameter;
2682 *buffer = ((GpBitmap *)image)->prop_item[i];
2683 buffer->value = buffer + 1;
2684 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2685 return Ok;
2689 return PropertyNotFound;
2692 reader = ((GpBitmap *)image)->metadata_reader;
2693 if (!reader) return PropertyNotFound;
2695 id.vt = VT_UI2;
2696 id.u.uiVal = propid;
2697 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2698 if (FAILED(hr)) return PropertyNotFound;
2700 stat = propvariant_to_item(&value, buffer, size, propid);
2701 PropVariantClear(&value);
2703 return stat;
2706 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2708 HRESULT hr;
2709 IWICMetadataReader *reader;
2710 IWICEnumMetadataItem *enumerator;
2711 UINT prop_count, prop_size, i;
2712 PROPVARIANT id, value;
2714 TRACE("(%p,%p,%p)\n", image, size, count);
2716 if (!image || !size || !count) return InvalidParameter;
2718 if (image->type != ImageTypeBitmap)
2720 FIXME("Not implemented for type %d\n", image->type);
2721 return NotImplemented;
2724 if (((GpBitmap *)image)->prop_item)
2726 *count = ((GpBitmap *)image)->prop_count;
2727 *size = 0;
2729 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2731 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2734 return Ok;
2737 reader = ((GpBitmap *)image)->metadata_reader;
2738 if (!reader) return PropertyNotFound;
2740 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2741 if (FAILED(hr)) return hresult_to_status(hr);
2743 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2744 if (FAILED(hr)) return hresult_to_status(hr);
2746 IWICEnumMetadataItem_Reset(enumerator);
2748 prop_size = 0;
2750 PropVariantInit(&id);
2751 PropVariantInit(&value);
2753 for (i = 0; i < prop_count; i++)
2755 UINT items_returned, item_size;
2757 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2758 if (hr != S_OK) break;
2760 item_size = propvariant_size(&value);
2761 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2763 PropVariantClear(&id);
2764 PropVariantClear(&value);
2767 IWICEnumMetadataItem_Release(enumerator);
2769 if (hr != S_OK) return PropertyNotFound;
2771 *count = prop_count;
2772 *size = prop_size;
2773 return Ok;
2776 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2777 UINT count, PropertyItem *buf)
2779 GpStatus status;
2780 HRESULT hr;
2781 IWICMetadataReader *reader;
2782 IWICEnumMetadataItem *enumerator;
2783 UINT prop_count, prop_size, i;
2784 PROPVARIANT id, value;
2785 char *item_value;
2787 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2789 if (!image || !buf) return InvalidParameter;
2791 if (image->type != ImageTypeBitmap)
2793 FIXME("Not implemented for type %d\n", image->type);
2794 return NotImplemented;
2797 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2798 if (status != Ok) return status;
2800 if (prop_count != count || prop_size != size) return InvalidParameter;
2802 if (((GpBitmap *)image)->prop_item)
2804 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2806 item_value = (char *)(buf + prop_count);
2808 for (i = 0; i < prop_count; i++)
2810 buf[i].value = item_value;
2811 item_value += buf[i].length;
2814 return Ok;
2817 reader = ((GpBitmap *)image)->metadata_reader;
2818 if (!reader) return PropertyNotFound;
2820 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2821 if (FAILED(hr)) return hresult_to_status(hr);
2823 IWICEnumMetadataItem_Reset(enumerator);
2825 item_value = (char *)(buf + prop_count);
2827 PropVariantInit(&id);
2828 PropVariantInit(&value);
2830 for (i = 0; i < prop_count; i++)
2832 PropertyItem *item;
2833 UINT items_returned, item_size;
2835 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2836 if (hr != S_OK) break;
2838 if (id.vt != VT_UI2)
2840 FIXME("not supported propvariant type for id: %u\n", id.vt);
2841 continue;
2844 item_size = propvariant_size(&value);
2845 if (item_size)
2847 item = heap_alloc(item_size + sizeof(*item));
2849 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2850 buf[i].id = item->id;
2851 buf[i].type = item->type;
2852 buf[i].length = item_size;
2853 buf[i].value = item_value;
2854 memcpy(item_value, item->value, item_size);
2855 item_value += item_size;
2857 heap_free(item);
2860 PropVariantClear(&id);
2861 PropVariantClear(&value);
2864 IWICEnumMetadataItem_Release(enumerator);
2866 if (hr != S_OK) return PropertyNotFound;
2868 return Ok;
2871 struct image_format_dimension
2873 const GUID *format;
2874 const GUID *dimension;
2877 static const struct image_format_dimension image_format_dimensions[] =
2879 {&ImageFormatGIF, &FrameDimensionTime},
2880 {&ImageFormatIcon, &FrameDimensionResolution},
2881 {NULL}
2884 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2885 GDIPCONST GUID* dimensionID, UINT* count)
2887 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2889 if(!image || !count)
2890 return InvalidParameter;
2892 if (!dimensionID ||
2893 IsEqualGUID(dimensionID, &image->format) ||
2894 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2895 IsEqualGUID(dimensionID, &FrameDimensionTime))
2897 *count = image->frame_count;
2898 return Ok;
2901 return InvalidParameter;
2904 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2905 UINT* count)
2907 TRACE("(%p, %p)\n", image, count);
2909 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2911 if(!image || !count)
2912 return InvalidParameter;
2914 *count = 1;
2916 return Ok;
2919 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2920 GUID* dimensionIDs, UINT count)
2922 int i;
2923 const GUID *result=NULL;
2925 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2927 if(!image || !dimensionIDs || count != 1)
2928 return InvalidParameter;
2930 for (i=0; image_format_dimensions[i].format; i++)
2932 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2934 result = image_format_dimensions[i].dimension;
2935 break;
2939 if (!result)
2940 result = &FrameDimensionPage;
2942 memcpy(dimensionIDs, result, sizeof(GUID));
2944 return Ok;
2947 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2948 GpImage **image)
2950 GpStatus stat;
2951 IStream *stream;
2953 TRACE("(%s) %p\n", debugstr_w(filename), image);
2955 if (!filename || !image)
2956 return InvalidParameter;
2958 *image = NULL;
2960 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2962 if (stat != Ok)
2963 return stat;
2965 stat = GdipLoadImageFromStream(stream, image);
2967 IStream_Release(stream);
2969 return stat;
2972 /* FIXME: no icm handling */
2973 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2975 TRACE("(%s) %p\n", debugstr_w(filename), image);
2977 return GdipLoadImageFromFile(filename, image);
2980 static void add_property(GpBitmap *bitmap, PropertyItem *item)
2982 UINT prop_size, prop_count;
2983 PropertyItem *prop_item;
2985 if (bitmap->prop_item == NULL)
2987 prop_size = prop_count = 0;
2988 prop_item = heap_alloc_zero(item->length + sizeof(PropertyItem));
2989 if (!prop_item) return;
2991 else
2993 UINT i;
2994 char *item_value;
2996 GdipGetPropertySize(&bitmap->image, &prop_size, &prop_count);
2998 prop_item = heap_alloc_zero(prop_size + item->length + sizeof(PropertyItem));
2999 if (!prop_item) return;
3000 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
3001 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
3002 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
3004 item_value = (char *)(prop_item + prop_count + 1);
3006 for (i = 0; i < prop_count; i++)
3008 prop_item[i].value = item_value;
3009 item_value += prop_item[i].length;
3013 prop_item[prop_count].id = item->id;
3014 prop_item[prop_count].type = item->type;
3015 prop_item[prop_count].length = item->length;
3016 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3017 memcpy(prop_item[prop_count].value, item->value, item->length);
3019 heap_free(bitmap->prop_item);
3020 bitmap->prop_item = prop_item;
3021 bitmap->prop_count++;
3024 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3026 HRESULT hr;
3027 GUID format;
3028 PROPVARIANT id, value;
3029 BOOL ret = FALSE;
3031 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3032 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return FALSE;
3034 PropVariantInit(&id);
3035 PropVariantInit(&value);
3037 id.vt = VT_LPWSTR;
3038 id.u.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3039 if (!id.u.pwszVal) return FALSE;
3040 lstrcpyW(id.u.pwszVal, prop_name);
3041 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3042 if (hr == S_OK && value.vt == VT_BOOL)
3043 ret = value.u.boolVal;
3045 PropVariantClear(&id);
3046 PropVariantClear(&value);
3048 return ret;
3051 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3053 HRESULT hr;
3054 GUID format;
3055 PROPVARIANT id, value;
3056 PropertyItem *item = NULL;
3058 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3059 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return NULL;
3061 PropVariantInit(&id);
3062 PropVariantInit(&value);
3064 id.vt = VT_LPWSTR;
3065 id.u.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3066 if (!id.u.pwszVal) return NULL;
3067 lstrcpyW(id.u.pwszVal, prop_name);
3068 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3069 if (hr == S_OK)
3071 UINT item_size = propvariant_size(&value);
3072 if (item_size)
3074 item_size += sizeof(*item);
3075 item = heap_alloc_zero(item_size);
3076 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3078 heap_free(item);
3079 item = NULL;
3084 PropVariantClear(&id);
3085 PropVariantClear(&value);
3087 return item;
3090 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3092 static const WCHAR textentryW[] = { 'T','e','x','t','E','n','t','r','y',0 };
3093 PropertyItem *comment;
3095 comment = get_property(reader, &GUID_MetadataFormatGifComment, textentryW);
3096 if (comment)
3097 comment->id = PropertyTagExifUserComment;
3099 return comment;
3102 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3104 static const WCHAR applicationW[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3105 static const WCHAR dataW[] = { 'D','a','t','a',0 };
3106 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3108 appext = get_property(reader, &GUID_MetadataFormatAPE, applicationW);
3109 if (appext)
3111 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3112 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3114 appdata = get_property(reader, &GUID_MetadataFormatAPE, dataW);
3115 if (appdata)
3117 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3119 BYTE *data = appdata->value;
3120 if (data[0] == 3 && data[1] == 1)
3122 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3123 if (loop)
3125 loop->type = PropertyTagTypeShort;
3126 loop->id = PropertyTagLoopCount;
3127 loop->length = sizeof(SHORT);
3128 loop->value = loop + 1;
3129 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3137 heap_free(appext);
3138 heap_free(appdata);
3140 return loop;
3143 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3145 static const WCHAR backgroundW[] = { 'B','a','c','k','g','r','o','u','n','d','C','o','l','o','r','I','n','d','e','x',0 };
3146 PropertyItem *background;
3148 background = get_property(reader, &GUID_MetadataFormatLSD, backgroundW);
3149 if (background)
3150 background->id = PropertyTagIndexBackground;
3152 return background;
3155 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3157 static const WCHAR global_flagW[] = { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 };
3158 HRESULT hr;
3159 IWICImagingFactory *factory;
3160 IWICPalette *palette;
3161 UINT count = 0;
3162 WICColor colors[256];
3164 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, global_flagW))
3165 return NULL;
3167 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3168 if (hr != S_OK) return NULL;
3170 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3171 if (hr == S_OK)
3173 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3174 if (hr == S_OK)
3175 IWICPalette_GetColors(palette, 256, colors, &count);
3177 IWICPalette_Release(palette);
3180 IWICImagingFactory_Release(factory);
3182 if (count)
3184 PropertyItem *pal;
3185 UINT i;
3186 BYTE *rgb;
3188 pal = heap_alloc_zero(sizeof(*pal) + count * 3);
3189 if (!pal) return NULL;
3190 pal->type = PropertyTagTypeByte;
3191 pal->id = PropertyTagGlobalPalette;
3192 pal->value = pal + 1;
3193 pal->length = count * 3;
3195 rgb = pal->value;
3197 for (i = 0; i < count; i++)
3199 rgb[i*3] = (colors[i] >> 16) & 0xff;
3200 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3201 rgb[i*3 + 2] = colors[i] & 0xff;
3204 return pal;
3207 return NULL;
3210 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3212 static const WCHAR transparency_flagW[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3213 static const WCHAR colorW[] = { 'T','r','a','n','s','p','a','r','e','n','t','C','o','l','o','r','I','n','d','e','x',0 };
3214 PropertyItem *index = NULL;
3216 if (get_bool_property(reader, &GUID_MetadataFormatGCE, transparency_flagW))
3218 index = get_property(reader, &GUID_MetadataFormatGCE, colorW);
3219 if (index)
3220 index->id = PropertyTagIndexTransparent;
3222 return index;
3225 static LONG get_gif_frame_property(IWICBitmapFrameDecode *frame, const GUID *format, const WCHAR *property)
3227 HRESULT hr;
3228 IWICMetadataBlockReader *block_reader;
3229 IWICMetadataReader *reader;
3230 UINT block_count, i;
3231 PropertyItem *prop;
3232 LONG value = 0;
3234 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3235 if (hr == S_OK)
3237 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3238 if (hr == S_OK)
3240 for (i = 0; i < block_count; i++)
3242 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3243 if (hr == S_OK)
3245 prop = get_property(reader, format, property);
3246 if (prop)
3248 if (prop->type == PropertyTagTypeByte && prop->length == 1)
3249 value = *(BYTE *)prop->value;
3250 else if (prop->type == PropertyTagTypeShort && prop->length == 2)
3251 value = *(SHORT *)prop->value;
3253 heap_free(prop);
3255 IWICMetadataReader_Release(reader);
3259 IWICMetadataBlockReader_Release(block_reader);
3262 return value;
3265 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3267 static const WCHAR delayW[] = { 'D','e','l','a','y',0 };
3268 HRESULT hr;
3269 IWICBitmapFrameDecode *frame;
3270 IWICMetadataBlockReader *block_reader;
3271 IWICMetadataReader *reader;
3272 UINT frame_count, block_count, i;
3273 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3274 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3276 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3277 if (frame_count > 1)
3279 delay = heap_alloc_zero(sizeof(*delay) + frame_count * sizeof(LONG));
3280 if (delay)
3282 LONG *value;
3284 delay->type = PropertyTagTypeLong;
3285 delay->id = PropertyTagFrameDelay;
3286 delay->length = frame_count * sizeof(LONG);
3287 delay->value = delay + 1;
3289 value = delay->value;
3291 for (i = 0; i < frame_count; i++)
3293 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3294 if (hr == S_OK)
3296 value[i] = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, delayW);
3297 IWICBitmapFrameDecode_Release(frame);
3299 else value[i] = 0;
3304 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3305 if (hr == S_OK)
3307 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3308 if (hr == S_OK)
3310 for (i = 0; i < block_count; i++)
3312 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3313 if (hr == S_OK)
3315 if (!comment)
3316 comment = get_gif_comment(reader);
3318 if (frame_count > 1 && !loop)
3319 loop = get_gif_loopcount(reader);
3321 if (!background)
3322 background = get_gif_background(reader);
3324 if (!palette)
3325 palette = get_gif_palette(decoder, reader);
3327 IWICMetadataReader_Release(reader);
3331 IWICMetadataBlockReader_Release(block_reader);
3334 if (frame_count > 1 && !loop)
3336 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3337 if (loop)
3339 loop->type = PropertyTagTypeShort;
3340 loop->id = PropertyTagLoopCount;
3341 loop->length = sizeof(SHORT);
3342 loop->value = loop + 1;
3343 *(SHORT *)loop->value = 1;
3347 if (delay) add_property(bitmap, delay);
3348 if (comment) add_property(bitmap, comment);
3349 if (loop) add_property(bitmap, loop);
3350 if (palette) add_property(bitmap, palette);
3351 if (background) add_property(bitmap, background);
3353 heap_free(delay);
3354 heap_free(comment);
3355 heap_free(loop);
3356 heap_free(palette);
3357 heap_free(background);
3359 /* Win7 gdiplus always returns transparent color index from frame 0 */
3360 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3361 if (hr != S_OK) return;
3363 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3364 if (hr == S_OK)
3366 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3367 if (hr == S_OK)
3369 for (i = 0; i < block_count; i++)
3371 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3372 if (hr == S_OK)
3374 if (!transparent_idx)
3375 transparent_idx = get_gif_transparent_idx(reader);
3377 IWICMetadataReader_Release(reader);
3381 IWICMetadataBlockReader_Release(block_reader);
3384 if (transparent_idx) add_property(bitmap, transparent_idx);
3385 heap_free(transparent_idx);
3387 IWICBitmapFrameDecode_Release(frame);
3390 static PropertyItem* create_prop(PROPID propid, PROPVARIANT* value)
3392 PropertyItem *item = NULL;
3393 UINT item_size = propvariant_size(value);
3395 if (item_size)
3397 item_size += sizeof(*item);
3398 item = heap_alloc_zero(item_size);
3399 if (propvariant_to_item(value, item, item_size, propid) != Ok)
3401 heap_free(item);
3402 item = NULL;
3406 return item;
3409 static ULONG get_ulong_by_index(IWICMetadataReader* reader, ULONG index)
3411 PROPVARIANT value;
3412 HRESULT hr;
3413 ULONG result=0;
3415 hr = IWICMetadataReader_GetValueByIndex(reader, index, NULL, NULL, &value);
3416 if (SUCCEEDED(hr))
3418 switch (value.vt)
3420 case VT_UI4:
3421 result = value.u.ulVal;
3422 break;
3423 default:
3424 ERR("unhandled case %u\n", value.vt);
3425 break;
3427 PropVariantClear(&value);
3429 return result;
3432 static void png_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3434 HRESULT hr;
3435 IWICBitmapFrameDecode *frame;
3436 IWICMetadataBlockReader *block_reader;
3437 IWICMetadataReader *reader;
3438 UINT block_count, i, j;
3439 struct keyword_info {
3440 const char* name;
3441 PROPID propid;
3442 BOOL seen;
3443 } keywords[] = {
3444 { "Title", PropertyTagImageTitle },
3445 { "Author", PropertyTagArtist },
3446 { "Description", PropertyTagImageDescription },
3447 { "Copyright", PropertyTagCopyright },
3448 { "Software", PropertyTagSoftwareUsed },
3449 { "Source", PropertyTagEquipModel },
3450 { "Comment", PropertyTagExifUserComment },
3452 BOOL seen_gamma=FALSE, seen_whitepoint=FALSE, seen_chrm=FALSE;
3454 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3455 if (hr != S_OK) return;
3457 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3458 if (hr == S_OK)
3460 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3461 if (hr == S_OK)
3463 for (i = 0; i < block_count; i++)
3465 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3466 if (hr == S_OK)
3468 GUID format;
3470 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3471 if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunktEXt, &format))
3473 PROPVARIANT name, value;
3474 PropertyItem* item;
3476 hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, &name, &value);
3478 if (SUCCEEDED(hr))
3480 if (name.vt == VT_LPSTR)
3482 for (j = 0; j < ARRAY_SIZE(keywords); j++)
3483 if (!strcmp(keywords[j].name, name.u.pszVal))
3484 break;
3485 if (j < ARRAY_SIZE(keywords) && !keywords[j].seen)
3487 keywords[j].seen = TRUE;
3488 item = create_prop(keywords[j].propid, &value);
3489 if (item)
3490 add_property(bitmap, item);
3491 heap_free(item);
3495 PropVariantClear(&name);
3496 PropVariantClear(&value);
3499 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA, &format))
3501 PropertyItem* item;
3503 if (!seen_gamma)
3505 item = heap_alloc_zero(sizeof(PropertyItem) + sizeof(ULONG) * 2);
3506 if (item)
3508 ULONG *rational;
3509 item->length = sizeof(ULONG) * 2;
3510 item->type = PropertyTagTypeRational;
3511 item->id = PropertyTagGamma;
3512 rational = item->value = item + 1;
3513 rational[0] = 100000;
3514 rational[1] = get_ulong_by_index(reader, 0);
3515 add_property(bitmap, item);
3516 seen_gamma = TRUE;
3517 heap_free(item);
3521 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkcHRM, &format))
3523 PropertyItem* item;
3525 if (!seen_whitepoint)
3527 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 4);
3528 if (item)
3530 ULONG *rational;
3531 item->length = sizeof(ULONG) * 4;
3532 item->type = PropertyTagTypeRational;
3533 item->id = PropertyTagWhitePoint;
3534 rational = item->value = item + 1;
3535 rational[0] = get_ulong_by_index(reader, 0);
3536 rational[1] = 100000;
3537 rational[2] = get_ulong_by_index(reader, 1);
3538 rational[3] = 100000;
3539 add_property(bitmap, item);
3540 seen_whitepoint = TRUE;
3541 GdipFree(item);
3544 if (!seen_chrm)
3546 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 12);
3547 if (item)
3549 ULONG *rational;
3550 item->length = sizeof(ULONG) * 12;
3551 item->type = PropertyTagTypeRational;
3552 item->id = PropertyTagPrimaryChromaticities;
3553 rational = item->value = item + 1;
3554 rational[0] = get_ulong_by_index(reader, 2);
3555 rational[1] = 100000;
3556 rational[2] = get_ulong_by_index(reader, 3);
3557 rational[3] = 100000;
3558 rational[4] = get_ulong_by_index(reader, 4);
3559 rational[5] = 100000;
3560 rational[6] = get_ulong_by_index(reader, 5);
3561 rational[7] = 100000;
3562 rational[8] = get_ulong_by_index(reader, 6);
3563 rational[9] = 100000;
3564 rational[10] = get_ulong_by_index(reader, 7);
3565 rational[11] = 100000;
3566 add_property(bitmap, item);
3567 seen_chrm = TRUE;
3568 GdipFree(item);
3573 IWICMetadataReader_Release(reader);
3577 IWICMetadataBlockReader_Release(block_reader);
3580 IWICBitmapFrameDecode_Release(frame);
3583 static GpStatus initialize_decoder_wic(IStream *stream, REFGUID container, IWICBitmapDecoder **decoder)
3585 IWICImagingFactory *factory;
3586 HRESULT hr;
3588 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
3590 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3591 if (FAILED(hr)) return hresult_to_status(hr);
3592 hr = IWICImagingFactory_CreateDecoder(factory, container, NULL, decoder);
3593 IWICImagingFactory_Release(factory);
3594 if (FAILED(hr)) return hresult_to_status(hr);
3596 hr = IWICBitmapDecoder_Initialize(*decoder, stream, WICDecodeMetadataCacheOnLoad);
3597 if (FAILED(hr))
3599 IWICBitmapDecoder_Release(*decoder);
3600 return hresult_to_status(hr);
3602 return Ok;
3605 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3607 static GpStatus decode_frame_wic(IWICBitmapDecoder *decoder, BOOL force_conversion,
3608 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3610 GpStatus status=Ok;
3611 GpBitmap *bitmap;
3612 HRESULT hr;
3613 IWICBitmapFrameDecode *frame;
3614 IWICBitmapSource *source=NULL;
3615 IWICMetadataBlockReader *block_reader;
3616 WICPixelFormatGUID wic_format;
3617 PixelFormat gdip_format=0;
3618 ColorPalette *palette = NULL;
3619 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3620 int i;
3621 UINT width, height, frame_count;
3622 BitmapData lockeddata;
3623 WICRect wrc;
3625 TRACE("%p,%u,%p\n", decoder, active_frame, image);
3627 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3628 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3629 if (SUCCEEDED(hr)) /* got frame */
3631 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3633 if (SUCCEEDED(hr))
3635 if (!force_conversion)
3637 for (i=0; pixel_formats[i].wic_format; i++)
3639 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3641 source = (IWICBitmapSource*)frame;
3642 IWICBitmapSource_AddRef(source);
3643 gdip_format = pixel_formats[i].gdip_format;
3644 palette_type = pixel_formats[i].palette_type;
3645 break;
3649 if (!source)
3651 /* unknown format; fall back on 32bppARGB */
3652 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3653 gdip_format = PixelFormat32bppARGB;
3655 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3658 if (SUCCEEDED(hr)) /* got source */
3660 hr = IWICBitmapSource_GetSize(source, &width, &height);
3662 if (SUCCEEDED(hr))
3663 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3664 NULL, &bitmap);
3666 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3668 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3669 gdip_format, &lockeddata);
3670 if (status == Ok) /* locked bitmap */
3672 wrc.X = 0;
3673 wrc.Width = width;
3674 wrc.Height = 1;
3675 for (i=0; i<height; i++)
3677 wrc.Y = i;
3678 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3679 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3680 if (FAILED(hr)) break;
3683 GdipBitmapUnlockBits(bitmap, &lockeddata);
3686 if (SUCCEEDED(hr) && status == Ok)
3687 *image = &bitmap->image;
3688 else
3690 *image = NULL;
3691 GdipDisposeImage(&bitmap->image);
3694 if (SUCCEEDED(hr) && status == Ok)
3696 double dpix, dpiy;
3697 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3698 if (SUCCEEDED(hr))
3700 bitmap->image.xres = dpix;
3701 bitmap->image.yres = dpiy;
3703 hr = S_OK;
3707 IWICBitmapSource_Release(source);
3710 if (SUCCEEDED(hr) && status == Ok) {
3711 bitmap->metadata_reader = NULL;
3713 if (metadata_reader)
3714 metadata_reader(bitmap, decoder, active_frame);
3715 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3717 UINT block_count = 0;
3718 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3719 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3720 IWICMetadataBlockReader_Release(block_reader);
3723 palette = get_palette(frame, palette_type);
3724 IWICBitmapFrameDecode_Release(frame);
3728 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3730 if (status == Ok)
3732 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3733 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI;
3734 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormat2bppGray) ||
3735 IsEqualGUID(&wic_format, &GUID_WICPixelFormat4bppGray) ||
3736 IsEqualGUID(&wic_format, &GUID_WICPixelFormat8bppGray) ||
3737 IsEqualGUID(&wic_format, &GUID_WICPixelFormat16bppGray))
3738 bitmap->image.flags |= ImageFlagsColorSpaceGRAY;
3739 else
3740 bitmap->image.flags |= ImageFlagsColorSpaceRGB;
3741 bitmap->image.frame_count = frame_count;
3742 bitmap->image.current_frame = active_frame;
3743 bitmap->image.decoder = decoder;
3744 IWICBitmapDecoder_AddRef(decoder);
3745 if (palette)
3747 heap_free(bitmap->image.palette);
3748 bitmap->image.palette = palette;
3750 else
3752 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3753 bitmap->image.palette->Flags = 0;
3755 TRACE("=> %p\n", *image);
3758 return status;
3761 static GpStatus decode_image_wic(IStream *stream, REFGUID container,
3762 metadata_reader_func metadata_reader, GpImage **image)
3764 IWICBitmapDecoder *decoder;
3765 GpStatus status;
3767 status = initialize_decoder_wic(stream, container, &decoder);
3768 if(status != Ok)
3769 return status;
3771 status = decode_frame_wic(decoder, FALSE, 0, metadata_reader, image);
3772 IWICBitmapDecoder_Release(decoder);
3773 return status;
3776 static GpStatus select_frame_wic(GpImage *image, UINT active_frame)
3778 GpImage *new_image;
3779 GpStatus status;
3781 status = decode_frame_wic(image->decoder, FALSE, active_frame, NULL, &new_image);
3782 if(status != Ok)
3783 return status;
3785 new_image->busy = image->busy;
3786 memcpy(&new_image->format, &image->format, sizeof(GUID));
3787 new_image->encoder = image->encoder;
3788 image->encoder = NULL;
3789 free_image_data(image);
3790 if (image->type == ImageTypeBitmap)
3791 *(GpBitmap *)image = *(GpBitmap *)new_image;
3792 else if (image->type == ImageTypeMetafile)
3793 *(GpMetafile *)image = *(GpMetafile *)new_image;
3794 new_image->type = ~0;
3795 heap_free(new_image);
3796 return Ok;
3799 static HRESULT get_gif_frame_rect(IWICBitmapFrameDecode *frame,
3800 UINT *left, UINT *top, UINT *width, UINT *height)
3802 static const WCHAR leftW[] = {'L','e','f','t',0};
3803 static const WCHAR topW[] = {'T','o','p',0};
3805 *left = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, leftW);
3806 *top = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, topW);
3808 return IWICBitmapFrameDecode_GetSize(frame, width, height);
3811 static HRESULT blit_gif_frame(GpBitmap *bitmap, IWICBitmapFrameDecode *frame, BOOL first_frame)
3813 UINT i, j, left, top, width, height;
3814 IWICBitmapSource *source;
3815 BYTE *new_bits;
3816 HRESULT hr;
3818 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3819 if(FAILED(hr))
3820 return hr;
3822 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3823 if(FAILED(hr))
3824 return hr;
3826 new_bits = heap_alloc_zero(width*height*4);
3827 if(!new_bits)
3828 return E_OUTOFMEMORY;
3830 hr = IWICBitmapSource_CopyPixels(source, NULL, width*4, width*height*4, new_bits);
3831 IWICBitmapSource_Release(source);
3832 if(FAILED(hr)) {
3833 heap_free(new_bits);
3834 return hr;
3837 for(i=0; i<height && i+top<bitmap->height; i++) {
3838 for(j=0; j<width && j+left<bitmap->width; j++) {
3839 DWORD *src = (DWORD*)(new_bits+i*width*4+j*4);
3840 DWORD *dst = (DWORD*)(bitmap->bits+(i+top)*bitmap->stride+(j+left)*4);
3842 if(first_frame || *src>>24 != 0)
3843 *dst = *src;
3846 heap_free(new_bits);
3847 return hr;
3850 static DWORD get_gif_background_color(GpBitmap *bitmap)
3852 BYTE bgcolor_idx = 0;
3853 UINT i;
3855 for(i=0; i<bitmap->prop_count; i++) {
3856 if(bitmap->prop_item[i].id == PropertyTagIndexBackground) {
3857 bgcolor_idx = *(BYTE*)bitmap->prop_item[i].value;
3858 break;
3862 for(i=0; i<bitmap->prop_count; i++) {
3863 if(bitmap->prop_item[i].id == PropertyTagIndexTransparent) {
3864 BYTE transparent_idx;
3865 transparent_idx = *(BYTE*)bitmap->prop_item[i].value;
3867 if(transparent_idx == bgcolor_idx)
3868 return 0;
3872 for(i=0; i<bitmap->prop_count; i++) {
3873 if(bitmap->prop_item[i].id == PropertyTagGlobalPalette) {
3874 if(bitmap->prop_item[i].length/3 > bgcolor_idx) {
3875 BYTE *color = ((BYTE*)bitmap->prop_item[i].value)+bgcolor_idx*3;
3876 return color[2] + (color[1]<<8) + (color[0]<<16) + (0xffu<<24);
3878 break;
3882 FIXME("can't get gif background color\n");
3883 return 0xffffffff;
3886 static GpStatus select_frame_gif(GpImage* image, UINT active_frame)
3888 static const WCHAR disposalW[] = {'D','i','s','p','o','s','a','l',0};
3890 GpBitmap *bitmap = (GpBitmap*)image;
3891 IWICBitmapFrameDecode *frame;
3892 int cur_frame=0, disposal;
3893 BOOL bgcolor_set = FALSE;
3894 DWORD bgcolor = 0;
3895 HRESULT hr;
3897 if(active_frame > image->current_frame) {
3898 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, image->current_frame, &frame);
3899 if(FAILED(hr))
3900 return hresult_to_status(hr);
3901 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3902 IWICBitmapFrameDecode_Release(frame);
3904 if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND)
3905 cur_frame = image->current_frame;
3906 else if(disposal != GIF_DISPOSE_RESTORE_TO_PREV)
3907 cur_frame = image->current_frame+1;
3910 while(cur_frame != active_frame) {
3911 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, cur_frame, &frame);
3912 if(FAILED(hr))
3913 return hresult_to_status(hr);
3914 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3916 if(disposal==GIF_DISPOSE_UNSPECIFIED || disposal==GIF_DISPOSE_DO_NOT_DISPOSE) {
3917 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3918 if(FAILED(hr))
3919 return hresult_to_status(hr);
3920 }else if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND) {
3921 UINT left, top, width, height, i, j;
3923 if(!bgcolor_set) {
3924 bgcolor = get_gif_background_color(bitmap);
3925 bgcolor_set = TRUE;
3928 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3929 if(FAILED(hr))
3930 return hresult_to_status(hr);
3931 for(i=top; i<top+height && i<bitmap->height; i++) {
3932 DWORD *bits = (DWORD*)(bitmap->bits+i*bitmap->stride);
3933 for(j=left; j<left+width && j<bitmap->width; j++)
3934 bits[j] = bgcolor;
3938 IWICBitmapFrameDecode_Release(frame);
3939 cur_frame++;
3942 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, active_frame, &frame);
3943 if(FAILED(hr))
3944 return hresult_to_status(hr);
3946 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3947 IWICBitmapFrameDecode_Release(frame);
3948 if(FAILED(hr))
3949 return hresult_to_status(hr);
3951 image->current_frame = active_frame;
3952 return Ok;
3955 static GpStatus decode_image_icon(IStream* stream, GpImage **image)
3957 return decode_image_wic(stream, &GUID_ContainerFormatIco, NULL, image);
3960 static GpStatus decode_image_bmp(IStream* stream, GpImage **image)
3962 GpStatus status;
3963 GpBitmap* bitmap;
3965 status = decode_image_wic(stream, &GUID_ContainerFormatBmp, NULL, image);
3967 bitmap = (GpBitmap*)*image;
3969 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3971 /* WIC supports bmp files with alpha, but gdiplus does not */
3972 bitmap->format = PixelFormat32bppRGB;
3975 return status;
3978 static GpStatus decode_image_jpeg(IStream* stream, GpImage **image)
3980 return decode_image_wic(stream, &GUID_ContainerFormatJpeg, NULL, image);
3983 static BOOL has_png_transparency_chunk(IStream *pIStream)
3985 LARGE_INTEGER seek;
3986 BOOL has_tRNS = FALSE;
3987 HRESULT hr;
3988 BYTE header[8];
3990 seek.QuadPart = 8;
3993 ULARGE_INTEGER chunk_start;
3994 ULONG bytesread, chunk_size;
3996 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, &chunk_start);
3997 if (FAILED(hr)) break;
3999 hr = IStream_Read(pIStream, header, 8, &bytesread);
4000 if (FAILED(hr) || bytesread < 8) break;
4002 chunk_size = (header[0] << 24) | (header[1] << 16) | (header[2] << 8) | header[3];
4003 if (!memcmp(&header[4], "tRNS", 4))
4005 has_tRNS = TRUE;
4006 break;
4009 seek.QuadPart = chunk_start.QuadPart + chunk_size + 12; /* skip data and CRC */
4010 } while (memcmp(&header[4], "IDAT", 4) && memcmp(&header[4], "IEND", 4));
4012 TRACE("has_tRNS = %d\n", has_tRNS);
4013 return has_tRNS;
4016 static GpStatus decode_image_png(IStream* stream, GpImage **image)
4018 IWICBitmapDecoder *decoder;
4019 IWICBitmapFrameDecode *frame;
4020 GpStatus status;
4021 HRESULT hr;
4022 GUID format;
4023 BOOL force_conversion = FALSE;
4025 status = initialize_decoder_wic(stream, &GUID_ContainerFormatPng, &decoder);
4026 if (status != Ok)
4027 return status;
4029 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
4030 if (hr == S_OK)
4032 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
4033 if (hr == S_OK)
4035 if (IsEqualGUID(&format, &GUID_WICPixelFormat8bppGray))
4036 force_conversion = TRUE;
4037 else if ((IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed) ||
4038 IsEqualGUID(&format, &GUID_WICPixelFormat4bppIndexed) ||
4039 IsEqualGUID(&format, &GUID_WICPixelFormat2bppIndexed) ||
4040 IsEqualGUID(&format, &GUID_WICPixelFormat1bppIndexed) ||
4041 IsEqualGUID(&format, &GUID_WICPixelFormat24bppBGR)) &&
4042 has_png_transparency_chunk(stream))
4043 force_conversion = TRUE;
4045 status = decode_frame_wic(decoder, force_conversion, 0, png_metadata_reader, image);
4047 else
4048 status = hresult_to_status(hr);
4050 IWICBitmapFrameDecode_Release(frame);
4052 else
4053 status = hresult_to_status(hr);
4055 IWICBitmapDecoder_Release(decoder);
4056 return status;
4059 static GpStatus decode_image_gif(IStream* stream, GpImage **image)
4061 IWICBitmapDecoder *decoder;
4062 UINT frame_count;
4063 GpStatus status;
4064 HRESULT hr;
4066 status = initialize_decoder_wic(stream, &GUID_ContainerFormatGif, &decoder);
4067 if(status != Ok)
4068 return status;
4070 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
4071 if(FAILED(hr))
4072 return hresult_to_status(hr);
4074 status = decode_frame_wic(decoder, frame_count > 1, 0, gif_metadata_reader, image);
4075 IWICBitmapDecoder_Release(decoder);
4076 if(status != Ok)
4077 return status;
4079 if(frame_count > 1) {
4080 heap_free((*image)->palette);
4081 (*image)->palette = NULL;
4083 return Ok;
4086 static GpStatus decode_image_tiff(IStream* stream, GpImage **image)
4088 return decode_image_wic(stream, &GUID_ContainerFormatTiff, NULL, image);
4091 static GpStatus load_wmf(IStream *stream, GpMetafile **metafile)
4093 WmfPlaceableFileHeader pfh;
4094 BOOL is_placeable = FALSE;
4095 LARGE_INTEGER seek;
4096 GpStatus status;
4097 METAHEADER mh;
4098 HMETAFILE hmf;
4099 HRESULT hr;
4100 UINT size;
4101 void *buf;
4103 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4104 if (hr != S_OK || size != sizeof(mh))
4105 return GenericError;
4107 if (((WmfPlaceableFileHeader *)&mh)->Key == WMF_PLACEABLE_KEY)
4109 seek.QuadPart = 0;
4110 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4111 if (FAILED(hr)) return hresult_to_status(hr);
4113 hr = IStream_Read(stream, &pfh, sizeof(pfh), &size);
4114 if (hr != S_OK || size != sizeof(pfh))
4115 return GenericError;
4117 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4118 if (hr != S_OK || size != sizeof(mh))
4119 return GenericError;
4121 is_placeable = TRUE;
4124 seek.QuadPart = is_placeable ? sizeof(pfh) : 0;
4125 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4126 if (FAILED(hr)) return hresult_to_status(hr);
4128 buf = heap_alloc(mh.mtSize * 2);
4129 if (!buf) return OutOfMemory;
4131 hr = IStream_Read(stream, buf, mh.mtSize * 2, &size);
4132 if (hr != S_OK || size != mh.mtSize * 2)
4134 heap_free(buf);
4135 return GenericError;
4138 hmf = SetMetaFileBitsEx(mh.mtSize * 2, buf);
4139 heap_free(buf);
4140 if (!hmf)
4141 return GenericError;
4143 status = GdipCreateMetafileFromWmf(hmf, TRUE, is_placeable ? &pfh : NULL, metafile);
4144 if (status != Ok)
4145 DeleteMetaFile(hmf);
4146 return status;
4149 static GpStatus decode_image_wmf(IStream *stream, GpImage **image)
4151 GpMetafile *metafile;
4152 GpStatus status;
4154 TRACE("%p %p\n", stream, image);
4156 if (!stream || !image)
4157 return InvalidParameter;
4159 status = load_wmf(stream, &metafile);
4160 if (status != Ok)
4162 TRACE("Could not load metafile\n");
4163 return status;
4166 *image = (GpImage *)metafile;
4167 TRACE("<-- %p\n", *image);
4169 return Ok;
4172 static GpStatus load_emf(IStream *stream, GpMetafile **metafile)
4174 LARGE_INTEGER seek;
4175 ENHMETAHEADER emh;
4176 HENHMETAFILE hemf;
4177 GpStatus status;
4178 HRESULT hr;
4179 UINT size;
4180 void *buf;
4182 hr = IStream_Read(stream, &emh, sizeof(emh), &size);
4183 if (hr != S_OK || size != sizeof(emh) || emh.dSignature != ENHMETA_SIGNATURE)
4184 return GenericError;
4186 seek.QuadPart = 0;
4187 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4188 if (FAILED(hr)) return hresult_to_status(hr);
4190 buf = heap_alloc(emh.nBytes);
4191 if (!buf) return OutOfMemory;
4193 hr = IStream_Read(stream, buf, emh.nBytes, &size);
4194 if (hr != S_OK || size != emh.nBytes)
4196 heap_free(buf);
4197 return GenericError;
4200 hemf = SetEnhMetaFileBits(emh.nBytes, buf);
4201 heap_free(buf);
4202 if (!hemf)
4203 return GenericError;
4205 status = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
4206 if (status != Ok)
4207 DeleteEnhMetaFile(hemf);
4208 return status;
4211 static GpStatus decode_image_emf(IStream *stream, GpImage **image)
4213 GpMetafile *metafile;
4214 GpStatus status;
4216 TRACE("%p %p\n", stream, image);
4218 if (!stream || !image)
4219 return InvalidParameter;
4221 status = load_emf(stream, &metafile);
4222 if (status != Ok)
4224 TRACE("Could not load metafile\n");
4225 return status;
4228 *image = (GpImage *)metafile;
4229 TRACE("<-- %p\n", *image);
4231 return Ok;
4234 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
4235 GDIPCONST EncoderParameters* params);
4237 typedef GpStatus (*decode_image_func)(IStream *stream, GpImage **image);
4239 typedef GpStatus (*select_image_func)(GpImage *image, UINT active_frame);
4241 typedef struct image_codec {
4242 ImageCodecInfo info;
4243 encode_image_func encode_func;
4244 decode_image_func decode_func;
4245 select_image_func select_func;
4246 } image_codec;
4248 typedef enum {
4249 BMP,
4250 JPEG,
4251 GIF,
4252 TIFF,
4253 EMF,
4254 WMF,
4255 PNG,
4256 ICO,
4257 NUM_CODECS
4258 } ImageFormat;
4260 static const struct image_codec codecs[NUM_CODECS];
4262 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
4264 BYTE signature[8];
4265 const BYTE *pattern, *mask;
4266 LARGE_INTEGER seek;
4267 HRESULT hr;
4268 UINT bytesread;
4269 int i;
4270 DWORD j, sig;
4272 /* seek to the start of the stream */
4273 seek.QuadPart = 0;
4274 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4275 if (FAILED(hr)) return hresult_to_status(hr);
4277 /* read the first 8 bytes */
4278 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4279 hr = IStream_Read(stream, signature, 8, &bytesread);
4280 if (FAILED(hr)) return hresult_to_status(hr);
4281 if (hr == S_FALSE || bytesread == 0) return GenericError;
4283 for (i = 0; i < NUM_CODECS; i++) {
4284 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4285 bytesread >= codecs[i].info.SigSize)
4287 for (sig=0; sig<codecs[i].info.SigCount; sig++)
4289 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
4290 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
4291 for (j=0; j<codecs[i].info.SigSize; j++)
4292 if ((signature[j] & mask[j]) != pattern[j])
4293 break;
4294 if (j == codecs[i].info.SigSize)
4296 *result = &codecs[i];
4297 return Ok;
4303 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
4304 signature[0],signature[1],signature[2],signature[3],
4305 signature[4],signature[5],signature[6],signature[7]);
4307 return GenericError;
4310 static GpStatus get_decoder_info_from_image(GpImage *image, const struct image_codec **result)
4312 int i;
4314 for (i = 0; i < NUM_CODECS; i++) {
4315 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4316 IsEqualIID(&codecs[i].info.FormatID, &image->format))
4318 *result = &codecs[i];
4319 return Ok;
4323 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image->format));
4324 return GenericError;
4327 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
4328 UINT frame)
4330 GpStatus stat;
4331 const struct image_codec *codec = NULL;
4332 BOOL unlock;
4334 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
4336 if (!image || !dimensionID)
4337 return InvalidParameter;
4338 if(!image_lock(image, &unlock))
4339 return ObjectBusy;
4341 if (frame >= image->frame_count)
4343 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
4344 image_unlock(image, unlock);
4345 return InvalidParameter;
4348 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
4350 WARN("invalid image type %d\n", image->type);
4351 image_unlock(image, unlock);
4352 return InvalidParameter;
4355 if (image->current_frame == frame)
4357 image_unlock(image, unlock);
4358 return Ok;
4361 if (!image->decoder)
4363 TRACE("image doesn't have an associated decoder\n");
4364 image_unlock(image, unlock);
4365 return Ok;
4368 /* choose an appropriate image decoder */
4369 stat = get_decoder_info_from_image(image, &codec);
4370 if (stat != Ok)
4372 WARN("can't find decoder info\n");
4373 image_unlock(image, unlock);
4374 return stat;
4377 stat = codec->select_func(image, frame);
4378 image_unlock(image, unlock);
4379 return stat;
4382 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
4384 GpStatus stat;
4385 LARGE_INTEGER seek;
4386 HRESULT hr;
4387 const struct image_codec *codec=NULL;
4389 TRACE("%p %p\n", stream, image);
4391 if (!stream || !image)
4392 return InvalidParameter;
4394 /* choose an appropriate image decoder */
4395 stat = get_decoder_info(stream, &codec);
4396 if (stat != Ok) return stat;
4398 /* seek to the start of the stream */
4399 seek.QuadPart = 0;
4400 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4401 if (FAILED(hr)) return hresult_to_status(hr);
4403 /* call on the image decoder to do the real work */
4404 stat = codec->decode_func(stream, image);
4406 /* take note of the original data format */
4407 if (stat == Ok)
4409 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
4410 return Ok;
4413 return stat;
4416 /* FIXME: no ICM */
4417 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
4419 TRACE("%p %p\n", stream, image);
4421 return GdipLoadImageFromStream(stream, image);
4424 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
4426 static int calls;
4428 TRACE("(%p,%u)\n", image, propId);
4430 if(!image)
4431 return InvalidParameter;
4433 if(!(calls++))
4434 FIXME("not implemented\n");
4436 return NotImplemented;
4439 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
4441 static int calls;
4443 if (!image || !item) return InvalidParameter;
4445 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
4447 if(!(calls++))
4448 FIXME("not implemented\n");
4450 return Ok;
4453 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
4454 GDIPCONST CLSID *clsidEncoder,
4455 GDIPCONST EncoderParameters *encoderParams)
4457 GpStatus stat;
4458 IStream *stream;
4460 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
4462 if (!image || !filename|| !clsidEncoder)
4463 return InvalidParameter;
4465 /* this might release an old file stream held by the encoder so we can re-create it below */
4466 terminate_encoder_wic(image);
4468 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4469 if (stat != Ok)
4470 return GenericError;
4472 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4474 IStream_Release(stream);
4475 return stat;
4478 /*************************************************************************
4479 * Encoding functions -
4480 * These functions encode an image in different image file formats.
4483 static GpStatus initialize_encoder_wic(IStream *stream, REFGUID container, GpImage *image)
4485 IWICImagingFactory *factory;
4486 HRESULT hr;
4488 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
4490 terminate_encoder_wic(image); /* terminate previous encoder if it exists */
4492 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
4493 if (FAILED(hr)) return hresult_to_status(hr);
4494 hr = IWICImagingFactory_CreateEncoder(factory, container, NULL, &image->encoder);
4495 IWICImagingFactory_Release(factory);
4496 if (FAILED(hr))
4498 image->encoder = NULL;
4499 return hresult_to_status(hr);
4502 hr = IWICBitmapEncoder_Initialize(image->encoder, stream, WICBitmapEncoderNoCache);
4503 if (FAILED(hr))
4505 IWICBitmapEncoder_Release(image->encoder);
4506 image->encoder = NULL;
4507 return hresult_to_status(hr);
4509 return Ok;
4512 GpStatus terminate_encoder_wic(GpImage *image)
4514 if (!image->encoder)
4515 return Ok;
4516 else
4518 HRESULT hr = IWICBitmapEncoder_Commit(image->encoder);
4519 IWICBitmapEncoder_Release(image->encoder);
4520 image->encoder = NULL;
4521 return hresult_to_status(hr);
4525 static GpStatus encode_frame_wic(IWICBitmapEncoder *encoder, GpImage *image)
4527 GpStatus stat;
4528 GpBitmap *bitmap;
4529 IWICBitmapFrameEncode *frameencode;
4530 IPropertyBag2 *encoderoptions;
4531 HRESULT hr;
4532 UINT width, height;
4533 PixelFormat gdipformat=0;
4534 const WICPixelFormatGUID *desired_wicformat;
4535 WICPixelFormatGUID wicformat;
4536 GpRect rc;
4537 BitmapData lockeddata;
4538 UINT i;
4540 if (image->type != ImageTypeBitmap)
4541 return GenericError;
4543 bitmap = (GpBitmap*)image;
4545 GdipGetImageWidth(image, &width);
4546 GdipGetImageHeight(image, &height);
4548 rc.X = 0;
4549 rc.Y = 0;
4550 rc.Width = width;
4551 rc.Height = height;
4553 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4555 if (SUCCEEDED(hr)) /* created frame */
4557 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4559 if (SUCCEEDED(hr))
4560 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4562 if (SUCCEEDED(hr))
4563 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4565 if (SUCCEEDED(hr))
4567 for (i=0; pixel_formats[i].wic_format; i++)
4569 if (pixel_formats[i].gdip_format == bitmap->format)
4571 desired_wicformat = pixel_formats[i].wic_format;
4572 gdipformat = bitmap->format;
4573 break;
4576 if (!gdipformat)
4578 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
4579 gdipformat = PixelFormat32bppARGB;
4582 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
4583 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4586 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
4588 /* Encoder doesn't support this bitmap's format. */
4589 gdipformat = 0;
4590 for (i=0; pixel_formats[i].wic_format; i++)
4592 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
4594 gdipformat = pixel_formats[i].gdip_format;
4595 break;
4598 if (!gdipformat)
4600 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
4601 hr = E_FAIL;
4605 if (SUCCEEDED(hr) && IsIndexedPixelFormat(gdipformat) && image->palette)
4606 hr = set_palette(frameencode, image->palette);
4608 if (SUCCEEDED(hr))
4610 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4611 &lockeddata);
4613 if (stat == Ok)
4615 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4616 BYTE *row;
4618 /* write one row at a time in case stride is negative */
4619 row = lockeddata.Scan0;
4620 for (i=0; i<lockeddata.Height; i++)
4622 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4623 if (FAILED(hr)) break;
4624 row += lockeddata.Stride;
4627 GdipBitmapUnlockBits(bitmap, &lockeddata);
4629 else
4630 hr = E_FAIL;
4633 if (SUCCEEDED(hr))
4634 hr = IWICBitmapFrameEncode_Commit(frameencode);
4636 IWICBitmapFrameEncode_Release(frameencode);
4637 IPropertyBag2_Release(encoderoptions);
4640 return hresult_to_status(hr);
4643 static BOOL has_encoder_param_long(GDIPCONST EncoderParameters *params, GUID param_guid, ULONG val)
4645 int param_idx, value_idx;
4647 if (!params)
4648 return FALSE;
4650 for (param_idx = 0; param_idx < params->Count; param_idx++)
4652 EncoderParameter param = params->Parameter[param_idx];
4653 if (param.Type == EncoderParameterValueTypeLong && IsEqualCLSID(&param.Guid, &param_guid))
4655 ULONG *value_array = (ULONG*) param.Value;
4656 for (value_idx = 0; value_idx < param.NumberOfValues; value_idx++)
4658 if (value_array[value_idx] == val)
4659 return TRUE;
4663 return FALSE;
4666 static GpStatus encode_image_wic(GpImage *image, IStream *stream,
4667 REFGUID container, GDIPCONST EncoderParameters *params)
4669 GpStatus status, terminate_status;
4671 if (image->type != ImageTypeBitmap)
4672 return GenericError;
4674 status = initialize_encoder_wic(stream, container, image);
4676 if (status == Ok)
4677 status = encode_frame_wic(image->encoder, image);
4679 if (!has_encoder_param_long(params, EncoderSaveFlag, EncoderValueMultiFrame))
4681 /* always try to terminate, but if something already failed earlier, keep the old status. */
4682 terminate_status = terminate_encoder_wic(image);
4683 if (status == Ok)
4684 status = terminate_status;
4687 return status;
4690 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4691 GDIPCONST EncoderParameters* params)
4693 return encode_image_wic(image, stream, &GUID_ContainerFormatBmp, params);
4696 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4697 GDIPCONST EncoderParameters* params)
4699 return encode_image_wic(image, stream, &GUID_ContainerFormatTiff, params);
4702 GpStatus encode_image_png(GpImage *image, IStream* stream,
4703 GDIPCONST EncoderParameters* params)
4705 return encode_image_wic(image, stream, &GUID_ContainerFormatPng, params);
4708 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4709 GDIPCONST EncoderParameters* params)
4711 return encode_image_wic(image, stream, &GUID_ContainerFormatJpeg, params);
4714 static GpStatus encode_image_gif(GpImage *image, IStream* stream,
4715 GDIPCONST EncoderParameters* params)
4717 return encode_image_wic(image, stream, &GUID_ContainerFormatGif, params);
4720 /*****************************************************************************
4721 * GdipSaveImageToStream [GDIPLUS.@]
4723 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4724 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4726 GpStatus stat;
4727 encode_image_func encode_image;
4728 int i;
4730 TRACE("%p, %p, %s, %p\n", image, stream, wine_dbgstr_guid(clsid), params);
4732 if(!image || !stream)
4733 return InvalidParameter;
4735 /* select correct encoder */
4736 encode_image = NULL;
4737 for (i = 0; i < NUM_CODECS; i++) {
4738 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4739 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4740 encode_image = codecs[i].encode_func;
4742 if (encode_image == NULL)
4743 return UnknownImageFormat;
4745 stat = encode_image(image, stream, params);
4747 return stat;
4750 /*****************************************************************************
4751 * GdipSaveAdd [GDIPLUS.@]
4753 * Like GdipSaveAddImage(), but encode the currently active frame of the given image into the file
4754 * or stream that is currently being encoded.
4756 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4758 return GdipSaveAddImage(image, image, params);
4761 /*****************************************************************************
4762 * GdipSaveAddImage [GDIPLUS.@]
4764 * Encode the currently active frame of additional_image into the file or stream that is currently
4765 * being encoded by the image given in the image parameter. The first frame of a multi-frame image
4766 * must be encoded using the normal GdipSaveImageToStream() or GdipSaveImageToFile() functions,
4767 * but with the "MultiFrame" encoding parameter set. The multi-frame encoding process must be
4768 * finished after adding the last frame by calling GdipSaveAdd() with the "Flush" encoding parameter
4769 * set.
4771 GpStatus WINGDIPAPI GdipSaveAddImage(GpImage *image, GpImage *additional_image,
4772 GDIPCONST EncoderParameters *params)
4774 TRACE("%p, %p, %p\n", image, additional_image, params);
4776 if (!image || !additional_image || !params)
4777 return InvalidParameter;
4779 if (!image->encoder)
4780 return Win32Error;
4782 if (has_encoder_param_long(params, EncoderSaveFlag, EncoderValueFlush))
4783 return terminate_encoder_wic(image);
4784 else if (has_encoder_param_long(params, EncoderSaveFlag, EncoderValueFrameDimensionPage))
4785 return encode_frame_wic(image->encoder, additional_image);
4786 else
4787 return InvalidParameter;
4790 /*****************************************************************************
4791 * GdipGetImagePalette [GDIPLUS.@]
4793 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4795 INT count;
4797 TRACE("(%p,%p,%i)\n", image, palette, size);
4799 if (!image || !palette)
4800 return InvalidParameter;
4802 count = image->palette ? image->palette->Count : 0;
4804 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4806 TRACE("<-- InsufficientBuffer\n");
4807 return InsufficientBuffer;
4810 if (image->palette)
4812 palette->Flags = image->palette->Flags;
4813 palette->Count = image->palette->Count;
4814 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4816 else
4818 palette->Flags = 0;
4819 palette->Count = 0;
4821 return Ok;
4824 /*****************************************************************************
4825 * GdipSetImagePalette [GDIPLUS.@]
4827 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4828 GDIPCONST ColorPalette *palette)
4830 ColorPalette *new_palette;
4832 TRACE("(%p,%p)\n", image, palette);
4834 if(!image || !palette || palette->Count > 256)
4835 return InvalidParameter;
4837 new_palette = heap_alloc_zero(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4838 if (!new_palette) return OutOfMemory;
4840 heap_free(image->palette);
4841 image->palette = new_palette;
4842 image->palette->Flags = palette->Flags;
4843 image->palette->Count = palette->Count;
4844 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4846 return Ok;
4849 /*************************************************************************
4850 * Encoders -
4851 * Structures that represent which formats we support for encoding.
4854 /* ImageCodecInfo creation routines taken from libgdiplus */
4855 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4856 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4857 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4858 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4859 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4860 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4862 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4863 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4864 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4865 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4866 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4867 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4869 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4870 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4871 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4872 static const WCHAR gif_format[] = {'G','I','F',0};
4873 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4874 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4876 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4877 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4878 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4879 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4880 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4881 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4883 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4884 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4885 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4886 static const WCHAR emf_format[] = {'E','M','F',0};
4887 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4888 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4890 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4891 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4892 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4893 static const WCHAR wmf_format[] = {'W','M','F',0};
4894 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4895 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4897 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4898 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4899 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4900 static const WCHAR png_format[] = {'P','N','G',0};
4901 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4902 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4904 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4905 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4906 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4907 static const WCHAR ico_format[] = {'I','C','O',0};
4908 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4909 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4911 static const struct image_codec codecs[NUM_CODECS] = {
4913 { /* BMP */
4914 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4915 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4916 /* CodecName */ bmp_codecname,
4917 /* DllName */ NULL,
4918 /* FormatDescription */ bmp_format,
4919 /* FilenameExtension */ bmp_extension,
4920 /* MimeType */ bmp_mimetype,
4921 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4922 /* Version */ 1,
4923 /* SigCount */ 1,
4924 /* SigSize */ 2,
4925 /* SigPattern */ bmp_sig_pattern,
4926 /* SigMask */ bmp_sig_mask,
4928 encode_image_BMP,
4929 decode_image_bmp,
4930 select_frame_wic
4933 { /* JPEG */
4934 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4935 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4936 /* CodecName */ jpeg_codecname,
4937 /* DllName */ NULL,
4938 /* FormatDescription */ jpeg_format,
4939 /* FilenameExtension */ jpeg_extension,
4940 /* MimeType */ jpeg_mimetype,
4941 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4942 /* Version */ 1,
4943 /* SigCount */ 1,
4944 /* SigSize */ 2,
4945 /* SigPattern */ jpeg_sig_pattern,
4946 /* SigMask */ jpeg_sig_mask,
4948 encode_image_jpeg,
4949 decode_image_jpeg,
4950 select_frame_wic
4953 { /* GIF */
4954 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4955 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4956 /* CodecName */ gif_codecname,
4957 /* DllName */ NULL,
4958 /* FormatDescription */ gif_format,
4959 /* FilenameExtension */ gif_extension,
4960 /* MimeType */ gif_mimetype,
4961 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4962 /* Version */ 1,
4963 /* SigCount */ 2,
4964 /* SigSize */ 6,
4965 /* SigPattern */ gif_sig_pattern,
4966 /* SigMask */ gif_sig_mask,
4968 encode_image_gif,
4969 decode_image_gif,
4970 select_frame_gif
4973 { /* TIFF */
4974 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4975 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4976 /* CodecName */ tiff_codecname,
4977 /* DllName */ NULL,
4978 /* FormatDescription */ tiff_format,
4979 /* FilenameExtension */ tiff_extension,
4980 /* MimeType */ tiff_mimetype,
4981 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4982 /* Version */ 1,
4983 /* SigCount */ 2,
4984 /* SigSize */ 4,
4985 /* SigPattern */ tiff_sig_pattern,
4986 /* SigMask */ tiff_sig_mask,
4988 encode_image_tiff,
4989 decode_image_tiff,
4990 select_frame_wic
4993 { /* EMF */
4994 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4995 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4996 /* CodecName */ emf_codecname,
4997 /* DllName */ NULL,
4998 /* FormatDescription */ emf_format,
4999 /* FilenameExtension */ emf_extension,
5000 /* MimeType */ emf_mimetype,
5001 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
5002 /* Version */ 1,
5003 /* SigCount */ 1,
5004 /* SigSize */ 4,
5005 /* SigPattern */ emf_sig_pattern,
5006 /* SigMask */ emf_sig_mask,
5008 NULL,
5009 decode_image_emf,
5010 NULL
5013 { /* WMF */
5014 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5015 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5016 /* CodecName */ wmf_codecname,
5017 /* DllName */ NULL,
5018 /* FormatDescription */ wmf_format,
5019 /* FilenameExtension */ wmf_extension,
5020 /* MimeType */ wmf_mimetype,
5021 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
5022 /* Version */ 1,
5023 /* SigCount */ 1,
5024 /* SigSize */ 2,
5025 /* SigPattern */ wmf_sig_pattern,
5026 /* SigMask */ wmf_sig_mask,
5028 NULL,
5029 decode_image_wmf,
5030 NULL
5033 { /* PNG */
5034 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5035 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5036 /* CodecName */ png_codecname,
5037 /* DllName */ NULL,
5038 /* FormatDescription */ png_format,
5039 /* FilenameExtension */ png_extension,
5040 /* MimeType */ png_mimetype,
5041 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5042 /* Version */ 1,
5043 /* SigCount */ 1,
5044 /* SigSize */ 8,
5045 /* SigPattern */ png_sig_pattern,
5046 /* SigMask */ png_sig_mask,
5048 encode_image_png,
5049 decode_image_png,
5050 select_frame_wic
5053 { /* ICO */
5054 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5055 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5056 /* CodecName */ ico_codecname,
5057 /* DllName */ NULL,
5058 /* FormatDescription */ ico_format,
5059 /* FilenameExtension */ ico_extension,
5060 /* MimeType */ ico_mimetype,
5061 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5062 /* Version */ 1,
5063 /* SigCount */ 1,
5064 /* SigSize */ 4,
5065 /* SigPattern */ ico_sig_pattern,
5066 /* SigMask */ ico_sig_mask,
5068 NULL,
5069 decode_image_icon,
5070 select_frame_wic
5074 /*****************************************************************************
5075 * GdipGetImageDecodersSize [GDIPLUS.@]
5077 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
5079 int decoder_count=0;
5080 int i;
5081 TRACE("%p %p\n", numDecoders, size);
5083 if (!numDecoders || !size)
5084 return InvalidParameter;
5086 for (i=0; i<NUM_CODECS; i++)
5088 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
5089 decoder_count++;
5092 *numDecoders = decoder_count;
5093 *size = decoder_count * sizeof(ImageCodecInfo);
5095 return Ok;
5098 /*****************************************************************************
5099 * GdipGetImageDecoders [GDIPLUS.@]
5101 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
5103 int i, decoder_count=0;
5104 TRACE("%u %u %p\n", numDecoders, size, decoders);
5106 if (!decoders ||
5107 size != numDecoders * sizeof(ImageCodecInfo))
5108 return GenericError;
5110 for (i=0; i<NUM_CODECS; i++)
5112 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
5114 if (decoder_count == numDecoders) return GenericError;
5115 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
5116 decoder_count++;
5120 if (decoder_count < numDecoders) return GenericError;
5122 return Ok;
5125 /*****************************************************************************
5126 * GdipGetImageEncodersSize [GDIPLUS.@]
5128 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
5130 int encoder_count=0;
5131 int i;
5132 TRACE("%p %p\n", numEncoders, size);
5134 if (!numEncoders || !size)
5135 return InvalidParameter;
5137 for (i=0; i<NUM_CODECS; i++)
5139 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
5140 encoder_count++;
5143 *numEncoders = encoder_count;
5144 *size = encoder_count * sizeof(ImageCodecInfo);
5146 return Ok;
5149 /*****************************************************************************
5150 * GdipGetImageEncoders [GDIPLUS.@]
5152 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
5154 int i, encoder_count=0;
5155 TRACE("%u %u %p\n", numEncoders, size, encoders);
5157 if (!encoders ||
5158 size != numEncoders * sizeof(ImageCodecInfo))
5159 return GenericError;
5161 for (i=0; i<NUM_CODECS; i++)
5163 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
5165 if (encoder_count == numEncoders) return GenericError;
5166 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
5167 encoder_count++;
5171 if (encoder_count < numEncoders) return GenericError;
5173 return Ok;
5176 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
5177 GDIPCONST CLSID* clsidEncoder, UINT *size)
5179 static int calls;
5181 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
5183 if(!(calls++))
5184 FIXME("not implemented\n");
5186 *size = 0;
5188 return NotImplemented;
5191 static PixelFormat get_16bpp_format(HBITMAP hbm)
5193 BITMAPV4HEADER bmh;
5194 HDC hdc;
5195 PixelFormat result;
5197 hdc = CreateCompatibleDC(NULL);
5199 memset(&bmh, 0, sizeof(bmh));
5200 bmh.bV4Size = sizeof(bmh);
5201 bmh.bV4Width = 1;
5202 bmh.bV4Height = 1;
5203 bmh.bV4V4Compression = BI_BITFIELDS;
5204 bmh.bV4BitCount = 16;
5206 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
5208 if (bmh.bV4RedMask == 0x7c00 &&
5209 bmh.bV4GreenMask == 0x3e0 &&
5210 bmh.bV4BlueMask == 0x1f)
5212 result = PixelFormat16bppRGB555;
5214 else if (bmh.bV4RedMask == 0xf800 &&
5215 bmh.bV4GreenMask == 0x7e0 &&
5216 bmh.bV4BlueMask == 0x1f)
5218 result = PixelFormat16bppRGB565;
5220 else
5222 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
5223 bmh.bV4GreenMask, bmh.bV4BlueMask);
5224 result = PixelFormatUndefined;
5227 DeleteDC(hdc);
5229 return result;
5232 /*****************************************************************************
5233 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
5235 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
5237 BITMAP bm;
5238 GpStatus retval;
5239 PixelFormat format;
5240 BitmapData lockeddata;
5241 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
5242 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
5244 TRACE("%p %p %p\n", hbm, hpal, bitmap);
5246 if(!hbm || !bitmap)
5247 return InvalidParameter;
5249 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
5250 return InvalidParameter;
5252 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
5253 switch(bm.bmBitsPixel) {
5254 case 1:
5255 format = PixelFormat1bppIndexed;
5256 break;
5257 case 4:
5258 format = PixelFormat4bppIndexed;
5259 break;
5260 case 8:
5261 format = PixelFormat8bppIndexed;
5262 break;
5263 case 16:
5264 format = get_16bpp_format(hbm);
5265 if (format == PixelFormatUndefined)
5266 return InvalidParameter;
5267 break;
5268 case 24:
5269 format = PixelFormat24bppRGB;
5270 break;
5271 case 32:
5272 format = PixelFormat32bppRGB;
5273 break;
5274 case 48:
5275 format = PixelFormat48bppRGB;
5276 break;
5277 default:
5278 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
5279 return InvalidParameter;
5282 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
5283 format, NULL, bitmap);
5285 if (retval == Ok)
5287 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
5288 format, &lockeddata);
5289 if (retval == Ok)
5291 HDC hdc;
5292 INT src_height;
5294 hdc = CreateCompatibleDC(NULL);
5296 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
5297 pbmi->bmiHeader.biBitCount = 0;
5299 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
5301 src_height = abs(pbmi->bmiHeader.biHeight);
5302 pbmi->bmiHeader.biHeight = -src_height;
5304 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
5306 DeleteDC(hdc);
5308 GdipBitmapUnlockBits(*bitmap, &lockeddata);
5311 /* According to the tests hpal is ignored */
5312 if (retval == Ok && pbmi->bmiHeader.biBitCount <= 8)
5314 ColorPalette *palette;
5315 int i, num_palette_entries;
5317 num_palette_entries = pbmi->bmiHeader.biClrUsed;
5318 if (!num_palette_entries)
5319 num_palette_entries = 1 << pbmi->bmiHeader.biBitCount;
5321 palette = heap_alloc_zero(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
5322 if (!palette)
5323 retval = OutOfMemory;
5324 else
5326 palette->Flags = 0;
5327 palette->Count = num_palette_entries;
5329 for (i=0; i<num_palette_entries; i++)
5331 palette->Entries[i] = 0xff000000 | pbmi->bmiColors[i].rgbRed << 16 |
5332 pbmi->bmiColors[i].rgbGreen << 8 | pbmi->bmiColors[i].rgbBlue;
5335 retval = GdipSetImagePalette(&(*bitmap)->image, palette);
5338 heap_free(palette);
5341 if (retval != Ok)
5343 GdipDisposeImage(&(*bitmap)->image);
5344 *bitmap = NULL;
5348 return retval;
5351 /*****************************************************************************
5352 * GdipCreateEffect [GDIPLUS.@]
5354 GpStatus WINGDIPAPI GdipCreateEffect(const GUID guid, CGpEffect **effect)
5356 FIXME("(%s, %p): stub\n", debugstr_guid(&guid), effect);
5358 if(!effect)
5359 return InvalidParameter;
5361 *effect = NULL;
5363 return NotImplemented;
5366 /*****************************************************************************
5367 * GdipDeleteEffect [GDIPLUS.@]
5369 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
5371 FIXME("(%p): stub\n", effect);
5372 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5373 * in Windows's gdiplus */
5374 return NotImplemented;
5377 /*****************************************************************************
5378 * GdipSetEffectParameters [GDIPLUS.@]
5380 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
5381 const VOID *params, const UINT size)
5383 static int calls;
5385 TRACE("(%p,%p,%u)\n", effect, params, size);
5387 if(!(calls++))
5388 FIXME("not implemented\n");
5390 return NotImplemented;
5393 /*****************************************************************************
5394 * GdipGetImageFlags [GDIPLUS.@]
5396 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
5398 TRACE("%p %p\n", image, flags);
5400 if(!image || !flags)
5401 return InvalidParameter;
5403 *flags = image->flags;
5405 return Ok;
5408 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
5410 TRACE("(%d, %p)\n", control, param);
5412 switch(control){
5413 case TestControlForceBilinear:
5414 if(param)
5415 FIXME("TestControlForceBilinear not handled\n");
5416 break;
5417 case TestControlNoICM:
5418 if(param)
5419 FIXME("TestControlNoICM not handled\n");
5420 break;
5421 case TestControlGetBuildNumber:
5422 *((DWORD*)param) = 3102;
5423 break;
5426 return Ok;
5429 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
5431 TRACE("%p\n", image);
5433 return Ok;
5436 /*****************************************************************************
5437 * GdipGetImageThumbnail [GDIPLUS.@]
5439 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
5440 GpImage **ret_image, GetThumbnailImageAbort cb,
5441 VOID * cb_data)
5443 GpStatus stat;
5444 GpGraphics *graphics;
5445 UINT srcwidth, srcheight;
5447 TRACE("(%p %u %u %p %p %p)\n",
5448 image, width, height, ret_image, cb, cb_data);
5450 if (!image || !ret_image)
5451 return InvalidParameter;
5453 if (!width) width = 120;
5454 if (!height) height = 120;
5456 GdipGetImageWidth(image, &srcwidth);
5457 GdipGetImageHeight(image, &srcheight);
5459 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
5460 NULL, (GpBitmap**)ret_image);
5462 if (stat == Ok)
5464 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
5466 if (stat == Ok)
5468 stat = GdipDrawImageRectRectI(graphics, image,
5469 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
5470 NULL, NULL, NULL);
5472 GdipDeleteGraphics(graphics);
5475 if (stat != Ok)
5477 GdipDisposeImage(*ret_image);
5478 *ret_image = NULL;
5482 return stat;
5485 /*****************************************************************************
5486 * GdipImageRotateFlip [GDIPLUS.@]
5488 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
5490 GpBitmap *new_bitmap;
5491 GpBitmap *bitmap;
5492 int bpp, bytesperpixel;
5493 BOOL rotate_90, flip_x, flip_y;
5494 int src_x_offset, src_y_offset;
5495 LPBYTE src_origin;
5496 UINT x, y, width, height;
5497 BitmapData src_lock, dst_lock;
5498 GpStatus stat;
5499 BOOL unlock;
5501 TRACE("(%p, %u)\n", image, type);
5503 if (!image)
5504 return InvalidParameter;
5505 if (!image_lock(image, &unlock))
5506 return ObjectBusy;
5508 rotate_90 = type&1;
5509 flip_x = (type&6) == 2 || (type&6) == 4;
5510 flip_y = (type&3) == 1 || (type&3) == 2;
5512 if (image->type != ImageTypeBitmap)
5514 FIXME("Not implemented for type %i\n", image->type);
5515 image_unlock(image, unlock);
5516 return NotImplemented;
5519 bitmap = (GpBitmap*)image;
5520 bpp = PIXELFORMATBPP(bitmap->format);
5522 if (bpp < 8)
5524 FIXME("Not implemented for %i bit images\n", bpp);
5525 image_unlock(image, unlock);
5526 return NotImplemented;
5529 if (rotate_90)
5531 width = bitmap->height;
5532 height = bitmap->width;
5534 else
5536 width = bitmap->width;
5537 height = bitmap->height;
5540 bytesperpixel = bpp/8;
5542 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5544 if (stat != Ok)
5546 image_unlock(image, unlock);
5547 return stat;
5550 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
5552 if (stat == Ok)
5554 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5556 if (stat == Ok)
5558 LPBYTE src_row, src_pixel;
5559 LPBYTE dst_row, dst_pixel;
5561 src_origin = src_lock.Scan0;
5562 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5563 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
5565 if (rotate_90)
5567 if (flip_y) src_x_offset = -src_lock.Stride;
5568 else src_x_offset = src_lock.Stride;
5569 if (flip_x) src_y_offset = -bytesperpixel;
5570 else src_y_offset = bytesperpixel;
5572 else
5574 if (flip_x) src_x_offset = -bytesperpixel;
5575 else src_x_offset = bytesperpixel;
5576 if (flip_y) src_y_offset = -src_lock.Stride;
5577 else src_y_offset = src_lock.Stride;
5580 src_row = src_origin;
5581 dst_row = dst_lock.Scan0;
5582 for (y=0; y<height; y++)
5584 src_pixel = src_row;
5585 dst_pixel = dst_row;
5586 for (x=0; x<width; x++)
5588 /* FIXME: This could probably be faster without memcpy. */
5589 memcpy(dst_pixel, src_pixel, bytesperpixel);
5590 dst_pixel += bytesperpixel;
5591 src_pixel += src_x_offset;
5593 src_row += src_y_offset;
5594 dst_row += dst_lock.Stride;
5597 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5600 GdipBitmapUnlockBits(bitmap, &src_lock);
5603 if (stat == Ok)
5604 move_bitmap(bitmap, new_bitmap, FALSE);
5605 else
5606 GdipDisposeImage(&new_bitmap->image);
5608 image_unlock(image, unlock);
5609 return stat;
5612 /*****************************************************************************
5613 * GdipImageSetAbort [GDIPLUS.@]
5615 GpStatus WINGDIPAPI GdipImageSetAbort(GpImage *image, GdiplusAbort *pabort)
5617 TRACE("(%p, %p)\n", image, pabort);
5619 if (!image)
5620 return InvalidParameter;
5622 if (pabort)
5623 FIXME("Abort callback is not supported.\n");
5625 return Ok;
5628 /*****************************************************************************
5629 * GdipBitmapConvertFormat [GDIPLUS.@]
5631 GpStatus WINGDIPAPI GdipBitmapConvertFormat(GpBitmap *bitmap, PixelFormat format, DitherType dithertype,
5632 PaletteType palettetype, ColorPalette *palette, REAL alphathreshold)
5634 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap, format, dithertype, palettetype, palette, alphathreshold);
5635 return NotImplemented;
5638 static void set_histogram_point_argb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5640 ch0[ color >> 24 ]++;
5641 ch1[(color >> 16) & 0xff]++;
5642 ch2[(color >> 8) & 0xff]++;
5643 ch3[ color & 0xff]++;
5646 static void set_histogram_point_pargb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5648 BYTE alpha = color >> 24;
5650 ch0[alpha]++;
5651 ch1[(((color >> 16) & 0xff) * alpha) / 0xff]++;
5652 ch2[(((color >> 8) & 0xff) * alpha) / 0xff]++;
5653 ch3[(( color & 0xff) * alpha) / 0xff]++;
5656 static void set_histogram_point_rgb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5658 ch0[(color >> 16) & 0xff]++;
5659 ch1[(color >> 8) & 0xff]++;
5660 ch2[ color & 0xff]++;
5663 static void set_histogram_point_gray(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5665 ch0[(76 * ((color >> 16) & 0xff) + 150 * ((color >> 8) & 0xff) + 29 * (color & 0xff)) / 0xff]++;
5668 static void set_histogram_point_b(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5670 ch0[color & 0xff]++;
5673 static void set_histogram_point_g(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5675 ch0[(color >> 8) & 0xff]++;
5678 static void set_histogram_point_r(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5680 ch0[(color >> 16) & 0xff]++;
5683 static void set_histogram_point_a(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5685 ch0[(color >> 24) & 0xff]++;
5688 /*****************************************************************************
5689 * GdipBitmapGetHistogram [GDIPLUS.@]
5691 GpStatus WINGDIPAPI GdipBitmapGetHistogram(GpBitmap *bitmap, HistogramFormat format, UINT num_of_entries,
5692 UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5694 static void (* const set_histogram_point[])(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3) =
5696 set_histogram_point_argb,
5697 set_histogram_point_pargb,
5698 set_histogram_point_rgb,
5699 set_histogram_point_gray,
5700 set_histogram_point_b,
5701 set_histogram_point_g,
5702 set_histogram_point_r,
5703 set_histogram_point_a,
5705 UINT width, height, x, y;
5707 TRACE("(%p, %d, %u, %p, %p, %p, %p)\n", bitmap, format, num_of_entries,
5708 ch0, ch1, ch2, ch3);
5710 if (!bitmap || num_of_entries != 256)
5711 return InvalidParameter;
5713 /* Make sure passed channel pointers match requested format */
5714 switch (format)
5716 case HistogramFormatARGB:
5717 case HistogramFormatPARGB:
5718 if (!ch0 || !ch1 || !ch2 || !ch3)
5719 return InvalidParameter;
5720 memset(ch0, 0, num_of_entries * sizeof(UINT));
5721 memset(ch1, 0, num_of_entries * sizeof(UINT));
5722 memset(ch2, 0, num_of_entries * sizeof(UINT));
5723 memset(ch3, 0, num_of_entries * sizeof(UINT));
5724 break;
5725 case HistogramFormatRGB:
5726 if (!ch0 || !ch1 || !ch2 || ch3)
5727 return InvalidParameter;
5728 memset(ch0, 0, num_of_entries * sizeof(UINT));
5729 memset(ch1, 0, num_of_entries * sizeof(UINT));
5730 memset(ch2, 0, num_of_entries * sizeof(UINT));
5731 break;
5732 case HistogramFormatGray:
5733 case HistogramFormatB:
5734 case HistogramFormatG:
5735 case HistogramFormatR:
5736 case HistogramFormatA:
5737 if (!ch0 || ch1 || ch2 || ch3)
5738 return InvalidParameter;
5739 memset(ch0, 0, num_of_entries * sizeof(UINT));
5740 break;
5741 default:
5742 WARN("Invalid histogram format requested, %d\n", format);
5743 return InvalidParameter;
5746 GdipGetImageWidth(&bitmap->image, &width);
5747 GdipGetImageHeight(&bitmap->image, &height);
5749 for (y = 0; y < height; y++)
5750 for (x = 0; x < width; x++)
5752 ARGB color;
5754 GdipBitmapGetPixel(bitmap, x, y, &color);
5755 set_histogram_point[format](color, ch0, ch1, ch2, ch3);
5758 return Ok;
5761 /*****************************************************************************
5762 * GdipBitmapGetHistogramSize [GDIPLUS.@]
5764 GpStatus WINGDIPAPI GdipBitmapGetHistogramSize(HistogramFormat format, UINT *num_of_entries)
5766 TRACE("(%d, %p)\n", format, num_of_entries);
5768 if (!num_of_entries)
5769 return InvalidParameter;
5771 *num_of_entries = 256;
5772 return Ok;
5775 static GpStatus create_optimal_palette(ColorPalette *palette, INT desired,
5776 BOOL transparent, GpBitmap *bitmap)
5778 GpStatus status;
5779 BitmapData data;
5780 HRESULT hr;
5781 IWICImagingFactory *factory;
5782 IWICPalette *wic_palette;
5784 if (!bitmap) return InvalidParameter;
5785 if (palette->Count < desired) return GenericError;
5787 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, PixelFormat24bppRGB, &data);
5788 if (status != Ok) return status;
5790 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
5791 if (hr != S_OK)
5793 GdipBitmapUnlockBits(bitmap, &data);
5794 return hresult_to_status(hr);
5797 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
5798 if (hr == S_OK)
5800 IWICBitmap *bitmap;
5802 /* PixelFormat24bppRGB actually stores the bitmap bits as BGR. */
5803 hr = IWICImagingFactory_CreateBitmapFromMemory(factory, data.Width, data.Height,
5804 &GUID_WICPixelFormat24bppBGR, data.Stride, data.Stride * data.Width, data.Scan0, &bitmap);
5805 if (hr == S_OK)
5807 hr = IWICPalette_InitializeFromBitmap(wic_palette, (IWICBitmapSource *)bitmap, desired, transparent);
5808 if (hr == S_OK)
5810 palette->Flags = 0;
5811 IWICPalette_GetColorCount(wic_palette, &palette->Count);
5812 IWICPalette_GetColors(wic_palette, palette->Count, palette->Entries, &palette->Count);
5815 IWICBitmap_Release(bitmap);
5818 IWICPalette_Release(wic_palette);
5821 IWICImagingFactory_Release(factory);
5822 GdipBitmapUnlockBits(bitmap, &data);
5824 return hresult_to_status(hr);
5827 /*****************************************************************************
5828 * GdipInitializePalette [GDIPLUS.@]
5830 GpStatus WINGDIPAPI GdipInitializePalette(ColorPalette *palette,
5831 PaletteType type, INT desired, BOOL transparent, GpBitmap *bitmap)
5833 TRACE("(%p,%d,%d,%d,%p)\n", palette, type, desired, transparent, bitmap);
5835 if (!palette) return InvalidParameter;
5837 switch (type)
5839 case PaletteTypeCustom:
5840 return Ok;
5842 case PaletteTypeOptimal:
5843 return create_optimal_palette(palette, desired, transparent, bitmap);
5845 /* WIC palette type enumeration matches these gdiplus enums */
5846 case PaletteTypeFixedBW:
5847 case PaletteTypeFixedHalftone8:
5848 case PaletteTypeFixedHalftone27:
5849 case PaletteTypeFixedHalftone64:
5850 case PaletteTypeFixedHalftone125:
5851 case PaletteTypeFixedHalftone216:
5852 case PaletteTypeFixedHalftone252:
5853 case PaletteTypeFixedHalftone256:
5855 ColorPalette *wic_palette;
5856 GpStatus status = Ok;
5858 wic_palette = get_palette(NULL, type);
5859 if (!wic_palette) return OutOfMemory;
5861 if (palette->Count >= wic_palette->Count)
5863 palette->Flags = wic_palette->Flags;
5864 palette->Count = wic_palette->Count;
5865 memcpy(palette->Entries, wic_palette->Entries, wic_palette->Count * sizeof(wic_palette->Entries[0]));
5867 else
5868 status = GenericError;
5870 heap_free(wic_palette);
5872 return status;
5875 default:
5876 FIXME("unknown palette type %d\n", type);
5877 break;
5880 return InvalidParameter;