kernel32/tests: Use todo_wine_if() in tests.
[wine.git] / dlls / gdiplus / image.c
blobf803efae5acff1ee09e191d7701fd2f1393e4bba
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;
2146 else
2148 WARN("invalid image: %p\n", image);
2149 return ObjectBusy;
2151 if (image->picture)
2152 IPicture_Release(image->picture);
2153 if (image->decoder)
2154 IWICBitmapDecoder_Release(image->decoder);
2155 heap_free(image->palette);
2157 return Ok;
2160 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2162 GpStatus status;
2164 TRACE("%p\n", image);
2166 status = free_image_data(image);
2167 if (status != Ok) return status;
2168 image->type = ~0;
2169 heap_free(image);
2171 return Ok;
2174 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2176 static int calls;
2178 TRACE("(%p,%p)\n", image, item);
2180 if(!image || !item)
2181 return InvalidParameter;
2183 if (!(calls++))
2184 FIXME("not implemented\n");
2186 return NotImplemented;
2189 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2191 static int calls;
2193 TRACE("(%p,%p)\n", image, item);
2195 if (!(calls++))
2196 FIXME("not implemented\n");
2198 return NotImplemented;
2201 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2202 GpUnit *srcUnit)
2204 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2206 if(!image || !srcRect || !srcUnit)
2207 return InvalidParameter;
2208 if(image->type == ImageTypeMetafile){
2209 *srcRect = ((GpMetafile*)image)->bounds;
2210 *srcUnit = ((GpMetafile*)image)->unit;
2212 else if(image->type == ImageTypeBitmap){
2213 srcRect->X = srcRect->Y = 0.0;
2214 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2215 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2216 *srcUnit = UnitPixel;
2218 else{
2219 srcRect->X = srcRect->Y = 0.0;
2220 srcRect->Width = ipicture_pixel_width(image->picture);
2221 srcRect->Height = ipicture_pixel_height(image->picture);
2222 *srcUnit = UnitPixel;
2225 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2226 srcRect->Width, srcRect->Height, *srcUnit);
2228 return Ok;
2231 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2232 REAL *height)
2234 TRACE("%p %p %p\n", image, width, height);
2236 if(!image || !height || !width)
2237 return InvalidParameter;
2239 if(image->type == ImageTypeMetafile){
2240 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2241 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2243 else if(image->type == ImageTypeBitmap){
2244 *height = ((GpBitmap*)image)->height;
2245 *width = ((GpBitmap*)image)->width;
2247 else{
2248 *height = ipicture_pixel_height(image->picture);
2249 *width = ipicture_pixel_width(image->picture);
2252 TRACE("returning (%f, %f)\n", *height, *width);
2253 return Ok;
2256 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2257 GpGraphics **graphics)
2259 HDC hdc;
2260 GpStatus stat;
2262 TRACE("%p %p\n", image, graphics);
2264 if(!image || !graphics)
2265 return InvalidParameter;
2267 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2269 hdc = ((GpBitmap*)image)->hdc;
2271 if(!hdc){
2272 hdc = CreateCompatibleDC(0);
2273 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2274 ((GpBitmap*)image)->hdc = hdc;
2277 stat = GdipCreateFromHDC(hdc, graphics);
2279 if (stat == Ok)
2281 (*graphics)->image = image;
2282 (*graphics)->xres = image->xres;
2283 (*graphics)->yres = image->yres;
2286 else if (image->type == ImageTypeMetafile)
2287 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2288 else
2289 stat = graphics_from_image(image, graphics);
2291 return stat;
2294 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2296 TRACE("%p %p\n", image, height);
2298 if(!image || !height)
2299 return InvalidParameter;
2301 if(image->type == ImageTypeMetafile)
2302 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2303 else if(image->type == ImageTypeBitmap)
2304 *height = ((GpBitmap*)image)->height;
2305 else
2306 *height = ipicture_pixel_height(image->picture);
2308 TRACE("returning %d\n", *height);
2310 return Ok;
2313 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2315 if(!image || !res)
2316 return InvalidParameter;
2318 *res = image->xres;
2320 TRACE("(%p) <-- %0.2f\n", image, *res);
2322 return Ok;
2325 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2327 TRACE("%p %p\n", image, size);
2329 if(!image || !size)
2330 return InvalidParameter;
2332 if (!image->palette || image->palette->Count == 0)
2333 *size = sizeof(ColorPalette);
2334 else
2335 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2337 TRACE("<-- %u\n", *size);
2339 return Ok;
2342 /* FIXME: test this function for non-bitmap types */
2343 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2345 TRACE("%p %p\n", image, format);
2347 if(!image || !format)
2348 return InvalidParameter;
2350 if(image->type != ImageTypeBitmap)
2351 *format = PixelFormat24bppRGB;
2352 else
2353 *format = ((GpBitmap*) image)->format;
2355 return Ok;
2358 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2360 TRACE("(%p, %p)\n", image, format);
2362 if(!image || !format)
2363 return InvalidParameter;
2365 memcpy(format, &image->format, sizeof(GUID));
2367 return Ok;
2370 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2372 TRACE("%p %p\n", image, type);
2374 if(!image || !type)
2375 return InvalidParameter;
2377 *type = image->type;
2379 return Ok;
2382 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2384 if(!image || !res)
2385 return InvalidParameter;
2387 *res = image->yres;
2389 TRACE("(%p) <-- %0.2f\n", image, *res);
2391 return Ok;
2394 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2396 TRACE("%p %p\n", image, width);
2398 if(!image || !width)
2399 return InvalidParameter;
2401 if(image->type == ImageTypeMetafile)
2402 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2403 else if(image->type == ImageTypeBitmap)
2404 *width = ((GpBitmap*)image)->width;
2405 else
2406 *width = ipicture_pixel_width(image->picture);
2408 TRACE("returning %d\n", *width);
2410 return Ok;
2413 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2415 TRACE("(%p, %p)\n", image, num);
2417 if (!image || !num) return InvalidParameter;
2419 *num = 0;
2421 if (image->type == ImageTypeBitmap)
2423 if (((GpBitmap *)image)->prop_item)
2425 *num = ((GpBitmap *)image)->prop_count;
2426 return Ok;
2429 if (((GpBitmap *)image)->metadata_reader)
2430 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2433 return Ok;
2436 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2438 HRESULT hr;
2439 IWICMetadataReader *reader;
2440 IWICEnumMetadataItem *enumerator;
2441 UINT prop_count, i, items_returned;
2443 TRACE("(%p, %u, %p)\n", image, num, list);
2445 if (!image || !list) return InvalidParameter;
2447 if (image->type != ImageTypeBitmap)
2449 FIXME("Not implemented for type %d\n", image->type);
2450 return NotImplemented;
2453 if (((GpBitmap *)image)->prop_item)
2455 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2457 for (i = 0; i < num; i++)
2459 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2462 return Ok;
2465 reader = ((GpBitmap *)image)->metadata_reader;
2466 if (!reader)
2468 if (num != 0) return InvalidParameter;
2469 return Ok;
2472 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2473 if (FAILED(hr)) return hresult_to_status(hr);
2475 if (num != prop_count) return InvalidParameter;
2477 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2478 if (FAILED(hr)) return hresult_to_status(hr);
2480 IWICEnumMetadataItem_Reset(enumerator);
2482 for (i = 0; i < num; i++)
2484 PROPVARIANT id;
2486 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2487 if (hr != S_OK) break;
2489 if (id.vt != VT_UI2)
2491 FIXME("not supported propvariant type for id: %u\n", id.vt);
2492 list[i] = 0;
2493 continue;
2495 list[i] = id.u.uiVal;
2498 IWICEnumMetadataItem_Release(enumerator);
2500 return hr == S_OK ? Ok : hresult_to_status(hr);
2503 static UINT propvariant_size(PROPVARIANT *value)
2505 switch (value->vt & ~VT_VECTOR)
2507 case VT_EMPTY:
2508 return 0;
2509 case VT_I1:
2510 case VT_UI1:
2511 if (!(value->vt & VT_VECTOR)) return 1;
2512 return value->u.caub.cElems;
2513 case VT_I2:
2514 case VT_UI2:
2515 if (!(value->vt & VT_VECTOR)) return 2;
2516 return value->u.caui.cElems * 2;
2517 case VT_I4:
2518 case VT_UI4:
2519 case VT_R4:
2520 if (!(value->vt & VT_VECTOR)) return 4;
2521 return value->u.caul.cElems * 4;
2522 case VT_I8:
2523 case VT_UI8:
2524 case VT_R8:
2525 if (!(value->vt & VT_VECTOR)) return 8;
2526 return value->u.cauh.cElems * 8;
2527 case VT_LPSTR:
2528 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2529 case VT_BLOB:
2530 return value->u.blob.cbSize;
2531 default:
2532 FIXME("not supported variant type %d\n", value->vt);
2533 return 0;
2537 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2539 HRESULT hr;
2540 IWICMetadataReader *reader;
2541 PROPVARIANT id, value;
2543 TRACE("(%p,%#x,%p)\n", image, propid, size);
2545 if (!size || !image) return InvalidParameter;
2547 if (image->type != ImageTypeBitmap)
2549 FIXME("Not implemented for type %d\n", image->type);
2550 return NotImplemented;
2553 if (((GpBitmap *)image)->prop_item)
2555 UINT i;
2557 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2559 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2561 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2562 return Ok;
2566 return PropertyNotFound;
2569 reader = ((GpBitmap *)image)->metadata_reader;
2570 if (!reader) return PropertyNotFound;
2572 id.vt = VT_UI2;
2573 id.u.uiVal = propid;
2574 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2575 if (FAILED(hr)) return PropertyNotFound;
2577 *size = propvariant_size(&value);
2578 if (*size) *size += sizeof(PropertyItem);
2579 PropVariantClear(&value);
2581 return Ok;
2584 #ifndef PropertyTagTypeSByte
2585 #define PropertyTagTypeSByte 6
2586 #define PropertyTagTypeSShort 8
2587 #define PropertyTagTypeFloat 11
2588 #define PropertyTagTypeDouble 12
2589 #endif
2591 static UINT vt_to_itemtype(UINT vt)
2593 static const struct
2595 UINT vt, type;
2596 } vt2type[] =
2598 { VT_I1, PropertyTagTypeSByte },
2599 { VT_UI1, PropertyTagTypeByte },
2600 { VT_I2, PropertyTagTypeSShort },
2601 { VT_UI2, PropertyTagTypeShort },
2602 { VT_I4, PropertyTagTypeSLONG },
2603 { VT_UI4, PropertyTagTypeLong },
2604 { VT_I8, PropertyTagTypeSRational },
2605 { VT_UI8, PropertyTagTypeRational },
2606 { VT_R4, PropertyTagTypeFloat },
2607 { VT_R8, PropertyTagTypeDouble },
2608 { VT_LPSTR, PropertyTagTypeASCII },
2609 { VT_BLOB, PropertyTagTypeUndefined }
2611 UINT i;
2612 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2614 if (vt2type[i].vt == vt) return vt2type[i].type;
2616 FIXME("not supported variant type %u\n", vt);
2617 return 0;
2620 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2621 UINT size, PROPID id)
2623 UINT item_size, item_type;
2625 item_size = propvariant_size(value);
2626 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2628 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2629 if (!item_type) return InvalidParameter;
2631 item->value = item + 1;
2633 switch (value->vt & ~VT_VECTOR)
2635 case VT_I1:
2636 case VT_UI1:
2637 if (!(value->vt & VT_VECTOR))
2638 *(BYTE *)item->value = value->u.bVal;
2639 else
2640 memcpy(item->value, value->u.caub.pElems, item_size);
2641 break;
2642 case VT_I2:
2643 case VT_UI2:
2644 if (!(value->vt & VT_VECTOR))
2645 *(USHORT *)item->value = value->u.uiVal;
2646 else
2647 memcpy(item->value, value->u.caui.pElems, item_size);
2648 break;
2649 case VT_I4:
2650 case VT_UI4:
2651 case VT_R4:
2652 if (!(value->vt & VT_VECTOR))
2653 *(ULONG *)item->value = value->u.ulVal;
2654 else
2655 memcpy(item->value, value->u.caul.pElems, item_size);
2656 break;
2657 case VT_I8:
2658 case VT_UI8:
2659 case VT_R8:
2660 if (!(value->vt & VT_VECTOR))
2661 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2662 else
2663 memcpy(item->value, value->u.cauh.pElems, item_size);
2664 break;
2665 case VT_LPSTR:
2666 memcpy(item->value, value->u.pszVal, item_size);
2667 break;
2668 case VT_BLOB:
2669 memcpy(item->value, value->u.blob.pBlobData, item_size);
2670 break;
2671 default:
2672 FIXME("not supported variant type %d\n", value->vt);
2673 return InvalidParameter;
2676 item->length = item_size;
2677 item->type = item_type;
2678 item->id = id;
2680 return Ok;
2683 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2684 PropertyItem *buffer)
2686 GpStatus stat;
2687 HRESULT hr;
2688 IWICMetadataReader *reader;
2689 PROPVARIANT id, value;
2691 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2693 if (!image || !buffer) return InvalidParameter;
2695 if (image->type != ImageTypeBitmap)
2697 FIXME("Not implemented for type %d\n", image->type);
2698 return NotImplemented;
2701 if (((GpBitmap *)image)->prop_item)
2703 UINT i;
2705 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2707 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2709 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2710 return InvalidParameter;
2712 *buffer = ((GpBitmap *)image)->prop_item[i];
2713 buffer->value = buffer + 1;
2714 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2715 return Ok;
2719 return PropertyNotFound;
2722 reader = ((GpBitmap *)image)->metadata_reader;
2723 if (!reader) return PropertyNotFound;
2725 id.vt = VT_UI2;
2726 id.u.uiVal = propid;
2727 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2728 if (FAILED(hr)) return PropertyNotFound;
2730 stat = propvariant_to_item(&value, buffer, size, propid);
2731 PropVariantClear(&value);
2733 return stat;
2736 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2738 HRESULT hr;
2739 IWICMetadataReader *reader;
2740 IWICEnumMetadataItem *enumerator;
2741 UINT prop_count, prop_size, i;
2742 PROPVARIANT id, value;
2744 TRACE("(%p,%p,%p)\n", image, size, count);
2746 if (!image || !size || !count) return InvalidParameter;
2748 if (image->type != ImageTypeBitmap)
2750 FIXME("Not implemented for type %d\n", image->type);
2751 return NotImplemented;
2754 if (((GpBitmap *)image)->prop_item)
2756 *count = ((GpBitmap *)image)->prop_count;
2757 *size = 0;
2759 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2761 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2764 return Ok;
2767 reader = ((GpBitmap *)image)->metadata_reader;
2768 if (!reader) return PropertyNotFound;
2770 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2771 if (FAILED(hr)) return hresult_to_status(hr);
2773 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2774 if (FAILED(hr)) return hresult_to_status(hr);
2776 IWICEnumMetadataItem_Reset(enumerator);
2778 prop_size = 0;
2780 PropVariantInit(&id);
2781 PropVariantInit(&value);
2783 for (i = 0; i < prop_count; i++)
2785 UINT items_returned, item_size;
2787 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2788 if (hr != S_OK) break;
2790 item_size = propvariant_size(&value);
2791 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2793 PropVariantClear(&id);
2794 PropVariantClear(&value);
2797 IWICEnumMetadataItem_Release(enumerator);
2799 if (hr != S_OK) return PropertyNotFound;
2801 *count = prop_count;
2802 *size = prop_size;
2803 return Ok;
2806 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2807 UINT count, PropertyItem *buf)
2809 GpStatus status;
2810 HRESULT hr;
2811 IWICMetadataReader *reader;
2812 IWICEnumMetadataItem *enumerator;
2813 UINT prop_count, prop_size, i;
2814 PROPVARIANT id, value;
2815 char *item_value;
2817 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2819 if (!image || !buf) return InvalidParameter;
2821 if (image->type != ImageTypeBitmap)
2823 FIXME("Not implemented for type %d\n", image->type);
2824 return NotImplemented;
2827 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2828 if (status != Ok) return status;
2830 if (prop_count != count || prop_size != size) return InvalidParameter;
2832 if (((GpBitmap *)image)->prop_item)
2834 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2836 item_value = (char *)(buf + prop_count);
2838 for (i = 0; i < prop_count; i++)
2840 buf[i].value = item_value;
2841 item_value += buf[i].length;
2844 return Ok;
2847 reader = ((GpBitmap *)image)->metadata_reader;
2848 if (!reader) return PropertyNotFound;
2850 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2851 if (FAILED(hr)) return hresult_to_status(hr);
2853 IWICEnumMetadataItem_Reset(enumerator);
2855 item_value = (char *)(buf + prop_count);
2857 PropVariantInit(&id);
2858 PropVariantInit(&value);
2860 for (i = 0; i < prop_count; i++)
2862 PropertyItem *item;
2863 UINT items_returned, item_size;
2865 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2866 if (hr != S_OK) break;
2868 if (id.vt != VT_UI2)
2870 FIXME("not supported propvariant type for id: %u\n", id.vt);
2871 continue;
2874 item_size = propvariant_size(&value);
2875 if (item_size)
2877 item = heap_alloc(item_size + sizeof(*item));
2879 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2880 buf[i].id = item->id;
2881 buf[i].type = item->type;
2882 buf[i].length = item_size;
2883 buf[i].value = item_value;
2884 memcpy(item_value, item->value, item_size);
2885 item_value += item_size;
2887 heap_free(item);
2890 PropVariantClear(&id);
2891 PropVariantClear(&value);
2894 IWICEnumMetadataItem_Release(enumerator);
2896 if (hr != S_OK) return PropertyNotFound;
2898 return Ok;
2901 struct image_format_dimension
2903 const GUID *format;
2904 const GUID *dimension;
2907 static const struct image_format_dimension image_format_dimensions[] =
2909 {&ImageFormatGIF, &FrameDimensionTime},
2910 {&ImageFormatIcon, &FrameDimensionResolution},
2911 {NULL}
2914 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2915 GDIPCONST GUID* dimensionID, UINT* count)
2917 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2919 if(!image || !count)
2920 return InvalidParameter;
2922 if (!dimensionID ||
2923 IsEqualGUID(dimensionID, &image->format) ||
2924 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2925 IsEqualGUID(dimensionID, &FrameDimensionTime))
2927 *count = image->frame_count;
2928 return Ok;
2931 return InvalidParameter;
2934 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2935 UINT* count)
2937 TRACE("(%p, %p)\n", image, count);
2939 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2941 if(!image || !count)
2942 return InvalidParameter;
2944 *count = 1;
2946 return Ok;
2949 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2950 GUID* dimensionIDs, UINT count)
2952 int i;
2953 const GUID *result=NULL;
2955 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2957 if(!image || !dimensionIDs || count != 1)
2958 return InvalidParameter;
2960 for (i=0; image_format_dimensions[i].format; i++)
2962 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2964 result = image_format_dimensions[i].dimension;
2965 break;
2969 if (!result)
2970 result = &FrameDimensionPage;
2972 memcpy(dimensionIDs, result, sizeof(GUID));
2974 return Ok;
2977 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2978 GpImage **image)
2980 GpStatus stat;
2981 IStream *stream;
2983 TRACE("(%s) %p\n", debugstr_w(filename), image);
2985 if (!filename || !image)
2986 return InvalidParameter;
2988 *image = NULL;
2990 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2992 if (stat != Ok)
2993 return stat;
2995 stat = GdipLoadImageFromStream(stream, image);
2997 IStream_Release(stream);
2999 return stat;
3002 /* FIXME: no icm handling */
3003 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
3005 TRACE("(%s) %p\n", debugstr_w(filename), image);
3007 return GdipLoadImageFromFile(filename, image);
3010 static void add_property(GpBitmap *bitmap, PropertyItem *item)
3012 UINT prop_size, prop_count;
3013 PropertyItem *prop_item;
3015 if (bitmap->prop_item == NULL)
3017 prop_size = prop_count = 0;
3018 prop_item = heap_alloc_zero(item->length + sizeof(PropertyItem));
3019 if (!prop_item) return;
3021 else
3023 UINT i;
3024 char *item_value;
3026 GdipGetPropertySize((GpImage *)bitmap, &prop_size, &prop_count);
3028 prop_item = heap_alloc_zero(prop_size + item->length + sizeof(PropertyItem));
3029 if (!prop_item) return;
3030 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
3031 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
3032 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
3034 item_value = (char *)(prop_item + prop_count + 1);
3036 for (i = 0; i < prop_count; i++)
3038 prop_item[i].value = item_value;
3039 item_value += prop_item[i].length;
3043 prop_item[prop_count].id = item->id;
3044 prop_item[prop_count].type = item->type;
3045 prop_item[prop_count].length = item->length;
3046 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3047 memcpy(prop_item[prop_count].value, item->value, item->length);
3049 heap_free(bitmap->prop_item);
3050 bitmap->prop_item = prop_item;
3051 bitmap->prop_count++;
3054 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3056 HRESULT hr;
3057 GUID format;
3058 PROPVARIANT id, value;
3059 BOOL ret = FALSE;
3061 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3062 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return FALSE;
3064 PropVariantInit(&id);
3065 PropVariantInit(&value);
3067 id.vt = VT_LPWSTR;
3068 id.u.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3069 if (!id.u.pwszVal) return FALSE;
3070 lstrcpyW(id.u.pwszVal, prop_name);
3071 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3072 if (hr == S_OK && value.vt == VT_BOOL)
3073 ret = value.u.boolVal;
3075 PropVariantClear(&id);
3076 PropVariantClear(&value);
3078 return ret;
3081 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3083 HRESULT hr;
3084 GUID format;
3085 PROPVARIANT id, value;
3086 PropertyItem *item = NULL;
3088 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3089 if (FAILED(hr) || !IsEqualGUID(&format, guid)) return NULL;
3091 PropVariantInit(&id);
3092 PropVariantInit(&value);
3094 id.vt = VT_LPWSTR;
3095 id.u.pwszVal = CoTaskMemAlloc((lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3096 if (!id.u.pwszVal) return NULL;
3097 lstrcpyW(id.u.pwszVal, prop_name);
3098 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3099 if (hr == S_OK)
3101 UINT item_size = propvariant_size(&value);
3102 if (item_size)
3104 item_size += sizeof(*item);
3105 item = heap_alloc_zero(item_size);
3106 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3108 heap_free(item);
3109 item = NULL;
3114 PropVariantClear(&id);
3115 PropVariantClear(&value);
3117 return item;
3120 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3122 static const WCHAR textentryW[] = { 'T','e','x','t','E','n','t','r','y',0 };
3123 PropertyItem *comment;
3125 comment = get_property(reader, &GUID_MetadataFormatGifComment, textentryW);
3126 if (comment)
3127 comment->id = PropertyTagExifUserComment;
3129 return comment;
3132 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3134 static const WCHAR applicationW[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3135 static const WCHAR dataW[] = { 'D','a','t','a',0 };
3136 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3138 appext = get_property(reader, &GUID_MetadataFormatAPE, applicationW);
3139 if (appext)
3141 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3142 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3144 appdata = get_property(reader, &GUID_MetadataFormatAPE, dataW);
3145 if (appdata)
3147 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3149 BYTE *data = appdata->value;
3150 if (data[0] == 3 && data[1] == 1)
3152 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3153 if (loop)
3155 loop->type = PropertyTagTypeShort;
3156 loop->id = PropertyTagLoopCount;
3157 loop->length = sizeof(SHORT);
3158 loop->value = loop + 1;
3159 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3167 heap_free(appext);
3168 heap_free(appdata);
3170 return loop;
3173 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3175 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 };
3176 PropertyItem *background;
3178 background = get_property(reader, &GUID_MetadataFormatLSD, backgroundW);
3179 if (background)
3180 background->id = PropertyTagIndexBackground;
3182 return background;
3185 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3187 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 };
3188 HRESULT hr;
3189 IWICImagingFactory *factory;
3190 IWICPalette *palette;
3191 UINT count = 0;
3192 WICColor colors[256];
3194 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, global_flagW))
3195 return NULL;
3197 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3198 if (hr != S_OK) return NULL;
3200 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3201 if (hr == S_OK)
3203 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3204 if (hr == S_OK)
3205 IWICPalette_GetColors(palette, 256, colors, &count);
3207 IWICPalette_Release(palette);
3210 IWICImagingFactory_Release(factory);
3212 if (count)
3214 PropertyItem *pal;
3215 UINT i;
3216 BYTE *rgb;
3218 pal = heap_alloc_zero(sizeof(*pal) + count * 3);
3219 if (!pal) return NULL;
3220 pal->type = PropertyTagTypeByte;
3221 pal->id = PropertyTagGlobalPalette;
3222 pal->value = pal + 1;
3223 pal->length = count * 3;
3225 rgb = pal->value;
3227 for (i = 0; i < count; i++)
3229 rgb[i*3] = (colors[i] >> 16) & 0xff;
3230 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3231 rgb[i*3 + 2] = colors[i] & 0xff;
3234 return pal;
3237 return NULL;
3240 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3242 static const WCHAR transparency_flagW[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3243 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 };
3244 PropertyItem *index = NULL;
3246 if (get_bool_property(reader, &GUID_MetadataFormatGCE, transparency_flagW))
3248 index = get_property(reader, &GUID_MetadataFormatGCE, colorW);
3249 if (index)
3250 index->id = PropertyTagIndexTransparent;
3252 return index;
3255 static LONG get_gif_frame_property(IWICBitmapFrameDecode *frame, const GUID *format, const WCHAR *property)
3257 HRESULT hr;
3258 IWICMetadataBlockReader *block_reader;
3259 IWICMetadataReader *reader;
3260 UINT block_count, i;
3261 PropertyItem *prop;
3262 LONG value = 0;
3264 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3265 if (hr == S_OK)
3267 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3268 if (hr == S_OK)
3270 for (i = 0; i < block_count; i++)
3272 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3273 if (hr == S_OK)
3275 prop = get_property(reader, format, property);
3276 if (prop)
3278 if (prop->type == PropertyTagTypeByte && prop->length == 1)
3279 value = *(BYTE *)prop->value;
3280 else if (prop->type == PropertyTagTypeShort && prop->length == 2)
3281 value = *(SHORT *)prop->value;
3283 heap_free(prop);
3285 IWICMetadataReader_Release(reader);
3289 IWICMetadataBlockReader_Release(block_reader);
3292 return value;
3295 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3297 static const WCHAR delayW[] = { 'D','e','l','a','y',0 };
3298 HRESULT hr;
3299 IWICBitmapFrameDecode *frame;
3300 IWICMetadataBlockReader *block_reader;
3301 IWICMetadataReader *reader;
3302 UINT frame_count, block_count, i;
3303 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3304 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3306 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3307 if (frame_count > 1)
3309 delay = heap_alloc_zero(sizeof(*delay) + frame_count * sizeof(LONG));
3310 if (delay)
3312 LONG *value;
3314 delay->type = PropertyTagTypeLong;
3315 delay->id = PropertyTagFrameDelay;
3316 delay->length = frame_count * sizeof(LONG);
3317 delay->value = delay + 1;
3319 value = delay->value;
3321 for (i = 0; i < frame_count; i++)
3323 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3324 if (hr == S_OK)
3326 value[i] = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, delayW);
3327 IWICBitmapFrameDecode_Release(frame);
3329 else value[i] = 0;
3334 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3335 if (hr == S_OK)
3337 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3338 if (hr == S_OK)
3340 for (i = 0; i < block_count; i++)
3342 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3343 if (hr == S_OK)
3345 if (!comment)
3346 comment = get_gif_comment(reader);
3348 if (frame_count > 1 && !loop)
3349 loop = get_gif_loopcount(reader);
3351 if (!background)
3352 background = get_gif_background(reader);
3354 if (!palette)
3355 palette = get_gif_palette(decoder, reader);
3357 IWICMetadataReader_Release(reader);
3361 IWICMetadataBlockReader_Release(block_reader);
3364 if (frame_count > 1 && !loop)
3366 loop = heap_alloc_zero(sizeof(*loop) + sizeof(SHORT));
3367 if (loop)
3369 loop->type = PropertyTagTypeShort;
3370 loop->id = PropertyTagLoopCount;
3371 loop->length = sizeof(SHORT);
3372 loop->value = loop + 1;
3373 *(SHORT *)loop->value = 1;
3377 if (delay) add_property(bitmap, delay);
3378 if (comment) add_property(bitmap, comment);
3379 if (loop) add_property(bitmap, loop);
3380 if (palette) add_property(bitmap, palette);
3381 if (background) add_property(bitmap, background);
3383 heap_free(delay);
3384 heap_free(comment);
3385 heap_free(loop);
3386 heap_free(palette);
3387 heap_free(background);
3389 /* Win7 gdiplus always returns transparent color index from frame 0 */
3390 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3391 if (hr != S_OK) return;
3393 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3394 if (hr == S_OK)
3396 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3397 if (hr == S_OK)
3399 for (i = 0; i < block_count; i++)
3401 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3402 if (hr == S_OK)
3404 if (!transparent_idx)
3405 transparent_idx = get_gif_transparent_idx(reader);
3407 IWICMetadataReader_Release(reader);
3411 IWICMetadataBlockReader_Release(block_reader);
3414 if (transparent_idx) add_property(bitmap, transparent_idx);
3415 heap_free(transparent_idx);
3417 IWICBitmapFrameDecode_Release(frame);
3420 static PropertyItem* create_prop(PROPID propid, PROPVARIANT* value)
3422 PropertyItem *item = NULL;
3423 UINT item_size = propvariant_size(value);
3425 if (item_size)
3427 item_size += sizeof(*item);
3428 item = heap_alloc_zero(item_size);
3429 if (propvariant_to_item(value, item, item_size, propid) != Ok)
3431 heap_free(item);
3432 item = NULL;
3436 return item;
3439 static ULONG get_ulong_by_index(IWICMetadataReader* reader, ULONG index)
3441 PROPVARIANT value;
3442 HRESULT hr;
3443 ULONG result=0;
3445 hr = IWICMetadataReader_GetValueByIndex(reader, index, NULL, NULL, &value);
3446 if (SUCCEEDED(hr))
3448 switch (value.vt)
3450 case VT_UI4:
3451 result = value.u.ulVal;
3452 break;
3453 default:
3454 ERR("unhandled case %u\n", value.vt);
3455 break;
3457 PropVariantClear(&value);
3459 return result;
3462 static void png_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3464 HRESULT hr;
3465 IWICBitmapFrameDecode *frame;
3466 IWICMetadataBlockReader *block_reader;
3467 IWICMetadataReader *reader;
3468 UINT block_count, i, j;
3469 struct keyword_info {
3470 const char* name;
3471 PROPID propid;
3472 BOOL seen;
3473 } keywords[] = {
3474 { "Title", PropertyTagImageTitle },
3475 { "Author", PropertyTagArtist },
3476 { "Description", PropertyTagImageDescription },
3477 { "Copyright", PropertyTagCopyright },
3478 { "Software", PropertyTagSoftwareUsed },
3479 { "Source", PropertyTagEquipModel },
3480 { "Comment", PropertyTagExifUserComment },
3482 BOOL seen_gamma=FALSE;
3484 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3485 if (hr != S_OK) return;
3487 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3488 if (hr == S_OK)
3490 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3491 if (hr == S_OK)
3493 for (i = 0; i < block_count; i++)
3495 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3496 if (hr == S_OK)
3498 GUID format;
3500 hr = IWICMetadataReader_GetMetadataFormat(reader, &format);
3501 if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunktEXt, &format))
3503 PROPVARIANT name, value;
3504 PropertyItem* item;
3506 hr = IWICMetadataReader_GetValueByIndex(reader, 0, NULL, &name, &value);
3508 if (SUCCEEDED(hr))
3510 if (name.vt == VT_LPSTR)
3512 for (j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++)
3513 if (!strcmp(keywords[j].name, name.u.pszVal))
3514 break;
3515 if (j < sizeof(keywords)/sizeof(keywords[0]) && !keywords[j].seen)
3517 keywords[j].seen = TRUE;
3518 item = create_prop(keywords[j].propid, &value);
3519 if (item)
3520 add_property(bitmap, item);
3521 heap_free(item);
3525 PropVariantClear(&name);
3526 PropVariantClear(&value);
3529 else if (SUCCEEDED(hr) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA, &format))
3531 PropertyItem* item;
3533 if (!seen_gamma)
3535 item = heap_alloc_zero(sizeof(PropertyItem) + sizeof(ULONG) * 2);
3536 if (item)
3538 ULONG *rational;
3539 item->length = sizeof(ULONG) * 2;
3540 item->type = PropertyTagTypeRational;
3541 item->id = PropertyTagGamma;
3542 rational = item->value = item + 1;
3543 rational[0] = 100000;
3544 rational[1] = get_ulong_by_index(reader, 0);
3545 add_property(bitmap, item);
3546 seen_gamma = TRUE;
3547 heap_free(item);
3552 IWICMetadataReader_Release(reader);
3556 IWICMetadataBlockReader_Release(block_reader);
3559 IWICBitmapFrameDecode_Release(frame);
3562 static GpStatus initialize_decoder_wic(IStream *stream, REFGUID container, IWICBitmapDecoder **decoder)
3564 IWICImagingFactory *factory;
3565 HRESULT hr;
3567 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
3569 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3570 if (FAILED(hr)) return hresult_to_status(hr);
3571 hr = IWICImagingFactory_CreateDecoder(factory, container, NULL, decoder);
3572 IWICImagingFactory_Release(factory);
3573 if (FAILED(hr)) return hresult_to_status(hr);
3575 hr = IWICBitmapDecoder_Initialize(*decoder, stream, WICDecodeMetadataCacheOnLoad);
3576 if (FAILED(hr)) return hresult_to_status(hr);
3577 return Ok;
3580 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3582 static GpStatus decode_frame_wic(IWICBitmapDecoder *decoder, BOOL force_conversion,
3583 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3585 GpStatus status=Ok;
3586 GpBitmap *bitmap;
3587 HRESULT hr;
3588 IWICBitmapFrameDecode *frame;
3589 IWICBitmapSource *source=NULL;
3590 IWICMetadataBlockReader *block_reader;
3591 WICPixelFormatGUID wic_format;
3592 PixelFormat gdip_format=0;
3593 ColorPalette *palette = NULL;
3594 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3595 int i;
3596 UINT width, height, frame_count;
3597 BitmapData lockeddata;
3598 WICRect wrc;
3600 TRACE("%p,%u,%p\n", decoder, active_frame, image);
3602 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3603 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3604 if (SUCCEEDED(hr)) /* got frame */
3606 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3608 if (SUCCEEDED(hr))
3610 if (!force_conversion)
3612 for (i=0; pixel_formats[i].wic_format; i++)
3614 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3616 source = (IWICBitmapSource*)frame;
3617 IWICBitmapSource_AddRef(source);
3618 gdip_format = pixel_formats[i].gdip_format;
3619 palette_type = pixel_formats[i].palette_type;
3620 break;
3624 if (!source)
3626 /* unknown format; fall back on 32bppARGB */
3627 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3628 gdip_format = PixelFormat32bppARGB;
3630 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3633 if (SUCCEEDED(hr)) /* got source */
3635 hr = IWICBitmapSource_GetSize(source, &width, &height);
3637 if (SUCCEEDED(hr))
3638 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3639 NULL, &bitmap);
3641 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3643 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3644 gdip_format, &lockeddata);
3645 if (status == Ok) /* locked bitmap */
3647 wrc.X = 0;
3648 wrc.Width = width;
3649 wrc.Height = 1;
3650 for (i=0; i<height; i++)
3652 wrc.Y = i;
3653 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3654 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3655 if (FAILED(hr)) break;
3658 GdipBitmapUnlockBits(bitmap, &lockeddata);
3661 if (SUCCEEDED(hr) && status == Ok)
3662 *image = (GpImage*)bitmap;
3663 else
3665 *image = NULL;
3666 GdipDisposeImage((GpImage*)bitmap);
3669 if (SUCCEEDED(hr) && status == Ok)
3671 double dpix, dpiy;
3672 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3673 if (SUCCEEDED(hr))
3675 bitmap->image.xres = dpix;
3676 bitmap->image.yres = dpiy;
3678 hr = S_OK;
3682 IWICBitmapSource_Release(source);
3685 if (SUCCEEDED(hr)) {
3686 bitmap->metadata_reader = NULL;
3688 if (metadata_reader)
3689 metadata_reader(bitmap, decoder, active_frame);
3690 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3692 UINT block_count = 0;
3693 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3694 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3695 IWICMetadataBlockReader_Release(block_reader);
3698 palette = get_palette(frame, palette_type);
3699 IWICBitmapFrameDecode_Release(frame);
3703 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3705 if (status == Ok)
3707 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3708 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3709 bitmap->image.frame_count = frame_count;
3710 bitmap->image.current_frame = active_frame;
3711 bitmap->image.decoder = decoder;
3712 IWICBitmapDecoder_AddRef(decoder);
3713 if (palette)
3715 heap_free(bitmap->image.palette);
3716 bitmap->image.palette = palette;
3718 else
3720 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3721 bitmap->image.palette->Flags = 0;
3723 TRACE("=> %p\n", *image);
3726 return status;
3729 static GpStatus decode_image_wic(IStream *stream, REFGUID container,
3730 metadata_reader_func metadata_reader, GpImage **image)
3732 IWICBitmapDecoder *decoder;
3733 GpStatus status;
3735 status = initialize_decoder_wic(stream, container, &decoder);
3736 if(status != Ok)
3737 return status;
3739 status = decode_frame_wic(decoder, FALSE, 0, metadata_reader, image);
3740 IWICBitmapDecoder_Release(decoder);
3741 return status;
3744 static GpStatus select_frame_wic(GpImage *image, UINT active_frame)
3746 GpImage *new_image;
3747 GpStatus status;
3749 status = decode_frame_wic(image->decoder, FALSE, active_frame, NULL, &new_image);
3750 if(status != Ok)
3751 return status;
3753 memcpy(&new_image->format, &image->format, sizeof(GUID));
3754 free_image_data(image);
3755 if (image->type == ImageTypeBitmap)
3756 *(GpBitmap *)image = *(GpBitmap *)new_image;
3757 else if (image->type == ImageTypeMetafile)
3758 *(GpMetafile *)image = *(GpMetafile *)new_image;
3759 new_image->type = ~0;
3760 heap_free(new_image);
3761 return Ok;
3764 static HRESULT get_gif_frame_rect(IWICBitmapFrameDecode *frame,
3765 UINT *left, UINT *top, UINT *width, UINT *height)
3767 static const WCHAR leftW[] = {'L','e','f','t',0};
3768 static const WCHAR topW[] = {'T','o','p',0};
3770 *left = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, leftW);
3771 *top = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, topW);
3773 return IWICBitmapFrameDecode_GetSize(frame, width, height);
3776 static HRESULT blit_gif_frame(GpBitmap *bitmap, IWICBitmapFrameDecode *frame, BOOL first_frame)
3778 UINT i, j, left, top, width, height;
3779 IWICBitmapSource *source;
3780 BYTE *new_bits;
3781 HRESULT hr;
3783 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3784 if(FAILED(hr))
3785 return hr;
3787 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3788 if(FAILED(hr))
3789 return hr;
3791 new_bits = heap_alloc_zero(width*height*4);
3792 if(!new_bits)
3793 return E_OUTOFMEMORY;
3795 hr = IWICBitmapSource_CopyPixels(source, NULL, width*4, width*height*4, new_bits);
3796 IWICBitmapSource_Release(source);
3797 if(FAILED(hr)) {
3798 heap_free(new_bits);
3799 return hr;
3802 for(i=0; i<height && i+top<bitmap->height; i++) {
3803 for(j=0; j<width && j+left<bitmap->width; j++) {
3804 DWORD *src = (DWORD*)(new_bits+i*width*4+j*4);
3805 DWORD *dst = (DWORD*)(bitmap->bits+(i+top)*bitmap->stride+(j+left)*4);
3807 if(first_frame || *src>>24 != 0)
3808 *dst = *src;
3811 heap_free(new_bits);
3812 return hr;
3815 static DWORD get_gif_background_color(GpBitmap *bitmap)
3817 BYTE bgcolor_idx = 0;
3818 UINT i;
3820 for(i=0; i<bitmap->prop_count; i++) {
3821 if(bitmap->prop_item[i].id == PropertyTagIndexBackground) {
3822 bgcolor_idx = *(BYTE*)bitmap->prop_item[i].value;
3823 break;
3827 for(i=0; i<bitmap->prop_count; i++) {
3828 if(bitmap->prop_item[i].id == PropertyTagIndexTransparent) {
3829 BYTE transparent_idx;
3830 transparent_idx = *(BYTE*)bitmap->prop_item[i].value;
3832 if(transparent_idx == bgcolor_idx)
3833 return 0;
3837 for(i=0; i<bitmap->prop_count; i++) {
3838 if(bitmap->prop_item[i].id == PropertyTagGlobalPalette) {
3839 if(bitmap->prop_item[i].length/3 > bgcolor_idx) {
3840 BYTE *color = ((BYTE*)bitmap->prop_item[i].value)+bgcolor_idx*3;
3841 return color[2] + (color[1]<<8) + (color[0]<<16) + (0xffu<<24);
3843 break;
3847 FIXME("can't get gif background color\n");
3848 return 0xffffffff;
3851 static GpStatus select_frame_gif(GpImage* image, UINT active_frame)
3853 static const WCHAR disposalW[] = {'D','i','s','p','o','s','a','l',0};
3855 GpBitmap *bitmap = (GpBitmap*)image;
3856 IWICBitmapFrameDecode *frame;
3857 int cur_frame=0, disposal;
3858 BOOL bgcolor_set = FALSE;
3859 DWORD bgcolor = 0;
3860 HRESULT hr;
3862 if(active_frame > image->current_frame) {
3863 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, image->current_frame, &frame);
3864 if(FAILED(hr))
3865 return hresult_to_status(hr);
3866 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3867 IWICBitmapFrameDecode_Release(frame);
3869 if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND)
3870 cur_frame = image->current_frame;
3871 else if(disposal != GIF_DISPOSE_RESTORE_TO_PREV)
3872 cur_frame = image->current_frame+1;
3875 while(cur_frame != active_frame) {
3876 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, cur_frame, &frame);
3877 if(FAILED(hr))
3878 return hresult_to_status(hr);
3879 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3881 if(disposal==GIF_DISPOSE_UNSPECIFIED || disposal==GIF_DISPOSE_DO_NOT_DISPOSE) {
3882 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3883 if(FAILED(hr))
3884 return hresult_to_status(hr);
3885 }else if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND) {
3886 UINT left, top, width, height, i, j;
3888 if(!bgcolor_set) {
3889 bgcolor = get_gif_background_color(bitmap);
3890 bgcolor_set = TRUE;
3893 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3894 if(FAILED(hr))
3895 return hresult_to_status(hr);
3896 for(i=top; i<top+height && i<bitmap->height; i++) {
3897 DWORD *bits = (DWORD*)(bitmap->bits+i*bitmap->stride);
3898 for(j=left; j<left+width && j<bitmap->width; j++)
3899 bits[j] = bgcolor;
3903 IWICBitmapFrameDecode_Release(frame);
3904 cur_frame++;
3907 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, active_frame, &frame);
3908 if(FAILED(hr))
3909 return hresult_to_status(hr);
3911 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3912 IWICBitmapFrameDecode_Release(frame);
3913 if(FAILED(hr))
3914 return hresult_to_status(hr);
3916 image->current_frame = active_frame;
3917 return Ok;
3920 static GpStatus decode_image_icon(IStream* stream, GpImage **image)
3922 return decode_image_wic(stream, &GUID_ContainerFormatIco, NULL, image);
3925 static GpStatus decode_image_bmp(IStream* stream, GpImage **image)
3927 GpStatus status;
3928 GpBitmap* bitmap;
3930 status = decode_image_wic(stream, &GUID_ContainerFormatBmp, NULL, image);
3932 bitmap = (GpBitmap*)*image;
3934 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3936 /* WIC supports bmp files with alpha, but gdiplus does not */
3937 bitmap->format = PixelFormat32bppRGB;
3940 return status;
3943 static GpStatus decode_image_jpeg(IStream* stream, GpImage **image)
3945 return decode_image_wic(stream, &GUID_ContainerFormatJpeg, NULL, image);
3948 static GpStatus decode_image_png(IStream* stream, GpImage **image)
3950 return decode_image_wic(stream, &GUID_ContainerFormatPng, png_metadata_reader, image);
3953 static GpStatus decode_image_gif(IStream* stream, GpImage **image)
3955 IWICBitmapDecoder *decoder;
3956 UINT frame_count;
3957 GpStatus status;
3958 HRESULT hr;
3960 status = initialize_decoder_wic(stream, &GUID_ContainerFormatGif, &decoder);
3961 if(status != Ok)
3962 return status;
3964 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3965 if(FAILED(hr))
3966 return hresult_to_status(hr);
3968 status = decode_frame_wic(decoder, frame_count > 1, 0, gif_metadata_reader, image);
3969 IWICBitmapDecoder_Release(decoder);
3970 if(status != Ok)
3971 return status;
3973 if(frame_count > 1) {
3974 heap_free((*image)->palette);
3975 (*image)->palette = NULL;
3977 return Ok;
3980 static GpStatus decode_image_tiff(IStream* stream, GpImage **image)
3982 return decode_image_wic(stream, &GUID_ContainerFormatTiff, NULL, image);
3985 static GpStatus decode_image_olepicture_metafile(IStream* stream, GpImage **image)
3987 IPicture *pic;
3989 TRACE("%p %p\n", stream, image);
3991 if(!stream || !image)
3992 return InvalidParameter;
3994 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3995 (LPVOID*) &pic) != S_OK){
3996 TRACE("Could not load picture\n");
3997 return GenericError;
4000 /* FIXME: missing initialization code */
4001 *image = heap_alloc_zero(sizeof(GpMetafile));
4002 if(!*image) return OutOfMemory;
4003 (*image)->type = ImageTypeMetafile;
4004 (*image)->decoder = NULL;
4005 (*image)->picture = pic;
4006 (*image)->flags = ImageFlagsNone;
4007 (*image)->frame_count = 1;
4008 (*image)->current_frame = 0;
4009 (*image)->palette = NULL;
4011 TRACE("<-- %p\n", *image);
4013 return Ok;
4016 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
4017 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
4019 typedef GpStatus (*decode_image_func)(IStream *stream, GpImage **image);
4021 typedef GpStatus (*select_image_func)(GpImage *image, UINT active_frame);
4023 typedef struct image_codec {
4024 ImageCodecInfo info;
4025 encode_image_func encode_func;
4026 decode_image_func decode_func;
4027 select_image_func select_func;
4028 } image_codec;
4030 typedef enum {
4031 BMP,
4032 JPEG,
4033 GIF,
4034 TIFF,
4035 EMF,
4036 WMF,
4037 PNG,
4038 ICO,
4039 NUM_CODECS
4040 } ImageFormat;
4042 static const struct image_codec codecs[NUM_CODECS];
4044 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
4046 BYTE signature[8];
4047 const BYTE *pattern, *mask;
4048 LARGE_INTEGER seek;
4049 HRESULT hr;
4050 UINT bytesread;
4051 int i;
4052 DWORD j, sig;
4054 /* seek to the start of the stream */
4055 seek.QuadPart = 0;
4056 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4057 if (FAILED(hr)) return hresult_to_status(hr);
4059 /* read the first 8 bytes */
4060 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4061 hr = IStream_Read(stream, signature, 8, &bytesread);
4062 if (FAILED(hr)) return hresult_to_status(hr);
4063 if (hr == S_FALSE || bytesread == 0) return GenericError;
4065 for (i = 0; i < NUM_CODECS; i++) {
4066 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4067 bytesread >= codecs[i].info.SigSize)
4069 for (sig=0; sig<codecs[i].info.SigCount; sig++)
4071 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
4072 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
4073 for (j=0; j<codecs[i].info.SigSize; j++)
4074 if ((signature[j] & mask[j]) != pattern[j])
4075 break;
4076 if (j == codecs[i].info.SigSize)
4078 *result = &codecs[i];
4079 return Ok;
4085 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
4086 signature[0],signature[1],signature[2],signature[3],
4087 signature[4],signature[5],signature[6],signature[7]);
4089 return GenericError;
4092 static GpStatus get_decoder_info_from_image(GpImage *image, const struct image_codec **result)
4094 int i;
4096 for (i = 0; i < NUM_CODECS; i++) {
4097 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
4098 IsEqualIID(&codecs[i].info.FormatID, &image->format))
4100 *result = &codecs[i];
4101 return Ok;
4105 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image->format));
4106 return GenericError;
4109 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
4110 UINT frame)
4112 GpStatus stat;
4113 const struct image_codec *codec = NULL;
4115 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
4117 if (!image || !dimensionID)
4118 return InvalidParameter;
4120 if (frame >= image->frame_count)
4122 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
4123 return InvalidParameter;
4126 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
4128 WARN("invalid image type %d\n", image->type);
4129 return InvalidParameter;
4132 if (image->current_frame == frame)
4133 return Ok;
4135 if (!image->decoder)
4137 TRACE("image doesn't have an associated decoder\n");
4138 return Ok;
4141 /* choose an appropriate image decoder */
4142 stat = get_decoder_info_from_image(image, &codec);
4143 if (stat != Ok)
4145 WARN("can't find decoder info\n");
4146 return stat;
4149 return codec->select_func(image, frame);
4152 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
4154 GpStatus stat;
4155 LARGE_INTEGER seek;
4156 HRESULT hr;
4157 const struct image_codec *codec=NULL;
4159 /* choose an appropriate image decoder */
4160 stat = get_decoder_info(stream, &codec);
4161 if (stat != Ok) return stat;
4163 /* seek to the start of the stream */
4164 seek.QuadPart = 0;
4165 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4166 if (FAILED(hr)) return hresult_to_status(hr);
4168 /* call on the image decoder to do the real work */
4169 stat = codec->decode_func(stream, image);
4171 /* take note of the original data format */
4172 if (stat == Ok)
4174 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
4175 return Ok;
4178 return stat;
4181 /* FIXME: no ICM */
4182 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
4184 TRACE("%p %p\n", stream, image);
4186 return GdipLoadImageFromStream(stream, image);
4189 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
4191 static int calls;
4193 TRACE("(%p,%u)\n", image, propId);
4195 if(!image)
4196 return InvalidParameter;
4198 if(!(calls++))
4199 FIXME("not implemented\n");
4201 return NotImplemented;
4204 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
4206 static int calls;
4208 if (!image || !item) return InvalidParameter;
4210 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
4212 if(!(calls++))
4213 FIXME("not implemented\n");
4215 return Ok;
4218 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
4219 GDIPCONST CLSID *clsidEncoder,
4220 GDIPCONST EncoderParameters *encoderParams)
4222 GpStatus stat;
4223 IStream *stream;
4225 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
4227 if (!image || !filename|| !clsidEncoder)
4228 return InvalidParameter;
4230 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4231 if (stat != Ok)
4232 return GenericError;
4234 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4236 IStream_Release(stream);
4237 return stat;
4240 /*************************************************************************
4241 * Encoding functions -
4242 * These functions encode an image in different image file formats.
4245 static GpStatus encode_image_wic(GpImage *image, IStream* stream,
4246 REFGUID container, GDIPCONST EncoderParameters* params)
4248 GpStatus stat;
4249 GpBitmap *bitmap;
4250 IWICImagingFactory *factory;
4251 IWICBitmapEncoder *encoder;
4252 IWICBitmapFrameEncode *frameencode;
4253 IPropertyBag2 *encoderoptions;
4254 HRESULT hr;
4255 UINT width, height;
4256 PixelFormat gdipformat=0;
4257 const WICPixelFormatGUID *desired_wicformat;
4258 WICPixelFormatGUID wicformat;
4259 GpRect rc;
4260 BitmapData lockeddata;
4261 UINT i;
4263 if (image->type != ImageTypeBitmap)
4264 return GenericError;
4266 bitmap = (GpBitmap*)image;
4268 GdipGetImageWidth(image, &width);
4269 GdipGetImageHeight(image, &height);
4271 rc.X = 0;
4272 rc.Y = 0;
4273 rc.Width = width;
4274 rc.Height = height;
4276 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
4277 if (FAILED(hr))
4278 return hresult_to_status(hr);
4279 hr = IWICImagingFactory_CreateEncoder(factory, container, NULL, &encoder);
4280 IWICImagingFactory_Release(factory);
4281 if (FAILED(hr))
4282 return hresult_to_status(hr);
4284 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
4286 if (SUCCEEDED(hr))
4288 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4291 if (SUCCEEDED(hr)) /* created frame */
4293 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4295 if (SUCCEEDED(hr))
4296 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4298 if (SUCCEEDED(hr))
4299 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4301 if (SUCCEEDED(hr))
4303 for (i=0; pixel_formats[i].wic_format; i++)
4305 if (pixel_formats[i].gdip_format == bitmap->format)
4307 desired_wicformat = pixel_formats[i].wic_format;
4308 gdipformat = bitmap->format;
4309 break;
4312 if (!gdipformat)
4314 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
4315 gdipformat = PixelFormat32bppARGB;
4318 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
4319 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4322 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
4324 /* Encoder doesn't support this bitmap's format. */
4325 gdipformat = 0;
4326 for (i=0; pixel_formats[i].wic_format; i++)
4328 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
4330 gdipformat = pixel_formats[i].gdip_format;
4331 break;
4334 if (!gdipformat)
4336 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
4337 hr = E_FAIL;
4341 if (SUCCEEDED(hr))
4343 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4344 &lockeddata);
4346 if (stat == Ok)
4348 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4349 BYTE *row;
4351 /* write one row at a time in case stride is negative */
4352 row = lockeddata.Scan0;
4353 for (i=0; i<lockeddata.Height; i++)
4355 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4356 if (FAILED(hr)) break;
4357 row += lockeddata.Stride;
4360 GdipBitmapUnlockBits(bitmap, &lockeddata);
4362 else
4363 hr = E_FAIL;
4366 if (SUCCEEDED(hr))
4367 hr = IWICBitmapFrameEncode_Commit(frameencode);
4369 IWICBitmapFrameEncode_Release(frameencode);
4370 IPropertyBag2_Release(encoderoptions);
4373 if (SUCCEEDED(hr))
4374 hr = IWICBitmapEncoder_Commit(encoder);
4376 IWICBitmapEncoder_Release(encoder);
4377 return hresult_to_status(hr);
4380 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4381 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4383 return encode_image_wic(image, stream, &GUID_ContainerFormatBmp, params);
4386 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4387 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4389 return encode_image_wic(image, stream, &GUID_ContainerFormatTiff, params);
4392 static GpStatus encode_image_png(GpImage *image, IStream* stream,
4393 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4395 return encode_image_wic(image, stream, &GUID_ContainerFormatPng, params);
4398 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4399 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4401 return encode_image_wic(image, stream, &GUID_ContainerFormatJpeg, params);
4404 static GpStatus encode_image_gif(GpImage *image, IStream* stream,
4405 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4407 return encode_image_wic(image, stream, &CLSID_WICGifEncoder, params);
4410 /*****************************************************************************
4411 * GdipSaveImageToStream [GDIPLUS.@]
4413 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4414 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4416 GpStatus stat;
4417 encode_image_func encode_image;
4418 int i;
4420 TRACE("%p %p %p %p\n", image, stream, clsid, params);
4422 if(!image || !stream)
4423 return InvalidParameter;
4425 /* select correct encoder */
4426 encode_image = NULL;
4427 for (i = 0; i < NUM_CODECS; i++) {
4428 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4429 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4430 encode_image = codecs[i].encode_func;
4432 if (encode_image == NULL)
4433 return UnknownImageFormat;
4435 stat = encode_image(image, stream, clsid, params);
4437 return stat;
4440 /*****************************************************************************
4441 * GdipSaveAdd [GDIPLUS.@]
4443 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4445 FIXME("(%p,%p): stub\n", image, params);
4446 return Ok;
4449 /*****************************************************************************
4450 * GdipGetImagePalette [GDIPLUS.@]
4452 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4454 INT count;
4456 TRACE("(%p,%p,%i)\n", image, palette, size);
4458 if (!image || !palette)
4459 return InvalidParameter;
4461 count = image->palette ? image->palette->Count : 0;
4463 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4465 TRACE("<-- InsufficientBuffer\n");
4466 return InsufficientBuffer;
4469 if (image->palette)
4471 palette->Flags = image->palette->Flags;
4472 palette->Count = image->palette->Count;
4473 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4475 else
4477 palette->Flags = 0;
4478 palette->Count = 0;
4480 return Ok;
4483 /*****************************************************************************
4484 * GdipSetImagePalette [GDIPLUS.@]
4486 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4487 GDIPCONST ColorPalette *palette)
4489 ColorPalette *new_palette;
4491 TRACE("(%p,%p)\n", image, palette);
4493 if(!image || !palette || palette->Count > 256)
4494 return InvalidParameter;
4496 new_palette = heap_alloc_zero(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4497 if (!new_palette) return OutOfMemory;
4499 heap_free(image->palette);
4500 image->palette = new_palette;
4501 image->palette->Flags = palette->Flags;
4502 image->palette->Count = palette->Count;
4503 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4505 return Ok;
4508 /*************************************************************************
4509 * Encoders -
4510 * Structures that represent which formats we support for encoding.
4513 /* ImageCodecInfo creation routines taken from libgdiplus */
4514 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4515 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4516 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4517 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4518 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4519 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4521 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4522 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4523 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4524 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4525 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4526 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4528 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4529 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4530 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4531 static const WCHAR gif_format[] = {'G','I','F',0};
4532 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4533 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4535 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4536 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4537 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4538 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4539 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4540 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4542 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4543 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4544 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4545 static const WCHAR emf_format[] = {'E','M','F',0};
4546 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4547 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4549 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4550 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4551 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4552 static const WCHAR wmf_format[] = {'W','M','F',0};
4553 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4554 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4556 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4557 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4558 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4559 static const WCHAR png_format[] = {'P','N','G',0};
4560 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4561 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4563 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4564 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4565 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4566 static const WCHAR ico_format[] = {'I','C','O',0};
4567 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4568 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4570 static const struct image_codec codecs[NUM_CODECS] = {
4572 { /* BMP */
4573 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4574 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4575 /* CodecName */ bmp_codecname,
4576 /* DllName */ NULL,
4577 /* FormatDescription */ bmp_format,
4578 /* FilenameExtension */ bmp_extension,
4579 /* MimeType */ bmp_mimetype,
4580 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4581 /* Version */ 1,
4582 /* SigCount */ 1,
4583 /* SigSize */ 2,
4584 /* SigPattern */ bmp_sig_pattern,
4585 /* SigMask */ bmp_sig_mask,
4587 encode_image_BMP,
4588 decode_image_bmp,
4589 select_frame_wic
4592 { /* JPEG */
4593 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4594 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4595 /* CodecName */ jpeg_codecname,
4596 /* DllName */ NULL,
4597 /* FormatDescription */ jpeg_format,
4598 /* FilenameExtension */ jpeg_extension,
4599 /* MimeType */ jpeg_mimetype,
4600 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4601 /* Version */ 1,
4602 /* SigCount */ 1,
4603 /* SigSize */ 2,
4604 /* SigPattern */ jpeg_sig_pattern,
4605 /* SigMask */ jpeg_sig_mask,
4607 encode_image_jpeg,
4608 decode_image_jpeg,
4609 select_frame_wic
4612 { /* GIF */
4613 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4614 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4615 /* CodecName */ gif_codecname,
4616 /* DllName */ NULL,
4617 /* FormatDescription */ gif_format,
4618 /* FilenameExtension */ gif_extension,
4619 /* MimeType */ gif_mimetype,
4620 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4621 /* Version */ 1,
4622 /* SigCount */ 2,
4623 /* SigSize */ 6,
4624 /* SigPattern */ gif_sig_pattern,
4625 /* SigMask */ gif_sig_mask,
4627 encode_image_gif,
4628 decode_image_gif,
4629 select_frame_gif
4632 { /* TIFF */
4633 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4634 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4635 /* CodecName */ tiff_codecname,
4636 /* DllName */ NULL,
4637 /* FormatDescription */ tiff_format,
4638 /* FilenameExtension */ tiff_extension,
4639 /* MimeType */ tiff_mimetype,
4640 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4641 /* Version */ 1,
4642 /* SigCount */ 2,
4643 /* SigSize */ 4,
4644 /* SigPattern */ tiff_sig_pattern,
4645 /* SigMask */ tiff_sig_mask,
4647 encode_image_tiff,
4648 decode_image_tiff,
4649 select_frame_wic
4652 { /* EMF */
4653 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4654 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4655 /* CodecName */ emf_codecname,
4656 /* DllName */ NULL,
4657 /* FormatDescription */ emf_format,
4658 /* FilenameExtension */ emf_extension,
4659 /* MimeType */ emf_mimetype,
4660 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4661 /* Version */ 1,
4662 /* SigCount */ 1,
4663 /* SigSize */ 4,
4664 /* SigPattern */ emf_sig_pattern,
4665 /* SigMask */ emf_sig_mask,
4667 NULL,
4668 decode_image_olepicture_metafile,
4669 NULL
4672 { /* WMF */
4673 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4674 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4675 /* CodecName */ wmf_codecname,
4676 /* DllName */ NULL,
4677 /* FormatDescription */ wmf_format,
4678 /* FilenameExtension */ wmf_extension,
4679 /* MimeType */ wmf_mimetype,
4680 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4681 /* Version */ 1,
4682 /* SigCount */ 1,
4683 /* SigSize */ 2,
4684 /* SigPattern */ wmf_sig_pattern,
4685 /* SigMask */ wmf_sig_mask,
4687 NULL,
4688 decode_image_olepicture_metafile,
4689 NULL
4692 { /* PNG */
4693 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4694 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4695 /* CodecName */ png_codecname,
4696 /* DllName */ NULL,
4697 /* FormatDescription */ png_format,
4698 /* FilenameExtension */ png_extension,
4699 /* MimeType */ png_mimetype,
4700 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4701 /* Version */ 1,
4702 /* SigCount */ 1,
4703 /* SigSize */ 8,
4704 /* SigPattern */ png_sig_pattern,
4705 /* SigMask */ png_sig_mask,
4707 encode_image_png,
4708 decode_image_png,
4709 select_frame_wic
4712 { /* ICO */
4713 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4714 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4715 /* CodecName */ ico_codecname,
4716 /* DllName */ NULL,
4717 /* FormatDescription */ ico_format,
4718 /* FilenameExtension */ ico_extension,
4719 /* MimeType */ ico_mimetype,
4720 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4721 /* Version */ 1,
4722 /* SigCount */ 1,
4723 /* SigSize */ 4,
4724 /* SigPattern */ ico_sig_pattern,
4725 /* SigMask */ ico_sig_mask,
4727 NULL,
4728 decode_image_icon,
4729 select_frame_wic
4733 /*****************************************************************************
4734 * GdipGetImageDecodersSize [GDIPLUS.@]
4736 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
4738 int decoder_count=0;
4739 int i;
4740 TRACE("%p %p\n", numDecoders, size);
4742 if (!numDecoders || !size)
4743 return InvalidParameter;
4745 for (i=0; i<NUM_CODECS; i++)
4747 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4748 decoder_count++;
4751 *numDecoders = decoder_count;
4752 *size = decoder_count * sizeof(ImageCodecInfo);
4754 return Ok;
4757 /*****************************************************************************
4758 * GdipGetImageDecoders [GDIPLUS.@]
4760 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
4762 int i, decoder_count=0;
4763 TRACE("%u %u %p\n", numDecoders, size, decoders);
4765 if (!decoders ||
4766 size != numDecoders * sizeof(ImageCodecInfo))
4767 return GenericError;
4769 for (i=0; i<NUM_CODECS; i++)
4771 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4773 if (decoder_count == numDecoders) return GenericError;
4774 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4775 decoder_count++;
4779 if (decoder_count < numDecoders) return GenericError;
4781 return Ok;
4784 /*****************************************************************************
4785 * GdipGetImageEncodersSize [GDIPLUS.@]
4787 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4789 int encoder_count=0;
4790 int i;
4791 TRACE("%p %p\n", numEncoders, size);
4793 if (!numEncoders || !size)
4794 return InvalidParameter;
4796 for (i=0; i<NUM_CODECS; i++)
4798 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4799 encoder_count++;
4802 *numEncoders = encoder_count;
4803 *size = encoder_count * sizeof(ImageCodecInfo);
4805 return Ok;
4808 /*****************************************************************************
4809 * GdipGetImageEncoders [GDIPLUS.@]
4811 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4813 int i, encoder_count=0;
4814 TRACE("%u %u %p\n", numEncoders, size, encoders);
4816 if (!encoders ||
4817 size != numEncoders * sizeof(ImageCodecInfo))
4818 return GenericError;
4820 for (i=0; i<NUM_CODECS; i++)
4822 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4824 if (encoder_count == numEncoders) return GenericError;
4825 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4826 encoder_count++;
4830 if (encoder_count < numEncoders) return GenericError;
4832 return Ok;
4835 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
4836 GDIPCONST CLSID* clsidEncoder, UINT *size)
4838 static int calls;
4840 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
4842 if(!(calls++))
4843 FIXME("not implemented\n");
4845 *size = 0;
4847 return NotImplemented;
4850 static PixelFormat get_16bpp_format(HBITMAP hbm)
4852 BITMAPV4HEADER bmh;
4853 HDC hdc;
4854 PixelFormat result;
4856 hdc = CreateCompatibleDC(NULL);
4858 memset(&bmh, 0, sizeof(bmh));
4859 bmh.bV4Size = sizeof(bmh);
4860 bmh.bV4Width = 1;
4861 bmh.bV4Height = 1;
4862 bmh.bV4V4Compression = BI_BITFIELDS;
4863 bmh.bV4BitCount = 16;
4865 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4867 if (bmh.bV4RedMask == 0x7c00 &&
4868 bmh.bV4GreenMask == 0x3e0 &&
4869 bmh.bV4BlueMask == 0x1f)
4871 result = PixelFormat16bppRGB555;
4873 else if (bmh.bV4RedMask == 0xf800 &&
4874 bmh.bV4GreenMask == 0x7e0 &&
4875 bmh.bV4BlueMask == 0x1f)
4877 result = PixelFormat16bppRGB565;
4879 else
4881 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4882 bmh.bV4GreenMask, bmh.bV4BlueMask);
4883 result = PixelFormatUndefined;
4886 DeleteDC(hdc);
4888 return result;
4891 /*****************************************************************************
4892 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4894 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4896 BITMAP bm;
4897 GpStatus retval;
4898 PixelFormat format;
4899 BitmapData lockeddata;
4901 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4903 if(!hbm || !bitmap)
4904 return InvalidParameter;
4906 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4907 return InvalidParameter;
4909 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4910 switch(bm.bmBitsPixel) {
4911 case 1:
4912 format = PixelFormat1bppIndexed;
4913 break;
4914 case 4:
4915 format = PixelFormat4bppIndexed;
4916 break;
4917 case 8:
4918 format = PixelFormat8bppIndexed;
4919 break;
4920 case 16:
4921 format = get_16bpp_format(hbm);
4922 if (format == PixelFormatUndefined)
4923 return InvalidParameter;
4924 break;
4925 case 24:
4926 format = PixelFormat24bppRGB;
4927 break;
4928 case 32:
4929 format = PixelFormat32bppRGB;
4930 break;
4931 case 48:
4932 format = PixelFormat48bppRGB;
4933 break;
4934 default:
4935 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4936 return InvalidParameter;
4939 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4940 format, NULL, bitmap);
4942 if (retval == Ok)
4944 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4945 format, &lockeddata);
4946 if (retval == Ok)
4948 HDC hdc;
4949 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
4950 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
4951 INT src_height;
4953 hdc = CreateCompatibleDC(NULL);
4955 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4956 pbmi->bmiHeader.biBitCount = 0;
4958 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4960 src_height = abs(pbmi->bmiHeader.biHeight);
4961 pbmi->bmiHeader.biHeight = -src_height;
4963 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
4965 DeleteDC(hdc);
4967 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4970 if (retval == Ok && hpal)
4972 PALETTEENTRY entry[256];
4973 ColorPalette *palette=NULL;
4974 int i, num_palette_entries;
4976 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
4977 if (!num_palette_entries)
4978 retval = GenericError;
4980 palette = heap_alloc_zero(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4981 if (!palette)
4982 retval = OutOfMemory;
4984 if (retval == Ok)
4986 palette->Flags = 0;
4987 palette->Count = num_palette_entries;
4989 for (i=0; i<num_palette_entries; i++)
4991 palette->Entries[i] = 0xff000000 | entry[i].peRed << 16 |
4992 entry[i].peGreen << 8 | entry[i].peBlue;
4995 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
4998 heap_free(palette);
5001 if (retval != Ok)
5003 GdipDisposeImage((GpImage*)*bitmap);
5004 *bitmap = NULL;
5008 return retval;
5011 /*****************************************************************************
5012 * GdipCreateEffect [GDIPLUS.@]
5014 GpStatus WINGDIPAPI GdipCreateEffect(const GUID guid, CGpEffect **effect)
5016 FIXME("(%s, %p): stub\n", debugstr_guid(&guid), effect);
5018 if(!effect)
5019 return InvalidParameter;
5021 *effect = NULL;
5023 return NotImplemented;
5026 /*****************************************************************************
5027 * GdipDeleteEffect [GDIPLUS.@]
5029 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
5031 FIXME("(%p): stub\n", effect);
5032 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5033 * in Windows's gdiplus */
5034 return NotImplemented;
5037 /*****************************************************************************
5038 * GdipSetEffectParameters [GDIPLUS.@]
5040 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
5041 const VOID *params, const UINT size)
5043 static int calls;
5045 TRACE("(%p,%p,%u)\n", effect, params, size);
5047 if(!(calls++))
5048 FIXME("not implemented\n");
5050 return NotImplemented;
5053 /*****************************************************************************
5054 * GdipGetImageFlags [GDIPLUS.@]
5056 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
5058 TRACE("%p %p\n", image, flags);
5060 if(!image || !flags)
5061 return InvalidParameter;
5063 *flags = image->flags;
5065 return Ok;
5068 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
5070 TRACE("(%d, %p)\n", control, param);
5072 switch(control){
5073 case TestControlForceBilinear:
5074 if(param)
5075 FIXME("TestControlForceBilinear not handled\n");
5076 break;
5077 case TestControlNoICM:
5078 if(param)
5079 FIXME("TestControlNoICM not handled\n");
5080 break;
5081 case TestControlGetBuildNumber:
5082 *((DWORD*)param) = 3102;
5083 break;
5086 return Ok;
5089 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
5091 TRACE("%p\n", image);
5093 return Ok;
5096 /*****************************************************************************
5097 * GdipGetImageThumbnail [GDIPLUS.@]
5099 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
5100 GpImage **ret_image, GetThumbnailImageAbort cb,
5101 VOID * cb_data)
5103 GpStatus stat;
5104 GpGraphics *graphics;
5105 UINT srcwidth, srcheight;
5107 TRACE("(%p %u %u %p %p %p)\n",
5108 image, width, height, ret_image, cb, cb_data);
5110 if (!image || !ret_image)
5111 return InvalidParameter;
5113 if (!width) width = 120;
5114 if (!height) height = 120;
5116 GdipGetImageWidth(image, &srcwidth);
5117 GdipGetImageHeight(image, &srcheight);
5119 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
5120 NULL, (GpBitmap**)ret_image);
5122 if (stat == Ok)
5124 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
5126 if (stat == Ok)
5128 stat = GdipDrawImageRectRectI(graphics, image,
5129 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
5130 NULL, NULL, NULL);
5132 GdipDeleteGraphics(graphics);
5135 if (stat != Ok)
5137 GdipDisposeImage(*ret_image);
5138 *ret_image = NULL;
5142 return stat;
5145 /*****************************************************************************
5146 * GdipImageRotateFlip [GDIPLUS.@]
5148 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
5150 GpBitmap *new_bitmap;
5151 GpBitmap *bitmap;
5152 int bpp, bytesperpixel;
5153 BOOL rotate_90, flip_x, flip_y;
5154 int src_x_offset, src_y_offset;
5155 LPBYTE src_origin;
5156 UINT x, y, width, height;
5157 BitmapData src_lock, dst_lock;
5158 GpStatus stat;
5160 TRACE("(%p, %u)\n", image, type);
5162 if (!image)
5163 return InvalidParameter;
5165 rotate_90 = type&1;
5166 flip_x = (type&6) == 2 || (type&6) == 4;
5167 flip_y = (type&3) == 1 || (type&3) == 2;
5169 if (image->type != ImageTypeBitmap)
5171 FIXME("Not implemented for type %i\n", image->type);
5172 return NotImplemented;
5175 bitmap = (GpBitmap*)image;
5176 bpp = PIXELFORMATBPP(bitmap->format);
5178 if (bpp < 8)
5180 FIXME("Not implemented for %i bit images\n", bpp);
5181 return NotImplemented;
5184 if (rotate_90)
5186 width = bitmap->height;
5187 height = bitmap->width;
5189 else
5191 width = bitmap->width;
5192 height = bitmap->height;
5195 bytesperpixel = bpp/8;
5197 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5199 if (stat != Ok)
5200 return stat;
5202 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
5204 if (stat == Ok)
5206 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5208 if (stat == Ok)
5210 LPBYTE src_row, src_pixel;
5211 LPBYTE dst_row, dst_pixel;
5213 src_origin = src_lock.Scan0;
5214 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5215 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
5217 if (rotate_90)
5219 if (flip_y) src_x_offset = -src_lock.Stride;
5220 else src_x_offset = src_lock.Stride;
5221 if (flip_x) src_y_offset = -bytesperpixel;
5222 else src_y_offset = bytesperpixel;
5224 else
5226 if (flip_x) src_x_offset = -bytesperpixel;
5227 else src_x_offset = bytesperpixel;
5228 if (flip_y) src_y_offset = -src_lock.Stride;
5229 else src_y_offset = src_lock.Stride;
5232 src_row = src_origin;
5233 dst_row = dst_lock.Scan0;
5234 for (y=0; y<height; y++)
5236 src_pixel = src_row;
5237 dst_pixel = dst_row;
5238 for (x=0; x<width; x++)
5240 /* FIXME: This could probably be faster without memcpy. */
5241 memcpy(dst_pixel, src_pixel, bytesperpixel);
5242 dst_pixel += bytesperpixel;
5243 src_pixel += src_x_offset;
5245 src_row += src_y_offset;
5246 dst_row += dst_lock.Stride;
5249 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5252 GdipBitmapUnlockBits(bitmap, &src_lock);
5255 if (stat == Ok)
5256 move_bitmap(bitmap, new_bitmap, FALSE);
5257 else
5258 GdipDisposeImage((GpImage*)new_bitmap);
5260 return stat;
5263 /*****************************************************************************
5264 * GdipImageSetAbort [GDIPLUS.@]
5266 GpStatus WINGDIPAPI GdipImageSetAbort(GpImage *image, GdiplusAbort *pabort)
5268 FIXME("(%p, %p): stub\n", image, pabort);
5269 return NotImplemented;
5272 /*****************************************************************************
5273 * GdipBitmapConvertFormat [GDIPLUS.@]
5275 GpStatus WINGDIPAPI GdipBitmapConvertFormat(GpBitmap *bitmap, PixelFormat format, DitherType dithertype,
5276 PaletteType palettetype, ColorPalette *palette, REAL alphathreshold)
5278 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap, format, dithertype, palettetype, palette, alphathreshold);
5279 return NotImplemented;