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_WICPixelFormat8bppGray
, PixelFormat8bppIndexed
, WICBitmapPaletteTypeFixedGray256
},
59 { &GUID_WICPixelFormat8bppIndexed
, PixelFormat8bppIndexed
, WICBitmapPaletteTypeFixedHalftone256
},
60 { &GUID_WICPixelFormat16bppBGR555
, PixelFormat16bppRGB555
, WICBitmapPaletteTypeFixedHalftone256
},
61 { &GUID_WICPixelFormat24bppBGR
, PixelFormat24bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
62 { &GUID_WICPixelFormat32bppBGR
, PixelFormat32bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
63 { &GUID_WICPixelFormat32bppBGRA
, PixelFormat32bppARGB
, WICBitmapPaletteTypeFixedHalftone256
},
64 { &GUID_WICPixelFormat32bppPBGRA
, PixelFormat32bppPARGB
, WICBitmapPaletteTypeFixedHalftone256
},
68 static ColorPalette
*get_palette(IWICBitmapFrameDecode
*frame
, WICBitmapPaletteType palette_type
)
71 IWICImagingFactory
*factory
;
72 IWICPalette
*wic_palette
;
73 ColorPalette
*palette
= NULL
;
75 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
76 if (hr
!= S_OK
) return NULL
;
78 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
81 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
83 hr
= IWICBitmapFrameDecode_CopyPalette(frame
, wic_palette
);
86 TRACE("using predefined palette %#x\n", palette_type
);
87 hr
= IWICPalette_InitializePredefined(wic_palette
, palette_type
, FALSE
);
91 WICBitmapPaletteType type
;
95 IWICPalette_GetColorCount(wic_palette
, &count
);
96 palette
= heap_alloc(2 * sizeof(UINT
) + count
* sizeof(ARGB
));
97 IWICPalette_GetColors(wic_palette
, count
, palette
->Entries
, &palette
->Count
);
99 IWICPalette_GetType(wic_palette
, &type
);
101 case WICBitmapPaletteTypeFixedGray4
:
102 case WICBitmapPaletteTypeFixedGray16
:
103 case WICBitmapPaletteTypeFixedGray256
:
104 palette
->Flags
= PaletteFlagsGrayScale
;
106 case WICBitmapPaletteTypeFixedHalftone8
:
107 case WICBitmapPaletteTypeFixedHalftone27
:
108 case WICBitmapPaletteTypeFixedHalftone64
:
109 case WICBitmapPaletteTypeFixedHalftone125
:
110 case WICBitmapPaletteTypeFixedHalftone216
:
111 case WICBitmapPaletteTypeFixedHalftone252
:
112 case WICBitmapPaletteTypeFixedHalftone256
:
113 palette
->Flags
= PaletteFlagsHalftone
;
118 IWICPalette_HasAlpha(wic_palette
, &alpha
);
120 palette
->Flags
|= PaletteFlagsHasAlpha
;
122 IWICPalette_Release(wic_palette
);
124 IWICImagingFactory_Release(factory
);
128 GpStatus WINGDIPAPI
GdipBitmapApplyEffect(GpBitmap
* bitmap
, CGpEffect
* effect
,
129 RECT
* roi
, BOOL useAuxData
, VOID
** auxData
, INT
* auxDataSize
)
131 FIXME("(%p %p %p %d %p %p): stub\n", bitmap
, effect
, roi
, useAuxData
, auxData
, auxDataSize
);
133 * Note: According to Jose Roca's GDI+ docs, this function is not
134 * implemented in Windows's GDI+.
136 return NotImplemented
;
139 GpStatus WINGDIPAPI
GdipBitmapCreateApplyEffect(GpBitmap
** inputBitmaps
,
140 INT numInputs
, CGpEffect
* effect
, RECT
* roi
, RECT
* outputRect
,
141 GpBitmap
** outputBitmap
, BOOL useAuxData
, VOID
** auxData
, INT
* auxDataSize
)
143 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps
, numInputs
, effect
, roi
, outputRect
, outputBitmap
, useAuxData
, auxData
, auxDataSize
);
145 * Note: According to Jose Roca's GDI+ docs, this function is not
146 * implemented in Windows's GDI+.
148 return NotImplemented
;
151 static inline void getpixel_1bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
153 *index
= (row
[x
/8]>>(7-x
%8)) & 1;
156 static inline void getpixel_4bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
159 *index
= row
[x
/2]&0xf;
161 *index
= row
[x
/2]>>4;
164 static inline void getpixel_8bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
169 static inline void getpixel_16bppGrayScale(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
170 const BYTE
*row
, UINT x
)
172 *r
= *g
= *b
= row
[x
*2+1];
176 static inline void getpixel_16bppRGB555(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
177 const BYTE
*row
, UINT x
)
179 WORD pixel
= *((const WORD
*)(row
)+x
);
180 *r
= (pixel
>>7&0xf8)|(pixel
>>12&0x7);
181 *g
= (pixel
>>2&0xf8)|(pixel
>>6&0x7);
182 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
186 static inline void getpixel_16bppRGB565(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
187 const BYTE
*row
, UINT x
)
189 WORD pixel
= *((const WORD
*)(row
)+x
);
190 *r
= (pixel
>>8&0xf8)|(pixel
>>13&0x7);
191 *g
= (pixel
>>3&0xfc)|(pixel
>>9&0x3);
192 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
196 static inline void getpixel_16bppARGB1555(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
197 const BYTE
*row
, UINT x
)
199 WORD pixel
= *((const WORD
*)(row
)+x
);
200 *r
= (pixel
>>7&0xf8)|(pixel
>>12&0x7);
201 *g
= (pixel
>>2&0xf8)|(pixel
>>6&0x7);
202 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
203 if ((pixel
&0x8000) == 0x8000)
209 static inline void getpixel_24bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
210 const BYTE
*row
, UINT x
)
218 static inline void getpixel_32bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
219 const BYTE
*row
, UINT x
)
227 static inline void getpixel_32bppARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
228 const BYTE
*row
, UINT x
)
236 static inline void getpixel_32bppPARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
237 const BYTE
*row
, UINT x
)
244 *r
= row
[x
*4+2] * 255 / *a
;
245 *g
= row
[x
*4+1] * 255 / *a
;
246 *b
= row
[x
*4] * 255 / *a
;
250 static inline void getpixel_48bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
251 const BYTE
*row
, UINT x
)
259 static inline void getpixel_64bppARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
260 const BYTE
*row
, UINT x
)
268 static inline void getpixel_64bppPARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
269 const BYTE
*row
, UINT x
)
276 *r
= row
[x
*8+5] * 255 / *a
;
277 *g
= row
[x
*8+3] * 255 / *a
;
278 *b
= row
[x
*8+1] * 255 / *a
;
282 GpStatus WINGDIPAPI
GdipBitmapGetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
289 if(!bitmap
|| !color
||
290 x
< 0 || y
< 0 || x
>= bitmap
->width
|| y
>= bitmap
->height
)
291 return InvalidParameter
;
293 row
= bitmap
->bits
+bitmap
->stride
*y
;
295 switch (bitmap
->format
)
297 case PixelFormat1bppIndexed
:
298 getpixel_1bppIndexed(&index
,row
,x
);
300 case PixelFormat4bppIndexed
:
301 getpixel_4bppIndexed(&index
,row
,x
);
303 case PixelFormat8bppIndexed
:
304 getpixel_8bppIndexed(&index
,row
,x
);
306 case PixelFormat16bppGrayScale
:
307 getpixel_16bppGrayScale(&r
,&g
,&b
,&a
,row
,x
);
309 case PixelFormat16bppRGB555
:
310 getpixel_16bppRGB555(&r
,&g
,&b
,&a
,row
,x
);
312 case PixelFormat16bppRGB565
:
313 getpixel_16bppRGB565(&r
,&g
,&b
,&a
,row
,x
);
315 case PixelFormat16bppARGB1555
:
316 getpixel_16bppARGB1555(&r
,&g
,&b
,&a
,row
,x
);
318 case PixelFormat24bppRGB
:
319 getpixel_24bppRGB(&r
,&g
,&b
,&a
,row
,x
);
321 case PixelFormat32bppRGB
:
322 getpixel_32bppRGB(&r
,&g
,&b
,&a
,row
,x
);
324 case PixelFormat32bppARGB
:
325 getpixel_32bppARGB(&r
,&g
,&b
,&a
,row
,x
);
327 case PixelFormat32bppPARGB
:
328 getpixel_32bppPARGB(&r
,&g
,&b
,&a
,row
,x
);
330 case PixelFormat48bppRGB
:
331 getpixel_48bppRGB(&r
,&g
,&b
,&a
,row
,x
);
333 case PixelFormat64bppARGB
:
334 getpixel_64bppARGB(&r
,&g
,&b
,&a
,row
,x
);
336 case PixelFormat64bppPARGB
:
337 getpixel_64bppPARGB(&r
,&g
,&b
,&a
,row
,x
);
340 FIXME("not implemented for format 0x%x\n", bitmap
->format
);
341 return NotImplemented
;
344 if (bitmap
->format
& PixelFormatIndexed
)
345 *color
= bitmap
->image
.palette
->Entries
[index
];
347 *color
= a
<<24|r
<<16|g
<<8|b
;
352 static inline UINT
get_palette_index(BYTE r
, BYTE g
, BYTE b
, BYTE a
, ColorPalette
*palette
)
355 int best_distance
= 0x7fff;
359 if (!palette
) return 0;
360 /* This algorithm scans entire palette,
361 computes difference from desired color (all color components have equal weight)
362 and returns the index of color with least difference.
364 Note: Maybe it could be replaced with a better algorithm for better image quality
365 and performance, though better algorithm would probably need some pre-built lookup
366 tables and thus may actually be slower if this method is called only few times per
369 for(i
=0;i
<palette
->Count
;i
++) {
370 ARGB color
=palette
->Entries
[i
];
371 distance
=abs(b
-(color
& 0xff)) + abs(g
-(color
>>8 & 0xff)) + abs(r
-(color
>>16 & 0xff)) + abs(a
-(color
>>24 & 0xff));
372 if (distance
<best_distance
) {
373 best_distance
=distance
;
380 static inline void setpixel_8bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
381 BYTE
*row
, UINT x
, ColorPalette
*palette
)
383 BYTE index
= get_palette_index(r
,g
,b
,a
,palette
);
387 static inline void setpixel_1bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
388 BYTE
*row
, UINT x
, ColorPalette
*palette
)
390 row
[x
/8] = (row
[x
/8] & ~(1<<(7-x
%8))) | (get_palette_index(r
,g
,b
,a
,palette
)<<(7-x
%8));
393 static inline void setpixel_4bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
394 BYTE
*row
, UINT x
, ColorPalette
*palette
)
397 row
[x
/2] = (row
[x
/2] & 0xf0) | get_palette_index(r
,g
,b
,a
,palette
);
399 row
[x
/2] = (row
[x
/2] & 0x0f) | get_palette_index(r
,g
,b
,a
,palette
)<<4;
402 static inline void setpixel_16bppGrayScale(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
405 *((WORD
*)(row
)+x
) = (r
+g
+b
)*85;
408 static inline void setpixel_16bppRGB555(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
411 *((WORD
*)(row
)+x
) = (r
<<7&0x7c00)|
416 static inline void setpixel_16bppRGB565(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
419 *((WORD
*)(row
)+x
) = (r
<<8&0xf800)|
424 static inline void setpixel_16bppARGB1555(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
427 *((WORD
*)(row
)+x
) = (a
<<8&0x8000)|
433 static inline void setpixel_24bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
441 static inline void setpixel_32bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
444 *((DWORD
*)(row
)+x
) = (r
<<16)|(g
<<8)|b
;
447 static inline void setpixel_32bppARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
450 *((DWORD
*)(row
)+x
) = (a
<<24)|(r
<<16)|(g
<<8)|b
;
453 static inline void setpixel_32bppPARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
459 *((DWORD
*)(row
)+x
) = (a
<<24)|(r
<<16)|(g
<<8)|b
;
462 static inline void setpixel_48bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
465 row
[x
*6+5] = row
[x
*6+4] = r
;
466 row
[x
*6+3] = row
[x
*6+2] = g
;
467 row
[x
*6+1] = row
[x
*6] = b
;
470 static inline void setpixel_64bppARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
473 UINT64 a64
=a
, r64
=r
, g64
=g
, b64
=b
;
474 *((UINT64
*)(row
)+x
) = (a64
<<56)|(a64
<<48)|(r64
<<40)|(r64
<<32)|(g64
<<24)|(g64
<<16)|(b64
<<8)|b64
;
477 static inline void setpixel_64bppPARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
480 UINT64 a64
, r64
, g64
, b64
;
485 *((UINT64
*)(row
)+x
) = (a64
<<48)|(r64
<<32)|(g64
<<16)|b64
;
488 GpStatus WINGDIPAPI
GdipBitmapSetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
494 if(!bitmap
|| x
< 0 || y
< 0 || x
>= bitmap
->width
|| y
>= bitmap
->height
)
495 return InvalidParameter
;
502 row
= bitmap
->bits
+ bitmap
->stride
* y
;
504 switch (bitmap
->format
)
506 case PixelFormat16bppGrayScale
:
507 setpixel_16bppGrayScale(r
,g
,b
,a
,row
,x
);
509 case PixelFormat16bppRGB555
:
510 setpixel_16bppRGB555(r
,g
,b
,a
,row
,x
);
512 case PixelFormat16bppRGB565
:
513 setpixel_16bppRGB565(r
,g
,b
,a
,row
,x
);
515 case PixelFormat16bppARGB1555
:
516 setpixel_16bppARGB1555(r
,g
,b
,a
,row
,x
);
518 case PixelFormat24bppRGB
:
519 setpixel_24bppRGB(r
,g
,b
,a
,row
,x
);
521 case PixelFormat32bppRGB
:
522 setpixel_32bppRGB(r
,g
,b
,a
,row
,x
);
524 case PixelFormat32bppARGB
:
525 setpixel_32bppARGB(r
,g
,b
,a
,row
,x
);
527 case PixelFormat32bppPARGB
:
528 setpixel_32bppPARGB(r
,g
,b
,a
,row
,x
);
530 case PixelFormat48bppRGB
:
531 setpixel_48bppRGB(r
,g
,b
,a
,row
,x
);
533 case PixelFormat64bppARGB
:
534 setpixel_64bppARGB(r
,g
,b
,a
,row
,x
);
536 case PixelFormat64bppPARGB
:
537 setpixel_64bppPARGB(r
,g
,b
,a
,row
,x
);
539 case PixelFormat8bppIndexed
:
540 setpixel_8bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
542 case PixelFormat4bppIndexed
:
543 setpixel_4bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
545 case PixelFormat1bppIndexed
:
546 setpixel_1bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
549 FIXME("not implemented for format 0x%x\n", bitmap
->format
);
550 return NotImplemented
;
556 GpStatus
convert_pixels(INT width
, INT height
,
557 INT dst_stride
, BYTE
*dst_bits
, PixelFormat dst_format
,
558 INT src_stride
, const BYTE
*src_bits
, PixelFormat src_format
,
559 ColorPalette
*palette
)
563 if (src_format
== dst_format
||
564 (dst_format
== PixelFormat32bppRGB
&& PIXELFORMATBPP(src_format
) == 32))
566 UINT widthbytes
= PIXELFORMATBPP(src_format
) * width
/ 8;
567 for (y
=0; y
<height
; y
++)
568 memcpy(dst_bits
+dst_stride
*y
, src_bits
+src_stride
*y
, widthbytes
);
572 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
573 for (y=0; y<height; y++) \
574 for (x=0; x<width; x++) { \
577 BYTE *color = (BYTE *)&argb; \
578 getpixel_function(&index, src_bits+src_stride*y, x); \
579 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
580 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
585 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
586 for (y=0; y<height; y++) \
587 for (x=0; x<width; x++) { \
589 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
590 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
595 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
596 for (y=0; y<height; y++) \
597 for (x=0; x<width; x++) { \
599 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
600 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
607 case PixelFormat1bppIndexed
:
610 case PixelFormat16bppGrayScale
:
611 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppGrayScale
);
612 case PixelFormat16bppRGB555
:
613 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppRGB555
);
614 case PixelFormat16bppRGB565
:
615 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppRGB565
);
616 case PixelFormat16bppARGB1555
:
617 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppARGB1555
);
618 case PixelFormat24bppRGB
:
619 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_24bppRGB
);
620 case PixelFormat32bppRGB
:
621 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppRGB
);
622 case PixelFormat32bppARGB
:
623 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppARGB
);
624 case PixelFormat32bppPARGB
:
625 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppPARGB
);
626 case PixelFormat48bppRGB
:
627 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_48bppRGB
);
628 case PixelFormat64bppARGB
:
629 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_64bppARGB
);
634 case PixelFormat4bppIndexed
:
637 case PixelFormat16bppGrayScale
:
638 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppGrayScale
);
639 case PixelFormat16bppRGB555
:
640 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppRGB555
);
641 case PixelFormat16bppRGB565
:
642 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppRGB565
);
643 case PixelFormat16bppARGB1555
:
644 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppARGB1555
);
645 case PixelFormat24bppRGB
:
646 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_24bppRGB
);
647 case PixelFormat32bppRGB
:
648 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppRGB
);
649 case PixelFormat32bppARGB
:
650 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppARGB
);
651 case PixelFormat32bppPARGB
:
652 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppPARGB
);
653 case PixelFormat48bppRGB
:
654 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_48bppRGB
);
655 case PixelFormat64bppARGB
:
656 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_64bppARGB
);
661 case PixelFormat8bppIndexed
:
664 case PixelFormat16bppGrayScale
:
665 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppGrayScale
);
666 case PixelFormat16bppRGB555
:
667 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppRGB555
);
668 case PixelFormat16bppRGB565
:
669 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppRGB565
);
670 case PixelFormat16bppARGB1555
:
671 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppARGB1555
);
672 case PixelFormat24bppRGB
:
673 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_24bppRGB
);
674 case PixelFormat32bppRGB
:
675 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppRGB
);
676 case PixelFormat32bppARGB
:
677 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppARGB
);
678 case PixelFormat32bppPARGB
:
679 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppPARGB
);
680 case PixelFormat48bppRGB
:
681 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_48bppRGB
);
682 case PixelFormat64bppARGB
:
683 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_64bppARGB
);
688 case PixelFormat16bppGrayScale
:
691 case PixelFormat1bppIndexed
:
692 convert_rgb_to_indexed(getpixel_16bppGrayScale
, setpixel_1bppIndexed
);
693 case PixelFormat8bppIndexed
:
694 convert_rgb_to_indexed(getpixel_16bppGrayScale
, setpixel_8bppIndexed
);
695 case PixelFormat16bppRGB555
:
696 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppRGB555
);
697 case PixelFormat16bppRGB565
:
698 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppRGB565
);
699 case PixelFormat16bppARGB1555
:
700 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppARGB1555
);
701 case PixelFormat24bppRGB
:
702 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_24bppRGB
);
703 case PixelFormat32bppRGB
:
704 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppRGB
);
705 case PixelFormat32bppARGB
:
706 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppARGB
);
707 case PixelFormat32bppPARGB
:
708 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppPARGB
);
709 case PixelFormat48bppRGB
:
710 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_48bppRGB
);
711 case PixelFormat64bppARGB
:
712 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_64bppARGB
);
717 case PixelFormat16bppRGB555
:
720 case PixelFormat1bppIndexed
:
721 convert_rgb_to_indexed(getpixel_16bppRGB555
, setpixel_1bppIndexed
);
722 case PixelFormat8bppIndexed
:
723 convert_rgb_to_indexed(getpixel_16bppRGB555
, setpixel_8bppIndexed
);
724 case PixelFormat16bppGrayScale
:
725 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppGrayScale
);
726 case PixelFormat16bppRGB565
:
727 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppRGB565
);
728 case PixelFormat16bppARGB1555
:
729 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppARGB1555
);
730 case PixelFormat24bppRGB
:
731 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_24bppRGB
);
732 case PixelFormat32bppRGB
:
733 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppRGB
);
734 case PixelFormat32bppARGB
:
735 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppARGB
);
736 case PixelFormat32bppPARGB
:
737 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppPARGB
);
738 case PixelFormat48bppRGB
:
739 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_48bppRGB
);
740 case PixelFormat64bppARGB
:
741 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_64bppARGB
);
746 case PixelFormat16bppRGB565
:
749 case PixelFormat1bppIndexed
:
750 convert_rgb_to_indexed(getpixel_16bppRGB565
, setpixel_1bppIndexed
);
751 case PixelFormat8bppIndexed
:
752 convert_rgb_to_indexed(getpixel_16bppRGB565
, setpixel_8bppIndexed
);
753 case PixelFormat16bppGrayScale
:
754 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppGrayScale
);
755 case PixelFormat16bppRGB555
:
756 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppRGB555
);
757 case PixelFormat16bppARGB1555
:
758 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppARGB1555
);
759 case PixelFormat24bppRGB
:
760 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_24bppRGB
);
761 case PixelFormat32bppRGB
:
762 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppRGB
);
763 case PixelFormat32bppARGB
:
764 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppARGB
);
765 case PixelFormat32bppPARGB
:
766 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppPARGB
);
767 case PixelFormat48bppRGB
:
768 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_48bppRGB
);
769 case PixelFormat64bppARGB
:
770 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_64bppARGB
);
775 case PixelFormat16bppARGB1555
:
778 case PixelFormat1bppIndexed
:
779 convert_rgb_to_indexed(getpixel_16bppARGB1555
, setpixel_1bppIndexed
);
780 case PixelFormat8bppIndexed
:
781 convert_rgb_to_indexed(getpixel_16bppARGB1555
, setpixel_8bppIndexed
);
782 case PixelFormat16bppGrayScale
:
783 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppGrayScale
);
784 case PixelFormat16bppRGB555
:
785 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppRGB555
);
786 case PixelFormat16bppRGB565
:
787 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppRGB565
);
788 case PixelFormat24bppRGB
:
789 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_24bppRGB
);
790 case PixelFormat32bppRGB
:
791 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppRGB
);
792 case PixelFormat32bppARGB
:
793 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppARGB
);
794 case PixelFormat32bppPARGB
:
795 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppPARGB
);
796 case PixelFormat48bppRGB
:
797 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_48bppRGB
);
798 case PixelFormat64bppARGB
:
799 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_64bppARGB
);
804 case PixelFormat24bppRGB
:
807 case PixelFormat1bppIndexed
:
808 convert_rgb_to_indexed(getpixel_24bppRGB
, setpixel_1bppIndexed
);
809 case PixelFormat8bppIndexed
:
810 convert_rgb_to_indexed(getpixel_24bppRGB
, setpixel_8bppIndexed
);
811 case PixelFormat16bppGrayScale
:
812 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppGrayScale
);
813 case PixelFormat16bppRGB555
:
814 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppRGB555
);
815 case PixelFormat16bppRGB565
:
816 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppRGB565
);
817 case PixelFormat16bppARGB1555
:
818 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppARGB1555
);
819 case PixelFormat32bppRGB
:
820 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppRGB
);
821 case PixelFormat32bppARGB
:
822 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppARGB
);
823 case PixelFormat32bppPARGB
:
824 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppPARGB
);
825 case PixelFormat48bppRGB
:
826 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_48bppRGB
);
827 case PixelFormat64bppARGB
:
828 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_64bppARGB
);
833 case PixelFormat32bppRGB
:
836 case PixelFormat1bppIndexed
:
837 convert_rgb_to_indexed(getpixel_32bppRGB
, setpixel_1bppIndexed
);
838 case PixelFormat8bppIndexed
:
839 convert_rgb_to_indexed(getpixel_32bppRGB
, setpixel_8bppIndexed
);
840 case PixelFormat16bppGrayScale
:
841 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppGrayScale
);
842 case PixelFormat16bppRGB555
:
843 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppRGB555
);
844 case PixelFormat16bppRGB565
:
845 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppRGB565
);
846 case PixelFormat16bppARGB1555
:
847 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppARGB1555
);
848 case PixelFormat24bppRGB
:
849 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_24bppRGB
);
850 case PixelFormat32bppARGB
:
851 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_32bppARGB
);
852 case PixelFormat32bppPARGB
:
853 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_32bppPARGB
);
854 case PixelFormat48bppRGB
:
855 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_48bppRGB
);
856 case PixelFormat64bppARGB
:
857 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_64bppARGB
);
862 case PixelFormat32bppARGB
:
865 case PixelFormat1bppIndexed
:
866 convert_rgb_to_indexed(getpixel_32bppARGB
, setpixel_1bppIndexed
);
867 case PixelFormat8bppIndexed
:
868 convert_rgb_to_indexed(getpixel_32bppARGB
, setpixel_8bppIndexed
);
869 case PixelFormat16bppGrayScale
:
870 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppGrayScale
);
871 case PixelFormat16bppRGB555
:
872 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppRGB555
);
873 case PixelFormat16bppRGB565
:
874 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppRGB565
);
875 case PixelFormat16bppARGB1555
:
876 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppARGB1555
);
877 case PixelFormat24bppRGB
:
878 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_24bppRGB
);
879 case PixelFormat32bppPARGB
:
880 convert_32bppARGB_to_32bppPARGB(width
, height
, dst_bits
, dst_stride
, src_bits
, src_stride
);
882 case PixelFormat48bppRGB
:
883 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_48bppRGB
);
884 case PixelFormat64bppARGB
:
885 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_64bppARGB
);
890 case PixelFormat32bppPARGB
:
893 case PixelFormat1bppIndexed
:
894 convert_rgb_to_indexed(getpixel_32bppPARGB
, setpixel_1bppIndexed
);
895 case PixelFormat8bppIndexed
:
896 convert_rgb_to_indexed(getpixel_32bppPARGB
, setpixel_8bppIndexed
);
897 case PixelFormat16bppGrayScale
:
898 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppGrayScale
);
899 case PixelFormat16bppRGB555
:
900 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppRGB555
);
901 case PixelFormat16bppRGB565
:
902 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppRGB565
);
903 case PixelFormat16bppARGB1555
:
904 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppARGB1555
);
905 case PixelFormat24bppRGB
:
906 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_24bppRGB
);
907 case PixelFormat32bppRGB
:
908 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_32bppRGB
);
909 case PixelFormat32bppARGB
:
910 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_32bppARGB
);
911 case PixelFormat48bppRGB
:
912 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_48bppRGB
);
913 case PixelFormat64bppARGB
:
914 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_64bppARGB
);
919 case PixelFormat48bppRGB
:
922 case PixelFormat1bppIndexed
:
923 convert_rgb_to_indexed(getpixel_48bppRGB
, setpixel_1bppIndexed
);
924 case PixelFormat8bppIndexed
:
925 convert_rgb_to_indexed(getpixel_48bppRGB
, setpixel_8bppIndexed
);
926 case PixelFormat16bppGrayScale
:
927 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppGrayScale
);
928 case PixelFormat16bppRGB555
:
929 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppRGB555
);
930 case PixelFormat16bppRGB565
:
931 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppRGB565
);
932 case PixelFormat16bppARGB1555
:
933 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppARGB1555
);
934 case PixelFormat24bppRGB
:
935 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_24bppRGB
);
936 case PixelFormat32bppRGB
:
937 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppRGB
);
938 case PixelFormat32bppARGB
:
939 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppARGB
);
940 case PixelFormat32bppPARGB
:
941 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppPARGB
);
942 case PixelFormat64bppARGB
:
943 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_64bppARGB
);
948 case PixelFormat64bppARGB
:
951 case PixelFormat1bppIndexed
:
952 convert_rgb_to_indexed(getpixel_64bppARGB
, setpixel_1bppIndexed
);
953 case PixelFormat8bppIndexed
:
954 convert_rgb_to_indexed(getpixel_64bppARGB
, setpixel_8bppIndexed
);
955 case PixelFormat16bppGrayScale
:
956 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppGrayScale
);
957 case PixelFormat16bppRGB555
:
958 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppRGB555
);
959 case PixelFormat16bppRGB565
:
960 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppRGB565
);
961 case PixelFormat16bppARGB1555
:
962 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppARGB1555
);
963 case PixelFormat24bppRGB
:
964 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_24bppRGB
);
965 case PixelFormat32bppRGB
:
966 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppRGB
);
967 case PixelFormat32bppARGB
:
968 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppARGB
);
969 case PixelFormat32bppPARGB
:
970 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppPARGB
);
971 case PixelFormat48bppRGB
:
972 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_48bppRGB
);
977 case PixelFormat64bppPARGB
:
980 case PixelFormat1bppIndexed
:
981 convert_rgb_to_indexed(getpixel_64bppPARGB
, setpixel_1bppIndexed
);
982 case PixelFormat8bppIndexed
:
983 convert_rgb_to_indexed(getpixel_64bppPARGB
, setpixel_8bppIndexed
);
984 case PixelFormat16bppGrayScale
:
985 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppGrayScale
);
986 case PixelFormat16bppRGB555
:
987 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppRGB555
);
988 case PixelFormat16bppRGB565
:
989 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppRGB565
);
990 case PixelFormat16bppARGB1555
:
991 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppARGB1555
);
992 case PixelFormat24bppRGB
:
993 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_24bppRGB
);
994 case PixelFormat32bppRGB
:
995 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppRGB
);
996 case PixelFormat32bppARGB
:
997 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppARGB
);
998 case PixelFormat32bppPARGB
:
999 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppPARGB
);
1000 case PixelFormat48bppRGB
:
1001 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_48bppRGB
);
1002 case PixelFormat64bppARGB
:
1003 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_64bppARGB
);
1012 #undef convert_indexed_to_rgb
1013 #undef convert_rgb_to_rgb
1015 return NotImplemented
;
1018 /* This function returns a pointer to an array of pixels that represents the
1019 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1020 * flags. It is correct behavior that a user who calls this function with write
1021 * privileges can write to the whole bitmap (not just the area in rect).
1023 * FIXME: only used portion of format is bits per pixel. */
1024 GpStatus WINGDIPAPI
GdipBitmapLockBits(GpBitmap
* bitmap
, GDIPCONST GpRect
* rect
,
1025 UINT flags
, PixelFormat format
, BitmapData
* lockeddata
)
1027 INT bitspp
= PIXELFORMATBPP(format
);
1028 GpRect act_rect
; /* actual rect to be used */
1032 TRACE("%p %p %d 0x%x %p\n", bitmap
, rect
, flags
, format
, lockeddata
);
1034 if(!lockeddata
|| !bitmap
)
1035 return InvalidParameter
;
1036 if(!image_lock(&bitmap
->image
, &unlock
))
1040 if(rect
->X
< 0 || rect
->Y
< 0 || (rect
->X
+ rect
->Width
> bitmap
->width
) ||
1041 (rect
->Y
+ rect
->Height
> bitmap
->height
) || !flags
)
1043 image_unlock(&bitmap
->image
, unlock
);
1044 return InvalidParameter
;
1050 act_rect
.X
= act_rect
.Y
= 0;
1051 act_rect
.Width
= bitmap
->width
;
1052 act_rect
.Height
= bitmap
->height
;
1055 if(bitmap
->lockmode
)
1057 WARN("bitmap is already locked and cannot be locked again\n");
1058 image_unlock(&bitmap
->image
, unlock
);
1062 if (bitmap
->bits
&& bitmap
->format
== format
&& !(flags
& ImageLockModeUserInputBuf
))
1064 /* no conversion is necessary; just use the bits directly */
1065 lockeddata
->Width
= act_rect
.Width
;
1066 lockeddata
->Height
= act_rect
.Height
;
1067 lockeddata
->PixelFormat
= format
;
1068 lockeddata
->Reserved
= flags
;
1069 lockeddata
->Stride
= bitmap
->stride
;
1070 lockeddata
->Scan0
= bitmap
->bits
+ (bitspp
/ 8) * act_rect
.X
+
1071 bitmap
->stride
* act_rect
.Y
;
1073 bitmap
->lockmode
= flags
| ImageLockModeRead
;
1075 image_unlock(&bitmap
->image
, unlock
);
1079 /* Make sure we can convert to the requested format. */
1080 if (flags
& ImageLockModeRead
)
1082 stat
= convert_pixels(0, 0, 0, NULL
, format
, 0, NULL
, bitmap
->format
, NULL
);
1083 if (stat
== NotImplemented
)
1085 FIXME("cannot read bitmap from %x to %x\n", bitmap
->format
, format
);
1086 image_unlock(&bitmap
->image
, unlock
);
1087 return NotImplemented
;
1091 /* If we're opening for writing, make sure we'll be able to write back in
1092 * the original format. */
1093 if (flags
& ImageLockModeWrite
)
1095 stat
= convert_pixels(0, 0, 0, NULL
, bitmap
->format
, 0, NULL
, format
, NULL
);
1096 if (stat
== NotImplemented
)
1098 FIXME("cannot write bitmap from %x to %x\n", format
, bitmap
->format
);
1099 image_unlock(&bitmap
->image
, unlock
);
1100 return NotImplemented
;
1104 lockeddata
->Width
= act_rect
.Width
;
1105 lockeddata
->Height
= act_rect
.Height
;
1106 lockeddata
->PixelFormat
= format
;
1107 lockeddata
->Reserved
= flags
;
1109 if(!(flags
& ImageLockModeUserInputBuf
))
1111 lockeddata
->Stride
= (((act_rect
.Width
* bitspp
+ 7) / 8) + 3) & ~3;
1113 bitmap
->bitmapbits
= heap_alloc_zero(lockeddata
->Stride
* act_rect
.Height
);
1115 if (!bitmap
->bitmapbits
)
1117 image_unlock(&bitmap
->image
, unlock
);
1121 lockeddata
->Scan0
= bitmap
->bitmapbits
;
1124 if (flags
& ImageLockModeRead
)
1126 static BOOL fixme
= FALSE
;
1128 if (!fixme
&& (PIXELFORMATBPP(bitmap
->format
) * act_rect
.X
) % 8 != 0)
1130 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1134 stat
= convert_pixels(act_rect
.Width
, act_rect
.Height
,
1135 lockeddata
->Stride
, lockeddata
->Scan0
, format
,
1137 bitmap
->bits
+ bitmap
->stride
* act_rect
.Y
+ PIXELFORMATBPP(bitmap
->format
) * act_rect
.X
/ 8,
1138 bitmap
->format
, bitmap
->image
.palette
);
1142 heap_free(bitmap
->bitmapbits
);
1143 bitmap
->bitmapbits
= NULL
;
1144 image_unlock(&bitmap
->image
, unlock
);
1149 bitmap
->lockmode
= flags
| ImageLockModeRead
;
1150 bitmap
->lockx
= act_rect
.X
;
1151 bitmap
->locky
= act_rect
.Y
;
1153 image_unlock(&bitmap
->image
, unlock
);
1157 GpStatus WINGDIPAPI
GdipBitmapSetResolution(GpBitmap
* bitmap
, REAL xdpi
, REAL ydpi
)
1159 TRACE("(%p, %.2f, %.2f)\n", bitmap
, xdpi
, ydpi
);
1161 if (!bitmap
|| xdpi
== 0.0 || ydpi
== 0.0)
1162 return InvalidParameter
;
1164 bitmap
->image
.xres
= xdpi
;
1165 bitmap
->image
.yres
= ydpi
;
1170 GpStatus WINGDIPAPI
GdipBitmapUnlockBits(GpBitmap
* bitmap
,
1171 BitmapData
* lockeddata
)
1174 static BOOL fixme
= FALSE
;
1177 TRACE("(%p,%p)\n", bitmap
, lockeddata
);
1179 if(!bitmap
|| !lockeddata
)
1180 return InvalidParameter
;
1181 if(!image_lock(&bitmap
->image
, &unlock
))
1184 if(!bitmap
->lockmode
)
1186 image_unlock(&bitmap
->image
, unlock
);
1190 if(!(lockeddata
->Reserved
& ImageLockModeWrite
)){
1191 bitmap
->lockmode
= 0;
1192 heap_free(bitmap
->bitmapbits
);
1193 bitmap
->bitmapbits
= NULL
;
1194 image_unlock(&bitmap
->image
, unlock
);
1198 if (!bitmap
->bitmapbits
&& !(lockeddata
->Reserved
& ImageLockModeUserInputBuf
))
1200 /* we passed a direct reference; no need to do anything */
1201 bitmap
->lockmode
= 0;
1202 image_unlock(&bitmap
->image
, unlock
);
1206 if (!fixme
&& (PIXELFORMATBPP(bitmap
->format
) * bitmap
->lockx
) % 8 != 0)
1208 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1212 stat
= convert_pixels(lockeddata
->Width
, lockeddata
->Height
,
1214 bitmap
->bits
+ bitmap
->stride
* bitmap
->locky
+ PIXELFORMATBPP(bitmap
->format
) * bitmap
->lockx
/ 8,
1216 lockeddata
->Stride
, lockeddata
->Scan0
, lockeddata
->PixelFormat
, NULL
);
1220 ERR("failed to convert pixels; this should never happen\n");
1223 heap_free(bitmap
->bitmapbits
);
1224 bitmap
->bitmapbits
= NULL
;
1225 bitmap
->lockmode
= 0;
1227 image_unlock(&bitmap
->image
, unlock
);
1231 GpStatus WINGDIPAPI
GdipCloneBitmapArea(REAL x
, REAL y
, REAL width
, REAL height
,
1232 PixelFormat format
, GpBitmap
* srcBitmap
, GpBitmap
** dstBitmap
)
1237 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1239 if (!srcBitmap
|| !dstBitmap
|| srcBitmap
->image
.type
!= ImageTypeBitmap
||
1241 x
+ width
> srcBitmap
->width
|| y
+ height
> srcBitmap
->height
)
1243 TRACE("<-- InvalidParameter\n");
1244 return InvalidParameter
;
1247 if (format
== PixelFormatDontCare
)
1248 format
= srcBitmap
->format
;
1250 area
.X
= gdip_round(x
);
1251 area
.Y
= gdip_round(y
);
1252 area
.Width
= gdip_round(width
);
1253 area
.Height
= gdip_round(height
);
1255 stat
= GdipCreateBitmapFromScan0(area
.Width
, area
.Height
, 0, format
, NULL
, dstBitmap
);
1258 stat
= convert_pixels(area
.Width
, area
.Height
, (*dstBitmap
)->stride
, (*dstBitmap
)->bits
, (*dstBitmap
)->format
,
1260 srcBitmap
->bits
+ srcBitmap
->stride
* area
.Y
+ PIXELFORMATBPP(srcBitmap
->format
) * area
.X
/ 8,
1261 srcBitmap
->format
, srcBitmap
->image
.palette
);
1263 if (stat
== Ok
&& srcBitmap
->image
.palette
)
1265 ColorPalette
*src_palette
, *dst_palette
;
1267 src_palette
= srcBitmap
->image
.palette
;
1269 dst_palette
= heap_alloc_zero(sizeof(UINT
) * 2 + sizeof(ARGB
) * src_palette
->Count
);
1273 dst_palette
->Flags
= src_palette
->Flags
;
1274 dst_palette
->Count
= src_palette
->Count
;
1275 memcpy(dst_palette
->Entries
, src_palette
->Entries
, sizeof(ARGB
) * src_palette
->Count
);
1277 heap_free((*dstBitmap
)->image
.palette
);
1278 (*dstBitmap
)->image
.palette
= dst_palette
;
1285 GdipDisposeImage(&(*dstBitmap
)->image
);
1294 GpStatus WINGDIPAPI
GdipCloneBitmapAreaI(INT x
, INT y
, INT width
, INT height
,
1295 PixelFormat format
, GpBitmap
* srcBitmap
, GpBitmap
** dstBitmap
)
1297 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1299 return GdipCloneBitmapArea(x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1302 GpStatus WINGDIPAPI
GdipCloneImage(GpImage
*image
, GpImage
**cloneImage
)
1304 TRACE("%p, %p\n", image
, cloneImage
);
1306 if (!image
|| !cloneImage
)
1307 return InvalidParameter
;
1309 if (image
->type
== ImageTypeBitmap
)
1311 GpBitmap
*bitmap
= (GpBitmap
*)image
;
1313 return GdipCloneBitmapAreaI(0, 0, bitmap
->width
, bitmap
->height
,
1314 bitmap
->format
, bitmap
, (GpBitmap
**)cloneImage
);
1316 else if (image
->type
== ImageTypeMetafile
&& ((GpMetafile
*)image
)->hemf
)
1318 GpMetafile
*result
, *metafile
;
1320 metafile
= (GpMetafile
*)image
;
1322 result
= heap_alloc_zero(sizeof(*result
));
1326 result
->image
.type
= ImageTypeMetafile
;
1327 result
->image
.format
= image
->format
;
1328 result
->image
.flags
= image
->flags
;
1329 result
->image
.frame_count
= 1;
1330 result
->image
.xres
= image
->xres
;
1331 result
->image
.yres
= image
->yres
;
1332 result
->bounds
= metafile
->bounds
;
1333 result
->unit
= metafile
->unit
;
1334 result
->metafile_type
= metafile
->metafile_type
;
1335 result
->hemf
= CopyEnhMetaFileW(metafile
->hemf
, NULL
);
1343 *cloneImage
= &result
->image
;
1348 WARN("GpImage with no image data (metafile in wrong state?)\n");
1349 return InvalidParameter
;
1353 GpStatus WINGDIPAPI
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
1359 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
1361 if(!filename
|| !bitmap
)
1362 return InvalidParameter
;
1366 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
1371 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
1373 IStream_Release(stream
);
1378 GpStatus WINGDIPAPI
GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO
* info
,
1379 VOID
*bits
, GpBitmap
**bitmap
)
1381 DWORD height
, stride
;
1384 FIXME("(%p, %p, %p) - partially implemented\n", info
, bits
, bitmap
);
1386 if (!info
|| !bits
|| !bitmap
)
1387 return InvalidParameter
;
1389 height
= abs(info
->bmiHeader
.biHeight
);
1390 stride
= ((info
->bmiHeader
.biWidth
* info
->bmiHeader
.biBitCount
+ 31) >> 3) & ~3;
1392 if(info
->bmiHeader
.biHeight
> 0) /* bottom-up */
1394 bits
= (BYTE
*)bits
+ (height
- 1) * stride
;
1398 switch(info
->bmiHeader
.biBitCount
) {
1400 format
= PixelFormat1bppIndexed
;
1403 format
= PixelFormat4bppIndexed
;
1406 format
= PixelFormat8bppIndexed
;
1409 format
= PixelFormat16bppRGB555
;
1412 format
= PixelFormat24bppRGB
;
1415 format
= PixelFormat32bppRGB
;
1418 FIXME("don't know how to handle %d bpp\n", info
->bmiHeader
.biBitCount
);
1420 return InvalidParameter
;
1423 return GdipCreateBitmapFromScan0(info
->bmiHeader
.biWidth
, height
, stride
, format
,
1429 GpStatus WINGDIPAPI
GdipCreateBitmapFromFileICM(GDIPCONST WCHAR
* filename
,
1432 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
1434 return GdipCreateBitmapFromFile(filename
, bitmap
);
1437 GpStatus WINGDIPAPI
GdipCreateBitmapFromResource(HINSTANCE hInstance
,
1438 GDIPCONST WCHAR
* lpBitmapName
, GpBitmap
** bitmap
)
1441 GpStatus stat
= InvalidParameter
;
1443 TRACE("%p (%s) %p\n", hInstance
, debugstr_w(lpBitmapName
), bitmap
);
1445 if(!lpBitmapName
|| !bitmap
)
1446 return InvalidParameter
;
1449 hbm
= LoadImageW(hInstance
, lpBitmapName
, IMAGE_BITMAP
, 0, 0,
1450 LR_CREATEDIBSECTION
);
1453 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, bitmap
);
1460 static inline DWORD
blend_argb_no_bkgnd_alpha(DWORD src
, DWORD bkgnd
)
1463 BYTE g
= (BYTE
)(src
>> 8);
1464 BYTE r
= (BYTE
)(src
>> 16);
1465 DWORD alpha
= (BYTE
)(src
>> 24);
1466 return ((b
+ ((BYTE
)bkgnd
* (255 - alpha
) + 127) / 255) |
1467 (g
+ ((BYTE
)(bkgnd
>> 8) * (255 - alpha
) + 127) / 255) << 8 |
1468 (r
+ ((BYTE
)(bkgnd
>> 16) * (255 - alpha
) + 127) / 255) << 16 |
1472 GpStatus WINGDIPAPI
GdipCreateHBITMAPFromBitmap(GpBitmap
* bitmap
,
1473 HBITMAP
* hbmReturn
, ARGB background
)
1478 BITMAPINFOHEADER bih
;
1482 TRACE("(%p,%p,%x)\n", bitmap
, hbmReturn
, background
);
1484 if (!bitmap
|| !hbmReturn
) return InvalidParameter
;
1485 if (!image_lock(&bitmap
->image
, &unlock
)) return ObjectBusy
;
1487 GdipGetImageWidth(&bitmap
->image
, &width
);
1488 GdipGetImageHeight(&bitmap
->image
, &height
);
1490 bih
.biSize
= sizeof(bih
);
1491 bih
.biWidth
= width
;
1492 bih
.biHeight
= height
;
1494 bih
.biBitCount
= 32;
1495 bih
.biCompression
= BI_RGB
;
1496 bih
.biSizeImage
= 0;
1497 bih
.biXPelsPerMeter
= 0;
1498 bih
.biYPelsPerMeter
= 0;
1500 bih
.biClrImportant
= 0;
1502 result
= CreateDIBSection(0, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
, (void**)&bits
, NULL
, 0);
1505 image_unlock(&bitmap
->image
, unlock
);
1506 return GenericError
;
1509 stat
= convert_pixels(width
, height
, -width
*4,
1510 bits
+ (width
* 4 * (height
- 1)), PixelFormat32bppPARGB
,
1511 bitmap
->stride
, bitmap
->bits
, bitmap
->format
, bitmap
->image
.palette
);
1514 DeleteObject(result
);
1515 image_unlock(&bitmap
->image
, unlock
);
1519 if (background
& 0xffffff)
1523 for (ptr
= (DWORD
*)bits
, i
= 0; i
< width
* height
; ptr
++, i
++)
1525 if ((*ptr
& 0xff000000) == 0xff000000) continue;
1526 *ptr
= blend_argb_no_bkgnd_alpha(*ptr
, background
);
1530 *hbmReturn
= result
;
1531 image_unlock(&bitmap
->image
, unlock
);
1535 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
1536 GpGraphics
* target
, GpBitmap
** bitmap
)
1540 TRACE("(%d, %d, %p, %p)\n", width
, height
, target
, bitmap
);
1542 if(!target
|| !bitmap
)
1543 return InvalidParameter
;
1545 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat32bppPARGB
,
1550 GdipGetDpiX(target
, &(*bitmap
)->image
.xres
);
1551 GdipGetDpiY(target
, &(*bitmap
)->image
.yres
);
1557 GpStatus WINGDIPAPI
GdipCreateBitmapFromHICON(HICON hicon
, GpBitmap
** bitmap
)
1563 UINT width
, height
, stride
;
1565 BitmapData lockeddata
;
1569 BITMAPINFOHEADER bih
;
1574 TRACE("%p, %p\n", hicon
, bitmap
);
1576 if(!bitmap
|| !GetIconInfo(hicon
, &iinfo
))
1577 return InvalidParameter
;
1579 /* get the size of the icon */
1580 ret
= GetObjectA(iinfo
.hbmColor
? iinfo
.hbmColor
: iinfo
.hbmMask
, sizeof(bm
), &bm
);
1582 DeleteObject(iinfo
.hbmColor
);
1583 DeleteObject(iinfo
.hbmMask
);
1584 return GenericError
;
1588 height
= iinfo
.hbmColor
? abs(bm
.bmHeight
) : abs(bm
.bmHeight
) / 2;
1591 stat
= GdipCreateBitmapFromScan0(width
, height
, stride
, PixelFormat32bppARGB
, NULL
, bitmap
);
1593 DeleteObject(iinfo
.hbmColor
);
1594 DeleteObject(iinfo
.hbmMask
);
1601 rect
.Height
= height
;
1603 stat
= GdipBitmapLockBits(*bitmap
, &rect
, ImageLockModeWrite
, PixelFormat32bppARGB
, &lockeddata
);
1605 DeleteObject(iinfo
.hbmColor
);
1606 DeleteObject(iinfo
.hbmMask
);
1607 GdipDisposeImage(&(*bitmap
)->image
);
1611 bih
.biSize
= sizeof(bih
);
1612 bih
.biWidth
= width
;
1613 bih
.biHeight
= iinfo
.hbmColor
? -height
: -height
* 2;
1615 bih
.biBitCount
= 32;
1616 bih
.biCompression
= BI_RGB
;
1617 bih
.biSizeImage
= 0;
1618 bih
.biXPelsPerMeter
= 0;
1619 bih
.biYPelsPerMeter
= 0;
1621 bih
.biClrImportant
= 0;
1623 screendc
= CreateCompatibleDC(0);
1626 GetDIBits(screendc
, iinfo
.hbmColor
, 0, height
, lockeddata
.Scan0
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1628 if (bm
.bmBitsPixel
== 32)
1632 /* If any pixel has a non-zero alpha, ignore hbmMask */
1633 src
= (DWORD
*)lockeddata
.Scan0
;
1634 for (x
=0; x
<width
&& !has_alpha
; x
++)
1635 for (y
=0; y
<height
&& !has_alpha
; y
++)
1636 if ((*src
++ & 0xff000000) != 0)
1639 else has_alpha
= FALSE
;
1643 GetDIBits(screendc
, iinfo
.hbmMask
, 0, height
, lockeddata
.Scan0
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1651 BYTE
*bits
= heap_alloc(height
* stride
);
1653 /* read alpha data from the mask */
1655 GetDIBits(screendc
, iinfo
.hbmMask
, 0, height
, bits
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1657 GetDIBits(screendc
, iinfo
.hbmMask
, height
, height
, bits
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1660 dst_row
= lockeddata
.Scan0
;
1661 for (y
=0; y
<height
; y
++)
1663 dst
= (DWORD
*)dst_row
;
1664 for (x
=0; x
<height
; x
++)
1666 DWORD src_value
= *src
++;
1670 *dst
++ |= 0xff000000;
1672 dst_row
+= lockeddata
.Stride
;
1679 /* set constant alpha of 255 */
1680 dst_row
= lockeddata
.Scan0
;
1681 for (y
=0; y
<height
; y
++)
1683 dst
= (DWORD
*)dst_row
;
1684 for (x
=0; x
<height
; x
++)
1685 *dst
++ |= 0xff000000;
1686 dst_row
+= lockeddata
.Stride
;
1693 DeleteObject(iinfo
.hbmColor
);
1694 DeleteObject(iinfo
.hbmMask
);
1696 GdipBitmapUnlockBits(*bitmap
, &lockeddata
);
1701 static void generate_halftone_palette(ARGB
*entries
, UINT count
)
1703 static const BYTE halftone_values
[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1706 for (i
=0; i
<8 && i
<count
; i
++)
1708 entries
[i
] = 0xff000000;
1709 if (i
&1) entries
[i
] |= 0x800000;
1710 if (i
&2) entries
[i
] |= 0x8000;
1711 if (i
&4) entries
[i
] |= 0x80;
1715 entries
[i
] = 0xffc0c0c0;
1717 for (i
=9; i
<16 && i
<count
; i
++)
1719 entries
[i
] = 0xff000000;
1720 if (i
&1) entries
[i
] |= 0xff0000;
1721 if (i
&2) entries
[i
] |= 0xff00;
1722 if (i
&4) entries
[i
] |= 0xff;
1725 for (i
=16; i
<40 && i
<count
; i
++)
1730 for (i
=40; i
<256 && i
<count
; i
++)
1732 entries
[i
] = 0xff000000;
1733 entries
[i
] |= halftone_values
[(i
-40)%6];
1734 entries
[i
] |= halftone_values
[((i
-40)/6)%6] << 8;
1735 entries
[i
] |= halftone_values
[((i
-40)/36)%6] << 16;
1739 static GpStatus
get_screen_resolution(REAL
*xres
, REAL
*yres
)
1741 HDC screendc
= CreateCompatibleDC(0);
1743 if (!screendc
) return GenericError
;
1745 *xres
= (REAL
)GetDeviceCaps(screendc
, LOGPIXELSX
);
1746 *yres
= (REAL
)GetDeviceCaps(screendc
, LOGPIXELSY
);
1753 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
1754 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
1756 HBITMAP hbitmap
=NULL
;
1757 INT row_size
, dib_stride
;
1758 BYTE
*bits
=NULL
, *own_bits
=NULL
;
1762 TRACE("%d %d %d 0x%x %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
1764 if (!bitmap
) return InvalidParameter
;
1766 if(width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
1768 return InvalidParameter
;
1771 if(scan0
&& !stride
)
1772 return InvalidParameter
;
1774 stat
= get_screen_resolution(&xres
, &yres
);
1775 if (stat
!= Ok
) return stat
;
1777 row_size
= (width
* PIXELFORMATBPP(format
)+7) / 8;
1778 dib_stride
= (row_size
+ 3) & ~3;
1781 stride
= dib_stride
;
1783 if (format
& PixelFormatGDI
&& !(format
& (PixelFormatAlpha
|PixelFormatIndexed
)) && !scan0
)
1785 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
[256])];
1786 BITMAPINFO
*pbmi
= (BITMAPINFO
*)bmibuf
;
1788 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1789 pbmi
->bmiHeader
.biWidth
= width
;
1790 pbmi
->bmiHeader
.biHeight
= -height
;
1791 pbmi
->bmiHeader
.biPlanes
= 1;
1792 /* FIXME: use the rest of the data from format */
1793 pbmi
->bmiHeader
.biBitCount
= PIXELFORMATBPP(format
);
1794 pbmi
->bmiHeader
.biCompression
= BI_RGB
;
1795 pbmi
->bmiHeader
.biSizeImage
= 0;
1796 pbmi
->bmiHeader
.biXPelsPerMeter
= 0;
1797 pbmi
->bmiHeader
.biYPelsPerMeter
= 0;
1798 pbmi
->bmiHeader
.biClrUsed
= 0;
1799 pbmi
->bmiHeader
.biClrImportant
= 0;
1801 hbitmap
= CreateDIBSection(0, pbmi
, DIB_RGB_COLORS
, (void**)&bits
, NULL
, 0);
1803 if (!hbitmap
) return GenericError
;
1805 stride
= dib_stride
;
1809 /* Not a GDI format; don't try to make an HBITMAP. */
1814 INT size
= abs(stride
) * height
;
1816 own_bits
= bits
= heap_alloc_zero(size
);
1817 if (!own_bits
) return OutOfMemory
;
1820 bits
+= stride
* (1 - height
);
1824 *bitmap
= heap_alloc_zero(sizeof(GpBitmap
));
1827 DeleteObject(hbitmap
);
1828 heap_free(own_bits
);
1832 (*bitmap
)->image
.type
= ImageTypeBitmap
;
1833 memcpy(&(*bitmap
)->image
.format
, &ImageFormatMemoryBMP
, sizeof(GUID
));
1834 (*bitmap
)->image
.flags
= ImageFlagsNone
;
1835 (*bitmap
)->image
.frame_count
= 1;
1836 (*bitmap
)->image
.current_frame
= 0;
1837 (*bitmap
)->image
.palette
= NULL
;
1838 (*bitmap
)->image
.xres
= xres
;
1839 (*bitmap
)->image
.yres
= yres
;
1840 (*bitmap
)->width
= width
;
1841 (*bitmap
)->height
= height
;
1842 (*bitmap
)->format
= format
;
1843 (*bitmap
)->image
.decoder
= NULL
;
1844 (*bitmap
)->hbitmap
= hbitmap
;
1845 (*bitmap
)->hdc
= NULL
;
1846 (*bitmap
)->bits
= bits
;
1847 (*bitmap
)->stride
= stride
;
1848 (*bitmap
)->own_bits
= own_bits
;
1849 (*bitmap
)->metadata_reader
= NULL
;
1850 (*bitmap
)->prop_count
= 0;
1851 (*bitmap
)->prop_item
= NULL
;
1853 /* set format-related flags */
1854 if (format
& (PixelFormatAlpha
|PixelFormatPAlpha
|PixelFormatIndexed
))
1855 (*bitmap
)->image
.flags
|= ImageFlagsHasAlpha
;
1857 if (format
== PixelFormat1bppIndexed
||
1858 format
== PixelFormat4bppIndexed
||
1859 format
== PixelFormat8bppIndexed
)
1861 (*bitmap
)->image
.palette
= heap_alloc_zero(sizeof(UINT
) * 2 + sizeof(ARGB
) * (1 << PIXELFORMATBPP(format
)));
1863 if (!(*bitmap
)->image
.palette
)
1865 GdipDisposeImage(&(*bitmap
)->image
);
1870 (*bitmap
)->image
.palette
->Count
= 1 << PIXELFORMATBPP(format
);
1872 if (format
== PixelFormat1bppIndexed
)
1874 (*bitmap
)->image
.palette
->Flags
= PaletteFlagsGrayScale
;
1875 (*bitmap
)->image
.palette
->Entries
[0] = 0xff000000;
1876 (*bitmap
)->image
.palette
->Entries
[1] = 0xffffffff;
1880 if (format
== PixelFormat8bppIndexed
)
1881 (*bitmap
)->image
.palette
->Flags
= PaletteFlagsHalftone
;
1883 generate_halftone_palette((*bitmap
)->image
.palette
->Entries
,
1884 (*bitmap
)->image
.palette
->Count
);
1888 TRACE("<-- %p\n", *bitmap
);
1893 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
1898 TRACE("%p %p\n", stream
, bitmap
);
1900 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
1905 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
1906 GdipDisposeImage(&(*bitmap
)->image
);
1908 return GenericError
; /* FIXME: what error to return? */
1915 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
1918 TRACE("%p %p\n", stream
, bitmap
);
1920 return GdipCreateBitmapFromStream(stream
, bitmap
);
1923 GpStatus WINGDIPAPI
GdipCreateCachedBitmap(GpBitmap
*bitmap
, GpGraphics
*graphics
,
1924 GpCachedBitmap
**cachedbmp
)
1928 TRACE("%p %p %p\n", bitmap
, graphics
, cachedbmp
);
1930 if(!bitmap
|| !graphics
|| !cachedbmp
)
1931 return InvalidParameter
;
1933 *cachedbmp
= heap_alloc_zero(sizeof(GpCachedBitmap
));
1937 stat
= GdipCloneImage(&(bitmap
->image
), &(*cachedbmp
)->image
);
1939 heap_free(*cachedbmp
);
1946 GpStatus WINGDIPAPI
GdipCreateHICONFromBitmap(GpBitmap
*bitmap
, HICON
*hicon
)
1949 BitmapData lockeddata
;
1950 ULONG andstride
, xorstride
, bitssize
;
1951 LPBYTE andbits
, xorbits
, androw
, xorrow
, srcrow
;
1954 TRACE("(%p, %p)\n", bitmap
, hicon
);
1956 if (!bitmap
|| !hicon
)
1957 return InvalidParameter
;
1959 stat
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
,
1960 PixelFormat32bppPARGB
, &lockeddata
);
1963 andstride
= ((lockeddata
.Width
+31)/32)*4;
1964 xorstride
= lockeddata
.Width
*4;
1965 bitssize
= (andstride
+ xorstride
) * lockeddata
.Height
;
1967 andbits
= heap_alloc_zero(bitssize
);
1971 xorbits
= andbits
+ andstride
* lockeddata
.Height
;
1973 for (y
=0; y
<lockeddata
.Height
; y
++)
1975 srcrow
= ((LPBYTE
)lockeddata
.Scan0
) + lockeddata
.Stride
* y
;
1977 androw
= andbits
+ andstride
* y
;
1978 for (x
=0; x
<lockeddata
.Width
; x
++)
1979 if (srcrow
[3+4*x
] >= 128)
1980 androw
[x
/8] |= 1 << (7-x
%8);
1982 xorrow
= xorbits
+ xorstride
* y
;
1983 memcpy(xorrow
, srcrow
, xorstride
);
1986 *hicon
= CreateIcon(NULL
, lockeddata
.Width
, lockeddata
.Height
, 1, 32,
1994 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
2000 GpStatus WINGDIPAPI
GdipDeleteCachedBitmap(GpCachedBitmap
*cachedbmp
)
2002 TRACE("%p\n", cachedbmp
);
2005 return InvalidParameter
;
2007 GdipDisposeImage(cachedbmp
->image
);
2008 heap_free(cachedbmp
);
2013 GpStatus WINGDIPAPI
GdipDrawCachedBitmap(GpGraphics
*graphics
,
2014 GpCachedBitmap
*cachedbmp
, INT x
, INT y
)
2016 TRACE("%p %p %d %d\n", graphics
, cachedbmp
, x
, y
);
2018 if(!graphics
|| !cachedbmp
)
2019 return InvalidParameter
;
2021 return GdipDrawImage(graphics
, cachedbmp
->image
, (REAL
)x
, (REAL
)y
);
2024 /* Internal utility function: Replace the image data of dst with that of src,
2026 static void move_bitmap(GpBitmap
*dst
, GpBitmap
*src
, BOOL clobber_palette
)
2028 assert(src
->image
.type
== ImageTypeBitmap
);
2029 assert(dst
->image
.type
== ImageTypeBitmap
);
2031 heap_free(dst
->bitmapbits
);
2032 heap_free(dst
->own_bits
);
2034 DeleteObject(dst
->hbitmap
);
2036 if (clobber_palette
)
2038 heap_free(dst
->image
.palette
);
2039 dst
->image
.palette
= src
->image
.palette
;
2042 heap_free(src
->image
.palette
);
2044 dst
->image
.xres
= src
->image
.xres
;
2045 dst
->image
.yres
= src
->image
.yres
;
2046 dst
->width
= src
->width
;
2047 dst
->height
= src
->height
;
2048 dst
->format
= src
->format
;
2049 dst
->hbitmap
= src
->hbitmap
;
2050 dst
->hdc
= src
->hdc
;
2051 dst
->bits
= src
->bits
;
2052 dst
->stride
= src
->stride
;
2053 dst
->own_bits
= src
->own_bits
;
2054 if (dst
->metadata_reader
)
2055 IWICMetadataReader_Release(dst
->metadata_reader
);
2056 dst
->metadata_reader
= src
->metadata_reader
;
2057 heap_free(dst
->prop_item
);
2058 dst
->prop_item
= src
->prop_item
;
2059 dst
->prop_count
= src
->prop_count
;
2060 if (dst
->image
.decoder
)
2061 IWICBitmapDecoder_Release(dst
->image
.decoder
);
2062 dst
->image
.decoder
= src
->image
.decoder
;
2063 dst
->image
.frame_count
= src
->image
.frame_count
;
2064 dst
->image
.current_frame
= src
->image
.current_frame
;
2065 dst
->image
.format
= src
->image
.format
;
2067 src
->image
.type
= ~0;
2071 static GpStatus
free_image_data(GpImage
*image
)
2074 return InvalidParameter
;
2076 if (image
->type
== ImageTypeBitmap
)
2078 heap_free(((GpBitmap
*)image
)->bitmapbits
);
2079 heap_free(((GpBitmap
*)image
)->own_bits
);
2080 DeleteDC(((GpBitmap
*)image
)->hdc
);
2081 DeleteObject(((GpBitmap
*)image
)->hbitmap
);
2082 if (((GpBitmap
*)image
)->metadata_reader
)
2083 IWICMetadataReader_Release(((GpBitmap
*)image
)->metadata_reader
);
2084 heap_free(((GpBitmap
*)image
)->prop_item
);
2086 else if (image
->type
== ImageTypeMetafile
)
2088 GpMetafile
*metafile
= (GpMetafile
*)image
;
2089 heap_free(metafile
->comment_data
);
2090 DeleteEnhMetaFile(CloseEnhMetaFile(metafile
->record_dc
));
2091 if (!metafile
->preserve_hemf
)
2092 DeleteEnhMetaFile(metafile
->hemf
);
2093 if (metafile
->record_graphics
)
2095 WARN("metafile closed while recording\n");
2096 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2097 metafile
->record_graphics
->image
= NULL
;
2098 metafile
->record_graphics
->busy
= TRUE
;
2100 if (metafile
->record_stream
)
2102 IStream_Release(metafile
->record_stream
);
2107 WARN("invalid image: %p\n", image
);
2111 IWICBitmapDecoder_Release(image
->decoder
);
2112 heap_free(image
->palette
);
2117 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
2121 TRACE("%p\n", image
);
2123 status
= free_image_data(image
);
2124 if (status
!= Ok
) return status
;
2131 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
2135 TRACE("(%p,%p)\n", image
, item
);
2138 return InvalidParameter
;
2141 FIXME("not implemented\n");
2143 return NotImplemented
;
2146 GpStatus WINGDIPAPI
GdipGetImageItemData(GpImage
*image
, ImageItemData
*item
)
2150 TRACE("(%p,%p)\n", image
, item
);
2153 FIXME("not implemented\n");
2155 return NotImplemented
;
2158 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
2161 TRACE("%p %p %p\n", image
, srcRect
, srcUnit
);
2163 if(!image
|| !srcRect
|| !srcUnit
)
2164 return InvalidParameter
;
2165 if(image
->type
== ImageTypeMetafile
){
2166 *srcRect
= ((GpMetafile
*)image
)->bounds
;
2167 *srcUnit
= ((GpMetafile
*)image
)->unit
;
2169 else if(image
->type
== ImageTypeBitmap
){
2170 srcRect
->X
= srcRect
->Y
= 0.0;
2171 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
2172 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
2173 *srcUnit
= UnitPixel
;
2176 WARN("GpImage with no image data\n");
2177 return InvalidParameter
;
2180 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
2181 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
2186 GpStatus WINGDIPAPI
GdipGetImageDimension(GpImage
*image
, REAL
*width
,
2189 TRACE("%p %p %p\n", image
, width
, height
);
2191 if(!image
|| !height
|| !width
)
2192 return InvalidParameter
;
2194 if(image
->type
== ImageTypeMetafile
){
2195 *height
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Height
, ((GpMetafile
*)image
)->unit
, image
->yres
);
2196 *width
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Width
, ((GpMetafile
*)image
)->unit
, image
->xres
);
2198 else if(image
->type
== ImageTypeBitmap
){
2199 *height
= ((GpBitmap
*)image
)->height
;
2200 *width
= ((GpBitmap
*)image
)->width
;
2203 WARN("GpImage with no image data\n");
2204 return InvalidParameter
;
2207 TRACE("returning (%f, %f)\n", *height
, *width
);
2211 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
2212 GpGraphics
**graphics
)
2217 TRACE("%p %p\n", image
, graphics
);
2219 if(!image
|| !graphics
)
2220 return InvalidParameter
;
2222 if (image
->type
== ImageTypeBitmap
&& ((GpBitmap
*)image
)->hbitmap
)
2224 hdc
= ((GpBitmap
*)image
)->hdc
;
2227 hdc
= CreateCompatibleDC(0);
2228 SelectObject(hdc
, ((GpBitmap
*)image
)->hbitmap
);
2229 ((GpBitmap
*)image
)->hdc
= hdc
;
2232 stat
= GdipCreateFromHDC(hdc
, graphics
);
2236 (*graphics
)->image
= image
;
2237 (*graphics
)->xres
= image
->xres
;
2238 (*graphics
)->yres
= image
->yres
;
2241 else if (image
->type
== ImageTypeMetafile
)
2242 stat
= METAFILE_GetGraphicsContext((GpMetafile
*)image
, graphics
);
2244 stat
= graphics_from_image(image
, graphics
);
2249 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
2251 TRACE("%p %p\n", image
, height
);
2253 if(!image
|| !height
)
2254 return InvalidParameter
;
2256 if(image
->type
== ImageTypeMetafile
)
2257 *height
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Height
, ((GpMetafile
*)image
)->unit
, image
->yres
);
2258 else if(image
->type
== ImageTypeBitmap
)
2259 *height
= ((GpBitmap
*)image
)->height
;
2262 WARN("GpImage with no image data\n");
2263 return InvalidParameter
;
2266 TRACE("returning %d\n", *height
);
2271 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
2274 return InvalidParameter
;
2278 TRACE("(%p) <-- %0.2f\n", image
, *res
);
2283 GpStatus WINGDIPAPI
GdipGetImagePaletteSize(GpImage
*image
, INT
*size
)
2285 TRACE("%p %p\n", image
, size
);
2288 return InvalidParameter
;
2290 if (!image
->palette
|| image
->palette
->Count
== 0)
2291 *size
= sizeof(ColorPalette
);
2293 *size
= sizeof(UINT
)*2 + sizeof(ARGB
)*image
->palette
->Count
;
2295 TRACE("<-- %u\n", *size
);
2300 /* FIXME: test this function for non-bitmap types */
2301 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
2303 TRACE("%p %p\n", image
, format
);
2305 if(!image
|| !format
)
2306 return InvalidParameter
;
2308 if(image
->type
!= ImageTypeBitmap
)
2309 *format
= PixelFormat24bppRGB
;
2311 *format
= ((GpBitmap
*) image
)->format
;
2316 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
2318 TRACE("(%p, %p)\n", image
, format
);
2320 if(!image
|| !format
)
2321 return InvalidParameter
;
2323 memcpy(format
, &image
->format
, sizeof(GUID
));
2328 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
2330 TRACE("%p %p\n", image
, type
);
2333 return InvalidParameter
;
2335 *type
= image
->type
;
2340 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
2343 return InvalidParameter
;
2347 TRACE("(%p) <-- %0.2f\n", image
, *res
);
2352 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
2354 TRACE("%p %p\n", image
, width
);
2356 if(!image
|| !width
)
2357 return InvalidParameter
;
2359 if(image
->type
== ImageTypeMetafile
)
2360 *width
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Width
, ((GpMetafile
*)image
)->unit
, image
->xres
);
2361 else if(image
->type
== ImageTypeBitmap
)
2362 *width
= ((GpBitmap
*)image
)->width
;
2365 WARN("GpImage with no image data\n");
2366 return InvalidParameter
;
2369 TRACE("returning %d\n", *width
);
2374 GpStatus WINGDIPAPI
GdipGetPropertyCount(GpImage
*image
, UINT
*num
)
2376 TRACE("(%p, %p)\n", image
, num
);
2378 if (!image
|| !num
) return InvalidParameter
;
2382 if (image
->type
== ImageTypeBitmap
)
2384 if (((GpBitmap
*)image
)->prop_item
)
2386 *num
= ((GpBitmap
*)image
)->prop_count
;
2390 if (((GpBitmap
*)image
)->metadata_reader
)
2391 IWICMetadataReader_GetCount(((GpBitmap
*)image
)->metadata_reader
, num
);
2397 GpStatus WINGDIPAPI
GdipGetPropertyIdList(GpImage
*image
, UINT num
, PROPID
*list
)
2400 IWICMetadataReader
*reader
;
2401 IWICEnumMetadataItem
*enumerator
;
2402 UINT prop_count
, i
, items_returned
;
2404 TRACE("(%p, %u, %p)\n", image
, num
, list
);
2406 if (!image
|| !list
) return InvalidParameter
;
2408 if (image
->type
!= ImageTypeBitmap
)
2410 FIXME("Not implemented for type %d\n", image
->type
);
2411 return NotImplemented
;
2414 if (((GpBitmap
*)image
)->prop_item
)
2416 if (num
!= ((GpBitmap
*)image
)->prop_count
) return InvalidParameter
;
2418 for (i
= 0; i
< num
; i
++)
2420 list
[i
] = ((GpBitmap
*)image
)->prop_item
[i
].id
;
2426 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2429 if (num
!= 0) return InvalidParameter
;
2433 hr
= IWICMetadataReader_GetCount(reader
, &prop_count
);
2434 if (FAILED(hr
)) return hresult_to_status(hr
);
2436 if (num
!= prop_count
) return InvalidParameter
;
2438 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2439 if (FAILED(hr
)) return hresult_to_status(hr
);
2441 IWICEnumMetadataItem_Reset(enumerator
);
2443 for (i
= 0; i
< num
; i
++)
2447 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, NULL
, &items_returned
);
2448 if (hr
!= S_OK
) break;
2450 if (id
.vt
!= VT_UI2
)
2452 FIXME("not supported propvariant type for id: %u\n", id
.vt
);
2456 list
[i
] = id
.u
.uiVal
;
2459 IWICEnumMetadataItem_Release(enumerator
);
2461 return hr
== S_OK
? Ok
: hresult_to_status(hr
);
2464 static UINT
propvariant_size(PROPVARIANT
*value
)
2466 switch (value
->vt
& ~VT_VECTOR
)
2472 if (!(value
->vt
& VT_VECTOR
)) return 1;
2473 return value
->u
.caub
.cElems
;
2476 if (!(value
->vt
& VT_VECTOR
)) return 2;
2477 return value
->u
.caui
.cElems
* 2;
2481 if (!(value
->vt
& VT_VECTOR
)) return 4;
2482 return value
->u
.caul
.cElems
* 4;
2486 if (!(value
->vt
& VT_VECTOR
)) return 8;
2487 return value
->u
.cauh
.cElems
* 8;
2489 return value
->u
.pszVal
? strlen(value
->u
.pszVal
) + 1 : 0;
2491 return value
->u
.blob
.cbSize
;
2493 FIXME("not supported variant type %d\n", value
->vt
);
2498 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID propid
, UINT
*size
)
2501 IWICMetadataReader
*reader
;
2502 PROPVARIANT id
, value
;
2504 TRACE("(%p,%#x,%p)\n", image
, propid
, size
);
2506 if (!size
|| !image
) return InvalidParameter
;
2508 if (image
->type
!= ImageTypeBitmap
)
2510 FIXME("Not implemented for type %d\n", image
->type
);
2511 return NotImplemented
;
2514 if (((GpBitmap
*)image
)->prop_item
)
2518 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2520 if (propid
== ((GpBitmap
*)image
)->prop_item
[i
].id
)
2522 *size
= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
;
2527 return PropertyNotFound
;
2530 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2531 if (!reader
) return PropertyNotFound
;
2534 id
.u
.uiVal
= propid
;
2535 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
2536 if (FAILED(hr
)) return PropertyNotFound
;
2538 *size
= propvariant_size(&value
);
2539 if (*size
) *size
+= sizeof(PropertyItem
);
2540 PropVariantClear(&value
);
2545 #ifndef PropertyTagTypeSByte
2546 #define PropertyTagTypeSByte 6
2547 #define PropertyTagTypeSShort 8
2548 #define PropertyTagTypeFloat 11
2549 #define PropertyTagTypeDouble 12
2552 static UINT
vt_to_itemtype(UINT vt
)
2559 { VT_I1
, PropertyTagTypeSByte
},
2560 { VT_UI1
, PropertyTagTypeByte
},
2561 { VT_I2
, PropertyTagTypeSShort
},
2562 { VT_UI2
, PropertyTagTypeShort
},
2563 { VT_I4
, PropertyTagTypeSLONG
},
2564 { VT_UI4
, PropertyTagTypeLong
},
2565 { VT_I8
, PropertyTagTypeSRational
},
2566 { VT_UI8
, PropertyTagTypeRational
},
2567 { VT_R4
, PropertyTagTypeFloat
},
2568 { VT_R8
, PropertyTagTypeDouble
},
2569 { VT_LPSTR
, PropertyTagTypeASCII
},
2570 { VT_BLOB
, PropertyTagTypeUndefined
}
2573 for (i
= 0; i
< sizeof(vt2type
)/sizeof(vt2type
[0]); i
++)
2575 if (vt2type
[i
].vt
== vt
) return vt2type
[i
].type
;
2577 FIXME("not supported variant type %u\n", vt
);
2581 static GpStatus
propvariant_to_item(PROPVARIANT
*value
, PropertyItem
*item
,
2582 UINT size
, PROPID id
)
2584 UINT item_size
, item_type
;
2586 item_size
= propvariant_size(value
);
2587 if (size
!= item_size
+ sizeof(PropertyItem
)) return InvalidParameter
;
2589 item_type
= vt_to_itemtype(value
->vt
& ~VT_VECTOR
);
2590 if (!item_type
) return InvalidParameter
;
2592 item
->value
= item
+ 1;
2594 switch (value
->vt
& ~VT_VECTOR
)
2598 if (!(value
->vt
& VT_VECTOR
))
2599 *(BYTE
*)item
->value
= value
->u
.bVal
;
2601 memcpy(item
->value
, value
->u
.caub
.pElems
, item_size
);
2605 if (!(value
->vt
& VT_VECTOR
))
2606 *(USHORT
*)item
->value
= value
->u
.uiVal
;
2608 memcpy(item
->value
, value
->u
.caui
.pElems
, item_size
);
2613 if (!(value
->vt
& VT_VECTOR
))
2614 *(ULONG
*)item
->value
= value
->u
.ulVal
;
2616 memcpy(item
->value
, value
->u
.caul
.pElems
, item_size
);
2621 if (!(value
->vt
& VT_VECTOR
))
2622 *(ULONGLONG
*)item
->value
= value
->u
.uhVal
.QuadPart
;
2624 memcpy(item
->value
, value
->u
.cauh
.pElems
, item_size
);
2627 memcpy(item
->value
, value
->u
.pszVal
, item_size
);
2630 memcpy(item
->value
, value
->u
.blob
.pBlobData
, item_size
);
2633 FIXME("not supported variant type %d\n", value
->vt
);
2634 return InvalidParameter
;
2637 item
->length
= item_size
;
2638 item
->type
= item_type
;
2644 GpStatus WINGDIPAPI
GdipGetPropertyItem(GpImage
*image
, PROPID propid
, UINT size
,
2645 PropertyItem
*buffer
)
2649 IWICMetadataReader
*reader
;
2650 PROPVARIANT id
, value
;
2652 TRACE("(%p,%#x,%u,%p)\n", image
, propid
, size
, buffer
);
2654 if (!image
|| !buffer
) return InvalidParameter
;
2656 if (image
->type
!= ImageTypeBitmap
)
2658 FIXME("Not implemented for type %d\n", image
->type
);
2659 return NotImplemented
;
2662 if (((GpBitmap
*)image
)->prop_item
)
2666 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2668 if (propid
== ((GpBitmap
*)image
)->prop_item
[i
].id
)
2670 if (size
!= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
)
2671 return InvalidParameter
;
2673 *buffer
= ((GpBitmap
*)image
)->prop_item
[i
];
2674 buffer
->value
= buffer
+ 1;
2675 memcpy(buffer
->value
, ((GpBitmap
*)image
)->prop_item
[i
].value
, buffer
->length
);
2680 return PropertyNotFound
;
2683 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2684 if (!reader
) return PropertyNotFound
;
2687 id
.u
.uiVal
= propid
;
2688 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
2689 if (FAILED(hr
)) return PropertyNotFound
;
2691 stat
= propvariant_to_item(&value
, buffer
, size
, propid
);
2692 PropVariantClear(&value
);
2697 GpStatus WINGDIPAPI
GdipGetPropertySize(GpImage
*image
, UINT
*size
, UINT
*count
)
2700 IWICMetadataReader
*reader
;
2701 IWICEnumMetadataItem
*enumerator
;
2702 UINT prop_count
, prop_size
, i
;
2703 PROPVARIANT id
, value
;
2705 TRACE("(%p,%p,%p)\n", image
, size
, count
);
2707 if (!image
|| !size
|| !count
) return InvalidParameter
;
2709 if (image
->type
!= ImageTypeBitmap
)
2711 FIXME("Not implemented for type %d\n", image
->type
);
2712 return NotImplemented
;
2715 if (((GpBitmap
*)image
)->prop_item
)
2717 *count
= ((GpBitmap
*)image
)->prop_count
;
2720 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2722 *size
+= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
;
2728 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2729 if (!reader
) return PropertyNotFound
;
2731 hr
= IWICMetadataReader_GetCount(reader
, &prop_count
);
2732 if (FAILED(hr
)) return hresult_to_status(hr
);
2734 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2735 if (FAILED(hr
)) return hresult_to_status(hr
);
2737 IWICEnumMetadataItem_Reset(enumerator
);
2741 PropVariantInit(&id
);
2742 PropVariantInit(&value
);
2744 for (i
= 0; i
< prop_count
; i
++)
2746 UINT items_returned
, item_size
;
2748 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, &value
, &items_returned
);
2749 if (hr
!= S_OK
) break;
2751 item_size
= propvariant_size(&value
);
2752 if (item_size
) prop_size
+= sizeof(PropertyItem
) + item_size
;
2754 PropVariantClear(&id
);
2755 PropVariantClear(&value
);
2758 IWICEnumMetadataItem_Release(enumerator
);
2760 if (hr
!= S_OK
) return PropertyNotFound
;
2762 *count
= prop_count
;
2767 GpStatus WINGDIPAPI
GdipGetAllPropertyItems(GpImage
*image
, UINT size
,
2768 UINT count
, PropertyItem
*buf
)
2772 IWICMetadataReader
*reader
;
2773 IWICEnumMetadataItem
*enumerator
;
2774 UINT prop_count
, prop_size
, i
;
2775 PROPVARIANT id
, value
;
2778 TRACE("(%p,%u,%u,%p)\n", image
, size
, count
, buf
);
2780 if (!image
|| !buf
) return InvalidParameter
;
2782 if (image
->type
!= ImageTypeBitmap
)
2784 FIXME("Not implemented for type %d\n", image
->type
);
2785 return NotImplemented
;
2788 status
= GdipGetPropertySize(image
, &prop_size
, &prop_count
);
2789 if (status
!= Ok
) return status
;
2791 if (prop_count
!= count
|| prop_size
!= size
) return InvalidParameter
;
2793 if (((GpBitmap
*)image
)->prop_item
)
2795 memcpy(buf
, ((GpBitmap
*)image
)->prop_item
, prop_size
);
2797 item_value
= (char *)(buf
+ prop_count
);
2799 for (i
= 0; i
< prop_count
; i
++)
2801 buf
[i
].value
= item_value
;
2802 item_value
+= buf
[i
].length
;
2808 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2809 if (!reader
) return PropertyNotFound
;
2811 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2812 if (FAILED(hr
)) return hresult_to_status(hr
);
2814 IWICEnumMetadataItem_Reset(enumerator
);
2816 item_value
= (char *)(buf
+ prop_count
);
2818 PropVariantInit(&id
);
2819 PropVariantInit(&value
);
2821 for (i
= 0; i
< prop_count
; i
++)
2824 UINT items_returned
, item_size
;
2826 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, &value
, &items_returned
);
2827 if (hr
!= S_OK
) break;
2829 if (id
.vt
!= VT_UI2
)
2831 FIXME("not supported propvariant type for id: %u\n", id
.vt
);
2835 item_size
= propvariant_size(&value
);
2838 item
= heap_alloc(item_size
+ sizeof(*item
));
2840 propvariant_to_item(&value
, item
, item_size
+ sizeof(*item
), id
.u
.uiVal
);
2841 buf
[i
].id
= item
->id
;
2842 buf
[i
].type
= item
->type
;
2843 buf
[i
].length
= item_size
;
2844 buf
[i
].value
= item_value
;
2845 memcpy(item_value
, item
->value
, item_size
);
2846 item_value
+= item_size
;
2851 PropVariantClear(&id
);
2852 PropVariantClear(&value
);
2855 IWICEnumMetadataItem_Release(enumerator
);
2857 if (hr
!= S_OK
) return PropertyNotFound
;
2862 struct image_format_dimension
2865 const GUID
*dimension
;
2868 static const struct image_format_dimension image_format_dimensions
[] =
2870 {&ImageFormatGIF
, &FrameDimensionTime
},
2871 {&ImageFormatIcon
, &FrameDimensionResolution
},
2875 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
2876 GDIPCONST GUID
* dimensionID
, UINT
* count
)
2878 TRACE("(%p,%s,%p)\n", image
, debugstr_guid(dimensionID
), count
);
2880 if(!image
|| !count
)
2881 return InvalidParameter
;
2884 IsEqualGUID(dimensionID
, &image
->format
) ||
2885 IsEqualGUID(dimensionID
, &FrameDimensionPage
) ||
2886 IsEqualGUID(dimensionID
, &FrameDimensionTime
))
2888 *count
= image
->frame_count
;
2892 return InvalidParameter
;
2895 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsCount(GpImage
*image
,
2898 TRACE("(%p, %p)\n", image
, count
);
2900 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2902 if(!image
|| !count
)
2903 return InvalidParameter
;
2910 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
2911 GUID
* dimensionIDs
, UINT count
)
2914 const GUID
*result
=NULL
;
2916 TRACE("(%p,%p,%u)\n", image
, dimensionIDs
, count
);
2918 if(!image
|| !dimensionIDs
|| count
!= 1)
2919 return InvalidParameter
;
2921 for (i
=0; image_format_dimensions
[i
].format
; i
++)
2923 if (IsEqualGUID(&image
->format
, image_format_dimensions
[i
].format
))
2925 result
= image_format_dimensions
[i
].dimension
;
2931 result
= &FrameDimensionPage
;
2933 memcpy(dimensionIDs
, result
, sizeof(GUID
));
2938 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
2944 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
2946 if (!filename
|| !image
)
2947 return InvalidParameter
;
2951 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
2956 stat
= GdipLoadImageFromStream(stream
, image
);
2958 IStream_Release(stream
);
2963 /* FIXME: no icm handling */
2964 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
2966 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
2968 return GdipLoadImageFromFile(filename
, image
);
2971 static void add_property(GpBitmap
*bitmap
, PropertyItem
*item
)
2973 UINT prop_size
, prop_count
;
2974 PropertyItem
*prop_item
;
2976 if (bitmap
->prop_item
== NULL
)
2978 prop_size
= prop_count
= 0;
2979 prop_item
= heap_alloc_zero(item
->length
+ sizeof(PropertyItem
));
2980 if (!prop_item
) return;
2987 GdipGetPropertySize(&bitmap
->image
, &prop_size
, &prop_count
);
2989 prop_item
= heap_alloc_zero(prop_size
+ item
->length
+ sizeof(PropertyItem
));
2990 if (!prop_item
) return;
2991 memcpy(prop_item
, bitmap
->prop_item
, sizeof(PropertyItem
) * bitmap
->prop_count
);
2992 prop_size
-= sizeof(PropertyItem
) * bitmap
->prop_count
;
2993 memcpy(prop_item
+ prop_count
+ 1, bitmap
->prop_item
+ prop_count
, prop_size
);
2995 item_value
= (char *)(prop_item
+ prop_count
+ 1);
2997 for (i
= 0; i
< prop_count
; i
++)
2999 prop_item
[i
].value
= item_value
;
3000 item_value
+= prop_item
[i
].length
;
3004 prop_item
[prop_count
].id
= item
->id
;
3005 prop_item
[prop_count
].type
= item
->type
;
3006 prop_item
[prop_count
].length
= item
->length
;
3007 prop_item
[prop_count
].value
= (char *)(prop_item
+ prop_count
+ 1) + prop_size
;
3008 memcpy(prop_item
[prop_count
].value
, item
->value
, item
->length
);
3010 heap_free(bitmap
->prop_item
);
3011 bitmap
->prop_item
= prop_item
;
3012 bitmap
->prop_count
++;
3015 static BOOL
get_bool_property(IWICMetadataReader
*reader
, const GUID
*guid
, const WCHAR
*prop_name
)
3019 PROPVARIANT id
, value
;
3022 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3023 if (FAILED(hr
) || !IsEqualGUID(&format
, guid
)) return FALSE
;
3025 PropVariantInit(&id
);
3026 PropVariantInit(&value
);
3029 id
.u
.pwszVal
= CoTaskMemAlloc((lstrlenW(prop_name
) + 1) * sizeof(WCHAR
));
3030 if (!id
.u
.pwszVal
) return FALSE
;
3031 lstrcpyW(id
.u
.pwszVal
, prop_name
);
3032 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
3033 if (hr
== S_OK
&& value
.vt
== VT_BOOL
)
3034 ret
= value
.u
.boolVal
;
3036 PropVariantClear(&id
);
3037 PropVariantClear(&value
);
3042 static PropertyItem
*get_property(IWICMetadataReader
*reader
, const GUID
*guid
, const WCHAR
*prop_name
)
3046 PROPVARIANT id
, value
;
3047 PropertyItem
*item
= NULL
;
3049 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3050 if (FAILED(hr
) || !IsEqualGUID(&format
, guid
)) return NULL
;
3052 PropVariantInit(&id
);
3053 PropVariantInit(&value
);
3056 id
.u
.pwszVal
= CoTaskMemAlloc((lstrlenW(prop_name
) + 1) * sizeof(WCHAR
));
3057 if (!id
.u
.pwszVal
) return NULL
;
3058 lstrcpyW(id
.u
.pwszVal
, prop_name
);
3059 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
3062 UINT item_size
= propvariant_size(&value
);
3065 item_size
+= sizeof(*item
);
3066 item
= heap_alloc_zero(item_size
);
3067 if (propvariant_to_item(&value
, item
, item_size
, 0) != Ok
)
3075 PropVariantClear(&id
);
3076 PropVariantClear(&value
);
3081 static PropertyItem
*get_gif_comment(IWICMetadataReader
*reader
)
3083 static const WCHAR textentryW
[] = { 'T','e','x','t','E','n','t','r','y',0 };
3084 PropertyItem
*comment
;
3086 comment
= get_property(reader
, &GUID_MetadataFormatGifComment
, textentryW
);
3088 comment
->id
= PropertyTagExifUserComment
;
3093 static PropertyItem
*get_gif_loopcount(IWICMetadataReader
*reader
)
3095 static const WCHAR applicationW
[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3096 static const WCHAR dataW
[] = { 'D','a','t','a',0 };
3097 PropertyItem
*appext
= NULL
, *appdata
= NULL
, *loop
= NULL
;
3099 appext
= get_property(reader
, &GUID_MetadataFormatAPE
, applicationW
);
3102 if (appext
->type
== PropertyTagTypeByte
&& appext
->length
== 11 &&
3103 (!memcmp(appext
->value
, "NETSCAPE2.0", 11) || !memcmp(appext
->value
, "ANIMEXTS1.0", 11)))
3105 appdata
= get_property(reader
, &GUID_MetadataFormatAPE
, dataW
);
3108 if (appdata
->type
== PropertyTagTypeByte
&& appdata
->length
== 4)
3110 BYTE
*data
= appdata
->value
;
3111 if (data
[0] == 3 && data
[1] == 1)
3113 loop
= heap_alloc_zero(sizeof(*loop
) + sizeof(SHORT
));
3116 loop
->type
= PropertyTagTypeShort
;
3117 loop
->id
= PropertyTagLoopCount
;
3118 loop
->length
= sizeof(SHORT
);
3119 loop
->value
= loop
+ 1;
3120 *(SHORT
*)loop
->value
= data
[2] | (data
[3] << 8);
3134 static PropertyItem
*get_gif_background(IWICMetadataReader
*reader
)
3136 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 };
3137 PropertyItem
*background
;
3139 background
= get_property(reader
, &GUID_MetadataFormatLSD
, backgroundW
);
3141 background
->id
= PropertyTagIndexBackground
;
3146 static PropertyItem
*get_gif_palette(IWICBitmapDecoder
*decoder
, IWICMetadataReader
*reader
)
3148 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 };
3150 IWICImagingFactory
*factory
;
3151 IWICPalette
*palette
;
3153 WICColor colors
[256];
3155 if (!get_bool_property(reader
, &GUID_MetadataFormatLSD
, global_flagW
))
3158 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
3159 if (hr
!= S_OK
) return NULL
;
3161 hr
= IWICImagingFactory_CreatePalette(factory
, &palette
);
3164 hr
= IWICBitmapDecoder_CopyPalette(decoder
, palette
);
3166 IWICPalette_GetColors(palette
, 256, colors
, &count
);
3168 IWICPalette_Release(palette
);
3171 IWICImagingFactory_Release(factory
);
3179 pal
= heap_alloc_zero(sizeof(*pal
) + count
* 3);
3180 if (!pal
) return NULL
;
3181 pal
->type
= PropertyTagTypeByte
;
3182 pal
->id
= PropertyTagGlobalPalette
;
3183 pal
->value
= pal
+ 1;
3184 pal
->length
= count
* 3;
3188 for (i
= 0; i
< count
; i
++)
3190 rgb
[i
*3] = (colors
[i
] >> 16) & 0xff;
3191 rgb
[i
*3 + 1] = (colors
[i
] >> 8) & 0xff;
3192 rgb
[i
*3 + 2] = colors
[i
] & 0xff;
3201 static PropertyItem
*get_gif_transparent_idx(IWICMetadataReader
*reader
)
3203 static const WCHAR transparency_flagW
[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3204 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 };
3205 PropertyItem
*index
= NULL
;
3207 if (get_bool_property(reader
, &GUID_MetadataFormatGCE
, transparency_flagW
))
3209 index
= get_property(reader
, &GUID_MetadataFormatGCE
, colorW
);
3211 index
->id
= PropertyTagIndexTransparent
;
3216 static LONG
get_gif_frame_property(IWICBitmapFrameDecode
*frame
, const GUID
*format
, const WCHAR
*property
)
3219 IWICMetadataBlockReader
*block_reader
;
3220 IWICMetadataReader
*reader
;
3221 UINT block_count
, i
;
3225 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3228 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3231 for (i
= 0; i
< block_count
; i
++)
3233 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3236 prop
= get_property(reader
, format
, property
);
3239 if (prop
->type
== PropertyTagTypeByte
&& prop
->length
== 1)
3240 value
= *(BYTE
*)prop
->value
;
3241 else if (prop
->type
== PropertyTagTypeShort
&& prop
->length
== 2)
3242 value
= *(SHORT
*)prop
->value
;
3246 IWICMetadataReader_Release(reader
);
3250 IWICMetadataBlockReader_Release(block_reader
);
3256 static void gif_metadata_reader(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT active_frame
)
3258 static const WCHAR delayW
[] = { 'D','e','l','a','y',0 };
3260 IWICBitmapFrameDecode
*frame
;
3261 IWICMetadataBlockReader
*block_reader
;
3262 IWICMetadataReader
*reader
;
3263 UINT frame_count
, block_count
, i
;
3264 PropertyItem
*delay
= NULL
, *comment
= NULL
, *background
= NULL
;
3265 PropertyItem
*transparent_idx
= NULL
, *loop
= NULL
, *palette
= NULL
;
3267 IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
3268 if (frame_count
> 1)
3270 delay
= heap_alloc_zero(sizeof(*delay
) + frame_count
* sizeof(LONG
));
3275 delay
->type
= PropertyTagTypeLong
;
3276 delay
->id
= PropertyTagFrameDelay
;
3277 delay
->length
= frame_count
* sizeof(LONG
);
3278 delay
->value
= delay
+ 1;
3280 value
= delay
->value
;
3282 for (i
= 0; i
< frame_count
; i
++)
3284 hr
= IWICBitmapDecoder_GetFrame(decoder
, i
, &frame
);
3287 value
[i
] = get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, delayW
);
3288 IWICBitmapFrameDecode_Release(frame
);
3295 hr
= IWICBitmapDecoder_QueryInterface(decoder
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3298 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3301 for (i
= 0; i
< block_count
; i
++)
3303 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3307 comment
= get_gif_comment(reader
);
3309 if (frame_count
> 1 && !loop
)
3310 loop
= get_gif_loopcount(reader
);
3313 background
= get_gif_background(reader
);
3316 palette
= get_gif_palette(decoder
, reader
);
3318 IWICMetadataReader_Release(reader
);
3322 IWICMetadataBlockReader_Release(block_reader
);
3325 if (frame_count
> 1 && !loop
)
3327 loop
= heap_alloc_zero(sizeof(*loop
) + sizeof(SHORT
));
3330 loop
->type
= PropertyTagTypeShort
;
3331 loop
->id
= PropertyTagLoopCount
;
3332 loop
->length
= sizeof(SHORT
);
3333 loop
->value
= loop
+ 1;
3334 *(SHORT
*)loop
->value
= 1;
3338 if (delay
) add_property(bitmap
, delay
);
3339 if (comment
) add_property(bitmap
, comment
);
3340 if (loop
) add_property(bitmap
, loop
);
3341 if (palette
) add_property(bitmap
, palette
);
3342 if (background
) add_property(bitmap
, background
);
3348 heap_free(background
);
3350 /* Win7 gdiplus always returns transparent color index from frame 0 */
3351 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
3352 if (hr
!= S_OK
) return;
3354 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3357 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3360 for (i
= 0; i
< block_count
; i
++)
3362 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3365 if (!transparent_idx
)
3366 transparent_idx
= get_gif_transparent_idx(reader
);
3368 IWICMetadataReader_Release(reader
);
3372 IWICMetadataBlockReader_Release(block_reader
);
3375 if (transparent_idx
) add_property(bitmap
, transparent_idx
);
3376 heap_free(transparent_idx
);
3378 IWICBitmapFrameDecode_Release(frame
);
3381 static PropertyItem
* create_prop(PROPID propid
, PROPVARIANT
* value
)
3383 PropertyItem
*item
= NULL
;
3384 UINT item_size
= propvariant_size(value
);
3388 item_size
+= sizeof(*item
);
3389 item
= heap_alloc_zero(item_size
);
3390 if (propvariant_to_item(value
, item
, item_size
, propid
) != Ok
)
3400 static ULONG
get_ulong_by_index(IWICMetadataReader
* reader
, ULONG index
)
3406 hr
= IWICMetadataReader_GetValueByIndex(reader
, index
, NULL
, NULL
, &value
);
3412 result
= value
.u
.ulVal
;
3415 ERR("unhandled case %u\n", value
.vt
);
3418 PropVariantClear(&value
);
3423 static void png_metadata_reader(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT active_frame
)
3426 IWICBitmapFrameDecode
*frame
;
3427 IWICMetadataBlockReader
*block_reader
;
3428 IWICMetadataReader
*reader
;
3429 UINT block_count
, i
, j
;
3430 struct keyword_info
{
3435 { "Title", PropertyTagImageTitle
},
3436 { "Author", PropertyTagArtist
},
3437 { "Description", PropertyTagImageDescription
},
3438 { "Copyright", PropertyTagCopyright
},
3439 { "Software", PropertyTagSoftwareUsed
},
3440 { "Source", PropertyTagEquipModel
},
3441 { "Comment", PropertyTagExifUserComment
},
3443 BOOL seen_gamma
=FALSE
, seen_whitepoint
=FALSE
, seen_chrm
=FALSE
;
3445 hr
= IWICBitmapDecoder_GetFrame(decoder
, active_frame
, &frame
);
3446 if (hr
!= S_OK
) return;
3448 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3451 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3454 for (i
= 0; i
< block_count
; i
++)
3456 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3461 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3462 if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunktEXt
, &format
))
3464 PROPVARIANT name
, value
;
3467 hr
= IWICMetadataReader_GetValueByIndex(reader
, 0, NULL
, &name
, &value
);
3471 if (name
.vt
== VT_LPSTR
)
3473 for (j
=0; j
<sizeof(keywords
)/sizeof(keywords
[0]); j
++)
3474 if (!strcmp(keywords
[j
].name
, name
.u
.pszVal
))
3476 if (j
< sizeof(keywords
)/sizeof(keywords
[0]) && !keywords
[j
].seen
)
3478 keywords
[j
].seen
= TRUE
;
3479 item
= create_prop(keywords
[j
].propid
, &value
);
3481 add_property(bitmap
, item
);
3486 PropVariantClear(&name
);
3487 PropVariantClear(&value
);
3490 else if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA
, &format
))
3496 item
= heap_alloc_zero(sizeof(PropertyItem
) + sizeof(ULONG
) * 2);
3500 item
->length
= sizeof(ULONG
) * 2;
3501 item
->type
= PropertyTagTypeRational
;
3502 item
->id
= PropertyTagGamma
;
3503 rational
= item
->value
= item
+ 1;
3504 rational
[0] = 100000;
3505 rational
[1] = get_ulong_by_index(reader
, 0);
3506 add_property(bitmap
, item
);
3512 else if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunkcHRM
, &format
))
3516 if (!seen_whitepoint
)
3518 item
= GdipAlloc(sizeof(PropertyItem
) + sizeof(ULONG
) * 4);
3522 item
->length
= sizeof(ULONG
) * 4;
3523 item
->type
= PropertyTagTypeRational
;
3524 item
->id
= PropertyTagWhitePoint
;
3525 rational
= item
->value
= item
+ 1;
3526 rational
[0] = get_ulong_by_index(reader
, 0);
3527 rational
[1] = 100000;
3528 rational
[2] = get_ulong_by_index(reader
, 1);
3529 rational
[3] = 100000;
3530 add_property(bitmap
, item
);
3531 seen_whitepoint
= TRUE
;
3537 item
= GdipAlloc(sizeof(PropertyItem
) + sizeof(ULONG
) * 12);
3541 item
->length
= sizeof(ULONG
) * 12;
3542 item
->type
= PropertyTagTypeRational
;
3543 item
->id
= PropertyTagPrimaryChromaticities
;
3544 rational
= item
->value
= item
+ 1;
3545 rational
[0] = get_ulong_by_index(reader
, 2);
3546 rational
[1] = 100000;
3547 rational
[2] = get_ulong_by_index(reader
, 3);
3548 rational
[3] = 100000;
3549 rational
[4] = get_ulong_by_index(reader
, 4);
3550 rational
[5] = 100000;
3551 rational
[6] = get_ulong_by_index(reader
, 5);
3552 rational
[7] = 100000;
3553 rational
[8] = get_ulong_by_index(reader
, 6);
3554 rational
[9] = 100000;
3555 rational
[10] = get_ulong_by_index(reader
, 7);
3556 rational
[11] = 100000;
3557 add_property(bitmap
, item
);
3564 IWICMetadataReader_Release(reader
);
3568 IWICMetadataBlockReader_Release(block_reader
);
3571 IWICBitmapFrameDecode_Release(frame
);
3574 static GpStatus
initialize_decoder_wic(IStream
*stream
, REFGUID container
, IWICBitmapDecoder
**decoder
)
3576 IWICImagingFactory
*factory
;
3579 TRACE("%p,%s\n", stream
, wine_dbgstr_guid(container
));
3581 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
3582 if (FAILED(hr
)) return hresult_to_status(hr
);
3583 hr
= IWICImagingFactory_CreateDecoder(factory
, container
, NULL
, decoder
);
3584 IWICImagingFactory_Release(factory
);
3585 if (FAILED(hr
)) return hresult_to_status(hr
);
3587 hr
= IWICBitmapDecoder_Initialize(*decoder
, stream
, WICDecodeMetadataCacheOnLoad
);
3588 if (FAILED(hr
)) return hresult_to_status(hr
);
3592 typedef void (*metadata_reader_func
)(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT frame
);
3594 static GpStatus
decode_frame_wic(IWICBitmapDecoder
*decoder
, BOOL force_conversion
,
3595 UINT active_frame
, metadata_reader_func metadata_reader
, GpImage
**image
)
3600 IWICBitmapFrameDecode
*frame
;
3601 IWICBitmapSource
*source
=NULL
;
3602 IWICMetadataBlockReader
*block_reader
;
3603 WICPixelFormatGUID wic_format
;
3604 PixelFormat gdip_format
=0;
3605 ColorPalette
*palette
= NULL
;
3606 WICBitmapPaletteType palette_type
= WICBitmapPaletteTypeFixedHalftone256
;
3608 UINT width
, height
, frame_count
;
3609 BitmapData lockeddata
;
3612 TRACE("%p,%u,%p\n", decoder
, active_frame
, image
);
3614 IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
3615 hr
= IWICBitmapDecoder_GetFrame(decoder
, active_frame
, &frame
);
3616 if (SUCCEEDED(hr
)) /* got frame */
3618 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &wic_format
);
3622 if (!force_conversion
)
3624 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
3626 if (IsEqualGUID(&wic_format
, pixel_formats
[i
].wic_format
))
3628 source
= (IWICBitmapSource
*)frame
;
3629 IWICBitmapSource_AddRef(source
);
3630 gdip_format
= pixel_formats
[i
].gdip_format
;
3631 palette_type
= pixel_formats
[i
].palette_type
;
3638 /* unknown format; fall back on 32bppARGB */
3639 hr
= WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA
, (IWICBitmapSource
*)frame
, &source
);
3640 gdip_format
= PixelFormat32bppARGB
;
3642 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format
), gdip_format
);
3645 if (SUCCEEDED(hr
)) /* got source */
3647 hr
= IWICBitmapSource_GetSize(source
, &width
, &height
);
3650 status
= GdipCreateBitmapFromScan0(width
, height
, 0, gdip_format
,
3653 if (SUCCEEDED(hr
) && status
== Ok
) /* created bitmap */
3655 status
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeWrite
,
3656 gdip_format
, &lockeddata
);
3657 if (status
== Ok
) /* locked bitmap */
3662 for (i
=0; i
<height
; i
++)
3665 hr
= IWICBitmapSource_CopyPixels(source
, &wrc
, abs(lockeddata
.Stride
),
3666 abs(lockeddata
.Stride
), (BYTE
*)lockeddata
.Scan0
+lockeddata
.Stride
*i
);
3667 if (FAILED(hr
)) break;
3670 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
3673 if (SUCCEEDED(hr
) && status
== Ok
)
3674 *image
= &bitmap
->image
;
3678 GdipDisposeImage(&bitmap
->image
);
3681 if (SUCCEEDED(hr
) && status
== Ok
)
3684 hr
= IWICBitmapSource_GetResolution(source
, &dpix
, &dpiy
);
3687 bitmap
->image
.xres
= dpix
;
3688 bitmap
->image
.yres
= dpiy
;
3694 IWICBitmapSource_Release(source
);
3697 if (SUCCEEDED(hr
)) {
3698 bitmap
->metadata_reader
= NULL
;
3700 if (metadata_reader
)
3701 metadata_reader(bitmap
, decoder
, active_frame
);
3702 else if (IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
) == S_OK
)
3704 UINT block_count
= 0;
3705 if (IWICMetadataBlockReader_GetCount(block_reader
, &block_count
) == S_OK
&& block_count
)
3706 IWICMetadataBlockReader_GetReaderByIndex(block_reader
, 0, &bitmap
->metadata_reader
);
3707 IWICMetadataBlockReader_Release(block_reader
);
3710 palette
= get_palette(frame
, palette_type
);
3711 IWICBitmapFrameDecode_Release(frame
);
3715 if (FAILED(hr
) && status
== Ok
) status
= hresult_to_status(hr
);
3719 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3720 bitmap
->image
.flags
|= ImageFlagsReadOnly
|ImageFlagsHasRealPixelSize
|ImageFlagsHasRealDPI
|ImageFlagsColorSpaceRGB
;
3721 bitmap
->image
.frame_count
= frame_count
;
3722 bitmap
->image
.current_frame
= active_frame
;
3723 bitmap
->image
.decoder
= decoder
;
3724 IWICBitmapDecoder_AddRef(decoder
);
3727 heap_free(bitmap
->image
.palette
);
3728 bitmap
->image
.palette
= palette
;
3732 if (IsEqualGUID(&wic_format
, &GUID_WICPixelFormatBlackWhite
))
3733 bitmap
->image
.palette
->Flags
= 0;
3735 TRACE("=> %p\n", *image
);
3741 static GpStatus
decode_image_wic(IStream
*stream
, REFGUID container
,
3742 metadata_reader_func metadata_reader
, GpImage
**image
)
3744 IWICBitmapDecoder
*decoder
;
3747 status
= initialize_decoder_wic(stream
, container
, &decoder
);
3751 status
= decode_frame_wic(decoder
, FALSE
, 0, metadata_reader
, image
);
3752 IWICBitmapDecoder_Release(decoder
);
3756 static GpStatus
select_frame_wic(GpImage
*image
, UINT active_frame
)
3761 status
= decode_frame_wic(image
->decoder
, FALSE
, active_frame
, NULL
, &new_image
);
3765 new_image
->busy
= image
->busy
;
3766 memcpy(&new_image
->format
, &image
->format
, sizeof(GUID
));
3767 free_image_data(image
);
3768 if (image
->type
== ImageTypeBitmap
)
3769 *(GpBitmap
*)image
= *(GpBitmap
*)new_image
;
3770 else if (image
->type
== ImageTypeMetafile
)
3771 *(GpMetafile
*)image
= *(GpMetafile
*)new_image
;
3772 new_image
->type
= ~0;
3773 heap_free(new_image
);
3777 static HRESULT
get_gif_frame_rect(IWICBitmapFrameDecode
*frame
,
3778 UINT
*left
, UINT
*top
, UINT
*width
, UINT
*height
)
3780 static const WCHAR leftW
[] = {'L','e','f','t',0};
3781 static const WCHAR topW
[] = {'T','o','p',0};
3783 *left
= get_gif_frame_property(frame
, &GUID_MetadataFormatIMD
, leftW
);
3784 *top
= get_gif_frame_property(frame
, &GUID_MetadataFormatIMD
, topW
);
3786 return IWICBitmapFrameDecode_GetSize(frame
, width
, height
);
3789 static HRESULT
blit_gif_frame(GpBitmap
*bitmap
, IWICBitmapFrameDecode
*frame
, BOOL first_frame
)
3791 UINT i
, j
, left
, top
, width
, height
;
3792 IWICBitmapSource
*source
;
3796 hr
= get_gif_frame_rect(frame
, &left
, &top
, &width
, &height
);
3800 hr
= WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA
, (IWICBitmapSource
*)frame
, &source
);
3804 new_bits
= heap_alloc_zero(width
*height
*4);
3806 return E_OUTOFMEMORY
;
3808 hr
= IWICBitmapSource_CopyPixels(source
, NULL
, width
*4, width
*height
*4, new_bits
);
3809 IWICBitmapSource_Release(source
);
3811 heap_free(new_bits
);
3815 for(i
=0; i
<height
&& i
+top
<bitmap
->height
; i
++) {
3816 for(j
=0; j
<width
&& j
+left
<bitmap
->width
; j
++) {
3817 DWORD
*src
= (DWORD
*)(new_bits
+i
*width
*4+j
*4);
3818 DWORD
*dst
= (DWORD
*)(bitmap
->bits
+(i
+top
)*bitmap
->stride
+(j
+left
)*4);
3820 if(first_frame
|| *src
>>24 != 0)
3824 heap_free(new_bits
);
3828 static DWORD
get_gif_background_color(GpBitmap
*bitmap
)
3830 BYTE bgcolor_idx
= 0;
3833 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3834 if(bitmap
->prop_item
[i
].id
== PropertyTagIndexBackground
) {
3835 bgcolor_idx
= *(BYTE
*)bitmap
->prop_item
[i
].value
;
3840 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3841 if(bitmap
->prop_item
[i
].id
== PropertyTagIndexTransparent
) {
3842 BYTE transparent_idx
;
3843 transparent_idx
= *(BYTE
*)bitmap
->prop_item
[i
].value
;
3845 if(transparent_idx
== bgcolor_idx
)
3850 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3851 if(bitmap
->prop_item
[i
].id
== PropertyTagGlobalPalette
) {
3852 if(bitmap
->prop_item
[i
].length
/3 > bgcolor_idx
) {
3853 BYTE
*color
= ((BYTE
*)bitmap
->prop_item
[i
].value
)+bgcolor_idx
*3;
3854 return color
[2] + (color
[1]<<8) + (color
[0]<<16) + (0xffu
<<24);
3860 FIXME("can't get gif background color\n");
3864 static GpStatus
select_frame_gif(GpImage
* image
, UINT active_frame
)
3866 static const WCHAR disposalW
[] = {'D','i','s','p','o','s','a','l',0};
3868 GpBitmap
*bitmap
= (GpBitmap
*)image
;
3869 IWICBitmapFrameDecode
*frame
;
3870 int cur_frame
=0, disposal
;
3871 BOOL bgcolor_set
= FALSE
;
3875 if(active_frame
> image
->current_frame
) {
3876 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, image
->current_frame
, &frame
);
3878 return hresult_to_status(hr
);
3879 disposal
= get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, disposalW
);
3880 IWICBitmapFrameDecode_Release(frame
);
3882 if(disposal
== GIF_DISPOSE_RESTORE_TO_BKGND
)
3883 cur_frame
= image
->current_frame
;
3884 else if(disposal
!= GIF_DISPOSE_RESTORE_TO_PREV
)
3885 cur_frame
= image
->current_frame
+1;
3888 while(cur_frame
!= active_frame
) {
3889 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, cur_frame
, &frame
);
3891 return hresult_to_status(hr
);
3892 disposal
= get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, disposalW
);
3894 if(disposal
==GIF_DISPOSE_UNSPECIFIED
|| disposal
==GIF_DISPOSE_DO_NOT_DISPOSE
) {
3895 hr
= blit_gif_frame(bitmap
, frame
, cur_frame
==0);
3897 return hresult_to_status(hr
);
3898 }else if(disposal
== GIF_DISPOSE_RESTORE_TO_BKGND
) {
3899 UINT left
, top
, width
, height
, i
, j
;
3902 bgcolor
= get_gif_background_color(bitmap
);
3906 hr
= get_gif_frame_rect(frame
, &left
, &top
, &width
, &height
);
3908 return hresult_to_status(hr
);
3909 for(i
=top
; i
<top
+height
&& i
<bitmap
->height
; i
++) {
3910 DWORD
*bits
= (DWORD
*)(bitmap
->bits
+i
*bitmap
->stride
);
3911 for(j
=left
; j
<left
+width
&& j
<bitmap
->width
; j
++)
3916 IWICBitmapFrameDecode_Release(frame
);
3920 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, active_frame
, &frame
);
3922 return hresult_to_status(hr
);
3924 hr
= blit_gif_frame(bitmap
, frame
, cur_frame
==0);
3925 IWICBitmapFrameDecode_Release(frame
);
3927 return hresult_to_status(hr
);
3929 image
->current_frame
= active_frame
;
3933 static GpStatus
decode_image_icon(IStream
* stream
, GpImage
**image
)
3935 return decode_image_wic(stream
, &GUID_ContainerFormatIco
, NULL
, image
);
3938 static GpStatus
decode_image_bmp(IStream
* stream
, GpImage
**image
)
3943 status
= decode_image_wic(stream
, &GUID_ContainerFormatBmp
, NULL
, image
);
3945 bitmap
= (GpBitmap
*)*image
;
3947 if (status
== Ok
&& bitmap
->format
== PixelFormat32bppARGB
)
3949 /* WIC supports bmp files with alpha, but gdiplus does not */
3950 bitmap
->format
= PixelFormat32bppRGB
;
3956 static GpStatus
decode_image_jpeg(IStream
* stream
, GpImage
**image
)
3958 return decode_image_wic(stream
, &GUID_ContainerFormatJpeg
, NULL
, image
);
3961 static GpStatus
decode_image_png(IStream
* stream
, GpImage
**image
)
3963 return decode_image_wic(stream
, &GUID_ContainerFormatPng
, png_metadata_reader
, image
);
3966 static GpStatus
decode_image_gif(IStream
* stream
, GpImage
**image
)
3968 IWICBitmapDecoder
*decoder
;
3973 status
= initialize_decoder_wic(stream
, &GUID_ContainerFormatGif
, &decoder
);
3977 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
3979 return hresult_to_status(hr
);
3981 status
= decode_frame_wic(decoder
, frame_count
> 1, 0, gif_metadata_reader
, image
);
3982 IWICBitmapDecoder_Release(decoder
);
3986 if(frame_count
> 1) {
3987 heap_free((*image
)->palette
);
3988 (*image
)->palette
= NULL
;
3993 static GpStatus
decode_image_tiff(IStream
* stream
, GpImage
**image
)
3995 return decode_image_wic(stream
, &GUID_ContainerFormatTiff
, NULL
, image
);
3998 static GpStatus
load_wmf(IStream
*stream
, GpMetafile
**metafile
)
4000 WmfPlaceableFileHeader pfh
;
4001 BOOL is_placeable
= FALSE
;
4010 hr
= IStream_Read(stream
, &mh
, sizeof(mh
), &size
);
4011 if (hr
!= S_OK
|| size
!= sizeof(mh
))
4012 return GenericError
;
4014 if (((WmfPlaceableFileHeader
*)&mh
)->Key
== WMF_PLACEABLE_KEY
)
4017 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4018 if (FAILED(hr
)) return hresult_to_status(hr
);
4020 hr
= IStream_Read(stream
, &pfh
, sizeof(pfh
), &size
);
4021 if (hr
!= S_OK
|| size
!= sizeof(pfh
))
4022 return GenericError
;
4024 hr
= IStream_Read(stream
, &mh
, sizeof(mh
), &size
);
4025 if (hr
!= S_OK
|| size
!= sizeof(mh
))
4026 return GenericError
;
4028 is_placeable
= TRUE
;
4031 seek
.QuadPart
= is_placeable
? sizeof(pfh
) : 0;
4032 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4033 if (FAILED(hr
)) return hresult_to_status(hr
);
4035 buf
= heap_alloc(mh
.mtSize
* 2);
4036 if (!buf
) return OutOfMemory
;
4038 hr
= IStream_Read(stream
, buf
, mh
.mtSize
* 2, &size
);
4039 if (hr
!= S_OK
|| size
!= mh
.mtSize
* 2)
4042 return GenericError
;
4045 hmf
= SetMetaFileBitsEx(mh
.mtSize
* 2, buf
);
4048 return GenericError
;
4050 status
= GdipCreateMetafileFromWmf(hmf
, TRUE
, is_placeable
? &pfh
: NULL
, metafile
);
4052 DeleteMetaFile(hmf
);
4056 static GpStatus
decode_image_wmf(IStream
*stream
, GpImage
**image
)
4058 GpMetafile
*metafile
;
4061 TRACE("%p %p\n", stream
, image
);
4063 if (!stream
|| !image
)
4064 return InvalidParameter
;
4066 status
= load_wmf(stream
, &metafile
);
4069 TRACE("Could not load metafile\n");
4073 *image
= (GpImage
*)metafile
;
4074 TRACE("<-- %p\n", *image
);
4079 static GpStatus
load_emf(IStream
*stream
, GpMetafile
**metafile
)
4089 hr
= IStream_Read(stream
, &emh
, sizeof(emh
), &size
);
4090 if (hr
!= S_OK
|| size
!= sizeof(emh
) || emh
.dSignature
!= ENHMETA_SIGNATURE
)
4091 return GenericError
;
4094 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4095 if (FAILED(hr
)) return hresult_to_status(hr
);
4097 buf
= heap_alloc(emh
.nBytes
);
4098 if (!buf
) return OutOfMemory
;
4100 hr
= IStream_Read(stream
, buf
, emh
.nBytes
, &size
);
4101 if (hr
!= S_OK
|| size
!= emh
.nBytes
)
4104 return GenericError
;
4107 hemf
= SetEnhMetaFileBits(emh
.nBytes
, buf
);
4110 return GenericError
;
4112 status
= GdipCreateMetafileFromEmf(hemf
, TRUE
, metafile
);
4114 DeleteEnhMetaFile(hemf
);
4118 static GpStatus
decode_image_emf(IStream
*stream
, GpImage
**image
)
4120 GpMetafile
*metafile
;
4123 TRACE("%p %p\n", stream
, image
);
4125 if (!stream
|| !image
)
4126 return InvalidParameter
;
4128 status
= load_emf(stream
, &metafile
);
4131 TRACE("Could not load metafile\n");
4135 *image
= (GpImage
*)metafile
;
4136 TRACE("<-- %p\n", *image
);
4141 typedef GpStatus (*encode_image_func
)(GpImage
*image
, IStream
* stream
,
4142 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
);
4144 typedef GpStatus (*decode_image_func
)(IStream
*stream
, GpImage
**image
);
4146 typedef GpStatus (*select_image_func
)(GpImage
*image
, UINT active_frame
);
4148 typedef struct image_codec
{
4149 ImageCodecInfo info
;
4150 encode_image_func encode_func
;
4151 decode_image_func decode_func
;
4152 select_image_func select_func
;
4167 static const struct image_codec codecs
[NUM_CODECS
];
4169 static GpStatus
get_decoder_info(IStream
* stream
, const struct image_codec
**result
)
4172 const BYTE
*pattern
, *mask
;
4179 /* seek to the start of the stream */
4181 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4182 if (FAILED(hr
)) return hresult_to_status(hr
);
4184 /* read the first 8 bytes */
4185 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4186 hr
= IStream_Read(stream
, signature
, 8, &bytesread
);
4187 if (FAILED(hr
)) return hresult_to_status(hr
);
4188 if (hr
== S_FALSE
|| bytesread
== 0) return GenericError
;
4190 for (i
= 0; i
< NUM_CODECS
; i
++) {
4191 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
) &&
4192 bytesread
>= codecs
[i
].info
.SigSize
)
4194 for (sig
=0; sig
<codecs
[i
].info
.SigCount
; sig
++)
4196 pattern
= &codecs
[i
].info
.SigPattern
[codecs
[i
].info
.SigSize
*sig
];
4197 mask
= &codecs
[i
].info
.SigMask
[codecs
[i
].info
.SigSize
*sig
];
4198 for (j
=0; j
<codecs
[i
].info
.SigSize
; j
++)
4199 if ((signature
[j
] & mask
[j
]) != pattern
[j
])
4201 if (j
== codecs
[i
].info
.SigSize
)
4203 *result
= &codecs
[i
];
4210 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread
,
4211 signature
[0],signature
[1],signature
[2],signature
[3],
4212 signature
[4],signature
[5],signature
[6],signature
[7]);
4214 return GenericError
;
4217 static GpStatus
get_decoder_info_from_image(GpImage
*image
, const struct image_codec
**result
)
4221 for (i
= 0; i
< NUM_CODECS
; i
++) {
4222 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
) &&
4223 IsEqualIID(&codecs
[i
].info
.FormatID
, &image
->format
))
4225 *result
= &codecs
[i
];
4230 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image
->format
));
4231 return GenericError
;
4234 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
, GDIPCONST GUID
*dimensionID
,
4238 const struct image_codec
*codec
= NULL
;
4241 TRACE("(%p,%s,%u)\n", image
, debugstr_guid(dimensionID
), frame
);
4243 if (!image
|| !dimensionID
)
4244 return InvalidParameter
;
4245 if(!image_lock(image
, &unlock
))
4248 if (frame
>= image
->frame_count
)
4250 WARN("requested frame %u, but image has only %u\n", frame
, image
->frame_count
);
4251 image_unlock(image
, unlock
);
4252 return InvalidParameter
;
4255 if (image
->type
!= ImageTypeBitmap
&& image
->type
!= ImageTypeMetafile
)
4257 WARN("invalid image type %d\n", image
->type
);
4258 image_unlock(image
, unlock
);
4259 return InvalidParameter
;
4262 if (image
->current_frame
== frame
)
4264 image_unlock(image
, unlock
);
4268 if (!image
->decoder
)
4270 TRACE("image doesn't have an associated decoder\n");
4271 image_unlock(image
, unlock
);
4275 /* choose an appropriate image decoder */
4276 stat
= get_decoder_info_from_image(image
, &codec
);
4279 WARN("can't find decoder info\n");
4280 image_unlock(image
, unlock
);
4284 stat
= codec
->select_func(image
, frame
);
4285 image_unlock(image
, unlock
);
4289 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
*stream
, GpImage
**image
)
4294 const struct image_codec
*codec
=NULL
;
4296 /* choose an appropriate image decoder */
4297 stat
= get_decoder_info(stream
, &codec
);
4298 if (stat
!= Ok
) return stat
;
4300 /* seek to the start of the stream */
4302 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4303 if (FAILED(hr
)) return hresult_to_status(hr
);
4305 /* call on the image decoder to do the real work */
4306 stat
= codec
->decode_func(stream
, image
);
4308 /* take note of the original data format */
4311 memcpy(&(*image
)->format
, &codec
->info
.FormatID
, sizeof(GUID
));
4319 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
4321 TRACE("%p %p\n", stream
, image
);
4323 return GdipLoadImageFromStream(stream
, image
);
4326 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
4330 TRACE("(%p,%u)\n", image
, propId
);
4333 return InvalidParameter
;
4336 FIXME("not implemented\n");
4338 return NotImplemented
;
4341 GpStatus WINGDIPAPI
GdipSetPropertyItem(GpImage
*image
, GDIPCONST PropertyItem
* item
)
4345 if (!image
|| !item
) return InvalidParameter
;
4347 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image
, item
, item
->id
, item
->type
, item
->length
, item
->value
);
4350 FIXME("not implemented\n");
4355 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
4356 GDIPCONST CLSID
*clsidEncoder
,
4357 GDIPCONST EncoderParameters
*encoderParams
)
4362 TRACE("%p (%s) %p %p\n", image
, debugstr_w(filename
), clsidEncoder
, encoderParams
);
4364 if (!image
|| !filename
|| !clsidEncoder
)
4365 return InvalidParameter
;
4367 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
4369 return GenericError
;
4371 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
4373 IStream_Release(stream
);
4377 /*************************************************************************
4378 * Encoding functions -
4379 * These functions encode an image in different image file formats.
4382 static GpStatus
encode_image_wic(GpImage
*image
, IStream
* stream
,
4383 REFGUID container
, GDIPCONST EncoderParameters
* params
)
4387 IWICImagingFactory
*factory
;
4388 IWICBitmapEncoder
*encoder
;
4389 IWICBitmapFrameEncode
*frameencode
;
4390 IPropertyBag2
*encoderoptions
;
4393 PixelFormat gdipformat
=0;
4394 const WICPixelFormatGUID
*desired_wicformat
;
4395 WICPixelFormatGUID wicformat
;
4397 BitmapData lockeddata
;
4400 if (image
->type
!= ImageTypeBitmap
)
4401 return GenericError
;
4403 bitmap
= (GpBitmap
*)image
;
4405 GdipGetImageWidth(image
, &width
);
4406 GdipGetImageHeight(image
, &height
);
4413 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
4415 return hresult_to_status(hr
);
4416 hr
= IWICImagingFactory_CreateEncoder(factory
, container
, NULL
, &encoder
);
4417 IWICImagingFactory_Release(factory
);
4419 return hresult_to_status(hr
);
4421 hr
= IWICBitmapEncoder_Initialize(encoder
, stream
, WICBitmapEncoderNoCache
);
4425 hr
= IWICBitmapEncoder_CreateNewFrame(encoder
, &frameencode
, &encoderoptions
);
4428 if (SUCCEEDED(hr
)) /* created frame */
4430 hr
= IWICBitmapFrameEncode_Initialize(frameencode
, encoderoptions
);
4433 hr
= IWICBitmapFrameEncode_SetSize(frameencode
, width
, height
);
4436 hr
= IWICBitmapFrameEncode_SetResolution(frameencode
, image
->xres
, image
->yres
);
4440 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
4442 if (pixel_formats
[i
].gdip_format
== bitmap
->format
)
4444 desired_wicformat
= pixel_formats
[i
].wic_format
;
4445 gdipformat
= bitmap
->format
;
4451 desired_wicformat
= &GUID_WICPixelFormat32bppBGRA
;
4452 gdipformat
= PixelFormat32bppARGB
;
4455 memcpy(&wicformat
, desired_wicformat
, sizeof(GUID
));
4456 hr
= IWICBitmapFrameEncode_SetPixelFormat(frameencode
, &wicformat
);
4459 if (SUCCEEDED(hr
) && !IsEqualGUID(desired_wicformat
, &wicformat
))
4461 /* Encoder doesn't support this bitmap's format. */
4463 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
4465 if (IsEqualGUID(&wicformat
, pixel_formats
[i
].wic_format
))
4467 gdipformat
= pixel_formats
[i
].gdip_format
;
4473 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat
));
4480 stat
= GdipBitmapLockBits(bitmap
, &rc
, ImageLockModeRead
, gdipformat
,
4485 UINT row_size
= (lockeddata
.Width
* PIXELFORMATBPP(gdipformat
) + 7)/8;
4488 /* write one row at a time in case stride is negative */
4489 row
= lockeddata
.Scan0
;
4490 for (i
=0; i
<lockeddata
.Height
; i
++)
4492 hr
= IWICBitmapFrameEncode_WritePixels(frameencode
, 1, row_size
, row_size
, row
);
4493 if (FAILED(hr
)) break;
4494 row
+= lockeddata
.Stride
;
4497 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
4504 hr
= IWICBitmapFrameEncode_Commit(frameencode
);
4506 IWICBitmapFrameEncode_Release(frameencode
);
4507 IPropertyBag2_Release(encoderoptions
);
4511 hr
= IWICBitmapEncoder_Commit(encoder
);
4513 IWICBitmapEncoder_Release(encoder
);
4514 return hresult_to_status(hr
);
4517 static GpStatus
encode_image_BMP(GpImage
*image
, IStream
* stream
,
4518 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
4520 return encode_image_wic(image
, stream
, &GUID_ContainerFormatBmp
, params
);
4523 static GpStatus
encode_image_tiff(GpImage
*image
, IStream
* stream
,
4524 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
4526 return encode_image_wic(image
, stream
, &GUID_ContainerFormatTiff
, params
);
4529 static GpStatus
encode_image_png(GpImage
*image
, IStream
* stream
,
4530 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
4532 return encode_image_wic(image
, stream
, &GUID_ContainerFormatPng
, params
);
4535 static GpStatus
encode_image_jpeg(GpImage
*image
, IStream
* stream
,
4536 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
4538 return encode_image_wic(image
, stream
, &GUID_ContainerFormatJpeg
, params
);
4541 static GpStatus
encode_image_gif(GpImage
*image
, IStream
* stream
,
4542 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
4544 return encode_image_wic(image
, stream
, &CLSID_WICGifEncoder
, params
);
4547 /*****************************************************************************
4548 * GdipSaveImageToStream [GDIPLUS.@]
4550 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
4551 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
4554 encode_image_func encode_image
;
4557 TRACE("%p %p %p %p\n", image
, stream
, clsid
, params
);
4559 if(!image
|| !stream
)
4560 return InvalidParameter
;
4562 /* select correct encoder */
4563 encode_image
= NULL
;
4564 for (i
= 0; i
< NUM_CODECS
; i
++) {
4565 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
) &&
4566 IsEqualCLSID(clsid
, &codecs
[i
].info
.Clsid
))
4567 encode_image
= codecs
[i
].encode_func
;
4569 if (encode_image
== NULL
)
4570 return UnknownImageFormat
;
4572 stat
= encode_image(image
, stream
, clsid
, params
);
4577 /*****************************************************************************
4578 * GdipSaveAdd [GDIPLUS.@]
4580 GpStatus WINGDIPAPI
GdipSaveAdd(GpImage
*image
, GDIPCONST EncoderParameters
*params
)
4582 FIXME("(%p,%p): stub\n", image
, params
);
4586 /*****************************************************************************
4587 * GdipGetImagePalette [GDIPLUS.@]
4589 GpStatus WINGDIPAPI
GdipGetImagePalette(GpImage
*image
, ColorPalette
*palette
, INT size
)
4593 TRACE("(%p,%p,%i)\n", image
, palette
, size
);
4595 if (!image
|| !palette
)
4596 return InvalidParameter
;
4598 count
= image
->palette
? image
->palette
->Count
: 0;
4600 if (size
< (sizeof(UINT
)*2+sizeof(ARGB
)*count
))
4602 TRACE("<-- InsufficientBuffer\n");
4603 return InsufficientBuffer
;
4608 palette
->Flags
= image
->palette
->Flags
;
4609 palette
->Count
= image
->palette
->Count
;
4610 memcpy(palette
->Entries
, image
->palette
->Entries
, sizeof(ARGB
)*image
->palette
->Count
);
4620 /*****************************************************************************
4621 * GdipSetImagePalette [GDIPLUS.@]
4623 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
4624 GDIPCONST ColorPalette
*palette
)
4626 ColorPalette
*new_palette
;
4628 TRACE("(%p,%p)\n", image
, palette
);
4630 if(!image
|| !palette
|| palette
->Count
> 256)
4631 return InvalidParameter
;
4633 new_palette
= heap_alloc_zero(2 * sizeof(UINT
) + palette
->Count
* sizeof(ARGB
));
4634 if (!new_palette
) return OutOfMemory
;
4636 heap_free(image
->palette
);
4637 image
->palette
= new_palette
;
4638 image
->palette
->Flags
= palette
->Flags
;
4639 image
->palette
->Count
= palette
->Count
;
4640 memcpy(image
->palette
->Entries
, palette
->Entries
, sizeof(ARGB
)*palette
->Count
);
4645 /*************************************************************************
4647 * Structures that represent which formats we support for encoding.
4650 /* ImageCodecInfo creation routines taken from libgdiplus */
4651 static const WCHAR bmp_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4652 static const WCHAR bmp_extension
[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4653 static const WCHAR bmp_mimetype
[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4654 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0}; /* BMP */
4655 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
4656 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
4658 static const WCHAR jpeg_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4659 static const WCHAR jpeg_extension
[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4660 static const WCHAR jpeg_mimetype
[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4661 static const WCHAR jpeg_format
[] = {'J','P','E','G',0};
4662 static const BYTE jpeg_sig_pattern
[] = { 0xFF, 0xD8 };
4663 static const BYTE jpeg_sig_mask
[] = { 0xFF, 0xFF };
4665 static const WCHAR gif_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4666 static const WCHAR gif_extension
[] = {'*','.','G','I','F',0};
4667 static const WCHAR gif_mimetype
[] = {'i','m','a','g','e','/','g','i','f', 0};
4668 static const WCHAR gif_format
[] = {'G','I','F',0};
4669 static const BYTE gif_sig_pattern
[12] = "GIF87aGIF89a";
4670 static const BYTE gif_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4672 static const WCHAR tiff_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4673 static const WCHAR tiff_extension
[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4674 static const WCHAR tiff_mimetype
[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4675 static const WCHAR tiff_format
[] = {'T','I','F','F',0};
4676 static const BYTE tiff_sig_pattern
[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4677 static const BYTE tiff_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4679 static const WCHAR emf_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4680 static const WCHAR emf_extension
[] = {'*','.','E','M','F',0};
4681 static const WCHAR emf_mimetype
[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4682 static const WCHAR emf_format
[] = {'E','M','F',0};
4683 static const BYTE emf_sig_pattern
[] = { 0x01, 0x00, 0x00, 0x00 };
4684 static const BYTE emf_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4686 static const WCHAR wmf_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4687 static const WCHAR wmf_extension
[] = {'*','.','W','M','F',0};
4688 static const WCHAR wmf_mimetype
[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4689 static const WCHAR wmf_format
[] = {'W','M','F',0};
4690 static const BYTE wmf_sig_pattern
[] = { 0xd7, 0xcd };
4691 static const BYTE wmf_sig_mask
[] = { 0xFF, 0xFF };
4693 static const WCHAR png_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4694 static const WCHAR png_extension
[] = {'*','.','P','N','G',0};
4695 static const WCHAR png_mimetype
[] = {'i','m','a','g','e','/','p','n','g', 0};
4696 static const WCHAR png_format
[] = {'P','N','G',0};
4697 static const BYTE png_sig_pattern
[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4698 static const BYTE png_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4700 static const WCHAR ico_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4701 static const WCHAR ico_extension
[] = {'*','.','I','C','O',0};
4702 static const WCHAR ico_mimetype
[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4703 static const WCHAR ico_format
[] = {'I','C','O',0};
4704 static const BYTE ico_sig_pattern
[] = { 0x00, 0x00, 0x01, 0x00 };
4705 static const BYTE ico_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4707 static const struct image_codec codecs
[NUM_CODECS
] = {
4710 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4711 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4712 /* CodecName */ bmp_codecname
,
4714 /* FormatDescription */ bmp_format
,
4715 /* FilenameExtension */ bmp_extension
,
4716 /* MimeType */ bmp_mimetype
,
4717 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4721 /* SigPattern */ bmp_sig_pattern
,
4722 /* SigMask */ bmp_sig_mask
,
4730 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4731 /* FormatID */ { 0xb96b3caeU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4732 /* CodecName */ jpeg_codecname
,
4734 /* FormatDescription */ jpeg_format
,
4735 /* FilenameExtension */ jpeg_extension
,
4736 /* MimeType */ jpeg_mimetype
,
4737 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4741 /* SigPattern */ jpeg_sig_pattern
,
4742 /* SigMask */ jpeg_sig_mask
,
4750 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4751 /* FormatID */ { 0xb96b3cb0U
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4752 /* CodecName */ gif_codecname
,
4754 /* FormatDescription */ gif_format
,
4755 /* FilenameExtension */ gif_extension
,
4756 /* MimeType */ gif_mimetype
,
4757 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsEncoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4761 /* SigPattern */ gif_sig_pattern
,
4762 /* SigMask */ gif_sig_mask
,
4770 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4771 /* FormatID */ { 0xb96b3cb1U
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4772 /* CodecName */ tiff_codecname
,
4774 /* FormatDescription */ tiff_format
,
4775 /* FilenameExtension */ tiff_extension
,
4776 /* MimeType */ tiff_mimetype
,
4777 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsEncoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4781 /* SigPattern */ tiff_sig_pattern
,
4782 /* SigMask */ tiff_sig_mask
,
4790 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4791 /* FormatID */ { 0xb96b3cacU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4792 /* CodecName */ emf_codecname
,
4794 /* FormatDescription */ emf_format
,
4795 /* FilenameExtension */ emf_extension
,
4796 /* MimeType */ emf_mimetype
,
4797 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportVector
| ImageCodecFlagsBuiltin
,
4801 /* SigPattern */ emf_sig_pattern
,
4802 /* SigMask */ emf_sig_mask
,
4810 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4811 /* FormatID */ { 0xb96b3cadU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4812 /* CodecName */ wmf_codecname
,
4814 /* FormatDescription */ wmf_format
,
4815 /* FilenameExtension */ wmf_extension
,
4816 /* MimeType */ wmf_mimetype
,
4817 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportVector
| ImageCodecFlagsBuiltin
,
4821 /* SigPattern */ wmf_sig_pattern
,
4822 /* SigMask */ wmf_sig_mask
,
4830 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4831 /* FormatID */ { 0xb96b3cafU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4832 /* CodecName */ png_codecname
,
4834 /* FormatDescription */ png_format
,
4835 /* FilenameExtension */ png_extension
,
4836 /* MimeType */ png_mimetype
,
4837 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4841 /* SigPattern */ png_sig_pattern
,
4842 /* SigMask */ png_sig_mask
,
4850 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4851 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4852 /* CodecName */ ico_codecname
,
4854 /* FormatDescription */ ico_format
,
4855 /* FilenameExtension */ ico_extension
,
4856 /* MimeType */ ico_mimetype
,
4857 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4861 /* SigPattern */ ico_sig_pattern
,
4862 /* SigMask */ ico_sig_mask
,
4870 /*****************************************************************************
4871 * GdipGetImageDecodersSize [GDIPLUS.@]
4873 GpStatus WINGDIPAPI
GdipGetImageDecodersSize(UINT
*numDecoders
, UINT
*size
)
4875 int decoder_count
=0;
4877 TRACE("%p %p\n", numDecoders
, size
);
4879 if (!numDecoders
|| !size
)
4880 return InvalidParameter
;
4882 for (i
=0; i
<NUM_CODECS
; i
++)
4884 if (codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
)
4888 *numDecoders
= decoder_count
;
4889 *size
= decoder_count
* sizeof(ImageCodecInfo
);
4894 /*****************************************************************************
4895 * GdipGetImageDecoders [GDIPLUS.@]
4897 GpStatus WINGDIPAPI
GdipGetImageDecoders(UINT numDecoders
, UINT size
, ImageCodecInfo
*decoders
)
4899 int i
, decoder_count
=0;
4900 TRACE("%u %u %p\n", numDecoders
, size
, decoders
);
4903 size
!= numDecoders
* sizeof(ImageCodecInfo
))
4904 return GenericError
;
4906 for (i
=0; i
<NUM_CODECS
; i
++)
4908 if (codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
)
4910 if (decoder_count
== numDecoders
) return GenericError
;
4911 memcpy(&decoders
[decoder_count
], &codecs
[i
].info
, sizeof(ImageCodecInfo
));
4916 if (decoder_count
< numDecoders
) return GenericError
;
4921 /*****************************************************************************
4922 * GdipGetImageEncodersSize [GDIPLUS.@]
4924 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
4926 int encoder_count
=0;
4928 TRACE("%p %p\n", numEncoders
, size
);
4930 if (!numEncoders
|| !size
)
4931 return InvalidParameter
;
4933 for (i
=0; i
<NUM_CODECS
; i
++)
4935 if (codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
)
4939 *numEncoders
= encoder_count
;
4940 *size
= encoder_count
* sizeof(ImageCodecInfo
);
4945 /*****************************************************************************
4946 * GdipGetImageEncoders [GDIPLUS.@]
4948 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
4950 int i
, encoder_count
=0;
4951 TRACE("%u %u %p\n", numEncoders
, size
, encoders
);
4954 size
!= numEncoders
* sizeof(ImageCodecInfo
))
4955 return GenericError
;
4957 for (i
=0; i
<NUM_CODECS
; i
++)
4959 if (codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
)
4961 if (encoder_count
== numEncoders
) return GenericError
;
4962 memcpy(&encoders
[encoder_count
], &codecs
[i
].info
, sizeof(ImageCodecInfo
));
4967 if (encoder_count
< numEncoders
) return GenericError
;
4972 GpStatus WINGDIPAPI
GdipGetEncoderParameterListSize(GpImage
*image
,
4973 GDIPCONST CLSID
* clsidEncoder
, UINT
*size
)
4977 TRACE("(%p,%s,%p)\n", image
, debugstr_guid(clsidEncoder
), size
);
4980 FIXME("not implemented\n");
4984 return NotImplemented
;
4987 static PixelFormat
get_16bpp_format(HBITMAP hbm
)
4993 hdc
= CreateCompatibleDC(NULL
);
4995 memset(&bmh
, 0, sizeof(bmh
));
4996 bmh
.bV4Size
= sizeof(bmh
);
4999 bmh
.bV4V4Compression
= BI_BITFIELDS
;
5000 bmh
.bV4BitCount
= 16;
5002 GetDIBits(hdc
, hbm
, 0, 0, NULL
, (BITMAPINFO
*)&bmh
, DIB_RGB_COLORS
);
5004 if (bmh
.bV4RedMask
== 0x7c00 &&
5005 bmh
.bV4GreenMask
== 0x3e0 &&
5006 bmh
.bV4BlueMask
== 0x1f)
5008 result
= PixelFormat16bppRGB555
;
5010 else if (bmh
.bV4RedMask
== 0xf800 &&
5011 bmh
.bV4GreenMask
== 0x7e0 &&
5012 bmh
.bV4BlueMask
== 0x1f)
5014 result
= PixelFormat16bppRGB565
;
5018 FIXME("unrecognized bitfields %x,%x,%x\n", bmh
.bV4RedMask
,
5019 bmh
.bV4GreenMask
, bmh
.bV4BlueMask
);
5020 result
= PixelFormatUndefined
;
5028 /*****************************************************************************
5029 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
5031 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
5036 BitmapData lockeddata
;
5038 TRACE("%p %p %p\n", hbm
, hpal
, bitmap
);
5041 return InvalidParameter
;
5043 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
5044 return InvalidParameter
;
5046 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
5047 switch(bm
.bmBitsPixel
) {
5049 format
= PixelFormat1bppIndexed
;
5052 format
= PixelFormat4bppIndexed
;
5055 format
= PixelFormat8bppIndexed
;
5058 format
= get_16bpp_format(hbm
);
5059 if (format
== PixelFormatUndefined
)
5060 return InvalidParameter
;
5063 format
= PixelFormat24bppRGB
;
5066 format
= PixelFormat32bppRGB
;
5069 format
= PixelFormat48bppRGB
;
5072 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
5073 return InvalidParameter
;
5076 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, 0,
5077 format
, NULL
, bitmap
);
5081 retval
= GdipBitmapLockBits(*bitmap
, NULL
, ImageLockModeWrite
,
5082 format
, &lockeddata
);
5086 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
[256])];
5087 BITMAPINFO
*pbmi
= (BITMAPINFO
*)bmibuf
;
5090 hdc
= CreateCompatibleDC(NULL
);
5092 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
5093 pbmi
->bmiHeader
.biBitCount
= 0;
5095 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
5097 src_height
= abs(pbmi
->bmiHeader
.biHeight
);
5098 pbmi
->bmiHeader
.biHeight
= -src_height
;
5100 GetDIBits(hdc
, hbm
, 0, src_height
, lockeddata
.Scan0
, pbmi
, DIB_RGB_COLORS
);
5104 GdipBitmapUnlockBits(*bitmap
, &lockeddata
);
5107 if (retval
== Ok
&& hpal
)
5109 PALETTEENTRY entry
[256];
5110 ColorPalette
*palette
=NULL
;
5111 int i
, num_palette_entries
;
5113 num_palette_entries
= GetPaletteEntries(hpal
, 0, 256, entry
);
5114 if (!num_palette_entries
)
5115 retval
= GenericError
;
5117 palette
= heap_alloc_zero(sizeof(ColorPalette
) + sizeof(ARGB
) * (num_palette_entries
-1));
5119 retval
= OutOfMemory
;
5124 palette
->Count
= num_palette_entries
;
5126 for (i
=0; i
<num_palette_entries
; i
++)
5128 palette
->Entries
[i
] = 0xff000000 | entry
[i
].peRed
<< 16 |
5129 entry
[i
].peGreen
<< 8 | entry
[i
].peBlue
;
5132 retval
= GdipSetImagePalette(&(*bitmap
)->image
, palette
);
5140 GdipDisposeImage(&(*bitmap
)->image
);
5148 /*****************************************************************************
5149 * GdipCreateEffect [GDIPLUS.@]
5151 GpStatus WINGDIPAPI
GdipCreateEffect(const GUID guid
, CGpEffect
**effect
)
5153 FIXME("(%s, %p): stub\n", debugstr_guid(&guid
), effect
);
5156 return InvalidParameter
;
5160 return NotImplemented
;
5163 /*****************************************************************************
5164 * GdipDeleteEffect [GDIPLUS.@]
5166 GpStatus WINGDIPAPI
GdipDeleteEffect(CGpEffect
*effect
)
5168 FIXME("(%p): stub\n", effect
);
5169 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5170 * in Windows's gdiplus */
5171 return NotImplemented
;
5174 /*****************************************************************************
5175 * GdipSetEffectParameters [GDIPLUS.@]
5177 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
5178 const VOID
*params
, const UINT size
)
5182 TRACE("(%p,%p,%u)\n", effect
, params
, size
);
5185 FIXME("not implemented\n");
5187 return NotImplemented
;
5190 /*****************************************************************************
5191 * GdipGetImageFlags [GDIPLUS.@]
5193 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
5195 TRACE("%p %p\n", image
, flags
);
5197 if(!image
|| !flags
)
5198 return InvalidParameter
;
5200 *flags
= image
->flags
;
5205 GpStatus WINGDIPAPI
GdipTestControl(GpTestControlEnum control
, void *param
)
5207 TRACE("(%d, %p)\n", control
, param
);
5210 case TestControlForceBilinear
:
5212 FIXME("TestControlForceBilinear not handled\n");
5214 case TestControlNoICM
:
5216 FIXME("TestControlNoICM not handled\n");
5218 case TestControlGetBuildNumber
:
5219 *((DWORD
*)param
) = 3102;
5226 GpStatus WINGDIPAPI
GdipImageForceValidation(GpImage
*image
)
5228 TRACE("%p\n", image
);
5233 /*****************************************************************************
5234 * GdipGetImageThumbnail [GDIPLUS.@]
5236 GpStatus WINGDIPAPI
GdipGetImageThumbnail(GpImage
*image
, UINT width
, UINT height
,
5237 GpImage
**ret_image
, GetThumbnailImageAbort cb
,
5241 GpGraphics
*graphics
;
5242 UINT srcwidth
, srcheight
;
5244 TRACE("(%p %u %u %p %p %p)\n",
5245 image
, width
, height
, ret_image
, cb
, cb_data
);
5247 if (!image
|| !ret_image
)
5248 return InvalidParameter
;
5250 if (!width
) width
= 120;
5251 if (!height
) height
= 120;
5253 GdipGetImageWidth(image
, &srcwidth
);
5254 GdipGetImageHeight(image
, &srcheight
);
5256 stat
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat32bppPARGB
,
5257 NULL
, (GpBitmap
**)ret_image
);
5261 stat
= GdipGetImageGraphicsContext(*ret_image
, &graphics
);
5265 stat
= GdipDrawImageRectRectI(graphics
, image
,
5266 0, 0, width
, height
, 0, 0, srcwidth
, srcheight
, UnitPixel
,
5269 GdipDeleteGraphics(graphics
);
5274 GdipDisposeImage(*ret_image
);
5282 /*****************************************************************************
5283 * GdipImageRotateFlip [GDIPLUS.@]
5285 GpStatus WINGDIPAPI
GdipImageRotateFlip(GpImage
*image
, RotateFlipType type
)
5287 GpBitmap
*new_bitmap
;
5289 int bpp
, bytesperpixel
;
5290 BOOL rotate_90
, flip_x
, flip_y
;
5291 int src_x_offset
, src_y_offset
;
5293 UINT x
, y
, width
, height
;
5294 BitmapData src_lock
, dst_lock
;
5298 TRACE("(%p, %u)\n", image
, type
);
5301 return InvalidParameter
;
5302 if (!image_lock(image
, &unlock
))
5306 flip_x
= (type
&6) == 2 || (type
&6) == 4;
5307 flip_y
= (type
&3) == 1 || (type
&3) == 2;
5309 if (image
->type
!= ImageTypeBitmap
)
5311 FIXME("Not implemented for type %i\n", image
->type
);
5312 image_unlock(image
, unlock
);
5313 return NotImplemented
;
5316 bitmap
= (GpBitmap
*)image
;
5317 bpp
= PIXELFORMATBPP(bitmap
->format
);
5321 FIXME("Not implemented for %i bit images\n", bpp
);
5322 image_unlock(image
, unlock
);
5323 return NotImplemented
;
5328 width
= bitmap
->height
;
5329 height
= bitmap
->width
;
5333 width
= bitmap
->width
;
5334 height
= bitmap
->height
;
5337 bytesperpixel
= bpp
/8;
5339 stat
= GdipCreateBitmapFromScan0(width
, height
, 0, bitmap
->format
, NULL
, &new_bitmap
);
5343 image_unlock(image
, unlock
);
5347 stat
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
, bitmap
->format
, &src_lock
);
5351 stat
= GdipBitmapLockBits(new_bitmap
, NULL
, ImageLockModeWrite
, bitmap
->format
, &dst_lock
);
5355 LPBYTE src_row
, src_pixel
;
5356 LPBYTE dst_row
, dst_pixel
;
5358 src_origin
= src_lock
.Scan0
;
5359 if (flip_x
) src_origin
+= bytesperpixel
* (bitmap
->width
- 1);
5360 if (flip_y
) src_origin
+= src_lock
.Stride
* (bitmap
->height
- 1);
5364 if (flip_y
) src_x_offset
= -src_lock
.Stride
;
5365 else src_x_offset
= src_lock
.Stride
;
5366 if (flip_x
) src_y_offset
= -bytesperpixel
;
5367 else src_y_offset
= bytesperpixel
;
5371 if (flip_x
) src_x_offset
= -bytesperpixel
;
5372 else src_x_offset
= bytesperpixel
;
5373 if (flip_y
) src_y_offset
= -src_lock
.Stride
;
5374 else src_y_offset
= src_lock
.Stride
;
5377 src_row
= src_origin
;
5378 dst_row
= dst_lock
.Scan0
;
5379 for (y
=0; y
<height
; y
++)
5381 src_pixel
= src_row
;
5382 dst_pixel
= dst_row
;
5383 for (x
=0; x
<width
; x
++)
5385 /* FIXME: This could probably be faster without memcpy. */
5386 memcpy(dst_pixel
, src_pixel
, bytesperpixel
);
5387 dst_pixel
+= bytesperpixel
;
5388 src_pixel
+= src_x_offset
;
5390 src_row
+= src_y_offset
;
5391 dst_row
+= dst_lock
.Stride
;
5394 GdipBitmapUnlockBits(new_bitmap
, &dst_lock
);
5397 GdipBitmapUnlockBits(bitmap
, &src_lock
);
5401 move_bitmap(bitmap
, new_bitmap
, FALSE
);
5403 GdipDisposeImage(&new_bitmap
->image
);
5405 image_unlock(image
, unlock
);
5409 /*****************************************************************************
5410 * GdipImageSetAbort [GDIPLUS.@]
5412 GpStatus WINGDIPAPI
GdipImageSetAbort(GpImage
*image
, GdiplusAbort
*pabort
)
5414 TRACE("(%p, %p)\n", image
, pabort
);
5417 return InvalidParameter
;
5420 FIXME("Abort callback is not supported.\n");
5425 /*****************************************************************************
5426 * GdipBitmapConvertFormat [GDIPLUS.@]
5428 GpStatus WINGDIPAPI
GdipBitmapConvertFormat(GpBitmap
*bitmap
, PixelFormat format
, DitherType dithertype
,
5429 PaletteType palettetype
, ColorPalette
*palette
, REAL alphathreshold
)
5431 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap
, format
, dithertype
, palettetype
, palette
, alphathreshold
);
5432 return NotImplemented
;
5435 static void set_histogram_point_argb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5437 ch0
[ color
>> 24 ]++;
5438 ch1
[(color
>> 16) & 0xff]++;
5439 ch2
[(color
>> 8) & 0xff]++;
5440 ch3
[ color
& 0xff]++;
5443 static void set_histogram_point_pargb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5445 BYTE alpha
= color
>> 24;
5448 ch1
[(((color
>> 16) & 0xff) * alpha
) / 0xff]++;
5449 ch2
[(((color
>> 8) & 0xff) * alpha
) / 0xff]++;
5450 ch3
[(( color
& 0xff) * alpha
) / 0xff]++;
5453 static void set_histogram_point_rgb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5455 ch0
[(color
>> 16) & 0xff]++;
5456 ch1
[(color
>> 8) & 0xff]++;
5457 ch2
[ color
& 0xff]++;
5460 static void set_histogram_point_gray(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5462 ch0
[(76 * ((color
>> 16) & 0xff) + 150 * ((color
>> 8) & 0xff) + 29 * (color
& 0xff)) / 0xff]++;
5465 static void set_histogram_point_b(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5467 ch0
[color
& 0xff]++;
5470 static void set_histogram_point_g(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5472 ch0
[(color
>> 8) & 0xff]++;
5475 static void set_histogram_point_r(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5477 ch0
[(color
>> 16) & 0xff]++;
5480 static void set_histogram_point_a(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5482 ch0
[(color
>> 24) & 0xff]++;
5485 /*****************************************************************************
5486 * GdipBitmapGetHistogram [GDIPLUS.@]
5488 GpStatus WINGDIPAPI
GdipBitmapGetHistogram(GpBitmap
*bitmap
, HistogramFormat format
, UINT num_of_entries
,
5489 UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5491 static void (* const set_histogram_point
[])(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
) =
5493 set_histogram_point_argb
,
5494 set_histogram_point_pargb
,
5495 set_histogram_point_rgb
,
5496 set_histogram_point_gray
,
5497 set_histogram_point_b
,
5498 set_histogram_point_g
,
5499 set_histogram_point_r
,
5500 set_histogram_point_a
,
5502 UINT width
, height
, x
, y
;
5504 TRACE("(%p, %d, %u, %p, %p, %p, %p)\n", bitmap
, format
, num_of_entries
,
5505 ch0
, ch1
, ch2
, ch3
);
5507 if (!bitmap
|| num_of_entries
!= 256)
5508 return InvalidParameter
;
5510 /* Make sure passed channel pointers match requested format */
5513 case HistogramFormatARGB
:
5514 case HistogramFormatPARGB
:
5515 if (!ch0
|| !ch1
|| !ch2
|| !ch3
)
5516 return InvalidParameter
;
5517 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5518 memset(ch1
, 0, num_of_entries
* sizeof(UINT
));
5519 memset(ch2
, 0, num_of_entries
* sizeof(UINT
));
5520 memset(ch3
, 0, num_of_entries
* sizeof(UINT
));
5522 case HistogramFormatRGB
:
5523 if (!ch0
|| !ch1
|| !ch2
|| ch3
)
5524 return InvalidParameter
;
5525 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5526 memset(ch1
, 0, num_of_entries
* sizeof(UINT
));
5527 memset(ch2
, 0, num_of_entries
* sizeof(UINT
));
5529 case HistogramFormatGray
:
5530 case HistogramFormatB
:
5531 case HistogramFormatG
:
5532 case HistogramFormatR
:
5533 case HistogramFormatA
:
5534 if (!ch0
|| ch1
|| ch2
|| ch3
)
5535 return InvalidParameter
;
5536 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5539 WARN("Invalid histogram format requested, %d\n", format
);
5540 return InvalidParameter
;
5543 GdipGetImageWidth(&bitmap
->image
, &width
);
5544 GdipGetImageHeight(&bitmap
->image
, &height
);
5546 for (y
= 0; y
< height
; y
++)
5547 for (x
= 0; x
< width
; x
++)
5551 GdipBitmapGetPixel(bitmap
, x
, y
, &color
);
5552 set_histogram_point
[format
](color
, ch0
, ch1
, ch2
, ch3
);
5558 /*****************************************************************************
5559 * GdipBitmapGetHistogramSize [GDIPLUS.@]
5561 GpStatus WINGDIPAPI
GdipBitmapGetHistogramSize(HistogramFormat format
, UINT
*num_of_entries
)
5563 TRACE("(%d, %p)\n", format
, num_of_entries
);
5565 if (!num_of_entries
)
5566 return InvalidParameter
;
5568 *num_of_entries
= 256;