wined3d: Use a separate STATE_VDECL state handler in the GLSL pipeline.
[wine/multimedia.git] / dlls / gdiplus / image.c
blob8eb1419f1b0e645aad459a7614d914ca58ca4cf0
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 = HeapAlloc(GetProcessHeap(), 0, 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 = GdipAlloc(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 GdipFree(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 GdipFree(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 GdipFree(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 = GdipAlloc(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 GdipFree((*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 = GdipAlloc(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 GdipFree(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 = HeapAlloc(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, 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 = GdipAlloc(size);
1861 if (!own_bits) return OutOfMemory;
1863 if (stride < 0)
1864 bits += stride * (1 - height);
1868 *bitmap = GdipAlloc(sizeof(GpBitmap));
1869 if(!*bitmap)
1871 DeleteObject(hbitmap);
1872 GdipFree(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 = GdipAlloc(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 = GdipAlloc(sizeof(GpCachedBitmap));
1979 if(!*cachedbmp)
1980 return OutOfMemory;
1982 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1983 if(stat != Ok){
1984 GdipFree(*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 = GdipAlloc(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 GdipFree(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 GdipFree(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 GdipFree(dst->bitmapbits);
2077 GdipFree(dst->own_bits);
2078 DeleteDC(dst->hdc);
2079 DeleteObject(dst->hbitmap);
2081 if (clobber_palette)
2083 GdipFree(dst->image.palette);
2084 dst->image.palette = src->image.palette;
2086 else
2087 GdipFree(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 GdipFree(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 GdipFree(src);
2116 static GpStatus free_image_data(GpImage *image)
2118 if(!image)
2119 return InvalidParameter;
2121 if (image->type == ImageTypeBitmap)
2123 GdipFree(((GpBitmap*)image)->bitmapbits);
2124 GdipFree(((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 GdipFree(((GpBitmap*)image)->prop_item);
2131 else if (image->type == ImageTypeMetafile)
2133 GpMetafile *metafile = (GpMetafile*)image;
2134 GdipFree(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 GdipFree(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 GdipFree(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 = HeapAlloc(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, 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 = GdipAlloc(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 = GdipAlloc(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 GdipFree(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 IWICMetadataReader_GetMetadataFormat(reader, &format);
3062 if (!IsEqualGUID(&format, guid)) return FALSE;
3064 PropVariantInit(&id);
3065 PropVariantInit(&value);
3067 id.vt = VT_LPWSTR;
3068 id.u.pwszVal = HeapAlloc(GetProcessHeap(), 0, (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 IWICMetadataReader_GetMetadataFormat(reader, &format);
3089 if (!IsEqualGUID(&format, guid)) return NULL;
3091 PropVariantInit(&id);
3092 PropVariantInit(&value);
3094 id.vt = VT_LPWSTR;
3095 id.u.pwszVal = HeapAlloc(GetProcessHeap(), 0, (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 = GdipAlloc(item_size);
3106 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3108 GdipFree(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 = GdipAlloc(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 GdipFree(appext);
3168 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 = GdipAlloc(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 GdipFree(delay);
3384 GdipFree(comment);
3385 GdipFree(loop);
3386 GdipFree(palette);
3387 GdipFree(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 GdipFree(transparent_idx);
3417 IWICBitmapFrameDecode_Release(frame);
3420 static GpStatus initialize_decoder_wic(IStream *stream, REFGUID container, IWICBitmapDecoder **decoder)
3422 IWICImagingFactory *factory;
3423 HRESULT hr;
3425 TRACE("%p,%s\n", stream, wine_dbgstr_guid(container));
3427 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
3428 if (FAILED(hr)) hresult_to_status(hr);
3429 hr = IWICImagingFactory_CreateDecoder(factory, container, NULL, decoder);
3430 IWICImagingFactory_Release(factory);
3431 if (FAILED(hr)) hresult_to_status(hr);
3433 hr = IWICBitmapDecoder_Initialize(*decoder, stream, WICDecodeMetadataCacheOnLoad);
3434 if (FAILED(hr)) hresult_to_status(hr);
3435 return Ok;
3438 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3440 static GpStatus decode_frame_wic(IWICBitmapDecoder *decoder, BOOL force_conversion,
3441 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3443 GpStatus status=Ok;
3444 GpBitmap *bitmap;
3445 HRESULT hr;
3446 IWICBitmapFrameDecode *frame;
3447 IWICBitmapSource *source=NULL;
3448 IWICMetadataBlockReader *block_reader;
3449 WICPixelFormatGUID wic_format;
3450 PixelFormat gdip_format=0;
3451 ColorPalette *palette = NULL;
3452 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3453 int i;
3454 UINT width, height, frame_count;
3455 BitmapData lockeddata;
3456 WICRect wrc;
3458 TRACE("%p,%u,%p\n", decoder, active_frame, image);
3460 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3461 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3462 if (SUCCEEDED(hr)) /* got frame */
3464 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3466 if (SUCCEEDED(hr))
3468 if (!force_conversion)
3470 for (i=0; pixel_formats[i].wic_format; i++)
3472 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3474 source = (IWICBitmapSource*)frame;
3475 IWICBitmapSource_AddRef(source);
3476 gdip_format = pixel_formats[i].gdip_format;
3477 palette_type = pixel_formats[i].palette_type;
3478 break;
3482 if (!source)
3484 /* unknown format; fall back on 32bppARGB */
3485 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3486 gdip_format = PixelFormat32bppARGB;
3488 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3491 if (SUCCEEDED(hr)) /* got source */
3493 hr = IWICBitmapSource_GetSize(source, &width, &height);
3495 if (SUCCEEDED(hr))
3496 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3497 NULL, &bitmap);
3499 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3501 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3502 gdip_format, &lockeddata);
3503 if (status == Ok) /* locked bitmap */
3505 wrc.X = 0;
3506 wrc.Width = width;
3507 wrc.Height = 1;
3508 for (i=0; i<height; i++)
3510 wrc.Y = i;
3511 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3512 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3513 if (FAILED(hr)) break;
3516 GdipBitmapUnlockBits(bitmap, &lockeddata);
3519 if (SUCCEEDED(hr) && status == Ok)
3520 *image = (GpImage*)bitmap;
3521 else
3523 *image = NULL;
3524 GdipDisposeImage((GpImage*)bitmap);
3527 if (SUCCEEDED(hr) && status == Ok)
3529 double dpix, dpiy;
3530 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3531 if (SUCCEEDED(hr))
3533 bitmap->image.xres = dpix;
3534 bitmap->image.yres = dpiy;
3536 hr = S_OK;
3540 IWICBitmapSource_Release(source);
3543 if (SUCCEEDED(hr)) {
3544 bitmap->metadata_reader = NULL;
3546 if (metadata_reader)
3547 metadata_reader(bitmap, decoder, active_frame);
3548 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3550 UINT block_count = 0;
3551 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3552 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3553 IWICMetadataBlockReader_Release(block_reader);
3556 palette = get_palette(frame, palette_type);
3557 IWICBitmapFrameDecode_Release(frame);
3561 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3563 if (status == Ok)
3565 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3566 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3567 bitmap->image.frame_count = frame_count;
3568 bitmap->image.current_frame = active_frame;
3569 bitmap->image.decoder = decoder;
3570 IWICBitmapDecoder_AddRef(decoder);
3571 if (palette)
3573 GdipFree(bitmap->image.palette);
3574 bitmap->image.palette = palette;
3576 else
3578 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3579 bitmap->image.palette->Flags = 0;
3581 TRACE("=> %p\n", *image);
3584 return status;
3587 static GpStatus decode_image_wic(IStream *stream, REFGUID container,
3588 metadata_reader_func metadata_reader, GpImage **image)
3590 IWICBitmapDecoder *decoder;
3591 GpStatus status;
3593 status = initialize_decoder_wic(stream, container, &decoder);
3594 if(status != Ok)
3595 return status;
3597 status = decode_frame_wic(decoder, FALSE, 0, metadata_reader, image);
3598 IWICBitmapDecoder_Release(decoder);
3599 return status;
3602 static GpStatus select_frame_wic(GpImage *image, UINT active_frame)
3604 GpImage *new_image;
3605 GpStatus status;
3607 status = decode_frame_wic(image->decoder, FALSE, active_frame, NULL, &new_image);
3608 if(status != Ok)
3609 return status;
3611 memcpy(&new_image->format, &image->format, sizeof(GUID));
3612 free_image_data(image);
3613 if (image->type == ImageTypeBitmap)
3614 *(GpBitmap *)image = *(GpBitmap *)new_image;
3615 else if (image->type == ImageTypeMetafile)
3616 *(GpMetafile *)image = *(GpMetafile *)new_image;
3617 new_image->type = ~0;
3618 GdipFree(new_image);
3619 return Ok;
3622 static HRESULT get_gif_frame_rect(IWICBitmapFrameDecode *frame,
3623 UINT *left, UINT *top, UINT *width, UINT *height)
3625 static const WCHAR leftW[] = {'L','e','f','t',0};
3626 static const WCHAR topW[] = {'T','o','p',0};
3628 *left = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, leftW);
3629 *top = get_gif_frame_property(frame, &GUID_MetadataFormatIMD, topW);
3631 return IWICBitmapFrameDecode_GetSize(frame, width, height);
3634 static HRESULT blit_gif_frame(GpBitmap *bitmap, IWICBitmapFrameDecode *frame, BOOL first_frame)
3636 UINT i, j, left, top, width, height;
3637 IWICBitmapSource *source;
3638 BYTE *new_bits;
3639 HRESULT hr;
3641 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3642 if(FAILED(hr))
3643 return hr;
3645 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
3646 if(FAILED(hr))
3647 return hr;
3649 new_bits = GdipAlloc(width*height*4);
3650 if(!new_bits)
3651 return E_OUTOFMEMORY;
3653 hr = IWICBitmapSource_CopyPixels(source, NULL, width*4, width*height*4, new_bits);
3654 IWICBitmapSource_Release(source);
3655 if(FAILED(hr)) {
3656 GdipFree(new_bits);
3657 return hr;
3660 for(i=0; i<height && i+top<bitmap->height; i++) {
3661 for(j=0; j<width && j+left<bitmap->width; j++) {
3662 DWORD *src = (DWORD*)(new_bits+i*width*4+j*4);
3663 DWORD *dst = (DWORD*)(bitmap->bits+(i+top)*bitmap->stride+(j+left)*4);
3665 if(first_frame || *src>>24 != 0)
3666 *dst = *src;
3669 GdipFree(new_bits);
3670 return hr;
3673 static DWORD get_gif_background_color(GpBitmap *bitmap)
3675 BYTE bgcolor_idx = 0;
3676 UINT i;
3678 for(i=0; i<bitmap->prop_count; i++) {
3679 if(bitmap->prop_item[i].id == PropertyTagIndexBackground) {
3680 bgcolor_idx = *(BYTE*)bitmap->prop_item[i].value;
3681 break;
3685 for(i=0; i<bitmap->prop_count; i++) {
3686 if(bitmap->prop_item[i].id == PropertyTagIndexTransparent) {
3687 BYTE transparent_idx;
3688 transparent_idx = *(BYTE*)bitmap->prop_item[i].value;
3690 if(transparent_idx == bgcolor_idx)
3691 return 0;
3695 for(i=0; i<bitmap->prop_count; i++) {
3696 if(bitmap->prop_item[i].id == PropertyTagGlobalPalette) {
3697 if(bitmap->prop_item[i].length/3 > bgcolor_idx) {
3698 BYTE *color = ((BYTE*)bitmap->prop_item[i].value)+bgcolor_idx*3;
3699 return color[2] + (color[1]<<8) + (color[0]<<16) + (0xff<<24);
3701 break;
3705 FIXME("can't get gif background color\n");
3706 return 0xffffffff;
3709 static GpStatus select_frame_gif(GpImage* image, UINT active_frame)
3711 static const WCHAR disposalW[] = {'D','i','s','p','o','s','a','l',0};
3713 GpBitmap *bitmap = (GpBitmap*)image;
3714 IWICBitmapFrameDecode *frame;
3715 int cur_frame=0, disposal;
3716 BOOL bgcolor_set = FALSE;
3717 DWORD bgcolor = 0;
3718 HRESULT hr;
3720 if(active_frame > image->current_frame) {
3721 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, image->current_frame, &frame);
3722 if(FAILED(hr))
3723 return hresult_to_status(hr);
3724 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3725 IWICBitmapFrameDecode_Release(frame);
3727 if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND)
3728 cur_frame = image->current_frame;
3729 else if(disposal != GIF_DISPOSE_RESTORE_TO_PREV)
3730 cur_frame = image->current_frame+1;
3733 while(cur_frame != active_frame) {
3734 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, cur_frame, &frame);
3735 if(FAILED(hr))
3736 return hresult_to_status(hr);
3737 disposal = get_gif_frame_property(frame, &GUID_MetadataFormatGCE, disposalW);
3739 if(disposal==GIF_DISPOSE_UNSPECIFIED || disposal==GIF_DISPOSE_DO_NOT_DISPOSE) {
3740 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3741 if(FAILED(hr))
3742 return hresult_to_status(hr);
3743 }else if(disposal == GIF_DISPOSE_RESTORE_TO_BKGND) {
3744 UINT left, top, width, height, i, j;
3746 if(!bgcolor_set) {
3747 bgcolor = get_gif_background_color(bitmap);
3748 bgcolor_set = TRUE;
3751 hr = get_gif_frame_rect(frame, &left, &top, &width, &height);
3752 if(FAILED(hr))
3753 return hresult_to_status(hr);
3754 for(i=top; i<top+height && i<bitmap->height; i++) {
3755 DWORD *bits = (DWORD*)(bitmap->bits+i*bitmap->stride);
3756 for(j=left; j<left+width && j<bitmap->width; j++)
3757 bits[j] = bgcolor;
3761 IWICBitmapFrameDecode_Release(frame);
3762 cur_frame++;
3765 hr = IWICBitmapDecoder_GetFrame(bitmap->image.decoder, active_frame, &frame);
3766 if(FAILED(hr))
3767 return hresult_to_status(hr);
3769 hr = blit_gif_frame(bitmap, frame, cur_frame==0);
3770 IWICBitmapFrameDecode_Release(frame);
3771 if(FAILED(hr))
3772 return hresult_to_status(hr);
3774 image->current_frame = active_frame;
3775 return Ok;
3778 static GpStatus decode_image_icon(IStream* stream, GpImage **image)
3780 return decode_image_wic(stream, &GUID_ContainerFormatIco, NULL, image);
3783 static GpStatus decode_image_bmp(IStream* stream, GpImage **image)
3785 GpStatus status;
3786 GpBitmap* bitmap;
3788 status = decode_image_wic(stream, &GUID_ContainerFormatBmp, NULL, image);
3790 bitmap = (GpBitmap*)*image;
3792 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3794 /* WIC supports bmp files with alpha, but gdiplus does not */
3795 bitmap->format = PixelFormat32bppRGB;
3798 return status;
3801 static GpStatus decode_image_jpeg(IStream* stream, GpImage **image)
3803 return decode_image_wic(stream, &GUID_ContainerFormatJpeg, NULL, image);
3806 static GpStatus decode_image_png(IStream* stream, GpImage **image)
3808 return decode_image_wic(stream, &GUID_ContainerFormatPng, NULL, image);
3811 static GpStatus decode_image_gif(IStream* stream, GpImage **image)
3813 IWICBitmapDecoder *decoder;
3814 UINT frame_count;
3815 GpStatus status;
3816 HRESULT hr;
3818 status = initialize_decoder_wic(stream, &GUID_ContainerFormatGif, &decoder);
3819 if(status != Ok)
3820 return status;
3822 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3823 if(FAILED(hr))
3824 return hresult_to_status(hr);
3826 status = decode_frame_wic(decoder, frame_count>1 ? TRUE : FALSE,
3827 0, gif_metadata_reader, image);
3828 IWICBitmapDecoder_Release(decoder);
3829 if(status != Ok)
3830 return status;
3832 if(frame_count > 1) {
3833 GdipFree((*image)->palette);
3834 (*image)->palette = NULL;
3836 return Ok;
3839 static GpStatus decode_image_tiff(IStream* stream, GpImage **image)
3841 return decode_image_wic(stream, &GUID_ContainerFormatTiff, NULL, image);
3844 static GpStatus decode_image_olepicture_metafile(IStream* stream, GpImage **image)
3846 IPicture *pic;
3848 TRACE("%p %p\n", stream, image);
3850 if(!stream || !image)
3851 return InvalidParameter;
3853 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3854 (LPVOID*) &pic) != S_OK){
3855 TRACE("Could not load picture\n");
3856 return GenericError;
3859 /* FIXME: missing initialization code */
3860 *image = GdipAlloc(sizeof(GpMetafile));
3861 if(!*image) return OutOfMemory;
3862 (*image)->type = ImageTypeMetafile;
3863 (*image)->decoder = NULL;
3864 (*image)->picture = pic;
3865 (*image)->flags = ImageFlagsNone;
3866 (*image)->frame_count = 1;
3867 (*image)->current_frame = 0;
3868 (*image)->palette = NULL;
3870 TRACE("<-- %p\n", *image);
3872 return Ok;
3875 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
3876 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
3878 typedef GpStatus (*decode_image_func)(IStream *stream, GpImage **image);
3880 typedef GpStatus (*select_image_func)(GpImage *image, UINT active_frame);
3882 typedef struct image_codec {
3883 ImageCodecInfo info;
3884 encode_image_func encode_func;
3885 decode_image_func decode_func;
3886 select_image_func select_func;
3887 } image_codec;
3889 typedef enum {
3890 BMP,
3891 JPEG,
3892 GIF,
3893 TIFF,
3894 EMF,
3895 WMF,
3896 PNG,
3897 ICO,
3898 NUM_CODECS
3899 } ImageFormat;
3901 static const struct image_codec codecs[NUM_CODECS];
3903 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
3905 BYTE signature[8];
3906 const BYTE *pattern, *mask;
3907 LARGE_INTEGER seek;
3908 HRESULT hr;
3909 UINT bytesread;
3910 int i;
3911 DWORD j, sig;
3913 /* seek to the start of the stream */
3914 seek.QuadPart = 0;
3915 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3916 if (FAILED(hr)) return hresult_to_status(hr);
3918 /* read the first 8 bytes */
3919 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
3920 hr = IStream_Read(stream, signature, 8, &bytesread);
3921 if (FAILED(hr)) return hresult_to_status(hr);
3922 if (hr == S_FALSE || bytesread == 0) return GenericError;
3924 for (i = 0; i < NUM_CODECS; i++) {
3925 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
3926 bytesread >= codecs[i].info.SigSize)
3928 for (sig=0; sig<codecs[i].info.SigCount; sig++)
3930 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
3931 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
3932 for (j=0; j<codecs[i].info.SigSize; j++)
3933 if ((signature[j] & mask[j]) != pattern[j])
3934 break;
3935 if (j == codecs[i].info.SigSize)
3937 *result = &codecs[i];
3938 return Ok;
3944 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
3945 signature[0],signature[1],signature[2],signature[3],
3946 signature[4],signature[5],signature[6],signature[7]);
3948 return GenericError;
3951 static GpStatus get_decoder_info_from_image(GpImage *image, const struct image_codec **result)
3953 int i;
3955 for (i = 0; i < NUM_CODECS; i++) {
3956 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
3957 IsEqualIID(&codecs[i].info.FormatID, &image->format))
3959 *result = &codecs[i];
3960 return Ok;
3964 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image->format));
3965 return GenericError;
3968 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
3969 UINT frame)
3971 GpStatus stat;
3972 const struct image_codec *codec = NULL;
3974 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
3976 if (!image || !dimensionID)
3977 return InvalidParameter;
3979 if (frame >= image->frame_count)
3981 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
3982 return InvalidParameter;
3985 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
3987 WARN("invalid image type %d\n", image->type);
3988 return InvalidParameter;
3991 if (image->current_frame == frame)
3992 return Ok;
3994 if (!image->decoder)
3996 TRACE("image doesn't have an associated decoder\n");
3997 return Ok;
4000 /* choose an appropriate image decoder */
4001 stat = get_decoder_info_from_image(image, &codec);
4002 if (stat != Ok)
4004 WARN("can't find decoder info\n");
4005 return stat;
4008 return codec->select_func(image, frame);
4011 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
4013 GpStatus stat;
4014 LARGE_INTEGER seek;
4015 HRESULT hr;
4016 const struct image_codec *codec=NULL;
4018 /* choose an appropriate image decoder */
4019 stat = get_decoder_info(stream, &codec);
4020 if (stat != Ok) return stat;
4022 /* seek to the start of the stream */
4023 seek.QuadPart = 0;
4024 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
4025 if (FAILED(hr)) return hresult_to_status(hr);
4027 /* call on the image decoder to do the real work */
4028 stat = codec->decode_func(stream, image);
4030 /* take note of the original data format */
4031 if (stat == Ok)
4033 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
4034 return Ok;
4037 return stat;
4040 /* FIXME: no ICM */
4041 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
4043 TRACE("%p %p\n", stream, image);
4045 return GdipLoadImageFromStream(stream, image);
4048 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
4050 static int calls;
4052 TRACE("(%p,%u)\n", image, propId);
4054 if(!image)
4055 return InvalidParameter;
4057 if(!(calls++))
4058 FIXME("not implemented\n");
4060 return NotImplemented;
4063 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
4065 static int calls;
4067 if (!image || !item) return InvalidParameter;
4069 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
4071 if(!(calls++))
4072 FIXME("not implemented\n");
4074 return Ok;
4077 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
4078 GDIPCONST CLSID *clsidEncoder,
4079 GDIPCONST EncoderParameters *encoderParams)
4081 GpStatus stat;
4082 IStream *stream;
4084 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
4086 if (!image || !filename|| !clsidEncoder)
4087 return InvalidParameter;
4089 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4090 if (stat != Ok)
4091 return GenericError;
4093 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4095 IStream_Release(stream);
4096 return stat;
4099 /*************************************************************************
4100 * Encoding functions -
4101 * These functions encode an image in different image file formats.
4104 static GpStatus encode_image_wic(GpImage *image, IStream* stream,
4105 REFGUID container, GDIPCONST EncoderParameters* params)
4107 GpStatus stat;
4108 GpBitmap *bitmap;
4109 IWICImagingFactory *factory;
4110 IWICBitmapEncoder *encoder;
4111 IWICBitmapFrameEncode *frameencode;
4112 IPropertyBag2 *encoderoptions;
4113 HRESULT hr;
4114 UINT width, height;
4115 PixelFormat gdipformat=0;
4116 const WICPixelFormatGUID *desired_wicformat;
4117 WICPixelFormatGUID wicformat;
4118 GpRect rc;
4119 BitmapData lockeddata;
4120 UINT i;
4122 if (image->type != ImageTypeBitmap)
4123 return GenericError;
4125 bitmap = (GpBitmap*)image;
4127 GdipGetImageWidth(image, &width);
4128 GdipGetImageHeight(image, &height);
4130 rc.X = 0;
4131 rc.Y = 0;
4132 rc.Width = width;
4133 rc.Height = height;
4135 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
4136 if (FAILED(hr))
4137 return hresult_to_status(hr);
4138 hr = IWICImagingFactory_CreateEncoder(factory, container, NULL, &encoder);
4139 IWICImagingFactory_Release(factory);
4140 if (FAILED(hr))
4141 return hresult_to_status(hr);
4143 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
4145 if (SUCCEEDED(hr))
4147 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4150 if (SUCCEEDED(hr)) /* created frame */
4152 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4154 if (SUCCEEDED(hr))
4155 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4157 if (SUCCEEDED(hr))
4158 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4160 if (SUCCEEDED(hr))
4162 for (i=0; pixel_formats[i].wic_format; i++)
4164 if (pixel_formats[i].gdip_format == bitmap->format)
4166 desired_wicformat = pixel_formats[i].wic_format;
4167 gdipformat = bitmap->format;
4168 break;
4171 if (!gdipformat)
4173 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
4174 gdipformat = PixelFormat32bppARGB;
4177 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
4178 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4181 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
4183 /* Encoder doesn't support this bitmap's format. */
4184 gdipformat = 0;
4185 for (i=0; pixel_formats[i].wic_format; i++)
4187 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
4189 gdipformat = pixel_formats[i].gdip_format;
4190 break;
4193 if (!gdipformat)
4195 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
4196 hr = E_FAIL;
4200 if (SUCCEEDED(hr))
4202 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4203 &lockeddata);
4205 if (stat == Ok)
4207 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4208 BYTE *row;
4210 /* write one row at a time in case stride is negative */
4211 row = lockeddata.Scan0;
4212 for (i=0; i<lockeddata.Height; i++)
4214 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4215 if (FAILED(hr)) break;
4216 row += lockeddata.Stride;
4219 GdipBitmapUnlockBits(bitmap, &lockeddata);
4221 else
4222 hr = E_FAIL;
4225 if (SUCCEEDED(hr))
4226 hr = IWICBitmapFrameEncode_Commit(frameencode);
4228 IWICBitmapFrameEncode_Release(frameencode);
4229 IPropertyBag2_Release(encoderoptions);
4232 if (SUCCEEDED(hr))
4233 hr = IWICBitmapEncoder_Commit(encoder);
4235 IWICBitmapEncoder_Release(encoder);
4236 return hresult_to_status(hr);
4239 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4240 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4242 return encode_image_wic(image, stream, &GUID_ContainerFormatBmp, params);
4245 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4246 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4248 return encode_image_wic(image, stream, &GUID_ContainerFormatTiff, params);
4251 static GpStatus encode_image_png(GpImage *image, IStream* stream,
4252 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4254 return encode_image_wic(image, stream, &GUID_ContainerFormatPng, params);
4257 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4258 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4260 return encode_image_wic(image, stream, &GUID_ContainerFormatJpeg, params);
4263 /*****************************************************************************
4264 * GdipSaveImageToStream [GDIPLUS.@]
4266 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4267 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4269 GpStatus stat;
4270 encode_image_func encode_image;
4271 int i;
4273 TRACE("%p %p %p %p\n", image, stream, clsid, params);
4275 if(!image || !stream)
4276 return InvalidParameter;
4278 /* select correct encoder */
4279 encode_image = NULL;
4280 for (i = 0; i < NUM_CODECS; i++) {
4281 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4282 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4283 encode_image = codecs[i].encode_func;
4285 if (encode_image == NULL)
4286 return UnknownImageFormat;
4288 stat = encode_image(image, stream, clsid, params);
4290 return stat;
4293 /*****************************************************************************
4294 * GdipSaveAdd [GDIPLUS.@]
4296 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4298 FIXME("(%p,%p): stub\n", image, params);
4299 return Ok;
4302 /*****************************************************************************
4303 * GdipGetImagePalette [GDIPLUS.@]
4305 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4307 INT count;
4309 TRACE("(%p,%p,%i)\n", image, palette, size);
4311 if (!image || !palette)
4312 return InvalidParameter;
4314 count = image->palette ? image->palette->Count : 0;
4316 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4318 TRACE("<-- InsufficientBuffer\n");
4319 return InsufficientBuffer;
4322 if (image->palette)
4324 palette->Flags = image->palette->Flags;
4325 palette->Count = image->palette->Count;
4326 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4328 else
4330 palette->Flags = 0;
4331 palette->Count = 0;
4333 return Ok;
4336 /*****************************************************************************
4337 * GdipSetImagePalette [GDIPLUS.@]
4339 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4340 GDIPCONST ColorPalette *palette)
4342 ColorPalette *new_palette;
4344 TRACE("(%p,%p)\n", image, palette);
4346 if(!image || !palette || palette->Count > 256)
4347 return InvalidParameter;
4349 new_palette = GdipAlloc(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4350 if (!new_palette) return OutOfMemory;
4352 GdipFree(image->palette);
4353 image->palette = new_palette;
4354 image->palette->Flags = palette->Flags;
4355 image->palette->Count = palette->Count;
4356 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4358 return Ok;
4361 /*************************************************************************
4362 * Encoders -
4363 * Structures that represent which formats we support for encoding.
4366 /* ImageCodecInfo creation routines taken from libgdiplus */
4367 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4368 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4369 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4370 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4371 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4372 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4374 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4375 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4376 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4377 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4378 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4379 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4381 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4382 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4383 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4384 static const WCHAR gif_format[] = {'G','I','F',0};
4385 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4386 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4388 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4389 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4390 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4391 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4392 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4393 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4395 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4396 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4397 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4398 static const WCHAR emf_format[] = {'E','M','F',0};
4399 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4400 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4402 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4403 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4404 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4405 static const WCHAR wmf_format[] = {'W','M','F',0};
4406 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4407 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4409 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4410 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4411 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4412 static const WCHAR png_format[] = {'P','N','G',0};
4413 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4414 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4416 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4417 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4418 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4419 static const WCHAR ico_format[] = {'I','C','O',0};
4420 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4421 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4423 static const struct image_codec codecs[NUM_CODECS] = {
4425 { /* BMP */
4426 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4427 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4428 /* CodecName */ bmp_codecname,
4429 /* DllName */ NULL,
4430 /* FormatDescription */ bmp_format,
4431 /* FilenameExtension */ bmp_extension,
4432 /* MimeType */ bmp_mimetype,
4433 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4434 /* Version */ 1,
4435 /* SigCount */ 1,
4436 /* SigSize */ 2,
4437 /* SigPattern */ bmp_sig_pattern,
4438 /* SigMask */ bmp_sig_mask,
4440 encode_image_BMP,
4441 decode_image_bmp,
4442 select_frame_wic
4445 { /* JPEG */
4446 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4447 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4448 /* CodecName */ jpeg_codecname,
4449 /* DllName */ NULL,
4450 /* FormatDescription */ jpeg_format,
4451 /* FilenameExtension */ jpeg_extension,
4452 /* MimeType */ jpeg_mimetype,
4453 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4454 /* Version */ 1,
4455 /* SigCount */ 1,
4456 /* SigSize */ 2,
4457 /* SigPattern */ jpeg_sig_pattern,
4458 /* SigMask */ jpeg_sig_mask,
4460 encode_image_jpeg,
4461 decode_image_jpeg,
4462 select_frame_wic
4465 { /* GIF */
4466 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4467 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4468 /* CodecName */ gif_codecname,
4469 /* DllName */ NULL,
4470 /* FormatDescription */ gif_format,
4471 /* FilenameExtension */ gif_extension,
4472 /* MimeType */ gif_mimetype,
4473 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4474 /* Version */ 1,
4475 /* SigCount */ 2,
4476 /* SigSize */ 6,
4477 /* SigPattern */ gif_sig_pattern,
4478 /* SigMask */ gif_sig_mask,
4480 NULL,
4481 decode_image_gif,
4482 select_frame_gif
4485 { /* TIFF */
4486 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4487 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4488 /* CodecName */ tiff_codecname,
4489 /* DllName */ NULL,
4490 /* FormatDescription */ tiff_format,
4491 /* FilenameExtension */ tiff_extension,
4492 /* MimeType */ tiff_mimetype,
4493 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4494 /* Version */ 1,
4495 /* SigCount */ 2,
4496 /* SigSize */ 4,
4497 /* SigPattern */ tiff_sig_pattern,
4498 /* SigMask */ tiff_sig_mask,
4500 encode_image_tiff,
4501 decode_image_tiff,
4502 select_frame_wic
4505 { /* EMF */
4506 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4507 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4508 /* CodecName */ emf_codecname,
4509 /* DllName */ NULL,
4510 /* FormatDescription */ emf_format,
4511 /* FilenameExtension */ emf_extension,
4512 /* MimeType */ emf_mimetype,
4513 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4514 /* Version */ 1,
4515 /* SigCount */ 1,
4516 /* SigSize */ 4,
4517 /* SigPattern */ emf_sig_pattern,
4518 /* SigMask */ emf_sig_mask,
4520 NULL,
4521 decode_image_olepicture_metafile,
4522 NULL
4525 { /* WMF */
4526 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4527 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4528 /* CodecName */ wmf_codecname,
4529 /* DllName */ NULL,
4530 /* FormatDescription */ wmf_format,
4531 /* FilenameExtension */ wmf_extension,
4532 /* MimeType */ wmf_mimetype,
4533 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4534 /* Version */ 1,
4535 /* SigCount */ 1,
4536 /* SigSize */ 2,
4537 /* SigPattern */ wmf_sig_pattern,
4538 /* SigMask */ wmf_sig_mask,
4540 NULL,
4541 decode_image_olepicture_metafile,
4542 NULL
4545 { /* PNG */
4546 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4547 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4548 /* CodecName */ png_codecname,
4549 /* DllName */ NULL,
4550 /* FormatDescription */ png_format,
4551 /* FilenameExtension */ png_extension,
4552 /* MimeType */ png_mimetype,
4553 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4554 /* Version */ 1,
4555 /* SigCount */ 1,
4556 /* SigSize */ 8,
4557 /* SigPattern */ png_sig_pattern,
4558 /* SigMask */ png_sig_mask,
4560 encode_image_png,
4561 decode_image_png,
4562 select_frame_wic
4565 { /* ICO */
4566 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4567 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4568 /* CodecName */ ico_codecname,
4569 /* DllName */ NULL,
4570 /* FormatDescription */ ico_format,
4571 /* FilenameExtension */ ico_extension,
4572 /* MimeType */ ico_mimetype,
4573 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4574 /* Version */ 1,
4575 /* SigCount */ 1,
4576 /* SigSize */ 4,
4577 /* SigPattern */ ico_sig_pattern,
4578 /* SigMask */ ico_sig_mask,
4580 NULL,
4581 decode_image_icon,
4582 select_frame_wic
4586 /*****************************************************************************
4587 * GdipGetImageDecodersSize [GDIPLUS.@]
4589 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
4591 int decoder_count=0;
4592 int i;
4593 TRACE("%p %p\n", numDecoders, size);
4595 if (!numDecoders || !size)
4596 return InvalidParameter;
4598 for (i=0; i<NUM_CODECS; i++)
4600 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4601 decoder_count++;
4604 *numDecoders = decoder_count;
4605 *size = decoder_count * sizeof(ImageCodecInfo);
4607 return Ok;
4610 /*****************************************************************************
4611 * GdipGetImageDecoders [GDIPLUS.@]
4613 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
4615 int i, decoder_count=0;
4616 TRACE("%u %u %p\n", numDecoders, size, decoders);
4618 if (!decoders ||
4619 size != numDecoders * sizeof(ImageCodecInfo))
4620 return GenericError;
4622 for (i=0; i<NUM_CODECS; i++)
4624 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4626 if (decoder_count == numDecoders) return GenericError;
4627 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4628 decoder_count++;
4632 if (decoder_count < numDecoders) return GenericError;
4634 return Ok;
4637 /*****************************************************************************
4638 * GdipGetImageEncodersSize [GDIPLUS.@]
4640 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4642 int encoder_count=0;
4643 int i;
4644 TRACE("%p %p\n", numEncoders, size);
4646 if (!numEncoders || !size)
4647 return InvalidParameter;
4649 for (i=0; i<NUM_CODECS; i++)
4651 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4652 encoder_count++;
4655 *numEncoders = encoder_count;
4656 *size = encoder_count * sizeof(ImageCodecInfo);
4658 return Ok;
4661 /*****************************************************************************
4662 * GdipGetImageEncoders [GDIPLUS.@]
4664 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4666 int i, encoder_count=0;
4667 TRACE("%u %u %p\n", numEncoders, size, encoders);
4669 if (!encoders ||
4670 size != numEncoders * sizeof(ImageCodecInfo))
4671 return GenericError;
4673 for (i=0; i<NUM_CODECS; i++)
4675 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4677 if (encoder_count == numEncoders) return GenericError;
4678 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4679 encoder_count++;
4683 if (encoder_count < numEncoders) return GenericError;
4685 return Ok;
4688 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
4689 GDIPCONST CLSID* clsidEncoder, UINT *size)
4691 static int calls;
4693 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
4695 if(!(calls++))
4696 FIXME("not implemented\n");
4698 *size = 0;
4700 return NotImplemented;
4703 static PixelFormat get_16bpp_format(HBITMAP hbm)
4705 BITMAPV4HEADER bmh;
4706 HDC hdc;
4707 PixelFormat result;
4709 hdc = CreateCompatibleDC(NULL);
4711 memset(&bmh, 0, sizeof(bmh));
4712 bmh.bV4Size = sizeof(bmh);
4713 bmh.bV4Width = 1;
4714 bmh.bV4Height = 1;
4715 bmh.bV4V4Compression = BI_BITFIELDS;
4716 bmh.bV4BitCount = 16;
4718 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4720 if (bmh.bV4RedMask == 0x7c00 &&
4721 bmh.bV4GreenMask == 0x3e0 &&
4722 bmh.bV4BlueMask == 0x1f)
4724 result = PixelFormat16bppRGB555;
4726 else if (bmh.bV4RedMask == 0xf800 &&
4727 bmh.bV4GreenMask == 0x7e0 &&
4728 bmh.bV4BlueMask == 0x1f)
4730 result = PixelFormat16bppRGB565;
4732 else
4734 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4735 bmh.bV4GreenMask, bmh.bV4BlueMask);
4736 result = PixelFormatUndefined;
4739 DeleteDC(hdc);
4741 return result;
4744 /*****************************************************************************
4745 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4747 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4749 BITMAP bm;
4750 GpStatus retval;
4751 PixelFormat format;
4752 BitmapData lockeddata;
4754 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4756 if(!hbm || !bitmap)
4757 return InvalidParameter;
4759 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4760 return InvalidParameter;
4762 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4763 switch(bm.bmBitsPixel) {
4764 case 1:
4765 format = PixelFormat1bppIndexed;
4766 break;
4767 case 4:
4768 format = PixelFormat4bppIndexed;
4769 break;
4770 case 8:
4771 format = PixelFormat8bppIndexed;
4772 break;
4773 case 16:
4774 format = get_16bpp_format(hbm);
4775 if (format == PixelFormatUndefined)
4776 return InvalidParameter;
4777 break;
4778 case 24:
4779 format = PixelFormat24bppRGB;
4780 break;
4781 case 32:
4782 format = PixelFormat32bppRGB;
4783 break;
4784 case 48:
4785 format = PixelFormat48bppRGB;
4786 break;
4787 default:
4788 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4789 return InvalidParameter;
4792 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4793 format, NULL, bitmap);
4795 if (retval == Ok)
4797 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4798 format, &lockeddata);
4799 if (retval == Ok)
4801 HDC hdc;
4802 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
4803 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
4804 INT src_height;
4806 hdc = CreateCompatibleDC(NULL);
4808 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4809 pbmi->bmiHeader.biBitCount = 0;
4811 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4813 src_height = abs(pbmi->bmiHeader.biHeight);
4814 pbmi->bmiHeader.biHeight = -src_height;
4816 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
4818 DeleteDC(hdc);
4820 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4823 if (retval == Ok && hpal)
4825 PALETTEENTRY entry[256];
4826 ColorPalette *palette=NULL;
4827 int i, num_palette_entries;
4829 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
4830 if (!num_palette_entries)
4831 retval = GenericError;
4833 palette = GdipAlloc(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4834 if (!palette)
4835 retval = OutOfMemory;
4837 if (retval == Ok)
4839 palette->Flags = 0;
4840 palette->Count = num_palette_entries;
4842 for (i=0; i<num_palette_entries; i++)
4844 palette->Entries[i] = 0xff000000 | entry[i].peRed << 16 |
4845 entry[i].peGreen << 8 | entry[i].peBlue;
4848 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
4851 GdipFree(palette);
4854 if (retval != Ok)
4856 GdipDisposeImage((GpImage*)*bitmap);
4857 *bitmap = NULL;
4861 return retval;
4864 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
4866 FIXME("(%p): stub\n", effect);
4867 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
4868 * in Windows's gdiplus */
4869 return NotImplemented;
4872 /*****************************************************************************
4873 * GdipSetEffectParameters [GDIPLUS.@]
4875 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
4876 const VOID *params, const UINT size)
4878 static int calls;
4880 TRACE("(%p,%p,%u)\n", effect, params, size);
4882 if(!(calls++))
4883 FIXME("not implemented\n");
4885 return NotImplemented;
4888 /*****************************************************************************
4889 * GdipGetImageFlags [GDIPLUS.@]
4891 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
4893 TRACE("%p %p\n", image, flags);
4895 if(!image || !flags)
4896 return InvalidParameter;
4898 *flags = image->flags;
4900 return Ok;
4903 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
4905 TRACE("(%d, %p)\n", control, param);
4907 switch(control){
4908 case TestControlForceBilinear:
4909 if(param)
4910 FIXME("TestControlForceBilinear not handled\n");
4911 break;
4912 case TestControlNoICM:
4913 if(param)
4914 FIXME("TestControlNoICM not handled\n");
4915 break;
4916 case TestControlGetBuildNumber:
4917 *((DWORD*)param) = 3102;
4918 break;
4921 return Ok;
4924 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
4926 TRACE("%p\n", image);
4928 return Ok;
4931 /*****************************************************************************
4932 * GdipGetImageThumbnail [GDIPLUS.@]
4934 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
4935 GpImage **ret_image, GetThumbnailImageAbort cb,
4936 VOID * cb_data)
4938 GpStatus stat;
4939 GpGraphics *graphics;
4940 UINT srcwidth, srcheight;
4942 TRACE("(%p %u %u %p %p %p)\n",
4943 image, width, height, ret_image, cb, cb_data);
4945 if (!image || !ret_image)
4946 return InvalidParameter;
4948 if (!width) width = 120;
4949 if (!height) height = 120;
4951 GdipGetImageWidth(image, &srcwidth);
4952 GdipGetImageHeight(image, &srcheight);
4954 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
4955 NULL, (GpBitmap**)ret_image);
4957 if (stat == Ok)
4959 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
4961 if (stat == Ok)
4963 stat = GdipDrawImageRectRectI(graphics, image,
4964 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
4965 NULL, NULL, NULL);
4967 GdipDeleteGraphics(graphics);
4970 if (stat != Ok)
4972 GdipDisposeImage(*ret_image);
4973 *ret_image = NULL;
4977 return stat;
4980 /*****************************************************************************
4981 * GdipImageRotateFlip [GDIPLUS.@]
4983 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
4985 GpBitmap *new_bitmap;
4986 GpBitmap *bitmap;
4987 int bpp, bytesperpixel;
4988 BOOL rotate_90, flip_x, flip_y;
4989 int src_x_offset, src_y_offset;
4990 LPBYTE src_origin;
4991 UINT x, y, width, height;
4992 BitmapData src_lock, dst_lock;
4993 GpStatus stat;
4995 TRACE("(%p, %u)\n", image, type);
4997 if (!image)
4998 return InvalidParameter;
5000 rotate_90 = type&1;
5001 flip_x = (type&6) == 2 || (type&6) == 4;
5002 flip_y = (type&3) == 1 || (type&3) == 2;
5004 if (image->type != ImageTypeBitmap)
5006 FIXME("Not implemented for type %i\n", image->type);
5007 return NotImplemented;
5010 bitmap = (GpBitmap*)image;
5011 bpp = PIXELFORMATBPP(bitmap->format);
5013 if (bpp < 8)
5015 FIXME("Not implemented for %i bit images\n", bpp);
5016 return NotImplemented;
5019 if (rotate_90)
5021 width = bitmap->height;
5022 height = bitmap->width;
5024 else
5026 width = bitmap->width;
5027 height = bitmap->height;
5030 bytesperpixel = bpp/8;
5032 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5034 if (stat != Ok)
5035 return stat;
5037 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
5039 if (stat == Ok)
5041 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5043 if (stat == Ok)
5045 LPBYTE src_row, src_pixel;
5046 LPBYTE dst_row, dst_pixel;
5048 src_origin = src_lock.Scan0;
5049 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5050 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
5052 if (rotate_90)
5054 if (flip_y) src_x_offset = -src_lock.Stride;
5055 else src_x_offset = src_lock.Stride;
5056 if (flip_x) src_y_offset = -bytesperpixel;
5057 else src_y_offset = bytesperpixel;
5059 else
5061 if (flip_x) src_x_offset = -bytesperpixel;
5062 else src_x_offset = bytesperpixel;
5063 if (flip_y) src_y_offset = -src_lock.Stride;
5064 else src_y_offset = src_lock.Stride;
5067 src_row = src_origin;
5068 dst_row = dst_lock.Scan0;
5069 for (y=0; y<height; y++)
5071 src_pixel = src_row;
5072 dst_pixel = dst_row;
5073 for (x=0; x<width; x++)
5075 /* FIXME: This could probably be faster without memcpy. */
5076 memcpy(dst_pixel, src_pixel, bytesperpixel);
5077 dst_pixel += bytesperpixel;
5078 src_pixel += src_x_offset;
5080 src_row += src_y_offset;
5081 dst_row += dst_lock.Stride;
5084 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5087 GdipBitmapUnlockBits(bitmap, &src_lock);
5090 if (stat == Ok)
5091 move_bitmap(bitmap, new_bitmap, FALSE);
5092 else
5093 GdipDisposeImage((GpImage*)new_bitmap);
5095 return stat;