2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012,2016 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus
);
43 HRESULT WINAPI
WICCreateImagingFactory_Proxy(UINT
, IWICImagingFactory
**);
45 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
46 #define WMF_PLACEABLE_KEY 0x9ac6cdd7
50 const WICPixelFormatGUID
*wic_format
;
51 PixelFormat gdip_format
;
52 /* predefined palette type to use for pixel format conversions */
53 WICBitmapPaletteType palette_type
;
56 { &GUID_WICPixelFormatBlackWhite
, PixelFormat1bppIndexed
, WICBitmapPaletteTypeFixedBW
},
57 { &GUID_WICPixelFormat1bppIndexed
, PixelFormat1bppIndexed
, WICBitmapPaletteTypeFixedBW
},
58 { &GUID_WICPixelFormat4bppIndexed
, PixelFormat4bppIndexed
, WICBitmapPaletteTypeFixedHalftone8
},
59 { &GUID_WICPixelFormat8bppGray
, PixelFormat8bppIndexed
, WICBitmapPaletteTypeFixedGray256
},
60 { &GUID_WICPixelFormat8bppIndexed
, PixelFormat8bppIndexed
, WICBitmapPaletteTypeFixedHalftone256
},
61 { &GUID_WICPixelFormat16bppBGR555
, PixelFormat16bppRGB555
, WICBitmapPaletteTypeFixedHalftone256
},
62 { &GUID_WICPixelFormat24bppBGR
, PixelFormat24bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
63 { &GUID_WICPixelFormat32bppBGR
, PixelFormat32bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
64 { &GUID_WICPixelFormat48bppRGB
, PixelFormat48bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
65 { &GUID_WICPixelFormat32bppBGRA
, PixelFormat32bppARGB
, WICBitmapPaletteTypeFixedHalftone256
},
66 { &GUID_WICPixelFormat32bppPBGRA
, PixelFormat32bppPARGB
, WICBitmapPaletteTypeFixedHalftone256
},
67 { &GUID_WICPixelFormat32bppCMYK
, PixelFormat32bppCMYK
, WICBitmapPaletteTypeFixedHalftone256
},
68 { &GUID_WICPixelFormat32bppGrayFloat
, PixelFormat32bppARGB
, WICBitmapPaletteTypeFixedGray256
},
69 { &GUID_WICPixelFormat64bppCMYK
, PixelFormat48bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
70 { &GUID_WICPixelFormat64bppRGBA
, PixelFormat48bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
74 static ColorPalette
*get_palette(IWICBitmapFrameDecode
*frame
, WICBitmapPaletteType palette_type
)
77 IWICImagingFactory
*factory
;
78 IWICPalette
*wic_palette
;
79 ColorPalette
*palette
= NULL
;
81 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
82 if (hr
!= S_OK
) return NULL
;
84 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
87 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
89 hr
= IWICBitmapFrameDecode_CopyPalette(frame
, wic_palette
);
92 TRACE("using predefined palette %#x\n", palette_type
);
93 hr
= IWICPalette_InitializePredefined(wic_palette
, palette_type
, FALSE
);
97 WICBitmapPaletteType type
;
101 IWICPalette_GetColorCount(wic_palette
, &count
);
102 palette
= heap_alloc(2 * sizeof(UINT
) + count
* sizeof(ARGB
));
103 IWICPalette_GetColors(wic_palette
, count
, palette
->Entries
, &palette
->Count
);
105 IWICPalette_GetType(wic_palette
, &type
);
107 case WICBitmapPaletteTypeFixedGray4
:
108 case WICBitmapPaletteTypeFixedGray16
:
109 case WICBitmapPaletteTypeFixedGray256
:
110 palette
->Flags
= PaletteFlagsGrayScale
;
112 case WICBitmapPaletteTypeFixedHalftone8
:
113 case WICBitmapPaletteTypeFixedHalftone27
:
114 case WICBitmapPaletteTypeFixedHalftone64
:
115 case WICBitmapPaletteTypeFixedHalftone125
:
116 case WICBitmapPaletteTypeFixedHalftone216
:
117 case WICBitmapPaletteTypeFixedHalftone252
:
118 case WICBitmapPaletteTypeFixedHalftone256
:
119 palette
->Flags
= PaletteFlagsHalftone
;
124 IWICPalette_HasAlpha(wic_palette
, &alpha
);
126 palette
->Flags
|= PaletteFlagsHasAlpha
;
128 IWICPalette_Release(wic_palette
);
130 IWICImagingFactory_Release(factory
);
134 GpStatus WINGDIPAPI
GdipBitmapApplyEffect(GpBitmap
* bitmap
, CGpEffect
* effect
,
135 RECT
* roi
, BOOL useAuxData
, VOID
** auxData
, INT
* auxDataSize
)
137 FIXME("(%p %p %p %d %p %p): stub\n", bitmap
, effect
, roi
, useAuxData
, auxData
, auxDataSize
);
139 * Note: According to Jose Roca's GDI+ docs, this function is not
140 * implemented in Windows's GDI+.
142 return NotImplemented
;
145 GpStatus WINGDIPAPI
GdipBitmapCreateApplyEffect(GpBitmap
** inputBitmaps
,
146 INT numInputs
, CGpEffect
* effect
, RECT
* roi
, RECT
* outputRect
,
147 GpBitmap
** outputBitmap
, BOOL useAuxData
, VOID
** auxData
, INT
* auxDataSize
)
149 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps
, numInputs
, effect
, roi
, outputRect
, outputBitmap
, useAuxData
, auxData
, auxDataSize
);
151 * Note: According to Jose Roca's GDI+ docs, this function is not
152 * implemented in Windows's GDI+.
154 return NotImplemented
;
157 static inline void getpixel_1bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
159 *index
= (row
[x
/8]>>(7-x
%8)) & 1;
162 static inline void getpixel_4bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
165 *index
= row
[x
/2]&0xf;
167 *index
= row
[x
/2]>>4;
170 static inline void getpixel_8bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
175 static inline void getpixel_16bppGrayScale(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
176 const BYTE
*row
, UINT x
)
178 *r
= *g
= *b
= row
[x
*2+1];
182 static inline void getpixel_16bppRGB555(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
183 const BYTE
*row
, UINT x
)
185 WORD pixel
= *((const WORD
*)(row
)+x
);
186 *r
= (pixel
>>7&0xf8)|(pixel
>>12&0x7);
187 *g
= (pixel
>>2&0xf8)|(pixel
>>6&0x7);
188 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
192 static inline void getpixel_16bppRGB565(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
193 const BYTE
*row
, UINT x
)
195 WORD pixel
= *((const WORD
*)(row
)+x
);
196 *r
= (pixel
>>8&0xf8)|(pixel
>>13&0x7);
197 *g
= (pixel
>>3&0xfc)|(pixel
>>9&0x3);
198 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
202 static inline void getpixel_16bppARGB1555(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
203 const BYTE
*row
, UINT x
)
205 WORD pixel
= *((const WORD
*)(row
)+x
);
206 *r
= (pixel
>>7&0xf8)|(pixel
>>12&0x7);
207 *g
= (pixel
>>2&0xf8)|(pixel
>>6&0x7);
208 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
209 if ((pixel
&0x8000) == 0x8000)
215 static inline void getpixel_24bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
216 const BYTE
*row
, UINT x
)
224 static inline void getpixel_32bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
225 const BYTE
*row
, UINT x
)
233 static inline void getpixel_32bppARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
234 const BYTE
*row
, UINT x
)
242 static inline void getpixel_32bppPARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
243 const BYTE
*row
, UINT x
)
250 *r
= row
[x
*4+2] * 255 / *a
;
251 *g
= row
[x
*4+1] * 255 / *a
;
252 *b
= row
[x
*4] * 255 / *a
;
256 static inline void getpixel_48bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
257 const BYTE
*row
, UINT x
)
265 static inline void getpixel_64bppARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
266 const BYTE
*row
, UINT x
)
274 static inline void getpixel_64bppPARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
275 const BYTE
*row
, UINT x
)
282 *r
= row
[x
*8+5] * 255 / *a
;
283 *g
= row
[x
*8+3] * 255 / *a
;
284 *b
= row
[x
*8+1] * 255 / *a
;
288 GpStatus WINGDIPAPI
GdipBitmapGetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
295 if(!bitmap
|| !color
||
296 x
< 0 || y
< 0 || x
>= bitmap
->width
|| y
>= bitmap
->height
)
297 return InvalidParameter
;
299 row
= bitmap
->bits
+bitmap
->stride
*y
;
301 switch (bitmap
->format
)
303 case PixelFormat1bppIndexed
:
304 getpixel_1bppIndexed(&index
,row
,x
);
306 case PixelFormat4bppIndexed
:
307 getpixel_4bppIndexed(&index
,row
,x
);
309 case PixelFormat8bppIndexed
:
310 getpixel_8bppIndexed(&index
,row
,x
);
312 case PixelFormat16bppGrayScale
:
313 getpixel_16bppGrayScale(&r
,&g
,&b
,&a
,row
,x
);
315 case PixelFormat16bppRGB555
:
316 getpixel_16bppRGB555(&r
,&g
,&b
,&a
,row
,x
);
318 case PixelFormat16bppRGB565
:
319 getpixel_16bppRGB565(&r
,&g
,&b
,&a
,row
,x
);
321 case PixelFormat16bppARGB1555
:
322 getpixel_16bppARGB1555(&r
,&g
,&b
,&a
,row
,x
);
324 case PixelFormat24bppRGB
:
325 getpixel_24bppRGB(&r
,&g
,&b
,&a
,row
,x
);
327 case PixelFormat32bppRGB
:
328 getpixel_32bppRGB(&r
,&g
,&b
,&a
,row
,x
);
330 case PixelFormat32bppARGB
:
331 getpixel_32bppARGB(&r
,&g
,&b
,&a
,row
,x
);
333 case PixelFormat32bppPARGB
:
334 getpixel_32bppPARGB(&r
,&g
,&b
,&a
,row
,x
);
336 case PixelFormat48bppRGB
:
337 getpixel_48bppRGB(&r
,&g
,&b
,&a
,row
,x
);
339 case PixelFormat64bppARGB
:
340 getpixel_64bppARGB(&r
,&g
,&b
,&a
,row
,x
);
342 case PixelFormat64bppPARGB
:
343 getpixel_64bppPARGB(&r
,&g
,&b
,&a
,row
,x
);
346 FIXME("not implemented for format 0x%x\n", bitmap
->format
);
347 return NotImplemented
;
350 if (bitmap
->format
& PixelFormatIndexed
)
351 *color
= bitmap
->image
.palette
->Entries
[index
];
353 *color
= a
<<24|r
<<16|g
<<8|b
;
358 static inline UINT
get_palette_index(BYTE r
, BYTE g
, BYTE b
, BYTE a
, ColorPalette
*palette
)
361 int best_distance
= 0x7fff;
365 if (!palette
) return 0;
366 /* This algorithm scans entire palette,
367 computes difference from desired color (all color components have equal weight)
368 and returns the index of color with least difference.
370 Note: Maybe it could be replaced with a better algorithm for better image quality
371 and performance, though better algorithm would probably need some pre-built lookup
372 tables and thus may actually be slower if this method is called only few times per
375 for(i
=0;i
<palette
->Count
;i
++) {
376 ARGB color
=palette
->Entries
[i
];
377 distance
=abs(b
-(color
& 0xff)) + abs(g
-(color
>>8 & 0xff)) + abs(r
-(color
>>16 & 0xff)) + abs(a
-(color
>>24 & 0xff));
378 if (distance
<best_distance
) {
379 best_distance
=distance
;
386 static inline void setpixel_8bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
387 BYTE
*row
, UINT x
, ColorPalette
*palette
)
389 BYTE index
= get_palette_index(r
,g
,b
,a
,palette
);
393 static inline void setpixel_1bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
394 BYTE
*row
, UINT x
, ColorPalette
*palette
)
396 row
[x
/8] = (row
[x
/8] & ~(1<<(7-x
%8))) | (get_palette_index(r
,g
,b
,a
,palette
)<<(7-x
%8));
399 static inline void setpixel_4bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
400 BYTE
*row
, UINT x
, ColorPalette
*palette
)
403 row
[x
/2] = (row
[x
/2] & 0xf0) | get_palette_index(r
,g
,b
,a
,palette
);
405 row
[x
/2] = (row
[x
/2] & 0x0f) | get_palette_index(r
,g
,b
,a
,palette
)<<4;
408 static inline void setpixel_16bppGrayScale(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
411 *((WORD
*)(row
)+x
) = (r
+g
+b
)*85;
414 static inline void setpixel_16bppRGB555(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
417 *((WORD
*)(row
)+x
) = (r
<<7&0x7c00)|
422 static inline void setpixel_16bppRGB565(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
425 *((WORD
*)(row
)+x
) = (r
<<8&0xf800)|
430 static inline void setpixel_16bppARGB1555(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
433 *((WORD
*)(row
)+x
) = (a
<<8&0x8000)|
439 static inline void setpixel_24bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
447 static inline void setpixel_32bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
450 *((DWORD
*)(row
)+x
) = (r
<<16)|(g
<<8)|b
;
453 static inline void setpixel_32bppARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
456 *((DWORD
*)(row
)+x
) = (a
<<24)|(r
<<16)|(g
<<8)|b
;
459 static inline void setpixel_32bppPARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
465 *((DWORD
*)(row
)+x
) = (a
<<24)|(r
<<16)|(g
<<8)|b
;
468 static inline void setpixel_48bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
471 row
[x
*6+5] = row
[x
*6+4] = r
;
472 row
[x
*6+3] = row
[x
*6+2] = g
;
473 row
[x
*6+1] = row
[x
*6] = b
;
476 static inline void setpixel_64bppARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
479 UINT64 a64
=a
, r64
=r
, g64
=g
, b64
=b
;
480 *((UINT64
*)(row
)+x
) = (a64
<<56)|(a64
<<48)|(r64
<<40)|(r64
<<32)|(g64
<<24)|(g64
<<16)|(b64
<<8)|b64
;
483 static inline void setpixel_64bppPARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
486 UINT64 a64
, r64
, g64
, b64
;
491 *((UINT64
*)(row
)+x
) = (a64
<<48)|(r64
<<32)|(g64
<<16)|b64
;
494 GpStatus WINGDIPAPI
GdipBitmapSetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
500 if(!bitmap
|| x
< 0 || y
< 0 || x
>= bitmap
->width
|| y
>= bitmap
->height
)
501 return InvalidParameter
;
508 row
= bitmap
->bits
+ bitmap
->stride
* y
;
510 switch (bitmap
->format
)
512 case PixelFormat16bppGrayScale
:
513 setpixel_16bppGrayScale(r
,g
,b
,a
,row
,x
);
515 case PixelFormat16bppRGB555
:
516 setpixel_16bppRGB555(r
,g
,b
,a
,row
,x
);
518 case PixelFormat16bppRGB565
:
519 setpixel_16bppRGB565(r
,g
,b
,a
,row
,x
);
521 case PixelFormat16bppARGB1555
:
522 setpixel_16bppARGB1555(r
,g
,b
,a
,row
,x
);
524 case PixelFormat24bppRGB
:
525 setpixel_24bppRGB(r
,g
,b
,a
,row
,x
);
527 case PixelFormat32bppRGB
:
528 setpixel_32bppRGB(r
,g
,b
,a
,row
,x
);
530 case PixelFormat32bppARGB
:
531 setpixel_32bppARGB(r
,g
,b
,a
,row
,x
);
533 case PixelFormat32bppPARGB
:
534 setpixel_32bppPARGB(r
,g
,b
,a
,row
,x
);
536 case PixelFormat48bppRGB
:
537 setpixel_48bppRGB(r
,g
,b
,a
,row
,x
);
539 case PixelFormat64bppARGB
:
540 setpixel_64bppARGB(r
,g
,b
,a
,row
,x
);
542 case PixelFormat64bppPARGB
:
543 setpixel_64bppPARGB(r
,g
,b
,a
,row
,x
);
545 case PixelFormat8bppIndexed
:
546 setpixel_8bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
548 case PixelFormat4bppIndexed
:
549 setpixel_4bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
551 case PixelFormat1bppIndexed
:
552 setpixel_1bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
555 FIXME("not implemented for format 0x%x\n", bitmap
->format
);
556 return NotImplemented
;
562 GpStatus
convert_pixels(INT width
, INT height
,
563 INT dst_stride
, BYTE
*dst_bits
, PixelFormat dst_format
,
564 INT src_stride
, const BYTE
*src_bits
, PixelFormat src_format
,
565 ColorPalette
*palette
)
569 if (src_format
== dst_format
||
570 (dst_format
== PixelFormat32bppRGB
&& PIXELFORMATBPP(src_format
) == 32))
572 UINT widthbytes
= PIXELFORMATBPP(src_format
) * width
/ 8;
573 for (y
=0; y
<height
; y
++)
574 memcpy(dst_bits
+dst_stride
*y
, src_bits
+src_stride
*y
, widthbytes
);
578 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
579 for (y=0; y<height; y++) \
580 for (x=0; x<width; x++) { \
583 BYTE *color = (BYTE *)&argb; \
584 getpixel_function(&index, src_bits+src_stride*y, x); \
585 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
586 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
591 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
592 for (y=0; y<height; y++) \
593 for (x=0; x<width; x++) { \
595 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
596 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
601 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
602 for (y=0; y<height; y++) \
603 for (x=0; x<width; x++) { \
605 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
606 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
613 case PixelFormat1bppIndexed
:
616 case PixelFormat16bppGrayScale
:
617 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppGrayScale
);
618 case PixelFormat16bppRGB555
:
619 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppRGB555
);
620 case PixelFormat16bppRGB565
:
621 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppRGB565
);
622 case PixelFormat16bppARGB1555
:
623 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppARGB1555
);
624 case PixelFormat24bppRGB
:
625 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_24bppRGB
);
626 case PixelFormat32bppRGB
:
627 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppRGB
);
628 case PixelFormat32bppARGB
:
629 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppARGB
);
630 case PixelFormat32bppPARGB
:
631 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppPARGB
);
632 case PixelFormat48bppRGB
:
633 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_48bppRGB
);
634 case PixelFormat64bppARGB
:
635 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_64bppARGB
);
640 case PixelFormat4bppIndexed
:
643 case PixelFormat16bppGrayScale
:
644 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppGrayScale
);
645 case PixelFormat16bppRGB555
:
646 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppRGB555
);
647 case PixelFormat16bppRGB565
:
648 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppRGB565
);
649 case PixelFormat16bppARGB1555
:
650 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppARGB1555
);
651 case PixelFormat24bppRGB
:
652 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_24bppRGB
);
653 case PixelFormat32bppRGB
:
654 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppRGB
);
655 case PixelFormat32bppARGB
:
656 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppARGB
);
657 case PixelFormat32bppPARGB
:
658 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppPARGB
);
659 case PixelFormat48bppRGB
:
660 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_48bppRGB
);
661 case PixelFormat64bppARGB
:
662 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_64bppARGB
);
667 case PixelFormat8bppIndexed
:
670 case PixelFormat16bppGrayScale
:
671 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppGrayScale
);
672 case PixelFormat16bppRGB555
:
673 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppRGB555
);
674 case PixelFormat16bppRGB565
:
675 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppRGB565
);
676 case PixelFormat16bppARGB1555
:
677 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppARGB1555
);
678 case PixelFormat24bppRGB
:
679 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_24bppRGB
);
680 case PixelFormat32bppRGB
:
681 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppRGB
);
682 case PixelFormat32bppARGB
:
683 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppARGB
);
684 case PixelFormat32bppPARGB
:
685 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppPARGB
);
686 case PixelFormat48bppRGB
:
687 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_48bppRGB
);
688 case PixelFormat64bppARGB
:
689 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_64bppARGB
);
694 case PixelFormat16bppGrayScale
:
697 case PixelFormat1bppIndexed
:
698 convert_rgb_to_indexed(getpixel_16bppGrayScale
, setpixel_1bppIndexed
);
699 case PixelFormat8bppIndexed
:
700 convert_rgb_to_indexed(getpixel_16bppGrayScale
, setpixel_8bppIndexed
);
701 case PixelFormat16bppRGB555
:
702 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppRGB555
);
703 case PixelFormat16bppRGB565
:
704 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppRGB565
);
705 case PixelFormat16bppARGB1555
:
706 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppARGB1555
);
707 case PixelFormat24bppRGB
:
708 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_24bppRGB
);
709 case PixelFormat32bppRGB
:
710 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppRGB
);
711 case PixelFormat32bppARGB
:
712 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppARGB
);
713 case PixelFormat32bppPARGB
:
714 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppPARGB
);
715 case PixelFormat48bppRGB
:
716 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_48bppRGB
);
717 case PixelFormat64bppARGB
:
718 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_64bppARGB
);
723 case PixelFormat16bppRGB555
:
726 case PixelFormat1bppIndexed
:
727 convert_rgb_to_indexed(getpixel_16bppRGB555
, setpixel_1bppIndexed
);
728 case PixelFormat8bppIndexed
:
729 convert_rgb_to_indexed(getpixel_16bppRGB555
, setpixel_8bppIndexed
);
730 case PixelFormat16bppGrayScale
:
731 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppGrayScale
);
732 case PixelFormat16bppRGB565
:
733 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppRGB565
);
734 case PixelFormat16bppARGB1555
:
735 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppARGB1555
);
736 case PixelFormat24bppRGB
:
737 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_24bppRGB
);
738 case PixelFormat32bppRGB
:
739 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppRGB
);
740 case PixelFormat32bppARGB
:
741 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppARGB
);
742 case PixelFormat32bppPARGB
:
743 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppPARGB
);
744 case PixelFormat48bppRGB
:
745 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_48bppRGB
);
746 case PixelFormat64bppARGB
:
747 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_64bppARGB
);
752 case PixelFormat16bppRGB565
:
755 case PixelFormat1bppIndexed
:
756 convert_rgb_to_indexed(getpixel_16bppRGB565
, setpixel_1bppIndexed
);
757 case PixelFormat8bppIndexed
:
758 convert_rgb_to_indexed(getpixel_16bppRGB565
, setpixel_8bppIndexed
);
759 case PixelFormat16bppGrayScale
:
760 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppGrayScale
);
761 case PixelFormat16bppRGB555
:
762 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppRGB555
);
763 case PixelFormat16bppARGB1555
:
764 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppARGB1555
);
765 case PixelFormat24bppRGB
:
766 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_24bppRGB
);
767 case PixelFormat32bppRGB
:
768 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppRGB
);
769 case PixelFormat32bppARGB
:
770 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppARGB
);
771 case PixelFormat32bppPARGB
:
772 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppPARGB
);
773 case PixelFormat48bppRGB
:
774 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_48bppRGB
);
775 case PixelFormat64bppARGB
:
776 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_64bppARGB
);
781 case PixelFormat16bppARGB1555
:
784 case PixelFormat1bppIndexed
:
785 convert_rgb_to_indexed(getpixel_16bppARGB1555
, setpixel_1bppIndexed
);
786 case PixelFormat8bppIndexed
:
787 convert_rgb_to_indexed(getpixel_16bppARGB1555
, setpixel_8bppIndexed
);
788 case PixelFormat16bppGrayScale
:
789 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppGrayScale
);
790 case PixelFormat16bppRGB555
:
791 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppRGB555
);
792 case PixelFormat16bppRGB565
:
793 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppRGB565
);
794 case PixelFormat24bppRGB
:
795 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_24bppRGB
);
796 case PixelFormat32bppRGB
:
797 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppRGB
);
798 case PixelFormat32bppARGB
:
799 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppARGB
);
800 case PixelFormat32bppPARGB
:
801 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppPARGB
);
802 case PixelFormat48bppRGB
:
803 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_48bppRGB
);
804 case PixelFormat64bppARGB
:
805 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_64bppARGB
);
810 case PixelFormat24bppRGB
:
813 case PixelFormat1bppIndexed
:
814 convert_rgb_to_indexed(getpixel_24bppRGB
, setpixel_1bppIndexed
);
815 case PixelFormat8bppIndexed
:
816 convert_rgb_to_indexed(getpixel_24bppRGB
, setpixel_8bppIndexed
);
817 case PixelFormat16bppGrayScale
:
818 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppGrayScale
);
819 case PixelFormat16bppRGB555
:
820 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppRGB555
);
821 case PixelFormat16bppRGB565
:
822 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppRGB565
);
823 case PixelFormat16bppARGB1555
:
824 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppARGB1555
);
825 case PixelFormat32bppRGB
:
826 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppRGB
);
827 case PixelFormat32bppARGB
:
828 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppARGB
);
829 case PixelFormat32bppPARGB
:
830 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppPARGB
);
831 case PixelFormat48bppRGB
:
832 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_48bppRGB
);
833 case PixelFormat64bppARGB
:
834 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_64bppARGB
);
839 case PixelFormat32bppRGB
:
842 case PixelFormat1bppIndexed
:
843 convert_rgb_to_indexed(getpixel_32bppRGB
, setpixel_1bppIndexed
);
844 case PixelFormat8bppIndexed
:
845 convert_rgb_to_indexed(getpixel_32bppRGB
, setpixel_8bppIndexed
);
846 case PixelFormat16bppGrayScale
:
847 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppGrayScale
);
848 case PixelFormat16bppRGB555
:
849 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppRGB555
);
850 case PixelFormat16bppRGB565
:
851 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppRGB565
);
852 case PixelFormat16bppARGB1555
:
853 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppARGB1555
);
854 case PixelFormat24bppRGB
:
855 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_24bppRGB
);
856 case PixelFormat32bppARGB
:
857 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_32bppARGB
);
858 case PixelFormat32bppPARGB
:
859 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_32bppPARGB
);
860 case PixelFormat48bppRGB
:
861 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_48bppRGB
);
862 case PixelFormat64bppARGB
:
863 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_64bppARGB
);
868 case PixelFormat32bppARGB
:
871 case PixelFormat1bppIndexed
:
872 convert_rgb_to_indexed(getpixel_32bppARGB
, setpixel_1bppIndexed
);
873 case PixelFormat8bppIndexed
:
874 convert_rgb_to_indexed(getpixel_32bppARGB
, setpixel_8bppIndexed
);
875 case PixelFormat16bppGrayScale
:
876 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppGrayScale
);
877 case PixelFormat16bppRGB555
:
878 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppRGB555
);
879 case PixelFormat16bppRGB565
:
880 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppRGB565
);
881 case PixelFormat16bppARGB1555
:
882 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppARGB1555
);
883 case PixelFormat24bppRGB
:
884 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_24bppRGB
);
885 case PixelFormat32bppPARGB
:
886 convert_32bppARGB_to_32bppPARGB(width
, height
, dst_bits
, dst_stride
, src_bits
, src_stride
);
888 case PixelFormat48bppRGB
:
889 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_48bppRGB
);
890 case PixelFormat64bppARGB
:
891 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_64bppARGB
);
896 case PixelFormat32bppPARGB
:
899 case PixelFormat1bppIndexed
:
900 convert_rgb_to_indexed(getpixel_32bppPARGB
, setpixel_1bppIndexed
);
901 case PixelFormat8bppIndexed
:
902 convert_rgb_to_indexed(getpixel_32bppPARGB
, setpixel_8bppIndexed
);
903 case PixelFormat16bppGrayScale
:
904 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppGrayScale
);
905 case PixelFormat16bppRGB555
:
906 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppRGB555
);
907 case PixelFormat16bppRGB565
:
908 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppRGB565
);
909 case PixelFormat16bppARGB1555
:
910 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppARGB1555
);
911 case PixelFormat24bppRGB
:
912 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_24bppRGB
);
913 case PixelFormat32bppRGB
:
914 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_32bppRGB
);
915 case PixelFormat32bppARGB
:
916 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_32bppARGB
);
917 case PixelFormat48bppRGB
:
918 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_48bppRGB
);
919 case PixelFormat64bppARGB
:
920 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_64bppARGB
);
925 case PixelFormat48bppRGB
:
928 case PixelFormat1bppIndexed
:
929 convert_rgb_to_indexed(getpixel_48bppRGB
, setpixel_1bppIndexed
);
930 case PixelFormat8bppIndexed
:
931 convert_rgb_to_indexed(getpixel_48bppRGB
, setpixel_8bppIndexed
);
932 case PixelFormat16bppGrayScale
:
933 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppGrayScale
);
934 case PixelFormat16bppRGB555
:
935 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppRGB555
);
936 case PixelFormat16bppRGB565
:
937 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppRGB565
);
938 case PixelFormat16bppARGB1555
:
939 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppARGB1555
);
940 case PixelFormat24bppRGB
:
941 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_24bppRGB
);
942 case PixelFormat32bppRGB
:
943 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppRGB
);
944 case PixelFormat32bppARGB
:
945 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppARGB
);
946 case PixelFormat32bppPARGB
:
947 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppPARGB
);
948 case PixelFormat64bppARGB
:
949 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_64bppARGB
);
954 case PixelFormat64bppARGB
:
957 case PixelFormat1bppIndexed
:
958 convert_rgb_to_indexed(getpixel_64bppARGB
, setpixel_1bppIndexed
);
959 case PixelFormat8bppIndexed
:
960 convert_rgb_to_indexed(getpixel_64bppARGB
, setpixel_8bppIndexed
);
961 case PixelFormat16bppGrayScale
:
962 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppGrayScale
);
963 case PixelFormat16bppRGB555
:
964 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppRGB555
);
965 case PixelFormat16bppRGB565
:
966 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppRGB565
);
967 case PixelFormat16bppARGB1555
:
968 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppARGB1555
);
969 case PixelFormat24bppRGB
:
970 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_24bppRGB
);
971 case PixelFormat32bppRGB
:
972 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppRGB
);
973 case PixelFormat32bppARGB
:
974 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppARGB
);
975 case PixelFormat32bppPARGB
:
976 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppPARGB
);
977 case PixelFormat48bppRGB
:
978 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_48bppRGB
);
983 case PixelFormat64bppPARGB
:
986 case PixelFormat1bppIndexed
:
987 convert_rgb_to_indexed(getpixel_64bppPARGB
, setpixel_1bppIndexed
);
988 case PixelFormat8bppIndexed
:
989 convert_rgb_to_indexed(getpixel_64bppPARGB
, setpixel_8bppIndexed
);
990 case PixelFormat16bppGrayScale
:
991 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppGrayScale
);
992 case PixelFormat16bppRGB555
:
993 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppRGB555
);
994 case PixelFormat16bppRGB565
:
995 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppRGB565
);
996 case PixelFormat16bppARGB1555
:
997 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppARGB1555
);
998 case PixelFormat24bppRGB
:
999 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_24bppRGB
);
1000 case PixelFormat32bppRGB
:
1001 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppRGB
);
1002 case PixelFormat32bppARGB
:
1003 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppARGB
);
1004 case PixelFormat32bppPARGB
:
1005 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppPARGB
);
1006 case PixelFormat48bppRGB
:
1007 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_48bppRGB
);
1008 case PixelFormat64bppARGB
:
1009 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_64bppARGB
);
1018 #undef convert_indexed_to_rgb
1019 #undef convert_rgb_to_rgb
1021 return NotImplemented
;
1024 /* This function returns a pointer to an array of pixels that represents the
1025 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1026 * flags. It is correct behavior that a user who calls this function with write
1027 * privileges can write to the whole bitmap (not just the area in rect).
1029 * FIXME: only used portion of format is bits per pixel. */
1030 GpStatus WINGDIPAPI
GdipBitmapLockBits(GpBitmap
* bitmap
, GDIPCONST GpRect
* rect
,
1031 UINT flags
, PixelFormat format
, BitmapData
* lockeddata
)
1033 INT bitspp
= PIXELFORMATBPP(format
);
1034 GpRect act_rect
; /* actual rect to be used */
1038 TRACE("%p %p %d 0x%x %p\n", bitmap
, rect
, flags
, format
, lockeddata
);
1040 if(!lockeddata
|| !bitmap
)
1041 return InvalidParameter
;
1042 if(!image_lock(&bitmap
->image
, &unlock
))
1046 if(rect
->X
< 0 || rect
->Y
< 0 || (rect
->X
+ rect
->Width
> bitmap
->width
) ||
1047 (rect
->Y
+ rect
->Height
> bitmap
->height
) || !flags
)
1049 image_unlock(&bitmap
->image
, unlock
);
1050 return InvalidParameter
;
1056 act_rect
.X
= act_rect
.Y
= 0;
1057 act_rect
.Width
= bitmap
->width
;
1058 act_rect
.Height
= bitmap
->height
;
1061 if(bitmap
->lockmode
)
1063 WARN("bitmap is already locked and cannot be locked again\n");
1064 image_unlock(&bitmap
->image
, unlock
);
1068 if (bitmap
->bits
&& bitmap
->format
== format
&& !(flags
& ImageLockModeUserInputBuf
))
1070 /* no conversion is necessary; just use the bits directly */
1071 lockeddata
->Width
= act_rect
.Width
;
1072 lockeddata
->Height
= act_rect
.Height
;
1073 lockeddata
->PixelFormat
= format
;
1074 lockeddata
->Reserved
= flags
;
1075 lockeddata
->Stride
= bitmap
->stride
;
1076 lockeddata
->Scan0
= bitmap
->bits
+ (bitspp
/ 8) * act_rect
.X
+
1077 bitmap
->stride
* act_rect
.Y
;
1079 bitmap
->lockmode
= flags
| ImageLockModeRead
;
1081 image_unlock(&bitmap
->image
, unlock
);
1085 /* Make sure we can convert to the requested format. */
1086 if (flags
& ImageLockModeRead
)
1088 stat
= convert_pixels(0, 0, 0, NULL
, format
, 0, NULL
, bitmap
->format
, NULL
);
1089 if (stat
== NotImplemented
)
1091 FIXME("cannot read bitmap from %x to %x\n", bitmap
->format
, format
);
1092 image_unlock(&bitmap
->image
, unlock
);
1093 return NotImplemented
;
1097 /* If we're opening for writing, make sure we'll be able to write back in
1098 * the original format. */
1099 if (flags
& ImageLockModeWrite
)
1101 stat
= convert_pixels(0, 0, 0, NULL
, bitmap
->format
, 0, NULL
, format
, NULL
);
1102 if (stat
== NotImplemented
)
1104 FIXME("cannot write bitmap from %x to %x\n", format
, bitmap
->format
);
1105 image_unlock(&bitmap
->image
, unlock
);
1106 return NotImplemented
;
1110 lockeddata
->Width
= act_rect
.Width
;
1111 lockeddata
->Height
= act_rect
.Height
;
1112 lockeddata
->PixelFormat
= format
;
1113 lockeddata
->Reserved
= flags
;
1115 if(!(flags
& ImageLockModeUserInputBuf
))
1117 lockeddata
->Stride
= (((act_rect
.Width
* bitspp
+ 7) / 8) + 3) & ~3;
1119 bitmap
->bitmapbits
= heap_alloc_zero(lockeddata
->Stride
* act_rect
.Height
);
1121 if (!bitmap
->bitmapbits
)
1123 image_unlock(&bitmap
->image
, unlock
);
1127 lockeddata
->Scan0
= bitmap
->bitmapbits
;
1130 if (flags
& ImageLockModeRead
)
1132 static BOOL fixme
= FALSE
;
1134 if (!fixme
&& (PIXELFORMATBPP(bitmap
->format
) * act_rect
.X
) % 8 != 0)
1136 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1140 stat
= convert_pixels(act_rect
.Width
, act_rect
.Height
,
1141 lockeddata
->Stride
, lockeddata
->Scan0
, format
,
1143 bitmap
->bits
+ bitmap
->stride
* act_rect
.Y
+ PIXELFORMATBPP(bitmap
->format
) * act_rect
.X
/ 8,
1144 bitmap
->format
, bitmap
->image
.palette
);
1148 heap_free(bitmap
->bitmapbits
);
1149 bitmap
->bitmapbits
= NULL
;
1150 image_unlock(&bitmap
->image
, unlock
);
1155 bitmap
->lockmode
= flags
| ImageLockModeRead
;
1156 bitmap
->lockx
= act_rect
.X
;
1157 bitmap
->locky
= act_rect
.Y
;
1159 image_unlock(&bitmap
->image
, unlock
);
1163 GpStatus WINGDIPAPI
GdipBitmapSetResolution(GpBitmap
* bitmap
, REAL xdpi
, REAL ydpi
)
1165 TRACE("(%p, %.2f, %.2f)\n", bitmap
, xdpi
, ydpi
);
1167 if (!bitmap
|| xdpi
== 0.0 || ydpi
== 0.0)
1168 return InvalidParameter
;
1170 bitmap
->image
.xres
= xdpi
;
1171 bitmap
->image
.yres
= ydpi
;
1176 GpStatus WINGDIPAPI
GdipBitmapUnlockBits(GpBitmap
* bitmap
,
1177 BitmapData
* lockeddata
)
1180 static BOOL fixme
= FALSE
;
1183 TRACE("(%p,%p)\n", bitmap
, lockeddata
);
1185 if(!bitmap
|| !lockeddata
)
1186 return InvalidParameter
;
1187 if(!image_lock(&bitmap
->image
, &unlock
))
1190 if(!bitmap
->lockmode
)
1192 image_unlock(&bitmap
->image
, unlock
);
1196 if(!(lockeddata
->Reserved
& ImageLockModeWrite
)){
1197 bitmap
->lockmode
= 0;
1198 heap_free(bitmap
->bitmapbits
);
1199 bitmap
->bitmapbits
= NULL
;
1200 image_unlock(&bitmap
->image
, unlock
);
1204 if (!bitmap
->bitmapbits
&& !(lockeddata
->Reserved
& ImageLockModeUserInputBuf
))
1206 /* we passed a direct reference; no need to do anything */
1207 bitmap
->lockmode
= 0;
1208 image_unlock(&bitmap
->image
, unlock
);
1212 if (!fixme
&& (PIXELFORMATBPP(bitmap
->format
) * bitmap
->lockx
) % 8 != 0)
1214 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1218 stat
= convert_pixels(lockeddata
->Width
, lockeddata
->Height
,
1220 bitmap
->bits
+ bitmap
->stride
* bitmap
->locky
+ PIXELFORMATBPP(bitmap
->format
) * bitmap
->lockx
/ 8,
1222 lockeddata
->Stride
, lockeddata
->Scan0
, lockeddata
->PixelFormat
, NULL
);
1226 ERR("failed to convert pixels; this should never happen\n");
1229 heap_free(bitmap
->bitmapbits
);
1230 bitmap
->bitmapbits
= NULL
;
1231 bitmap
->lockmode
= 0;
1233 image_unlock(&bitmap
->image
, unlock
);
1237 GpStatus WINGDIPAPI
GdipCloneBitmapArea(REAL x
, REAL y
, REAL width
, REAL height
,
1238 PixelFormat format
, GpBitmap
* srcBitmap
, GpBitmap
** dstBitmap
)
1243 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1245 if (!srcBitmap
|| !dstBitmap
|| srcBitmap
->image
.type
!= ImageTypeBitmap
||
1247 x
+ width
> srcBitmap
->width
|| y
+ height
> srcBitmap
->height
)
1249 TRACE("<-- InvalidParameter\n");
1250 return InvalidParameter
;
1253 if (format
== PixelFormatDontCare
)
1254 format
= srcBitmap
->format
;
1256 area
.X
= gdip_round(x
);
1257 area
.Y
= gdip_round(y
);
1258 area
.Width
= gdip_round(width
);
1259 area
.Height
= gdip_round(height
);
1261 stat
= GdipCreateBitmapFromScan0(area
.Width
, area
.Height
, 0, format
, NULL
, dstBitmap
);
1264 stat
= convert_pixels(area
.Width
, area
.Height
, (*dstBitmap
)->stride
, (*dstBitmap
)->bits
, (*dstBitmap
)->format
,
1266 srcBitmap
->bits
+ srcBitmap
->stride
* area
.Y
+ PIXELFORMATBPP(srcBitmap
->format
) * area
.X
/ 8,
1267 srcBitmap
->format
, srcBitmap
->image
.palette
);
1269 if (stat
== Ok
&& srcBitmap
->image
.palette
)
1271 ColorPalette
*src_palette
, *dst_palette
;
1273 src_palette
= srcBitmap
->image
.palette
;
1275 dst_palette
= heap_alloc_zero(sizeof(UINT
) * 2 + sizeof(ARGB
) * src_palette
->Count
);
1279 dst_palette
->Flags
= src_palette
->Flags
;
1280 dst_palette
->Count
= src_palette
->Count
;
1281 memcpy(dst_palette
->Entries
, src_palette
->Entries
, sizeof(ARGB
) * src_palette
->Count
);
1283 heap_free((*dstBitmap
)->image
.palette
);
1284 (*dstBitmap
)->image
.palette
= dst_palette
;
1291 GdipDisposeImage(&(*dstBitmap
)->image
);
1300 GpStatus WINGDIPAPI
GdipCloneBitmapAreaI(INT x
, INT y
, INT width
, INT height
,
1301 PixelFormat format
, GpBitmap
* srcBitmap
, GpBitmap
** dstBitmap
)
1303 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1305 return GdipCloneBitmapArea(x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1308 GpStatus WINGDIPAPI
GdipCloneImage(GpImage
*image
, GpImage
**cloneImage
)
1310 TRACE("%p, %p\n", image
, cloneImage
);
1312 if (!image
|| !cloneImage
)
1313 return InvalidParameter
;
1315 if (image
->type
== ImageTypeBitmap
)
1317 GpBitmap
*bitmap
= (GpBitmap
*)image
;
1319 return GdipCloneBitmapAreaI(0, 0, bitmap
->width
, bitmap
->height
,
1320 bitmap
->format
, bitmap
, (GpBitmap
**)cloneImage
);
1322 else if (image
->type
== ImageTypeMetafile
&& ((GpMetafile
*)image
)->hemf
)
1324 GpMetafile
*result
, *metafile
;
1326 metafile
= (GpMetafile
*)image
;
1328 result
= heap_alloc_zero(sizeof(*result
));
1332 result
->image
.type
= ImageTypeMetafile
;
1333 result
->image
.format
= image
->format
;
1334 result
->image
.flags
= image
->flags
;
1335 result
->image
.frame_count
= 1;
1336 result
->image
.xres
= image
->xres
;
1337 result
->image
.yres
= image
->yres
;
1338 result
->bounds
= metafile
->bounds
;
1339 result
->unit
= metafile
->unit
;
1340 result
->metafile_type
= metafile
->metafile_type
;
1341 result
->hemf
= CopyEnhMetaFileW(metafile
->hemf
, NULL
);
1342 list_init(&result
->containers
);
1350 *cloneImage
= &result
->image
;
1355 WARN("GpImage with no image data (metafile in wrong state?)\n");
1356 return InvalidParameter
;
1360 GpStatus WINGDIPAPI
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
1366 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
1368 if(!filename
|| !bitmap
)
1369 return InvalidParameter
;
1373 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
1378 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
1380 IStream_Release(stream
);
1385 GpStatus WINGDIPAPI
GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO
* info
,
1386 VOID
*bits
, GpBitmap
**bitmap
)
1388 DWORD height
, stride
;
1391 FIXME("(%p, %p, %p) - partially implemented\n", info
, bits
, bitmap
);
1393 if (!info
|| !bits
|| !bitmap
)
1394 return InvalidParameter
;
1396 height
= abs(info
->bmiHeader
.biHeight
);
1397 stride
= ((info
->bmiHeader
.biWidth
* info
->bmiHeader
.biBitCount
+ 31) >> 3) & ~3;
1399 if(info
->bmiHeader
.biHeight
> 0) /* bottom-up */
1401 bits
= (BYTE
*)bits
+ (height
- 1) * stride
;
1405 switch(info
->bmiHeader
.biBitCount
) {
1407 format
= PixelFormat1bppIndexed
;
1410 format
= PixelFormat4bppIndexed
;
1413 format
= PixelFormat8bppIndexed
;
1416 format
= PixelFormat16bppRGB555
;
1419 format
= PixelFormat24bppRGB
;
1422 format
= PixelFormat32bppRGB
;
1425 FIXME("don't know how to handle %d bpp\n", info
->bmiHeader
.biBitCount
);
1427 return InvalidParameter
;
1430 return GdipCreateBitmapFromScan0(info
->bmiHeader
.biWidth
, height
, stride
, format
,
1436 GpStatus WINGDIPAPI
GdipCreateBitmapFromFileICM(GDIPCONST WCHAR
* filename
,
1439 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
1441 return GdipCreateBitmapFromFile(filename
, bitmap
);
1444 GpStatus WINGDIPAPI
GdipCreateBitmapFromResource(HINSTANCE hInstance
,
1445 GDIPCONST WCHAR
* lpBitmapName
, GpBitmap
** bitmap
)
1448 GpStatus stat
= InvalidParameter
;
1450 TRACE("%p (%s) %p\n", hInstance
, debugstr_w(lpBitmapName
), bitmap
);
1452 if(!lpBitmapName
|| !bitmap
)
1453 return InvalidParameter
;
1456 hbm
= LoadImageW(hInstance
, lpBitmapName
, IMAGE_BITMAP
, 0, 0,
1457 LR_CREATEDIBSECTION
);
1460 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, bitmap
);
1467 static inline DWORD
blend_argb_no_bkgnd_alpha(DWORD src
, DWORD bkgnd
)
1470 BYTE g
= (BYTE
)(src
>> 8);
1471 BYTE r
= (BYTE
)(src
>> 16);
1472 DWORD alpha
= (BYTE
)(src
>> 24);
1473 return ((b
+ ((BYTE
)bkgnd
* (255 - alpha
) + 127) / 255) |
1474 (g
+ ((BYTE
)(bkgnd
>> 8) * (255 - alpha
) + 127) / 255) << 8 |
1475 (r
+ ((BYTE
)(bkgnd
>> 16) * (255 - alpha
) + 127) / 255) << 16 |
1479 GpStatus WINGDIPAPI
GdipCreateHBITMAPFromBitmap(GpBitmap
* bitmap
,
1480 HBITMAP
* hbmReturn
, ARGB background
)
1485 BITMAPINFOHEADER bih
;
1489 TRACE("(%p,%p,%x)\n", bitmap
, hbmReturn
, background
);
1491 if (!bitmap
|| !hbmReturn
) return InvalidParameter
;
1492 if (!image_lock(&bitmap
->image
, &unlock
)) return ObjectBusy
;
1494 GdipGetImageWidth(&bitmap
->image
, &width
);
1495 GdipGetImageHeight(&bitmap
->image
, &height
);
1497 bih
.biSize
= sizeof(bih
);
1498 bih
.biWidth
= width
;
1499 bih
.biHeight
= height
;
1501 bih
.biBitCount
= 32;
1502 bih
.biCompression
= BI_RGB
;
1503 bih
.biSizeImage
= 0;
1504 bih
.biXPelsPerMeter
= 0;
1505 bih
.biYPelsPerMeter
= 0;
1507 bih
.biClrImportant
= 0;
1509 result
= CreateDIBSection(0, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
, (void**)&bits
, NULL
, 0);
1512 image_unlock(&bitmap
->image
, unlock
);
1513 return GenericError
;
1516 stat
= convert_pixels(width
, height
, -width
*4,
1517 bits
+ (width
* 4 * (height
- 1)), PixelFormat32bppPARGB
,
1518 bitmap
->stride
, bitmap
->bits
, bitmap
->format
, bitmap
->image
.palette
);
1521 DeleteObject(result
);
1522 image_unlock(&bitmap
->image
, unlock
);
1526 if (background
& 0xffffff)
1530 for (ptr
= (DWORD
*)bits
, i
= 0; i
< width
* height
; ptr
++, i
++)
1532 if ((*ptr
& 0xff000000) == 0xff000000) continue;
1533 *ptr
= blend_argb_no_bkgnd_alpha(*ptr
, background
);
1537 *hbmReturn
= result
;
1538 image_unlock(&bitmap
->image
, unlock
);
1542 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
1543 GpGraphics
* target
, GpBitmap
** bitmap
)
1547 TRACE("(%d, %d, %p, %p)\n", width
, height
, target
, bitmap
);
1549 if(!target
|| !bitmap
)
1550 return InvalidParameter
;
1552 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat32bppPARGB
,
1557 GdipGetDpiX(target
, &(*bitmap
)->image
.xres
);
1558 GdipGetDpiY(target
, &(*bitmap
)->image
.yres
);
1564 GpStatus WINGDIPAPI
GdipCreateBitmapFromHICON(HICON hicon
, GpBitmap
** bitmap
)
1570 UINT width
, height
, stride
;
1572 BitmapData lockeddata
;
1576 BITMAPINFOHEADER bih
;
1581 TRACE("%p, %p\n", hicon
, bitmap
);
1583 if(!bitmap
|| !GetIconInfo(hicon
, &iinfo
))
1584 return InvalidParameter
;
1586 /* get the size of the icon */
1587 ret
= GetObjectA(iinfo
.hbmColor
? iinfo
.hbmColor
: iinfo
.hbmMask
, sizeof(bm
), &bm
);
1589 DeleteObject(iinfo
.hbmColor
);
1590 DeleteObject(iinfo
.hbmMask
);
1591 return GenericError
;
1595 height
= iinfo
.hbmColor
? abs(bm
.bmHeight
) : abs(bm
.bmHeight
) / 2;
1598 stat
= GdipCreateBitmapFromScan0(width
, height
, stride
, PixelFormat32bppARGB
, NULL
, bitmap
);
1600 DeleteObject(iinfo
.hbmColor
);
1601 DeleteObject(iinfo
.hbmMask
);
1608 rect
.Height
= height
;
1610 stat
= GdipBitmapLockBits(*bitmap
, &rect
, ImageLockModeWrite
, PixelFormat32bppARGB
, &lockeddata
);
1612 DeleteObject(iinfo
.hbmColor
);
1613 DeleteObject(iinfo
.hbmMask
);
1614 GdipDisposeImage(&(*bitmap
)->image
);
1618 bih
.biSize
= sizeof(bih
);
1619 bih
.biWidth
= width
;
1620 bih
.biHeight
= iinfo
.hbmColor
? -height
: -height
* 2;
1622 bih
.biBitCount
= 32;
1623 bih
.biCompression
= BI_RGB
;
1624 bih
.biSizeImage
= 0;
1625 bih
.biXPelsPerMeter
= 0;
1626 bih
.biYPelsPerMeter
= 0;
1628 bih
.biClrImportant
= 0;
1630 screendc
= CreateCompatibleDC(0);
1633 GetDIBits(screendc
, iinfo
.hbmColor
, 0, height
, lockeddata
.Scan0
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1635 if (bm
.bmBitsPixel
== 32)
1639 /* If any pixel has a non-zero alpha, ignore hbmMask */
1640 src
= (DWORD
*)lockeddata
.Scan0
;
1641 for (x
=0; x
<width
&& !has_alpha
; x
++)
1642 for (y
=0; y
<height
&& !has_alpha
; y
++)
1643 if ((*src
++ & 0xff000000) != 0)
1646 else has_alpha
= FALSE
;
1650 GetDIBits(screendc
, iinfo
.hbmMask
, 0, height
, lockeddata
.Scan0
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1658 BYTE
*bits
= heap_alloc(height
* stride
);
1660 /* read alpha data from the mask */
1662 GetDIBits(screendc
, iinfo
.hbmMask
, 0, height
, bits
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1664 GetDIBits(screendc
, iinfo
.hbmMask
, height
, height
, bits
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1667 dst_row
= lockeddata
.Scan0
;
1668 for (y
=0; y
<height
; y
++)
1670 dst
= (DWORD
*)dst_row
;
1671 for (x
=0; x
<height
; x
++)
1673 DWORD src_value
= *src
++;
1677 *dst
++ |= 0xff000000;
1679 dst_row
+= lockeddata
.Stride
;
1686 /* set constant alpha of 255 */
1687 dst_row
= lockeddata
.Scan0
;
1688 for (y
=0; y
<height
; y
++)
1690 dst
= (DWORD
*)dst_row
;
1691 for (x
=0; x
<height
; x
++)
1692 *dst
++ |= 0xff000000;
1693 dst_row
+= lockeddata
.Stride
;
1700 DeleteObject(iinfo
.hbmColor
);
1701 DeleteObject(iinfo
.hbmMask
);
1703 GdipBitmapUnlockBits(*bitmap
, &lockeddata
);
1708 static void generate_halftone_palette(ARGB
*entries
, UINT count
)
1710 static const BYTE halftone_values
[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1713 for (i
=0; i
<8 && i
<count
; i
++)
1715 entries
[i
] = 0xff000000;
1716 if (i
&1) entries
[i
] |= 0x800000;
1717 if (i
&2) entries
[i
] |= 0x8000;
1718 if (i
&4) entries
[i
] |= 0x80;
1722 entries
[i
] = 0xffc0c0c0;
1724 for (i
=9; i
<16 && i
<count
; i
++)
1726 entries
[i
] = 0xff000000;
1727 if (i
&1) entries
[i
] |= 0xff0000;
1728 if (i
&2) entries
[i
] |= 0xff00;
1729 if (i
&4) entries
[i
] |= 0xff;
1732 for (i
=16; i
<40 && i
<count
; i
++)
1737 for (i
=40; i
<256 && i
<count
; i
++)
1739 entries
[i
] = 0xff000000;
1740 entries
[i
] |= halftone_values
[(i
-40)%6];
1741 entries
[i
] |= halftone_values
[((i
-40)/6)%6] << 8;
1742 entries
[i
] |= halftone_values
[((i
-40)/36)%6] << 16;
1746 static GpStatus
get_screen_resolution(REAL
*xres
, REAL
*yres
)
1748 HDC screendc
= CreateCompatibleDC(0);
1750 if (!screendc
) return GenericError
;
1752 *xres
= (REAL
)GetDeviceCaps(screendc
, LOGPIXELSX
);
1753 *yres
= (REAL
)GetDeviceCaps(screendc
, LOGPIXELSY
);
1760 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
1761 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
1763 HBITMAP hbitmap
=NULL
;
1764 INT row_size
, dib_stride
;
1765 BYTE
*bits
=NULL
, *own_bits
=NULL
;
1769 TRACE("%d %d %d 0x%x %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
1771 if (!bitmap
) return InvalidParameter
;
1773 if(width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
1775 return InvalidParameter
;
1778 if(scan0
&& !stride
)
1779 return InvalidParameter
;
1781 stat
= get_screen_resolution(&xres
, &yres
);
1782 if (stat
!= Ok
) return stat
;
1784 row_size
= (width
* PIXELFORMATBPP(format
)+7) / 8;
1785 dib_stride
= (row_size
+ 3) & ~3;
1788 stride
= dib_stride
;
1790 if (format
& PixelFormatGDI
&& !(format
& (PixelFormatAlpha
|PixelFormatIndexed
)) && !scan0
)
1792 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
) + 256 * sizeof(RGBQUAD
)];
1793 BITMAPINFO
*pbmi
= (BITMAPINFO
*)bmibuf
;
1795 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1796 pbmi
->bmiHeader
.biWidth
= width
;
1797 pbmi
->bmiHeader
.biHeight
= -height
;
1798 pbmi
->bmiHeader
.biPlanes
= 1;
1799 /* FIXME: use the rest of the data from format */
1800 pbmi
->bmiHeader
.biBitCount
= PIXELFORMATBPP(format
);
1801 pbmi
->bmiHeader
.biCompression
= BI_RGB
;
1802 pbmi
->bmiHeader
.biSizeImage
= 0;
1803 pbmi
->bmiHeader
.biXPelsPerMeter
= 0;
1804 pbmi
->bmiHeader
.biYPelsPerMeter
= 0;
1805 pbmi
->bmiHeader
.biClrUsed
= 0;
1806 pbmi
->bmiHeader
.biClrImportant
= 0;
1808 hbitmap
= CreateDIBSection(0, pbmi
, DIB_RGB_COLORS
, (void**)&bits
, NULL
, 0);
1810 if (!hbitmap
) return GenericError
;
1812 stride
= dib_stride
;
1816 /* Not a GDI format; don't try to make an HBITMAP. */
1821 INT size
= abs(stride
) * height
;
1823 own_bits
= bits
= heap_alloc_zero(size
);
1824 if (!own_bits
) return OutOfMemory
;
1827 bits
+= stride
* (1 - height
);
1831 *bitmap
= heap_alloc_zero(sizeof(GpBitmap
));
1834 DeleteObject(hbitmap
);
1835 heap_free(own_bits
);
1839 (*bitmap
)->image
.type
= ImageTypeBitmap
;
1840 memcpy(&(*bitmap
)->image
.format
, &ImageFormatMemoryBMP
, sizeof(GUID
));
1841 (*bitmap
)->image
.flags
= ImageFlagsNone
;
1842 (*bitmap
)->image
.frame_count
= 1;
1843 (*bitmap
)->image
.current_frame
= 0;
1844 (*bitmap
)->image
.palette
= NULL
;
1845 (*bitmap
)->image
.xres
= xres
;
1846 (*bitmap
)->image
.yres
= yres
;
1847 (*bitmap
)->width
= width
;
1848 (*bitmap
)->height
= height
;
1849 (*bitmap
)->format
= format
;
1850 (*bitmap
)->image
.decoder
= NULL
;
1851 (*bitmap
)->hbitmap
= hbitmap
;
1852 (*bitmap
)->hdc
= NULL
;
1853 (*bitmap
)->bits
= bits
;
1854 (*bitmap
)->stride
= stride
;
1855 (*bitmap
)->own_bits
= own_bits
;
1856 (*bitmap
)->metadata_reader
= NULL
;
1857 (*bitmap
)->prop_count
= 0;
1858 (*bitmap
)->prop_item
= NULL
;
1860 /* set format-related flags */
1861 if (format
& (PixelFormatAlpha
|PixelFormatPAlpha
|PixelFormatIndexed
))
1862 (*bitmap
)->image
.flags
|= ImageFlagsHasAlpha
;
1864 if (format
== PixelFormat1bppIndexed
||
1865 format
== PixelFormat4bppIndexed
||
1866 format
== PixelFormat8bppIndexed
)
1868 (*bitmap
)->image
.palette
= heap_alloc_zero(sizeof(UINT
) * 2 + sizeof(ARGB
) * (1 << PIXELFORMATBPP(format
)));
1870 if (!(*bitmap
)->image
.palette
)
1872 GdipDisposeImage(&(*bitmap
)->image
);
1877 (*bitmap
)->image
.palette
->Count
= 1 << PIXELFORMATBPP(format
);
1879 if (format
== PixelFormat1bppIndexed
)
1881 (*bitmap
)->image
.palette
->Flags
= PaletteFlagsGrayScale
;
1882 (*bitmap
)->image
.palette
->Entries
[0] = 0xff000000;
1883 (*bitmap
)->image
.palette
->Entries
[1] = 0xffffffff;
1887 if (format
== PixelFormat8bppIndexed
)
1888 (*bitmap
)->image
.palette
->Flags
= PaletteFlagsHalftone
;
1890 generate_halftone_palette((*bitmap
)->image
.palette
->Entries
,
1891 (*bitmap
)->image
.palette
->Count
);
1895 TRACE("<-- %p\n", *bitmap
);
1901 static HBITMAP
hbitmap_from_emf(HENHMETAFILE hemf
)
1906 ENHMETAHEADER header
;
1911 GetEnhMetaFileHeader(hemf
, sizeof(header
), &header
);
1912 size
.cx
= header
.rclBounds
.right
- header
.rclBounds
.left
+ 1;
1913 size
.cy
= header
.rclBounds
.bottom
- header
.rclBounds
.top
+ 1;
1915 ZeroMemory(&bmi
, sizeof(bmi
));
1916 bmi
.bmiHeader
.biSize
= sizeof(bmi
.bmiHeader
);
1917 bmi
.bmiHeader
.biWidth
= size
.cx
;
1918 bmi
.bmiHeader
.biHeight
= size
.cy
;
1919 bmi
.bmiHeader
.biPlanes
= 1;
1920 bmi
.bmiHeader
.biBitCount
= 24;
1922 hdc
= CreateCompatibleDC(NULL
);
1923 hbm
= CreateDIBSection(hdc
, &bmi
, DIB_RGB_COLORS
, NULL
, NULL
, 0);
1925 hbmOld
= SelectObject(hdc
, hbm
);
1926 SetRect(&rc
, 0, 0, size
.cx
, size
.cy
);
1927 PlayEnhMetaFile(hdc
, hemf
, &rc
);
1928 SelectObject(hdc
, hbmOld
);
1935 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
1940 TRACE("%p %p\n", stream
, bitmap
);
1942 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
1948 if ((*bitmap
)->image
.type
== ImageTypeMetafile
)
1950 HBITMAP hbm
= hbitmap_from_emf(((GpMetafile
*)*bitmap
)->hemf
);
1951 GdipDisposeImage(&(*bitmap
)->image
);
1953 return GenericError
; /* FIXME: what error to return? */
1955 GdipCreateBitmapFromHBITMAP(hbm
, NULL
, bitmap
);
1960 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
1961 GdipDisposeImage(&(*bitmap
)->image
);
1963 return GenericError
; /* FIXME: what error to return? */
1970 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
1973 TRACE("%p %p\n", stream
, bitmap
);
1975 return GdipCreateBitmapFromStream(stream
, bitmap
);
1978 GpStatus WINGDIPAPI
GdipCreateCachedBitmap(GpBitmap
*bitmap
, GpGraphics
*graphics
,
1979 GpCachedBitmap
**cachedbmp
)
1983 TRACE("%p %p %p\n", bitmap
, graphics
, cachedbmp
);
1985 if(!bitmap
|| !graphics
|| !cachedbmp
)
1986 return InvalidParameter
;
1988 *cachedbmp
= heap_alloc_zero(sizeof(GpCachedBitmap
));
1992 stat
= GdipCloneImage(&(bitmap
->image
), &(*cachedbmp
)->image
);
1994 heap_free(*cachedbmp
);
2001 GpStatus WINGDIPAPI
GdipCreateHICONFromBitmap(GpBitmap
*bitmap
, HICON
*hicon
)
2004 BitmapData lockeddata
;
2005 ULONG andstride
, xorstride
, bitssize
;
2006 LPBYTE andbits
, xorbits
, androw
, xorrow
, srcrow
;
2009 TRACE("(%p, %p)\n", bitmap
, hicon
);
2011 if (!bitmap
|| !hicon
)
2012 return InvalidParameter
;
2014 stat
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
,
2015 PixelFormat32bppPARGB
, &lockeddata
);
2018 andstride
= ((lockeddata
.Width
+31)/32)*4;
2019 xorstride
= lockeddata
.Width
*4;
2020 bitssize
= (andstride
+ xorstride
) * lockeddata
.Height
;
2022 andbits
= heap_alloc_zero(bitssize
);
2026 xorbits
= andbits
+ andstride
* lockeddata
.Height
;
2028 for (y
=0; y
<lockeddata
.Height
; y
++)
2030 srcrow
= ((LPBYTE
)lockeddata
.Scan0
) + lockeddata
.Stride
* y
;
2032 androw
= andbits
+ andstride
* y
;
2033 for (x
=0; x
<lockeddata
.Width
; x
++)
2034 if (srcrow
[3+4*x
] >= 128)
2035 androw
[x
/8] |= 1 << (7-x
%8);
2037 xorrow
= xorbits
+ xorstride
* y
;
2038 memcpy(xorrow
, srcrow
, xorstride
);
2041 *hicon
= CreateIcon(NULL
, lockeddata
.Width
, lockeddata
.Height
, 1, 32,
2049 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
2055 GpStatus WINGDIPAPI
GdipDeleteCachedBitmap(GpCachedBitmap
*cachedbmp
)
2057 TRACE("%p\n", cachedbmp
);
2060 return InvalidParameter
;
2062 GdipDisposeImage(cachedbmp
->image
);
2063 heap_free(cachedbmp
);
2068 GpStatus WINGDIPAPI
GdipDrawCachedBitmap(GpGraphics
*graphics
,
2069 GpCachedBitmap
*cachedbmp
, INT x
, INT y
)
2071 TRACE("%p %p %d %d\n", graphics
, cachedbmp
, x
, y
);
2073 if(!graphics
|| !cachedbmp
)
2074 return InvalidParameter
;
2076 return GdipDrawImage(graphics
, cachedbmp
->image
, (REAL
)x
, (REAL
)y
);
2079 /* Internal utility function: Replace the image data of dst with that of src,
2081 static void move_bitmap(GpBitmap
*dst
, GpBitmap
*src
, BOOL clobber_palette
)
2083 assert(src
->image
.type
== ImageTypeBitmap
);
2084 assert(dst
->image
.type
== ImageTypeBitmap
);
2086 heap_free(dst
->bitmapbits
);
2087 heap_free(dst
->own_bits
);
2089 DeleteObject(dst
->hbitmap
);
2091 if (clobber_palette
)
2093 heap_free(dst
->image
.palette
);
2094 dst
->image
.palette
= src
->image
.palette
;
2097 heap_free(src
->image
.palette
);
2099 dst
->image
.xres
= src
->image
.xres
;
2100 dst
->image
.yres
= src
->image
.yres
;
2101 dst
->width
= src
->width
;
2102 dst
->height
= src
->height
;
2103 dst
->format
= src
->format
;
2104 dst
->hbitmap
= src
->hbitmap
;
2105 dst
->hdc
= src
->hdc
;
2106 dst
->bits
= src
->bits
;
2107 dst
->stride
= src
->stride
;
2108 dst
->own_bits
= src
->own_bits
;
2109 if (dst
->metadata_reader
)
2110 IWICMetadataReader_Release(dst
->metadata_reader
);
2111 dst
->metadata_reader
= src
->metadata_reader
;
2112 heap_free(dst
->prop_item
);
2113 dst
->prop_item
= src
->prop_item
;
2114 dst
->prop_count
= src
->prop_count
;
2115 if (dst
->image
.decoder
)
2116 IWICBitmapDecoder_Release(dst
->image
.decoder
);
2117 dst
->image
.decoder
= src
->image
.decoder
;
2118 dst
->image
.frame_count
= src
->image
.frame_count
;
2119 dst
->image
.current_frame
= src
->image
.current_frame
;
2120 dst
->image
.format
= src
->image
.format
;
2122 src
->image
.type
= ~0;
2126 static GpStatus
free_image_data(GpImage
*image
)
2129 return InvalidParameter
;
2131 if (image
->type
== ImageTypeBitmap
)
2133 heap_free(((GpBitmap
*)image
)->bitmapbits
);
2134 heap_free(((GpBitmap
*)image
)->own_bits
);
2135 DeleteDC(((GpBitmap
*)image
)->hdc
);
2136 DeleteObject(((GpBitmap
*)image
)->hbitmap
);
2137 if (((GpBitmap
*)image
)->metadata_reader
)
2138 IWICMetadataReader_Release(((GpBitmap
*)image
)->metadata_reader
);
2139 heap_free(((GpBitmap
*)image
)->prop_item
);
2141 else if (image
->type
== ImageTypeMetafile
)
2142 METAFILE_Free((GpMetafile
*)image
);
2145 WARN("invalid image: %p\n", image
);
2149 IWICBitmapDecoder_Release(image
->decoder
);
2150 heap_free(image
->palette
);
2155 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
2159 TRACE("%p\n", image
);
2161 status
= free_image_data(image
);
2162 if (status
!= Ok
) return status
;
2169 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
2173 TRACE("(%p,%p)\n", image
, item
);
2176 return InvalidParameter
;
2179 FIXME("not implemented\n");
2181 return NotImplemented
;
2184 GpStatus WINGDIPAPI
GdipGetImageItemData(GpImage
*image
, ImageItemData
*item
)
2188 TRACE("(%p,%p)\n", image
, item
);
2191 FIXME("not implemented\n");
2193 return NotImplemented
;
2196 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
2199 TRACE("%p %p %p\n", image
, srcRect
, srcUnit
);
2201 if(!image
|| !srcRect
|| !srcUnit
)
2202 return InvalidParameter
;
2203 if(image
->type
== ImageTypeMetafile
){
2204 *srcRect
= ((GpMetafile
*)image
)->bounds
;
2205 *srcUnit
= ((GpMetafile
*)image
)->unit
;
2207 else if(image
->type
== ImageTypeBitmap
){
2208 srcRect
->X
= srcRect
->Y
= 0.0;
2209 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
2210 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
2211 *srcUnit
= UnitPixel
;
2214 WARN("GpImage with no image data\n");
2215 return InvalidParameter
;
2218 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
2219 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
2224 GpStatus WINGDIPAPI
GdipGetImageDimension(GpImage
*image
, REAL
*width
,
2227 TRACE("%p %p %p\n", image
, width
, height
);
2229 if(!image
|| !height
|| !width
)
2230 return InvalidParameter
;
2232 if(image
->type
== ImageTypeMetafile
){
2233 *height
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Height
, ((GpMetafile
*)image
)->unit
, image
->yres
);
2234 *width
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Width
, ((GpMetafile
*)image
)->unit
, image
->xres
);
2236 else if(image
->type
== ImageTypeBitmap
){
2237 *height
= ((GpBitmap
*)image
)->height
;
2238 *width
= ((GpBitmap
*)image
)->width
;
2241 WARN("GpImage with no image data\n");
2242 return InvalidParameter
;
2245 TRACE("returning (%f, %f)\n", *height
, *width
);
2249 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
2250 GpGraphics
**graphics
)
2255 TRACE("%p %p\n", image
, graphics
);
2257 if(!image
|| !graphics
)
2258 return InvalidParameter
;
2260 if (image
->type
== ImageTypeBitmap
&& ((GpBitmap
*)image
)->hbitmap
)
2262 hdc
= ((GpBitmap
*)image
)->hdc
;
2265 hdc
= CreateCompatibleDC(0);
2266 SelectObject(hdc
, ((GpBitmap
*)image
)->hbitmap
);
2267 ((GpBitmap
*)image
)->hdc
= hdc
;
2270 stat
= GdipCreateFromHDC(hdc
, graphics
);
2274 (*graphics
)->image
= image
;
2275 (*graphics
)->xres
= image
->xres
;
2276 (*graphics
)->yres
= image
->yres
;
2279 else if (image
->type
== ImageTypeMetafile
)
2280 stat
= METAFILE_GetGraphicsContext((GpMetafile
*)image
, graphics
);
2282 stat
= graphics_from_image(image
, graphics
);
2287 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
2289 TRACE("%p %p\n", image
, height
);
2291 if(!image
|| !height
)
2292 return InvalidParameter
;
2294 if(image
->type
== ImageTypeMetafile
)
2295 *height
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Height
, ((GpMetafile
*)image
)->unit
, image
->yres
);
2296 else if(image
->type
== ImageTypeBitmap
)
2297 *height
= ((GpBitmap
*)image
)->height
;
2300 WARN("GpImage with no image data\n");
2301 return InvalidParameter
;
2304 TRACE("returning %d\n", *height
);
2309 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
2312 return InvalidParameter
;
2316 TRACE("(%p) <-- %0.2f\n", image
, *res
);
2321 GpStatus WINGDIPAPI
GdipGetImagePaletteSize(GpImage
*image
, INT
*size
)
2323 TRACE("%p %p\n", image
, size
);
2326 return InvalidParameter
;
2328 if (!image
->palette
|| image
->palette
->Count
== 0)
2329 *size
= sizeof(ColorPalette
);
2331 *size
= sizeof(UINT
)*2 + sizeof(ARGB
)*image
->palette
->Count
;
2333 TRACE("<-- %u\n", *size
);
2338 /* FIXME: test this function for non-bitmap types */
2339 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
2341 TRACE("%p %p\n", image
, format
);
2343 if(!image
|| !format
)
2344 return InvalidParameter
;
2346 if(image
->type
!= ImageTypeBitmap
)
2347 *format
= PixelFormat24bppRGB
;
2349 *format
= ((GpBitmap
*) image
)->format
;
2354 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
2356 TRACE("(%p, %p)\n", image
, format
);
2358 if(!image
|| !format
)
2359 return InvalidParameter
;
2361 memcpy(format
, &image
->format
, sizeof(GUID
));
2366 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
2368 TRACE("%p %p\n", image
, type
);
2371 return InvalidParameter
;
2373 *type
= image
->type
;
2378 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
2381 return InvalidParameter
;
2385 TRACE("(%p) <-- %0.2f\n", image
, *res
);
2390 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
2392 TRACE("%p %p\n", image
, width
);
2394 if(!image
|| !width
)
2395 return InvalidParameter
;
2397 if(image
->type
== ImageTypeMetafile
)
2398 *width
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Width
, ((GpMetafile
*)image
)->unit
, image
->xres
);
2399 else if(image
->type
== ImageTypeBitmap
)
2400 *width
= ((GpBitmap
*)image
)->width
;
2403 WARN("GpImage with no image data\n");
2404 return InvalidParameter
;
2407 TRACE("returning %d\n", *width
);
2412 GpStatus WINGDIPAPI
GdipGetPropertyCount(GpImage
*image
, UINT
*num
)
2414 TRACE("(%p, %p)\n", image
, num
);
2416 if (!image
|| !num
) return InvalidParameter
;
2420 if (image
->type
== ImageTypeBitmap
)
2422 if (((GpBitmap
*)image
)->prop_item
)
2424 *num
= ((GpBitmap
*)image
)->prop_count
;
2428 if (((GpBitmap
*)image
)->metadata_reader
)
2429 IWICMetadataReader_GetCount(((GpBitmap
*)image
)->metadata_reader
, num
);
2435 GpStatus WINGDIPAPI
GdipGetPropertyIdList(GpImage
*image
, UINT num
, PROPID
*list
)
2438 IWICMetadataReader
*reader
;
2439 IWICEnumMetadataItem
*enumerator
;
2440 UINT prop_count
, i
, items_returned
;
2442 TRACE("(%p, %u, %p)\n", image
, num
, list
);
2444 if (!image
|| !list
) return InvalidParameter
;
2446 if (image
->type
!= ImageTypeBitmap
)
2448 FIXME("Not implemented for type %d\n", image
->type
);
2449 return NotImplemented
;
2452 if (((GpBitmap
*)image
)->prop_item
)
2454 if (num
!= ((GpBitmap
*)image
)->prop_count
) return InvalidParameter
;
2456 for (i
= 0; i
< num
; i
++)
2458 list
[i
] = ((GpBitmap
*)image
)->prop_item
[i
].id
;
2464 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2467 if (num
!= 0) return InvalidParameter
;
2471 hr
= IWICMetadataReader_GetCount(reader
, &prop_count
);
2472 if (FAILED(hr
)) return hresult_to_status(hr
);
2474 if (num
!= prop_count
) return InvalidParameter
;
2476 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2477 if (FAILED(hr
)) return hresult_to_status(hr
);
2479 IWICEnumMetadataItem_Reset(enumerator
);
2481 for (i
= 0; i
< num
; i
++)
2485 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, NULL
, &items_returned
);
2486 if (hr
!= S_OK
) break;
2488 if (id
.vt
!= VT_UI2
)
2490 FIXME("not supported propvariant type for id: %u\n", id
.vt
);
2494 list
[i
] = id
.u
.uiVal
;
2497 IWICEnumMetadataItem_Release(enumerator
);
2499 return hr
== S_OK
? Ok
: hresult_to_status(hr
);
2502 static UINT
propvariant_size(PROPVARIANT
*value
)
2504 switch (value
->vt
& ~VT_VECTOR
)
2510 if (!(value
->vt
& VT_VECTOR
)) return 1;
2511 return value
->u
.caub
.cElems
;
2514 if (!(value
->vt
& VT_VECTOR
)) return 2;
2515 return value
->u
.caui
.cElems
* 2;
2519 if (!(value
->vt
& VT_VECTOR
)) return 4;
2520 return value
->u
.caul
.cElems
* 4;
2524 if (!(value
->vt
& VT_VECTOR
)) return 8;
2525 return value
->u
.cauh
.cElems
* 8;
2527 return value
->u
.pszVal
? strlen(value
->u
.pszVal
) + 1 : 0;
2529 return value
->u
.blob
.cbSize
;
2531 FIXME("not supported variant type %d\n", value
->vt
);
2536 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID propid
, UINT
*size
)
2539 IWICMetadataReader
*reader
;
2540 PROPVARIANT id
, value
;
2542 TRACE("(%p,%#x,%p)\n", image
, propid
, size
);
2544 if (!size
|| !image
) return InvalidParameter
;
2546 if (image
->type
!= ImageTypeBitmap
)
2548 FIXME("Not implemented for type %d\n", image
->type
);
2549 return NotImplemented
;
2552 if (((GpBitmap
*)image
)->prop_item
)
2556 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2558 if (propid
== ((GpBitmap
*)image
)->prop_item
[i
].id
)
2560 *size
= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
;
2565 return PropertyNotFound
;
2568 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2569 if (!reader
) return PropertyNotFound
;
2572 id
.u
.uiVal
= propid
;
2573 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
2574 if (FAILED(hr
)) return PropertyNotFound
;
2576 *size
= propvariant_size(&value
);
2577 if (*size
) *size
+= sizeof(PropertyItem
);
2578 PropVariantClear(&value
);
2583 #ifndef PropertyTagTypeSByte
2584 #define PropertyTagTypeSByte 6
2585 #define PropertyTagTypeSShort 8
2586 #define PropertyTagTypeFloat 11
2587 #define PropertyTagTypeDouble 12
2590 static UINT
vt_to_itemtype(UINT vt
)
2597 { VT_I1
, PropertyTagTypeSByte
},
2598 { VT_UI1
, PropertyTagTypeByte
},
2599 { VT_I2
, PropertyTagTypeSShort
},
2600 { VT_UI2
, PropertyTagTypeShort
},
2601 { VT_I4
, PropertyTagTypeSLONG
},
2602 { VT_UI4
, PropertyTagTypeLong
},
2603 { VT_I8
, PropertyTagTypeSRational
},
2604 { VT_UI8
, PropertyTagTypeRational
},
2605 { VT_R4
, PropertyTagTypeFloat
},
2606 { VT_R8
, PropertyTagTypeDouble
},
2607 { VT_LPSTR
, PropertyTagTypeASCII
},
2608 { VT_BLOB
, PropertyTagTypeUndefined
}
2611 for (i
= 0; i
< ARRAY_SIZE(vt2type
); i
++)
2613 if (vt2type
[i
].vt
== vt
) return vt2type
[i
].type
;
2615 FIXME("not supported variant type %u\n", vt
);
2619 static GpStatus
propvariant_to_item(PROPVARIANT
*value
, PropertyItem
*item
,
2620 UINT size
, PROPID id
)
2622 UINT item_size
, item_type
;
2624 item_size
= propvariant_size(value
);
2625 if (size
!= item_size
+ sizeof(PropertyItem
)) return InvalidParameter
;
2627 item_type
= vt_to_itemtype(value
->vt
& ~VT_VECTOR
);
2628 if (!item_type
) return InvalidParameter
;
2630 item
->value
= item
+ 1;
2632 switch (value
->vt
& ~VT_VECTOR
)
2636 if (!(value
->vt
& VT_VECTOR
))
2637 *(BYTE
*)item
->value
= value
->u
.bVal
;
2639 memcpy(item
->value
, value
->u
.caub
.pElems
, item_size
);
2643 if (!(value
->vt
& VT_VECTOR
))
2644 *(USHORT
*)item
->value
= value
->u
.uiVal
;
2646 memcpy(item
->value
, value
->u
.caui
.pElems
, item_size
);
2651 if (!(value
->vt
& VT_VECTOR
))
2652 *(ULONG
*)item
->value
= value
->u
.ulVal
;
2654 memcpy(item
->value
, value
->u
.caul
.pElems
, item_size
);
2659 if (!(value
->vt
& VT_VECTOR
))
2660 *(ULONGLONG
*)item
->value
= value
->u
.uhVal
.QuadPart
;
2662 memcpy(item
->value
, value
->u
.cauh
.pElems
, item_size
);
2665 memcpy(item
->value
, value
->u
.pszVal
, item_size
);
2668 memcpy(item
->value
, value
->u
.blob
.pBlobData
, item_size
);
2671 FIXME("not supported variant type %d\n", value
->vt
);
2672 return InvalidParameter
;
2675 item
->length
= item_size
;
2676 item
->type
= item_type
;
2682 GpStatus WINGDIPAPI
GdipGetPropertyItem(GpImage
*image
, PROPID propid
, UINT size
,
2683 PropertyItem
*buffer
)
2687 IWICMetadataReader
*reader
;
2688 PROPVARIANT id
, value
;
2690 TRACE("(%p,%#x,%u,%p)\n", image
, propid
, size
, buffer
);
2692 if (!image
|| !buffer
) return InvalidParameter
;
2694 if (image
->type
!= ImageTypeBitmap
)
2696 FIXME("Not implemented for type %d\n", image
->type
);
2697 return NotImplemented
;
2700 if (((GpBitmap
*)image
)->prop_item
)
2704 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2706 if (propid
== ((GpBitmap
*)image
)->prop_item
[i
].id
)
2708 if (size
!= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
)
2709 return InvalidParameter
;
2711 *buffer
= ((GpBitmap
*)image
)->prop_item
[i
];
2712 buffer
->value
= buffer
+ 1;
2713 memcpy(buffer
->value
, ((GpBitmap
*)image
)->prop_item
[i
].value
, buffer
->length
);
2718 return PropertyNotFound
;
2721 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2722 if (!reader
) return PropertyNotFound
;
2725 id
.u
.uiVal
= propid
;
2726 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
2727 if (FAILED(hr
)) return PropertyNotFound
;
2729 stat
= propvariant_to_item(&value
, buffer
, size
, propid
);
2730 PropVariantClear(&value
);
2735 GpStatus WINGDIPAPI
GdipGetPropertySize(GpImage
*image
, UINT
*size
, UINT
*count
)
2738 IWICMetadataReader
*reader
;
2739 IWICEnumMetadataItem
*enumerator
;
2740 UINT prop_count
, prop_size
, i
;
2741 PROPVARIANT id
, value
;
2743 TRACE("(%p,%p,%p)\n", image
, size
, count
);
2745 if (!image
|| !size
|| !count
) return InvalidParameter
;
2747 if (image
->type
!= ImageTypeBitmap
)
2749 FIXME("Not implemented for type %d\n", image
->type
);
2750 return NotImplemented
;
2753 if (((GpBitmap
*)image
)->prop_item
)
2755 *count
= ((GpBitmap
*)image
)->prop_count
;
2758 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2760 *size
+= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
;
2766 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2767 if (!reader
) return PropertyNotFound
;
2769 hr
= IWICMetadataReader_GetCount(reader
, &prop_count
);
2770 if (FAILED(hr
)) return hresult_to_status(hr
);
2772 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2773 if (FAILED(hr
)) return hresult_to_status(hr
);
2775 IWICEnumMetadataItem_Reset(enumerator
);
2779 PropVariantInit(&id
);
2780 PropVariantInit(&value
);
2782 for (i
= 0; i
< prop_count
; i
++)
2784 UINT items_returned
, item_size
;
2786 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, &value
, &items_returned
);
2787 if (hr
!= S_OK
) break;
2789 item_size
= propvariant_size(&value
);
2790 if (item_size
) prop_size
+= sizeof(PropertyItem
) + item_size
;
2792 PropVariantClear(&id
);
2793 PropVariantClear(&value
);
2796 IWICEnumMetadataItem_Release(enumerator
);
2798 if (hr
!= S_OK
) return PropertyNotFound
;
2800 *count
= prop_count
;
2805 GpStatus WINGDIPAPI
GdipGetAllPropertyItems(GpImage
*image
, UINT size
,
2806 UINT count
, PropertyItem
*buf
)
2810 IWICMetadataReader
*reader
;
2811 IWICEnumMetadataItem
*enumerator
;
2812 UINT prop_count
, prop_size
, i
;
2813 PROPVARIANT id
, value
;
2816 TRACE("(%p,%u,%u,%p)\n", image
, size
, count
, buf
);
2818 if (!image
|| !buf
) return InvalidParameter
;
2820 if (image
->type
!= ImageTypeBitmap
)
2822 FIXME("Not implemented for type %d\n", image
->type
);
2823 return NotImplemented
;
2826 status
= GdipGetPropertySize(image
, &prop_size
, &prop_count
);
2827 if (status
!= Ok
) return status
;
2829 if (prop_count
!= count
|| prop_size
!= size
) return InvalidParameter
;
2831 if (((GpBitmap
*)image
)->prop_item
)
2833 memcpy(buf
, ((GpBitmap
*)image
)->prop_item
, prop_size
);
2835 item_value
= (char *)(buf
+ prop_count
);
2837 for (i
= 0; i
< prop_count
; i
++)
2839 buf
[i
].value
= item_value
;
2840 item_value
+= buf
[i
].length
;
2846 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2847 if (!reader
) return PropertyNotFound
;
2849 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2850 if (FAILED(hr
)) return hresult_to_status(hr
);
2852 IWICEnumMetadataItem_Reset(enumerator
);
2854 item_value
= (char *)(buf
+ prop_count
);
2856 PropVariantInit(&id
);
2857 PropVariantInit(&value
);
2859 for (i
= 0; i
< prop_count
; i
++)
2862 UINT items_returned
, item_size
;
2864 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, &value
, &items_returned
);
2865 if (hr
!= S_OK
) break;
2867 if (id
.vt
!= VT_UI2
)
2869 FIXME("not supported propvariant type for id: %u\n", id
.vt
);
2873 item_size
= propvariant_size(&value
);
2876 item
= heap_alloc(item_size
+ sizeof(*item
));
2878 propvariant_to_item(&value
, item
, item_size
+ sizeof(*item
), id
.u
.uiVal
);
2879 buf
[i
].id
= item
->id
;
2880 buf
[i
].type
= item
->type
;
2881 buf
[i
].length
= item_size
;
2882 buf
[i
].value
= item_value
;
2883 memcpy(item_value
, item
->value
, item_size
);
2884 item_value
+= item_size
;
2889 PropVariantClear(&id
);
2890 PropVariantClear(&value
);
2893 IWICEnumMetadataItem_Release(enumerator
);
2895 if (hr
!= S_OK
) return PropertyNotFound
;
2900 struct image_format_dimension
2903 const GUID
*dimension
;
2906 static const struct image_format_dimension image_format_dimensions
[] =
2908 {&ImageFormatGIF
, &FrameDimensionTime
},
2909 {&ImageFormatIcon
, &FrameDimensionResolution
},
2913 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
2914 GDIPCONST GUID
* dimensionID
, UINT
* count
)
2916 TRACE("(%p,%s,%p)\n", image
, debugstr_guid(dimensionID
), count
);
2918 if(!image
|| !count
)
2919 return InvalidParameter
;
2922 IsEqualGUID(dimensionID
, &image
->format
) ||
2923 IsEqualGUID(dimensionID
, &FrameDimensionPage
) ||
2924 IsEqualGUID(dimensionID
, &FrameDimensionTime
))
2926 *count
= image
->frame_count
;
2930 return InvalidParameter
;
2933 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsCount(GpImage
*image
,
2936 TRACE("(%p, %p)\n", image
, count
);
2938 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2940 if(!image
|| !count
)
2941 return InvalidParameter
;
2948 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
2949 GUID
* dimensionIDs
, UINT count
)
2952 const GUID
*result
=NULL
;
2954 TRACE("(%p,%p,%u)\n", image
, dimensionIDs
, count
);
2956 if(!image
|| !dimensionIDs
|| count
!= 1)
2957 return InvalidParameter
;
2959 for (i
=0; image_format_dimensions
[i
].format
; i
++)
2961 if (IsEqualGUID(&image
->format
, image_format_dimensions
[i
].format
))
2963 result
= image_format_dimensions
[i
].dimension
;
2969 result
= &FrameDimensionPage
;
2971 memcpy(dimensionIDs
, result
, sizeof(GUID
));
2976 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
2982 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
2984 if (!filename
|| !image
)
2985 return InvalidParameter
;
2989 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
2994 stat
= GdipLoadImageFromStream(stream
, image
);
2996 IStream_Release(stream
);
3001 /* FIXME: no icm handling */
3002 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
3004 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
3006 return GdipLoadImageFromFile(filename
, image
);
3009 static void add_property(GpBitmap
*bitmap
, PropertyItem
*item
)
3011 UINT prop_size
, prop_count
;
3012 PropertyItem
*prop_item
;
3014 if (bitmap
->prop_item
== NULL
)
3016 prop_size
= prop_count
= 0;
3017 prop_item
= heap_alloc_zero(item
->length
+ sizeof(PropertyItem
));
3018 if (!prop_item
) return;
3025 GdipGetPropertySize(&bitmap
->image
, &prop_size
, &prop_count
);
3027 prop_item
= heap_alloc_zero(prop_size
+ item
->length
+ sizeof(PropertyItem
));
3028 if (!prop_item
) return;
3029 memcpy(prop_item
, bitmap
->prop_item
, sizeof(PropertyItem
) * bitmap
->prop_count
);
3030 prop_size
-= sizeof(PropertyItem
) * bitmap
->prop_count
;
3031 memcpy(prop_item
+ prop_count
+ 1, bitmap
->prop_item
+ prop_count
, prop_size
);
3033 item_value
= (char *)(prop_item
+ prop_count
+ 1);
3035 for (i
= 0; i
< prop_count
; i
++)
3037 prop_item
[i
].value
= item_value
;
3038 item_value
+= prop_item
[i
].length
;
3042 prop_item
[prop_count
].id
= item
->id
;
3043 prop_item
[prop_count
].type
= item
->type
;
3044 prop_item
[prop_count
].length
= item
->length
;
3045 prop_item
[prop_count
].value
= (char *)(prop_item
+ prop_count
+ 1) + prop_size
;
3046 memcpy(prop_item
[prop_count
].value
, item
->value
, item
->length
);
3048 heap_free(bitmap
->prop_item
);
3049 bitmap
->prop_item
= prop_item
;
3050 bitmap
->prop_count
++;
3053 static BOOL
get_bool_property(IWICMetadataReader
*reader
, const GUID
*guid
, const WCHAR
*prop_name
)
3057 PROPVARIANT id
, value
;
3060 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3061 if (FAILED(hr
) || !IsEqualGUID(&format
, guid
)) return FALSE
;
3063 PropVariantInit(&id
);
3064 PropVariantInit(&value
);
3067 id
.u
.pwszVal
= CoTaskMemAlloc((lstrlenW(prop_name
) + 1) * sizeof(WCHAR
));
3068 if (!id
.u
.pwszVal
) return FALSE
;
3069 lstrcpyW(id
.u
.pwszVal
, prop_name
);
3070 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
3071 if (hr
== S_OK
&& value
.vt
== VT_BOOL
)
3072 ret
= value
.u
.boolVal
;
3074 PropVariantClear(&id
);
3075 PropVariantClear(&value
);
3080 static PropertyItem
*get_property(IWICMetadataReader
*reader
, const GUID
*guid
, const WCHAR
*prop_name
)
3084 PROPVARIANT id
, value
;
3085 PropertyItem
*item
= NULL
;
3087 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3088 if (FAILED(hr
) || !IsEqualGUID(&format
, guid
)) return NULL
;
3090 PropVariantInit(&id
);
3091 PropVariantInit(&value
);
3094 id
.u
.pwszVal
= CoTaskMemAlloc((lstrlenW(prop_name
) + 1) * sizeof(WCHAR
));
3095 if (!id
.u
.pwszVal
) return NULL
;
3096 lstrcpyW(id
.u
.pwszVal
, prop_name
);
3097 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
3100 UINT item_size
= propvariant_size(&value
);
3103 item_size
+= sizeof(*item
);
3104 item
= heap_alloc_zero(item_size
);
3105 if (propvariant_to_item(&value
, item
, item_size
, 0) != Ok
)
3113 PropVariantClear(&id
);
3114 PropVariantClear(&value
);
3119 static PropertyItem
*get_gif_comment(IWICMetadataReader
*reader
)
3121 static const WCHAR textentryW
[] = { 'T','e','x','t','E','n','t','r','y',0 };
3122 PropertyItem
*comment
;
3124 comment
= get_property(reader
, &GUID_MetadataFormatGifComment
, textentryW
);
3126 comment
->id
= PropertyTagExifUserComment
;
3131 static PropertyItem
*get_gif_loopcount(IWICMetadataReader
*reader
)
3133 static const WCHAR applicationW
[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3134 static const WCHAR dataW
[] = { 'D','a','t','a',0 };
3135 PropertyItem
*appext
= NULL
, *appdata
= NULL
, *loop
= NULL
;
3137 appext
= get_property(reader
, &GUID_MetadataFormatAPE
, applicationW
);
3140 if (appext
->type
== PropertyTagTypeByte
&& appext
->length
== 11 &&
3141 (!memcmp(appext
->value
, "NETSCAPE2.0", 11) || !memcmp(appext
->value
, "ANIMEXTS1.0", 11)))
3143 appdata
= get_property(reader
, &GUID_MetadataFormatAPE
, dataW
);
3146 if (appdata
->type
== PropertyTagTypeByte
&& appdata
->length
== 4)
3148 BYTE
*data
= appdata
->value
;
3149 if (data
[0] == 3 && data
[1] == 1)
3151 loop
= heap_alloc_zero(sizeof(*loop
) + sizeof(SHORT
));
3154 loop
->type
= PropertyTagTypeShort
;
3155 loop
->id
= PropertyTagLoopCount
;
3156 loop
->length
= sizeof(SHORT
);
3157 loop
->value
= loop
+ 1;
3158 *(SHORT
*)loop
->value
= data
[2] | (data
[3] << 8);
3172 static PropertyItem
*get_gif_background(IWICMetadataReader
*reader
)
3174 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 };
3175 PropertyItem
*background
;
3177 background
= get_property(reader
, &GUID_MetadataFormatLSD
, backgroundW
);
3179 background
->id
= PropertyTagIndexBackground
;
3184 static PropertyItem
*get_gif_palette(IWICBitmapDecoder
*decoder
, IWICMetadataReader
*reader
)
3186 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 IWICImagingFactory
*factory
;
3189 IWICPalette
*palette
;
3191 WICColor colors
[256];
3193 if (!get_bool_property(reader
, &GUID_MetadataFormatLSD
, global_flagW
))
3196 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
3197 if (hr
!= S_OK
) return NULL
;
3199 hr
= IWICImagingFactory_CreatePalette(factory
, &palette
);
3202 hr
= IWICBitmapDecoder_CopyPalette(decoder
, palette
);
3204 IWICPalette_GetColors(palette
, 256, colors
, &count
);
3206 IWICPalette_Release(palette
);
3209 IWICImagingFactory_Release(factory
);
3217 pal
= heap_alloc_zero(sizeof(*pal
) + count
* 3);
3218 if (!pal
) return NULL
;
3219 pal
->type
= PropertyTagTypeByte
;
3220 pal
->id
= PropertyTagGlobalPalette
;
3221 pal
->value
= pal
+ 1;
3222 pal
->length
= count
* 3;
3226 for (i
= 0; i
< count
; i
++)
3228 rgb
[i
*3] = (colors
[i
] >> 16) & 0xff;
3229 rgb
[i
*3 + 1] = (colors
[i
] >> 8) & 0xff;
3230 rgb
[i
*3 + 2] = colors
[i
] & 0xff;
3239 static PropertyItem
*get_gif_transparent_idx(IWICMetadataReader
*reader
)
3241 static const WCHAR transparency_flagW
[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3242 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 };
3243 PropertyItem
*index
= NULL
;
3245 if (get_bool_property(reader
, &GUID_MetadataFormatGCE
, transparency_flagW
))
3247 index
= get_property(reader
, &GUID_MetadataFormatGCE
, colorW
);
3249 index
->id
= PropertyTagIndexTransparent
;
3254 static LONG
get_gif_frame_property(IWICBitmapFrameDecode
*frame
, const GUID
*format
, const WCHAR
*property
)
3257 IWICMetadataBlockReader
*block_reader
;
3258 IWICMetadataReader
*reader
;
3259 UINT block_count
, i
;
3263 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3266 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3269 for (i
= 0; i
< block_count
; i
++)
3271 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3274 prop
= get_property(reader
, format
, property
);
3277 if (prop
->type
== PropertyTagTypeByte
&& prop
->length
== 1)
3278 value
= *(BYTE
*)prop
->value
;
3279 else if (prop
->type
== PropertyTagTypeShort
&& prop
->length
== 2)
3280 value
= *(SHORT
*)prop
->value
;
3284 IWICMetadataReader_Release(reader
);
3288 IWICMetadataBlockReader_Release(block_reader
);
3294 static void gif_metadata_reader(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT active_frame
)
3296 static const WCHAR delayW
[] = { 'D','e','l','a','y',0 };
3298 IWICBitmapFrameDecode
*frame
;
3299 IWICMetadataBlockReader
*block_reader
;
3300 IWICMetadataReader
*reader
;
3301 UINT frame_count
, block_count
, i
;
3302 PropertyItem
*delay
= NULL
, *comment
= NULL
, *background
= NULL
;
3303 PropertyItem
*transparent_idx
= NULL
, *loop
= NULL
, *palette
= NULL
;
3305 IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
3306 if (frame_count
> 1)
3308 delay
= heap_alloc_zero(sizeof(*delay
) + frame_count
* sizeof(LONG
));
3313 delay
->type
= PropertyTagTypeLong
;
3314 delay
->id
= PropertyTagFrameDelay
;
3315 delay
->length
= frame_count
* sizeof(LONG
);
3316 delay
->value
= delay
+ 1;
3318 value
= delay
->value
;
3320 for (i
= 0; i
< frame_count
; i
++)
3322 hr
= IWICBitmapDecoder_GetFrame(decoder
, i
, &frame
);
3325 value
[i
] = get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, delayW
);
3326 IWICBitmapFrameDecode_Release(frame
);
3333 hr
= IWICBitmapDecoder_QueryInterface(decoder
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3336 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3339 for (i
= 0; i
< block_count
; i
++)
3341 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3345 comment
= get_gif_comment(reader
);
3347 if (frame_count
> 1 && !loop
)
3348 loop
= get_gif_loopcount(reader
);
3351 background
= get_gif_background(reader
);
3354 palette
= get_gif_palette(decoder
, reader
);
3356 IWICMetadataReader_Release(reader
);
3360 IWICMetadataBlockReader_Release(block_reader
);
3363 if (frame_count
> 1 && !loop
)
3365 loop
= heap_alloc_zero(sizeof(*loop
) + sizeof(SHORT
));
3368 loop
->type
= PropertyTagTypeShort
;
3369 loop
->id
= PropertyTagLoopCount
;
3370 loop
->length
= sizeof(SHORT
);
3371 loop
->value
= loop
+ 1;
3372 *(SHORT
*)loop
->value
= 1;
3376 if (delay
) add_property(bitmap
, delay
);
3377 if (comment
) add_property(bitmap
, comment
);
3378 if (loop
) add_property(bitmap
, loop
);
3379 if (palette
) add_property(bitmap
, palette
);
3380 if (background
) add_property(bitmap
, background
);
3386 heap_free(background
);
3388 /* Win7 gdiplus always returns transparent color index from frame 0 */
3389 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
3390 if (hr
!= S_OK
) return;
3392 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3395 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3398 for (i
= 0; i
< block_count
; i
++)
3400 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3403 if (!transparent_idx
)
3404 transparent_idx
= get_gif_transparent_idx(reader
);
3406 IWICMetadataReader_Release(reader
);
3410 IWICMetadataBlockReader_Release(block_reader
);
3413 if (transparent_idx
) add_property(bitmap
, transparent_idx
);
3414 heap_free(transparent_idx
);
3416 IWICBitmapFrameDecode_Release(frame
);
3419 static PropertyItem
* create_prop(PROPID propid
, PROPVARIANT
* value
)
3421 PropertyItem
*item
= NULL
;
3422 UINT item_size
= propvariant_size(value
);
3426 item_size
+= sizeof(*item
);
3427 item
= heap_alloc_zero(item_size
);
3428 if (propvariant_to_item(value
, item
, item_size
, propid
) != Ok
)
3438 static ULONG
get_ulong_by_index(IWICMetadataReader
* reader
, ULONG index
)
3444 hr
= IWICMetadataReader_GetValueByIndex(reader
, index
, NULL
, NULL
, &value
);
3450 result
= value
.u
.ulVal
;
3453 ERR("unhandled case %u\n", value
.vt
);
3456 PropVariantClear(&value
);
3461 static void png_metadata_reader(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT active_frame
)
3464 IWICBitmapFrameDecode
*frame
;
3465 IWICMetadataBlockReader
*block_reader
;
3466 IWICMetadataReader
*reader
;
3467 UINT block_count
, i
, j
;
3468 struct keyword_info
{
3473 { "Title", PropertyTagImageTitle
},
3474 { "Author", PropertyTagArtist
},
3475 { "Description", PropertyTagImageDescription
},
3476 { "Copyright", PropertyTagCopyright
},
3477 { "Software", PropertyTagSoftwareUsed
},
3478 { "Source", PropertyTagEquipModel
},
3479 { "Comment", PropertyTagExifUserComment
},
3481 BOOL seen_gamma
=FALSE
, seen_whitepoint
=FALSE
, seen_chrm
=FALSE
;
3483 hr
= IWICBitmapDecoder_GetFrame(decoder
, active_frame
, &frame
);
3484 if (hr
!= S_OK
) return;
3486 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3489 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3492 for (i
= 0; i
< block_count
; i
++)
3494 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3499 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3500 if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunktEXt
, &format
))
3502 PROPVARIANT name
, value
;
3505 hr
= IWICMetadataReader_GetValueByIndex(reader
, 0, NULL
, &name
, &value
);
3509 if (name
.vt
== VT_LPSTR
)
3511 for (j
= 0; j
< ARRAY_SIZE(keywords
); j
++)
3512 if (!strcmp(keywords
[j
].name
, name
.u
.pszVal
))
3514 if (j
< ARRAY_SIZE(keywords
) && !keywords
[j
].seen
)
3516 keywords
[j
].seen
= TRUE
;
3517 item
= create_prop(keywords
[j
].propid
, &value
);
3519 add_property(bitmap
, item
);
3524 PropVariantClear(&name
);
3525 PropVariantClear(&value
);
3528 else if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA
, &format
))
3534 item
= heap_alloc_zero(sizeof(PropertyItem
) + sizeof(ULONG
) * 2);
3538 item
->length
= sizeof(ULONG
) * 2;
3539 item
->type
= PropertyTagTypeRational
;
3540 item
->id
= PropertyTagGamma
;
3541 rational
= item
->value
= item
+ 1;
3542 rational
[0] = 100000;
3543 rational
[1] = get_ulong_by_index(reader
, 0);
3544 add_property(bitmap
, item
);
3550 else if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunkcHRM
, &format
))
3554 if (!seen_whitepoint
)
3556 item
= GdipAlloc(sizeof(PropertyItem
) + sizeof(ULONG
) * 4);
3560 item
->length
= sizeof(ULONG
) * 4;
3561 item
->type
= PropertyTagTypeRational
;
3562 item
->id
= PropertyTagWhitePoint
;
3563 rational
= item
->value
= item
+ 1;
3564 rational
[0] = get_ulong_by_index(reader
, 0);
3565 rational
[1] = 100000;
3566 rational
[2] = get_ulong_by_index(reader
, 1);
3567 rational
[3] = 100000;
3568 add_property(bitmap
, item
);
3569 seen_whitepoint
= TRUE
;
3575 item
= GdipAlloc(sizeof(PropertyItem
) + sizeof(ULONG
) * 12);
3579 item
->length
= sizeof(ULONG
) * 12;
3580 item
->type
= PropertyTagTypeRational
;
3581 item
->id
= PropertyTagPrimaryChromaticities
;
3582 rational
= item
->value
= item
+ 1;
3583 rational
[0] = get_ulong_by_index(reader
, 2);
3584 rational
[1] = 100000;
3585 rational
[2] = get_ulong_by_index(reader
, 3);
3586 rational
[3] = 100000;
3587 rational
[4] = get_ulong_by_index(reader
, 4);
3588 rational
[5] = 100000;
3589 rational
[6] = get_ulong_by_index(reader
, 5);
3590 rational
[7] = 100000;
3591 rational
[8] = get_ulong_by_index(reader
, 6);
3592 rational
[9] = 100000;
3593 rational
[10] = get_ulong_by_index(reader
, 7);
3594 rational
[11] = 100000;
3595 add_property(bitmap
, item
);
3602 IWICMetadataReader_Release(reader
);
3606 IWICMetadataBlockReader_Release(block_reader
);
3609 IWICBitmapFrameDecode_Release(frame
);
3612 static GpStatus
initialize_decoder_wic(IStream
*stream
, REFGUID container
, IWICBitmapDecoder
**decoder
)
3614 IWICImagingFactory
*factory
;
3617 TRACE("%p,%s\n", stream
, wine_dbgstr_guid(container
));
3619 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
3620 if (FAILED(hr
)) return hresult_to_status(hr
);
3621 hr
= IWICImagingFactory_CreateDecoder(factory
, container
, NULL
, decoder
);
3622 IWICImagingFactory_Release(factory
);
3623 if (FAILED(hr
)) return hresult_to_status(hr
);
3625 hr
= IWICBitmapDecoder_Initialize(*decoder
, stream
, WICDecodeMetadataCacheOnLoad
);
3626 if (FAILED(hr
)) return hresult_to_status(hr
);
3630 typedef void (*metadata_reader_func
)(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT frame
);
3632 static GpStatus
decode_frame_wic(IWICBitmapDecoder
*decoder
, BOOL force_conversion
,
3633 UINT active_frame
, metadata_reader_func metadata_reader
, GpImage
**image
)
3638 IWICBitmapFrameDecode
*frame
;
3639 IWICBitmapSource
*source
=NULL
;
3640 IWICMetadataBlockReader
*block_reader
;
3641 WICPixelFormatGUID wic_format
;
3642 PixelFormat gdip_format
=0;
3643 ColorPalette
*palette
= NULL
;
3644 WICBitmapPaletteType palette_type
= WICBitmapPaletteTypeFixedHalftone256
;
3646 UINT width
, height
, frame_count
;
3647 BitmapData lockeddata
;
3650 TRACE("%p,%u,%p\n", decoder
, active_frame
, image
);
3652 IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
3653 hr
= IWICBitmapDecoder_GetFrame(decoder
, active_frame
, &frame
);
3654 if (SUCCEEDED(hr
)) /* got frame */
3656 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &wic_format
);
3660 if (!force_conversion
)
3662 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
3664 if (IsEqualGUID(&wic_format
, pixel_formats
[i
].wic_format
))
3666 source
= (IWICBitmapSource
*)frame
;
3667 IWICBitmapSource_AddRef(source
);
3668 gdip_format
= pixel_formats
[i
].gdip_format
;
3669 palette_type
= pixel_formats
[i
].palette_type
;
3676 /* unknown format; fall back on 32bppARGB */
3677 hr
= WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA
, (IWICBitmapSource
*)frame
, &source
);
3678 gdip_format
= PixelFormat32bppARGB
;
3680 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format
), gdip_format
);
3683 if (SUCCEEDED(hr
)) /* got source */
3685 hr
= IWICBitmapSource_GetSize(source
, &width
, &height
);
3688 status
= GdipCreateBitmapFromScan0(width
, height
, 0, gdip_format
,
3691 if (SUCCEEDED(hr
) && status
== Ok
) /* created bitmap */
3693 status
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeWrite
,
3694 gdip_format
, &lockeddata
);
3695 if (status
== Ok
) /* locked bitmap */
3700 for (i
=0; i
<height
; i
++)
3703 hr
= IWICBitmapSource_CopyPixels(source
, &wrc
, abs(lockeddata
.Stride
),
3704 abs(lockeddata
.Stride
), (BYTE
*)lockeddata
.Scan0
+lockeddata
.Stride
*i
);
3705 if (FAILED(hr
)) break;
3708 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
3711 if (SUCCEEDED(hr
) && status
== Ok
)
3712 *image
= &bitmap
->image
;
3716 GdipDisposeImage(&bitmap
->image
);
3719 if (SUCCEEDED(hr
) && status
== Ok
)
3722 hr
= IWICBitmapSource_GetResolution(source
, &dpix
, &dpiy
);
3725 bitmap
->image
.xres
= dpix
;
3726 bitmap
->image
.yres
= dpiy
;
3732 IWICBitmapSource_Release(source
);
3735 if (SUCCEEDED(hr
)) {
3736 bitmap
->metadata_reader
= NULL
;
3738 if (metadata_reader
)
3739 metadata_reader(bitmap
, decoder
, active_frame
);
3740 else if (IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
) == S_OK
)
3742 UINT block_count
= 0;
3743 if (IWICMetadataBlockReader_GetCount(block_reader
, &block_count
) == S_OK
&& block_count
)
3744 IWICMetadataBlockReader_GetReaderByIndex(block_reader
, 0, &bitmap
->metadata_reader
);
3745 IWICMetadataBlockReader_Release(block_reader
);
3748 palette
= get_palette(frame
, palette_type
);
3749 IWICBitmapFrameDecode_Release(frame
);
3753 if (FAILED(hr
) && status
== Ok
) status
= hresult_to_status(hr
);
3757 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3758 bitmap
->image
.flags
|= ImageFlagsReadOnly
|ImageFlagsHasRealPixelSize
|ImageFlagsHasRealDPI
;
3759 if (IsEqualGUID(&wic_format
, &GUID_WICPixelFormat2bppGray
) ||
3760 IsEqualGUID(&wic_format
, &GUID_WICPixelFormat4bppGray
) ||
3761 IsEqualGUID(&wic_format
, &GUID_WICPixelFormat8bppGray
) ||
3762 IsEqualGUID(&wic_format
, &GUID_WICPixelFormat16bppGray
))
3763 bitmap
->image
.flags
|= ImageFlagsColorSpaceGRAY
;
3765 bitmap
->image
.flags
|= ImageFlagsColorSpaceRGB
;
3766 bitmap
->image
.frame_count
= frame_count
;
3767 bitmap
->image
.current_frame
= active_frame
;
3768 bitmap
->image
.decoder
= decoder
;
3769 IWICBitmapDecoder_AddRef(decoder
);
3772 heap_free(bitmap
->image
.palette
);
3773 bitmap
->image
.palette
= palette
;
3777 if (IsEqualGUID(&wic_format
, &GUID_WICPixelFormatBlackWhite
))
3778 bitmap
->image
.palette
->Flags
= 0;
3780 TRACE("=> %p\n", *image
);
3786 static GpStatus
decode_image_wic(IStream
*stream
, REFGUID container
,
3787 metadata_reader_func metadata_reader
, GpImage
**image
)
3789 IWICBitmapDecoder
*decoder
;
3792 status
= initialize_decoder_wic(stream
, container
, &decoder
);
3796 status
= decode_frame_wic(decoder
, FALSE
, 0, metadata_reader
, image
);
3797 IWICBitmapDecoder_Release(decoder
);
3801 static GpStatus
select_frame_wic(GpImage
*image
, UINT active_frame
)
3806 status
= decode_frame_wic(image
->decoder
, FALSE
, active_frame
, NULL
, &new_image
);
3810 new_image
->busy
= image
->busy
;
3811 memcpy(&new_image
->format
, &image
->format
, sizeof(GUID
));
3812 free_image_data(image
);
3813 if (image
->type
== ImageTypeBitmap
)
3814 *(GpBitmap
*)image
= *(GpBitmap
*)new_image
;
3815 else if (image
->type
== ImageTypeMetafile
)
3816 *(GpMetafile
*)image
= *(GpMetafile
*)new_image
;
3817 new_image
->type
= ~0;
3818 heap_free(new_image
);
3822 static HRESULT
get_gif_frame_rect(IWICBitmapFrameDecode
*frame
,
3823 UINT
*left
, UINT
*top
, UINT
*width
, UINT
*height
)
3825 static const WCHAR leftW
[] = {'L','e','f','t',0};
3826 static const WCHAR topW
[] = {'T','o','p',0};
3828 *left
= get_gif_frame_property(frame
, &GUID_MetadataFormatIMD
, leftW
);
3829 *top
= get_gif_frame_property(frame
, &GUID_MetadataFormatIMD
, topW
);
3831 return IWICBitmapFrameDecode_GetSize(frame
, width
, height
);
3834 static HRESULT
blit_gif_frame(GpBitmap
*bitmap
, IWICBitmapFrameDecode
*frame
, BOOL first_frame
)
3836 UINT i
, j
, left
, top
, width
, height
;
3837 IWICBitmapSource
*source
;
3841 hr
= get_gif_frame_rect(frame
, &left
, &top
, &width
, &height
);
3845 hr
= WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA
, (IWICBitmapSource
*)frame
, &source
);
3849 new_bits
= heap_alloc_zero(width
*height
*4);
3851 return E_OUTOFMEMORY
;
3853 hr
= IWICBitmapSource_CopyPixels(source
, NULL
, width
*4, width
*height
*4, new_bits
);
3854 IWICBitmapSource_Release(source
);
3856 heap_free(new_bits
);
3860 for(i
=0; i
<height
&& i
+top
<bitmap
->height
; i
++) {
3861 for(j
=0; j
<width
&& j
+left
<bitmap
->width
; j
++) {
3862 DWORD
*src
= (DWORD
*)(new_bits
+i
*width
*4+j
*4);
3863 DWORD
*dst
= (DWORD
*)(bitmap
->bits
+(i
+top
)*bitmap
->stride
+(j
+left
)*4);
3865 if(first_frame
|| *src
>>24 != 0)
3869 heap_free(new_bits
);
3873 static DWORD
get_gif_background_color(GpBitmap
*bitmap
)
3875 BYTE bgcolor_idx
= 0;
3878 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3879 if(bitmap
->prop_item
[i
].id
== PropertyTagIndexBackground
) {
3880 bgcolor_idx
= *(BYTE
*)bitmap
->prop_item
[i
].value
;
3885 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3886 if(bitmap
->prop_item
[i
].id
== PropertyTagIndexTransparent
) {
3887 BYTE transparent_idx
;
3888 transparent_idx
= *(BYTE
*)bitmap
->prop_item
[i
].value
;
3890 if(transparent_idx
== bgcolor_idx
)
3895 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3896 if(bitmap
->prop_item
[i
].id
== PropertyTagGlobalPalette
) {
3897 if(bitmap
->prop_item
[i
].length
/3 > bgcolor_idx
) {
3898 BYTE
*color
= ((BYTE
*)bitmap
->prop_item
[i
].value
)+bgcolor_idx
*3;
3899 return color
[2] + (color
[1]<<8) + (color
[0]<<16) + (0xffu
<<24);
3905 FIXME("can't get gif background color\n");
3909 static GpStatus
select_frame_gif(GpImage
* image
, UINT active_frame
)
3911 static const WCHAR disposalW
[] = {'D','i','s','p','o','s','a','l',0};
3913 GpBitmap
*bitmap
= (GpBitmap
*)image
;
3914 IWICBitmapFrameDecode
*frame
;
3915 int cur_frame
=0, disposal
;
3916 BOOL bgcolor_set
= FALSE
;
3920 if(active_frame
> image
->current_frame
) {
3921 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, image
->current_frame
, &frame
);
3923 return hresult_to_status(hr
);
3924 disposal
= get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, disposalW
);
3925 IWICBitmapFrameDecode_Release(frame
);
3927 if(disposal
== GIF_DISPOSE_RESTORE_TO_BKGND
)
3928 cur_frame
= image
->current_frame
;
3929 else if(disposal
!= GIF_DISPOSE_RESTORE_TO_PREV
)
3930 cur_frame
= image
->current_frame
+1;
3933 while(cur_frame
!= active_frame
) {
3934 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, cur_frame
, &frame
);
3936 return hresult_to_status(hr
);
3937 disposal
= get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, disposalW
);
3939 if(disposal
==GIF_DISPOSE_UNSPECIFIED
|| disposal
==GIF_DISPOSE_DO_NOT_DISPOSE
) {
3940 hr
= blit_gif_frame(bitmap
, frame
, cur_frame
==0);
3942 return hresult_to_status(hr
);
3943 }else if(disposal
== GIF_DISPOSE_RESTORE_TO_BKGND
) {
3944 UINT left
, top
, width
, height
, i
, j
;
3947 bgcolor
= get_gif_background_color(bitmap
);
3951 hr
= get_gif_frame_rect(frame
, &left
, &top
, &width
, &height
);
3953 return hresult_to_status(hr
);
3954 for(i
=top
; i
<top
+height
&& i
<bitmap
->height
; i
++) {
3955 DWORD
*bits
= (DWORD
*)(bitmap
->bits
+i
*bitmap
->stride
);
3956 for(j
=left
; j
<left
+width
&& j
<bitmap
->width
; j
++)
3961 IWICBitmapFrameDecode_Release(frame
);
3965 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, active_frame
, &frame
);
3967 return hresult_to_status(hr
);
3969 hr
= blit_gif_frame(bitmap
, frame
, cur_frame
==0);
3970 IWICBitmapFrameDecode_Release(frame
);
3972 return hresult_to_status(hr
);
3974 image
->current_frame
= active_frame
;
3978 static GpStatus
decode_image_icon(IStream
* stream
, GpImage
**image
)
3980 return decode_image_wic(stream
, &GUID_ContainerFormatIco
, NULL
, image
);
3983 static GpStatus
decode_image_bmp(IStream
* stream
, GpImage
**image
)
3988 status
= decode_image_wic(stream
, &GUID_ContainerFormatBmp
, NULL
, image
);
3990 bitmap
= (GpBitmap
*)*image
;
3992 if (status
== Ok
&& bitmap
->format
== PixelFormat32bppARGB
)
3994 /* WIC supports bmp files with alpha, but gdiplus does not */
3995 bitmap
->format
= PixelFormat32bppRGB
;
4001 static GpStatus
decode_image_jpeg(IStream
* stream
, GpImage
**image
)
4003 return decode_image_wic(stream
, &GUID_ContainerFormatJpeg
, NULL
, image
);
4006 static BOOL
has_png_transparency_chunk(IStream
*pIStream
)
4009 BOOL has_tRNS
= FALSE
;
4016 ULARGE_INTEGER chunk_start
;
4017 ULONG bytesread
, chunk_size
;
4019 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, &chunk_start
);
4020 if (FAILED(hr
)) break;
4022 hr
= IStream_Read(pIStream
, header
, 8, &bytesread
);
4023 if (FAILED(hr
) || bytesread
< 8) break;
4025 chunk_size
= (header
[0] << 24) | (header
[1] << 16) | (header
[2] << 8) | header
[3];
4026 if (!memcmp(&header
[4], "tRNS", 4))
4032 seek
.QuadPart
= chunk_start
.QuadPart
+ chunk_size
+ 12; /* skip data and CRC */
4033 } while (memcmp(&header
[4], "IDAT", 4) && memcmp(&header
[4], "IEND", 4));
4035 TRACE("has_tRNS = %d\n", has_tRNS
);
4039 static GpStatus
decode_image_png(IStream
* stream
, GpImage
**image
)
4041 IWICBitmapDecoder
*decoder
;
4042 IWICBitmapFrameDecode
*frame
;
4046 BOOL force_conversion
= FALSE
;
4048 status
= initialize_decoder_wic(stream
, &GUID_ContainerFormatPng
, &decoder
);
4052 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
4055 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &format
);
4058 if (IsEqualGUID(&format
, &GUID_WICPixelFormat8bppGray
))
4059 force_conversion
= TRUE
;
4060 else if ((IsEqualGUID(&format
, &GUID_WICPixelFormat8bppIndexed
) ||
4061 IsEqualGUID(&format
, &GUID_WICPixelFormat4bppIndexed
) ||
4062 IsEqualGUID(&format
, &GUID_WICPixelFormat2bppIndexed
) ||
4063 IsEqualGUID(&format
, &GUID_WICPixelFormat1bppIndexed
) ||
4064 IsEqualGUID(&format
, &GUID_WICPixelFormat24bppBGR
)) &&
4065 has_png_transparency_chunk(stream
))
4066 force_conversion
= TRUE
;
4068 status
= decode_frame_wic(decoder
, force_conversion
, 0, png_metadata_reader
, image
);
4071 status
= hresult_to_status(hr
);
4073 IWICBitmapFrameDecode_Release(frame
);
4076 status
= hresult_to_status(hr
);
4078 IWICBitmapDecoder_Release(decoder
);
4082 static GpStatus
decode_image_gif(IStream
* stream
, GpImage
**image
)
4084 IWICBitmapDecoder
*decoder
;
4089 status
= initialize_decoder_wic(stream
, &GUID_ContainerFormatGif
, &decoder
);
4093 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
4095 return hresult_to_status(hr
);
4097 status
= decode_frame_wic(decoder
, frame_count
> 1, 0, gif_metadata_reader
, image
);
4098 IWICBitmapDecoder_Release(decoder
);
4102 if(frame_count
> 1) {
4103 heap_free((*image
)->palette
);
4104 (*image
)->palette
= NULL
;
4109 static GpStatus
decode_image_tiff(IStream
* stream
, GpImage
**image
)
4111 return decode_image_wic(stream
, &GUID_ContainerFormatTiff
, NULL
, image
);
4114 static GpStatus
load_wmf(IStream
*stream
, GpMetafile
**metafile
)
4116 WmfPlaceableFileHeader pfh
;
4117 BOOL is_placeable
= FALSE
;
4126 hr
= IStream_Read(stream
, &mh
, sizeof(mh
), &size
);
4127 if (hr
!= S_OK
|| size
!= sizeof(mh
))
4128 return GenericError
;
4130 if (((WmfPlaceableFileHeader
*)&mh
)->Key
== WMF_PLACEABLE_KEY
)
4133 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4134 if (FAILED(hr
)) return hresult_to_status(hr
);
4136 hr
= IStream_Read(stream
, &pfh
, sizeof(pfh
), &size
);
4137 if (hr
!= S_OK
|| size
!= sizeof(pfh
))
4138 return GenericError
;
4140 hr
= IStream_Read(stream
, &mh
, sizeof(mh
), &size
);
4141 if (hr
!= S_OK
|| size
!= sizeof(mh
))
4142 return GenericError
;
4144 is_placeable
= TRUE
;
4147 seek
.QuadPart
= is_placeable
? sizeof(pfh
) : 0;
4148 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4149 if (FAILED(hr
)) return hresult_to_status(hr
);
4151 buf
= heap_alloc(mh
.mtSize
* 2);
4152 if (!buf
) return OutOfMemory
;
4154 hr
= IStream_Read(stream
, buf
, mh
.mtSize
* 2, &size
);
4155 if (hr
!= S_OK
|| size
!= mh
.mtSize
* 2)
4158 return GenericError
;
4161 hmf
= SetMetaFileBitsEx(mh
.mtSize
* 2, buf
);
4164 return GenericError
;
4166 status
= GdipCreateMetafileFromWmf(hmf
, TRUE
, is_placeable
? &pfh
: NULL
, metafile
);
4168 DeleteMetaFile(hmf
);
4172 static GpStatus
decode_image_wmf(IStream
*stream
, GpImage
**image
)
4174 GpMetafile
*metafile
;
4177 TRACE("%p %p\n", stream
, image
);
4179 if (!stream
|| !image
)
4180 return InvalidParameter
;
4182 status
= load_wmf(stream
, &metafile
);
4185 TRACE("Could not load metafile\n");
4189 *image
= (GpImage
*)metafile
;
4190 TRACE("<-- %p\n", *image
);
4195 static GpStatus
load_emf(IStream
*stream
, GpMetafile
**metafile
)
4205 hr
= IStream_Read(stream
, &emh
, sizeof(emh
), &size
);
4206 if (hr
!= S_OK
|| size
!= sizeof(emh
) || emh
.dSignature
!= ENHMETA_SIGNATURE
)
4207 return GenericError
;
4210 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4211 if (FAILED(hr
)) return hresult_to_status(hr
);
4213 buf
= heap_alloc(emh
.nBytes
);
4214 if (!buf
) return OutOfMemory
;
4216 hr
= IStream_Read(stream
, buf
, emh
.nBytes
, &size
);
4217 if (hr
!= S_OK
|| size
!= emh
.nBytes
)
4220 return GenericError
;
4223 hemf
= SetEnhMetaFileBits(emh
.nBytes
, buf
);
4226 return GenericError
;
4228 status
= GdipCreateMetafileFromEmf(hemf
, TRUE
, metafile
);
4230 DeleteEnhMetaFile(hemf
);
4234 static GpStatus
decode_image_emf(IStream
*stream
, GpImage
**image
)
4236 GpMetafile
*metafile
;
4239 TRACE("%p %p\n", stream
, image
);
4241 if (!stream
|| !image
)
4242 return InvalidParameter
;
4244 status
= load_emf(stream
, &metafile
);
4247 TRACE("Could not load metafile\n");
4251 *image
= (GpImage
*)metafile
;
4252 TRACE("<-- %p\n", *image
);
4257 typedef GpStatus (*encode_image_func
)(GpImage
*image
, IStream
* stream
,
4258 GDIPCONST EncoderParameters
* params
);
4260 typedef GpStatus (*decode_image_func
)(IStream
*stream
, GpImage
**image
);
4262 typedef GpStatus (*select_image_func
)(GpImage
*image
, UINT active_frame
);
4264 typedef struct image_codec
{
4265 ImageCodecInfo info
;
4266 encode_image_func encode_func
;
4267 decode_image_func decode_func
;
4268 select_image_func select_func
;
4283 static const struct image_codec codecs
[NUM_CODECS
];
4285 static GpStatus
get_decoder_info(IStream
* stream
, const struct image_codec
**result
)
4288 const BYTE
*pattern
, *mask
;
4295 /* seek to the start of the stream */
4297 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4298 if (FAILED(hr
)) return hresult_to_status(hr
);
4300 /* read the first 8 bytes */
4301 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4302 hr
= IStream_Read(stream
, signature
, 8, &bytesread
);
4303 if (FAILED(hr
)) return hresult_to_status(hr
);
4304 if (hr
== S_FALSE
|| bytesread
== 0) return GenericError
;
4306 for (i
= 0; i
< NUM_CODECS
; i
++) {
4307 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
) &&
4308 bytesread
>= codecs
[i
].info
.SigSize
)
4310 for (sig
=0; sig
<codecs
[i
].info
.SigCount
; sig
++)
4312 pattern
= &codecs
[i
].info
.SigPattern
[codecs
[i
].info
.SigSize
*sig
];
4313 mask
= &codecs
[i
].info
.SigMask
[codecs
[i
].info
.SigSize
*sig
];
4314 for (j
=0; j
<codecs
[i
].info
.SigSize
; j
++)
4315 if ((signature
[j
] & mask
[j
]) != pattern
[j
])
4317 if (j
== codecs
[i
].info
.SigSize
)
4319 *result
= &codecs
[i
];
4326 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread
,
4327 signature
[0],signature
[1],signature
[2],signature
[3],
4328 signature
[4],signature
[5],signature
[6],signature
[7]);
4330 return GenericError
;
4333 static GpStatus
get_decoder_info_from_image(GpImage
*image
, const struct image_codec
**result
)
4337 for (i
= 0; i
< NUM_CODECS
; i
++) {
4338 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
) &&
4339 IsEqualIID(&codecs
[i
].info
.FormatID
, &image
->format
))
4341 *result
= &codecs
[i
];
4346 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image
->format
));
4347 return GenericError
;
4350 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
, GDIPCONST GUID
*dimensionID
,
4354 const struct image_codec
*codec
= NULL
;
4357 TRACE("(%p,%s,%u)\n", image
, debugstr_guid(dimensionID
), frame
);
4359 if (!image
|| !dimensionID
)
4360 return InvalidParameter
;
4361 if(!image_lock(image
, &unlock
))
4364 if (frame
>= image
->frame_count
)
4366 WARN("requested frame %u, but image has only %u\n", frame
, image
->frame_count
);
4367 image_unlock(image
, unlock
);
4368 return InvalidParameter
;
4371 if (image
->type
!= ImageTypeBitmap
&& image
->type
!= ImageTypeMetafile
)
4373 WARN("invalid image type %d\n", image
->type
);
4374 image_unlock(image
, unlock
);
4375 return InvalidParameter
;
4378 if (image
->current_frame
== frame
)
4380 image_unlock(image
, unlock
);
4384 if (!image
->decoder
)
4386 TRACE("image doesn't have an associated decoder\n");
4387 image_unlock(image
, unlock
);
4391 /* choose an appropriate image decoder */
4392 stat
= get_decoder_info_from_image(image
, &codec
);
4395 WARN("can't find decoder info\n");
4396 image_unlock(image
, unlock
);
4400 stat
= codec
->select_func(image
, frame
);
4401 image_unlock(image
, unlock
);
4405 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
*stream
, GpImage
**image
)
4410 const struct image_codec
*codec
=NULL
;
4412 TRACE("%p %p\n", stream
, image
);
4414 if (!stream
|| !image
)
4415 return InvalidParameter
;
4417 /* choose an appropriate image decoder */
4418 stat
= get_decoder_info(stream
, &codec
);
4419 if (stat
!= Ok
) return stat
;
4421 /* seek to the start of the stream */
4423 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4424 if (FAILED(hr
)) return hresult_to_status(hr
);
4426 /* call on the image decoder to do the real work */
4427 stat
= codec
->decode_func(stream
, image
);
4429 /* take note of the original data format */
4432 memcpy(&(*image
)->format
, &codec
->info
.FormatID
, sizeof(GUID
));
4440 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
4442 TRACE("%p %p\n", stream
, image
);
4444 return GdipLoadImageFromStream(stream
, image
);
4447 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
4451 TRACE("(%p,%u)\n", image
, propId
);
4454 return InvalidParameter
;
4457 FIXME("not implemented\n");
4459 return NotImplemented
;
4462 GpStatus WINGDIPAPI
GdipSetPropertyItem(GpImage
*image
, GDIPCONST PropertyItem
* item
)
4466 if (!image
|| !item
) return InvalidParameter
;
4468 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image
, item
, item
->id
, item
->type
, item
->length
, item
->value
);
4471 FIXME("not implemented\n");
4476 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
4477 GDIPCONST CLSID
*clsidEncoder
,
4478 GDIPCONST EncoderParameters
*encoderParams
)
4483 TRACE("%p (%s) %p %p\n", image
, debugstr_w(filename
), clsidEncoder
, encoderParams
);
4485 if (!image
|| !filename
|| !clsidEncoder
)
4486 return InvalidParameter
;
4488 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
4490 return GenericError
;
4492 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
4494 IStream_Release(stream
);
4498 /*************************************************************************
4499 * Encoding functions -
4500 * These functions encode an image in different image file formats.
4503 static GpStatus
encode_image_wic(GpImage
*image
, IStream
* stream
,
4504 REFGUID container
, GDIPCONST EncoderParameters
* params
)
4508 IWICImagingFactory
*factory
;
4509 IWICBitmapEncoder
*encoder
;
4510 IWICBitmapFrameEncode
*frameencode
;
4511 IPropertyBag2
*encoderoptions
;
4514 PixelFormat gdipformat
=0;
4515 const WICPixelFormatGUID
*desired_wicformat
;
4516 WICPixelFormatGUID wicformat
;
4518 BitmapData lockeddata
;
4521 if (image
->type
!= ImageTypeBitmap
)
4522 return GenericError
;
4524 bitmap
= (GpBitmap
*)image
;
4526 GdipGetImageWidth(image
, &width
);
4527 GdipGetImageHeight(image
, &height
);
4534 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
4536 return hresult_to_status(hr
);
4537 hr
= IWICImagingFactory_CreateEncoder(factory
, container
, NULL
, &encoder
);
4538 IWICImagingFactory_Release(factory
);
4540 return hresult_to_status(hr
);
4542 hr
= IWICBitmapEncoder_Initialize(encoder
, stream
, WICBitmapEncoderNoCache
);
4546 hr
= IWICBitmapEncoder_CreateNewFrame(encoder
, &frameencode
, &encoderoptions
);
4549 if (SUCCEEDED(hr
)) /* created frame */
4551 hr
= IWICBitmapFrameEncode_Initialize(frameencode
, encoderoptions
);
4554 hr
= IWICBitmapFrameEncode_SetSize(frameencode
, width
, height
);
4557 hr
= IWICBitmapFrameEncode_SetResolution(frameencode
, image
->xres
, image
->yres
);
4561 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
4563 if (pixel_formats
[i
].gdip_format
== bitmap
->format
)
4565 desired_wicformat
= pixel_formats
[i
].wic_format
;
4566 gdipformat
= bitmap
->format
;
4572 desired_wicformat
= &GUID_WICPixelFormat32bppBGRA
;
4573 gdipformat
= PixelFormat32bppARGB
;
4576 memcpy(&wicformat
, desired_wicformat
, sizeof(GUID
));
4577 hr
= IWICBitmapFrameEncode_SetPixelFormat(frameencode
, &wicformat
);
4580 if (SUCCEEDED(hr
) && !IsEqualGUID(desired_wicformat
, &wicformat
))
4582 /* Encoder doesn't support this bitmap's format. */
4584 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
4586 if (IsEqualGUID(&wicformat
, pixel_formats
[i
].wic_format
))
4588 gdipformat
= pixel_formats
[i
].gdip_format
;
4594 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat
));
4601 stat
= GdipBitmapLockBits(bitmap
, &rc
, ImageLockModeRead
, gdipformat
,
4606 UINT row_size
= (lockeddata
.Width
* PIXELFORMATBPP(gdipformat
) + 7)/8;
4609 /* write one row at a time in case stride is negative */
4610 row
= lockeddata
.Scan0
;
4611 for (i
=0; i
<lockeddata
.Height
; i
++)
4613 hr
= IWICBitmapFrameEncode_WritePixels(frameencode
, 1, row_size
, row_size
, row
);
4614 if (FAILED(hr
)) break;
4615 row
+= lockeddata
.Stride
;
4618 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
4625 hr
= IWICBitmapFrameEncode_Commit(frameencode
);
4627 IWICBitmapFrameEncode_Release(frameencode
);
4628 IPropertyBag2_Release(encoderoptions
);
4632 hr
= IWICBitmapEncoder_Commit(encoder
);
4634 IWICBitmapEncoder_Release(encoder
);
4635 return hresult_to_status(hr
);
4638 static GpStatus
encode_image_BMP(GpImage
*image
, IStream
* stream
,
4639 GDIPCONST EncoderParameters
* params
)
4641 return encode_image_wic(image
, stream
, &GUID_ContainerFormatBmp
, params
);
4644 static GpStatus
encode_image_tiff(GpImage
*image
, IStream
* stream
,
4645 GDIPCONST EncoderParameters
* params
)
4647 return encode_image_wic(image
, stream
, &GUID_ContainerFormatTiff
, params
);
4650 GpStatus
encode_image_png(GpImage
*image
, IStream
* stream
,
4651 GDIPCONST EncoderParameters
* params
)
4653 return encode_image_wic(image
, stream
, &GUID_ContainerFormatPng
, params
);
4656 static GpStatus
encode_image_jpeg(GpImage
*image
, IStream
* stream
,
4657 GDIPCONST EncoderParameters
* params
)
4659 return encode_image_wic(image
, stream
, &GUID_ContainerFormatJpeg
, params
);
4662 static GpStatus
encode_image_gif(GpImage
*image
, IStream
* stream
,
4663 GDIPCONST EncoderParameters
* params
)
4665 return encode_image_wic(image
, stream
, &GUID_ContainerFormatGif
, params
);
4668 /*****************************************************************************
4669 * GdipSaveImageToStream [GDIPLUS.@]
4671 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
4672 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
4675 encode_image_func encode_image
;
4678 TRACE("%p, %p, %s, %p\n", image
, stream
, wine_dbgstr_guid(clsid
), params
);
4680 if(!image
|| !stream
)
4681 return InvalidParameter
;
4683 /* select correct encoder */
4684 encode_image
= NULL
;
4685 for (i
= 0; i
< NUM_CODECS
; i
++) {
4686 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
) &&
4687 IsEqualCLSID(clsid
, &codecs
[i
].info
.Clsid
))
4688 encode_image
= codecs
[i
].encode_func
;
4690 if (encode_image
== NULL
)
4691 return UnknownImageFormat
;
4693 stat
= encode_image(image
, stream
, params
);
4698 /*****************************************************************************
4699 * GdipSaveAdd [GDIPLUS.@]
4701 GpStatus WINGDIPAPI
GdipSaveAdd(GpImage
*image
, GDIPCONST EncoderParameters
*params
)
4703 FIXME("(%p,%p): stub\n", image
, params
);
4707 /*****************************************************************************
4708 * GdipGetImagePalette [GDIPLUS.@]
4710 GpStatus WINGDIPAPI
GdipGetImagePalette(GpImage
*image
, ColorPalette
*palette
, INT size
)
4714 TRACE("(%p,%p,%i)\n", image
, palette
, size
);
4716 if (!image
|| !palette
)
4717 return InvalidParameter
;
4719 count
= image
->palette
? image
->palette
->Count
: 0;
4721 if (size
< (sizeof(UINT
)*2+sizeof(ARGB
)*count
))
4723 TRACE("<-- InsufficientBuffer\n");
4724 return InsufficientBuffer
;
4729 palette
->Flags
= image
->palette
->Flags
;
4730 palette
->Count
= image
->palette
->Count
;
4731 memcpy(palette
->Entries
, image
->palette
->Entries
, sizeof(ARGB
)*image
->palette
->Count
);
4741 /*****************************************************************************
4742 * GdipSetImagePalette [GDIPLUS.@]
4744 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
4745 GDIPCONST ColorPalette
*palette
)
4747 ColorPalette
*new_palette
;
4749 TRACE("(%p,%p)\n", image
, palette
);
4751 if(!image
|| !palette
|| palette
->Count
> 256)
4752 return InvalidParameter
;
4754 new_palette
= heap_alloc_zero(2 * sizeof(UINT
) + palette
->Count
* sizeof(ARGB
));
4755 if (!new_palette
) return OutOfMemory
;
4757 heap_free(image
->palette
);
4758 image
->palette
= new_palette
;
4759 image
->palette
->Flags
= palette
->Flags
;
4760 image
->palette
->Count
= palette
->Count
;
4761 memcpy(image
->palette
->Entries
, palette
->Entries
, sizeof(ARGB
)*palette
->Count
);
4766 /*************************************************************************
4768 * Structures that represent which formats we support for encoding.
4771 /* ImageCodecInfo creation routines taken from libgdiplus */
4772 static const WCHAR bmp_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4773 static const WCHAR bmp_extension
[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4774 static const WCHAR bmp_mimetype
[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4775 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0}; /* BMP */
4776 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
4777 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
4779 static const WCHAR jpeg_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4780 static const WCHAR jpeg_extension
[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4781 static const WCHAR jpeg_mimetype
[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4782 static const WCHAR jpeg_format
[] = {'J','P','E','G',0};
4783 static const BYTE jpeg_sig_pattern
[] = { 0xFF, 0xD8 };
4784 static const BYTE jpeg_sig_mask
[] = { 0xFF, 0xFF };
4786 static const WCHAR gif_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4787 static const WCHAR gif_extension
[] = {'*','.','G','I','F',0};
4788 static const WCHAR gif_mimetype
[] = {'i','m','a','g','e','/','g','i','f', 0};
4789 static const WCHAR gif_format
[] = {'G','I','F',0};
4790 static const BYTE gif_sig_pattern
[12] = "GIF87aGIF89a";
4791 static const BYTE gif_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4793 static const WCHAR tiff_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4794 static const WCHAR tiff_extension
[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4795 static const WCHAR tiff_mimetype
[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4796 static const WCHAR tiff_format
[] = {'T','I','F','F',0};
4797 static const BYTE tiff_sig_pattern
[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4798 static const BYTE tiff_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4800 static const WCHAR emf_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4801 static const WCHAR emf_extension
[] = {'*','.','E','M','F',0};
4802 static const WCHAR emf_mimetype
[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4803 static const WCHAR emf_format
[] = {'E','M','F',0};
4804 static const BYTE emf_sig_pattern
[] = { 0x01, 0x00, 0x00, 0x00 };
4805 static const BYTE emf_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4807 static const WCHAR wmf_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4808 static const WCHAR wmf_extension
[] = {'*','.','W','M','F',0};
4809 static const WCHAR wmf_mimetype
[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4810 static const WCHAR wmf_format
[] = {'W','M','F',0};
4811 static const BYTE wmf_sig_pattern
[] = { 0xd7, 0xcd };
4812 static const BYTE wmf_sig_mask
[] = { 0xFF, 0xFF };
4814 static const WCHAR png_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4815 static const WCHAR png_extension
[] = {'*','.','P','N','G',0};
4816 static const WCHAR png_mimetype
[] = {'i','m','a','g','e','/','p','n','g', 0};
4817 static const WCHAR png_format
[] = {'P','N','G',0};
4818 static const BYTE png_sig_pattern
[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4819 static const BYTE png_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4821 static const WCHAR ico_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4822 static const WCHAR ico_extension
[] = {'*','.','I','C','O',0};
4823 static const WCHAR ico_mimetype
[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4824 static const WCHAR ico_format
[] = {'I','C','O',0};
4825 static const BYTE ico_sig_pattern
[] = { 0x00, 0x00, 0x01, 0x00 };
4826 static const BYTE ico_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4828 static const struct image_codec codecs
[NUM_CODECS
] = {
4831 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4832 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4833 /* CodecName */ bmp_codecname
,
4835 /* FormatDescription */ bmp_format
,
4836 /* FilenameExtension */ bmp_extension
,
4837 /* MimeType */ bmp_mimetype
,
4838 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4842 /* SigPattern */ bmp_sig_pattern
,
4843 /* SigMask */ bmp_sig_mask
,
4851 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4852 /* FormatID */ { 0xb96b3caeU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4853 /* CodecName */ jpeg_codecname
,
4855 /* FormatDescription */ jpeg_format
,
4856 /* FilenameExtension */ jpeg_extension
,
4857 /* MimeType */ jpeg_mimetype
,
4858 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4862 /* SigPattern */ jpeg_sig_pattern
,
4863 /* SigMask */ jpeg_sig_mask
,
4871 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4872 /* FormatID */ { 0xb96b3cb0U
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4873 /* CodecName */ gif_codecname
,
4875 /* FormatDescription */ gif_format
,
4876 /* FilenameExtension */ gif_extension
,
4877 /* MimeType */ gif_mimetype
,
4878 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsEncoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4882 /* SigPattern */ gif_sig_pattern
,
4883 /* SigMask */ gif_sig_mask
,
4891 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4892 /* FormatID */ { 0xb96b3cb1U
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4893 /* CodecName */ tiff_codecname
,
4895 /* FormatDescription */ tiff_format
,
4896 /* FilenameExtension */ tiff_extension
,
4897 /* MimeType */ tiff_mimetype
,
4898 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsEncoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4902 /* SigPattern */ tiff_sig_pattern
,
4903 /* SigMask */ tiff_sig_mask
,
4911 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4912 /* FormatID */ { 0xb96b3cacU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4913 /* CodecName */ emf_codecname
,
4915 /* FormatDescription */ emf_format
,
4916 /* FilenameExtension */ emf_extension
,
4917 /* MimeType */ emf_mimetype
,
4918 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportVector
| ImageCodecFlagsBuiltin
,
4922 /* SigPattern */ emf_sig_pattern
,
4923 /* SigMask */ emf_sig_mask
,
4931 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4932 /* FormatID */ { 0xb96b3cadU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4933 /* CodecName */ wmf_codecname
,
4935 /* FormatDescription */ wmf_format
,
4936 /* FilenameExtension */ wmf_extension
,
4937 /* MimeType */ wmf_mimetype
,
4938 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportVector
| ImageCodecFlagsBuiltin
,
4942 /* SigPattern */ wmf_sig_pattern
,
4943 /* SigMask */ wmf_sig_mask
,
4951 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4952 /* FormatID */ { 0xb96b3cafU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4953 /* CodecName */ png_codecname
,
4955 /* FormatDescription */ png_format
,
4956 /* FilenameExtension */ png_extension
,
4957 /* MimeType */ png_mimetype
,
4958 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4962 /* SigPattern */ png_sig_pattern
,
4963 /* SigMask */ png_sig_mask
,
4971 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4972 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4973 /* CodecName */ ico_codecname
,
4975 /* FormatDescription */ ico_format
,
4976 /* FilenameExtension */ ico_extension
,
4977 /* MimeType */ ico_mimetype
,
4978 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4982 /* SigPattern */ ico_sig_pattern
,
4983 /* SigMask */ ico_sig_mask
,
4991 /*****************************************************************************
4992 * GdipGetImageDecodersSize [GDIPLUS.@]
4994 GpStatus WINGDIPAPI
GdipGetImageDecodersSize(UINT
*numDecoders
, UINT
*size
)
4996 int decoder_count
=0;
4998 TRACE("%p %p\n", numDecoders
, size
);
5000 if (!numDecoders
|| !size
)
5001 return InvalidParameter
;
5003 for (i
=0; i
<NUM_CODECS
; i
++)
5005 if (codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
)
5009 *numDecoders
= decoder_count
;
5010 *size
= decoder_count
* sizeof(ImageCodecInfo
);
5015 /*****************************************************************************
5016 * GdipGetImageDecoders [GDIPLUS.@]
5018 GpStatus WINGDIPAPI
GdipGetImageDecoders(UINT numDecoders
, UINT size
, ImageCodecInfo
*decoders
)
5020 int i
, decoder_count
=0;
5021 TRACE("%u %u %p\n", numDecoders
, size
, decoders
);
5024 size
!= numDecoders
* sizeof(ImageCodecInfo
))
5025 return GenericError
;
5027 for (i
=0; i
<NUM_CODECS
; i
++)
5029 if (codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
)
5031 if (decoder_count
== numDecoders
) return GenericError
;
5032 memcpy(&decoders
[decoder_count
], &codecs
[i
].info
, sizeof(ImageCodecInfo
));
5037 if (decoder_count
< numDecoders
) return GenericError
;
5042 /*****************************************************************************
5043 * GdipGetImageEncodersSize [GDIPLUS.@]
5045 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
5047 int encoder_count
=0;
5049 TRACE("%p %p\n", numEncoders
, size
);
5051 if (!numEncoders
|| !size
)
5052 return InvalidParameter
;
5054 for (i
=0; i
<NUM_CODECS
; i
++)
5056 if (codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
)
5060 *numEncoders
= encoder_count
;
5061 *size
= encoder_count
* sizeof(ImageCodecInfo
);
5066 /*****************************************************************************
5067 * GdipGetImageEncoders [GDIPLUS.@]
5069 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
5071 int i
, encoder_count
=0;
5072 TRACE("%u %u %p\n", numEncoders
, size
, encoders
);
5075 size
!= numEncoders
* sizeof(ImageCodecInfo
))
5076 return GenericError
;
5078 for (i
=0; i
<NUM_CODECS
; i
++)
5080 if (codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
)
5082 if (encoder_count
== numEncoders
) return GenericError
;
5083 memcpy(&encoders
[encoder_count
], &codecs
[i
].info
, sizeof(ImageCodecInfo
));
5088 if (encoder_count
< numEncoders
) return GenericError
;
5093 GpStatus WINGDIPAPI
GdipGetEncoderParameterListSize(GpImage
*image
,
5094 GDIPCONST CLSID
* clsidEncoder
, UINT
*size
)
5098 TRACE("(%p,%s,%p)\n", image
, debugstr_guid(clsidEncoder
), size
);
5101 FIXME("not implemented\n");
5105 return NotImplemented
;
5108 static PixelFormat
get_16bpp_format(HBITMAP hbm
)
5114 hdc
= CreateCompatibleDC(NULL
);
5116 memset(&bmh
, 0, sizeof(bmh
));
5117 bmh
.bV4Size
= sizeof(bmh
);
5120 bmh
.bV4V4Compression
= BI_BITFIELDS
;
5121 bmh
.bV4BitCount
= 16;
5123 GetDIBits(hdc
, hbm
, 0, 0, NULL
, (BITMAPINFO
*)&bmh
, DIB_RGB_COLORS
);
5125 if (bmh
.bV4RedMask
== 0x7c00 &&
5126 bmh
.bV4GreenMask
== 0x3e0 &&
5127 bmh
.bV4BlueMask
== 0x1f)
5129 result
= PixelFormat16bppRGB555
;
5131 else if (bmh
.bV4RedMask
== 0xf800 &&
5132 bmh
.bV4GreenMask
== 0x7e0 &&
5133 bmh
.bV4BlueMask
== 0x1f)
5135 result
= PixelFormat16bppRGB565
;
5139 FIXME("unrecognized bitfields %x,%x,%x\n", bmh
.bV4RedMask
,
5140 bmh
.bV4GreenMask
, bmh
.bV4BlueMask
);
5141 result
= PixelFormatUndefined
;
5149 /*****************************************************************************
5150 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
5152 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
5157 BitmapData lockeddata
;
5159 TRACE("%p %p %p\n", hbm
, hpal
, bitmap
);
5162 return InvalidParameter
;
5164 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
5165 return InvalidParameter
;
5167 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
5168 switch(bm
.bmBitsPixel
) {
5170 format
= PixelFormat1bppIndexed
;
5173 format
= PixelFormat4bppIndexed
;
5176 format
= PixelFormat8bppIndexed
;
5179 format
= get_16bpp_format(hbm
);
5180 if (format
== PixelFormatUndefined
)
5181 return InvalidParameter
;
5184 format
= PixelFormat24bppRGB
;
5187 format
= PixelFormat32bppRGB
;
5190 format
= PixelFormat48bppRGB
;
5193 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
5194 return InvalidParameter
;
5197 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, 0,
5198 format
, NULL
, bitmap
);
5202 retval
= GdipBitmapLockBits(*bitmap
, NULL
, ImageLockModeWrite
,
5203 format
, &lockeddata
);
5207 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
) + 256 * sizeof(RGBQUAD
)];
5208 BITMAPINFO
*pbmi
= (BITMAPINFO
*)bmibuf
;
5211 hdc
= CreateCompatibleDC(NULL
);
5213 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
5214 pbmi
->bmiHeader
.biBitCount
= 0;
5216 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
5218 src_height
= abs(pbmi
->bmiHeader
.biHeight
);
5219 pbmi
->bmiHeader
.biHeight
= -src_height
;
5221 GetDIBits(hdc
, hbm
, 0, src_height
, lockeddata
.Scan0
, pbmi
, DIB_RGB_COLORS
);
5225 GdipBitmapUnlockBits(*bitmap
, &lockeddata
);
5228 if (retval
== Ok
&& hpal
)
5230 PALETTEENTRY entry
[256];
5231 ColorPalette
*palette
=NULL
;
5232 int i
, num_palette_entries
;
5234 num_palette_entries
= GetPaletteEntries(hpal
, 0, 256, entry
);
5235 if (!num_palette_entries
)
5236 retval
= GenericError
;
5238 palette
= heap_alloc_zero(sizeof(ColorPalette
) + sizeof(ARGB
) * (num_palette_entries
-1));
5240 retval
= OutOfMemory
;
5245 palette
->Count
= num_palette_entries
;
5247 for (i
=0; i
<num_palette_entries
; i
++)
5249 palette
->Entries
[i
] = 0xff000000 | entry
[i
].peRed
<< 16 |
5250 entry
[i
].peGreen
<< 8 | entry
[i
].peBlue
;
5253 retval
= GdipSetImagePalette(&(*bitmap
)->image
, palette
);
5261 GdipDisposeImage(&(*bitmap
)->image
);
5269 /*****************************************************************************
5270 * GdipCreateEffect [GDIPLUS.@]
5272 GpStatus WINGDIPAPI
GdipCreateEffect(const GUID guid
, CGpEffect
**effect
)
5274 FIXME("(%s, %p): stub\n", debugstr_guid(&guid
), effect
);
5277 return InvalidParameter
;
5281 return NotImplemented
;
5284 /*****************************************************************************
5285 * GdipDeleteEffect [GDIPLUS.@]
5287 GpStatus WINGDIPAPI
GdipDeleteEffect(CGpEffect
*effect
)
5289 FIXME("(%p): stub\n", effect
);
5290 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5291 * in Windows's gdiplus */
5292 return NotImplemented
;
5295 /*****************************************************************************
5296 * GdipSetEffectParameters [GDIPLUS.@]
5298 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
5299 const VOID
*params
, const UINT size
)
5303 TRACE("(%p,%p,%u)\n", effect
, params
, size
);
5306 FIXME("not implemented\n");
5308 return NotImplemented
;
5311 /*****************************************************************************
5312 * GdipGetImageFlags [GDIPLUS.@]
5314 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
5316 TRACE("%p %p\n", image
, flags
);
5318 if(!image
|| !flags
)
5319 return InvalidParameter
;
5321 *flags
= image
->flags
;
5326 GpStatus WINGDIPAPI
GdipTestControl(GpTestControlEnum control
, void *param
)
5328 TRACE("(%d, %p)\n", control
, param
);
5331 case TestControlForceBilinear
:
5333 FIXME("TestControlForceBilinear not handled\n");
5335 case TestControlNoICM
:
5337 FIXME("TestControlNoICM not handled\n");
5339 case TestControlGetBuildNumber
:
5340 *((DWORD
*)param
) = 3102;
5347 GpStatus WINGDIPAPI
GdipImageForceValidation(GpImage
*image
)
5349 TRACE("%p\n", image
);
5354 /*****************************************************************************
5355 * GdipGetImageThumbnail [GDIPLUS.@]
5357 GpStatus WINGDIPAPI
GdipGetImageThumbnail(GpImage
*image
, UINT width
, UINT height
,
5358 GpImage
**ret_image
, GetThumbnailImageAbort cb
,
5362 GpGraphics
*graphics
;
5363 UINT srcwidth
, srcheight
;
5365 TRACE("(%p %u %u %p %p %p)\n",
5366 image
, width
, height
, ret_image
, cb
, cb_data
);
5368 if (!image
|| !ret_image
)
5369 return InvalidParameter
;
5371 if (!width
) width
= 120;
5372 if (!height
) height
= 120;
5374 GdipGetImageWidth(image
, &srcwidth
);
5375 GdipGetImageHeight(image
, &srcheight
);
5377 stat
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat32bppPARGB
,
5378 NULL
, (GpBitmap
**)ret_image
);
5382 stat
= GdipGetImageGraphicsContext(*ret_image
, &graphics
);
5386 stat
= GdipDrawImageRectRectI(graphics
, image
,
5387 0, 0, width
, height
, 0, 0, srcwidth
, srcheight
, UnitPixel
,
5390 GdipDeleteGraphics(graphics
);
5395 GdipDisposeImage(*ret_image
);
5403 /*****************************************************************************
5404 * GdipImageRotateFlip [GDIPLUS.@]
5406 GpStatus WINGDIPAPI
GdipImageRotateFlip(GpImage
*image
, RotateFlipType type
)
5408 GpBitmap
*new_bitmap
;
5410 int bpp
, bytesperpixel
;
5411 BOOL rotate_90
, flip_x
, flip_y
;
5412 int src_x_offset
, src_y_offset
;
5414 UINT x
, y
, width
, height
;
5415 BitmapData src_lock
, dst_lock
;
5419 TRACE("(%p, %u)\n", image
, type
);
5422 return InvalidParameter
;
5423 if (!image_lock(image
, &unlock
))
5427 flip_x
= (type
&6) == 2 || (type
&6) == 4;
5428 flip_y
= (type
&3) == 1 || (type
&3) == 2;
5430 if (image
->type
!= ImageTypeBitmap
)
5432 FIXME("Not implemented for type %i\n", image
->type
);
5433 image_unlock(image
, unlock
);
5434 return NotImplemented
;
5437 bitmap
= (GpBitmap
*)image
;
5438 bpp
= PIXELFORMATBPP(bitmap
->format
);
5442 FIXME("Not implemented for %i bit images\n", bpp
);
5443 image_unlock(image
, unlock
);
5444 return NotImplemented
;
5449 width
= bitmap
->height
;
5450 height
= bitmap
->width
;
5454 width
= bitmap
->width
;
5455 height
= bitmap
->height
;
5458 bytesperpixel
= bpp
/8;
5460 stat
= GdipCreateBitmapFromScan0(width
, height
, 0, bitmap
->format
, NULL
, &new_bitmap
);
5464 image_unlock(image
, unlock
);
5468 stat
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
, bitmap
->format
, &src_lock
);
5472 stat
= GdipBitmapLockBits(new_bitmap
, NULL
, ImageLockModeWrite
, bitmap
->format
, &dst_lock
);
5476 LPBYTE src_row
, src_pixel
;
5477 LPBYTE dst_row
, dst_pixel
;
5479 src_origin
= src_lock
.Scan0
;
5480 if (flip_x
) src_origin
+= bytesperpixel
* (bitmap
->width
- 1);
5481 if (flip_y
) src_origin
+= src_lock
.Stride
* (bitmap
->height
- 1);
5485 if (flip_y
) src_x_offset
= -src_lock
.Stride
;
5486 else src_x_offset
= src_lock
.Stride
;
5487 if (flip_x
) src_y_offset
= -bytesperpixel
;
5488 else src_y_offset
= bytesperpixel
;
5492 if (flip_x
) src_x_offset
= -bytesperpixel
;
5493 else src_x_offset
= bytesperpixel
;
5494 if (flip_y
) src_y_offset
= -src_lock
.Stride
;
5495 else src_y_offset
= src_lock
.Stride
;
5498 src_row
= src_origin
;
5499 dst_row
= dst_lock
.Scan0
;
5500 for (y
=0; y
<height
; y
++)
5502 src_pixel
= src_row
;
5503 dst_pixel
= dst_row
;
5504 for (x
=0; x
<width
; x
++)
5506 /* FIXME: This could probably be faster without memcpy. */
5507 memcpy(dst_pixel
, src_pixel
, bytesperpixel
);
5508 dst_pixel
+= bytesperpixel
;
5509 src_pixel
+= src_x_offset
;
5511 src_row
+= src_y_offset
;
5512 dst_row
+= dst_lock
.Stride
;
5515 GdipBitmapUnlockBits(new_bitmap
, &dst_lock
);
5518 GdipBitmapUnlockBits(bitmap
, &src_lock
);
5522 move_bitmap(bitmap
, new_bitmap
, FALSE
);
5524 GdipDisposeImage(&new_bitmap
->image
);
5526 image_unlock(image
, unlock
);
5530 /*****************************************************************************
5531 * GdipImageSetAbort [GDIPLUS.@]
5533 GpStatus WINGDIPAPI
GdipImageSetAbort(GpImage
*image
, GdiplusAbort
*pabort
)
5535 TRACE("(%p, %p)\n", image
, pabort
);
5538 return InvalidParameter
;
5541 FIXME("Abort callback is not supported.\n");
5546 /*****************************************************************************
5547 * GdipBitmapConvertFormat [GDIPLUS.@]
5549 GpStatus WINGDIPAPI
GdipBitmapConvertFormat(GpBitmap
*bitmap
, PixelFormat format
, DitherType dithertype
,
5550 PaletteType palettetype
, ColorPalette
*palette
, REAL alphathreshold
)
5552 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap
, format
, dithertype
, palettetype
, palette
, alphathreshold
);
5553 return NotImplemented
;
5556 static void set_histogram_point_argb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5558 ch0
[ color
>> 24 ]++;
5559 ch1
[(color
>> 16) & 0xff]++;
5560 ch2
[(color
>> 8) & 0xff]++;
5561 ch3
[ color
& 0xff]++;
5564 static void set_histogram_point_pargb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5566 BYTE alpha
= color
>> 24;
5569 ch1
[(((color
>> 16) & 0xff) * alpha
) / 0xff]++;
5570 ch2
[(((color
>> 8) & 0xff) * alpha
) / 0xff]++;
5571 ch3
[(( color
& 0xff) * alpha
) / 0xff]++;
5574 static void set_histogram_point_rgb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5576 ch0
[(color
>> 16) & 0xff]++;
5577 ch1
[(color
>> 8) & 0xff]++;
5578 ch2
[ color
& 0xff]++;
5581 static void set_histogram_point_gray(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5583 ch0
[(76 * ((color
>> 16) & 0xff) + 150 * ((color
>> 8) & 0xff) + 29 * (color
& 0xff)) / 0xff]++;
5586 static void set_histogram_point_b(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5588 ch0
[color
& 0xff]++;
5591 static void set_histogram_point_g(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5593 ch0
[(color
>> 8) & 0xff]++;
5596 static void set_histogram_point_r(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5598 ch0
[(color
>> 16) & 0xff]++;
5601 static void set_histogram_point_a(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5603 ch0
[(color
>> 24) & 0xff]++;
5606 /*****************************************************************************
5607 * GdipBitmapGetHistogram [GDIPLUS.@]
5609 GpStatus WINGDIPAPI
GdipBitmapGetHistogram(GpBitmap
*bitmap
, HistogramFormat format
, UINT num_of_entries
,
5610 UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5612 static void (* const set_histogram_point
[])(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
) =
5614 set_histogram_point_argb
,
5615 set_histogram_point_pargb
,
5616 set_histogram_point_rgb
,
5617 set_histogram_point_gray
,
5618 set_histogram_point_b
,
5619 set_histogram_point_g
,
5620 set_histogram_point_r
,
5621 set_histogram_point_a
,
5623 UINT width
, height
, x
, y
;
5625 TRACE("(%p, %d, %u, %p, %p, %p, %p)\n", bitmap
, format
, num_of_entries
,
5626 ch0
, ch1
, ch2
, ch3
);
5628 if (!bitmap
|| num_of_entries
!= 256)
5629 return InvalidParameter
;
5631 /* Make sure passed channel pointers match requested format */
5634 case HistogramFormatARGB
:
5635 case HistogramFormatPARGB
:
5636 if (!ch0
|| !ch1
|| !ch2
|| !ch3
)
5637 return InvalidParameter
;
5638 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5639 memset(ch1
, 0, num_of_entries
* sizeof(UINT
));
5640 memset(ch2
, 0, num_of_entries
* sizeof(UINT
));
5641 memset(ch3
, 0, num_of_entries
* sizeof(UINT
));
5643 case HistogramFormatRGB
:
5644 if (!ch0
|| !ch1
|| !ch2
|| ch3
)
5645 return InvalidParameter
;
5646 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5647 memset(ch1
, 0, num_of_entries
* sizeof(UINT
));
5648 memset(ch2
, 0, num_of_entries
* sizeof(UINT
));
5650 case HistogramFormatGray
:
5651 case HistogramFormatB
:
5652 case HistogramFormatG
:
5653 case HistogramFormatR
:
5654 case HistogramFormatA
:
5655 if (!ch0
|| ch1
|| ch2
|| ch3
)
5656 return InvalidParameter
;
5657 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5660 WARN("Invalid histogram format requested, %d\n", format
);
5661 return InvalidParameter
;
5664 GdipGetImageWidth(&bitmap
->image
, &width
);
5665 GdipGetImageHeight(&bitmap
->image
, &height
);
5667 for (y
= 0; y
< height
; y
++)
5668 for (x
= 0; x
< width
; x
++)
5672 GdipBitmapGetPixel(bitmap
, x
, y
, &color
);
5673 set_histogram_point
[format
](color
, ch0
, ch1
, ch2
, ch3
);
5679 /*****************************************************************************
5680 * GdipBitmapGetHistogramSize [GDIPLUS.@]
5682 GpStatus WINGDIPAPI
GdipBitmapGetHistogramSize(HistogramFormat format
, UINT
*num_of_entries
)
5684 TRACE("(%d, %p)\n", format
, num_of_entries
);
5686 if (!num_of_entries
)
5687 return InvalidParameter
;
5689 *num_of_entries
= 256;
5693 static GpStatus
create_optimal_palette(ColorPalette
*palette
, INT desired
,
5694 BOOL transparent
, GpBitmap
*bitmap
)
5699 IWICImagingFactory
*factory
;
5700 IWICPalette
*wic_palette
;
5702 if (!bitmap
) return InvalidParameter
;
5703 if (palette
->Count
< desired
) return GenericError
;
5705 status
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
, PixelFormat24bppRGB
, &data
);
5706 if (status
!= Ok
) return status
;
5708 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
5711 GdipBitmapUnlockBits(bitmap
, &data
);
5712 return hresult_to_status(hr
);
5715 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
5720 /* PixelFormat24bppRGB actually stores the bitmap bits as BGR. */
5721 hr
= IWICImagingFactory_CreateBitmapFromMemory(factory
, data
.Width
, data
.Height
,
5722 &GUID_WICPixelFormat24bppBGR
, data
.Stride
, data
.Stride
* data
.Width
, data
.Scan0
, &bitmap
);
5725 hr
= IWICPalette_InitializeFromBitmap(wic_palette
, (IWICBitmapSource
*)bitmap
, desired
, transparent
);
5729 IWICPalette_GetColorCount(wic_palette
, &palette
->Count
);
5730 IWICPalette_GetColors(wic_palette
, palette
->Count
, palette
->Entries
, &palette
->Count
);
5733 IWICBitmap_Release(bitmap
);
5736 IWICPalette_Release(wic_palette
);
5739 IWICImagingFactory_Release(factory
);
5740 GdipBitmapUnlockBits(bitmap
, &data
);
5742 return hresult_to_status(hr
);
5745 /*****************************************************************************
5746 * GdipInitializePalette [GDIPLUS.@]
5748 GpStatus WINGDIPAPI
GdipInitializePalette(ColorPalette
*palette
,
5749 PaletteType type
, INT desired
, BOOL transparent
, GpBitmap
*bitmap
)
5751 TRACE("(%p,%d,%d,%d,%p)\n", palette
, type
, desired
, transparent
, bitmap
);
5753 if (!palette
) return InvalidParameter
;
5757 case PaletteTypeCustom
:
5760 case PaletteTypeOptimal
:
5761 return create_optimal_palette(palette
, desired
, transparent
, bitmap
);
5763 /* WIC palette type enumeration matches these gdiplus enums */
5764 case PaletteTypeFixedBW
:
5765 case PaletteTypeFixedHalftone8
:
5766 case PaletteTypeFixedHalftone27
:
5767 case PaletteTypeFixedHalftone64
:
5768 case PaletteTypeFixedHalftone125
:
5769 case PaletteTypeFixedHalftone216
:
5770 case PaletteTypeFixedHalftone252
:
5771 case PaletteTypeFixedHalftone256
:
5773 ColorPalette
*wic_palette
;
5774 GpStatus status
= Ok
;
5776 wic_palette
= get_palette(NULL
, type
);
5777 if (!wic_palette
) return OutOfMemory
;
5779 if (palette
->Count
>= wic_palette
->Count
)
5781 palette
->Flags
= wic_palette
->Flags
;
5782 palette
->Count
= wic_palette
->Count
;
5783 memcpy(palette
->Entries
, wic_palette
->Entries
, wic_palette
->Count
* sizeof(wic_palette
->Entries
[0]));
5786 status
= GenericError
;
5788 heap_free(wic_palette
);
5794 FIXME("unknown palette type %d\n", type
);
5798 return InvalidParameter
;