strmbase: Avoid a COM object to interface cast.
[wine.git] / dlls / gdiplus / image.c
blob1681099284aed449478f1b38d5861b9ed1193fba
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012 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)
47 static const struct
49 const WICPixelFormatGUID *wic_format;
50 PixelFormat gdip_format;
51 /* predefined palette type to use for pixel format conversions */
52 WICBitmapPaletteType palette_type;
53 } pixel_formats[] =
55 { &GUID_WICPixelFormatBlackWhite, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
56 { &GUID_WICPixelFormat1bppIndexed, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
57 { &GUID_WICPixelFormat8bppGray, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedGray256 },
58 { &GUID_WICPixelFormat8bppIndexed, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedHalftone256 },
59 { &GUID_WICPixelFormat16bppBGR555, PixelFormat16bppRGB555, WICBitmapPaletteTypeFixedHalftone256 },
60 { &GUID_WICPixelFormat24bppBGR, PixelFormat24bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
61 { &GUID_WICPixelFormat32bppBGR, PixelFormat32bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
62 { &GUID_WICPixelFormat32bppBGRA, PixelFormat32bppARGB, WICBitmapPaletteTypeFixedHalftone256 },
63 { &GUID_WICPixelFormat32bppPBGRA, PixelFormat32bppPARGB, WICBitmapPaletteTypeFixedHalftone256 },
64 { NULL }
67 static ColorPalette *get_palette(IWICBitmapFrameDecode *frame, WICBitmapPaletteType palette_type)
69 HRESULT hr;
70 IWICImagingFactory *factory;
71 IWICPalette *wic_palette;
72 ColorPalette *palette = NULL;
74 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
75 if (hr != S_OK) return NULL;
77 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
78 if (hr == S_OK)
80 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
81 if (frame)
82 hr = IWICBitmapFrameDecode_CopyPalette(frame, wic_palette);
83 if (hr != S_OK)
85 TRACE("using predefined palette %#x\n", palette_type);
86 hr = IWICPalette_InitializePredefined(wic_palette, palette_type, FALSE);
88 if (hr == S_OK)
90 WICBitmapPaletteType type;
91 BOOL alpha;
92 UINT count;
94 IWICPalette_GetColorCount(wic_palette, &count);
95 palette = heap_alloc(2 * sizeof(UINT) + count * sizeof(ARGB));
96 IWICPalette_GetColors(wic_palette, count, palette->Entries, &palette->Count);
98 IWICPalette_GetType(wic_palette, &type);
99 switch(type) {
100 case WICBitmapPaletteTypeFixedGray4:
101 case WICBitmapPaletteTypeFixedGray16:
102 case WICBitmapPaletteTypeFixedGray256:
103 palette->Flags = PaletteFlagsGrayScale;
104 break;
105 case WICBitmapPaletteTypeFixedHalftone8:
106 case WICBitmapPaletteTypeFixedHalftone27:
107 case WICBitmapPaletteTypeFixedHalftone64:
108 case WICBitmapPaletteTypeFixedHalftone125:
109 case WICBitmapPaletteTypeFixedHalftone216:
110 case WICBitmapPaletteTypeFixedHalftone252:
111 case WICBitmapPaletteTypeFixedHalftone256:
112 palette->Flags = PaletteFlagsHalftone;
113 break;
114 default:
115 palette->Flags = 0;
117 IWICPalette_HasAlpha(wic_palette, &alpha);
118 if(alpha)
119 palette->Flags |= PaletteFlagsHasAlpha;
121 IWICPalette_Release(wic_palette);
123 IWICImagingFactory_Release(factory);
124 return palette;
127 static INT ipicture_pixel_height(IPicture *pic)
129 HDC hdcref;
130 OLE_YSIZE_HIMETRIC y;
132 IPicture_get_Height(pic, &y);
134 hdcref = CreateCompatibleDC(0);
135 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
136 DeleteDC(hdcref);
138 return y;
141 static INT ipicture_pixel_width(IPicture *pic)
143 HDC hdcref;
144 OLE_XSIZE_HIMETRIC x;
146 IPicture_get_Width(pic, &x);
148 hdcref = CreateCompatibleDC(0);
149 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
150 DeleteDC(hdcref);
152 return x;
155 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
156 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
158 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
160 * Note: According to Jose Roca's GDI+ docs, this function is not
161 * implemented in Windows's GDI+.
163 return NotImplemented;
166 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
167 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
168 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
170 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
172 * Note: According to Jose Roca's GDI+ docs, this function is not
173 * implemented in Windows's GDI+.
175 return NotImplemented;
178 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
180 *index = (row[x/8]>>(7-x%8)) & 1;
183 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
185 if (x & 1)
186 *index = row[x/2]&0xf;
187 else
188 *index = row[x/2]>>4;
191 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
193 *index = row[x];
196 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
197 const BYTE *row, UINT x)
199 *r = *g = *b = row[x*2+1];
200 *a = 255;
203 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
204 const BYTE *row, UINT x)
206 WORD pixel = *((const WORD*)(row)+x);
207 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
208 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
209 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
210 *a = 255;
213 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
214 const BYTE *row, UINT x)
216 WORD pixel = *((const WORD*)(row)+x);
217 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
218 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
219 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
220 *a = 255;
223 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
224 const BYTE *row, UINT x)
226 WORD pixel = *((const WORD*)(row)+x);
227 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
228 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
229 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
230 if ((pixel&0x8000) == 0x8000)
231 *a = 255;
232 else
233 *a = 0;
236 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
237 const BYTE *row, UINT x)
239 *r = row[x*3+2];
240 *g = row[x*3+1];
241 *b = row[x*3];
242 *a = 255;
245 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
246 const BYTE *row, UINT x)
248 *r = row[x*4+2];
249 *g = row[x*4+1];
250 *b = row[x*4];
251 *a = 255;
254 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
255 const BYTE *row, UINT x)
257 *r = row[x*4+2];
258 *g = row[x*4+1];
259 *b = row[x*4];
260 *a = row[x*4+3];
263 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
264 const BYTE *row, UINT x)
266 *a = row[x*4+3];
267 if (*a == 0)
268 *r = *g = *b = 0;
269 else
271 *r = row[x*4+2] * 255 / *a;
272 *g = row[x*4+1] * 255 / *a;
273 *b = row[x*4] * 255 / *a;
277 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
278 const BYTE *row, UINT x)
280 *r = row[x*6+5];
281 *g = row[x*6+3];
282 *b = row[x*6+1];
283 *a = 255;
286 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
287 const BYTE *row, UINT x)
289 *r = row[x*8+5];
290 *g = row[x*8+3];
291 *b = row[x*8+1];
292 *a = row[x*8+7];
295 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
296 const BYTE *row, UINT x)
298 *a = row[x*8+7];
299 if (*a == 0)
300 *r = *g = *b = 0;
301 else
303 *r = row[x*8+5] * 255 / *a;
304 *g = row[x*8+3] * 255 / *a;
305 *b = row[x*8+1] * 255 / *a;
309 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
310 ARGB *color)
312 BYTE r, g, b, a;
313 BYTE index;
314 BYTE *row;
316 if(!bitmap || !color ||
317 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
318 return InvalidParameter;
320 row = bitmap->bits+bitmap->stride*y;
322 switch (bitmap->format)
324 case PixelFormat1bppIndexed:
325 getpixel_1bppIndexed(&index,row,x);
326 break;
327 case PixelFormat4bppIndexed:
328 getpixel_4bppIndexed(&index,row,x);
329 break;
330 case PixelFormat8bppIndexed:
331 getpixel_8bppIndexed(&index,row,x);
332 break;
333 case PixelFormat16bppGrayScale:
334 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
335 break;
336 case PixelFormat16bppRGB555:
337 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
338 break;
339 case PixelFormat16bppRGB565:
340 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
341 break;
342 case PixelFormat16bppARGB1555:
343 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
344 break;
345 case PixelFormat24bppRGB:
346 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
347 break;
348 case PixelFormat32bppRGB:
349 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
350 break;
351 case PixelFormat32bppARGB:
352 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
353 break;
354 case PixelFormat32bppPARGB:
355 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
356 break;
357 case PixelFormat48bppRGB:
358 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
359 break;
360 case PixelFormat64bppARGB:
361 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
362 break;
363 case PixelFormat64bppPARGB:
364 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
365 break;
366 default:
367 FIXME("not implemented for format 0x%x\n", bitmap->format);
368 return NotImplemented;
371 if (bitmap->format & PixelFormatIndexed)
372 *color = bitmap->image.palette->Entries[index];
373 else
374 *color = a<<24|r<<16|g<<8|b;
376 return Ok;
379 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
381 BYTE index = 0;
382 int best_distance = 0x7fff;
383 int distance;
384 UINT i;
386 if (!palette) return 0;
387 /* This algorithm scans entire palette,
388 computes difference from desired color (all color components have equal weight)
389 and returns the index of color with least difference.
391 Note: Maybe it could be replaced with a better algorithm for better image quality
392 and performance, though better algorithm would probably need some pre-built lookup
393 tables and thus may actually be slower if this method is called only few times per
394 every image.
396 for(i=0;i<palette->Count;i++) {
397 ARGB color=palette->Entries[i];
398 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
399 if (distance<best_distance) {
400 best_distance=distance;
401 index=i;
404 return index;
407 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
408 BYTE *row, UINT x, ColorPalette *palette)
410 BYTE index = get_palette_index(r,g,b,a,palette);
411 row[x]=index;
414 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
415 BYTE *row, UINT x, ColorPalette *palette)
417 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
420 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
421 BYTE *row, UINT x, ColorPalette *palette)
423 if (x & 1)
424 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
425 else
426 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
429 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
430 BYTE *row, UINT x)
432 *((WORD*)(row)+x) = (r+g+b)*85;
435 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
436 BYTE *row, UINT x)
438 *((WORD*)(row)+x) = (r<<7&0x7c00)|
439 (g<<2&0x03e0)|
440 (b>>3&0x001f);
443 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
444 BYTE *row, UINT x)
446 *((WORD*)(row)+x) = (r<<8&0xf800)|
447 (g<<3&0x07e0)|
448 (b>>3&0x001f);
451 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
452 BYTE *row, UINT x)
454 *((WORD*)(row)+x) = (a<<8&0x8000)|
455 (r<<7&0x7c00)|
456 (g<<2&0x03e0)|
457 (b>>3&0x001f);
460 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
461 BYTE *row, UINT x)
463 row[x*3+2] = r;
464 row[x*3+1] = g;
465 row[x*3] = b;
468 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
469 BYTE *row, UINT x)
471 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
474 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
475 BYTE *row, UINT x)
477 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
480 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
481 BYTE *row, UINT x)
483 r = r * a / 255;
484 g = g * a / 255;
485 b = b * a / 255;
486 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
489 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
490 BYTE *row, UINT x)
492 row[x*6+5] = row[x*6+4] = r;
493 row[x*6+3] = row[x*6+2] = g;
494 row[x*6+1] = row[x*6] = b;
497 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
498 BYTE *row, UINT x)
500 UINT64 a64=a, r64=r, g64=g, b64=b;
501 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
504 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
505 BYTE *row, UINT x)
507 UINT64 a64, r64, g64, b64;
508 a64 = a * 257;
509 r64 = r * a / 255;
510 g64 = g * a / 255;
511 b64 = b * a / 255;
512 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
515 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
516 ARGB color)
518 BYTE a, r, g, b;
519 BYTE *row;
521 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
522 return InvalidParameter;
524 a = color>>24;
525 r = color>>16;
526 g = color>>8;
527 b = color;
529 row = bitmap->bits + bitmap->stride * y;
531 switch (bitmap->format)
533 case PixelFormat16bppGrayScale:
534 setpixel_16bppGrayScale(r,g,b,a,row,x);
535 break;
536 case PixelFormat16bppRGB555:
537 setpixel_16bppRGB555(r,g,b,a,row,x);
538 break;
539 case PixelFormat16bppRGB565:
540 setpixel_16bppRGB565(r,g,b,a,row,x);
541 break;
542 case PixelFormat16bppARGB1555:
543 setpixel_16bppARGB1555(r,g,b,a,row,x);
544 break;
545 case PixelFormat24bppRGB:
546 setpixel_24bppRGB(r,g,b,a,row,x);
547 break;
548 case PixelFormat32bppRGB:
549 setpixel_32bppRGB(r,g,b,a,row,x);
550 break;
551 case PixelFormat32bppARGB:
552 setpixel_32bppARGB(r,g,b,a,row,x);
553 break;
554 case PixelFormat32bppPARGB:
555 setpixel_32bppPARGB(r,g,b,a,row,x);
556 break;
557 case PixelFormat48bppRGB:
558 setpixel_48bppRGB(r,g,b,a,row,x);
559 break;
560 case PixelFormat64bppARGB:
561 setpixel_64bppARGB(r,g,b,a,row,x);
562 break;
563 case PixelFormat64bppPARGB:
564 setpixel_64bppPARGB(r,g,b,a,row,x);
565 break;
566 case PixelFormat8bppIndexed:
567 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
568 break;
569 case PixelFormat4bppIndexed:
570 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
571 break;
572 case PixelFormat1bppIndexed:
573 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
574 break;
575 default:
576 FIXME("not implemented for format 0x%x\n", bitmap->format);
577 return NotImplemented;
580 return Ok;
583 GpStatus convert_pixels(INT width, INT height,
584 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
585 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
586 ColorPalette *palette)
588 INT x, y;
590 if (src_format == dst_format ||
591 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
593 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
594 for (y=0; y<height; y++)
595 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
596 return Ok;
599 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
600 for (y=0; y<height; y++) \
601 for (x=0; x<width; x++) { \
602 BYTE index; \
603 ARGB argb; \
604 BYTE *color = (BYTE *)&argb; \
605 getpixel_function(&index, src_bits+src_stride*y, x); \
606 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
607 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
609 return Ok; \
610 } while (0);
612 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
613 for (y=0; y<height; y++) \
614 for (x=0; x<width; x++) { \
615 BYTE r, g, b, a; \
616 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
617 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
619 return Ok; \
620 } while (0);
622 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
623 for (y=0; y<height; y++) \
624 for (x=0; x<width; x++) { \
625 BYTE r, g, b, a; \
626 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
627 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
629 return Ok; \
630 } while (0);
632 switch (src_format)
634 case PixelFormat1bppIndexed:
635 switch (dst_format)
637 case PixelFormat16bppGrayScale:
638 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
639 case PixelFormat16bppRGB555:
640 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
641 case PixelFormat16bppRGB565:
642 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
643 case PixelFormat16bppARGB1555:
644 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
645 case PixelFormat24bppRGB:
646 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
647 case PixelFormat32bppRGB:
648 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
649 case PixelFormat32bppARGB:
650 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
651 case PixelFormat32bppPARGB:
652 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
653 case PixelFormat48bppRGB:
654 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
655 case PixelFormat64bppARGB:
656 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
657 default:
658 break;
660 break;
661 case PixelFormat4bppIndexed:
662 switch (dst_format)
664 case PixelFormat16bppGrayScale:
665 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
666 case PixelFormat16bppRGB555:
667 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
668 case PixelFormat16bppRGB565:
669 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
670 case PixelFormat16bppARGB1555:
671 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
672 case PixelFormat24bppRGB:
673 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
674 case PixelFormat32bppRGB:
675 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
676 case PixelFormat32bppARGB:
677 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
678 case PixelFormat32bppPARGB:
679 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
680 case PixelFormat48bppRGB:
681 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
682 case PixelFormat64bppARGB:
683 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
684 default:
685 break;
687 break;
688 case PixelFormat8bppIndexed:
689 switch (dst_format)
691 case PixelFormat16bppGrayScale:
692 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
693 case PixelFormat16bppRGB555:
694 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
695 case PixelFormat16bppRGB565:
696 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
697 case PixelFormat16bppARGB1555:
698 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
699 case PixelFormat24bppRGB:
700 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
701 case PixelFormat32bppRGB:
702 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
703 case PixelFormat32bppARGB:
704 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
705 case PixelFormat32bppPARGB:
706 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
707 case PixelFormat48bppRGB:
708 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
709 case PixelFormat64bppARGB:
710 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
711 default:
712 break;
714 break;
715 case PixelFormat16bppGrayScale:
716 switch (dst_format)
718 case PixelFormat1bppIndexed:
719 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
720 case PixelFormat8bppIndexed:
721 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
722 case PixelFormat16bppRGB555:
723 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
724 case PixelFormat16bppRGB565:
725 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
726 case PixelFormat16bppARGB1555:
727 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
728 case PixelFormat24bppRGB:
729 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
730 case PixelFormat32bppRGB:
731 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
732 case PixelFormat32bppARGB:
733 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
734 case PixelFormat32bppPARGB:
735 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
736 case PixelFormat48bppRGB:
737 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
738 case PixelFormat64bppARGB:
739 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
740 default:
741 break;
743 break;
744 case PixelFormat16bppRGB555:
745 switch (dst_format)
747 case PixelFormat1bppIndexed:
748 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
749 case PixelFormat8bppIndexed:
750 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
751 case PixelFormat16bppGrayScale:
752 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
753 case PixelFormat16bppRGB565:
754 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
755 case PixelFormat16bppARGB1555:
756 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
757 case PixelFormat24bppRGB:
758 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
759 case PixelFormat32bppRGB:
760 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
761 case PixelFormat32bppARGB:
762 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
763 case PixelFormat32bppPARGB:
764 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
765 case PixelFormat48bppRGB:
766 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
767 case PixelFormat64bppARGB:
768 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
769 default:
770 break;
772 break;
773 case PixelFormat16bppRGB565:
774 switch (dst_format)
776 case PixelFormat1bppIndexed:
777 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
778 case PixelFormat8bppIndexed:
779 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
780 case PixelFormat16bppGrayScale:
781 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
782 case PixelFormat16bppRGB555:
783 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
784 case PixelFormat16bppARGB1555:
785 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
786 case PixelFormat24bppRGB:
787 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
788 case PixelFormat32bppRGB:
789 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
790 case PixelFormat32bppARGB:
791 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
792 case PixelFormat32bppPARGB:
793 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
794 case PixelFormat48bppRGB:
795 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
796 case PixelFormat64bppARGB:
797 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
798 default:
799 break;
801 break;
802 case PixelFormat16bppARGB1555:
803 switch (dst_format)
805 case PixelFormat1bppIndexed:
806 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
807 case PixelFormat8bppIndexed:
808 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
809 case PixelFormat16bppGrayScale:
810 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
811 case PixelFormat16bppRGB555:
812 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
813 case PixelFormat16bppRGB565:
814 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
815 case PixelFormat24bppRGB:
816 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
817 case PixelFormat32bppRGB:
818 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
819 case PixelFormat32bppARGB:
820 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
821 case PixelFormat32bppPARGB:
822 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
823 case PixelFormat48bppRGB:
824 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
825 case PixelFormat64bppARGB:
826 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
827 default:
828 break;
830 break;
831 case PixelFormat24bppRGB:
832 switch (dst_format)
834 case PixelFormat1bppIndexed:
835 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
836 case PixelFormat8bppIndexed:
837 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
838 case PixelFormat16bppGrayScale:
839 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
840 case PixelFormat16bppRGB555:
841 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
842 case PixelFormat16bppRGB565:
843 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
844 case PixelFormat16bppARGB1555:
845 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
846 case PixelFormat32bppRGB:
847 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
848 case PixelFormat32bppARGB:
849 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
850 case PixelFormat32bppPARGB:
851 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
852 case PixelFormat48bppRGB:
853 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
854 case PixelFormat64bppARGB:
855 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
856 default:
857 break;
859 break;
860 case PixelFormat32bppRGB:
861 switch (dst_format)
863 case PixelFormat1bppIndexed:
864 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
865 case PixelFormat8bppIndexed:
866 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
867 case PixelFormat16bppGrayScale:
868 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
869 case PixelFormat16bppRGB555:
870 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
871 case PixelFormat16bppRGB565:
872 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
873 case PixelFormat16bppARGB1555:
874 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
875 case PixelFormat24bppRGB:
876 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
877 case PixelFormat32bppARGB:
878 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
879 case PixelFormat32bppPARGB:
880 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
881 case PixelFormat48bppRGB:
882 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
883 case PixelFormat64bppARGB:
884 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
885 default:
886 break;
888 break;
889 case PixelFormat32bppARGB:
890 switch (dst_format)
892 case PixelFormat1bppIndexed:
893 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
894 case PixelFormat8bppIndexed:
895 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
896 case PixelFormat16bppGrayScale:
897 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
898 case PixelFormat16bppRGB555:
899 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
900 case PixelFormat16bppRGB565:
901 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
902 case PixelFormat16bppARGB1555:
903 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
904 case PixelFormat24bppRGB:
905 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
906 case PixelFormat32bppPARGB:
907 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
908 return Ok;
909 case PixelFormat48bppRGB:
910 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
911 case PixelFormat64bppARGB:
912 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
913 default:
914 break;
916 break;
917 case PixelFormat32bppPARGB:
918 switch (dst_format)
920 case PixelFormat1bppIndexed:
921 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
922 case PixelFormat8bppIndexed:
923 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
924 case PixelFormat16bppGrayScale:
925 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
926 case PixelFormat16bppRGB555:
927 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
928 case PixelFormat16bppRGB565:
929 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
930 case PixelFormat16bppARGB1555:
931 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
932 case PixelFormat24bppRGB:
933 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
934 case PixelFormat32bppRGB:
935 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
936 case PixelFormat32bppARGB:
937 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
938 case PixelFormat48bppRGB:
939 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
940 case PixelFormat64bppARGB:
941 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
942 default:
943 break;
945 break;
946 case PixelFormat48bppRGB:
947 switch (dst_format)
949 case PixelFormat1bppIndexed:
950 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
951 case PixelFormat8bppIndexed:
952 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
953 case PixelFormat16bppGrayScale:
954 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
955 case PixelFormat16bppRGB555:
956 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
957 case PixelFormat16bppRGB565:
958 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
959 case PixelFormat16bppARGB1555:
960 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
961 case PixelFormat24bppRGB:
962 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
963 case PixelFormat32bppRGB:
964 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
965 case PixelFormat32bppARGB:
966 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
967 case PixelFormat32bppPARGB:
968 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
969 case PixelFormat64bppARGB:
970 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
971 default:
972 break;
974 break;
975 case PixelFormat64bppARGB:
976 switch (dst_format)
978 case PixelFormat1bppIndexed:
979 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
980 case PixelFormat8bppIndexed:
981 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
982 case PixelFormat16bppGrayScale:
983 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
984 case PixelFormat16bppRGB555:
985 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
986 case PixelFormat16bppRGB565:
987 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
988 case PixelFormat16bppARGB1555:
989 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
990 case PixelFormat24bppRGB:
991 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
992 case PixelFormat32bppRGB:
993 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
994 case PixelFormat32bppARGB:
995 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
996 case PixelFormat32bppPARGB:
997 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
998 case PixelFormat48bppRGB:
999 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
1000 default:
1001 break;
1003 break;
1004 case PixelFormat64bppPARGB:
1005 switch (dst_format)
1007 case PixelFormat1bppIndexed:
1008 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
1009 case PixelFormat8bppIndexed:
1010 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
1011 case PixelFormat16bppGrayScale:
1012 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
1013 case PixelFormat16bppRGB555:
1014 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
1015 case PixelFormat16bppRGB565:
1016 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
1017 case PixelFormat16bppARGB1555:
1018 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
1019 case PixelFormat24bppRGB:
1020 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
1021 case PixelFormat32bppRGB:
1022 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
1023 case PixelFormat32bppARGB:
1024 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
1025 case PixelFormat32bppPARGB:
1026 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1027 case PixelFormat48bppRGB:
1028 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1029 case PixelFormat64bppARGB:
1030 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1031 default:
1032 break;
1034 break;
1035 default:
1036 break;
1039 #undef convert_indexed_to_rgb
1040 #undef convert_rgb_to_rgb
1042 return NotImplemented;
1045 /* This function returns a pointer to an array of pixels that represents the
1046 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1047 * flags. It is correct behavior that a user who calls this function with write
1048 * privileges can write to the whole bitmap (not just the area in rect).
1050 * FIXME: only used portion of format is bits per pixel. */
1051 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1052 UINT flags, PixelFormat format, BitmapData* lockeddata)
1054 INT bitspp = PIXELFORMATBPP(format);
1055 GpRect act_rect; /* actual rect to be used */
1056 GpStatus stat;
1058 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1060 if(!lockeddata || !bitmap)
1061 return InvalidParameter;
1063 if(rect){
1064 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1065 (rect->Y + rect->Height > bitmap->height) || !flags)
1066 return InvalidParameter;
1068 act_rect = *rect;
1070 else{
1071 act_rect.X = act_rect.Y = 0;
1072 act_rect.Width = bitmap->width;
1073 act_rect.Height = bitmap->height;
1076 if(bitmap->lockmode)
1078 WARN("bitmap is already locked and cannot be locked again\n");
1079 return WrongState;
1082 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1084 /* no conversion is necessary; just use the bits directly */
1085 lockeddata->Width = act_rect.Width;
1086 lockeddata->Height = act_rect.Height;
1087 lockeddata->PixelFormat = format;
1088 lockeddata->Reserved = flags;
1089 lockeddata->Stride = bitmap->stride;
1090 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1091 bitmap->stride * act_rect.Y;
1093 bitmap->lockmode = flags | ImageLockModeRead;
1094 bitmap->numlocks++;
1096 return Ok;
1099 /* Make sure we can convert to the requested format. */
1100 if (flags & ImageLockModeRead)
1102 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
1103 if (stat == NotImplemented)
1105 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1106 return NotImplemented;
1110 /* If we're opening for writing, make sure we'll be able to write back in
1111 * the original format. */
1112 if (flags & ImageLockModeWrite)
1114 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
1115 if (stat == NotImplemented)
1117 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1118 return NotImplemented;
1122 lockeddata->Width = act_rect.Width;
1123 lockeddata->Height = act_rect.Height;
1124 lockeddata->PixelFormat = format;
1125 lockeddata->Reserved = flags;
1127 if(!(flags & ImageLockModeUserInputBuf))
1129 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1131 bitmap->bitmapbits = heap_alloc_zero(lockeddata->Stride * act_rect.Height);
1133 if (!bitmap->bitmapbits) return OutOfMemory;
1135 lockeddata->Scan0 = bitmap->bitmapbits;
1138 if (flags & ImageLockModeRead)
1140 static BOOL fixme = FALSE;
1142 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1144 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1145 fixme = TRUE;
1148 stat = convert_pixels(act_rect.Width, act_rect.Height,
1149 lockeddata->Stride, lockeddata->Scan0, format,
1150 bitmap->stride,
1151 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1152 bitmap->format, bitmap->image.palette);
1154 if (stat != Ok)
1156 heap_free(bitmap->bitmapbits);
1157 bitmap->bitmapbits = NULL;
1158 return stat;
1162 bitmap->lockmode = flags | ImageLockModeRead;
1163 bitmap->numlocks++;
1164 bitmap->lockx = act_rect.X;
1165 bitmap->locky = act_rect.Y;
1167 return Ok;
1170 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1172 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1174 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1175 return InvalidParameter;
1177 bitmap->image.xres = xdpi;
1178 bitmap->image.yres = ydpi;
1180 return Ok;
1183 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1184 BitmapData* lockeddata)
1186 GpStatus stat;
1187 static BOOL fixme = FALSE;
1189 TRACE("(%p,%p)\n", bitmap, lockeddata);
1191 if(!bitmap || !lockeddata)
1192 return InvalidParameter;
1194 if(!bitmap->lockmode)
1195 return WrongState;
1197 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1198 if(!(--bitmap->numlocks))
1199 bitmap->lockmode = 0;
1201 heap_free(bitmap->bitmapbits);
1202 bitmap->bitmapbits = NULL;
1203 return Ok;
1206 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1208 /* we passed a direct reference; no need to do anything */
1209 bitmap->lockmode = 0;
1210 bitmap->numlocks = 0;
1211 return Ok;
1214 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1216 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1217 fixme = TRUE;
1220 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1221 bitmap->stride,
1222 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1223 bitmap->format,
1224 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1226 if (stat != Ok)
1228 ERR("failed to convert pixels; this should never happen\n");
1231 heap_free(bitmap->bitmapbits);
1232 bitmap->bitmapbits = NULL;
1233 bitmap->lockmode = 0;
1234 bitmap->numlocks = 0;
1236 return stat;
1239 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1240 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1242 Rect area;
1243 GpStatus stat;
1245 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1247 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1248 x < 0 || y < 0 ||
1249 x + width > srcBitmap->width || y + height > srcBitmap->height)
1251 TRACE("<-- InvalidParameter\n");
1252 return InvalidParameter;
1255 if (format == PixelFormatDontCare)
1256 format = srcBitmap->format;
1258 area.X = gdip_round(x);
1259 area.Y = gdip_round(y);
1260 area.Width = gdip_round(width);
1261 area.Height = gdip_round(height);
1263 stat = GdipCreateBitmapFromScan0(area.Width, area.Height, 0, format, NULL, dstBitmap);
1264 if (stat == Ok)
1266 stat = convert_pixels(area.Width, area.Height, (*dstBitmap)->stride, (*dstBitmap)->bits, (*dstBitmap)->format,
1267 srcBitmap->stride,
1268 srcBitmap->bits + srcBitmap->stride * area.Y + PIXELFORMATBPP(srcBitmap->format) * area.X / 8,
1269 srcBitmap->format, srcBitmap->image.palette);
1271 if (stat == Ok && srcBitmap->image.palette)
1273 ColorPalette *src_palette, *dst_palette;
1275 src_palette = srcBitmap->image.palette;
1277 dst_palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * src_palette->Count);
1279 if (dst_palette)
1281 dst_palette->Flags = src_palette->Flags;
1282 dst_palette->Count = src_palette->Count;
1283 memcpy(dst_palette->Entries, src_palette->Entries, sizeof(ARGB) * src_palette->Count);
1285 heap_free((*dstBitmap)->image.palette);
1286 (*dstBitmap)->image.palette = dst_palette;
1288 else
1289 stat = OutOfMemory;
1292 if (stat != Ok)
1293 GdipDisposeImage((GpImage*)*dstBitmap);
1296 if (stat != Ok)
1297 *dstBitmap = NULL;
1299 return stat;
1302 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1303 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1305 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1307 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1310 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1312 GpStatus stat = GenericError;
1314 TRACE("%p, %p\n", image, cloneImage);
1316 if (!image || !cloneImage)
1317 return InvalidParameter;
1319 if (image->picture)
1321 IStream* stream;
1322 HRESULT hr;
1323 INT size;
1324 LARGE_INTEGER move;
1326 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1327 if (FAILED(hr))
1328 return GenericError;
1330 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1331 if(FAILED(hr))
1333 WARN("Failed to save image on stream\n");
1334 goto out;
1337 /* Set seek pointer back to the beginning of the picture */
1338 move.QuadPart = 0;
1339 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1340 if (FAILED(hr))
1341 goto out;
1343 stat = GdipLoadImageFromStream(stream, cloneImage);
1344 if (stat != Ok) WARN("Failed to load image from stream\n");
1346 out:
1347 IStream_Release(stream);
1348 return stat;
1350 else if (image->type == ImageTypeBitmap)
1352 GpBitmap *bitmap = (GpBitmap *)image;
1354 return GdipCloneBitmapAreaI(0, 0, bitmap->width, bitmap->height,
1355 bitmap->format, bitmap, (GpBitmap **)cloneImage);
1357 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
1359 GpMetafile *result, *metafile;
1361 metafile = (GpMetafile*)image;
1363 result = heap_alloc_zero(sizeof(*result));
1364 if (!result)
1365 return OutOfMemory;
1367 result->image.type = ImageTypeMetafile;
1368 result->image.format = image->format;
1369 result->image.flags = image->flags;
1370 result->image.frame_count = 1;
1371 result->image.xres = image->xres;
1372 result->image.yres = image->yres;
1373 result->bounds = metafile->bounds;
1374 result->unit = metafile->unit;
1375 result->metafile_type = metafile->metafile_type;
1376 result->hemf = CopyEnhMetaFileW(metafile->hemf, NULL);
1378 if (!result->hemf)
1380 heap_free(result);
1381 return OutOfMemory;
1384 *cloneImage = &result->image;
1385 return Ok;
1387 else
1389 WARN("GpImage with no image data (metafile in wrong state?)\n");
1390 return InvalidParameter;
1394 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1395 GpBitmap **bitmap)
1397 GpStatus stat;
1398 IStream *stream;
1400 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1402 if(!filename || !bitmap)
1403 return InvalidParameter;
1405 *bitmap = NULL;
1407 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1409 if(stat != Ok)
1410 return stat;
1412 stat = GdipCreateBitmapFromStream(stream, bitmap);
1414 IStream_Release(stream);
1416 return stat;
1419 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1420 VOID *bits, GpBitmap **bitmap)
1422 DWORD height, stride;
1423 PixelFormat format;
1425 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1427 if (!info || !bits || !bitmap)
1428 return InvalidParameter;
1430 height = abs(info->bmiHeader.biHeight);
1431 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1433 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1435 bits = (BYTE*)bits + (height - 1) * stride;
1436 stride = -stride;
1439 switch(info->bmiHeader.biBitCount) {
1440 case 1:
1441 format = PixelFormat1bppIndexed;
1442 break;
1443 case 4:
1444 format = PixelFormat4bppIndexed;
1445 break;
1446 case 8:
1447 format = PixelFormat8bppIndexed;
1448 break;
1449 case 16:
1450 format = PixelFormat16bppRGB555;
1451 break;
1452 case 24:
1453 format = PixelFormat24bppRGB;
1454 break;
1455 case 32:
1456 format = PixelFormat32bppRGB;
1457 break;
1458 default:
1459 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1460 *bitmap = NULL;
1461 return InvalidParameter;
1464 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1465 bits, bitmap);
1469 /* FIXME: no icm */
1470 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1471 GpBitmap **bitmap)
1473 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1475 return GdipCreateBitmapFromFile(filename, bitmap);
1478 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1479 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1481 HBITMAP hbm;
1482 GpStatus stat = InvalidParameter;
1484 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1486 if(!lpBitmapName || !bitmap)
1487 return InvalidParameter;
1489 /* load DIB */
1490 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1491 LR_CREATEDIBSECTION);
1493 if(hbm){
1494 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1495 DeleteObject(hbm);
1498 return stat;
1501 static inline DWORD blend_argb_no_bkgnd_alpha(DWORD src, DWORD bkgnd)
1503 BYTE b = (BYTE)src;
1504 BYTE g = (BYTE)(src >> 8);
1505 BYTE r = (BYTE)(src >> 16);
1506 DWORD alpha = (BYTE)(src >> 24);
1507 return ((b + ((BYTE)bkgnd * (255 - alpha) + 127) / 255) |
1508 (g + ((BYTE)(bkgnd >> 8) * (255 - alpha) + 127) / 255) << 8 |
1509 (r + ((BYTE)(bkgnd >> 16) * (255 - alpha) + 127) / 255) << 16 |
1510 (alpha << 24));
1513 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1514 HBITMAP* hbmReturn, ARGB background)
1516 GpStatus stat;
1517 HBITMAP result;
1518 UINT width, height;
1519 BITMAPINFOHEADER bih;
1520 LPBYTE bits;
1521 BitmapData lockeddata;
1522 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1524 if (!bitmap || !hbmReturn) return InvalidParameter;
1526 GdipGetImageWidth((GpImage*)bitmap, &width);
1527 GdipGetImageHeight((GpImage*)bitmap, &height);
1529 bih.biSize = sizeof(bih);
1530 bih.biWidth = width;
1531 bih.biHeight = height;
1532 bih.biPlanes = 1;
1533 bih.biBitCount = 32;
1534 bih.biCompression = BI_RGB;
1535 bih.biSizeImage = 0;
1536 bih.biXPelsPerMeter = 0;
1537 bih.biYPelsPerMeter = 0;
1538 bih.biClrUsed = 0;
1539 bih.biClrImportant = 0;
1541 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1543 if (result)
1545 lockeddata.Stride = -width * 4;
1546 lockeddata.Scan0 = bits + (width * 4 * (height - 1));
1548 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead|ImageLockModeUserInputBuf,
1549 PixelFormat32bppPARGB, &lockeddata);
1551 if (stat == Ok)
1552 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1554 if (stat == Ok && (background & 0xffffff))
1556 DWORD *ptr;
1557 UINT i;
1558 for (ptr = (DWORD*)bits, i = 0; i < width * height; ptr++, i++)
1560 if ((*ptr & 0xff000000) == 0xff000000) continue;
1561 *ptr = blend_argb_no_bkgnd_alpha(*ptr, background);
1565 else
1566 stat = GenericError;
1568 if (stat != Ok && result)
1570 DeleteObject(result);
1571 result = NULL;
1574 *hbmReturn = result;
1576 return stat;
1579 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1580 GpGraphics* target, GpBitmap** bitmap)
1582 GpStatus ret;
1584 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1586 if(!target || !bitmap)
1587 return InvalidParameter;
1589 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
1590 NULL, bitmap);
1592 if (ret == Ok)
1594 GdipGetDpiX(target, &(*bitmap)->image.xres);
1595 GdipGetDpiY(target, &(*bitmap)->image.yres);
1598 return ret;
1601 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1603 GpStatus stat;
1604 ICONINFO iinfo;
1605 BITMAP bm;
1606 int ret;
1607 UINT width, height, stride;
1608 GpRect rect;
1609 BitmapData lockeddata;
1610 HDC screendc;
1611 BOOL has_alpha;
1612 int x, y;
1613 BITMAPINFOHEADER bih;
1614 DWORD *src;
1615 BYTE *dst_row;
1616 DWORD *dst;
1618 TRACE("%p, %p\n", hicon, bitmap);
1620 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1621 return InvalidParameter;
1623 /* get the size of the icon */
1624 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1625 if (ret == 0) {
1626 DeleteObject(iinfo.hbmColor);
1627 DeleteObject(iinfo.hbmMask);
1628 return GenericError;
1631 width = bm.bmWidth;
1632 height = iinfo.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
1633 stride = width * 4;
1635 stat = GdipCreateBitmapFromScan0(width, height, stride, PixelFormat32bppARGB, NULL, bitmap);
1636 if (stat != Ok) {
1637 DeleteObject(iinfo.hbmColor);
1638 DeleteObject(iinfo.hbmMask);
1639 return stat;
1642 rect.X = 0;
1643 rect.Y = 0;
1644 rect.Width = width;
1645 rect.Height = height;
1647 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1648 if (stat != Ok) {
1649 DeleteObject(iinfo.hbmColor);
1650 DeleteObject(iinfo.hbmMask);
1651 GdipDisposeImage((GpImage*)*bitmap);
1652 return stat;
1655 bih.biSize = sizeof(bih);
1656 bih.biWidth = width;
1657 bih.biHeight = iinfo.hbmColor ? -height: -height * 2;
1658 bih.biPlanes = 1;
1659 bih.biBitCount = 32;
1660 bih.biCompression = BI_RGB;
1661 bih.biSizeImage = 0;
1662 bih.biXPelsPerMeter = 0;
1663 bih.biYPelsPerMeter = 0;
1664 bih.biClrUsed = 0;
1665 bih.biClrImportant = 0;
1667 screendc = CreateCompatibleDC(0);
1668 if (iinfo.hbmColor)
1670 GetDIBits(screendc, iinfo.hbmColor, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1672 if (bm.bmBitsPixel == 32)
1674 has_alpha = FALSE;
1676 /* If any pixel has a non-zero alpha, ignore hbmMask */
1677 src = (DWORD*)lockeddata.Scan0;
1678 for (x=0; x<width && !has_alpha; x++)
1679 for (y=0; y<height && !has_alpha; y++)
1680 if ((*src++ & 0xff000000) != 0)
1681 has_alpha = TRUE;
1683 else has_alpha = FALSE;
1685 else
1687 GetDIBits(screendc, iinfo.hbmMask, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1688 has_alpha = FALSE;
1691 if (!has_alpha)
1693 if (iinfo.hbmMask)
1695 BYTE *bits = heap_alloc(height * stride);
1697 /* read alpha data from the mask */
1698 if (iinfo.hbmColor)
1699 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1700 else
1701 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1703 src = (DWORD*)bits;
1704 dst_row = lockeddata.Scan0;
1705 for (y=0; y<height; y++)
1707 dst = (DWORD*)dst_row;
1708 for (x=0; x<height; x++)
1710 DWORD src_value = *src++;
1711 if (src_value)
1712 *dst++ = 0;
1713 else
1714 *dst++ |= 0xff000000;
1716 dst_row += lockeddata.Stride;
1719 heap_free(bits);
1721 else
1723 /* set constant alpha of 255 */
1724 dst_row = lockeddata.Scan0;
1725 for (y=0; y<height; y++)
1727 dst = (DWORD*)dst_row;
1728 for (x=0; x<height; x++)
1729 *dst++ |= 0xff000000;
1730 dst_row += lockeddata.Stride;
1735 DeleteDC(screendc);
1737 DeleteObject(iinfo.hbmColor);
1738 DeleteObject(iinfo.hbmMask);
1740 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1742 return Ok;
1745 static void generate_halftone_palette(ARGB *entries, UINT count)
1747 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1748 UINT i;
1750 for (i=0; i<8 && i<count; i++)
1752 entries[i] = 0xff000000;
1753 if (i&1) entries[i] |= 0x800000;
1754 if (i&2) entries[i] |= 0x8000;
1755 if (i&4) entries[i] |= 0x80;
1758 if (8 < count)
1759 entries[i] = 0xffc0c0c0;
1761 for (i=9; i<16 && i<count; i++)
1763 entries[i] = 0xff000000;
1764 if (i&1) entries[i] |= 0xff0000;
1765 if (i&2) entries[i] |= 0xff00;
1766 if (i&4) entries[i] |= 0xff;
1769 for (i=16; i<40 && i<count; i++)
1771 entries[i] = 0;
1774 for (i=40; i<256 && i<count; i++)
1776 entries[i] = 0xff000000;
1777 entries[i] |= halftone_values[(i-40)%6];
1778 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1779 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1783 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1785 HDC screendc = CreateCompatibleDC(0);
1787 if (!screendc) return GenericError;
1789 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1790 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1792 DeleteDC(screendc);
1794 return Ok;
1797 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1798 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1800 HBITMAP hbitmap=NULL;
1801 INT row_size, dib_stride;
1802 BYTE *bits=NULL, *own_bits=NULL;
1803 REAL xres, yres;
1804 GpStatus stat;
1806 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1808 if (!bitmap) return InvalidParameter;
1810 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1811 *bitmap = NULL;
1812 return InvalidParameter;
1815 if(scan0 && !stride)
1816 return InvalidParameter;
1818 stat = get_screen_resolution(&xres, &yres);
1819 if (stat != Ok) return stat;
1821 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1822 dib_stride = (row_size + 3) & ~3;
1824 if(stride == 0)
1825 stride = dib_stride;
1827 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1829 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
1830 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
1832 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1833 pbmi->bmiHeader.biWidth = width;
1834 pbmi->bmiHeader.biHeight = -height;
1835 pbmi->bmiHeader.biPlanes = 1;
1836 /* FIXME: use the rest of the data from format */
1837 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1838 pbmi->bmiHeader.biCompression = BI_RGB;
1839 pbmi->bmiHeader.biSizeImage = 0;
1840 pbmi->bmiHeader.biXPelsPerMeter = 0;
1841 pbmi->bmiHeader.biYPelsPerMeter = 0;
1842 pbmi->bmiHeader.biClrUsed = 0;
1843 pbmi->bmiHeader.biClrImportant = 0;
1845 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1847 if (!hbitmap) return GenericError;
1849 stride = dib_stride;
1851 else
1853 /* Not a GDI format; don't try to make an HBITMAP. */
1854 if (scan0)
1855 bits = scan0;
1856 else
1858 INT size = abs(stride) * height;
1860 own_bits = bits = heap_alloc_zero(size);
1861 if (!own_bits) return OutOfMemory;
1863 if (stride < 0)
1864 bits += stride * (1 - height);
1868 *bitmap = heap_alloc_zero(sizeof(GpBitmap));
1869 if(!*bitmap)
1871 DeleteObject(hbitmap);
1872 heap_free(own_bits);
1873 return OutOfMemory;
1876 (*bitmap)->image.type = ImageTypeBitmap;
1877 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1878 (*bitmap)->image.flags = ImageFlagsNone;
1879 (*bitmap)->image.frame_count = 1;
1880 (*bitmap)->image.current_frame = 0;
1881 (*bitmap)->image.palette = NULL;
1882 (*bitmap)->image.xres = xres;
1883 (*bitmap)->image.yres = yres;
1884 (*bitmap)->width = width;
1885 (*bitmap)->height = height;
1886 (*bitmap)->format = format;
1887 (*bitmap)->image.picture = NULL;
1888 (*bitmap)->image.decoder = NULL;
1889 (*bitmap)->hbitmap = hbitmap;
1890 (*bitmap)->hdc = NULL;
1891 (*bitmap)->bits = bits;
1892 (*bitmap)->stride = stride;
1893 (*bitmap)->own_bits = own_bits;
1894 (*bitmap)->metadata_reader = NULL;
1895 (*bitmap)->prop_count = 0;
1896 (*bitmap)->prop_item = NULL;
1898 /* set format-related flags */
1899 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1900 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1902 if (format == PixelFormat1bppIndexed ||
1903 format == PixelFormat4bppIndexed ||
1904 format == PixelFormat8bppIndexed)
1906 (*bitmap)->image.palette = heap_alloc_zero(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1908 if (!(*bitmap)->image.palette)
1910 GdipDisposeImage(&(*bitmap)->image);
1911 *bitmap = NULL;
1912 return OutOfMemory;
1915 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1917 if (format == PixelFormat1bppIndexed)
1919 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1920 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1921 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1923 else
1925 if (format == PixelFormat8bppIndexed)
1926 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1928 generate_halftone_palette((*bitmap)->image.palette->Entries,
1929 (*bitmap)->image.palette->Count);
1933 TRACE("<-- %p\n", *bitmap);
1935 return Ok;
1938 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1939 GpBitmap **bitmap)
1941 GpStatus stat;
1943 TRACE("%p %p\n", stream, bitmap);
1945 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1947 if(stat != Ok)
1948 return stat;
1950 if((*bitmap)->image.type != ImageTypeBitmap){
1951 GdipDisposeImage(&(*bitmap)->image);
1952 *bitmap = NULL;
1953 return GenericError; /* FIXME: what error to return? */
1956 return Ok;
1959 /* FIXME: no icm */
1960 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1961 GpBitmap **bitmap)
1963 TRACE("%p %p\n", stream, bitmap);
1965 return GdipCreateBitmapFromStream(stream, bitmap);
1968 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1969 GpCachedBitmap **cachedbmp)
1971 GpStatus stat;
1973 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1975 if(!bitmap || !graphics || !cachedbmp)
1976 return InvalidParameter;
1978 *cachedbmp = heap_alloc_zero(sizeof(GpCachedBitmap));
1979 if(!*cachedbmp)
1980 return OutOfMemory;
1982 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1983 if(stat != Ok){
1984 heap_free(*cachedbmp);
1985 return stat;
1988 return Ok;
1991 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1993 GpStatus stat;
1994 BitmapData lockeddata;
1995 ULONG andstride, xorstride, bitssize;
1996 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1997 UINT x, y;
1999 TRACE("(%p, %p)\n", bitmap, hicon);
2001 if (!bitmap || !hicon)
2002 return InvalidParameter;
2004 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
2005 PixelFormat32bppPARGB, &lockeddata);
2006 if (stat == Ok)
2008 andstride = ((lockeddata.Width+31)/32)*4;
2009 xorstride = lockeddata.Width*4;
2010 bitssize = (andstride + xorstride) * lockeddata.Height;
2012 andbits = heap_alloc_zero(bitssize);
2014 if (andbits)
2016 xorbits = andbits + andstride * lockeddata.Height;
2018 for (y=0; y<lockeddata.Height; y++)
2020 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
2022 androw = andbits + andstride * y;
2023 for (x=0; x<lockeddata.Width; x++)
2024 if (srcrow[3+4*x] >= 128)
2025 androw[x/8] |= 1 << (7-x%8);
2027 xorrow = xorbits + xorstride * y;
2028 memcpy(xorrow, srcrow, xorstride);
2031 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
2032 andbits, xorbits);
2034 heap_free(andbits);
2036 else
2037 stat = OutOfMemory;
2039 GdipBitmapUnlockBits(bitmap, &lockeddata);
2042 return stat;
2045 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
2047 TRACE("%p\n", cachedbmp);
2049 if(!cachedbmp)
2050 return InvalidParameter;
2052 GdipDisposeImage(cachedbmp->image);
2053 heap_free(cachedbmp);
2055 return Ok;
2058 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2059 GpCachedBitmap *cachedbmp, INT x, INT y)
2061 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2063 if(!graphics || !cachedbmp)
2064 return InvalidParameter;
2066 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2069 /* Internal utility function: Replace the image data of dst with that of src,
2070 * and free src. */
2071 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2073 assert(src->image.type == ImageTypeBitmap);
2074 assert(dst->image.type == ImageTypeBitmap);
2076 heap_free(dst->bitmapbits);
2077 heap_free(dst->own_bits);
2078 DeleteDC(dst->hdc);
2079 DeleteObject(dst->hbitmap);
2081 if (clobber_palette)
2083 heap_free(dst->image.palette);
2084 dst->image.palette = src->image.palette;
2086 else
2087 heap_free(src->image.palette);
2089 dst->image.xres = src->image.xres;
2090 dst->image.yres = src->image.yres;
2091 dst->width = src->width;
2092 dst->height = src->height;
2093 dst->format = src->format;
2094 dst->hbitmap = src->hbitmap;
2095 dst->hdc = src->hdc;
2096 dst->bits = src->bits;
2097 dst->stride = src->stride;
2098 dst->own_bits = src->own_bits;
2099 if (dst->metadata_reader)
2100 IWICMetadataReader_Release(dst->metadata_reader);
2101 dst->metadata_reader = src->metadata_reader;
2102 heap_free(dst->prop_item);
2103 dst->prop_item = src->prop_item;
2104 dst->prop_count = src->prop_count;
2105 if (dst->image.decoder)
2106 IWICBitmapDecoder_Release(dst->image.decoder);
2107 dst->image.decoder = src->image.decoder;
2108 dst->image.frame_count = src->image.frame_count;
2109 dst->image.current_frame = src->image.current_frame;
2110 dst->image.format = src->image.format;
2112 src->image.type = ~0;
2113 heap_free(src);
2116 static GpStatus free_image_data(GpImage *image)
2118 if(!image)
2119 return InvalidParameter;
2121 if (image->type == ImageTypeBitmap)
2123 heap_free(((GpBitmap*)image)->bitmapbits);
2124 heap_free(((GpBitmap*)image)->own_bits);
2125 DeleteDC(((GpBitmap*)image)->hdc);
2126 DeleteObject(((GpBitmap*)image)->hbitmap);
2127 if (((GpBitmap*)image)->metadata_reader)
2128 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2129 heap_free(((GpBitmap*)image)->prop_item);
2131 else if (image->type == ImageTypeMetafile)
2133 GpMetafile *metafile = (GpMetafile*)image;
2134 heap_free(metafile->comment_data);
2135 DeleteEnhMetaFile(CloseEnhMetaFile(metafile->record_dc));
2136 if (!metafile->preserve_hemf)
2137 DeleteEnhMetaFile(metafile->hemf);
2138 if (metafile->record_graphics)
2140 WARN("metafile closed while recording\n");
2141 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2142 metafile->record_graphics->image = NULL;
2143 metafile->record_graphics->busy = TRUE;
2145 if (metafile->record_stream)
2147 IStream_Release(metafile->record_stream);
2150 else
2152 WARN("invalid image: %p\n", image);
2153 return ObjectBusy;
2155 if (image->picture)
2156 IPicture_Release(image->picture);
2157 if (image->decoder)
2158 IWICBitmapDecoder_Release(image->decoder);
2159 heap_free(image->palette);
2161 return Ok;
2164 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2166 GpStatus status;
2168 TRACE("%p\n", image);
2170 status = free_image_data(image);
2171 if (status != Ok) return status;
2172 image->type = ~0;
2173 heap_free(image);
2175 return Ok;
2178 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2180 static int calls;
2182 TRACE("(%p,%p)\n", image, item);
2184 if(!image || !item)
2185 return InvalidParameter;
2187 if (!(calls++))
2188 FIXME("not implemented\n");
2190 return NotImplemented;
2193 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2195 static int calls;
2197 TRACE("(%p,%p)\n", image, item);
2199 if (!(calls++))
2200 FIXME("not implemented\n");
2202 return NotImplemented;
2205 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2206 GpUnit *srcUnit)
2208 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2210 if(!image || !srcRect || !srcUnit)
2211 return InvalidParameter;
2212 if(image->type == ImageTypeMetafile){
2213 *srcRect = ((GpMetafile*)image)->bounds;
2214 *srcUnit = ((GpMetafile*)image)->unit;
2216 else if(image->type == ImageTypeBitmap){
2217 srcRect->X = srcRect->Y = 0.0;
2218 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2219 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2220 *srcUnit = UnitPixel;
2222 else{
2223 srcRect->X = srcRect->Y = 0.0;
2224 srcRect->Width = ipicture_pixel_width(image->picture);
2225 srcRect->Height = ipicture_pixel_height(image->picture);
2226 *srcUnit = UnitPixel;
2229 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2230 srcRect->Width, srcRect->Height, *srcUnit);
2232 return Ok;
2235 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2236 REAL *height)
2238 TRACE("%p %p %p\n", image, width, height);
2240 if(!image || !height || !width)
2241 return InvalidParameter;
2243 if(image->type == ImageTypeMetafile){
2244 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2245 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2247 else if(image->type == ImageTypeBitmap){
2248 *height = ((GpBitmap*)image)->height;
2249 *width = ((GpBitmap*)image)->width;
2251 else{
2252 *height = ipicture_pixel_height(image->picture);
2253 *width = ipicture_pixel_width(image->picture);
2256 TRACE("returning (%f, %f)\n", *height, *width);
2257 return Ok;
2260 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2261 GpGraphics **graphics)
2263 HDC hdc;
2264 GpStatus stat;
2266 TRACE("%p %p\n", image, graphics);
2268 if(!image || !graphics)
2269 return InvalidParameter;
2271 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2273 hdc = ((GpBitmap*)image)->hdc;
2275 if(!hdc){
2276 hdc = CreateCompatibleDC(0);
2277 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2278 ((GpBitmap*)image)->hdc = hdc;
2281 stat = GdipCreateFromHDC(hdc, graphics);
2283 if (stat == Ok)
2285 (*graphics)->image = image;
2286 (*graphics)->xres = image->xres;
2287 (*graphics)->yres = image->yres;
2290 else if (image->type == ImageTypeMetafile)
2291 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2292 else
2293 stat = graphics_from_image(image, graphics);
2295 return stat;
2298 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2300 TRACE("%p %p\n", image, height);
2302 if(!image || !height)
2303 return InvalidParameter;
2305 if(image->type == ImageTypeMetafile)
2306 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2307 else if(image->type == ImageTypeBitmap)
2308 *height = ((GpBitmap*)image)->height;
2309 else
2310 *height = ipicture_pixel_height(image->picture);
2312 TRACE("returning %d\n", *height);
2314 return Ok;
2317 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2319 if(!image || !res)
2320 return InvalidParameter;
2322 *res = image->xres;
2324 TRACE("(%p) <-- %0.2f\n", image, *res);
2326 return Ok;
2329 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2331 TRACE("%p %p\n", image, size);
2333 if(!image || !size)
2334 return InvalidParameter;
2336 if (!image->palette || image->palette->Count == 0)
2337 *size = sizeof(ColorPalette);
2338 else
2339 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2341 TRACE("<-- %u\n", *size);
2343 return Ok;
2346 /* FIXME: test this function for non-bitmap types */
2347 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2349 TRACE("%p %p\n", image, format);
2351 if(!image || !format)
2352 return InvalidParameter;
2354 if(image->type != ImageTypeBitmap)
2355 *format = PixelFormat24bppRGB;
2356 else
2357 *format = ((GpBitmap*) image)->format;
2359 return Ok;
2362 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2364 TRACE("(%p, %p)\n", image, format);
2366 if(!image || !format)
2367 return InvalidParameter;
2369 memcpy(format, &image->format, sizeof(GUID));
2371 return Ok;
2374 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2376 TRACE("%p %p\n", image, type);
2378 if(!image || !type)
2379 return InvalidParameter;
2381 *type = image->type;
2383 return Ok;
2386 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2388 if(!image || !res)
2389 return InvalidParameter;
2391 *res = image->yres;
2393 TRACE("(%p) <-- %0.2f\n", image, *res);
2395 return Ok;
2398 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2400 TRACE("%p %p\n", image, width);
2402 if(!image || !width)
2403 return InvalidParameter;
2405 if(image->type == ImageTypeMetafile)
2406 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2407 else if(image->type == ImageTypeBitmap)
2408 *width = ((GpBitmap*)image)->width;
2409 else
2410 *width = ipicture_pixel_width(image->picture);
2412 TRACE("returning %d\n", *width);
2414 return Ok;
2417 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2419 TRACE("(%p, %p)\n", image, num);
2421 if (!image || !num) return InvalidParameter;
2423 *num = 0;
2425 if (image->type == ImageTypeBitmap)
2427 if (((GpBitmap *)image)->prop_item)
2429 *num = ((GpBitmap *)image)->prop_count;
2430 return Ok;
2433 if (((GpBitmap *)image)->metadata_reader)
2434 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2437 return Ok;
2440 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2442 HRESULT hr;
2443 IWICMetadataReader *reader;
2444 IWICEnumMetadataItem *enumerator;
2445 UINT prop_count, i, items_returned;
2447 TRACE("(%p, %u, %p)\n", image, num, list);
2449 if (!image || !list) return InvalidParameter;
2451 if (image->type != ImageTypeBitmap)
2453 FIXME("Not implemented for type %d\n", image->type);
2454 return NotImplemented;
2457 if (((GpBitmap *)image)->prop_item)
2459 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2461 for (i = 0; i < num; i++)
2463 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2466 return Ok;
2469 reader = ((GpBitmap *)image)->metadata_reader;
2470 if (!reader)
2472 if (num != 0) return InvalidParameter;
2473 return Ok;
2476 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2477 if (FAILED(hr)) return hresult_to_status(hr);
2479 if (num != prop_count) return InvalidParameter;
2481 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2482 if (FAILED(hr)) return hresult_to_status(hr);
2484 IWICEnumMetadataItem_Reset(enumerator);
2486 for (i = 0; i < num; i++)
2488 PROPVARIANT id;
2490 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2491 if (hr != S_OK) break;
2493 if (id.vt != VT_UI2)
2495 FIXME("not supported propvariant type for id: %u\n", id.vt);
2496 list[i] = 0;
2497 continue;
2499 list[i] = id.u.uiVal;
2502 IWICEnumMetadataItem_Release(enumerator);
2504 return hr == S_OK ? Ok : hresult_to_status(hr);
2507 static UINT propvariant_size(PROPVARIANT *value)
2509 switch (value->vt & ~VT_VECTOR)
2511 case VT_EMPTY:
2512 return 0;
2513 case VT_I1:
2514 case VT_UI1:
2515 if (!(value->vt & VT_VECTOR)) return 1;
2516 return value->u.caub.cElems;
2517 case VT_I2:
2518 case VT_UI2:
2519 if (!(value->vt & VT_VECTOR)) return 2;
2520 return value->u.caui.cElems * 2;
2521 case VT_I4:
2522 case VT_UI4:
2523 case VT_R4:
2524 if (!(value->vt & VT_VECTOR)) return 4;
2525 return value->u.caul.cElems * 4;
2526 case VT_I8:
2527 case VT_UI8:
2528 case VT_R8:
2529 if (!(value->vt & VT_VECTOR)) return 8;
2530 return value->u.cauh.cElems * 8;
2531 case VT_LPSTR:
2532 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2533 case VT_BLOB:
2534 return value->u.blob.cbSize;
2535 default:
2536 FIXME("not supported variant type %d\n", value->vt);
2537 return 0;
2541 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2543 HRESULT hr;
2544 IWICMetadataReader *reader;
2545 PROPVARIANT id, value;
2547 TRACE("(%p,%#x,%p)\n", image, propid, size);
2549 if (!size || !image) return InvalidParameter;
2551 if (image->type != ImageTypeBitmap)
2553 FIXME("Not implemented for type %d\n", image->type);
2554 return NotImplemented;
2557 if (((GpBitmap *)image)->prop_item)
2559 UINT i;
2561 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2563 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2565 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2566 return Ok;
2570 return PropertyNotFound;
2573 reader = ((GpBitmap *)image)->metadata_reader;
2574 if (!reader) return PropertyNotFound;
2576 id.vt = VT_UI2;
2577 id.u.uiVal = propid;
2578 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2579 if (FAILED(hr)) return PropertyNotFound;
2581 *size = propvariant_size(&value);
2582 if (*size) *size += sizeof(PropertyItem);
2583 PropVariantClear(&value);
2585 return Ok;
2588 #ifndef PropertyTagTypeSByte
2589 #define PropertyTagTypeSByte 6
2590 #define PropertyTagTypeSShort 8
2591 #define PropertyTagTypeFloat 11
2592 #define PropertyTagTypeDouble 12
2593 #endif
2595 static UINT vt_to_itemtype(UINT vt)
2597 static const struct
2599 UINT vt, type;
2600 } vt2type[] =
2602 { VT_I1, PropertyTagTypeSByte },
2603 { VT_UI1, PropertyTagTypeByte },
2604 { VT_I2, PropertyTagTypeSShort },
2605 { VT_UI2, PropertyTagTypeShort },
2606 { VT_I4, PropertyTagTypeSLONG },
2607 { VT_UI4, PropertyTagTypeLong },
2608 { VT_I8, PropertyTagTypeSRational },
2609 { VT_UI8, PropertyTagTypeRational },
2610 { VT_R4, PropertyTagTypeFloat },
2611 { VT_R8, PropertyTagTypeDouble },
2612 { VT_LPSTR, PropertyTagTypeASCII },
2613 { VT_BLOB, PropertyTagTypeUndefined }
2615 UINT i;
2616 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2618 if (vt2type[i].vt == vt) return vt2type[i].type;
2620 FIXME("not supported variant type %u\n", vt);
2621 return 0;
2624 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2625 UINT size, PROPID id)
2627 UINT item_size, item_type;
2629 item_size = propvariant_size(value);
2630 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2632 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2633 if (!item_type) return InvalidParameter;
2635 item->value = item + 1;
2637 switch (value->vt & ~VT_VECTOR)
2639 case VT_I1:
2640 case VT_UI1:
2641 if (!(value->vt & VT_VECTOR))
2642 *(BYTE *)item->value = value->u.bVal;
2643 else
2644 memcpy(item->value, value->u.caub.pElems, item_size);
2645 break;
2646 case VT_I2:
2647 case VT_UI2:
2648 if (!(value->vt & VT_VECTOR))
2649 *(USHORT *)item->value = value->u.uiVal;
2650 else
2651 memcpy(item->value, value->u.caui.pElems, item_size);
2652 break;
2653 case VT_I4:
2654 case VT_UI4:
2655 case VT_R4:
2656 if (!(value->vt & VT_VECTOR))
2657 *(ULONG *)item->value = value->u.ulVal;
2658 else
2659 memcpy(item->value, value->u.caul.pElems, item_size);
2660 break;
2661 case VT_I8:
2662 case VT_UI8:
2663 case VT_R8:
2664 if (!(value->vt & VT_VECTOR))
2665 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2666 else
2667 memcpy(item->value, value->u.cauh.pElems, item_size);
2668 break;
2669 case VT_LPSTR:
2670 memcpy(item->value, value->u.pszVal, item_size);
2671 break;
2672 case VT_BLOB:
2673 memcpy(item->value, value->u.blob.pBlobData, item_size);
2674 break;
2675 default:
2676 FIXME("not supported variant type %d\n", value->vt);
2677 return InvalidParameter;
2680 item->length = item_size;
2681 item->type = item_type;
2682 item->id = id;
2684 return Ok;
2687 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2688 PropertyItem *buffer)
2690 GpStatus stat;
2691 HRESULT hr;
2692 IWICMetadataReader *reader;
2693 PROPVARIANT id, value;
2695 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2697 if (!image || !buffer) return InvalidParameter;
2699 if (image->type != ImageTypeBitmap)
2701 FIXME("Not implemented for type %d\n", image->type);
2702 return NotImplemented;
2705 if (((GpBitmap *)image)->prop_item)
2707 UINT i;
2709 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2711 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2713 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2714 return InvalidParameter;
2716 *buffer = ((GpBitmap *)image)->prop_item[i];
2717 buffer->value = buffer + 1;
2718 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2719 return Ok;
2723 return PropertyNotFound;
2726 reader = ((GpBitmap *)image)->metadata_reader;
2727 if (!reader) return PropertyNotFound;
2729 id.vt = VT_UI2;
2730 id.u.uiVal = propid;
2731 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2732 if (FAILED(hr)) return PropertyNotFound;
2734 stat = propvariant_to_item(&value, buffer, size, propid);
2735 PropVariantClear(&value);
2737 return stat;
2740 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2742 HRESULT hr;
2743 IWICMetadataReader *reader;
2744 IWICEnumMetadataItem *enumerator;
2745 UINT prop_count, prop_size, i;
2746 PROPVARIANT id, value;
2748 TRACE("(%p,%p,%p)\n", image, size, count);
2750 if (!image || !size || !count) return InvalidParameter;
2752 if (image->type != ImageTypeBitmap)
2754 FIXME("Not implemented for type %d\n", image->type);
2755 return NotImplemented;
2758 if (((GpBitmap *)image)->prop_item)
2760 *count = ((GpBitmap *)image)->prop_count;
2761 *size = 0;
2763 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2765 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2768 return Ok;
2771 reader = ((GpBitmap *)image)->metadata_reader;
2772 if (!reader) return PropertyNotFound;
2774 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2775 if (FAILED(hr)) return hresult_to_status(hr);
2777 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2778 if (FAILED(hr)) return hresult_to_status(hr);
2780 IWICEnumMetadataItem_Reset(enumerator);
2782 prop_size = 0;
2784 PropVariantInit(&id);
2785 PropVariantInit(&value);
2787 for (i = 0; i < prop_count; i++)
2789 UINT items_returned, item_size;
2791 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2792 if (hr != S_OK) break;
2794 item_size = propvariant_size(&value);
2795 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2797 PropVariantClear(&id);
2798 PropVariantClear(&value);
2801 IWICEnumMetadataItem_Release(enumerator);
2803 if (hr != S_OK) return PropertyNotFound;
2805 *count = prop_count;
2806 *size = prop_size;
2807 return Ok;
2810 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2811 UINT count, PropertyItem *buf)
2813 GpStatus status;
2814 HRESULT hr;
2815 IWICMetadataReader *reader;
2816 IWICEnumMetadataItem *enumerator;
2817 UINT prop_count, prop_size, i;
2818 PROPVARIANT id, value;
2819 char *item_value;
2821 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2823 if (!image || !buf) return InvalidParameter;
2825 if (image->type != ImageTypeBitmap)
2827 FIXME("Not implemented for type %d\n", image->type);
2828 return NotImplemented;
2831 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2832 if (status != Ok) return status;
2834 if (prop_count != count || prop_size != size) return InvalidParameter;
2836 if (((GpBitmap *)image)->prop_item)
2838 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2840 item_value = (char *)(buf + prop_count);
2842 for (i = 0; i < prop_count; i++)
2844 buf[i].value = item_value;
2845 item_value += buf[i].length;
2848 return Ok;
2851 reader = ((GpBitmap *)image)->metadata_reader;
2852 if (!reader) return PropertyNotFound;
2854 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2855 if (FAILED(hr)) return hresult_to_status(hr);
2857 IWICEnumMetadataItem_Reset(enumerator);
2859 item_value = (char *)(buf + prop_count);
2861 PropVariantInit(&id);
2862 PropVariantInit(&value);
2864 for (i = 0; i < prop_count; i++)
2866 PropertyItem *item;
2867 UINT items_returned, item_size;
2869 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2870 if (hr != S_OK) break;
2872 if (id.vt != VT_UI2)
2874 FIXME("not supported propvariant type for id: %u\n", id.vt);
2875 continue;
2878 item_size = propvariant_size(&value);
2879 if (item_size)
2881 item = heap_alloc(item_size + sizeof(*item));
2883 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2884 buf[i].id = item->id;
2885 buf[i].type = item->type;
2886 buf[i].length = item_size;
2887 buf[i].value = item_value;
2888 memcpy(item_value, item->value, item_size);
2889 item_value += item_size;
2891 heap_free(item);
2894 PropVariantClear(&id);
2895 PropVariantClear(&value);
2898 IWICEnumMetadataItem_Release(enumerator);
2900 if (hr != S_OK) return PropertyNotFound;
2902 return Ok;
2905 struct image_format_dimension
2907 const GUID *format;
2908 const GUID *dimension;
2911 static const struct image_format_dimension image_format_dimensions[] =
2913 {&ImageFormatGIF, &FrameDimensionTime},
2914 {&ImageFormatIcon, &FrameDimensionResolution},
2915 {NULL}
2918 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2919 GDIPCONST GUID* dimensionID, UINT* count)
2921 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2923 if(!image || !count)
2924 return InvalidParameter;
2926 if (!dimensionID ||
2927 IsEqualGUID(dimensionID, &image->format) ||
2928 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2929 IsEqualGUID(dimensionID, &FrameDimensionTime))
2931 *count = image->frame_count;
2932 return Ok;
2935 return InvalidParameter;
2938 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2939 UINT* count)
2941 TRACE("(%p, %p)\n", image, count);
2943 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2945 if(!image || !count)
2946 return InvalidParameter;
2948 *count = 1;
2950 return Ok;
2953 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2954 GUID* dimensionIDs, UINT count)
2956 int i;
2957 const GUID *result=NULL;
2959 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2961 if(!image || !dimensionIDs || count != 1)
2962 return InvalidParameter;
2964 for (i=0; image_format_dimensions[i].format; i++)
2966 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2968 result = image_format_dimensions[i].dimension;
2969 break;
2973 if (!result)
2974 result = &FrameDimensionPage;
2976 memcpy(dimensionIDs, result, sizeof(GUID));
2978 return Ok;
2981 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2982 GpImage **image)
2984 GpStatus stat;
2985 IStream *stream;
2987 TRACE("(%s) %p\n", debugstr_w(filename), image);
2989 if (!filename || !image)
2990 return InvalidParameter;
2992 *image = NULL;
2994 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2996 if (stat != Ok)
2997 return stat;
2999 stat = GdipLoadImageFromStream(stream, image);
3001 IStream_Release(stream);
3003 return stat;
3006 /* FIXME: no icm handling */
3007 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
3009 TRACE("(%s) %p\n", debugstr_w(filename), image);
3011 return GdipLoadImageFromFile(filename, image);
3014 static void add_property(GpBitmap *bitmap, PropertyItem *item)
3016 UINT prop_size, prop_count;
3017 PropertyItem *prop_item;
3019 if (bitmap->prop_item == NULL)
3021 prop_size = prop_count = 0;
3022 prop_item = heap_alloc_zero(item->length + sizeof(PropertyItem));
3023 if (!prop_item) return;
3025 else
3027 UINT i;
3028 char *item_value;
3030 GdipGetPropertySize((GpImage *)bitmap, &prop_size, &prop_count);
3032 prop_item = heap_alloc_zero(prop_size + item->length + sizeof(PropertyItem));
3033 if (!prop_item) return;
3034 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
3035 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
3036 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
3038 item_value = (char *)(prop_item + prop_count + 1);
3040 for (i = 0; i < prop_count; i++)
3042 prop_item[i].value = item_value;
3043 item_value += prop_item[i].length;
3047 prop_item[prop_count].id = item->id;
3048 prop_item[prop_count].type = item->type;
3049 prop_item[prop_count].length = item->length;
3050 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3051 memcpy(prop_item[prop_count].value, item->value, item->length);
3053 heap_free(bitmap->prop_item);
3054 bitmap->prop_item = prop_item;
3055 bitmap->prop_count++;
3058 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3060 HRESULT hr;
3061 GUID format;
3062 PROPVARIANT id, value;
3063 BOOL ret = FALSE;
3065 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3066 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return FALSE;
3068 PropVariantInit(&id);
3069 PropVariantInit(&value);
3071 id.vt = VT_LPWSTR;
3072 id.u.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3073 if (!id.u.pwszVal) return FALSE;
3074 lstrcpyW(id.u.pwszVal, prop_name);
3075 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3076 if (hr == S_OK && value.vt == VT_BOOL)
3077 ret = value.u.boolVal;
3079 PropVariantClear(&id);
3080 PropVariantClear(&value);
3082 return ret;
3085 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3087 HRESULT hr;
3088 GUID format;
3089 PROPVARIANT id, value;
3090 PropertyItem *item = NULL;
3092 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3093 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return NULL;
3095 PropVariantInit(&id);
3096 PropVariantInit(&value);
3098 id.vt = VT_LPWSTR;
3099 id.u.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3100 if (!id.u.pwszVal) return NULL;
3101 lstrcpyW(id.u.pwszVal, prop_name);
3102 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3103 if (hr == S_OK)
3105 UINT item_size = propvariant_size(&value);
3106 if (item_size)
3108 item_size += sizeof(*item);
3109 item = heap_alloc_zero(item_size);
3110 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3112 heap_free(item);
3113 item = NULL;
3118 PropVariantClear(&id);
3119 PropVariantClear(&value);
3121 return item;
3124 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3126 static const WCHAR textentryW[] = { 'T','e','x','t','E','n','t','r','y',0 };
3127 PropertyItem *comment;
3129 comment = get_property(reader, &GUID_MetadataFormatGifComment, textentryW);
3130 if (comment)
3131 comment->id = PropertyTagExifUserComment;
3133 return comment;
3136 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3138 static const WCHAR applicationW[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3139 static const WCHAR dataW[] = { 'D','a','t','a',0 };
3140 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3142 appext = get_property(reader, &GUID_MetadataFormatAPE, applicationW);
3143 if (appext)
3145 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3146 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3148 appdata = get_property(reader, &GUID_MetadataFormatAPE, dataW);
3149 if (appdata)
3151 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3153 BYTE *data = appdata->value;
3154 if (data[0] == 3 && data[1] == 1)
3156 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3157 if (loop)
3159 loop->type = PropertyTagTypeShort;
3160 loop->id = PropertyTagLoopCount;
3161 loop->length = sizeof(SHORT);
3162 loop->value = loop + 1;
3163 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3171 heap_free(appext);
3172 heap_free(appdata);
3174 return loop;
3177 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3179 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 };
3180 PropertyItem *background;
3182 background = get_property(reader, &GUID_MetadataFormatLSD, backgroundW);
3183 if (background)
3184 background->id = PropertyTagIndexBackground;
3186 return background;
3189 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3191 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 };
3192 HRESULT hr;
3193 IWICImagingFactory *factory;
3194 IWICPalette *palette;
3195 UINT count = 0;
3196 WICColor colors[256];
3198 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, global_flagW))
3199 return NULL;
3201 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3202 if (hr != S_OK) return NULL;
3204 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3205 if (hr == S_OK)
3207 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3208 if (hr == S_OK)
3209 IWICPalette_GetColors(palette, 256, colors, &count);
3211 IWICPalette_Release(palette);
3214 IWICImagingFactory_Release(factory);
3216 if (count)
3218 PropertyItem *pal;
3219 UINT i;
3220 BYTE *rgb;
3222 pal = heap_alloc_zero(sizeof(*pal) + count * 3);
3223 if (!pal) return NULL;
3224 pal->type = PropertyTagTypeByte;
3225 pal->id = PropertyTagGlobalPalette;
3226 pal->value = pal + 1;
3227 pal->length = count * 3;
3229 rgb = pal->value;
3231 for (i = 0; i < count; i++)
3233 rgb[i*3] = (colors[i] >> 16) & 0xff;
3234 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3235 rgb[i*3 + 2] = colors[i] & 0xff;
3238 return pal;
3241 return NULL;
3244 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3246 static const WCHAR transparency_flagW[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3247 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 };
3248 PropertyItem *index = NULL;
3250 if (get_bool_property(reader, &GUID_MetadataFormatGCE, transparency_flagW))
3252 index = get_property(reader, &GUID_MetadataFormatGCE, colorW);
3253 if (index)
3254 index->id = PropertyTagIndexTransparent;
3256 return index;
3259 static LONG get_gif_frame_property(IWICBitmapFrameDecode *frame, const GUID *format, const WCHAR *property)
3261 HRESULT hr;
3262 IWICMetadataBlockReader *block_reader;
3263 IWICMetadataReader *reader;
3264 UINT block_count, i;
3265 PropertyItem *prop;
3266 LONG value = 0;
3268 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3269 if (hr == S_OK)
3271 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3272 if (hr == S_OK)
3274 for (i = 0; i < block_count; i++)
3276 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3277 if (hr == S_OK)
3279 prop = get_property(reader, format, property);
3280 if (prop)
3282 if (prop->type == PropertyTagTypeByte && prop->length == 1)
3283 value = *(BYTE *)prop->value;
3284 else if (prop->type == PropertyTagTypeShort && prop->length == 2)
3285 value = *(SHORT *)prop->value;
3287 heap_free(prop);
3289 IWICMetadataReader_Release(reader);
3293 IWICMetadataBlockReader_Release(block_reader);
3296 return value;
3299 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3301 static const WCHAR delayW[] = { 'D','e','l','a','y',0 };
3302 HRESULT hr;
3303 IWICBitmapFrameDecode *frame;
3304 IWICMetadataBlockReader *block_reader;
3305 IWICMetadataReader *reader;
3306 UINT frame_count, block_count, i;
3307 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3308 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3310 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3311 if (frame_count > 1)
3313 delay = heap_alloc_zero(sizeof(*delay) + frame_count * sizeof(LONG));
3314 if (delay)
3316 LONG *value;
3318 delay->type = PropertyTagTypeLong;
3319 delay->id = PropertyTagFrameDelay;
3320 delay->length = frame_count * sizeof(LONG);
3321 delay->value = delay + 1;
3323 value = delay->value;
3325 for (i = 0; i < frame_count; i++)
3327 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3328 if (hr == S_OK)
3330 value[i] = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, delayW);
3331 IWICBitmapFrameDecode_Release(frame);
3333 else value[i] = 0;
3338 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3339 if (hr == S_OK)
3341 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3342 if (hr == S_OK)
3344 for (i = 0; i < block_count; i++)
3346 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3347 if (hr == S_OK)
3349 if (!comment)
3350 comment = get_gif_comment(reader);
3352 if (frame_count > 1 && !loop)
3353 loop = get_gif_loopcount(reader);
3355 if (!background)
3356 background = get_gif_background(reader);
3358 if (!palette)
3359 palette = get_gif_palette(decoder, reader);
3361 IWICMetadataReader_Release(reader);
3365 IWICMetadataBlockReader_Release(block_reader);
3368 if (frame_count > 1 && !loop)
3370 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3371 if (loop)
3373 loop->type = PropertyTagTypeShort;
3374 loop->id = PropertyTagLoopCount;
3375 loop->length = sizeof(SHORT);
3376 loop->value = loop + 1;
3377 *(SHORT *)loop->value = 1;
3381 if (delay) add_property(bitmap, delay);
3382 if (comment) add_property(bitmap, comment);
3383 if (loop) add_property(bitmap, loop);
3384 if (palette) add_property(bitmap, palette);
3385 if (background) add_property(bitmap, background);
3387 heap_free(delay);
3388 heap_free(comment);
3389 heap_free(loop);
3390 heap_free(palette);
3391 heap_free(background);
3393 /* Win7 gdiplus always returns transparent color index from frame 0 */
3394 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3395 if (hr != S_OK) return;
3397 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3398 if (hr == S_OK)
3400 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3401 if (hr == S_OK)
3403 for (i = 0; i < block_count; i++)
3405 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3406 if (hr == S_OK)
3408 if (!transparent_idx)
3409 transparent_idx = get_gif_transparent_idx(reader);
3411 IWICMetadataReader_Release(reader);
3415 IWICMetadataBlockReader_Release(block_reader);
3418 if (transparent_idx) add_property(bitmap, transparent_idx);
3419 heap_free(transparent_idx);
3421 IWICBitmapFrameDecode_Release(frame);
3424 static PropertyItem* create_prop(PROPID propid, PROPVARIANT* value)
3426 PropertyItem *item = NULL;
3427 UINT item_size = propvariant_size(value);
3429 if (item_size)
3431 item_size += sizeof(*item);
3432 item = heap_alloc_zero(item_size);
3433 if (propvariant_to_item(value, item, item_size, propid) != Ok)
3435 heap_free(item);
3436 item = NULL;
3440 return item;
3443 static ULONG get_ulong_by_index(IWICMetadataReader* reader, ULONG index)
3445 PROPVARIANT value;
3446 HRESULT hr;
3447 ULONG result=0;
3449 hr = IWICMetadataReader_GetValueByIndex(reader, index, NULL, NULL, &value);
3450 if (SUCCEEDED(hr))
3452 switch (value.vt)
3454 case VT_UI4:
3455 result = value.u.ulVal;
3456 break;
3457 default:
3458 ERR("unhandled case %u\n", value.vt);
3459 break;
3461 PropVariantClear(&value);
3463 return result;
3466 static void png_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3468 HRESULT hr;
3469 IWICBitmapFrameDecode *frame;
3470 IWICMetadataBlockReader *block_reader;
3471 IWICMetadataReader *reader;
3472 UINT block_count, i, j;
3473 struct keyword_info {
3474 const char* name;
3475 PROPID propid;
3476 BOOL seen;
3477 } keywords[] = {
3478 { "Title", PropertyTagImageTitle },
3479 { "Author", PropertyTagArtist },
3480 { "Description", PropertyTagImageDescription },
3481 { "Copyright", PropertyTagCopyright },
3482 { "Software", PropertyTagSoftwareUsed },
3483 { "Source", PropertyTagEquipModel },
3484 { "Comment", PropertyTagExifUserComment },
3486 BOOL seen_gamma=FALSE, seen_whitepoint=FALSE, seen_chrm=FALSE;
3488 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3489 if (hr != S_OK) return;
3491 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3492 if (hr == S_OK)
3494 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3495 if (hr == S_OK)
3497 for (i = 0; i < block_count; i++)
3499 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3500 if (hr == S_OK)
3502 GUID format;
3504 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3505 if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunktEXt, &format))
3507 PROPVARIANT name, value;
3508 PropertyItem* item;
3510 hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, &name, &value);
3512 if (SUCCEEDED(hr))
3514 if (name.vt == VT_LPSTR)
3516 for (j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++)
3517 if (!strcmp(keywords[j].name, name.u.pszVal))
3518 break;
3519 if (j < sizeof(keywords)/sizeof(keywords[0]) && !keywords[j].seen)
3521 keywords[j].seen = TRUE;
3522 item = create_prop(keywords[j].propid, &value);
3523 if (item)
3524 add_property(bitmap, item);
3525 heap_free(item);
3529 PropVariantClear(&name);
3530 PropVariantClear(&value);
3533 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA, &format))
3535 PropertyItem* item;
3537 if (!seen_gamma)
3539 item = heap_alloc_zero(sizeof(PropertyItem) + sizeof(ULONG) * 2);
3540 if (item)
3542 ULONG *rational;
3543 item->length = sizeof(ULONG) * 2;
3544 item->type = PropertyTagTypeRational;
3545 item->id = PropertyTagGamma;
3546 rational = item->value = item + 1;
3547 rational[0] = 100000;
3548 rational[1] = get_ulong_by_index(reader, 0);
3549 add_property(bitmap, item);
3550 seen_gamma = TRUE;
3551 heap_free(item);
3555 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkcHRM, &format))
3557 PropertyItem* item;
3559 if (!seen_whitepoint)
3561 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 4);
3562 if (item)
3564 ULONG *rational;
3565 item->length = sizeof(ULONG) * 4;
3566 item->type = PropertyTagTypeRational;
3567 item->id = PropertyTagWhitePoint;
3568 rational = item->value = item + 1;
3569 rational[0] = get_ulong_by_index(reader, 0);
3570 rational[1] = 100000;
3571 rational[2] = get_ulong_by_index(reader, 1);
3572 rational[3] = 100000;
3573 add_property(bitmap, item);
3574 seen_whitepoint = TRUE;
3575 GdipFree(item);
3578 if (!seen_chrm)
3580 item = GdipAlloc(sizeof(PropertyItem) + sizeof(ULONG) * 12);
3581 if (item)
3583 ULONG *rational;
3584 item->length = sizeof(ULONG) * 12;
3585 item->type = PropertyTagTypeRational;
3586 item->id = PropertyTagPrimaryChromaticities;
3587 rational = item->value = item + 1;
3588 rational[0] = get_ulong_by_index(reader, 2);
3589 rational[1] = 100000;
3590 rational[2] = get_ulong_by_index(reader, 3);
3591 rational[3] = 100000;
3592 rational[4] = get_ulong_by_index(reader, 4);
3593 rational[5] = 100000;
3594 rational[6] = get_ulong_by_index(reader, 5);
3595 rational[7] = 100000;
3596 rational[8] = get_ulong_by_index(reader, 6);
3597 rational[9] = 100000;
3598 rational[10] = get_ulong_by_index(reader, 7);
3599 rational[11] = 100000;
3600 add_property(bitmap, item);
3601 seen_chrm = TRUE;
3602 GdipFree(item);
3607 IWICMetadataReader_Release(reader);
3611 IWICMetadataBlockReader_Release(block_reader);
3614 IWICBitmapFrameDecode_Release(frame);
3617 static GpStatus initialize_decoder_wic(IStream *stream, REFGUID container, IWICBitmapDecoder **decoder)
3619 IWICImagingFactory *factory;
3620 HRESULT hr;
3622 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
3624 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3625 if (FAILED(hr)) return hresult_to_status(hr);
3626 hr = IWICImagingFactory_CreateDecoder(factory, container, NULL, decoder);
3627 IWICImagingFactory_Release(factory);
3628 if (FAILED(hr)) return hresult_to_status(hr);
3630 hr = IWICBitmapDecoder_Initialize(*decoder, stream, WICDecodeMetadataCacheOnLoad);
3631 if (FAILED(hr)) return hresult_to_status(hr);
3632 return Ok;
3635 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3637 static GpStatus decode_frame_wic(IWICBitmapDecoder *decoder, BOOL force_conversion,
3638 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3640 GpStatus status=Ok;
3641 GpBitmap *bitmap;
3642 HRESULT hr;
3643 IWICBitmapFrameDecode *frame;
3644 IWICBitmapSource *source=NULL;
3645 IWICMetadataBlockReader *block_reader;
3646 WICPixelFormatGUID wic_format;
3647 PixelFormat gdip_format=0;
3648 ColorPalette *palette = NULL;
3649 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3650 int i;
3651 UINT width, height, frame_count;
3652 BitmapData lockeddata;
3653 WICRect wrc;
3655 TRACE("%p,%u,%p\n", decoder, active_frame, image);
3657 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3658 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3659 if (SUCCEEDED(hr)) /* got frame */
3661 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3663 if (SUCCEEDED(hr))
3665 if (!force_conversion)
3667 for (i=0; pixel_formats[i].wic_format; i++)
3669 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3671 source = (IWICBitmapSource*)frame;
3672 IWICBitmapSource_AddRef(source);
3673 gdip_format = pixel_formats[i].gdip_format;
3674 palette_type = pixel_formats[i].palette_type;
3675 break;
3679 if (!source)
3681 /* unknown format; fall back on 32bppARGB */
3682 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3683 gdip_format = PixelFormat32bppARGB;
3685 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3688 if (SUCCEEDED(hr)) /* got source */
3690 hr = IWICBitmapSource_GetSize(source, &width, &height);
3692 if (SUCCEEDED(hr))
3693 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3694 NULL, &bitmap);
3696 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3698 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3699 gdip_format, &lockeddata);
3700 if (status == Ok) /* locked bitmap */
3702 wrc.X = 0;
3703 wrc.Width = width;
3704 wrc.Height = 1;
3705 for (i=0; i<height; i++)
3707 wrc.Y = i;
3708 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3709 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3710 if (FAILED(hr)) break;
3713 GdipBitmapUnlockBits(bitmap, &lockeddata);
3716 if (SUCCEEDED(hr) && status == Ok)
3717 *image = (GpImage*)bitmap;
3718 else
3720 *image = NULL;
3721 GdipDisposeImage((GpImage*)bitmap);
3724 if (SUCCEEDED(hr) && status == Ok)
3726 double dpix, dpiy;
3727 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3728 if (SUCCEEDED(hr))
3730 bitmap->image.xres = dpix;
3731 bitmap->image.yres = dpiy;
3733 hr = S_OK;
3737 IWICBitmapSource_Release(source);
3740 if (SUCCEEDED(hr)) {
3741 bitmap->metadata_reader = NULL;
3743 if (metadata_reader)
3744 metadata_reader(bitmap, decoder, active_frame);
3745 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3747 UINT block_count = 0;
3748 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3749 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3750 IWICMetadataBlockReader_Release(block_reader);
3753 palette = get_palette(frame, palette_type);
3754 IWICBitmapFrameDecode_Release(frame);
3758 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3760 if (status == Ok)
3762 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3763 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3764 bitmap->image.frame_count = frame_count;
3765 bitmap->image.current_frame = active_frame;
3766 bitmap->image.decoder = decoder;
3767 IWICBitmapDecoder_AddRef(decoder);
3768 if (palette)
3770 heap_free(bitmap->image.palette);
3771 bitmap->image.palette = palette;
3773 else
3775 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3776 bitmap->image.palette->Flags = 0;
3778 TRACE("=> %p\n", *image);
3781 return status;
3784 static GpStatus decode_image_wic(IStream *stream, REFGUID container,
3785 metadata_reader_func metadata_reader, GpImage **image)
3787 IWICBitmapDecoder *decoder;
3788 GpStatus status;
3790 status = initialize_decoder_wic(stream, container, &decoder);
3791 if(status != Ok)
3792 return status;
3794 status = decode_frame_wic(decoder, FALSE, 0, metadata_reader, image);
3795 IWICBitmapDecoder_Release(decoder);
3796 return status;
3799 static GpStatus select_frame_wic(GpImage *image, UINT active_frame)
3801 GpImage *new_image;
3802 GpStatus status;
3804 status = decode_frame_wic(image->decoder, FALSE, active_frame, NULL, &new_image);
3805 if(status != Ok)
3806 return status;
3808 memcpy(&new_image->format, &image->format, sizeof(GUID));
3809 free_image_data(image);
3810 if (image->type == ImageTypeBitmap)
3811 *(GpBitmap *)image = *(GpBitmap *)new_image;
3812 else if (image->type == ImageTypeMetafile)
3813 *(GpMetafile *)image = *(GpMetafile *)new_image;
3814 new_image->type = ~0;
3815 heap_free(new_image);
3816 return Ok;
3819 static HRESULT get_gif_frame_rect(IWICBitmapFrameDecode *frame,
3820 UINT *left, UINT *top, UINT *width, UINT *height)
3822 static const WCHAR leftW[] = {'L','e','f','t',0};
3823 static const WCHAR topW[] = {'T','o','p',0};
3825 *left = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, leftW);
3826 *top = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, topW);
3828 return IWICBitmapFrameDecode_GetSize(frame, width, height);
3831 static HRESULT blit_gif_frame(GpBitmap *bitmap, IWICBitmapFrameDecode *frame, BOOL first_frame)
3833 UINT i, j, left, top, width, height;
3834 IWICBitmapSource *source;
3835 BYTE *new_bits;
3836 HRESULT hr;
3838 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3839 if(FAILED(hr))
3840 return hr;
3842 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3843 if(FAILED(hr))
3844 return hr;
3846 new_bits = heap_alloc_zero(width*height*4);
3847 if(!new_bits)
3848 return E_OUTOFMEMORY;
3850 hr = IWICBitmapSource_CopyPixels(source, NULL, width*4, width*height*4, new_bits);
3851 IWICBitmapSource_Release(source);
3852 if(FAILED(hr)) {
3853 heap_free(new_bits);
3854 return hr;
3857 for(i=0; i<height && i+top<bitmap->height; i++) {
3858 for(j=0; j<width && j+left<bitmap->width; j++) {
3859 DWORD *src = (DWORD*)(new_bits+i*width*4+j*4);
3860 DWORD *dst = (DWORD*)(bitmap->bits+(i+top)*bitmap->stride+(j+left)*4);
3862 if(first_frame || *src>>24 != 0)
3863 *dst = *src;
3866 heap_free(new_bits);
3867 return hr;
3870 static DWORD get_gif_background_color(GpBitmap *bitmap)
3872 BYTE bgcolor_idx = 0;
3873 UINT i;
3875 for(i=0; i<bitmap->prop_count; i++) {
3876 if(bitmap->prop_item[i].id == PropertyTagIndexBackground) {
3877 bgcolor_idx = *(BYTE*)bitmap->prop_item[i].value;
3878 break;
3882 for(i=0; i<bitmap->prop_count; i++) {
3883 if(bitmap->prop_item[i].id == PropertyTagIndexTransparent) {
3884 BYTE transparent_idx;
3885 transparent_idx = *(BYTE*)bitmap->prop_item[i].value;
3887 if(transparent_idx == bgcolor_idx)
3888 return 0;
3892 for(i=0; i<bitmap->prop_count; i++) {
3893 if(bitmap->prop_item[i].id == PropertyTagGlobalPalette) {
3894 if(bitmap->prop_item[i].length/3 > bgcolor_idx) {
3895 BYTE *color = ((BYTE*)bitmap->prop_item[i].value)+bgcolor_idx*3;
3896 return color[2] + (color[1]<<8) + (color[0]<<16) + (0xffu<<24);
3898 break;
3902 FIXME("can't get gif background color\n");
3903 return 0xffffffff;
3906 static GpStatus select_frame_gif(GpImage* image, UINT active_frame)
3908 static const WCHAR disposalW[] = {'D','i','s','p','o','s','a','l',0};
3910 GpBitmap *bitmap = (GpBitmap*)image;
3911 IWICBitmapFrameDecode *frame;
3912 int cur_frame=0, disposal;
3913 BOOL bgcolor_set = FALSE;
3914 DWORD bgcolor = 0;
3915 HRESULT hr;
3917 if(active_frame > image->current_frame) {
3918 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, image->current_frame, &frame);
3919 if(FAILED(hr))
3920 return hresult_to_status(hr);
3921 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3922 IWICBitmapFrameDecode_Release(frame);
3924 if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND)
3925 cur_frame = image->current_frame;
3926 else if(disposal != GIF_DISPOSE_RESTORE_TO_PREV)
3927 cur_frame = image->current_frame+1;
3930 while(cur_frame != active_frame) {
3931 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, cur_frame, &frame);
3932 if(FAILED(hr))
3933 return hresult_to_status(hr);
3934 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3936 if(disposal==GIF_DISPOSE_UNSPECIFIED || disposal==GIF_DISPOSE_DO_NOT_DISPOSE) {
3937 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3938 if(FAILED(hr))
3939 return hresult_to_status(hr);
3940 }else if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND) {
3941 UINT left, top, width, height, i, j;
3943 if(!bgcolor_set) {
3944 bgcolor = get_gif_background_color(bitmap);
3945 bgcolor_set = TRUE;
3948 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3949 if(FAILED(hr))
3950 return hresult_to_status(hr);
3951 for(i=top; i<top+height && i<bitmap->height; i++) {
3952 DWORD *bits = (DWORD*)(bitmap->bits+i*bitmap->stride);
3953 for(j=left; j<left+width && j<bitmap->width; j++)
3954 bits[j] = bgcolor;
3958 IWICBitmapFrameDecode_Release(frame);
3959 cur_frame++;
3962 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, active_frame, &frame);
3963 if(FAILED(hr))
3964 return hresult_to_status(hr);
3966 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3967 IWICBitmapFrameDecode_Release(frame);
3968 if(FAILED(hr))
3969 return hresult_to_status(hr);
3971 image->current_frame = active_frame;
3972 return Ok;
3975 static GpStatus decode_image_icon(IStream* stream, GpImage **image)
3977 return decode_image_wic(stream, &GUID_ContainerFormatIco, NULL, image);
3980 static GpStatus decode_image_bmp(IStream* stream, GpImage **image)
3982 GpStatus status;
3983 GpBitmap* bitmap;
3985 status = decode_image_wic(stream, &GUID_ContainerFormatBmp, NULL, image);
3987 bitmap = (GpBitmap*)*image;
3989 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3991 /* WIC supports bmp files with alpha, but gdiplus does not */
3992 bitmap->format = PixelFormat32bppRGB;
3995 return status;
3998 static GpStatus decode_image_jpeg(IStream* stream, GpImage **image)
4000 return decode_image_wic(stream, &GUID_ContainerFormatJpeg, NULL, image);
4003 static GpStatus decode_image_png(IStream* stream, GpImage **image)
4005 return decode_image_wic(stream, &GUID_ContainerFormatPng, png_metadata_reader, image);
4008 static GpStatus decode_image_gif(IStream* stream, GpImage **image)
4010 IWICBitmapDecoder *decoder;
4011 UINT frame_count;
4012 GpStatus status;
4013 HRESULT hr;
4015 status = initialize_decoder_wic(stream, &GUID_ContainerFormatGif, &decoder);
4016 if(status != Ok)
4017 return status;
4019 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
4020 if(FAILED(hr))
4021 return hresult_to_status(hr);
4023 status = decode_frame_wic(decoder, frame_count > 1, 0, gif_metadata_reader, image);
4024 IWICBitmapDecoder_Release(decoder);
4025 if(status != Ok)
4026 return status;
4028 if(frame_count > 1) {
4029 heap_free((*image)->palette);
4030 (*image)->palette = NULL;
4032 return Ok;
4035 static GpStatus decode_image_tiff(IStream* stream, GpImage **image)
4037 return decode_image_wic(stream, &GUID_ContainerFormatTiff, NULL, image);
4040 static GpStatus decode_image_olepicture_metafile(IStream* stream, GpImage **image)
4042 IPicture *pic;
4044 TRACE("%p %p\n", stream, image);
4046 if(!stream || !image)
4047 return InvalidParameter;
4049 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
4050 (LPVOID*) &pic) != S_OK){
4051 TRACE("Could not load picture\n");
4052 return GenericError;
4055 /* FIXME: missing initialization code */
4056 *image = heap_alloc_zero(sizeof(GpMetafile));
4057 if(!*image) return OutOfMemory;
4058 (*image)->type = ImageTypeMetafile;
4059 (*image)->decoder = NULL;
4060 (*image)->picture = pic;
4061 (*image)->flags = ImageFlagsNone;
4062 (*image)->frame_count = 1;
4063 (*image)->current_frame = 0;
4064 (*image)->palette = NULL;
4066 TRACE("<-- %p\n", *image);
4068 return Ok;
4071 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
4072 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
4074 typedef GpStatus (*decode_image_func)(IStream *stream, GpImage **image);
4076 typedef GpStatus (*select_image_func)(GpImage *image, UINT active_frame);
4078 typedef struct image_codec {
4079 ImageCodecInfo info;
4080 encode_image_func encode_func;
4081 decode_image_func decode_func;
4082 select_image_func select_func;
4083 } image_codec;
4085 typedef enum {
4086 BMP,
4087 JPEG,
4088 GIF,
4089 TIFF,
4090 EMF,
4091 WMF,
4092 PNG,
4093 ICO,
4094 NUM_CODECS
4095 } ImageFormat;
4097 static const struct image_codec codecs[NUM_CODECS];
4099 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
4101 BYTE signature[8];
4102 const BYTE *pattern, *mask;
4103 LARGE_INTEGER seek;
4104 HRESULT hr;
4105 UINT bytesread;
4106 int i;
4107 DWORD j, sig;
4109 /* seek to the start of the stream */
4110 seek.QuadPart = 0;
4111 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4112 if (FAILED(hr)) return hresult_to_status(hr);
4114 /* read the first 8 bytes */
4115 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4116 hr = IStream_Read(stream, signature, 8, &bytesread);
4117 if (FAILED(hr)) return hresult_to_status(hr);
4118 if (hr == S_FALSE || bytesread == 0) return GenericError;
4120 for (i = 0; i < NUM_CODECS; i++) {
4121 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4122 bytesread >= codecs[i].info.SigSize)
4124 for (sig=0; sig<codecs[i].info.SigCount; sig++)
4126 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
4127 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
4128 for (j=0; j<codecs[i].info.SigSize; j++)
4129 if ((signature[j] & mask[j]) != pattern[j])
4130 break;
4131 if (j == codecs[i].info.SigSize)
4133 *result = &codecs[i];
4134 return Ok;
4140 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
4141 signature[0],signature[1],signature[2],signature[3],
4142 signature[4],signature[5],signature[6],signature[7]);
4144 return GenericError;
4147 static GpStatus get_decoder_info_from_image(GpImage *image, const struct image_codec **result)
4149 int i;
4151 for (i = 0; i < NUM_CODECS; i++) {
4152 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4153 IsEqualIID(&codecs[i].info.FormatID, &image->format))
4155 *result = &codecs[i];
4156 return Ok;
4160 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image->format));
4161 return GenericError;
4164 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
4165 UINT frame)
4167 GpStatus stat;
4168 const struct image_codec *codec = NULL;
4170 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
4172 if (!image || !dimensionID)
4173 return InvalidParameter;
4175 if (frame >= image->frame_count)
4177 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
4178 return InvalidParameter;
4181 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
4183 WARN("invalid image type %d\n", image->type);
4184 return InvalidParameter;
4187 if (image->current_frame == frame)
4188 return Ok;
4190 if (!image->decoder)
4192 TRACE("image doesn't have an associated decoder\n");
4193 return Ok;
4196 /* choose an appropriate image decoder */
4197 stat = get_decoder_info_from_image(image, &codec);
4198 if (stat != Ok)
4200 WARN("can't find decoder info\n");
4201 return stat;
4204 return codec->select_func(image, frame);
4207 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
4209 GpStatus stat;
4210 LARGE_INTEGER seek;
4211 HRESULT hr;
4212 const struct image_codec *codec=NULL;
4214 /* choose an appropriate image decoder */
4215 stat = get_decoder_info(stream, &codec);
4216 if (stat != Ok) return stat;
4218 /* seek to the start of the stream */
4219 seek.QuadPart = 0;
4220 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4221 if (FAILED(hr)) return hresult_to_status(hr);
4223 /* call on the image decoder to do the real work */
4224 stat = codec->decode_func(stream, image);
4226 /* take note of the original data format */
4227 if (stat == Ok)
4229 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
4230 return Ok;
4233 return stat;
4236 /* FIXME: no ICM */
4237 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
4239 TRACE("%p %p\n", stream, image);
4241 return GdipLoadImageFromStream(stream, image);
4244 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
4246 static int calls;
4248 TRACE("(%p,%u)\n", image, propId);
4250 if(!image)
4251 return InvalidParameter;
4253 if(!(calls++))
4254 FIXME("not implemented\n");
4256 return NotImplemented;
4259 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
4261 static int calls;
4263 if (!image || !item) return InvalidParameter;
4265 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
4267 if(!(calls++))
4268 FIXME("not implemented\n");
4270 return Ok;
4273 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
4274 GDIPCONST CLSID *clsidEncoder,
4275 GDIPCONST EncoderParameters *encoderParams)
4277 GpStatus stat;
4278 IStream *stream;
4280 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
4282 if (!image || !filename|| !clsidEncoder)
4283 return InvalidParameter;
4285 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4286 if (stat != Ok)
4287 return GenericError;
4289 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4291 IStream_Release(stream);
4292 return stat;
4295 /*************************************************************************
4296 * Encoding functions -
4297 * These functions encode an image in different image file formats.
4300 static GpStatus encode_image_wic(GpImage *image, IStream* stream,
4301 REFGUID container, GDIPCONST EncoderParameters* params)
4303 GpStatus stat;
4304 GpBitmap *bitmap;
4305 IWICImagingFactory *factory;
4306 IWICBitmapEncoder *encoder;
4307 IWICBitmapFrameEncode *frameencode;
4308 IPropertyBag2 *encoderoptions;
4309 HRESULT hr;
4310 UINT width, height;
4311 PixelFormat gdipformat=0;
4312 const WICPixelFormatGUID *desired_wicformat;
4313 WICPixelFormatGUID wicformat;
4314 GpRect rc;
4315 BitmapData lockeddata;
4316 UINT i;
4318 if (image->type != ImageTypeBitmap)
4319 return GenericError;
4321 bitmap = (GpBitmap*)image;
4323 GdipGetImageWidth(image, &width);
4324 GdipGetImageHeight(image, &height);
4326 rc.X = 0;
4327 rc.Y = 0;
4328 rc.Width = width;
4329 rc.Height = height;
4331 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
4332 if (FAILED(hr))
4333 return hresult_to_status(hr);
4334 hr = IWICImagingFactory_CreateEncoder(factory, container, NULL, &encoder);
4335 IWICImagingFactory_Release(factory);
4336 if (FAILED(hr))
4337 return hresult_to_status(hr);
4339 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
4341 if (SUCCEEDED(hr))
4343 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4346 if (SUCCEEDED(hr)) /* created frame */
4348 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4350 if (SUCCEEDED(hr))
4351 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4353 if (SUCCEEDED(hr))
4354 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4356 if (SUCCEEDED(hr))
4358 for (i=0; pixel_formats[i].wic_format; i++)
4360 if (pixel_formats[i].gdip_format == bitmap->format)
4362 desired_wicformat = pixel_formats[i].wic_format;
4363 gdipformat = bitmap->format;
4364 break;
4367 if (!gdipformat)
4369 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
4370 gdipformat = PixelFormat32bppARGB;
4373 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
4374 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4377 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
4379 /* Encoder doesn't support this bitmap's format. */
4380 gdipformat = 0;
4381 for (i=0; pixel_formats[i].wic_format; i++)
4383 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
4385 gdipformat = pixel_formats[i].gdip_format;
4386 break;
4389 if (!gdipformat)
4391 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
4392 hr = E_FAIL;
4396 if (SUCCEEDED(hr))
4398 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4399 &lockeddata);
4401 if (stat == Ok)
4403 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4404 BYTE *row;
4406 /* write one row at a time in case stride is negative */
4407 row = lockeddata.Scan0;
4408 for (i=0; i<lockeddata.Height; i++)
4410 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4411 if (FAILED(hr)) break;
4412 row += lockeddata.Stride;
4415 GdipBitmapUnlockBits(bitmap, &lockeddata);
4417 else
4418 hr = E_FAIL;
4421 if (SUCCEEDED(hr))
4422 hr = IWICBitmapFrameEncode_Commit(frameencode);
4424 IWICBitmapFrameEncode_Release(frameencode);
4425 IPropertyBag2_Release(encoderoptions);
4428 if (SUCCEEDED(hr))
4429 hr = IWICBitmapEncoder_Commit(encoder);
4431 IWICBitmapEncoder_Release(encoder);
4432 return hresult_to_status(hr);
4435 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4436 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4438 return encode_image_wic(image, stream, &GUID_ContainerFormatBmp, params);
4441 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4442 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4444 return encode_image_wic(image, stream, &GUID_ContainerFormatTiff, params);
4447 static GpStatus encode_image_png(GpImage *image, IStream* stream,
4448 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4450 return encode_image_wic(image, stream, &GUID_ContainerFormatPng, params);
4453 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4454 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4456 return encode_image_wic(image, stream, &GUID_ContainerFormatJpeg, params);
4459 static GpStatus encode_image_gif(GpImage *image, IStream* stream,
4460 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4462 return encode_image_wic(image, stream, &CLSID_WICGifEncoder, params);
4465 /*****************************************************************************
4466 * GdipSaveImageToStream [GDIPLUS.@]
4468 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4469 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4471 GpStatus stat;
4472 encode_image_func encode_image;
4473 int i;
4475 TRACE("%p %p %p %p\n", image, stream, clsid, params);
4477 if(!image || !stream)
4478 return InvalidParameter;
4480 /* select correct encoder */
4481 encode_image = NULL;
4482 for (i = 0; i < NUM_CODECS; i++) {
4483 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4484 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4485 encode_image = codecs[i].encode_func;
4487 if (encode_image == NULL)
4488 return UnknownImageFormat;
4490 stat = encode_image(image, stream, clsid, params);
4492 return stat;
4495 /*****************************************************************************
4496 * GdipSaveAdd [GDIPLUS.@]
4498 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4500 FIXME("(%p,%p): stub\n", image, params);
4501 return Ok;
4504 /*****************************************************************************
4505 * GdipGetImagePalette [GDIPLUS.@]
4507 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4509 INT count;
4511 TRACE("(%p,%p,%i)\n", image, palette, size);
4513 if (!image || !palette)
4514 return InvalidParameter;
4516 count = image->palette ? image->palette->Count : 0;
4518 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4520 TRACE("<-- InsufficientBuffer\n");
4521 return InsufficientBuffer;
4524 if (image->palette)
4526 palette->Flags = image->palette->Flags;
4527 palette->Count = image->palette->Count;
4528 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4530 else
4532 palette->Flags = 0;
4533 palette->Count = 0;
4535 return Ok;
4538 /*****************************************************************************
4539 * GdipSetImagePalette [GDIPLUS.@]
4541 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4542 GDIPCONST ColorPalette *palette)
4544 ColorPalette *new_palette;
4546 TRACE("(%p,%p)\n", image, palette);
4548 if(!image || !palette || palette->Count > 256)
4549 return InvalidParameter;
4551 new_palette = heap_alloc_zero(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4552 if (!new_palette) return OutOfMemory;
4554 heap_free(image->palette);
4555 image->palette = new_palette;
4556 image->palette->Flags = palette->Flags;
4557 image->palette->Count = palette->Count;
4558 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4560 return Ok;
4563 /*************************************************************************
4564 * Encoders -
4565 * Structures that represent which formats we support for encoding.
4568 /* ImageCodecInfo creation routines taken from libgdiplus */
4569 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4570 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4571 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4572 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4573 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4574 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4576 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4577 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4578 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4579 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4580 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4581 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4583 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4584 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4585 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4586 static const WCHAR gif_format[] = {'G','I','F',0};
4587 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4588 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4590 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4591 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4592 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4593 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4594 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4595 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4597 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4598 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4599 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4600 static const WCHAR emf_format[] = {'E','M','F',0};
4601 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4602 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4604 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4605 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4606 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4607 static const WCHAR wmf_format[] = {'W','M','F',0};
4608 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4609 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4611 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4612 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4613 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4614 static const WCHAR png_format[] = {'P','N','G',0};
4615 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4616 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4618 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4619 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4620 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4621 static const WCHAR ico_format[] = {'I','C','O',0};
4622 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4623 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4625 static const struct image_codec codecs[NUM_CODECS] = {
4627 { /* BMP */
4628 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4629 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4630 /* CodecName */ bmp_codecname,
4631 /* DllName */ NULL,
4632 /* FormatDescription */ bmp_format,
4633 /* FilenameExtension */ bmp_extension,
4634 /* MimeType */ bmp_mimetype,
4635 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4636 /* Version */ 1,
4637 /* SigCount */ 1,
4638 /* SigSize */ 2,
4639 /* SigPattern */ bmp_sig_pattern,
4640 /* SigMask */ bmp_sig_mask,
4642 encode_image_BMP,
4643 decode_image_bmp,
4644 select_frame_wic
4647 { /* JPEG */
4648 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4649 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4650 /* CodecName */ jpeg_codecname,
4651 /* DllName */ NULL,
4652 /* FormatDescription */ jpeg_format,
4653 /* FilenameExtension */ jpeg_extension,
4654 /* MimeType */ jpeg_mimetype,
4655 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4656 /* Version */ 1,
4657 /* SigCount */ 1,
4658 /* SigSize */ 2,
4659 /* SigPattern */ jpeg_sig_pattern,
4660 /* SigMask */ jpeg_sig_mask,
4662 encode_image_jpeg,
4663 decode_image_jpeg,
4664 select_frame_wic
4667 { /* GIF */
4668 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4669 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4670 /* CodecName */ gif_codecname,
4671 /* DllName */ NULL,
4672 /* FormatDescription */ gif_format,
4673 /* FilenameExtension */ gif_extension,
4674 /* MimeType */ gif_mimetype,
4675 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4676 /* Version */ 1,
4677 /* SigCount */ 2,
4678 /* SigSize */ 6,
4679 /* SigPattern */ gif_sig_pattern,
4680 /* SigMask */ gif_sig_mask,
4682 encode_image_gif,
4683 decode_image_gif,
4684 select_frame_gif
4687 { /* TIFF */
4688 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4689 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4690 /* CodecName */ tiff_codecname,
4691 /* DllName */ NULL,
4692 /* FormatDescription */ tiff_format,
4693 /* FilenameExtension */ tiff_extension,
4694 /* MimeType */ tiff_mimetype,
4695 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4696 /* Version */ 1,
4697 /* SigCount */ 2,
4698 /* SigSize */ 4,
4699 /* SigPattern */ tiff_sig_pattern,
4700 /* SigMask */ tiff_sig_mask,
4702 encode_image_tiff,
4703 decode_image_tiff,
4704 select_frame_wic
4707 { /* EMF */
4708 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4709 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4710 /* CodecName */ emf_codecname,
4711 /* DllName */ NULL,
4712 /* FormatDescription */ emf_format,
4713 /* FilenameExtension */ emf_extension,
4714 /* MimeType */ emf_mimetype,
4715 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4716 /* Version */ 1,
4717 /* SigCount */ 1,
4718 /* SigSize */ 4,
4719 /* SigPattern */ emf_sig_pattern,
4720 /* SigMask */ emf_sig_mask,
4722 NULL,
4723 decode_image_olepicture_metafile,
4724 NULL
4727 { /* WMF */
4728 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4729 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4730 /* CodecName */ wmf_codecname,
4731 /* DllName */ NULL,
4732 /* FormatDescription */ wmf_format,
4733 /* FilenameExtension */ wmf_extension,
4734 /* MimeType */ wmf_mimetype,
4735 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4736 /* Version */ 1,
4737 /* SigCount */ 1,
4738 /* SigSize */ 2,
4739 /* SigPattern */ wmf_sig_pattern,
4740 /* SigMask */ wmf_sig_mask,
4742 NULL,
4743 decode_image_olepicture_metafile,
4744 NULL
4747 { /* PNG */
4748 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4749 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4750 /* CodecName */ png_codecname,
4751 /* DllName */ NULL,
4752 /* FormatDescription */ png_format,
4753 /* FilenameExtension */ png_extension,
4754 /* MimeType */ png_mimetype,
4755 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4756 /* Version */ 1,
4757 /* SigCount */ 1,
4758 /* SigSize */ 8,
4759 /* SigPattern */ png_sig_pattern,
4760 /* SigMask */ png_sig_mask,
4762 encode_image_png,
4763 decode_image_png,
4764 select_frame_wic
4767 { /* ICO */
4768 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4769 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4770 /* CodecName */ ico_codecname,
4771 /* DllName */ NULL,
4772 /* FormatDescription */ ico_format,
4773 /* FilenameExtension */ ico_extension,
4774 /* MimeType */ ico_mimetype,
4775 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4776 /* Version */ 1,
4777 /* SigCount */ 1,
4778 /* SigSize */ 4,
4779 /* SigPattern */ ico_sig_pattern,
4780 /* SigMask */ ico_sig_mask,
4782 NULL,
4783 decode_image_icon,
4784 select_frame_wic
4788 /*****************************************************************************
4789 * GdipGetImageDecodersSize [GDIPLUS.@]
4791 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
4793 int decoder_count=0;
4794 int i;
4795 TRACE("%p %p\n", numDecoders, size);
4797 if (!numDecoders || !size)
4798 return InvalidParameter;
4800 for (i=0; i<NUM_CODECS; i++)
4802 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4803 decoder_count++;
4806 *numDecoders = decoder_count;
4807 *size = decoder_count * sizeof(ImageCodecInfo);
4809 return Ok;
4812 /*****************************************************************************
4813 * GdipGetImageDecoders [GDIPLUS.@]
4815 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
4817 int i, decoder_count=0;
4818 TRACE("%u %u %p\n", numDecoders, size, decoders);
4820 if (!decoders ||
4821 size != numDecoders * sizeof(ImageCodecInfo))
4822 return GenericError;
4824 for (i=0; i<NUM_CODECS; i++)
4826 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4828 if (decoder_count == numDecoders) return GenericError;
4829 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4830 decoder_count++;
4834 if (decoder_count < numDecoders) return GenericError;
4836 return Ok;
4839 /*****************************************************************************
4840 * GdipGetImageEncodersSize [GDIPLUS.@]
4842 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4844 int encoder_count=0;
4845 int i;
4846 TRACE("%p %p\n", numEncoders, size);
4848 if (!numEncoders || !size)
4849 return InvalidParameter;
4851 for (i=0; i<NUM_CODECS; i++)
4853 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4854 encoder_count++;
4857 *numEncoders = encoder_count;
4858 *size = encoder_count * sizeof(ImageCodecInfo);
4860 return Ok;
4863 /*****************************************************************************
4864 * GdipGetImageEncoders [GDIPLUS.@]
4866 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4868 int i, encoder_count=0;
4869 TRACE("%u %u %p\n", numEncoders, size, encoders);
4871 if (!encoders ||
4872 size != numEncoders * sizeof(ImageCodecInfo))
4873 return GenericError;
4875 for (i=0; i<NUM_CODECS; i++)
4877 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4879 if (encoder_count == numEncoders) return GenericError;
4880 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4881 encoder_count++;
4885 if (encoder_count < numEncoders) return GenericError;
4887 return Ok;
4890 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
4891 GDIPCONST CLSID* clsidEncoder, UINT *size)
4893 static int calls;
4895 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
4897 if(!(calls++))
4898 FIXME("not implemented\n");
4900 *size = 0;
4902 return NotImplemented;
4905 static PixelFormat get_16bpp_format(HBITMAP hbm)
4907 BITMAPV4HEADER bmh;
4908 HDC hdc;
4909 PixelFormat result;
4911 hdc = CreateCompatibleDC(NULL);
4913 memset(&bmh, 0, sizeof(bmh));
4914 bmh.bV4Size = sizeof(bmh);
4915 bmh.bV4Width = 1;
4916 bmh.bV4Height = 1;
4917 bmh.bV4V4Compression = BI_BITFIELDS;
4918 bmh.bV4BitCount = 16;
4920 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4922 if (bmh.bV4RedMask == 0x7c00 &&
4923 bmh.bV4GreenMask == 0x3e0 &&
4924 bmh.bV4BlueMask == 0x1f)
4926 result = PixelFormat16bppRGB555;
4928 else if (bmh.bV4RedMask == 0xf800 &&
4929 bmh.bV4GreenMask == 0x7e0 &&
4930 bmh.bV4BlueMask == 0x1f)
4932 result = PixelFormat16bppRGB565;
4934 else
4936 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4937 bmh.bV4GreenMask, bmh.bV4BlueMask);
4938 result = PixelFormatUndefined;
4941 DeleteDC(hdc);
4943 return result;
4946 /*****************************************************************************
4947 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4949 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4951 BITMAP bm;
4952 GpStatus retval;
4953 PixelFormat format;
4954 BitmapData lockeddata;
4956 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4958 if(!hbm || !bitmap)
4959 return InvalidParameter;
4961 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4962 return InvalidParameter;
4964 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4965 switch(bm.bmBitsPixel) {
4966 case 1:
4967 format = PixelFormat1bppIndexed;
4968 break;
4969 case 4:
4970 format = PixelFormat4bppIndexed;
4971 break;
4972 case 8:
4973 format = PixelFormat8bppIndexed;
4974 break;
4975 case 16:
4976 format = get_16bpp_format(hbm);
4977 if (format == PixelFormatUndefined)
4978 return InvalidParameter;
4979 break;
4980 case 24:
4981 format = PixelFormat24bppRGB;
4982 break;
4983 case 32:
4984 format = PixelFormat32bppRGB;
4985 break;
4986 case 48:
4987 format = PixelFormat48bppRGB;
4988 break;
4989 default:
4990 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4991 return InvalidParameter;
4994 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4995 format, NULL, bitmap);
4997 if (retval == Ok)
4999 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
5000 format, &lockeddata);
5001 if (retval == Ok)
5003 HDC hdc;
5004 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
5005 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
5006 INT src_height;
5008 hdc = CreateCompatibleDC(NULL);
5010 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
5011 pbmi->bmiHeader.biBitCount = 0;
5013 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
5015 src_height = abs(pbmi->bmiHeader.biHeight);
5016 pbmi->bmiHeader.biHeight = -src_height;
5018 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
5020 DeleteDC(hdc);
5022 GdipBitmapUnlockBits(*bitmap, &lockeddata);
5025 if (retval == Ok && hpal)
5027 PALETTEENTRY entry[256];
5028 ColorPalette *palette=NULL;
5029 int i, num_palette_entries;
5031 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
5032 if (!num_palette_entries)
5033 retval = GenericError;
5035 palette = heap_alloc_zero(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
5036 if (!palette)
5037 retval = OutOfMemory;
5039 if (retval == Ok)
5041 palette->Flags = 0;
5042 palette->Count = num_palette_entries;
5044 for (i=0; i<num_palette_entries; i++)
5046 palette->Entries[i] = 0xff000000 | entry[i].peRed << 16 |
5047 entry[i].peGreen << 8 | entry[i].peBlue;
5050 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
5053 heap_free(palette);
5056 if (retval != Ok)
5058 GdipDisposeImage((GpImage*)*bitmap);
5059 *bitmap = NULL;
5063 return retval;
5066 /*****************************************************************************
5067 * GdipCreateEffect [GDIPLUS.@]
5069 GpStatus WINGDIPAPI GdipCreateEffect(const GUID guid, CGpEffect **effect)
5071 FIXME("(%s, %p): stub\n", debugstr_guid(&guid), effect);
5073 if(!effect)
5074 return InvalidParameter;
5076 *effect = NULL;
5078 return NotImplemented;
5081 /*****************************************************************************
5082 * GdipDeleteEffect [GDIPLUS.@]
5084 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
5086 FIXME("(%p): stub\n", effect);
5087 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5088 * in Windows's gdiplus */
5089 return NotImplemented;
5092 /*****************************************************************************
5093 * GdipSetEffectParameters [GDIPLUS.@]
5095 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
5096 const VOID *params, const UINT size)
5098 static int calls;
5100 TRACE("(%p,%p,%u)\n", effect, params, size);
5102 if(!(calls++))
5103 FIXME("not implemented\n");
5105 return NotImplemented;
5108 /*****************************************************************************
5109 * GdipGetImageFlags [GDIPLUS.@]
5111 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
5113 TRACE("%p %p\n", image, flags);
5115 if(!image || !flags)
5116 return InvalidParameter;
5118 *flags = image->flags;
5120 return Ok;
5123 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
5125 TRACE("(%d, %p)\n", control, param);
5127 switch(control){
5128 case TestControlForceBilinear:
5129 if(param)
5130 FIXME("TestControlForceBilinear not handled\n");
5131 break;
5132 case TestControlNoICM:
5133 if(param)
5134 FIXME("TestControlNoICM not handled\n");
5135 break;
5136 case TestControlGetBuildNumber:
5137 *((DWORD*)param) = 3102;
5138 break;
5141 return Ok;
5144 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
5146 TRACE("%p\n", image);
5148 return Ok;
5151 /*****************************************************************************
5152 * GdipGetImageThumbnail [GDIPLUS.@]
5154 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
5155 GpImage **ret_image, GetThumbnailImageAbort cb,
5156 VOID * cb_data)
5158 GpStatus stat;
5159 GpGraphics *graphics;
5160 UINT srcwidth, srcheight;
5162 TRACE("(%p %u %u %p %p %p)\n",
5163 image, width, height, ret_image, cb, cb_data);
5165 if (!image || !ret_image)
5166 return InvalidParameter;
5168 if (!width) width = 120;
5169 if (!height) height = 120;
5171 GdipGetImageWidth(image, &srcwidth);
5172 GdipGetImageHeight(image, &srcheight);
5174 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
5175 NULL, (GpBitmap**)ret_image);
5177 if (stat == Ok)
5179 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
5181 if (stat == Ok)
5183 stat = GdipDrawImageRectRectI(graphics, image,
5184 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
5185 NULL, NULL, NULL);
5187 GdipDeleteGraphics(graphics);
5190 if (stat != Ok)
5192 GdipDisposeImage(*ret_image);
5193 *ret_image = NULL;
5197 return stat;
5200 /*****************************************************************************
5201 * GdipImageRotateFlip [GDIPLUS.@]
5203 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
5205 GpBitmap *new_bitmap;
5206 GpBitmap *bitmap;
5207 int bpp, bytesperpixel;
5208 BOOL rotate_90, flip_x, flip_y;
5209 int src_x_offset, src_y_offset;
5210 LPBYTE src_origin;
5211 UINT x, y, width, height;
5212 BitmapData src_lock, dst_lock;
5213 GpStatus stat;
5215 TRACE("(%p, %u)\n", image, type);
5217 if (!image)
5218 return InvalidParameter;
5220 rotate_90 = type&1;
5221 flip_x = (type&6) == 2 || (type&6) == 4;
5222 flip_y = (type&3) == 1 || (type&3) == 2;
5224 if (image->type != ImageTypeBitmap)
5226 FIXME("Not implemented for type %i\n", image->type);
5227 return NotImplemented;
5230 bitmap = (GpBitmap*)image;
5231 bpp = PIXELFORMATBPP(bitmap->format);
5233 if (bpp < 8)
5235 FIXME("Not implemented for %i bit images\n", bpp);
5236 return NotImplemented;
5239 if (rotate_90)
5241 width = bitmap->height;
5242 height = bitmap->width;
5244 else
5246 width = bitmap->width;
5247 height = bitmap->height;
5250 bytesperpixel = bpp/8;
5252 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5254 if (stat != Ok)
5255 return stat;
5257 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
5259 if (stat == Ok)
5261 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5263 if (stat == Ok)
5265 LPBYTE src_row, src_pixel;
5266 LPBYTE dst_row, dst_pixel;
5268 src_origin = src_lock.Scan0;
5269 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5270 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
5272 if (rotate_90)
5274 if (flip_y) src_x_offset = -src_lock.Stride;
5275 else src_x_offset = src_lock.Stride;
5276 if (flip_x) src_y_offset = -bytesperpixel;
5277 else src_y_offset = bytesperpixel;
5279 else
5281 if (flip_x) src_x_offset = -bytesperpixel;
5282 else src_x_offset = bytesperpixel;
5283 if (flip_y) src_y_offset = -src_lock.Stride;
5284 else src_y_offset = src_lock.Stride;
5287 src_row = src_origin;
5288 dst_row = dst_lock.Scan0;
5289 for (y=0; y<height; y++)
5291 src_pixel = src_row;
5292 dst_pixel = dst_row;
5293 for (x=0; x<width; x++)
5295 /* FIXME: This could probably be faster without memcpy. */
5296 memcpy(dst_pixel, src_pixel, bytesperpixel);
5297 dst_pixel += bytesperpixel;
5298 src_pixel += src_x_offset;
5300 src_row += src_y_offset;
5301 dst_row += dst_lock.Stride;
5304 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5307 GdipBitmapUnlockBits(bitmap, &src_lock);
5310 if (stat == Ok)
5311 move_bitmap(bitmap, new_bitmap, FALSE);
5312 else
5313 GdipDisposeImage((GpImage*)new_bitmap);
5315 return stat;
5318 /*****************************************************************************
5319 * GdipImageSetAbort [GDIPLUS.@]
5321 GpStatus WINGDIPAPI GdipImageSetAbort(GpImage *image, GdiplusAbort *pabort)
5323 FIXME("(%p, %p): stub\n", image, pabort);
5324 return NotImplemented;
5327 /*****************************************************************************
5328 * GdipBitmapConvertFormat [GDIPLUS.@]
5330 GpStatus WINGDIPAPI GdipBitmapConvertFormat(GpBitmap *bitmap, PixelFormat format, DitherType dithertype,
5331 PaletteType palettetype, ColorPalette *palette, REAL alphathreshold)
5333 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap, format, dithertype, palettetype, palette, alphathreshold);
5334 return NotImplemented;