winebus.sys: Add missing keyboard free_device callback.
[wine.git] / dlls / gdiplus / image.c
blobead76fc4b276d6ec115908a6a536f1b85252f5b3
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,
2199 image->yres, ((GpMetafile*)image)->printer_display);
2200 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit,
2201 image->xres, ((GpMetafile*)image)->printer_display);
2203 else if(image->type == ImageTypeBitmap){
2204 *height = ((GpBitmap*)image)->height;
2205 *width = ((GpBitmap*)image)->width;
2207 else{
2208 WARN("GpImage with no image data\n");
2209 return InvalidParameter;
2212 TRACE("returning (%f, %f)\n", *height, *width);
2213 return Ok;
2216 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2217 GpGraphics **graphics)
2219 HDC hdc;
2220 GpStatus stat;
2222 TRACE("%p %p\n", image, graphics);
2224 if(!image || !graphics)
2225 return InvalidParameter;
2227 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2229 hdc = ((GpBitmap*)image)->hdc;
2231 if(!hdc){
2232 hdc = CreateCompatibleDC(0);
2233 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2234 ((GpBitmap*)image)->hdc = hdc;
2237 stat = GdipCreateFromHDC(hdc, graphics);
2239 if (stat == Ok)
2241 (*graphics)->image = image;
2242 (*graphics)->image_type = image->type;
2243 (*graphics)->xres = image->xres;
2244 (*graphics)->yres = image->yres;
2247 else if (image->type == ImageTypeMetafile)
2248 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2249 else
2250 stat = graphics_from_image(image, graphics);
2252 return stat;
2255 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2257 TRACE("%p %p\n", image, height);
2259 if(!image || !height)
2260 return InvalidParameter;
2262 if(image->type == ImageTypeMetafile)
2263 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit,
2264 image->yres, ((GpMetafile*)image)->printer_display);
2265 else if(image->type == ImageTypeBitmap)
2266 *height = ((GpBitmap*)image)->height;
2267 else
2269 WARN("GpImage with no image data\n");
2270 return InvalidParameter;
2273 TRACE("returning %d\n", *height);
2275 return Ok;
2278 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2280 if(!image || !res)
2281 return InvalidParameter;
2283 *res = image->xres;
2285 TRACE("(%p) <-- %0.2f\n", image, *res);
2287 return Ok;
2290 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2292 TRACE("%p %p\n", image, size);
2294 if(!image || !size)
2295 return InvalidParameter;
2297 if (image->type == ImageTypeMetafile)
2299 *size = 0;
2300 return GenericError;
2303 if (!image->palette || image->palette->Count == 0)
2304 *size = sizeof(ColorPalette);
2305 else
2306 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2308 TRACE("<-- %u\n", *size);
2310 return Ok;
2313 /* FIXME: test this function for non-bitmap types */
2314 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2316 TRACE("%p %p\n", image, format);
2318 if(!image || !format)
2319 return InvalidParameter;
2321 if(image->type != ImageTypeBitmap)
2322 *format = PixelFormat24bppRGB;
2323 else
2324 *format = ((GpBitmap*) image)->format;
2326 return Ok;
2329 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2331 TRACE("(%p, %p)\n", image, format);
2333 if(!image || !format)
2334 return InvalidParameter;
2336 memcpy(format, &image->format, sizeof(GUID));
2338 return Ok;
2341 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2343 TRACE("%p %p\n", image, type);
2345 if(!image || !type)
2346 return InvalidParameter;
2348 *type = image->type;
2350 return Ok;
2353 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2355 if(!image || !res)
2356 return InvalidParameter;
2358 *res = image->yres;
2360 TRACE("(%p) <-- %0.2f\n", image, *res);
2362 return Ok;
2365 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2367 TRACE("%p %p\n", image, width);
2369 if(!image || !width)
2370 return InvalidParameter;
2372 if(image->type == ImageTypeMetafile)
2373 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit,
2374 image->xres, ((GpMetafile*)image)->printer_display);
2375 else if(image->type == ImageTypeBitmap)
2376 *width = ((GpBitmap*)image)->width;
2377 else
2379 WARN("GpImage with no image data\n");
2380 return InvalidParameter;
2383 TRACE("returning %d\n", *width);
2385 return Ok;
2388 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2390 TRACE("(%p, %p)\n", image, num);
2392 if (!image || !num) return InvalidParameter;
2394 *num = 0;
2396 if (image->type == ImageTypeBitmap)
2398 if (((GpBitmap *)image)->prop_item)
2400 *num = ((GpBitmap *)image)->prop_count;
2401 return Ok;
2404 if (((GpBitmap *)image)->metadata_reader)
2405 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2408 return Ok;
2411 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2413 HRESULT hr;
2414 IWICMetadataReader *reader;
2415 IWICEnumMetadataItem *enumerator;
2416 UINT prop_count, i, items_returned;
2418 TRACE("(%p, %u, %p)\n", image, num, list);
2420 if (!image || !list) return InvalidParameter;
2422 if (image->type != ImageTypeBitmap)
2424 FIXME("Not implemented for type %d\n", image->type);
2425 return NotImplemented;
2428 if (((GpBitmap *)image)->prop_item)
2430 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2432 for (i = 0; i < num; i++)
2434 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2437 return Ok;
2440 reader = ((GpBitmap *)image)->metadata_reader;
2441 if (!reader)
2443 if (num != 0) return InvalidParameter;
2444 return Ok;
2447 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2448 if (FAILED(hr)) return hresult_to_status(hr);
2450 if (num != prop_count) return InvalidParameter;
2452 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2453 if (FAILED(hr)) return hresult_to_status(hr);
2455 IWICEnumMetadataItem_Reset(enumerator);
2457 for (i = 0; i < num; i++)
2459 PROPVARIANT id;
2461 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2462 if (hr != S_OK) break;
2464 if (id.vt != VT_UI2)
2466 FIXME("not supported propvariant type for id: %u\n", id.vt);
2467 list[i] = 0;
2468 continue;
2470 list[i] = id.uiVal;
2473 IWICEnumMetadataItem_Release(enumerator);
2475 return hr == S_OK ? Ok : hresult_to_status(hr);
2478 static UINT propvariant_size(PROPVARIANT *value)
2480 switch (value->vt & ~VT_VECTOR)
2482 case VT_EMPTY:
2483 return 0;
2484 case VT_I1:
2485 case VT_UI1:
2486 if (!(value->vt & VT_VECTOR)) return 1;
2487 return value->caub.cElems;
2488 case VT_I2:
2489 case VT_UI2:
2490 if (!(value->vt & VT_VECTOR)) return 2;
2491 return value->caui.cElems * 2;
2492 case VT_I4:
2493 case VT_UI4:
2494 case VT_R4:
2495 if (!(value->vt & VT_VECTOR)) return 4;
2496 return value->caul.cElems * 4;
2497 case VT_I8:
2498 case VT_UI8:
2499 case VT_R8:
2500 if (!(value->vt & VT_VECTOR)) return 8;
2501 return value->cauh.cElems * 8;
2502 case VT_LPSTR:
2503 return value->pszVal ? strlen(value->pszVal) + 1 : 0;
2504 case VT_BLOB:
2505 return value->blob.cbSize;
2506 default:
2507 FIXME("not supported variant type %d\n", value->vt);
2508 return 0;
2512 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2514 HRESULT hr;
2515 IWICMetadataReader *reader;
2516 PROPVARIANT id, value;
2518 TRACE("(%p,%#x,%p)\n", image, propid, size);
2520 if (!size || !image) return InvalidParameter;
2522 if (image->type != ImageTypeBitmap)
2524 FIXME("Not implemented for type %d\n", image->type);
2525 return NotImplemented;
2528 if (((GpBitmap *)image)->prop_item)
2530 UINT i;
2532 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2534 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2536 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2537 return Ok;
2541 return PropertyNotFound;
2544 reader = ((GpBitmap *)image)->metadata_reader;
2545 if (!reader) return PropertyNotFound;
2547 id.vt = VT_UI2;
2548 id.uiVal = propid;
2549 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2550 if (FAILED(hr)) return PropertyNotFound;
2552 *size = propvariant_size(&value);
2553 if (*size) *size += sizeof(PropertyItem);
2554 PropVariantClear(&value);
2556 return Ok;
2559 #ifndef PropertyTagTypeSByte
2560 #define PropertyTagTypeSByte 6
2561 #define PropertyTagTypeSShort 8
2562 #define PropertyTagTypeFloat 11
2563 #define PropertyTagTypeDouble 12
2564 #endif
2566 static UINT vt_to_itemtype(UINT vt)
2568 static const struct
2570 UINT vt, type;
2571 } vt2type[] =
2573 { VT_I1, PropertyTagTypeSByte },
2574 { VT_UI1, PropertyTagTypeByte },
2575 { VT_I2, PropertyTagTypeSShort },
2576 { VT_UI2, PropertyTagTypeShort },
2577 { VT_I4, PropertyTagTypeSLONG },
2578 { VT_UI4, PropertyTagTypeLong },
2579 { VT_I8, PropertyTagTypeSRational },
2580 { VT_UI8, PropertyTagTypeRational },
2581 { VT_R4, PropertyTagTypeFloat },
2582 { VT_R8, PropertyTagTypeDouble },
2583 { VT_LPSTR, PropertyTagTypeASCII },
2584 { VT_BLOB, PropertyTagTypeUndefined }
2586 UINT i;
2587 for (i = 0; i < ARRAY_SIZE(vt2type); i++)
2589 if (vt2type[i].vt == vt) return vt2type[i].type;
2591 FIXME("not supported variant type %u\n", vt);
2592 return 0;
2595 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2596 UINT size, PROPID id)
2598 UINT item_size, item_type;
2600 item_size = propvariant_size(value);
2601 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2603 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2604 if (!item_type) return InvalidParameter;
2606 item->value = item + 1;
2608 switch (value->vt & ~VT_VECTOR)
2610 case VT_I1:
2611 case VT_UI1:
2612 if (!(value->vt & VT_VECTOR))
2613 *(BYTE *)item->value = value->bVal;
2614 else
2615 memcpy(item->value, value->caub.pElems, item_size);
2616 break;
2617 case VT_I2:
2618 case VT_UI2:
2619 if (!(value->vt & VT_VECTOR))
2620 *(USHORT *)item->value = value->uiVal;
2621 else
2622 memcpy(item->value, value->caui.pElems, item_size);
2623 break;
2624 case VT_I4:
2625 case VT_UI4:
2626 case VT_R4:
2627 if (!(value->vt & VT_VECTOR))
2628 *(ULONG *)item->value = value->ulVal;
2629 else
2630 memcpy(item->value, value->caul.pElems, item_size);
2631 break;
2632 case VT_I8:
2633 case VT_UI8:
2634 case VT_R8:
2635 if (!(value->vt & VT_VECTOR))
2636 *(ULONGLONG *)item->value = value->uhVal.QuadPart;
2637 else
2638 memcpy(item->value, value->cauh.pElems, item_size);
2639 break;
2640 case VT_LPSTR:
2641 memcpy(item->value, value->pszVal, item_size);
2642 break;
2643 case VT_BLOB:
2644 memcpy(item->value, value->blob.pBlobData, item_size);
2645 break;
2646 default:
2647 FIXME("not supported variant type %d\n", value->vt);
2648 return InvalidParameter;
2651 item->length = item_size;
2652 item->type = item_type;
2653 item->id = id;
2655 return Ok;
2658 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2659 PropertyItem *buffer)
2661 GpStatus stat;
2662 HRESULT hr;
2663 IWICMetadataReader *reader;
2664 PROPVARIANT id, value;
2666 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2668 if (!image || !buffer) return InvalidParameter;
2670 if (image->type != ImageTypeBitmap)
2672 FIXME("Not implemented for type %d\n", image->type);
2673 return NotImplemented;
2676 if (((GpBitmap *)image)->prop_item)
2678 UINT i;
2680 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2682 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2684 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2685 return InvalidParameter;
2687 *buffer = ((GpBitmap *)image)->prop_item[i];
2688 buffer->value = buffer + 1;
2689 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2690 return Ok;
2694 return PropertyNotFound;
2697 reader = ((GpBitmap *)image)->metadata_reader;
2698 if (!reader) return PropertyNotFound;
2700 id.vt = VT_UI2;
2701 id.uiVal = propid;
2702 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2703 if (FAILED(hr)) return PropertyNotFound;
2705 stat = propvariant_to_item(&value, buffer, size, propid);
2706 PropVariantClear(&value);
2708 return stat;
2711 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2713 HRESULT hr;
2714 IWICMetadataReader *reader;
2715 IWICEnumMetadataItem *enumerator;
2716 UINT prop_count, prop_size, i;
2717 PROPVARIANT id, value;
2719 TRACE("(%p,%p,%p)\n", image, size, count);
2721 if (!image || !size || !count) return InvalidParameter;
2723 if (image->type != ImageTypeBitmap)
2725 FIXME("Not implemented for type %d\n", image->type);
2726 return NotImplemented;
2729 if (((GpBitmap *)image)->prop_item)
2731 *count = ((GpBitmap *)image)->prop_count;
2732 *size = 0;
2734 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2736 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2739 return Ok;
2742 reader = ((GpBitmap *)image)->metadata_reader;
2743 if (!reader) return PropertyNotFound;
2745 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2746 if (FAILED(hr)) return hresult_to_status(hr);
2748 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2749 if (FAILED(hr)) return hresult_to_status(hr);
2751 IWICEnumMetadataItem_Reset(enumerator);
2753 prop_size = 0;
2755 PropVariantInit(&id);
2756 PropVariantInit(&value);
2758 for (i = 0; i < prop_count; i++)
2760 UINT items_returned, item_size;
2762 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2763 if (hr != S_OK) break;
2765 item_size = propvariant_size(&value);
2766 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2768 PropVariantClear(&id);
2769 PropVariantClear(&value);
2772 IWICEnumMetadataItem_Release(enumerator);
2774 if (hr != S_OK) return PropertyNotFound;
2776 *count = prop_count;
2777 *size = prop_size;
2778 return Ok;
2781 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2782 UINT count, PropertyItem *buf)
2784 GpStatus status;
2785 HRESULT hr;
2786 IWICMetadataReader *reader;
2787 IWICEnumMetadataItem *enumerator;
2788 UINT prop_count, prop_size, i;
2789 PROPVARIANT id, value;
2790 char *item_value;
2792 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2794 if (!image || !buf) return InvalidParameter;
2796 if (image->type != ImageTypeBitmap)
2798 FIXME("Not implemented for type %d\n", image->type);
2799 return NotImplemented;
2802 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2803 if (status != Ok) return status;
2805 if (prop_count != count || prop_size != size) return InvalidParameter;
2807 if (((GpBitmap *)image)->prop_item)
2809 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2811 item_value = (char *)(buf + prop_count);
2813 for (i = 0; i < prop_count; i++)
2815 buf[i].value = item_value;
2816 item_value += buf[i].length;
2819 return Ok;
2822 reader = ((GpBitmap *)image)->metadata_reader;
2823 if (!reader) return PropertyNotFound;
2825 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2826 if (FAILED(hr)) return hresult_to_status(hr);
2828 IWICEnumMetadataItem_Reset(enumerator);
2830 item_value = (char *)(buf + prop_count);
2832 PropVariantInit(&id);
2833 PropVariantInit(&value);
2835 for (i = 0; i < prop_count; i++)
2837 PropertyItem *item;
2838 UINT items_returned, item_size;
2840 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2841 if (hr != S_OK) break;
2843 if (id.vt != VT_UI2)
2845 FIXME("not supported propvariant type for id: %u\n", id.vt);
2846 continue;
2849 item_size = propvariant_size(&value);
2850 if (item_size)
2852 item = heap_alloc(item_size + sizeof(*item));
2854 propvariant_to_item(&value, item, item_size + sizeof(*item), id.uiVal);
2855 buf[i].id = item->id;
2856 buf[i].type = item->type;
2857 buf[i].length = item_size;
2858 buf[i].value = item_value;
2859 memcpy(item_value, item->value, item_size);
2860 item_value += item_size;
2862 heap_free(item);
2865 PropVariantClear(&id);
2866 PropVariantClear(&value);
2869 IWICEnumMetadataItem_Release(enumerator);
2871 if (hr != S_OK) return PropertyNotFound;
2873 return Ok;
2876 struct image_format_dimension
2878 const GUID *format;
2879 const GUID *dimension;
2882 static const struct image_format_dimension image_format_dimensions[] =
2884 {&ImageFormatGIF, &FrameDimensionTime},
2885 {&ImageFormatIcon, &FrameDimensionResolution},
2886 {NULL}
2889 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2890 GDIPCONST GUID* dimensionID, UINT* count)
2892 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2894 if(!image || !count)
2895 return InvalidParameter;
2897 if (!dimensionID ||
2898 IsEqualGUID(dimensionID, &image->format) ||
2899 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2900 IsEqualGUID(dimensionID, &FrameDimensionTime))
2902 *count = image->frame_count;
2903 return Ok;
2906 return InvalidParameter;
2909 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2910 UINT* count)
2912 TRACE("(%p, %p)\n", image, count);
2914 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2916 if(!image || !count)
2917 return InvalidParameter;
2919 *count = 1;
2921 return Ok;
2924 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2925 GUID* dimensionIDs, UINT count)
2927 int i;
2928 const GUID *result=NULL;
2930 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2932 if(!image || !dimensionIDs || count != 1)
2933 return InvalidParameter;
2935 for (i=0; image_format_dimensions[i].format; i++)
2937 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2939 result = image_format_dimensions[i].dimension;
2940 break;
2944 if (!result)
2945 result = &FrameDimensionPage;
2947 memcpy(dimensionIDs, result, sizeof(GUID));
2949 return Ok;
2952 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2953 GpImage **image)
2955 GpStatus stat;
2956 IStream *stream;
2958 TRACE("(%s) %p\n", debugstr_w(filename), image);
2960 if (!filename || !image)
2961 return InvalidParameter;
2963 *image = NULL;
2965 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2967 if (stat != Ok)
2968 return stat;
2970 stat = GdipLoadImageFromStream(stream, image);
2972 IStream_Release(stream);
2974 return stat;
2977 /* FIXME: no icm handling */
2978 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2980 TRACE("(%s) %p\n", debugstr_w(filename), image);
2982 return GdipLoadImageFromFile(filename, image);
2985 static void add_property(GpBitmap *bitmap, PropertyItem *item)
2987 UINT prop_size, prop_count;
2988 PropertyItem *prop_item;
2990 if (bitmap->prop_item == NULL)
2992 prop_size = prop_count = 0;
2993 prop_item = heap_alloc_zero(item->length + sizeof(PropertyItem));
2994 if (!prop_item) return;
2996 else
2998 UINT i;
2999 char *item_value;
3001 GdipGetPropertySize(&bitmap->image, &prop_size, &prop_count);
3003 prop_item = heap_alloc_zero(prop_size + item->length + sizeof(PropertyItem));
3004 if (!prop_item) return;
3005 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
3006 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
3007 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
3009 item_value = (char *)(prop_item + prop_count + 1);
3011 for (i = 0; i < prop_count; i++)
3013 prop_item[i].value = item_value;
3014 item_value += prop_item[i].length;
3018 prop_item[prop_count].id = item->id;
3019 prop_item[prop_count].type = item->type;
3020 prop_item[prop_count].length = item->length;
3021 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3022 memcpy(prop_item[prop_count].value, item->value, item->length);
3024 heap_free(bitmap->prop_item);
3025 bitmap->prop_item = prop_item;
3026 bitmap->prop_count++;
3029 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3031 HRESULT hr;
3032 GUID format;
3033 PROPVARIANT id, value;
3034 BOOL ret = FALSE;
3036 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3037 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return FALSE;
3039 PropVariantInit(&id);
3040 PropVariantInit(&value);
3042 id.vt = VT_LPWSTR;
3043 id.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3044 if (!id.pwszVal) return FALSE;
3045 lstrcpyW(id.pwszVal, prop_name);
3046 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3047 if (hr == S_OK && value.vt == VT_BOOL)
3048 ret = value.boolVal;
3050 PropVariantClear(&id);
3051 PropVariantClear(&value);
3053 return ret;
3056 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3058 HRESULT hr;
3059 GUID format;
3060 PROPVARIANT id, value;
3061 PropertyItem *item = NULL;
3063 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3064 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return NULL;
3066 PropVariantInit(&id);
3067 PropVariantInit(&value);
3069 id.vt = VT_LPWSTR;
3070 id.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3071 if (!id.pwszVal) return NULL;
3072 lstrcpyW(id.pwszVal, prop_name);
3073 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3074 if (hr == S_OK)
3076 UINT item_size = propvariant_size(&value);
3077 if (item_size)
3079 item_size += sizeof(*item);
3080 item = heap_alloc_zero(item_size);
3081 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3083 heap_free(item);
3084 item = NULL;
3089 PropVariantClear(&id);
3090 PropVariantClear(&value);
3092 return item;
3095 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3097 PropertyItem *comment;
3099 comment = get_property(reader, &GUID_MetadataFormatGifComment, L"TextEntry");
3100 if (comment)
3101 comment->id = PropertyTagExifUserComment;
3103 return comment;
3106 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3108 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3110 appext = get_property(reader, &GUID_MetadataFormatAPE, L"Application");
3111 if (appext)
3113 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3114 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3116 appdata = get_property(reader, &GUID_MetadataFormatAPE, L"Data");
3117 if (appdata)
3119 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3121 BYTE *data = appdata->value;
3122 if (data[0] == 3 && data[1] == 1)
3124 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3125 if (loop)
3127 loop->type = PropertyTagTypeShort;
3128 loop->id = PropertyTagLoopCount;
3129 loop->length = sizeof(SHORT);
3130 loop->value = loop + 1;
3131 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3139 heap_free(appext);
3140 heap_free(appdata);
3142 return loop;
3145 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3147 PropertyItem *background;
3149 background = get_property(reader, &GUID_MetadataFormatLSD, L"BackgroundColorIndex");
3150 if (background)
3151 background->id = PropertyTagIndexBackground;
3153 return background;
3156 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
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, L"GlobalColorTableFlag"))
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 PropertyItem *index = NULL;
3214 if (get_bool_property(reader, &GUID_MetadataFormatGCE, L"TransparencyFlag"))
3216 index = get_property(reader, &GUID_MetadataFormatGCE, L"TransparentColorIndex");
3217 if (index)
3218 index->id = PropertyTagIndexTransparent;
3220 return index;
3223 static LONG get_gif_frame_property(IWICBitmapFrameDecode *frame, const GUID *format, const WCHAR *property)
3225 HRESULT hr;
3226 IWICMetadataBlockReader *block_reader;
3227 IWICMetadataReader *reader;
3228 UINT block_count, i;
3229 PropertyItem *prop;
3230 LONG value = 0;
3232 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3233 if (hr == S_OK)
3235 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3236 if (hr == S_OK)
3238 for (i = 0; i < block_count; i++)
3240 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3241 if (hr == S_OK)
3243 prop = get_property(reader, format, property);
3244 if (prop)
3246 if (prop->type == PropertyTagTypeByte && prop->length == 1)
3247 value = *(BYTE *)prop->value;
3248 else if (prop->type == PropertyTagTypeShort && prop->length == 2)
3249 value = *(SHORT *)prop->value;
3251 heap_free(prop);
3253 IWICMetadataReader_Release(reader);
3257 IWICMetadataBlockReader_Release(block_reader);
3260 return value;
3263 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3265 HRESULT hr;
3266 IWICBitmapFrameDecode *frame;
3267 IWICMetadataBlockReader *block_reader;
3268 IWICMetadataReader *reader;
3269 UINT frame_count, block_count, i;
3270 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3271 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3273 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3274 if (frame_count > 1)
3276 delay = heap_alloc_zero(sizeof(*delay) + frame_count * sizeof(LONG));
3277 if (delay)
3279 LONG *value;
3281 delay->type = PropertyTagTypeLong;
3282 delay->id = PropertyTagFrameDelay;
3283 delay->length = frame_count * sizeof(LONG);
3284 delay->value = delay + 1;
3286 value = delay->value;
3288 for (i = 0; i < frame_count; i++)
3290 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3291 if (hr == S_OK)
3293 value[i] = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, L"Delay");
3294 IWICBitmapFrameDecode_Release(frame);
3296 else value[i] = 0;
3301 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3302 if (hr == S_OK)
3304 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3305 if (hr == S_OK)
3307 for (i = 0; i < block_count; i++)
3309 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3310 if (hr == S_OK)
3312 if (!comment)
3313 comment = get_gif_comment(reader);
3315 if (frame_count > 1 && !loop)
3316 loop = get_gif_loopcount(reader);
3318 if (!background)
3319 background = get_gif_background(reader);
3321 if (!palette)
3322 palette = get_gif_palette(decoder, reader);
3324 IWICMetadataReader_Release(reader);
3328 IWICMetadataBlockReader_Release(block_reader);
3331 if (frame_count > 1 && !loop)
3333 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3334 if (loop)
3336 loop->type = PropertyTagTypeShort;
3337 loop->id = PropertyTagLoopCount;
3338 loop->length = sizeof(SHORT);
3339 loop->value = loop + 1;
3340 *(SHORT *)loop->value = 1;
3344 if (delay) add_property(bitmap, delay);
3345 if (comment) add_property(bitmap, comment);
3346 if (loop) add_property(bitmap, loop);
3347 if (palette) add_property(bitmap, palette);
3348 if (background) add_property(bitmap, background);
3350 heap_free(delay);
3351 heap_free(comment);
3352 heap_free(loop);
3353 heap_free(palette);
3354 heap_free(background);
3356 /* Win7 gdiplus always returns transparent color index from frame 0 */
3357 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3358 if (hr != S_OK) return;
3360 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3361 if (hr == S_OK)
3363 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3364 if (hr == S_OK)
3366 for (i = 0; i < block_count; i++)
3368 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3369 if (hr == S_OK)
3371 if (!transparent_idx)
3372 transparent_idx = get_gif_transparent_idx(reader);
3374 IWICMetadataReader_Release(reader);
3378 IWICMetadataBlockReader_Release(block_reader);
3381 if (transparent_idx) add_property(bitmap, transparent_idx);
3382 heap_free(transparent_idx);
3384 IWICBitmapFrameDecode_Release(frame);
3387 static PropertyItem* create_prop(PROPID propid, PROPVARIANT* value)
3389 PropertyItem *item = NULL;
3390 UINT item_size = propvariant_size(value);
3392 if (item_size)
3394 item_size += sizeof(*item);
3395 item = heap_alloc_zero(item_size);
3396 if (propvariant_to_item(value, item, item_size, propid) != Ok)
3398 heap_free(item);
3399 item = NULL;
3403 return item;
3406 static ULONG get_ulong_by_index(IWICMetadataReader* reader, ULONG index)
3408 PROPVARIANT value;
3409 HRESULT hr;
3410 ULONG result=0;
3412 hr = IWICMetadataReader_GetValueByIndex(reader, index, NULL, NULL, &value);
3413 if (SUCCEEDED(hr))
3415 switch (value.vt)
3417 case VT_UI4:
3418 result = value.ulVal;
3419 break;
3420 default:
3421 ERR("unhandled case %u\n", value.vt);
3422 break;
3424 PropVariantClear(&value);
3426 return result;
3429 static void png_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3431 HRESULT hr;
3432 IWICBitmapFrameDecode *frame;
3433 IWICMetadataBlockReader *block_reader;
3434 IWICMetadataReader *reader;
3435 UINT block_count, i, j;
3436 struct keyword_info {
3437 const char* name;
3438 PROPID propid;
3439 BOOL seen;
3440 } keywords[] = {
3441 { "Title", PropertyTagImageTitle },
3442 { "Author", PropertyTagArtist },
3443 { "Description", PropertyTagImageDescription },
3444 { "Copyright", PropertyTagCopyright },
3445 { "Software", PropertyTagSoftwareUsed },
3446 { "Source", PropertyTagEquipModel },
3447 { "Comment", PropertyTagExifUserComment },
3449 BOOL seen_gamma=FALSE, seen_whitepoint=FALSE, seen_chrm=FALSE;
3451 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3452 if (hr != S_OK) return;
3454 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3455 if (hr == S_OK)
3457 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3458 if (hr == S_OK)
3460 for (i = 0; i < block_count; i++)
3462 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3463 if (hr == S_OK)
3465 GUID format;
3467 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3468 if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunktEXt, &format))
3470 PROPVARIANT name, value;
3471 PropertyItem* item;
3473 hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, &name, &value);
3475 if (SUCCEEDED(hr))
3477 if (name.vt == VT_LPSTR)
3479 for (j = 0; j < ARRAY_SIZE(keywords); j++)
3480 if (!strcmp(keywords[j].name, name.pszVal))
3481 break;
3482 if (j < ARRAY_SIZE(keywords) && !keywords[j].seen)
3484 keywords[j].seen = TRUE;
3485 item = create_prop(keywords[j].propid, &value);
3486 if (item)
3487 add_property(bitmap, item);
3488 heap_free(item);
3492 PropVariantClear(&name);
3493 PropVariantClear(&value);
3496 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA, &format))
3498 PropertyItem* item;
3500 if (!seen_gamma)
3502 item = heap_alloc_zero(sizeof(PropertyItem) + sizeof(ULONG) * 2);
3503 if (item)
3505 ULONG *rational;
3506 item->length = sizeof(ULONG) * 2;
3507 item->type = PropertyTagTypeRational;
3508 item->id = PropertyTagGamma;
3509 rational = item->value = item + 1;
3510 rational[0] = 100000;
3511 rational[1] = get_ulong_by_index(reader, 0);
3512 add_property(bitmap, item);
3513 seen_gamma = TRUE;
3514 heap_free(item);
3518 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkcHRM, &format))
3520 PropertyItem* item;
3522 if (!seen_whitepoint)
3524 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 4);
3525 if (item)
3527 ULONG *rational;
3528 item->length = sizeof(ULONG) * 4;
3529 item->type = PropertyTagTypeRational;
3530 item->id = PropertyTagWhitePoint;
3531 rational = item->value = item + 1;
3532 rational[0] = get_ulong_by_index(reader, 0);
3533 rational[1] = 100000;
3534 rational[2] = get_ulong_by_index(reader, 1);
3535 rational[3] = 100000;
3536 add_property(bitmap, item);
3537 seen_whitepoint = TRUE;
3538 GdipFree(item);
3541 if (!seen_chrm)
3543 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 12);
3544 if (item)
3546 ULONG *rational;
3547 item->length = sizeof(ULONG) * 12;
3548 item->type = PropertyTagTypeRational;
3549 item->id = PropertyTagPrimaryChromaticities;
3550 rational = item->value = item + 1;
3551 rational[0] = get_ulong_by_index(reader, 2);
3552 rational[1] = 100000;
3553 rational[2] = get_ulong_by_index(reader, 3);
3554 rational[3] = 100000;
3555 rational[4] = get_ulong_by_index(reader, 4);
3556 rational[5] = 100000;
3557 rational[6] = get_ulong_by_index(reader, 5);
3558 rational[7] = 100000;
3559 rational[8] = get_ulong_by_index(reader, 6);
3560 rational[9] = 100000;
3561 rational[10] = get_ulong_by_index(reader, 7);
3562 rational[11] = 100000;
3563 add_property(bitmap, item);
3564 seen_chrm = TRUE;
3565 GdipFree(item);
3570 IWICMetadataReader_Release(reader);
3574 IWICMetadataBlockReader_Release(block_reader);
3577 IWICBitmapFrameDecode_Release(frame);
3580 static GpStatus initialize_decoder_wic(IStream *stream, REFGUID container, IWICBitmapDecoder **decoder)
3582 IWICImagingFactory *factory;
3583 HRESULT hr;
3585 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
3587 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3588 if (FAILED(hr)) return hresult_to_status(hr);
3589 hr = IWICImagingFactory_CreateDecoder(factory, container, NULL, decoder);
3590 IWICImagingFactory_Release(factory);
3591 if (FAILED(hr)) return hresult_to_status(hr);
3593 hr = IWICBitmapDecoder_Initialize(*decoder, stream, WICDecodeMetadataCacheOnLoad);
3594 if (FAILED(hr))
3596 IWICBitmapDecoder_Release(*decoder);
3597 return hresult_to_status(hr);
3599 return Ok;
3602 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3604 static GpStatus decode_frame_wic(IWICBitmapDecoder *decoder, BOOL force_conversion,
3605 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3607 GpStatus status=Ok;
3608 GpBitmap *bitmap;
3609 HRESULT hr;
3610 IWICBitmapFrameDecode *frame;
3611 IWICBitmapSource *source=NULL;
3612 IWICMetadataBlockReader *block_reader;
3613 WICPixelFormatGUID wic_format;
3614 PixelFormat gdip_format=0;
3615 ColorPalette *palette = NULL;
3616 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3617 int i;
3618 UINT width, height, frame_count;
3619 BitmapData lockeddata;
3620 WICRect wrc;
3622 TRACE("%p,%u,%p\n", decoder, active_frame, image);
3624 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3625 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3626 if (SUCCEEDED(hr)) /* got frame */
3628 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3630 if (SUCCEEDED(hr))
3632 if (!force_conversion)
3634 for (i=0; pixel_formats[i].wic_format; i++)
3636 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3638 source = (IWICBitmapSource*)frame;
3639 IWICBitmapSource_AddRef(source);
3640 gdip_format = pixel_formats[i].gdip_format;
3641 palette_type = pixel_formats[i].palette_type;
3642 break;
3646 if (!source)
3648 /* unknown format; fall back on 32bppARGB */
3649 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3650 gdip_format = PixelFormat32bppARGB;
3652 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3655 if (SUCCEEDED(hr)) /* got source */
3657 hr = IWICBitmapSource_GetSize(source, &width, &height);
3659 if (SUCCEEDED(hr))
3660 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3661 NULL, &bitmap);
3663 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3665 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3666 gdip_format, &lockeddata);
3667 if (status == Ok) /* locked bitmap */
3669 wrc.X = 0;
3670 wrc.Width = width;
3671 wrc.Height = 1;
3672 for (i=0; i<height; i++)
3674 wrc.Y = i;
3675 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3676 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3677 if (FAILED(hr)) break;
3680 GdipBitmapUnlockBits(bitmap, &lockeddata);
3683 if (SUCCEEDED(hr) && status == Ok)
3684 *image = &bitmap->image;
3685 else
3687 *image = NULL;
3688 GdipDisposeImage(&bitmap->image);
3691 if (SUCCEEDED(hr) && status == Ok)
3693 double dpix, dpiy;
3694 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3695 if (SUCCEEDED(hr))
3697 bitmap->image.xres = dpix;
3698 bitmap->image.yres = dpiy;
3700 hr = S_OK;
3704 IWICBitmapSource_Release(source);
3707 if (SUCCEEDED(hr) && status == Ok) {
3708 bitmap->metadata_reader = NULL;
3710 if (metadata_reader)
3711 metadata_reader(bitmap, decoder, active_frame);
3712 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3714 UINT block_count = 0;
3715 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3716 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3717 IWICMetadataBlockReader_Release(block_reader);
3720 palette = get_palette(frame, palette_type);
3721 IWICBitmapFrameDecode_Release(frame);
3725 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3727 if (status == Ok)
3729 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3730 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI;
3731 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormat2bppGray) ||
3732 IsEqualGUID(&wic_format, &GUID_WICPixelFormat4bppGray) ||
3733 IsEqualGUID(&wic_format, &GUID_WICPixelFormat8bppGray) ||
3734 IsEqualGUID(&wic_format, &GUID_WICPixelFormat16bppGray))
3735 bitmap->image.flags |= ImageFlagsColorSpaceGRAY;
3736 else
3737 bitmap->image.flags |= ImageFlagsColorSpaceRGB;
3738 bitmap->image.frame_count = frame_count;
3739 bitmap->image.current_frame = active_frame;
3740 bitmap->image.decoder = decoder;
3741 IWICBitmapDecoder_AddRef(decoder);
3742 if (palette)
3744 heap_free(bitmap->image.palette);
3745 bitmap->image.palette = palette;
3747 else
3749 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3750 bitmap->image.palette->Flags = 0;
3752 TRACE("=> %p\n", *image);
3755 return status;
3758 static GpStatus decode_image_wic(IStream *stream, REFGUID container,
3759 metadata_reader_func metadata_reader, GpImage **image)
3761 IWICBitmapDecoder *decoder;
3762 GpStatus status;
3764 status = initialize_decoder_wic(stream, container, &decoder);
3765 if(status != Ok)
3766 return status;
3768 status = decode_frame_wic(decoder, FALSE, 0, metadata_reader, image);
3769 IWICBitmapDecoder_Release(decoder);
3770 return status;
3773 static GpStatus select_frame_wic(GpImage *image, UINT active_frame)
3775 GpImage *new_image;
3776 GpStatus status;
3778 status = decode_frame_wic(image->decoder, FALSE, active_frame, NULL, &new_image);
3779 if(status != Ok)
3780 return status;
3782 new_image->busy = image->busy;
3783 memcpy(&new_image->format, &image->format, sizeof(GUID));
3784 new_image->encoder = image->encoder;
3785 image->encoder = NULL;
3786 free_image_data(image);
3787 if (image->type == ImageTypeBitmap)
3788 *(GpBitmap *)image = *(GpBitmap *)new_image;
3789 else if (image->type == ImageTypeMetafile)
3790 *(GpMetafile *)image = *(GpMetafile *)new_image;
3791 new_image->type = ~0;
3792 heap_free(new_image);
3793 return Ok;
3796 static HRESULT get_gif_frame_rect(IWICBitmapFrameDecode *frame,
3797 UINT *left, UINT *top, UINT *width, UINT *height)
3799 *left = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, L"Left");
3800 *top = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, L"Top");
3802 return IWICBitmapFrameDecode_GetSize(frame, width, height);
3805 static HRESULT blit_gif_frame(GpBitmap *bitmap, IWICBitmapFrameDecode *frame, BOOL first_frame)
3807 UINT i, j, left, top, width, height;
3808 IWICBitmapSource *source;
3809 BYTE *new_bits;
3810 HRESULT hr;
3812 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3813 if(FAILED(hr))
3814 return hr;
3816 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3817 if(FAILED(hr))
3818 return hr;
3820 new_bits = heap_alloc_zero(width*height*4);
3821 if(!new_bits)
3822 return E_OUTOFMEMORY;
3824 hr = IWICBitmapSource_CopyPixels(source, NULL, width*4, width*height*4, new_bits);
3825 IWICBitmapSource_Release(source);
3826 if(FAILED(hr)) {
3827 heap_free(new_bits);
3828 return hr;
3831 for(i=0; i<height && i+top<bitmap->height; i++) {
3832 for(j=0; j<width && j+left<bitmap->width; j++) {
3833 DWORD *src = (DWORD*)(new_bits+i*width*4+j*4);
3834 DWORD *dst = (DWORD*)(bitmap->bits+(i+top)*bitmap->stride+(j+left)*4);
3836 if(first_frame || *src>>24 != 0)
3837 *dst = *src;
3840 heap_free(new_bits);
3841 return hr;
3844 static DWORD get_gif_background_color(GpBitmap *bitmap)
3846 BYTE bgcolor_idx = 0;
3847 UINT i;
3849 for(i=0; i<bitmap->prop_count; i++) {
3850 if(bitmap->prop_item[i].id == PropertyTagIndexBackground) {
3851 bgcolor_idx = *(BYTE*)bitmap->prop_item[i].value;
3852 break;
3856 for(i=0; i<bitmap->prop_count; i++) {
3857 if(bitmap->prop_item[i].id == PropertyTagIndexTransparent) {
3858 BYTE transparent_idx;
3859 transparent_idx = *(BYTE*)bitmap->prop_item[i].value;
3861 if(transparent_idx == bgcolor_idx)
3862 return 0;
3866 for(i=0; i<bitmap->prop_count; i++) {
3867 if(bitmap->prop_item[i].id == PropertyTagGlobalPalette) {
3868 if(bitmap->prop_item[i].length/3 > bgcolor_idx) {
3869 BYTE *color = ((BYTE*)bitmap->prop_item[i].value)+bgcolor_idx*3;
3870 return color[2] + (color[1]<<8) + (color[0]<<16) + (0xffu<<24);
3872 break;
3876 FIXME("can't get gif background color\n");
3877 return 0xffffffff;
3880 static GpStatus select_frame_gif(GpImage* image, UINT active_frame)
3882 GpBitmap *bitmap = (GpBitmap*)image;
3883 IWICBitmapFrameDecode *frame;
3884 int cur_frame=0, disposal;
3885 BOOL bgcolor_set = FALSE;
3886 DWORD bgcolor = 0;
3887 HRESULT hr;
3889 if(active_frame > image->current_frame) {
3890 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, image->current_frame, &frame);
3891 if(FAILED(hr))
3892 return hresult_to_status(hr);
3893 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, L"Disposal");
3894 IWICBitmapFrameDecode_Release(frame);
3896 if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND)
3897 cur_frame = image->current_frame;
3898 else if(disposal != GIF_DISPOSE_RESTORE_TO_PREV)
3899 cur_frame = image->current_frame+1;
3902 while(cur_frame != active_frame) {
3903 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, cur_frame, &frame);
3904 if(FAILED(hr))
3905 return hresult_to_status(hr);
3906 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, L"Disposal");
3908 if(disposal==GIF_DISPOSE_UNSPECIFIED || disposal==GIF_DISPOSE_DO_NOT_DISPOSE) {
3909 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3910 if(FAILED(hr))
3911 return hresult_to_status(hr);
3912 }else if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND) {
3913 UINT left, top, width, height, i, j;
3915 if(!bgcolor_set) {
3916 bgcolor = get_gif_background_color(bitmap);
3917 bgcolor_set = TRUE;
3920 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3921 if(FAILED(hr))
3922 return hresult_to_status(hr);
3923 for(i=top; i<top+height && i<bitmap->height; i++) {
3924 DWORD *bits = (DWORD*)(bitmap->bits+i*bitmap->stride);
3925 for(j=left; j<left+width && j<bitmap->width; j++)
3926 bits[j] = bgcolor;
3930 IWICBitmapFrameDecode_Release(frame);
3931 cur_frame++;
3934 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, active_frame, &frame);
3935 if(FAILED(hr))
3936 return hresult_to_status(hr);
3938 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3939 IWICBitmapFrameDecode_Release(frame);
3940 if(FAILED(hr))
3941 return hresult_to_status(hr);
3943 image->current_frame = active_frame;
3944 return Ok;
3947 static GpStatus decode_image_icon(IStream* stream, GpImage **image)
3949 return decode_image_wic(stream, &GUID_ContainerFormatIco, NULL, image);
3952 static GpStatus decode_image_bmp(IStream* stream, GpImage **image)
3954 GpStatus status;
3955 GpBitmap* bitmap;
3957 status = decode_image_wic(stream, &GUID_ContainerFormatBmp, NULL, image);
3959 bitmap = (GpBitmap*)*image;
3961 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3963 /* WIC supports bmp files with alpha, but gdiplus does not */
3964 bitmap->format = PixelFormat32bppRGB;
3967 return status;
3970 static GpStatus decode_image_jpeg(IStream* stream, GpImage **image)
3972 return decode_image_wic(stream, &GUID_ContainerFormatJpeg, NULL, image);
3975 static BOOL has_png_transparency_chunk(IStream *pIStream)
3977 LARGE_INTEGER seek;
3978 BOOL has_tRNS = FALSE;
3979 HRESULT hr;
3980 BYTE header[8];
3982 seek.QuadPart = 8;
3985 ULARGE_INTEGER chunk_start;
3986 ULONG bytesread, chunk_size;
3988 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, &chunk_start);
3989 if (FAILED(hr)) break;
3991 hr = IStream_Read(pIStream, header, 8, &bytesread);
3992 if (FAILED(hr) || bytesread < 8) break;
3994 chunk_size = (header[0] << 24) | (header[1] << 16) | (header[2] << 8) | header[3];
3995 if (!memcmp(&header[4], "tRNS", 4))
3997 has_tRNS = TRUE;
3998 break;
4001 seek.QuadPart = chunk_start.QuadPart + chunk_size + 12; /* skip data and CRC */
4002 } while (memcmp(&header[4], "IDAT", 4) && memcmp(&header[4], "IEND", 4));
4004 TRACE("has_tRNS = %d\n", has_tRNS);
4005 return has_tRNS;
4008 static GpStatus decode_image_png(IStream* stream, GpImage **image)
4010 IWICBitmapDecoder *decoder;
4011 IWICBitmapFrameDecode *frame;
4012 GpStatus status;
4013 HRESULT hr;
4014 GUID format;
4015 BOOL force_conversion = FALSE;
4017 status = initialize_decoder_wic(stream, &GUID_ContainerFormatPng, &decoder);
4018 if (status != Ok)
4019 return status;
4021 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
4022 if (hr == S_OK)
4024 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
4025 if (hr == S_OK)
4027 if (IsEqualGUID(&format, &GUID_WICPixelFormat8bppGray))
4028 force_conversion = TRUE;
4029 else if ((IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed) ||
4030 IsEqualGUID(&format, &GUID_WICPixelFormat4bppIndexed) ||
4031 IsEqualGUID(&format, &GUID_WICPixelFormat2bppIndexed) ||
4032 IsEqualGUID(&format, &GUID_WICPixelFormat1bppIndexed) ||
4033 IsEqualGUID(&format, &GUID_WICPixelFormat24bppBGR)) &&
4034 has_png_transparency_chunk(stream))
4035 force_conversion = TRUE;
4037 status = decode_frame_wic(decoder, force_conversion, 0, png_metadata_reader, image);
4039 else
4040 status = hresult_to_status(hr);
4042 IWICBitmapFrameDecode_Release(frame);
4044 else
4045 status = hresult_to_status(hr);
4047 IWICBitmapDecoder_Release(decoder);
4048 return status;
4051 static GpStatus decode_image_gif(IStream* stream, GpImage **image)
4053 IWICBitmapDecoder *decoder;
4054 UINT frame_count;
4055 GpStatus status;
4056 HRESULT hr;
4058 status = initialize_decoder_wic(stream, &GUID_ContainerFormatGif, &decoder);
4059 if(status != Ok)
4060 return status;
4062 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
4063 if(FAILED(hr))
4064 return hresult_to_status(hr);
4066 status = decode_frame_wic(decoder, frame_count > 1, 0, gif_metadata_reader, image);
4067 IWICBitmapDecoder_Release(decoder);
4068 if(status != Ok)
4069 return status;
4071 if(frame_count > 1) {
4072 heap_free((*image)->palette);
4073 (*image)->palette = NULL;
4075 return Ok;
4078 static GpStatus decode_image_tiff(IStream* stream, GpImage **image)
4080 return decode_image_wic(stream, &GUID_ContainerFormatTiff, NULL, image);
4083 static GpStatus load_wmf(IStream *stream, GpMetafile **metafile)
4085 WmfPlaceableFileHeader pfh;
4086 BOOL is_placeable = FALSE;
4087 LARGE_INTEGER seek;
4088 GpStatus status;
4089 METAHEADER mh;
4090 HMETAFILE hmf;
4091 HRESULT hr;
4092 UINT size;
4093 void *buf;
4095 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4096 if (hr != S_OK || size != sizeof(mh))
4097 return GenericError;
4099 if (((WmfPlaceableFileHeader *)&mh)->Key == WMF_PLACEABLE_KEY)
4101 seek.QuadPart = 0;
4102 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4103 if (FAILED(hr)) return hresult_to_status(hr);
4105 hr = IStream_Read(stream, &pfh, sizeof(pfh), &size);
4106 if (hr != S_OK || size != sizeof(pfh))
4107 return GenericError;
4109 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4110 if (hr != S_OK || size != sizeof(mh))
4111 return GenericError;
4113 is_placeable = TRUE;
4116 seek.QuadPart = is_placeable ? sizeof(pfh) : 0;
4117 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4118 if (FAILED(hr)) return hresult_to_status(hr);
4120 buf = heap_alloc(mh.mtSize * 2);
4121 if (!buf) return OutOfMemory;
4123 hr = IStream_Read(stream, buf, mh.mtSize * 2, &size);
4124 if (hr != S_OK || size != mh.mtSize * 2)
4126 heap_free(buf);
4127 return GenericError;
4130 hmf = SetMetaFileBitsEx(mh.mtSize * 2, buf);
4131 heap_free(buf);
4132 if (!hmf)
4133 return GenericError;
4135 status = GdipCreateMetafileFromWmf(hmf, TRUE, is_placeable ? &pfh : NULL, metafile);
4136 if (status != Ok)
4137 DeleteMetaFile(hmf);
4138 return status;
4141 static GpStatus decode_image_wmf(IStream *stream, GpImage **image)
4143 GpMetafile *metafile;
4144 GpStatus status;
4146 TRACE("%p %p\n", stream, image);
4148 if (!stream || !image)
4149 return InvalidParameter;
4151 status = load_wmf(stream, &metafile);
4152 if (status != Ok)
4154 TRACE("Could not load metafile\n");
4155 return status;
4158 *image = (GpImage *)metafile;
4159 TRACE("<-- %p\n", *image);
4161 return Ok;
4164 static GpStatus load_emf(IStream *stream, GpMetafile **metafile)
4166 LARGE_INTEGER seek;
4167 ENHMETAHEADER emh;
4168 HENHMETAFILE hemf;
4169 GpStatus status;
4170 HRESULT hr;
4171 UINT size;
4172 void *buf;
4174 hr = IStream_Read(stream, &emh, sizeof(emh), &size);
4175 if (hr != S_OK || size != sizeof(emh) || emh.dSignature != ENHMETA_SIGNATURE)
4176 return GenericError;
4178 seek.QuadPart = 0;
4179 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4180 if (FAILED(hr)) return hresult_to_status(hr);
4182 buf = heap_alloc(emh.nBytes);
4183 if (!buf) return OutOfMemory;
4185 hr = IStream_Read(stream, buf, emh.nBytes, &size);
4186 if (hr != S_OK || size != emh.nBytes)
4188 heap_free(buf);
4189 return GenericError;
4192 hemf = SetEnhMetaFileBits(emh.nBytes, buf);
4193 heap_free(buf);
4194 if (!hemf)
4195 return GenericError;
4197 status = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
4198 if (status != Ok)
4199 DeleteEnhMetaFile(hemf);
4200 return status;
4203 static GpStatus decode_image_emf(IStream *stream, GpImage **image)
4205 GpMetafile *metafile;
4206 GpStatus status;
4208 TRACE("%p %p\n", stream, image);
4210 if (!stream || !image)
4211 return InvalidParameter;
4213 status = load_emf(stream, &metafile);
4214 if (status != Ok)
4216 TRACE("Could not load metafile\n");
4217 return status;
4220 *image = (GpImage *)metafile;
4221 TRACE("<-- %p\n", *image);
4223 return Ok;
4226 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
4227 GDIPCONST EncoderParameters* params);
4229 typedef GpStatus (*decode_image_func)(IStream *stream, GpImage **image);
4231 typedef GpStatus (*select_image_func)(GpImage *image, UINT active_frame);
4233 typedef struct image_codec {
4234 ImageCodecInfo info;
4235 encode_image_func encode_func;
4236 decode_image_func decode_func;
4237 select_image_func select_func;
4238 } image_codec;
4240 typedef enum {
4241 BMP,
4242 JPEG,
4243 GIF,
4244 TIFF,
4245 EMF,
4246 WMF,
4247 PNG,
4248 ICO,
4249 NUM_CODECS
4250 } ImageFormat;
4252 static const struct image_codec codecs[NUM_CODECS];
4254 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
4256 BYTE signature[8];
4257 const BYTE *pattern, *mask;
4258 LARGE_INTEGER seek;
4259 HRESULT hr;
4260 UINT bytesread;
4261 int i;
4262 DWORD j, sig;
4264 /* seek to the start of the stream */
4265 seek.QuadPart = 0;
4266 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4267 if (FAILED(hr)) return hresult_to_status(hr);
4269 /* read the first 8 bytes */
4270 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4271 hr = IStream_Read(stream, signature, 8, &bytesread);
4272 if (FAILED(hr)) return hresult_to_status(hr);
4273 if (hr == S_FALSE || bytesread == 0) return GenericError;
4275 for (i = 0; i < NUM_CODECS; i++) {
4276 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4277 bytesread >= codecs[i].info.SigSize)
4279 for (sig=0; sig<codecs[i].info.SigCount; sig++)
4281 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
4282 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
4283 for (j=0; j<codecs[i].info.SigSize; j++)
4284 if ((signature[j] & mask[j]) != pattern[j])
4285 break;
4286 if (j == codecs[i].info.SigSize)
4288 *result = &codecs[i];
4289 return Ok;
4295 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
4296 signature[0],signature[1],signature[2],signature[3],
4297 signature[4],signature[5],signature[6],signature[7]);
4299 return GenericError;
4302 static GpStatus get_decoder_info_from_image(GpImage *image, const struct image_codec **result)
4304 int i;
4306 for (i = 0; i < NUM_CODECS; i++) {
4307 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4308 IsEqualIID(&codecs[i].info.FormatID, &image->format))
4310 *result = &codecs[i];
4311 return Ok;
4315 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image->format));
4316 return GenericError;
4319 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
4320 UINT frame)
4322 GpStatus stat;
4323 const struct image_codec *codec = NULL;
4324 BOOL unlock;
4326 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
4328 if (!image || !dimensionID)
4329 return InvalidParameter;
4330 if(!image_lock(image, &unlock))
4331 return ObjectBusy;
4333 if (frame >= image->frame_count)
4335 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
4336 image_unlock(image, unlock);
4337 return InvalidParameter;
4340 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
4342 WARN("invalid image type %d\n", image->type);
4343 image_unlock(image, unlock);
4344 return InvalidParameter;
4347 if (image->current_frame == frame)
4349 image_unlock(image, unlock);
4350 return Ok;
4353 if (!image->decoder)
4355 TRACE("image doesn't have an associated decoder\n");
4356 image_unlock(image, unlock);
4357 return Ok;
4360 /* choose an appropriate image decoder */
4361 stat = get_decoder_info_from_image(image, &codec);
4362 if (stat != Ok)
4364 WARN("can't find decoder info\n");
4365 image_unlock(image, unlock);
4366 return stat;
4369 stat = codec->select_func(image, frame);
4370 image_unlock(image, unlock);
4371 return stat;
4374 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
4376 GpStatus stat;
4377 LARGE_INTEGER seek;
4378 HRESULT hr;
4379 const struct image_codec *codec=NULL;
4381 TRACE("%p %p\n", stream, image);
4383 if (!stream || !image)
4384 return InvalidParameter;
4386 /* choose an appropriate image decoder */
4387 stat = get_decoder_info(stream, &codec);
4388 if (stat != Ok) return stat;
4390 /* seek to the start of the stream */
4391 seek.QuadPart = 0;
4392 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4393 if (FAILED(hr)) return hresult_to_status(hr);
4395 /* call on the image decoder to do the real work */
4396 stat = codec->decode_func(stream, image);
4398 /* take note of the original data format */
4399 if (stat == Ok)
4401 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
4402 return Ok;
4405 return stat;
4408 /* FIXME: no ICM */
4409 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
4411 TRACE("%p %p\n", stream, image);
4413 return GdipLoadImageFromStream(stream, image);
4416 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
4418 static int calls;
4420 TRACE("(%p,%u)\n", image, propId);
4422 if(!image)
4423 return InvalidParameter;
4425 if(!(calls++))
4426 FIXME("not implemented\n");
4428 return NotImplemented;
4431 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
4433 static int calls;
4435 if (!image || !item) return InvalidParameter;
4437 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
4439 if(!(calls++))
4440 FIXME("not implemented\n");
4442 return Ok;
4445 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
4446 GDIPCONST CLSID *clsidEncoder,
4447 GDIPCONST EncoderParameters *encoderParams)
4449 GpStatus stat;
4450 IStream *stream;
4452 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
4454 if (!image || !filename|| !clsidEncoder)
4455 return InvalidParameter;
4457 /* this might release an old file stream held by the encoder so we can re-create it below */
4458 terminate_encoder_wic(image);
4460 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4461 if (stat != Ok)
4462 return GenericError;
4464 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4466 IStream_Release(stream);
4467 return stat;
4470 /*************************************************************************
4471 * Encoding functions -
4472 * These functions encode an image in different image file formats.
4475 static GpStatus initialize_encoder_wic(IStream *stream, REFGUID container, GpImage *image)
4477 IWICImagingFactory *factory;
4478 HRESULT hr;
4480 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
4482 terminate_encoder_wic(image); /* terminate previous encoder if it exists */
4484 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
4485 if (FAILED(hr)) return hresult_to_status(hr);
4486 hr = IWICImagingFactory_CreateEncoder(factory, container, NULL, &image->encoder);
4487 IWICImagingFactory_Release(factory);
4488 if (FAILED(hr))
4490 image->encoder = NULL;
4491 return hresult_to_status(hr);
4494 hr = IWICBitmapEncoder_Initialize(image->encoder, stream, WICBitmapEncoderNoCache);
4495 if (FAILED(hr))
4497 IWICBitmapEncoder_Release(image->encoder);
4498 image->encoder = NULL;
4499 return hresult_to_status(hr);
4501 return Ok;
4504 GpStatus terminate_encoder_wic(GpImage *image)
4506 if (!image->encoder)
4507 return Ok;
4508 else
4510 HRESULT hr = IWICBitmapEncoder_Commit(image->encoder);
4511 IWICBitmapEncoder_Release(image->encoder);
4512 image->encoder = NULL;
4513 return hresult_to_status(hr);
4517 static GpStatus encode_frame_wic(IWICBitmapEncoder *encoder, GpImage *image)
4519 GpStatus stat;
4520 GpBitmap *bitmap;
4521 IWICBitmapFrameEncode *frameencode;
4522 IPropertyBag2 *encoderoptions;
4523 HRESULT hr;
4524 UINT width, height;
4525 PixelFormat gdipformat=0;
4526 const WICPixelFormatGUID *desired_wicformat;
4527 WICPixelFormatGUID wicformat;
4528 GpRect rc;
4529 BitmapData lockeddata;
4530 UINT i;
4532 if (image->type != ImageTypeBitmap)
4533 return GenericError;
4535 bitmap = (GpBitmap*)image;
4537 GdipGetImageWidth(image, &width);
4538 GdipGetImageHeight(image, &height);
4540 rc.X = 0;
4541 rc.Y = 0;
4542 rc.Width = width;
4543 rc.Height = height;
4545 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4547 if (SUCCEEDED(hr)) /* created frame */
4549 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4551 if (SUCCEEDED(hr))
4552 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4554 if (SUCCEEDED(hr))
4555 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4557 if (SUCCEEDED(hr))
4559 for (i=0; pixel_formats[i].wic_format; i++)
4561 if (pixel_formats[i].gdip_format == bitmap->format)
4563 desired_wicformat = pixel_formats[i].wic_format;
4564 gdipformat = bitmap->format;
4565 break;
4568 if (!gdipformat)
4570 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
4571 gdipformat = PixelFormat32bppARGB;
4574 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
4575 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4578 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
4580 /* Encoder doesn't support this bitmap's format. */
4581 gdipformat = 0;
4582 for (i=0; pixel_formats[i].wic_format; i++)
4584 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
4586 gdipformat = pixel_formats[i].gdip_format;
4587 break;
4590 if (!gdipformat)
4592 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
4593 hr = E_FAIL;
4597 if (SUCCEEDED(hr) && IsIndexedPixelFormat(gdipformat) && image->palette)
4598 hr = set_palette(frameencode, image->palette);
4600 if (SUCCEEDED(hr))
4602 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4603 &lockeddata);
4605 if (stat == Ok)
4607 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4608 BYTE *row;
4610 /* write one row at a time in case stride is negative */
4611 row = lockeddata.Scan0;
4612 for (i=0; i<lockeddata.Height; i++)
4614 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4615 if (FAILED(hr)) break;
4616 row += lockeddata.Stride;
4619 GdipBitmapUnlockBits(bitmap, &lockeddata);
4621 else
4622 hr = E_FAIL;
4625 if (SUCCEEDED(hr))
4626 hr = IWICBitmapFrameEncode_Commit(frameencode);
4628 IWICBitmapFrameEncode_Release(frameencode);
4629 IPropertyBag2_Release(encoderoptions);
4632 return hresult_to_status(hr);
4635 static BOOL has_encoder_param_long(GDIPCONST EncoderParameters *params, GUID param_guid, ULONG val)
4637 int param_idx, value_idx;
4639 if (!params)
4640 return FALSE;
4642 for (param_idx = 0; param_idx < params->Count; param_idx++)
4644 EncoderParameter param = params->Parameter[param_idx];
4645 if (param.Type == EncoderParameterValueTypeLong && IsEqualCLSID(&param.Guid, &param_guid))
4647 ULONG *value_array = (ULONG*) param.Value;
4648 for (value_idx = 0; value_idx < param.NumberOfValues; value_idx++)
4650 if (value_array[value_idx] == val)
4651 return TRUE;
4655 return FALSE;
4658 static GpStatus encode_image_wic(GpImage *image, IStream *stream,
4659 REFGUID container, GDIPCONST EncoderParameters *params)
4661 GpStatus status, terminate_status;
4663 if (image->type != ImageTypeBitmap)
4664 return GenericError;
4666 status = initialize_encoder_wic(stream, container, image);
4668 if (status == Ok)
4669 status = encode_frame_wic(image->encoder, image);
4671 if (!has_encoder_param_long(params, EncoderSaveFlag, EncoderValueMultiFrame))
4673 /* always try to terminate, but if something already failed earlier, keep the old status. */
4674 terminate_status = terminate_encoder_wic(image);
4675 if (status == Ok)
4676 status = terminate_status;
4679 return status;
4682 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4683 GDIPCONST EncoderParameters* params)
4685 return encode_image_wic(image, stream, &GUID_ContainerFormatBmp, params);
4688 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4689 GDIPCONST EncoderParameters* params)
4691 return encode_image_wic(image, stream, &GUID_ContainerFormatTiff, params);
4694 GpStatus encode_image_png(GpImage *image, IStream* stream,
4695 GDIPCONST EncoderParameters* params)
4697 return encode_image_wic(image, stream, &GUID_ContainerFormatPng, params);
4700 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4701 GDIPCONST EncoderParameters* params)
4703 return encode_image_wic(image, stream, &GUID_ContainerFormatJpeg, params);
4706 static GpStatus encode_image_gif(GpImage *image, IStream* stream,
4707 GDIPCONST EncoderParameters* params)
4709 return encode_image_wic(image, stream, &GUID_ContainerFormatGif, params);
4712 /*****************************************************************************
4713 * GdipSaveImageToStream [GDIPLUS.@]
4715 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4716 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4718 GpStatus stat;
4719 encode_image_func encode_image;
4720 int i;
4722 TRACE("%p, %p, %s, %p\n", image, stream, wine_dbgstr_guid(clsid), params);
4724 if(!image || !stream)
4725 return InvalidParameter;
4727 /* select correct encoder */
4728 encode_image = NULL;
4729 for (i = 0; i < NUM_CODECS; i++) {
4730 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4731 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4732 encode_image = codecs[i].encode_func;
4734 if (encode_image == NULL)
4735 return UnknownImageFormat;
4737 stat = encode_image(image, stream, params);
4739 return stat;
4742 /*****************************************************************************
4743 * GdipSaveAdd [GDIPLUS.@]
4745 * Like GdipSaveAddImage(), but encode the currently active frame of the given image into the file
4746 * or stream that is currently being encoded.
4748 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4750 return GdipSaveAddImage(image, image, params);
4753 /*****************************************************************************
4754 * GdipSaveAddImage [GDIPLUS.@]
4756 * Encode the currently active frame of additional_image into the file or stream that is currently
4757 * being encoded by the image given in the image parameter. The first frame of a multi-frame image
4758 * must be encoded using the normal GdipSaveImageToStream() or GdipSaveImageToFile() functions,
4759 * but with the "MultiFrame" encoding parameter set. The multi-frame encoding process must be
4760 * finished after adding the last frame by calling GdipSaveAdd() with the "Flush" encoding parameter
4761 * set.
4763 GpStatus WINGDIPAPI GdipSaveAddImage(GpImage *image, GpImage *additional_image,
4764 GDIPCONST EncoderParameters *params)
4766 TRACE("%p, %p, %p\n", image, additional_image, params);
4768 if (!image || !additional_image || !params)
4769 return InvalidParameter;
4771 if (!image->encoder)
4772 return Win32Error;
4774 if (has_encoder_param_long(params, EncoderSaveFlag, EncoderValueFlush))
4775 return terminate_encoder_wic(image);
4776 else if (has_encoder_param_long(params, EncoderSaveFlag, EncoderValueFrameDimensionPage))
4777 return encode_frame_wic(image->encoder, additional_image);
4778 else
4779 return InvalidParameter;
4782 /*****************************************************************************
4783 * GdipGetImagePalette [GDIPLUS.@]
4785 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4787 INT count;
4789 TRACE("(%p,%p,%i)\n", image, palette, size);
4791 if (!image || !palette)
4792 return InvalidParameter;
4794 count = image->palette ? image->palette->Count : 0;
4796 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4798 TRACE("<-- InsufficientBuffer\n");
4799 return InsufficientBuffer;
4802 if (image->palette)
4804 palette->Flags = image->palette->Flags;
4805 palette->Count = image->palette->Count;
4806 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4808 else
4810 palette->Flags = 0;
4811 palette->Count = 0;
4813 return Ok;
4816 /*****************************************************************************
4817 * GdipSetImagePalette [GDIPLUS.@]
4819 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4820 GDIPCONST ColorPalette *palette)
4822 ColorPalette *new_palette;
4824 TRACE("(%p,%p)\n", image, palette);
4826 if(!image || !palette || palette->Count > 256)
4827 return InvalidParameter;
4829 new_palette = heap_alloc_zero(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4830 if (!new_palette) return OutOfMemory;
4832 heap_free(image->palette);
4833 image->palette = new_palette;
4834 image->palette->Flags = palette->Flags;
4835 image->palette->Count = palette->Count;
4836 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4838 return Ok;
4841 /*************************************************************************
4842 * Encoders -
4843 * Structures that represent which formats we support for encoding.
4846 /* ImageCodecInfo creation routines taken from libgdiplus */
4847 static const WCHAR bmp_codecname[] = L"Built-in BMP";
4848 static const WCHAR bmp_extension[] = L"*.BMP;*.DIB;*.RLE";
4849 static const WCHAR bmp_mimetype[] = L"image/bmp";
4850 static const WCHAR bmp_format[] = L"BMP";
4851 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4852 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4854 static const WCHAR jpeg_codecname[] = L"Built-in JPEG";
4855 static const WCHAR jpeg_extension[] = L"*.JPG;*.JPEG;*.JPE;*.JFIF";
4856 static const WCHAR jpeg_mimetype[] = L"image/jpeg";
4857 static const WCHAR jpeg_format[] = L"JPEG";
4858 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4859 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4861 static const WCHAR gif_codecname[] = L"Built-in GIF";
4862 static const WCHAR gif_extension[] = L"*.GIF";
4863 static const WCHAR gif_mimetype[] = L"image/gif";
4864 static const WCHAR gif_format[] = L"GIF";
4865 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4866 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4868 static const WCHAR tiff_codecname[] = L"Built-in TIFF";
4869 static const WCHAR tiff_extension[] = L"*.TIFF;*.TIF";
4870 static const WCHAR tiff_mimetype[] = L"image/tiff";
4871 static const WCHAR tiff_format[] = L"TIFF";
4872 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4873 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4875 static const WCHAR emf_codecname[] = L"Built-in EMF";
4876 static const WCHAR emf_extension[] = L"*.EMF";
4877 static const WCHAR emf_mimetype[] = L"image/x-emf";
4878 static const WCHAR emf_format[] = L"EMF";
4879 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4880 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4882 static const WCHAR wmf_codecname[] = L"Built-in WMF";
4883 static const WCHAR wmf_extension[] = L"*.WMF";
4884 static const WCHAR wmf_mimetype[] = L"image/x-wmf";
4885 static const WCHAR wmf_format[] = L"WMF";
4886 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4887 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4889 static const WCHAR png_codecname[] = L"Built-in PNG";
4890 static const WCHAR png_extension[] = L"*.PNG";
4891 static const WCHAR png_mimetype[] = L"image/png";
4892 static const WCHAR png_format[] = L"PNG";
4893 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4894 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4896 static const WCHAR ico_codecname[] = L"Built-in ICO";
4897 static const WCHAR ico_extension[] = L"*.ICO";
4898 static const WCHAR ico_mimetype[] = L"image/x-icon";
4899 static const WCHAR ico_format[] = L"ICO";
4900 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4901 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4903 static const struct image_codec codecs[NUM_CODECS] = {
4905 { /* BMP */
4906 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4907 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4908 /* CodecName */ bmp_codecname,
4909 /* DllName */ NULL,
4910 /* FormatDescription */ bmp_format,
4911 /* FilenameExtension */ bmp_extension,
4912 /* MimeType */ bmp_mimetype,
4913 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4914 /* Version */ 1,
4915 /* SigCount */ 1,
4916 /* SigSize */ 2,
4917 /* SigPattern */ bmp_sig_pattern,
4918 /* SigMask */ bmp_sig_mask,
4920 encode_image_BMP,
4921 decode_image_bmp,
4922 select_frame_wic
4925 { /* JPEG */
4926 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4927 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4928 /* CodecName */ jpeg_codecname,
4929 /* DllName */ NULL,
4930 /* FormatDescription */ jpeg_format,
4931 /* FilenameExtension */ jpeg_extension,
4932 /* MimeType */ jpeg_mimetype,
4933 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4934 /* Version */ 1,
4935 /* SigCount */ 1,
4936 /* SigSize */ 2,
4937 /* SigPattern */ jpeg_sig_pattern,
4938 /* SigMask */ jpeg_sig_mask,
4940 encode_image_jpeg,
4941 decode_image_jpeg,
4942 select_frame_wic
4945 { /* GIF */
4946 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4947 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4948 /* CodecName */ gif_codecname,
4949 /* DllName */ NULL,
4950 /* FormatDescription */ gif_format,
4951 /* FilenameExtension */ gif_extension,
4952 /* MimeType */ gif_mimetype,
4953 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4954 /* Version */ 1,
4955 /* SigCount */ 2,
4956 /* SigSize */ 6,
4957 /* SigPattern */ gif_sig_pattern,
4958 /* SigMask */ gif_sig_mask,
4960 encode_image_gif,
4961 decode_image_gif,
4962 select_frame_gif
4965 { /* TIFF */
4966 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4967 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4968 /* CodecName */ tiff_codecname,
4969 /* DllName */ NULL,
4970 /* FormatDescription */ tiff_format,
4971 /* FilenameExtension */ tiff_extension,
4972 /* MimeType */ tiff_mimetype,
4973 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4974 /* Version */ 1,
4975 /* SigCount */ 2,
4976 /* SigSize */ 4,
4977 /* SigPattern */ tiff_sig_pattern,
4978 /* SigMask */ tiff_sig_mask,
4980 encode_image_tiff,
4981 decode_image_tiff,
4982 select_frame_wic
4985 { /* EMF */
4986 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4987 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4988 /* CodecName */ emf_codecname,
4989 /* DllName */ NULL,
4990 /* FormatDescription */ emf_format,
4991 /* FilenameExtension */ emf_extension,
4992 /* MimeType */ emf_mimetype,
4993 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4994 /* Version */ 1,
4995 /* SigCount */ 1,
4996 /* SigSize */ 4,
4997 /* SigPattern */ emf_sig_pattern,
4998 /* SigMask */ emf_sig_mask,
5000 NULL,
5001 decode_image_emf,
5002 NULL
5005 { /* WMF */
5006 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5007 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5008 /* CodecName */ wmf_codecname,
5009 /* DllName */ NULL,
5010 /* FormatDescription */ wmf_format,
5011 /* FilenameExtension */ wmf_extension,
5012 /* MimeType */ wmf_mimetype,
5013 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
5014 /* Version */ 1,
5015 /* SigCount */ 1,
5016 /* SigSize */ 2,
5017 /* SigPattern */ wmf_sig_pattern,
5018 /* SigMask */ wmf_sig_mask,
5020 NULL,
5021 decode_image_wmf,
5022 NULL
5025 { /* PNG */
5026 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5027 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5028 /* CodecName */ png_codecname,
5029 /* DllName */ NULL,
5030 /* FormatDescription */ png_format,
5031 /* FilenameExtension */ png_extension,
5032 /* MimeType */ png_mimetype,
5033 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5034 /* Version */ 1,
5035 /* SigCount */ 1,
5036 /* SigSize */ 8,
5037 /* SigPattern */ png_sig_pattern,
5038 /* SigMask */ png_sig_mask,
5040 encode_image_png,
5041 decode_image_png,
5042 select_frame_wic
5045 { /* ICO */
5046 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5047 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5048 /* CodecName */ ico_codecname,
5049 /* DllName */ NULL,
5050 /* FormatDescription */ ico_format,
5051 /* FilenameExtension */ ico_extension,
5052 /* MimeType */ ico_mimetype,
5053 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
5054 /* Version */ 1,
5055 /* SigCount */ 1,
5056 /* SigSize */ 4,
5057 /* SigPattern */ ico_sig_pattern,
5058 /* SigMask */ ico_sig_mask,
5060 NULL,
5061 decode_image_icon,
5062 select_frame_wic
5066 /*****************************************************************************
5067 * GdipGetImageDecodersSize [GDIPLUS.@]
5069 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
5071 int decoder_count=0;
5072 int i;
5073 TRACE("%p %p\n", numDecoders, size);
5075 if (!numDecoders || !size)
5076 return InvalidParameter;
5078 for (i=0; i<NUM_CODECS; i++)
5080 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
5081 decoder_count++;
5084 *numDecoders = decoder_count;
5085 *size = decoder_count * sizeof(ImageCodecInfo);
5087 return Ok;
5090 /*****************************************************************************
5091 * GdipGetImageDecoders [GDIPLUS.@]
5093 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
5095 int i, decoder_count=0;
5096 TRACE("%u %u %p\n", numDecoders, size, decoders);
5098 if (!decoders ||
5099 size != numDecoders * sizeof(ImageCodecInfo))
5100 return GenericError;
5102 for (i=0; i<NUM_CODECS; i++)
5104 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
5106 if (decoder_count == numDecoders) return GenericError;
5107 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
5108 decoder_count++;
5112 if (decoder_count < numDecoders) return GenericError;
5114 return Ok;
5117 /*****************************************************************************
5118 * GdipGetImageEncodersSize [GDIPLUS.@]
5120 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
5122 int encoder_count=0;
5123 int i;
5124 TRACE("%p %p\n", numEncoders, size);
5126 if (!numEncoders || !size)
5127 return InvalidParameter;
5129 for (i=0; i<NUM_CODECS; i++)
5131 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
5132 encoder_count++;
5135 *numEncoders = encoder_count;
5136 *size = encoder_count * sizeof(ImageCodecInfo);
5138 return Ok;
5141 /*****************************************************************************
5142 * GdipGetImageEncoders [GDIPLUS.@]
5144 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
5146 int i, encoder_count=0;
5147 TRACE("%u %u %p\n", numEncoders, size, encoders);
5149 if (!encoders ||
5150 size != numEncoders * sizeof(ImageCodecInfo))
5151 return GenericError;
5153 for (i=0; i<NUM_CODECS; i++)
5155 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
5157 if (encoder_count == numEncoders) return GenericError;
5158 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
5159 encoder_count++;
5163 if (encoder_count < numEncoders) return GenericError;
5165 return Ok;
5168 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
5169 GDIPCONST CLSID* clsidEncoder, UINT *size)
5171 static int calls;
5173 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
5175 if(!(calls++))
5176 FIXME("not implemented\n");
5178 *size = 0;
5180 return NotImplemented;
5183 static PixelFormat get_16bpp_format(HBITMAP hbm)
5185 BITMAPV4HEADER bmh;
5186 HDC hdc;
5187 PixelFormat result;
5189 hdc = CreateCompatibleDC(NULL);
5191 memset(&bmh, 0, sizeof(bmh));
5192 bmh.bV4Size = sizeof(bmh);
5193 bmh.bV4Width = 1;
5194 bmh.bV4Height = 1;
5195 bmh.bV4V4Compression = BI_BITFIELDS;
5196 bmh.bV4BitCount = 16;
5198 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
5200 if (bmh.bV4RedMask == 0x7c00 &&
5201 bmh.bV4GreenMask == 0x3e0 &&
5202 bmh.bV4BlueMask == 0x1f)
5204 result = PixelFormat16bppRGB555;
5206 else if (bmh.bV4RedMask == 0xf800 &&
5207 bmh.bV4GreenMask == 0x7e0 &&
5208 bmh.bV4BlueMask == 0x1f)
5210 result = PixelFormat16bppRGB565;
5212 else
5214 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
5215 bmh.bV4GreenMask, bmh.bV4BlueMask);
5216 result = PixelFormatUndefined;
5219 DeleteDC(hdc);
5221 return result;
5224 /*****************************************************************************
5225 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
5227 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
5229 BITMAP bm;
5230 GpStatus retval;
5231 PixelFormat format;
5232 BitmapData lockeddata;
5233 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
5234 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
5236 TRACE("%p %p %p\n", hbm, hpal, bitmap);
5238 if(!hbm || !bitmap)
5239 return InvalidParameter;
5241 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
5242 return InvalidParameter;
5244 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
5245 switch(bm.bmBitsPixel) {
5246 case 1:
5247 format = PixelFormat1bppIndexed;
5248 break;
5249 case 4:
5250 format = PixelFormat4bppIndexed;
5251 break;
5252 case 8:
5253 format = PixelFormat8bppIndexed;
5254 break;
5255 case 16:
5256 format = get_16bpp_format(hbm);
5257 if (format == PixelFormatUndefined)
5258 return InvalidParameter;
5259 break;
5260 case 24:
5261 format = PixelFormat24bppRGB;
5262 break;
5263 case 32:
5264 format = PixelFormat32bppRGB;
5265 break;
5266 case 48:
5267 format = PixelFormat48bppRGB;
5268 break;
5269 default:
5270 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
5271 return InvalidParameter;
5274 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
5275 format, NULL, bitmap);
5277 if (retval == Ok)
5279 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
5280 format, &lockeddata);
5281 if (retval == Ok)
5283 HDC hdc;
5284 INT src_height;
5286 hdc = CreateCompatibleDC(NULL);
5288 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
5289 pbmi->bmiHeader.biBitCount = 0;
5291 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
5293 src_height = abs(pbmi->bmiHeader.biHeight);
5294 pbmi->bmiHeader.biHeight = -src_height;
5296 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
5298 DeleteDC(hdc);
5300 GdipBitmapUnlockBits(*bitmap, &lockeddata);
5303 /* According to the tests hpal is ignored */
5304 if (retval == Ok && pbmi->bmiHeader.biBitCount <= 8)
5306 ColorPalette *palette;
5307 int i, num_palette_entries;
5309 num_palette_entries = pbmi->bmiHeader.biClrUsed;
5310 if (!num_palette_entries)
5311 num_palette_entries = 1 << pbmi->bmiHeader.biBitCount;
5313 palette = heap_alloc_zero(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
5314 if (!palette)
5315 retval = OutOfMemory;
5316 else
5318 palette->Flags = 0;
5319 palette->Count = num_palette_entries;
5321 for (i=0; i<num_palette_entries; i++)
5323 palette->Entries[i] = 0xff000000 | pbmi->bmiColors[i].rgbRed << 16 |
5324 pbmi->bmiColors[i].rgbGreen << 8 | pbmi->bmiColors[i].rgbBlue;
5327 retval = GdipSetImagePalette(&(*bitmap)->image, palette);
5330 heap_free(palette);
5333 if (retval != Ok)
5335 GdipDisposeImage(&(*bitmap)->image);
5336 *bitmap = NULL;
5340 return retval;
5343 /*****************************************************************************
5344 * GdipCreateEffect [GDIPLUS.@]
5346 GpStatus WINGDIPAPI GdipCreateEffect(const GUID guid, CGpEffect **effect)
5348 FIXME("(%s, %p): stub\n", debugstr_guid(&guid), effect);
5350 if(!effect)
5351 return InvalidParameter;
5353 *effect = NULL;
5355 return NotImplemented;
5358 /*****************************************************************************
5359 * GdipDeleteEffect [GDIPLUS.@]
5361 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
5363 FIXME("(%p): stub\n", effect);
5364 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5365 * in Windows's gdiplus */
5366 return NotImplemented;
5369 /*****************************************************************************
5370 * GdipSetEffectParameters [GDIPLUS.@]
5372 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
5373 const VOID *params, const UINT size)
5375 static int calls;
5377 TRACE("(%p,%p,%u)\n", effect, params, size);
5379 if(!(calls++))
5380 FIXME("not implemented\n");
5382 return NotImplemented;
5385 /*****************************************************************************
5386 * GdipGetImageFlags [GDIPLUS.@]
5388 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
5390 TRACE("%p %p\n", image, flags);
5392 if(!image || !flags)
5393 return InvalidParameter;
5395 *flags = image->flags;
5397 return Ok;
5400 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
5402 TRACE("(%d, %p)\n", control, param);
5404 switch(control){
5405 case TestControlForceBilinear:
5406 if(param)
5407 FIXME("TestControlForceBilinear not handled\n");
5408 break;
5409 case TestControlNoICM:
5410 if(param)
5411 FIXME("TestControlNoICM not handled\n");
5412 break;
5413 case TestControlGetBuildNumber:
5414 *((DWORD*)param) = 3102;
5415 break;
5418 return Ok;
5421 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
5423 TRACE("%p\n", image);
5425 return Ok;
5428 /*****************************************************************************
5429 * GdipGetImageThumbnail [GDIPLUS.@]
5431 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
5432 GpImage **ret_image, GetThumbnailImageAbort cb,
5433 VOID * cb_data)
5435 GpStatus stat;
5436 GpGraphics *graphics;
5437 UINT srcwidth, srcheight;
5439 TRACE("(%p %u %u %p %p %p)\n",
5440 image, width, height, ret_image, cb, cb_data);
5442 if (!image || !ret_image)
5443 return InvalidParameter;
5445 if (!width) width = 120;
5446 if (!height) height = 120;
5448 GdipGetImageWidth(image, &srcwidth);
5449 GdipGetImageHeight(image, &srcheight);
5451 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
5452 NULL, (GpBitmap**)ret_image);
5454 if (stat == Ok)
5456 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
5458 if (stat == Ok)
5460 stat = GdipDrawImageRectRectI(graphics, image,
5461 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
5462 NULL, NULL, NULL);
5464 GdipDeleteGraphics(graphics);
5467 if (stat != Ok)
5469 GdipDisposeImage(*ret_image);
5470 *ret_image = NULL;
5474 return stat;
5477 /*****************************************************************************
5478 * GdipImageRotateFlip [GDIPLUS.@]
5480 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
5482 GpBitmap *new_bitmap;
5483 GpBitmap *bitmap;
5484 int bpp, bytesperpixel;
5485 BOOL rotate_90, flip_x, flip_y;
5486 int src_x_offset, src_y_offset;
5487 LPBYTE src_origin;
5488 UINT x, y, width, height;
5489 BitmapData src_lock, dst_lock;
5490 GpStatus stat;
5491 BOOL unlock;
5493 TRACE("(%p, %u)\n", image, type);
5495 if (!image)
5496 return InvalidParameter;
5497 if (!image_lock(image, &unlock))
5498 return ObjectBusy;
5500 rotate_90 = type&1;
5501 flip_x = (type&6) == 2 || (type&6) == 4;
5502 flip_y = (type&3) == 1 || (type&3) == 2;
5504 if (image->type != ImageTypeBitmap)
5506 FIXME("Not implemented for type %i\n", image->type);
5507 image_unlock(image, unlock);
5508 return NotImplemented;
5511 bitmap = (GpBitmap*)image;
5512 bpp = PIXELFORMATBPP(bitmap->format);
5514 if (bpp < 8)
5516 FIXME("Not implemented for %i bit images\n", bpp);
5517 image_unlock(image, unlock);
5518 return NotImplemented;
5521 if (rotate_90)
5523 width = bitmap->height;
5524 height = bitmap->width;
5526 else
5528 width = bitmap->width;
5529 height = bitmap->height;
5532 bytesperpixel = bpp/8;
5534 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5536 if (stat != Ok)
5538 image_unlock(image, unlock);
5539 return stat;
5542 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
5544 if (stat == Ok)
5546 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5548 if (stat == Ok)
5550 LPBYTE src_row, src_pixel;
5551 LPBYTE dst_row, dst_pixel;
5553 src_origin = src_lock.Scan0;
5554 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5555 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
5557 if (rotate_90)
5559 if (flip_y) src_x_offset = -src_lock.Stride;
5560 else src_x_offset = src_lock.Stride;
5561 if (flip_x) src_y_offset = -bytesperpixel;
5562 else src_y_offset = bytesperpixel;
5564 else
5566 if (flip_x) src_x_offset = -bytesperpixel;
5567 else src_x_offset = bytesperpixel;
5568 if (flip_y) src_y_offset = -src_lock.Stride;
5569 else src_y_offset = src_lock.Stride;
5572 src_row = src_origin;
5573 dst_row = dst_lock.Scan0;
5574 for (y=0; y<height; y++)
5576 src_pixel = src_row;
5577 dst_pixel = dst_row;
5578 for (x=0; x<width; x++)
5580 /* FIXME: This could probably be faster without memcpy. */
5581 memcpy(dst_pixel, src_pixel, bytesperpixel);
5582 dst_pixel += bytesperpixel;
5583 src_pixel += src_x_offset;
5585 src_row += src_y_offset;
5586 dst_row += dst_lock.Stride;
5589 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5592 GdipBitmapUnlockBits(bitmap, &src_lock);
5595 if (stat == Ok)
5596 move_bitmap(bitmap, new_bitmap, FALSE);
5597 else
5598 GdipDisposeImage(&new_bitmap->image);
5600 image_unlock(image, unlock);
5601 return stat;
5604 /*****************************************************************************
5605 * GdipImageSetAbort [GDIPLUS.@]
5607 GpStatus WINGDIPAPI GdipImageSetAbort(GpImage *image, GdiplusAbort *pabort)
5609 TRACE("(%p, %p)\n", image, pabort);
5611 if (!image)
5612 return InvalidParameter;
5614 if (pabort)
5615 FIXME("Abort callback is not supported.\n");
5617 return Ok;
5620 /*****************************************************************************
5621 * GdipBitmapConvertFormat [GDIPLUS.@]
5623 GpStatus WINGDIPAPI GdipBitmapConvertFormat(GpBitmap *bitmap, PixelFormat format, DitherType dithertype,
5624 PaletteType palettetype, ColorPalette *palette, REAL alphathreshold)
5626 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap, format, dithertype, palettetype, palette, alphathreshold);
5627 return NotImplemented;
5630 static void set_histogram_point_argb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5632 ch0[ color >> 24 ]++;
5633 ch1[(color >> 16) & 0xff]++;
5634 ch2[(color >> 8) & 0xff]++;
5635 ch3[ color & 0xff]++;
5638 static void set_histogram_point_pargb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5640 BYTE alpha = color >> 24;
5642 ch0[alpha]++;
5643 ch1[(((color >> 16) & 0xff) * alpha) / 0xff]++;
5644 ch2[(((color >> 8) & 0xff) * alpha) / 0xff]++;
5645 ch3[(( color & 0xff) * alpha) / 0xff]++;
5648 static void set_histogram_point_rgb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5650 ch0[(color >> 16) & 0xff]++;
5651 ch1[(color >> 8) & 0xff]++;
5652 ch2[ color & 0xff]++;
5655 static void set_histogram_point_gray(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5657 ch0[(76 * ((color >> 16) & 0xff) + 150 * ((color >> 8) & 0xff) + 29 * (color & 0xff)) / 0xff]++;
5660 static void set_histogram_point_b(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5662 ch0[color & 0xff]++;
5665 static void set_histogram_point_g(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5667 ch0[(color >> 8) & 0xff]++;
5670 static void set_histogram_point_r(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5672 ch0[(color >> 16) & 0xff]++;
5675 static void set_histogram_point_a(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5677 ch0[(color >> 24) & 0xff]++;
5680 /*****************************************************************************
5681 * GdipBitmapGetHistogram [GDIPLUS.@]
5683 GpStatus WINGDIPAPI GdipBitmapGetHistogram(GpBitmap *bitmap, HistogramFormat format, UINT num_of_entries,
5684 UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5686 static void (* const set_histogram_point[])(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3) =
5688 set_histogram_point_argb,
5689 set_histogram_point_pargb,
5690 set_histogram_point_rgb,
5691 set_histogram_point_gray,
5692 set_histogram_point_b,
5693 set_histogram_point_g,
5694 set_histogram_point_r,
5695 set_histogram_point_a,
5697 UINT width, height, x, y;
5699 TRACE("(%p, %d, %u, %p, %p, %p, %p)\n", bitmap, format, num_of_entries,
5700 ch0, ch1, ch2, ch3);
5702 if (!bitmap || num_of_entries != 256)
5703 return InvalidParameter;
5705 /* Make sure passed channel pointers match requested format */
5706 switch (format)
5708 case HistogramFormatARGB:
5709 case HistogramFormatPARGB:
5710 if (!ch0 || !ch1 || !ch2 || !ch3)
5711 return InvalidParameter;
5712 memset(ch0, 0, num_of_entries * sizeof(UINT));
5713 memset(ch1, 0, num_of_entries * sizeof(UINT));
5714 memset(ch2, 0, num_of_entries * sizeof(UINT));
5715 memset(ch3, 0, num_of_entries * sizeof(UINT));
5716 break;
5717 case HistogramFormatRGB:
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 break;
5724 case HistogramFormatGray:
5725 case HistogramFormatB:
5726 case HistogramFormatG:
5727 case HistogramFormatR:
5728 case HistogramFormatA:
5729 if (!ch0 || ch1 || ch2 || ch3)
5730 return InvalidParameter;
5731 memset(ch0, 0, num_of_entries * sizeof(UINT));
5732 break;
5733 default:
5734 WARN("Invalid histogram format requested, %d\n", format);
5735 return InvalidParameter;
5738 GdipGetImageWidth(&bitmap->image, &width);
5739 GdipGetImageHeight(&bitmap->image, &height);
5741 for (y = 0; y < height; y++)
5742 for (x = 0; x < width; x++)
5744 ARGB color;
5746 GdipBitmapGetPixel(bitmap, x, y, &color);
5747 set_histogram_point[format](color, ch0, ch1, ch2, ch3);
5750 return Ok;
5753 /*****************************************************************************
5754 * GdipBitmapGetHistogramSize [GDIPLUS.@]
5756 GpStatus WINGDIPAPI GdipBitmapGetHistogramSize(HistogramFormat format, UINT *num_of_entries)
5758 TRACE("(%d, %p)\n", format, num_of_entries);
5760 if (!num_of_entries)
5761 return InvalidParameter;
5763 *num_of_entries = 256;
5764 return Ok;
5767 static GpStatus create_optimal_palette(ColorPalette *palette, INT desired,
5768 BOOL transparent, GpBitmap *bitmap)
5770 GpStatus status;
5771 BitmapData data;
5772 HRESULT hr;
5773 IWICImagingFactory *factory;
5774 IWICPalette *wic_palette;
5776 if (!bitmap) return InvalidParameter;
5777 if (palette->Count < desired) return GenericError;
5779 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, PixelFormat24bppRGB, &data);
5780 if (status != Ok) return status;
5782 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
5783 if (hr != S_OK)
5785 GdipBitmapUnlockBits(bitmap, &data);
5786 return hresult_to_status(hr);
5789 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
5790 if (hr == S_OK)
5792 IWICBitmap *bitmap;
5794 /* PixelFormat24bppRGB actually stores the bitmap bits as BGR. */
5795 hr = IWICImagingFactory_CreateBitmapFromMemory(factory, data.Width, data.Height,
5796 &GUID_WICPixelFormat24bppBGR, data.Stride, data.Stride * data.Width, data.Scan0, &bitmap);
5797 if (hr == S_OK)
5799 hr = IWICPalette_InitializeFromBitmap(wic_palette, (IWICBitmapSource *)bitmap, desired, transparent);
5800 if (hr == S_OK)
5802 palette->Flags = 0;
5803 IWICPalette_GetColorCount(wic_palette, &palette->Count);
5804 IWICPalette_GetColors(wic_palette, palette->Count, palette->Entries, &palette->Count);
5807 IWICBitmap_Release(bitmap);
5810 IWICPalette_Release(wic_palette);
5813 IWICImagingFactory_Release(factory);
5814 GdipBitmapUnlockBits(bitmap, &data);
5816 return hresult_to_status(hr);
5819 /*****************************************************************************
5820 * GdipInitializePalette [GDIPLUS.@]
5822 GpStatus WINGDIPAPI GdipInitializePalette(ColorPalette *palette,
5823 PaletteType type, INT desired, BOOL transparent, GpBitmap *bitmap)
5825 TRACE("(%p,%d,%d,%d,%p)\n", palette, type, desired, transparent, bitmap);
5827 if (!palette) return InvalidParameter;
5829 switch (type)
5831 case PaletteTypeCustom:
5832 return Ok;
5834 case PaletteTypeOptimal:
5835 return create_optimal_palette(palette, desired, transparent, bitmap);
5837 /* WIC palette type enumeration matches these gdiplus enums */
5838 case PaletteTypeFixedBW:
5839 case PaletteTypeFixedHalftone8:
5840 case PaletteTypeFixedHalftone27:
5841 case PaletteTypeFixedHalftone64:
5842 case PaletteTypeFixedHalftone125:
5843 case PaletteTypeFixedHalftone216:
5844 case PaletteTypeFixedHalftone252:
5845 case PaletteTypeFixedHalftone256:
5847 ColorPalette *wic_palette;
5848 GpStatus status = Ok;
5850 wic_palette = get_palette(NULL, type);
5851 if (!wic_palette) return OutOfMemory;
5853 if (palette->Count >= wic_palette->Count)
5855 palette->Flags = wic_palette->Flags;
5856 palette->Count = wic_palette->Count;
5857 memcpy(palette->Entries, wic_palette->Entries, wic_palette->Count * sizeof(wic_palette->Entries[0]));
5859 else
5860 status = GenericError;
5862 heap_free(wic_palette);
5864 return status;
5867 default:
5868 FIXME("unknown palette type %d\n", type);
5869 break;
5872 return InvalidParameter;