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
20 #include "gdiplus_private.h"
26 HRESULT WINAPI
WICCreateImagingFactory_Proxy(UINT
, IWICImagingFactory
**);
28 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
29 #define WMF_PLACEABLE_KEY 0x9ac6cdd7
33 const WICPixelFormatGUID
*wic_format
;
34 PixelFormat gdip_format
;
35 /* predefined palette type to use for pixel format conversions */
36 WICBitmapPaletteType palette_type
;
39 { &GUID_WICPixelFormatBlackWhite
, PixelFormat1bppIndexed
, WICBitmapPaletteTypeFixedBW
},
40 { &GUID_WICPixelFormat1bppIndexed
, PixelFormat1bppIndexed
, WICBitmapPaletteTypeFixedBW
},
41 { &GUID_WICPixelFormat4bppIndexed
, PixelFormat4bppIndexed
, WICBitmapPaletteTypeFixedHalftone8
},
42 { &GUID_WICPixelFormat8bppGray
, PixelFormat8bppIndexed
, WICBitmapPaletteTypeFixedGray256
},
43 { &GUID_WICPixelFormat8bppIndexed
, PixelFormat8bppIndexed
, WICBitmapPaletteTypeFixedHalftone256
},
44 { &GUID_WICPixelFormat16bppBGR555
, PixelFormat16bppRGB555
, WICBitmapPaletteTypeFixedHalftone256
},
45 { &GUID_WICPixelFormat24bppBGR
, PixelFormat24bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
46 { &GUID_WICPixelFormat32bppBGR
, PixelFormat32bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
47 { &GUID_WICPixelFormat48bppRGB
, PixelFormat48bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
48 { &GUID_WICPixelFormat32bppBGRA
, PixelFormat32bppARGB
, WICBitmapPaletteTypeFixedHalftone256
},
49 { &GUID_WICPixelFormat32bppPBGRA
, PixelFormat32bppPARGB
, WICBitmapPaletteTypeFixedHalftone256
},
50 { &GUID_WICPixelFormat32bppCMYK
, PixelFormat32bppCMYK
, WICBitmapPaletteTypeFixedHalftone256
},
51 { &GUID_WICPixelFormat32bppGrayFloat
, PixelFormat32bppARGB
, WICBitmapPaletteTypeFixedGray256
},
52 { &GUID_WICPixelFormat64bppCMYK
, PixelFormat48bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
53 { &GUID_WICPixelFormat64bppRGBA
, PixelFormat48bppRGB
, WICBitmapPaletteTypeFixedHalftone256
},
57 static ColorPalette
*get_palette(IWICBitmapFrameDecode
*frame
, WICBitmapPaletteType palette_type
)
60 IWICImagingFactory
*factory
;
61 IWICPalette
*wic_palette
;
62 ColorPalette
*palette
= NULL
;
64 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
65 if (hr
!= S_OK
) return NULL
;
67 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
70 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
72 hr
= IWICBitmapFrameDecode_CopyPalette(frame
, wic_palette
);
75 TRACE("using predefined palette %#x\n", palette_type
);
76 hr
= IWICPalette_InitializePredefined(wic_palette
, palette_type
, FALSE
);
80 WICBitmapPaletteType type
;
84 IWICPalette_GetColorCount(wic_palette
, &count
);
85 palette
= heap_alloc(2 * sizeof(UINT
) + count
* sizeof(ARGB
));
86 IWICPalette_GetColors(wic_palette
, count
, palette
->Entries
, &palette
->Count
);
88 IWICPalette_GetType(wic_palette
, &type
);
90 case WICBitmapPaletteTypeFixedGray4
:
91 case WICBitmapPaletteTypeFixedGray16
:
92 case WICBitmapPaletteTypeFixedGray256
:
93 palette
->Flags
= PaletteFlagsGrayScale
;
95 case WICBitmapPaletteTypeFixedHalftone8
:
96 case WICBitmapPaletteTypeFixedHalftone27
:
97 case WICBitmapPaletteTypeFixedHalftone64
:
98 case WICBitmapPaletteTypeFixedHalftone125
:
99 case WICBitmapPaletteTypeFixedHalftone216
:
100 case WICBitmapPaletteTypeFixedHalftone252
:
101 case WICBitmapPaletteTypeFixedHalftone256
:
102 palette
->Flags
= PaletteFlagsHalftone
;
107 IWICPalette_HasAlpha(wic_palette
, &alpha
);
109 palette
->Flags
|= PaletteFlagsHasAlpha
;
111 IWICPalette_Release(wic_palette
);
113 IWICImagingFactory_Release(factory
);
117 GpStatus WINGDIPAPI
GdipBitmapApplyEffect(GpBitmap
* bitmap
, CGpEffect
* effect
,
118 RECT
* roi
, BOOL useAuxData
, VOID
** auxData
, INT
* auxDataSize
)
120 FIXME("(%p %p %p %d %p %p): stub\n", bitmap
, effect
, roi
, useAuxData
, auxData
, auxDataSize
);
122 * Note: According to Jose Roca's GDI+ docs, this function is not
123 * implemented in Windows's GDI+.
125 return NotImplemented
;
128 GpStatus WINGDIPAPI
GdipBitmapCreateApplyEffect(GpBitmap
** inputBitmaps
,
129 INT numInputs
, CGpEffect
* effect
, RECT
* roi
, RECT
* outputRect
,
130 GpBitmap
** outputBitmap
, BOOL useAuxData
, VOID
** auxData
, INT
* auxDataSize
)
132 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps
, numInputs
, effect
, roi
, outputRect
, outputBitmap
, useAuxData
, auxData
, auxDataSize
);
134 * Note: According to Jose Roca's GDI+ docs, this function is not
135 * implemented in Windows's GDI+.
137 return NotImplemented
;
140 static inline void getpixel_1bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
142 *index
= (row
[x
/8]>>(7-x
%8)) & 1;
145 static inline void getpixel_4bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
148 *index
= row
[x
/2]&0xf;
150 *index
= row
[x
/2]>>4;
153 static inline void getpixel_8bppIndexed(BYTE
*index
, const BYTE
*row
, UINT x
)
158 static inline void getpixel_16bppGrayScale(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
159 const BYTE
*row
, UINT x
)
161 *r
= *g
= *b
= row
[x
*2+1];
165 static inline void getpixel_16bppRGB555(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
166 const BYTE
*row
, UINT x
)
168 WORD pixel
= *((const WORD
*)(row
)+x
);
169 *r
= (pixel
>>7&0xf8)|(pixel
>>12&0x7);
170 *g
= (pixel
>>2&0xf8)|(pixel
>>6&0x7);
171 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
175 static inline void getpixel_16bppRGB565(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
176 const BYTE
*row
, UINT x
)
178 WORD pixel
= *((const WORD
*)(row
)+x
);
179 *r
= (pixel
>>8&0xf8)|(pixel
>>13&0x7);
180 *g
= (pixel
>>3&0xfc)|(pixel
>>9&0x3);
181 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
185 static inline void getpixel_16bppARGB1555(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
186 const BYTE
*row
, UINT x
)
188 WORD pixel
= *((const WORD
*)(row
)+x
);
189 *r
= (pixel
>>7&0xf8)|(pixel
>>12&0x7);
190 *g
= (pixel
>>2&0xf8)|(pixel
>>6&0x7);
191 *b
= (pixel
<<3&0xf8)|(pixel
>>2&0x7);
192 if ((pixel
&0x8000) == 0x8000)
198 static inline void getpixel_24bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
199 const BYTE
*row
, UINT x
)
207 static inline void getpixel_32bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
208 const BYTE
*row
, UINT x
)
216 static inline void getpixel_32bppARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
217 const BYTE
*row
, UINT x
)
225 static inline void getpixel_32bppPARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
226 const BYTE
*row
, UINT x
)
233 *r
= row
[x
*4+2] * 255 / *a
;
234 *g
= row
[x
*4+1] * 255 / *a
;
235 *b
= row
[x
*4] * 255 / *a
;
239 static inline void getpixel_48bppRGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
240 const BYTE
*row
, UINT x
)
248 static inline void getpixel_64bppARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
249 const BYTE
*row
, UINT x
)
257 static inline void getpixel_64bppPARGB(BYTE
*r
, BYTE
*g
, BYTE
*b
, BYTE
*a
,
258 const BYTE
*row
, UINT x
)
265 *r
= row
[x
*8+5] * 255 / *a
;
266 *g
= row
[x
*8+3] * 255 / *a
;
267 *b
= row
[x
*8+1] * 255 / *a
;
271 GpStatus WINGDIPAPI
GdipBitmapGetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
278 if(!bitmap
|| !color
||
279 x
< 0 || y
< 0 || x
>= bitmap
->width
|| y
>= bitmap
->height
)
280 return InvalidParameter
;
282 row
= bitmap
->bits
+bitmap
->stride
*y
;
284 switch (bitmap
->format
)
286 case PixelFormat1bppIndexed
:
287 getpixel_1bppIndexed(&index
,row
,x
);
289 case PixelFormat4bppIndexed
:
290 getpixel_4bppIndexed(&index
,row
,x
);
292 case PixelFormat8bppIndexed
:
293 getpixel_8bppIndexed(&index
,row
,x
);
295 case PixelFormat16bppGrayScale
:
296 getpixel_16bppGrayScale(&r
,&g
,&b
,&a
,row
,x
);
298 case PixelFormat16bppRGB555
:
299 getpixel_16bppRGB555(&r
,&g
,&b
,&a
,row
,x
);
301 case PixelFormat16bppRGB565
:
302 getpixel_16bppRGB565(&r
,&g
,&b
,&a
,row
,x
);
304 case PixelFormat16bppARGB1555
:
305 getpixel_16bppARGB1555(&r
,&g
,&b
,&a
,row
,x
);
307 case PixelFormat24bppRGB
:
308 getpixel_24bppRGB(&r
,&g
,&b
,&a
,row
,x
);
310 case PixelFormat32bppRGB
:
311 getpixel_32bppRGB(&r
,&g
,&b
,&a
,row
,x
);
313 case PixelFormat32bppARGB
:
314 getpixel_32bppARGB(&r
,&g
,&b
,&a
,row
,x
);
316 case PixelFormat32bppPARGB
:
317 getpixel_32bppPARGB(&r
,&g
,&b
,&a
,row
,x
);
319 case PixelFormat48bppRGB
:
320 getpixel_48bppRGB(&r
,&g
,&b
,&a
,row
,x
);
322 case PixelFormat64bppARGB
:
323 getpixel_64bppARGB(&r
,&g
,&b
,&a
,row
,x
);
325 case PixelFormat64bppPARGB
:
326 getpixel_64bppPARGB(&r
,&g
,&b
,&a
,row
,x
);
329 FIXME("not implemented for format 0x%x\n", bitmap
->format
);
330 return NotImplemented
;
333 if (bitmap
->format
& PixelFormatIndexed
)
334 *color
= bitmap
->image
.palette
->Entries
[index
];
336 *color
= a
<<24|r
<<16|g
<<8|b
;
341 static inline UINT
get_palette_index(BYTE r
, BYTE g
, BYTE b
, BYTE a
, ColorPalette
*palette
)
344 int best_distance
= 0x7fff;
348 if (!palette
) return 0;
349 /* This algorithm scans entire palette,
350 computes difference from desired color (all color components have equal weight)
351 and returns the index of color with least difference.
353 Note: Maybe it could be replaced with a better algorithm for better image quality
354 and performance, though better algorithm would probably need some pre-built lookup
355 tables and thus may actually be slower if this method is called only few times per
358 for(i
=0;i
<palette
->Count
;i
++) {
359 ARGB color
=palette
->Entries
[i
];
360 distance
=abs(b
-(color
& 0xff)) + abs(g
-(color
>>8 & 0xff)) + abs(r
-(color
>>16 & 0xff)) + abs(a
-(color
>>24 & 0xff));
361 if (distance
<best_distance
) {
362 best_distance
=distance
;
369 static inline void setpixel_8bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
370 BYTE
*row
, UINT x
, ColorPalette
*palette
)
372 BYTE index
= get_palette_index(r
,g
,b
,a
,palette
);
376 static inline void setpixel_1bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
377 BYTE
*row
, UINT x
, ColorPalette
*palette
)
379 row
[x
/8] = (row
[x
/8] & ~(1<<(7-x
%8))) | (get_palette_index(r
,g
,b
,a
,palette
)<<(7-x
%8));
382 static inline void setpixel_4bppIndexed(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
383 BYTE
*row
, UINT x
, ColorPalette
*palette
)
386 row
[x
/2] = (row
[x
/2] & 0xf0) | get_palette_index(r
,g
,b
,a
,palette
);
388 row
[x
/2] = (row
[x
/2] & 0x0f) | get_palette_index(r
,g
,b
,a
,palette
)<<4;
391 static inline void setpixel_16bppGrayScale(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
394 *((WORD
*)(row
)+x
) = (r
+g
+b
)*85;
397 static inline void setpixel_16bppRGB555(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
400 *((WORD
*)(row
)+x
) = (r
<<7&0x7c00)|
405 static inline void setpixel_16bppRGB565(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
408 *((WORD
*)(row
)+x
) = (r
<<8&0xf800)|
413 static inline void setpixel_16bppARGB1555(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
416 *((WORD
*)(row
)+x
) = (a
<<8&0x8000)|
422 static inline void setpixel_24bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
430 static inline void setpixel_32bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
433 *((DWORD
*)(row
)+x
) = (r
<<16)|(g
<<8)|b
;
436 static inline void setpixel_32bppARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
439 *((DWORD
*)(row
)+x
) = (a
<<24)|(r
<<16)|(g
<<8)|b
;
442 static inline void setpixel_32bppPARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
448 *((DWORD
*)(row
)+x
) = (a
<<24)|(r
<<16)|(g
<<8)|b
;
451 static inline void setpixel_48bppRGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
454 row
[x
*6+5] = row
[x
*6+4] = r
;
455 row
[x
*6+3] = row
[x
*6+2] = g
;
456 row
[x
*6+1] = row
[x
*6] = b
;
459 static inline void setpixel_64bppARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
462 UINT64 a64
=a
, r64
=r
, g64
=g
, b64
=b
;
463 *((UINT64
*)(row
)+x
) = (a64
<<56)|(a64
<<48)|(r64
<<40)|(r64
<<32)|(g64
<<24)|(g64
<<16)|(b64
<<8)|b64
;
466 static inline void setpixel_64bppPARGB(BYTE r
, BYTE g
, BYTE b
, BYTE a
,
469 UINT64 a64
, r64
, g64
, b64
;
474 *((UINT64
*)(row
)+x
) = (a64
<<48)|(r64
<<32)|(g64
<<16)|b64
;
477 GpStatus WINGDIPAPI
GdipBitmapSetPixel(GpBitmap
* bitmap
, INT x
, INT y
,
483 if(!bitmap
|| x
< 0 || y
< 0 || x
>= bitmap
->width
|| y
>= bitmap
->height
)
484 return InvalidParameter
;
491 row
= bitmap
->bits
+ bitmap
->stride
* y
;
493 switch (bitmap
->format
)
495 case PixelFormat16bppGrayScale
:
496 setpixel_16bppGrayScale(r
,g
,b
,a
,row
,x
);
498 case PixelFormat16bppRGB555
:
499 setpixel_16bppRGB555(r
,g
,b
,a
,row
,x
);
501 case PixelFormat16bppRGB565
:
502 setpixel_16bppRGB565(r
,g
,b
,a
,row
,x
);
504 case PixelFormat16bppARGB1555
:
505 setpixel_16bppARGB1555(r
,g
,b
,a
,row
,x
);
507 case PixelFormat24bppRGB
:
508 setpixel_24bppRGB(r
,g
,b
,a
,row
,x
);
510 case PixelFormat32bppRGB
:
511 setpixel_32bppRGB(r
,g
,b
,a
,row
,x
);
513 case PixelFormat32bppARGB
:
514 setpixel_32bppARGB(r
,g
,b
,a
,row
,x
);
516 case PixelFormat32bppPARGB
:
517 setpixel_32bppPARGB(r
,g
,b
,a
,row
,x
);
519 case PixelFormat48bppRGB
:
520 setpixel_48bppRGB(r
,g
,b
,a
,row
,x
);
522 case PixelFormat64bppARGB
:
523 setpixel_64bppARGB(r
,g
,b
,a
,row
,x
);
525 case PixelFormat64bppPARGB
:
526 setpixel_64bppPARGB(r
,g
,b
,a
,row
,x
);
528 case PixelFormat8bppIndexed
:
529 setpixel_8bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
531 case PixelFormat4bppIndexed
:
532 setpixel_4bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
534 case PixelFormat1bppIndexed
:
535 setpixel_1bppIndexed(r
,g
,b
,a
,row
,x
,bitmap
->image
.palette
);
538 FIXME("not implemented for format 0x%x\n", bitmap
->format
);
539 return NotImplemented
;
545 GpStatus
convert_pixels(INT width
, INT height
,
546 INT dst_stride
, BYTE
*dst_bits
, PixelFormat dst_format
,
547 INT src_stride
, const BYTE
*src_bits
, PixelFormat src_format
,
548 ColorPalette
*palette
)
552 if (src_format
== dst_format
||
553 (dst_format
== PixelFormat32bppRGB
&& PIXELFORMATBPP(src_format
) == 32))
555 UINT widthbytes
= PIXELFORMATBPP(src_format
) * width
/ 8;
556 for (y
=0; y
<height
; y
++)
557 memcpy(dst_bits
+dst_stride
*y
, src_bits
+src_stride
*y
, widthbytes
);
561 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
562 for (y=0; y<height; y++) \
563 for (x=0; x<width; x++) { \
566 BYTE *color = (BYTE *)&argb; \
567 getpixel_function(&index, src_bits+src_stride*y, x); \
568 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
569 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
574 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
575 for (y=0; y<height; y++) \
576 for (x=0; x<width; x++) { \
578 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
579 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
584 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
585 for (y=0; y<height; y++) \
586 for (x=0; x<width; x++) { \
588 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
589 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
596 case PixelFormat1bppIndexed
:
599 case PixelFormat16bppGrayScale
:
600 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppGrayScale
);
601 case PixelFormat16bppRGB555
:
602 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppRGB555
);
603 case PixelFormat16bppRGB565
:
604 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppRGB565
);
605 case PixelFormat16bppARGB1555
:
606 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_16bppARGB1555
);
607 case PixelFormat24bppRGB
:
608 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_24bppRGB
);
609 case PixelFormat32bppRGB
:
610 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppRGB
);
611 case PixelFormat32bppARGB
:
612 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppARGB
);
613 case PixelFormat32bppPARGB
:
614 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_32bppPARGB
);
615 case PixelFormat48bppRGB
:
616 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_48bppRGB
);
617 case PixelFormat64bppARGB
:
618 convert_indexed_to_rgb(getpixel_1bppIndexed
, setpixel_64bppARGB
);
623 case PixelFormat4bppIndexed
:
626 case PixelFormat16bppGrayScale
:
627 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppGrayScale
);
628 case PixelFormat16bppRGB555
:
629 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppRGB555
);
630 case PixelFormat16bppRGB565
:
631 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppRGB565
);
632 case PixelFormat16bppARGB1555
:
633 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_16bppARGB1555
);
634 case PixelFormat24bppRGB
:
635 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_24bppRGB
);
636 case PixelFormat32bppRGB
:
637 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppRGB
);
638 case PixelFormat32bppARGB
:
639 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppARGB
);
640 case PixelFormat32bppPARGB
:
641 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_32bppPARGB
);
642 case PixelFormat48bppRGB
:
643 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_48bppRGB
);
644 case PixelFormat64bppARGB
:
645 convert_indexed_to_rgb(getpixel_4bppIndexed
, setpixel_64bppARGB
);
650 case PixelFormat8bppIndexed
:
653 case PixelFormat16bppGrayScale
:
654 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppGrayScale
);
655 case PixelFormat16bppRGB555
:
656 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppRGB555
);
657 case PixelFormat16bppRGB565
:
658 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppRGB565
);
659 case PixelFormat16bppARGB1555
:
660 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_16bppARGB1555
);
661 case PixelFormat24bppRGB
:
662 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_24bppRGB
);
663 case PixelFormat32bppRGB
:
664 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppRGB
);
665 case PixelFormat32bppARGB
:
666 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppARGB
);
667 case PixelFormat32bppPARGB
:
668 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_32bppPARGB
);
669 case PixelFormat48bppRGB
:
670 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_48bppRGB
);
671 case PixelFormat64bppARGB
:
672 convert_indexed_to_rgb(getpixel_8bppIndexed
, setpixel_64bppARGB
);
677 case PixelFormat16bppGrayScale
:
680 case PixelFormat1bppIndexed
:
681 convert_rgb_to_indexed(getpixel_16bppGrayScale
, setpixel_1bppIndexed
);
682 case PixelFormat8bppIndexed
:
683 convert_rgb_to_indexed(getpixel_16bppGrayScale
, setpixel_8bppIndexed
);
684 case PixelFormat16bppRGB555
:
685 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppRGB555
);
686 case PixelFormat16bppRGB565
:
687 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppRGB565
);
688 case PixelFormat16bppARGB1555
:
689 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_16bppARGB1555
);
690 case PixelFormat24bppRGB
:
691 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_24bppRGB
);
692 case PixelFormat32bppRGB
:
693 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppRGB
);
694 case PixelFormat32bppARGB
:
695 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppARGB
);
696 case PixelFormat32bppPARGB
:
697 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_32bppPARGB
);
698 case PixelFormat48bppRGB
:
699 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_48bppRGB
);
700 case PixelFormat64bppARGB
:
701 convert_rgb_to_rgb(getpixel_16bppGrayScale
, setpixel_64bppARGB
);
706 case PixelFormat16bppRGB555
:
709 case PixelFormat1bppIndexed
:
710 convert_rgb_to_indexed(getpixel_16bppRGB555
, setpixel_1bppIndexed
);
711 case PixelFormat8bppIndexed
:
712 convert_rgb_to_indexed(getpixel_16bppRGB555
, setpixel_8bppIndexed
);
713 case PixelFormat16bppGrayScale
:
714 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppGrayScale
);
715 case PixelFormat16bppRGB565
:
716 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppRGB565
);
717 case PixelFormat16bppARGB1555
:
718 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_16bppARGB1555
);
719 case PixelFormat24bppRGB
:
720 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_24bppRGB
);
721 case PixelFormat32bppRGB
:
722 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppRGB
);
723 case PixelFormat32bppARGB
:
724 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppARGB
);
725 case PixelFormat32bppPARGB
:
726 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_32bppPARGB
);
727 case PixelFormat48bppRGB
:
728 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_48bppRGB
);
729 case PixelFormat64bppARGB
:
730 convert_rgb_to_rgb(getpixel_16bppRGB555
, setpixel_64bppARGB
);
735 case PixelFormat16bppRGB565
:
738 case PixelFormat1bppIndexed
:
739 convert_rgb_to_indexed(getpixel_16bppRGB565
, setpixel_1bppIndexed
);
740 case PixelFormat8bppIndexed
:
741 convert_rgb_to_indexed(getpixel_16bppRGB565
, setpixel_8bppIndexed
);
742 case PixelFormat16bppGrayScale
:
743 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppGrayScale
);
744 case PixelFormat16bppRGB555
:
745 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppRGB555
);
746 case PixelFormat16bppARGB1555
:
747 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_16bppARGB1555
);
748 case PixelFormat24bppRGB
:
749 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_24bppRGB
);
750 case PixelFormat32bppRGB
:
751 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppRGB
);
752 case PixelFormat32bppARGB
:
753 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppARGB
);
754 case PixelFormat32bppPARGB
:
755 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_32bppPARGB
);
756 case PixelFormat48bppRGB
:
757 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_48bppRGB
);
758 case PixelFormat64bppARGB
:
759 convert_rgb_to_rgb(getpixel_16bppRGB565
, setpixel_64bppARGB
);
764 case PixelFormat16bppARGB1555
:
767 case PixelFormat1bppIndexed
:
768 convert_rgb_to_indexed(getpixel_16bppARGB1555
, setpixel_1bppIndexed
);
769 case PixelFormat8bppIndexed
:
770 convert_rgb_to_indexed(getpixel_16bppARGB1555
, setpixel_8bppIndexed
);
771 case PixelFormat16bppGrayScale
:
772 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppGrayScale
);
773 case PixelFormat16bppRGB555
:
774 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppRGB555
);
775 case PixelFormat16bppRGB565
:
776 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_16bppRGB565
);
777 case PixelFormat24bppRGB
:
778 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_24bppRGB
);
779 case PixelFormat32bppRGB
:
780 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppRGB
);
781 case PixelFormat32bppARGB
:
782 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppARGB
);
783 case PixelFormat32bppPARGB
:
784 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_32bppPARGB
);
785 case PixelFormat48bppRGB
:
786 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_48bppRGB
);
787 case PixelFormat64bppARGB
:
788 convert_rgb_to_rgb(getpixel_16bppARGB1555
, setpixel_64bppARGB
);
793 case PixelFormat24bppRGB
:
796 case PixelFormat1bppIndexed
:
797 convert_rgb_to_indexed(getpixel_24bppRGB
, setpixel_1bppIndexed
);
798 case PixelFormat8bppIndexed
:
799 convert_rgb_to_indexed(getpixel_24bppRGB
, setpixel_8bppIndexed
);
800 case PixelFormat16bppGrayScale
:
801 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppGrayScale
);
802 case PixelFormat16bppRGB555
:
803 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppRGB555
);
804 case PixelFormat16bppRGB565
:
805 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppRGB565
);
806 case PixelFormat16bppARGB1555
:
807 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_16bppARGB1555
);
808 case PixelFormat32bppRGB
:
809 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppRGB
);
810 case PixelFormat32bppARGB
:
811 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppARGB
);
812 case PixelFormat32bppPARGB
:
813 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_32bppPARGB
);
814 case PixelFormat48bppRGB
:
815 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_48bppRGB
);
816 case PixelFormat64bppARGB
:
817 convert_rgb_to_rgb(getpixel_24bppRGB
, setpixel_64bppARGB
);
822 case PixelFormat32bppRGB
:
825 case PixelFormat1bppIndexed
:
826 convert_rgb_to_indexed(getpixel_32bppRGB
, setpixel_1bppIndexed
);
827 case PixelFormat8bppIndexed
:
828 convert_rgb_to_indexed(getpixel_32bppRGB
, setpixel_8bppIndexed
);
829 case PixelFormat16bppGrayScale
:
830 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppGrayScale
);
831 case PixelFormat16bppRGB555
:
832 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppRGB555
);
833 case PixelFormat16bppRGB565
:
834 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppRGB565
);
835 case PixelFormat16bppARGB1555
:
836 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_16bppARGB1555
);
837 case PixelFormat24bppRGB
:
838 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_24bppRGB
);
839 case PixelFormat32bppARGB
:
840 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_32bppARGB
);
841 case PixelFormat32bppPARGB
:
842 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_32bppPARGB
);
843 case PixelFormat48bppRGB
:
844 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_48bppRGB
);
845 case PixelFormat64bppARGB
:
846 convert_rgb_to_rgb(getpixel_32bppRGB
, setpixel_64bppARGB
);
851 case PixelFormat32bppARGB
:
854 case PixelFormat1bppIndexed
:
855 convert_rgb_to_indexed(getpixel_32bppARGB
, setpixel_1bppIndexed
);
856 case PixelFormat8bppIndexed
:
857 convert_rgb_to_indexed(getpixel_32bppARGB
, setpixel_8bppIndexed
);
858 case PixelFormat16bppGrayScale
:
859 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppGrayScale
);
860 case PixelFormat16bppRGB555
:
861 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppRGB555
);
862 case PixelFormat16bppRGB565
:
863 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppRGB565
);
864 case PixelFormat16bppARGB1555
:
865 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_16bppARGB1555
);
866 case PixelFormat24bppRGB
:
867 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_24bppRGB
);
868 case PixelFormat32bppPARGB
:
869 convert_32bppARGB_to_32bppPARGB(width
, height
, dst_bits
, dst_stride
, src_bits
, src_stride
);
871 case PixelFormat48bppRGB
:
872 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_48bppRGB
);
873 case PixelFormat64bppARGB
:
874 convert_rgb_to_rgb(getpixel_32bppARGB
, setpixel_64bppARGB
);
879 case PixelFormat32bppPARGB
:
882 case PixelFormat1bppIndexed
:
883 convert_rgb_to_indexed(getpixel_32bppPARGB
, setpixel_1bppIndexed
);
884 case PixelFormat8bppIndexed
:
885 convert_rgb_to_indexed(getpixel_32bppPARGB
, setpixel_8bppIndexed
);
886 case PixelFormat16bppGrayScale
:
887 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppGrayScale
);
888 case PixelFormat16bppRGB555
:
889 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppRGB555
);
890 case PixelFormat16bppRGB565
:
891 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppRGB565
);
892 case PixelFormat16bppARGB1555
:
893 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_16bppARGB1555
);
894 case PixelFormat24bppRGB
:
895 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_24bppRGB
);
896 case PixelFormat32bppRGB
:
897 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_32bppRGB
);
898 case PixelFormat32bppARGB
:
899 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_32bppARGB
);
900 case PixelFormat48bppRGB
:
901 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_48bppRGB
);
902 case PixelFormat64bppARGB
:
903 convert_rgb_to_rgb(getpixel_32bppPARGB
, setpixel_64bppARGB
);
908 case PixelFormat48bppRGB
:
911 case PixelFormat1bppIndexed
:
912 convert_rgb_to_indexed(getpixel_48bppRGB
, setpixel_1bppIndexed
);
913 case PixelFormat8bppIndexed
:
914 convert_rgb_to_indexed(getpixel_48bppRGB
, setpixel_8bppIndexed
);
915 case PixelFormat16bppGrayScale
:
916 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppGrayScale
);
917 case PixelFormat16bppRGB555
:
918 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppRGB555
);
919 case PixelFormat16bppRGB565
:
920 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppRGB565
);
921 case PixelFormat16bppARGB1555
:
922 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_16bppARGB1555
);
923 case PixelFormat24bppRGB
:
924 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_24bppRGB
);
925 case PixelFormat32bppRGB
:
926 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppRGB
);
927 case PixelFormat32bppARGB
:
928 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppARGB
);
929 case PixelFormat32bppPARGB
:
930 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_32bppPARGB
);
931 case PixelFormat64bppARGB
:
932 convert_rgb_to_rgb(getpixel_48bppRGB
, setpixel_64bppARGB
);
937 case PixelFormat64bppARGB
:
940 case PixelFormat1bppIndexed
:
941 convert_rgb_to_indexed(getpixel_64bppARGB
, setpixel_1bppIndexed
);
942 case PixelFormat8bppIndexed
:
943 convert_rgb_to_indexed(getpixel_64bppARGB
, setpixel_8bppIndexed
);
944 case PixelFormat16bppGrayScale
:
945 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppGrayScale
);
946 case PixelFormat16bppRGB555
:
947 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppRGB555
);
948 case PixelFormat16bppRGB565
:
949 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppRGB565
);
950 case PixelFormat16bppARGB1555
:
951 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_16bppARGB1555
);
952 case PixelFormat24bppRGB
:
953 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_24bppRGB
);
954 case PixelFormat32bppRGB
:
955 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppRGB
);
956 case PixelFormat32bppARGB
:
957 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppARGB
);
958 case PixelFormat32bppPARGB
:
959 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_32bppPARGB
);
960 case PixelFormat48bppRGB
:
961 convert_rgb_to_rgb(getpixel_64bppARGB
, setpixel_48bppRGB
);
966 case PixelFormat64bppPARGB
:
969 case PixelFormat1bppIndexed
:
970 convert_rgb_to_indexed(getpixel_64bppPARGB
, setpixel_1bppIndexed
);
971 case PixelFormat8bppIndexed
:
972 convert_rgb_to_indexed(getpixel_64bppPARGB
, setpixel_8bppIndexed
);
973 case PixelFormat16bppGrayScale
:
974 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppGrayScale
);
975 case PixelFormat16bppRGB555
:
976 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppRGB555
);
977 case PixelFormat16bppRGB565
:
978 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppRGB565
);
979 case PixelFormat16bppARGB1555
:
980 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_16bppARGB1555
);
981 case PixelFormat24bppRGB
:
982 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_24bppRGB
);
983 case PixelFormat32bppRGB
:
984 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppRGB
);
985 case PixelFormat32bppARGB
:
986 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppARGB
);
987 case PixelFormat32bppPARGB
:
988 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_32bppPARGB
);
989 case PixelFormat48bppRGB
:
990 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_48bppRGB
);
991 case PixelFormat64bppARGB
:
992 convert_rgb_to_rgb(getpixel_64bppPARGB
, setpixel_64bppARGB
);
1001 #undef convert_indexed_to_rgb
1002 #undef convert_rgb_to_rgb
1004 return NotImplemented
;
1007 /* This function returns a pointer to an array of pixels that represents the
1008 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1009 * flags. It is correct behavior that a user who calls this function with write
1010 * privileges can write to the whole bitmap (not just the area in rect).
1012 * FIXME: only used portion of format is bits per pixel. */
1013 GpStatus WINGDIPAPI
GdipBitmapLockBits(GpBitmap
* bitmap
, GDIPCONST GpRect
* rect
,
1014 UINT flags
, PixelFormat format
, BitmapData
* lockeddata
)
1016 INT bitspp
= PIXELFORMATBPP(format
);
1017 GpRect act_rect
; /* actual rect to be used */
1021 TRACE("%p %p %d 0x%x %p\n", bitmap
, rect
, flags
, format
, lockeddata
);
1023 if(!lockeddata
|| !bitmap
)
1024 return InvalidParameter
;
1025 if(!image_lock(&bitmap
->image
, &unlock
))
1029 if(rect
->X
< 0 || rect
->Y
< 0 || (rect
->X
+ rect
->Width
> bitmap
->width
) ||
1030 (rect
->Y
+ rect
->Height
> bitmap
->height
) || !flags
)
1032 image_unlock(&bitmap
->image
, unlock
);
1033 return InvalidParameter
;
1039 act_rect
.X
= act_rect
.Y
= 0;
1040 act_rect
.Width
= bitmap
->width
;
1041 act_rect
.Height
= bitmap
->height
;
1044 if(bitmap
->lockmode
)
1046 WARN("bitmap is already locked and cannot be locked again\n");
1047 image_unlock(&bitmap
->image
, unlock
);
1051 if (bitmap
->bits
&& bitmap
->format
== format
&& !(flags
& ImageLockModeUserInputBuf
))
1053 /* no conversion is necessary; just use the bits directly */
1054 lockeddata
->Width
= act_rect
.Width
;
1055 lockeddata
->Height
= act_rect
.Height
;
1056 lockeddata
->PixelFormat
= format
;
1057 lockeddata
->Reserved
= flags
;
1058 lockeddata
->Stride
= bitmap
->stride
;
1059 lockeddata
->Scan0
= bitmap
->bits
+ (bitspp
/ 8) * act_rect
.X
+
1060 bitmap
->stride
* act_rect
.Y
;
1062 bitmap
->lockmode
= flags
| ImageLockModeRead
;
1064 image_unlock(&bitmap
->image
, unlock
);
1068 /* Make sure we can convert to the requested format. */
1069 if (flags
& ImageLockModeRead
)
1071 stat
= convert_pixels(0, 0, 0, NULL
, format
, 0, NULL
, bitmap
->format
, NULL
);
1072 if (stat
== NotImplemented
)
1074 FIXME("cannot read bitmap from %x to %x\n", bitmap
->format
, format
);
1075 image_unlock(&bitmap
->image
, unlock
);
1076 return NotImplemented
;
1080 /* If we're opening for writing, make sure we'll be able to write back in
1081 * the original format. */
1082 if (flags
& ImageLockModeWrite
)
1084 stat
= convert_pixels(0, 0, 0, NULL
, bitmap
->format
, 0, NULL
, format
, NULL
);
1085 if (stat
== NotImplemented
)
1087 FIXME("cannot write bitmap from %x to %x\n", format
, bitmap
->format
);
1088 image_unlock(&bitmap
->image
, unlock
);
1089 return NotImplemented
;
1093 lockeddata
->Width
= act_rect
.Width
;
1094 lockeddata
->Height
= act_rect
.Height
;
1095 lockeddata
->PixelFormat
= format
;
1096 lockeddata
->Reserved
= flags
;
1098 if(!(flags
& ImageLockModeUserInputBuf
))
1100 lockeddata
->Stride
= (((act_rect
.Width
* bitspp
+ 7) / 8) + 3) & ~3;
1102 bitmap
->bitmapbits
= heap_alloc_zero(lockeddata
->Stride
* act_rect
.Height
);
1104 if (!bitmap
->bitmapbits
)
1106 image_unlock(&bitmap
->image
, unlock
);
1110 lockeddata
->Scan0
= bitmap
->bitmapbits
;
1113 if (flags
& ImageLockModeRead
)
1115 static BOOL fixme
= FALSE
;
1117 if (!fixme
&& (PIXELFORMATBPP(bitmap
->format
) * act_rect
.X
) % 8 != 0)
1119 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1123 stat
= convert_pixels(act_rect
.Width
, act_rect
.Height
,
1124 lockeddata
->Stride
, lockeddata
->Scan0
, format
,
1126 bitmap
->bits
+ bitmap
->stride
* act_rect
.Y
+ PIXELFORMATBPP(bitmap
->format
) * act_rect
.X
/ 8,
1127 bitmap
->format
, bitmap
->image
.palette
);
1131 heap_free(bitmap
->bitmapbits
);
1132 bitmap
->bitmapbits
= NULL
;
1133 image_unlock(&bitmap
->image
, unlock
);
1138 bitmap
->lockmode
= flags
| ImageLockModeRead
;
1139 bitmap
->lockx
= act_rect
.X
;
1140 bitmap
->locky
= act_rect
.Y
;
1142 image_unlock(&bitmap
->image
, unlock
);
1146 GpStatus WINGDIPAPI
GdipBitmapSetResolution(GpBitmap
* bitmap
, REAL xdpi
, REAL ydpi
)
1148 TRACE("(%p, %.2f, %.2f)\n", bitmap
, xdpi
, ydpi
);
1150 if (!bitmap
|| xdpi
== 0.0 || ydpi
== 0.0)
1151 return InvalidParameter
;
1153 bitmap
->image
.xres
= xdpi
;
1154 bitmap
->image
.yres
= ydpi
;
1159 GpStatus WINGDIPAPI
GdipBitmapUnlockBits(GpBitmap
* bitmap
,
1160 BitmapData
* lockeddata
)
1163 static BOOL fixme
= FALSE
;
1166 TRACE("(%p,%p)\n", bitmap
, lockeddata
);
1168 if(!bitmap
|| !lockeddata
)
1169 return InvalidParameter
;
1170 if(!image_lock(&bitmap
->image
, &unlock
))
1173 if(!bitmap
->lockmode
)
1175 image_unlock(&bitmap
->image
, unlock
);
1179 if(!(lockeddata
->Reserved
& ImageLockModeWrite
)){
1180 bitmap
->lockmode
= 0;
1181 heap_free(bitmap
->bitmapbits
);
1182 bitmap
->bitmapbits
= NULL
;
1183 image_unlock(&bitmap
->image
, unlock
);
1187 if (!bitmap
->bitmapbits
&& !(lockeddata
->Reserved
& ImageLockModeUserInputBuf
))
1189 /* we passed a direct reference; no need to do anything */
1190 bitmap
->lockmode
= 0;
1191 image_unlock(&bitmap
->image
, unlock
);
1195 if (!fixme
&& (PIXELFORMATBPP(bitmap
->format
) * bitmap
->lockx
) % 8 != 0)
1197 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1201 stat
= convert_pixels(lockeddata
->Width
, lockeddata
->Height
,
1203 bitmap
->bits
+ bitmap
->stride
* bitmap
->locky
+ PIXELFORMATBPP(bitmap
->format
) * bitmap
->lockx
/ 8,
1205 lockeddata
->Stride
, lockeddata
->Scan0
, lockeddata
->PixelFormat
, NULL
);
1209 ERR("failed to convert pixels; this should never happen\n");
1212 heap_free(bitmap
->bitmapbits
);
1213 bitmap
->bitmapbits
= NULL
;
1214 bitmap
->lockmode
= 0;
1216 image_unlock(&bitmap
->image
, unlock
);
1220 GpStatus WINGDIPAPI
GdipCloneBitmapArea(REAL x
, REAL y
, REAL width
, REAL height
,
1221 PixelFormat format
, GpBitmap
* srcBitmap
, GpBitmap
** dstBitmap
)
1226 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1228 if (!srcBitmap
|| !dstBitmap
|| srcBitmap
->image
.type
!= ImageTypeBitmap
||
1230 x
+ width
> srcBitmap
->width
|| y
+ height
> srcBitmap
->height
)
1232 TRACE("<-- InvalidParameter\n");
1233 return InvalidParameter
;
1236 if (format
== PixelFormatDontCare
)
1237 format
= srcBitmap
->format
;
1239 area
.X
= gdip_round(x
);
1240 area
.Y
= gdip_round(y
);
1241 area
.Width
= gdip_round(width
);
1242 area
.Height
= gdip_round(height
);
1244 stat
= GdipCreateBitmapFromScan0(area
.Width
, area
.Height
, 0, format
, NULL
, dstBitmap
);
1247 stat
= convert_pixels(area
.Width
, area
.Height
, (*dstBitmap
)->stride
, (*dstBitmap
)->bits
, (*dstBitmap
)->format
,
1249 srcBitmap
->bits
+ srcBitmap
->stride
* area
.Y
+ PIXELFORMATBPP(srcBitmap
->format
) * area
.X
/ 8,
1250 srcBitmap
->format
, srcBitmap
->image
.palette
);
1252 if (stat
== Ok
&& srcBitmap
->image
.palette
)
1254 ColorPalette
*src_palette
, *dst_palette
;
1256 src_palette
= srcBitmap
->image
.palette
;
1258 dst_palette
= heap_alloc_zero(sizeof(UINT
) * 2 + sizeof(ARGB
) * src_palette
->Count
);
1262 dst_palette
->Flags
= src_palette
->Flags
;
1263 dst_palette
->Count
= src_palette
->Count
;
1264 memcpy(dst_palette
->Entries
, src_palette
->Entries
, sizeof(ARGB
) * src_palette
->Count
);
1266 heap_free((*dstBitmap
)->image
.palette
);
1267 (*dstBitmap
)->image
.palette
= dst_palette
;
1274 GdipDisposeImage(&(*dstBitmap
)->image
);
1283 GpStatus WINGDIPAPI
GdipCloneBitmapAreaI(INT x
, INT y
, INT width
, INT height
,
1284 PixelFormat format
, GpBitmap
* srcBitmap
, GpBitmap
** dstBitmap
)
1286 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1288 return GdipCloneBitmapArea(x
, y
, width
, height
, format
, srcBitmap
, dstBitmap
);
1291 GpStatus WINGDIPAPI
GdipCloneImage(GpImage
*image
, GpImage
**cloneImage
)
1293 TRACE("%p, %p\n", image
, cloneImage
);
1295 if (!image
|| !cloneImage
)
1296 return InvalidParameter
;
1298 if (image
->type
== ImageTypeBitmap
)
1300 GpBitmap
*bitmap
= (GpBitmap
*)image
;
1302 return GdipCloneBitmapAreaI(0, 0, bitmap
->width
, bitmap
->height
,
1303 bitmap
->format
, bitmap
, (GpBitmap
**)cloneImage
);
1305 else if (image
->type
== ImageTypeMetafile
&& ((GpMetafile
*)image
)->hemf
)
1307 GpMetafile
*result
, *metafile
;
1309 metafile
= (GpMetafile
*)image
;
1311 result
= heap_alloc_zero(sizeof(*result
));
1315 result
->image
.type
= ImageTypeMetafile
;
1316 result
->image
.format
= image
->format
;
1317 result
->image
.flags
= image
->flags
;
1318 result
->image
.frame_count
= 1;
1319 result
->image
.xres
= image
->xres
;
1320 result
->image
.yres
= image
->yres
;
1321 result
->bounds
= metafile
->bounds
;
1322 result
->unit
= metafile
->unit
;
1323 result
->metafile_type
= metafile
->metafile_type
;
1324 result
->hemf
= CopyEnhMetaFileW(metafile
->hemf
, NULL
);
1325 list_init(&result
->containers
);
1333 *cloneImage
= &result
->image
;
1338 WARN("GpImage with no image data (metafile in wrong state?)\n");
1339 return InvalidParameter
;
1343 GpStatus WINGDIPAPI
GdipCreateBitmapFromFile(GDIPCONST WCHAR
* filename
,
1349 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
1351 if(!filename
|| !bitmap
)
1352 return InvalidParameter
;
1356 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
1361 stat
= GdipCreateBitmapFromStream(stream
, bitmap
);
1363 IStream_Release(stream
);
1368 GpStatus WINGDIPAPI
GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO
* info
,
1369 VOID
*bits
, GpBitmap
**bitmap
)
1371 DWORD height
, stride
;
1374 FIXME("(%p, %p, %p) - partially implemented\n", info
, bits
, bitmap
);
1376 if (!info
|| !bits
|| !bitmap
)
1377 return InvalidParameter
;
1379 height
= abs(info
->bmiHeader
.biHeight
);
1380 stride
= ((info
->bmiHeader
.biWidth
* info
->bmiHeader
.biBitCount
+ 31) >> 3) & ~3;
1382 if(info
->bmiHeader
.biHeight
> 0) /* bottom-up */
1384 bits
= (BYTE
*)bits
+ (height
- 1) * stride
;
1388 switch(info
->bmiHeader
.biBitCount
) {
1390 format
= PixelFormat1bppIndexed
;
1393 format
= PixelFormat4bppIndexed
;
1396 format
= PixelFormat8bppIndexed
;
1399 format
= PixelFormat16bppRGB555
;
1402 format
= PixelFormat24bppRGB
;
1405 format
= PixelFormat32bppRGB
;
1408 FIXME("don't know how to handle %d bpp\n", info
->bmiHeader
.biBitCount
);
1410 return InvalidParameter
;
1413 return GdipCreateBitmapFromScan0(info
->bmiHeader
.biWidth
, height
, stride
, format
,
1419 GpStatus WINGDIPAPI
GdipCreateBitmapFromFileICM(GDIPCONST WCHAR
* filename
,
1422 TRACE("(%s) %p\n", debugstr_w(filename
), bitmap
);
1424 return GdipCreateBitmapFromFile(filename
, bitmap
);
1427 GpStatus WINGDIPAPI
GdipCreateBitmapFromResource(HINSTANCE hInstance
,
1428 GDIPCONST WCHAR
* lpBitmapName
, GpBitmap
** bitmap
)
1431 GpStatus stat
= InvalidParameter
;
1433 TRACE("%p (%s) %p\n", hInstance
, debugstr_w(lpBitmapName
), bitmap
);
1435 if(!lpBitmapName
|| !bitmap
)
1436 return InvalidParameter
;
1439 hbm
= LoadImageW(hInstance
, lpBitmapName
, IMAGE_BITMAP
, 0, 0,
1440 LR_CREATEDIBSECTION
);
1443 stat
= GdipCreateBitmapFromHBITMAP(hbm
, NULL
, bitmap
);
1450 static inline DWORD
blend_argb_no_bkgnd_alpha(DWORD src
, DWORD bkgnd
)
1453 BYTE g
= (BYTE
)(src
>> 8);
1454 BYTE r
= (BYTE
)(src
>> 16);
1455 DWORD alpha
= (BYTE
)(src
>> 24);
1456 return ((b
+ ((BYTE
)bkgnd
* (255 - alpha
) + 127) / 255) |
1457 (g
+ ((BYTE
)(bkgnd
>> 8) * (255 - alpha
) + 127) / 255) << 8 |
1458 (r
+ ((BYTE
)(bkgnd
>> 16) * (255 - alpha
) + 127) / 255) << 16 |
1462 GpStatus WINGDIPAPI
GdipCreateHBITMAPFromBitmap(GpBitmap
* bitmap
,
1463 HBITMAP
* hbmReturn
, ARGB background
)
1468 BITMAPINFOHEADER bih
;
1472 TRACE("(%p,%p,%x)\n", bitmap
, hbmReturn
, background
);
1474 if (!bitmap
|| !hbmReturn
) return InvalidParameter
;
1475 if (!image_lock(&bitmap
->image
, &unlock
)) return ObjectBusy
;
1477 GdipGetImageWidth(&bitmap
->image
, &width
);
1478 GdipGetImageHeight(&bitmap
->image
, &height
);
1480 bih
.biSize
= sizeof(bih
);
1481 bih
.biWidth
= width
;
1482 bih
.biHeight
= height
;
1484 bih
.biBitCount
= 32;
1485 bih
.biCompression
= BI_RGB
;
1486 bih
.biSizeImage
= 0;
1487 bih
.biXPelsPerMeter
= 0;
1488 bih
.biYPelsPerMeter
= 0;
1490 bih
.biClrImportant
= 0;
1492 result
= CreateDIBSection(0, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
, (void**)&bits
, NULL
, 0);
1495 image_unlock(&bitmap
->image
, unlock
);
1496 return GenericError
;
1499 stat
= convert_pixels(width
, height
, -width
*4,
1500 bits
+ (width
* 4 * (height
- 1)), PixelFormat32bppPARGB
,
1501 bitmap
->stride
, bitmap
->bits
, bitmap
->format
, bitmap
->image
.palette
);
1504 DeleteObject(result
);
1505 image_unlock(&bitmap
->image
, unlock
);
1509 if (background
& 0xffffff)
1513 for (ptr
= (DWORD
*)bits
, i
= 0; i
< width
* height
; ptr
++, i
++)
1515 if ((*ptr
& 0xff000000) == 0xff000000) continue;
1516 *ptr
= blend_argb_no_bkgnd_alpha(*ptr
, background
);
1520 *hbmReturn
= result
;
1521 image_unlock(&bitmap
->image
, unlock
);
1525 GpStatus WINGDIPAPI
GdipCreateBitmapFromGraphics(INT width
, INT height
,
1526 GpGraphics
* target
, GpBitmap
** bitmap
)
1530 TRACE("(%d, %d, %p, %p)\n", width
, height
, target
, bitmap
);
1532 if(!target
|| !bitmap
)
1533 return InvalidParameter
;
1535 ret
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat32bppPARGB
,
1540 GdipGetDpiX(target
, &(*bitmap
)->image
.xres
);
1541 GdipGetDpiY(target
, &(*bitmap
)->image
.yres
);
1547 GpStatus WINGDIPAPI
GdipCreateBitmapFromHICON(HICON hicon
, GpBitmap
** bitmap
)
1553 UINT width
, height
, stride
;
1555 BitmapData lockeddata
;
1559 BITMAPINFOHEADER bih
;
1564 TRACE("%p, %p\n", hicon
, bitmap
);
1566 if(!bitmap
|| !GetIconInfo(hicon
, &iinfo
))
1567 return InvalidParameter
;
1569 /* get the size of the icon */
1570 ret
= GetObjectA(iinfo
.hbmColor
? iinfo
.hbmColor
: iinfo
.hbmMask
, sizeof(bm
), &bm
);
1572 DeleteObject(iinfo
.hbmColor
);
1573 DeleteObject(iinfo
.hbmMask
);
1574 return GenericError
;
1578 height
= iinfo
.hbmColor
? abs(bm
.bmHeight
) : abs(bm
.bmHeight
) / 2;
1581 stat
= GdipCreateBitmapFromScan0(width
, height
, stride
, PixelFormat32bppARGB
, NULL
, bitmap
);
1583 DeleteObject(iinfo
.hbmColor
);
1584 DeleteObject(iinfo
.hbmMask
);
1591 rect
.Height
= height
;
1593 stat
= GdipBitmapLockBits(*bitmap
, &rect
, ImageLockModeWrite
, PixelFormat32bppARGB
, &lockeddata
);
1595 DeleteObject(iinfo
.hbmColor
);
1596 DeleteObject(iinfo
.hbmMask
);
1597 GdipDisposeImage(&(*bitmap
)->image
);
1601 bih
.biSize
= sizeof(bih
);
1602 bih
.biWidth
= width
;
1603 bih
.biHeight
= iinfo
.hbmColor
? -height
: -height
* 2;
1605 bih
.biBitCount
= 32;
1606 bih
.biCompression
= BI_RGB
;
1607 bih
.biSizeImage
= 0;
1608 bih
.biXPelsPerMeter
= 0;
1609 bih
.biYPelsPerMeter
= 0;
1611 bih
.biClrImportant
= 0;
1613 screendc
= CreateCompatibleDC(0);
1616 GetDIBits(screendc
, iinfo
.hbmColor
, 0, height
, lockeddata
.Scan0
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1618 if (bm
.bmBitsPixel
== 32)
1622 /* If any pixel has a non-zero alpha, ignore hbmMask */
1623 src
= (DWORD
*)lockeddata
.Scan0
;
1624 for (x
=0; x
<width
&& !has_alpha
; x
++)
1625 for (y
=0; y
<height
&& !has_alpha
; y
++)
1626 if ((*src
++ & 0xff000000) != 0)
1629 else has_alpha
= FALSE
;
1633 GetDIBits(screendc
, iinfo
.hbmMask
, 0, height
, lockeddata
.Scan0
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1641 BYTE
*bits
= heap_alloc(height
* stride
);
1643 /* read alpha data from the mask */
1645 GetDIBits(screendc
, iinfo
.hbmMask
, 0, height
, bits
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1647 GetDIBits(screendc
, iinfo
.hbmMask
, height
, height
, bits
, (BITMAPINFO
*)&bih
, DIB_RGB_COLORS
);
1650 dst_row
= lockeddata
.Scan0
;
1651 for (y
=0; y
<height
; y
++)
1653 dst
= (DWORD
*)dst_row
;
1654 for (x
=0; x
<height
; x
++)
1656 DWORD src_value
= *src
++;
1660 *dst
++ |= 0xff000000;
1662 dst_row
+= lockeddata
.Stride
;
1669 /* set constant alpha of 255 */
1670 dst_row
= lockeddata
.Scan0
;
1671 for (y
=0; y
<height
; y
++)
1673 dst
= (DWORD
*)dst_row
;
1674 for (x
=0; x
<height
; x
++)
1675 *dst
++ |= 0xff000000;
1676 dst_row
+= lockeddata
.Stride
;
1683 DeleteObject(iinfo
.hbmColor
);
1684 DeleteObject(iinfo
.hbmMask
);
1686 GdipBitmapUnlockBits(*bitmap
, &lockeddata
);
1691 static void generate_halftone_palette(ARGB
*entries
, UINT count
)
1693 static const BYTE halftone_values
[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1696 for (i
=0; i
<8 && i
<count
; i
++)
1698 entries
[i
] = 0xff000000;
1699 if (i
&1) entries
[i
] |= 0x800000;
1700 if (i
&2) entries
[i
] |= 0x8000;
1701 if (i
&4) entries
[i
] |= 0x80;
1705 entries
[i
] = 0xffc0c0c0;
1707 for (i
=9; i
<16 && i
<count
; i
++)
1709 entries
[i
] = 0xff000000;
1710 if (i
&1) entries
[i
] |= 0xff0000;
1711 if (i
&2) entries
[i
] |= 0xff00;
1712 if (i
&4) entries
[i
] |= 0xff;
1715 for (i
=16; i
<40 && i
<count
; i
++)
1720 for (i
=40; i
<256 && i
<count
; i
++)
1722 entries
[i
] = 0xff000000;
1723 entries
[i
] |= halftone_values
[(i
-40)%6];
1724 entries
[i
] |= halftone_values
[((i
-40)/6)%6] << 8;
1725 entries
[i
] |= halftone_values
[((i
-40)/36)%6] << 16;
1729 static GpStatus
get_screen_resolution(REAL
*xres
, REAL
*yres
)
1731 HDC screendc
= CreateCompatibleDC(0);
1733 if (!screendc
) return GenericError
;
1735 *xres
= (REAL
)GetDeviceCaps(screendc
, LOGPIXELSX
);
1736 *yres
= (REAL
)GetDeviceCaps(screendc
, LOGPIXELSY
);
1743 GpStatus WINGDIPAPI
GdipCreateBitmapFromScan0(INT width
, INT height
, INT stride
,
1744 PixelFormat format
, BYTE
* scan0
, GpBitmap
** bitmap
)
1746 HBITMAP hbitmap
=NULL
;
1747 INT row_size
, dib_stride
;
1748 BYTE
*bits
=NULL
, *own_bits
=NULL
;
1752 TRACE("%d %d %d 0x%x %p %p\n", width
, height
, stride
, format
, scan0
, bitmap
);
1754 if (!bitmap
) return InvalidParameter
;
1756 if(width
<= 0 || height
<= 0 || (scan0
&& (stride
% 4))){
1758 return InvalidParameter
;
1761 if(scan0
&& !stride
)
1762 return InvalidParameter
;
1764 stat
= get_screen_resolution(&xres
, &yres
);
1765 if (stat
!= Ok
) return stat
;
1767 row_size
= (width
* PIXELFORMATBPP(format
)+7) / 8;
1768 dib_stride
= (row_size
+ 3) & ~3;
1771 stride
= dib_stride
;
1773 if (format
& PixelFormatGDI
&& !(format
& (PixelFormatAlpha
|PixelFormatIndexed
)) && !scan0
)
1775 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
) + 256 * sizeof(RGBQUAD
)];
1776 BITMAPINFO
*pbmi
= (BITMAPINFO
*)bmibuf
;
1778 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1779 pbmi
->bmiHeader
.biWidth
= width
;
1780 pbmi
->bmiHeader
.biHeight
= -height
;
1781 pbmi
->bmiHeader
.biPlanes
= 1;
1782 /* FIXME: use the rest of the data from format */
1783 pbmi
->bmiHeader
.biBitCount
= PIXELFORMATBPP(format
);
1784 pbmi
->bmiHeader
.biCompression
= BI_RGB
;
1785 pbmi
->bmiHeader
.biSizeImage
= 0;
1786 pbmi
->bmiHeader
.biXPelsPerMeter
= 0;
1787 pbmi
->bmiHeader
.biYPelsPerMeter
= 0;
1788 pbmi
->bmiHeader
.biClrUsed
= 0;
1789 pbmi
->bmiHeader
.biClrImportant
= 0;
1791 hbitmap
= CreateDIBSection(0, pbmi
, DIB_RGB_COLORS
, (void**)&bits
, NULL
, 0);
1793 if (!hbitmap
) return GenericError
;
1795 stride
= dib_stride
;
1799 /* Not a GDI format; don't try to make an HBITMAP. */
1804 INT size
= abs(stride
) * height
;
1806 own_bits
= bits
= heap_alloc_zero(size
);
1807 if (!own_bits
) return OutOfMemory
;
1810 bits
+= stride
* (1 - height
);
1814 *bitmap
= heap_alloc_zero(sizeof(GpBitmap
));
1817 DeleteObject(hbitmap
);
1818 heap_free(own_bits
);
1822 (*bitmap
)->image
.type
= ImageTypeBitmap
;
1823 memcpy(&(*bitmap
)->image
.format
, &ImageFormatMemoryBMP
, sizeof(GUID
));
1824 (*bitmap
)->image
.flags
= ImageFlagsNone
;
1825 (*bitmap
)->image
.frame_count
= 1;
1826 (*bitmap
)->image
.current_frame
= 0;
1827 (*bitmap
)->image
.palette
= NULL
;
1828 (*bitmap
)->image
.xres
= xres
;
1829 (*bitmap
)->image
.yres
= yres
;
1830 (*bitmap
)->width
= width
;
1831 (*bitmap
)->height
= height
;
1832 (*bitmap
)->format
= format
;
1833 (*bitmap
)->image
.decoder
= NULL
;
1834 (*bitmap
)->hbitmap
= hbitmap
;
1835 (*bitmap
)->hdc
= NULL
;
1836 (*bitmap
)->bits
= bits
;
1837 (*bitmap
)->stride
= stride
;
1838 (*bitmap
)->own_bits
= own_bits
;
1839 (*bitmap
)->metadata_reader
= NULL
;
1840 (*bitmap
)->prop_count
= 0;
1841 (*bitmap
)->prop_item
= NULL
;
1843 /* set format-related flags */
1844 if (format
& (PixelFormatAlpha
|PixelFormatPAlpha
|PixelFormatIndexed
))
1845 (*bitmap
)->image
.flags
|= ImageFlagsHasAlpha
;
1847 if (format
== PixelFormat1bppIndexed
||
1848 format
== PixelFormat4bppIndexed
||
1849 format
== PixelFormat8bppIndexed
)
1851 (*bitmap
)->image
.palette
= heap_alloc_zero(sizeof(UINT
) * 2 + sizeof(ARGB
) * (1 << PIXELFORMATBPP(format
)));
1853 if (!(*bitmap
)->image
.palette
)
1855 GdipDisposeImage(&(*bitmap
)->image
);
1860 (*bitmap
)->image
.palette
->Count
= 1 << PIXELFORMATBPP(format
);
1862 if (format
== PixelFormat1bppIndexed
)
1864 (*bitmap
)->image
.palette
->Flags
= PaletteFlagsGrayScale
;
1865 (*bitmap
)->image
.palette
->Entries
[0] = 0xff000000;
1866 (*bitmap
)->image
.palette
->Entries
[1] = 0xffffffff;
1870 if (format
== PixelFormat8bppIndexed
)
1871 (*bitmap
)->image
.palette
->Flags
= PaletteFlagsHalftone
;
1873 generate_halftone_palette((*bitmap
)->image
.palette
->Entries
,
1874 (*bitmap
)->image
.palette
->Count
);
1878 TRACE("<-- %p\n", *bitmap
);
1883 GpStatus WINGDIPAPI
GdipCreateBitmapFromStream(IStream
* stream
,
1888 TRACE("%p %p\n", stream
, bitmap
);
1890 stat
= GdipLoadImageFromStream(stream
, (GpImage
**) bitmap
);
1895 if((*bitmap
)->image
.type
!= ImageTypeBitmap
){
1896 GdipDisposeImage(&(*bitmap
)->image
);
1898 return GenericError
; /* FIXME: what error to return? */
1905 GpStatus WINGDIPAPI
GdipCreateBitmapFromStreamICM(IStream
* stream
,
1908 TRACE("%p %p\n", stream
, bitmap
);
1910 return GdipCreateBitmapFromStream(stream
, bitmap
);
1913 GpStatus WINGDIPAPI
GdipCreateCachedBitmap(GpBitmap
*bitmap
, GpGraphics
*graphics
,
1914 GpCachedBitmap
**cachedbmp
)
1918 TRACE("%p %p %p\n", bitmap
, graphics
, cachedbmp
);
1920 if(!bitmap
|| !graphics
|| !cachedbmp
)
1921 return InvalidParameter
;
1923 *cachedbmp
= heap_alloc_zero(sizeof(GpCachedBitmap
));
1927 stat
= GdipCloneImage(&(bitmap
->image
), &(*cachedbmp
)->image
);
1929 heap_free(*cachedbmp
);
1936 GpStatus WINGDIPAPI
GdipCreateHICONFromBitmap(GpBitmap
*bitmap
, HICON
*hicon
)
1939 BitmapData lockeddata
;
1940 ULONG andstride
, xorstride
, bitssize
;
1941 LPBYTE andbits
, xorbits
, androw
, xorrow
, srcrow
;
1944 TRACE("(%p, %p)\n", bitmap
, hicon
);
1946 if (!bitmap
|| !hicon
)
1947 return InvalidParameter
;
1949 stat
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
,
1950 PixelFormat32bppPARGB
, &lockeddata
);
1953 andstride
= ((lockeddata
.Width
+31)/32)*4;
1954 xorstride
= lockeddata
.Width
*4;
1955 bitssize
= (andstride
+ xorstride
) * lockeddata
.Height
;
1957 andbits
= heap_alloc_zero(bitssize
);
1961 xorbits
= andbits
+ andstride
* lockeddata
.Height
;
1963 for (y
=0; y
<lockeddata
.Height
; y
++)
1965 srcrow
= ((LPBYTE
)lockeddata
.Scan0
) + lockeddata
.Stride
* y
;
1967 androw
= andbits
+ andstride
* y
;
1968 for (x
=0; x
<lockeddata
.Width
; x
++)
1969 if (srcrow
[3+4*x
] >= 128)
1970 androw
[x
/8] |= 1 << (7-x
%8);
1972 xorrow
= xorbits
+ xorstride
* y
;
1973 memcpy(xorrow
, srcrow
, xorstride
);
1976 *hicon
= CreateIcon(NULL
, lockeddata
.Width
, lockeddata
.Height
, 1, 32,
1984 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
1990 GpStatus WINGDIPAPI
GdipDeleteCachedBitmap(GpCachedBitmap
*cachedbmp
)
1992 TRACE("%p\n", cachedbmp
);
1995 return InvalidParameter
;
1997 GdipDisposeImage(cachedbmp
->image
);
1998 heap_free(cachedbmp
);
2003 GpStatus WINGDIPAPI
GdipDrawCachedBitmap(GpGraphics
*graphics
,
2004 GpCachedBitmap
*cachedbmp
, INT x
, INT y
)
2006 TRACE("%p %p %d %d\n", graphics
, cachedbmp
, x
, y
);
2008 if(!graphics
|| !cachedbmp
)
2009 return InvalidParameter
;
2011 return GdipDrawImage(graphics
, cachedbmp
->image
, (REAL
)x
, (REAL
)y
);
2014 /* Internal utility function: Replace the image data of dst with that of src,
2016 static void move_bitmap(GpBitmap
*dst
, GpBitmap
*src
, BOOL clobber_palette
)
2018 assert(src
->image
.type
== ImageTypeBitmap
);
2019 assert(dst
->image
.type
== ImageTypeBitmap
);
2021 heap_free(dst
->bitmapbits
);
2022 heap_free(dst
->own_bits
);
2024 DeleteObject(dst
->hbitmap
);
2026 if (clobber_palette
)
2028 heap_free(dst
->image
.palette
);
2029 dst
->image
.palette
= src
->image
.palette
;
2032 heap_free(src
->image
.palette
);
2034 dst
->image
.xres
= src
->image
.xres
;
2035 dst
->image
.yres
= src
->image
.yres
;
2036 dst
->width
= src
->width
;
2037 dst
->height
= src
->height
;
2038 dst
->format
= src
->format
;
2039 dst
->hbitmap
= src
->hbitmap
;
2040 dst
->hdc
= src
->hdc
;
2041 dst
->bits
= src
->bits
;
2042 dst
->stride
= src
->stride
;
2043 dst
->own_bits
= src
->own_bits
;
2044 if (dst
->metadata_reader
)
2045 IWICMetadataReader_Release(dst
->metadata_reader
);
2046 dst
->metadata_reader
= src
->metadata_reader
;
2047 heap_free(dst
->prop_item
);
2048 dst
->prop_item
= src
->prop_item
;
2049 dst
->prop_count
= src
->prop_count
;
2050 if (dst
->image
.decoder
)
2051 IWICBitmapDecoder_Release(dst
->image
.decoder
);
2052 dst
->image
.decoder
= src
->image
.decoder
;
2053 dst
->image
.frame_count
= src
->image
.frame_count
;
2054 dst
->image
.current_frame
= src
->image
.current_frame
;
2055 dst
->image
.format
= src
->image
.format
;
2057 src
->image
.type
= ~0;
2061 static GpStatus
free_image_data(GpImage
*image
)
2064 return InvalidParameter
;
2066 if (image
->type
== ImageTypeBitmap
)
2068 heap_free(((GpBitmap
*)image
)->bitmapbits
);
2069 heap_free(((GpBitmap
*)image
)->own_bits
);
2070 DeleteDC(((GpBitmap
*)image
)->hdc
);
2071 DeleteObject(((GpBitmap
*)image
)->hbitmap
);
2072 if (((GpBitmap
*)image
)->metadata_reader
)
2073 IWICMetadataReader_Release(((GpBitmap
*)image
)->metadata_reader
);
2074 heap_free(((GpBitmap
*)image
)->prop_item
);
2076 else if (image
->type
== ImageTypeMetafile
)
2078 GpMetafile
*metafile
= (GpMetafile
*)image
;
2079 heap_free(metafile
->comment_data
);
2080 DeleteEnhMetaFile(CloseEnhMetaFile(metafile
->record_dc
));
2081 if (!metafile
->preserve_hemf
)
2082 DeleteEnhMetaFile(metafile
->hemf
);
2083 if (metafile
->record_graphics
)
2085 WARN("metafile closed while recording\n");
2086 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2087 metafile
->record_graphics
->image
= NULL
;
2088 metafile
->record_graphics
->busy
= TRUE
;
2090 if (metafile
->record_stream
)
2092 IStream_Release(metafile
->record_stream
);
2097 WARN("invalid image: %p\n", image
);
2101 IWICBitmapDecoder_Release(image
->decoder
);
2102 heap_free(image
->palette
);
2107 GpStatus WINGDIPAPI
GdipDisposeImage(GpImage
*image
)
2111 TRACE("%p\n", image
);
2113 status
= free_image_data(image
);
2114 if (status
!= Ok
) return status
;
2121 GpStatus WINGDIPAPI
GdipFindFirstImageItem(GpImage
*image
, ImageItemData
* item
)
2125 TRACE("(%p,%p)\n", image
, item
);
2128 return InvalidParameter
;
2131 FIXME("not implemented\n");
2133 return NotImplemented
;
2136 GpStatus WINGDIPAPI
GdipGetImageItemData(GpImage
*image
, ImageItemData
*item
)
2140 TRACE("(%p,%p)\n", image
, item
);
2143 FIXME("not implemented\n");
2145 return NotImplemented
;
2148 GpStatus WINGDIPAPI
GdipGetImageBounds(GpImage
*image
, GpRectF
*srcRect
,
2151 TRACE("%p %p %p\n", image
, srcRect
, srcUnit
);
2153 if(!image
|| !srcRect
|| !srcUnit
)
2154 return InvalidParameter
;
2155 if(image
->type
== ImageTypeMetafile
){
2156 *srcRect
= ((GpMetafile
*)image
)->bounds
;
2157 *srcUnit
= ((GpMetafile
*)image
)->unit
;
2159 else if(image
->type
== ImageTypeBitmap
){
2160 srcRect
->X
= srcRect
->Y
= 0.0;
2161 srcRect
->Width
= (REAL
) ((GpBitmap
*)image
)->width
;
2162 srcRect
->Height
= (REAL
) ((GpBitmap
*)image
)->height
;
2163 *srcUnit
= UnitPixel
;
2166 WARN("GpImage with no image data\n");
2167 return InvalidParameter
;
2170 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect
->X
, srcRect
->Y
,
2171 srcRect
->Width
, srcRect
->Height
, *srcUnit
);
2176 GpStatus WINGDIPAPI
GdipGetImageDimension(GpImage
*image
, REAL
*width
,
2179 TRACE("%p %p %p\n", image
, width
, height
);
2181 if(!image
|| !height
|| !width
)
2182 return InvalidParameter
;
2184 if(image
->type
== ImageTypeMetafile
){
2185 *height
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Height
, ((GpMetafile
*)image
)->unit
, image
->yres
);
2186 *width
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Width
, ((GpMetafile
*)image
)->unit
, image
->xres
);
2188 else if(image
->type
== ImageTypeBitmap
){
2189 *height
= ((GpBitmap
*)image
)->height
;
2190 *width
= ((GpBitmap
*)image
)->width
;
2193 WARN("GpImage with no image data\n");
2194 return InvalidParameter
;
2197 TRACE("returning (%f, %f)\n", *height
, *width
);
2201 GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage
*image
,
2202 GpGraphics
**graphics
)
2207 TRACE("%p %p\n", image
, graphics
);
2209 if(!image
|| !graphics
)
2210 return InvalidParameter
;
2212 if (image
->type
== ImageTypeBitmap
&& ((GpBitmap
*)image
)->hbitmap
)
2214 hdc
= ((GpBitmap
*)image
)->hdc
;
2217 hdc
= CreateCompatibleDC(0);
2218 SelectObject(hdc
, ((GpBitmap
*)image
)->hbitmap
);
2219 ((GpBitmap
*)image
)->hdc
= hdc
;
2222 stat
= GdipCreateFromHDC(hdc
, graphics
);
2226 (*graphics
)->image
= image
;
2227 (*graphics
)->xres
= image
->xres
;
2228 (*graphics
)->yres
= image
->yres
;
2231 else if (image
->type
== ImageTypeMetafile
)
2232 stat
= METAFILE_GetGraphicsContext((GpMetafile
*)image
, graphics
);
2234 stat
= graphics_from_image(image
, graphics
);
2239 GpStatus WINGDIPAPI
GdipGetImageHeight(GpImage
*image
, UINT
*height
)
2241 TRACE("%p %p\n", image
, height
);
2243 if(!image
|| !height
)
2244 return InvalidParameter
;
2246 if(image
->type
== ImageTypeMetafile
)
2247 *height
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Height
, ((GpMetafile
*)image
)->unit
, image
->yres
);
2248 else if(image
->type
== ImageTypeBitmap
)
2249 *height
= ((GpBitmap
*)image
)->height
;
2252 WARN("GpImage with no image data\n");
2253 return InvalidParameter
;
2256 TRACE("returning %d\n", *height
);
2261 GpStatus WINGDIPAPI
GdipGetImageHorizontalResolution(GpImage
*image
, REAL
*res
)
2264 return InvalidParameter
;
2268 TRACE("(%p) <-- %0.2f\n", image
, *res
);
2273 GpStatus WINGDIPAPI
GdipGetImagePaletteSize(GpImage
*image
, INT
*size
)
2275 TRACE("%p %p\n", image
, size
);
2278 return InvalidParameter
;
2280 if (!image
->palette
|| image
->palette
->Count
== 0)
2281 *size
= sizeof(ColorPalette
);
2283 *size
= sizeof(UINT
)*2 + sizeof(ARGB
)*image
->palette
->Count
;
2285 TRACE("<-- %u\n", *size
);
2290 /* FIXME: test this function for non-bitmap types */
2291 GpStatus WINGDIPAPI
GdipGetImagePixelFormat(GpImage
*image
, PixelFormat
*format
)
2293 TRACE("%p %p\n", image
, format
);
2295 if(!image
|| !format
)
2296 return InvalidParameter
;
2298 if(image
->type
!= ImageTypeBitmap
)
2299 *format
= PixelFormat24bppRGB
;
2301 *format
= ((GpBitmap
*) image
)->format
;
2306 GpStatus WINGDIPAPI
GdipGetImageRawFormat(GpImage
*image
, GUID
*format
)
2308 TRACE("(%p, %p)\n", image
, format
);
2310 if(!image
|| !format
)
2311 return InvalidParameter
;
2313 memcpy(format
, &image
->format
, sizeof(GUID
));
2318 GpStatus WINGDIPAPI
GdipGetImageType(GpImage
*image
, ImageType
*type
)
2320 TRACE("%p %p\n", image
, type
);
2323 return InvalidParameter
;
2325 *type
= image
->type
;
2330 GpStatus WINGDIPAPI
GdipGetImageVerticalResolution(GpImage
*image
, REAL
*res
)
2333 return InvalidParameter
;
2337 TRACE("(%p) <-- %0.2f\n", image
, *res
);
2342 GpStatus WINGDIPAPI
GdipGetImageWidth(GpImage
*image
, UINT
*width
)
2344 TRACE("%p %p\n", image
, width
);
2346 if(!image
|| !width
)
2347 return InvalidParameter
;
2349 if(image
->type
== ImageTypeMetafile
)
2350 *width
= units_to_pixels(((GpMetafile
*)image
)->bounds
.Width
, ((GpMetafile
*)image
)->unit
, image
->xres
);
2351 else if(image
->type
== ImageTypeBitmap
)
2352 *width
= ((GpBitmap
*)image
)->width
;
2355 WARN("GpImage with no image data\n");
2356 return InvalidParameter
;
2359 TRACE("returning %d\n", *width
);
2364 GpStatus WINGDIPAPI
GdipGetPropertyCount(GpImage
*image
, UINT
*num
)
2366 TRACE("(%p, %p)\n", image
, num
);
2368 if (!image
|| !num
) return InvalidParameter
;
2372 if (image
->type
== ImageTypeBitmap
)
2374 if (((GpBitmap
*)image
)->prop_item
)
2376 *num
= ((GpBitmap
*)image
)->prop_count
;
2380 if (((GpBitmap
*)image
)->metadata_reader
)
2381 IWICMetadataReader_GetCount(((GpBitmap
*)image
)->metadata_reader
, num
);
2387 GpStatus WINGDIPAPI
GdipGetPropertyIdList(GpImage
*image
, UINT num
, PROPID
*list
)
2390 IWICMetadataReader
*reader
;
2391 IWICEnumMetadataItem
*enumerator
;
2392 UINT prop_count
, i
, items_returned
;
2394 TRACE("(%p, %u, %p)\n", image
, num
, list
);
2396 if (!image
|| !list
) return InvalidParameter
;
2398 if (image
->type
!= ImageTypeBitmap
)
2400 FIXME("Not implemented for type %d\n", image
->type
);
2401 return NotImplemented
;
2404 if (((GpBitmap
*)image
)->prop_item
)
2406 if (num
!= ((GpBitmap
*)image
)->prop_count
) return InvalidParameter
;
2408 for (i
= 0; i
< num
; i
++)
2410 list
[i
] = ((GpBitmap
*)image
)->prop_item
[i
].id
;
2416 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2419 if (num
!= 0) return InvalidParameter
;
2423 hr
= IWICMetadataReader_GetCount(reader
, &prop_count
);
2424 if (FAILED(hr
)) return hresult_to_status(hr
);
2426 if (num
!= prop_count
) return InvalidParameter
;
2428 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2429 if (FAILED(hr
)) return hresult_to_status(hr
);
2431 IWICEnumMetadataItem_Reset(enumerator
);
2433 for (i
= 0; i
< num
; i
++)
2437 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, NULL
, &items_returned
);
2438 if (hr
!= S_OK
) break;
2440 if (id
.vt
!= VT_UI2
)
2442 FIXME("not supported propvariant type for id: %u\n", id
.vt
);
2446 list
[i
] = id
.u
.uiVal
;
2449 IWICEnumMetadataItem_Release(enumerator
);
2451 return hr
== S_OK
? Ok
: hresult_to_status(hr
);
2454 static UINT
propvariant_size(PROPVARIANT
*value
)
2456 switch (value
->vt
& ~VT_VECTOR
)
2462 if (!(value
->vt
& VT_VECTOR
)) return 1;
2463 return value
->u
.caub
.cElems
;
2466 if (!(value
->vt
& VT_VECTOR
)) return 2;
2467 return value
->u
.caui
.cElems
* 2;
2471 if (!(value
->vt
& VT_VECTOR
)) return 4;
2472 return value
->u
.caul
.cElems
* 4;
2476 if (!(value
->vt
& VT_VECTOR
)) return 8;
2477 return value
->u
.cauh
.cElems
* 8;
2479 return value
->u
.pszVal
? strlen(value
->u
.pszVal
) + 1 : 0;
2481 return value
->u
.blob
.cbSize
;
2483 FIXME("not supported variant type %d\n", value
->vt
);
2488 GpStatus WINGDIPAPI
GdipGetPropertyItemSize(GpImage
*image
, PROPID propid
, UINT
*size
)
2491 IWICMetadataReader
*reader
;
2492 PROPVARIANT id
, value
;
2494 TRACE("(%p,%#x,%p)\n", image
, propid
, size
);
2496 if (!size
|| !image
) return InvalidParameter
;
2498 if (image
->type
!= ImageTypeBitmap
)
2500 FIXME("Not implemented for type %d\n", image
->type
);
2501 return NotImplemented
;
2504 if (((GpBitmap
*)image
)->prop_item
)
2508 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2510 if (propid
== ((GpBitmap
*)image
)->prop_item
[i
].id
)
2512 *size
= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
;
2517 return PropertyNotFound
;
2520 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2521 if (!reader
) return PropertyNotFound
;
2524 id
.u
.uiVal
= propid
;
2525 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
2526 if (FAILED(hr
)) return PropertyNotFound
;
2528 *size
= propvariant_size(&value
);
2529 if (*size
) *size
+= sizeof(PropertyItem
);
2530 PropVariantClear(&value
);
2535 #ifndef PropertyTagTypeSByte
2536 #define PropertyTagTypeSByte 6
2537 #define PropertyTagTypeSShort 8
2538 #define PropertyTagTypeFloat 11
2539 #define PropertyTagTypeDouble 12
2542 static UINT
vt_to_itemtype(UINT vt
)
2549 { VT_I1
, PropertyTagTypeSByte
},
2550 { VT_UI1
, PropertyTagTypeByte
},
2551 { VT_I2
, PropertyTagTypeSShort
},
2552 { VT_UI2
, PropertyTagTypeShort
},
2553 { VT_I4
, PropertyTagTypeSLONG
},
2554 { VT_UI4
, PropertyTagTypeLong
},
2555 { VT_I8
, PropertyTagTypeSRational
},
2556 { VT_UI8
, PropertyTagTypeRational
},
2557 { VT_R4
, PropertyTagTypeFloat
},
2558 { VT_R8
, PropertyTagTypeDouble
},
2559 { VT_LPSTR
, PropertyTagTypeASCII
},
2560 { VT_BLOB
, PropertyTagTypeUndefined
}
2563 for (i
= 0; i
< sizeof(vt2type
)/sizeof(vt2type
[0]); i
++)
2565 if (vt2type
[i
].vt
== vt
) return vt2type
[i
].type
;
2567 FIXME("not supported variant type %u\n", vt
);
2571 static GpStatus
propvariant_to_item(PROPVARIANT
*value
, PropertyItem
*item
,
2572 UINT size
, PROPID id
)
2574 UINT item_size
, item_type
;
2576 item_size
= propvariant_size(value
);
2577 if (size
!= item_size
+ sizeof(PropertyItem
)) return InvalidParameter
;
2579 item_type
= vt_to_itemtype(value
->vt
& ~VT_VECTOR
);
2580 if (!item_type
) return InvalidParameter
;
2582 item
->value
= item
+ 1;
2584 switch (value
->vt
& ~VT_VECTOR
)
2588 if (!(value
->vt
& VT_VECTOR
))
2589 *(BYTE
*)item
->value
= value
->u
.bVal
;
2591 memcpy(item
->value
, value
->u
.caub
.pElems
, item_size
);
2595 if (!(value
->vt
& VT_VECTOR
))
2596 *(USHORT
*)item
->value
= value
->u
.uiVal
;
2598 memcpy(item
->value
, value
->u
.caui
.pElems
, item_size
);
2603 if (!(value
->vt
& VT_VECTOR
))
2604 *(ULONG
*)item
->value
= value
->u
.ulVal
;
2606 memcpy(item
->value
, value
->u
.caul
.pElems
, item_size
);
2611 if (!(value
->vt
& VT_VECTOR
))
2612 *(ULONGLONG
*)item
->value
= value
->u
.uhVal
.QuadPart
;
2614 memcpy(item
->value
, value
->u
.cauh
.pElems
, item_size
);
2617 memcpy(item
->value
, value
->u
.pszVal
, item_size
);
2620 memcpy(item
->value
, value
->u
.blob
.pBlobData
, item_size
);
2623 FIXME("not supported variant type %d\n", value
->vt
);
2624 return InvalidParameter
;
2627 item
->length
= item_size
;
2628 item
->type
= item_type
;
2634 GpStatus WINGDIPAPI
GdipGetPropertyItem(GpImage
*image
, PROPID propid
, UINT size
,
2635 PropertyItem
*buffer
)
2639 IWICMetadataReader
*reader
;
2640 PROPVARIANT id
, value
;
2642 TRACE("(%p,%#x,%u,%p)\n", image
, propid
, size
, buffer
);
2644 if (!image
|| !buffer
) return InvalidParameter
;
2646 if (image
->type
!= ImageTypeBitmap
)
2648 FIXME("Not implemented for type %d\n", image
->type
);
2649 return NotImplemented
;
2652 if (((GpBitmap
*)image
)->prop_item
)
2656 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2658 if (propid
== ((GpBitmap
*)image
)->prop_item
[i
].id
)
2660 if (size
!= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
)
2661 return InvalidParameter
;
2663 *buffer
= ((GpBitmap
*)image
)->prop_item
[i
];
2664 buffer
->value
= buffer
+ 1;
2665 memcpy(buffer
->value
, ((GpBitmap
*)image
)->prop_item
[i
].value
, buffer
->length
);
2670 return PropertyNotFound
;
2673 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2674 if (!reader
) return PropertyNotFound
;
2677 id
.u
.uiVal
= propid
;
2678 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
2679 if (FAILED(hr
)) return PropertyNotFound
;
2681 stat
= propvariant_to_item(&value
, buffer
, size
, propid
);
2682 PropVariantClear(&value
);
2687 GpStatus WINGDIPAPI
GdipGetPropertySize(GpImage
*image
, UINT
*size
, UINT
*count
)
2690 IWICMetadataReader
*reader
;
2691 IWICEnumMetadataItem
*enumerator
;
2692 UINT prop_count
, prop_size
, i
;
2693 PROPVARIANT id
, value
;
2695 TRACE("(%p,%p,%p)\n", image
, size
, count
);
2697 if (!image
|| !size
|| !count
) return InvalidParameter
;
2699 if (image
->type
!= ImageTypeBitmap
)
2701 FIXME("Not implemented for type %d\n", image
->type
);
2702 return NotImplemented
;
2705 if (((GpBitmap
*)image
)->prop_item
)
2707 *count
= ((GpBitmap
*)image
)->prop_count
;
2710 for (i
= 0; i
< ((GpBitmap
*)image
)->prop_count
; i
++)
2712 *size
+= sizeof(PropertyItem
) + ((GpBitmap
*)image
)->prop_item
[i
].length
;
2718 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2719 if (!reader
) return PropertyNotFound
;
2721 hr
= IWICMetadataReader_GetCount(reader
, &prop_count
);
2722 if (FAILED(hr
)) return hresult_to_status(hr
);
2724 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2725 if (FAILED(hr
)) return hresult_to_status(hr
);
2727 IWICEnumMetadataItem_Reset(enumerator
);
2731 PropVariantInit(&id
);
2732 PropVariantInit(&value
);
2734 for (i
= 0; i
< prop_count
; i
++)
2736 UINT items_returned
, item_size
;
2738 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, &value
, &items_returned
);
2739 if (hr
!= S_OK
) break;
2741 item_size
= propvariant_size(&value
);
2742 if (item_size
) prop_size
+= sizeof(PropertyItem
) + item_size
;
2744 PropVariantClear(&id
);
2745 PropVariantClear(&value
);
2748 IWICEnumMetadataItem_Release(enumerator
);
2750 if (hr
!= S_OK
) return PropertyNotFound
;
2752 *count
= prop_count
;
2757 GpStatus WINGDIPAPI
GdipGetAllPropertyItems(GpImage
*image
, UINT size
,
2758 UINT count
, PropertyItem
*buf
)
2762 IWICMetadataReader
*reader
;
2763 IWICEnumMetadataItem
*enumerator
;
2764 UINT prop_count
, prop_size
, i
;
2765 PROPVARIANT id
, value
;
2768 TRACE("(%p,%u,%u,%p)\n", image
, size
, count
, buf
);
2770 if (!image
|| !buf
) return InvalidParameter
;
2772 if (image
->type
!= ImageTypeBitmap
)
2774 FIXME("Not implemented for type %d\n", image
->type
);
2775 return NotImplemented
;
2778 status
= GdipGetPropertySize(image
, &prop_size
, &prop_count
);
2779 if (status
!= Ok
) return status
;
2781 if (prop_count
!= count
|| prop_size
!= size
) return InvalidParameter
;
2783 if (((GpBitmap
*)image
)->prop_item
)
2785 memcpy(buf
, ((GpBitmap
*)image
)->prop_item
, prop_size
);
2787 item_value
= (char *)(buf
+ prop_count
);
2789 for (i
= 0; i
< prop_count
; i
++)
2791 buf
[i
].value
= item_value
;
2792 item_value
+= buf
[i
].length
;
2798 reader
= ((GpBitmap
*)image
)->metadata_reader
;
2799 if (!reader
) return PropertyNotFound
;
2801 hr
= IWICMetadataReader_GetEnumerator(reader
, &enumerator
);
2802 if (FAILED(hr
)) return hresult_to_status(hr
);
2804 IWICEnumMetadataItem_Reset(enumerator
);
2806 item_value
= (char *)(buf
+ prop_count
);
2808 PropVariantInit(&id
);
2809 PropVariantInit(&value
);
2811 for (i
= 0; i
< prop_count
; i
++)
2814 UINT items_returned
, item_size
;
2816 hr
= IWICEnumMetadataItem_Next(enumerator
, 1, NULL
, &id
, &value
, &items_returned
);
2817 if (hr
!= S_OK
) break;
2819 if (id
.vt
!= VT_UI2
)
2821 FIXME("not supported propvariant type for id: %u\n", id
.vt
);
2825 item_size
= propvariant_size(&value
);
2828 item
= heap_alloc(item_size
+ sizeof(*item
));
2830 propvariant_to_item(&value
, item
, item_size
+ sizeof(*item
), id
.u
.uiVal
);
2831 buf
[i
].id
= item
->id
;
2832 buf
[i
].type
= item
->type
;
2833 buf
[i
].length
= item_size
;
2834 buf
[i
].value
= item_value
;
2835 memcpy(item_value
, item
->value
, item_size
);
2836 item_value
+= item_size
;
2841 PropVariantClear(&id
);
2842 PropVariantClear(&value
);
2845 IWICEnumMetadataItem_Release(enumerator
);
2847 if (hr
!= S_OK
) return PropertyNotFound
;
2852 struct image_format_dimension
2855 const GUID
*dimension
;
2858 static const struct image_format_dimension image_format_dimensions
[] =
2860 {&ImageFormatGIF
, &FrameDimensionTime
},
2861 {&ImageFormatIcon
, &FrameDimensionResolution
},
2865 GpStatus WINGDIPAPI
GdipImageGetFrameCount(GpImage
*image
,
2866 GDIPCONST GUID
* dimensionID
, UINT
* count
)
2868 TRACE("(%p,%s,%p)\n", image
, debugstr_guid(dimensionID
), count
);
2870 if(!image
|| !count
)
2871 return InvalidParameter
;
2874 IsEqualGUID(dimensionID
, &image
->format
) ||
2875 IsEqualGUID(dimensionID
, &FrameDimensionPage
) ||
2876 IsEqualGUID(dimensionID
, &FrameDimensionTime
))
2878 *count
= image
->frame_count
;
2882 return InvalidParameter
;
2885 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsCount(GpImage
*image
,
2888 TRACE("(%p, %p)\n", image
, count
);
2890 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2892 if(!image
|| !count
)
2893 return InvalidParameter
;
2900 GpStatus WINGDIPAPI
GdipImageGetFrameDimensionsList(GpImage
* image
,
2901 GUID
* dimensionIDs
, UINT count
)
2904 const GUID
*result
=NULL
;
2906 TRACE("(%p,%p,%u)\n", image
, dimensionIDs
, count
);
2908 if(!image
|| !dimensionIDs
|| count
!= 1)
2909 return InvalidParameter
;
2911 for (i
=0; image_format_dimensions
[i
].format
; i
++)
2913 if (IsEqualGUID(&image
->format
, image_format_dimensions
[i
].format
))
2915 result
= image_format_dimensions
[i
].dimension
;
2921 result
= &FrameDimensionPage
;
2923 memcpy(dimensionIDs
, result
, sizeof(GUID
));
2928 GpStatus WINGDIPAPI
GdipLoadImageFromFile(GDIPCONST WCHAR
* filename
,
2934 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
2936 if (!filename
|| !image
)
2937 return InvalidParameter
;
2941 stat
= GdipCreateStreamOnFile(filename
, GENERIC_READ
, &stream
);
2946 stat
= GdipLoadImageFromStream(stream
, image
);
2948 IStream_Release(stream
);
2953 /* FIXME: no icm handling */
2954 GpStatus WINGDIPAPI
GdipLoadImageFromFileICM(GDIPCONST WCHAR
* filename
,GpImage
**image
)
2956 TRACE("(%s) %p\n", debugstr_w(filename
), image
);
2958 return GdipLoadImageFromFile(filename
, image
);
2961 static void add_property(GpBitmap
*bitmap
, PropertyItem
*item
)
2963 UINT prop_size
, prop_count
;
2964 PropertyItem
*prop_item
;
2966 if (bitmap
->prop_item
== NULL
)
2968 prop_size
= prop_count
= 0;
2969 prop_item
= heap_alloc_zero(item
->length
+ sizeof(PropertyItem
));
2970 if (!prop_item
) return;
2977 GdipGetPropertySize(&bitmap
->image
, &prop_size
, &prop_count
);
2979 prop_item
= heap_alloc_zero(prop_size
+ item
->length
+ sizeof(PropertyItem
));
2980 if (!prop_item
) return;
2981 memcpy(prop_item
, bitmap
->prop_item
, sizeof(PropertyItem
) * bitmap
->prop_count
);
2982 prop_size
-= sizeof(PropertyItem
) * bitmap
->prop_count
;
2983 memcpy(prop_item
+ prop_count
+ 1, bitmap
->prop_item
+ prop_count
, prop_size
);
2985 item_value
= (char *)(prop_item
+ prop_count
+ 1);
2987 for (i
= 0; i
< prop_count
; i
++)
2989 prop_item
[i
].value
= item_value
;
2990 item_value
+= prop_item
[i
].length
;
2994 prop_item
[prop_count
].id
= item
->id
;
2995 prop_item
[prop_count
].type
= item
->type
;
2996 prop_item
[prop_count
].length
= item
->length
;
2997 prop_item
[prop_count
].value
= (char *)(prop_item
+ prop_count
+ 1) + prop_size
;
2998 memcpy(prop_item
[prop_count
].value
, item
->value
, item
->length
);
3000 heap_free(bitmap
->prop_item
);
3001 bitmap
->prop_item
= prop_item
;
3002 bitmap
->prop_count
++;
3005 static BOOL
get_bool_property(IWICMetadataReader
*reader
, const GUID
*guid
, const WCHAR
*prop_name
)
3009 PROPVARIANT id
, value
;
3012 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3013 if (FAILED(hr
) || !IsEqualGUID(&format
, guid
)) return FALSE
;
3015 PropVariantInit(&id
);
3016 PropVariantInit(&value
);
3019 id
.u
.pwszVal
= CoTaskMemAlloc((lstrlenW(prop_name
) + 1) * sizeof(WCHAR
));
3020 if (!id
.u
.pwszVal
) return FALSE
;
3021 lstrcpyW(id
.u
.pwszVal
, prop_name
);
3022 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
3023 if (hr
== S_OK
&& value
.vt
== VT_BOOL
)
3024 ret
= value
.u
.boolVal
;
3026 PropVariantClear(&id
);
3027 PropVariantClear(&value
);
3032 static PropertyItem
*get_property(IWICMetadataReader
*reader
, const GUID
*guid
, const WCHAR
*prop_name
)
3036 PROPVARIANT id
, value
;
3037 PropertyItem
*item
= NULL
;
3039 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3040 if (FAILED(hr
) || !IsEqualGUID(&format
, guid
)) return NULL
;
3042 PropVariantInit(&id
);
3043 PropVariantInit(&value
);
3046 id
.u
.pwszVal
= CoTaskMemAlloc((lstrlenW(prop_name
) + 1) * sizeof(WCHAR
));
3047 if (!id
.u
.pwszVal
) return NULL
;
3048 lstrcpyW(id
.u
.pwszVal
, prop_name
);
3049 hr
= IWICMetadataReader_GetValue(reader
, NULL
, &id
, &value
);
3052 UINT item_size
= propvariant_size(&value
);
3055 item_size
+= sizeof(*item
);
3056 item
= heap_alloc_zero(item_size
);
3057 if (propvariant_to_item(&value
, item
, item_size
, 0) != Ok
)
3065 PropVariantClear(&id
);
3066 PropVariantClear(&value
);
3071 static PropertyItem
*get_gif_comment(IWICMetadataReader
*reader
)
3073 static const WCHAR textentryW
[] = { 'T','e','x','t','E','n','t','r','y',0 };
3074 PropertyItem
*comment
;
3076 comment
= get_property(reader
, &GUID_MetadataFormatGifComment
, textentryW
);
3078 comment
->id
= PropertyTagExifUserComment
;
3083 static PropertyItem
*get_gif_loopcount(IWICMetadataReader
*reader
)
3085 static const WCHAR applicationW
[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3086 static const WCHAR dataW
[] = { 'D','a','t','a',0 };
3087 PropertyItem
*appext
= NULL
, *appdata
= NULL
, *loop
= NULL
;
3089 appext
= get_property(reader
, &GUID_MetadataFormatAPE
, applicationW
);
3092 if (appext
->type
== PropertyTagTypeByte
&& appext
->length
== 11 &&
3093 (!memcmp(appext
->value
, "NETSCAPE2.0", 11) || !memcmp(appext
->value
, "ANIMEXTS1.0", 11)))
3095 appdata
= get_property(reader
, &GUID_MetadataFormatAPE
, dataW
);
3098 if (appdata
->type
== PropertyTagTypeByte
&& appdata
->length
== 4)
3100 BYTE
*data
= appdata
->value
;
3101 if (data
[0] == 3 && data
[1] == 1)
3103 loop
= heap_alloc_zero(sizeof(*loop
) + sizeof(SHORT
));
3106 loop
->type
= PropertyTagTypeShort
;
3107 loop
->id
= PropertyTagLoopCount
;
3108 loop
->length
= sizeof(SHORT
);
3109 loop
->value
= loop
+ 1;
3110 *(SHORT
*)loop
->value
= data
[2] | (data
[3] << 8);
3124 static PropertyItem
*get_gif_background(IWICMetadataReader
*reader
)
3126 static const WCHAR backgroundW
[] = { 'B','a','c','k','g','r','o','u','n','d','C','o','l','o','r','I','n','d','e','x',0 };
3127 PropertyItem
*background
;
3129 background
= get_property(reader
, &GUID_MetadataFormatLSD
, backgroundW
);
3131 background
->id
= PropertyTagIndexBackground
;
3136 static PropertyItem
*get_gif_palette(IWICBitmapDecoder
*decoder
, IWICMetadataReader
*reader
)
3138 static const WCHAR global_flagW
[] = { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 };
3140 IWICImagingFactory
*factory
;
3141 IWICPalette
*palette
;
3143 WICColor colors
[256];
3145 if (!get_bool_property(reader
, &GUID_MetadataFormatLSD
, global_flagW
))
3148 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
3149 if (hr
!= S_OK
) return NULL
;
3151 hr
= IWICImagingFactory_CreatePalette(factory
, &palette
);
3154 hr
= IWICBitmapDecoder_CopyPalette(decoder
, palette
);
3156 IWICPalette_GetColors(palette
, 256, colors
, &count
);
3158 IWICPalette_Release(palette
);
3161 IWICImagingFactory_Release(factory
);
3169 pal
= heap_alloc_zero(sizeof(*pal
) + count
* 3);
3170 if (!pal
) return NULL
;
3171 pal
->type
= PropertyTagTypeByte
;
3172 pal
->id
= PropertyTagGlobalPalette
;
3173 pal
->value
= pal
+ 1;
3174 pal
->length
= count
* 3;
3178 for (i
= 0; i
< count
; i
++)
3180 rgb
[i
*3] = (colors
[i
] >> 16) & 0xff;
3181 rgb
[i
*3 + 1] = (colors
[i
] >> 8) & 0xff;
3182 rgb
[i
*3 + 2] = colors
[i
] & 0xff;
3191 static PropertyItem
*get_gif_transparent_idx(IWICMetadataReader
*reader
)
3193 static const WCHAR transparency_flagW
[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3194 static const WCHAR colorW
[] = { 'T','r','a','n','s','p','a','r','e','n','t','C','o','l','o','r','I','n','d','e','x',0 };
3195 PropertyItem
*index
= NULL
;
3197 if (get_bool_property(reader
, &GUID_MetadataFormatGCE
, transparency_flagW
))
3199 index
= get_property(reader
, &GUID_MetadataFormatGCE
, colorW
);
3201 index
->id
= PropertyTagIndexTransparent
;
3206 static LONG
get_gif_frame_property(IWICBitmapFrameDecode
*frame
, const GUID
*format
, const WCHAR
*property
)
3209 IWICMetadataBlockReader
*block_reader
;
3210 IWICMetadataReader
*reader
;
3211 UINT block_count
, i
;
3215 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3218 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3221 for (i
= 0; i
< block_count
; i
++)
3223 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3226 prop
= get_property(reader
, format
, property
);
3229 if (prop
->type
== PropertyTagTypeByte
&& prop
->length
== 1)
3230 value
= *(BYTE
*)prop
->value
;
3231 else if (prop
->type
== PropertyTagTypeShort
&& prop
->length
== 2)
3232 value
= *(SHORT
*)prop
->value
;
3236 IWICMetadataReader_Release(reader
);
3240 IWICMetadataBlockReader_Release(block_reader
);
3246 static void gif_metadata_reader(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT active_frame
)
3248 static const WCHAR delayW
[] = { 'D','e','l','a','y',0 };
3250 IWICBitmapFrameDecode
*frame
;
3251 IWICMetadataBlockReader
*block_reader
;
3252 IWICMetadataReader
*reader
;
3253 UINT frame_count
, block_count
, i
;
3254 PropertyItem
*delay
= NULL
, *comment
= NULL
, *background
= NULL
;
3255 PropertyItem
*transparent_idx
= NULL
, *loop
= NULL
, *palette
= NULL
;
3257 IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
3258 if (frame_count
> 1)
3260 delay
= heap_alloc_zero(sizeof(*delay
) + frame_count
* sizeof(LONG
));
3265 delay
->type
= PropertyTagTypeLong
;
3266 delay
->id
= PropertyTagFrameDelay
;
3267 delay
->length
= frame_count
* sizeof(LONG
);
3268 delay
->value
= delay
+ 1;
3270 value
= delay
->value
;
3272 for (i
= 0; i
< frame_count
; i
++)
3274 hr
= IWICBitmapDecoder_GetFrame(decoder
, i
, &frame
);
3277 value
[i
] = get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, delayW
);
3278 IWICBitmapFrameDecode_Release(frame
);
3285 hr
= IWICBitmapDecoder_QueryInterface(decoder
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3288 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3291 for (i
= 0; i
< block_count
; i
++)
3293 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3297 comment
= get_gif_comment(reader
);
3299 if (frame_count
> 1 && !loop
)
3300 loop
= get_gif_loopcount(reader
);
3303 background
= get_gif_background(reader
);
3306 palette
= get_gif_palette(decoder
, reader
);
3308 IWICMetadataReader_Release(reader
);
3312 IWICMetadataBlockReader_Release(block_reader
);
3315 if (frame_count
> 1 && !loop
)
3317 loop
= heap_alloc_zero(sizeof(*loop
) + sizeof(SHORT
));
3320 loop
->type
= PropertyTagTypeShort
;
3321 loop
->id
= PropertyTagLoopCount
;
3322 loop
->length
= sizeof(SHORT
);
3323 loop
->value
= loop
+ 1;
3324 *(SHORT
*)loop
->value
= 1;
3328 if (delay
) add_property(bitmap
, delay
);
3329 if (comment
) add_property(bitmap
, comment
);
3330 if (loop
) add_property(bitmap
, loop
);
3331 if (palette
) add_property(bitmap
, palette
);
3332 if (background
) add_property(bitmap
, background
);
3338 heap_free(background
);
3340 /* Win7 gdiplus always returns transparent color index from frame 0 */
3341 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
3342 if (hr
!= S_OK
) return;
3344 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3347 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3350 for (i
= 0; i
< block_count
; i
++)
3352 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3355 if (!transparent_idx
)
3356 transparent_idx
= get_gif_transparent_idx(reader
);
3358 IWICMetadataReader_Release(reader
);
3362 IWICMetadataBlockReader_Release(block_reader
);
3365 if (transparent_idx
) add_property(bitmap
, transparent_idx
);
3366 heap_free(transparent_idx
);
3368 IWICBitmapFrameDecode_Release(frame
);
3371 static PropertyItem
* create_prop(PROPID propid
, PROPVARIANT
* value
)
3373 PropertyItem
*item
= NULL
;
3374 UINT item_size
= propvariant_size(value
);
3378 item_size
+= sizeof(*item
);
3379 item
= heap_alloc_zero(item_size
);
3380 if (propvariant_to_item(value
, item
, item_size
, propid
) != Ok
)
3390 static ULONG
get_ulong_by_index(IWICMetadataReader
* reader
, ULONG index
)
3396 hr
= IWICMetadataReader_GetValueByIndex(reader
, index
, NULL
, NULL
, &value
);
3402 result
= value
.u
.ulVal
;
3405 ERR("unhandled case %u\n", value
.vt
);
3408 PropVariantClear(&value
);
3413 static void png_metadata_reader(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT active_frame
)
3416 IWICBitmapFrameDecode
*frame
;
3417 IWICMetadataBlockReader
*block_reader
;
3418 IWICMetadataReader
*reader
;
3419 UINT block_count
, i
, j
;
3420 struct keyword_info
{
3425 { "Title", PropertyTagImageTitle
},
3426 { "Author", PropertyTagArtist
},
3427 { "Description", PropertyTagImageDescription
},
3428 { "Copyright", PropertyTagCopyright
},
3429 { "Software", PropertyTagSoftwareUsed
},
3430 { "Source", PropertyTagEquipModel
},
3431 { "Comment", PropertyTagExifUserComment
},
3433 BOOL seen_gamma
=FALSE
, seen_whitepoint
=FALSE
, seen_chrm
=FALSE
;
3435 hr
= IWICBitmapDecoder_GetFrame(decoder
, active_frame
, &frame
);
3436 if (hr
!= S_OK
) return;
3438 hr
= IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
);
3441 hr
= IWICMetadataBlockReader_GetCount(block_reader
, &block_count
);
3444 for (i
= 0; i
< block_count
; i
++)
3446 hr
= IWICMetadataBlockReader_GetReaderByIndex(block_reader
, i
, &reader
);
3451 hr
= IWICMetadataReader_GetMetadataFormat(reader
, &format
);
3452 if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunktEXt
, &format
))
3454 PROPVARIANT name
, value
;
3457 hr
= IWICMetadataReader_GetValueByIndex(reader
, 0, NULL
, &name
, &value
);
3461 if (name
.vt
== VT_LPSTR
)
3463 for (j
=0; j
<sizeof(keywords
)/sizeof(keywords
[0]); j
++)
3464 if (!strcmp(keywords
[j
].name
, name
.u
.pszVal
))
3466 if (j
< sizeof(keywords
)/sizeof(keywords
[0]) && !keywords
[j
].seen
)
3468 keywords
[j
].seen
= TRUE
;
3469 item
= create_prop(keywords
[j
].propid
, &value
);
3471 add_property(bitmap
, item
);
3476 PropVariantClear(&name
);
3477 PropVariantClear(&value
);
3480 else if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunkgAMA
, &format
))
3486 item
= heap_alloc_zero(sizeof(PropertyItem
) + sizeof(ULONG
) * 2);
3490 item
->length
= sizeof(ULONG
) * 2;
3491 item
->type
= PropertyTagTypeRational
;
3492 item
->id
= PropertyTagGamma
;
3493 rational
= item
->value
= item
+ 1;
3494 rational
[0] = 100000;
3495 rational
[1] = get_ulong_by_index(reader
, 0);
3496 add_property(bitmap
, item
);
3502 else if (SUCCEEDED(hr
) && IsEqualGUID(&GUID_MetadataFormatChunkcHRM
, &format
))
3506 if (!seen_whitepoint
)
3508 item
= GdipAlloc(sizeof(PropertyItem
) + sizeof(ULONG
) * 4);
3512 item
->length
= sizeof(ULONG
) * 4;
3513 item
->type
= PropertyTagTypeRational
;
3514 item
->id
= PropertyTagWhitePoint
;
3515 rational
= item
->value
= item
+ 1;
3516 rational
[0] = get_ulong_by_index(reader
, 0);
3517 rational
[1] = 100000;
3518 rational
[2] = get_ulong_by_index(reader
, 1);
3519 rational
[3] = 100000;
3520 add_property(bitmap
, item
);
3521 seen_whitepoint
= TRUE
;
3527 item
= GdipAlloc(sizeof(PropertyItem
) + sizeof(ULONG
) * 12);
3531 item
->length
= sizeof(ULONG
) * 12;
3532 item
->type
= PropertyTagTypeRational
;
3533 item
->id
= PropertyTagPrimaryChromaticities
;
3534 rational
= item
->value
= item
+ 1;
3535 rational
[0] = get_ulong_by_index(reader
, 2);
3536 rational
[1] = 100000;
3537 rational
[2] = get_ulong_by_index(reader
, 3);
3538 rational
[3] = 100000;
3539 rational
[4] = get_ulong_by_index(reader
, 4);
3540 rational
[5] = 100000;
3541 rational
[6] = get_ulong_by_index(reader
, 5);
3542 rational
[7] = 100000;
3543 rational
[8] = get_ulong_by_index(reader
, 6);
3544 rational
[9] = 100000;
3545 rational
[10] = get_ulong_by_index(reader
, 7);
3546 rational
[11] = 100000;
3547 add_property(bitmap
, item
);
3554 IWICMetadataReader_Release(reader
);
3558 IWICMetadataBlockReader_Release(block_reader
);
3561 IWICBitmapFrameDecode_Release(frame
);
3564 static GpStatus
initialize_decoder_wic(IStream
*stream
, REFGUID container
, IWICBitmapDecoder
**decoder
)
3566 IWICImagingFactory
*factory
;
3569 TRACE("%p,%s\n", stream
, wine_dbgstr_guid(container
));
3571 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
3572 if (FAILED(hr
)) return hresult_to_status(hr
);
3573 hr
= IWICImagingFactory_CreateDecoder(factory
, container
, NULL
, decoder
);
3574 IWICImagingFactory_Release(factory
);
3575 if (FAILED(hr
)) return hresult_to_status(hr
);
3577 hr
= IWICBitmapDecoder_Initialize(*decoder
, stream
, WICDecodeMetadataCacheOnLoad
);
3578 if (FAILED(hr
)) return hresult_to_status(hr
);
3582 typedef void (*metadata_reader_func
)(GpBitmap
*bitmap
, IWICBitmapDecoder
*decoder
, UINT frame
);
3584 static GpStatus
decode_frame_wic(IWICBitmapDecoder
*decoder
, BOOL force_conversion
,
3585 UINT active_frame
, metadata_reader_func metadata_reader
, GpImage
**image
)
3590 IWICBitmapFrameDecode
*frame
;
3591 IWICBitmapSource
*source
=NULL
;
3592 IWICMetadataBlockReader
*block_reader
;
3593 WICPixelFormatGUID wic_format
;
3594 PixelFormat gdip_format
=0;
3595 ColorPalette
*palette
= NULL
;
3596 WICBitmapPaletteType palette_type
= WICBitmapPaletteTypeFixedHalftone256
;
3598 UINT width
, height
, frame_count
;
3599 BitmapData lockeddata
;
3602 TRACE("%p,%u,%p\n", decoder
, active_frame
, image
);
3604 IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
3605 hr
= IWICBitmapDecoder_GetFrame(decoder
, active_frame
, &frame
);
3606 if (SUCCEEDED(hr
)) /* got frame */
3608 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &wic_format
);
3612 if (!force_conversion
)
3614 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
3616 if (IsEqualGUID(&wic_format
, pixel_formats
[i
].wic_format
))
3618 source
= (IWICBitmapSource
*)frame
;
3619 IWICBitmapSource_AddRef(source
);
3620 gdip_format
= pixel_formats
[i
].gdip_format
;
3621 palette_type
= pixel_formats
[i
].palette_type
;
3628 /* unknown format; fall back on 32bppARGB */
3629 hr
= WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA
, (IWICBitmapSource
*)frame
, &source
);
3630 gdip_format
= PixelFormat32bppARGB
;
3632 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format
), gdip_format
);
3635 if (SUCCEEDED(hr
)) /* got source */
3637 hr
= IWICBitmapSource_GetSize(source
, &width
, &height
);
3640 status
= GdipCreateBitmapFromScan0(width
, height
, 0, gdip_format
,
3643 if (SUCCEEDED(hr
) && status
== Ok
) /* created bitmap */
3645 status
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeWrite
,
3646 gdip_format
, &lockeddata
);
3647 if (status
== Ok
) /* locked bitmap */
3652 for (i
=0; i
<height
; i
++)
3655 hr
= IWICBitmapSource_CopyPixels(source
, &wrc
, abs(lockeddata
.Stride
),
3656 abs(lockeddata
.Stride
), (BYTE
*)lockeddata
.Scan0
+lockeddata
.Stride
*i
);
3657 if (FAILED(hr
)) break;
3660 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
3663 if (SUCCEEDED(hr
) && status
== Ok
)
3664 *image
= &bitmap
->image
;
3668 GdipDisposeImage(&bitmap
->image
);
3671 if (SUCCEEDED(hr
) && status
== Ok
)
3674 hr
= IWICBitmapSource_GetResolution(source
, &dpix
, &dpiy
);
3677 bitmap
->image
.xres
= dpix
;
3678 bitmap
->image
.yres
= dpiy
;
3684 IWICBitmapSource_Release(source
);
3687 if (SUCCEEDED(hr
)) {
3688 bitmap
->metadata_reader
= NULL
;
3690 if (metadata_reader
)
3691 metadata_reader(bitmap
, decoder
, active_frame
);
3692 else if (IWICBitmapFrameDecode_QueryInterface(frame
, &IID_IWICMetadataBlockReader
, (void **)&block_reader
) == S_OK
)
3694 UINT block_count
= 0;
3695 if (IWICMetadataBlockReader_GetCount(block_reader
, &block_count
) == S_OK
&& block_count
)
3696 IWICMetadataBlockReader_GetReaderByIndex(block_reader
, 0, &bitmap
->metadata_reader
);
3697 IWICMetadataBlockReader_Release(block_reader
);
3700 palette
= get_palette(frame
, palette_type
);
3701 IWICBitmapFrameDecode_Release(frame
);
3705 if (FAILED(hr
) && status
== Ok
) status
= hresult_to_status(hr
);
3709 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3710 bitmap
->image
.flags
|= ImageFlagsReadOnly
|ImageFlagsHasRealPixelSize
|ImageFlagsHasRealDPI
;
3711 if (IsEqualGUID(&wic_format
, &GUID_WICPixelFormat2bppGray
) ||
3712 IsEqualGUID(&wic_format
, &GUID_WICPixelFormat4bppGray
) ||
3713 IsEqualGUID(&wic_format
, &GUID_WICPixelFormat8bppGray
) ||
3714 IsEqualGUID(&wic_format
, &GUID_WICPixelFormat16bppGray
))
3715 bitmap
->image
.flags
|= ImageFlagsColorSpaceGRAY
;
3717 bitmap
->image
.flags
|= ImageFlagsColorSpaceRGB
;
3718 bitmap
->image
.frame_count
= frame_count
;
3719 bitmap
->image
.current_frame
= active_frame
;
3720 bitmap
->image
.decoder
= decoder
;
3721 IWICBitmapDecoder_AddRef(decoder
);
3724 heap_free(bitmap
->image
.palette
);
3725 bitmap
->image
.palette
= palette
;
3729 if (IsEqualGUID(&wic_format
, &GUID_WICPixelFormatBlackWhite
))
3730 bitmap
->image
.palette
->Flags
= 0;
3732 TRACE("=> %p\n", *image
);
3738 static GpStatus
decode_image_wic(IStream
*stream
, REFGUID container
,
3739 metadata_reader_func metadata_reader
, GpImage
**image
)
3741 IWICBitmapDecoder
*decoder
;
3744 status
= initialize_decoder_wic(stream
, container
, &decoder
);
3748 status
= decode_frame_wic(decoder
, FALSE
, 0, metadata_reader
, image
);
3749 IWICBitmapDecoder_Release(decoder
);
3753 static GpStatus
select_frame_wic(GpImage
*image
, UINT active_frame
)
3758 status
= decode_frame_wic(image
->decoder
, FALSE
, active_frame
, NULL
, &new_image
);
3762 new_image
->busy
= image
->busy
;
3763 memcpy(&new_image
->format
, &image
->format
, sizeof(GUID
));
3764 free_image_data(image
);
3765 if (image
->type
== ImageTypeBitmap
)
3766 *(GpBitmap
*)image
= *(GpBitmap
*)new_image
;
3767 else if (image
->type
== ImageTypeMetafile
)
3768 *(GpMetafile
*)image
= *(GpMetafile
*)new_image
;
3769 new_image
->type
= ~0;
3770 heap_free(new_image
);
3774 static HRESULT
get_gif_frame_rect(IWICBitmapFrameDecode
*frame
,
3775 UINT
*left
, UINT
*top
, UINT
*width
, UINT
*height
)
3777 static const WCHAR leftW
[] = {'L','e','f','t',0};
3778 static const WCHAR topW
[] = {'T','o','p',0};
3780 *left
= get_gif_frame_property(frame
, &GUID_MetadataFormatIMD
, leftW
);
3781 *top
= get_gif_frame_property(frame
, &GUID_MetadataFormatIMD
, topW
);
3783 return IWICBitmapFrameDecode_GetSize(frame
, width
, height
);
3786 static HRESULT
blit_gif_frame(GpBitmap
*bitmap
, IWICBitmapFrameDecode
*frame
, BOOL first_frame
)
3788 UINT i
, j
, left
, top
, width
, height
;
3789 IWICBitmapSource
*source
;
3793 hr
= get_gif_frame_rect(frame
, &left
, &top
, &width
, &height
);
3797 hr
= WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA
, (IWICBitmapSource
*)frame
, &source
);
3801 new_bits
= heap_alloc_zero(width
*height
*4);
3803 return E_OUTOFMEMORY
;
3805 hr
= IWICBitmapSource_CopyPixels(source
, NULL
, width
*4, width
*height
*4, new_bits
);
3806 IWICBitmapSource_Release(source
);
3808 heap_free(new_bits
);
3812 for(i
=0; i
<height
&& i
+top
<bitmap
->height
; i
++) {
3813 for(j
=0; j
<width
&& j
+left
<bitmap
->width
; j
++) {
3814 DWORD
*src
= (DWORD
*)(new_bits
+i
*width
*4+j
*4);
3815 DWORD
*dst
= (DWORD
*)(bitmap
->bits
+(i
+top
)*bitmap
->stride
+(j
+left
)*4);
3817 if(first_frame
|| *src
>>24 != 0)
3821 heap_free(new_bits
);
3825 static DWORD
get_gif_background_color(GpBitmap
*bitmap
)
3827 BYTE bgcolor_idx
= 0;
3830 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3831 if(bitmap
->prop_item
[i
].id
== PropertyTagIndexBackground
) {
3832 bgcolor_idx
= *(BYTE
*)bitmap
->prop_item
[i
].value
;
3837 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3838 if(bitmap
->prop_item
[i
].id
== PropertyTagIndexTransparent
) {
3839 BYTE transparent_idx
;
3840 transparent_idx
= *(BYTE
*)bitmap
->prop_item
[i
].value
;
3842 if(transparent_idx
== bgcolor_idx
)
3847 for(i
=0; i
<bitmap
->prop_count
; i
++) {
3848 if(bitmap
->prop_item
[i
].id
== PropertyTagGlobalPalette
) {
3849 if(bitmap
->prop_item
[i
].length
/3 > bgcolor_idx
) {
3850 BYTE
*color
= ((BYTE
*)bitmap
->prop_item
[i
].value
)+bgcolor_idx
*3;
3851 return color
[2] + (color
[1]<<8) + (color
[0]<<16) + (0xffu
<<24);
3857 FIXME("can't get gif background color\n");
3861 static GpStatus
select_frame_gif(GpImage
* image
, UINT active_frame
)
3863 static const WCHAR disposalW
[] = {'D','i','s','p','o','s','a','l',0};
3865 GpBitmap
*bitmap
= (GpBitmap
*)image
;
3866 IWICBitmapFrameDecode
*frame
;
3867 int cur_frame
=0, disposal
;
3868 BOOL bgcolor_set
= FALSE
;
3872 if(active_frame
> image
->current_frame
) {
3873 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, image
->current_frame
, &frame
);
3875 return hresult_to_status(hr
);
3876 disposal
= get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, disposalW
);
3877 IWICBitmapFrameDecode_Release(frame
);
3879 if(disposal
== GIF_DISPOSE_RESTORE_TO_BKGND
)
3880 cur_frame
= image
->current_frame
;
3881 else if(disposal
!= GIF_DISPOSE_RESTORE_TO_PREV
)
3882 cur_frame
= image
->current_frame
+1;
3885 while(cur_frame
!= active_frame
) {
3886 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, cur_frame
, &frame
);
3888 return hresult_to_status(hr
);
3889 disposal
= get_gif_frame_property(frame
, &GUID_MetadataFormatGCE
, disposalW
);
3891 if(disposal
==GIF_DISPOSE_UNSPECIFIED
|| disposal
==GIF_DISPOSE_DO_NOT_DISPOSE
) {
3892 hr
= blit_gif_frame(bitmap
, frame
, cur_frame
==0);
3894 return hresult_to_status(hr
);
3895 }else if(disposal
== GIF_DISPOSE_RESTORE_TO_BKGND
) {
3896 UINT left
, top
, width
, height
, i
, j
;
3899 bgcolor
= get_gif_background_color(bitmap
);
3903 hr
= get_gif_frame_rect(frame
, &left
, &top
, &width
, &height
);
3905 return hresult_to_status(hr
);
3906 for(i
=top
; i
<top
+height
&& i
<bitmap
->height
; i
++) {
3907 DWORD
*bits
= (DWORD
*)(bitmap
->bits
+i
*bitmap
->stride
);
3908 for(j
=left
; j
<left
+width
&& j
<bitmap
->width
; j
++)
3913 IWICBitmapFrameDecode_Release(frame
);
3917 hr
= IWICBitmapDecoder_GetFrame(bitmap
->image
.decoder
, active_frame
, &frame
);
3919 return hresult_to_status(hr
);
3921 hr
= blit_gif_frame(bitmap
, frame
, cur_frame
==0);
3922 IWICBitmapFrameDecode_Release(frame
);
3924 return hresult_to_status(hr
);
3926 image
->current_frame
= active_frame
;
3930 static GpStatus
decode_image_icon(IStream
* stream
, GpImage
**image
)
3932 return decode_image_wic(stream
, &GUID_ContainerFormatIco
, NULL
, image
);
3935 static GpStatus
decode_image_bmp(IStream
* stream
, GpImage
**image
)
3940 status
= decode_image_wic(stream
, &GUID_ContainerFormatBmp
, NULL
, image
);
3942 bitmap
= (GpBitmap
*)*image
;
3944 if (status
== Ok
&& bitmap
->format
== PixelFormat32bppARGB
)
3946 /* WIC supports bmp files with alpha, but gdiplus does not */
3947 bitmap
->format
= PixelFormat32bppRGB
;
3953 static GpStatus
decode_image_jpeg(IStream
* stream
, GpImage
**image
)
3955 return decode_image_wic(stream
, &GUID_ContainerFormatJpeg
, NULL
, image
);
3958 static GpStatus
decode_image_png(IStream
* stream
, GpImage
**image
)
3960 IWICBitmapDecoder
*decoder
;
3961 IWICBitmapFrameDecode
*frame
;
3965 BOOL force_conversion
= FALSE
;
3967 status
= initialize_decoder_wic(stream
, &GUID_ContainerFormatPng
, &decoder
);
3971 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
3974 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &format
);
3977 if (IsEqualGUID(&format
, &GUID_WICPixelFormat8bppGray
))
3978 force_conversion
= TRUE
;
3979 status
= decode_frame_wic(decoder
, force_conversion
, 0, png_metadata_reader
, image
);
3982 status
= hresult_to_status(hr
);
3984 IWICBitmapFrameDecode_Release(frame
);
3987 status
= hresult_to_status(hr
);
3989 IWICBitmapDecoder_Release(decoder
);
3993 static GpStatus
decode_image_gif(IStream
* stream
, GpImage
**image
)
3995 IWICBitmapDecoder
*decoder
;
4000 status
= initialize_decoder_wic(stream
, &GUID_ContainerFormatGif
, &decoder
);
4004 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
4006 return hresult_to_status(hr
);
4008 status
= decode_frame_wic(decoder
, frame_count
> 1, 0, gif_metadata_reader
, image
);
4009 IWICBitmapDecoder_Release(decoder
);
4013 if(frame_count
> 1) {
4014 heap_free((*image
)->palette
);
4015 (*image
)->palette
= NULL
;
4020 static GpStatus
decode_image_tiff(IStream
* stream
, GpImage
**image
)
4022 return decode_image_wic(stream
, &GUID_ContainerFormatTiff
, NULL
, image
);
4025 static GpStatus
load_wmf(IStream
*stream
, GpMetafile
**metafile
)
4027 WmfPlaceableFileHeader pfh
;
4028 BOOL is_placeable
= FALSE
;
4037 hr
= IStream_Read(stream
, &mh
, sizeof(mh
), &size
);
4038 if (hr
!= S_OK
|| size
!= sizeof(mh
))
4039 return GenericError
;
4041 if (((WmfPlaceableFileHeader
*)&mh
)->Key
== WMF_PLACEABLE_KEY
)
4044 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4045 if (FAILED(hr
)) return hresult_to_status(hr
);
4047 hr
= IStream_Read(stream
, &pfh
, sizeof(pfh
), &size
);
4048 if (hr
!= S_OK
|| size
!= sizeof(pfh
))
4049 return GenericError
;
4051 hr
= IStream_Read(stream
, &mh
, sizeof(mh
), &size
);
4052 if (hr
!= S_OK
|| size
!= sizeof(mh
))
4053 return GenericError
;
4055 is_placeable
= TRUE
;
4058 seek
.QuadPart
= is_placeable
? sizeof(pfh
) : 0;
4059 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4060 if (FAILED(hr
)) return hresult_to_status(hr
);
4062 buf
= heap_alloc(mh
.mtSize
* 2);
4063 if (!buf
) return OutOfMemory
;
4065 hr
= IStream_Read(stream
, buf
, mh
.mtSize
* 2, &size
);
4066 if (hr
!= S_OK
|| size
!= mh
.mtSize
* 2)
4069 return GenericError
;
4072 hmf
= SetMetaFileBitsEx(mh
.mtSize
* 2, buf
);
4075 return GenericError
;
4077 status
= GdipCreateMetafileFromWmf(hmf
, TRUE
, is_placeable
? &pfh
: NULL
, metafile
);
4079 DeleteMetaFile(hmf
);
4083 static GpStatus
decode_image_wmf(IStream
*stream
, GpImage
**image
)
4085 GpMetafile
*metafile
;
4088 TRACE("%p %p\n", stream
, image
);
4090 if (!stream
|| !image
)
4091 return InvalidParameter
;
4093 status
= load_wmf(stream
, &metafile
);
4096 TRACE("Could not load metafile\n");
4100 *image
= (GpImage
*)metafile
;
4101 TRACE("<-- %p\n", *image
);
4106 static GpStatus
load_emf(IStream
*stream
, GpMetafile
**metafile
)
4116 hr
= IStream_Read(stream
, &emh
, sizeof(emh
), &size
);
4117 if (hr
!= S_OK
|| size
!= sizeof(emh
) || emh
.dSignature
!= ENHMETA_SIGNATURE
)
4118 return GenericError
;
4121 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4122 if (FAILED(hr
)) return hresult_to_status(hr
);
4124 buf
= heap_alloc(emh
.nBytes
);
4125 if (!buf
) return OutOfMemory
;
4127 hr
= IStream_Read(stream
, buf
, emh
.nBytes
, &size
);
4128 if (hr
!= S_OK
|| size
!= emh
.nBytes
)
4131 return GenericError
;
4134 hemf
= SetEnhMetaFileBits(emh
.nBytes
, buf
);
4137 return GenericError
;
4139 status
= GdipCreateMetafileFromEmf(hemf
, TRUE
, metafile
);
4141 DeleteEnhMetaFile(hemf
);
4145 static GpStatus
decode_image_emf(IStream
*stream
, GpImage
**image
)
4147 GpMetafile
*metafile
;
4150 TRACE("%p %p\n", stream
, image
);
4152 if (!stream
|| !image
)
4153 return InvalidParameter
;
4155 status
= load_emf(stream
, &metafile
);
4158 TRACE("Could not load metafile\n");
4162 *image
= (GpImage
*)metafile
;
4163 TRACE("<-- %p\n", *image
);
4168 typedef GpStatus (*encode_image_func
)(GpImage
*image
, IStream
* stream
,
4169 GDIPCONST EncoderParameters
* params
);
4171 typedef GpStatus (*decode_image_func
)(IStream
*stream
, GpImage
**image
);
4173 typedef GpStatus (*select_image_func
)(GpImage
*image
, UINT active_frame
);
4175 typedef struct image_codec
{
4176 ImageCodecInfo info
;
4177 encode_image_func encode_func
;
4178 decode_image_func decode_func
;
4179 select_image_func select_func
;
4194 static const struct image_codec codecs
[NUM_CODECS
];
4196 static GpStatus
get_decoder_info(IStream
* stream
, const struct image_codec
**result
)
4199 const BYTE
*pattern
, *mask
;
4206 /* seek to the start of the stream */
4208 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4209 if (FAILED(hr
)) return hresult_to_status(hr
);
4211 /* read the first 8 bytes */
4212 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
4213 hr
= IStream_Read(stream
, signature
, 8, &bytesread
);
4214 if (FAILED(hr
)) return hresult_to_status(hr
);
4215 if (hr
== S_FALSE
|| bytesread
== 0) return GenericError
;
4217 for (i
= 0; i
< NUM_CODECS
; i
++) {
4218 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
) &&
4219 bytesread
>= codecs
[i
].info
.SigSize
)
4221 for (sig
=0; sig
<codecs
[i
].info
.SigCount
; sig
++)
4223 pattern
= &codecs
[i
].info
.SigPattern
[codecs
[i
].info
.SigSize
*sig
];
4224 mask
= &codecs
[i
].info
.SigMask
[codecs
[i
].info
.SigSize
*sig
];
4225 for (j
=0; j
<codecs
[i
].info
.SigSize
; j
++)
4226 if ((signature
[j
] & mask
[j
]) != pattern
[j
])
4228 if (j
== codecs
[i
].info
.SigSize
)
4230 *result
= &codecs
[i
];
4237 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread
,
4238 signature
[0],signature
[1],signature
[2],signature
[3],
4239 signature
[4],signature
[5],signature
[6],signature
[7]);
4241 return GenericError
;
4244 static GpStatus
get_decoder_info_from_image(GpImage
*image
, const struct image_codec
**result
)
4248 for (i
= 0; i
< NUM_CODECS
; i
++) {
4249 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
) &&
4250 IsEqualIID(&codecs
[i
].info
.FormatID
, &image
->format
))
4252 *result
= &codecs
[i
];
4257 TRACE("no match for format: %s\n", wine_dbgstr_guid(&image
->format
));
4258 return GenericError
;
4261 GpStatus WINGDIPAPI
GdipImageSelectActiveFrame(GpImage
*image
, GDIPCONST GUID
*dimensionID
,
4265 const struct image_codec
*codec
= NULL
;
4268 TRACE("(%p,%s,%u)\n", image
, debugstr_guid(dimensionID
), frame
);
4270 if (!image
|| !dimensionID
)
4271 return InvalidParameter
;
4272 if(!image_lock(image
, &unlock
))
4275 if (frame
>= image
->frame_count
)
4277 WARN("requested frame %u, but image has only %u\n", frame
, image
->frame_count
);
4278 image_unlock(image
, unlock
);
4279 return InvalidParameter
;
4282 if (image
->type
!= ImageTypeBitmap
&& image
->type
!= ImageTypeMetafile
)
4284 WARN("invalid image type %d\n", image
->type
);
4285 image_unlock(image
, unlock
);
4286 return InvalidParameter
;
4289 if (image
->current_frame
== frame
)
4291 image_unlock(image
, unlock
);
4295 if (!image
->decoder
)
4297 TRACE("image doesn't have an associated decoder\n");
4298 image_unlock(image
, unlock
);
4302 /* choose an appropriate image decoder */
4303 stat
= get_decoder_info_from_image(image
, &codec
);
4306 WARN("can't find decoder info\n");
4307 image_unlock(image
, unlock
);
4311 stat
= codec
->select_func(image
, frame
);
4312 image_unlock(image
, unlock
);
4316 GpStatus WINGDIPAPI
GdipLoadImageFromStream(IStream
*stream
, GpImage
**image
)
4321 const struct image_codec
*codec
=NULL
;
4323 /* choose an appropriate image decoder */
4324 stat
= get_decoder_info(stream
, &codec
);
4325 if (stat
!= Ok
) return stat
;
4327 /* seek to the start of the stream */
4329 hr
= IStream_Seek(stream
, seek
, STREAM_SEEK_SET
, NULL
);
4330 if (FAILED(hr
)) return hresult_to_status(hr
);
4332 /* call on the image decoder to do the real work */
4333 stat
= codec
->decode_func(stream
, image
);
4335 /* take note of the original data format */
4338 memcpy(&(*image
)->format
, &codec
->info
.FormatID
, sizeof(GUID
));
4346 GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM(IStream
* stream
, GpImage
**image
)
4348 TRACE("%p %p\n", stream
, image
);
4350 return GdipLoadImageFromStream(stream
, image
);
4353 GpStatus WINGDIPAPI
GdipRemovePropertyItem(GpImage
*image
, PROPID propId
)
4357 TRACE("(%p,%u)\n", image
, propId
);
4360 return InvalidParameter
;
4363 FIXME("not implemented\n");
4365 return NotImplemented
;
4368 GpStatus WINGDIPAPI
GdipSetPropertyItem(GpImage
*image
, GDIPCONST PropertyItem
* item
)
4372 if (!image
|| !item
) return InvalidParameter
;
4374 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image
, item
, item
->id
, item
->type
, item
->length
, item
->value
);
4377 FIXME("not implemented\n");
4382 GpStatus WINGDIPAPI
GdipSaveImageToFile(GpImage
*image
, GDIPCONST WCHAR
* filename
,
4383 GDIPCONST CLSID
*clsidEncoder
,
4384 GDIPCONST EncoderParameters
*encoderParams
)
4389 TRACE("%p (%s) %p %p\n", image
, debugstr_w(filename
), clsidEncoder
, encoderParams
);
4391 if (!image
|| !filename
|| !clsidEncoder
)
4392 return InvalidParameter
;
4394 stat
= GdipCreateStreamOnFile(filename
, GENERIC_WRITE
, &stream
);
4396 return GenericError
;
4398 stat
= GdipSaveImageToStream(image
, stream
, clsidEncoder
, encoderParams
);
4400 IStream_Release(stream
);
4404 /*************************************************************************
4405 * Encoding functions -
4406 * These functions encode an image in different image file formats.
4409 static GpStatus
encode_image_wic(GpImage
*image
, IStream
* stream
,
4410 REFGUID container
, GDIPCONST EncoderParameters
* params
)
4414 IWICImagingFactory
*factory
;
4415 IWICBitmapEncoder
*encoder
;
4416 IWICBitmapFrameEncode
*frameencode
;
4417 IPropertyBag2
*encoderoptions
;
4420 PixelFormat gdipformat
=0;
4421 const WICPixelFormatGUID
*desired_wicformat
;
4422 WICPixelFormatGUID wicformat
;
4424 BitmapData lockeddata
;
4427 if (image
->type
!= ImageTypeBitmap
)
4428 return GenericError
;
4430 bitmap
= (GpBitmap
*)image
;
4432 GdipGetImageWidth(image
, &width
);
4433 GdipGetImageHeight(image
, &height
);
4440 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
4442 return hresult_to_status(hr
);
4443 hr
= IWICImagingFactory_CreateEncoder(factory
, container
, NULL
, &encoder
);
4444 IWICImagingFactory_Release(factory
);
4446 return hresult_to_status(hr
);
4448 hr
= IWICBitmapEncoder_Initialize(encoder
, stream
, WICBitmapEncoderNoCache
);
4452 hr
= IWICBitmapEncoder_CreateNewFrame(encoder
, &frameencode
, &encoderoptions
);
4455 if (SUCCEEDED(hr
)) /* created frame */
4457 hr
= IWICBitmapFrameEncode_Initialize(frameencode
, encoderoptions
);
4460 hr
= IWICBitmapFrameEncode_SetSize(frameencode
, width
, height
);
4463 hr
= IWICBitmapFrameEncode_SetResolution(frameencode
, image
->xres
, image
->yres
);
4467 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
4469 if (pixel_formats
[i
].gdip_format
== bitmap
->format
)
4471 desired_wicformat
= pixel_formats
[i
].wic_format
;
4472 gdipformat
= bitmap
->format
;
4478 desired_wicformat
= &GUID_WICPixelFormat32bppBGRA
;
4479 gdipformat
= PixelFormat32bppARGB
;
4482 memcpy(&wicformat
, desired_wicformat
, sizeof(GUID
));
4483 hr
= IWICBitmapFrameEncode_SetPixelFormat(frameencode
, &wicformat
);
4486 if (SUCCEEDED(hr
) && !IsEqualGUID(desired_wicformat
, &wicformat
))
4488 /* Encoder doesn't support this bitmap's format. */
4490 for (i
=0; pixel_formats
[i
].wic_format
; i
++)
4492 if (IsEqualGUID(&wicformat
, pixel_formats
[i
].wic_format
))
4494 gdipformat
= pixel_formats
[i
].gdip_format
;
4500 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat
));
4507 stat
= GdipBitmapLockBits(bitmap
, &rc
, ImageLockModeRead
, gdipformat
,
4512 UINT row_size
= (lockeddata
.Width
* PIXELFORMATBPP(gdipformat
) + 7)/8;
4515 /* write one row at a time in case stride is negative */
4516 row
= lockeddata
.Scan0
;
4517 for (i
=0; i
<lockeddata
.Height
; i
++)
4519 hr
= IWICBitmapFrameEncode_WritePixels(frameencode
, 1, row_size
, row_size
, row
);
4520 if (FAILED(hr
)) break;
4521 row
+= lockeddata
.Stride
;
4524 GdipBitmapUnlockBits(bitmap
, &lockeddata
);
4531 hr
= IWICBitmapFrameEncode_Commit(frameencode
);
4533 IWICBitmapFrameEncode_Release(frameencode
);
4534 IPropertyBag2_Release(encoderoptions
);
4538 hr
= IWICBitmapEncoder_Commit(encoder
);
4540 IWICBitmapEncoder_Release(encoder
);
4541 return hresult_to_status(hr
);
4544 static GpStatus
encode_image_BMP(GpImage
*image
, IStream
* stream
,
4545 GDIPCONST EncoderParameters
* params
)
4547 return encode_image_wic(image
, stream
, &GUID_ContainerFormatBmp
, params
);
4550 static GpStatus
encode_image_tiff(GpImage
*image
, IStream
* stream
,
4551 GDIPCONST EncoderParameters
* params
)
4553 return encode_image_wic(image
, stream
, &GUID_ContainerFormatTiff
, params
);
4556 GpStatus
encode_image_png(GpImage
*image
, IStream
* stream
,
4557 GDIPCONST EncoderParameters
* params
)
4559 return encode_image_wic(image
, stream
, &GUID_ContainerFormatPng
, params
);
4562 static GpStatus
encode_image_jpeg(GpImage
*image
, IStream
* stream
,
4563 GDIPCONST EncoderParameters
* params
)
4565 return encode_image_wic(image
, stream
, &GUID_ContainerFormatJpeg
, params
);
4568 static GpStatus
encode_image_gif(GpImage
*image
, IStream
* stream
,
4569 GDIPCONST EncoderParameters
* params
)
4571 return encode_image_wic(image
, stream
, &GUID_ContainerFormatGif
, params
);
4574 /*****************************************************************************
4575 * GdipSaveImageToStream [GDIPLUS.@]
4577 GpStatus WINGDIPAPI
GdipSaveImageToStream(GpImage
*image
, IStream
* stream
,
4578 GDIPCONST CLSID
* clsid
, GDIPCONST EncoderParameters
* params
)
4581 encode_image_func encode_image
;
4584 TRACE("%p %p %s %p\n", image
, stream
, wine_dbgstr_guid(clsid
), params
);
4586 if(!image
|| !stream
)
4587 return InvalidParameter
;
4589 /* select correct encoder */
4590 encode_image
= NULL
;
4591 for (i
= 0; i
< NUM_CODECS
; i
++) {
4592 if ((codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
) &&
4593 IsEqualCLSID(clsid
, &codecs
[i
].info
.Clsid
))
4594 encode_image
= codecs
[i
].encode_func
;
4596 if (encode_image
== NULL
)
4597 return UnknownImageFormat
;
4599 stat
= encode_image(image
, stream
, params
);
4604 /*****************************************************************************
4605 * GdipSaveAdd [GDIPLUS.@]
4607 GpStatus WINGDIPAPI
GdipSaveAdd(GpImage
*image
, GDIPCONST EncoderParameters
*params
)
4609 FIXME("(%p,%p): stub\n", image
, params
);
4613 /*****************************************************************************
4614 * GdipGetImagePalette [GDIPLUS.@]
4616 GpStatus WINGDIPAPI
GdipGetImagePalette(GpImage
*image
, ColorPalette
*palette
, INT size
)
4620 TRACE("(%p,%p,%i)\n", image
, palette
, size
);
4622 if (!image
|| !palette
)
4623 return InvalidParameter
;
4625 count
= image
->palette
? image
->palette
->Count
: 0;
4627 if (size
< (sizeof(UINT
)*2+sizeof(ARGB
)*count
))
4629 TRACE("<-- InsufficientBuffer\n");
4630 return InsufficientBuffer
;
4635 palette
->Flags
= image
->palette
->Flags
;
4636 palette
->Count
= image
->palette
->Count
;
4637 memcpy(palette
->Entries
, image
->palette
->Entries
, sizeof(ARGB
)*image
->palette
->Count
);
4647 /*****************************************************************************
4648 * GdipSetImagePalette [GDIPLUS.@]
4650 GpStatus WINGDIPAPI
GdipSetImagePalette(GpImage
*image
,
4651 GDIPCONST ColorPalette
*palette
)
4653 ColorPalette
*new_palette
;
4655 TRACE("(%p,%p)\n", image
, palette
);
4657 if(!image
|| !palette
|| palette
->Count
> 256)
4658 return InvalidParameter
;
4660 new_palette
= heap_alloc_zero(2 * sizeof(UINT
) + palette
->Count
* sizeof(ARGB
));
4661 if (!new_palette
) return OutOfMemory
;
4663 heap_free(image
->palette
);
4664 image
->palette
= new_palette
;
4665 image
->palette
->Flags
= palette
->Flags
;
4666 image
->palette
->Count
= palette
->Count
;
4667 memcpy(image
->palette
->Entries
, palette
->Entries
, sizeof(ARGB
)*palette
->Count
);
4672 /*************************************************************************
4674 * Structures that represent which formats we support for encoding.
4677 /* ImageCodecInfo creation routines taken from libgdiplus */
4678 static const WCHAR bmp_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4679 static const WCHAR bmp_extension
[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4680 static const WCHAR bmp_mimetype
[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4681 static const WCHAR bmp_format
[] = {'B', 'M', 'P', 0}; /* BMP */
4682 static const BYTE bmp_sig_pattern
[] = { 0x42, 0x4D };
4683 static const BYTE bmp_sig_mask
[] = { 0xFF, 0xFF };
4685 static const WCHAR jpeg_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4686 static const WCHAR jpeg_extension
[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4687 static const WCHAR jpeg_mimetype
[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4688 static const WCHAR jpeg_format
[] = {'J','P','E','G',0};
4689 static const BYTE jpeg_sig_pattern
[] = { 0xFF, 0xD8 };
4690 static const BYTE jpeg_sig_mask
[] = { 0xFF, 0xFF };
4692 static const WCHAR gif_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4693 static const WCHAR gif_extension
[] = {'*','.','G','I','F',0};
4694 static const WCHAR gif_mimetype
[] = {'i','m','a','g','e','/','g','i','f', 0};
4695 static const WCHAR gif_format
[] = {'G','I','F',0};
4696 static const BYTE gif_sig_pattern
[12] = "GIF87aGIF89a";
4697 static const BYTE gif_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4699 static const WCHAR tiff_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4700 static const WCHAR tiff_extension
[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4701 static const WCHAR tiff_mimetype
[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4702 static const WCHAR tiff_format
[] = {'T','I','F','F',0};
4703 static const BYTE tiff_sig_pattern
[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4704 static const BYTE tiff_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4706 static const WCHAR emf_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4707 static const WCHAR emf_extension
[] = {'*','.','E','M','F',0};
4708 static const WCHAR emf_mimetype
[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4709 static const WCHAR emf_format
[] = {'E','M','F',0};
4710 static const BYTE emf_sig_pattern
[] = { 0x01, 0x00, 0x00, 0x00 };
4711 static const BYTE emf_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4713 static const WCHAR wmf_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4714 static const WCHAR wmf_extension
[] = {'*','.','W','M','F',0};
4715 static const WCHAR wmf_mimetype
[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4716 static const WCHAR wmf_format
[] = {'W','M','F',0};
4717 static const BYTE wmf_sig_pattern
[] = { 0xd7, 0xcd };
4718 static const BYTE wmf_sig_mask
[] = { 0xFF, 0xFF };
4720 static const WCHAR png_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4721 static const WCHAR png_extension
[] = {'*','.','P','N','G',0};
4722 static const WCHAR png_mimetype
[] = {'i','m','a','g','e','/','p','n','g', 0};
4723 static const WCHAR png_format
[] = {'P','N','G',0};
4724 static const BYTE png_sig_pattern
[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4725 static const BYTE png_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4727 static const WCHAR ico_codecname
[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4728 static const WCHAR ico_extension
[] = {'*','.','I','C','O',0};
4729 static const WCHAR ico_mimetype
[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4730 static const WCHAR ico_format
[] = {'I','C','O',0};
4731 static const BYTE ico_sig_pattern
[] = { 0x00, 0x00, 0x01, 0x00 };
4732 static const BYTE ico_sig_mask
[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4734 static const struct image_codec codecs
[NUM_CODECS
] = {
4737 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4738 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4739 /* CodecName */ bmp_codecname
,
4741 /* FormatDescription */ bmp_format
,
4742 /* FilenameExtension */ bmp_extension
,
4743 /* MimeType */ bmp_mimetype
,
4744 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4748 /* SigPattern */ bmp_sig_pattern
,
4749 /* SigMask */ bmp_sig_mask
,
4757 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4758 /* FormatID */ { 0xb96b3caeU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4759 /* CodecName */ jpeg_codecname
,
4761 /* FormatDescription */ jpeg_format
,
4762 /* FilenameExtension */ jpeg_extension
,
4763 /* MimeType */ jpeg_mimetype
,
4764 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4768 /* SigPattern */ jpeg_sig_pattern
,
4769 /* SigMask */ jpeg_sig_mask
,
4777 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4778 /* FormatID */ { 0xb96b3cb0U
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4779 /* CodecName */ gif_codecname
,
4781 /* FormatDescription */ gif_format
,
4782 /* FilenameExtension */ gif_extension
,
4783 /* MimeType */ gif_mimetype
,
4784 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsEncoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4788 /* SigPattern */ gif_sig_pattern
,
4789 /* SigMask */ gif_sig_mask
,
4797 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4798 /* FormatID */ { 0xb96b3cb1U
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4799 /* CodecName */ tiff_codecname
,
4801 /* FormatDescription */ tiff_format
,
4802 /* FilenameExtension */ tiff_extension
,
4803 /* MimeType */ tiff_mimetype
,
4804 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsEncoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4808 /* SigPattern */ tiff_sig_pattern
,
4809 /* SigMask */ tiff_sig_mask
,
4817 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4818 /* FormatID */ { 0xb96b3cacU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4819 /* CodecName */ emf_codecname
,
4821 /* FormatDescription */ emf_format
,
4822 /* FilenameExtension */ emf_extension
,
4823 /* MimeType */ emf_mimetype
,
4824 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportVector
| ImageCodecFlagsBuiltin
,
4828 /* SigPattern */ emf_sig_pattern
,
4829 /* SigMask */ emf_sig_mask
,
4837 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4838 /* FormatID */ { 0xb96b3cadU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4839 /* CodecName */ wmf_codecname
,
4841 /* FormatDescription */ wmf_format
,
4842 /* FilenameExtension */ wmf_extension
,
4843 /* MimeType */ wmf_mimetype
,
4844 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportVector
| ImageCodecFlagsBuiltin
,
4848 /* SigPattern */ wmf_sig_pattern
,
4849 /* SigMask */ wmf_sig_mask
,
4857 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4858 /* FormatID */ { 0xb96b3cafU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4859 /* CodecName */ png_codecname
,
4861 /* FormatDescription */ png_format
,
4862 /* FilenameExtension */ png_extension
,
4863 /* MimeType */ png_mimetype
,
4864 /* Flags */ ImageCodecFlagsEncoder
| ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4868 /* SigPattern */ png_sig_pattern
,
4869 /* SigMask */ png_sig_mask
,
4877 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4878 /* FormatID */ { 0xb96b3cabU
, 0x0728U
, 0x11d3U
, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4879 /* CodecName */ ico_codecname
,
4881 /* FormatDescription */ ico_format
,
4882 /* FilenameExtension */ ico_extension
,
4883 /* MimeType */ ico_mimetype
,
4884 /* Flags */ ImageCodecFlagsDecoder
| ImageCodecFlagsSupportBitmap
| ImageCodecFlagsBuiltin
,
4888 /* SigPattern */ ico_sig_pattern
,
4889 /* SigMask */ ico_sig_mask
,
4897 /*****************************************************************************
4898 * GdipGetImageDecodersSize [GDIPLUS.@]
4900 GpStatus WINGDIPAPI
GdipGetImageDecodersSize(UINT
*numDecoders
, UINT
*size
)
4902 int decoder_count
=0;
4904 TRACE("%p %p\n", numDecoders
, size
);
4906 if (!numDecoders
|| !size
)
4907 return InvalidParameter
;
4909 for (i
=0; i
<NUM_CODECS
; i
++)
4911 if (codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
)
4915 *numDecoders
= decoder_count
;
4916 *size
= decoder_count
* sizeof(ImageCodecInfo
);
4921 /*****************************************************************************
4922 * GdipGetImageDecoders [GDIPLUS.@]
4924 GpStatus WINGDIPAPI
GdipGetImageDecoders(UINT numDecoders
, UINT size
, ImageCodecInfo
*decoders
)
4926 int i
, decoder_count
=0;
4927 TRACE("%u %u %p\n", numDecoders
, size
, decoders
);
4930 size
!= numDecoders
* sizeof(ImageCodecInfo
))
4931 return GenericError
;
4933 for (i
=0; i
<NUM_CODECS
; i
++)
4935 if (codecs
[i
].info
.Flags
& ImageCodecFlagsDecoder
)
4937 if (decoder_count
== numDecoders
) return GenericError
;
4938 memcpy(&decoders
[decoder_count
], &codecs
[i
].info
, sizeof(ImageCodecInfo
));
4943 if (decoder_count
< numDecoders
) return GenericError
;
4948 /*****************************************************************************
4949 * GdipGetImageEncodersSize [GDIPLUS.@]
4951 GpStatus WINGDIPAPI
GdipGetImageEncodersSize(UINT
*numEncoders
, UINT
*size
)
4953 int encoder_count
=0;
4955 TRACE("%p %p\n", numEncoders
, size
);
4957 if (!numEncoders
|| !size
)
4958 return InvalidParameter
;
4960 for (i
=0; i
<NUM_CODECS
; i
++)
4962 if (codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
)
4966 *numEncoders
= encoder_count
;
4967 *size
= encoder_count
* sizeof(ImageCodecInfo
);
4972 /*****************************************************************************
4973 * GdipGetImageEncoders [GDIPLUS.@]
4975 GpStatus WINGDIPAPI
GdipGetImageEncoders(UINT numEncoders
, UINT size
, ImageCodecInfo
*encoders
)
4977 int i
, encoder_count
=0;
4978 TRACE("%u %u %p\n", numEncoders
, size
, encoders
);
4981 size
!= numEncoders
* sizeof(ImageCodecInfo
))
4982 return GenericError
;
4984 for (i
=0; i
<NUM_CODECS
; i
++)
4986 if (codecs
[i
].info
.Flags
& ImageCodecFlagsEncoder
)
4988 if (encoder_count
== numEncoders
) return GenericError
;
4989 memcpy(&encoders
[encoder_count
], &codecs
[i
].info
, sizeof(ImageCodecInfo
));
4994 if (encoder_count
< numEncoders
) return GenericError
;
4999 GpStatus WINGDIPAPI
GdipGetEncoderParameterListSize(GpImage
*image
,
5000 GDIPCONST CLSID
* clsidEncoder
, UINT
*size
)
5004 TRACE("(%p,%s,%p)\n", image
, debugstr_guid(clsidEncoder
), size
);
5007 FIXME("not implemented\n");
5011 return NotImplemented
;
5014 static PixelFormat
get_16bpp_format(HBITMAP hbm
)
5020 hdc
= CreateCompatibleDC(NULL
);
5022 memset(&bmh
, 0, sizeof(bmh
));
5023 bmh
.bV4Size
= sizeof(bmh
);
5026 bmh
.bV4V4Compression
= BI_BITFIELDS
;
5027 bmh
.bV4BitCount
= 16;
5029 GetDIBits(hdc
, hbm
, 0, 0, NULL
, (BITMAPINFO
*)&bmh
, DIB_RGB_COLORS
);
5031 if (bmh
.bV4RedMask
== 0x7c00 &&
5032 bmh
.bV4GreenMask
== 0x3e0 &&
5033 bmh
.bV4BlueMask
== 0x1f)
5035 result
= PixelFormat16bppRGB555
;
5037 else if (bmh
.bV4RedMask
== 0xf800 &&
5038 bmh
.bV4GreenMask
== 0x7e0 &&
5039 bmh
.bV4BlueMask
== 0x1f)
5041 result
= PixelFormat16bppRGB565
;
5045 FIXME("unrecognized bitfields %x,%x,%x\n", bmh
.bV4RedMask
,
5046 bmh
.bV4GreenMask
, bmh
.bV4BlueMask
);
5047 result
= PixelFormatUndefined
;
5055 /*****************************************************************************
5056 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
5058 GpStatus WINGDIPAPI
GdipCreateBitmapFromHBITMAP(HBITMAP hbm
, HPALETTE hpal
, GpBitmap
** bitmap
)
5063 BitmapData lockeddata
;
5065 TRACE("%p %p %p\n", hbm
, hpal
, bitmap
);
5068 return InvalidParameter
;
5070 if (GetObjectA(hbm
, sizeof(bm
), &bm
) != sizeof(bm
))
5071 return InvalidParameter
;
5073 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
5074 switch(bm
.bmBitsPixel
) {
5076 format
= PixelFormat1bppIndexed
;
5079 format
= PixelFormat4bppIndexed
;
5082 format
= PixelFormat8bppIndexed
;
5085 format
= get_16bpp_format(hbm
);
5086 if (format
== PixelFormatUndefined
)
5087 return InvalidParameter
;
5090 format
= PixelFormat24bppRGB
;
5093 format
= PixelFormat32bppRGB
;
5096 format
= PixelFormat48bppRGB
;
5099 FIXME("don't know how to handle %d bpp\n", bm
.bmBitsPixel
);
5100 return InvalidParameter
;
5103 retval
= GdipCreateBitmapFromScan0(bm
.bmWidth
, bm
.bmHeight
, 0,
5104 format
, NULL
, bitmap
);
5108 retval
= GdipBitmapLockBits(*bitmap
, NULL
, ImageLockModeWrite
,
5109 format
, &lockeddata
);
5113 char bmibuf
[FIELD_OFFSET(BITMAPINFO
, bmiColors
) + 256 * sizeof(RGBQUAD
)];
5114 BITMAPINFO
*pbmi
= (BITMAPINFO
*)bmibuf
;
5117 hdc
= CreateCompatibleDC(NULL
);
5119 pbmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
5120 pbmi
->bmiHeader
.biBitCount
= 0;
5122 GetDIBits(hdc
, hbm
, 0, 0, NULL
, pbmi
, DIB_RGB_COLORS
);
5124 src_height
= abs(pbmi
->bmiHeader
.biHeight
);
5125 pbmi
->bmiHeader
.biHeight
= -src_height
;
5127 GetDIBits(hdc
, hbm
, 0, src_height
, lockeddata
.Scan0
, pbmi
, DIB_RGB_COLORS
);
5131 GdipBitmapUnlockBits(*bitmap
, &lockeddata
);
5134 if (retval
== Ok
&& hpal
)
5136 PALETTEENTRY entry
[256];
5137 ColorPalette
*palette
=NULL
;
5138 int i
, num_palette_entries
;
5140 num_palette_entries
= GetPaletteEntries(hpal
, 0, 256, entry
);
5141 if (!num_palette_entries
)
5142 retval
= GenericError
;
5144 palette
= heap_alloc_zero(sizeof(ColorPalette
) + sizeof(ARGB
) * (num_palette_entries
-1));
5146 retval
= OutOfMemory
;
5151 palette
->Count
= num_palette_entries
;
5153 for (i
=0; i
<num_palette_entries
; i
++)
5155 palette
->Entries
[i
] = 0xff000000 | entry
[i
].peRed
<< 16 |
5156 entry
[i
].peGreen
<< 8 | entry
[i
].peBlue
;
5159 retval
= GdipSetImagePalette(&(*bitmap
)->image
, palette
);
5167 GdipDisposeImage(&(*bitmap
)->image
);
5175 /*****************************************************************************
5176 * GdipCreateEffect [GDIPLUS.@]
5178 GpStatus WINGDIPAPI
GdipCreateEffect(const GUID guid
, CGpEffect
**effect
)
5180 FIXME("(%s, %p): stub\n", debugstr_guid(&guid
), effect
);
5183 return InvalidParameter
;
5187 return NotImplemented
;
5190 /*****************************************************************************
5191 * GdipDeleteEffect [GDIPLUS.@]
5193 GpStatus WINGDIPAPI
GdipDeleteEffect(CGpEffect
*effect
)
5195 FIXME("(%p): stub\n", effect
);
5196 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
5197 * in Windows's gdiplus */
5198 return NotImplemented
;
5201 /*****************************************************************************
5202 * GdipSetEffectParameters [GDIPLUS.@]
5204 GpStatus WINGDIPAPI
GdipSetEffectParameters(CGpEffect
*effect
,
5205 const VOID
*params
, const UINT size
)
5209 TRACE("(%p,%p,%u)\n", effect
, params
, size
);
5212 FIXME("not implemented\n");
5214 return NotImplemented
;
5217 /*****************************************************************************
5218 * GdipGetImageFlags [GDIPLUS.@]
5220 GpStatus WINGDIPAPI
GdipGetImageFlags(GpImage
*image
, UINT
*flags
)
5222 TRACE("%p %p\n", image
, flags
);
5224 if(!image
|| !flags
)
5225 return InvalidParameter
;
5227 *flags
= image
->flags
;
5232 GpStatus WINGDIPAPI
GdipTestControl(GpTestControlEnum control
, void *param
)
5234 TRACE("(%d, %p)\n", control
, param
);
5237 case TestControlForceBilinear
:
5239 FIXME("TestControlForceBilinear not handled\n");
5241 case TestControlNoICM
:
5243 FIXME("TestControlNoICM not handled\n");
5245 case TestControlGetBuildNumber
:
5246 *((DWORD
*)param
) = 3102;
5253 GpStatus WINGDIPAPI
GdipImageForceValidation(GpImage
*image
)
5255 TRACE("%p\n", image
);
5260 /*****************************************************************************
5261 * GdipGetImageThumbnail [GDIPLUS.@]
5263 GpStatus WINGDIPAPI
GdipGetImageThumbnail(GpImage
*image
, UINT width
, UINT height
,
5264 GpImage
**ret_image
, GetThumbnailImageAbort cb
,
5268 GpGraphics
*graphics
;
5269 UINT srcwidth
, srcheight
;
5271 TRACE("(%p %u %u %p %p %p)\n",
5272 image
, width
, height
, ret_image
, cb
, cb_data
);
5274 if (!image
|| !ret_image
)
5275 return InvalidParameter
;
5277 if (!width
) width
= 120;
5278 if (!height
) height
= 120;
5280 GdipGetImageWidth(image
, &srcwidth
);
5281 GdipGetImageHeight(image
, &srcheight
);
5283 stat
= GdipCreateBitmapFromScan0(width
, height
, 0, PixelFormat32bppPARGB
,
5284 NULL
, (GpBitmap
**)ret_image
);
5288 stat
= GdipGetImageGraphicsContext(*ret_image
, &graphics
);
5292 stat
= GdipDrawImageRectRectI(graphics
, image
,
5293 0, 0, width
, height
, 0, 0, srcwidth
, srcheight
, UnitPixel
,
5296 GdipDeleteGraphics(graphics
);
5301 GdipDisposeImage(*ret_image
);
5309 /*****************************************************************************
5310 * GdipImageRotateFlip [GDIPLUS.@]
5312 GpStatus WINGDIPAPI
GdipImageRotateFlip(GpImage
*image
, RotateFlipType type
)
5314 GpBitmap
*new_bitmap
;
5316 int bpp
, bytesperpixel
;
5317 BOOL rotate_90
, flip_x
, flip_y
;
5318 int src_x_offset
, src_y_offset
;
5320 UINT x
, y
, width
, height
;
5321 BitmapData src_lock
, dst_lock
;
5325 TRACE("(%p, %u)\n", image
, type
);
5328 return InvalidParameter
;
5329 if (!image_lock(image
, &unlock
))
5333 flip_x
= (type
&6) == 2 || (type
&6) == 4;
5334 flip_y
= (type
&3) == 1 || (type
&3) == 2;
5336 if (image
->type
!= ImageTypeBitmap
)
5338 FIXME("Not implemented for type %i\n", image
->type
);
5339 image_unlock(image
, unlock
);
5340 return NotImplemented
;
5343 bitmap
= (GpBitmap
*)image
;
5344 bpp
= PIXELFORMATBPP(bitmap
->format
);
5348 FIXME("Not implemented for %i bit images\n", bpp
);
5349 image_unlock(image
, unlock
);
5350 return NotImplemented
;
5355 width
= bitmap
->height
;
5356 height
= bitmap
->width
;
5360 width
= bitmap
->width
;
5361 height
= bitmap
->height
;
5364 bytesperpixel
= bpp
/8;
5366 stat
= GdipCreateBitmapFromScan0(width
, height
, 0, bitmap
->format
, NULL
, &new_bitmap
);
5370 image_unlock(image
, unlock
);
5374 stat
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
, bitmap
->format
, &src_lock
);
5378 stat
= GdipBitmapLockBits(new_bitmap
, NULL
, ImageLockModeWrite
, bitmap
->format
, &dst_lock
);
5382 LPBYTE src_row
, src_pixel
;
5383 LPBYTE dst_row
, dst_pixel
;
5385 src_origin
= src_lock
.Scan0
;
5386 if (flip_x
) src_origin
+= bytesperpixel
* (bitmap
->width
- 1);
5387 if (flip_y
) src_origin
+= src_lock
.Stride
* (bitmap
->height
- 1);
5391 if (flip_y
) src_x_offset
= -src_lock
.Stride
;
5392 else src_x_offset
= src_lock
.Stride
;
5393 if (flip_x
) src_y_offset
= -bytesperpixel
;
5394 else src_y_offset
= bytesperpixel
;
5398 if (flip_x
) src_x_offset
= -bytesperpixel
;
5399 else src_x_offset
= bytesperpixel
;
5400 if (flip_y
) src_y_offset
= -src_lock
.Stride
;
5401 else src_y_offset
= src_lock
.Stride
;
5404 src_row
= src_origin
;
5405 dst_row
= dst_lock
.Scan0
;
5406 for (y
=0; y
<height
; y
++)
5408 src_pixel
= src_row
;
5409 dst_pixel
= dst_row
;
5410 for (x
=0; x
<width
; x
++)
5412 /* FIXME: This could probably be faster without memcpy. */
5413 memcpy(dst_pixel
, src_pixel
, bytesperpixel
);
5414 dst_pixel
+= bytesperpixel
;
5415 src_pixel
+= src_x_offset
;
5417 src_row
+= src_y_offset
;
5418 dst_row
+= dst_lock
.Stride
;
5421 GdipBitmapUnlockBits(new_bitmap
, &dst_lock
);
5424 GdipBitmapUnlockBits(bitmap
, &src_lock
);
5428 move_bitmap(bitmap
, new_bitmap
, FALSE
);
5430 GdipDisposeImage(&new_bitmap
->image
);
5432 image_unlock(image
, unlock
);
5436 /*****************************************************************************
5437 * GdipImageSetAbort [GDIPLUS.@]
5439 GpStatus WINGDIPAPI
GdipImageSetAbort(GpImage
*image
, GdiplusAbort
*pabort
)
5441 TRACE("(%p, %p)\n", image
, pabort
);
5444 return InvalidParameter
;
5447 FIXME("Abort callback is not supported.\n");
5452 /*****************************************************************************
5453 * GdipBitmapConvertFormat [GDIPLUS.@]
5455 GpStatus WINGDIPAPI
GdipBitmapConvertFormat(GpBitmap
*bitmap
, PixelFormat format
, DitherType dithertype
,
5456 PaletteType palettetype
, ColorPalette
*palette
, REAL alphathreshold
)
5458 FIXME("(%p, 0x%08x, %d, %d, %p, %f): stub\n", bitmap
, format
, dithertype
, palettetype
, palette
, alphathreshold
);
5459 return NotImplemented
;
5462 static void set_histogram_point_argb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5464 ch0
[ color
>> 24 ]++;
5465 ch1
[(color
>> 16) & 0xff]++;
5466 ch2
[(color
>> 8) & 0xff]++;
5467 ch3
[ color
& 0xff]++;
5470 static void set_histogram_point_pargb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5472 BYTE alpha
= color
>> 24;
5475 ch1
[(((color
>> 16) & 0xff) * alpha
) / 0xff]++;
5476 ch2
[(((color
>> 8) & 0xff) * alpha
) / 0xff]++;
5477 ch3
[(( color
& 0xff) * alpha
) / 0xff]++;
5480 static void set_histogram_point_rgb(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5482 ch0
[(color
>> 16) & 0xff]++;
5483 ch1
[(color
>> 8) & 0xff]++;
5484 ch2
[ color
& 0xff]++;
5487 static void set_histogram_point_gray(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5489 ch0
[(76 * ((color
>> 16) & 0xff) + 150 * ((color
>> 8) & 0xff) + 29 * (color
& 0xff)) / 0xff]++;
5492 static void set_histogram_point_b(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5494 ch0
[color
& 0xff]++;
5497 static void set_histogram_point_g(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5499 ch0
[(color
>> 8) & 0xff]++;
5502 static void set_histogram_point_r(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5504 ch0
[(color
>> 16) & 0xff]++;
5507 static void set_histogram_point_a(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5509 ch0
[(color
>> 24) & 0xff]++;
5512 /*****************************************************************************
5513 * GdipBitmapGetHistogram [GDIPLUS.@]
5515 GpStatus WINGDIPAPI
GdipBitmapGetHistogram(GpBitmap
*bitmap
, HistogramFormat format
, UINT num_of_entries
,
5516 UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
)
5518 static void (* const set_histogram_point
[])(ARGB color
, UINT
*ch0
, UINT
*ch1
, UINT
*ch2
, UINT
*ch3
) =
5520 set_histogram_point_argb
,
5521 set_histogram_point_pargb
,
5522 set_histogram_point_rgb
,
5523 set_histogram_point_gray
,
5524 set_histogram_point_b
,
5525 set_histogram_point_g
,
5526 set_histogram_point_r
,
5527 set_histogram_point_a
,
5529 UINT width
, height
, x
, y
;
5531 TRACE("(%p, %d, %u, %p, %p, %p, %p)\n", bitmap
, format
, num_of_entries
,
5532 ch0
, ch1
, ch2
, ch3
);
5534 if (!bitmap
|| num_of_entries
!= 256)
5535 return InvalidParameter
;
5537 /* Make sure passed channel pointers match requested format */
5540 case HistogramFormatARGB
:
5541 case HistogramFormatPARGB
:
5542 if (!ch0
|| !ch1
|| !ch2
|| !ch3
)
5543 return InvalidParameter
;
5544 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5545 memset(ch1
, 0, num_of_entries
* sizeof(UINT
));
5546 memset(ch2
, 0, num_of_entries
* sizeof(UINT
));
5547 memset(ch3
, 0, num_of_entries
* sizeof(UINT
));
5549 case HistogramFormatRGB
:
5550 if (!ch0
|| !ch1
|| !ch2
|| ch3
)
5551 return InvalidParameter
;
5552 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5553 memset(ch1
, 0, num_of_entries
* sizeof(UINT
));
5554 memset(ch2
, 0, num_of_entries
* sizeof(UINT
));
5556 case HistogramFormatGray
:
5557 case HistogramFormatB
:
5558 case HistogramFormatG
:
5559 case HistogramFormatR
:
5560 case HistogramFormatA
:
5561 if (!ch0
|| ch1
|| ch2
|| ch3
)
5562 return InvalidParameter
;
5563 memset(ch0
, 0, num_of_entries
* sizeof(UINT
));
5566 WARN("Invalid histogram format requested, %d\n", format
);
5567 return InvalidParameter
;
5570 GdipGetImageWidth(&bitmap
->image
, &width
);
5571 GdipGetImageHeight(&bitmap
->image
, &height
);
5573 for (y
= 0; y
< height
; y
++)
5574 for (x
= 0; x
< width
; x
++)
5578 GdipBitmapGetPixel(bitmap
, x
, y
, &color
);
5579 set_histogram_point
[format
](color
, ch0
, ch1
, ch2
, ch3
);
5585 /*****************************************************************************
5586 * GdipBitmapGetHistogramSize [GDIPLUS.@]
5588 GpStatus WINGDIPAPI
GdipBitmapGetHistogramSize(HistogramFormat format
, UINT
*num_of_entries
)
5590 TRACE("(%d, %p)\n", format
, num_of_entries
);
5592 if (!num_of_entries
)
5593 return InvalidParameter
;
5595 *num_of_entries
= 256;
5599 static GpStatus
create_optimal_palette(ColorPalette
*palette
, INT desired
,
5600 BOOL transparent
, GpBitmap
*bitmap
)
5605 IWICImagingFactory
*factory
;
5606 IWICPalette
*wic_palette
;
5608 if (!bitmap
) return InvalidParameter
;
5609 if (palette
->Count
< desired
) return GenericError
;
5611 status
= GdipBitmapLockBits(bitmap
, NULL
, ImageLockModeRead
, PixelFormat24bppRGB
, &data
);
5612 if (status
!= Ok
) return status
;
5614 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
5617 GdipBitmapUnlockBits(bitmap
, &data
);
5618 return hresult_to_status(hr
);
5621 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
5626 /* PixelFormat24bppRGB actually stores the bitmap bits as BGR. */
5627 hr
= IWICImagingFactory_CreateBitmapFromMemory(factory
, data
.Width
, data
.Height
,
5628 &GUID_WICPixelFormat24bppBGR
, data
.Stride
, data
.Stride
* data
.Width
, data
.Scan0
, &bitmap
);
5631 hr
= IWICPalette_InitializeFromBitmap(wic_palette
, (IWICBitmapSource
*)bitmap
, desired
, transparent
);
5635 IWICPalette_GetColorCount(wic_palette
, &palette
->Count
);
5636 IWICPalette_GetColors(wic_palette
, palette
->Count
, palette
->Entries
, &palette
->Count
);
5639 IWICBitmap_Release(bitmap
);
5642 IWICPalette_Release(wic_palette
);
5645 IWICImagingFactory_Release(factory
);
5646 GdipBitmapUnlockBits(bitmap
, &data
);
5648 return hresult_to_status(hr
);
5651 /*****************************************************************************
5652 * GdipInitializePalette [GDIPLUS.@]
5654 GpStatus WINGDIPAPI
GdipInitializePalette(ColorPalette
*palette
,
5655 PaletteType type
, INT desired
, BOOL transparent
, GpBitmap
*bitmap
)
5657 TRACE("(%p,%d,%d,%d,%p)\n", palette
, type
, desired
, transparent
, bitmap
);
5659 if (!palette
) return InvalidParameter
;
5663 case PaletteTypeCustom
:
5666 case PaletteTypeOptimal
:
5667 return create_optimal_palette(palette
, desired
, transparent
, bitmap
);
5669 /* WIC palette type enumeration matches these gdiplus enums */
5670 case PaletteTypeFixedBW
:
5671 case PaletteTypeFixedHalftone8
:
5672 case PaletteTypeFixedHalftone27
:
5673 case PaletteTypeFixedHalftone64
:
5674 case PaletteTypeFixedHalftone125
:
5675 case PaletteTypeFixedHalftone216
:
5676 case PaletteTypeFixedHalftone252
:
5677 case PaletteTypeFixedHalftone256
:
5679 ColorPalette
*wic_palette
;
5680 GpStatus status
= Ok
;
5682 wic_palette
= get_palette(NULL
, type
);
5683 if (!wic_palette
) return OutOfMemory
;
5685 if (palette
->Count
>= wic_palette
->Count
)
5687 palette
->Flags
= wic_palette
->Flags
;
5688 palette
->Count
= wic_palette
->Count
;
5689 memcpy(palette
->Entries
, wic_palette
->Entries
, wic_palette
->Count
* sizeof(wic_palette
->Entries
[0]));
5692 status
= GenericError
;
5694 heap_free(wic_palette
);
5700 FIXME("unknown palette type %d\n", type
);
5704 return InvalidParameter
;