kernel32: Add GetConsoleFontInfo stub.
[wine.git] / dlls / gdiplus / image.c
blobd1a5593f97d0c32dfceeec740f19da3646208d40
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;
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);
3556 IWICMetadataReader_Release(reader);
3560 IWICMetadataBlockReader_Release(block_reader);
3563 IWICBitmapFrameDecode_Release(frame);
3566 static GpStatus initialize_decoder_wic(IStream *stream, REFGUID container, IWICBitmapDecoder **decoder)
3568 IWICImagingFactory *factory;
3569 HRESULT hr;
3571 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
3573 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3574 if (FAILED(hr)) return hresult_to_status(hr);
3575 hr = IWICImagingFactory_CreateDecoder(factory, container, NULL, decoder);
3576 IWICImagingFactory_Release(factory);
3577 if (FAILED(hr)) return hresult_to_status(hr);
3579 hr = IWICBitmapDecoder_Initialize(*decoder, stream, WICDecodeMetadataCacheOnLoad);
3580 if (FAILED(hr)) return hresult_to_status(hr);
3581 return Ok;
3584 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3586 static GpStatus decode_frame_wic(IWICBitmapDecoder *decoder, BOOL force_conversion,
3587 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3589 GpStatus status=Ok;
3590 GpBitmap *bitmap;
3591 HRESULT hr;
3592 IWICBitmapFrameDecode *frame;
3593 IWICBitmapSource *source=NULL;
3594 IWICMetadataBlockReader *block_reader;
3595 WICPixelFormatGUID wic_format;
3596 PixelFormat gdip_format=0;
3597 ColorPalette *palette = NULL;
3598 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3599 int i;
3600 UINT width, height, frame_count;
3601 BitmapData lockeddata;
3602 WICRect wrc;
3604 TRACE("%p,%u,%p\n", decoder, active_frame, image);
3606 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3607 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3608 if (SUCCEEDED(hr)) /* got frame */
3610 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3612 if (SUCCEEDED(hr))
3614 if (!force_conversion)
3616 for (i=0; pixel_formats[i].wic_format; i++)
3618 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3620 source = (IWICBitmapSource*)frame;
3621 IWICBitmapSource_AddRef(source);
3622 gdip_format = pixel_formats[i].gdip_format;
3623 palette_type = pixel_formats[i].palette_type;
3624 break;
3628 if (!source)
3630 /* unknown format; fall back on 32bppARGB */
3631 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3632 gdip_format = PixelFormat32bppARGB;
3634 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3637 if (SUCCEEDED(hr)) /* got source */
3639 hr = IWICBitmapSource_GetSize(source, &width, &height);
3641 if (SUCCEEDED(hr))
3642 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3643 NULL, &bitmap);
3645 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3647 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3648 gdip_format, &lockeddata);
3649 if (status == Ok) /* locked bitmap */
3651 wrc.X = 0;
3652 wrc.Width = width;
3653 wrc.Height = 1;
3654 for (i=0; i<height; i++)
3656 wrc.Y = i;
3657 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3658 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3659 if (FAILED(hr)) break;
3662 GdipBitmapUnlockBits(bitmap, &lockeddata);
3665 if (SUCCEEDED(hr) && status == Ok)
3666 *image = (GpImage*)bitmap;
3667 else
3669 *image = NULL;
3670 GdipDisposeImage((GpImage*)bitmap);
3673 if (SUCCEEDED(hr) && status == Ok)
3675 double dpix, dpiy;
3676 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3677 if (SUCCEEDED(hr))
3679 bitmap->image.xres = dpix;
3680 bitmap->image.yres = dpiy;
3682 hr = S_OK;
3686 IWICBitmapSource_Release(source);
3689 if (SUCCEEDED(hr)) {
3690 bitmap->metadata_reader = NULL;
3692 if (metadata_reader)
3693 metadata_reader(bitmap, decoder, active_frame);
3694 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3696 UINT block_count = 0;
3697 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3698 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3699 IWICMetadataBlockReader_Release(block_reader);
3702 palette = get_palette(frame, palette_type);
3703 IWICBitmapFrameDecode_Release(frame);
3707 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3709 if (status == Ok)
3711 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3712 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3713 bitmap->image.frame_count = frame_count;
3714 bitmap->image.current_frame = active_frame;
3715 bitmap->image.decoder = decoder;
3716 IWICBitmapDecoder_AddRef(decoder);
3717 if (palette)
3719 heap_free(bitmap->image.palette);
3720 bitmap->image.palette = palette;
3722 else
3724 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3725 bitmap->image.palette->Flags = 0;
3727 TRACE("=> %p\n", *image);
3730 return status;
3733 static GpStatus decode_image_wic(IStream *stream, REFGUID container,
3734 metadata_reader_func metadata_reader, GpImage **image)
3736 IWICBitmapDecoder *decoder;
3737 GpStatus status;
3739 status = initialize_decoder_wic(stream, container, &decoder);
3740 if(status != Ok)
3741 return status;
3743 status = decode_frame_wic(decoder, FALSE, 0, metadata_reader, image);
3744 IWICBitmapDecoder_Release(decoder);
3745 return status;
3748 static GpStatus select_frame_wic(GpImage *image, UINT active_frame)
3750 GpImage *new_image;
3751 GpStatus status;
3753 status = decode_frame_wic(image->decoder, FALSE, active_frame, NULL, &new_image);
3754 if(status != Ok)
3755 return status;
3757 memcpy(&new_image->format, &image->format, sizeof(GUID));
3758 free_image_data(image);
3759 if (image->type == ImageTypeBitmap)
3760 *(GpBitmap *)image = *(GpBitmap *)new_image;
3761 else if (image->type == ImageTypeMetafile)
3762 *(GpMetafile *)image = *(GpMetafile *)new_image;
3763 new_image->type = ~0;
3764 heap_free(new_image);
3765 return Ok;
3768 static HRESULT get_gif_frame_rect(IWICBitmapFrameDecode *frame,
3769 UINT *left, UINT *top, UINT *width, UINT *height)
3771 static const WCHAR leftW[] = {'L','e','f','t',0};
3772 static const WCHAR topW[] = {'T','o','p',0};
3774 *left = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, leftW);
3775 *top = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, topW);
3777 return IWICBitmapFrameDecode_GetSize(frame, width, height);
3780 static HRESULT blit_gif_frame(GpBitmap *bitmap, IWICBitmapFrameDecode *frame, BOOL first_frame)
3782 UINT i, j, left, top, width, height;
3783 IWICBitmapSource *source;
3784 BYTE *new_bits;
3785 HRESULT hr;
3787 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3788 if(FAILED(hr))
3789 return hr;
3791 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3792 if(FAILED(hr))
3793 return hr;
3795 new_bits = heap_alloc_zero(width*height*4);
3796 if(!new_bits)
3797 return E_OUTOFMEMORY;
3799 hr = IWICBitmapSource_CopyPixels(source, NULL, width*4, width*height*4, new_bits);
3800 IWICBitmapSource_Release(source);
3801 if(FAILED(hr)) {
3802 heap_free(new_bits);
3803 return hr;
3806 for(i=0; i<height && i+top<bitmap->height; i++) {
3807 for(j=0; j<width && j+left<bitmap->width; j++) {
3808 DWORD *src = (DWORD*)(new_bits+i*width*4+j*4);
3809 DWORD *dst = (DWORD*)(bitmap->bits+(i+top)*bitmap->stride+(j+left)*4);
3811 if(first_frame || *src>>24 != 0)
3812 *dst = *src;
3815 heap_free(new_bits);
3816 return hr;
3819 static DWORD get_gif_background_color(GpBitmap *bitmap)
3821 BYTE bgcolor_idx = 0;
3822 UINT i;
3824 for(i=0; i<bitmap->prop_count; i++) {
3825 if(bitmap->prop_item[i].id == PropertyTagIndexBackground) {
3826 bgcolor_idx = *(BYTE*)bitmap->prop_item[i].value;
3827 break;
3831 for(i=0; i<bitmap->prop_count; i++) {
3832 if(bitmap->prop_item[i].id == PropertyTagIndexTransparent) {
3833 BYTE transparent_idx;
3834 transparent_idx = *(BYTE*)bitmap->prop_item[i].value;
3836 if(transparent_idx == bgcolor_idx)
3837 return 0;
3841 for(i=0; i<bitmap->prop_count; i++) {
3842 if(bitmap->prop_item[i].id == PropertyTagGlobalPalette) {
3843 if(bitmap->prop_item[i].length/3 > bgcolor_idx) {
3844 BYTE *color = ((BYTE*)bitmap->prop_item[i].value)+bgcolor_idx*3;
3845 return color[2] + (color[1]<<8) + (color[0]<<16) + (0xffu<<24);
3847 break;
3851 FIXME("can't get gif background color\n");
3852 return 0xffffffff;
3855 static GpStatus select_frame_gif(GpImage* image, UINT active_frame)
3857 static const WCHAR disposalW[] = {'D','i','s','p','o','s','a','l',0};
3859 GpBitmap *bitmap = (GpBitmap*)image;
3860 IWICBitmapFrameDecode *frame;
3861 int cur_frame=0, disposal;
3862 BOOL bgcolor_set = FALSE;
3863 DWORD bgcolor = 0;
3864 HRESULT hr;
3866 if(active_frame > image->current_frame) {
3867 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, image->current_frame, &frame);
3868 if(FAILED(hr))
3869 return hresult_to_status(hr);
3870 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3871 IWICBitmapFrameDecode_Release(frame);
3873 if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND)
3874 cur_frame = image->current_frame;
3875 else if(disposal != GIF_DISPOSE_RESTORE_TO_PREV)
3876 cur_frame = image->current_frame+1;
3879 while(cur_frame != active_frame) {
3880 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, cur_frame, &frame);
3881 if(FAILED(hr))
3882 return hresult_to_status(hr);
3883 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3885 if(disposal==GIF_DISPOSE_UNSPECIFIED || disposal==GIF_DISPOSE_DO_NOT_DISPOSE) {
3886 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3887 if(FAILED(hr))
3888 return hresult_to_status(hr);
3889 }else if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND) {
3890 UINT left, top, width, height, i, j;
3892 if(!bgcolor_set) {
3893 bgcolor = get_gif_background_color(bitmap);
3894 bgcolor_set = TRUE;
3897 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3898 if(FAILED(hr))
3899 return hresult_to_status(hr);
3900 for(i=top; i<top+height && i<bitmap->height; i++) {
3901 DWORD *bits = (DWORD*)(bitmap->bits+i*bitmap->stride);
3902 for(j=left; j<left+width && j<bitmap->width; j++)
3903 bits[j] = bgcolor;
3907 IWICBitmapFrameDecode_Release(frame);
3908 cur_frame++;
3911 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, active_frame, &frame);
3912 if(FAILED(hr))
3913 return hresult_to_status(hr);
3915 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3916 IWICBitmapFrameDecode_Release(frame);
3917 if(FAILED(hr))
3918 return hresult_to_status(hr);
3920 image->current_frame = active_frame;
3921 return Ok;
3924 static GpStatus decode_image_icon(IStream* stream, GpImage **image)
3926 return decode_image_wic(stream, &GUID_ContainerFormatIco, NULL, image);
3929 static GpStatus decode_image_bmp(IStream* stream, GpImage **image)
3931 GpStatus status;
3932 GpBitmap* bitmap;
3934 status = decode_image_wic(stream, &GUID_ContainerFormatBmp, NULL, image);
3936 bitmap = (GpBitmap*)*image;
3938 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3940 /* WIC supports bmp files with alpha, but gdiplus does not */
3941 bitmap->format = PixelFormat32bppRGB;
3944 return status;
3947 static GpStatus decode_image_jpeg(IStream* stream, GpImage **image)
3949 return decode_image_wic(stream, &GUID_ContainerFormatJpeg, NULL, image);
3952 static GpStatus decode_image_png(IStream* stream, GpImage **image)
3954 return decode_image_wic(stream, &GUID_ContainerFormatPng, png_metadata_reader, image);
3957 static GpStatus decode_image_gif(IStream* stream, GpImage **image)
3959 IWICBitmapDecoder *decoder;
3960 UINT frame_count;
3961 GpStatus status;
3962 HRESULT hr;
3964 status = initialize_decoder_wic(stream, &GUID_ContainerFormatGif, &decoder);
3965 if(status != Ok)
3966 return status;
3968 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3969 if(FAILED(hr))
3970 return hresult_to_status(hr);
3972 status = decode_frame_wic(decoder, frame_count > 1, 0, gif_metadata_reader, image);
3973 IWICBitmapDecoder_Release(decoder);
3974 if(status != Ok)
3975 return status;
3977 if(frame_count > 1) {
3978 heap_free((*image)->palette);
3979 (*image)->palette = NULL;
3981 return Ok;
3984 static GpStatus decode_image_tiff(IStream* stream, GpImage **image)
3986 return decode_image_wic(stream, &GUID_ContainerFormatTiff, NULL, image);
3989 static GpStatus decode_image_olepicture_metafile(IStream* stream, GpImage **image)
3991 IPicture *pic;
3993 TRACE("%p %p\n", stream, image);
3995 if(!stream || !image)
3996 return InvalidParameter;
3998 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3999 (LPVOID*) &pic) != S_OK){
4000 TRACE("Could not load picture\n");
4001 return GenericError;
4004 /* FIXME: missing initialization code */
4005 *image = heap_alloc_zero(sizeof(GpMetafile));
4006 if(!*image) return OutOfMemory;
4007 (*image)->type = ImageTypeMetafile;
4008 (*image)->decoder = NULL;
4009 (*image)->picture = pic;
4010 (*image)->flags = ImageFlagsNone;
4011 (*image)->frame_count = 1;
4012 (*image)->current_frame = 0;
4013 (*image)->palette = NULL;
4015 TRACE("<-- %p\n", *image);
4017 return Ok;
4020 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
4021 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
4023 typedef GpStatus (*decode_image_func)(IStream *stream, GpImage **image);
4025 typedef GpStatus (*select_image_func)(GpImage *image, UINT active_frame);
4027 typedef struct image_codec {
4028 ImageCodecInfo info;
4029 encode_image_func encode_func;
4030 decode_image_func decode_func;
4031 select_image_func select_func;
4032 } image_codec;
4034 typedef enum {
4035 BMP,
4036 JPEG,
4037 GIF,
4038 TIFF,
4039 EMF,
4040 WMF,
4041 PNG,
4042 ICO,
4043 NUM_CODECS
4044 } ImageFormat;
4046 static const struct image_codec codecs[NUM_CODECS];
4048 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
4050 BYTE signature[8];
4051 const BYTE *pattern, *mask;
4052 LARGE_INTEGER seek;
4053 HRESULT hr;
4054 UINT bytesread;
4055 int i;
4056 DWORD j, sig;
4058 /* seek to the start of the stream */
4059 seek.QuadPart = 0;
4060 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4061 if (FAILED(hr)) return hresult_to_status(hr);
4063 /* read the first 8 bytes */
4064 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4065 hr = IStream_Read(stream, signature, 8, &bytesread);
4066 if (FAILED(hr)) return hresult_to_status(hr);
4067 if (hr == S_FALSE || bytesread == 0) return GenericError;
4069 for (i = 0; i < NUM_CODECS; i++) {
4070 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4071 bytesread >= codecs[i].info.SigSize)
4073 for (sig=0; sig<codecs[i].info.SigCount; sig++)
4075 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
4076 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
4077 for (j=0; j<codecs[i].info.SigSize; j++)
4078 if ((signature[j] & mask[j]) != pattern[j])
4079 break;
4080 if (j == codecs[i].info.SigSize)
4082 *result = &codecs[i];
4083 return Ok;
4089 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
4090 signature[0],signature[1],signature[2],signature[3],
4091 signature[4],signature[5],signature[6],signature[7]);
4093 return GenericError;
4096 static GpStatus get_decoder_info_from_image(GpImage *image, const struct image_codec **result)
4098 int i;
4100 for (i = 0; i < NUM_CODECS; i++) {
4101 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4102 IsEqualIID(&codecs[i].info.FormatID, &image->format))
4104 *result = &codecs[i];
4105 return Ok;
4109 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image->format));
4110 return GenericError;
4113 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
4114 UINT frame)
4116 GpStatus stat;
4117 const struct image_codec *codec = NULL;
4119 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
4121 if (!image || !dimensionID)
4122 return InvalidParameter;
4124 if (frame >= image->frame_count)
4126 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
4127 return InvalidParameter;
4130 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
4132 WARN("invalid image type %d\n", image->type);
4133 return InvalidParameter;
4136 if (image->current_frame == frame)
4137 return Ok;
4139 if (!image->decoder)
4141 TRACE("image doesn't have an associated decoder\n");
4142 return Ok;
4145 /* choose an appropriate image decoder */
4146 stat = get_decoder_info_from_image(image, &codec);
4147 if (stat != Ok)
4149 WARN("can't find decoder info\n");
4150 return stat;
4153 return codec->select_func(image, frame);
4156 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
4158 GpStatus stat;
4159 LARGE_INTEGER seek;
4160 HRESULT hr;
4161 const struct image_codec *codec=NULL;
4163 /* choose an appropriate image decoder */
4164 stat = get_decoder_info(stream, &codec);
4165 if (stat != Ok) return stat;
4167 /* seek to the start of the stream */
4168 seek.QuadPart = 0;
4169 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4170 if (FAILED(hr)) return hresult_to_status(hr);
4172 /* call on the image decoder to do the real work */
4173 stat = codec->decode_func(stream, image);
4175 /* take note of the original data format */
4176 if (stat == Ok)
4178 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
4179 return Ok;
4182 return stat;
4185 /* FIXME: no ICM */
4186 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
4188 TRACE("%p %p\n", stream, image);
4190 return GdipLoadImageFromStream(stream, image);
4193 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
4195 static int calls;
4197 TRACE("(%p,%u)\n", image, propId);
4199 if(!image)
4200 return InvalidParameter;
4202 if(!(calls++))
4203 FIXME("not implemented\n");
4205 return NotImplemented;
4208 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
4210 static int calls;
4212 if (!image || !item) return InvalidParameter;
4214 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
4216 if(!(calls++))
4217 FIXME("not implemented\n");
4219 return Ok;
4222 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
4223 GDIPCONST CLSID *clsidEncoder,
4224 GDIPCONST EncoderParameters *encoderParams)
4226 GpStatus stat;
4227 IStream *stream;
4229 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
4231 if (!image || !filename|| !clsidEncoder)
4232 return InvalidParameter;
4234 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4235 if (stat != Ok)
4236 return GenericError;
4238 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4240 IStream_Release(stream);
4241 return stat;
4244 /*************************************************************************
4245 * Encoding functions -
4246 * These functions encode an image in different image file formats.
4249 static GpStatus encode_image_wic(GpImage *image, IStream* stream,
4250 REFGUID container, GDIPCONST EncoderParameters* params)
4252 GpStatus stat;
4253 GpBitmap *bitmap;
4254 IWICImagingFactory *factory;
4255 IWICBitmapEncoder *encoder;
4256 IWICBitmapFrameEncode *frameencode;
4257 IPropertyBag2 *encoderoptions;
4258 HRESULT hr;
4259 UINT width, height;
4260 PixelFormat gdipformat=0;
4261 const WICPixelFormatGUID *desired_wicformat;
4262 WICPixelFormatGUID wicformat;
4263 GpRect rc;
4264 BitmapData lockeddata;
4265 UINT i;
4267 if (image->type != ImageTypeBitmap)
4268 return GenericError;
4270 bitmap = (GpBitmap*)image;
4272 GdipGetImageWidth(image, &width);
4273 GdipGetImageHeight(image, &height);
4275 rc.X = 0;
4276 rc.Y = 0;
4277 rc.Width = width;
4278 rc.Height = height;
4280 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
4281 if (FAILED(hr))
4282 return hresult_to_status(hr);
4283 hr = IWICImagingFactory_CreateEncoder(factory, container, NULL, &encoder);
4284 IWICImagingFactory_Release(factory);
4285 if (FAILED(hr))
4286 return hresult_to_status(hr);
4288 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
4290 if (SUCCEEDED(hr))
4292 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4295 if (SUCCEEDED(hr)) /* created frame */
4297 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4299 if (SUCCEEDED(hr))
4300 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4302 if (SUCCEEDED(hr))
4303 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4305 if (SUCCEEDED(hr))
4307 for (i=0; pixel_formats[i].wic_format; i++)
4309 if (pixel_formats[i].gdip_format == bitmap->format)
4311 desired_wicformat = pixel_formats[i].wic_format;
4312 gdipformat = bitmap->format;
4313 break;
4316 if (!gdipformat)
4318 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
4319 gdipformat = PixelFormat32bppARGB;
4322 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
4323 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4326 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
4328 /* Encoder doesn't support this bitmap's format. */
4329 gdipformat = 0;
4330 for (i=0; pixel_formats[i].wic_format; i++)
4332 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
4334 gdipformat = pixel_formats[i].gdip_format;
4335 break;
4338 if (!gdipformat)
4340 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
4341 hr = E_FAIL;
4345 if (SUCCEEDED(hr))
4347 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4348 &lockeddata);
4350 if (stat == Ok)
4352 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4353 BYTE *row;
4355 /* write one row at a time in case stride is negative */
4356 row = lockeddata.Scan0;
4357 for (i=0; i<lockeddata.Height; i++)
4359 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4360 if (FAILED(hr)) break;
4361 row += lockeddata.Stride;
4364 GdipBitmapUnlockBits(bitmap, &lockeddata);
4366 else
4367 hr = E_FAIL;
4370 if (SUCCEEDED(hr))
4371 hr = IWICBitmapFrameEncode_Commit(frameencode);
4373 IWICBitmapFrameEncode_Release(frameencode);
4374 IPropertyBag2_Release(encoderoptions);
4377 if (SUCCEEDED(hr))
4378 hr = IWICBitmapEncoder_Commit(encoder);
4380 IWICBitmapEncoder_Release(encoder);
4381 return hresult_to_status(hr);
4384 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4385 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4387 return encode_image_wic(image, stream, &GUID_ContainerFormatBmp, params);
4390 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4391 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4393 return encode_image_wic(image, stream, &GUID_ContainerFormatTiff, params);
4396 static GpStatus encode_image_png(GpImage *image, IStream* stream,
4397 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4399 return encode_image_wic(image, stream, &GUID_ContainerFormatPng, params);
4402 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4403 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4405 return encode_image_wic(image, stream, &GUID_ContainerFormatJpeg, params);
4408 static GpStatus encode_image_gif(GpImage *image, IStream* stream,
4409 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4411 return encode_image_wic(image, stream, &CLSID_WICGifEncoder, params);
4414 /*****************************************************************************
4415 * GdipSaveImageToStream [GDIPLUS.@]
4417 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4418 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4420 GpStatus stat;
4421 encode_image_func encode_image;
4422 int i;
4424 TRACE("%p %p %p %p\n", image, stream, clsid, params);
4426 if(!image || !stream)
4427 return InvalidParameter;
4429 /* select correct encoder */
4430 encode_image = NULL;
4431 for (i = 0; i < NUM_CODECS; i++) {
4432 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4433 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4434 encode_image = codecs[i].encode_func;
4436 if (encode_image == NULL)
4437 return UnknownImageFormat;
4439 stat = encode_image(image, stream, clsid, params);
4441 return stat;
4444 /*****************************************************************************
4445 * GdipSaveAdd [GDIPLUS.@]
4447 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4449 FIXME("(%p,%p): stub\n", image, params);
4450 return Ok;
4453 /*****************************************************************************
4454 * GdipGetImagePalette [GDIPLUS.@]
4456 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4458 INT count;
4460 TRACE("(%p,%p,%i)\n", image, palette, size);
4462 if (!image || !palette)
4463 return InvalidParameter;
4465 count = image->palette ? image->palette->Count : 0;
4467 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4469 TRACE("<-- InsufficientBuffer\n");
4470 return InsufficientBuffer;
4473 if (image->palette)
4475 palette->Flags = image->palette->Flags;
4476 palette->Count = image->palette->Count;
4477 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4479 else
4481 palette->Flags = 0;
4482 palette->Count = 0;
4484 return Ok;
4487 /*****************************************************************************
4488 * GdipSetImagePalette [GDIPLUS.@]
4490 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4491 GDIPCONST ColorPalette *palette)
4493 ColorPalette *new_palette;
4495 TRACE("(%p,%p)\n", image, palette);
4497 if(!image || !palette || palette->Count > 256)
4498 return InvalidParameter;
4500 new_palette = heap_alloc_zero(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4501 if (!new_palette) return OutOfMemory;
4503 heap_free(image->palette);
4504 image->palette = new_palette;
4505 image->palette->Flags = palette->Flags;
4506 image->palette->Count = palette->Count;
4507 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4509 return Ok;
4512 /*************************************************************************
4513 * Encoders -
4514 * Structures that represent which formats we support for encoding.
4517 /* ImageCodecInfo creation routines taken from libgdiplus */
4518 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4519 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4520 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4521 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4522 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4523 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4525 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4526 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4527 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4528 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4529 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4530 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4532 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4533 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4534 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4535 static const WCHAR gif_format[] = {'G','I','F',0};
4536 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4537 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4539 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4540 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4541 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4542 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4543 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4544 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4546 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4547 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4548 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4549 static const WCHAR emf_format[] = {'E','M','F',0};
4550 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4551 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4553 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4554 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4555 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4556 static const WCHAR wmf_format[] = {'W','M','F',0};
4557 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4558 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4560 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4561 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4562 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4563 static const WCHAR png_format[] = {'P','N','G',0};
4564 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4565 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4567 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4568 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4569 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4570 static const WCHAR ico_format[] = {'I','C','O',0};
4571 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4572 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4574 static const struct image_codec codecs[NUM_CODECS] = {
4576 { /* BMP */
4577 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4578 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4579 /* CodecName */ bmp_codecname,
4580 /* DllName */ NULL,
4581 /* FormatDescription */ bmp_format,
4582 /* FilenameExtension */ bmp_extension,
4583 /* MimeType */ bmp_mimetype,
4584 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4585 /* Version */ 1,
4586 /* SigCount */ 1,
4587 /* SigSize */ 2,
4588 /* SigPattern */ bmp_sig_pattern,
4589 /* SigMask */ bmp_sig_mask,
4591 encode_image_BMP,
4592 decode_image_bmp,
4593 select_frame_wic
4596 { /* JPEG */
4597 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4598 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4599 /* CodecName */ jpeg_codecname,
4600 /* DllName */ NULL,
4601 /* FormatDescription */ jpeg_format,
4602 /* FilenameExtension */ jpeg_extension,
4603 /* MimeType */ jpeg_mimetype,
4604 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4605 /* Version */ 1,
4606 /* SigCount */ 1,
4607 /* SigSize */ 2,
4608 /* SigPattern */ jpeg_sig_pattern,
4609 /* SigMask */ jpeg_sig_mask,
4611 encode_image_jpeg,
4612 decode_image_jpeg,
4613 select_frame_wic
4616 { /* GIF */
4617 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4618 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4619 /* CodecName */ gif_codecname,
4620 /* DllName */ NULL,
4621 /* FormatDescription */ gif_format,
4622 /* FilenameExtension */ gif_extension,
4623 /* MimeType */ gif_mimetype,
4624 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4625 /* Version */ 1,
4626 /* SigCount */ 2,
4627 /* SigSize */ 6,
4628 /* SigPattern */ gif_sig_pattern,
4629 /* SigMask */ gif_sig_mask,
4631 encode_image_gif,
4632 decode_image_gif,
4633 select_frame_gif
4636 { /* TIFF */
4637 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4638 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4639 /* CodecName */ tiff_codecname,
4640 /* DllName */ NULL,
4641 /* FormatDescription */ tiff_format,
4642 /* FilenameExtension */ tiff_extension,
4643 /* MimeType */ tiff_mimetype,
4644 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4645 /* Version */ 1,
4646 /* SigCount */ 2,
4647 /* SigSize */ 4,
4648 /* SigPattern */ tiff_sig_pattern,
4649 /* SigMask */ tiff_sig_mask,
4651 encode_image_tiff,
4652 decode_image_tiff,
4653 select_frame_wic
4656 { /* EMF */
4657 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4658 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4659 /* CodecName */ emf_codecname,
4660 /* DllName */ NULL,
4661 /* FormatDescription */ emf_format,
4662 /* FilenameExtension */ emf_extension,
4663 /* MimeType */ emf_mimetype,
4664 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4665 /* Version */ 1,
4666 /* SigCount */ 1,
4667 /* SigSize */ 4,
4668 /* SigPattern */ emf_sig_pattern,
4669 /* SigMask */ emf_sig_mask,
4671 NULL,
4672 decode_image_olepicture_metafile,
4673 NULL
4676 { /* WMF */
4677 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4678 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4679 /* CodecName */ wmf_codecname,
4680 /* DllName */ NULL,
4681 /* FormatDescription */ wmf_format,
4682 /* FilenameExtension */ wmf_extension,
4683 /* MimeType */ wmf_mimetype,
4684 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4685 /* Version */ 1,
4686 /* SigCount */ 1,
4687 /* SigSize */ 2,
4688 /* SigPattern */ wmf_sig_pattern,
4689 /* SigMask */ wmf_sig_mask,
4691 NULL,
4692 decode_image_olepicture_metafile,
4693 NULL
4696 { /* PNG */
4697 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4698 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4699 /* CodecName */ png_codecname,
4700 /* DllName */ NULL,
4701 /* FormatDescription */ png_format,
4702 /* FilenameExtension */ png_extension,
4703 /* MimeType */ png_mimetype,
4704 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4705 /* Version */ 1,
4706 /* SigCount */ 1,
4707 /* SigSize */ 8,
4708 /* SigPattern */ png_sig_pattern,
4709 /* SigMask */ png_sig_mask,
4711 encode_image_png,
4712 decode_image_png,
4713 select_frame_wic
4716 { /* ICO */
4717 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4718 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4719 /* CodecName */ ico_codecname,
4720 /* DllName */ NULL,
4721 /* FormatDescription */ ico_format,
4722 /* FilenameExtension */ ico_extension,
4723 /* MimeType */ ico_mimetype,
4724 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4725 /* Version */ 1,
4726 /* SigCount */ 1,
4727 /* SigSize */ 4,
4728 /* SigPattern */ ico_sig_pattern,
4729 /* SigMask */ ico_sig_mask,
4731 NULL,
4732 decode_image_icon,
4733 select_frame_wic
4737 /*****************************************************************************
4738 * GdipGetImageDecodersSize [GDIPLUS.@]
4740 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
4742 int decoder_count=0;
4743 int i;
4744 TRACE("%p %p\n", numDecoders, size);
4746 if (!numDecoders || !size)
4747 return InvalidParameter;
4749 for (i=0; i<NUM_CODECS; i++)
4751 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4752 decoder_count++;
4755 *numDecoders = decoder_count;
4756 *size = decoder_count * sizeof(ImageCodecInfo);
4758 return Ok;
4761 /*****************************************************************************
4762 * GdipGetImageDecoders [GDIPLUS.@]
4764 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
4766 int i, decoder_count=0;
4767 TRACE("%u %u %p\n", numDecoders, size, decoders);
4769 if (!decoders ||
4770 size != numDecoders * sizeof(ImageCodecInfo))
4771 return GenericError;
4773 for (i=0; i<NUM_CODECS; i++)
4775 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4777 if (decoder_count == numDecoders) return GenericError;
4778 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4779 decoder_count++;
4783 if (decoder_count < numDecoders) return GenericError;
4785 return Ok;
4788 /*****************************************************************************
4789 * GdipGetImageEncodersSize [GDIPLUS.@]
4791 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4793 int encoder_count=0;
4794 int i;
4795 TRACE("%p %p\n", numEncoders, size);
4797 if (!numEncoders || !size)
4798 return InvalidParameter;
4800 for (i=0; i<NUM_CODECS; i++)
4802 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4803 encoder_count++;
4806 *numEncoders = encoder_count;
4807 *size = encoder_count * sizeof(ImageCodecInfo);
4809 return Ok;
4812 /*****************************************************************************
4813 * GdipGetImageEncoders [GDIPLUS.@]
4815 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4817 int i, encoder_count=0;
4818 TRACE("%u %u %p\n", numEncoders, size, encoders);
4820 if (!encoders ||
4821 size != numEncoders * sizeof(ImageCodecInfo))
4822 return GenericError;
4824 for (i=0; i<NUM_CODECS; i++)
4826 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4828 if (encoder_count == numEncoders) return GenericError;
4829 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4830 encoder_count++;
4834 if (encoder_count < numEncoders) return GenericError;
4836 return Ok;
4839 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
4840 GDIPCONST CLSID* clsidEncoder, UINT *size)
4842 static int calls;
4844 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
4846 if(!(calls++))
4847 FIXME("not implemented\n");
4849 *size = 0;
4851 return NotImplemented;
4854 static PixelFormat get_16bpp_format(HBITMAP hbm)
4856 BITMAPV4HEADER bmh;
4857 HDC hdc;
4858 PixelFormat result;
4860 hdc = CreateCompatibleDC(NULL);
4862 memset(&bmh, 0, sizeof(bmh));
4863 bmh.bV4Size = sizeof(bmh);
4864 bmh.bV4Width = 1;
4865 bmh.bV4Height = 1;
4866 bmh.bV4V4Compression = BI_BITFIELDS;
4867 bmh.bV4BitCount = 16;
4869 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4871 if (bmh.bV4RedMask == 0x7c00 &&
4872 bmh.bV4GreenMask == 0x3e0 &&
4873 bmh.bV4BlueMask == 0x1f)
4875 result = PixelFormat16bppRGB555;
4877 else if (bmh.bV4RedMask == 0xf800 &&
4878 bmh.bV4GreenMask == 0x7e0 &&
4879 bmh.bV4BlueMask == 0x1f)
4881 result = PixelFormat16bppRGB565;
4883 else
4885 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4886 bmh.bV4GreenMask, bmh.bV4BlueMask);
4887 result = PixelFormatUndefined;
4890 DeleteDC(hdc);
4892 return result;
4895 /*****************************************************************************
4896 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4898 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4900 BITMAP bm;
4901 GpStatus retval;
4902 PixelFormat format;
4903 BitmapData lockeddata;
4905 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4907 if(!hbm || !bitmap)
4908 return InvalidParameter;
4910 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4911 return InvalidParameter;
4913 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4914 switch(bm.bmBitsPixel) {
4915 case 1:
4916 format = PixelFormat1bppIndexed;
4917 break;
4918 case 4:
4919 format = PixelFormat4bppIndexed;
4920 break;
4921 case 8:
4922 format = PixelFormat8bppIndexed;
4923 break;
4924 case 16:
4925 format = get_16bpp_format(hbm);
4926 if (format == PixelFormatUndefined)
4927 return InvalidParameter;
4928 break;
4929 case 24:
4930 format = PixelFormat24bppRGB;
4931 break;
4932 case 32:
4933 format = PixelFormat32bppRGB;
4934 break;
4935 case 48:
4936 format = PixelFormat48bppRGB;
4937 break;
4938 default:
4939 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4940 return InvalidParameter;
4943 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4944 format, NULL, bitmap);
4946 if (retval == Ok)
4948 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4949 format, &lockeddata);
4950 if (retval == Ok)
4952 HDC hdc;
4953 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
4954 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
4955 INT src_height;
4957 hdc = CreateCompatibleDC(NULL);
4959 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4960 pbmi->bmiHeader.biBitCount = 0;
4962 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4964 src_height = abs(pbmi->bmiHeader.biHeight);
4965 pbmi->bmiHeader.biHeight = -src_height;
4967 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
4969 DeleteDC(hdc);
4971 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4974 if (retval == Ok && hpal)
4976 PALETTEENTRY entry[256];
4977 ColorPalette *palette=NULL;
4978 int i, num_palette_entries;
4980 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
4981 if (!num_palette_entries)
4982 retval = GenericError;
4984 palette = heap_alloc_zero(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4985 if (!palette)
4986 retval = OutOfMemory;
4988 if (retval == Ok)
4990 palette->Flags = 0;
4991 palette->Count = num_palette_entries;
4993 for (i=0; i<num_palette_entries; i++)
4995 palette->Entries[i] = 0xff000000 | entry[i].peRed << 16 |
4996 entry[i].peGreen << 8 | entry[i].peBlue;
4999 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
5002 heap_free(palette);
5005 if (retval != Ok)
5007 GdipDisposeImage((GpImage*)*bitmap);
5008 *bitmap = NULL;
5012 return retval;
5015 /*****************************************************************************
5016 * GdipCreateEffect [GDIPLUS.@]
5018 GpStatus WINGDIPAPI GdipCreateEffect(const GUID guid, CGpEffect **effect)
5020 FIXME("(%s, %p): stub\n", debugstr_guid(&guid), effect);
5022 if(!effect)
5023 return InvalidParameter;
5025 *effect = NULL;
5027 return NotImplemented;
5030 /*****************************************************************************
5031 * GdipDeleteEffect [GDIPLUS.@]
5033 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
5035 FIXME("(%p): stub\n", effect);
5036 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5037 * in Windows's gdiplus */
5038 return NotImplemented;
5041 /*****************************************************************************
5042 * GdipSetEffectParameters [GDIPLUS.@]
5044 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
5045 const VOID *params, const UINT size)
5047 static int calls;
5049 TRACE("(%p,%p,%u)\n", effect, params, size);
5051 if(!(calls++))
5052 FIXME("not implemented\n");
5054 return NotImplemented;
5057 /*****************************************************************************
5058 * GdipGetImageFlags [GDIPLUS.@]
5060 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
5062 TRACE("%p %p\n", image, flags);
5064 if(!image || !flags)
5065 return InvalidParameter;
5067 *flags = image->flags;
5069 return Ok;
5072 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
5074 TRACE("(%d, %p)\n", control, param);
5076 switch(control){
5077 case TestControlForceBilinear:
5078 if(param)
5079 FIXME("TestControlForceBilinear not handled\n");
5080 break;
5081 case TestControlNoICM:
5082 if(param)
5083 FIXME("TestControlNoICM not handled\n");
5084 break;
5085 case TestControlGetBuildNumber:
5086 *((DWORD*)param) = 3102;
5087 break;
5090 return Ok;
5093 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
5095 TRACE("%p\n", image);
5097 return Ok;
5100 /*****************************************************************************
5101 * GdipGetImageThumbnail [GDIPLUS.@]
5103 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
5104 GpImage **ret_image, GetThumbnailImageAbort cb,
5105 VOID * cb_data)
5107 GpStatus stat;
5108 GpGraphics *graphics;
5109 UINT srcwidth, srcheight;
5111 TRACE("(%p %u %u %p %p %p)\n",
5112 image, width, height, ret_image, cb, cb_data);
5114 if (!image || !ret_image)
5115 return InvalidParameter;
5117 if (!width) width = 120;
5118 if (!height) height = 120;
5120 GdipGetImageWidth(image, &srcwidth);
5121 GdipGetImageHeight(image, &srcheight);
5123 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
5124 NULL, (GpBitmap**)ret_image);
5126 if (stat == Ok)
5128 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
5130 if (stat == Ok)
5132 stat = GdipDrawImageRectRectI(graphics, image,
5133 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
5134 NULL, NULL, NULL);
5136 GdipDeleteGraphics(graphics);
5139 if (stat != Ok)
5141 GdipDisposeImage(*ret_image);
5142 *ret_image = NULL;
5146 return stat;
5149 /*****************************************************************************
5150 * GdipImageRotateFlip [GDIPLUS.@]
5152 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
5154 GpBitmap *new_bitmap;
5155 GpBitmap *bitmap;
5156 int bpp, bytesperpixel;
5157 BOOL rotate_90, flip_x, flip_y;
5158 int src_x_offset, src_y_offset;
5159 LPBYTE src_origin;
5160 UINT x, y, width, height;
5161 BitmapData src_lock, dst_lock;
5162 GpStatus stat;
5164 TRACE("(%p, %u)\n", image, type);
5166 if (!image)
5167 return InvalidParameter;
5169 rotate_90 = type&1;
5170 flip_x = (type&6) == 2 || (type&6) == 4;
5171 flip_y = (type&3) == 1 || (type&3) == 2;
5173 if (image->type != ImageTypeBitmap)
5175 FIXME("Not implemented for type %i\n", image->type);
5176 return NotImplemented;
5179 bitmap = (GpBitmap*)image;
5180 bpp = PIXELFORMATBPP(bitmap->format);
5182 if (bpp < 8)
5184 FIXME("Not implemented for %i bit images\n", bpp);
5185 return NotImplemented;
5188 if (rotate_90)
5190 width = bitmap->height;
5191 height = bitmap->width;
5193 else
5195 width = bitmap->width;
5196 height = bitmap->height;
5199 bytesperpixel = bpp/8;
5201 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5203 if (stat != Ok)
5204 return stat;
5206 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
5208 if (stat == Ok)
5210 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5212 if (stat == Ok)
5214 LPBYTE src_row, src_pixel;
5215 LPBYTE dst_row, dst_pixel;
5217 src_origin = src_lock.Scan0;
5218 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5219 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
5221 if (rotate_90)
5223 if (flip_y) src_x_offset = -src_lock.Stride;
5224 else src_x_offset = src_lock.Stride;
5225 if (flip_x) src_y_offset = -bytesperpixel;
5226 else src_y_offset = bytesperpixel;
5228 else
5230 if (flip_x) src_x_offset = -bytesperpixel;
5231 else src_x_offset = bytesperpixel;
5232 if (flip_y) src_y_offset = -src_lock.Stride;
5233 else src_y_offset = src_lock.Stride;
5236 src_row = src_origin;
5237 dst_row = dst_lock.Scan0;
5238 for (y=0; y<height; y++)
5240 src_pixel = src_row;
5241 dst_pixel = dst_row;
5242 for (x=0; x<width; x++)
5244 /* FIXME: This could probably be faster without memcpy. */
5245 memcpy(dst_pixel, src_pixel, bytesperpixel);
5246 dst_pixel += bytesperpixel;
5247 src_pixel += src_x_offset;
5249 src_row += src_y_offset;
5250 dst_row += dst_lock.Stride;
5253 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5256 GdipBitmapUnlockBits(bitmap, &src_lock);
5259 if (stat == Ok)
5260 move_bitmap(bitmap, new_bitmap, FALSE);
5261 else
5262 GdipDisposeImage((GpImage*)new_bitmap);
5264 return stat;
5267 /*****************************************************************************
5268 * GdipImageSetAbort [GDIPLUS.@]
5270 GpStatus WINGDIPAPI GdipImageSetAbort(GpImage *image, GdiplusAbort *pabort)
5272 FIXME("(%p, %p): stub\n", image, pabort);
5273 return NotImplemented;
5276 /*****************************************************************************
5277 * GdipBitmapConvertFormat [GDIPLUS.@]
5279 GpStatus WINGDIPAPI GdipBitmapConvertFormat(GpBitmap *bitmap, PixelFormat format, DitherType dithertype,
5280 PaletteType palettetype, ColorPalette *palette, REAL alphathreshold)
5282 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap, format, dithertype, palettetype, palette, alphathreshold);
5283 return NotImplemented;