comctl32/tests: Avoid a structure initialization warning.
[wine.git] / dlls / gdiplus / image.c
blob2e844cfcc581b84025dc2f7a7c948872460a3719
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_WICPixelFormat4bppIndexed, PixelFormat4bppIndexed, WICBitmapPaletteTypeFixedHalftone8 },
59 { &GUID_WICPixelFormat8bppGray, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedGray256 },
60 { &GUID_WICPixelFormat8bppIndexed, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedHalftone256 },
61 { &GUID_WICPixelFormat16bppBGR555, PixelFormat16bppRGB555, WICBitmapPaletteTypeFixedHalftone256 },
62 { &GUID_WICPixelFormat24bppBGR, PixelFormat24bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
63 { &GUID_WICPixelFormat32bppBGR, PixelFormat32bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
64 { &GUID_WICPixelFormat32bppBGRA, PixelFormat32bppARGB, WICBitmapPaletteTypeFixedHalftone256 },
65 { &GUID_WICPixelFormat32bppPBGRA, PixelFormat32bppPARGB, WICBitmapPaletteTypeFixedHalftone256 },
66 { NULL }
69 static ColorPalette *get_palette(IWICBitmapFrameDecode *frame, WICBitmapPaletteType palette_type)
71 HRESULT hr;
72 IWICImagingFactory *factory;
73 IWICPalette *wic_palette;
74 ColorPalette *palette = NULL;
76 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
77 if (hr != S_OK) return NULL;
79 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
80 if (hr == S_OK)
82 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
83 if (frame)
84 hr = IWICBitmapFrameDecode_CopyPalette(frame, wic_palette);
85 if (hr != S_OK)
87 TRACE("using predefined palette %#x\n", palette_type);
88 hr = IWICPalette_InitializePredefined(wic_palette, palette_type, FALSE);
90 if (hr == S_OK)
92 WICBitmapPaletteType type;
93 BOOL alpha;
94 UINT count;
96 IWICPalette_GetColorCount(wic_palette, &count);
97 palette = heap_alloc(2 * sizeof(UINT) + count * sizeof(ARGB));
98 IWICPalette_GetColors(wic_palette, count, palette->Entries, &palette->Count);
100 IWICPalette_GetType(wic_palette, &type);
101 switch(type) {
102 case WICBitmapPaletteTypeFixedGray4:
103 case WICBitmapPaletteTypeFixedGray16:
104 case WICBitmapPaletteTypeFixedGray256:
105 palette->Flags = PaletteFlagsGrayScale;
106 break;
107 case WICBitmapPaletteTypeFixedHalftone8:
108 case WICBitmapPaletteTypeFixedHalftone27:
109 case WICBitmapPaletteTypeFixedHalftone64:
110 case WICBitmapPaletteTypeFixedHalftone125:
111 case WICBitmapPaletteTypeFixedHalftone216:
112 case WICBitmapPaletteTypeFixedHalftone252:
113 case WICBitmapPaletteTypeFixedHalftone256:
114 palette->Flags = PaletteFlagsHalftone;
115 break;
116 default:
117 palette->Flags = 0;
119 IWICPalette_HasAlpha(wic_palette, &alpha);
120 if(alpha)
121 palette->Flags |= PaletteFlagsHasAlpha;
123 IWICPalette_Release(wic_palette);
125 IWICImagingFactory_Release(factory);
126 return palette;
129 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
130 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
132 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
134 * Note: According to Jose Roca's GDI+ docs, this function is not
135 * implemented in Windows's GDI+.
137 return NotImplemented;
140 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
141 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
142 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
144 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
146 * Note: According to Jose Roca's GDI+ docs, this function is not
147 * implemented in Windows's GDI+.
149 return NotImplemented;
152 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
154 *index = (row[x/8]>>(7-x%8)) & 1;
157 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
159 if (x & 1)
160 *index = row[x/2]&0xf;
161 else
162 *index = row[x/2]>>4;
165 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
167 *index = row[x];
170 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
171 const BYTE *row, UINT x)
173 *r = *g = *b = row[x*2+1];
174 *a = 255;
177 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
178 const BYTE *row, UINT x)
180 WORD pixel = *((const WORD*)(row)+x);
181 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
182 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
183 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
184 *a = 255;
187 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
188 const BYTE *row, UINT x)
190 WORD pixel = *((const WORD*)(row)+x);
191 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
192 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
193 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
194 *a = 255;
197 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
198 const BYTE *row, UINT x)
200 WORD pixel = *((const WORD*)(row)+x);
201 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
202 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
203 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
204 if ((pixel&0x8000) == 0x8000)
205 *a = 255;
206 else
207 *a = 0;
210 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
211 const BYTE *row, UINT x)
213 *r = row[x*3+2];
214 *g = row[x*3+1];
215 *b = row[x*3];
216 *a = 255;
219 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
220 const BYTE *row, UINT x)
222 *r = row[x*4+2];
223 *g = row[x*4+1];
224 *b = row[x*4];
225 *a = 255;
228 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
229 const BYTE *row, UINT x)
231 *r = row[x*4+2];
232 *g = row[x*4+1];
233 *b = row[x*4];
234 *a = row[x*4+3];
237 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
238 const BYTE *row, UINT x)
240 *a = row[x*4+3];
241 if (*a == 0)
242 *r = *g = *b = 0;
243 else
245 *r = row[x*4+2] * 255 / *a;
246 *g = row[x*4+1] * 255 / *a;
247 *b = row[x*4] * 255 / *a;
251 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
252 const BYTE *row, UINT x)
254 *r = row[x*6+5];
255 *g = row[x*6+3];
256 *b = row[x*6+1];
257 *a = 255;
260 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
261 const BYTE *row, UINT x)
263 *r = row[x*8+5];
264 *g = row[x*8+3];
265 *b = row[x*8+1];
266 *a = row[x*8+7];
269 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
270 const BYTE *row, UINT x)
272 *a = row[x*8+7];
273 if (*a == 0)
274 *r = *g = *b = 0;
275 else
277 *r = row[x*8+5] * 255 / *a;
278 *g = row[x*8+3] * 255 / *a;
279 *b = row[x*8+1] * 255 / *a;
283 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
284 ARGB *color)
286 BYTE r, g, b, a;
287 BYTE index;
288 BYTE *row;
290 if(!bitmap || !color ||
291 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
292 return InvalidParameter;
294 row = bitmap->bits+bitmap->stride*y;
296 switch (bitmap->format)
298 case PixelFormat1bppIndexed:
299 getpixel_1bppIndexed(&index,row,x);
300 break;
301 case PixelFormat4bppIndexed:
302 getpixel_4bppIndexed(&index,row,x);
303 break;
304 case PixelFormat8bppIndexed:
305 getpixel_8bppIndexed(&index,row,x);
306 break;
307 case PixelFormat16bppGrayScale:
308 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
309 break;
310 case PixelFormat16bppRGB555:
311 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
312 break;
313 case PixelFormat16bppRGB565:
314 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
315 break;
316 case PixelFormat16bppARGB1555:
317 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
318 break;
319 case PixelFormat24bppRGB:
320 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
321 break;
322 case PixelFormat32bppRGB:
323 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
324 break;
325 case PixelFormat32bppARGB:
326 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
327 break;
328 case PixelFormat32bppPARGB:
329 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
330 break;
331 case PixelFormat48bppRGB:
332 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
333 break;
334 case PixelFormat64bppARGB:
335 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
336 break;
337 case PixelFormat64bppPARGB:
338 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
339 break;
340 default:
341 FIXME("not implemented for format 0x%x\n", bitmap->format);
342 return NotImplemented;
345 if (bitmap->format & PixelFormatIndexed)
346 *color = bitmap->image.palette->Entries[index];
347 else
348 *color = a<<24|r<<16|g<<8|b;
350 return Ok;
353 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
355 BYTE index = 0;
356 int best_distance = 0x7fff;
357 int distance;
358 UINT i;
360 if (!palette) return 0;
361 /* This algorithm scans entire palette,
362 computes difference from desired color (all color components have equal weight)
363 and returns the index of color with least difference.
365 Note: Maybe it could be replaced with a better algorithm for better image quality
366 and performance, though better algorithm would probably need some pre-built lookup
367 tables and thus may actually be slower if this method is called only few times per
368 every image.
370 for(i=0;i<palette->Count;i++) {
371 ARGB color=palette->Entries[i];
372 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
373 if (distance<best_distance) {
374 best_distance=distance;
375 index=i;
378 return index;
381 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
382 BYTE *row, UINT x, ColorPalette *palette)
384 BYTE index = get_palette_index(r,g,b,a,palette);
385 row[x]=index;
388 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
389 BYTE *row, UINT x, ColorPalette *palette)
391 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
394 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
395 BYTE *row, UINT x, ColorPalette *palette)
397 if (x & 1)
398 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
399 else
400 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
403 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
404 BYTE *row, UINT x)
406 *((WORD*)(row)+x) = (r+g+b)*85;
409 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
410 BYTE *row, UINT x)
412 *((WORD*)(row)+x) = (r<<7&0x7c00)|
413 (g<<2&0x03e0)|
414 (b>>3&0x001f);
417 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
418 BYTE *row, UINT x)
420 *((WORD*)(row)+x) = (r<<8&0xf800)|
421 (g<<3&0x07e0)|
422 (b>>3&0x001f);
425 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
426 BYTE *row, UINT x)
428 *((WORD*)(row)+x) = (a<<8&0x8000)|
429 (r<<7&0x7c00)|
430 (g<<2&0x03e0)|
431 (b>>3&0x001f);
434 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
435 BYTE *row, UINT x)
437 row[x*3+2] = r;
438 row[x*3+1] = g;
439 row[x*3] = b;
442 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
443 BYTE *row, UINT x)
445 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
448 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
449 BYTE *row, UINT x)
451 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
454 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
455 BYTE *row, UINT x)
457 r = r * a / 255;
458 g = g * a / 255;
459 b = b * a / 255;
460 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
463 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
464 BYTE *row, UINT x)
466 row[x*6+5] = row[x*6+4] = r;
467 row[x*6+3] = row[x*6+2] = g;
468 row[x*6+1] = row[x*6] = b;
471 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
472 BYTE *row, UINT x)
474 UINT64 a64=a, r64=r, g64=g, b64=b;
475 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
478 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
479 BYTE *row, UINT x)
481 UINT64 a64, r64, g64, b64;
482 a64 = a * 257;
483 r64 = r * a / 255;
484 g64 = g * a / 255;
485 b64 = b * a / 255;
486 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
489 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
490 ARGB color)
492 BYTE a, r, g, b;
493 BYTE *row;
495 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
496 return InvalidParameter;
498 a = color>>24;
499 r = color>>16;
500 g = color>>8;
501 b = color;
503 row = bitmap->bits + bitmap->stride * y;
505 switch (bitmap->format)
507 case PixelFormat16bppGrayScale:
508 setpixel_16bppGrayScale(r,g,b,a,row,x);
509 break;
510 case PixelFormat16bppRGB555:
511 setpixel_16bppRGB555(r,g,b,a,row,x);
512 break;
513 case PixelFormat16bppRGB565:
514 setpixel_16bppRGB565(r,g,b,a,row,x);
515 break;
516 case PixelFormat16bppARGB1555:
517 setpixel_16bppARGB1555(r,g,b,a,row,x);
518 break;
519 case PixelFormat24bppRGB:
520 setpixel_24bppRGB(r,g,b,a,row,x);
521 break;
522 case PixelFormat32bppRGB:
523 setpixel_32bppRGB(r,g,b,a,row,x);
524 break;
525 case PixelFormat32bppARGB:
526 setpixel_32bppARGB(r,g,b,a,row,x);
527 break;
528 case PixelFormat32bppPARGB:
529 setpixel_32bppPARGB(r,g,b,a,row,x);
530 break;
531 case PixelFormat48bppRGB:
532 setpixel_48bppRGB(r,g,b,a,row,x);
533 break;
534 case PixelFormat64bppARGB:
535 setpixel_64bppARGB(r,g,b,a,row,x);
536 break;
537 case PixelFormat64bppPARGB:
538 setpixel_64bppPARGB(r,g,b,a,row,x);
539 break;
540 case PixelFormat8bppIndexed:
541 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
542 break;
543 case PixelFormat4bppIndexed:
544 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
545 break;
546 case PixelFormat1bppIndexed:
547 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
548 break;
549 default:
550 FIXME("not implemented for format 0x%x\n", bitmap->format);
551 return NotImplemented;
554 return Ok;
557 GpStatus convert_pixels(INT width, INT height,
558 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
559 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
560 ColorPalette *palette)
562 INT x, y;
564 if (src_format == dst_format ||
565 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
567 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
568 for (y=0; y<height; y++)
569 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
570 return Ok;
573 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
574 for (y=0; y<height; y++) \
575 for (x=0; x<width; x++) { \
576 BYTE index; \
577 ARGB argb; \
578 BYTE *color = (BYTE *)&argb; \
579 getpixel_function(&index, src_bits+src_stride*y, x); \
580 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
581 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
583 return Ok; \
584 } while (0);
586 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
587 for (y=0; y<height; y++) \
588 for (x=0; x<width; x++) { \
589 BYTE r, g, b, a; \
590 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
591 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
593 return Ok; \
594 } while (0);
596 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
597 for (y=0; y<height; y++) \
598 for (x=0; x<width; x++) { \
599 BYTE r, g, b, a; \
600 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
601 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
603 return Ok; \
604 } while (0);
606 switch (src_format)
608 case PixelFormat1bppIndexed:
609 switch (dst_format)
611 case PixelFormat16bppGrayScale:
612 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
613 case PixelFormat16bppRGB555:
614 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
615 case PixelFormat16bppRGB565:
616 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
617 case PixelFormat16bppARGB1555:
618 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
619 case PixelFormat24bppRGB:
620 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
621 case PixelFormat32bppRGB:
622 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
623 case PixelFormat32bppARGB:
624 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
625 case PixelFormat32bppPARGB:
626 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
627 case PixelFormat48bppRGB:
628 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
629 case PixelFormat64bppARGB:
630 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
631 default:
632 break;
634 break;
635 case PixelFormat4bppIndexed:
636 switch (dst_format)
638 case PixelFormat16bppGrayScale:
639 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
640 case PixelFormat16bppRGB555:
641 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
642 case PixelFormat16bppRGB565:
643 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
644 case PixelFormat16bppARGB1555:
645 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
646 case PixelFormat24bppRGB:
647 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
648 case PixelFormat32bppRGB:
649 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
650 case PixelFormat32bppARGB:
651 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
652 case PixelFormat32bppPARGB:
653 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
654 case PixelFormat48bppRGB:
655 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
656 case PixelFormat64bppARGB:
657 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
658 default:
659 break;
661 break;
662 case PixelFormat8bppIndexed:
663 switch (dst_format)
665 case PixelFormat16bppGrayScale:
666 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
667 case PixelFormat16bppRGB555:
668 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
669 case PixelFormat16bppRGB565:
670 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
671 case PixelFormat16bppARGB1555:
672 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
673 case PixelFormat24bppRGB:
674 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
675 case PixelFormat32bppRGB:
676 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
677 case PixelFormat32bppARGB:
678 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
679 case PixelFormat32bppPARGB:
680 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
681 case PixelFormat48bppRGB:
682 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
683 case PixelFormat64bppARGB:
684 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
685 default:
686 break;
688 break;
689 case PixelFormat16bppGrayScale:
690 switch (dst_format)
692 case PixelFormat1bppIndexed:
693 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
694 case PixelFormat8bppIndexed:
695 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
696 case PixelFormat16bppRGB555:
697 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
698 case PixelFormat16bppRGB565:
699 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
700 case PixelFormat16bppARGB1555:
701 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
702 case PixelFormat24bppRGB:
703 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
704 case PixelFormat32bppRGB:
705 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
706 case PixelFormat32bppARGB:
707 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
708 case PixelFormat32bppPARGB:
709 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
710 case PixelFormat48bppRGB:
711 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
712 case PixelFormat64bppARGB:
713 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
714 default:
715 break;
717 break;
718 case PixelFormat16bppRGB555:
719 switch (dst_format)
721 case PixelFormat1bppIndexed:
722 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
723 case PixelFormat8bppIndexed:
724 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
725 case PixelFormat16bppGrayScale:
726 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
727 case PixelFormat16bppRGB565:
728 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
729 case PixelFormat16bppARGB1555:
730 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
731 case PixelFormat24bppRGB:
732 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
733 case PixelFormat32bppRGB:
734 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
735 case PixelFormat32bppARGB:
736 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
737 case PixelFormat32bppPARGB:
738 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
739 case PixelFormat48bppRGB:
740 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
741 case PixelFormat64bppARGB:
742 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
743 default:
744 break;
746 break;
747 case PixelFormat16bppRGB565:
748 switch (dst_format)
750 case PixelFormat1bppIndexed:
751 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
752 case PixelFormat8bppIndexed:
753 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
754 case PixelFormat16bppGrayScale:
755 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
756 case PixelFormat16bppRGB555:
757 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
758 case PixelFormat16bppARGB1555:
759 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
760 case PixelFormat24bppRGB:
761 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
762 case PixelFormat32bppRGB:
763 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
764 case PixelFormat32bppARGB:
765 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
766 case PixelFormat32bppPARGB:
767 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
768 case PixelFormat48bppRGB:
769 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
770 case PixelFormat64bppARGB:
771 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
772 default:
773 break;
775 break;
776 case PixelFormat16bppARGB1555:
777 switch (dst_format)
779 case PixelFormat1bppIndexed:
780 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
781 case PixelFormat8bppIndexed:
782 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
783 case PixelFormat16bppGrayScale:
784 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
785 case PixelFormat16bppRGB555:
786 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
787 case PixelFormat16bppRGB565:
788 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
789 case PixelFormat24bppRGB:
790 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
791 case PixelFormat32bppRGB:
792 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
793 case PixelFormat32bppARGB:
794 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
795 case PixelFormat32bppPARGB:
796 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
797 case PixelFormat48bppRGB:
798 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
799 case PixelFormat64bppARGB:
800 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
801 default:
802 break;
804 break;
805 case PixelFormat24bppRGB:
806 switch (dst_format)
808 case PixelFormat1bppIndexed:
809 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
810 case PixelFormat8bppIndexed:
811 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
812 case PixelFormat16bppGrayScale:
813 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
814 case PixelFormat16bppRGB555:
815 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
816 case PixelFormat16bppRGB565:
817 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
818 case PixelFormat16bppARGB1555:
819 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
820 case PixelFormat32bppRGB:
821 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
822 case PixelFormat32bppARGB:
823 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
824 case PixelFormat32bppPARGB:
825 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
826 case PixelFormat48bppRGB:
827 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
828 case PixelFormat64bppARGB:
829 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
830 default:
831 break;
833 break;
834 case PixelFormat32bppRGB:
835 switch (dst_format)
837 case PixelFormat1bppIndexed:
838 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
839 case PixelFormat8bppIndexed:
840 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
841 case PixelFormat16bppGrayScale:
842 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
843 case PixelFormat16bppRGB555:
844 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
845 case PixelFormat16bppRGB565:
846 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
847 case PixelFormat16bppARGB1555:
848 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
849 case PixelFormat24bppRGB:
850 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
851 case PixelFormat32bppARGB:
852 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
853 case PixelFormat32bppPARGB:
854 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
855 case PixelFormat48bppRGB:
856 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
857 case PixelFormat64bppARGB:
858 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
859 default:
860 break;
862 break;
863 case PixelFormat32bppARGB:
864 switch (dst_format)
866 case PixelFormat1bppIndexed:
867 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
868 case PixelFormat8bppIndexed:
869 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
870 case PixelFormat16bppGrayScale:
871 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
872 case PixelFormat16bppRGB555:
873 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
874 case PixelFormat16bppRGB565:
875 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
876 case PixelFormat16bppARGB1555:
877 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
878 case PixelFormat24bppRGB:
879 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
880 case PixelFormat32bppPARGB:
881 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
882 return Ok;
883 case PixelFormat48bppRGB:
884 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
885 case PixelFormat64bppARGB:
886 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
887 default:
888 break;
890 break;
891 case PixelFormat32bppPARGB:
892 switch (dst_format)
894 case PixelFormat1bppIndexed:
895 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
896 case PixelFormat8bppIndexed:
897 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
898 case PixelFormat16bppGrayScale:
899 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
900 case PixelFormat16bppRGB555:
901 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
902 case PixelFormat16bppRGB565:
903 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
904 case PixelFormat16bppARGB1555:
905 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
906 case PixelFormat24bppRGB:
907 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
908 case PixelFormat32bppRGB:
909 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
910 case PixelFormat32bppARGB:
911 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
912 case PixelFormat48bppRGB:
913 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
914 case PixelFormat64bppARGB:
915 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
916 default:
917 break;
919 break;
920 case PixelFormat48bppRGB:
921 switch (dst_format)
923 case PixelFormat1bppIndexed:
924 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
925 case PixelFormat8bppIndexed:
926 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
927 case PixelFormat16bppGrayScale:
928 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
929 case PixelFormat16bppRGB555:
930 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
931 case PixelFormat16bppRGB565:
932 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
933 case PixelFormat16bppARGB1555:
934 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
935 case PixelFormat24bppRGB:
936 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
937 case PixelFormat32bppRGB:
938 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
939 case PixelFormat32bppARGB:
940 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
941 case PixelFormat32bppPARGB:
942 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
943 case PixelFormat64bppARGB:
944 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
945 default:
946 break;
948 break;
949 case PixelFormat64bppARGB:
950 switch (dst_format)
952 case PixelFormat1bppIndexed:
953 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
954 case PixelFormat8bppIndexed:
955 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
956 case PixelFormat16bppGrayScale:
957 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
958 case PixelFormat16bppRGB555:
959 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
960 case PixelFormat16bppRGB565:
961 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
962 case PixelFormat16bppARGB1555:
963 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
964 case PixelFormat24bppRGB:
965 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
966 case PixelFormat32bppRGB:
967 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
968 case PixelFormat32bppARGB:
969 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
970 case PixelFormat32bppPARGB:
971 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
972 case PixelFormat48bppRGB:
973 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
974 default:
975 break;
977 break;
978 case PixelFormat64bppPARGB:
979 switch (dst_format)
981 case PixelFormat1bppIndexed:
982 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
983 case PixelFormat8bppIndexed:
984 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
985 case PixelFormat16bppGrayScale:
986 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
987 case PixelFormat16bppRGB555:
988 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
989 case PixelFormat16bppRGB565:
990 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
991 case PixelFormat16bppARGB1555:
992 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
993 case PixelFormat24bppRGB:
994 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
995 case PixelFormat32bppRGB:
996 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
997 case PixelFormat32bppARGB:
998 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
999 case PixelFormat32bppPARGB:
1000 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1001 case PixelFormat48bppRGB:
1002 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1003 case PixelFormat64bppARGB:
1004 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1005 default:
1006 break;
1008 break;
1009 default:
1010 break;
1013 #undef convert_indexed_to_rgb
1014 #undef convert_rgb_to_rgb
1016 return NotImplemented;
1019 /* This function returns a pointer to an array of pixels that represents the
1020 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1021 * flags. It is correct behavior that a user who calls this function with write
1022 * privileges can write to the whole bitmap (not just the area in rect).
1024 * FIXME: only used portion of format is bits per pixel. */
1025 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1026 UINT flags, PixelFormat format, BitmapData* lockeddata)
1028 INT bitspp = PIXELFORMATBPP(format);
1029 GpRect act_rect; /* actual rect to be used */
1030 GpStatus stat;
1031 BOOL unlock;
1033 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1035 if(!lockeddata || !bitmap)
1036 return InvalidParameter;
1037 if(!image_lock(&bitmap->image, &unlock))
1038 return ObjectBusy;
1040 if(rect){
1041 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1042 (rect->Y + rect->Height > bitmap->height) || !flags)
1044 image_unlock(&bitmap->image, unlock);
1045 return InvalidParameter;
1048 act_rect = *rect;
1050 else{
1051 act_rect.X = act_rect.Y = 0;
1052 act_rect.Width = bitmap->width;
1053 act_rect.Height = bitmap->height;
1056 if(bitmap->lockmode)
1058 WARN("bitmap is already locked and cannot be locked again\n");
1059 image_unlock(&bitmap->image, unlock);
1060 return WrongState;
1063 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1065 /* no conversion is necessary; just use the bits directly */
1066 lockeddata->Width = act_rect.Width;
1067 lockeddata->Height = act_rect.Height;
1068 lockeddata->PixelFormat = format;
1069 lockeddata->Reserved = flags;
1070 lockeddata->Stride = bitmap->stride;
1071 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1072 bitmap->stride * act_rect.Y;
1074 bitmap->lockmode = flags | ImageLockModeRead;
1076 image_unlock(&bitmap->image, unlock);
1077 return Ok;
1080 /* Make sure we can convert to the requested format. */
1081 if (flags & ImageLockModeRead)
1083 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
1084 if (stat == NotImplemented)
1086 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1087 image_unlock(&bitmap->image, unlock);
1088 return NotImplemented;
1092 /* If we're opening for writing, make sure we'll be able to write back in
1093 * the original format. */
1094 if (flags & ImageLockModeWrite)
1096 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
1097 if (stat == NotImplemented)
1099 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1100 image_unlock(&bitmap->image, unlock);
1101 return NotImplemented;
1105 lockeddata->Width = act_rect.Width;
1106 lockeddata->Height = act_rect.Height;
1107 lockeddata->PixelFormat = format;
1108 lockeddata->Reserved = flags;
1110 if(!(flags & ImageLockModeUserInputBuf))
1112 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1114 bitmap->bitmapbits = heap_alloc_zero(lockeddata->Stride * act_rect.Height);
1116 if (!bitmap->bitmapbits)
1118 image_unlock(&bitmap->image, unlock);
1119 return OutOfMemory;
1122 lockeddata->Scan0 = bitmap->bitmapbits;
1125 if (flags & ImageLockModeRead)
1127 static BOOL fixme = FALSE;
1129 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1131 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1132 fixme = TRUE;
1135 stat = convert_pixels(act_rect.Width, act_rect.Height,
1136 lockeddata->Stride, lockeddata->Scan0, format,
1137 bitmap->stride,
1138 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1139 bitmap->format, bitmap->image.palette);
1141 if (stat != Ok)
1143 heap_free(bitmap->bitmapbits);
1144 bitmap->bitmapbits = NULL;
1145 image_unlock(&bitmap->image, unlock);
1146 return stat;
1150 bitmap->lockmode = flags | ImageLockModeRead;
1151 bitmap->lockx = act_rect.X;
1152 bitmap->locky = act_rect.Y;
1154 image_unlock(&bitmap->image, unlock);
1155 return Ok;
1158 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1160 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1162 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1163 return InvalidParameter;
1165 bitmap->image.xres = xdpi;
1166 bitmap->image.yres = ydpi;
1168 return Ok;
1171 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1172 BitmapData* lockeddata)
1174 GpStatus stat;
1175 static BOOL fixme = FALSE;
1176 BOOL unlock;
1178 TRACE("(%p,%p)\n", bitmap, lockeddata);
1180 if(!bitmap || !lockeddata)
1181 return InvalidParameter;
1182 if(!image_lock(&bitmap->image, &unlock))
1183 return ObjectBusy;
1185 if(!bitmap->lockmode)
1187 image_unlock(&bitmap->image, unlock);
1188 return WrongState;
1191 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1192 bitmap->lockmode = 0;
1193 heap_free(bitmap->bitmapbits);
1194 bitmap->bitmapbits = NULL;
1195 image_unlock(&bitmap->image, unlock);
1196 return Ok;
1199 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1201 /* we passed a direct reference; no need to do anything */
1202 bitmap->lockmode = 0;
1203 image_unlock(&bitmap->image, unlock);
1204 return Ok;
1207 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1209 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1210 fixme = TRUE;
1213 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1214 bitmap->stride,
1215 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1216 bitmap->format,
1217 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1219 if (stat != Ok)
1221 ERR("failed to convert pixels; this should never happen\n");
1224 heap_free(bitmap->bitmapbits);
1225 bitmap->bitmapbits = NULL;
1226 bitmap->lockmode = 0;
1228 image_unlock(&bitmap->image, unlock);
1229 return stat;
1232 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1233 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1235 Rect area;
1236 GpStatus stat;
1238 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1240 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1241 x < 0 || y < 0 ||
1242 x + width > srcBitmap->width || y + height > srcBitmap->height)
1244 TRACE("<-- InvalidParameter\n");
1245 return InvalidParameter;
1248 if (format == PixelFormatDontCare)
1249 format = srcBitmap->format;
1251 area.X = gdip_round(x);
1252 area.Y = gdip_round(y);
1253 area.Width = gdip_round(width);
1254 area.Height = gdip_round(height);
1256 stat = GdipCreateBitmapFromScan0(area.Width, area.Height, 0, format, NULL, dstBitmap);
1257 if (stat == Ok)
1259 stat = convert_pixels(area.Width, area.Height, (*dstBitmap)->stride, (*dstBitmap)->bits, (*dstBitmap)->format,
1260 srcBitmap->stride,
1261 srcBitmap->bits + srcBitmap->stride * area.Y + PIXELFORMATBPP(srcBitmap->format) * area.X / 8,
1262 srcBitmap->format, srcBitmap->image.palette);
1264 if (stat == Ok && srcBitmap->image.palette)
1266 ColorPalette *src_palette, *dst_palette;
1268 src_palette = srcBitmap->image.palette;
1270 dst_palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * src_palette->Count);
1272 if (dst_palette)
1274 dst_palette->Flags = src_palette->Flags;
1275 dst_palette->Count = src_palette->Count;
1276 memcpy(dst_palette->Entries, src_palette->Entries, sizeof(ARGB) * src_palette->Count);
1278 heap_free((*dstBitmap)->image.palette);
1279 (*dstBitmap)->image.palette = dst_palette;
1281 else
1282 stat = OutOfMemory;
1285 if (stat != Ok)
1286 GdipDisposeImage(&(*dstBitmap)->image);
1289 if (stat != Ok)
1290 *dstBitmap = NULL;
1292 return stat;
1295 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1296 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1298 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1300 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1303 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1305 TRACE("%p, %p\n", image, cloneImage);
1307 if (!image || !cloneImage)
1308 return InvalidParameter;
1310 if (image->type == ImageTypeBitmap)
1312 GpBitmap *bitmap = (GpBitmap *)image;
1314 return GdipCloneBitmapAreaI(0, 0, bitmap->width, bitmap->height,
1315 bitmap->format, bitmap, (GpBitmap **)cloneImage);
1317 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
1319 GpMetafile *result, *metafile;
1321 metafile = (GpMetafile*)image;
1323 result = heap_alloc_zero(sizeof(*result));
1324 if (!result)
1325 return OutOfMemory;
1327 result->image.type = ImageTypeMetafile;
1328 result->image.format = image->format;
1329 result->image.flags = image->flags;
1330 result->image.frame_count = 1;
1331 result->image.xres = image->xres;
1332 result->image.yres = image->yres;
1333 result->bounds = metafile->bounds;
1334 result->unit = metafile->unit;
1335 result->metafile_type = metafile->metafile_type;
1336 result->hemf = CopyEnhMetaFileW(metafile->hemf, NULL);
1337 list_init(&result->containers);
1339 if (!result->hemf)
1341 heap_free(result);
1342 return OutOfMemory;
1345 *cloneImage = &result->image;
1346 return Ok;
1348 else
1350 WARN("GpImage with no image data (metafile in wrong state?)\n");
1351 return InvalidParameter;
1355 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1356 GpBitmap **bitmap)
1358 GpStatus stat;
1359 IStream *stream;
1361 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1363 if(!filename || !bitmap)
1364 return InvalidParameter;
1366 *bitmap = NULL;
1368 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1370 if(stat != Ok)
1371 return stat;
1373 stat = GdipCreateBitmapFromStream(stream, bitmap);
1375 IStream_Release(stream);
1377 return stat;
1380 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1381 VOID *bits, GpBitmap **bitmap)
1383 DWORD height, stride;
1384 PixelFormat format;
1386 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1388 if (!info || !bits || !bitmap)
1389 return InvalidParameter;
1391 height = abs(info->bmiHeader.biHeight);
1392 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1394 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1396 bits = (BYTE*)bits + (height - 1) * stride;
1397 stride = -stride;
1400 switch(info->bmiHeader.biBitCount) {
1401 case 1:
1402 format = PixelFormat1bppIndexed;
1403 break;
1404 case 4:
1405 format = PixelFormat4bppIndexed;
1406 break;
1407 case 8:
1408 format = PixelFormat8bppIndexed;
1409 break;
1410 case 16:
1411 format = PixelFormat16bppRGB555;
1412 break;
1413 case 24:
1414 format = PixelFormat24bppRGB;
1415 break;
1416 case 32:
1417 format = PixelFormat32bppRGB;
1418 break;
1419 default:
1420 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1421 *bitmap = NULL;
1422 return InvalidParameter;
1425 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1426 bits, bitmap);
1430 /* FIXME: no icm */
1431 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1432 GpBitmap **bitmap)
1434 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1436 return GdipCreateBitmapFromFile(filename, bitmap);
1439 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1440 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1442 HBITMAP hbm;
1443 GpStatus stat = InvalidParameter;
1445 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1447 if(!lpBitmapName || !bitmap)
1448 return InvalidParameter;
1450 /* load DIB */
1451 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1452 LR_CREATEDIBSECTION);
1454 if(hbm){
1455 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1456 DeleteObject(hbm);
1459 return stat;
1462 static inline DWORD blend_argb_no_bkgnd_alpha(DWORD src, DWORD bkgnd)
1464 BYTE b = (BYTE)src;
1465 BYTE g = (BYTE)(src >> 8);
1466 BYTE r = (BYTE)(src >> 16);
1467 DWORD alpha = (BYTE)(src >> 24);
1468 return ((b + ((BYTE)bkgnd * (255 - alpha) + 127) / 255) |
1469 (g + ((BYTE)(bkgnd >> 8) * (255 - alpha) + 127) / 255) << 8 |
1470 (r + ((BYTE)(bkgnd >> 16) * (255 - alpha) + 127) / 255) << 16 |
1471 (alpha << 24));
1474 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1475 HBITMAP* hbmReturn, ARGB background)
1477 GpStatus stat;
1478 HBITMAP result;
1479 UINT width, height;
1480 BITMAPINFOHEADER bih;
1481 LPBYTE bits;
1482 BOOL unlock;
1484 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1486 if (!bitmap || !hbmReturn) return InvalidParameter;
1487 if (!image_lock(&bitmap->image, &unlock)) return ObjectBusy;
1489 GdipGetImageWidth(&bitmap->image, &width);
1490 GdipGetImageHeight(&bitmap->image, &height);
1492 bih.biSize = sizeof(bih);
1493 bih.biWidth = width;
1494 bih.biHeight = height;
1495 bih.biPlanes = 1;
1496 bih.biBitCount = 32;
1497 bih.biCompression = BI_RGB;
1498 bih.biSizeImage = 0;
1499 bih.biXPelsPerMeter = 0;
1500 bih.biYPelsPerMeter = 0;
1501 bih.biClrUsed = 0;
1502 bih.biClrImportant = 0;
1504 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1505 if (!result)
1507 image_unlock(&bitmap->image, unlock);
1508 return GenericError;
1511 stat = convert_pixels(width, height, -width*4,
1512 bits + (width * 4 * (height - 1)), PixelFormat32bppPARGB,
1513 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette);
1514 if (stat != Ok)
1516 DeleteObject(result);
1517 image_unlock(&bitmap->image, unlock);
1518 return stat;
1521 if (background & 0xffffff)
1523 DWORD *ptr;
1524 UINT i;
1525 for (ptr = (DWORD*)bits, i = 0; i < width * height; ptr++, i++)
1527 if ((*ptr & 0xff000000) == 0xff000000) continue;
1528 *ptr = blend_argb_no_bkgnd_alpha(*ptr, background);
1532 *hbmReturn = result;
1533 image_unlock(&bitmap->image, unlock);
1534 return Ok;
1537 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1538 GpGraphics* target, GpBitmap** bitmap)
1540 GpStatus ret;
1542 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1544 if(!target || !bitmap)
1545 return InvalidParameter;
1547 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
1548 NULL, bitmap);
1550 if (ret == Ok)
1552 GdipGetDpiX(target, &(*bitmap)->image.xres);
1553 GdipGetDpiY(target, &(*bitmap)->image.yres);
1556 return ret;
1559 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1561 GpStatus stat;
1562 ICONINFO iinfo;
1563 BITMAP bm;
1564 int ret;
1565 UINT width, height, stride;
1566 GpRect rect;
1567 BitmapData lockeddata;
1568 HDC screendc;
1569 BOOL has_alpha;
1570 int x, y;
1571 BITMAPINFOHEADER bih;
1572 DWORD *src;
1573 BYTE *dst_row;
1574 DWORD *dst;
1576 TRACE("%p, %p\n", hicon, bitmap);
1578 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1579 return InvalidParameter;
1581 /* get the size of the icon */
1582 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1583 if (ret == 0) {
1584 DeleteObject(iinfo.hbmColor);
1585 DeleteObject(iinfo.hbmMask);
1586 return GenericError;
1589 width = bm.bmWidth;
1590 height = iinfo.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
1591 stride = width * 4;
1593 stat = GdipCreateBitmapFromScan0(width, height, stride, PixelFormat32bppARGB, NULL, bitmap);
1594 if (stat != Ok) {
1595 DeleteObject(iinfo.hbmColor);
1596 DeleteObject(iinfo.hbmMask);
1597 return stat;
1600 rect.X = 0;
1601 rect.Y = 0;
1602 rect.Width = width;
1603 rect.Height = height;
1605 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1606 if (stat != Ok) {
1607 DeleteObject(iinfo.hbmColor);
1608 DeleteObject(iinfo.hbmMask);
1609 GdipDisposeImage(&(*bitmap)->image);
1610 return stat;
1613 bih.biSize = sizeof(bih);
1614 bih.biWidth = width;
1615 bih.biHeight = iinfo.hbmColor ? -height: -height * 2;
1616 bih.biPlanes = 1;
1617 bih.biBitCount = 32;
1618 bih.biCompression = BI_RGB;
1619 bih.biSizeImage = 0;
1620 bih.biXPelsPerMeter = 0;
1621 bih.biYPelsPerMeter = 0;
1622 bih.biClrUsed = 0;
1623 bih.biClrImportant = 0;
1625 screendc = CreateCompatibleDC(0);
1626 if (iinfo.hbmColor)
1628 GetDIBits(screendc, iinfo.hbmColor, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1630 if (bm.bmBitsPixel == 32)
1632 has_alpha = FALSE;
1634 /* If any pixel has a non-zero alpha, ignore hbmMask */
1635 src = (DWORD*)lockeddata.Scan0;
1636 for (x=0; x<width && !has_alpha; x++)
1637 for (y=0; y<height && !has_alpha; y++)
1638 if ((*src++ & 0xff000000) != 0)
1639 has_alpha = TRUE;
1641 else has_alpha = FALSE;
1643 else
1645 GetDIBits(screendc, iinfo.hbmMask, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1646 has_alpha = FALSE;
1649 if (!has_alpha)
1651 if (iinfo.hbmMask)
1653 BYTE *bits = heap_alloc(height * stride);
1655 /* read alpha data from the mask */
1656 if (iinfo.hbmColor)
1657 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1658 else
1659 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1661 src = (DWORD*)bits;
1662 dst_row = lockeddata.Scan0;
1663 for (y=0; y<height; y++)
1665 dst = (DWORD*)dst_row;
1666 for (x=0; x<height; x++)
1668 DWORD src_value = *src++;
1669 if (src_value)
1670 *dst++ = 0;
1671 else
1672 *dst++ |= 0xff000000;
1674 dst_row += lockeddata.Stride;
1677 heap_free(bits);
1679 else
1681 /* set constant alpha of 255 */
1682 dst_row = lockeddata.Scan0;
1683 for (y=0; y<height; y++)
1685 dst = (DWORD*)dst_row;
1686 for (x=0; x<height; x++)
1687 *dst++ |= 0xff000000;
1688 dst_row += lockeddata.Stride;
1693 DeleteDC(screendc);
1695 DeleteObject(iinfo.hbmColor);
1696 DeleteObject(iinfo.hbmMask);
1698 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1700 return Ok;
1703 static void generate_halftone_palette(ARGB *entries, UINT count)
1705 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1706 UINT i;
1708 for (i=0; i<8 && i<count; i++)
1710 entries[i] = 0xff000000;
1711 if (i&1) entries[i] |= 0x800000;
1712 if (i&2) entries[i] |= 0x8000;
1713 if (i&4) entries[i] |= 0x80;
1716 if (8 < count)
1717 entries[i] = 0xffc0c0c0;
1719 for (i=9; i<16 && i<count; i++)
1721 entries[i] = 0xff000000;
1722 if (i&1) entries[i] |= 0xff0000;
1723 if (i&2) entries[i] |= 0xff00;
1724 if (i&4) entries[i] |= 0xff;
1727 for (i=16; i<40 && i<count; i++)
1729 entries[i] = 0;
1732 for (i=40; i<256 && i<count; i++)
1734 entries[i] = 0xff000000;
1735 entries[i] |= halftone_values[(i-40)%6];
1736 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1737 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1741 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1743 HDC screendc = CreateCompatibleDC(0);
1745 if (!screendc) return GenericError;
1747 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1748 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1750 DeleteDC(screendc);
1752 return Ok;
1755 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1756 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1758 HBITMAP hbitmap=NULL;
1759 INT row_size, dib_stride;
1760 BYTE *bits=NULL, *own_bits=NULL;
1761 REAL xres, yres;
1762 GpStatus stat;
1764 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1766 if (!bitmap) return InvalidParameter;
1768 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1769 *bitmap = NULL;
1770 return InvalidParameter;
1773 if(scan0 && !stride)
1774 return InvalidParameter;
1776 stat = get_screen_resolution(&xres, &yres);
1777 if (stat != Ok) return stat;
1779 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1780 dib_stride = (row_size + 3) & ~3;
1782 if(stride == 0)
1783 stride = dib_stride;
1785 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1787 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
1788 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
1790 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1791 pbmi->bmiHeader.biWidth = width;
1792 pbmi->bmiHeader.biHeight = -height;
1793 pbmi->bmiHeader.biPlanes = 1;
1794 /* FIXME: use the rest of the data from format */
1795 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1796 pbmi->bmiHeader.biCompression = BI_RGB;
1797 pbmi->bmiHeader.biSizeImage = 0;
1798 pbmi->bmiHeader.biXPelsPerMeter = 0;
1799 pbmi->bmiHeader.biYPelsPerMeter = 0;
1800 pbmi->bmiHeader.biClrUsed = 0;
1801 pbmi->bmiHeader.biClrImportant = 0;
1803 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1805 if (!hbitmap) return GenericError;
1807 stride = dib_stride;
1809 else
1811 /* Not a GDI format; don't try to make an HBITMAP. */
1812 if (scan0)
1813 bits = scan0;
1814 else
1816 INT size = abs(stride) * height;
1818 own_bits = bits = heap_alloc_zero(size);
1819 if (!own_bits) return OutOfMemory;
1821 if (stride < 0)
1822 bits += stride * (1 - height);
1826 *bitmap = heap_alloc_zero(sizeof(GpBitmap));
1827 if(!*bitmap)
1829 DeleteObject(hbitmap);
1830 heap_free(own_bits);
1831 return OutOfMemory;
1834 (*bitmap)->image.type = ImageTypeBitmap;
1835 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1836 (*bitmap)->image.flags = ImageFlagsNone;
1837 (*bitmap)->image.frame_count = 1;
1838 (*bitmap)->image.current_frame = 0;
1839 (*bitmap)->image.palette = NULL;
1840 (*bitmap)->image.xres = xres;
1841 (*bitmap)->image.yres = yres;
1842 (*bitmap)->width = width;
1843 (*bitmap)->height = height;
1844 (*bitmap)->format = format;
1845 (*bitmap)->image.decoder = NULL;
1846 (*bitmap)->hbitmap = hbitmap;
1847 (*bitmap)->hdc = NULL;
1848 (*bitmap)->bits = bits;
1849 (*bitmap)->stride = stride;
1850 (*bitmap)->own_bits = own_bits;
1851 (*bitmap)->metadata_reader = NULL;
1852 (*bitmap)->prop_count = 0;
1853 (*bitmap)->prop_item = NULL;
1855 /* set format-related flags */
1856 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1857 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1859 if (format == PixelFormat1bppIndexed ||
1860 format == PixelFormat4bppIndexed ||
1861 format == PixelFormat8bppIndexed)
1863 (*bitmap)->image.palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1865 if (!(*bitmap)->image.palette)
1867 GdipDisposeImage(&(*bitmap)->image);
1868 *bitmap = NULL;
1869 return OutOfMemory;
1872 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1874 if (format == PixelFormat1bppIndexed)
1876 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1877 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1878 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1880 else
1882 if (format == PixelFormat8bppIndexed)
1883 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1885 generate_halftone_palette((*bitmap)->image.palette->Entries,
1886 (*bitmap)->image.palette->Count);
1890 TRACE("<-- %p\n", *bitmap);
1892 return Ok;
1895 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1896 GpBitmap **bitmap)
1898 GpStatus stat;
1900 TRACE("%p %p\n", stream, bitmap);
1902 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1904 if(stat != Ok)
1905 return stat;
1907 if((*bitmap)->image.type != ImageTypeBitmap){
1908 GdipDisposeImage(&(*bitmap)->image);
1909 *bitmap = NULL;
1910 return GenericError; /* FIXME: what error to return? */
1913 return Ok;
1916 /* FIXME: no icm */
1917 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1918 GpBitmap **bitmap)
1920 TRACE("%p %p\n", stream, bitmap);
1922 return GdipCreateBitmapFromStream(stream, bitmap);
1925 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1926 GpCachedBitmap **cachedbmp)
1928 GpStatus stat;
1930 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1932 if(!bitmap || !graphics || !cachedbmp)
1933 return InvalidParameter;
1935 *cachedbmp = heap_alloc_zero(sizeof(GpCachedBitmap));
1936 if(!*cachedbmp)
1937 return OutOfMemory;
1939 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1940 if(stat != Ok){
1941 heap_free(*cachedbmp);
1942 return stat;
1945 return Ok;
1948 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1950 GpStatus stat;
1951 BitmapData lockeddata;
1952 ULONG andstride, xorstride, bitssize;
1953 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1954 UINT x, y;
1956 TRACE("(%p, %p)\n", bitmap, hicon);
1958 if (!bitmap || !hicon)
1959 return InvalidParameter;
1961 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1962 PixelFormat32bppPARGB, &lockeddata);
1963 if (stat == Ok)
1965 andstride = ((lockeddata.Width+31)/32)*4;
1966 xorstride = lockeddata.Width*4;
1967 bitssize = (andstride + xorstride) * lockeddata.Height;
1969 andbits = heap_alloc_zero(bitssize);
1971 if (andbits)
1973 xorbits = andbits + andstride * lockeddata.Height;
1975 for (y=0; y<lockeddata.Height; y++)
1977 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
1979 androw = andbits + andstride * y;
1980 for (x=0; x<lockeddata.Width; x++)
1981 if (srcrow[3+4*x] >= 128)
1982 androw[x/8] |= 1 << (7-x%8);
1984 xorrow = xorbits + xorstride * y;
1985 memcpy(xorrow, srcrow, xorstride);
1988 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
1989 andbits, xorbits);
1991 heap_free(andbits);
1993 else
1994 stat = OutOfMemory;
1996 GdipBitmapUnlockBits(bitmap, &lockeddata);
1999 return stat;
2002 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
2004 TRACE("%p\n", cachedbmp);
2006 if(!cachedbmp)
2007 return InvalidParameter;
2009 GdipDisposeImage(cachedbmp->image);
2010 heap_free(cachedbmp);
2012 return Ok;
2015 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2016 GpCachedBitmap *cachedbmp, INT x, INT y)
2018 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2020 if(!graphics || !cachedbmp)
2021 return InvalidParameter;
2023 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2026 /* Internal utility function: Replace the image data of dst with that of src,
2027 * and free src. */
2028 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2030 assert(src->image.type == ImageTypeBitmap);
2031 assert(dst->image.type == ImageTypeBitmap);
2033 heap_free(dst->bitmapbits);
2034 heap_free(dst->own_bits);
2035 DeleteDC(dst->hdc);
2036 DeleteObject(dst->hbitmap);
2038 if (clobber_palette)
2040 heap_free(dst->image.palette);
2041 dst->image.palette = src->image.palette;
2043 else
2044 heap_free(src->image.palette);
2046 dst->image.xres = src->image.xres;
2047 dst->image.yres = src->image.yres;
2048 dst->width = src->width;
2049 dst->height = src->height;
2050 dst->format = src->format;
2051 dst->hbitmap = src->hbitmap;
2052 dst->hdc = src->hdc;
2053 dst->bits = src->bits;
2054 dst->stride = src->stride;
2055 dst->own_bits = src->own_bits;
2056 if (dst->metadata_reader)
2057 IWICMetadataReader_Release(dst->metadata_reader);
2058 dst->metadata_reader = src->metadata_reader;
2059 heap_free(dst->prop_item);
2060 dst->prop_item = src->prop_item;
2061 dst->prop_count = src->prop_count;
2062 if (dst->image.decoder)
2063 IWICBitmapDecoder_Release(dst->image.decoder);
2064 dst->image.decoder = src->image.decoder;
2065 dst->image.frame_count = src->image.frame_count;
2066 dst->image.current_frame = src->image.current_frame;
2067 dst->image.format = src->image.format;
2069 src->image.type = ~0;
2070 heap_free(src);
2073 static GpStatus free_image_data(GpImage *image)
2075 if(!image)
2076 return InvalidParameter;
2078 if (image->type == ImageTypeBitmap)
2080 heap_free(((GpBitmap*)image)->bitmapbits);
2081 heap_free(((GpBitmap*)image)->own_bits);
2082 DeleteDC(((GpBitmap*)image)->hdc);
2083 DeleteObject(((GpBitmap*)image)->hbitmap);
2084 if (((GpBitmap*)image)->metadata_reader)
2085 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2086 heap_free(((GpBitmap*)image)->prop_item);
2088 else if (image->type == ImageTypeMetafile)
2089 METAFILE_Free((GpMetafile *)image);
2090 else
2092 WARN("invalid image: %p\n", image);
2093 return ObjectBusy;
2095 if (image->decoder)
2096 IWICBitmapDecoder_Release(image->decoder);
2097 heap_free(image->palette);
2099 return Ok;
2102 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2104 GpStatus status;
2106 TRACE("%p\n", image);
2108 status = free_image_data(image);
2109 if (status != Ok) return status;
2110 image->type = ~0;
2111 heap_free(image);
2113 return Ok;
2116 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2118 static int calls;
2120 TRACE("(%p,%p)\n", image, item);
2122 if(!image || !item)
2123 return InvalidParameter;
2125 if (!(calls++))
2126 FIXME("not implemented\n");
2128 return NotImplemented;
2131 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2133 static int calls;
2135 TRACE("(%p,%p)\n", image, item);
2137 if (!(calls++))
2138 FIXME("not implemented\n");
2140 return NotImplemented;
2143 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2144 GpUnit *srcUnit)
2146 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2148 if(!image || !srcRect || !srcUnit)
2149 return InvalidParameter;
2150 if(image->type == ImageTypeMetafile){
2151 *srcRect = ((GpMetafile*)image)->bounds;
2152 *srcUnit = ((GpMetafile*)image)->unit;
2154 else if(image->type == ImageTypeBitmap){
2155 srcRect->X = srcRect->Y = 0.0;
2156 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2157 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2158 *srcUnit = UnitPixel;
2160 else{
2161 WARN("GpImage with no image data\n");
2162 return InvalidParameter;
2165 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2166 srcRect->Width, srcRect->Height, *srcUnit);
2168 return Ok;
2171 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2172 REAL *height)
2174 TRACE("%p %p %p\n", image, width, height);
2176 if(!image || !height || !width)
2177 return InvalidParameter;
2179 if(image->type == ImageTypeMetafile){
2180 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2181 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2183 else if(image->type == ImageTypeBitmap){
2184 *height = ((GpBitmap*)image)->height;
2185 *width = ((GpBitmap*)image)->width;
2187 else{
2188 WARN("GpImage with no image data\n");
2189 return InvalidParameter;
2192 TRACE("returning (%f, %f)\n", *height, *width);
2193 return Ok;
2196 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2197 GpGraphics **graphics)
2199 HDC hdc;
2200 GpStatus stat;
2202 TRACE("%p %p\n", image, graphics);
2204 if(!image || !graphics)
2205 return InvalidParameter;
2207 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2209 hdc = ((GpBitmap*)image)->hdc;
2211 if(!hdc){
2212 hdc = CreateCompatibleDC(0);
2213 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2214 ((GpBitmap*)image)->hdc = hdc;
2217 stat = GdipCreateFromHDC(hdc, graphics);
2219 if (stat == Ok)
2221 (*graphics)->image = image;
2222 (*graphics)->xres = image->xres;
2223 (*graphics)->yres = image->yres;
2226 else if (image->type == ImageTypeMetafile)
2227 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2228 else
2229 stat = graphics_from_image(image, graphics);
2231 return stat;
2234 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2236 TRACE("%p %p\n", image, height);
2238 if(!image || !height)
2239 return InvalidParameter;
2241 if(image->type == ImageTypeMetafile)
2242 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2243 else if(image->type == ImageTypeBitmap)
2244 *height = ((GpBitmap*)image)->height;
2245 else
2247 WARN("GpImage with no image data\n");
2248 return InvalidParameter;
2251 TRACE("returning %d\n", *height);
2253 return Ok;
2256 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2258 if(!image || !res)
2259 return InvalidParameter;
2261 *res = image->xres;
2263 TRACE("(%p) <-- %0.2f\n", image, *res);
2265 return Ok;
2268 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2270 TRACE("%p %p\n", image, size);
2272 if(!image || !size)
2273 return InvalidParameter;
2275 if (!image->palette || image->palette->Count == 0)
2276 *size = sizeof(ColorPalette);
2277 else
2278 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2280 TRACE("<-- %u\n", *size);
2282 return Ok;
2285 /* FIXME: test this function for non-bitmap types */
2286 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2288 TRACE("%p %p\n", image, format);
2290 if(!image || !format)
2291 return InvalidParameter;
2293 if(image->type != ImageTypeBitmap)
2294 *format = PixelFormat24bppRGB;
2295 else
2296 *format = ((GpBitmap*) image)->format;
2298 return Ok;
2301 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2303 TRACE("(%p, %p)\n", image, format);
2305 if(!image || !format)
2306 return InvalidParameter;
2308 memcpy(format, &image->format, sizeof(GUID));
2310 return Ok;
2313 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2315 TRACE("%p %p\n", image, type);
2317 if(!image || !type)
2318 return InvalidParameter;
2320 *type = image->type;
2322 return Ok;
2325 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2327 if(!image || !res)
2328 return InvalidParameter;
2330 *res = image->yres;
2332 TRACE("(%p) <-- %0.2f\n", image, *res);
2334 return Ok;
2337 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2339 TRACE("%p %p\n", image, width);
2341 if(!image || !width)
2342 return InvalidParameter;
2344 if(image->type == ImageTypeMetafile)
2345 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2346 else if(image->type == ImageTypeBitmap)
2347 *width = ((GpBitmap*)image)->width;
2348 else
2350 WARN("GpImage with no image data\n");
2351 return InvalidParameter;
2354 TRACE("returning %d\n", *width);
2356 return Ok;
2359 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2361 TRACE("(%p, %p)\n", image, num);
2363 if (!image || !num) return InvalidParameter;
2365 *num = 0;
2367 if (image->type == ImageTypeBitmap)
2369 if (((GpBitmap *)image)->prop_item)
2371 *num = ((GpBitmap *)image)->prop_count;
2372 return Ok;
2375 if (((GpBitmap *)image)->metadata_reader)
2376 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2379 return Ok;
2382 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2384 HRESULT hr;
2385 IWICMetadataReader *reader;
2386 IWICEnumMetadataItem *enumerator;
2387 UINT prop_count, i, items_returned;
2389 TRACE("(%p, %u, %p)\n", image, num, list);
2391 if (!image || !list) return InvalidParameter;
2393 if (image->type != ImageTypeBitmap)
2395 FIXME("Not implemented for type %d\n", image->type);
2396 return NotImplemented;
2399 if (((GpBitmap *)image)->prop_item)
2401 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2403 for (i = 0; i < num; i++)
2405 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2408 return Ok;
2411 reader = ((GpBitmap *)image)->metadata_reader;
2412 if (!reader)
2414 if (num != 0) return InvalidParameter;
2415 return Ok;
2418 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2419 if (FAILED(hr)) return hresult_to_status(hr);
2421 if (num != prop_count) return InvalidParameter;
2423 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2424 if (FAILED(hr)) return hresult_to_status(hr);
2426 IWICEnumMetadataItem_Reset(enumerator);
2428 for (i = 0; i < num; i++)
2430 PROPVARIANT id;
2432 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2433 if (hr != S_OK) break;
2435 if (id.vt != VT_UI2)
2437 FIXME("not supported propvariant type for id: %u\n", id.vt);
2438 list[i] = 0;
2439 continue;
2441 list[i] = id.u.uiVal;
2444 IWICEnumMetadataItem_Release(enumerator);
2446 return hr == S_OK ? Ok : hresult_to_status(hr);
2449 static UINT propvariant_size(PROPVARIANT *value)
2451 switch (value->vt & ~VT_VECTOR)
2453 case VT_EMPTY:
2454 return 0;
2455 case VT_I1:
2456 case VT_UI1:
2457 if (!(value->vt & VT_VECTOR)) return 1;
2458 return value->u.caub.cElems;
2459 case VT_I2:
2460 case VT_UI2:
2461 if (!(value->vt & VT_VECTOR)) return 2;
2462 return value->u.caui.cElems * 2;
2463 case VT_I4:
2464 case VT_UI4:
2465 case VT_R4:
2466 if (!(value->vt & VT_VECTOR)) return 4;
2467 return value->u.caul.cElems * 4;
2468 case VT_I8:
2469 case VT_UI8:
2470 case VT_R8:
2471 if (!(value->vt & VT_VECTOR)) return 8;
2472 return value->u.cauh.cElems * 8;
2473 case VT_LPSTR:
2474 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2475 case VT_BLOB:
2476 return value->u.blob.cbSize;
2477 default:
2478 FIXME("not supported variant type %d\n", value->vt);
2479 return 0;
2483 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2485 HRESULT hr;
2486 IWICMetadataReader *reader;
2487 PROPVARIANT id, value;
2489 TRACE("(%p,%#x,%p)\n", image, propid, size);
2491 if (!size || !image) return InvalidParameter;
2493 if (image->type != ImageTypeBitmap)
2495 FIXME("Not implemented for type %d\n", image->type);
2496 return NotImplemented;
2499 if (((GpBitmap *)image)->prop_item)
2501 UINT i;
2503 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2505 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2507 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2508 return Ok;
2512 return PropertyNotFound;
2515 reader = ((GpBitmap *)image)->metadata_reader;
2516 if (!reader) return PropertyNotFound;
2518 id.vt = VT_UI2;
2519 id.u.uiVal = propid;
2520 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2521 if (FAILED(hr)) return PropertyNotFound;
2523 *size = propvariant_size(&value);
2524 if (*size) *size += sizeof(PropertyItem);
2525 PropVariantClear(&value);
2527 return Ok;
2530 #ifndef PropertyTagTypeSByte
2531 #define PropertyTagTypeSByte 6
2532 #define PropertyTagTypeSShort 8
2533 #define PropertyTagTypeFloat 11
2534 #define PropertyTagTypeDouble 12
2535 #endif
2537 static UINT vt_to_itemtype(UINT vt)
2539 static const struct
2541 UINT vt, type;
2542 } vt2type[] =
2544 { VT_I1, PropertyTagTypeSByte },
2545 { VT_UI1, PropertyTagTypeByte },
2546 { VT_I2, PropertyTagTypeSShort },
2547 { VT_UI2, PropertyTagTypeShort },
2548 { VT_I4, PropertyTagTypeSLONG },
2549 { VT_UI4, PropertyTagTypeLong },
2550 { VT_I8, PropertyTagTypeSRational },
2551 { VT_UI8, PropertyTagTypeRational },
2552 { VT_R4, PropertyTagTypeFloat },
2553 { VT_R8, PropertyTagTypeDouble },
2554 { VT_LPSTR, PropertyTagTypeASCII },
2555 { VT_BLOB, PropertyTagTypeUndefined }
2557 UINT i;
2558 for (i = 0; i < ARRAY_SIZE(vt2type); i++)
2560 if (vt2type[i].vt == vt) return vt2type[i].type;
2562 FIXME("not supported variant type %u\n", vt);
2563 return 0;
2566 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2567 UINT size, PROPID id)
2569 UINT item_size, item_type;
2571 item_size = propvariant_size(value);
2572 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2574 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2575 if (!item_type) return InvalidParameter;
2577 item->value = item + 1;
2579 switch (value->vt & ~VT_VECTOR)
2581 case VT_I1:
2582 case VT_UI1:
2583 if (!(value->vt & VT_VECTOR))
2584 *(BYTE *)item->value = value->u.bVal;
2585 else
2586 memcpy(item->value, value->u.caub.pElems, item_size);
2587 break;
2588 case VT_I2:
2589 case VT_UI2:
2590 if (!(value->vt & VT_VECTOR))
2591 *(USHORT *)item->value = value->u.uiVal;
2592 else
2593 memcpy(item->value, value->u.caui.pElems, item_size);
2594 break;
2595 case VT_I4:
2596 case VT_UI4:
2597 case VT_R4:
2598 if (!(value->vt & VT_VECTOR))
2599 *(ULONG *)item->value = value->u.ulVal;
2600 else
2601 memcpy(item->value, value->u.caul.pElems, item_size);
2602 break;
2603 case VT_I8:
2604 case VT_UI8:
2605 case VT_R8:
2606 if (!(value->vt & VT_VECTOR))
2607 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2608 else
2609 memcpy(item->value, value->u.cauh.pElems, item_size);
2610 break;
2611 case VT_LPSTR:
2612 memcpy(item->value, value->u.pszVal, item_size);
2613 break;
2614 case VT_BLOB:
2615 memcpy(item->value, value->u.blob.pBlobData, item_size);
2616 break;
2617 default:
2618 FIXME("not supported variant type %d\n", value->vt);
2619 return InvalidParameter;
2622 item->length = item_size;
2623 item->type = item_type;
2624 item->id = id;
2626 return Ok;
2629 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2630 PropertyItem *buffer)
2632 GpStatus stat;
2633 HRESULT hr;
2634 IWICMetadataReader *reader;
2635 PROPVARIANT id, value;
2637 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2639 if (!image || !buffer) return InvalidParameter;
2641 if (image->type != ImageTypeBitmap)
2643 FIXME("Not implemented for type %d\n", image->type);
2644 return NotImplemented;
2647 if (((GpBitmap *)image)->prop_item)
2649 UINT i;
2651 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2653 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2655 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2656 return InvalidParameter;
2658 *buffer = ((GpBitmap *)image)->prop_item[i];
2659 buffer->value = buffer + 1;
2660 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2661 return Ok;
2665 return PropertyNotFound;
2668 reader = ((GpBitmap *)image)->metadata_reader;
2669 if (!reader) return PropertyNotFound;
2671 id.vt = VT_UI2;
2672 id.u.uiVal = propid;
2673 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2674 if (FAILED(hr)) return PropertyNotFound;
2676 stat = propvariant_to_item(&value, buffer, size, propid);
2677 PropVariantClear(&value);
2679 return stat;
2682 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2684 HRESULT hr;
2685 IWICMetadataReader *reader;
2686 IWICEnumMetadataItem *enumerator;
2687 UINT prop_count, prop_size, i;
2688 PROPVARIANT id, value;
2690 TRACE("(%p,%p,%p)\n", image, size, count);
2692 if (!image || !size || !count) return InvalidParameter;
2694 if (image->type != ImageTypeBitmap)
2696 FIXME("Not implemented for type %d\n", image->type);
2697 return NotImplemented;
2700 if (((GpBitmap *)image)->prop_item)
2702 *count = ((GpBitmap *)image)->prop_count;
2703 *size = 0;
2705 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2707 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2710 return Ok;
2713 reader = ((GpBitmap *)image)->metadata_reader;
2714 if (!reader) return PropertyNotFound;
2716 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2717 if (FAILED(hr)) return hresult_to_status(hr);
2719 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2720 if (FAILED(hr)) return hresult_to_status(hr);
2722 IWICEnumMetadataItem_Reset(enumerator);
2724 prop_size = 0;
2726 PropVariantInit(&id);
2727 PropVariantInit(&value);
2729 for (i = 0; i < prop_count; i++)
2731 UINT items_returned, item_size;
2733 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2734 if (hr != S_OK) break;
2736 item_size = propvariant_size(&value);
2737 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2739 PropVariantClear(&id);
2740 PropVariantClear(&value);
2743 IWICEnumMetadataItem_Release(enumerator);
2745 if (hr != S_OK) return PropertyNotFound;
2747 *count = prop_count;
2748 *size = prop_size;
2749 return Ok;
2752 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2753 UINT count, PropertyItem *buf)
2755 GpStatus status;
2756 HRESULT hr;
2757 IWICMetadataReader *reader;
2758 IWICEnumMetadataItem *enumerator;
2759 UINT prop_count, prop_size, i;
2760 PROPVARIANT id, value;
2761 char *item_value;
2763 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2765 if (!image || !buf) return InvalidParameter;
2767 if (image->type != ImageTypeBitmap)
2769 FIXME("Not implemented for type %d\n", image->type);
2770 return NotImplemented;
2773 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2774 if (status != Ok) return status;
2776 if (prop_count != count || prop_size != size) return InvalidParameter;
2778 if (((GpBitmap *)image)->prop_item)
2780 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2782 item_value = (char *)(buf + prop_count);
2784 for (i = 0; i < prop_count; i++)
2786 buf[i].value = item_value;
2787 item_value += buf[i].length;
2790 return Ok;
2793 reader = ((GpBitmap *)image)->metadata_reader;
2794 if (!reader) return PropertyNotFound;
2796 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2797 if (FAILED(hr)) return hresult_to_status(hr);
2799 IWICEnumMetadataItem_Reset(enumerator);
2801 item_value = (char *)(buf + prop_count);
2803 PropVariantInit(&id);
2804 PropVariantInit(&value);
2806 for (i = 0; i < prop_count; i++)
2808 PropertyItem *item;
2809 UINT items_returned, item_size;
2811 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2812 if (hr != S_OK) break;
2814 if (id.vt != VT_UI2)
2816 FIXME("not supported propvariant type for id: %u\n", id.vt);
2817 continue;
2820 item_size = propvariant_size(&value);
2821 if (item_size)
2823 item = heap_alloc(item_size + sizeof(*item));
2825 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2826 buf[i].id = item->id;
2827 buf[i].type = item->type;
2828 buf[i].length = item_size;
2829 buf[i].value = item_value;
2830 memcpy(item_value, item->value, item_size);
2831 item_value += item_size;
2833 heap_free(item);
2836 PropVariantClear(&id);
2837 PropVariantClear(&value);
2840 IWICEnumMetadataItem_Release(enumerator);
2842 if (hr != S_OK) return PropertyNotFound;
2844 return Ok;
2847 struct image_format_dimension
2849 const GUID *format;
2850 const GUID *dimension;
2853 static const struct image_format_dimension image_format_dimensions[] =
2855 {&ImageFormatGIF, &FrameDimensionTime},
2856 {&ImageFormatIcon, &FrameDimensionResolution},
2857 {NULL}
2860 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2861 GDIPCONST GUID* dimensionID, UINT* count)
2863 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2865 if(!image || !count)
2866 return InvalidParameter;
2868 if (!dimensionID ||
2869 IsEqualGUID(dimensionID, &image->format) ||
2870 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2871 IsEqualGUID(dimensionID, &FrameDimensionTime))
2873 *count = image->frame_count;
2874 return Ok;
2877 return InvalidParameter;
2880 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2881 UINT* count)
2883 TRACE("(%p, %p)\n", image, count);
2885 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2887 if(!image || !count)
2888 return InvalidParameter;
2890 *count = 1;
2892 return Ok;
2895 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2896 GUID* dimensionIDs, UINT count)
2898 int i;
2899 const GUID *result=NULL;
2901 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2903 if(!image || !dimensionIDs || count != 1)
2904 return InvalidParameter;
2906 for (i=0; image_format_dimensions[i].format; i++)
2908 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2910 result = image_format_dimensions[i].dimension;
2911 break;
2915 if (!result)
2916 result = &FrameDimensionPage;
2918 memcpy(dimensionIDs, result, sizeof(GUID));
2920 return Ok;
2923 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2924 GpImage **image)
2926 GpStatus stat;
2927 IStream *stream;
2929 TRACE("(%s) %p\n", debugstr_w(filename), image);
2931 if (!filename || !image)
2932 return InvalidParameter;
2934 *image = NULL;
2936 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2938 if (stat != Ok)
2939 return stat;
2941 stat = GdipLoadImageFromStream(stream, image);
2943 IStream_Release(stream);
2945 return stat;
2948 /* FIXME: no icm handling */
2949 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2951 TRACE("(%s) %p\n", debugstr_w(filename), image);
2953 return GdipLoadImageFromFile(filename, image);
2956 static void add_property(GpBitmap *bitmap, PropertyItem *item)
2958 UINT prop_size, prop_count;
2959 PropertyItem *prop_item;
2961 if (bitmap->prop_item == NULL)
2963 prop_size = prop_count = 0;
2964 prop_item = heap_alloc_zero(item->length + sizeof(PropertyItem));
2965 if (!prop_item) return;
2967 else
2969 UINT i;
2970 char *item_value;
2972 GdipGetPropertySize(&bitmap->image, &prop_size, &prop_count);
2974 prop_item = heap_alloc_zero(prop_size + item->length + sizeof(PropertyItem));
2975 if (!prop_item) return;
2976 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
2977 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
2978 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
2980 item_value = (char *)(prop_item + prop_count + 1);
2982 for (i = 0; i < prop_count; i++)
2984 prop_item[i].value = item_value;
2985 item_value += prop_item[i].length;
2989 prop_item[prop_count].id = item->id;
2990 prop_item[prop_count].type = item->type;
2991 prop_item[prop_count].length = item->length;
2992 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
2993 memcpy(prop_item[prop_count].value, item->value, item->length);
2995 heap_free(bitmap->prop_item);
2996 bitmap->prop_item = prop_item;
2997 bitmap->prop_count++;
3000 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3002 HRESULT hr;
3003 GUID format;
3004 PROPVARIANT id, value;
3005 BOOL ret = FALSE;
3007 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3008 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return FALSE;
3010 PropVariantInit(&id);
3011 PropVariantInit(&value);
3013 id.vt = VT_LPWSTR;
3014 id.u.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3015 if (!id.u.pwszVal) return FALSE;
3016 lstrcpyW(id.u.pwszVal, prop_name);
3017 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3018 if (hr == S_OK && value.vt == VT_BOOL)
3019 ret = value.u.boolVal;
3021 PropVariantClear(&id);
3022 PropVariantClear(&value);
3024 return ret;
3027 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3029 HRESULT hr;
3030 GUID format;
3031 PROPVARIANT id, value;
3032 PropertyItem *item = NULL;
3034 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3035 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return NULL;
3037 PropVariantInit(&id);
3038 PropVariantInit(&value);
3040 id.vt = VT_LPWSTR;
3041 id.u.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3042 if (!id.u.pwszVal) return NULL;
3043 lstrcpyW(id.u.pwszVal, prop_name);
3044 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3045 if (hr == S_OK)
3047 UINT item_size = propvariant_size(&value);
3048 if (item_size)
3050 item_size += sizeof(*item);
3051 item = heap_alloc_zero(item_size);
3052 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3054 heap_free(item);
3055 item = NULL;
3060 PropVariantClear(&id);
3061 PropVariantClear(&value);
3063 return item;
3066 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3068 static const WCHAR textentryW[] = { 'T','e','x','t','E','n','t','r','y',0 };
3069 PropertyItem *comment;
3071 comment = get_property(reader, &GUID_MetadataFormatGifComment, textentryW);
3072 if (comment)
3073 comment->id = PropertyTagExifUserComment;
3075 return comment;
3078 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3080 static const WCHAR applicationW[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3081 static const WCHAR dataW[] = { 'D','a','t','a',0 };
3082 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3084 appext = get_property(reader, &GUID_MetadataFormatAPE, applicationW);
3085 if (appext)
3087 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3088 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3090 appdata = get_property(reader, &GUID_MetadataFormatAPE, dataW);
3091 if (appdata)
3093 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3095 BYTE *data = appdata->value;
3096 if (data[0] == 3 && data[1] == 1)
3098 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3099 if (loop)
3101 loop->type = PropertyTagTypeShort;
3102 loop->id = PropertyTagLoopCount;
3103 loop->length = sizeof(SHORT);
3104 loop->value = loop + 1;
3105 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3113 heap_free(appext);
3114 heap_free(appdata);
3116 return loop;
3119 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3121 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 };
3122 PropertyItem *background;
3124 background = get_property(reader, &GUID_MetadataFormatLSD, backgroundW);
3125 if (background)
3126 background->id = PropertyTagIndexBackground;
3128 return background;
3131 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3133 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 };
3134 HRESULT hr;
3135 IWICImagingFactory *factory;
3136 IWICPalette *palette;
3137 UINT count = 0;
3138 WICColor colors[256];
3140 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, global_flagW))
3141 return NULL;
3143 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3144 if (hr != S_OK) return NULL;
3146 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3147 if (hr == S_OK)
3149 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3150 if (hr == S_OK)
3151 IWICPalette_GetColors(palette, 256, colors, &count);
3153 IWICPalette_Release(palette);
3156 IWICImagingFactory_Release(factory);
3158 if (count)
3160 PropertyItem *pal;
3161 UINT i;
3162 BYTE *rgb;
3164 pal = heap_alloc_zero(sizeof(*pal) + count * 3);
3165 if (!pal) return NULL;
3166 pal->type = PropertyTagTypeByte;
3167 pal->id = PropertyTagGlobalPalette;
3168 pal->value = pal + 1;
3169 pal->length = count * 3;
3171 rgb = pal->value;
3173 for (i = 0; i < count; i++)
3175 rgb[i*3] = (colors[i] >> 16) & 0xff;
3176 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3177 rgb[i*3 + 2] = colors[i] & 0xff;
3180 return pal;
3183 return NULL;
3186 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3188 static const WCHAR transparency_flagW[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3189 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 };
3190 PropertyItem *index = NULL;
3192 if (get_bool_property(reader, &GUID_MetadataFormatGCE, transparency_flagW))
3194 index = get_property(reader, &GUID_MetadataFormatGCE, colorW);
3195 if (index)
3196 index->id = PropertyTagIndexTransparent;
3198 return index;
3201 static LONG get_gif_frame_property(IWICBitmapFrameDecode *frame, const GUID *format, const WCHAR *property)
3203 HRESULT hr;
3204 IWICMetadataBlockReader *block_reader;
3205 IWICMetadataReader *reader;
3206 UINT block_count, i;
3207 PropertyItem *prop;
3208 LONG value = 0;
3210 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3211 if (hr == S_OK)
3213 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3214 if (hr == S_OK)
3216 for (i = 0; i < block_count; i++)
3218 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3219 if (hr == S_OK)
3221 prop = get_property(reader, format, property);
3222 if (prop)
3224 if (prop->type == PropertyTagTypeByte && prop->length == 1)
3225 value = *(BYTE *)prop->value;
3226 else if (prop->type == PropertyTagTypeShort && prop->length == 2)
3227 value = *(SHORT *)prop->value;
3229 heap_free(prop);
3231 IWICMetadataReader_Release(reader);
3235 IWICMetadataBlockReader_Release(block_reader);
3238 return value;
3241 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3243 static const WCHAR delayW[] = { 'D','e','l','a','y',0 };
3244 HRESULT hr;
3245 IWICBitmapFrameDecode *frame;
3246 IWICMetadataBlockReader *block_reader;
3247 IWICMetadataReader *reader;
3248 UINT frame_count, block_count, i;
3249 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3250 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3252 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3253 if (frame_count > 1)
3255 delay = heap_alloc_zero(sizeof(*delay) + frame_count * sizeof(LONG));
3256 if (delay)
3258 LONG *value;
3260 delay->type = PropertyTagTypeLong;
3261 delay->id = PropertyTagFrameDelay;
3262 delay->length = frame_count * sizeof(LONG);
3263 delay->value = delay + 1;
3265 value = delay->value;
3267 for (i = 0; i < frame_count; i++)
3269 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3270 if (hr == S_OK)
3272 value[i] = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, delayW);
3273 IWICBitmapFrameDecode_Release(frame);
3275 else value[i] = 0;
3280 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3281 if (hr == S_OK)
3283 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3284 if (hr == S_OK)
3286 for (i = 0; i < block_count; i++)
3288 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3289 if (hr == S_OK)
3291 if (!comment)
3292 comment = get_gif_comment(reader);
3294 if (frame_count > 1 && !loop)
3295 loop = get_gif_loopcount(reader);
3297 if (!background)
3298 background = get_gif_background(reader);
3300 if (!palette)
3301 palette = get_gif_palette(decoder, reader);
3303 IWICMetadataReader_Release(reader);
3307 IWICMetadataBlockReader_Release(block_reader);
3310 if (frame_count > 1 && !loop)
3312 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3313 if (loop)
3315 loop->type = PropertyTagTypeShort;
3316 loop->id = PropertyTagLoopCount;
3317 loop->length = sizeof(SHORT);
3318 loop->value = loop + 1;
3319 *(SHORT *)loop->value = 1;
3323 if (delay) add_property(bitmap, delay);
3324 if (comment) add_property(bitmap, comment);
3325 if (loop) add_property(bitmap, loop);
3326 if (palette) add_property(bitmap, palette);
3327 if (background) add_property(bitmap, background);
3329 heap_free(delay);
3330 heap_free(comment);
3331 heap_free(loop);
3332 heap_free(palette);
3333 heap_free(background);
3335 /* Win7 gdiplus always returns transparent color index from frame 0 */
3336 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3337 if (hr != S_OK) return;
3339 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3340 if (hr == S_OK)
3342 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3343 if (hr == S_OK)
3345 for (i = 0; i < block_count; i++)
3347 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3348 if (hr == S_OK)
3350 if (!transparent_idx)
3351 transparent_idx = get_gif_transparent_idx(reader);
3353 IWICMetadataReader_Release(reader);
3357 IWICMetadataBlockReader_Release(block_reader);
3360 if (transparent_idx) add_property(bitmap, transparent_idx);
3361 heap_free(transparent_idx);
3363 IWICBitmapFrameDecode_Release(frame);
3366 static PropertyItem* create_prop(PROPID propid, PROPVARIANT* value)
3368 PropertyItem *item = NULL;
3369 UINT item_size = propvariant_size(value);
3371 if (item_size)
3373 item_size += sizeof(*item);
3374 item = heap_alloc_zero(item_size);
3375 if (propvariant_to_item(value, item, item_size, propid) != Ok)
3377 heap_free(item);
3378 item = NULL;
3382 return item;
3385 static ULONG get_ulong_by_index(IWICMetadataReader* reader, ULONG index)
3387 PROPVARIANT value;
3388 HRESULT hr;
3389 ULONG result=0;
3391 hr = IWICMetadataReader_GetValueByIndex(reader, index, NULL, NULL, &value);
3392 if (SUCCEEDED(hr))
3394 switch (value.vt)
3396 case VT_UI4:
3397 result = value.u.ulVal;
3398 break;
3399 default:
3400 ERR("unhandled case %u\n", value.vt);
3401 break;
3403 PropVariantClear(&value);
3405 return result;
3408 static void png_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3410 HRESULT hr;
3411 IWICBitmapFrameDecode *frame;
3412 IWICMetadataBlockReader *block_reader;
3413 IWICMetadataReader *reader;
3414 UINT block_count, i, j;
3415 struct keyword_info {
3416 const char* name;
3417 PROPID propid;
3418 BOOL seen;
3419 } keywords[] = {
3420 { "Title", PropertyTagImageTitle },
3421 { "Author", PropertyTagArtist },
3422 { "Description", PropertyTagImageDescription },
3423 { "Copyright", PropertyTagCopyright },
3424 { "Software", PropertyTagSoftwareUsed },
3425 { "Source", PropertyTagEquipModel },
3426 { "Comment", PropertyTagExifUserComment },
3428 BOOL seen_gamma=FALSE, seen_whitepoint=FALSE, seen_chrm=FALSE;
3430 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3431 if (hr != S_OK) return;
3433 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3434 if (hr == S_OK)
3436 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3437 if (hr == S_OK)
3439 for (i = 0; i < block_count; i++)
3441 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3442 if (hr == S_OK)
3444 GUID format;
3446 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3447 if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunktEXt, &format))
3449 PROPVARIANT name, value;
3450 PropertyItem* item;
3452 hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, &name, &value);
3454 if (SUCCEEDED(hr))
3456 if (name.vt == VT_LPSTR)
3458 for (j = 0; j < ARRAY_SIZE(keywords); j++)
3459 if (!strcmp(keywords[j].name, name.u.pszVal))
3460 break;
3461 if (j < ARRAY_SIZE(keywords) && !keywords[j].seen)
3463 keywords[j].seen = TRUE;
3464 item = create_prop(keywords[j].propid, &value);
3465 if (item)
3466 add_property(bitmap, item);
3467 heap_free(item);
3471 PropVariantClear(&name);
3472 PropVariantClear(&value);
3475 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA, &format))
3477 PropertyItem* item;
3479 if (!seen_gamma)
3481 item = heap_alloc_zero(sizeof(PropertyItem) + sizeof(ULONG) * 2);
3482 if (item)
3484 ULONG *rational;
3485 item->length = sizeof(ULONG) * 2;
3486 item->type = PropertyTagTypeRational;
3487 item->id = PropertyTagGamma;
3488 rational = item->value = item + 1;
3489 rational[0] = 100000;
3490 rational[1] = get_ulong_by_index(reader, 0);
3491 add_property(bitmap, item);
3492 seen_gamma = TRUE;
3493 heap_free(item);
3497 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkcHRM, &format))
3499 PropertyItem* item;
3501 if (!seen_whitepoint)
3503 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 4);
3504 if (item)
3506 ULONG *rational;
3507 item->length = sizeof(ULONG) * 4;
3508 item->type = PropertyTagTypeRational;
3509 item->id = PropertyTagWhitePoint;
3510 rational = item->value = item + 1;
3511 rational[0] = get_ulong_by_index(reader, 0);
3512 rational[1] = 100000;
3513 rational[2] = get_ulong_by_index(reader, 1);
3514 rational[3] = 100000;
3515 add_property(bitmap, item);
3516 seen_whitepoint = TRUE;
3517 GdipFree(item);
3520 if (!seen_chrm)
3522 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 12);
3523 if (item)
3525 ULONG *rational;
3526 item->length = sizeof(ULONG) * 12;
3527 item->type = PropertyTagTypeRational;
3528 item->id = PropertyTagPrimaryChromaticities;
3529 rational = item->value = item + 1;
3530 rational[0] = get_ulong_by_index(reader, 2);
3531 rational[1] = 100000;
3532 rational[2] = get_ulong_by_index(reader, 3);
3533 rational[3] = 100000;
3534 rational[4] = get_ulong_by_index(reader, 4);
3535 rational[5] = 100000;
3536 rational[6] = get_ulong_by_index(reader, 5);
3537 rational[7] = 100000;
3538 rational[8] = get_ulong_by_index(reader, 6);
3539 rational[9] = 100000;
3540 rational[10] = get_ulong_by_index(reader, 7);
3541 rational[11] = 100000;
3542 add_property(bitmap, item);
3543 seen_chrm = TRUE;
3544 GdipFree(item);
3549 IWICMetadataReader_Release(reader);
3553 IWICMetadataBlockReader_Release(block_reader);
3556 IWICBitmapFrameDecode_Release(frame);
3559 static GpStatus initialize_decoder_wic(IStream *stream, REFGUID container, IWICBitmapDecoder **decoder)
3561 IWICImagingFactory *factory;
3562 HRESULT hr;
3564 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
3566 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3567 if (FAILED(hr)) return hresult_to_status(hr);
3568 hr = IWICImagingFactory_CreateDecoder(factory, container, NULL, decoder);
3569 IWICImagingFactory_Release(factory);
3570 if (FAILED(hr)) return hresult_to_status(hr);
3572 hr = IWICBitmapDecoder_Initialize(*decoder, stream, WICDecodeMetadataCacheOnLoad);
3573 if (FAILED(hr)) return hresult_to_status(hr);
3574 return Ok;
3577 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3579 static GpStatus decode_frame_wic(IWICBitmapDecoder *decoder, BOOL force_conversion,
3580 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3582 GpStatus status=Ok;
3583 GpBitmap *bitmap;
3584 HRESULT hr;
3585 IWICBitmapFrameDecode *frame;
3586 IWICBitmapSource *source=NULL;
3587 IWICMetadataBlockReader *block_reader;
3588 WICPixelFormatGUID wic_format;
3589 PixelFormat gdip_format=0;
3590 ColorPalette *palette = NULL;
3591 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3592 int i;
3593 UINT width, height, frame_count;
3594 BitmapData lockeddata;
3595 WICRect wrc;
3597 TRACE("%p,%u,%p\n", decoder, active_frame, image);
3599 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3600 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3601 if (SUCCEEDED(hr)) /* got frame */
3603 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3605 if (SUCCEEDED(hr))
3607 if (!force_conversion)
3609 for (i=0; pixel_formats[i].wic_format; i++)
3611 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3613 source = (IWICBitmapSource*)frame;
3614 IWICBitmapSource_AddRef(source);
3615 gdip_format = pixel_formats[i].gdip_format;
3616 palette_type = pixel_formats[i].palette_type;
3617 break;
3621 if (!source)
3623 /* unknown format; fall back on 32bppARGB */
3624 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3625 gdip_format = PixelFormat32bppARGB;
3627 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3630 if (SUCCEEDED(hr)) /* got source */
3632 hr = IWICBitmapSource_GetSize(source, &width, &height);
3634 if (SUCCEEDED(hr))
3635 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3636 NULL, &bitmap);
3638 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3640 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3641 gdip_format, &lockeddata);
3642 if (status == Ok) /* locked bitmap */
3644 wrc.X = 0;
3645 wrc.Width = width;
3646 wrc.Height = 1;
3647 for (i=0; i<height; i++)
3649 wrc.Y = i;
3650 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3651 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3652 if (FAILED(hr)) break;
3655 GdipBitmapUnlockBits(bitmap, &lockeddata);
3658 if (SUCCEEDED(hr) && status == Ok)
3659 *image = &bitmap->image;
3660 else
3662 *image = NULL;
3663 GdipDisposeImage(&bitmap->image);
3666 if (SUCCEEDED(hr) && status == Ok)
3668 double dpix, dpiy;
3669 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3670 if (SUCCEEDED(hr))
3672 bitmap->image.xres = dpix;
3673 bitmap->image.yres = dpiy;
3675 hr = S_OK;
3679 IWICBitmapSource_Release(source);
3682 if (SUCCEEDED(hr)) {
3683 bitmap->metadata_reader = NULL;
3685 if (metadata_reader)
3686 metadata_reader(bitmap, decoder, active_frame);
3687 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3689 UINT block_count = 0;
3690 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3691 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3692 IWICMetadataBlockReader_Release(block_reader);
3695 palette = get_palette(frame, palette_type);
3696 IWICBitmapFrameDecode_Release(frame);
3700 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3702 if (status == Ok)
3704 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3705 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI;
3706 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormat2bppGray) ||
3707 IsEqualGUID(&wic_format, &GUID_WICPixelFormat4bppGray) ||
3708 IsEqualGUID(&wic_format, &GUID_WICPixelFormat8bppGray) ||
3709 IsEqualGUID(&wic_format, &GUID_WICPixelFormat16bppGray))
3710 bitmap->image.flags |= ImageFlagsColorSpaceGRAY;
3711 else
3712 bitmap->image.flags |= ImageFlagsColorSpaceRGB;
3713 bitmap->image.frame_count = frame_count;
3714 bitmap->image.current_frame = active_frame;
3715 bitmap->image.decoder = decoder;
3716 IWICBitmapDecoder_AddRef(decoder);
3717 if (palette)
3719 heap_free(bitmap->image.palette);
3720 bitmap->image.palette = palette;
3722 else
3724 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3725 bitmap->image.palette->Flags = 0;
3727 TRACE("=> %p\n", *image);
3730 return status;
3733 static GpStatus decode_image_wic(IStream *stream, REFGUID container,
3734 metadata_reader_func metadata_reader, GpImage **image)
3736 IWICBitmapDecoder *decoder;
3737 GpStatus status;
3739 status = initialize_decoder_wic(stream, container, &decoder);
3740 if(status != Ok)
3741 return status;
3743 status = decode_frame_wic(decoder, FALSE, 0, metadata_reader, image);
3744 IWICBitmapDecoder_Release(decoder);
3745 return status;
3748 static GpStatus select_frame_wic(GpImage *image, UINT active_frame)
3750 GpImage *new_image;
3751 GpStatus status;
3753 status = decode_frame_wic(image->decoder, FALSE, active_frame, NULL, &new_image);
3754 if(status != Ok)
3755 return status;
3757 new_image->busy = image->busy;
3758 memcpy(&new_image->format, &image->format, sizeof(GUID));
3759 free_image_data(image);
3760 if (image->type == ImageTypeBitmap)
3761 *(GpBitmap *)image = *(GpBitmap *)new_image;
3762 else if (image->type == ImageTypeMetafile)
3763 *(GpMetafile *)image = *(GpMetafile *)new_image;
3764 new_image->type = ~0;
3765 heap_free(new_image);
3766 return Ok;
3769 static HRESULT get_gif_frame_rect(IWICBitmapFrameDecode *frame,
3770 UINT *left, UINT *top, UINT *width, UINT *height)
3772 static const WCHAR leftW[] = {'L','e','f','t',0};
3773 static const WCHAR topW[] = {'T','o','p',0};
3775 *left = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, leftW);
3776 *top = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, topW);
3778 return IWICBitmapFrameDecode_GetSize(frame, width, height);
3781 static HRESULT blit_gif_frame(GpBitmap *bitmap, IWICBitmapFrameDecode *frame, BOOL first_frame)
3783 UINT i, j, left, top, width, height;
3784 IWICBitmapSource *source;
3785 BYTE *new_bits;
3786 HRESULT hr;
3788 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3789 if(FAILED(hr))
3790 return hr;
3792 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3793 if(FAILED(hr))
3794 return hr;
3796 new_bits = heap_alloc_zero(width*height*4);
3797 if(!new_bits)
3798 return E_OUTOFMEMORY;
3800 hr = IWICBitmapSource_CopyPixels(source, NULL, width*4, width*height*4, new_bits);
3801 IWICBitmapSource_Release(source);
3802 if(FAILED(hr)) {
3803 heap_free(new_bits);
3804 return hr;
3807 for(i=0; i<height && i+top<bitmap->height; i++) {
3808 for(j=0; j<width && j+left<bitmap->width; j++) {
3809 DWORD *src = (DWORD*)(new_bits+i*width*4+j*4);
3810 DWORD *dst = (DWORD*)(bitmap->bits+(i+top)*bitmap->stride+(j+left)*4);
3812 if(first_frame || *src>>24 != 0)
3813 *dst = *src;
3816 heap_free(new_bits);
3817 return hr;
3820 static DWORD get_gif_background_color(GpBitmap *bitmap)
3822 BYTE bgcolor_idx = 0;
3823 UINT i;
3825 for(i=0; i<bitmap->prop_count; i++) {
3826 if(bitmap->prop_item[i].id == PropertyTagIndexBackground) {
3827 bgcolor_idx = *(BYTE*)bitmap->prop_item[i].value;
3828 break;
3832 for(i=0; i<bitmap->prop_count; i++) {
3833 if(bitmap->prop_item[i].id == PropertyTagIndexTransparent) {
3834 BYTE transparent_idx;
3835 transparent_idx = *(BYTE*)bitmap->prop_item[i].value;
3837 if(transparent_idx == bgcolor_idx)
3838 return 0;
3842 for(i=0; i<bitmap->prop_count; i++) {
3843 if(bitmap->prop_item[i].id == PropertyTagGlobalPalette) {
3844 if(bitmap->prop_item[i].length/3 > bgcolor_idx) {
3845 BYTE *color = ((BYTE*)bitmap->prop_item[i].value)+bgcolor_idx*3;
3846 return color[2] + (color[1]<<8) + (color[0]<<16) + (0xffu<<24);
3848 break;
3852 FIXME("can't get gif background color\n");
3853 return 0xffffffff;
3856 static GpStatus select_frame_gif(GpImage* image, UINT active_frame)
3858 static const WCHAR disposalW[] = {'D','i','s','p','o','s','a','l',0};
3860 GpBitmap *bitmap = (GpBitmap*)image;
3861 IWICBitmapFrameDecode *frame;
3862 int cur_frame=0, disposal;
3863 BOOL bgcolor_set = FALSE;
3864 DWORD bgcolor = 0;
3865 HRESULT hr;
3867 if(active_frame > image->current_frame) {
3868 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, image->current_frame, &frame);
3869 if(FAILED(hr))
3870 return hresult_to_status(hr);
3871 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3872 IWICBitmapFrameDecode_Release(frame);
3874 if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND)
3875 cur_frame = image->current_frame;
3876 else if(disposal != GIF_DISPOSE_RESTORE_TO_PREV)
3877 cur_frame = image->current_frame+1;
3880 while(cur_frame != active_frame) {
3881 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, cur_frame, &frame);
3882 if(FAILED(hr))
3883 return hresult_to_status(hr);
3884 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3886 if(disposal==GIF_DISPOSE_UNSPECIFIED || disposal==GIF_DISPOSE_DO_NOT_DISPOSE) {
3887 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3888 if(FAILED(hr))
3889 return hresult_to_status(hr);
3890 }else if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND) {
3891 UINT left, top, width, height, i, j;
3893 if(!bgcolor_set) {
3894 bgcolor = get_gif_background_color(bitmap);
3895 bgcolor_set = TRUE;
3898 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3899 if(FAILED(hr))
3900 return hresult_to_status(hr);
3901 for(i=top; i<top+height && i<bitmap->height; i++) {
3902 DWORD *bits = (DWORD*)(bitmap->bits+i*bitmap->stride);
3903 for(j=left; j<left+width && j<bitmap->width; j++)
3904 bits[j] = bgcolor;
3908 IWICBitmapFrameDecode_Release(frame);
3909 cur_frame++;
3912 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, active_frame, &frame);
3913 if(FAILED(hr))
3914 return hresult_to_status(hr);
3916 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3917 IWICBitmapFrameDecode_Release(frame);
3918 if(FAILED(hr))
3919 return hresult_to_status(hr);
3921 image->current_frame = active_frame;
3922 return Ok;
3925 static GpStatus decode_image_icon(IStream* stream, GpImage **image)
3927 return decode_image_wic(stream, &GUID_ContainerFormatIco, NULL, image);
3930 static GpStatus decode_image_bmp(IStream* stream, GpImage **image)
3932 GpStatus status;
3933 GpBitmap* bitmap;
3935 status = decode_image_wic(stream, &GUID_ContainerFormatBmp, NULL, image);
3937 bitmap = (GpBitmap*)*image;
3939 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3941 /* WIC supports bmp files with alpha, but gdiplus does not */
3942 bitmap->format = PixelFormat32bppRGB;
3945 return status;
3948 static GpStatus decode_image_jpeg(IStream* stream, GpImage **image)
3950 return decode_image_wic(stream, &GUID_ContainerFormatJpeg, NULL, image);
3953 static BOOL has_png_transparency_chunk(IStream *pIStream)
3955 LARGE_INTEGER seek;
3956 BOOL has_tRNS = FALSE;
3957 HRESULT hr;
3958 BYTE header[8];
3960 seek.QuadPart = 8;
3963 ULARGE_INTEGER chunk_start;
3964 ULONG bytesread, chunk_size;
3966 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, &chunk_start);
3967 if (FAILED(hr)) break;
3969 hr = IStream_Read(pIStream, header, 8, &bytesread);
3970 if (FAILED(hr) || bytesread < 8) break;
3972 chunk_size = (header[0] << 24) | (header[1] << 16) | (header[2] << 8) | header[3];
3973 if (!memcmp(&header[4], "tRNS", 4))
3975 has_tRNS = TRUE;
3976 break;
3979 seek.QuadPart = chunk_start.QuadPart + chunk_size + 12; /* skip data and CRC */
3980 } while (memcmp(&header[4], "IDAT", 4) && memcmp(&header[4], "IEND", 4));
3982 TRACE("has_tRNS = %d\n", has_tRNS);
3983 return has_tRNS;
3986 static GpStatus decode_image_png(IStream* stream, GpImage **image)
3988 IWICBitmapDecoder *decoder;
3989 IWICBitmapFrameDecode *frame;
3990 GpStatus status;
3991 HRESULT hr;
3992 GUID format;
3993 BOOL force_conversion = FALSE;
3995 status = initialize_decoder_wic(stream, &GUID_ContainerFormatPng, &decoder);
3996 if (status != Ok)
3997 return status;
3999 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
4000 if (hr == S_OK)
4002 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &format);
4003 if (hr == S_OK)
4005 if (IsEqualGUID(&format, &GUID_WICPixelFormat8bppGray))
4006 force_conversion = TRUE;
4007 else if ((IsEqualGUID(&format, &GUID_WICPixelFormat8bppIndexed) ||
4008 IsEqualGUID(&format, &GUID_WICPixelFormat4bppIndexed) ||
4009 IsEqualGUID(&format, &GUID_WICPixelFormat2bppIndexed) ||
4010 IsEqualGUID(&format, &GUID_WICPixelFormat1bppIndexed) ||
4011 IsEqualGUID(&format, &GUID_WICPixelFormat24bppBGR)) &&
4012 has_png_transparency_chunk(stream))
4013 force_conversion = TRUE;
4015 status = decode_frame_wic(decoder, force_conversion, 0, png_metadata_reader, image);
4017 else
4018 status = hresult_to_status(hr);
4020 IWICBitmapFrameDecode_Release(frame);
4022 else
4023 status = hresult_to_status(hr);
4025 IWICBitmapDecoder_Release(decoder);
4026 return status;
4029 static GpStatus decode_image_gif(IStream* stream, GpImage **image)
4031 IWICBitmapDecoder *decoder;
4032 UINT frame_count;
4033 GpStatus status;
4034 HRESULT hr;
4036 status = initialize_decoder_wic(stream, &GUID_ContainerFormatGif, &decoder);
4037 if(status != Ok)
4038 return status;
4040 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
4041 if(FAILED(hr))
4042 return hresult_to_status(hr);
4044 status = decode_frame_wic(decoder, frame_count > 1, 0, gif_metadata_reader, image);
4045 IWICBitmapDecoder_Release(decoder);
4046 if(status != Ok)
4047 return status;
4049 if(frame_count > 1) {
4050 heap_free((*image)->palette);
4051 (*image)->palette = NULL;
4053 return Ok;
4056 static GpStatus decode_image_tiff(IStream* stream, GpImage **image)
4058 return decode_image_wic(stream, &GUID_ContainerFormatTiff, NULL, image);
4061 static GpStatus load_wmf(IStream *stream, GpMetafile **metafile)
4063 WmfPlaceableFileHeader pfh;
4064 BOOL is_placeable = FALSE;
4065 LARGE_INTEGER seek;
4066 GpStatus status;
4067 METAHEADER mh;
4068 HMETAFILE hmf;
4069 HRESULT hr;
4070 UINT size;
4071 void *buf;
4073 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4074 if (hr != S_OK || size != sizeof(mh))
4075 return GenericError;
4077 if (((WmfPlaceableFileHeader *)&mh)->Key == WMF_PLACEABLE_KEY)
4079 seek.QuadPart = 0;
4080 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4081 if (FAILED(hr)) return hresult_to_status(hr);
4083 hr = IStream_Read(stream, &pfh, sizeof(pfh), &size);
4084 if (hr != S_OK || size != sizeof(pfh))
4085 return GenericError;
4087 hr = IStream_Read(stream, &mh, sizeof(mh), &size);
4088 if (hr != S_OK || size != sizeof(mh))
4089 return GenericError;
4091 is_placeable = TRUE;
4094 seek.QuadPart = is_placeable ? sizeof(pfh) : 0;
4095 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4096 if (FAILED(hr)) return hresult_to_status(hr);
4098 buf = heap_alloc(mh.mtSize * 2);
4099 if (!buf) return OutOfMemory;
4101 hr = IStream_Read(stream, buf, mh.mtSize * 2, &size);
4102 if (hr != S_OK || size != mh.mtSize * 2)
4104 heap_free(buf);
4105 return GenericError;
4108 hmf = SetMetaFileBitsEx(mh.mtSize * 2, buf);
4109 heap_free(buf);
4110 if (!hmf)
4111 return GenericError;
4113 status = GdipCreateMetafileFromWmf(hmf, TRUE, is_placeable ? &pfh : NULL, metafile);
4114 if (status != Ok)
4115 DeleteMetaFile(hmf);
4116 return status;
4119 static GpStatus decode_image_wmf(IStream *stream, GpImage **image)
4121 GpMetafile *metafile;
4122 GpStatus status;
4124 TRACE("%p %p\n", stream, image);
4126 if (!stream || !image)
4127 return InvalidParameter;
4129 status = load_wmf(stream, &metafile);
4130 if (status != Ok)
4132 TRACE("Could not load metafile\n");
4133 return status;
4136 *image = (GpImage *)metafile;
4137 TRACE("<-- %p\n", *image);
4139 return Ok;
4142 static GpStatus load_emf(IStream *stream, GpMetafile **metafile)
4144 LARGE_INTEGER seek;
4145 ENHMETAHEADER emh;
4146 HENHMETAFILE hemf;
4147 GpStatus status;
4148 HRESULT hr;
4149 UINT size;
4150 void *buf;
4152 hr = IStream_Read(stream, &emh, sizeof(emh), &size);
4153 if (hr != S_OK || size != sizeof(emh) || emh.dSignature != ENHMETA_SIGNATURE)
4154 return GenericError;
4156 seek.QuadPart = 0;
4157 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4158 if (FAILED(hr)) return hresult_to_status(hr);
4160 buf = heap_alloc(emh.nBytes);
4161 if (!buf) return OutOfMemory;
4163 hr = IStream_Read(stream, buf, emh.nBytes, &size);
4164 if (hr != S_OK || size != emh.nBytes)
4166 heap_free(buf);
4167 return GenericError;
4170 hemf = SetEnhMetaFileBits(emh.nBytes, buf);
4171 heap_free(buf);
4172 if (!hemf)
4173 return GenericError;
4175 status = GdipCreateMetafileFromEmf(hemf, TRUE, metafile);
4176 if (status != Ok)
4177 DeleteEnhMetaFile(hemf);
4178 return status;
4181 static GpStatus decode_image_emf(IStream *stream, GpImage **image)
4183 GpMetafile *metafile;
4184 GpStatus status;
4186 TRACE("%p %p\n", stream, image);
4188 if (!stream || !image)
4189 return InvalidParameter;
4191 status = load_emf(stream, &metafile);
4192 if (status != Ok)
4194 TRACE("Could not load metafile\n");
4195 return status;
4198 *image = (GpImage *)metafile;
4199 TRACE("<-- %p\n", *image);
4201 return Ok;
4204 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
4205 GDIPCONST EncoderParameters* params);
4207 typedef GpStatus (*decode_image_func)(IStream *stream, GpImage **image);
4209 typedef GpStatus (*select_image_func)(GpImage *image, UINT active_frame);
4211 typedef struct image_codec {
4212 ImageCodecInfo info;
4213 encode_image_func encode_func;
4214 decode_image_func decode_func;
4215 select_image_func select_func;
4216 } image_codec;
4218 typedef enum {
4219 BMP,
4220 JPEG,
4221 GIF,
4222 TIFF,
4223 EMF,
4224 WMF,
4225 PNG,
4226 ICO,
4227 NUM_CODECS
4228 } ImageFormat;
4230 static const struct image_codec codecs[NUM_CODECS];
4232 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
4234 BYTE signature[8];
4235 const BYTE *pattern, *mask;
4236 LARGE_INTEGER seek;
4237 HRESULT hr;
4238 UINT bytesread;
4239 int i;
4240 DWORD j, sig;
4242 /* seek to the start of the stream */
4243 seek.QuadPart = 0;
4244 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4245 if (FAILED(hr)) return hresult_to_status(hr);
4247 /* read the first 8 bytes */
4248 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4249 hr = IStream_Read(stream, signature, 8, &bytesread);
4250 if (FAILED(hr)) return hresult_to_status(hr);
4251 if (hr == S_FALSE || bytesread == 0) return GenericError;
4253 for (i = 0; i < NUM_CODECS; i++) {
4254 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4255 bytesread >= codecs[i].info.SigSize)
4257 for (sig=0; sig<codecs[i].info.SigCount; sig++)
4259 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
4260 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
4261 for (j=0; j<codecs[i].info.SigSize; j++)
4262 if ((signature[j] & mask[j]) != pattern[j])
4263 break;
4264 if (j == codecs[i].info.SigSize)
4266 *result = &codecs[i];
4267 return Ok;
4273 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
4274 signature[0],signature[1],signature[2],signature[3],
4275 signature[4],signature[5],signature[6],signature[7]);
4277 return GenericError;
4280 static GpStatus get_decoder_info_from_image(GpImage *image, const struct image_codec **result)
4282 int i;
4284 for (i = 0; i < NUM_CODECS; i++) {
4285 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4286 IsEqualIID(&codecs[i].info.FormatID, &image->format))
4288 *result = &codecs[i];
4289 return Ok;
4293 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image->format));
4294 return GenericError;
4297 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
4298 UINT frame)
4300 GpStatus stat;
4301 const struct image_codec *codec = NULL;
4302 BOOL unlock;
4304 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
4306 if (!image || !dimensionID)
4307 return InvalidParameter;
4308 if(!image_lock(image, &unlock))
4309 return ObjectBusy;
4311 if (frame >= image->frame_count)
4313 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
4314 image_unlock(image, unlock);
4315 return InvalidParameter;
4318 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
4320 WARN("invalid image type %d\n", image->type);
4321 image_unlock(image, unlock);
4322 return InvalidParameter;
4325 if (image->current_frame == frame)
4327 image_unlock(image, unlock);
4328 return Ok;
4331 if (!image->decoder)
4333 TRACE("image doesn't have an associated decoder\n");
4334 image_unlock(image, unlock);
4335 return Ok;
4338 /* choose an appropriate image decoder */
4339 stat = get_decoder_info_from_image(image, &codec);
4340 if (stat != Ok)
4342 WARN("can't find decoder info\n");
4343 image_unlock(image, unlock);
4344 return stat;
4347 stat = codec->select_func(image, frame);
4348 image_unlock(image, unlock);
4349 return stat;
4352 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
4354 GpStatus stat;
4355 LARGE_INTEGER seek;
4356 HRESULT hr;
4357 const struct image_codec *codec=NULL;
4359 TRACE("%p %p\n", stream, image);
4361 if (!stream || !image)
4362 return InvalidParameter;
4364 /* choose an appropriate image decoder */
4365 stat = get_decoder_info(stream, &codec);
4366 if (stat != Ok) return stat;
4368 /* seek to the start of the stream */
4369 seek.QuadPart = 0;
4370 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4371 if (FAILED(hr)) return hresult_to_status(hr);
4373 /* call on the image decoder to do the real work */
4374 stat = codec->decode_func(stream, image);
4376 /* take note of the original data format */
4377 if (stat == Ok)
4379 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
4380 return Ok;
4383 return stat;
4386 /* FIXME: no ICM */
4387 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
4389 TRACE("%p %p\n", stream, image);
4391 return GdipLoadImageFromStream(stream, image);
4394 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
4396 static int calls;
4398 TRACE("(%p,%u)\n", image, propId);
4400 if(!image)
4401 return InvalidParameter;
4403 if(!(calls++))
4404 FIXME("not implemented\n");
4406 return NotImplemented;
4409 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
4411 static int calls;
4413 if (!image || !item) return InvalidParameter;
4415 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
4417 if(!(calls++))
4418 FIXME("not implemented\n");
4420 return Ok;
4423 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
4424 GDIPCONST CLSID *clsidEncoder,
4425 GDIPCONST EncoderParameters *encoderParams)
4427 GpStatus stat;
4428 IStream *stream;
4430 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
4432 if (!image || !filename|| !clsidEncoder)
4433 return InvalidParameter;
4435 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4436 if (stat != Ok)
4437 return GenericError;
4439 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4441 IStream_Release(stream);
4442 return stat;
4445 /*************************************************************************
4446 * Encoding functions -
4447 * These functions encode an image in different image file formats.
4450 static GpStatus encode_image_wic(GpImage *image, IStream* stream,
4451 REFGUID container, GDIPCONST EncoderParameters* params)
4453 GpStatus stat;
4454 GpBitmap *bitmap;
4455 IWICImagingFactory *factory;
4456 IWICBitmapEncoder *encoder;
4457 IWICBitmapFrameEncode *frameencode;
4458 IPropertyBag2 *encoderoptions;
4459 HRESULT hr;
4460 UINT width, height;
4461 PixelFormat gdipformat=0;
4462 const WICPixelFormatGUID *desired_wicformat;
4463 WICPixelFormatGUID wicformat;
4464 GpRect rc;
4465 BitmapData lockeddata;
4466 UINT i;
4468 if (image->type != ImageTypeBitmap)
4469 return GenericError;
4471 bitmap = (GpBitmap*)image;
4473 GdipGetImageWidth(image, &width);
4474 GdipGetImageHeight(image, &height);
4476 rc.X = 0;
4477 rc.Y = 0;
4478 rc.Width = width;
4479 rc.Height = height;
4481 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
4482 if (FAILED(hr))
4483 return hresult_to_status(hr);
4484 hr = IWICImagingFactory_CreateEncoder(factory, container, NULL, &encoder);
4485 IWICImagingFactory_Release(factory);
4486 if (FAILED(hr))
4487 return hresult_to_status(hr);
4489 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
4491 if (SUCCEEDED(hr))
4493 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4496 if (SUCCEEDED(hr)) /* created frame */
4498 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4500 if (SUCCEEDED(hr))
4501 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4503 if (SUCCEEDED(hr))
4504 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4506 if (SUCCEEDED(hr))
4508 for (i=0; pixel_formats[i].wic_format; i++)
4510 if (pixel_formats[i].gdip_format == bitmap->format)
4512 desired_wicformat = pixel_formats[i].wic_format;
4513 gdipformat = bitmap->format;
4514 break;
4517 if (!gdipformat)
4519 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
4520 gdipformat = PixelFormat32bppARGB;
4523 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
4524 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4527 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
4529 /* Encoder doesn't support this bitmap's format. */
4530 gdipformat = 0;
4531 for (i=0; pixel_formats[i].wic_format; i++)
4533 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
4535 gdipformat = pixel_formats[i].gdip_format;
4536 break;
4539 if (!gdipformat)
4541 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
4542 hr = E_FAIL;
4546 if (SUCCEEDED(hr))
4548 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4549 &lockeddata);
4551 if (stat == Ok)
4553 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4554 BYTE *row;
4556 /* write one row at a time in case stride is negative */
4557 row = lockeddata.Scan0;
4558 for (i=0; i<lockeddata.Height; i++)
4560 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4561 if (FAILED(hr)) break;
4562 row += lockeddata.Stride;
4565 GdipBitmapUnlockBits(bitmap, &lockeddata);
4567 else
4568 hr = E_FAIL;
4571 if (SUCCEEDED(hr))
4572 hr = IWICBitmapFrameEncode_Commit(frameencode);
4574 IWICBitmapFrameEncode_Release(frameencode);
4575 IPropertyBag2_Release(encoderoptions);
4578 if (SUCCEEDED(hr))
4579 hr = IWICBitmapEncoder_Commit(encoder);
4581 IWICBitmapEncoder_Release(encoder);
4582 return hresult_to_status(hr);
4585 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4586 GDIPCONST EncoderParameters* params)
4588 return encode_image_wic(image, stream, &GUID_ContainerFormatBmp, params);
4591 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4592 GDIPCONST EncoderParameters* params)
4594 return encode_image_wic(image, stream, &GUID_ContainerFormatTiff, params);
4597 GpStatus encode_image_png(GpImage *image, IStream* stream,
4598 GDIPCONST EncoderParameters* params)
4600 return encode_image_wic(image, stream, &GUID_ContainerFormatPng, params);
4603 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4604 GDIPCONST EncoderParameters* params)
4606 return encode_image_wic(image, stream, &GUID_ContainerFormatJpeg, params);
4609 static GpStatus encode_image_gif(GpImage *image, IStream* stream,
4610 GDIPCONST EncoderParameters* params)
4612 return encode_image_wic(image, stream, &GUID_ContainerFormatGif, params);
4615 /*****************************************************************************
4616 * GdipSaveImageToStream [GDIPLUS.@]
4618 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4619 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4621 GpStatus stat;
4622 encode_image_func encode_image;
4623 int i;
4625 TRACE("%p, %p, %s, %p\n", image, stream, wine_dbgstr_guid(clsid), params);
4627 if(!image || !stream)
4628 return InvalidParameter;
4630 /* select correct encoder */
4631 encode_image = NULL;
4632 for (i = 0; i < NUM_CODECS; i++) {
4633 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4634 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4635 encode_image = codecs[i].encode_func;
4637 if (encode_image == NULL)
4638 return UnknownImageFormat;
4640 stat = encode_image(image, stream, params);
4642 return stat;
4645 /*****************************************************************************
4646 * GdipSaveAdd [GDIPLUS.@]
4648 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4650 FIXME("(%p,%p): stub\n", image, params);
4651 return Ok;
4654 /*****************************************************************************
4655 * GdipGetImagePalette [GDIPLUS.@]
4657 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4659 INT count;
4661 TRACE("(%p,%p,%i)\n", image, palette, size);
4663 if (!image || !palette)
4664 return InvalidParameter;
4666 count = image->palette ? image->palette->Count : 0;
4668 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4670 TRACE("<-- InsufficientBuffer\n");
4671 return InsufficientBuffer;
4674 if (image->palette)
4676 palette->Flags = image->palette->Flags;
4677 palette->Count = image->palette->Count;
4678 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4680 else
4682 palette->Flags = 0;
4683 palette->Count = 0;
4685 return Ok;
4688 /*****************************************************************************
4689 * GdipSetImagePalette [GDIPLUS.@]
4691 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4692 GDIPCONST ColorPalette *palette)
4694 ColorPalette *new_palette;
4696 TRACE("(%p,%p)\n", image, palette);
4698 if(!image || !palette || palette->Count > 256)
4699 return InvalidParameter;
4701 new_palette = heap_alloc_zero(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4702 if (!new_palette) return OutOfMemory;
4704 heap_free(image->palette);
4705 image->palette = new_palette;
4706 image->palette->Flags = palette->Flags;
4707 image->palette->Count = palette->Count;
4708 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4710 return Ok;
4713 /*************************************************************************
4714 * Encoders -
4715 * Structures that represent which formats we support for encoding.
4718 /* ImageCodecInfo creation routines taken from libgdiplus */
4719 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4720 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4721 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4722 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4723 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4724 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4726 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4727 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4728 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4729 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4730 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4731 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4733 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4734 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4735 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4736 static const WCHAR gif_format[] = {'G','I','F',0};
4737 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4738 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4740 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4741 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4742 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4743 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4744 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4745 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4747 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4748 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4749 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4750 static const WCHAR emf_format[] = {'E','M','F',0};
4751 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4752 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4754 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4755 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4756 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4757 static const WCHAR wmf_format[] = {'W','M','F',0};
4758 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4759 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4761 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4762 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4763 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4764 static const WCHAR png_format[] = {'P','N','G',0};
4765 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4766 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4768 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4769 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4770 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4771 static const WCHAR ico_format[] = {'I','C','O',0};
4772 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4773 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4775 static const struct image_codec codecs[NUM_CODECS] = {
4777 { /* BMP */
4778 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4779 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4780 /* CodecName */ bmp_codecname,
4781 /* DllName */ NULL,
4782 /* FormatDescription */ bmp_format,
4783 /* FilenameExtension */ bmp_extension,
4784 /* MimeType */ bmp_mimetype,
4785 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4786 /* Version */ 1,
4787 /* SigCount */ 1,
4788 /* SigSize */ 2,
4789 /* SigPattern */ bmp_sig_pattern,
4790 /* SigMask */ bmp_sig_mask,
4792 encode_image_BMP,
4793 decode_image_bmp,
4794 select_frame_wic
4797 { /* JPEG */
4798 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4799 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4800 /* CodecName */ jpeg_codecname,
4801 /* DllName */ NULL,
4802 /* FormatDescription */ jpeg_format,
4803 /* FilenameExtension */ jpeg_extension,
4804 /* MimeType */ jpeg_mimetype,
4805 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4806 /* Version */ 1,
4807 /* SigCount */ 1,
4808 /* SigSize */ 2,
4809 /* SigPattern */ jpeg_sig_pattern,
4810 /* SigMask */ jpeg_sig_mask,
4812 encode_image_jpeg,
4813 decode_image_jpeg,
4814 select_frame_wic
4817 { /* GIF */
4818 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4819 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4820 /* CodecName */ gif_codecname,
4821 /* DllName */ NULL,
4822 /* FormatDescription */ gif_format,
4823 /* FilenameExtension */ gif_extension,
4824 /* MimeType */ gif_mimetype,
4825 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4826 /* Version */ 1,
4827 /* SigCount */ 2,
4828 /* SigSize */ 6,
4829 /* SigPattern */ gif_sig_pattern,
4830 /* SigMask */ gif_sig_mask,
4832 encode_image_gif,
4833 decode_image_gif,
4834 select_frame_gif
4837 { /* TIFF */
4838 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4839 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4840 /* CodecName */ tiff_codecname,
4841 /* DllName */ NULL,
4842 /* FormatDescription */ tiff_format,
4843 /* FilenameExtension */ tiff_extension,
4844 /* MimeType */ tiff_mimetype,
4845 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4846 /* Version */ 1,
4847 /* SigCount */ 2,
4848 /* SigSize */ 4,
4849 /* SigPattern */ tiff_sig_pattern,
4850 /* SigMask */ tiff_sig_mask,
4852 encode_image_tiff,
4853 decode_image_tiff,
4854 select_frame_wic
4857 { /* EMF */
4858 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4859 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4860 /* CodecName */ emf_codecname,
4861 /* DllName */ NULL,
4862 /* FormatDescription */ emf_format,
4863 /* FilenameExtension */ emf_extension,
4864 /* MimeType */ emf_mimetype,
4865 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4866 /* Version */ 1,
4867 /* SigCount */ 1,
4868 /* SigSize */ 4,
4869 /* SigPattern */ emf_sig_pattern,
4870 /* SigMask */ emf_sig_mask,
4872 NULL,
4873 decode_image_emf,
4874 NULL
4877 { /* WMF */
4878 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4879 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4880 /* CodecName */ wmf_codecname,
4881 /* DllName */ NULL,
4882 /* FormatDescription */ wmf_format,
4883 /* FilenameExtension */ wmf_extension,
4884 /* MimeType */ wmf_mimetype,
4885 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4886 /* Version */ 1,
4887 /* SigCount */ 1,
4888 /* SigSize */ 2,
4889 /* SigPattern */ wmf_sig_pattern,
4890 /* SigMask */ wmf_sig_mask,
4892 NULL,
4893 decode_image_wmf,
4894 NULL
4897 { /* PNG */
4898 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4899 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4900 /* CodecName */ png_codecname,
4901 /* DllName */ NULL,
4902 /* FormatDescription */ png_format,
4903 /* FilenameExtension */ png_extension,
4904 /* MimeType */ png_mimetype,
4905 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4906 /* Version */ 1,
4907 /* SigCount */ 1,
4908 /* SigSize */ 8,
4909 /* SigPattern */ png_sig_pattern,
4910 /* SigMask */ png_sig_mask,
4912 encode_image_png,
4913 decode_image_png,
4914 select_frame_wic
4917 { /* ICO */
4918 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4919 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4920 /* CodecName */ ico_codecname,
4921 /* DllName */ NULL,
4922 /* FormatDescription */ ico_format,
4923 /* FilenameExtension */ ico_extension,
4924 /* MimeType */ ico_mimetype,
4925 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4926 /* Version */ 1,
4927 /* SigCount */ 1,
4928 /* SigSize */ 4,
4929 /* SigPattern */ ico_sig_pattern,
4930 /* SigMask */ ico_sig_mask,
4932 NULL,
4933 decode_image_icon,
4934 select_frame_wic
4938 /*****************************************************************************
4939 * GdipGetImageDecodersSize [GDIPLUS.@]
4941 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
4943 int decoder_count=0;
4944 int i;
4945 TRACE("%p %p\n", numDecoders, size);
4947 if (!numDecoders || !size)
4948 return InvalidParameter;
4950 for (i=0; i<NUM_CODECS; i++)
4952 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4953 decoder_count++;
4956 *numDecoders = decoder_count;
4957 *size = decoder_count * sizeof(ImageCodecInfo);
4959 return Ok;
4962 /*****************************************************************************
4963 * GdipGetImageDecoders [GDIPLUS.@]
4965 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
4967 int i, decoder_count=0;
4968 TRACE("%u %u %p\n", numDecoders, size, decoders);
4970 if (!decoders ||
4971 size != numDecoders * sizeof(ImageCodecInfo))
4972 return GenericError;
4974 for (i=0; i<NUM_CODECS; i++)
4976 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4978 if (decoder_count == numDecoders) return GenericError;
4979 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4980 decoder_count++;
4984 if (decoder_count < numDecoders) return GenericError;
4986 return Ok;
4989 /*****************************************************************************
4990 * GdipGetImageEncodersSize [GDIPLUS.@]
4992 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4994 int encoder_count=0;
4995 int i;
4996 TRACE("%p %p\n", numEncoders, size);
4998 if (!numEncoders || !size)
4999 return InvalidParameter;
5001 for (i=0; i<NUM_CODECS; i++)
5003 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
5004 encoder_count++;
5007 *numEncoders = encoder_count;
5008 *size = encoder_count * sizeof(ImageCodecInfo);
5010 return Ok;
5013 /*****************************************************************************
5014 * GdipGetImageEncoders [GDIPLUS.@]
5016 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
5018 int i, encoder_count=0;
5019 TRACE("%u %u %p\n", numEncoders, size, encoders);
5021 if (!encoders ||
5022 size != numEncoders * sizeof(ImageCodecInfo))
5023 return GenericError;
5025 for (i=0; i<NUM_CODECS; i++)
5027 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
5029 if (encoder_count == numEncoders) return GenericError;
5030 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
5031 encoder_count++;
5035 if (encoder_count < numEncoders) return GenericError;
5037 return Ok;
5040 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
5041 GDIPCONST CLSID* clsidEncoder, UINT *size)
5043 static int calls;
5045 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
5047 if(!(calls++))
5048 FIXME("not implemented\n");
5050 *size = 0;
5052 return NotImplemented;
5055 static PixelFormat get_16bpp_format(HBITMAP hbm)
5057 BITMAPV4HEADER bmh;
5058 HDC hdc;
5059 PixelFormat result;
5061 hdc = CreateCompatibleDC(NULL);
5063 memset(&bmh, 0, sizeof(bmh));
5064 bmh.bV4Size = sizeof(bmh);
5065 bmh.bV4Width = 1;
5066 bmh.bV4Height = 1;
5067 bmh.bV4V4Compression = BI_BITFIELDS;
5068 bmh.bV4BitCount = 16;
5070 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
5072 if (bmh.bV4RedMask == 0x7c00 &&
5073 bmh.bV4GreenMask == 0x3e0 &&
5074 bmh.bV4BlueMask == 0x1f)
5076 result = PixelFormat16bppRGB555;
5078 else if (bmh.bV4RedMask == 0xf800 &&
5079 bmh.bV4GreenMask == 0x7e0 &&
5080 bmh.bV4BlueMask == 0x1f)
5082 result = PixelFormat16bppRGB565;
5084 else
5086 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
5087 bmh.bV4GreenMask, bmh.bV4BlueMask);
5088 result = PixelFormatUndefined;
5091 DeleteDC(hdc);
5093 return result;
5096 /*****************************************************************************
5097 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
5099 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
5101 BITMAP bm;
5102 GpStatus retval;
5103 PixelFormat format;
5104 BitmapData lockeddata;
5106 TRACE("%p %p %p\n", hbm, hpal, bitmap);
5108 if(!hbm || !bitmap)
5109 return InvalidParameter;
5111 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
5112 return InvalidParameter;
5114 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
5115 switch(bm.bmBitsPixel) {
5116 case 1:
5117 format = PixelFormat1bppIndexed;
5118 break;
5119 case 4:
5120 format = PixelFormat4bppIndexed;
5121 break;
5122 case 8:
5123 format = PixelFormat8bppIndexed;
5124 break;
5125 case 16:
5126 format = get_16bpp_format(hbm);
5127 if (format == PixelFormatUndefined)
5128 return InvalidParameter;
5129 break;
5130 case 24:
5131 format = PixelFormat24bppRGB;
5132 break;
5133 case 32:
5134 format = PixelFormat32bppRGB;
5135 break;
5136 case 48:
5137 format = PixelFormat48bppRGB;
5138 break;
5139 default:
5140 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
5141 return InvalidParameter;
5144 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
5145 format, NULL, bitmap);
5147 if (retval == Ok)
5149 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
5150 format, &lockeddata);
5151 if (retval == Ok)
5153 HDC hdc;
5154 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
5155 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
5156 INT src_height;
5158 hdc = CreateCompatibleDC(NULL);
5160 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
5161 pbmi->bmiHeader.biBitCount = 0;
5163 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
5165 src_height = abs(pbmi->bmiHeader.biHeight);
5166 pbmi->bmiHeader.biHeight = -src_height;
5168 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
5170 DeleteDC(hdc);
5172 GdipBitmapUnlockBits(*bitmap, &lockeddata);
5175 if (retval == Ok && hpal)
5177 PALETTEENTRY entry[256];
5178 ColorPalette *palette=NULL;
5179 int i, num_palette_entries;
5181 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
5182 if (!num_palette_entries)
5183 retval = GenericError;
5185 palette = heap_alloc_zero(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
5186 if (!palette)
5187 retval = OutOfMemory;
5189 if (retval == Ok)
5191 palette->Flags = 0;
5192 palette->Count = num_palette_entries;
5194 for (i=0; i<num_palette_entries; i++)
5196 palette->Entries[i] = 0xff000000 | entry[i].peRed << 16 |
5197 entry[i].peGreen << 8 | entry[i].peBlue;
5200 retval = GdipSetImagePalette(&(*bitmap)->image, palette);
5203 heap_free(palette);
5206 if (retval != Ok)
5208 GdipDisposeImage(&(*bitmap)->image);
5209 *bitmap = NULL;
5213 return retval;
5216 /*****************************************************************************
5217 * GdipCreateEffect [GDIPLUS.@]
5219 GpStatus WINGDIPAPI GdipCreateEffect(const GUID guid, CGpEffect **effect)
5221 FIXME("(%s, %p): stub\n", debugstr_guid(&guid), effect);
5223 if(!effect)
5224 return InvalidParameter;
5226 *effect = NULL;
5228 return NotImplemented;
5231 /*****************************************************************************
5232 * GdipDeleteEffect [GDIPLUS.@]
5234 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
5236 FIXME("(%p): stub\n", effect);
5237 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5238 * in Windows's gdiplus */
5239 return NotImplemented;
5242 /*****************************************************************************
5243 * GdipSetEffectParameters [GDIPLUS.@]
5245 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
5246 const VOID *params, const UINT size)
5248 static int calls;
5250 TRACE("(%p,%p,%u)\n", effect, params, size);
5252 if(!(calls++))
5253 FIXME("not implemented\n");
5255 return NotImplemented;
5258 /*****************************************************************************
5259 * GdipGetImageFlags [GDIPLUS.@]
5261 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
5263 TRACE("%p %p\n", image, flags);
5265 if(!image || !flags)
5266 return InvalidParameter;
5268 *flags = image->flags;
5270 return Ok;
5273 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
5275 TRACE("(%d, %p)\n", control, param);
5277 switch(control){
5278 case TestControlForceBilinear:
5279 if(param)
5280 FIXME("TestControlForceBilinear not handled\n");
5281 break;
5282 case TestControlNoICM:
5283 if(param)
5284 FIXME("TestControlNoICM not handled\n");
5285 break;
5286 case TestControlGetBuildNumber:
5287 *((DWORD*)param) = 3102;
5288 break;
5291 return Ok;
5294 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
5296 TRACE("%p\n", image);
5298 return Ok;
5301 /*****************************************************************************
5302 * GdipGetImageThumbnail [GDIPLUS.@]
5304 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
5305 GpImage **ret_image, GetThumbnailImageAbort cb,
5306 VOID * cb_data)
5308 GpStatus stat;
5309 GpGraphics *graphics;
5310 UINT srcwidth, srcheight;
5312 TRACE("(%p %u %u %p %p %p)\n",
5313 image, width, height, ret_image, cb, cb_data);
5315 if (!image || !ret_image)
5316 return InvalidParameter;
5318 if (!width) width = 120;
5319 if (!height) height = 120;
5321 GdipGetImageWidth(image, &srcwidth);
5322 GdipGetImageHeight(image, &srcheight);
5324 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
5325 NULL, (GpBitmap**)ret_image);
5327 if (stat == Ok)
5329 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
5331 if (stat == Ok)
5333 stat = GdipDrawImageRectRectI(graphics, image,
5334 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
5335 NULL, NULL, NULL);
5337 GdipDeleteGraphics(graphics);
5340 if (stat != Ok)
5342 GdipDisposeImage(*ret_image);
5343 *ret_image = NULL;
5347 return stat;
5350 /*****************************************************************************
5351 * GdipImageRotateFlip [GDIPLUS.@]
5353 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
5355 GpBitmap *new_bitmap;
5356 GpBitmap *bitmap;
5357 int bpp, bytesperpixel;
5358 BOOL rotate_90, flip_x, flip_y;
5359 int src_x_offset, src_y_offset;
5360 LPBYTE src_origin;
5361 UINT x, y, width, height;
5362 BitmapData src_lock, dst_lock;
5363 GpStatus stat;
5364 BOOL unlock;
5366 TRACE("(%p, %u)\n", image, type);
5368 if (!image)
5369 return InvalidParameter;
5370 if (!image_lock(image, &unlock))
5371 return ObjectBusy;
5373 rotate_90 = type&1;
5374 flip_x = (type&6) == 2 || (type&6) == 4;
5375 flip_y = (type&3) == 1 || (type&3) == 2;
5377 if (image->type != ImageTypeBitmap)
5379 FIXME("Not implemented for type %i\n", image->type);
5380 image_unlock(image, unlock);
5381 return NotImplemented;
5384 bitmap = (GpBitmap*)image;
5385 bpp = PIXELFORMATBPP(bitmap->format);
5387 if (bpp < 8)
5389 FIXME("Not implemented for %i bit images\n", bpp);
5390 image_unlock(image, unlock);
5391 return NotImplemented;
5394 if (rotate_90)
5396 width = bitmap->height;
5397 height = bitmap->width;
5399 else
5401 width = bitmap->width;
5402 height = bitmap->height;
5405 bytesperpixel = bpp/8;
5407 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5409 if (stat != Ok)
5411 image_unlock(image, unlock);
5412 return stat;
5415 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
5417 if (stat == Ok)
5419 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5421 if (stat == Ok)
5423 LPBYTE src_row, src_pixel;
5424 LPBYTE dst_row, dst_pixel;
5426 src_origin = src_lock.Scan0;
5427 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5428 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
5430 if (rotate_90)
5432 if (flip_y) src_x_offset = -src_lock.Stride;
5433 else src_x_offset = src_lock.Stride;
5434 if (flip_x) src_y_offset = -bytesperpixel;
5435 else src_y_offset = bytesperpixel;
5437 else
5439 if (flip_x) src_x_offset = -bytesperpixel;
5440 else src_x_offset = bytesperpixel;
5441 if (flip_y) src_y_offset = -src_lock.Stride;
5442 else src_y_offset = src_lock.Stride;
5445 src_row = src_origin;
5446 dst_row = dst_lock.Scan0;
5447 for (y=0; y<height; y++)
5449 src_pixel = src_row;
5450 dst_pixel = dst_row;
5451 for (x=0; x<width; x++)
5453 /* FIXME: This could probably be faster without memcpy. */
5454 memcpy(dst_pixel, src_pixel, bytesperpixel);
5455 dst_pixel += bytesperpixel;
5456 src_pixel += src_x_offset;
5458 src_row += src_y_offset;
5459 dst_row += dst_lock.Stride;
5462 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5465 GdipBitmapUnlockBits(bitmap, &src_lock);
5468 if (stat == Ok)
5469 move_bitmap(bitmap, new_bitmap, FALSE);
5470 else
5471 GdipDisposeImage(&new_bitmap->image);
5473 image_unlock(image, unlock);
5474 return stat;
5477 /*****************************************************************************
5478 * GdipImageSetAbort [GDIPLUS.@]
5480 GpStatus WINGDIPAPI GdipImageSetAbort(GpImage *image, GdiplusAbort *pabort)
5482 TRACE("(%p, %p)\n", image, pabort);
5484 if (!image)
5485 return InvalidParameter;
5487 if (pabort)
5488 FIXME("Abort callback is not supported.\n");
5490 return Ok;
5493 /*****************************************************************************
5494 * GdipBitmapConvertFormat [GDIPLUS.@]
5496 GpStatus WINGDIPAPI GdipBitmapConvertFormat(GpBitmap *bitmap, PixelFormat format, DitherType dithertype,
5497 PaletteType palettetype, ColorPalette *palette, REAL alphathreshold)
5499 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap, format, dithertype, palettetype, palette, alphathreshold);
5500 return NotImplemented;
5503 static void set_histogram_point_argb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5505 ch0[ color >> 24 ]++;
5506 ch1[(color >> 16) & 0xff]++;
5507 ch2[(color >> 8) & 0xff]++;
5508 ch3[ color & 0xff]++;
5511 static void set_histogram_point_pargb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5513 BYTE alpha = color >> 24;
5515 ch0[alpha]++;
5516 ch1[(((color >> 16) & 0xff) * alpha) / 0xff]++;
5517 ch2[(((color >> 8) & 0xff) * alpha) / 0xff]++;
5518 ch3[(( color & 0xff) * alpha) / 0xff]++;
5521 static void set_histogram_point_rgb(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5523 ch0[(color >> 16) & 0xff]++;
5524 ch1[(color >> 8) & 0xff]++;
5525 ch2[ color & 0xff]++;
5528 static void set_histogram_point_gray(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5530 ch0[(76 * ((color >> 16) & 0xff) + 150 * ((color >> 8) & 0xff) + 29 * (color & 0xff)) / 0xff]++;
5533 static void set_histogram_point_b(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5535 ch0[color & 0xff]++;
5538 static void set_histogram_point_g(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5540 ch0[(color >> 8) & 0xff]++;
5543 static void set_histogram_point_r(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5545 ch0[(color >> 16) & 0xff]++;
5548 static void set_histogram_point_a(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5550 ch0[(color >> 24) & 0xff]++;
5553 /*****************************************************************************
5554 * GdipBitmapGetHistogram [GDIPLUS.@]
5556 GpStatus WINGDIPAPI GdipBitmapGetHistogram(GpBitmap *bitmap, HistogramFormat format, UINT num_of_entries,
5557 UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3)
5559 static void (* const set_histogram_point[])(ARGB color, UINT *ch0, UINT *ch1, UINT *ch2, UINT *ch3) =
5561 set_histogram_point_argb,
5562 set_histogram_point_pargb,
5563 set_histogram_point_rgb,
5564 set_histogram_point_gray,
5565 set_histogram_point_b,
5566 set_histogram_point_g,
5567 set_histogram_point_r,
5568 set_histogram_point_a,
5570 UINT width, height, x, y;
5572 TRACE("(%p, %d, %u, %p, %p, %p, %p)\n", bitmap, format, num_of_entries,
5573 ch0, ch1, ch2, ch3);
5575 if (!bitmap || num_of_entries != 256)
5576 return InvalidParameter;
5578 /* Make sure passed channel pointers match requested format */
5579 switch (format)
5581 case HistogramFormatARGB:
5582 case HistogramFormatPARGB:
5583 if (!ch0 || !ch1 || !ch2 || !ch3)
5584 return InvalidParameter;
5585 memset(ch0, 0, num_of_entries * sizeof(UINT));
5586 memset(ch1, 0, num_of_entries * sizeof(UINT));
5587 memset(ch2, 0, num_of_entries * sizeof(UINT));
5588 memset(ch3, 0, num_of_entries * sizeof(UINT));
5589 break;
5590 case HistogramFormatRGB:
5591 if (!ch0 || !ch1 || !ch2 || ch3)
5592 return InvalidParameter;
5593 memset(ch0, 0, num_of_entries * sizeof(UINT));
5594 memset(ch1, 0, num_of_entries * sizeof(UINT));
5595 memset(ch2, 0, num_of_entries * sizeof(UINT));
5596 break;
5597 case HistogramFormatGray:
5598 case HistogramFormatB:
5599 case HistogramFormatG:
5600 case HistogramFormatR:
5601 case HistogramFormatA:
5602 if (!ch0 || ch1 || ch2 || ch3)
5603 return InvalidParameter;
5604 memset(ch0, 0, num_of_entries * sizeof(UINT));
5605 break;
5606 default:
5607 WARN("Invalid histogram format requested, %d\n", format);
5608 return InvalidParameter;
5611 GdipGetImageWidth(&bitmap->image, &width);
5612 GdipGetImageHeight(&bitmap->image, &height);
5614 for (y = 0; y < height; y++)
5615 for (x = 0; x < width; x++)
5617 ARGB color;
5619 GdipBitmapGetPixel(bitmap, x, y, &color);
5620 set_histogram_point[format](color, ch0, ch1, ch2, ch3);
5623 return Ok;
5626 /*****************************************************************************
5627 * GdipBitmapGetHistogramSize [GDIPLUS.@]
5629 GpStatus WINGDIPAPI GdipBitmapGetHistogramSize(HistogramFormat format, UINT *num_of_entries)
5631 TRACE("(%d, %p)\n", format, num_of_entries);
5633 if (!num_of_entries)
5634 return InvalidParameter;
5636 *num_of_entries = 256;
5637 return Ok;