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_WICPixelFormat1bppIndexed
, PixelFormat1bppIndexed
, WICBitmapPaletteTypeFixedBW
},
57 { &GUID_WICPixelFormatBlackWhite
, PixelFormat1bppIndexed
, WICBitmapPaletteTypeFixedBW
},
58 { &GUID_WICPixelFormat4bppIndexed
, PixelFormat4bppIndexed
, WICBitmapPaletteTypeFixedHalftone8
},
59 { &GUID_WICPixelFormat8bppIndexed
, PixelFormat8bppIndexed
, WICBitmapPaletteTypeFixedHalftone256
},
60 { &GUID_WICPixelFormat8bppGray
, PixelFormat8bppIndexed
, WICBitmapPaletteTypeFixedGray256
},
61 { &GUID_WICPixelFormat16bppBGR555
, PixelFormat16bppRGB555
, 0 },
62 { &GUID_WICPixelFormat24bppBGR
, PixelFormat24bppRGB
, 0 },
63 { &GUID_WICPixelFormat32bppBGR
, PixelFormat32bppRGB
, 0 },
64 { &GUID_WICPixelFormat32bppBGRA
, PixelFormat32bppARGB
, 0 },
65 { &GUID_WICPixelFormat32bppPBGRA
, PixelFormat32bppPARGB
, 0 },
69 static ColorPalette
*get_palette(IWICBitmapFrameDecode
*frame
, WICBitmapPaletteType palette_type
)
72 IWICImagingFactory
*factory
;
73 IWICPalette
*wic_palette
;
74 ColorPalette
*palette
= NULL
;
76 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
77 if (hr
!= S_OK
) return NULL
;
79 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
82 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
84 hr
= IWICBitmapFrameDecode_CopyPalette(frame
, wic_palette
);
85 if (hr
!= S_OK
&& palette_type
!= 0)
87 TRACE("using predefined palette %#x\n", palette_type
);
88 hr
= IWICPalette_InitializePredefined(wic_palette
, palette_type
, FALSE
);
92 WICBitmapPaletteType type
;
96 IWICPalette_GetColorCount(wic_palette
, &count
);
97 palette
= heap_alloc(2 * sizeof(UINT
) + count
* sizeof(ARGB
));
98 IWICPalette_GetColors(wic_palette
, count
, palette
->Entries
, &palette
->Count
);
100 IWICPalette_GetType(wic_palette
, &type
);
102 case WICBitmapPaletteTypeFixedGray4
:
103 case WICBitmapPaletteTypeFixedGray16
:
104 case WICBitmapPaletteTypeFixedGray256
:
105 palette
->Flags
= PaletteFlagsGrayScale
;
107 case WICBitmapPaletteTypeFixedHalftone8
:
108 case WICBitmapPaletteTypeFixedHalftone27
:
109 case WICBitmapPaletteTypeFixedHalftone64
:
110 case WICBitmapPaletteTypeFixedHalftone125
:
111 case WICBitmapPaletteTypeFixedHalftone216
:
112 case WICBitmapPaletteTypeFixedHalftone252
:
113 case WICBitmapPaletteTypeFixedHalftone256
:
114 palette
->Flags
= PaletteFlagsHalftone
;
119 IWICPalette_HasAlpha(wic_palette
, &alpha
);
121 palette
->Flags
|= PaletteFlagsHasAlpha
;
123 IWICPalette_Release(wic_palette
);
125 IWICImagingFactory_Release(factory
);
129 static HRESULT
set_palette(IWICBitmapFrameEncode
*frame
, ColorPalette
*palette
)
132 IWICImagingFactory
*factory
;
133 IWICPalette
*wic_palette
;
135 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
139 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
140 IWICImagingFactory_Release(factory
);
143 hr
= IWICPalette_InitializeCustom(wic_palette
, palette
->Entries
, palette
->Count
);
146 hr
= IWICBitmapFrameEncode_SetPalette(frame
, wic_palette
);
148 IWICPalette_Release(wic_palette
);
154 GpStatus WINGDIPAPI
GdipBitmapApplyEffect(GpBitmap
* bitmap
, CGpEffect
* effect
,
155 RECT
* roi
, BOOL useAuxData
, VOID
** auxData
, INT
* auxDataSize
)
157 FIXME("(%p %p %p %d %p %p): stub\n", bitmap
, effect
, roi
, useAuxData
, auxData
, auxDataSize
);
159 * Note: According to Jose Roca's GDI+ docs, this function is not
160 * implemented in Windows's GDI+.
162 return NotImplemented
;
165 GpStatus WINGDIPAPI
GdipBitmapCreateApplyEffect(GpBitmap
** inputBitmaps
,
166 INT numInputs
, CGpEffect
* effect
, RECT
* roi
, RECT
* outputRect
,
167 GpBitmap
** outputBitmap
, BOOL useAuxData
, VOID
** auxData
, INT
* auxDataSize
)
169 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps
, numInputs
, effect
, roi
, outputRect
, outputBitmap
, useAuxData
, auxData
, auxDataSize
);
171 * Note: According to Jose Roca's GDI+ docs, this function is not
172 * implemented in Windows's GDI+.
174 return NotImplemented
;
177 static inline void getpixel_1bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
179 *index
= (row
[x
/8]>>(7-x
%8)) & 1;
182 static inline void getpixel_4bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
185 *index
= row
[x
/2]&0xf;
187 *index
= row
[x
/2]>>4;
190 static inline void getpixel_8bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
195 static inline void getpixel_16bppGrayScale(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
196 const BYTE
*row
, UINT x
)
198 *r
= *g
= *b
= row
[x
*2+1];
202 static inline void getpixel_16bppRGB555(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
203 const BYTE
*row
, UINT x
)
205 WORD pixel
= *((const WORD
*)(row
)+x
);
206 *r
= (pixel
>>7&0xf8)|(pixel
>>12&0x7);
207 *g
= (pixel
>>2&0xf8)|(pixel
>>6&0x7);
208 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
212 static inline void getpixel_16bppRGB565(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
213 const BYTE
*row
, UINT x
)
215 WORD pixel
= *((const WORD
*)(row
)+x
);
216 *r
= (pixel
>>8&0xf8)|(pixel
>>13&0x7);
217 *g
= (pixel
>>3&0xfc)|(pixel
>>9&0x3);
218 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
222 static inline void getpixel_16bppARGB1555(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
223 const BYTE
*row
, UINT x
)
225 WORD pixel
= *((const WORD
*)(row
)+x
);
226 *r
= (pixel
>>7&0xf8)|(pixel
>>12&0x7);
227 *g
= (pixel
>>2&0xf8)|(pixel
>>6&0x7);
228 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
229 if ((pixel
&0x8000) == 0x8000)
235 static inline void getpixel_24bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
236 const BYTE
*row
, UINT x
)
244 static inline void getpixel_32bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
245 const BYTE
*row
, UINT x
)
253 static inline void getpixel_32bppARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
254 const BYTE
*row
, UINT x
)
262 static inline void getpixel_32bppPARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
263 const BYTE
*row
, UINT x
)
274 DWORD scaled_q
= (255 << 15) / *a
;
275 *r
= (row
[x
*4+2] > *a
) ? 0xff : (row
[x
*4+2] * scaled_q
) >> 15;
276 *g
= (row
[x
*4+1] > *a
) ? 0xff : (row
[x
*4+1] * scaled_q
) >> 15;
277 *b
= (row
[x
*4] > *a
) ? 0xff : (row
[x
*4] * scaled_q
) >> 15;
281 static inline void getpixel_48bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
282 const BYTE
*row
, UINT x
)
290 static inline void getpixel_64bppARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
291 const BYTE
*row
, UINT x
)
299 static inline void getpixel_64bppPARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
300 const BYTE
*row
, UINT x
)
307 *r
= row
[x
*8+5] * 255 / *a
;
308 *g
= row
[x
*8+3] * 255 / *a
;
309 *b
= row
[x
*8+1] * 255 / *a
;
313 GpStatus WINGDIPAPI
GdipBitmapGetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
320 if(!bitmap
|| !color
||
321 x
< 0 || y
< 0 || x
>= bitmap
->width
|| y
>= bitmap
->height
)
322 return InvalidParameter
;
324 row
= bitmap
->bits
+bitmap
->stride
*y
;
326 switch (bitmap
->format
)
328 case PixelFormat1bppIndexed
:
329 getpixel_1bppIndexed(&index
,row
,x
);
331 case PixelFormat4bppIndexed
:
332 getpixel_4bppIndexed(&index
,row
,x
);
334 case PixelFormat8bppIndexed
:
335 getpixel_8bppIndexed(&index
,row
,x
);
337 case PixelFormat16bppGrayScale
:
338 getpixel_16bppGrayScale(&r
,&g
,&b
,&a
,row
,x
);
340 case PixelFormat16bppRGB555
:
341 getpixel_16bppRGB555(&r
,&g
,&b
,&a
,row
,x
);
343 case PixelFormat16bppRGB565
:
344 getpixel_16bppRGB565(&r
,&g
,&b
,&a
,row
,x
);
346 case PixelFormat16bppARGB1555
:
347 getpixel_16bppARGB1555(&r
,&g
,&b
,&a
,row
,x
);
349 case PixelFormat24bppRGB
:
350 getpixel_24bppRGB(&r
,&g
,&b
,&a
,row
,x
);
352 case PixelFormat32bppRGB
:
353 getpixel_32bppRGB(&r
,&g
,&b
,&a
,row
,x
);
355 case PixelFormat32bppARGB
:
356 getpixel_32bppARGB(&r
,&g
,&b
,&a
,row
,x
);
358 case PixelFormat32bppPARGB
:
359 getpixel_32bppPARGB(&r
,&g
,&b
,&a
,row
,x
);
361 case PixelFormat48bppRGB
:
362 getpixel_48bppRGB(&r
,&g
,&b
,&a
,row
,x
);
364 case PixelFormat64bppARGB
:
365 getpixel_64bppARGB(&r
,&g
,&b
,&a
,row
,x
);
367 case PixelFormat64bppPARGB
:
368 getpixel_64bppPARGB(&r
,&g
,&b
,&a
,row
,x
);
371 FIXME("not implemented for format 0x%x\n", bitmap
->format
);
372 return NotImplemented
;
375 if (bitmap
->format
& PixelFormatIndexed
)
376 *color
= bitmap
->image
.palette
->Entries
[index
];
378 *color
= a
<<24|r
<<16|g
<<8|b
;
383 static unsigned int absdiff(unsigned int x
, unsigned int y
)
385 return x
> y
? x
- y
: y
- x
;
388 static inline UINT
get_palette_index(BYTE r
, BYTE g
, BYTE b
, BYTE a
, ColorPalette
*palette
)
391 int best_distance
= 0x7fff;
395 if (!palette
) return 0;
396 /* This algorithm scans entire palette,
397 computes difference from desired color (all color components have equal weight)
398 and returns the index of color with least difference.
400 Note: Maybe it could be replaced with a better algorithm for better image quality
401 and performance, though better algorithm would probably need some pre-built lookup
402 tables and thus may actually be slower if this method is called only few times per
405 for(i
=0;i
<palette
->Count
;i
++) {
406 ARGB color
=palette
->Entries
[i
];
407 distance
=absdiff(b
, color
& 0xff) + absdiff(g
, color
>>8 & 0xff) + absdiff(r
, color
>>16 & 0xff) + absdiff(a
, color
>>24 & 0xff);
408 if (distance
<best_distance
) {
409 best_distance
=distance
;
416 static inline void setpixel_8bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
417 BYTE
*row
, UINT x
, ColorPalette
*palette
)
419 BYTE index
= get_palette_index(r
,g
,b
,a
,palette
);
423 static inline void setpixel_1bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
424 BYTE
*row
, UINT x
, ColorPalette
*palette
)
426 row
[x
/8] = (row
[x
/8] & ~(1<<(7-x
%8))) | (get_palette_index(r
,g
,b
,a
,palette
)<<(7-x
%8));
429 static inline void setpixel_4bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
430 BYTE
*row
, UINT x
, ColorPalette
*palette
)
433 row
[x
/2] = (row
[x
/2] & 0xf0) | get_palette_index(r
,g
,b
,a
,palette
);
435 row
[x
/2] = (row
[x
/2] & 0x0f) | get_palette_index(r
,g
,b
,a
,palette
)<<4;
438 static inline void setpixel_16bppGrayScale(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
441 *((WORD
*)(row
)+x
) = (r
+g
+b
)*85;
444 static inline void setpixel_16bppRGB555(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
447 *((WORD
*)(row
)+x
) = (r
<<7&0x7c00)|
452 static inline void setpixel_16bppRGB565(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
455 *((WORD
*)(row
)+x
) = (r
<<8&0xf800)|
460 static inline void setpixel_16bppARGB1555(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
463 *((WORD
*)(row
)+x
) = (a
<<8&0x8000)|
469 static inline void setpixel_24bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
477 static inline void setpixel_32bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
480 *((DWORD
*)(row
)+x
) = (r
<<16)|(g
<<8)|b
;
483 static inline void setpixel_32bppARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
486 *((DWORD
*)(row
)+x
) = (a
<<24)|(r
<<16)|(g
<<8)|b
;
489 static inline void setpixel_32bppPARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
492 r
= (r
* a
+ 127) / 255;
493 g
= (g
* a
+ 127) / 255;
494 b
= (b
* a
+ 127) / 255;
495 *((DWORD
*)(row
)+x
) = (a
<<24)|(r
<<16)|(g
<<8)|b
;
498 static inline void setpixel_48bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
501 row
[x
*6+5] = row
[x
*6+4] = r
;
502 row
[x
*6+3] = row
[x
*6+2] = g
;
503 row
[x
*6+1] = row
[x
*6] = b
;
506 static inline void setpixel_64bppARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
509 UINT64 a64
=a
, r64
=r
, g64
=g
, b64
=b
;
510 *((UINT64
*)(row
)+x
) = (a64
<<56)|(a64
<<48)|(r64
<<40)|(r64
<<32)|(g64
<<24)|(g64
<<16)|(b64
<<8)|b64
;
513 static inline void setpixel_64bppPARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
516 UINT64 a64
, r64
, g64
, b64
;
521 *((UINT64
*)(row
)+x
) = (a64
<<48)|(r64
<<32)|(g64
<<16)|b64
;
524 GpStatus WINGDIPAPI
GdipBitmapSetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
530 if(!bitmap
|| x
< 0 || y
< 0 || x
>= bitmap
->width
|| y
>= bitmap
->height
)
531 return InvalidParameter
;
538 row
= bitmap
->bits
+ bitmap
->stride
* y
;
540 switch (bitmap
->format
)
542 case PixelFormat16bppGrayScale
:
543 setpixel_16bppGrayScale(r
,g
,b
,a
,row
,x
);
545 case PixelFormat16bppRGB555
:
546 setpixel_16bppRGB555(r
,g
,b
,a
,row
,x
);
548 case PixelFormat16bppRGB565
:
549 setpixel_16bppRGB565(r
,g
,b
,a
,row
,x
);
551 case PixelFormat16bppARGB1555
:
552 setpixel_16bppARGB1555(r
,g
,b
,a
,row
,x
);
554 case PixelFormat24bppRGB
:
555 setpixel_24bppRGB(r
,g
,b
,a
,row
,x
);
557 case PixelFormat32bppRGB
:
558 setpixel_32bppRGB(r
,g
,b
,a
,row
,x
);
560 case PixelFormat32bppARGB
:
561 setpixel_32bppARGB(r
,g
,b
,a
,row
,x
);
563 case PixelFormat32bppPARGB
:
564 setpixel_32bppPARGB(r
,g
,b
,a
,row
,x
);
566 case PixelFormat48bppRGB
:
567 setpixel_48bppRGB(r
,g
,b
,a
,row
,x
);
569 case PixelFormat64bppARGB
:
570 setpixel_64bppARGB(r
,g
,b
,a
,row
,x
);
572 case PixelFormat64bppPARGB
:
573 setpixel_64bppPARGB(r
,g
,b
,a
,row
,x
);
575 case PixelFormat8bppIndexed
:
576 setpixel_8bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
578 case PixelFormat4bppIndexed
:
579 setpixel_4bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
581 case PixelFormat1bppIndexed
:
582 setpixel_1bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
585 FIXME("not implemented for format 0x%x\n", bitmap
->format
);
586 return NotImplemented
;
592 GpStatus
convert_pixels(INT width
, INT height
,
593 INT dst_stride
, BYTE
*dst_bits
, PixelFormat dst_format
,
594 INT src_stride
, const BYTE
*src_bits
, PixelFormat src_format
,
595 ColorPalette
*palette
)
599 if (src_format
== dst_format
||
600 (dst_format
== PixelFormat32bppRGB
&& PIXELFORMATBPP(src_format
) == 32))
602 UINT widthbytes
= PIXELFORMATBPP(src_format
) * width
/ 8;
603 for (y
=0; y
<height
; y
++)
604 memcpy(dst_bits
+dst_stride
*y
, src_bits
+src_stride
*y
, widthbytes
);
608 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
609 for (y=0; y<height; y++) \
610 for (x=0; x<width; x++) { \
613 BYTE *color = (BYTE *)&argb; \
614 getpixel_function(&index, src_bits+src_stride*y, x); \
615 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
616 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
621 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
622 for (y=0; y<height; y++) \
623 for (x=0; x<width; x++) { \
625 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
626 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
631 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
632 for (y=0; y<height; y++) \
633 for (x=0; x<width; x++) { \
635 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
636 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
643 case PixelFormat1bppIndexed
:
646 case PixelFormat16bppGrayScale
:
647 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppGrayScale
);
648 case PixelFormat16bppRGB555
:
649 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppRGB555
);
650 case PixelFormat16bppRGB565
:
651 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppRGB565
);
652 case PixelFormat16bppARGB1555
:
653 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppARGB1555
);
654 case PixelFormat24bppRGB
:
655 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_24bppRGB
);
656 case PixelFormat32bppRGB
:
657 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppRGB
);
658 case PixelFormat32bppARGB
:
659 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppARGB
);
660 case PixelFormat32bppPARGB
:
661 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppPARGB
);
662 case PixelFormat48bppRGB
:
663 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_48bppRGB
);
664 case PixelFormat64bppARGB
:
665 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_64bppARGB
);
670 case PixelFormat4bppIndexed
:
673 case PixelFormat16bppGrayScale
:
674 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppGrayScale
);
675 case PixelFormat16bppRGB555
:
676 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppRGB555
);
677 case PixelFormat16bppRGB565
:
678 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppRGB565
);
679 case PixelFormat16bppARGB1555
:
680 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppARGB1555
);
681 case PixelFormat24bppRGB
:
682 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_24bppRGB
);
683 case PixelFormat32bppRGB
:
684 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppRGB
);
685 case PixelFormat32bppARGB
:
686 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppARGB
);
687 case PixelFormat32bppPARGB
:
688 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppPARGB
);
689 case PixelFormat48bppRGB
:
690 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_48bppRGB
);
691 case PixelFormat64bppARGB
:
692 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_64bppARGB
);
697 case PixelFormat8bppIndexed
:
700 case PixelFormat16bppGrayScale
:
701 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppGrayScale
);
702 case PixelFormat16bppRGB555
:
703 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppRGB555
);
704 case PixelFormat16bppRGB565
:
705 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppRGB565
);
706 case PixelFormat16bppARGB1555
:
707 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppARGB1555
);
708 case PixelFormat24bppRGB
:
709 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_24bppRGB
);
710 case PixelFormat32bppRGB
:
711 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppRGB
);
712 case PixelFormat32bppARGB
:
713 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppARGB
);
714 case PixelFormat32bppPARGB
:
715 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppPARGB
);
716 case PixelFormat48bppRGB
:
717 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_48bppRGB
);
718 case PixelFormat64bppARGB
:
719 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_64bppARGB
);
724 case PixelFormat16bppGrayScale
:
727 case PixelFormat1bppIndexed
:
728 convert_rgb_to_indexed(getpixel_16bppGrayScale
, setpixel_1bppIndexed
);
729 case PixelFormat8bppIndexed
:
730 convert_rgb_to_indexed(getpixel_16bppGrayScale
, setpixel_8bppIndexed
);
731 case PixelFormat16bppRGB555
:
732 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppRGB555
);
733 case PixelFormat16bppRGB565
:
734 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppRGB565
);
735 case PixelFormat16bppARGB1555
:
736 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppARGB1555
);
737 case PixelFormat24bppRGB
:
738 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_24bppRGB
);
739 case PixelFormat32bppRGB
:
740 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppRGB
);
741 case PixelFormat32bppARGB
:
742 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppARGB
);
743 case PixelFormat32bppPARGB
:
744 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppPARGB
);
745 case PixelFormat48bppRGB
:
746 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_48bppRGB
);
747 case PixelFormat64bppARGB
:
748 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_64bppARGB
);
753 case PixelFormat16bppRGB555
:
756 case PixelFormat1bppIndexed
:
757 convert_rgb_to_indexed(getpixel_16bppRGB555
, setpixel_1bppIndexed
);
758 case PixelFormat8bppIndexed
:
759 convert_rgb_to_indexed(getpixel_16bppRGB555
, setpixel_8bppIndexed
);
760 case PixelFormat16bppGrayScale
:
761 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppGrayScale
);
762 case PixelFormat16bppRGB565
:
763 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppRGB565
);
764 case PixelFormat16bppARGB1555
:
765 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppARGB1555
);
766 case PixelFormat24bppRGB
:
767 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_24bppRGB
);
768 case PixelFormat32bppRGB
:
769 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppRGB
);
770 case PixelFormat32bppARGB
:
771 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppARGB
);
772 case PixelFormat32bppPARGB
:
773 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppPARGB
);
774 case PixelFormat48bppRGB
:
775 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_48bppRGB
);
776 case PixelFormat64bppARGB
:
777 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_64bppARGB
);
782 case PixelFormat16bppRGB565
:
785 case PixelFormat1bppIndexed
:
786 convert_rgb_to_indexed(getpixel_16bppRGB565
, setpixel_1bppIndexed
);
787 case PixelFormat8bppIndexed
:
788 convert_rgb_to_indexed(getpixel_16bppRGB565
, setpixel_8bppIndexed
);
789 case PixelFormat16bppGrayScale
:
790 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppGrayScale
);
791 case PixelFormat16bppRGB555
:
792 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppRGB555
);
793 case PixelFormat16bppARGB1555
:
794 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppARGB1555
);
795 case PixelFormat24bppRGB
:
796 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_24bppRGB
);
797 case PixelFormat32bppRGB
:
798 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppRGB
);
799 case PixelFormat32bppARGB
:
800 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppARGB
);
801 case PixelFormat32bppPARGB
:
802 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppPARGB
);
803 case PixelFormat48bppRGB
:
804 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_48bppRGB
);
805 case PixelFormat64bppARGB
:
806 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_64bppARGB
);
811 case PixelFormat16bppARGB1555
:
814 case PixelFormat1bppIndexed
:
815 convert_rgb_to_indexed(getpixel_16bppARGB1555
, setpixel_1bppIndexed
);
816 case PixelFormat8bppIndexed
:
817 convert_rgb_to_indexed(getpixel_16bppARGB1555
, setpixel_8bppIndexed
);
818 case PixelFormat16bppGrayScale
:
819 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppGrayScale
);
820 case PixelFormat16bppRGB555
:
821 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppRGB555
);
822 case PixelFormat16bppRGB565
:
823 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppRGB565
);
824 case PixelFormat24bppRGB
:
825 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_24bppRGB
);
826 case PixelFormat32bppRGB
:
827 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppRGB
);
828 case PixelFormat32bppARGB
:
829 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppARGB
);
830 case PixelFormat32bppPARGB
:
831 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppPARGB
);
832 case PixelFormat48bppRGB
:
833 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_48bppRGB
);
834 case PixelFormat64bppARGB
:
835 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_64bppARGB
);
840 case PixelFormat24bppRGB
:
843 case PixelFormat1bppIndexed
:
844 convert_rgb_to_indexed(getpixel_24bppRGB
, setpixel_1bppIndexed
);
845 case PixelFormat8bppIndexed
:
846 convert_rgb_to_indexed(getpixel_24bppRGB
, setpixel_8bppIndexed
);
847 case PixelFormat16bppGrayScale
:
848 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppGrayScale
);
849 case PixelFormat16bppRGB555
:
850 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppRGB555
);
851 case PixelFormat16bppRGB565
:
852 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppRGB565
);
853 case PixelFormat16bppARGB1555
:
854 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppARGB1555
);
855 case PixelFormat32bppRGB
:
856 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppRGB
);
857 case PixelFormat32bppARGB
:
858 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppARGB
);
859 case PixelFormat32bppPARGB
:
860 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppPARGB
);
861 case PixelFormat48bppRGB
:
862 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_48bppRGB
);
863 case PixelFormat64bppARGB
:
864 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_64bppARGB
);
869 case PixelFormat32bppRGB
:
872 case PixelFormat1bppIndexed
:
873 convert_rgb_to_indexed(getpixel_32bppRGB
, setpixel_1bppIndexed
);
874 case PixelFormat8bppIndexed
:
875 convert_rgb_to_indexed(getpixel_32bppRGB
, setpixel_8bppIndexed
);
876 case PixelFormat16bppGrayScale
:
877 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppGrayScale
);
878 case PixelFormat16bppRGB555
:
879 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppRGB555
);
880 case PixelFormat16bppRGB565
:
881 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppRGB565
);
882 case PixelFormat16bppARGB1555
:
883 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppARGB1555
);
884 case PixelFormat24bppRGB
:
885 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_24bppRGB
);
886 case PixelFormat32bppARGB
:
887 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_32bppARGB
);
888 case PixelFormat32bppPARGB
:
889 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_32bppPARGB
);
890 case PixelFormat48bppRGB
:
891 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_48bppRGB
);
892 case PixelFormat64bppARGB
:
893 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_64bppARGB
);
898 case PixelFormat32bppARGB
:
901 case PixelFormat1bppIndexed
:
902 convert_rgb_to_indexed(getpixel_32bppARGB
, setpixel_1bppIndexed
);
903 case PixelFormat8bppIndexed
:
904 convert_rgb_to_indexed(getpixel_32bppARGB
, setpixel_8bppIndexed
);
905 case PixelFormat16bppGrayScale
:
906 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppGrayScale
);
907 case PixelFormat16bppRGB555
:
908 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppRGB555
);
909 case PixelFormat16bppRGB565
:
910 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppRGB565
);
911 case PixelFormat16bppARGB1555
:
912 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppARGB1555
);
913 case PixelFormat24bppRGB
:
914 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_24bppRGB
);
915 case PixelFormat32bppPARGB
:
916 convert_32bppARGB_to_32bppPARGB(width
, height
, dst_bits
, dst_stride
, src_bits
, src_stride
);
918 case PixelFormat48bppRGB
:
919 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_48bppRGB
);
920 case PixelFormat64bppARGB
:
921 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_64bppARGB
);
926 case PixelFormat32bppPARGB
:
929 case PixelFormat1bppIndexed
:
930 convert_rgb_to_indexed(getpixel_32bppPARGB
, setpixel_1bppIndexed
);
931 case PixelFormat8bppIndexed
:
932 convert_rgb_to_indexed(getpixel_32bppPARGB
, setpixel_8bppIndexed
);
933 case PixelFormat16bppGrayScale
:
934 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppGrayScale
);
935 case PixelFormat16bppRGB555
:
936 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppRGB555
);
937 case PixelFormat16bppRGB565
:
938 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppRGB565
);
939 case PixelFormat16bppARGB1555
:
940 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppARGB1555
);
941 case PixelFormat24bppRGB
:
942 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_24bppRGB
);
943 case PixelFormat32bppRGB
:
944 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_32bppRGB
);
945 case PixelFormat32bppARGB
:
946 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_32bppARGB
);
947 case PixelFormat48bppRGB
:
948 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_48bppRGB
);
949 case PixelFormat64bppARGB
:
950 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_64bppARGB
);
955 case PixelFormat48bppRGB
:
958 case PixelFormat1bppIndexed
:
959 convert_rgb_to_indexed(getpixel_48bppRGB
, setpixel_1bppIndexed
);
960 case PixelFormat8bppIndexed
:
961 convert_rgb_to_indexed(getpixel_48bppRGB
, setpixel_8bppIndexed
);
962 case PixelFormat16bppGrayScale
:
963 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppGrayScale
);
964 case PixelFormat16bppRGB555
:
965 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppRGB555
);
966 case PixelFormat16bppRGB565
:
967 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppRGB565
);
968 case PixelFormat16bppARGB1555
:
969 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppARGB1555
);
970 case PixelFormat24bppRGB
:
971 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_24bppRGB
);
972 case PixelFormat32bppRGB
:
973 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppRGB
);
974 case PixelFormat32bppARGB
:
975 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppARGB
);
976 case PixelFormat32bppPARGB
:
977 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppPARGB
);
978 case PixelFormat64bppARGB
:
979 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_64bppARGB
);
984 case PixelFormat64bppARGB
:
987 case PixelFormat1bppIndexed
:
988 convert_rgb_to_indexed(getpixel_64bppARGB
, setpixel_1bppIndexed
);
989 case PixelFormat8bppIndexed
:
990 convert_rgb_to_indexed(getpixel_64bppARGB
, setpixel_8bppIndexed
);
991 case PixelFormat16bppGrayScale
:
992 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppGrayScale
);
993 case PixelFormat16bppRGB555
:
994 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppRGB555
);
995 case PixelFormat16bppRGB565
:
996 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppRGB565
);
997 case PixelFormat16bppARGB1555
:
998 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppARGB1555
);
999 case PixelFormat24bppRGB
:
1000 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_24bppRGB
);
1001 case PixelFormat32bppRGB
:
1002 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppRGB
);
1003 case PixelFormat32bppARGB
:
1004 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppARGB
);
1005 case PixelFormat32bppPARGB
:
1006 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppPARGB
);
1007 case PixelFormat48bppRGB
:
1008 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_48bppRGB
);
1013 case PixelFormat64bppPARGB
:
1016 case PixelFormat1bppIndexed
:
1017 convert_rgb_to_indexed(getpixel_64bppPARGB
, setpixel_1bppIndexed
);
1018 case PixelFormat8bppIndexed
:
1019 convert_rgb_to_indexed(getpixel_64bppPARGB
, setpixel_8bppIndexed
);
1020 case PixelFormat16bppGrayScale
:
1021 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppGrayScale
);
1022 case PixelFormat16bppRGB555
:
1023 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppRGB555
);
1024 case PixelFormat16bppRGB565
:
1025 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppRGB565
);
1026 case PixelFormat16bppARGB1555
:
1027 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppARGB1555
);
1028 case PixelFormat24bppRGB
:
1029 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_24bppRGB
);
1030 case PixelFormat32bppRGB
:
1031 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppRGB
);
1032 case PixelFormat32bppARGB
:
1033 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppARGB
);
1034 case PixelFormat32bppPARGB
:
1035 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppPARGB
);
1036 case PixelFormat48bppRGB
:
1037 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_48bppRGB
);
1038 case PixelFormat64bppARGB
:
1039 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_64bppARGB
);
1048 #undef convert_indexed_to_rgb
1049 #undef convert_rgb_to_rgb
1051 return NotImplemented
;
1054 /* This function returns a pointer to an array of pixels that represents the
1055 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1056 * flags. It is correct behavior that a user who calls this function with write
1057 * privileges can write to the whole bitmap (not just the area in rect).
1059 * FIXME: only used portion of format is bits per pixel. */
1060 GpStatus WINGDIPAPI
GdipBitmapLockBits(GpBitmap
* bitmap
, GDIPCONST GpRect
* rect
,
1061 UINT flags
, PixelFormat format
, BitmapData
* lockeddata
)
1063 INT bitspp
= PIXELFORMATBPP(format
);
1064 GpRect act_rect
; /* actual rect to be used */
1068 TRACE("%p %p %d 0x%x %p\n", bitmap
, rect
, flags
, format
, lockeddata
);
1070 if(!lockeddata
|| !bitmap
)
1071 return InvalidParameter
;
1072 if(!image_lock(&bitmap
->image
, &unlock
))
1076 if(rect
->X
< 0 || rect
->Y
< 0 || (rect
->X
+ rect
->Width
> bitmap
->width
) ||
1077 (rect
->Y
+ rect
->Height
> bitmap
->height
) || !flags
)
1079 image_unlock(&bitmap
->image
, unlock
);
1080 return InvalidParameter
;
1086 act_rect
.X
= act_rect
.Y
= 0;
1087 act_rect
.Width
= bitmap
->width
;
1088 act_rect
.Height
= bitmap
->height
;
1091 if(bitmap
->lockmode
)
1093 WARN("bitmap is already locked and cannot be locked again\n");
1094 image_unlock(&bitmap
->image
, unlock
);
1098 if (bitmap
->bits
&& bitmap
->format
== format
&& !(flags
& ImageLockModeUserInputBuf
))
1100 /* no conversion is necessary; just use the bits directly */
1101 lockeddata
->Width
= act_rect
.Width
;
1102 lockeddata
->Height
= act_rect
.Height
;
1103 lockeddata
->PixelFormat
= format
;
1104 lockeddata
->Reserved
= flags
;
1105 lockeddata
->Stride
= bitmap
->stride
;
1106 lockeddata
->Scan0
= bitmap
->bits
+ (bitspp
/ 8) * act_rect
.X
+
1107 bitmap
->stride
* act_rect
.Y
;
1109 bitmap
->lockmode
= flags
| ImageLockModeRead
;
1111 image_unlock(&bitmap
->image
, unlock
);
1115 /* Make sure we can convert to the requested format. */
1116 if (flags
& ImageLockModeRead
)
1118 stat
= convert_pixels(0, 0, 0, NULL
, format
, 0, NULL
, bitmap
->format
, NULL
);
1119 if (stat
== NotImplemented
)
1121 FIXME("cannot read bitmap from %x to %x\n", bitmap
->format
, format
);
1122 image_unlock(&bitmap
->image
, unlock
);
1123 return NotImplemented
;
1127 /* If we're opening for writing, make sure we'll be able to write back in
1128 * the original format. */
1129 if (flags
& ImageLockModeWrite
)
1131 stat
= convert_pixels(0, 0, 0, NULL
, bitmap
->format
, 0, NULL
, format
, NULL
);
1132 if (stat
== NotImplemented
)
1134 FIXME("cannot write bitmap from %x to %x\n", format
, bitmap
->format
);
1135 image_unlock(&bitmap
->image
, unlock
);
1136 return NotImplemented
;
1140 lockeddata
->Width
= act_rect
.Width
;
1141 lockeddata
->Height
= act_rect
.Height
;
1142 lockeddata
->PixelFormat
= format
;
1143 lockeddata
->Reserved
= flags
;
1145 if(!(flags
& ImageLockModeUserInputBuf
))
1147 lockeddata
->Stride
= (((act_rect
.Width
* bitspp
+ 7) / 8) + 3) & ~3;
1149 bitmap
->bitmapbits
= heap_alloc_zero(lockeddata
->Stride
* act_rect
.Height
);
1151 if (!bitmap
->bitmapbits
)
1153 image_unlock(&bitmap
->image
, unlock
);
1157 lockeddata
->Scan0
= bitmap
->bitmapbits
;
1160 if (flags
& ImageLockModeRead
)
1162 static BOOL fixme
= FALSE
;
1164 if (!fixme
&& (PIXELFORMATBPP(bitmap
->format
) * act_rect
.X
) % 8 != 0)
1166 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1170 stat
= convert_pixels(act_rect
.Width
, act_rect
.Height
,
1171 lockeddata
->Stride
, lockeddata
->Scan0
, format
,
1173 bitmap
->bits
+ bitmap
->stride
* act_rect
.Y
+ PIXELFORMATBPP(bitmap
->format
) * act_rect
.X
/ 8,
1174 bitmap
->format
, bitmap
->image
.palette
);
1178 heap_free(bitmap
->bitmapbits
);
1179 bitmap
->bitmapbits
= NULL
;
1180 image_unlock(&bitmap
->image
, unlock
);
1185 bitmap
->lockmode
= flags
| ImageLockModeRead
;
1186 bitmap
->lockx
= act_rect
.X
;
1187 bitmap
->locky
= act_rect
.Y
;
1189 image_unlock(&bitmap
->image
, unlock
);
1193 GpStatus WINGDIPAPI
GdipBitmapSetResolution(GpBitmap
* bitmap
, REAL xdpi
, REAL ydpi
)
1195 TRACE("(%p, %.2f, %.2f)\n", bitmap
, xdpi
, ydpi
);
1197 if (!bitmap
|| xdpi
== 0.0 || ydpi
== 0.0)
1198 return InvalidParameter
;
1200 bitmap
->image
.xres
= xdpi
;
1201 bitmap
->image
.yres
= ydpi
;
1206 GpStatus WINGDIPAPI
GdipBitmapUnlockBits(GpBitmap
* bitmap
,
1207 BitmapData
* lockeddata
)
1210 static BOOL fixme
= FALSE
;
1213 TRACE("(%p,%p)\n", bitmap
, lockeddata
);
1215 if(!bitmap
|| !lockeddata
)
1216 return InvalidParameter
;
1217 if(!image_lock(&bitmap
->image
, &unlock
))
1220 if(!bitmap
->lockmode
)
1222 image_unlock(&bitmap
->image
, unlock
);
1226 if(!(lockeddata
->Reserved
& ImageLockModeWrite
)){
1227 bitmap
->lockmode
= 0;
1228 heap_free(bitmap
->bitmapbits
);
1229 bitmap
->bitmapbits
= NULL
;
1230 image_unlock(&bitmap
->image
, unlock
);
1234 if (!bitmap
->bitmapbits
&& !(lockeddata
->Reserved
& ImageLockModeUserInputBuf
))
1236 /* we passed a direct reference; no need to do anything */
1237 bitmap
->lockmode
= 0;
1238 image_unlock(&bitmap
->image
, unlock
);
1242 if (!fixme
&& (PIXELFORMATBPP(bitmap
->format
) * bitmap
->lockx
) % 8 != 0)
1244 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1248 stat
= convert_pixels(lockeddata
->Width
, lockeddata
->Height
,
1250 bitmap
->bits
+ bitmap
->stride
* bitmap
->locky
+ PIXELFORMATBPP(bitmap
->format
) * bitmap
->lockx
/ 8,
1252 lockeddata
->Stride
, lockeddata
->Scan0
, lockeddata
->PixelFormat
, NULL
);
1256 ERR("failed to convert pixels; this should never happen\n");
1259 heap_free(bitmap
->bitmapbits
);
1260 bitmap
->bitmapbits
= NULL
;
1261 bitmap
->lockmode
= 0;
1263 image_unlock(&bitmap
->image
, unlock
);
1267 GpStatus WINGDIPAPI
GdipCloneBitmapArea(REAL x
, REAL y
, REAL width
, REAL height
,
1268 PixelFormat format
, GpBitmap
* srcBitmap
, GpBitmap
** dstBitmap
)
1273 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1275 if (!srcBitmap
|| !dstBitmap
|| srcBitmap
->image
.type
!= ImageTypeBitmap
||
1277 x
+ width
> srcBitmap
->width
|| y
+ height
> srcBitmap
->height
)
1279 TRACE("<-- InvalidParameter\n");
1280 return InvalidParameter
;
1283 if (format
== PixelFormatDontCare
)
1284 format
= srcBitmap
->format
;
1286 area
.X
= gdip_round(x
);
1287 area
.Y
= gdip_round(y
);
1288 area
.Width
= gdip_round(width
);
1289 area
.Height
= gdip_round(height
);
1291 stat
= GdipCreateBitmapFromScan0(area
.Width
, area
.Height
, 0, format
, NULL
, dstBitmap
);
1294 stat
= convert_pixels(area
.Width
, area
.Height
, (*dstBitmap
)->stride
, (*dstBitmap
)->bits
, (*dstBitmap
)->format
,
1296 srcBitmap
->bits
+ srcBitmap
->stride
* area
.Y
+ PIXELFORMATBPP(srcBitmap
->format
) * area
.X
/ 8,
1297 srcBitmap
->format
, srcBitmap
->image
.palette
);
1299 if (stat
== Ok
&& srcBitmap
->image
.palette
)
1301 ColorPalette
*src_palette
, *dst_palette
;
1303 src_palette
= srcBitmap
->image
.palette
;
1305 dst_palette
= heap_alloc_zero(sizeof(UINT
) * 2 + sizeof(ARGB
) * src_palette
->Count
);
1309 dst_palette
->Flags
= src_palette
->Flags
;
1310 dst_palette
->Count
= src_palette
->Count
;
1311 memcpy(dst_palette
->Entries
, src_palette
->Entries
, sizeof(ARGB
) * src_palette
->Count
);
1313 heap_free((*dstBitmap
)->image
.palette
);
1314 (*dstBitmap
)->image
.palette
= dst_palette
;
1321 GdipDisposeImage(&(*dstBitmap
)->image
);
1330 GpStatus WINGDIPAPI
GdipCloneBitmapAreaI(INT x
, INT y
, INT width
, INT height
,
1331 PixelFormat format
, GpBitmap
* srcBitmap
, GpBitmap
** dstBitmap
)
1333 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1335 return GdipCloneBitmapArea(x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1338 GpStatus WINGDIPAPI
GdipCloneImage(GpImage
*image
, GpImage
**cloneImage
)
1340 TRACE("%p, %p\n", image
, cloneImage
);
1342 if (!image
|| !cloneImage
)
1343 return InvalidParameter
;
1345 if (image
->type
== ImageTypeBitmap
)
1347 GpBitmap
*bitmap
= (GpBitmap
*)image
;
1349 return GdipCloneBitmapAreaI(0, 0, bitmap
->width
, bitmap
->height
,
1350 bitmap
->format
, bitmap
, (GpBitmap
**)cloneImage
);
1352 else if (image
->type
== ImageTypeMetafile
&& ((GpMetafile
*)image
)->hemf
)
1354 GpMetafile
*result
, *metafile
;
1356 metafile
= (GpMetafile
*)image
;
1358 result
= heap_alloc_zero(sizeof(*result
));
1362 result
->image
.type
= ImageTypeMetafile
;
1363 result
->image
.format
= image
->format
;
1364 result
->image
.flags
= image
->flags
;
1365 result
->image
.frame_count
= 1;
1366 result
->image
.xres
= image
->xres
;
1367 result
->image
.yres
= image
->yres
;
1368 result
->bounds
= metafile
->bounds
;
1369 result
->unit
= metafile
->unit
;
1370 result
->metafile_type
= metafile
->metafile_type
;
1371 result
->hemf
= CopyEnhMetaFileW(metafile
->hemf
, NULL
);
1372 list_init(&result
->containers
);
1380 *cloneImage
= &result
->image
;
1385 WARN("GpImage with no image data (metafile in wrong state?)\n");
1386 return InvalidParameter
;
1390 GpStatus WINGDIPAPI
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
1396 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
1398 if(!filename
|| !bitmap
)
1399 return InvalidParameter
;
1403 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
1408 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
1410 IStream_Release(stream
);
1415 GpStatus WINGDIPAPI
GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO
* info
,
1416 VOID
*bits
, GpBitmap
**bitmap
)
1418 DWORD height
, stride
;
1423 TRACE("(%p, %p, %p)\n", info
, bits
, bitmap
);
1425 if (!info
|| !bits
|| !bitmap
)
1426 return InvalidParameter
;
1428 hbm
= CreateDIBSection(0, info
, DIB_RGB_COLORS
, &bmbits
, NULL
, 0);
1430 return InvalidParameter
;
1432 height
= abs(info
->bmiHeader
.biHeight
);
1433 stride
= ((info
->bmiHeader
.biWidth
* info
->bmiHeader
.biBitCount
+ 31) >> 3) & ~3;
1434 TRACE("height %u, stride %u, image size %u\n", height
, stride
, height
* stride
);
1436 memcpy(bmbits
, bits
, height
* stride
);
1438 status
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, bitmap
);
1445 GpStatus WINGDIPAPI
GdipCreateBitmapFromFileICM(GDIPCONST WCHAR
* filename
,
1448 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
1450 return GdipCreateBitmapFromFile(filename
, bitmap
);
1453 GpStatus WINGDIPAPI
GdipCreateBitmapFromResource(HINSTANCE hInstance
,
1454 GDIPCONST WCHAR
* lpBitmapName
, GpBitmap
** bitmap
)
1457 GpStatus stat
= InvalidParameter
;
1459 TRACE("%p (%s) %p\n", hInstance
, debugstr_w(lpBitmapName
), bitmap
);
1461 if(!lpBitmapName
|| !bitmap
)
1462 return InvalidParameter
;
1465 hbm
= LoadImageW(hInstance
, lpBitmapName
, IMAGE_BITMAP
, 0, 0,
1466 LR_CREATEDIBSECTION
);
1469 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, bitmap
);
1476 static inline DWORD
blend_argb_no_bkgnd_alpha(DWORD src
, DWORD bkgnd
)
1479 BYTE g
= (BYTE
)(src
>> 8);
1480 BYTE r
= (BYTE
)(src
>> 16);
1481 DWORD alpha
= (BYTE
)(src
>> 24);
1482 return ((b
+ ((BYTE
)bkgnd
* (255 - alpha
) + 127) / 255) |
1483 (g
+ ((BYTE
)(bkgnd
>> 8) * (255 - alpha
) + 127) / 255) << 8 |
1484 (r
+ ((BYTE
)(bkgnd
>> 16) * (255 - alpha
) + 127) / 255) << 16 |
1488 GpStatus WINGDIPAPI
GdipCreateHBITMAPFromBitmap(GpBitmap
* bitmap
,
1489 HBITMAP
* hbmReturn
, ARGB background
)
1494 BITMAPINFOHEADER bih
;
1498 TRACE("(%p,%p,%x)\n", bitmap
, hbmReturn
, background
);
1500 if (!bitmap
|| !hbmReturn
) return InvalidParameter
;
1501 if (!image_lock(&bitmap
->image
, &unlock
)) return ObjectBusy
;
1503 GdipGetImageWidth(&bitmap
->image
, &width
);
1504 GdipGetImageHeight(&bitmap
->image
, &height
);
1506 bih
.biSize
= sizeof(bih
);
1507 bih
.biWidth
= width
;
1508 bih
.biHeight
= height
;
1510 bih
.biBitCount
= 32;
1511 bih
.biCompression
= BI_RGB
;
1512 bih
.biSizeImage
= 0;
1513 bih
.biXPelsPerMeter
= 0;
1514 bih
.biYPelsPerMeter
= 0;
1516 bih
.biClrImportant
= 0;
1518 result
= CreateDIBSection(0, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
, (void**)&bits
, NULL
, 0);
1521 image_unlock(&bitmap
->image
, unlock
);
1522 return GenericError
;
1525 stat
= convert_pixels(width
, height
, -width
*4,
1526 bits
+ (width
* 4 * (height
- 1)), PixelFormat32bppPARGB
,
1527 bitmap
->stride
, bitmap
->bits
, bitmap
->format
, bitmap
->image
.palette
);
1530 DeleteObject(result
);
1531 image_unlock(&bitmap
->image
, unlock
);
1535 if (background
& 0xffffff)
1539 for (ptr
= (DWORD
*)bits
, i
= 0; i
< width
* height
; ptr
++, i
++)
1541 if ((*ptr
& 0xff000000) == 0xff000000) continue;
1542 *ptr
= blend_argb_no_bkgnd_alpha(*ptr
, background
);
1546 *hbmReturn
= result
;
1547 image_unlock(&bitmap
->image
, unlock
);
1551 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
1552 GpGraphics
* target
, GpBitmap
** bitmap
)
1556 TRACE("(%d, %d, %p, %p)\n", width
, height
, target
, bitmap
);
1558 if(!target
|| !bitmap
)
1559 return InvalidParameter
;
1561 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat32bppPARGB
,
1566 GdipGetDpiX(target
, &(*bitmap
)->image
.xres
);
1567 GdipGetDpiY(target
, &(*bitmap
)->image
.yres
);
1573 GpStatus WINGDIPAPI
GdipCreateBitmapFromHICON(HICON hicon
, GpBitmap
** bitmap
)
1579 UINT width
, height
, stride
;
1581 BitmapData lockeddata
;
1585 BITMAPINFOHEADER bih
;
1590 TRACE("%p, %p\n", hicon
, bitmap
);
1592 if(!bitmap
|| !GetIconInfo(hicon
, &iinfo
))
1593 return InvalidParameter
;
1595 /* get the size of the icon */
1596 ret
= GetObjectA(iinfo
.hbmColor
? iinfo
.hbmColor
: iinfo
.hbmMask
, sizeof(bm
), &bm
);
1598 DeleteObject(iinfo
.hbmColor
);
1599 DeleteObject(iinfo
.hbmMask
);
1600 return GenericError
;
1604 height
= iinfo
.hbmColor
? abs(bm
.bmHeight
) : abs(bm
.bmHeight
) / 2;
1607 stat
= GdipCreateBitmapFromScan0(width
, height
, stride
, PixelFormat32bppARGB
, NULL
, bitmap
);
1609 DeleteObject(iinfo
.hbmColor
);
1610 DeleteObject(iinfo
.hbmMask
);
1617 rect
.Height
= height
;
1619 stat
= GdipBitmapLockBits(*bitmap
, &rect
, ImageLockModeWrite
, PixelFormat32bppARGB
, &lockeddata
);
1621 DeleteObject(iinfo
.hbmColor
);
1622 DeleteObject(iinfo
.hbmMask
);
1623 GdipDisposeImage(&(*bitmap
)->image
);
1627 bih
.biSize
= sizeof(bih
);
1628 bih
.biWidth
= width
;
1629 bih
.biHeight
= iinfo
.hbmColor
? -height
: -height
* 2;
1631 bih
.biBitCount
= 32;
1632 bih
.biCompression
= BI_RGB
;
1633 bih
.biSizeImage
= 0;
1634 bih
.biXPelsPerMeter
= 0;
1635 bih
.biYPelsPerMeter
= 0;
1637 bih
.biClrImportant
= 0;
1639 screendc
= CreateCompatibleDC(0);
1642 GetDIBits(screendc
, iinfo
.hbmColor
, 0, height
, lockeddata
.Scan0
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1644 if (bm
.bmBitsPixel
== 32)
1648 /* If any pixel has a non-zero alpha, ignore hbmMask */
1649 src
= (DWORD
*)lockeddata
.Scan0
;
1650 for (x
=0; x
<width
&& !has_alpha
; x
++)
1651 for (y
=0; y
<height
&& !has_alpha
; y
++)
1652 if ((*src
++ & 0xff000000) != 0)
1655 else has_alpha
= FALSE
;
1659 GetDIBits(screendc
, iinfo
.hbmMask
, 0, height
, lockeddata
.Scan0
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1667 BYTE
*bits
= heap_alloc(height
* stride
);
1669 /* read alpha data from the mask */
1671 GetDIBits(screendc
, iinfo
.hbmMask
, 0, height
, bits
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1673 GetDIBits(screendc
, iinfo
.hbmMask
, height
, height
, bits
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1676 dst_row
= lockeddata
.Scan0
;
1677 for (y
=0; y
<height
; y
++)
1679 dst
= (DWORD
*)dst_row
;
1680 for (x
=0; x
<height
; x
++)
1682 DWORD src_value
= *src
++;
1686 *dst
++ |= 0xff000000;
1688 dst_row
+= lockeddata
.Stride
;
1695 /* set constant alpha of 255 */
1696 dst_row
= lockeddata
.Scan0
;
1697 for (y
=0; y
<height
; y
++)
1699 dst
= (DWORD
*)dst_row
;
1700 for (x
=0; x
<height
; x
++)
1701 *dst
++ |= 0xff000000;
1702 dst_row
+= lockeddata
.Stride
;
1709 DeleteObject(iinfo
.hbmColor
);
1710 DeleteObject(iinfo
.hbmMask
);
1712 GdipBitmapUnlockBits(*bitmap
, &lockeddata
);
1717 static void generate_halftone_palette(ARGB
*entries
, UINT count
)
1719 static const BYTE halftone_values
[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1722 for (i
=0; i
<8 && i
<count
; i
++)
1724 entries
[i
] = 0xff000000;
1725 if (i
&1) entries
[i
] |= 0x800000;
1726 if (i
&2) entries
[i
] |= 0x8000;
1727 if (i
&4) entries
[i
] |= 0x80;
1731 entries
[i
] = 0xffc0c0c0;
1733 for (i
=9; i
<16 && i
<count
; i
++)
1735 entries
[i
] = 0xff000000;
1736 if (i
&1) entries
[i
] |= 0xff0000;
1737 if (i
&2) entries
[i
] |= 0xff00;
1738 if (i
&4) entries
[i
] |= 0xff;
1741 for (i
=16; i
<40 && i
<count
; i
++)
1746 for (i
=40; i
<256 && i
<count
; i
++)
1748 entries
[i
] = 0xff000000;
1749 entries
[i
] |= halftone_values
[(i
-40)%6];
1750 entries
[i
] |= halftone_values
[((i
-40)/6)%6] << 8;
1751 entries
[i
] |= halftone_values
[((i
-40)/36)%6] << 16;
1755 static GpStatus
get_screen_resolution(REAL
*xres
, REAL
*yres
)
1757 HDC screendc
= CreateCompatibleDC(0);
1759 if (!screendc
) return GenericError
;
1761 *xres
= (REAL
)GetDeviceCaps(screendc
, LOGPIXELSX
);
1762 *yres
= (REAL
)GetDeviceCaps(screendc
, LOGPIXELSY
);
1769 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
1770 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
1772 HBITMAP hbitmap
=NULL
;
1773 INT row_size
, dib_stride
;
1774 BYTE
*bits
=NULL
, *own_bits
=NULL
;
1778 TRACE("%d %d %d 0x%x %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
1780 if (!bitmap
) return InvalidParameter
;
1782 if(width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
1784 return InvalidParameter
;
1787 if(scan0
&& !stride
)
1788 return InvalidParameter
;
1790 stat
= get_screen_resolution(&xres
, &yres
);
1791 if (stat
!= Ok
) return stat
;
1793 row_size
= (width
* PIXELFORMATBPP(format
)+7) / 8;
1794 dib_stride
= (row_size
+ 3) & ~3;
1797 stride
= dib_stride
;
1799 if (format
& PixelFormatGDI
&& !(format
& (PixelFormatAlpha
|PixelFormatIndexed
)) && !scan0
)
1801 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
[256])];
1802 BITMAPINFO
*pbmi
= (BITMAPINFO
*)bmibuf
;
1804 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1805 pbmi
->bmiHeader
.biWidth
= width
;
1806 pbmi
->bmiHeader
.biHeight
= -height
;
1807 pbmi
->bmiHeader
.biPlanes
= 1;
1808 /* FIXME: use the rest of the data from format */
1809 pbmi
->bmiHeader
.biBitCount
= PIXELFORMATBPP(format
);
1810 pbmi
->bmiHeader
.biCompression
= BI_RGB
;
1811 pbmi
->bmiHeader
.biSizeImage
= 0;
1812 pbmi
->bmiHeader
.biXPelsPerMeter
= 0;
1813 pbmi
->bmiHeader
.biYPelsPerMeter
= 0;
1814 pbmi
->bmiHeader
.biClrUsed
= 0;
1815 pbmi
->bmiHeader
.biClrImportant
= 0;
1817 hbitmap
= CreateDIBSection(0, pbmi
, DIB_RGB_COLORS
, (void**)&bits
, NULL
, 0);
1819 if (!hbitmap
) return GenericError
;
1821 stride
= dib_stride
;
1825 /* Not a GDI format; don't try to make an HBITMAP. */
1830 INT size
= abs(stride
) * height
;
1832 own_bits
= bits
= heap_alloc_zero(size
);
1833 if (!own_bits
) return OutOfMemory
;
1836 bits
+= stride
* (1 - height
);
1840 *bitmap
= heap_alloc_zero(sizeof(GpBitmap
));
1843 DeleteObject(hbitmap
);
1844 heap_free(own_bits
);
1848 (*bitmap
)->image
.type
= ImageTypeBitmap
;
1849 memcpy(&(*bitmap
)->image
.format
, &ImageFormatMemoryBMP
, sizeof(GUID
));
1850 (*bitmap
)->image
.flags
= ImageFlagsNone
;
1851 (*bitmap
)->image
.frame_count
= 1;
1852 (*bitmap
)->image
.current_frame
= 0;
1853 (*bitmap
)->image
.palette
= NULL
;
1854 (*bitmap
)->image
.xres
= xres
;
1855 (*bitmap
)->image
.yres
= yres
;
1856 (*bitmap
)->width
= width
;
1857 (*bitmap
)->height
= height
;
1858 (*bitmap
)->format
= format
;
1859 (*bitmap
)->image
.decoder
= NULL
;
1860 (*bitmap
)->image
.encoder
= NULL
;
1861 (*bitmap
)->hbitmap
= hbitmap
;
1862 (*bitmap
)->hdc
= NULL
;
1863 (*bitmap
)->bits
= bits
;
1864 (*bitmap
)->stride
= stride
;
1865 (*bitmap
)->own_bits
= own_bits
;
1866 (*bitmap
)->metadata_reader
= NULL
;
1867 (*bitmap
)->prop_count
= 0;
1868 (*bitmap
)->prop_item
= NULL
;
1870 /* set format-related flags */
1871 if (format
& (PixelFormatAlpha
|PixelFormatPAlpha
|PixelFormatIndexed
))
1872 (*bitmap
)->image
.flags
|= ImageFlagsHasAlpha
;
1874 if (format
== PixelFormat1bppIndexed
||
1875 format
== PixelFormat4bppIndexed
||
1876 format
== PixelFormat8bppIndexed
)
1878 (*bitmap
)->image
.palette
= heap_alloc_zero(sizeof(UINT
) * 2 + sizeof(ARGB
) * (1 << PIXELFORMATBPP(format
)));
1880 if (!(*bitmap
)->image
.palette
)
1882 GdipDisposeImage(&(*bitmap
)->image
);
1887 (*bitmap
)->image
.palette
->Count
= 1 << PIXELFORMATBPP(format
);
1889 if (format
== PixelFormat1bppIndexed
)
1891 (*bitmap
)->image
.palette
->Flags
= PaletteFlagsGrayScale
;
1892 (*bitmap
)->image
.palette
->Entries
[0] = 0xff000000;
1893 (*bitmap
)->image
.palette
->Entries
[1] = 0xffffffff;
1897 if (format
== PixelFormat8bppIndexed
)
1898 (*bitmap
)->image
.palette
->Flags
= PaletteFlagsHalftone
;
1900 generate_halftone_palette((*bitmap
)->image
.palette
->Entries
,
1901 (*bitmap
)->image
.palette
->Count
);
1905 TRACE("<-- %p\n", *bitmap
);
1910 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
1915 TRACE("%p %p\n", stream
, bitmap
);
1917 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
1922 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
1923 GdipDisposeImage(&(*bitmap
)->image
);
1925 return GenericError
; /* FIXME: what error to return? */
1932 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
1935 TRACE("%p %p\n", stream
, bitmap
);
1937 return GdipCreateBitmapFromStream(stream
, bitmap
);
1940 GpStatus WINGDIPAPI
GdipCreateCachedBitmap(GpBitmap
*bitmap
, GpGraphics
*graphics
,
1941 GpCachedBitmap
**cachedbmp
)
1945 TRACE("%p %p %p\n", bitmap
, graphics
, cachedbmp
);
1947 if(!bitmap
|| !graphics
|| !cachedbmp
)
1948 return InvalidParameter
;
1950 *cachedbmp
= heap_alloc_zero(sizeof(GpCachedBitmap
));
1954 stat
= GdipCloneImage(&(bitmap
->image
), &(*cachedbmp
)->image
);
1956 heap_free(*cachedbmp
);
1963 GpStatus WINGDIPAPI
GdipCreateHICONFromBitmap(GpBitmap
*bitmap
, HICON
*hicon
)
1966 BitmapData lockeddata
;
1967 ULONG andstride
, xorstride
, bitssize
;
1968 LPBYTE andbits
, xorbits
, androw
, xorrow
, srcrow
;
1971 TRACE("(%p, %p)\n", bitmap
, hicon
);
1973 if (!bitmap
|| !hicon
)
1974 return InvalidParameter
;
1976 stat
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
,
1977 PixelFormat32bppPARGB
, &lockeddata
);
1980 andstride
= ((lockeddata
.Width
+31)/32)*4;
1981 xorstride
= lockeddata
.Width
*4;
1982 bitssize
= (andstride
+ xorstride
) * lockeddata
.Height
;
1984 andbits
= heap_alloc_zero(bitssize
);
1988 xorbits
= andbits
+ andstride
* lockeddata
.Height
;
1990 for (y
=0; y
<lockeddata
.Height
; y
++)
1992 srcrow
= ((LPBYTE
)lockeddata
.Scan0
) + lockeddata
.Stride
* y
;
1994 androw
= andbits
+ andstride
* y
;
1995 for (x
=0; x
<lockeddata
.Width
; x
++)
1996 if (srcrow
[3+4*x
] >= 128)
1997 androw
[x
/8] |= 1 << (7-x
%8);
1999 xorrow
= xorbits
+ xorstride
* y
;
2000 memcpy(xorrow
, srcrow
, xorstride
);
2003 *hicon
= CreateIcon(NULL
, lockeddata
.Width
, lockeddata
.Height
, 1, 32,
2011 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
2017 GpStatus WINGDIPAPI
GdipDeleteCachedBitmap(GpCachedBitmap
*cachedbmp
)
2019 TRACE("%p\n", cachedbmp
);
2022 return InvalidParameter
;
2024 GdipDisposeImage(cachedbmp
->image
);
2025 heap_free(cachedbmp
);
2030 GpStatus WINGDIPAPI
GdipDrawCachedBitmap(GpGraphics
*graphics
,
2031 GpCachedBitmap
*cachedbmp
, INT x
, INT y
)
2033 TRACE("%p %p %d %d\n", graphics
, cachedbmp
, x
, y
);
2035 if(!graphics
|| !cachedbmp
)
2036 return InvalidParameter
;
2038 return GdipDrawImage(graphics
, cachedbmp
->image
, (REAL
)x
, (REAL
)y
);
2041 /* Internal utility function: Replace the image data of dst with that of src,
2043 static void move_bitmap(GpBitmap
*dst
, GpBitmap
*src
, BOOL clobber_palette
)
2045 assert(src
->image
.type
== ImageTypeBitmap
);
2046 assert(dst
->image
.type
== ImageTypeBitmap
);
2048 heap_free(dst
->bitmapbits
);
2049 heap_free(dst
->own_bits
);
2051 DeleteObject(dst
->hbitmap
);
2053 if (clobber_palette
)
2055 heap_free(dst
->image
.palette
);
2056 dst
->image
.palette
= src
->image
.palette
;
2059 heap_free(src
->image
.palette
);
2061 dst
->image
.xres
= src
->image
.xres
;
2062 dst
->image
.yres
= src
->image
.yres
;
2063 dst
->width
= src
->width
;
2064 dst
->height
= src
->height
;
2065 dst
->format
= src
->format
;
2066 dst
->hbitmap
= src
->hbitmap
;
2067 dst
->hdc
= src
->hdc
;
2068 dst
->bits
= src
->bits
;
2069 dst
->stride
= src
->stride
;
2070 dst
->own_bits
= src
->own_bits
;
2071 if (dst
->metadata_reader
)
2072 IWICMetadataReader_Release(dst
->metadata_reader
);
2073 dst
->metadata_reader
= src
->metadata_reader
;
2074 heap_free(dst
->prop_item
);
2075 dst
->prop_item
= src
->prop_item
;
2076 dst
->prop_count
= src
->prop_count
;
2077 if (dst
->image
.decoder
)
2078 IWICBitmapDecoder_Release(dst
->image
.decoder
);
2079 dst
->image
.decoder
= src
->image
.decoder
;
2080 terminate_encoder_wic(&dst
->image
); /* terminate active encoder before overwriting with src */
2081 dst
->image
.encoder
= src
->image
.encoder
;
2082 dst
->image
.frame_count
= src
->image
.frame_count
;
2083 dst
->image
.current_frame
= src
->image
.current_frame
;
2084 dst
->image
.format
= src
->image
.format
;
2086 src
->image
.type
= ~0;
2090 static GpStatus
free_image_data(GpImage
*image
)
2093 return InvalidParameter
;
2095 if (image
->type
== ImageTypeBitmap
)
2097 heap_free(((GpBitmap
*)image
)->bitmapbits
);
2098 heap_free(((GpBitmap
*)image
)->own_bits
);
2099 DeleteDC(((GpBitmap
*)image
)->hdc
);
2100 DeleteObject(((GpBitmap
*)image
)->hbitmap
);
2101 if (((GpBitmap
*)image
)->metadata_reader
)
2102 IWICMetadataReader_Release(((GpBitmap
*)image
)->metadata_reader
);
2103 heap_free(((GpBitmap
*)image
)->prop_item
);
2105 else if (image
->type
== ImageTypeMetafile
)
2106 METAFILE_Free((GpMetafile
*)image
);
2109 WARN("invalid image: %p\n", image
);
2113 IWICBitmapDecoder_Release(image
->decoder
);
2114 terminate_encoder_wic(image
);
2115 heap_free(image
->palette
);
2120 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
2124 TRACE("%p\n", image
);
2126 status
= free_image_data(image
);
2127 if (status
!= Ok
) return status
;
2134 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
2138 TRACE("(%p,%p)\n", image
, item
);
2141 return InvalidParameter
;
2144 FIXME("not implemented\n");
2146 return NotImplemented
;
2149 GpStatus WINGDIPAPI
GdipGetImageItemData(GpImage
*image
, ImageItemData
*item
)
2153 TRACE("(%p,%p)\n", image
, item
);
2156 FIXME("not implemented\n");
2158 return NotImplemented
;
2161 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
2164 TRACE("%p %p %p\n", image
, srcRect
, srcUnit
);
2166 if(!image
|| !srcRect
|| !srcUnit
)
2167 return InvalidParameter
;
2168 if(image
->type
== ImageTypeMetafile
){
2169 *srcRect
= ((GpMetafile
*)image
)->bounds
;
2170 *srcUnit
= ((GpMetafile
*)image
)->unit
;
2172 else if(image
->type
== ImageTypeBitmap
){
2173 srcRect
->X
= srcRect
->Y
= 0.0;
2174 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
2175 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
2176 *srcUnit
= UnitPixel
;
2179 WARN("GpImage with no image data\n");
2180 return InvalidParameter
;
2183 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
2184 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
2189 GpStatus WINGDIPAPI
GdipGetImageDimension(GpImage
*image
, REAL
*width
,
2192 TRACE("%p %p %p\n", image
, width
, height
);
2194 if(!image
|| !height
|| !width
)
2195 return InvalidParameter
;
2197 if(image
->type
== ImageTypeMetafile
){
2198 *height
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Height
, ((GpMetafile
*)image
)->unit
,
2199 image
->yres
, ((GpMetafile
*)image
)->printer_display
);
2200 *width
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Width
, ((GpMetafile
*)image
)->unit
,
2201 image
->xres
, ((GpMetafile
*)image
)->printer_display
);
2203 else if(image
->type
== ImageTypeBitmap
){
2204 *height
= ((GpBitmap
*)image
)->height
;
2205 *width
= ((GpBitmap
*)image
)->width
;
2208 WARN("GpImage with no image data\n");
2209 return InvalidParameter
;
2212 TRACE("returning (%f, %f)\n", *height
, *width
);
2216 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
2217 GpGraphics
**graphics
)
2222 TRACE("%p %p\n", image
, graphics
);
2224 if(!image
|| !graphics
)
2225 return InvalidParameter
;
2227 if (image
->type
== ImageTypeBitmap
&& ((GpBitmap
*)image
)->hbitmap
)
2229 hdc
= ((GpBitmap
*)image
)->hdc
;
2232 hdc
= CreateCompatibleDC(0);
2233 SelectObject(hdc
, ((GpBitmap
*)image
)->hbitmap
);
2234 ((GpBitmap
*)image
)->hdc
= hdc
;
2237 stat
= GdipCreateFromHDC(hdc
, graphics
);
2241 (*graphics
)->image
= image
;
2242 (*graphics
)->image_type
= image
->type
;
2243 (*graphics
)->xres
= image
->xres
;
2244 (*graphics
)->yres
= image
->yres
;
2247 else if (image
->type
== ImageTypeMetafile
)
2248 stat
= METAFILE_GetGraphicsContext((GpMetafile
*)image
, graphics
);
2250 stat
= graphics_from_image(image
, graphics
);
2255 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
2257 TRACE("%p %p\n", image
, height
);
2259 if(!image
|| !height
)
2260 return InvalidParameter
;
2262 if(image
->type
== ImageTypeMetafile
)
2263 *height
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Height
, ((GpMetafile
*)image
)->unit
,
2264 image
->yres
, ((GpMetafile
*)image
)->printer_display
);
2265 else if(image
->type
== ImageTypeBitmap
)
2266 *height
= ((GpBitmap
*)image
)->height
;
2269 WARN("GpImage with no image data\n");
2270 return InvalidParameter
;
2273 TRACE("returning %d\n", *height
);
2278 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
2281 return InvalidParameter
;
2285 TRACE("(%p) <-- %0.2f\n", image
, *res
);
2290 GpStatus WINGDIPAPI
GdipGetImagePaletteSize(GpImage
*image
, INT
*size
)
2292 TRACE("%p %p\n", image
, size
);
2295 return InvalidParameter
;
2297 if (image
->type
== ImageTypeMetafile
)
2300 return GenericError
;
2303 if (!image
->palette
|| image
->palette
->Count
== 0)
2304 *size
= sizeof(ColorPalette
);
2306 *size
= sizeof(UINT
)*2 + sizeof(ARGB
)*image
->palette
->Count
;
2308 TRACE("<-- %u\n", *size
);
2313 /* FIXME: test this function for non-bitmap types */
2314 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
2316 TRACE("%p %p\n", image
, format
);
2318 if(!image
|| !format
)
2319 return InvalidParameter
;
2321 if(image
->type
!= ImageTypeBitmap
)
2322 *format
= PixelFormat24bppRGB
;
2324 *format
= ((GpBitmap
*) image
)->format
;
2329 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
2331 TRACE("(%p, %p)\n", image
, format
);
2333 if(!image
|| !format
)
2334 return InvalidParameter
;
2336 memcpy(format
, &image
->format
, sizeof(GUID
));
2341 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
2343 TRACE("%p %p\n", image
, type
);
2346 return InvalidParameter
;
2348 *type
= image
->type
;
2353 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
2356 return InvalidParameter
;
2360 TRACE("(%p) <-- %0.2f\n", image
, *res
);
2365 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
2367 TRACE("%p %p\n", image
, width
);
2369 if(!image
|| !width
)
2370 return InvalidParameter
;
2372 if(image
->type
== ImageTypeMetafile
)
2373 *width
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Width
, ((GpMetafile
*)image
)->unit
,
2374 image
->xres
, ((GpMetafile
*)image
)->printer_display
);
2375 else if(image
->type
== ImageTypeBitmap
)
2376 *width
= ((GpBitmap
*)image
)->width
;
2379 WARN("GpImage with no image data\n");
2380 return InvalidParameter
;
2383 TRACE("returning %d\n", *width
);
2388 GpStatus WINGDIPAPI
GdipGetPropertyCount(GpImage
*image
, UINT
*num
)
2390 TRACE("(%p, %p)\n", image
, num
);
2392 if (!image
|| !num
) return InvalidParameter
;
2396 if (image
->type
== ImageTypeBitmap
)
2398 if (((GpBitmap
*)image
)->prop_item
)
2400 *num
= ((GpBitmap
*)image
)->prop_count
;
2404 if (((GpBitmap
*)image
)->metadata_reader
)
2405 IWICMetadataReader_GetCount(((GpBitmap
*)image
)->metadata_reader
, num
);
2411 GpStatus WINGDIPAPI
GdipGetPropertyIdList(GpImage
*image
, UINT num
, PROPID
*list
)
2414 IWICMetadataReader
*reader
;
2415 IWICEnumMetadataItem
*enumerator
;
2416 UINT prop_count
, i
, items_returned
;
2418 TRACE("(%p, %u, %p)\n", image
, num
, list
);
2420 if (!image
|| !list
) return InvalidParameter
;
2422 if (image
->type
!= ImageTypeBitmap
)
2424 FIXME("Not implemented for type %d\n", image
->type
);
2425 return NotImplemented
;
2428 if (((GpBitmap
*)image
)->prop_item
)
2430 if (num
!= ((GpBitmap
*)image
)->prop_count
) return InvalidParameter
;
2432 for (i
= 0; i
< num
; i
++)
2434 list
[i
] = ((GpBitmap
*)image
)->prop_item
[i
].id
;
2440 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2443 if (num
!= 0) return InvalidParameter
;
2447 hr
= IWICMetadataReader_GetCount(reader
, &prop_count
);
2448 if (FAILED(hr
)) return hresult_to_status(hr
);
2450 if (num
!= prop_count
) return InvalidParameter
;
2452 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2453 if (FAILED(hr
)) return hresult_to_status(hr
);
2455 IWICEnumMetadataItem_Reset(enumerator
);
2457 for (i
= 0; i
< num
; i
++)
2461 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, NULL
, &items_returned
);
2462 if (hr
!= S_OK
) break;
2464 if (id
.vt
!= VT_UI2
)
2466 FIXME("not supported propvariant type for id: %u\n", id
.vt
);
2473 IWICEnumMetadataItem_Release(enumerator
);
2475 return hr
== S_OK
? Ok
: hresult_to_status(hr
);
2478 static UINT
propvariant_size(PROPVARIANT
*value
)
2480 switch (value
->vt
& ~VT_VECTOR
)
2486 if (!(value
->vt
& VT_VECTOR
)) return 1;
2487 return value
->caub
.cElems
;
2490 if (!(value
->vt
& VT_VECTOR
)) return 2;
2491 return value
->caui
.cElems
* 2;
2495 if (!(value
->vt
& VT_VECTOR
)) return 4;
2496 return value
->caul
.cElems
* 4;
2500 if (!(value
->vt
& VT_VECTOR
)) return 8;
2501 return value
->cauh
.cElems
* 8;
2503 return value
->pszVal
? strlen(value
->pszVal
) + 1 : 0;
2505 return value
->blob
.cbSize
;
2507 FIXME("not supported variant type %d\n", value
->vt
);
2512 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID propid
, UINT
*size
)
2515 IWICMetadataReader
*reader
;
2516 PROPVARIANT id
, value
;
2518 TRACE("(%p,%#x,%p)\n", image
, propid
, size
);
2520 if (!size
|| !image
) return InvalidParameter
;
2522 if (image
->type
!= ImageTypeBitmap
)
2524 FIXME("Not implemented for type %d\n", image
->type
);
2525 return NotImplemented
;
2528 if (((GpBitmap
*)image
)->prop_item
)
2532 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2534 if (propid
== ((GpBitmap
*)image
)->prop_item
[i
].id
)
2536 *size
= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
;
2541 return PropertyNotFound
;
2544 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2545 if (!reader
) return PropertyNotFound
;
2549 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
2550 if (FAILED(hr
)) return PropertyNotFound
;
2552 *size
= propvariant_size(&value
);
2553 if (*size
) *size
+= sizeof(PropertyItem
);
2554 PropVariantClear(&value
);
2559 #ifndef PropertyTagTypeSByte
2560 #define PropertyTagTypeSByte 6
2561 #define PropertyTagTypeSShort 8
2562 #define PropertyTagTypeFloat 11
2563 #define PropertyTagTypeDouble 12
2566 static UINT
vt_to_itemtype(UINT vt
)
2573 { VT_I1
, PropertyTagTypeSByte
},
2574 { VT_UI1
, PropertyTagTypeByte
},
2575 { VT_I2
, PropertyTagTypeSShort
},
2576 { VT_UI2
, PropertyTagTypeShort
},
2577 { VT_I4
, PropertyTagTypeSLONG
},
2578 { VT_UI4
, PropertyTagTypeLong
},
2579 { VT_I8
, PropertyTagTypeSRational
},
2580 { VT_UI8
, PropertyTagTypeRational
},
2581 { VT_R4
, PropertyTagTypeFloat
},
2582 { VT_R8
, PropertyTagTypeDouble
},
2583 { VT_LPSTR
, PropertyTagTypeASCII
},
2584 { VT_BLOB
, PropertyTagTypeUndefined
}
2587 for (i
= 0; i
< ARRAY_SIZE(vt2type
); i
++)
2589 if (vt2type
[i
].vt
== vt
) return vt2type
[i
].type
;
2591 FIXME("not supported variant type %u\n", vt
);
2595 static GpStatus
propvariant_to_item(PROPVARIANT
*value
, PropertyItem
*item
,
2596 UINT size
, PROPID id
)
2598 UINT item_size
, item_type
;
2600 item_size
= propvariant_size(value
);
2601 if (size
!= item_size
+ sizeof(PropertyItem
)) return InvalidParameter
;
2603 item_type
= vt_to_itemtype(value
->vt
& ~VT_VECTOR
);
2604 if (!item_type
) return InvalidParameter
;
2606 item
->value
= item
+ 1;
2608 switch (value
->vt
& ~VT_VECTOR
)
2612 if (!(value
->vt
& VT_VECTOR
))
2613 *(BYTE
*)item
->value
= value
->bVal
;
2615 memcpy(item
->value
, value
->caub
.pElems
, item_size
);
2619 if (!(value
->vt
& VT_VECTOR
))
2620 *(USHORT
*)item
->value
= value
->uiVal
;
2622 memcpy(item
->value
, value
->caui
.pElems
, item_size
);
2627 if (!(value
->vt
& VT_VECTOR
))
2628 *(ULONG
*)item
->value
= value
->ulVal
;
2630 memcpy(item
->value
, value
->caul
.pElems
, item_size
);
2635 if (!(value
->vt
& VT_VECTOR
))
2636 *(ULONGLONG
*)item
->value
= value
->uhVal
.QuadPart
;
2638 memcpy(item
->value
, value
->cauh
.pElems
, item_size
);
2641 memcpy(item
->value
, value
->pszVal
, item_size
);
2644 memcpy(item
->value
, value
->blob
.pBlobData
, item_size
);
2647 FIXME("not supported variant type %d\n", value
->vt
);
2648 return InvalidParameter
;
2651 item
->length
= item_size
;
2652 item
->type
= item_type
;
2658 GpStatus WINGDIPAPI
GdipGetPropertyItem(GpImage
*image
, PROPID propid
, UINT size
,
2659 PropertyItem
*buffer
)
2663 IWICMetadataReader
*reader
;
2664 PROPVARIANT id
, value
;
2666 TRACE("(%p,%#x,%u,%p)\n", image
, propid
, size
, buffer
);
2668 if (!image
|| !buffer
) return InvalidParameter
;
2670 if (image
->type
!= ImageTypeBitmap
)
2672 FIXME("Not implemented for type %d\n", image
->type
);
2673 return NotImplemented
;
2676 if (((GpBitmap
*)image
)->prop_item
)
2680 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2682 if (propid
== ((GpBitmap
*)image
)->prop_item
[i
].id
)
2684 if (size
!= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
)
2685 return InvalidParameter
;
2687 *buffer
= ((GpBitmap
*)image
)->prop_item
[i
];
2688 buffer
->value
= buffer
+ 1;
2689 memcpy(buffer
->value
, ((GpBitmap
*)image
)->prop_item
[i
].value
, buffer
->length
);
2694 return PropertyNotFound
;
2697 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2698 if (!reader
) return PropertyNotFound
;
2702 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
2703 if (FAILED(hr
)) return PropertyNotFound
;
2705 stat
= propvariant_to_item(&value
, buffer
, size
, propid
);
2706 PropVariantClear(&value
);
2711 GpStatus WINGDIPAPI
GdipGetPropertySize(GpImage
*image
, UINT
*size
, UINT
*count
)
2714 IWICMetadataReader
*reader
;
2715 IWICEnumMetadataItem
*enumerator
;
2716 UINT prop_count
, prop_size
, i
;
2717 PROPVARIANT id
, value
;
2719 TRACE("(%p,%p,%p)\n", image
, size
, count
);
2721 if (!image
|| !size
|| !count
) return InvalidParameter
;
2723 if (image
->type
!= ImageTypeBitmap
)
2725 FIXME("Not implemented for type %d\n", image
->type
);
2726 return NotImplemented
;
2729 if (((GpBitmap
*)image
)->prop_item
)
2731 *count
= ((GpBitmap
*)image
)->prop_count
;
2734 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2736 *size
+= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
;
2742 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2743 if (!reader
) return PropertyNotFound
;
2745 hr
= IWICMetadataReader_GetCount(reader
, &prop_count
);
2746 if (FAILED(hr
)) return hresult_to_status(hr
);
2748 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2749 if (FAILED(hr
)) return hresult_to_status(hr
);
2751 IWICEnumMetadataItem_Reset(enumerator
);
2755 PropVariantInit(&id
);
2756 PropVariantInit(&value
);
2758 for (i
= 0; i
< prop_count
; i
++)
2760 UINT items_returned
, item_size
;
2762 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, &value
, &items_returned
);
2763 if (hr
!= S_OK
) break;
2765 item_size
= propvariant_size(&value
);
2766 if (item_size
) prop_size
+= sizeof(PropertyItem
) + item_size
;
2768 PropVariantClear(&id
);
2769 PropVariantClear(&value
);
2772 IWICEnumMetadataItem_Release(enumerator
);
2774 if (hr
!= S_OK
) return PropertyNotFound
;
2776 *count
= prop_count
;
2781 GpStatus WINGDIPAPI
GdipGetAllPropertyItems(GpImage
*image
, UINT size
,
2782 UINT count
, PropertyItem
*buf
)
2786 IWICMetadataReader
*reader
;
2787 IWICEnumMetadataItem
*enumerator
;
2788 UINT prop_count
, prop_size
, i
;
2789 PROPVARIANT id
, value
;
2792 TRACE("(%p,%u,%u,%p)\n", image
, size
, count
, buf
);
2794 if (!image
|| !buf
) return InvalidParameter
;
2796 if (image
->type
!= ImageTypeBitmap
)
2798 FIXME("Not implemented for type %d\n", image
->type
);
2799 return NotImplemented
;
2802 status
= GdipGetPropertySize(image
, &prop_size
, &prop_count
);
2803 if (status
!= Ok
) return status
;
2805 if (prop_count
!= count
|| prop_size
!= size
) return InvalidParameter
;
2807 if (((GpBitmap
*)image
)->prop_item
)
2809 memcpy(buf
, ((GpBitmap
*)image
)->prop_item
, prop_size
);
2811 item_value
= (char *)(buf
+ prop_count
);
2813 for (i
= 0; i
< prop_count
; i
++)
2815 buf
[i
].value
= item_value
;
2816 item_value
+= buf
[i
].length
;
2822 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2823 if (!reader
) return PropertyNotFound
;
2825 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2826 if (FAILED(hr
)) return hresult_to_status(hr
);
2828 IWICEnumMetadataItem_Reset(enumerator
);
2830 item_value
= (char *)(buf
+ prop_count
);
2832 PropVariantInit(&id
);
2833 PropVariantInit(&value
);
2835 for (i
= 0; i
< prop_count
; i
++)
2838 UINT items_returned
, item_size
;
2840 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, &value
, &items_returned
);
2841 if (hr
!= S_OK
) break;
2843 if (id
.vt
!= VT_UI2
)
2845 FIXME("not supported propvariant type for id: %u\n", id
.vt
);
2849 item_size
= propvariant_size(&value
);
2852 item
= heap_alloc(item_size
+ sizeof(*item
));
2854 propvariant_to_item(&value
, item
, item_size
+ sizeof(*item
), id
.uiVal
);
2855 buf
[i
].id
= item
->id
;
2856 buf
[i
].type
= item
->type
;
2857 buf
[i
].length
= item_size
;
2858 buf
[i
].value
= item_value
;
2859 memcpy(item_value
, item
->value
, item_size
);
2860 item_value
+= item_size
;
2865 PropVariantClear(&id
);
2866 PropVariantClear(&value
);
2869 IWICEnumMetadataItem_Release(enumerator
);
2871 if (hr
!= S_OK
) return PropertyNotFound
;
2876 struct image_format_dimension
2879 const GUID
*dimension
;
2882 static const struct image_format_dimension image_format_dimensions
[] =
2884 {&ImageFormatGIF
, &FrameDimensionTime
},
2885 {&ImageFormatIcon
, &FrameDimensionResolution
},
2889 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
2890 GDIPCONST GUID
* dimensionID
, UINT
* count
)
2892 TRACE("(%p,%s,%p)\n", image
, debugstr_guid(dimensionID
), count
);
2894 if(!image
|| !count
)
2895 return InvalidParameter
;
2898 IsEqualGUID(dimensionID
, &image
->format
) ||
2899 IsEqualGUID(dimensionID
, &FrameDimensionPage
) ||
2900 IsEqualGUID(dimensionID
, &FrameDimensionTime
))
2902 *count
= image
->frame_count
;
2906 return InvalidParameter
;
2909 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsCount(GpImage
*image
,
2912 TRACE("(%p, %p)\n", image
, count
);
2914 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2916 if(!image
|| !count
)
2917 return InvalidParameter
;
2924 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
2925 GUID
* dimensionIDs
, UINT count
)
2928 const GUID
*result
=NULL
;
2930 TRACE("(%p,%p,%u)\n", image
, dimensionIDs
, count
);
2932 if(!image
|| !dimensionIDs
|| count
!= 1)
2933 return InvalidParameter
;
2935 for (i
=0; image_format_dimensions
[i
].format
; i
++)
2937 if (IsEqualGUID(&image
->format
, image_format_dimensions
[i
].format
))
2939 result
= image_format_dimensions
[i
].dimension
;
2945 result
= &FrameDimensionPage
;
2947 memcpy(dimensionIDs
, result
, sizeof(GUID
));
2952 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
2958 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
2960 if (!filename
|| !image
)
2961 return InvalidParameter
;
2965 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
2970 stat
= GdipLoadImageFromStream(stream
, image
);
2972 IStream_Release(stream
);
2977 /* FIXME: no icm handling */
2978 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
2980 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
2982 return GdipLoadImageFromFile(filename
, image
);
2985 static void add_property(GpBitmap
*bitmap
, PropertyItem
*item
)
2987 UINT prop_size
, prop_count
;
2988 PropertyItem
*prop_item
;
2990 if (bitmap
->prop_item
== NULL
)
2992 prop_size
= prop_count
= 0;
2993 prop_item
= heap_alloc_zero(item
->length
+ sizeof(PropertyItem
));
2994 if (!prop_item
) return;
3001 GdipGetPropertySize(&bitmap
->image
, &prop_size
, &prop_count
);
3003 prop_item
= heap_alloc_zero(prop_size
+ item
->length
+ sizeof(PropertyItem
));
3004 if (!prop_item
) return;
3005 memcpy(prop_item
, bitmap
->prop_item
, sizeof(PropertyItem
) * bitmap
->prop_count
);
3006 prop_size
-= sizeof(PropertyItem
) * bitmap
->prop_count
;
3007 memcpy(prop_item
+ prop_count
+ 1, bitmap
->prop_item
+ prop_count
, prop_size
);
3009 item_value
= (char *)(prop_item
+ prop_count
+ 1);
3011 for (i
= 0; i
< prop_count
; i
++)
3013 prop_item
[i
].value
= item_value
;
3014 item_value
+= prop_item
[i
].length
;
3018 prop_item
[prop_count
].id
= item
->id
;
3019 prop_item
[prop_count
].type
= item
->type
;
3020 prop_item
[prop_count
].length
= item
->length
;
3021 prop_item
[prop_count
].value
= (char *)(prop_item
+ prop_count
+ 1) + prop_size
;
3022 memcpy(prop_item
[prop_count
].value
, item
->value
, item
->length
);
3024 heap_free(bitmap
->prop_item
);
3025 bitmap
->prop_item
= prop_item
;
3026 bitmap
->prop_count
++;
3029 static BOOL
get_bool_property(IWICMetadataReader
*reader
, const GUID
*guid
, const WCHAR
*prop_name
)
3033 PROPVARIANT id
, value
;
3036 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3037 if (FAILED(hr
) || !IsEqualGUID(&format
, guid
)) return FALSE
;
3039 PropVariantInit(&id
);
3040 PropVariantInit(&value
);
3043 id
.pwszVal
= CoTaskMemAlloc((lstrlenW(prop_name
) + 1) * sizeof(WCHAR
));
3044 if (!id
.pwszVal
) return FALSE
;
3045 lstrcpyW(id
.pwszVal
, prop_name
);
3046 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
3047 if (hr
== S_OK
&& value
.vt
== VT_BOOL
)
3048 ret
= value
.boolVal
;
3050 PropVariantClear(&id
);
3051 PropVariantClear(&value
);
3056 static PropertyItem
*get_property(IWICMetadataReader
*reader
, const GUID
*guid
, const WCHAR
*prop_name
)
3060 PROPVARIANT id
, value
;
3061 PropertyItem
*item
= NULL
;
3063 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3064 if (FAILED(hr
) || !IsEqualGUID(&format
, guid
)) return NULL
;
3066 PropVariantInit(&id
);
3067 PropVariantInit(&value
);
3070 id
.pwszVal
= CoTaskMemAlloc((lstrlenW(prop_name
) + 1) * sizeof(WCHAR
));
3071 if (!id
.pwszVal
) return NULL
;
3072 lstrcpyW(id
.pwszVal
, prop_name
);
3073 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
3076 UINT item_size
= propvariant_size(&value
);
3079 item_size
+= sizeof(*item
);
3080 item
= heap_alloc_zero(item_size
);
3081 if (propvariant_to_item(&value
, item
, item_size
, 0) != Ok
)
3089 PropVariantClear(&id
);
3090 PropVariantClear(&value
);
3095 static PropertyItem
*get_gif_comment(IWICMetadataReader
*reader
)
3097 PropertyItem
*comment
;
3099 comment
= get_property(reader
, &GUID_MetadataFormatGifComment
, L
"TextEntry");
3101 comment
->id
= PropertyTagExifUserComment
;
3106 static PropertyItem
*get_gif_loopcount(IWICMetadataReader
*reader
)
3108 PropertyItem
*appext
= NULL
, *appdata
= NULL
, *loop
= NULL
;
3110 appext
= get_property(reader
, &GUID_MetadataFormatAPE
, L
"Application");
3113 if (appext
->type
== PropertyTagTypeByte
&& appext
->length
== 11 &&
3114 (!memcmp(appext
->value
, "NETSCAPE2.0", 11) || !memcmp(appext
->value
, "ANIMEXTS1.0", 11)))
3116 appdata
= get_property(reader
, &GUID_MetadataFormatAPE
, L
"Data");
3119 if (appdata
->type
== PropertyTagTypeByte
&& appdata
->length
== 4)
3121 BYTE
*data
= appdata
->value
;
3122 if (data
[0] == 3 && data
[1] == 1)
3124 loop
= heap_alloc_zero(sizeof(*loop
) + sizeof(SHORT
));
3127 loop
->type
= PropertyTagTypeShort
;
3128 loop
->id
= PropertyTagLoopCount
;
3129 loop
->length
= sizeof(SHORT
);
3130 loop
->value
= loop
+ 1;
3131 *(SHORT
*)loop
->value
= data
[2] | (data
[3] << 8);
3145 static PropertyItem
*get_gif_background(IWICMetadataReader
*reader
)
3147 PropertyItem
*background
;
3149 background
= get_property(reader
, &GUID_MetadataFormatLSD
, L
"BackgroundColorIndex");
3151 background
->id
= PropertyTagIndexBackground
;
3156 static PropertyItem
*get_gif_palette(IWICBitmapDecoder
*decoder
, IWICMetadataReader
*reader
)
3159 IWICImagingFactory
*factory
;
3160 IWICPalette
*palette
;
3162 WICColor colors
[256];
3164 if (!get_bool_property(reader
, &GUID_MetadataFormatLSD
, L
"GlobalColorTableFlag"))
3167 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
3168 if (hr
!= S_OK
) return NULL
;
3170 hr
= IWICImagingFactory_CreatePalette(factory
, &palette
);
3173 hr
= IWICBitmapDecoder_CopyPalette(decoder
, palette
);
3175 IWICPalette_GetColors(palette
, 256, colors
, &count
);
3177 IWICPalette_Release(palette
);
3180 IWICImagingFactory_Release(factory
);
3188 pal
= heap_alloc_zero(sizeof(*pal
) + count
* 3);
3189 if (!pal
) return NULL
;
3190 pal
->type
= PropertyTagTypeByte
;
3191 pal
->id
= PropertyTagGlobalPalette
;
3192 pal
->value
= pal
+ 1;
3193 pal
->length
= count
* 3;
3197 for (i
= 0; i
< count
; i
++)
3199 rgb
[i
*3] = (colors
[i
] >> 16) & 0xff;
3200 rgb
[i
*3 + 1] = (colors
[i
] >> 8) & 0xff;
3201 rgb
[i
*3 + 2] = colors
[i
] & 0xff;
3210 static PropertyItem
*get_gif_transparent_idx(IWICMetadataReader
*reader
)
3212 PropertyItem
*index
= NULL
;
3214 if (get_bool_property(reader
, &GUID_MetadataFormatGCE
, L
"TransparencyFlag"))
3216 index
= get_property(reader
, &GUID_MetadataFormatGCE
, L
"TransparentColorIndex");
3218 index
->id
= PropertyTagIndexTransparent
;
3223 static LONG
get_gif_frame_property(IWICBitmapFrameDecode
*frame
, const GUID
*format
, const WCHAR
*property
)
3226 IWICMetadataBlockReader
*block_reader
;
3227 IWICMetadataReader
*reader
;
3228 UINT block_count
, i
;
3232 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3235 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3238 for (i
= 0; i
< block_count
; i
++)
3240 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3243 prop
= get_property(reader
, format
, property
);
3246 if (prop
->type
== PropertyTagTypeByte
&& prop
->length
== 1)
3247 value
= *(BYTE
*)prop
->value
;
3248 else if (prop
->type
== PropertyTagTypeShort
&& prop
->length
== 2)
3249 value
= *(SHORT
*)prop
->value
;
3253 IWICMetadataReader_Release(reader
);
3257 IWICMetadataBlockReader_Release(block_reader
);
3263 static void gif_metadata_reader(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT active_frame
)
3266 IWICBitmapFrameDecode
*frame
;
3267 IWICMetadataBlockReader
*block_reader
;
3268 IWICMetadataReader
*reader
;
3269 UINT frame_count
, block_count
, i
;
3270 PropertyItem
*delay
= NULL
, *comment
= NULL
, *background
= NULL
;
3271 PropertyItem
*transparent_idx
= NULL
, *loop
= NULL
, *palette
= NULL
;
3273 IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
3274 if (frame_count
> 1)
3276 delay
= heap_alloc_zero(sizeof(*delay
) + frame_count
* sizeof(LONG
));
3281 delay
->type
= PropertyTagTypeLong
;
3282 delay
->id
= PropertyTagFrameDelay
;
3283 delay
->length
= frame_count
* sizeof(LONG
);
3284 delay
->value
= delay
+ 1;
3286 value
= delay
->value
;
3288 for (i
= 0; i
< frame_count
; i
++)
3290 hr
= IWICBitmapDecoder_GetFrame(decoder
, i
, &frame
);
3293 value
[i
] = get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, L
"Delay");
3294 IWICBitmapFrameDecode_Release(frame
);
3301 hr
= IWICBitmapDecoder_QueryInterface(decoder
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3304 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3307 for (i
= 0; i
< block_count
; i
++)
3309 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3313 comment
= get_gif_comment(reader
);
3315 if (frame_count
> 1 && !loop
)
3316 loop
= get_gif_loopcount(reader
);
3319 background
= get_gif_background(reader
);
3322 palette
= get_gif_palette(decoder
, reader
);
3324 IWICMetadataReader_Release(reader
);
3328 IWICMetadataBlockReader_Release(block_reader
);
3331 if (frame_count
> 1 && !loop
)
3333 loop
= heap_alloc_zero(sizeof(*loop
) + sizeof(SHORT
));
3336 loop
->type
= PropertyTagTypeShort
;
3337 loop
->id
= PropertyTagLoopCount
;
3338 loop
->length
= sizeof(SHORT
);
3339 loop
->value
= loop
+ 1;
3340 *(SHORT
*)loop
->value
= 1;
3344 if (delay
) add_property(bitmap
, delay
);
3345 if (comment
) add_property(bitmap
, comment
);
3346 if (loop
) add_property(bitmap
, loop
);
3347 if (palette
) add_property(bitmap
, palette
);
3348 if (background
) add_property(bitmap
, background
);
3354 heap_free(background
);
3356 /* Win7 gdiplus always returns transparent color index from frame 0 */
3357 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
3358 if (hr
!= S_OK
) return;
3360 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3363 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3366 for (i
= 0; i
< block_count
; i
++)
3368 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3371 if (!transparent_idx
)
3372 transparent_idx
= get_gif_transparent_idx(reader
);
3374 IWICMetadataReader_Release(reader
);
3378 IWICMetadataBlockReader_Release(block_reader
);
3381 if (transparent_idx
) add_property(bitmap
, transparent_idx
);
3382 heap_free(transparent_idx
);
3384 IWICBitmapFrameDecode_Release(frame
);
3387 static PropertyItem
* create_prop(PROPID propid
, PROPVARIANT
* value
)
3389 PropertyItem
*item
= NULL
;
3390 UINT item_size
= propvariant_size(value
);
3394 item_size
+= sizeof(*item
);
3395 item
= heap_alloc_zero(item_size
);
3396 if (propvariant_to_item(value
, item
, item_size
, propid
) != Ok
)
3406 static ULONG
get_ulong_by_index(IWICMetadataReader
* reader
, ULONG index
)
3412 hr
= IWICMetadataReader_GetValueByIndex(reader
, index
, NULL
, NULL
, &value
);
3418 result
= value
.ulVal
;
3421 ERR("unhandled case %u\n", value
.vt
);
3424 PropVariantClear(&value
);
3429 static void png_metadata_reader(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT active_frame
)
3432 IWICBitmapFrameDecode
*frame
;
3433 IWICMetadataBlockReader
*block_reader
;
3434 IWICMetadataReader
*reader
;
3435 UINT block_count
, i
, j
;
3436 struct keyword_info
{
3441 { "Title", PropertyTagImageTitle
},
3442 { "Author", PropertyTagArtist
},
3443 { "Description", PropertyTagImageDescription
},
3444 { "Copyright", PropertyTagCopyright
},
3445 { "Software", PropertyTagSoftwareUsed
},
3446 { "Source", PropertyTagEquipModel
},
3447 { "Comment", PropertyTagExifUserComment
},
3449 BOOL seen_gamma
=FALSE
, seen_whitepoint
=FALSE
, seen_chrm
=FALSE
;
3451 hr
= IWICBitmapDecoder_GetFrame(decoder
, active_frame
, &frame
);
3452 if (hr
!= S_OK
) return;
3454 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3457 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3460 for (i
= 0; i
< block_count
; i
++)
3462 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3467 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3468 if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunktEXt
, &format
))
3470 PROPVARIANT name
, value
;
3473 hr
= IWICMetadataReader_GetValueByIndex(reader
, 0, NULL
, &name
, &value
);
3477 if (name
.vt
== VT_LPSTR
)
3479 for (j
= 0; j
< ARRAY_SIZE(keywords
); j
++)
3480 if (!strcmp(keywords
[j
].name
, name
.pszVal
))
3482 if (j
< ARRAY_SIZE(keywords
) && !keywords
[j
].seen
)
3484 keywords
[j
].seen
= TRUE
;
3485 item
= create_prop(keywords
[j
].propid
, &value
);
3487 add_property(bitmap
, item
);
3492 PropVariantClear(&name
);
3493 PropVariantClear(&value
);
3496 else if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA
, &format
))
3502 item
= heap_alloc_zero(sizeof(PropertyItem
) + sizeof(ULONG
) * 2);
3506 item
->length
= sizeof(ULONG
) * 2;
3507 item
->type
= PropertyTagTypeRational
;
3508 item
->id
= PropertyTagGamma
;
3509 rational
= item
->value
= item
+ 1;
3510 rational
[0] = 100000;
3511 rational
[1] = get_ulong_by_index(reader
, 0);
3512 add_property(bitmap
, item
);
3518 else if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunkcHRM
, &format
))
3522 if (!seen_whitepoint
)
3524 item
= GdipAlloc(sizeof(PropertyItem
) + sizeof(ULONG
) * 4);
3528 item
->length
= sizeof(ULONG
) * 4;
3529 item
->type
= PropertyTagTypeRational
;
3530 item
->id
= PropertyTagWhitePoint
;
3531 rational
= item
->value
= item
+ 1;
3532 rational
[0] = get_ulong_by_index(reader
, 0);
3533 rational
[1] = 100000;
3534 rational
[2] = get_ulong_by_index(reader
, 1);
3535 rational
[3] = 100000;
3536 add_property(bitmap
, item
);
3537 seen_whitepoint
= TRUE
;
3543 item
= GdipAlloc(sizeof(PropertyItem
) + sizeof(ULONG
) * 12);
3547 item
->length
= sizeof(ULONG
) * 12;
3548 item
->type
= PropertyTagTypeRational
;
3549 item
->id
= PropertyTagPrimaryChromaticities
;
3550 rational
= item
->value
= item
+ 1;
3551 rational
[0] = get_ulong_by_index(reader
, 2);
3552 rational
[1] = 100000;
3553 rational
[2] = get_ulong_by_index(reader
, 3);
3554 rational
[3] = 100000;
3555 rational
[4] = get_ulong_by_index(reader
, 4);
3556 rational
[5] = 100000;
3557 rational
[6] = get_ulong_by_index(reader
, 5);
3558 rational
[7] = 100000;
3559 rational
[8] = get_ulong_by_index(reader
, 6);
3560 rational
[9] = 100000;
3561 rational
[10] = get_ulong_by_index(reader
, 7);
3562 rational
[11] = 100000;
3563 add_property(bitmap
, item
);
3570 IWICMetadataReader_Release(reader
);
3574 IWICMetadataBlockReader_Release(block_reader
);
3577 IWICBitmapFrameDecode_Release(frame
);
3580 static GpStatus
initialize_decoder_wic(IStream
*stream
, REFGUID container
, IWICBitmapDecoder
**decoder
)
3582 IWICImagingFactory
*factory
;
3585 TRACE("%p,%s\n", stream
, wine_dbgstr_guid(container
));
3587 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
3588 if (FAILED(hr
)) return hresult_to_status(hr
);
3589 hr
= IWICImagingFactory_CreateDecoder(factory
, container
, NULL
, decoder
);
3590 IWICImagingFactory_Release(factory
);
3591 if (FAILED(hr
)) return hresult_to_status(hr
);
3593 hr
= IWICBitmapDecoder_Initialize(*decoder
, stream
, WICDecodeMetadataCacheOnLoad
);
3596 IWICBitmapDecoder_Release(*decoder
);
3597 return hresult_to_status(hr
);
3602 typedef void (*metadata_reader_func
)(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT frame
);
3604 static GpStatus
decode_frame_wic(IWICBitmapDecoder
*decoder
, BOOL force_conversion
,
3605 UINT active_frame
, metadata_reader_func metadata_reader
, GpImage
**image
)
3610 IWICBitmapFrameDecode
*frame
;
3611 IWICBitmapSource
*source
=NULL
;
3612 IWICMetadataBlockReader
*block_reader
;
3613 WICPixelFormatGUID wic_format
;
3614 PixelFormat gdip_format
=0;
3615 ColorPalette
*palette
= NULL
;
3616 WICBitmapPaletteType palette_type
= WICBitmapPaletteTypeFixedHalftone256
;
3618 UINT width
, height
, frame_count
;
3619 BitmapData lockeddata
;
3622 TRACE("%p,%u,%p\n", decoder
, active_frame
, image
);
3624 IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
3625 hr
= IWICBitmapDecoder_GetFrame(decoder
, active_frame
, &frame
);
3626 if (SUCCEEDED(hr
)) /* got frame */
3628 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &wic_format
);
3632 if (!force_conversion
)
3634 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
3636 if (IsEqualGUID(&wic_format
, pixel_formats
[i
].wic_format
))
3638 source
= (IWICBitmapSource
*)frame
;
3639 IWICBitmapSource_AddRef(source
);
3640 gdip_format
= pixel_formats
[i
].gdip_format
;
3641 palette_type
= pixel_formats
[i
].palette_type
;
3648 /* unknown format; fall back on 32bppARGB */
3649 hr
= WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA
, (IWICBitmapSource
*)frame
, &source
);
3650 gdip_format
= PixelFormat32bppARGB
;
3652 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format
), gdip_format
);
3655 if (SUCCEEDED(hr
)) /* got source */
3657 hr
= IWICBitmapSource_GetSize(source
, &width
, &height
);
3660 status
= GdipCreateBitmapFromScan0(width
, height
, 0, gdip_format
,
3663 if (SUCCEEDED(hr
) && status
== Ok
) /* created bitmap */
3665 status
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeWrite
,
3666 gdip_format
, &lockeddata
);
3667 if (status
== Ok
) /* locked bitmap */
3672 for (i
=0; i
<height
; i
++)
3675 hr
= IWICBitmapSource_CopyPixels(source
, &wrc
, abs(lockeddata
.Stride
),
3676 abs(lockeddata
.Stride
), (BYTE
*)lockeddata
.Scan0
+lockeddata
.Stride
*i
);
3677 if (FAILED(hr
)) break;
3680 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
3683 if (SUCCEEDED(hr
) && status
== Ok
)
3684 *image
= &bitmap
->image
;
3688 GdipDisposeImage(&bitmap
->image
);
3691 if (SUCCEEDED(hr
) && status
== Ok
)
3694 hr
= IWICBitmapSource_GetResolution(source
, &dpix
, &dpiy
);
3697 bitmap
->image
.xres
= dpix
;
3698 bitmap
->image
.yres
= dpiy
;
3704 IWICBitmapSource_Release(source
);
3707 if (SUCCEEDED(hr
) && status
== Ok
) {
3708 bitmap
->metadata_reader
= NULL
;
3710 if (metadata_reader
)
3711 metadata_reader(bitmap
, decoder
, active_frame
);
3712 else if (IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
) == S_OK
)
3714 UINT block_count
= 0;
3715 if (IWICMetadataBlockReader_GetCount(block_reader
, &block_count
) == S_OK
&& block_count
)
3716 IWICMetadataBlockReader_GetReaderByIndex(block_reader
, 0, &bitmap
->metadata_reader
);
3717 IWICMetadataBlockReader_Release(block_reader
);
3720 palette
= get_palette(frame
, palette_type
);
3721 IWICBitmapFrameDecode_Release(frame
);
3725 if (FAILED(hr
) && status
== Ok
) status
= hresult_to_status(hr
);
3729 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3730 bitmap
->image
.flags
|= ImageFlagsReadOnly
|ImageFlagsHasRealPixelSize
|ImageFlagsHasRealDPI
;
3731 if (IsEqualGUID(&wic_format
, &GUID_WICPixelFormat2bppGray
) ||
3732 IsEqualGUID(&wic_format
, &GUID_WICPixelFormat4bppGray
) ||
3733 IsEqualGUID(&wic_format
, &GUID_WICPixelFormat8bppGray
) ||
3734 IsEqualGUID(&wic_format
, &GUID_WICPixelFormat16bppGray
))
3735 bitmap
->image
.flags
|= ImageFlagsColorSpaceGRAY
;
3737 bitmap
->image
.flags
|= ImageFlagsColorSpaceRGB
;
3738 bitmap
->image
.frame_count
= frame_count
;
3739 bitmap
->image
.current_frame
= active_frame
;
3740 bitmap
->image
.decoder
= decoder
;
3741 IWICBitmapDecoder_AddRef(decoder
);
3744 heap_free(bitmap
->image
.palette
);
3745 bitmap
->image
.palette
= palette
;
3749 if (IsEqualGUID(&wic_format
, &GUID_WICPixelFormatBlackWhite
))
3750 bitmap
->image
.palette
->Flags
= 0;
3752 TRACE("=> %p\n", *image
);
3758 static GpStatus
decode_image_wic(IStream
*stream
, REFGUID container
,
3759 metadata_reader_func metadata_reader
, GpImage
**image
)
3761 IWICBitmapDecoder
*decoder
;
3764 status
= initialize_decoder_wic(stream
, container
, &decoder
);
3768 status
= decode_frame_wic(decoder
, FALSE
, 0, metadata_reader
, image
);
3769 IWICBitmapDecoder_Release(decoder
);
3773 static GpStatus
select_frame_wic(GpImage
*image
, UINT active_frame
)
3778 status
= decode_frame_wic(image
->decoder
, FALSE
, active_frame
, NULL
, &new_image
);
3782 new_image
->busy
= image
->busy
;
3783 memcpy(&new_image
->format
, &image
->format
, sizeof(GUID
));
3784 new_image
->encoder
= image
->encoder
;
3785 image
->encoder
= NULL
;
3786 free_image_data(image
);
3787 if (image
->type
== ImageTypeBitmap
)
3788 *(GpBitmap
*)image
= *(GpBitmap
*)new_image
;
3789 else if (image
->type
== ImageTypeMetafile
)
3790 *(GpMetafile
*)image
= *(GpMetafile
*)new_image
;
3791 new_image
->type
= ~0;
3792 heap_free(new_image
);
3796 static HRESULT
get_gif_frame_rect(IWICBitmapFrameDecode
*frame
,
3797 UINT
*left
, UINT
*top
, UINT
*width
, UINT
*height
)
3799 *left
= get_gif_frame_property(frame
, &GUID_MetadataFormatIMD
, L
"Left");
3800 *top
= get_gif_frame_property(frame
, &GUID_MetadataFormatIMD
, L
"Top");
3802 return IWICBitmapFrameDecode_GetSize(frame
, width
, height
);
3805 static HRESULT
blit_gif_frame(GpBitmap
*bitmap
, IWICBitmapFrameDecode
*frame
, BOOL first_frame
)
3807 UINT i
, j
, left
, top
, width
, height
;
3808 IWICBitmapSource
*source
;
3812 hr
= get_gif_frame_rect(frame
, &left
, &top
, &width
, &height
);
3816 hr
= WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA
, (IWICBitmapSource
*)frame
, &source
);
3820 new_bits
= heap_alloc_zero(width
*height
*4);
3822 return E_OUTOFMEMORY
;
3824 hr
= IWICBitmapSource_CopyPixels(source
, NULL
, width
*4, width
*height
*4, new_bits
);
3825 IWICBitmapSource_Release(source
);
3827 heap_free(new_bits
);
3831 for(i
=0; i
<height
&& i
+top
<bitmap
->height
; i
++) {
3832 for(j
=0; j
<width
&& j
+left
<bitmap
->width
; j
++) {
3833 DWORD
*src
= (DWORD
*)(new_bits
+i
*width
*4+j
*4);
3834 DWORD
*dst
= (DWORD
*)(bitmap
->bits
+(i
+top
)*bitmap
->stride
+(j
+left
)*4);
3836 if(first_frame
|| *src
>>24 != 0)
3840 heap_free(new_bits
);
3844 static DWORD
get_gif_background_color(GpBitmap
*bitmap
)
3846 BYTE bgcolor_idx
= 0;
3849 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3850 if(bitmap
->prop_item
[i
].id
== PropertyTagIndexBackground
) {
3851 bgcolor_idx
= *(BYTE
*)bitmap
->prop_item
[i
].value
;
3856 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3857 if(bitmap
->prop_item
[i
].id
== PropertyTagIndexTransparent
) {
3858 BYTE transparent_idx
;
3859 transparent_idx
= *(BYTE
*)bitmap
->prop_item
[i
].value
;
3861 if(transparent_idx
== bgcolor_idx
)
3866 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3867 if(bitmap
->prop_item
[i
].id
== PropertyTagGlobalPalette
) {
3868 if(bitmap
->prop_item
[i
].length
/3 > bgcolor_idx
) {
3869 BYTE
*color
= ((BYTE
*)bitmap
->prop_item
[i
].value
)+bgcolor_idx
*3;
3870 return color
[2] + (color
[1]<<8) + (color
[0]<<16) + (0xffu
<<24);
3876 FIXME("can't get gif background color\n");
3880 static GpStatus
select_frame_gif(GpImage
* image
, UINT active_frame
)
3882 GpBitmap
*bitmap
= (GpBitmap
*)image
;
3883 IWICBitmapFrameDecode
*frame
;
3884 int cur_frame
=0, disposal
;
3885 BOOL bgcolor_set
= FALSE
;
3889 if(active_frame
> image
->current_frame
) {
3890 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, image
->current_frame
, &frame
);
3892 return hresult_to_status(hr
);
3893 disposal
= get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, L
"Disposal");
3894 IWICBitmapFrameDecode_Release(frame
);
3896 if(disposal
== GIF_DISPOSE_RESTORE_TO_BKGND
)
3897 cur_frame
= image
->current_frame
;
3898 else if(disposal
!= GIF_DISPOSE_RESTORE_TO_PREV
)
3899 cur_frame
= image
->current_frame
+1;
3902 while(cur_frame
!= active_frame
) {
3903 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, cur_frame
, &frame
);
3905 return hresult_to_status(hr
);
3906 disposal
= get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, L
"Disposal");
3908 if(disposal
==GIF_DISPOSE_UNSPECIFIED
|| disposal
==GIF_DISPOSE_DO_NOT_DISPOSE
) {
3909 hr
= blit_gif_frame(bitmap
, frame
, cur_frame
==0);
3911 return hresult_to_status(hr
);
3912 }else if(disposal
== GIF_DISPOSE_RESTORE_TO_BKGND
) {
3913 UINT left
, top
, width
, height
, i
, j
;
3916 bgcolor
= get_gif_background_color(bitmap
);
3920 hr
= get_gif_frame_rect(frame
, &left
, &top
, &width
, &height
);
3922 return hresult_to_status(hr
);
3923 for(i
=top
; i
<top
+height
&& i
<bitmap
->height
; i
++) {
3924 DWORD
*bits
= (DWORD
*)(bitmap
->bits
+i
*bitmap
->stride
);
3925 for(j
=left
; j
<left
+width
&& j
<bitmap
->width
; j
++)
3930 IWICBitmapFrameDecode_Release(frame
);
3934 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, active_frame
, &frame
);
3936 return hresult_to_status(hr
);
3938 hr
= blit_gif_frame(bitmap
, frame
, cur_frame
==0);
3939 IWICBitmapFrameDecode_Release(frame
);
3941 return hresult_to_status(hr
);
3943 image
->current_frame
= active_frame
;
3947 static GpStatus
decode_image_icon(IStream
* stream
, GpImage
**image
)
3949 return decode_image_wic(stream
, &GUID_ContainerFormatIco
, NULL
, image
);
3952 static GpStatus
decode_image_bmp(IStream
* stream
, GpImage
**image
)
3957 status
= decode_image_wic(stream
, &GUID_ContainerFormatBmp
, NULL
, image
);
3959 bitmap
= (GpBitmap
*)*image
;
3961 if (status
== Ok
&& bitmap
->format
== PixelFormat32bppARGB
)
3963 /* WIC supports bmp files with alpha, but gdiplus does not */
3964 bitmap
->format
= PixelFormat32bppRGB
;
3970 static GpStatus
decode_image_jpeg(IStream
* stream
, GpImage
**image
)
3972 return decode_image_wic(stream
, &GUID_ContainerFormatJpeg
, NULL
, image
);
3975 static BOOL
has_png_transparency_chunk(IStream
*pIStream
)
3978 BOOL has_tRNS
= FALSE
;
3985 ULARGE_INTEGER chunk_start
;
3986 ULONG bytesread
, chunk_size
;
3988 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, &chunk_start
);
3989 if (FAILED(hr
)) break;
3991 hr
= IStream_Read(pIStream
, header
, 8, &bytesread
);
3992 if (FAILED(hr
) || bytesread
< 8) break;
3994 chunk_size
= (header
[0] << 24) | (header
[1] << 16) | (header
[2] << 8) | header
[3];
3995 if (!memcmp(&header
[4], "tRNS", 4))
4001 seek
.QuadPart
= chunk_start
.QuadPart
+ chunk_size
+ 12; /* skip data and CRC */
4002 } while (memcmp(&header
[4], "IDAT", 4) && memcmp(&header
[4], "IEND", 4));
4004 TRACE("has_tRNS = %d\n", has_tRNS
);
4008 static GpStatus
decode_image_png(IStream
* stream
, GpImage
**image
)
4010 IWICBitmapDecoder
*decoder
;
4011 IWICBitmapFrameDecode
*frame
;
4015 BOOL force_conversion
= FALSE
;
4017 status
= initialize_decoder_wic(stream
, &GUID_ContainerFormatPng
, &decoder
);
4021 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
4024 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &format
);
4027 if (IsEqualGUID(&format
, &GUID_WICPixelFormat8bppGray
))
4028 force_conversion
= TRUE
;
4029 else if ((IsEqualGUID(&format
, &GUID_WICPixelFormat8bppIndexed
) ||
4030 IsEqualGUID(&format
, &GUID_WICPixelFormat4bppIndexed
) ||
4031 IsEqualGUID(&format
, &GUID_WICPixelFormat2bppIndexed
) ||
4032 IsEqualGUID(&format
, &GUID_WICPixelFormat1bppIndexed
) ||
4033 IsEqualGUID(&format
, &GUID_WICPixelFormat24bppBGR
)) &&
4034 has_png_transparency_chunk(stream
))
4035 force_conversion
= TRUE
;
4037 status
= decode_frame_wic(decoder
, force_conversion
, 0, png_metadata_reader
, image
);
4040 status
= hresult_to_status(hr
);
4042 IWICBitmapFrameDecode_Release(frame
);
4045 status
= hresult_to_status(hr
);
4047 IWICBitmapDecoder_Release(decoder
);
4051 static GpStatus
decode_image_gif(IStream
* stream
, GpImage
**image
)
4053 IWICBitmapDecoder
*decoder
;
4058 status
= initialize_decoder_wic(stream
, &GUID_ContainerFormatGif
, &decoder
);
4062 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
4064 return hresult_to_status(hr
);
4066 status
= decode_frame_wic(decoder
, frame_count
> 1, 0, gif_metadata_reader
, image
);
4067 IWICBitmapDecoder_Release(decoder
);
4071 if(frame_count
> 1) {
4072 heap_free((*image
)->palette
);
4073 (*image
)->palette
= NULL
;
4078 static GpStatus
decode_image_tiff(IStream
* stream
, GpImage
**image
)
4080 return decode_image_wic(stream
, &GUID_ContainerFormatTiff
, NULL
, image
);
4083 static GpStatus
load_wmf(IStream
*stream
, GpMetafile
**metafile
)
4085 WmfPlaceableFileHeader pfh
;
4086 BOOL is_placeable
= FALSE
;
4095 hr
= IStream_Read(stream
, &mh
, sizeof(mh
), &size
);
4096 if (hr
!= S_OK
|| size
!= sizeof(mh
))
4097 return GenericError
;
4099 if (((WmfPlaceableFileHeader
*)&mh
)->Key
== WMF_PLACEABLE_KEY
)
4102 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4103 if (FAILED(hr
)) return hresult_to_status(hr
);
4105 hr
= IStream_Read(stream
, &pfh
, sizeof(pfh
), &size
);
4106 if (hr
!= S_OK
|| size
!= sizeof(pfh
))
4107 return GenericError
;
4109 hr
= IStream_Read(stream
, &mh
, sizeof(mh
), &size
);
4110 if (hr
!= S_OK
|| size
!= sizeof(mh
))
4111 return GenericError
;
4113 is_placeable
= TRUE
;
4116 seek
.QuadPart
= is_placeable
? sizeof(pfh
) : 0;
4117 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4118 if (FAILED(hr
)) return hresult_to_status(hr
);
4120 buf
= heap_alloc(mh
.mtSize
* 2);
4121 if (!buf
) return OutOfMemory
;
4123 hr
= IStream_Read(stream
, buf
, mh
.mtSize
* 2, &size
);
4124 if (hr
!= S_OK
|| size
!= mh
.mtSize
* 2)
4127 return GenericError
;
4130 hmf
= SetMetaFileBitsEx(mh
.mtSize
* 2, buf
);
4133 return GenericError
;
4135 status
= GdipCreateMetafileFromWmf(hmf
, TRUE
, is_placeable
? &pfh
: NULL
, metafile
);
4137 DeleteMetaFile(hmf
);
4141 static GpStatus
decode_image_wmf(IStream
*stream
, GpImage
**image
)
4143 GpMetafile
*metafile
;
4146 TRACE("%p %p\n", stream
, image
);
4148 if (!stream
|| !image
)
4149 return InvalidParameter
;
4151 status
= load_wmf(stream
, &metafile
);
4154 TRACE("Could not load metafile\n");
4158 *image
= (GpImage
*)metafile
;
4159 TRACE("<-- %p\n", *image
);
4164 static GpStatus
load_emf(IStream
*stream
, GpMetafile
**metafile
)
4174 hr
= IStream_Read(stream
, &emh
, sizeof(emh
), &size
);
4175 if (hr
!= S_OK
|| size
!= sizeof(emh
) || emh
.dSignature
!= ENHMETA_SIGNATURE
)
4176 return GenericError
;
4179 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4180 if (FAILED(hr
)) return hresult_to_status(hr
);
4182 buf
= heap_alloc(emh
.nBytes
);
4183 if (!buf
) return OutOfMemory
;
4185 hr
= IStream_Read(stream
, buf
, emh
.nBytes
, &size
);
4186 if (hr
!= S_OK
|| size
!= emh
.nBytes
)
4189 return GenericError
;
4192 hemf
= SetEnhMetaFileBits(emh
.nBytes
, buf
);
4195 return GenericError
;
4197 status
= GdipCreateMetafileFromEmf(hemf
, TRUE
, metafile
);
4199 DeleteEnhMetaFile(hemf
);
4203 static GpStatus
decode_image_emf(IStream
*stream
, GpImage
**image
)
4205 GpMetafile
*metafile
;
4208 TRACE("%p %p\n", stream
, image
);
4210 if (!stream
|| !image
)
4211 return InvalidParameter
;
4213 status
= load_emf(stream
, &metafile
);
4216 TRACE("Could not load metafile\n");
4220 *image
= (GpImage
*)metafile
;
4221 TRACE("<-- %p\n", *image
);
4226 typedef GpStatus (*encode_image_func
)(GpImage
*image
, IStream
* stream
,
4227 GDIPCONST EncoderParameters
* params
);
4229 typedef GpStatus (*decode_image_func
)(IStream
*stream
, GpImage
**image
);
4231 typedef GpStatus (*select_image_func
)(GpImage
*image
, UINT active_frame
);
4233 typedef struct image_codec
{
4234 ImageCodecInfo info
;
4235 encode_image_func encode_func
;
4236 decode_image_func decode_func
;
4237 select_image_func select_func
;
4252 static const struct image_codec codecs
[NUM_CODECS
];
4254 static GpStatus
get_decoder_info(IStream
* stream
, const struct image_codec
**result
)
4257 const BYTE
*pattern
, *mask
;
4264 /* seek to the start of the stream */
4266 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4267 if (FAILED(hr
)) return hresult_to_status(hr
);
4269 /* read the first 8 bytes */
4270 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4271 hr
= IStream_Read(stream
, signature
, 8, &bytesread
);
4272 if (FAILED(hr
)) return hresult_to_status(hr
);
4273 if (hr
== S_FALSE
|| bytesread
== 0) return GenericError
;
4275 for (i
= 0; i
< NUM_CODECS
; i
++) {
4276 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
) &&
4277 bytesread
>= codecs
[i
].info
.SigSize
)
4279 for (sig
=0; sig
<codecs
[i
].info
.SigCount
; sig
++)
4281 pattern
= &codecs
[i
].info
.SigPattern
[codecs
[i
].info
.SigSize
*sig
];
4282 mask
= &codecs
[i
].info
.SigMask
[codecs
[i
].info
.SigSize
*sig
];
4283 for (j
=0; j
<codecs
[i
].info
.SigSize
; j
++)
4284 if ((signature
[j
] & mask
[j
]) != pattern
[j
])
4286 if (j
== codecs
[i
].info
.SigSize
)
4288 *result
= &codecs
[i
];
4295 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread
,
4296 signature
[0],signature
[1],signature
[2],signature
[3],
4297 signature
[4],signature
[5],signature
[6],signature
[7]);
4299 return GenericError
;
4302 static GpStatus
get_decoder_info_from_image(GpImage
*image
, const struct image_codec
**result
)
4306 for (i
= 0; i
< NUM_CODECS
; i
++) {
4307 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
) &&
4308 IsEqualIID(&codecs
[i
].info
.FormatID
, &image
->format
))
4310 *result
= &codecs
[i
];
4315 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image
->format
));
4316 return GenericError
;
4319 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
, GDIPCONST GUID
*dimensionID
,
4323 const struct image_codec
*codec
= NULL
;
4326 TRACE("(%p,%s,%u)\n", image
, debugstr_guid(dimensionID
), frame
);
4328 if (!image
|| !dimensionID
)
4329 return InvalidParameter
;
4330 if(!image_lock(image
, &unlock
))
4333 if (frame
>= image
->frame_count
)
4335 WARN("requested frame %u, but image has only %u\n", frame
, image
->frame_count
);
4336 image_unlock(image
, unlock
);
4337 return InvalidParameter
;
4340 if (image
->type
!= ImageTypeBitmap
&& image
->type
!= ImageTypeMetafile
)
4342 WARN("invalid image type %d\n", image
->type
);
4343 image_unlock(image
, unlock
);
4344 return InvalidParameter
;
4347 if (image
->current_frame
== frame
)
4349 image_unlock(image
, unlock
);
4353 if (!image
->decoder
)
4355 TRACE("image doesn't have an associated decoder\n");
4356 image_unlock(image
, unlock
);
4360 /* choose an appropriate image decoder */
4361 stat
= get_decoder_info_from_image(image
, &codec
);
4364 WARN("can't find decoder info\n");
4365 image_unlock(image
, unlock
);
4369 stat
= codec
->select_func(image
, frame
);
4370 image_unlock(image
, unlock
);
4374 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
*stream
, GpImage
**image
)
4379 const struct image_codec
*codec
=NULL
;
4381 TRACE("%p %p\n", stream
, image
);
4383 if (!stream
|| !image
)
4384 return InvalidParameter
;
4386 /* choose an appropriate image decoder */
4387 stat
= get_decoder_info(stream
, &codec
);
4388 if (stat
!= Ok
) return stat
;
4390 /* seek to the start of the stream */
4392 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4393 if (FAILED(hr
)) return hresult_to_status(hr
);
4395 /* call on the image decoder to do the real work */
4396 stat
= codec
->decode_func(stream
, image
);
4398 /* take note of the original data format */
4401 memcpy(&(*image
)->format
, &codec
->info
.FormatID
, sizeof(GUID
));
4409 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
4411 TRACE("%p %p\n", stream
, image
);
4413 return GdipLoadImageFromStream(stream
, image
);
4416 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
4420 TRACE("(%p,%u)\n", image
, propId
);
4423 return InvalidParameter
;
4426 FIXME("not implemented\n");
4428 return NotImplemented
;
4431 GpStatus WINGDIPAPI
GdipSetPropertyItem(GpImage
*image
, GDIPCONST PropertyItem
* item
)
4435 if (!image
|| !item
) return InvalidParameter
;
4437 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image
, item
, item
->id
, item
->type
, item
->length
, item
->value
);
4440 FIXME("not implemented\n");
4445 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
4446 GDIPCONST CLSID
*clsidEncoder
,
4447 GDIPCONST EncoderParameters
*encoderParams
)
4452 TRACE("%p (%s) %p %p\n", image
, debugstr_w(filename
), clsidEncoder
, encoderParams
);
4454 if (!image
|| !filename
|| !clsidEncoder
)
4455 return InvalidParameter
;
4457 /* this might release an old file stream held by the encoder so we can re-create it below */
4458 terminate_encoder_wic(image
);
4460 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
4462 return GenericError
;
4464 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
4466 IStream_Release(stream
);
4470 /*************************************************************************
4471 * Encoding functions -
4472 * These functions encode an image in different image file formats.
4475 static GpStatus
initialize_encoder_wic(IStream
*stream
, REFGUID container
, GpImage
*image
)
4477 IWICImagingFactory
*factory
;
4480 TRACE("%p,%s\n", stream
, wine_dbgstr_guid(container
));
4482 terminate_encoder_wic(image
); /* terminate previous encoder if it exists */
4484 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
4485 if (FAILED(hr
)) return hresult_to_status(hr
);
4486 hr
= IWICImagingFactory_CreateEncoder(factory
, container
, NULL
, &image
->encoder
);
4487 IWICImagingFactory_Release(factory
);
4490 image
->encoder
= NULL
;
4491 return hresult_to_status(hr
);
4494 hr
= IWICBitmapEncoder_Initialize(image
->encoder
, stream
, WICBitmapEncoderNoCache
);
4497 IWICBitmapEncoder_Release(image
->encoder
);
4498 image
->encoder
= NULL
;
4499 return hresult_to_status(hr
);
4504 GpStatus
terminate_encoder_wic(GpImage
*image
)
4506 if (!image
->encoder
)
4510 HRESULT hr
= IWICBitmapEncoder_Commit(image
->encoder
);
4511 IWICBitmapEncoder_Release(image
->encoder
);
4512 image
->encoder
= NULL
;
4513 return hresult_to_status(hr
);
4517 static GpStatus
encode_frame_wic(IWICBitmapEncoder
*encoder
, GpImage
*image
)
4521 IWICBitmapFrameEncode
*frameencode
;
4522 IPropertyBag2
*encoderoptions
;
4525 PixelFormat gdipformat
=0;
4526 const WICPixelFormatGUID
*desired_wicformat
;
4527 WICPixelFormatGUID wicformat
;
4529 BitmapData lockeddata
;
4532 if (image
->type
!= ImageTypeBitmap
)
4533 return GenericError
;
4535 bitmap
= (GpBitmap
*)image
;
4537 GdipGetImageWidth(image
, &width
);
4538 GdipGetImageHeight(image
, &height
);
4545 hr
= IWICBitmapEncoder_CreateNewFrame(encoder
, &frameencode
, &encoderoptions
);
4547 if (SUCCEEDED(hr
)) /* created frame */
4549 hr
= IWICBitmapFrameEncode_Initialize(frameencode
, encoderoptions
);
4552 hr
= IWICBitmapFrameEncode_SetSize(frameencode
, width
, height
);
4555 hr
= IWICBitmapFrameEncode_SetResolution(frameencode
, image
->xres
, image
->yres
);
4559 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
4561 if (pixel_formats
[i
].gdip_format
== bitmap
->format
)
4563 desired_wicformat
= pixel_formats
[i
].wic_format
;
4564 gdipformat
= bitmap
->format
;
4570 desired_wicformat
= &GUID_WICPixelFormat32bppBGRA
;
4571 gdipformat
= PixelFormat32bppARGB
;
4574 memcpy(&wicformat
, desired_wicformat
, sizeof(GUID
));
4575 hr
= IWICBitmapFrameEncode_SetPixelFormat(frameencode
, &wicformat
);
4578 if (SUCCEEDED(hr
) && !IsEqualGUID(desired_wicformat
, &wicformat
))
4580 /* Encoder doesn't support this bitmap's format. */
4582 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
4584 if (IsEqualGUID(&wicformat
, pixel_formats
[i
].wic_format
))
4586 gdipformat
= pixel_formats
[i
].gdip_format
;
4592 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat
));
4597 if (SUCCEEDED(hr
) && IsIndexedPixelFormat(gdipformat
) && image
->palette
)
4598 hr
= set_palette(frameencode
, image
->palette
);
4602 stat
= GdipBitmapLockBits(bitmap
, &rc
, ImageLockModeRead
, gdipformat
,
4607 UINT row_size
= (lockeddata
.Width
* PIXELFORMATBPP(gdipformat
) + 7)/8;
4610 /* write one row at a time in case stride is negative */
4611 row
= lockeddata
.Scan0
;
4612 for (i
=0; i
<lockeddata
.Height
; i
++)
4614 hr
= IWICBitmapFrameEncode_WritePixels(frameencode
, 1, row_size
, row_size
, row
);
4615 if (FAILED(hr
)) break;
4616 row
+= lockeddata
.Stride
;
4619 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
4626 hr
= IWICBitmapFrameEncode_Commit(frameencode
);
4628 IWICBitmapFrameEncode_Release(frameencode
);
4629 IPropertyBag2_Release(encoderoptions
);
4632 return hresult_to_status(hr
);
4635 static BOOL
has_encoder_param_long(GDIPCONST EncoderParameters
*params
, GUID param_guid
, ULONG val
)
4637 int param_idx
, value_idx
;
4642 for (param_idx
= 0; param_idx
< params
->Count
; param_idx
++)
4644 EncoderParameter param
= params
->Parameter
[param_idx
];
4645 if (param
.Type
== EncoderParameterValueTypeLong
&& IsEqualCLSID(¶m
.Guid
, ¶m_guid
))
4647 ULONG
*value_array
= (ULONG
*) param
.Value
;
4648 for (value_idx
= 0; value_idx
< param
.NumberOfValues
; value_idx
++)
4650 if (value_array
[value_idx
] == val
)
4658 static GpStatus
encode_image_wic(GpImage
*image
, IStream
*stream
,
4659 REFGUID container
, GDIPCONST EncoderParameters
*params
)
4661 GpStatus status
, terminate_status
;
4663 if (image
->type
!= ImageTypeBitmap
)
4664 return GenericError
;
4666 status
= initialize_encoder_wic(stream
, container
, image
);
4669 status
= encode_frame_wic(image
->encoder
, image
);
4671 if (!has_encoder_param_long(params
, EncoderSaveFlag
, EncoderValueMultiFrame
))
4673 /* always try to terminate, but if something already failed earlier, keep the old status. */
4674 terminate_status
= terminate_encoder_wic(image
);
4676 status
= terminate_status
;
4682 static GpStatus
encode_image_BMP(GpImage
*image
, IStream
* stream
,
4683 GDIPCONST EncoderParameters
* params
)
4685 return encode_image_wic(image
, stream
, &GUID_ContainerFormatBmp
, params
);
4688 static GpStatus
encode_image_tiff(GpImage
*image
, IStream
* stream
,
4689 GDIPCONST EncoderParameters
* params
)
4691 return encode_image_wic(image
, stream
, &GUID_ContainerFormatTiff
, params
);
4694 GpStatus
encode_image_png(GpImage
*image
, IStream
* stream
,
4695 GDIPCONST EncoderParameters
* params
)
4697 return encode_image_wic(image
, stream
, &GUID_ContainerFormatPng
, params
);
4700 static GpStatus
encode_image_jpeg(GpImage
*image
, IStream
* stream
,
4701 GDIPCONST EncoderParameters
* params
)
4703 return encode_image_wic(image
, stream
, &GUID_ContainerFormatJpeg
, params
);
4706 static GpStatus
encode_image_gif(GpImage
*image
, IStream
* stream
,
4707 GDIPCONST EncoderParameters
* params
)
4709 return encode_image_wic(image
, stream
, &GUID_ContainerFormatGif
, params
);
4712 /*****************************************************************************
4713 * GdipSaveImageToStream [GDIPLUS.@]
4715 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
4716 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
4719 encode_image_func encode_image
;
4722 TRACE("%p, %p, %s, %p\n", image
, stream
, wine_dbgstr_guid(clsid
), params
);
4724 if(!image
|| !stream
)
4725 return InvalidParameter
;
4727 /* select correct encoder */
4728 encode_image
= NULL
;
4729 for (i
= 0; i
< NUM_CODECS
; i
++) {
4730 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
) &&
4731 IsEqualCLSID(clsid
, &codecs
[i
].info
.Clsid
))
4732 encode_image
= codecs
[i
].encode_func
;
4734 if (encode_image
== NULL
)
4735 return UnknownImageFormat
;
4737 stat
= encode_image(image
, stream
, params
);
4742 /*****************************************************************************
4743 * GdipSaveAdd [GDIPLUS.@]
4745 * Like GdipSaveAddImage(), but encode the currently active frame of the given image into the file
4746 * or stream that is currently being encoded.
4748 GpStatus WINGDIPAPI
GdipSaveAdd(GpImage
*image
, GDIPCONST EncoderParameters
*params
)
4750 return GdipSaveAddImage(image
, image
, params
);
4753 /*****************************************************************************
4754 * GdipSaveAddImage [GDIPLUS.@]
4756 * Encode the currently active frame of additional_image into the file or stream that is currently
4757 * being encoded by the image given in the image parameter. The first frame of a multi-frame image
4758 * must be encoded using the normal GdipSaveImageToStream() or GdipSaveImageToFile() functions,
4759 * but with the "MultiFrame" encoding parameter set. The multi-frame encoding process must be
4760 * finished after adding the last frame by calling GdipSaveAdd() with the "Flush" encoding parameter
4763 GpStatus WINGDIPAPI
GdipSaveAddImage(GpImage
*image
, GpImage
*additional_image
,
4764 GDIPCONST EncoderParameters
*params
)
4766 TRACE("%p, %p, %p\n", image
, additional_image
, params
);
4768 if (!image
|| !additional_image
|| !params
)
4769 return InvalidParameter
;
4771 if (!image
->encoder
)
4774 if (has_encoder_param_long(params
, EncoderSaveFlag
, EncoderValueFlush
))
4775 return terminate_encoder_wic(image
);
4776 else if (has_encoder_param_long(params
, EncoderSaveFlag
, EncoderValueFrameDimensionPage
))
4777 return encode_frame_wic(image
->encoder
, additional_image
);
4779 return InvalidParameter
;
4782 /*****************************************************************************
4783 * GdipGetImagePalette [GDIPLUS.@]
4785 GpStatus WINGDIPAPI
GdipGetImagePalette(GpImage
*image
, ColorPalette
*palette
, INT size
)
4789 TRACE("(%p,%p,%i)\n", image
, palette
, size
);
4791 if (!image
|| !palette
)
4792 return InvalidParameter
;
4794 count
= image
->palette
? image
->palette
->Count
: 0;
4796 if (size
< (sizeof(UINT
)*2+sizeof(ARGB
)*count
))
4798 TRACE("<-- InsufficientBuffer\n");
4799 return InsufficientBuffer
;
4804 palette
->Flags
= image
->palette
->Flags
;
4805 palette
->Count
= image
->palette
->Count
;
4806 memcpy(palette
->Entries
, image
->palette
->Entries
, sizeof(ARGB
)*image
->palette
->Count
);
4816 /*****************************************************************************
4817 * GdipSetImagePalette [GDIPLUS.@]
4819 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
4820 GDIPCONST ColorPalette
*palette
)
4822 ColorPalette
*new_palette
;
4824 TRACE("(%p,%p)\n", image
, palette
);
4826 if(!image
|| !palette
|| palette
->Count
> 256)
4827 return InvalidParameter
;
4829 new_palette
= heap_alloc_zero(2 * sizeof(UINT
) + palette
->Count
* sizeof(ARGB
));
4830 if (!new_palette
) return OutOfMemory
;
4832 heap_free(image
->palette
);
4833 image
->palette
= new_palette
;
4834 image
->palette
->Flags
= palette
->Flags
;
4835 image
->palette
->Count
= palette
->Count
;
4836 memcpy(image
->palette
->Entries
, palette
->Entries
, sizeof(ARGB
)*palette
->Count
);
4841 /*************************************************************************
4843 * Structures that represent which formats we support for encoding.
4846 /* ImageCodecInfo creation routines taken from libgdiplus */
4847 static const WCHAR bmp_codecname
[] = L
"Built-in BMP";
4848 static const WCHAR bmp_extension
[] = L
"*.BMP;*.DIB;*.RLE";
4849 static const WCHAR bmp_mimetype
[] = L
"image/bmp";
4850 static const WCHAR bmp_format
[] = L
"BMP";
4851 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
4852 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
4854 static const WCHAR jpeg_codecname
[] = L
"Built-in JPEG";
4855 static const WCHAR jpeg_extension
[] = L
"*.JPG;*.JPEG;*.JPE;*.JFIF";
4856 static const WCHAR jpeg_mimetype
[] = L
"image/jpeg";
4857 static const WCHAR jpeg_format
[] = L
"JPEG";
4858 static const BYTE jpeg_sig_pattern
[] = { 0xFF, 0xD8 };
4859 static const BYTE jpeg_sig_mask
[] = { 0xFF, 0xFF };
4861 static const WCHAR gif_codecname
[] = L
"Built-in GIF";
4862 static const WCHAR gif_extension
[] = L
"*.GIF";
4863 static const WCHAR gif_mimetype
[] = L
"image/gif";
4864 static const WCHAR gif_format
[] = L
"GIF";
4865 static const BYTE gif_sig_pattern
[12] = "GIF87aGIF89a";
4866 static const BYTE gif_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4868 static const WCHAR tiff_codecname
[] = L
"Built-in TIFF";
4869 static const WCHAR tiff_extension
[] = L
"*.TIFF;*.TIF";
4870 static const WCHAR tiff_mimetype
[] = L
"image/tiff";
4871 static const WCHAR tiff_format
[] = L
"TIFF";
4872 static const BYTE tiff_sig_pattern
[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4873 static const BYTE tiff_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4875 static const WCHAR emf_codecname
[] = L
"Built-in EMF";
4876 static const WCHAR emf_extension
[] = L
"*.EMF";
4877 static const WCHAR emf_mimetype
[] = L
"image/x-emf";
4878 static const WCHAR emf_format
[] = L
"EMF";
4879 static const BYTE emf_sig_pattern
[] = { 0x01, 0x00, 0x00, 0x00 };
4880 static const BYTE emf_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4882 static const WCHAR wmf_codecname
[] = L
"Built-in WMF";
4883 static const WCHAR wmf_extension
[] = L
"*.WMF";
4884 static const WCHAR wmf_mimetype
[] = L
"image/x-wmf";
4885 static const WCHAR wmf_format
[] = L
"WMF";
4886 static const BYTE wmf_sig_pattern
[] = { 0xd7, 0xcd };
4887 static const BYTE wmf_sig_mask
[] = { 0xFF, 0xFF };
4889 static const WCHAR png_codecname
[] = L
"Built-in PNG";
4890 static const WCHAR png_extension
[] = L
"*.PNG";
4891 static const WCHAR png_mimetype
[] = L
"image/png";
4892 static const WCHAR png_format
[] = L
"PNG";
4893 static const BYTE png_sig_pattern
[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4894 static const BYTE png_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4896 static const WCHAR ico_codecname
[] = L
"Built-in ICO";
4897 static const WCHAR ico_extension
[] = L
"*.ICO";
4898 static const WCHAR ico_mimetype
[] = L
"image/x-icon";
4899 static const WCHAR ico_format
[] = L
"ICO";
4900 static const BYTE ico_sig_pattern
[] = { 0x00, 0x00, 0x01, 0x00 };
4901 static const BYTE ico_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4903 static const struct image_codec codecs
[NUM_CODECS
] = {
4906 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4907 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4908 /* CodecName */ bmp_codecname
,
4910 /* FormatDescription */ bmp_format
,
4911 /* FilenameExtension */ bmp_extension
,
4912 /* MimeType */ bmp_mimetype
,
4913 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4917 /* SigPattern */ bmp_sig_pattern
,
4918 /* SigMask */ bmp_sig_mask
,
4926 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4927 /* FormatID */ { 0xb96b3caeU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4928 /* CodecName */ jpeg_codecname
,
4930 /* FormatDescription */ jpeg_format
,
4931 /* FilenameExtension */ jpeg_extension
,
4932 /* MimeType */ jpeg_mimetype
,
4933 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4937 /* SigPattern */ jpeg_sig_pattern
,
4938 /* SigMask */ jpeg_sig_mask
,
4946 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4947 /* FormatID */ { 0xb96b3cb0U
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4948 /* CodecName */ gif_codecname
,
4950 /* FormatDescription */ gif_format
,
4951 /* FilenameExtension */ gif_extension
,
4952 /* MimeType */ gif_mimetype
,
4953 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsEncoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4957 /* SigPattern */ gif_sig_pattern
,
4958 /* SigMask */ gif_sig_mask
,
4966 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4967 /* FormatID */ { 0xb96b3cb1U
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4968 /* CodecName */ tiff_codecname
,
4970 /* FormatDescription */ tiff_format
,
4971 /* FilenameExtension */ tiff_extension
,
4972 /* MimeType */ tiff_mimetype
,
4973 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsEncoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4977 /* SigPattern */ tiff_sig_pattern
,
4978 /* SigMask */ tiff_sig_mask
,
4986 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4987 /* FormatID */ { 0xb96b3cacU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4988 /* CodecName */ emf_codecname
,
4990 /* FormatDescription */ emf_format
,
4991 /* FilenameExtension */ emf_extension
,
4992 /* MimeType */ emf_mimetype
,
4993 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportVector
| ImageCodecFlagsBuiltin
,
4997 /* SigPattern */ emf_sig_pattern
,
4998 /* SigMask */ emf_sig_mask
,
5006 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5007 /* FormatID */ { 0xb96b3cadU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5008 /* CodecName */ wmf_codecname
,
5010 /* FormatDescription */ wmf_format
,
5011 /* FilenameExtension */ wmf_extension
,
5012 /* MimeType */ wmf_mimetype
,
5013 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportVector
| ImageCodecFlagsBuiltin
,
5017 /* SigPattern */ wmf_sig_pattern
,
5018 /* SigMask */ wmf_sig_mask
,
5026 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5027 /* FormatID */ { 0xb96b3cafU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5028 /* CodecName */ png_codecname
,
5030 /* FormatDescription */ png_format
,
5031 /* FilenameExtension */ png_extension
,
5032 /* MimeType */ png_mimetype
,
5033 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
5037 /* SigPattern */ png_sig_pattern
,
5038 /* SigMask */ png_sig_mask
,
5046 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
5047 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
5048 /* CodecName */ ico_codecname
,
5050 /* FormatDescription */ ico_format
,
5051 /* FilenameExtension */ ico_extension
,
5052 /* MimeType */ ico_mimetype
,
5053 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
5057 /* SigPattern */ ico_sig_pattern
,
5058 /* SigMask */ ico_sig_mask
,
5066 /*****************************************************************************
5067 * GdipGetImageDecodersSize [GDIPLUS.@]
5069 GpStatus WINGDIPAPI
GdipGetImageDecodersSize(UINT
*numDecoders
, UINT
*size
)
5071 int decoder_count
=0;
5073 TRACE("%p %p\n", numDecoders
, size
);
5075 if (!numDecoders
|| !size
)
5076 return InvalidParameter
;
5078 for (i
=0; i
<NUM_CODECS
; i
++)
5080 if (codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
)
5084 *numDecoders
= decoder_count
;
5085 *size
= decoder_count
* sizeof(ImageCodecInfo
);
5090 /*****************************************************************************
5091 * GdipGetImageDecoders [GDIPLUS.@]
5093 GpStatus WINGDIPAPI
GdipGetImageDecoders(UINT numDecoders
, UINT size
, ImageCodecInfo
*decoders
)
5095 int i
, decoder_count
=0;
5096 TRACE("%u %u %p\n", numDecoders
, size
, decoders
);
5099 size
!= numDecoders
* sizeof(ImageCodecInfo
))
5100 return GenericError
;
5102 for (i
=0; i
<NUM_CODECS
; i
++)
5104 if (codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
)
5106 if (decoder_count
== numDecoders
) return GenericError
;
5107 memcpy(&decoders
[decoder_count
], &codecs
[i
].info
, sizeof(ImageCodecInfo
));
5112 if (decoder_count
< numDecoders
) return GenericError
;
5117 /*****************************************************************************
5118 * GdipGetImageEncodersSize [GDIPLUS.@]
5120 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
5122 int encoder_count
=0;
5124 TRACE("%p %p\n", numEncoders
, size
);
5126 if (!numEncoders
|| !size
)
5127 return InvalidParameter
;
5129 for (i
=0; i
<NUM_CODECS
; i
++)
5131 if (codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
)
5135 *numEncoders
= encoder_count
;
5136 *size
= encoder_count
* sizeof(ImageCodecInfo
);
5141 /*****************************************************************************
5142 * GdipGetImageEncoders [GDIPLUS.@]
5144 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
5146 int i
, encoder_count
=0;
5147 TRACE("%u %u %p\n", numEncoders
, size
, encoders
);
5150 size
!= numEncoders
* sizeof(ImageCodecInfo
))
5151 return GenericError
;
5153 for (i
=0; i
<NUM_CODECS
; i
++)
5155 if (codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
)
5157 if (encoder_count
== numEncoders
) return GenericError
;
5158 memcpy(&encoders
[encoder_count
], &codecs
[i
].info
, sizeof(ImageCodecInfo
));
5163 if (encoder_count
< numEncoders
) return GenericError
;
5168 GpStatus WINGDIPAPI
GdipGetEncoderParameterListSize(GpImage
*image
,
5169 GDIPCONST CLSID
* clsidEncoder
, UINT
*size
)
5173 TRACE("(%p,%s,%p)\n", image
, debugstr_guid(clsidEncoder
), size
);
5176 FIXME("not implemented\n");
5180 return NotImplemented
;
5183 static PixelFormat
get_16bpp_format(HBITMAP hbm
)
5189 hdc
= CreateCompatibleDC(NULL
);
5191 memset(&bmh
, 0, sizeof(bmh
));
5192 bmh
.bV4Size
= sizeof(bmh
);
5195 bmh
.bV4V4Compression
= BI_BITFIELDS
;
5196 bmh
.bV4BitCount
= 16;
5198 GetDIBits(hdc
, hbm
, 0, 0, NULL
, (BITMAPINFO
*)&bmh
, DIB_RGB_COLORS
);
5200 if (bmh
.bV4RedMask
== 0x7c00 &&
5201 bmh
.bV4GreenMask
== 0x3e0 &&
5202 bmh
.bV4BlueMask
== 0x1f)
5204 result
= PixelFormat16bppRGB555
;
5206 else if (bmh
.bV4RedMask
== 0xf800 &&
5207 bmh
.bV4GreenMask
== 0x7e0 &&
5208 bmh
.bV4BlueMask
== 0x1f)
5210 result
= PixelFormat16bppRGB565
;
5214 FIXME("unrecognized bitfields %x,%x,%x\n", bmh
.bV4RedMask
,
5215 bmh
.bV4GreenMask
, bmh
.bV4BlueMask
);
5216 result
= PixelFormatUndefined
;
5224 /*****************************************************************************
5225 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
5227 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
5232 BitmapData lockeddata
;
5233 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
[256])];
5234 BITMAPINFO
*pbmi
= (BITMAPINFO
*)bmibuf
;
5236 TRACE("%p %p %p\n", hbm
, hpal
, bitmap
);
5239 return InvalidParameter
;
5241 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
5242 return InvalidParameter
;
5244 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
5245 switch(bm
.bmBitsPixel
) {
5247 format
= PixelFormat1bppIndexed
;
5250 format
= PixelFormat4bppIndexed
;
5253 format
= PixelFormat8bppIndexed
;
5256 format
= get_16bpp_format(hbm
);
5257 if (format
== PixelFormatUndefined
)
5258 return InvalidParameter
;
5261 format
= PixelFormat24bppRGB
;
5264 format
= PixelFormat32bppRGB
;
5267 format
= PixelFormat48bppRGB
;
5270 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
5271 return InvalidParameter
;
5274 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, 0,
5275 format
, NULL
, bitmap
);
5279 retval
= GdipBitmapLockBits(*bitmap
, NULL
, ImageLockModeWrite
,
5280 format
, &lockeddata
);
5286 hdc
= CreateCompatibleDC(NULL
);
5288 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
5289 pbmi
->bmiHeader
.biBitCount
= 0;
5291 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
5293 src_height
= abs(pbmi
->bmiHeader
.biHeight
);
5294 pbmi
->bmiHeader
.biHeight
= -src_height
;
5296 GetDIBits(hdc
, hbm
, 0, src_height
, lockeddata
.Scan0
, pbmi
, DIB_RGB_COLORS
);
5300 GdipBitmapUnlockBits(*bitmap
, &lockeddata
);
5303 /* According to the tests hpal is ignored */
5304 if (retval
== Ok
&& pbmi
->bmiHeader
.biBitCount
<= 8)
5306 ColorPalette
*palette
;
5307 int i
, num_palette_entries
;
5309 num_palette_entries
= pbmi
->bmiHeader
.biClrUsed
;
5310 if (!num_palette_entries
)
5311 num_palette_entries
= 1 << pbmi
->bmiHeader
.biBitCount
;
5313 palette
= heap_alloc_zero(sizeof(ColorPalette
) + sizeof(ARGB
) * (num_palette_entries
-1));
5315 retval
= OutOfMemory
;
5319 palette
->Count
= num_palette_entries
;
5321 for (i
=0; i
<num_palette_entries
; i
++)
5323 palette
->Entries
[i
] = 0xff000000 | pbmi
->bmiColors
[i
].rgbRed
<< 16 |
5324 pbmi
->bmiColors
[i
].rgbGreen
<< 8 | pbmi
->bmiColors
[i
].rgbBlue
;
5327 retval
= GdipSetImagePalette(&(*bitmap
)->image
, palette
);
5335 GdipDisposeImage(&(*bitmap
)->image
);
5343 /*****************************************************************************
5344 * GdipCreateEffect [GDIPLUS.@]
5346 GpStatus WINGDIPAPI
GdipCreateEffect(const GUID guid
, CGpEffect
**effect
)
5348 FIXME("(%s, %p): stub\n", debugstr_guid(&guid
), effect
);
5351 return InvalidParameter
;
5355 return NotImplemented
;
5358 /*****************************************************************************
5359 * GdipDeleteEffect [GDIPLUS.@]
5361 GpStatus WINGDIPAPI
GdipDeleteEffect(CGpEffect
*effect
)
5363 FIXME("(%p): stub\n", effect
);
5364 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5365 * in Windows's gdiplus */
5366 return NotImplemented
;
5369 /*****************************************************************************
5370 * GdipSetEffectParameters [GDIPLUS.@]
5372 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
5373 const VOID
*params
, const UINT size
)
5377 TRACE("(%p,%p,%u)\n", effect
, params
, size
);
5380 FIXME("not implemented\n");
5382 return NotImplemented
;
5385 /*****************************************************************************
5386 * GdipGetImageFlags [GDIPLUS.@]
5388 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
5390 TRACE("%p %p\n", image
, flags
);
5392 if(!image
|| !flags
)
5393 return InvalidParameter
;
5395 *flags
= image
->flags
;
5400 GpStatus WINGDIPAPI
GdipTestControl(GpTestControlEnum control
, void *param
)
5402 TRACE("(%d, %p)\n", control
, param
);
5405 case TestControlForceBilinear
:
5407 FIXME("TestControlForceBilinear not handled\n");
5409 case TestControlNoICM
:
5411 FIXME("TestControlNoICM not handled\n");
5413 case TestControlGetBuildNumber
:
5414 *((DWORD
*)param
) = 3102;
5421 GpStatus WINGDIPAPI
GdipImageForceValidation(GpImage
*image
)
5423 TRACE("%p\n", image
);
5428 /*****************************************************************************
5429 * GdipGetImageThumbnail [GDIPLUS.@]
5431 GpStatus WINGDIPAPI
GdipGetImageThumbnail(GpImage
*image
, UINT width
, UINT height
,
5432 GpImage
**ret_image
, GetThumbnailImageAbort cb
,
5436 GpGraphics
*graphics
;
5437 UINT srcwidth
, srcheight
;
5439 TRACE("(%p %u %u %p %p %p)\n",
5440 image
, width
, height
, ret_image
, cb
, cb_data
);
5442 if (!image
|| !ret_image
)
5443 return InvalidParameter
;
5445 if (!width
) width
= 120;
5446 if (!height
) height
= 120;
5448 GdipGetImageWidth(image
, &srcwidth
);
5449 GdipGetImageHeight(image
, &srcheight
);
5451 stat
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat32bppPARGB
,
5452 NULL
, (GpBitmap
**)ret_image
);
5456 stat
= GdipGetImageGraphicsContext(*ret_image
, &graphics
);
5460 stat
= GdipDrawImageRectRectI(graphics
, image
,
5461 0, 0, width
, height
, 0, 0, srcwidth
, srcheight
, UnitPixel
,
5464 GdipDeleteGraphics(graphics
);
5469 GdipDisposeImage(*ret_image
);
5477 /*****************************************************************************
5478 * GdipImageRotateFlip [GDIPLUS.@]
5480 GpStatus WINGDIPAPI
GdipImageRotateFlip(GpImage
*image
, RotateFlipType type
)
5482 GpBitmap
*new_bitmap
;
5484 int bpp
, bytesperpixel
;
5485 BOOL rotate_90
, flip_x
, flip_y
;
5486 int src_x_offset
, src_y_offset
;
5488 UINT x
, y
, width
, height
;
5489 BitmapData src_lock
, dst_lock
;
5493 TRACE("(%p, %u)\n", image
, type
);
5496 return InvalidParameter
;
5497 if (!image_lock(image
, &unlock
))
5501 flip_x
= (type
&6) == 2 || (type
&6) == 4;
5502 flip_y
= (type
&3) == 1 || (type
&3) == 2;
5504 if (image
->type
!= ImageTypeBitmap
)
5506 FIXME("Not implemented for type %i\n", image
->type
);
5507 image_unlock(image
, unlock
);
5508 return NotImplemented
;
5511 bitmap
= (GpBitmap
*)image
;
5512 bpp
= PIXELFORMATBPP(bitmap
->format
);
5516 FIXME("Not implemented for %i bit images\n", bpp
);
5517 image_unlock(image
, unlock
);
5518 return NotImplemented
;
5523 width
= bitmap
->height
;
5524 height
= bitmap
->width
;
5528 width
= bitmap
->width
;
5529 height
= bitmap
->height
;
5532 bytesperpixel
= bpp
/8;
5534 stat
= GdipCreateBitmapFromScan0(width
, height
, 0, bitmap
->format
, NULL
, &new_bitmap
);
5538 image_unlock(image
, unlock
);
5542 stat
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
, bitmap
->format
, &src_lock
);
5546 stat
= GdipBitmapLockBits(new_bitmap
, NULL
, ImageLockModeWrite
, bitmap
->format
, &dst_lock
);
5550 LPBYTE src_row
, src_pixel
;
5551 LPBYTE dst_row
, dst_pixel
;
5553 src_origin
= src_lock
.Scan0
;
5554 if (flip_x
) src_origin
+= bytesperpixel
* (bitmap
->width
- 1);
5555 if (flip_y
) src_origin
+= src_lock
.Stride
* (bitmap
->height
- 1);
5559 if (flip_y
) src_x_offset
= -src_lock
.Stride
;
5560 else src_x_offset
= src_lock
.Stride
;
5561 if (flip_x
) src_y_offset
= -bytesperpixel
;
5562 else src_y_offset
= bytesperpixel
;
5566 if (flip_x
) src_x_offset
= -bytesperpixel
;
5567 else src_x_offset
= bytesperpixel
;
5568 if (flip_y
) src_y_offset
= -src_lock
.Stride
;
5569 else src_y_offset
= src_lock
.Stride
;
5572 src_row
= src_origin
;
5573 dst_row
= dst_lock
.Scan0
;
5574 for (y
=0; y
<height
; y
++)
5576 src_pixel
= src_row
;
5577 dst_pixel
= dst_row
;
5578 for (x
=0; x
<width
; x
++)
5580 /* FIXME: This could probably be faster without memcpy. */
5581 memcpy(dst_pixel
, src_pixel
, bytesperpixel
);
5582 dst_pixel
+= bytesperpixel
;
5583 src_pixel
+= src_x_offset
;
5585 src_row
+= src_y_offset
;
5586 dst_row
+= dst_lock
.Stride
;
5589 GdipBitmapUnlockBits(new_bitmap
, &dst_lock
);
5592 GdipBitmapUnlockBits(bitmap
, &src_lock
);
5596 move_bitmap(bitmap
, new_bitmap
, FALSE
);
5598 GdipDisposeImage(&new_bitmap
->image
);
5600 image_unlock(image
, unlock
);
5604 /*****************************************************************************
5605 * GdipImageSetAbort [GDIPLUS.@]
5607 GpStatus WINGDIPAPI
GdipImageSetAbort(GpImage
*image
, GdiplusAbort
*pabort
)
5609 TRACE("(%p, %p)\n", image
, pabort
);
5612 return InvalidParameter
;
5615 FIXME("Abort callback is not supported.\n");
5620 /*****************************************************************************
5621 * GdipBitmapConvertFormat [GDIPLUS.@]
5623 GpStatus WINGDIPAPI
GdipBitmapConvertFormat(GpBitmap
*bitmap
, PixelFormat format
, DitherType dithertype
,
5624 PaletteType palettetype
, ColorPalette
*palette
, REAL alphathreshold
)
5626 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap
, format
, dithertype
, palettetype
, palette
, alphathreshold
);
5627 return NotImplemented
;
5630 static void set_histogram_point_argb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5632 ch0
[ color
>> 24 ]++;
5633 ch1
[(color
>> 16) & 0xff]++;
5634 ch2
[(color
>> 8) & 0xff]++;
5635 ch3
[ color
& 0xff]++;
5638 static void set_histogram_point_pargb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5640 BYTE alpha
= color
>> 24;
5643 ch1
[(((color
>> 16) & 0xff) * alpha
) / 0xff]++;
5644 ch2
[(((color
>> 8) & 0xff) * alpha
) / 0xff]++;
5645 ch3
[(( color
& 0xff) * alpha
) / 0xff]++;
5648 static void set_histogram_point_rgb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5650 ch0
[(color
>> 16) & 0xff]++;
5651 ch1
[(color
>> 8) & 0xff]++;
5652 ch2
[ color
& 0xff]++;
5655 static void set_histogram_point_gray(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5657 ch0
[(76 * ((color
>> 16) & 0xff) + 150 * ((color
>> 8) & 0xff) + 29 * (color
& 0xff)) / 0xff]++;
5660 static void set_histogram_point_b(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5662 ch0
[color
& 0xff]++;
5665 static void set_histogram_point_g(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5667 ch0
[(color
>> 8) & 0xff]++;
5670 static void set_histogram_point_r(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5672 ch0
[(color
>> 16) & 0xff]++;
5675 static void set_histogram_point_a(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5677 ch0
[(color
>> 24) & 0xff]++;
5680 /*****************************************************************************
5681 * GdipBitmapGetHistogram [GDIPLUS.@]
5683 GpStatus WINGDIPAPI
GdipBitmapGetHistogram(GpBitmap
*bitmap
, HistogramFormat format
, UINT num_of_entries
,
5684 UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5686 static void (* const set_histogram_point
[])(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
) =
5688 set_histogram_point_argb
,
5689 set_histogram_point_pargb
,
5690 set_histogram_point_rgb
,
5691 set_histogram_point_gray
,
5692 set_histogram_point_b
,
5693 set_histogram_point_g
,
5694 set_histogram_point_r
,
5695 set_histogram_point_a
,
5697 UINT width
, height
, x
, y
;
5699 TRACE("(%p, %d, %u, %p, %p, %p, %p)\n", bitmap
, format
, num_of_entries
,
5700 ch0
, ch1
, ch2
, ch3
);
5702 if (!bitmap
|| num_of_entries
!= 256)
5703 return InvalidParameter
;
5705 /* Make sure passed channel pointers match requested format */
5708 case HistogramFormatARGB
:
5709 case HistogramFormatPARGB
:
5710 if (!ch0
|| !ch1
|| !ch2
|| !ch3
)
5711 return InvalidParameter
;
5712 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5713 memset(ch1
, 0, num_of_entries
* sizeof(UINT
));
5714 memset(ch2
, 0, num_of_entries
* sizeof(UINT
));
5715 memset(ch3
, 0, num_of_entries
* sizeof(UINT
));
5717 case HistogramFormatRGB
:
5718 if (!ch0
|| !ch1
|| !ch2
|| ch3
)
5719 return InvalidParameter
;
5720 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5721 memset(ch1
, 0, num_of_entries
* sizeof(UINT
));
5722 memset(ch2
, 0, num_of_entries
* sizeof(UINT
));
5724 case HistogramFormatGray
:
5725 case HistogramFormatB
:
5726 case HistogramFormatG
:
5727 case HistogramFormatR
:
5728 case HistogramFormatA
:
5729 if (!ch0
|| ch1
|| ch2
|| ch3
)
5730 return InvalidParameter
;
5731 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5734 WARN("Invalid histogram format requested, %d\n", format
);
5735 return InvalidParameter
;
5738 GdipGetImageWidth(&bitmap
->image
, &width
);
5739 GdipGetImageHeight(&bitmap
->image
, &height
);
5741 for (y
= 0; y
< height
; y
++)
5742 for (x
= 0; x
< width
; x
++)
5746 GdipBitmapGetPixel(bitmap
, x
, y
, &color
);
5747 set_histogram_point
[format
](color
, ch0
, ch1
, ch2
, ch3
);
5753 /*****************************************************************************
5754 * GdipBitmapGetHistogramSize [GDIPLUS.@]
5756 GpStatus WINGDIPAPI
GdipBitmapGetHistogramSize(HistogramFormat format
, UINT
*num_of_entries
)
5758 TRACE("(%d, %p)\n", format
, num_of_entries
);
5760 if (!num_of_entries
)
5761 return InvalidParameter
;
5763 *num_of_entries
= 256;
5767 static GpStatus
create_optimal_palette(ColorPalette
*palette
, INT desired
,
5768 BOOL transparent
, GpBitmap
*bitmap
)
5773 IWICImagingFactory
*factory
;
5774 IWICPalette
*wic_palette
;
5776 if (!bitmap
) return InvalidParameter
;
5777 if (palette
->Count
< desired
) return GenericError
;
5779 status
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
, PixelFormat24bppRGB
, &data
);
5780 if (status
!= Ok
) return status
;
5782 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
5785 GdipBitmapUnlockBits(bitmap
, &data
);
5786 return hresult_to_status(hr
);
5789 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
5794 /* PixelFormat24bppRGB actually stores the bitmap bits as BGR. */
5795 hr
= IWICImagingFactory_CreateBitmapFromMemory(factory
, data
.Width
, data
.Height
,
5796 &GUID_WICPixelFormat24bppBGR
, data
.Stride
, data
.Stride
* data
.Width
, data
.Scan0
, &bitmap
);
5799 hr
= IWICPalette_InitializeFromBitmap(wic_palette
, (IWICBitmapSource
*)bitmap
, desired
, transparent
);
5803 IWICPalette_GetColorCount(wic_palette
, &palette
->Count
);
5804 IWICPalette_GetColors(wic_palette
, palette
->Count
, palette
->Entries
, &palette
->Count
);
5807 IWICBitmap_Release(bitmap
);
5810 IWICPalette_Release(wic_palette
);
5813 IWICImagingFactory_Release(factory
);
5814 GdipBitmapUnlockBits(bitmap
, &data
);
5816 return hresult_to_status(hr
);
5819 /*****************************************************************************
5820 * GdipInitializePalette [GDIPLUS.@]
5822 GpStatus WINGDIPAPI
GdipInitializePalette(ColorPalette
*palette
,
5823 PaletteType type
, INT desired
, BOOL transparent
, GpBitmap
*bitmap
)
5825 TRACE("(%p,%d,%d,%d,%p)\n", palette
, type
, desired
, transparent
, bitmap
);
5827 if (!palette
) return InvalidParameter
;
5831 case PaletteTypeCustom
:
5834 case PaletteTypeOptimal
:
5835 return create_optimal_palette(palette
, desired
, transparent
, bitmap
);
5837 /* WIC palette type enumeration matches these gdiplus enums */
5838 case PaletteTypeFixedBW
:
5839 case PaletteTypeFixedHalftone8
:
5840 case PaletteTypeFixedHalftone27
:
5841 case PaletteTypeFixedHalftone64
:
5842 case PaletteTypeFixedHalftone125
:
5843 case PaletteTypeFixedHalftone216
:
5844 case PaletteTypeFixedHalftone252
:
5845 case PaletteTypeFixedHalftone256
:
5847 ColorPalette
*wic_palette
;
5848 GpStatus status
= Ok
;
5850 wic_palette
= get_palette(NULL
, type
);
5851 if (!wic_palette
) return OutOfMemory
;
5853 if (palette
->Count
>= wic_palette
->Count
)
5855 palette
->Flags
= wic_palette
->Flags
;
5856 palette
->Count
= wic_palette
->Count
;
5857 memcpy(palette
->Entries
, wic_palette
->Entries
, wic_palette
->Count
* sizeof(wic_palette
->Entries
[0]));
5860 status
= GenericError
;
5862 heap_free(wic_palette
);
5868 FIXME("unknown palette type %d\n", type
);
5872 return InvalidParameter
;