windowscodecs/tests: Add more tests for loading PNG images in various color formats.
[wine.git] / dlls / gdiplus / image.c
blob80ad65f050d9deae8691fc5ef4589adbe1161b69
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_WICPixelFormatBlackWhite, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
57 { &GUID_WICPixelFormat1bppIndexed, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
58 { &GUID_WICPixelFormat8bppGray, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedGray256 },
59 { &GUID_WICPixelFormat8bppIndexed, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedHalftone256 },
60 { &GUID_WICPixelFormat16bppBGR555, PixelFormat16bppRGB555, WICBitmapPaletteTypeFixedHalftone256 },
61 { &GUID_WICPixelFormat24bppBGR, PixelFormat24bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
62 { &GUID_WICPixelFormat32bppBGR, PixelFormat32bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
63 { &GUID_WICPixelFormat32bppBGRA, PixelFormat32bppARGB, WICBitmapPaletteTypeFixedHalftone256 },
64 { &GUID_WICPixelFormat32bppPBGRA, PixelFormat32bppPARGB, WICBitmapPaletteTypeFixedHalftone256 },
65 { NULL }
68 static ColorPalette *get_palette(IWICBitmapFrameDecode *frame, WICBitmapPaletteType palette_type)
70 HRESULT hr;
71 IWICImagingFactory *factory;
72 IWICPalette *wic_palette;
73 ColorPalette *palette = NULL;
75 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
76 if (hr != S_OK) return NULL;
78 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
79 if (hr == S_OK)
81 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
82 if (frame)
83 hr = IWICBitmapFrameDecode_CopyPalette(frame, wic_palette);
84 if (hr != S_OK)
86 TRACE("using predefined palette %#x\n", palette_type);
87 hr = IWICPalette_InitializePredefined(wic_palette, palette_type, FALSE);
89 if (hr == S_OK)
91 WICBitmapPaletteType type;
92 BOOL alpha;
93 UINT count;
95 IWICPalette_GetColorCount(wic_palette, &count);
96 palette = heap_alloc(2 * sizeof(UINT) + count * sizeof(ARGB));
97 IWICPalette_GetColors(wic_palette, count, palette->Entries, &palette->Count);
99 IWICPalette_GetType(wic_palette, &type);
100 switch(type) {
101 case WICBitmapPaletteTypeFixedGray4:
102 case WICBitmapPaletteTypeFixedGray16:
103 case WICBitmapPaletteTypeFixedGray256:
104 palette->Flags = PaletteFlagsGrayScale;
105 break;
106 case WICBitmapPaletteTypeFixedHalftone8:
107 case WICBitmapPaletteTypeFixedHalftone27:
108 case WICBitmapPaletteTypeFixedHalftone64:
109 case WICBitmapPaletteTypeFixedHalftone125:
110 case WICBitmapPaletteTypeFixedHalftone216:
111 case WICBitmapPaletteTypeFixedHalftone252:
112 case WICBitmapPaletteTypeFixedHalftone256:
113 palette->Flags = PaletteFlagsHalftone;
114 break;
115 default:
116 palette->Flags = 0;
118 IWICPalette_HasAlpha(wic_palette, &alpha);
119 if(alpha)
120 palette->Flags |= PaletteFlagsHasAlpha;
122 IWICPalette_Release(wic_palette);
124 IWICImagingFactory_Release(factory);
125 return palette;
128 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
129 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
131 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
133 * Note: According to Jose Roca's GDI+ docs, this function is not
134 * implemented in Windows's GDI+.
136 return NotImplemented;
139 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
140 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
141 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
143 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
145 * Note: According to Jose Roca's GDI+ docs, this function is not
146 * implemented in Windows's GDI+.
148 return NotImplemented;
151 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
153 *index = (row[x/8]>>(7-x%8)) & 1;
156 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
158 if (x & 1)
159 *index = row[x/2]&0xf;
160 else
161 *index = row[x/2]>>4;
164 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
166 *index = row[x];
169 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
170 const BYTE *row, UINT x)
172 *r = *g = *b = row[x*2+1];
173 *a = 255;
176 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
177 const BYTE *row, UINT x)
179 WORD pixel = *((const WORD*)(row)+x);
180 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
181 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
182 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
183 *a = 255;
186 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
187 const BYTE *row, UINT x)
189 WORD pixel = *((const WORD*)(row)+x);
190 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
191 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
192 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
193 *a = 255;
196 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
197 const BYTE *row, UINT x)
199 WORD pixel = *((const WORD*)(row)+x);
200 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
201 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
202 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
203 if ((pixel&0x8000) == 0x8000)
204 *a = 255;
205 else
206 *a = 0;
209 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
210 const BYTE *row, UINT x)
212 *r = row[x*3+2];
213 *g = row[x*3+1];
214 *b = row[x*3];
215 *a = 255;
218 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
219 const BYTE *row, UINT x)
221 *r = row[x*4+2];
222 *g = row[x*4+1];
223 *b = row[x*4];
224 *a = 255;
227 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
228 const BYTE *row, UINT x)
230 *r = row[x*4+2];
231 *g = row[x*4+1];
232 *b = row[x*4];
233 *a = row[x*4+3];
236 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
237 const BYTE *row, UINT x)
239 *a = row[x*4+3];
240 if (*a == 0)
241 *r = *g = *b = 0;
242 else
244 *r = row[x*4+2] * 255 / *a;
245 *g = row[x*4+1] * 255 / *a;
246 *b = row[x*4] * 255 / *a;
250 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
251 const BYTE *row, UINT x)
253 *r = row[x*6+5];
254 *g = row[x*6+3];
255 *b = row[x*6+1];
256 *a = 255;
259 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
260 const BYTE *row, UINT x)
262 *r = row[x*8+5];
263 *g = row[x*8+3];
264 *b = row[x*8+1];
265 *a = row[x*8+7];
268 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
269 const BYTE *row, UINT x)
271 *a = row[x*8+7];
272 if (*a == 0)
273 *r = *g = *b = 0;
274 else
276 *r = row[x*8+5] * 255 / *a;
277 *g = row[x*8+3] * 255 / *a;
278 *b = row[x*8+1] * 255 / *a;
282 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
283 ARGB *color)
285 BYTE r, g, b, a;
286 BYTE index;
287 BYTE *row;
289 if(!bitmap || !color ||
290 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
291 return InvalidParameter;
293 row = bitmap->bits+bitmap->stride*y;
295 switch (bitmap->format)
297 case PixelFormat1bppIndexed:
298 getpixel_1bppIndexed(&index,row,x);
299 break;
300 case PixelFormat4bppIndexed:
301 getpixel_4bppIndexed(&index,row,x);
302 break;
303 case PixelFormat8bppIndexed:
304 getpixel_8bppIndexed(&index,row,x);
305 break;
306 case PixelFormat16bppGrayScale:
307 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
308 break;
309 case PixelFormat16bppRGB555:
310 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
311 break;
312 case PixelFormat16bppRGB565:
313 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
314 break;
315 case PixelFormat16bppARGB1555:
316 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
317 break;
318 case PixelFormat24bppRGB:
319 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
320 break;
321 case PixelFormat32bppRGB:
322 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
323 break;
324 case PixelFormat32bppARGB:
325 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
326 break;
327 case PixelFormat32bppPARGB:
328 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
329 break;
330 case PixelFormat48bppRGB:
331 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
332 break;
333 case PixelFormat64bppARGB:
334 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
335 break;
336 case PixelFormat64bppPARGB:
337 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
338 break;
339 default:
340 FIXME("not implemented for format 0x%x\n", bitmap->format);
341 return NotImplemented;
344 if (bitmap->format & PixelFormatIndexed)
345 *color = bitmap->image.palette->Entries[index];
346 else
347 *color = a<<24|r<<16|g<<8|b;
349 return Ok;
352 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
354 BYTE index = 0;
355 int best_distance = 0x7fff;
356 int distance;
357 UINT i;
359 if (!palette) return 0;
360 /* This algorithm scans entire palette,
361 computes difference from desired color (all color components have equal weight)
362 and returns the index of color with least difference.
364 Note: Maybe it could be replaced with a better algorithm for better image quality
365 and performance, though better algorithm would probably need some pre-built lookup
366 tables and thus may actually be slower if this method is called only few times per
367 every image.
369 for(i=0;i<palette->Count;i++) {
370 ARGB color=palette->Entries[i];
371 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
372 if (distance<best_distance) {
373 best_distance=distance;
374 index=i;
377 return index;
380 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
381 BYTE *row, UINT x, ColorPalette *palette)
383 BYTE index = get_palette_index(r,g,b,a,palette);
384 row[x]=index;
387 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
388 BYTE *row, UINT x, ColorPalette *palette)
390 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
393 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
394 BYTE *row, UINT x, ColorPalette *palette)
396 if (x & 1)
397 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
398 else
399 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
402 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
403 BYTE *row, UINT x)
405 *((WORD*)(row)+x) = (r+g+b)*85;
408 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
409 BYTE *row, UINT x)
411 *((WORD*)(row)+x) = (r<<7&0x7c00)|
412 (g<<2&0x03e0)|
413 (b>>3&0x001f);
416 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
417 BYTE *row, UINT x)
419 *((WORD*)(row)+x) = (r<<8&0xf800)|
420 (g<<3&0x07e0)|
421 (b>>3&0x001f);
424 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
425 BYTE *row, UINT x)
427 *((WORD*)(row)+x) = (a<<8&0x8000)|
428 (r<<7&0x7c00)|
429 (g<<2&0x03e0)|
430 (b>>3&0x001f);
433 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
434 BYTE *row, UINT x)
436 row[x*3+2] = r;
437 row[x*3+1] = g;
438 row[x*3] = b;
441 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
442 BYTE *row, UINT x)
444 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
447 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
448 BYTE *row, UINT x)
450 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
453 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
454 BYTE *row, UINT x)
456 r = r * a / 255;
457 g = g * a / 255;
458 b = b * a / 255;
459 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
462 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
463 BYTE *row, UINT x)
465 row[x*6+5] = row[x*6+4] = r;
466 row[x*6+3] = row[x*6+2] = g;
467 row[x*6+1] = row[x*6] = b;
470 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
471 BYTE *row, UINT x)
473 UINT64 a64=a, r64=r, g64=g, b64=b;
474 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
477 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
478 BYTE *row, UINT x)
480 UINT64 a64, r64, g64, b64;
481 a64 = a * 257;
482 r64 = r * a / 255;
483 g64 = g * a / 255;
484 b64 = b * a / 255;
485 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
488 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
489 ARGB color)
491 BYTE a, r, g, b;
492 BYTE *row;
494 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
495 return InvalidParameter;
497 a = color>>24;
498 r = color>>16;
499 g = color>>8;
500 b = color;
502 row = bitmap->bits + bitmap->stride * y;
504 switch (bitmap->format)
506 case PixelFormat16bppGrayScale:
507 setpixel_16bppGrayScale(r,g,b,a,row,x);
508 break;
509 case PixelFormat16bppRGB555:
510 setpixel_16bppRGB555(r,g,b,a,row,x);
511 break;
512 case PixelFormat16bppRGB565:
513 setpixel_16bppRGB565(r,g,b,a,row,x);
514 break;
515 case PixelFormat16bppARGB1555:
516 setpixel_16bppARGB1555(r,g,b,a,row,x);
517 break;
518 case PixelFormat24bppRGB:
519 setpixel_24bppRGB(r,g,b,a,row,x);
520 break;
521 case PixelFormat32bppRGB:
522 setpixel_32bppRGB(r,g,b,a,row,x);
523 break;
524 case PixelFormat32bppARGB:
525 setpixel_32bppARGB(r,g,b,a,row,x);
526 break;
527 case PixelFormat32bppPARGB:
528 setpixel_32bppPARGB(r,g,b,a,row,x);
529 break;
530 case PixelFormat48bppRGB:
531 setpixel_48bppRGB(r,g,b,a,row,x);
532 break;
533 case PixelFormat64bppARGB:
534 setpixel_64bppARGB(r,g,b,a,row,x);
535 break;
536 case PixelFormat64bppPARGB:
537 setpixel_64bppPARGB(r,g,b,a,row,x);
538 break;
539 case PixelFormat8bppIndexed:
540 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
541 break;
542 case PixelFormat4bppIndexed:
543 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
544 break;
545 case PixelFormat1bppIndexed:
546 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
547 break;
548 default:
549 FIXME("not implemented for format 0x%x\n", bitmap->format);
550 return NotImplemented;
553 return Ok;
556 GpStatus convert_pixels(INT width, INT height,
557 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
558 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
559 ColorPalette *palette)
561 INT x, y;
563 if (src_format == dst_format ||
564 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
566 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
567 for (y=0; y<height; y++)
568 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
569 return Ok;
572 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
573 for (y=0; y<height; y++) \
574 for (x=0; x<width; x++) { \
575 BYTE index; \
576 ARGB argb; \
577 BYTE *color = (BYTE *)&argb; \
578 getpixel_function(&index, src_bits+src_stride*y, x); \
579 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
580 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
582 return Ok; \
583 } while (0);
585 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
586 for (y=0; y<height; y++) \
587 for (x=0; x<width; x++) { \
588 BYTE r, g, b, a; \
589 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
590 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
592 return Ok; \
593 } while (0);
595 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
596 for (y=0; y<height; y++) \
597 for (x=0; x<width; x++) { \
598 BYTE r, g, b, a; \
599 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
600 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
602 return Ok; \
603 } while (0);
605 switch (src_format)
607 case PixelFormat1bppIndexed:
608 switch (dst_format)
610 case PixelFormat16bppGrayScale:
611 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
612 case PixelFormat16bppRGB555:
613 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
614 case PixelFormat16bppRGB565:
615 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
616 case PixelFormat16bppARGB1555:
617 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
618 case PixelFormat24bppRGB:
619 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
620 case PixelFormat32bppRGB:
621 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
622 case PixelFormat32bppARGB:
623 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
624 case PixelFormat32bppPARGB:
625 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
626 case PixelFormat48bppRGB:
627 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
628 case PixelFormat64bppARGB:
629 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
630 default:
631 break;
633 break;
634 case PixelFormat4bppIndexed:
635 switch (dst_format)
637 case PixelFormat16bppGrayScale:
638 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
639 case PixelFormat16bppRGB555:
640 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
641 case PixelFormat16bppRGB565:
642 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
643 case PixelFormat16bppARGB1555:
644 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
645 case PixelFormat24bppRGB:
646 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
647 case PixelFormat32bppRGB:
648 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
649 case PixelFormat32bppARGB:
650 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
651 case PixelFormat32bppPARGB:
652 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
653 case PixelFormat48bppRGB:
654 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
655 case PixelFormat64bppARGB:
656 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
657 default:
658 break;
660 break;
661 case PixelFormat8bppIndexed:
662 switch (dst_format)
664 case PixelFormat16bppGrayScale:
665 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
666 case PixelFormat16bppRGB555:
667 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
668 case PixelFormat16bppRGB565:
669 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
670 case PixelFormat16bppARGB1555:
671 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
672 case PixelFormat24bppRGB:
673 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
674 case PixelFormat32bppRGB:
675 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
676 case PixelFormat32bppARGB:
677 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
678 case PixelFormat32bppPARGB:
679 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
680 case PixelFormat48bppRGB:
681 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
682 case PixelFormat64bppARGB:
683 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
684 default:
685 break;
687 break;
688 case PixelFormat16bppGrayScale:
689 switch (dst_format)
691 case PixelFormat1bppIndexed:
692 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
693 case PixelFormat8bppIndexed:
694 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
695 case PixelFormat16bppRGB555:
696 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
697 case PixelFormat16bppRGB565:
698 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
699 case PixelFormat16bppARGB1555:
700 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
701 case PixelFormat24bppRGB:
702 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
703 case PixelFormat32bppRGB:
704 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
705 case PixelFormat32bppARGB:
706 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
707 case PixelFormat32bppPARGB:
708 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
709 case PixelFormat48bppRGB:
710 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
711 case PixelFormat64bppARGB:
712 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
713 default:
714 break;
716 break;
717 case PixelFormat16bppRGB555:
718 switch (dst_format)
720 case PixelFormat1bppIndexed:
721 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
722 case PixelFormat8bppIndexed:
723 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
724 case PixelFormat16bppGrayScale:
725 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
726 case PixelFormat16bppRGB565:
727 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
728 case PixelFormat16bppARGB1555:
729 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
730 case PixelFormat24bppRGB:
731 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
732 case PixelFormat32bppRGB:
733 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
734 case PixelFormat32bppARGB:
735 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
736 case PixelFormat32bppPARGB:
737 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
738 case PixelFormat48bppRGB:
739 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
740 case PixelFormat64bppARGB:
741 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
742 default:
743 break;
745 break;
746 case PixelFormat16bppRGB565:
747 switch (dst_format)
749 case PixelFormat1bppIndexed:
750 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
751 case PixelFormat8bppIndexed:
752 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
753 case PixelFormat16bppGrayScale:
754 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
755 case PixelFormat16bppRGB555:
756 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
757 case PixelFormat16bppARGB1555:
758 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
759 case PixelFormat24bppRGB:
760 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
761 case PixelFormat32bppRGB:
762 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
763 case PixelFormat32bppARGB:
764 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
765 case PixelFormat32bppPARGB:
766 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
767 case PixelFormat48bppRGB:
768 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
769 case PixelFormat64bppARGB:
770 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
771 default:
772 break;
774 break;
775 case PixelFormat16bppARGB1555:
776 switch (dst_format)
778 case PixelFormat1bppIndexed:
779 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
780 case PixelFormat8bppIndexed:
781 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
782 case PixelFormat16bppGrayScale:
783 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
784 case PixelFormat16bppRGB555:
785 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
786 case PixelFormat16bppRGB565:
787 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
788 case PixelFormat24bppRGB:
789 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
790 case PixelFormat32bppRGB:
791 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
792 case PixelFormat32bppARGB:
793 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
794 case PixelFormat32bppPARGB:
795 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
796 case PixelFormat48bppRGB:
797 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
798 case PixelFormat64bppARGB:
799 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
800 default:
801 break;
803 break;
804 case PixelFormat24bppRGB:
805 switch (dst_format)
807 case PixelFormat1bppIndexed:
808 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
809 case PixelFormat8bppIndexed:
810 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
811 case PixelFormat16bppGrayScale:
812 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
813 case PixelFormat16bppRGB555:
814 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
815 case PixelFormat16bppRGB565:
816 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
817 case PixelFormat16bppARGB1555:
818 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
819 case PixelFormat32bppRGB:
820 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
821 case PixelFormat32bppARGB:
822 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
823 case PixelFormat32bppPARGB:
824 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
825 case PixelFormat48bppRGB:
826 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
827 case PixelFormat64bppARGB:
828 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
829 default:
830 break;
832 break;
833 case PixelFormat32bppRGB:
834 switch (dst_format)
836 case PixelFormat1bppIndexed:
837 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
838 case PixelFormat8bppIndexed:
839 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
840 case PixelFormat16bppGrayScale:
841 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
842 case PixelFormat16bppRGB555:
843 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
844 case PixelFormat16bppRGB565:
845 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
846 case PixelFormat16bppARGB1555:
847 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
848 case PixelFormat24bppRGB:
849 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
850 case PixelFormat32bppARGB:
851 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
852 case PixelFormat32bppPARGB:
853 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
854 case PixelFormat48bppRGB:
855 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
856 case PixelFormat64bppARGB:
857 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
858 default:
859 break;
861 break;
862 case PixelFormat32bppARGB:
863 switch (dst_format)
865 case PixelFormat1bppIndexed:
866 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
867 case PixelFormat8bppIndexed:
868 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
869 case PixelFormat16bppGrayScale:
870 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
871 case PixelFormat16bppRGB555:
872 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
873 case PixelFormat16bppRGB565:
874 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
875 case PixelFormat16bppARGB1555:
876 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
877 case PixelFormat24bppRGB:
878 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
879 case PixelFormat32bppPARGB:
880 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
881 return Ok;
882 case PixelFormat48bppRGB:
883 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
884 case PixelFormat64bppARGB:
885 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
886 default:
887 break;
889 break;
890 case PixelFormat32bppPARGB:
891 switch (dst_format)
893 case PixelFormat1bppIndexed:
894 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
895 case PixelFormat8bppIndexed:
896 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
897 case PixelFormat16bppGrayScale:
898 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
899 case PixelFormat16bppRGB555:
900 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
901 case PixelFormat16bppRGB565:
902 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
903 case PixelFormat16bppARGB1555:
904 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
905 case PixelFormat24bppRGB:
906 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
907 case PixelFormat32bppRGB:
908 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
909 case PixelFormat32bppARGB:
910 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
911 case PixelFormat48bppRGB:
912 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
913 case PixelFormat64bppARGB:
914 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
915 default:
916 break;
918 break;
919 case PixelFormat48bppRGB:
920 switch (dst_format)
922 case PixelFormat1bppIndexed:
923 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
924 case PixelFormat8bppIndexed:
925 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
926 case PixelFormat16bppGrayScale:
927 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
928 case PixelFormat16bppRGB555:
929 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
930 case PixelFormat16bppRGB565:
931 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
932 case PixelFormat16bppARGB1555:
933 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
934 case PixelFormat24bppRGB:
935 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
936 case PixelFormat32bppRGB:
937 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
938 case PixelFormat32bppARGB:
939 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
940 case PixelFormat32bppPARGB:
941 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
942 case PixelFormat64bppARGB:
943 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
944 default:
945 break;
947 break;
948 case PixelFormat64bppARGB:
949 switch (dst_format)
951 case PixelFormat1bppIndexed:
952 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
953 case PixelFormat8bppIndexed:
954 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
955 case PixelFormat16bppGrayScale:
956 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
957 case PixelFormat16bppRGB555:
958 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
959 case PixelFormat16bppRGB565:
960 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
961 case PixelFormat16bppARGB1555:
962 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
963 case PixelFormat24bppRGB:
964 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
965 case PixelFormat32bppRGB:
966 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
967 case PixelFormat32bppARGB:
968 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
969 case PixelFormat32bppPARGB:
970 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
971 case PixelFormat48bppRGB:
972 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
973 default:
974 break;
976 break;
977 case PixelFormat64bppPARGB:
978 switch (dst_format)
980 case PixelFormat1bppIndexed:
981 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
982 case PixelFormat8bppIndexed:
983 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
984 case PixelFormat16bppGrayScale:
985 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
986 case PixelFormat16bppRGB555:
987 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
988 case PixelFormat16bppRGB565:
989 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
990 case PixelFormat16bppARGB1555:
991 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
992 case PixelFormat24bppRGB:
993 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
994 case PixelFormat32bppRGB:
995 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
996 case PixelFormat32bppARGB:
997 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
998 case PixelFormat32bppPARGB:
999 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1000 case PixelFormat48bppRGB:
1001 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1002 case PixelFormat64bppARGB:
1003 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1004 default:
1005 break;
1007 break;
1008 default:
1009 break;
1012 #undef convert_indexed_to_rgb
1013 #undef convert_rgb_to_rgb
1015 return NotImplemented;
1018 /* This function returns a pointer to an array of pixels that represents the
1019 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1020 * flags. It is correct behavior that a user who calls this function with write
1021 * privileges can write to the whole bitmap (not just the area in rect).
1023 * FIXME: only used portion of format is bits per pixel. */
1024 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1025 UINT flags, PixelFormat format, BitmapData* lockeddata)
1027 INT bitspp = PIXELFORMATBPP(format);
1028 GpRect act_rect; /* actual rect to be used */
1029 GpStatus stat;
1030 BOOL unlock;
1032 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1034 if(!lockeddata || !bitmap)
1035 return InvalidParameter;
1036 if(!image_lock(&bitmap->image, &unlock))
1037 return ObjectBusy;
1039 if(rect){
1040 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1041 (rect->Y + rect->Height > bitmap->height) || !flags)
1043 image_unlock(&bitmap->image, unlock);
1044 return InvalidParameter;
1047 act_rect = *rect;
1049 else{
1050 act_rect.X = act_rect.Y = 0;
1051 act_rect.Width = bitmap->width;
1052 act_rect.Height = bitmap->height;
1055 if(bitmap->lockmode)
1057 WARN("bitmap is already locked and cannot be locked again\n");
1058 image_unlock(&bitmap->image, unlock);
1059 return WrongState;
1062 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1064 /* no conversion is necessary; just use the bits directly */
1065 lockeddata->Width = act_rect.Width;
1066 lockeddata->Height = act_rect.Height;
1067 lockeddata->PixelFormat = format;
1068 lockeddata->Reserved = flags;
1069 lockeddata->Stride = bitmap->stride;
1070 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1071 bitmap->stride * act_rect.Y;
1073 bitmap->lockmode = flags | ImageLockModeRead;
1075 image_unlock(&bitmap->image, unlock);
1076 return Ok;
1079 /* Make sure we can convert to the requested format. */
1080 if (flags & ImageLockModeRead)
1082 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
1083 if (stat == NotImplemented)
1085 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1086 image_unlock(&bitmap->image, unlock);
1087 return NotImplemented;
1091 /* If we're opening for writing, make sure we'll be able to write back in
1092 * the original format. */
1093 if (flags & ImageLockModeWrite)
1095 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
1096 if (stat == NotImplemented)
1098 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1099 image_unlock(&bitmap->image, unlock);
1100 return NotImplemented;
1104 lockeddata->Width = act_rect.Width;
1105 lockeddata->Height = act_rect.Height;
1106 lockeddata->PixelFormat = format;
1107 lockeddata->Reserved = flags;
1109 if(!(flags & ImageLockModeUserInputBuf))
1111 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1113 bitmap->bitmapbits = heap_alloc_zero(lockeddata->Stride * act_rect.Height);
1115 if (!bitmap->bitmapbits)
1117 image_unlock(&bitmap->image, unlock);
1118 return OutOfMemory;
1121 lockeddata->Scan0 = bitmap->bitmapbits;
1124 if (flags & ImageLockModeRead)
1126 static BOOL fixme = FALSE;
1128 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1130 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1131 fixme = TRUE;
1134 stat = convert_pixels(act_rect.Width, act_rect.Height,
1135 lockeddata->Stride, lockeddata->Scan0, format,
1136 bitmap->stride,
1137 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1138 bitmap->format, bitmap->image.palette);
1140 if (stat != Ok)
1142 heap_free(bitmap->bitmapbits);
1143 bitmap->bitmapbits = NULL;
1144 image_unlock(&bitmap->image, unlock);
1145 return stat;
1149 bitmap->lockmode = flags | ImageLockModeRead;
1150 bitmap->lockx = act_rect.X;
1151 bitmap->locky = act_rect.Y;
1153 image_unlock(&bitmap->image, unlock);
1154 return Ok;
1157 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1159 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1161 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1162 return InvalidParameter;
1164 bitmap->image.xres = xdpi;
1165 bitmap->image.yres = ydpi;
1167 return Ok;
1170 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1171 BitmapData* lockeddata)
1173 GpStatus stat;
1174 static BOOL fixme = FALSE;
1175 BOOL unlock;
1177 TRACE("(%p,%p)\n", bitmap, lockeddata);
1179 if(!bitmap || !lockeddata)
1180 return InvalidParameter;
1181 if(!image_lock(&bitmap->image, &unlock))
1182 return ObjectBusy;
1184 if(!bitmap->lockmode)
1186 image_unlock(&bitmap->image, unlock);
1187 return WrongState;
1190 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1191 bitmap->lockmode = 0;
1192 heap_free(bitmap->bitmapbits);
1193 bitmap->bitmapbits = NULL;
1194 image_unlock(&bitmap->image, unlock);
1195 return Ok;
1198 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1200 /* we passed a direct reference; no need to do anything */
1201 bitmap->lockmode = 0;
1202 image_unlock(&bitmap->image, unlock);
1203 return Ok;
1206 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1208 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1209 fixme = TRUE;
1212 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1213 bitmap->stride,
1214 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1215 bitmap->format,
1216 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1218 if (stat != Ok)
1220 ERR("failed to convert pixels; this should never happen\n");
1223 heap_free(bitmap->bitmapbits);
1224 bitmap->bitmapbits = NULL;
1225 bitmap->lockmode = 0;
1227 image_unlock(&bitmap->image, unlock);
1228 return stat;
1231 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1232 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1234 Rect area;
1235 GpStatus stat;
1237 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1239 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1240 x < 0 || y < 0 ||
1241 x + width > srcBitmap->width || y + height > srcBitmap->height)
1243 TRACE("<-- InvalidParameter\n");
1244 return InvalidParameter;
1247 if (format == PixelFormatDontCare)
1248 format = srcBitmap->format;
1250 area.X = gdip_round(x);
1251 area.Y = gdip_round(y);
1252 area.Width = gdip_round(width);
1253 area.Height = gdip_round(height);
1255 stat = GdipCreateBitmapFromScan0(area.Width, area.Height, 0, format, NULL, dstBitmap);
1256 if (stat == Ok)
1258 stat = convert_pixels(area.Width, area.Height, (*dstBitmap)->stride, (*dstBitmap)->bits, (*dstBitmap)->format,
1259 srcBitmap->stride,
1260 srcBitmap->bits + srcBitmap->stride * area.Y + PIXELFORMATBPP(srcBitmap->format) * area.X / 8,
1261 srcBitmap->format, srcBitmap->image.palette);
1263 if (stat == Ok && srcBitmap->image.palette)
1265 ColorPalette *src_palette, *dst_palette;
1267 src_palette = srcBitmap->image.palette;
1269 dst_palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * src_palette->Count);
1271 if (dst_palette)
1273 dst_palette->Flags = src_palette->Flags;
1274 dst_palette->Count = src_palette->Count;
1275 memcpy(dst_palette->Entries, src_palette->Entries, sizeof(ARGB) * src_palette->Count);
1277 heap_free((*dstBitmap)->image.palette);
1278 (*dstBitmap)->image.palette = dst_palette;
1280 else
1281 stat = OutOfMemory;
1284 if (stat != Ok)
1285 GdipDisposeImage(&(*dstBitmap)->image);
1288 if (stat != Ok)
1289 *dstBitmap = NULL;
1291 return stat;
1294 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1295 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1297 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1299 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1302 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1304 TRACE("%p, %p\n", image, cloneImage);
1306 if (!image || !cloneImage)
1307 return InvalidParameter;
1309 if (image->type == ImageTypeBitmap)
1311 GpBitmap *bitmap = (GpBitmap *)image;
1313 return GdipCloneBitmapAreaI(0, 0, bitmap->width, bitmap->height,
1314 bitmap->format, bitmap, (GpBitmap **)cloneImage);
1316 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
1318 GpMetafile *result, *metafile;
1320 metafile = (GpMetafile*)image;
1322 result = heap_alloc_zero(sizeof(*result));
1323 if (!result)
1324 return OutOfMemory;
1326 result->image.type = ImageTypeMetafile;
1327 result->image.format = image->format;
1328 result->image.flags = image->flags;
1329 result->image.frame_count = 1;
1330 result->image.xres = image->xres;
1331 result->image.yres = image->yres;
1332 result->bounds = metafile->bounds;
1333 result->unit = metafile->unit;
1334 result->metafile_type = metafile->metafile_type;
1335 result->hemf = CopyEnhMetaFileW(metafile->hemf, NULL);
1336 list_init(&result->containers);
1338 if (!result->hemf)
1340 heap_free(result);
1341 return OutOfMemory;
1344 *cloneImage = &result->image;
1345 return Ok;
1347 else
1349 WARN("GpImage with no image data (metafile in wrong state?)\n");
1350 return InvalidParameter;
1354 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1355 GpBitmap **bitmap)
1357 GpStatus stat;
1358 IStream *stream;
1360 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1362 if(!filename || !bitmap)
1363 return InvalidParameter;
1365 *bitmap = NULL;
1367 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1369 if(stat != Ok)
1370 return stat;
1372 stat = GdipCreateBitmapFromStream(stream, bitmap);
1374 IStream_Release(stream);
1376 return stat;
1379 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1380 VOID *bits, GpBitmap **bitmap)
1382 DWORD height, stride;
1383 PixelFormat format;
1385 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1387 if (!info || !bits || !bitmap)
1388 return InvalidParameter;
1390 height = abs(info->bmiHeader.biHeight);
1391 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1393 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1395 bits = (BYTE*)bits + (height - 1) * stride;
1396 stride = -stride;
1399 switch(info->bmiHeader.biBitCount) {
1400 case 1:
1401 format = PixelFormat1bppIndexed;
1402 break;
1403 case 4:
1404 format = PixelFormat4bppIndexed;
1405 break;
1406 case 8:
1407 format = PixelFormat8bppIndexed;
1408 break;
1409 case 16:
1410 format = PixelFormat16bppRGB555;
1411 break;
1412 case 24:
1413 format = PixelFormat24bppRGB;
1414 break;
1415 case 32:
1416 format = PixelFormat32bppRGB;
1417 break;
1418 default:
1419 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1420 *bitmap = NULL;
1421 return InvalidParameter;
1424 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1425 bits, bitmap);
1429 /* FIXME: no icm */
1430 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1431 GpBitmap **bitmap)
1433 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1435 return GdipCreateBitmapFromFile(filename, bitmap);
1438 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1439 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1441 HBITMAP hbm;
1442 GpStatus stat = InvalidParameter;
1444 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1446 if(!lpBitmapName || !bitmap)
1447 return InvalidParameter;
1449 /* load DIB */
1450 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1451 LR_CREATEDIBSECTION);
1453 if(hbm){
1454 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1455 DeleteObject(hbm);
1458 return stat;
1461 static inline DWORD blend_argb_no_bkgnd_alpha(DWORD src, DWORD bkgnd)
1463 BYTE b = (BYTE)src;
1464 BYTE g = (BYTE)(src >> 8);
1465 BYTE r = (BYTE)(src >> 16);
1466 DWORD alpha = (BYTE)(src >> 24);
1467 return ((b + ((BYTE)bkgnd * (255 - alpha) + 127) / 255) |
1468 (g + ((BYTE)(bkgnd >> 8) * (255 - alpha) + 127) / 255) << 8 |
1469 (r + ((BYTE)(bkgnd >> 16) * (255 - alpha) + 127) / 255) << 16 |
1470 (alpha << 24));
1473 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1474 HBITMAP* hbmReturn, ARGB background)
1476 GpStatus stat;
1477 HBITMAP result;
1478 UINT width, height;
1479 BITMAPINFOHEADER bih;
1480 LPBYTE bits;
1481 BOOL unlock;
1483 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1485 if (!bitmap || !hbmReturn) return InvalidParameter;
1486 if (!image_lock(&bitmap->image, &unlock)) return ObjectBusy;
1488 GdipGetImageWidth(&bitmap->image, &width);
1489 GdipGetImageHeight(&bitmap->image, &height);
1491 bih.biSize = sizeof(bih);
1492 bih.biWidth = width;
1493 bih.biHeight = height;
1494 bih.biPlanes = 1;
1495 bih.biBitCount = 32;
1496 bih.biCompression = BI_RGB;
1497 bih.biSizeImage = 0;
1498 bih.biXPelsPerMeter = 0;
1499 bih.biYPelsPerMeter = 0;
1500 bih.biClrUsed = 0;
1501 bih.biClrImportant = 0;
1503 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1504 if (!result)
1506 image_unlock(&bitmap->image, unlock);
1507 return GenericError;
1510 stat = convert_pixels(width, height, -width*4,
1511 bits + (width * 4 * (height - 1)), PixelFormat32bppPARGB,
1512 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette);
1513 if (stat != Ok)
1515 DeleteObject(result);
1516 image_unlock(&bitmap->image, unlock);
1517 return stat;
1520 if (background & 0xffffff)
1522 DWORD *ptr;
1523 UINT i;
1524 for (ptr = (DWORD*)bits, i = 0; i < width * height; ptr++, i++)
1526 if ((*ptr & 0xff000000) == 0xff000000) continue;
1527 *ptr = blend_argb_no_bkgnd_alpha(*ptr, background);
1531 *hbmReturn = result;
1532 image_unlock(&bitmap->image, unlock);
1533 return Ok;
1536 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1537 GpGraphics* target, GpBitmap** bitmap)
1539 GpStatus ret;
1541 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1543 if(!target || !bitmap)
1544 return InvalidParameter;
1546 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
1547 NULL, bitmap);
1549 if (ret == Ok)
1551 GdipGetDpiX(target, &(*bitmap)->image.xres);
1552 GdipGetDpiY(target, &(*bitmap)->image.yres);
1555 return ret;
1558 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1560 GpStatus stat;
1561 ICONINFO iinfo;
1562 BITMAP bm;
1563 int ret;
1564 UINT width, height, stride;
1565 GpRect rect;
1566 BitmapData lockeddata;
1567 HDC screendc;
1568 BOOL has_alpha;
1569 int x, y;
1570 BITMAPINFOHEADER bih;
1571 DWORD *src;
1572 BYTE *dst_row;
1573 DWORD *dst;
1575 TRACE("%p, %p\n", hicon, bitmap);
1577 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1578 return InvalidParameter;
1580 /* get the size of the icon */
1581 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1582 if (ret == 0) {
1583 DeleteObject(iinfo.hbmColor);
1584 DeleteObject(iinfo.hbmMask);
1585 return GenericError;
1588 width = bm.bmWidth;
1589 height = iinfo.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
1590 stride = width * 4;
1592 stat = GdipCreateBitmapFromScan0(width, height, stride, PixelFormat32bppARGB, NULL, bitmap);
1593 if (stat != Ok) {
1594 DeleteObject(iinfo.hbmColor);
1595 DeleteObject(iinfo.hbmMask);
1596 return stat;
1599 rect.X = 0;
1600 rect.Y = 0;
1601 rect.Width = width;
1602 rect.Height = height;
1604 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1605 if (stat != Ok) {
1606 DeleteObject(iinfo.hbmColor);
1607 DeleteObject(iinfo.hbmMask);
1608 GdipDisposeImage(&(*bitmap)->image);
1609 return stat;
1612 bih.biSize = sizeof(bih);
1613 bih.biWidth = width;
1614 bih.biHeight = iinfo.hbmColor ? -height: -height * 2;
1615 bih.biPlanes = 1;
1616 bih.biBitCount = 32;
1617 bih.biCompression = BI_RGB;
1618 bih.biSizeImage = 0;
1619 bih.biXPelsPerMeter = 0;
1620 bih.biYPelsPerMeter = 0;
1621 bih.biClrUsed = 0;
1622 bih.biClrImportant = 0;
1624 screendc = CreateCompatibleDC(0);
1625 if (iinfo.hbmColor)
1627 GetDIBits(screendc, iinfo.hbmColor, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1629 if (bm.bmBitsPixel == 32)
1631 has_alpha = FALSE;
1633 /* If any pixel has a non-zero alpha, ignore hbmMask */
1634 src = (DWORD*)lockeddata.Scan0;
1635 for (x=0; x<width && !has_alpha; x++)
1636 for (y=0; y<height && !has_alpha; y++)
1637 if ((*src++ & 0xff000000) != 0)
1638 has_alpha = TRUE;
1640 else has_alpha = FALSE;
1642 else
1644 GetDIBits(screendc, iinfo.hbmMask, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1645 has_alpha = FALSE;
1648 if (!has_alpha)
1650 if (iinfo.hbmMask)
1652 BYTE *bits = heap_alloc(height * stride);
1654 /* read alpha data from the mask */
1655 if (iinfo.hbmColor)
1656 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1657 else
1658 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1660 src = (DWORD*)bits;
1661 dst_row = lockeddata.Scan0;
1662 for (y=0; y<height; y++)
1664 dst = (DWORD*)dst_row;
1665 for (x=0; x<height; x++)
1667 DWORD src_value = *src++;
1668 if (src_value)
1669 *dst++ = 0;
1670 else
1671 *dst++ |= 0xff000000;
1673 dst_row += lockeddata.Stride;
1676 heap_free(bits);
1678 else
1680 /* set constant alpha of 255 */
1681 dst_row = lockeddata.Scan0;
1682 for (y=0; y<height; y++)
1684 dst = (DWORD*)dst_row;
1685 for (x=0; x<height; x++)
1686 *dst++ |= 0xff000000;
1687 dst_row += lockeddata.Stride;
1692 DeleteDC(screendc);
1694 DeleteObject(iinfo.hbmColor);
1695 DeleteObject(iinfo.hbmMask);
1697 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1699 return Ok;
1702 static void generate_halftone_palette(ARGB *entries, UINT count)
1704 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1705 UINT i;
1707 for (i=0; i<8 && i<count; i++)
1709 entries[i] = 0xff000000;
1710 if (i&1) entries[i] |= 0x800000;
1711 if (i&2) entries[i] |= 0x8000;
1712 if (i&4) entries[i] |= 0x80;
1715 if (8 < count)
1716 entries[i] = 0xffc0c0c0;
1718 for (i=9; i<16 && i<count; i++)
1720 entries[i] = 0xff000000;
1721 if (i&1) entries[i] |= 0xff0000;
1722 if (i&2) entries[i] |= 0xff00;
1723 if (i&4) entries[i] |= 0xff;
1726 for (i=16; i<40 && i<count; i++)
1728 entries[i] = 0;
1731 for (i=40; i<256 && i<count; i++)
1733 entries[i] = 0xff000000;
1734 entries[i] |= halftone_values[(i-40)%6];
1735 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1736 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1740 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1742 HDC screendc = CreateCompatibleDC(0);
1744 if (!screendc) return GenericError;
1746 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1747 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1749 DeleteDC(screendc);
1751 return Ok;
1754 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1755 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1757 HBITMAP hbitmap=NULL;
1758 INT row_size, dib_stride;
1759 BYTE *bits=NULL, *own_bits=NULL;
1760 REAL xres, yres;
1761 GpStatus stat;
1763 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1765 if (!bitmap) return InvalidParameter;
1767 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1768 *bitmap = NULL;
1769 return InvalidParameter;
1772 if(scan0 && !stride)
1773 return InvalidParameter;
1775 stat = get_screen_resolution(&xres, &yres);
1776 if (stat != Ok) return stat;
1778 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1779 dib_stride = (row_size + 3) & ~3;
1781 if(stride == 0)
1782 stride = dib_stride;
1784 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1786 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
1787 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
1789 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1790 pbmi->bmiHeader.biWidth = width;
1791 pbmi->bmiHeader.biHeight = -height;
1792 pbmi->bmiHeader.biPlanes = 1;
1793 /* FIXME: use the rest of the data from format */
1794 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1795 pbmi->bmiHeader.biCompression = BI_RGB;
1796 pbmi->bmiHeader.biSizeImage = 0;
1797 pbmi->bmiHeader.biXPelsPerMeter = 0;
1798 pbmi->bmiHeader.biYPelsPerMeter = 0;
1799 pbmi->bmiHeader.biClrUsed = 0;
1800 pbmi->bmiHeader.biClrImportant = 0;
1802 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1804 if (!hbitmap) return GenericError;
1806 stride = dib_stride;
1808 else
1810 /* Not a GDI format; don't try to make an HBITMAP. */
1811 if (scan0)
1812 bits = scan0;
1813 else
1815 INT size = abs(stride) * height;
1817 own_bits = bits = heap_alloc_zero(size);
1818 if (!own_bits) return OutOfMemory;
1820 if (stride < 0)
1821 bits += stride * (1 - height);
1825 *bitmap = heap_alloc_zero(sizeof(GpBitmap));
1826 if(!*bitmap)
1828 DeleteObject(hbitmap);
1829 heap_free(own_bits);
1830 return OutOfMemory;
1833 (*bitmap)->image.type = ImageTypeBitmap;
1834 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1835 (*bitmap)->image.flags = ImageFlagsNone;
1836 (*bitmap)->image.frame_count = 1;
1837 (*bitmap)->image.current_frame = 0;
1838 (*bitmap)->image.palette = NULL;
1839 (*bitmap)->image.xres = xres;
1840 (*bitmap)->image.yres = yres;
1841 (*bitmap)->width = width;
1842 (*bitmap)->height = height;
1843 (*bitmap)->format = format;
1844 (*bitmap)->image.decoder = NULL;
1845 (*bitmap)->hbitmap = hbitmap;
1846 (*bitmap)->hdc = NULL;
1847 (*bitmap)->bits = bits;
1848 (*bitmap)->stride = stride;
1849 (*bitmap)->own_bits = own_bits;
1850 (*bitmap)->metadata_reader = NULL;
1851 (*bitmap)->prop_count = 0;
1852 (*bitmap)->prop_item = NULL;
1854 /* set format-related flags */
1855 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1856 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1858 if (format == PixelFormat1bppIndexed ||
1859 format == PixelFormat4bppIndexed ||
1860 format == PixelFormat8bppIndexed)
1862 (*bitmap)->image.palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1864 if (!(*bitmap)->image.palette)
1866 GdipDisposeImage(&(*bitmap)->image);
1867 *bitmap = NULL;
1868 return OutOfMemory;
1871 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1873 if (format == PixelFormat1bppIndexed)
1875 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1876 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1877 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1879 else
1881 if (format == PixelFormat8bppIndexed)
1882 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1884 generate_halftone_palette((*bitmap)->image.palette->Entries,
1885 (*bitmap)->image.palette->Count);
1889 TRACE("<-- %p\n", *bitmap);
1891 return Ok;
1894 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1895 GpBitmap **bitmap)
1897 GpStatus stat;
1899 TRACE("%p %p\n", stream, bitmap);
1901 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1903 if(stat != Ok)
1904 return stat;
1906 if((*bitmap)->image.type != ImageTypeBitmap){
1907 GdipDisposeImage(&(*bitmap)->image);
1908 *bitmap = NULL;
1909 return GenericError; /* FIXME: what error to return? */
1912 return Ok;
1915 /* FIXME: no icm */
1916 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1917 GpBitmap **bitmap)
1919 TRACE("%p %p\n", stream, bitmap);
1921 return GdipCreateBitmapFromStream(stream, bitmap);
1924 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1925 GpCachedBitmap **cachedbmp)
1927 GpStatus stat;
1929 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1931 if(!bitmap || !graphics || !cachedbmp)
1932 return InvalidParameter;
1934 *cachedbmp = heap_alloc_zero(sizeof(GpCachedBitmap));
1935 if(!*cachedbmp)
1936 return OutOfMemory;
1938 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1939 if(stat != Ok){
1940 heap_free(*cachedbmp);
1941 return stat;
1944 return Ok;
1947 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1949 GpStatus stat;
1950 BitmapData lockeddata;
1951 ULONG andstride, xorstride, bitssize;
1952 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1953 UINT x, y;
1955 TRACE("(%p, %p)\n", bitmap, hicon);
1957 if (!bitmap || !hicon)
1958 return InvalidParameter;
1960 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1961 PixelFormat32bppPARGB, &lockeddata);
1962 if (stat == Ok)
1964 andstride = ((lockeddata.Width+31)/32)*4;
1965 xorstride = lockeddata.Width*4;
1966 bitssize = (andstride + xorstride) * lockeddata.Height;
1968 andbits = heap_alloc_zero(bitssize);
1970 if (andbits)
1972 xorbits = andbits + andstride * lockeddata.Height;
1974 for (y=0; y<lockeddata.Height; y++)
1976 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
1978 androw = andbits + andstride * y;
1979 for (x=0; x<lockeddata.Width; x++)
1980 if (srcrow[3+4*x] >= 128)
1981 androw[x/8] |= 1 << (7-x%8);
1983 xorrow = xorbits + xorstride * y;
1984 memcpy(xorrow, srcrow, xorstride);
1987 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
1988 andbits, xorbits);
1990 heap_free(andbits);
1992 else
1993 stat = OutOfMemory;
1995 GdipBitmapUnlockBits(bitmap, &lockeddata);
1998 return stat;
2001 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
2003 TRACE("%p\n", cachedbmp);
2005 if(!cachedbmp)
2006 return InvalidParameter;
2008 GdipDisposeImage(cachedbmp->image);
2009 heap_free(cachedbmp);
2011 return Ok;
2014 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2015 GpCachedBitmap *cachedbmp, INT x, INT y)
2017 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2019 if(!graphics || !cachedbmp)
2020 return InvalidParameter;
2022 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2025 /* Internal utility function: Replace the image data of dst with that of src,
2026 * and free src. */
2027 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2029 assert(src->image.type == ImageTypeBitmap);
2030 assert(dst->image.type == ImageTypeBitmap);
2032 heap_free(dst->bitmapbits);
2033 heap_free(dst->own_bits);
2034 DeleteDC(dst->hdc);
2035 DeleteObject(dst->hbitmap);
2037 if (clobber_palette)
2039 heap_free(dst->image.palette);
2040 dst->image.palette = src->image.palette;
2042 else
2043 heap_free(src->image.palette);
2045 dst->image.xres = src->image.xres;
2046 dst->image.yres = src->image.yres;
2047 dst->width = src->width;
2048 dst->height = src->height;
2049 dst->format = src->format;
2050 dst->hbitmap = src->hbitmap;
2051 dst->hdc = src->hdc;
2052 dst->bits = src->bits;
2053 dst->stride = src->stride;
2054 dst->own_bits = src->own_bits;
2055 if (dst->metadata_reader)
2056 IWICMetadataReader_Release(dst->metadata_reader);
2057 dst->metadata_reader = src->metadata_reader;
2058 heap_free(dst->prop_item);
2059 dst->prop_item = src->prop_item;
2060 dst->prop_count = src->prop_count;
2061 if (dst->image.decoder)
2062 IWICBitmapDecoder_Release(dst->image.decoder);
2063 dst->image.decoder = src->image.decoder;
2064 dst->image.frame_count = src->image.frame_count;
2065 dst->image.current_frame = src->image.current_frame;
2066 dst->image.format = src->image.format;
2068 src->image.type = ~0;
2069 heap_free(src);
2072 static GpStatus free_image_data(GpImage *image)
2074 if(!image)
2075 return InvalidParameter;
2077 if (image->type == ImageTypeBitmap)
2079 heap_free(((GpBitmap*)image)->bitmapbits);
2080 heap_free(((GpBitmap*)image)->own_bits);
2081 DeleteDC(((GpBitmap*)image)->hdc);
2082 DeleteObject(((GpBitmap*)image)->hbitmap);
2083 if (((GpBitmap*)image)->metadata_reader)
2084 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2085 heap_free(((GpBitmap*)image)->prop_item);
2087 else if (image->type == ImageTypeMetafile)
2089 GpMetafile *metafile = (GpMetafile*)image;
2090 heap_free(metafile->comment_data);
2091 DeleteEnhMetaFile(CloseEnhMetaFile(metafile->record_dc));
2092 if (!metafile->preserve_hemf)
2093 DeleteEnhMetaFile(metafile->hemf);
2094 if (metafile->record_graphics)
2096 WARN("metafile closed while recording\n");
2097 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2098 metafile->record_graphics->image = NULL;
2099 metafile->record_graphics->busy = TRUE;
2101 if (metafile->record_stream)
2103 IStream_Release(metafile->record_stream);
2106 else
2108 WARN("invalid image: %p\n", image);
2109 return ObjectBusy;
2111 if (image->decoder)
2112 IWICBitmapDecoder_Release(image->decoder);
2113 heap_free(image->palette);
2115 return Ok;
2118 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2120 GpStatus status;
2122 TRACE("%p\n", image);
2124 status = free_image_data(image);
2125 if (status != Ok) return status;
2126 image->type = ~0;
2127 heap_free(image);
2129 return Ok;
2132 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2134 static int calls;
2136 TRACE("(%p,%p)\n", image, item);
2138 if(!image || !item)
2139 return InvalidParameter;
2141 if (!(calls++))
2142 FIXME("not implemented\n");
2144 return NotImplemented;
2147 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2149 static int calls;
2151 TRACE("(%p,%p)\n", image, item);
2153 if (!(calls++))
2154 FIXME("not implemented\n");
2156 return NotImplemented;
2159 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2160 GpUnit *srcUnit)
2162 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2164 if(!image || !srcRect || !srcUnit)
2165 return InvalidParameter;
2166 if(image->type == ImageTypeMetafile){
2167 *srcRect = ((GpMetafile*)image)->bounds;
2168 *srcUnit = ((GpMetafile*)image)->unit;
2170 else if(image->type == ImageTypeBitmap){
2171 srcRect->X = srcRect->Y = 0.0;
2172 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2173 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2174 *srcUnit = UnitPixel;
2176 else{
2177 WARN("GpImage with no image data\n");
2178 return InvalidParameter;
2181 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2182 srcRect->Width, srcRect->Height, *srcUnit);
2184 return Ok;
2187 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2188 REAL *height)
2190 TRACE("%p %p %p\n", image, width, height);
2192 if(!image || !height || !width)
2193 return InvalidParameter;
2195 if(image->type == ImageTypeMetafile){
2196 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2197 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2199 else if(image->type == ImageTypeBitmap){
2200 *height = ((GpBitmap*)image)->height;
2201 *width = ((GpBitmap*)image)->width;
2203 else{
2204 WARN("GpImage with no image data\n");
2205 return InvalidParameter;
2208 TRACE("returning (%f, %f)\n", *height, *width);
2209 return Ok;
2212 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2213 GpGraphics **graphics)
2215 HDC hdc;
2216 GpStatus stat;
2218 TRACE("%p %p\n", image, graphics);
2220 if(!image || !graphics)
2221 return InvalidParameter;
2223 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2225 hdc = ((GpBitmap*)image)->hdc;
2227 if(!hdc){
2228 hdc = CreateCompatibleDC(0);
2229 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2230 ((GpBitmap*)image)->hdc = hdc;
2233 stat = GdipCreateFromHDC(hdc, graphics);
2235 if (stat == Ok)
2237 (*graphics)->image = image;
2238 (*graphics)->xres = image->xres;
2239 (*graphics)->yres = image->yres;
2242 else if (image->type == ImageTypeMetafile)
2243 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2244 else
2245 stat = graphics_from_image(image, graphics);
2247 return stat;
2250 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2252 TRACE("%p %p\n", image, height);
2254 if(!image || !height)
2255 return InvalidParameter;
2257 if(image->type == ImageTypeMetafile)
2258 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2259 else if(image->type == ImageTypeBitmap)
2260 *height = ((GpBitmap*)image)->height;
2261 else
2263 WARN("GpImage with no image data\n");
2264 return InvalidParameter;
2267 TRACE("returning %d\n", *height);
2269 return Ok;
2272 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2274 if(!image || !res)
2275 return InvalidParameter;
2277 *res = image->xres;
2279 TRACE("(%p) <-- %0.2f\n", image, *res);
2281 return Ok;
2284 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2286 TRACE("%p %p\n", image, size);
2288 if(!image || !size)
2289 return InvalidParameter;
2291 if (!image->palette || image->palette->Count == 0)
2292 *size = sizeof(ColorPalette);
2293 else
2294 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2296 TRACE("<-- %u\n", *size);
2298 return Ok;
2301 /* FIXME: test this function for non-bitmap types */
2302 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2304 TRACE("%p %p\n", image, format);
2306 if(!image || !format)
2307 return InvalidParameter;
2309 if(image->type != ImageTypeBitmap)
2310 *format = PixelFormat24bppRGB;
2311 else
2312 *format = ((GpBitmap*) image)->format;
2314 return Ok;
2317 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2319 TRACE("(%p, %p)\n", image, format);
2321 if(!image || !format)
2322 return InvalidParameter;
2324 memcpy(format, &image->format, sizeof(GUID));
2326 return Ok;
2329 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2331 TRACE("%p %p\n", image, type);
2333 if(!image || !type)
2334 return InvalidParameter;
2336 *type = image->type;
2338 return Ok;
2341 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2343 if(!image || !res)
2344 return InvalidParameter;
2346 *res = image->yres;
2348 TRACE("(%p) <-- %0.2f\n", image, *res);
2350 return Ok;
2353 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2355 TRACE("%p %p\n", image, width);
2357 if(!image || !width)
2358 return InvalidParameter;
2360 if(image->type == ImageTypeMetafile)
2361 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2362 else if(image->type == ImageTypeBitmap)
2363 *width = ((GpBitmap*)image)->width;
2364 else
2366 WARN("GpImage with no image data\n");
2367 return InvalidParameter;
2370 TRACE("returning %d\n", *width);
2372 return Ok;
2375 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2377 TRACE("(%p, %p)\n", image, num);
2379 if (!image || !num) return InvalidParameter;
2381 *num = 0;
2383 if (image->type == ImageTypeBitmap)
2385 if (((GpBitmap *)image)->prop_item)
2387 *num = ((GpBitmap *)image)->prop_count;
2388 return Ok;
2391 if (((GpBitmap *)image)->metadata_reader)
2392 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2395 return Ok;
2398 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2400 HRESULT hr;
2401 IWICMetadataReader *reader;
2402 IWICEnumMetadataItem *enumerator;
2403 UINT prop_count, i, items_returned;
2405 TRACE("(%p, %u, %p)\n", image, num, list);
2407 if (!image || !list) return InvalidParameter;
2409 if (image->type != ImageTypeBitmap)
2411 FIXME("Not implemented for type %d\n", image->type);
2412 return NotImplemented;
2415 if (((GpBitmap *)image)->prop_item)
2417 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2419 for (i = 0; i < num; i++)
2421 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2424 return Ok;
2427 reader = ((GpBitmap *)image)->metadata_reader;
2428 if (!reader)
2430 if (num != 0) return InvalidParameter;
2431 return Ok;
2434 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2435 if (FAILED(hr)) return hresult_to_status(hr);
2437 if (num != prop_count) return InvalidParameter;
2439 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2440 if (FAILED(hr)) return hresult_to_status(hr);
2442 IWICEnumMetadataItem_Reset(enumerator);
2444 for (i = 0; i < num; i++)
2446 PROPVARIANT id;
2448 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2449 if (hr != S_OK) break;
2451 if (id.vt != VT_UI2)
2453 FIXME("not supported propvariant type for id: %u\n", id.vt);
2454 list[i] = 0;
2455 continue;
2457 list[i] = id.u.uiVal;
2460 IWICEnumMetadataItem_Release(enumerator);
2462 return hr == S_OK ? Ok : hresult_to_status(hr);
2465 static UINT propvariant_size(PROPVARIANT *value)
2467 switch (value->vt & ~VT_VECTOR)
2469 case VT_EMPTY:
2470 return 0;
2471 case VT_I1:
2472 case VT_UI1:
2473 if (!(value->vt & VT_VECTOR)) return 1;
2474 return value->u.caub.cElems;
2475 case VT_I2:
2476 case VT_UI2:
2477 if (!(value->vt & VT_VECTOR)) return 2;
2478 return value->u.caui.cElems * 2;
2479 case VT_I4:
2480 case VT_UI4:
2481 case VT_R4:
2482 if (!(value->vt & VT_VECTOR)) return 4;
2483 return value->u.caul.cElems * 4;
2484 case VT_I8:
2485 case VT_UI8:
2486 case VT_R8:
2487 if (!(value->vt & VT_VECTOR)) return 8;
2488 return value->u.cauh.cElems * 8;
2489 case VT_LPSTR:
2490 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2491 case VT_BLOB:
2492 return value->u.blob.cbSize;
2493 default:
2494 FIXME("not supported variant type %d\n", value->vt);
2495 return 0;
2499 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2501 HRESULT hr;
2502 IWICMetadataReader *reader;
2503 PROPVARIANT id, value;
2505 TRACE("(%p,%#x,%p)\n", image, propid, size);
2507 if (!size || !image) return InvalidParameter;
2509 if (image->type != ImageTypeBitmap)
2511 FIXME("Not implemented for type %d\n", image->type);
2512 return NotImplemented;
2515 if (((GpBitmap *)image)->prop_item)
2517 UINT i;
2519 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2521 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2523 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2524 return Ok;
2528 return PropertyNotFound;
2531 reader = ((GpBitmap *)image)->metadata_reader;
2532 if (!reader) return PropertyNotFound;
2534 id.vt = VT_UI2;
2535 id.u.uiVal = propid;
2536 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2537 if (FAILED(hr)) return PropertyNotFound;
2539 *size = propvariant_size(&value);
2540 if (*size) *size += sizeof(PropertyItem);
2541 PropVariantClear(&value);
2543 return Ok;
2546 #ifndef PropertyTagTypeSByte
2547 #define PropertyTagTypeSByte 6
2548 #define PropertyTagTypeSShort 8
2549 #define PropertyTagTypeFloat 11
2550 #define PropertyTagTypeDouble 12
2551 #endif
2553 static UINT vt_to_itemtype(UINT vt)
2555 static const struct
2557 UINT vt, type;
2558 } vt2type[] =
2560 { VT_I1, PropertyTagTypeSByte },
2561 { VT_UI1, PropertyTagTypeByte },
2562 { VT_I2, PropertyTagTypeSShort },
2563 { VT_UI2, PropertyTagTypeShort },
2564 { VT_I4, PropertyTagTypeSLONG },
2565 { VT_UI4, PropertyTagTypeLong },
2566 { VT_I8, PropertyTagTypeSRational },
2567 { VT_UI8, PropertyTagTypeRational },
2568 { VT_R4, PropertyTagTypeFloat },
2569 { VT_R8, PropertyTagTypeDouble },
2570 { VT_LPSTR, PropertyTagTypeASCII },
2571 { VT_BLOB, PropertyTagTypeUndefined }
2573 UINT i;
2574 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2576 if (vt2type[i].vt == vt) return vt2type[i].type;
2578 FIXME("not supported variant type %u\n", vt);
2579 return 0;
2582 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2583 UINT size, PROPID id)
2585 UINT item_size, item_type;
2587 item_size = propvariant_size(value);
2588 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2590 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2591 if (!item_type) return InvalidParameter;
2593 item->value = item + 1;
2595 switch (value->vt & ~VT_VECTOR)
2597 case VT_I1:
2598 case VT_UI1:
2599 if (!(value->vt & VT_VECTOR))
2600 *(BYTE *)item->value = value->u.bVal;
2601 else
2602 memcpy(item->value, value->u.caub.pElems, item_size);
2603 break;
2604 case VT_I2:
2605 case VT_UI2:
2606 if (!(value->vt & VT_VECTOR))
2607 *(USHORT *)item->value = value->u.uiVal;
2608 else
2609 memcpy(item->value, value->u.caui.pElems, item_size);
2610 break;
2611 case VT_I4:
2612 case VT_UI4:
2613 case VT_R4:
2614 if (!(value->vt & VT_VECTOR))
2615 *(ULONG *)item->value = value->u.ulVal;
2616 else
2617 memcpy(item->value, value->u.caul.pElems, item_size);
2618 break;
2619 case VT_I8:
2620 case VT_UI8:
2621 case VT_R8:
2622 if (!(value->vt & VT_VECTOR))
2623 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2624 else
2625 memcpy(item->value, value->u.cauh.pElems, item_size);
2626 break;
2627 case VT_LPSTR:
2628 memcpy(item->value, value->u.pszVal, item_size);
2629 break;
2630 case VT_BLOB:
2631 memcpy(item->value, value->u.blob.pBlobData, item_size);
2632 break;
2633 default:
2634 FIXME("not supported variant type %d\n", value->vt);
2635 return InvalidParameter;
2638 item->length = item_size;
2639 item->type = item_type;
2640 item->id = id;
2642 return Ok;
2645 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2646 PropertyItem *buffer)
2648 GpStatus stat;
2649 HRESULT hr;
2650 IWICMetadataReader *reader;
2651 PROPVARIANT id, value;
2653 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2655 if (!image || !buffer) return InvalidParameter;
2657 if (image->type != ImageTypeBitmap)
2659 FIXME("Not implemented for type %d\n", image->type);
2660 return NotImplemented;
2663 if (((GpBitmap *)image)->prop_item)
2665 UINT i;
2667 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2669 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2671 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2672 return InvalidParameter;
2674 *buffer = ((GpBitmap *)image)->prop_item[i];
2675 buffer->value = buffer + 1;
2676 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2677 return Ok;
2681 return PropertyNotFound;
2684 reader = ((GpBitmap *)image)->metadata_reader;
2685 if (!reader) return PropertyNotFound;
2687 id.vt = VT_UI2;
2688 id.u.uiVal = propid;
2689 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2690 if (FAILED(hr)) return PropertyNotFound;
2692 stat = propvariant_to_item(&value, buffer, size, propid);
2693 PropVariantClear(&value);
2695 return stat;
2698 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2700 HRESULT hr;
2701 IWICMetadataReader *reader;
2702 IWICEnumMetadataItem *enumerator;
2703 UINT prop_count, prop_size, i;
2704 PROPVARIANT id, value;
2706 TRACE("(%p,%p,%p)\n", image, size, count);
2708 if (!image || !size || !count) return InvalidParameter;
2710 if (image->type != ImageTypeBitmap)
2712 FIXME("Not implemented for type %d\n", image->type);
2713 return NotImplemented;
2716 if (((GpBitmap *)image)->prop_item)
2718 *count = ((GpBitmap *)image)->prop_count;
2719 *size = 0;
2721 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2723 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2726 return Ok;
2729 reader = ((GpBitmap *)image)->metadata_reader;
2730 if (!reader) return PropertyNotFound;
2732 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2733 if (FAILED(hr)) return hresult_to_status(hr);
2735 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2736 if (FAILED(hr)) return hresult_to_status(hr);
2738 IWICEnumMetadataItem_Reset(enumerator);
2740 prop_size = 0;
2742 PropVariantInit(&id);
2743 PropVariantInit(&value);
2745 for (i = 0; i < prop_count; i++)
2747 UINT items_returned, item_size;
2749 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2750 if (hr != S_OK) break;
2752 item_size = propvariant_size(&value);
2753 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2755 PropVariantClear(&id);
2756 PropVariantClear(&value);
2759 IWICEnumMetadataItem_Release(enumerator);
2761 if (hr != S_OK) return PropertyNotFound;
2763 *count = prop_count;
2764 *size = prop_size;
2765 return Ok;
2768 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2769 UINT count, PropertyItem *buf)
2771 GpStatus status;
2772 HRESULT hr;
2773 IWICMetadataReader *reader;
2774 IWICEnumMetadataItem *enumerator;
2775 UINT prop_count, prop_size, i;
2776 PROPVARIANT id, value;
2777 char *item_value;
2779 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2781 if (!image || !buf) return InvalidParameter;
2783 if (image->type != ImageTypeBitmap)
2785 FIXME("Not implemented for type %d\n", image->type);
2786 return NotImplemented;
2789 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2790 if (status != Ok) return status;
2792 if (prop_count != count || prop_size != size) return InvalidParameter;
2794 if (((GpBitmap *)image)->prop_item)
2796 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2798 item_value = (char *)(buf + prop_count);
2800 for (i = 0; i < prop_count; i++)
2802 buf[i].value = item_value;
2803 item_value += buf[i].length;
2806 return Ok;
2809 reader = ((GpBitmap *)image)->metadata_reader;
2810 if (!reader) return PropertyNotFound;
2812 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2813 if (FAILED(hr)) return hresult_to_status(hr);
2815 IWICEnumMetadataItem_Reset(enumerator);
2817 item_value = (char *)(buf + prop_count);
2819 PropVariantInit(&id);
2820 PropVariantInit(&value);
2822 for (i = 0; i < prop_count; i++)
2824 PropertyItem *item;
2825 UINT items_returned, item_size;
2827 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2828 if (hr != S_OK) break;
2830 if (id.vt != VT_UI2)
2832 FIXME("not supported propvariant type for id: %u\n", id.vt);
2833 continue;
2836 item_size = propvariant_size(&value);
2837 if (item_size)
2839 item = heap_alloc(item_size + sizeof(*item));
2841 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2842 buf[i].id = item->id;
2843 buf[i].type = item->type;
2844 buf[i].length = item_size;
2845 buf[i].value = item_value;
2846 memcpy(item_value, item->value, item_size);
2847 item_value += item_size;
2849 heap_free(item);
2852 PropVariantClear(&id);
2853 PropVariantClear(&value);
2856 IWICEnumMetadataItem_Release(enumerator);
2858 if (hr != S_OK) return PropertyNotFound;
2860 return Ok;
2863 struct image_format_dimension
2865 const GUID *format;
2866 const GUID *dimension;
2869 static const struct image_format_dimension image_format_dimensions[] =
2871 {&ImageFormatGIF, &FrameDimensionTime},
2872 {&ImageFormatIcon, &FrameDimensionResolution},
2873 {NULL}
2876 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2877 GDIPCONST GUID* dimensionID, UINT* count)
2879 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2881 if(!image || !count)
2882 return InvalidParameter;
2884 if (!dimensionID ||
2885 IsEqualGUID(dimensionID, &image->format) ||
2886 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2887 IsEqualGUID(dimensionID, &FrameDimensionTime))
2889 *count = image->frame_count;
2890 return Ok;
2893 return InvalidParameter;
2896 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2897 UINT* count)
2899 TRACE("(%p, %p)\n", image, count);
2901 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2903 if(!image || !count)
2904 return InvalidParameter;
2906 *count = 1;
2908 return Ok;
2911 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2912 GUID* dimensionIDs, UINT count)
2914 int i;
2915 const GUID *result=NULL;
2917 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2919 if(!image || !dimensionIDs || count != 1)
2920 return InvalidParameter;
2922 for (i=0; image_format_dimensions[i].format; i++)
2924 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2926 result = image_format_dimensions[i].dimension;
2927 break;
2931 if (!result)
2932 result = &FrameDimensionPage;
2934 memcpy(dimensionIDs, result, sizeof(GUID));
2936 return Ok;
2939 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2940 GpImage **image)
2942 GpStatus stat;
2943 IStream *stream;
2945 TRACE("(%s) %p\n", debugstr_w(filename), image);
2947 if (!filename || !image)
2948 return InvalidParameter;
2950 *image = NULL;
2952 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2954 if (stat != Ok)
2955 return stat;
2957 stat = GdipLoadImageFromStream(stream, image);
2959 IStream_Release(stream);
2961 return stat;
2964 /* FIXME: no icm handling */
2965 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2967 TRACE("(%s) %p\n", debugstr_w(filename), image);
2969 return GdipLoadImageFromFile(filename, image);
2972 static void add_property(GpBitmap *bitmap, PropertyItem *item)
2974 UINT prop_size, prop_count;
2975 PropertyItem *prop_item;
2977 if (bitmap->prop_item == NULL)
2979 prop_size = prop_count = 0;
2980 prop_item = heap_alloc_zero(item->length + sizeof(PropertyItem));
2981 if (!prop_item) return;
2983 else
2985 UINT i;
2986 char *item_value;
2988 GdipGetPropertySize(&bitmap->image, &prop_size, &prop_count);
2990 prop_item = heap_alloc_zero(prop_size + item->length + sizeof(PropertyItem));
2991 if (!prop_item) return;
2992 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
2993 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
2994 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
2996 item_value = (char *)(prop_item + prop_count + 1);
2998 for (i = 0; i < prop_count; i++)
3000 prop_item[i].value = item_value;
3001 item_value += prop_item[i].length;
3005 prop_item[prop_count].id = item->id;
3006 prop_item[prop_count].type = item->type;
3007 prop_item[prop_count].length = item->length;
3008 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3009 memcpy(prop_item[prop_count].value, item->value, item->length);
3011 heap_free(bitmap->prop_item);
3012 bitmap->prop_item = prop_item;
3013 bitmap->prop_count++;
3016 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3018 HRESULT hr;
3019 GUID format;
3020 PROPVARIANT id, value;
3021 BOOL ret = FALSE;
3023 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3024 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return FALSE;
3026 PropVariantInit(&id);
3027 PropVariantInit(&value);
3029 id.vt = VT_LPWSTR;
3030 id.u.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3031 if (!id.u.pwszVal) return FALSE;
3032 lstrcpyW(id.u.pwszVal, prop_name);
3033 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3034 if (hr == S_OK && value.vt == VT_BOOL)
3035 ret = value.u.boolVal;
3037 PropVariantClear(&id);
3038 PropVariantClear(&value);
3040 return ret;
3043 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3045 HRESULT hr;
3046 GUID format;
3047 PROPVARIANT id, value;
3048 PropertyItem *item = NULL;
3050 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3051 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return NULL;
3053 PropVariantInit(&id);
3054 PropVariantInit(&value);
3056 id.vt = VT_LPWSTR;
3057 id.u.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3058 if (!id.u.pwszVal) return NULL;
3059 lstrcpyW(id.u.pwszVal, prop_name);
3060 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3061 if (hr == S_OK)
3063 UINT item_size = propvariant_size(&value);
3064 if (item_size)
3066 item_size += sizeof(*item);
3067 item = heap_alloc_zero(item_size);
3068 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3070 heap_free(item);
3071 item = NULL;
3076 PropVariantClear(&id);
3077 PropVariantClear(&value);
3079 return item;
3082 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3084 static const WCHAR textentryW[] = { 'T','e','x','t','E','n','t','r','y',0 };
3085 PropertyItem *comment;
3087 comment = get_property(reader, &GUID_MetadataFormatGifComment, textentryW);
3088 if (comment)
3089 comment->id = PropertyTagExifUserComment;
3091 return comment;
3094 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3096 static const WCHAR applicationW[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3097 static const WCHAR dataW[] = { 'D','a','t','a',0 };
3098 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3100 appext = get_property(reader, &GUID_MetadataFormatAPE, applicationW);
3101 if (appext)
3103 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3104 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3106 appdata = get_property(reader, &GUID_MetadataFormatAPE, dataW);
3107 if (appdata)
3109 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3111 BYTE *data = appdata->value;
3112 if (data[0] == 3 && data[1] == 1)
3114 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3115 if (loop)
3117 loop->type = PropertyTagTypeShort;
3118 loop->id = PropertyTagLoopCount;
3119 loop->length = sizeof(SHORT);
3120 loop->value = loop + 1;
3121 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3129 heap_free(appext);
3130 heap_free(appdata);
3132 return loop;
3135 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3137 static const WCHAR backgroundW[] = { 'B','a','c','k','g','r','o','u','n','d','C','o','l','o','r','I','n','d','e','x',0 };
3138 PropertyItem *background;
3140 background = get_property(reader, &GUID_MetadataFormatLSD, backgroundW);
3141 if (background)
3142 background->id = PropertyTagIndexBackground;
3144 return background;
3147 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3149 static const WCHAR global_flagW[] = { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 };
3150 HRESULT hr;
3151 IWICImagingFactory *factory;
3152 IWICPalette *palette;
3153 UINT count = 0;
3154 WICColor colors[256];
3156 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, global_flagW))
3157 return NULL;
3159 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3160 if (hr != S_OK) return NULL;
3162 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3163 if (hr == S_OK)
3165 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3166 if (hr == S_OK)
3167 IWICPalette_GetColors(palette, 256, colors, &count);
3169 IWICPalette_Release(palette);
3172 IWICImagingFactory_Release(factory);
3174 if (count)
3176 PropertyItem *pal;
3177 UINT i;
3178 BYTE *rgb;
3180 pal = heap_alloc_zero(sizeof(*pal) + count * 3);
3181 if (!pal) return NULL;
3182 pal->type = PropertyTagTypeByte;
3183 pal->id = PropertyTagGlobalPalette;
3184 pal->value = pal + 1;
3185 pal->length = count * 3;
3187 rgb = pal->value;
3189 for (i = 0; i < count; i++)
3191 rgb[i*3] = (colors[i] >> 16) & 0xff;
3192 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3193 rgb[i*3 + 2] = colors[i] & 0xff;
3196 return pal;
3199 return NULL;
3202 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3204 static const WCHAR transparency_flagW[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3205 static const WCHAR colorW[] = { 'T','r','a','n','s','p','a','r','e','n','t','C','o','l','o','r','I','n','d','e','x',0 };
3206 PropertyItem *index = NULL;
3208 if (get_bool_property(reader, &GUID_MetadataFormatGCE, transparency_flagW))
3210 index = get_property(reader, &GUID_MetadataFormatGCE, colorW);
3211 if (index)
3212 index->id = PropertyTagIndexTransparent;
3214 return index;
3217 static LONG get_gif_frame_property(IWICBitmapFrameDecode *frame, const GUID *format, const WCHAR *property)
3219 HRESULT hr;
3220 IWICMetadataBlockReader *block_reader;
3221 IWICMetadataReader *reader;
3222 UINT block_count, i;
3223 PropertyItem *prop;
3224 LONG value = 0;
3226 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3227 if (hr == S_OK)
3229 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3230 if (hr == S_OK)
3232 for (i = 0; i < block_count; i++)
3234 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3235 if (hr == S_OK)
3237 prop = get_property(reader, format, property);
3238 if (prop)
3240 if (prop->type == PropertyTagTypeByte && prop->length == 1)
3241 value = *(BYTE *)prop->value;
3242 else if (prop->type == PropertyTagTypeShort && prop->length == 2)
3243 value = *(SHORT *)prop->value;
3245 heap_free(prop);
3247 IWICMetadataReader_Release(reader);
3251 IWICMetadataBlockReader_Release(block_reader);
3254 return value;
3257 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3259 static const WCHAR delayW[] = { 'D','e','l','a','y',0 };
3260 HRESULT hr;
3261 IWICBitmapFrameDecode *frame;
3262 IWICMetadataBlockReader *block_reader;
3263 IWICMetadataReader *reader;
3264 UINT frame_count, block_count, i;
3265 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3266 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3268 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3269 if (frame_count > 1)
3271 delay = heap_alloc_zero(sizeof(*delay) + frame_count * sizeof(LONG));
3272 if (delay)
3274 LONG *value;
3276 delay->type = PropertyTagTypeLong;
3277 delay->id = PropertyTagFrameDelay;
3278 delay->length = frame_count * sizeof(LONG);
3279 delay->value = delay + 1;
3281 value = delay->value;
3283 for (i = 0; i < frame_count; i++)
3285 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3286 if (hr == S_OK)
3288 value[i] = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, delayW);
3289 IWICBitmapFrameDecode_Release(frame);
3291 else value[i] = 0;
3296 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3297 if (hr == S_OK)
3299 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3300 if (hr == S_OK)
3302 for (i = 0; i < block_count; i++)
3304 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3305 if (hr == S_OK)
3307 if (!comment)
3308 comment = get_gif_comment(reader);
3310 if (frame_count > 1 && !loop)
3311 loop = get_gif_loopcount(reader);
3313 if (!background)
3314 background = get_gif_background(reader);
3316 if (!palette)
3317 palette = get_gif_palette(decoder, reader);
3319 IWICMetadataReader_Release(reader);
3323 IWICMetadataBlockReader_Release(block_reader);
3326 if (frame_count > 1 && !loop)
3328 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3329 if (loop)
3331 loop->type = PropertyTagTypeShort;
3332 loop->id = PropertyTagLoopCount;
3333 loop->length = sizeof(SHORT);
3334 loop->value = loop + 1;
3335 *(SHORT *)loop->value = 1;
3339 if (delay) add_property(bitmap, delay);
3340 if (comment) add_property(bitmap, comment);
3341 if (loop) add_property(bitmap, loop);
3342 if (palette) add_property(bitmap, palette);
3343 if (background) add_property(bitmap, background);
3345 heap_free(delay);
3346 heap_free(comment);
3347 heap_free(loop);
3348 heap_free(palette);
3349 heap_free(background);
3351 /* Win7 gdiplus always returns transparent color index from frame 0 */
3352 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3353 if (hr != S_OK) return;
3355 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3356 if (hr == S_OK)
3358 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3359 if (hr == S_OK)
3361 for (i = 0; i < block_count; i++)
3363 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3364 if (hr == S_OK)
3366 if (!transparent_idx)
3367 transparent_idx = get_gif_transparent_idx(reader);
3369 IWICMetadataReader_Release(reader);
3373 IWICMetadataBlockReader_Release(block_reader);
3376 if (transparent_idx) add_property(bitmap, transparent_idx);
3377 heap_free(transparent_idx);
3379 IWICBitmapFrameDecode_Release(frame);
3382 static PropertyItem* create_prop(PROPID propid, PROPVARIANT* value)
3384 PropertyItem *item = NULL;
3385 UINT item_size = propvariant_size(value);
3387 if (item_size)
3389 item_size += sizeof(*item);
3390 item = heap_alloc_zero(item_size);
3391 if (propvariant_to_item(value, item, item_size, propid) != Ok)
3393 heap_free(item);
3394 item = NULL;
3398 return item;
3401 static ULONG get_ulong_by_index(IWICMetadataReader* reader, ULONG index)
3403 PROPVARIANT value;
3404 HRESULT hr;
3405 ULONG result=0;
3407 hr = IWICMetadataReader_GetValueByIndex(reader, index, NULL, NULL, &value);
3408 if (SUCCEEDED(hr))
3410 switch (value.vt)
3412 case VT_UI4:
3413 result = value.u.ulVal;
3414 break;
3415 default:
3416 ERR("unhandled case %u\n", value.vt);
3417 break;
3419 PropVariantClear(&value);
3421 return result;
3424 static void png_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3426 HRESULT hr;
3427 IWICBitmapFrameDecode *frame;
3428 IWICMetadataBlockReader *block_reader;
3429 IWICMetadataReader *reader;
3430 UINT block_count, i, j;
3431 struct keyword_info {
3432 const char* name;
3433 PROPID propid;
3434 BOOL seen;
3435 } keywords[] = {
3436 { "Title", PropertyTagImageTitle },
3437 { "Author", PropertyTagArtist },
3438 { "Description", PropertyTagImageDescription },
3439 { "Copyright", PropertyTagCopyright },
3440 { "Software", PropertyTagSoftwareUsed },
3441 { "Source", PropertyTagEquipModel },
3442 { "Comment", PropertyTagExifUserComment },
3444 BOOL seen_gamma=FALSE, seen_whitepoint=FALSE, seen_chrm=FALSE;
3446 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3447 if (hr != S_OK) return;
3449 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3450 if (hr == S_OK)
3452 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3453 if (hr == S_OK)
3455 for (i = 0; i < block_count; i++)
3457 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3458 if (hr == S_OK)
3460 GUID format;
3462 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3463 if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunktEXt, &format))
3465 PROPVARIANT name, value;
3466 PropertyItem* item;
3468 hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, &name, &value);
3470 if (SUCCEEDED(hr))
3472 if (name.vt == VT_LPSTR)
3474 for (j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++)
3475 if (!strcmp(keywords[j].name, name.u.pszVal))
3476 break;
3477 if (j < sizeof(keywords)/sizeof(keywords[0]) && !keywords[j].seen)
3479 keywords[j].seen = TRUE;
3480 item = create_prop(keywords[j].propid, &value);
3481 if (item)
3482 add_property(bitmap, item);
3483 heap_free(item);
3487 PropVariantClear(&name);
3488 PropVariantClear(&value);
3491 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA, &format))
3493 PropertyItem* item;
3495 if (!seen_gamma)
3497 item = heap_alloc_zero(sizeof(PropertyItem) + sizeof(ULONG) * 2);
3498 if (item)
3500 ULONG *rational;
3501 item->length = sizeof(ULONG) * 2;
3502 item->type = PropertyTagTypeRational;
3503 item->id = PropertyTagGamma;
3504 rational = item->value = item + 1;
3505 rational[0] = 100000;
3506 rational[1] = get_ulong_by_index(reader, 0);
3507 add_property(bitmap, item);
3508 seen_gamma = TRUE;
3509 heap_free(item);
3513 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkcHRM, &format))
3515 PropertyItem* item;
3517 if (!seen_whitepoint)
3519 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 4);
3520 if (item)
3522 ULONG *rational;
3523 item->length = sizeof(ULONG) * 4;
3524 item->type = PropertyTagTypeRational;
3525 item->id = PropertyTagWhitePoint;
3526 rational = item->value = item + 1;
3527 rational[0] = get_ulong_by_index(reader, 0);
3528 rational[1] = 100000;
3529 rational[2] = get_ulong_by_index(reader, 1);
3530 rational[3] = 100000;
3531 add_property(bitmap, item);
3532 seen_whitepoint = TRUE;
3533 GdipFree(item);
3536 if (!seen_chrm)
3538 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 12);
3539 if (item)
3541 ULONG *rational;
3542 item->length = sizeof(ULONG) * 12;
3543 item->type = PropertyTagTypeRational;
3544 item->id = PropertyTagPrimaryChromaticities;
3545 rational = item->value = item + 1;
3546 rational[0] = get_ulong_by_index(reader, 2);
3547 rational[1] = 100000;
3548 rational[2] = get_ulong_by_index(reader, 3);
3549 rational[3] = 100000;
3550 rational[4] = get_ulong_by_index(reader, 4);
3551 rational[5] = 100000;
3552 rational[6] = get_ulong_by_index(reader, 5);
3553 rational[7] = 100000;
3554 rational[8] = get_ulong_by_index(reader, 6);
3555 rational[9] = 100000;
3556 rational[10] = get_ulong_by_index(reader, 7);
3557 rational[11] = 100000;
3558 add_property(bitmap, item);
3559 seen_chrm = TRUE;
3560 GdipFree(item);
3565 IWICMetadataReader_Release(reader);
3569 IWICMetadataBlockReader_Release(block_reader);
3572 IWICBitmapFrameDecode_Release(frame);
3575 static GpStatus initialize_decoder_wic(IStream *stream, REFGUID container, IWICBitmapDecoder **decoder)
3577 IWICImagingFactory *factory;
3578 HRESULT hr;
3580 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
3582 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3583 if (FAILED(hr)) return hresult_to_status(hr);
3584 hr = IWICImagingFactory_CreateDecoder(factory, container, NULL, decoder);
3585 IWICImagingFactory_Release(factory);
3586 if (FAILED(hr)) return hresult_to_status(hr);
3588 hr = IWICBitmapDecoder_Initialize(*decoder, stream, WICDecodeMetadataCacheOnLoad);
3589 if (FAILED(hr)) return hresult_to_status(hr);
3590 return Ok;
3593 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3595 static GpStatus decode_frame_wic(IWICBitmapDecoder *decoder, BOOL force_conversion,
3596 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3598 GpStatus status=Ok;
3599 GpBitmap *bitmap;
3600 HRESULT hr;
3601 IWICBitmapFrameDecode *frame;
3602 IWICBitmapSource *source=NULL;
3603 IWICMetadataBlockReader *block_reader;
3604 WICPixelFormatGUID wic_format;
3605 PixelFormat gdip_format=0;
3606 ColorPalette *palette = NULL;
3607 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3608 int i;
3609 UINT width, height, frame_count;
3610 BitmapData lockeddata;
3611 WICRect wrc;
3613 TRACE("%p,%u,%p\n", decoder, active_frame, image);
3615 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3616 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3617 if (SUCCEEDED(hr)) /* got frame */
3619 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3621 if (SUCCEEDED(hr))
3623 if (!force_conversion)
3625 for (i=0; pixel_formats[i].wic_format; i++)
3627 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3629 source = (IWICBitmapSource*)frame;
3630 IWICBitmapSource_AddRef(source);
3631 gdip_format = pixel_formats[i].gdip_format;
3632 palette_type = pixel_formats[i].palette_type;
3633 break;
3637 if (!source)
3639 /* unknown format; fall back on 32bppARGB */
3640 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3641 gdip_format = PixelFormat32bppARGB;
3643 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3646 if (SUCCEEDED(hr)) /* got source */
3648 hr = IWICBitmapSource_GetSize(source, &width, &height);
3650 if (SUCCEEDED(hr))
3651 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3652 NULL, &bitmap);
3654 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3656 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3657 gdip_format, &lockeddata);
3658 if (status == Ok) /* locked bitmap */
3660 wrc.X = 0;
3661 wrc.Width = width;
3662 wrc.Height = 1;
3663 for (i=0; i<height; i++)
3665 wrc.Y = i;
3666 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3667 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3668 if (FAILED(hr)) break;
3671 GdipBitmapUnlockBits(bitmap, &lockeddata);
3674 if (SUCCEEDED(hr) && status == Ok)
3675 *image = &bitmap->image;
3676 else
3678 *image = NULL;
3679 GdipDisposeImage(&bitmap->image);
3682 if (SUCCEEDED(hr) && status == Ok)
3684 double dpix, dpiy;
3685 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3686 if (SUCCEEDED(hr))
3688 bitmap->image.xres = dpix;
3689 bitmap->image.yres = dpiy;
3691 hr = S_OK;
3695 IWICBitmapSource_Release(source);
3698 if (SUCCEEDED(hr)) {
3699 bitmap->metadata_reader = NULL;
3701 if (metadata_reader)
3702 metadata_reader(bitmap, decoder, active_frame);
3703 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3705 UINT block_count = 0;
3706 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3707 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3708 IWICMetadataBlockReader_Release(block_reader);
3711 palette = get_palette(frame, palette_type);
3712 IWICBitmapFrameDecode_Release(frame);
3716 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3718 if (status == Ok)
3720 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3721 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI;
3722 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormat2bppGray) ||
3723 IsEqualGUID(&wic_format, &GUID_WICPixelFormat4bppGray) ||
3724 IsEqualGUID(&wic_format, &GUID_WICPixelFormat8bppGray) ||
3725 IsEqualGUID(&wic_format, &GUID_WICPixelFormat16bppGray))
3726 bitmap->image.flags |= ImageFlagsColorSpaceGRAY;
3727 else
3728 bitmap->image.flags |= ImageFlagsColorSpaceRGB;
3729 bitmap->image.frame_count = frame_count;
3730 bitmap->image.current_frame = active_frame;
3731 bitmap->image.decoder = decoder;
3732 IWICBitmapDecoder_AddRef(decoder);
3733 if (palette)
3735 heap_free(bitmap->image.palette);
3736 bitmap->image.palette = palette;
3738 else
3740 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3741 bitmap->image.palette->Flags = 0;
3743 TRACE("=> %p\n", *image);
3746 return status;
3749 static GpStatus decode_image_wic(IStream *stream, REFGUID container,
3750 metadata_reader_func metadata_reader, GpImage **image)
3752 IWICBitmapDecoder *decoder;
3753 GpStatus status;
3755 status = initialize_decoder_wic(stream, container, &decoder);
3756 if(status != Ok)
3757 return status;
3759 status = decode_frame_wic(decoder, FALSE, 0, metadata_reader, image);
3760 IWICBitmapDecoder_Release(decoder);
3761 return status;
3764 static GpStatus select_frame_wic(GpImage *image, UINT active_frame)
3766 GpImage *new_image;
3767 GpStatus status;
3769 status = decode_frame_wic(image->decoder, FALSE, active_frame, NULL, &new_image);
3770 if(status != Ok)
3771 return status;
3773 new_image->busy = image->busy;
3774 memcpy(&new_image->format, &image->format, sizeof(GUID));
3775 free_image_data(image);
3776 if (image->type == ImageTypeBitmap)
3777 *(GpBitmap *)image = *(GpBitmap *)new_image;
3778 else if (image->type == ImageTypeMetafile)
3779 *(GpMetafile *)image = *(GpMetafile *)new_image;
3780 new_image->type = ~0;
3781 heap_free(new_image);
3782 return Ok;
3785 static HRESULT get_gif_frame_rect(IWICBitmapFrameDecode *frame,
3786 UINT *left, UINT *top, UINT *width, UINT *height)
3788 static const WCHAR leftW[] = {'L','e','f','t',0};
3789 static const WCHAR topW[] = {'T','o','p',0};
3791 *left = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, leftW);
3792 *top = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, topW);
3794 return IWICBitmapFrameDecode_GetSize(frame, width, height);
3797 static HRESULT blit_gif_frame(GpBitmap *bitmap, IWICBitmapFrameDecode *frame, BOOL first_frame)
3799 UINT i, j, left, top, width, height;
3800 IWICBitmapSource *source;
3801 BYTE *new_bits;
3802 HRESULT hr;
3804 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3805 if(FAILED(hr))
3806 return hr;
3808 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3809 if(FAILED(hr))
3810 return hr;
3812 new_bits = heap_alloc_zero(width*height*4);
3813 if(!new_bits)
3814 return E_OUTOFMEMORY;
3816 hr = IWICBitmapSource_CopyPixels(source, NULL, width*4, width*height*4, new_bits);
3817 IWICBitmapSource_Release(source);
3818 if(FAILED(hr)) {
3819 heap_free(new_bits);
3820 return hr;
3823 for(i=0; i<height && i+top<bitmap->height; i++) {
3824 for(j=0; j<width && j+left<bitmap->width; j++) {
3825 DWORD *src = (DWORD*)(new_bits+i*width*4+j*4);
3826 DWORD *dst = (DWORD*)(bitmap->bits+(i+top)*bitmap->stride+(j+left)*4);
3828 if(first_frame || *src>>24 != 0)
3829 *dst = *src;
3832 heap_free(new_bits);
3833 return hr;
3836 static DWORD get_gif_background_color(GpBitmap *bitmap)
3838 BYTE bgcolor_idx = 0;
3839 UINT i;
3841 for(i=0; i<bitmap->prop_count; i++) {
3842 if(bitmap->prop_item[i].id == PropertyTagIndexBackground) {
3843 bgcolor_idx = *(BYTE*)bitmap->prop_item[i].value;
3844 break;
3848 for(i=0; i<bitmap->prop_count; i++) {
3849 if(bitmap->prop_item[i].id == PropertyTagIndexTransparent) {
3850 BYTE transparent_idx;
3851 transparent_idx = *(BYTE*)bitmap->prop_item[i].value;
3853 if(transparent_idx == bgcolor_idx)
3854 return 0;
3858 for(i=0; i<bitmap->prop_count; i++) {
3859 if(bitmap->prop_item[i].id == PropertyTagGlobalPalette) {
3860 if(bitmap->prop_item[i].length/3 > bgcolor_idx) {
3861 BYTE *color = ((BYTE*)bitmap->prop_item[i].value)+bgcolor_idx*3;
3862 return color[2] + (color[1]<<8) + (color[0]<<16) + (0xffu<<24);
3864 break;
3868 FIXME("can't get gif background color\n");
3869 return 0xffffffff;
3872 static GpStatus select_frame_gif(GpImage* image, UINT active_frame)
3874 static const WCHAR disposalW[] = {'D','i','s','p','o','s','a','l',0};
3876 GpBitmap *bitmap = (GpBitmap*)image;
3877 IWICBitmapFrameDecode *frame;
3878 int cur_frame=0, disposal;
3879 BOOL bgcolor_set = FALSE;
3880 DWORD bgcolor = 0;
3881 HRESULT hr;
3883 if(active_frame > image->current_frame) {
3884 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, image->current_frame, &frame);
3885 if(FAILED(hr))
3886 return hresult_to_status(hr);
3887 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3888 IWICBitmapFrameDecode_Release(frame);
3890 if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND)
3891 cur_frame = image->current_frame;
3892 else if(disposal != GIF_DISPOSE_RESTORE_TO_PREV)
3893 cur_frame = image->current_frame+1;
3896 while(cur_frame != active_frame) {
3897 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, cur_frame, &frame);
3898 if(FAILED(hr))
3899 return hresult_to_status(hr);
3900 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3902 if(disposal==GIF_DISPOSE_UNSPECIFIED || disposal==GIF_DISPOSE_DO_NOT_DISPOSE) {
3903 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3904 if(FAILED(hr))
3905 return hresult_to_status(hr);
3906 }else if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND) {
3907 UINT left, top, width, height, i, j;
3909 if(!bgcolor_set) {
3910 bgcolor = get_gif_background_color(bitmap);
3911 bgcolor_set = TRUE;
3914 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3915 if(FAILED(hr))
3916 return hresult_to_status(hr);
3917 for(i=top; i<top+height && i<bitmap->height; i++) {
3918 DWORD *bits = (DWORD*)(bitmap->bits+i*bitmap->stride);
3919 for(j=left; j<left+width && j<bitmap->width; j++)
3920 bits[j] = bgcolor;
3924 IWICBitmapFrameDecode_Release(frame);
3925 cur_frame++;
3928 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, active_frame, &frame);
3929 if(FAILED(hr))
3930 return hresult_to_status(hr);
3932 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3933 IWICBitmapFrameDecode_Release(frame);
3934 if(FAILED(hr))
3935 return hresult_to_status(hr);
3937 image->current_frame = active_frame;
3938 return Ok;
3941 static GpStatus decode_image_icon(IStream* stream, GpImage **image)
3943 return decode_image_wic(stream, &GUID_ContainerFormatIco, NULL, image);
3946 static GpStatus decode_image_bmp(IStream* stream, GpImage **image)
3948 GpStatus status;
3949 GpBitmap* bitmap;
3951 status = decode_image_wic(stream, &GUID_ContainerFormatBmp, NULL, image);
3953 bitmap = (GpBitmap*)*image;
3955 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3957 /* WIC supports bmp files with alpha, but gdiplus does not */
3958 bitmap->format = PixelFormat32bppRGB;
3961 return status;
3964 static GpStatus decode_image_jpeg(IStream* stream, GpImage **image)
3966 return decode_image_wic(stream, &GUID_ContainerFormatJpeg, NULL, image);
3969 static GpStatus decode_image_png(IStream* stream, GpImage **image)
3971 IWICBitmapDecoder *decoder;
3972 IWICBitmapFrameDecode *frame;
3973 GpStatus status;
3974 HRESULT hr;
3975 GUID format;
3976 BOOL force_conversion = FALSE;
3978 status = initialize_decoder_wic(stream, &GUID_ContainerFormatPng, &decoder);
3979 if (status != Ok)
3980 return status;
3982 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3983 if (hr == S_OK)
3985 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
3986 if (hr == S_OK)
3988 if (IsEqualGUID(&format, &GUID_WICPixelFormat8bppGray))
3989 force_conversion = TRUE;
3990 status = decode_frame_wic(decoder, force_conversion, 0, png_metadata_reader, image);
3992 else
3993 status = hresult_to_status(hr);
3995 IWICBitmapFrameDecode_Release(frame);
3997 else
3998 status = hresult_to_status(hr);
4000 IWICBitmapDecoder_Release(decoder);
4001 return status;
4004 static GpStatus decode_image_gif(IStream* stream, GpImage **image)
4006 IWICBitmapDecoder *decoder;
4007 UINT frame_count;
4008 GpStatus status;
4009 HRESULT hr;
4011 status = initialize_decoder_wic(stream, &GUID_ContainerFormatGif, &decoder);
4012 if(status != Ok)
4013 return status;
4015 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
4016 if(FAILED(hr))
4017 return hresult_to_status(hr);
4019 status = decode_frame_wic(decoder, frame_count > 1, 0, gif_metadata_reader, image);
4020 IWICBitmapDecoder_Release(decoder);
4021 if(status != Ok)
4022 return status;
4024 if(frame_count > 1) {
4025 heap_free((*image)->palette);
4026 (*image)->palette = NULL;
4028 return Ok;
4031 static GpStatus decode_image_tiff(IStream* stream, GpImage **image)
4033 return decode_image_wic(stream, &GUID_ContainerFormatTiff, NULL, image);
4036 static GpStatus load_wmf(IStream *stream, GpMetafile **metafile)
4038 WmfPlaceableFileHeader pfh;
4039 BOOL is_placeable = FALSE;
4040 LARGE_INTEGER seek;
4041 GpStatus status;
4042 METAHEADER mh;
4043 HMETAFILE hmf;
4044 HRESULT hr;
4045 UINT size;
4046 void *buf;
4048 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4049 if (hr != S_OK || size != sizeof(mh))
4050 return GenericError;
4052 if (((WmfPlaceableFileHeader *)&mh)->Key == WMF_PLACEABLE_KEY)
4054 seek.QuadPart = 0;
4055 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4056 if (FAILED(hr)) return hresult_to_status(hr);
4058 hr = IStream_Read(stream, &pfh, sizeof(pfh), &size);
4059 if (hr != S_OK || size != sizeof(pfh))
4060 return GenericError;
4062 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4063 if (hr != S_OK || size != sizeof(mh))
4064 return GenericError;
4066 is_placeable = TRUE;
4069 seek.QuadPart = is_placeable ? sizeof(pfh) : 0;
4070 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4071 if (FAILED(hr)) return hresult_to_status(hr);
4073 buf = heap_alloc(mh.mtSize * 2);
4074 if (!buf) return OutOfMemory;
4076 hr = IStream_Read(stream, buf, mh.mtSize * 2, &size);
4077 if (hr != S_OK || size != mh.mtSize * 2)
4079 heap_free(buf);
4080 return GenericError;
4083 hmf = SetMetaFileBitsEx(mh.mtSize * 2, buf);
4084 heap_free(buf);
4085 if (!hmf)
4086 return GenericError;
4088 status = GdipCreateMetafileFromWmf(hmf, TRUE, is_placeable ? &pfh : NULL, metafile);
4089 if (status != Ok)
4090 DeleteMetaFile(hmf);
4091 return status;
4094 static GpStatus decode_image_wmf(IStream *stream, GpImage **image)
4096 GpMetafile *metafile;
4097 GpStatus status;
4099 TRACE("%p %p\n", stream, image);
4101 if (!stream || !image)
4102 return InvalidParameter;
4104 status = load_wmf(stream, &metafile);
4105 if (status != Ok)
4107 TRACE("Could not load metafile\n");
4108 return status;
4111 *image = (GpImage *)metafile;
4112 TRACE("<-- %p\n", *image);
4114 return Ok;
4117 static GpStatus load_emf(IStream *stream, GpMetafile **metafile)
4119 LARGE_INTEGER seek;
4120 ENHMETAHEADER emh;
4121 HENHMETAFILE hemf;
4122 GpStatus status;
4123 HRESULT hr;
4124 UINT size;
4125 void *buf;
4127 hr = IStream_Read(stream, &emh, sizeof(emh), &size);
4128 if (hr != S_OK || size != sizeof(emh) || emh.dSignature != ENHMETA_SIGNATURE)
4129 return GenericError;
4131 seek.QuadPart = 0;
4132 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4133 if (FAILED(hr)) return hresult_to_status(hr);
4135 buf = heap_alloc(emh.nBytes);
4136 if (!buf) return OutOfMemory;
4138 hr = IStream_Read(stream, buf, emh.nBytes, &size);
4139 if (hr != S_OK || size != emh.nBytes)
4141 heap_free(buf);
4142 return GenericError;
4145 hemf = SetEnhMetaFileBits(emh.nBytes, buf);
4146 heap_free(buf);
4147 if (!hemf)
4148 return GenericError;
4150 status = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
4151 if (status != Ok)
4152 DeleteEnhMetaFile(hemf);
4153 return status;
4156 static GpStatus decode_image_emf(IStream *stream, GpImage **image)
4158 GpMetafile *metafile;
4159 GpStatus status;
4161 TRACE("%p %p\n", stream, image);
4163 if (!stream || !image)
4164 return InvalidParameter;
4166 status = load_emf(stream, &metafile);
4167 if (status != Ok)
4169 TRACE("Could not load metafile\n");
4170 return status;
4173 *image = (GpImage *)metafile;
4174 TRACE("<-- %p\n", *image);
4176 return Ok;
4179 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
4180 GDIPCONST EncoderParameters* params);
4182 typedef GpStatus (*decode_image_func)(IStream *stream, GpImage **image);
4184 typedef GpStatus (*select_image_func)(GpImage *image, UINT active_frame);
4186 typedef struct image_codec {
4187 ImageCodecInfo info;
4188 encode_image_func encode_func;
4189 decode_image_func decode_func;
4190 select_image_func select_func;
4191 } image_codec;
4193 typedef enum {
4194 BMP,
4195 JPEG,
4196 GIF,
4197 TIFF,
4198 EMF,
4199 WMF,
4200 PNG,
4201 ICO,
4202 NUM_CODECS
4203 } ImageFormat;
4205 static const struct image_codec codecs[NUM_CODECS];
4207 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
4209 BYTE signature[8];
4210 const BYTE *pattern, *mask;
4211 LARGE_INTEGER seek;
4212 HRESULT hr;
4213 UINT bytesread;
4214 int i;
4215 DWORD j, sig;
4217 /* seek to the start of the stream */
4218 seek.QuadPart = 0;
4219 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4220 if (FAILED(hr)) return hresult_to_status(hr);
4222 /* read the first 8 bytes */
4223 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4224 hr = IStream_Read(stream, signature, 8, &bytesread);
4225 if (FAILED(hr)) return hresult_to_status(hr);
4226 if (hr == S_FALSE || bytesread == 0) return GenericError;
4228 for (i = 0; i < NUM_CODECS; i++) {
4229 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4230 bytesread >= codecs[i].info.SigSize)
4232 for (sig=0; sig<codecs[i].info.SigCount; sig++)
4234 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
4235 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
4236 for (j=0; j<codecs[i].info.SigSize; j++)
4237 if ((signature[j] & mask[j]) != pattern[j])
4238 break;
4239 if (j == codecs[i].info.SigSize)
4241 *result = &codecs[i];
4242 return Ok;
4248 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
4249 signature[0],signature[1],signature[2],signature[3],
4250 signature[4],signature[5],signature[6],signature[7]);
4252 return GenericError;
4255 static GpStatus get_decoder_info_from_image(GpImage *image, const struct image_codec **result)
4257 int i;
4259 for (i = 0; i < NUM_CODECS; i++) {
4260 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4261 IsEqualIID(&codecs[i].info.FormatID, &image->format))
4263 *result = &codecs[i];
4264 return Ok;
4268 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image->format));
4269 return GenericError;
4272 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
4273 UINT frame)
4275 GpStatus stat;
4276 const struct image_codec *codec = NULL;
4277 BOOL unlock;
4279 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
4281 if (!image || !dimensionID)
4282 return InvalidParameter;
4283 if(!image_lock(image, &unlock))
4284 return ObjectBusy;
4286 if (frame >= image->frame_count)
4288 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
4289 image_unlock(image, unlock);
4290 return InvalidParameter;
4293 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
4295 WARN("invalid image type %d\n", image->type);
4296 image_unlock(image, unlock);
4297 return InvalidParameter;
4300 if (image->current_frame == frame)
4302 image_unlock(image, unlock);
4303 return Ok;
4306 if (!image->decoder)
4308 TRACE("image doesn't have an associated decoder\n");
4309 image_unlock(image, unlock);
4310 return Ok;
4313 /* choose an appropriate image decoder */
4314 stat = get_decoder_info_from_image(image, &codec);
4315 if (stat != Ok)
4317 WARN("can't find decoder info\n");
4318 image_unlock(image, unlock);
4319 return stat;
4322 stat = codec->select_func(image, frame);
4323 image_unlock(image, unlock);
4324 return stat;
4327 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
4329 GpStatus stat;
4330 LARGE_INTEGER seek;
4331 HRESULT hr;
4332 const struct image_codec *codec=NULL;
4334 /* choose an appropriate image decoder */
4335 stat = get_decoder_info(stream, &codec);
4336 if (stat != Ok) return stat;
4338 /* seek to the start of the stream */
4339 seek.QuadPart = 0;
4340 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4341 if (FAILED(hr)) return hresult_to_status(hr);
4343 /* call on the image decoder to do the real work */
4344 stat = codec->decode_func(stream, image);
4346 /* take note of the original data format */
4347 if (stat == Ok)
4349 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
4350 return Ok;
4353 return stat;
4356 /* FIXME: no ICM */
4357 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
4359 TRACE("%p %p\n", stream, image);
4361 return GdipLoadImageFromStream(stream, image);
4364 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
4366 static int calls;
4368 TRACE("(%p,%u)\n", image, propId);
4370 if(!image)
4371 return InvalidParameter;
4373 if(!(calls++))
4374 FIXME("not implemented\n");
4376 return NotImplemented;
4379 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
4381 static int calls;
4383 if (!image || !item) return InvalidParameter;
4385 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
4387 if(!(calls++))
4388 FIXME("not implemented\n");
4390 return Ok;
4393 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
4394 GDIPCONST CLSID *clsidEncoder,
4395 GDIPCONST EncoderParameters *encoderParams)
4397 GpStatus stat;
4398 IStream *stream;
4400 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
4402 if (!image || !filename|| !clsidEncoder)
4403 return InvalidParameter;
4405 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4406 if (stat != Ok)
4407 return GenericError;
4409 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4411 IStream_Release(stream);
4412 return stat;
4415 /*************************************************************************
4416 * Encoding functions -
4417 * These functions encode an image in different image file formats.
4420 static GpStatus encode_image_wic(GpImage *image, IStream* stream,
4421 REFGUID container, GDIPCONST EncoderParameters* params)
4423 GpStatus stat;
4424 GpBitmap *bitmap;
4425 IWICImagingFactory *factory;
4426 IWICBitmapEncoder *encoder;
4427 IWICBitmapFrameEncode *frameencode;
4428 IPropertyBag2 *encoderoptions;
4429 HRESULT hr;
4430 UINT width, height;
4431 PixelFormat gdipformat=0;
4432 const WICPixelFormatGUID *desired_wicformat;
4433 WICPixelFormatGUID wicformat;
4434 GpRect rc;
4435 BitmapData lockeddata;
4436 UINT i;
4438 if (image->type != ImageTypeBitmap)
4439 return GenericError;
4441 bitmap = (GpBitmap*)image;
4443 GdipGetImageWidth(image, &width);
4444 GdipGetImageHeight(image, &height);
4446 rc.X = 0;
4447 rc.Y = 0;
4448 rc.Width = width;
4449 rc.Height = height;
4451 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
4452 if (FAILED(hr))
4453 return hresult_to_status(hr);
4454 hr = IWICImagingFactory_CreateEncoder(factory, container, NULL, &encoder);
4455 IWICImagingFactory_Release(factory);
4456 if (FAILED(hr))
4457 return hresult_to_status(hr);
4459 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
4461 if (SUCCEEDED(hr))
4463 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4466 if (SUCCEEDED(hr)) /* created frame */
4468 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4470 if (SUCCEEDED(hr))
4471 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4473 if (SUCCEEDED(hr))
4474 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4476 if (SUCCEEDED(hr))
4478 for (i=0; pixel_formats[i].wic_format; i++)
4480 if (pixel_formats[i].gdip_format == bitmap->format)
4482 desired_wicformat = pixel_formats[i].wic_format;
4483 gdipformat = bitmap->format;
4484 break;
4487 if (!gdipformat)
4489 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
4490 gdipformat = PixelFormat32bppARGB;
4493 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
4494 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4497 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
4499 /* Encoder doesn't support this bitmap's format. */
4500 gdipformat = 0;
4501 for (i=0; pixel_formats[i].wic_format; i++)
4503 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
4505 gdipformat = pixel_formats[i].gdip_format;
4506 break;
4509 if (!gdipformat)
4511 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
4512 hr = E_FAIL;
4516 if (SUCCEEDED(hr))
4518 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4519 &lockeddata);
4521 if (stat == Ok)
4523 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4524 BYTE *row;
4526 /* write one row at a time in case stride is negative */
4527 row = lockeddata.Scan0;
4528 for (i=0; i<lockeddata.Height; i++)
4530 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4531 if (FAILED(hr)) break;
4532 row += lockeddata.Stride;
4535 GdipBitmapUnlockBits(bitmap, &lockeddata);
4537 else
4538 hr = E_FAIL;
4541 if (SUCCEEDED(hr))
4542 hr = IWICBitmapFrameEncode_Commit(frameencode);
4544 IWICBitmapFrameEncode_Release(frameencode);
4545 IPropertyBag2_Release(encoderoptions);
4548 if (SUCCEEDED(hr))
4549 hr = IWICBitmapEncoder_Commit(encoder);
4551 IWICBitmapEncoder_Release(encoder);
4552 return hresult_to_status(hr);
4555 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4556 GDIPCONST EncoderParameters* params)
4558 return encode_image_wic(image, stream, &GUID_ContainerFormatBmp, params);
4561 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4562 GDIPCONST EncoderParameters* params)
4564 return encode_image_wic(image, stream, &GUID_ContainerFormatTiff, params);
4567 GpStatus encode_image_png(GpImage *image, IStream* stream,
4568 GDIPCONST EncoderParameters* params)
4570 return encode_image_wic(image, stream, &GUID_ContainerFormatPng, params);
4573 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4574 GDIPCONST EncoderParameters* params)
4576 return encode_image_wic(image, stream, &GUID_ContainerFormatJpeg, params);
4579 static GpStatus encode_image_gif(GpImage *image, IStream* stream,
4580 GDIPCONST EncoderParameters* params)
4582 return encode_image_wic(image, stream, &CLSID_WICGifEncoder, params);
4585 /*****************************************************************************
4586 * GdipSaveImageToStream [GDIPLUS.@]
4588 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4589 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4591 GpStatus stat;
4592 encode_image_func encode_image;
4593 int i;
4595 TRACE("%p %p %p %p\n", image, stream, clsid, params);
4597 if(!image || !stream)
4598 return InvalidParameter;
4600 /* select correct encoder */
4601 encode_image = NULL;
4602 for (i = 0; i < NUM_CODECS; i++) {
4603 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4604 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4605 encode_image = codecs[i].encode_func;
4607 if (encode_image == NULL)
4608 return UnknownImageFormat;
4610 stat = encode_image(image, stream, params);
4612 return stat;
4615 /*****************************************************************************
4616 * GdipSaveAdd [GDIPLUS.@]
4618 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4620 FIXME("(%p,%p): stub\n", image, params);
4621 return Ok;
4624 /*****************************************************************************
4625 * GdipGetImagePalette [GDIPLUS.@]
4627 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4629 INT count;
4631 TRACE("(%p,%p,%i)\n", image, palette, size);
4633 if (!image || !palette)
4634 return InvalidParameter;
4636 count = image->palette ? image->palette->Count : 0;
4638 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4640 TRACE("<-- InsufficientBuffer\n");
4641 return InsufficientBuffer;
4644 if (image->palette)
4646 palette->Flags = image->palette->Flags;
4647 palette->Count = image->palette->Count;
4648 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4650 else
4652 palette->Flags = 0;
4653 palette->Count = 0;
4655 return Ok;
4658 /*****************************************************************************
4659 * GdipSetImagePalette [GDIPLUS.@]
4661 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4662 GDIPCONST ColorPalette *palette)
4664 ColorPalette *new_palette;
4666 TRACE("(%p,%p)\n", image, palette);
4668 if(!image || !palette || palette->Count > 256)
4669 return InvalidParameter;
4671 new_palette = heap_alloc_zero(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4672 if (!new_palette) return OutOfMemory;
4674 heap_free(image->palette);
4675 image->palette = new_palette;
4676 image->palette->Flags = palette->Flags;
4677 image->palette->Count = palette->Count;
4678 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4680 return Ok;
4683 /*************************************************************************
4684 * Encoders -
4685 * Structures that represent which formats we support for encoding.
4688 /* ImageCodecInfo creation routines taken from libgdiplus */
4689 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4690 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4691 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4692 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4693 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4694 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4696 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4697 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4698 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4699 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4700 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4701 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4703 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4704 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4705 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4706 static const WCHAR gif_format[] = {'G','I','F',0};
4707 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4708 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4710 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4711 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4712 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4713 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4714 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4715 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4717 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4718 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4719 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4720 static const WCHAR emf_format[] = {'E','M','F',0};
4721 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4722 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4724 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4725 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4726 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4727 static const WCHAR wmf_format[] = {'W','M','F',0};
4728 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4729 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4731 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4732 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4733 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4734 static const WCHAR png_format[] = {'P','N','G',0};
4735 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4736 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4738 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4739 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4740 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4741 static const WCHAR ico_format[] = {'I','C','O',0};
4742 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4743 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4745 static const struct image_codec codecs[NUM_CODECS] = {
4747 { /* BMP */
4748 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4749 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4750 /* CodecName */ bmp_codecname,
4751 /* DllName */ NULL,
4752 /* FormatDescription */ bmp_format,
4753 /* FilenameExtension */ bmp_extension,
4754 /* MimeType */ bmp_mimetype,
4755 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4756 /* Version */ 1,
4757 /* SigCount */ 1,
4758 /* SigSize */ 2,
4759 /* SigPattern */ bmp_sig_pattern,
4760 /* SigMask */ bmp_sig_mask,
4762 encode_image_BMP,
4763 decode_image_bmp,
4764 select_frame_wic
4767 { /* JPEG */
4768 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4769 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4770 /* CodecName */ jpeg_codecname,
4771 /* DllName */ NULL,
4772 /* FormatDescription */ jpeg_format,
4773 /* FilenameExtension */ jpeg_extension,
4774 /* MimeType */ jpeg_mimetype,
4775 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4776 /* Version */ 1,
4777 /* SigCount */ 1,
4778 /* SigSize */ 2,
4779 /* SigPattern */ jpeg_sig_pattern,
4780 /* SigMask */ jpeg_sig_mask,
4782 encode_image_jpeg,
4783 decode_image_jpeg,
4784 select_frame_wic
4787 { /* GIF */
4788 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4789 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4790 /* CodecName */ gif_codecname,
4791 /* DllName */ NULL,
4792 /* FormatDescription */ gif_format,
4793 /* FilenameExtension */ gif_extension,
4794 /* MimeType */ gif_mimetype,
4795 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4796 /* Version */ 1,
4797 /* SigCount */ 2,
4798 /* SigSize */ 6,
4799 /* SigPattern */ gif_sig_pattern,
4800 /* SigMask */ gif_sig_mask,
4802 encode_image_gif,
4803 decode_image_gif,
4804 select_frame_gif
4807 { /* TIFF */
4808 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4809 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4810 /* CodecName */ tiff_codecname,
4811 /* DllName */ NULL,
4812 /* FormatDescription */ tiff_format,
4813 /* FilenameExtension */ tiff_extension,
4814 /* MimeType */ tiff_mimetype,
4815 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4816 /* Version */ 1,
4817 /* SigCount */ 2,
4818 /* SigSize */ 4,
4819 /* SigPattern */ tiff_sig_pattern,
4820 /* SigMask */ tiff_sig_mask,
4822 encode_image_tiff,
4823 decode_image_tiff,
4824 select_frame_wic
4827 { /* EMF */
4828 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4829 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4830 /* CodecName */ emf_codecname,
4831 /* DllName */ NULL,
4832 /* FormatDescription */ emf_format,
4833 /* FilenameExtension */ emf_extension,
4834 /* MimeType */ emf_mimetype,
4835 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4836 /* Version */ 1,
4837 /* SigCount */ 1,
4838 /* SigSize */ 4,
4839 /* SigPattern */ emf_sig_pattern,
4840 /* SigMask */ emf_sig_mask,
4842 NULL,
4843 decode_image_emf,
4844 NULL
4847 { /* WMF */
4848 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4849 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4850 /* CodecName */ wmf_codecname,
4851 /* DllName */ NULL,
4852 /* FormatDescription */ wmf_format,
4853 /* FilenameExtension */ wmf_extension,
4854 /* MimeType */ wmf_mimetype,
4855 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4856 /* Version */ 1,
4857 /* SigCount */ 1,
4858 /* SigSize */ 2,
4859 /* SigPattern */ wmf_sig_pattern,
4860 /* SigMask */ wmf_sig_mask,
4862 NULL,
4863 decode_image_wmf,
4864 NULL
4867 { /* PNG */
4868 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4869 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4870 /* CodecName */ png_codecname,
4871 /* DllName */ NULL,
4872 /* FormatDescription */ png_format,
4873 /* FilenameExtension */ png_extension,
4874 /* MimeType */ png_mimetype,
4875 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4876 /* Version */ 1,
4877 /* SigCount */ 1,
4878 /* SigSize */ 8,
4879 /* SigPattern */ png_sig_pattern,
4880 /* SigMask */ png_sig_mask,
4882 encode_image_png,
4883 decode_image_png,
4884 select_frame_wic
4887 { /* ICO */
4888 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4889 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4890 /* CodecName */ ico_codecname,
4891 /* DllName */ NULL,
4892 /* FormatDescription */ ico_format,
4893 /* FilenameExtension */ ico_extension,
4894 /* MimeType */ ico_mimetype,
4895 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4896 /* Version */ 1,
4897 /* SigCount */ 1,
4898 /* SigSize */ 4,
4899 /* SigPattern */ ico_sig_pattern,
4900 /* SigMask */ ico_sig_mask,
4902 NULL,
4903 decode_image_icon,
4904 select_frame_wic
4908 /*****************************************************************************
4909 * GdipGetImageDecodersSize [GDIPLUS.@]
4911 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
4913 int decoder_count=0;
4914 int i;
4915 TRACE("%p %p\n", numDecoders, size);
4917 if (!numDecoders || !size)
4918 return InvalidParameter;
4920 for (i=0; i<NUM_CODECS; i++)
4922 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4923 decoder_count++;
4926 *numDecoders = decoder_count;
4927 *size = decoder_count * sizeof(ImageCodecInfo);
4929 return Ok;
4932 /*****************************************************************************
4933 * GdipGetImageDecoders [GDIPLUS.@]
4935 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
4937 int i, decoder_count=0;
4938 TRACE("%u %u %p\n", numDecoders, size, decoders);
4940 if (!decoders ||
4941 size != numDecoders * sizeof(ImageCodecInfo))
4942 return GenericError;
4944 for (i=0; i<NUM_CODECS; i++)
4946 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4948 if (decoder_count == numDecoders) return GenericError;
4949 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4950 decoder_count++;
4954 if (decoder_count < numDecoders) return GenericError;
4956 return Ok;
4959 /*****************************************************************************
4960 * GdipGetImageEncodersSize [GDIPLUS.@]
4962 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4964 int encoder_count=0;
4965 int i;
4966 TRACE("%p %p\n", numEncoders, size);
4968 if (!numEncoders || !size)
4969 return InvalidParameter;
4971 for (i=0; i<NUM_CODECS; i++)
4973 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4974 encoder_count++;
4977 *numEncoders = encoder_count;
4978 *size = encoder_count * sizeof(ImageCodecInfo);
4980 return Ok;
4983 /*****************************************************************************
4984 * GdipGetImageEncoders [GDIPLUS.@]
4986 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4988 int i, encoder_count=0;
4989 TRACE("%u %u %p\n", numEncoders, size, encoders);
4991 if (!encoders ||
4992 size != numEncoders * sizeof(ImageCodecInfo))
4993 return GenericError;
4995 for (i=0; i<NUM_CODECS; i++)
4997 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4999 if (encoder_count == numEncoders) return GenericError;
5000 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
5001 encoder_count++;
5005 if (encoder_count < numEncoders) return GenericError;
5007 return Ok;
5010 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
5011 GDIPCONST CLSID* clsidEncoder, UINT *size)
5013 static int calls;
5015 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
5017 if(!(calls++))
5018 FIXME("not implemented\n");
5020 *size = 0;
5022 return NotImplemented;
5025 static PixelFormat get_16bpp_format(HBITMAP hbm)
5027 BITMAPV4HEADER bmh;
5028 HDC hdc;
5029 PixelFormat result;
5031 hdc = CreateCompatibleDC(NULL);
5033 memset(&bmh, 0, sizeof(bmh));
5034 bmh.bV4Size = sizeof(bmh);
5035 bmh.bV4Width = 1;
5036 bmh.bV4Height = 1;
5037 bmh.bV4V4Compression = BI_BITFIELDS;
5038 bmh.bV4BitCount = 16;
5040 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
5042 if (bmh.bV4RedMask == 0x7c00 &&
5043 bmh.bV4GreenMask == 0x3e0 &&
5044 bmh.bV4BlueMask == 0x1f)
5046 result = PixelFormat16bppRGB555;
5048 else if (bmh.bV4RedMask == 0xf800 &&
5049 bmh.bV4GreenMask == 0x7e0 &&
5050 bmh.bV4BlueMask == 0x1f)
5052 result = PixelFormat16bppRGB565;
5054 else
5056 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
5057 bmh.bV4GreenMask, bmh.bV4BlueMask);
5058 result = PixelFormatUndefined;
5061 DeleteDC(hdc);
5063 return result;
5066 /*****************************************************************************
5067 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
5069 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
5071 BITMAP bm;
5072 GpStatus retval;
5073 PixelFormat format;
5074 BitmapData lockeddata;
5076 TRACE("%p %p %p\n", hbm, hpal, bitmap);
5078 if(!hbm || !bitmap)
5079 return InvalidParameter;
5081 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
5082 return InvalidParameter;
5084 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
5085 switch(bm.bmBitsPixel) {
5086 case 1:
5087 format = PixelFormat1bppIndexed;
5088 break;
5089 case 4:
5090 format = PixelFormat4bppIndexed;
5091 break;
5092 case 8:
5093 format = PixelFormat8bppIndexed;
5094 break;
5095 case 16:
5096 format = get_16bpp_format(hbm);
5097 if (format == PixelFormatUndefined)
5098 return InvalidParameter;
5099 break;
5100 case 24:
5101 format = PixelFormat24bppRGB;
5102 break;
5103 case 32:
5104 format = PixelFormat32bppRGB;
5105 break;
5106 case 48:
5107 format = PixelFormat48bppRGB;
5108 break;
5109 default:
5110 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
5111 return InvalidParameter;
5114 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
5115 format, NULL, bitmap);
5117 if (retval == Ok)
5119 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
5120 format, &lockeddata);
5121 if (retval == Ok)
5123 HDC hdc;
5124 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
5125 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
5126 INT src_height;
5128 hdc = CreateCompatibleDC(NULL);
5130 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
5131 pbmi->bmiHeader.biBitCount = 0;
5133 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
5135 src_height = abs(pbmi->bmiHeader.biHeight);
5136 pbmi->bmiHeader.biHeight = -src_height;
5138 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
5140 DeleteDC(hdc);
5142 GdipBitmapUnlockBits(*bitmap, &lockeddata);
5145 if (retval == Ok && hpal)
5147 PALETTEENTRY entry[256];
5148 ColorPalette *palette=NULL;
5149 int i, num_palette_entries;
5151 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
5152 if (!num_palette_entries)
5153 retval = GenericError;
5155 palette = heap_alloc_zero(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
5156 if (!palette)
5157 retval = OutOfMemory;
5159 if (retval == Ok)
5161 palette->Flags = 0;
5162 palette->Count = num_palette_entries;
5164 for (i=0; i<num_palette_entries; i++)
5166 palette->Entries[i] = 0xff000000 | entry[i].peRed << 16 |
5167 entry[i].peGreen << 8 | entry[i].peBlue;
5170 retval = GdipSetImagePalette(&(*bitmap)->image, palette);
5173 heap_free(palette);
5176 if (retval != Ok)
5178 GdipDisposeImage(&(*bitmap)->image);
5179 *bitmap = NULL;
5183 return retval;
5186 /*****************************************************************************
5187 * GdipCreateEffect [GDIPLUS.@]
5189 GpStatus WINGDIPAPI GdipCreateEffect(const GUID guid, CGpEffect **effect)
5191 FIXME("(%s, %p): stub\n", debugstr_guid(&guid), effect);
5193 if(!effect)
5194 return InvalidParameter;
5196 *effect = NULL;
5198 return NotImplemented;
5201 /*****************************************************************************
5202 * GdipDeleteEffect [GDIPLUS.@]
5204 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
5206 FIXME("(%p): stub\n", effect);
5207 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5208 * in Windows's gdiplus */
5209 return NotImplemented;
5212 /*****************************************************************************
5213 * GdipSetEffectParameters [GDIPLUS.@]
5215 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
5216 const VOID *params, const UINT size)
5218 static int calls;
5220 TRACE("(%p,%p,%u)\n", effect, params, size);
5222 if(!(calls++))
5223 FIXME("not implemented\n");
5225 return NotImplemented;
5228 /*****************************************************************************
5229 * GdipGetImageFlags [GDIPLUS.@]
5231 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
5233 TRACE("%p %p\n", image, flags);
5235 if(!image || !flags)
5236 return InvalidParameter;
5238 *flags = image->flags;
5240 return Ok;
5243 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
5245 TRACE("(%d, %p)\n", control, param);
5247 switch(control){
5248 case TestControlForceBilinear:
5249 if(param)
5250 FIXME("TestControlForceBilinear not handled\n");
5251 break;
5252 case TestControlNoICM:
5253 if(param)
5254 FIXME("TestControlNoICM not handled\n");
5255 break;
5256 case TestControlGetBuildNumber:
5257 *((DWORD*)param) = 3102;
5258 break;
5261 return Ok;
5264 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
5266 TRACE("%p\n", image);
5268 return Ok;
5271 /*****************************************************************************
5272 * GdipGetImageThumbnail [GDIPLUS.@]
5274 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
5275 GpImage **ret_image, GetThumbnailImageAbort cb,
5276 VOID * cb_data)
5278 GpStatus stat;
5279 GpGraphics *graphics;
5280 UINT srcwidth, srcheight;
5282 TRACE("(%p %u %u %p %p %p)\n",
5283 image, width, height, ret_image, cb, cb_data);
5285 if (!image || !ret_image)
5286 return InvalidParameter;
5288 if (!width) width = 120;
5289 if (!height) height = 120;
5291 GdipGetImageWidth(image, &srcwidth);
5292 GdipGetImageHeight(image, &srcheight);
5294 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
5295 NULL, (GpBitmap**)ret_image);
5297 if (stat == Ok)
5299 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
5301 if (stat == Ok)
5303 stat = GdipDrawImageRectRectI(graphics, image,
5304 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
5305 NULL, NULL, NULL);
5307 GdipDeleteGraphics(graphics);
5310 if (stat != Ok)
5312 GdipDisposeImage(*ret_image);
5313 *ret_image = NULL;
5317 return stat;
5320 /*****************************************************************************
5321 * GdipImageRotateFlip [GDIPLUS.@]
5323 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
5325 GpBitmap *new_bitmap;
5326 GpBitmap *bitmap;
5327 int bpp, bytesperpixel;
5328 BOOL rotate_90, flip_x, flip_y;
5329 int src_x_offset, src_y_offset;
5330 LPBYTE src_origin;
5331 UINT x, y, width, height;
5332 BitmapData src_lock, dst_lock;
5333 GpStatus stat;
5334 BOOL unlock;
5336 TRACE("(%p, %u)\n", image, type);
5338 if (!image)
5339 return InvalidParameter;
5340 if (!image_lock(image, &unlock))
5341 return ObjectBusy;
5343 rotate_90 = type&1;
5344 flip_x = (type&6) == 2 || (type&6) == 4;
5345 flip_y = (type&3) == 1 || (type&3) == 2;
5347 if (image->type != ImageTypeBitmap)
5349 FIXME("Not implemented for type %i\n", image->type);
5350 image_unlock(image, unlock);
5351 return NotImplemented;
5354 bitmap = (GpBitmap*)image;
5355 bpp = PIXELFORMATBPP(bitmap->format);
5357 if (bpp < 8)
5359 FIXME("Not implemented for %i bit images\n", bpp);
5360 image_unlock(image, unlock);
5361 return NotImplemented;
5364 if (rotate_90)
5366 width = bitmap->height;
5367 height = bitmap->width;
5369 else
5371 width = bitmap->width;
5372 height = bitmap->height;
5375 bytesperpixel = bpp/8;
5377 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5379 if (stat != Ok)
5381 image_unlock(image, unlock);
5382 return stat;
5385 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
5387 if (stat == Ok)
5389 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5391 if (stat == Ok)
5393 LPBYTE src_row, src_pixel;
5394 LPBYTE dst_row, dst_pixel;
5396 src_origin = src_lock.Scan0;
5397 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5398 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
5400 if (rotate_90)
5402 if (flip_y) src_x_offset = -src_lock.Stride;
5403 else src_x_offset = src_lock.Stride;
5404 if (flip_x) src_y_offset = -bytesperpixel;
5405 else src_y_offset = bytesperpixel;
5407 else
5409 if (flip_x) src_x_offset = -bytesperpixel;
5410 else src_x_offset = bytesperpixel;
5411 if (flip_y) src_y_offset = -src_lock.Stride;
5412 else src_y_offset = src_lock.Stride;
5415 src_row = src_origin;
5416 dst_row = dst_lock.Scan0;
5417 for (y=0; y<height; y++)
5419 src_pixel = src_row;
5420 dst_pixel = dst_row;
5421 for (x=0; x<width; x++)
5423 /* FIXME: This could probably be faster without memcpy. */
5424 memcpy(dst_pixel, src_pixel, bytesperpixel);
5425 dst_pixel += bytesperpixel;
5426 src_pixel += src_x_offset;
5428 src_row += src_y_offset;
5429 dst_row += dst_lock.Stride;
5432 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5435 GdipBitmapUnlockBits(bitmap, &src_lock);
5438 if (stat == Ok)
5439 move_bitmap(bitmap, new_bitmap, FALSE);
5440 else
5441 GdipDisposeImage(&new_bitmap->image);
5443 image_unlock(image, unlock);
5444 return stat;
5447 /*****************************************************************************
5448 * GdipImageSetAbort [GDIPLUS.@]
5450 GpStatus WINGDIPAPI GdipImageSetAbort(GpImage *image, GdiplusAbort *pabort)
5452 TRACE("(%p, %p)\n", image, pabort);
5454 if (!image)
5455 return InvalidParameter;
5457 if (pabort)
5458 FIXME("Abort callback is not supported.\n");
5460 return Ok;
5463 /*****************************************************************************
5464 * GdipBitmapConvertFormat [GDIPLUS.@]
5466 GpStatus WINGDIPAPI GdipBitmapConvertFormat(GpBitmap *bitmap, PixelFormat format, DitherType dithertype,
5467 PaletteType palettetype, ColorPalette *palette, REAL alphathreshold)
5469 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap, format, dithertype, palettetype, palette, alphathreshold);
5470 return NotImplemented;
5473 static void set_histogram_point_argb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5475 ch0[ color >> 24 ]++;
5476 ch1[(color >> 16) & 0xff]++;
5477 ch2[(color >> 8) & 0xff]++;
5478 ch3[ color & 0xff]++;
5481 static void set_histogram_point_pargb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5483 BYTE alpha = color >> 24;
5485 ch0[alpha]++;
5486 ch1[(((color >> 16) & 0xff) * alpha) / 0xff]++;
5487 ch2[(((color >> 8) & 0xff) * alpha) / 0xff]++;
5488 ch3[(( color & 0xff) * alpha) / 0xff]++;
5491 static void set_histogram_point_rgb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5493 ch0[(color >> 16) & 0xff]++;
5494 ch1[(color >> 8) & 0xff]++;
5495 ch2[ color & 0xff]++;
5498 static void set_histogram_point_gray(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5500 ch0[(76 * ((color >> 16) & 0xff) + 150 * ((color >> 8) & 0xff) + 29 * (color & 0xff)) / 0xff]++;
5503 static void set_histogram_point_b(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5505 ch0[color & 0xff]++;
5508 static void set_histogram_point_g(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5510 ch0[(color >> 8) & 0xff]++;
5513 static void set_histogram_point_r(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5515 ch0[(color >> 16) & 0xff]++;
5518 static void set_histogram_point_a(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5520 ch0[(color >> 24) & 0xff]++;
5523 /*****************************************************************************
5524 * GdipBitmapGetHistogram [GDIPLUS.@]
5526 GpStatus WINGDIPAPI GdipBitmapGetHistogram(GpBitmap *bitmap, HistogramFormat format, UINT num_of_entries,
5527 UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5529 static void (* const set_histogram_point[])(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3) =
5531 set_histogram_point_argb,
5532 set_histogram_point_pargb,
5533 set_histogram_point_rgb,
5534 set_histogram_point_gray,
5535 set_histogram_point_b,
5536 set_histogram_point_g,
5537 set_histogram_point_r,
5538 set_histogram_point_a,
5540 UINT width, height, x, y;
5542 TRACE("(%p, %d, %u, %p, %p, %p, %p)\n", bitmap, format, num_of_entries,
5543 ch0, ch1, ch2, ch3);
5545 if (!bitmap || num_of_entries != 256)
5546 return InvalidParameter;
5548 /* Make sure passed channel pointers match requested format */
5549 switch (format)
5551 case HistogramFormatARGB:
5552 case HistogramFormatPARGB:
5553 if (!ch0 || !ch1 || !ch2 || !ch3)
5554 return InvalidParameter;
5555 memset(ch0, 0, num_of_entries * sizeof(UINT));
5556 memset(ch1, 0, num_of_entries * sizeof(UINT));
5557 memset(ch2, 0, num_of_entries * sizeof(UINT));
5558 memset(ch3, 0, num_of_entries * sizeof(UINT));
5559 break;
5560 case HistogramFormatRGB:
5561 if (!ch0 || !ch1 || !ch2 || ch3)
5562 return InvalidParameter;
5563 memset(ch0, 0, num_of_entries * sizeof(UINT));
5564 memset(ch1, 0, num_of_entries * sizeof(UINT));
5565 memset(ch2, 0, num_of_entries * sizeof(UINT));
5566 break;
5567 case HistogramFormatGray:
5568 case HistogramFormatB:
5569 case HistogramFormatG:
5570 case HistogramFormatR:
5571 case HistogramFormatA:
5572 if (!ch0 || ch1 || ch2 || ch3)
5573 return InvalidParameter;
5574 memset(ch0, 0, num_of_entries * sizeof(UINT));
5575 break;
5576 default:
5577 WARN("Invalid histogram format requested, %d\n", format);
5578 return InvalidParameter;
5581 GdipGetImageWidth(&bitmap->image, &width);
5582 GdipGetImageHeight(&bitmap->image, &height);
5584 for (y = 0; y < height; y++)
5585 for (x = 0; x < width; x++)
5587 ARGB color;
5589 GdipBitmapGetPixel(bitmap, x, y, &color);
5590 set_histogram_point[format](color, ch0, ch1, ch2, ch3);
5593 return Ok;
5596 /*****************************************************************************
5597 * GdipBitmapGetHistogramSize [GDIPLUS.@]
5599 GpStatus WINGDIPAPI GdipBitmapGetHistogramSize(HistogramFormat format, UINT *num_of_entries)
5601 TRACE("(%d, %p)\n", format, num_of_entries);
5603 if (!num_of_entries)
5604 return InvalidParameter;
5606 *num_of_entries = 256;
5607 return Ok;