2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 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
21 #include "wine/port.h"
29 #define NONAMELESSUNION
36 #include "wincodecs_private.h"
38 #include "wine/debug.h"
39 #include "wine/library.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
43 static inline ULONG
read_ulong_be(BYTE
* data
)
45 return data
[0] << 24 | data
[1] << 16 | data
[2] << 8 | data
[3];
48 static HRESULT
read_png_chunk(IStream
*stream
, BYTE
*type
, BYTE
**data
, ULONG
*data_size
)
54 hr
= IStream_Read(stream
, header
, 8, &bytesread
);
55 if (FAILED(hr
) || bytesread
< 8)
62 *data_size
= read_ulong_be(&header
[0]);
64 memcpy(type
, &header
[4], 4);
68 *data
= HeapAlloc(GetProcessHeap(), 0, *data_size
);
72 hr
= IStream_Read(stream
, *data
, *data_size
, &bytesread
);
74 if (FAILED(hr
) || bytesread
< *data_size
)
78 HeapFree(GetProcessHeap(), 0, *data
);
83 /* Windows ignores CRC of the chunk */
89 static HRESULT
LoadTextMetadata(IStream
*stream
, const GUID
*preferred_vendor
,
90 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
96 ULONG name_len
, value_len
;
101 hr
= read_png_chunk(stream
, type
, &data
, &data_size
);
102 if (FAILED(hr
)) return hr
;
104 name_end_ptr
= memchr(data
, 0, data_size
);
106 name_len
= name_end_ptr
- data
;
108 if (!name_end_ptr
|| name_len
> 79)
110 HeapFree(GetProcessHeap(), 0, data
);
114 value_len
= data_size
- name_len
- 1;
116 result
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MetadataItem
));
117 name
= HeapAlloc(GetProcessHeap(), 0, name_len
+ 1);
118 value
= HeapAlloc(GetProcessHeap(), 0, value_len
+ 1);
119 if (!result
|| !name
|| !value
)
121 HeapFree(GetProcessHeap(), 0, data
);
122 HeapFree(GetProcessHeap(), 0, result
);
123 HeapFree(GetProcessHeap(), 0, name
);
124 HeapFree(GetProcessHeap(), 0, value
);
125 return E_OUTOFMEMORY
;
128 PropVariantInit(&result
[0].schema
);
129 PropVariantInit(&result
[0].id
);
130 PropVariantInit(&result
[0].value
);
132 memcpy(name
, data
, name_len
+ 1);
133 memcpy(value
, name_end_ptr
+ 1, value_len
);
134 value
[value_len
] = 0;
136 result
[0].id
.vt
= VT_LPSTR
;
137 result
[0].id
.u
.pszVal
= name
;
138 result
[0].value
.vt
= VT_LPSTR
;
139 result
[0].value
.u
.pszVal
= value
;
144 HeapFree(GetProcessHeap(), 0, data
);
149 static const MetadataHandlerVtbl TextReader_Vtbl
= {
151 &CLSID_WICPngTextMetadataReader
,
155 HRESULT
PngTextReader_CreateInstance(REFIID iid
, void** ppv
)
157 return MetadataReader_Create(&TextReader_Vtbl
, iid
, ppv
);
160 static HRESULT
LoadGamaMetadata(IStream
*stream
, const GUID
*preferred_vendor
,
161 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
168 static const WCHAR ImageGamma
[] = {'I','m','a','g','e','G','a','m','m','a',0};
170 MetadataItem
*result
;
172 hr
= read_png_chunk(stream
, type
, &data
, &data_size
);
173 if (FAILED(hr
)) return hr
;
177 HeapFree(GetProcessHeap(), 0, data
);
181 gamma
= read_ulong_be(data
);
183 HeapFree(GetProcessHeap(), 0, data
);
185 result
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MetadataItem
));
186 name
= HeapAlloc(GetProcessHeap(), 0, sizeof(ImageGamma
));
187 if (!result
|| !name
)
189 HeapFree(GetProcessHeap(), 0, result
);
190 HeapFree(GetProcessHeap(), 0, name
);
191 return E_OUTOFMEMORY
;
194 PropVariantInit(&result
[0].schema
);
195 PropVariantInit(&result
[0].id
);
196 PropVariantInit(&result
[0].value
);
198 memcpy(name
, ImageGamma
, sizeof(ImageGamma
));
200 result
[0].id
.vt
= VT_LPWSTR
;
201 result
[0].id
.u
.pwszVal
= name
;
202 result
[0].value
.vt
= VT_UI4
;
203 result
[0].value
.u
.ulVal
= gamma
;
211 static const MetadataHandlerVtbl GamaReader_Vtbl
= {
213 &CLSID_WICPngGamaMetadataReader
,
217 HRESULT
PngGamaReader_CreateInstance(REFIID iid
, void** ppv
)
219 return MetadataReader_Create(&GamaReader_Vtbl
, iid
, ppv
);
222 static HRESULT
LoadChrmMetadata(IStream
*stream
, const GUID
*preferred_vendor
,
223 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
229 static const WCHAR names
[8][12] = {
230 {'W','h','i','t','e','P','o','i','n','t','X',0},
231 {'W','h','i','t','e','P','o','i','n','t','Y',0},
234 {'G','r','e','e','n','X',0},
235 {'G','r','e','e','n','Y',0},
236 {'B','l','u','e','X',0},
237 {'B','l','u','e','Y',0},
239 LPWSTR dyn_names
[8] = {0};
240 MetadataItem
*result
;
243 hr
= read_png_chunk(stream
, type
, &data
, &data_size
);
244 if (FAILED(hr
)) return hr
;
248 HeapFree(GetProcessHeap(), 0, data
);
252 result
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(MetadataItem
)*8);
255 dyn_names
[i
] = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
)*(lstrlenW(names
[i
])+1));
256 if (!dyn_names
[i
]) break;
258 if (!result
|| i
< 8)
260 HeapFree(GetProcessHeap(), 0, result
);
262 HeapFree(GetProcessHeap(), 0, dyn_names
[i
]);
263 HeapFree(GetProcessHeap(), 0, data
);
264 return E_OUTOFMEMORY
;
269 PropVariantInit(&result
[i
].schema
);
271 PropVariantInit(&result
[i
].id
);
272 result
[i
].id
.vt
= VT_LPWSTR
;
273 result
[i
].id
.u
.pwszVal
= dyn_names
[i
];
274 lstrcpyW(dyn_names
[i
], names
[i
]);
276 PropVariantInit(&result
[i
].value
);
277 result
[i
].value
.vt
= VT_UI4
;
278 result
[i
].value
.u
.ulVal
= read_ulong_be(&data
[i
*4]);
284 HeapFree(GetProcessHeap(), 0, data
);
289 static const MetadataHandlerVtbl ChrmReader_Vtbl
= {
291 &CLSID_WICPngChrmMetadataReader
,
295 HRESULT
PngChrmReader_CreateInstance(REFIID iid
, void** ppv
)
297 return MetadataReader_Create(&ChrmReader_Vtbl
, iid
, ppv
);
302 static void *libpng_handle
;
303 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
304 MAKE_FUNCPTR(png_create_read_struct
);
305 MAKE_FUNCPTR(png_create_info_struct
);
306 MAKE_FUNCPTR(png_create_write_struct
);
307 MAKE_FUNCPTR(png_destroy_read_struct
);
308 MAKE_FUNCPTR(png_destroy_write_struct
);
309 MAKE_FUNCPTR(png_error
);
310 MAKE_FUNCPTR(png_get_bit_depth
);
311 MAKE_FUNCPTR(png_get_color_type
);
312 MAKE_FUNCPTR(png_get_error_ptr
);
313 MAKE_FUNCPTR(png_get_iCCP
);
314 MAKE_FUNCPTR(png_get_image_height
);
315 MAKE_FUNCPTR(png_get_image_width
);
316 MAKE_FUNCPTR(png_get_io_ptr
);
317 MAKE_FUNCPTR(png_get_pHYs
);
318 MAKE_FUNCPTR(png_get_PLTE
);
319 MAKE_FUNCPTR(png_get_tRNS
);
320 MAKE_FUNCPTR(png_set_bgr
);
321 MAKE_FUNCPTR(png_set_crc_action
);
322 MAKE_FUNCPTR(png_set_error_fn
);
323 MAKE_FUNCPTR(png_set_filler
);
324 MAKE_FUNCPTR(png_set_filter
);
325 MAKE_FUNCPTR(png_set_gray_to_rgb
);
326 MAKE_FUNCPTR(png_set_interlace_handling
);
327 MAKE_FUNCPTR(png_set_IHDR
);
328 MAKE_FUNCPTR(png_set_pHYs
);
329 MAKE_FUNCPTR(png_set_PLTE
);
330 MAKE_FUNCPTR(png_set_read_fn
);
331 MAKE_FUNCPTR(png_set_strip_16
);
332 MAKE_FUNCPTR(png_set_tRNS
);
333 MAKE_FUNCPTR(png_set_tRNS_to_alpha
);
334 MAKE_FUNCPTR(png_set_write_fn
);
335 MAKE_FUNCPTR(png_read_end
);
336 MAKE_FUNCPTR(png_read_image
);
337 MAKE_FUNCPTR(png_read_info
);
338 MAKE_FUNCPTR(png_write_end
);
339 MAKE_FUNCPTR(png_write_info
);
340 MAKE_FUNCPTR(png_write_rows
);
343 static CRITICAL_SECTION init_png_cs
;
344 static CRITICAL_SECTION_DEBUG init_png_cs_debug
=
347 { &init_png_cs_debug
.ProcessLocksList
,
348 &init_png_cs_debug
.ProcessLocksList
},
349 0, 0, { (DWORD_PTR
)(__FILE__
": init_png_cs") }
351 static CRITICAL_SECTION init_png_cs
= { &init_png_cs_debug
, -1, 0, 0, 0, 0 };
353 static const WCHAR wszPngInterlaceOption
[] = {'I','n','t','e','r','l','a','c','e','O','p','t','i','o','n',0};
354 static const WCHAR wszPngFilterOption
[] = {'F','i','l','t','e','r','O','p','t','i','o','n',0};
356 static void *load_libpng(void)
360 EnterCriticalSection(&init_png_cs
);
362 if(!libpng_handle
&& (libpng_handle
= wine_dlopen(SONAME_LIBPNG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
364 #define LOAD_FUNCPTR(f) \
365 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
366 libpng_handle = NULL; \
367 LeaveCriticalSection(&init_png_cs); \
370 LOAD_FUNCPTR(png_create_read_struct
);
371 LOAD_FUNCPTR(png_create_info_struct
);
372 LOAD_FUNCPTR(png_create_write_struct
);
373 LOAD_FUNCPTR(png_destroy_read_struct
);
374 LOAD_FUNCPTR(png_destroy_write_struct
);
375 LOAD_FUNCPTR(png_error
);
376 LOAD_FUNCPTR(png_get_bit_depth
);
377 LOAD_FUNCPTR(png_get_color_type
);
378 LOAD_FUNCPTR(png_get_error_ptr
);
379 LOAD_FUNCPTR(png_get_iCCP
);
380 LOAD_FUNCPTR(png_get_image_height
);
381 LOAD_FUNCPTR(png_get_image_width
);
382 LOAD_FUNCPTR(png_get_io_ptr
);
383 LOAD_FUNCPTR(png_get_pHYs
);
384 LOAD_FUNCPTR(png_get_PLTE
);
385 LOAD_FUNCPTR(png_get_tRNS
);
386 LOAD_FUNCPTR(png_set_bgr
);
387 LOAD_FUNCPTR(png_set_crc_action
);
388 LOAD_FUNCPTR(png_set_error_fn
);
389 LOAD_FUNCPTR(png_set_filler
);
390 LOAD_FUNCPTR(png_set_filter
);
391 LOAD_FUNCPTR(png_set_gray_to_rgb
);
392 LOAD_FUNCPTR(png_set_interlace_handling
);
393 LOAD_FUNCPTR(png_set_IHDR
);
394 LOAD_FUNCPTR(png_set_pHYs
);
395 LOAD_FUNCPTR(png_set_PLTE
);
396 LOAD_FUNCPTR(png_set_read_fn
);
397 LOAD_FUNCPTR(png_set_strip_16
);
398 LOAD_FUNCPTR(png_set_tRNS
);
399 LOAD_FUNCPTR(png_set_tRNS_to_alpha
);
400 LOAD_FUNCPTR(png_set_write_fn
);
401 LOAD_FUNCPTR(png_read_end
);
402 LOAD_FUNCPTR(png_read_image
);
403 LOAD_FUNCPTR(png_read_info
);
404 LOAD_FUNCPTR(png_write_end
);
405 LOAD_FUNCPTR(png_write_info
);
406 LOAD_FUNCPTR(png_write_rows
);
411 result
= libpng_handle
;
413 LeaveCriticalSection(&init_png_cs
);
418 static void user_error_fn(png_structp png_ptr
, png_const_charp error_message
)
422 /* This uses setjmp/longjmp just like the default. We can't use the
423 * default because there's no way to access the jmp buffer in the png_struct
424 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
425 WARN("PNG error: %s\n", debugstr_a(error_message
));
426 pjmpbuf
= ppng_get_error_ptr(png_ptr
);
427 longjmp(*pjmpbuf
, 1);
430 static void user_warning_fn(png_structp png_ptr
, png_const_charp warning_message
)
432 WARN("PNG warning: %s\n", debugstr_a(warning_message
));
436 ULARGE_INTEGER ofs
, len
;
437 IWICMetadataReader
* reader
;
438 } metadata_block_info
;
441 IWICBitmapDecoder IWICBitmapDecoder_iface
;
442 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
443 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
453 const WICPixelFormatGUID
*format
;
455 CRITICAL_SECTION lock
; /* must be held when png structures are accessed or initialized is set */
456 ULONG metadata_count
;
457 metadata_block_info
* metadata_blocks
;
460 static inline PngDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
462 return CONTAINING_RECORD(iface
, PngDecoder
, IWICBitmapDecoder_iface
);
465 static inline PngDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
467 return CONTAINING_RECORD(iface
, PngDecoder
, IWICBitmapFrameDecode_iface
);
470 static inline PngDecoder
*impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
472 return CONTAINING_RECORD(iface
, PngDecoder
, IWICMetadataBlockReader_iface
);
475 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
;
477 static HRESULT WINAPI
PngDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
480 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
481 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
483 if (!ppv
) return E_INVALIDARG
;
485 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
487 *ppv
= &This
->IWICBitmapDecoder_iface
;
492 return E_NOINTERFACE
;
495 IUnknown_AddRef((IUnknown
*)*ppv
);
499 static ULONG WINAPI
PngDecoder_AddRef(IWICBitmapDecoder
*iface
)
501 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
502 ULONG ref
= InterlockedIncrement(&This
->ref
);
504 TRACE("(%p) refcount=%u\n", iface
, ref
);
509 static ULONG WINAPI
PngDecoder_Release(IWICBitmapDecoder
*iface
)
511 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
512 ULONG ref
= InterlockedDecrement(&This
->ref
);
515 TRACE("(%p) refcount=%u\n", iface
, ref
);
520 IStream_Release(This
->stream
);
522 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
523 This
->lock
.DebugInfo
->Spare
[0] = 0;
524 DeleteCriticalSection(&This
->lock
);
525 HeapFree(GetProcessHeap(), 0, This
->image_bits
);
526 for (i
=0; i
<This
->metadata_count
; i
++)
528 if (This
->metadata_blocks
[i
].reader
)
529 IWICMetadataReader_Release(This
->metadata_blocks
[i
].reader
);
531 HeapFree(GetProcessHeap(), 0, This
->metadata_blocks
);
532 HeapFree(GetProcessHeap(), 0, This
);
538 static HRESULT WINAPI
PngDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
543 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
545 if (!stream
|| !capability
) return E_INVALIDARG
;
547 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
548 if (hr
!= S_OK
) return hr
;
550 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
551 WICBitmapDecoderCapabilityCanDecodeSomeImages
;
552 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
556 static void user_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
558 IStream
*stream
= ppng_get_io_ptr(png_ptr
);
562 hr
= IStream_Read(stream
, data
, length
, &bytesread
);
563 if (FAILED(hr
) || bytesread
!= length
)
565 ppng_error(png_ptr
, "failed reading data");
569 static HRESULT WINAPI
PngDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
570 WICDecodeOptions cacheOptions
)
572 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
575 png_bytep
*row_pointers
=NULL
;
578 int color_type
, bit_depth
;
581 png_uint_32 transparency
;
582 png_color_16p trans_values
;
586 ULARGE_INTEGER chunk_start
;
587 ULONG metadata_blocks_size
= 0;
589 TRACE("(%p,%p,%x)\n", iface
, pIStream
, cacheOptions
);
591 EnterCriticalSection(&This
->lock
);
593 /* initialize libpng */
594 This
->png_ptr
= ppng_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
601 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
604 ppng_destroy_read_struct(&This
->png_ptr
, NULL
, NULL
);
605 This
->png_ptr
= NULL
;
610 This
->end_info
= ppng_create_info_struct(This
->png_ptr
);
613 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, NULL
);
614 This
->png_ptr
= NULL
;
619 /* set up setjmp/longjmp error handling */
622 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
623 HeapFree(GetProcessHeap(), 0, row_pointers
);
624 This
->png_ptr
= NULL
;
628 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
629 ppng_set_crc_action(This
->png_ptr
, PNG_CRC_QUIET_USE
, PNG_CRC_QUIET_USE
);
631 /* seek to the start of the stream */
633 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
634 if (FAILED(hr
)) goto end
;
636 /* set up custom i/o handling */
637 ppng_set_read_fn(This
->png_ptr
, pIStream
, user_read_data
);
639 /* read the header */
640 ppng_read_info(This
->png_ptr
, This
->info_ptr
);
642 /* choose a pixel format */
643 color_type
= ppng_get_color_type(This
->png_ptr
, This
->info_ptr
);
644 bit_depth
= ppng_get_bit_depth(This
->png_ptr
, This
->info_ptr
);
646 /* check for color-keyed alpha */
647 transparency
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
649 if (transparency
&& (color_type
== PNG_COLOR_TYPE_RGB
||
650 (color_type
== PNG_COLOR_TYPE_GRAY
&& bit_depth
== 16)))
653 if (color_type
== PNG_COLOR_TYPE_GRAY
)
654 ppng_set_gray_to_rgb(This
->png_ptr
);
655 ppng_set_tRNS_to_alpha(This
->png_ptr
);
656 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
661 case PNG_COLOR_TYPE_GRAY_ALPHA
:
662 /* WIC does not support grayscale alpha formats so use RGBA */
663 ppng_set_gray_to_rgb(This
->png_ptr
);
665 case PNG_COLOR_TYPE_RGB_ALPHA
:
666 This
->bpp
= bit_depth
* 4;
670 ppng_set_bgr(This
->png_ptr
);
671 This
->format
= &GUID_WICPixelFormat32bppBGRA
;
673 case 16: This
->format
= &GUID_WICPixelFormat64bppRGBA
; break;
675 ERR("invalid RGBA bit depth: %i\n", bit_depth
);
680 case PNG_COLOR_TYPE_GRAY
:
681 This
->bpp
= bit_depth
;
686 case 1: This
->format
= &GUID_WICPixelFormatBlackWhite
; break;
687 case 2: This
->format
= &GUID_WICPixelFormat2bppGray
; break;
688 case 4: This
->format
= &GUID_WICPixelFormat4bppGray
; break;
689 case 8: This
->format
= &GUID_WICPixelFormat8bppGray
; break;
690 case 16: This
->format
= &GUID_WICPixelFormat16bppGray
; break;
692 ERR("invalid grayscale bit depth: %i\n", bit_depth
);
698 /* else fall through */
699 case PNG_COLOR_TYPE_PALETTE
:
700 This
->bpp
= bit_depth
;
703 case 1: This
->format
= &GUID_WICPixelFormat1bppIndexed
; break;
704 case 2: This
->format
= &GUID_WICPixelFormat2bppIndexed
; break;
705 case 4: This
->format
= &GUID_WICPixelFormat4bppIndexed
; break;
706 case 8: This
->format
= &GUID_WICPixelFormat8bppIndexed
; break;
708 ERR("invalid indexed color bit depth: %i\n", bit_depth
);
713 case PNG_COLOR_TYPE_RGB
:
714 This
->bpp
= bit_depth
* 3;
718 ppng_set_bgr(This
->png_ptr
);
719 This
->format
= &GUID_WICPixelFormat24bppBGR
;
721 case 16: This
->format
= &GUID_WICPixelFormat48bppRGB
; break;
723 ERR("invalid RGB color bit depth: %i\n", bit_depth
);
729 ERR("invalid color type %i\n", color_type
);
734 /* read the image data */
735 This
->width
= ppng_get_image_width(This
->png_ptr
, This
->info_ptr
);
736 This
->height
= ppng_get_image_height(This
->png_ptr
, This
->info_ptr
);
737 This
->stride
= (This
->width
* This
->bpp
+ 7) / 8;
738 image_size
= This
->stride
* This
->height
;
740 This
->image_bits
= HeapAlloc(GetProcessHeap(), 0, image_size
);
741 if (!This
->image_bits
)
747 row_pointers
= HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep
)*This
->height
);
754 for (i
=0; i
<This
->height
; i
++)
755 row_pointers
[i
] = This
->image_bits
+ i
* This
->stride
;
757 ppng_read_image(This
->png_ptr
, row_pointers
);
759 HeapFree(GetProcessHeap(), 0, row_pointers
);
762 ppng_read_end(This
->png_ptr
, This
->end_info
);
764 /* Find the metadata chunks in the file. */
769 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, &chunk_start
);
770 if (FAILED(hr
)) goto end
;
772 hr
= read_png_chunk(pIStream
, chunk_type
, NULL
, &chunk_size
);
773 if (FAILED(hr
)) goto end
;
775 if (chunk_type
[0] >= 'a' && chunk_type
[0] <= 'z' &&
776 memcmp(chunk_type
, "tRNS", 4) && memcmp(chunk_type
, "pHYs", 4))
778 /* This chunk is considered metadata. */
779 if (This
->metadata_count
== metadata_blocks_size
)
781 metadata_block_info
* new_metadata_blocks
;
782 ULONG new_metadata_blocks_size
;
784 new_metadata_blocks_size
= 4 + metadata_blocks_size
* 2;
785 new_metadata_blocks
= HeapAlloc(GetProcessHeap(), 0,
786 new_metadata_blocks_size
* sizeof(*new_metadata_blocks
));
788 if (!new_metadata_blocks
)
794 memcpy(new_metadata_blocks
, This
->metadata_blocks
,
795 This
->metadata_count
* sizeof(*new_metadata_blocks
));
797 HeapFree(GetProcessHeap(), 0, This
->metadata_blocks
);
798 This
->metadata_blocks
= new_metadata_blocks
;
799 metadata_blocks_size
= new_metadata_blocks_size
;
802 This
->metadata_blocks
[This
->metadata_count
].ofs
= chunk_start
;
803 This
->metadata_blocks
[This
->metadata_count
].len
.QuadPart
= chunk_size
+ 12;
804 This
->metadata_blocks
[This
->metadata_count
].reader
= NULL
;
805 This
->metadata_count
++;
808 seek
.QuadPart
= chunk_start
.QuadPart
+ chunk_size
+ 12; /* skip data and CRC */
809 } while (memcmp(chunk_type
, "IEND", 4));
811 This
->stream
= pIStream
;
812 IStream_AddRef(This
->stream
);
814 This
->initialized
= TRUE
;
817 LeaveCriticalSection(&This
->lock
);
822 static HRESULT WINAPI
PngDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
823 GUID
*pguidContainerFormat
)
825 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
829 static HRESULT WINAPI
PngDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
830 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
832 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
834 return get_decoder_info(&CLSID_WICPngDecoder
, ppIDecoderInfo
);
837 static HRESULT WINAPI
PngDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
838 IWICPalette
*palette
)
840 TRACE("(%p,%p)\n", iface
, palette
);
841 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
844 static HRESULT WINAPI
PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
845 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
847 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
851 static HRESULT WINAPI
PngDecoder_GetPreview(IWICBitmapDecoder
*iface
,
852 IWICBitmapSource
**ppIBitmapSource
)
854 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
856 if (!ppIBitmapSource
) return E_INVALIDARG
;
858 *ppIBitmapSource
= NULL
;
859 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
862 static HRESULT WINAPI
PngDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
863 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
865 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
866 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
869 static HRESULT WINAPI
PngDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
870 IWICBitmapSource
**ppIThumbnail
)
872 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
874 if (!ppIThumbnail
) return E_INVALIDARG
;
876 *ppIThumbnail
= NULL
;
877 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
880 static HRESULT WINAPI
PngDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
883 if (!pCount
) return E_INVALIDARG
;
889 static HRESULT WINAPI
PngDecoder_GetFrame(IWICBitmapDecoder
*iface
,
890 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
892 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
893 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
895 if (!This
->initialized
) return WINCODEC_ERR_FRAMEMISSING
;
897 if (index
!= 0) return E_INVALIDARG
;
899 IWICBitmapDecoder_AddRef(iface
);
901 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
906 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl
= {
907 PngDecoder_QueryInterface
,
910 PngDecoder_QueryCapability
,
911 PngDecoder_Initialize
,
912 PngDecoder_GetContainerFormat
,
913 PngDecoder_GetDecoderInfo
,
914 PngDecoder_CopyPalette
,
915 PngDecoder_GetMetadataQueryReader
,
916 PngDecoder_GetPreview
,
917 PngDecoder_GetColorContexts
,
918 PngDecoder_GetThumbnail
,
919 PngDecoder_GetFrameCount
,
923 static HRESULT WINAPI
PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
926 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
927 if (!ppv
) return E_INVALIDARG
;
929 if (IsEqualIID(&IID_IUnknown
, iid
) ||
930 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
931 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
933 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
935 else if (IsEqualIID(&IID_IWICMetadataBlockReader
, iid
))
937 *ppv
= &This
->IWICMetadataBlockReader_iface
;
942 return E_NOINTERFACE
;
945 IUnknown_AddRef((IUnknown
*)*ppv
);
949 static ULONG WINAPI
PngDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
951 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
952 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
955 static ULONG WINAPI
PngDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
957 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
958 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
961 static HRESULT WINAPI
PngDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
962 UINT
*puiWidth
, UINT
*puiHeight
)
964 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
965 *puiWidth
= This
->width
;
966 *puiHeight
= This
->height
;
967 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
971 static HRESULT WINAPI
PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
972 WICPixelFormatGUID
*pPixelFormat
)
974 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
975 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
977 memcpy(pPixelFormat
, This
->format
, sizeof(GUID
));
982 static HRESULT WINAPI
PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
983 double *pDpiX
, double *pDpiY
)
985 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
986 png_uint_32 ret
, xres
, yres
;
989 EnterCriticalSection(&This
->lock
);
991 ret
= ppng_get_pHYs(This
->png_ptr
, This
->info_ptr
, &xres
, &yres
, &unit_type
);
993 if (ret
&& unit_type
== PNG_RESOLUTION_METER
)
995 *pDpiX
= xres
* 0.0254;
996 *pDpiY
= yres
* 0.0254;
1000 WARN("no pHYs block present\n");
1001 *pDpiX
= *pDpiY
= 96.0;
1004 LeaveCriticalSection(&This
->lock
);
1006 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
1011 static HRESULT WINAPI
PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
1012 IWICPalette
*pIPalette
)
1014 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
1015 png_uint_32 ret
, color_type
, bit_depth
;
1016 png_colorp png_palette
;
1018 WICColor palette
[256];
1019 png_bytep trans_alpha
;
1021 png_color_16p trans_values
;
1025 TRACE("(%p,%p)\n", iface
, pIPalette
);
1027 EnterCriticalSection(&This
->lock
);
1029 color_type
= ppng_get_color_type(This
->png_ptr
, This
->info_ptr
);
1030 bit_depth
= ppng_get_bit_depth(This
->png_ptr
, This
->info_ptr
);
1032 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
1034 ret
= ppng_get_PLTE(This
->png_ptr
, This
->info_ptr
, &png_palette
, &num_palette
);
1037 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
1041 if (num_palette
> 256)
1043 ERR("palette has %i colors?!\n", num_palette
);
1048 ret
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans_alpha
, &num_trans
, &trans_values
);
1049 if (!ret
) num_trans
= 0;
1051 for (i
=0; i
<num_palette
; i
++)
1053 BYTE alpha
= (i
< num_trans
) ? trans_alpha
[i
] : 0xff;
1054 palette
[i
] = (alpha
<< 24 |
1055 png_palette
[i
].red
<< 16|
1056 png_palette
[i
].green
<< 8|
1057 png_palette
[i
].blue
);
1060 else if (color_type
== PNG_COLOR_TYPE_GRAY
&& bit_depth
<= 8) {
1061 ret
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans_alpha
, &num_trans
, &trans_values
);
1065 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
1069 num_palette
= 1 << bit_depth
;
1071 for (i
=0; i
<num_palette
; i
++)
1073 BYTE alpha
= (i
== trans_values
[0].gray
) ? 0 : 0xff;
1074 BYTE val
= i
* 255 / (num_palette
- 1);
1075 palette
[i
] = (alpha
<< 24 | val
<< 16 | val
<< 8 | val
);
1080 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
1085 LeaveCriticalSection(&This
->lock
);
1088 hr
= IWICPalette_InitializeCustom(pIPalette
, palette
, num_palette
);
1093 static HRESULT WINAPI
PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
1094 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
1096 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
1097 TRACE("(%p,%s,%u,%u,%p)\n", iface
, debug_wic_rect(prc
), cbStride
, cbBufferSize
, pbBuffer
);
1099 return copy_pixels(This
->bpp
, This
->image_bits
,
1100 This
->width
, This
->height
, This
->stride
,
1101 prc
, cbStride
, cbBufferSize
, pbBuffer
);
1104 static HRESULT WINAPI
PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
1105 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
1107 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
1109 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
1111 if (!ppIMetadataQueryReader
)
1112 return E_INVALIDARG
;
1114 return MetadataQueryReader_CreateInstance(&This
->IWICMetadataBlockReader_iface
, NULL
, ppIMetadataQueryReader
);
1117 static HRESULT WINAPI
PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
1118 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
1120 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
1124 int compression_type
;
1127 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
1129 if (!pcActualCount
) return E_INVALIDARG
;
1131 EnterCriticalSection(&This
->lock
);
1133 if (ppng_get_iCCP(This
->png_ptr
, This
->info_ptr
, &name
, &compression_type
, (void *)&profile
, &len
))
1135 if (cCount
&& ppIColorContexts
)
1137 hr
= IWICColorContext_InitializeFromMemory(*ppIColorContexts
, profile
, len
);
1140 LeaveCriticalSection(&This
->lock
);
1149 LeaveCriticalSection(&This
->lock
);
1154 static HRESULT WINAPI
PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
1155 IWICBitmapSource
**ppIThumbnail
)
1157 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
1159 if (!ppIThumbnail
) return E_INVALIDARG
;
1161 *ppIThumbnail
= NULL
;
1162 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
1165 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
= {
1166 PngDecoder_Frame_QueryInterface
,
1167 PngDecoder_Frame_AddRef
,
1168 PngDecoder_Frame_Release
,
1169 PngDecoder_Frame_GetSize
,
1170 PngDecoder_Frame_GetPixelFormat
,
1171 PngDecoder_Frame_GetResolution
,
1172 PngDecoder_Frame_CopyPalette
,
1173 PngDecoder_Frame_CopyPixels
,
1174 PngDecoder_Frame_GetMetadataQueryReader
,
1175 PngDecoder_Frame_GetColorContexts
,
1176 PngDecoder_Frame_GetThumbnail
1179 static HRESULT WINAPI
PngDecoder_Block_QueryInterface(IWICMetadataBlockReader
*iface
, REFIID iid
,
1182 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
1183 return IWICBitmapFrameDecode_QueryInterface(&This
->IWICBitmapFrameDecode_iface
, iid
, ppv
);
1186 static ULONG WINAPI
PngDecoder_Block_AddRef(IWICMetadataBlockReader
*iface
)
1188 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
1189 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
1192 static ULONG WINAPI
PngDecoder_Block_Release(IWICMetadataBlockReader
*iface
)
1194 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
1195 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1198 static HRESULT WINAPI
PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
1199 GUID
*pguidContainerFormat
)
1201 if (!pguidContainerFormat
) return E_INVALIDARG
;
1202 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
1206 static HRESULT WINAPI
PngDecoder_Block_GetCount(IWICMetadataBlockReader
*iface
,
1209 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
1211 TRACE("%p,%p\n", iface
, pcCount
);
1213 if (!pcCount
) return E_INVALIDARG
;
1215 *pcCount
= This
->metadata_count
;
1220 static HRESULT WINAPI
PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
1221 UINT nIndex
, IWICMetadataReader
**ppIMetadataReader
)
1223 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
1225 IWICComponentFactory
* factory
;
1228 TRACE("%p,%d,%p\n", iface
, nIndex
, ppIMetadataReader
);
1230 if (nIndex
>= This
->metadata_count
|| !ppIMetadataReader
)
1231 return E_INVALIDARG
;
1233 if (!This
->metadata_blocks
[nIndex
].reader
)
1235 hr
= StreamImpl_Create(&stream
);
1239 hr
= IWICStream_InitializeFromIStreamRegion(stream
, This
->stream
,
1240 This
->metadata_blocks
[nIndex
].ofs
, This
->metadata_blocks
[nIndex
].len
);
1243 hr
= ImagingFactory_CreateInstance(&IID_IWICComponentFactory
, (void**)&factory
);
1247 hr
= IWICComponentFactory_CreateMetadataReaderFromContainer(factory
,
1248 &GUID_ContainerFormatPng
, NULL
, WICMetadataCreationAllowUnknown
,
1249 (IStream
*)stream
, &This
->metadata_blocks
[nIndex
].reader
);
1251 IWICComponentFactory_Release(factory
);
1254 IWICStream_Release(stream
);
1259 *ppIMetadataReader
= NULL
;
1264 *ppIMetadataReader
= This
->metadata_blocks
[nIndex
].reader
;
1265 IWICMetadataReader_AddRef(*ppIMetadataReader
);
1270 static HRESULT WINAPI
PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
1271 IEnumUnknown
**ppIEnumMetadata
)
1273 FIXME("%p,%p\n", iface
, ppIEnumMetadata
);
1277 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl
= {
1278 PngDecoder_Block_QueryInterface
,
1279 PngDecoder_Block_AddRef
,
1280 PngDecoder_Block_Release
,
1281 PngDecoder_Block_GetContainerFormat
,
1282 PngDecoder_Block_GetCount
,
1283 PngDecoder_Block_GetReaderByIndex
,
1284 PngDecoder_Block_GetEnumerator
,
1287 HRESULT
PngDecoder_CreateInstance(REFIID iid
, void** ppv
)
1292 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1298 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG
);
1302 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder
));
1303 if (!This
) return E_OUTOFMEMORY
;
1305 This
->IWICBitmapDecoder_iface
.lpVtbl
= &PngDecoder_Vtbl
;
1306 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &PngDecoder_FrameVtbl
;
1307 This
->IWICMetadataBlockReader_iface
.lpVtbl
= &PngDecoder_BlockVtbl
;
1309 This
->png_ptr
= NULL
;
1310 This
->info_ptr
= NULL
;
1311 This
->end_info
= NULL
;
1312 This
->stream
= NULL
;
1313 This
->initialized
= FALSE
;
1314 This
->image_bits
= NULL
;
1315 InitializeCriticalSection(&This
->lock
);
1316 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngDecoder.lock");
1317 This
->metadata_count
= 0;
1318 This
->metadata_blocks
= NULL
;
1320 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1321 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1326 struct png_pixelformat
{
1327 const WICPixelFormatGUID
*guid
;
1335 static const struct png_pixelformat formats
[] = {
1336 {&GUID_WICPixelFormat32bppBGRA
, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 1},
1337 {&GUID_WICPixelFormat24bppBGR
, 24, 8, PNG_COLOR_TYPE_RGB
, 0, 1},
1338 {&GUID_WICPixelFormatBlackWhite
, 1, 1, PNG_COLOR_TYPE_GRAY
, 0, 0},
1339 {&GUID_WICPixelFormat2bppGray
, 2, 2, PNG_COLOR_TYPE_GRAY
, 0, 0},
1340 {&GUID_WICPixelFormat4bppGray
, 4, 4, PNG_COLOR_TYPE_GRAY
, 0, 0},
1341 {&GUID_WICPixelFormat8bppGray
, 8, 8, PNG_COLOR_TYPE_GRAY
, 0, 0},
1342 {&GUID_WICPixelFormat16bppGray
, 16, 16, PNG_COLOR_TYPE_GRAY
, 0, 0},
1343 {&GUID_WICPixelFormat32bppBGR
, 32, 8, PNG_COLOR_TYPE_RGB
, 1, 1},
1344 {&GUID_WICPixelFormat48bppRGB
, 48, 16, PNG_COLOR_TYPE_RGB
, 0, 0},
1345 {&GUID_WICPixelFormat64bppRGBA
, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 0},
1346 {&GUID_WICPixelFormat1bppIndexed
, 1, 1, PNG_COLOR_TYPE_PALETTE
, 0, 0},
1347 {&GUID_WICPixelFormat2bppIndexed
, 2, 2, PNG_COLOR_TYPE_PALETTE
, 0, 0},
1348 {&GUID_WICPixelFormat4bppIndexed
, 4, 4, PNG_COLOR_TYPE_PALETTE
, 0, 0},
1349 {&GUID_WICPixelFormat8bppIndexed
, 8, 8, PNG_COLOR_TYPE_PALETTE
, 0, 0},
1353 typedef struct PngEncoder
{
1354 IWICBitmapEncoder IWICBitmapEncoder_iface
;
1355 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
1358 png_structp png_ptr
;
1361 BOOL frame_initialized
;
1362 const struct png_pixelformat
*format
;
1367 BOOL frame_committed
;
1369 CRITICAL_SECTION lock
;
1371 WICPngFilterOption filter
;
1375 WICColor palette
[256];
1379 static inline PngEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
1381 return CONTAINING_RECORD(iface
, PngEncoder
, IWICBitmapEncoder_iface
);
1384 static inline PngEncoder
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
1386 return CONTAINING_RECORD(iface
, PngEncoder
, IWICBitmapFrameEncode_iface
);
1389 static HRESULT WINAPI
PngFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
1392 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1393 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1395 if (!ppv
) return E_INVALIDARG
;
1397 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1398 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
1400 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
1405 return E_NOINTERFACE
;
1408 IUnknown_AddRef((IUnknown
*)*ppv
);
1412 static ULONG WINAPI
PngFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
1414 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1415 return IWICBitmapEncoder_AddRef(&This
->IWICBitmapEncoder_iface
);
1418 static ULONG WINAPI
PngFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
1420 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1421 return IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1424 static HRESULT WINAPI
PngFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
1425 IPropertyBag2
*pIEncoderOptions
)
1427 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1428 WICPngFilterOption filter
;
1430 PROPBAG2 opts
[2]= {{0}};
1431 VARIANT opt_values
[2];
1432 HRESULT opt_hres
[2];
1435 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
1437 opts
[0].pstrName
= (LPOLESTR
)wszPngInterlaceOption
;
1438 opts
[0].vt
= VT_BOOL
;
1439 opts
[1].pstrName
= (LPOLESTR
)wszPngFilterOption
;
1440 opts
[1].vt
= VT_UI1
;
1442 if (pIEncoderOptions
)
1444 hr
= IPropertyBag2_Read(pIEncoderOptions
, ARRAY_SIZE(opts
), opts
, NULL
, opt_values
, opt_hres
);
1449 if (V_VT(&opt_values
[0]) == VT_EMPTY
)
1452 interlace
= (V_BOOL(&opt_values
[0]) != 0);
1454 filter
= V_UI1(&opt_values
[1]);
1455 if (filter
> WICPngFilterAdaptive
)
1457 WARN("Unrecognized filter option value %u.\n", filter
);
1458 filter
= WICPngFilterUnspecified
;
1464 filter
= WICPngFilterUnspecified
;
1467 EnterCriticalSection(&This
->lock
);
1469 if (This
->frame_initialized
)
1471 LeaveCriticalSection(&This
->lock
);
1472 return WINCODEC_ERR_WRONGSTATE
;
1475 This
->interlace
= interlace
;
1476 This
->filter
= filter
;
1478 This
->frame_initialized
= TRUE
;
1480 LeaveCriticalSection(&This
->lock
);
1485 static HRESULT WINAPI
PngFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
1486 UINT uiWidth
, UINT uiHeight
)
1488 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1489 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
1491 EnterCriticalSection(&This
->lock
);
1493 if (!This
->frame_initialized
|| This
->info_written
)
1495 LeaveCriticalSection(&This
->lock
);
1496 return WINCODEC_ERR_WRONGSTATE
;
1499 This
->width
= uiWidth
;
1500 This
->height
= uiHeight
;
1502 LeaveCriticalSection(&This
->lock
);
1507 static HRESULT WINAPI
PngFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
1508 double dpiX
, double dpiY
)
1510 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1511 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
1513 EnterCriticalSection(&This
->lock
);
1515 if (!This
->frame_initialized
|| This
->info_written
)
1517 LeaveCriticalSection(&This
->lock
);
1518 return WINCODEC_ERR_WRONGSTATE
;
1524 LeaveCriticalSection(&This
->lock
);
1529 static HRESULT WINAPI
PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
1530 WICPixelFormatGUID
*pPixelFormat
)
1532 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1534 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
1536 EnterCriticalSection(&This
->lock
);
1538 if (!This
->frame_initialized
|| This
->info_written
)
1540 LeaveCriticalSection(&This
->lock
);
1541 return WINCODEC_ERR_WRONGSTATE
;
1544 for (i
=0; formats
[i
].guid
; i
++)
1546 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
1550 if (!formats
[i
].guid
) i
= 0;
1552 This
->format
= &formats
[i
];
1553 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
1555 LeaveCriticalSection(&This
->lock
);
1560 static HRESULT WINAPI
PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
1561 UINT cCount
, IWICColorContext
**ppIColorContext
)
1563 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1567 static HRESULT WINAPI
PngFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
1568 IWICPalette
*palette
)
1570 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1573 TRACE("(%p,%p)\n", iface
, palette
);
1575 if (!palette
) return E_INVALIDARG
;
1577 EnterCriticalSection(&This
->lock
);
1579 if (This
->frame_initialized
)
1580 hr
= IWICPalette_GetColors(palette
, 256, This
->palette
, &This
->colors
);
1582 hr
= WINCODEC_ERR_NOTINITIALIZED
;
1584 LeaveCriticalSection(&This
->lock
);
1588 static HRESULT WINAPI
PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
1589 IWICBitmapSource
*pIThumbnail
)
1591 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
1592 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1595 static HRESULT WINAPI
PngFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
1596 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
1598 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1599 png_byte
**row_pointers
=NULL
;
1602 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
1604 EnterCriticalSection(&This
->lock
);
1606 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
1608 LeaveCriticalSection(&This
->lock
);
1609 return WINCODEC_ERR_WRONGSTATE
;
1612 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
1614 LeaveCriticalSection(&This
->lock
);
1615 return E_INVALIDARG
;
1618 /* set up setjmp/longjmp error handling */
1621 LeaveCriticalSection(&This
->lock
);
1622 HeapFree(GetProcessHeap(), 0, row_pointers
);
1625 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1627 if (!This
->info_written
)
1629 if (This
->interlace
)
1631 /* libpng requires us to write all data multiple times in this case. */
1632 This
->stride
= (This
->format
->bpp
* This
->width
+ 7)/8;
1633 This
->data
= HeapAlloc(GetProcessHeap(), 0, This
->height
* This
->stride
);
1636 LeaveCriticalSection(&This
->lock
);
1637 return E_OUTOFMEMORY
;
1641 ppng_set_IHDR(This
->png_ptr
, This
->info_ptr
, This
->width
, This
->height
,
1642 This
->format
->bit_depth
, This
->format
->color_type
,
1643 This
->interlace
? PNG_INTERLACE_ADAM7
: PNG_INTERLACE_NONE
,
1644 PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
1646 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
1648 ppng_set_pHYs(This
->png_ptr
, This
->info_ptr
, (This
->xres
+0.0127) / 0.0254,
1649 (This
->yres
+0.0127) / 0.0254, PNG_RESOLUTION_METER
);
1652 if (This
->format
->color_type
== PNG_COLOR_TYPE_PALETTE
&& This
->colors
)
1654 png_color png_palette
[256];
1655 png_byte trans
[256];
1656 UINT i
, num_trans
= 0, colors
;
1658 /* Newer libpng versions don't accept larger palettes than the declared
1659 * bit depth, so we need to generate the palette of the correct length.
1661 colors
= min(This
->colors
, 1 << This
->format
->bit_depth
);
1663 for (i
= 0; i
< colors
; i
++)
1665 png_palette
[i
].red
= (This
->palette
[i
] >> 16) & 0xff;
1666 png_palette
[i
].green
= (This
->palette
[i
] >> 8) & 0xff;
1667 png_palette
[i
].blue
= This
->palette
[i
] & 0xff;
1668 trans
[i
] = (This
->palette
[i
] >> 24) & 0xff;
1669 if (trans
[i
] != 0xff)
1673 ppng_set_PLTE(This
->png_ptr
, This
->info_ptr
, png_palette
, colors
);
1676 ppng_set_tRNS(This
->png_ptr
, This
->info_ptr
, trans
, num_trans
, NULL
);
1679 ppng_write_info(This
->png_ptr
, This
->info_ptr
);
1681 if (This
->format
->remove_filler
)
1682 ppng_set_filler(This
->png_ptr
, 0, PNG_FILLER_AFTER
);
1684 if (This
->format
->swap_rgb
)
1685 ppng_set_bgr(This
->png_ptr
);
1687 if (This
->interlace
)
1688 This
->passes
= ppng_set_interlace_handling(This
->png_ptr
);
1690 if (This
->filter
!= WICPngFilterUnspecified
)
1692 static const int png_filter_map
[] =
1694 /* WICPngFilterUnspecified */ PNG_NO_FILTERS
,
1695 /* WICPngFilterNone */ PNG_FILTER_NONE
,
1696 /* WICPngFilterSub */ PNG_FILTER_SUB
,
1697 /* WICPngFilterUp */ PNG_FILTER_UP
,
1698 /* WICPngFilterAverage */ PNG_FILTER_AVG
,
1699 /* WICPngFilterPaeth */ PNG_FILTER_PAETH
,
1700 /* WICPngFilterAdaptive */ PNG_ALL_FILTERS
,
1703 ppng_set_filter(This
->png_ptr
, 0, png_filter_map
[This
->filter
]);
1706 This
->info_written
= TRUE
;
1709 if (This
->interlace
)
1711 /* Just store the data so we can write it in multiple passes in Commit. */
1712 for (i
=0; i
<lineCount
; i
++)
1713 memcpy(This
->data
+ This
->stride
* (This
->lines_written
+ i
),
1714 pbPixels
+ cbStride
* i
,
1717 This
->lines_written
+= lineCount
;
1719 LeaveCriticalSection(&This
->lock
);
1723 row_pointers
= HeapAlloc(GetProcessHeap(), 0, lineCount
* sizeof(png_byte
*));
1726 LeaveCriticalSection(&This
->lock
);
1727 return E_OUTOFMEMORY
;
1730 for (i
=0; i
<lineCount
; i
++)
1731 row_pointers
[i
] = pbPixels
+ cbStride
* i
;
1733 ppng_write_rows(This
->png_ptr
, row_pointers
, lineCount
);
1734 This
->lines_written
+= lineCount
;
1736 LeaveCriticalSection(&This
->lock
);
1738 HeapFree(GetProcessHeap(), 0, row_pointers
);
1743 static HRESULT WINAPI
PngFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
1744 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
1746 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1748 TRACE("(%p,%p,%s)\n", iface
, pIBitmapSource
, debug_wic_rect(prc
));
1750 if (!This
->frame_initialized
)
1751 return WINCODEC_ERR_WRONGSTATE
;
1753 hr
= configure_write_source(iface
, pIBitmapSource
, prc
,
1754 This
->format
? This
->format
->guid
: NULL
, This
->width
, This
->height
,
1755 This
->xres
, This
->yres
);
1759 hr
= write_source(iface
, pIBitmapSource
, prc
,
1760 This
->format
->guid
, This
->format
->bpp
, This
->width
, This
->height
);
1766 static HRESULT WINAPI
PngFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
1768 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1769 png_byte
**row_pointers
=NULL
;
1771 TRACE("(%p)\n", iface
);
1773 EnterCriticalSection(&This
->lock
);
1775 if (!This
->info_written
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
1777 LeaveCriticalSection(&This
->lock
);
1778 return WINCODEC_ERR_WRONGSTATE
;
1781 /* set up setjmp/longjmp error handling */
1784 LeaveCriticalSection(&This
->lock
);
1785 HeapFree(GetProcessHeap(), 0, row_pointers
);
1788 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1790 if (This
->interlace
)
1794 row_pointers
= HeapAlloc(GetProcessHeap(), 0, This
->height
* sizeof(png_byte
*));
1797 LeaveCriticalSection(&This
->lock
);
1798 return E_OUTOFMEMORY
;
1801 for (i
=0; i
<This
->height
; i
++)
1802 row_pointers
[i
] = This
->data
+ This
->stride
* i
;
1804 for (i
=0; i
<This
->passes
; i
++)
1805 ppng_write_rows(This
->png_ptr
, row_pointers
, This
->height
);
1808 ppng_write_end(This
->png_ptr
, This
->info_ptr
);
1810 This
->frame_committed
= TRUE
;
1812 HeapFree(GetProcessHeap(), 0, row_pointers
);
1814 LeaveCriticalSection(&This
->lock
);
1819 static HRESULT WINAPI
PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1820 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1822 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1826 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl
= {
1827 PngFrameEncode_QueryInterface
,
1828 PngFrameEncode_AddRef
,
1829 PngFrameEncode_Release
,
1830 PngFrameEncode_Initialize
,
1831 PngFrameEncode_SetSize
,
1832 PngFrameEncode_SetResolution
,
1833 PngFrameEncode_SetPixelFormat
,
1834 PngFrameEncode_SetColorContexts
,
1835 PngFrameEncode_SetPalette
,
1836 PngFrameEncode_SetThumbnail
,
1837 PngFrameEncode_WritePixels
,
1838 PngFrameEncode_WriteSource
,
1839 PngFrameEncode_Commit
,
1840 PngFrameEncode_GetMetadataQueryWriter
1843 static HRESULT WINAPI
PngEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1846 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1847 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1849 if (!ppv
) return E_INVALIDARG
;
1851 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1852 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1854 *ppv
= &This
->IWICBitmapEncoder_iface
;
1859 return E_NOINTERFACE
;
1862 IUnknown_AddRef((IUnknown
*)*ppv
);
1866 static ULONG WINAPI
PngEncoder_AddRef(IWICBitmapEncoder
*iface
)
1868 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1869 ULONG ref
= InterlockedIncrement(&This
->ref
);
1871 TRACE("(%p) refcount=%u\n", iface
, ref
);
1876 static ULONG WINAPI
PngEncoder_Release(IWICBitmapEncoder
*iface
)
1878 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1879 ULONG ref
= InterlockedDecrement(&This
->ref
);
1881 TRACE("(%p) refcount=%u\n", iface
, ref
);
1885 This
->lock
.DebugInfo
->Spare
[0] = 0;
1886 DeleteCriticalSection(&This
->lock
);
1888 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1890 IStream_Release(This
->stream
);
1891 HeapFree(GetProcessHeap(), 0, This
->data
);
1892 HeapFree(GetProcessHeap(), 0, This
);
1898 static void user_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
1900 PngEncoder
*This
= ppng_get_io_ptr(png_ptr
);
1904 hr
= IStream_Write(This
->stream
, data
, length
, &byteswritten
);
1905 if (FAILED(hr
) || byteswritten
!= length
)
1907 ppng_error(png_ptr
, "failed writing data");
1911 static void user_flush(png_structp png_ptr
)
1915 static HRESULT WINAPI
PngEncoder_Initialize(IWICBitmapEncoder
*iface
,
1916 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1918 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1921 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1923 EnterCriticalSection(&This
->lock
);
1927 LeaveCriticalSection(&This
->lock
);
1928 return WINCODEC_ERR_WRONGSTATE
;
1931 /* initialize libpng */
1932 This
->png_ptr
= ppng_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
1935 LeaveCriticalSection(&This
->lock
);
1939 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
1940 if (!This
->info_ptr
)
1942 ppng_destroy_write_struct(&This
->png_ptr
, NULL
);
1943 This
->png_ptr
= NULL
;
1944 LeaveCriticalSection(&This
->lock
);
1948 IStream_AddRef(pIStream
);
1949 This
->stream
= pIStream
;
1951 /* set up setjmp/longjmp error handling */
1954 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1955 This
->png_ptr
= NULL
;
1956 IStream_Release(This
->stream
);
1957 This
->stream
= NULL
;
1958 LeaveCriticalSection(&This
->lock
);
1961 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1963 /* set up custom i/o handling */
1964 ppng_set_write_fn(This
->png_ptr
, This
, user_write_data
, user_flush
);
1966 LeaveCriticalSection(&This
->lock
);
1971 static HRESULT WINAPI
PngEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1972 GUID
*pguidContainerFormat
)
1974 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1978 static HRESULT WINAPI
PngEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1979 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1981 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1985 static HRESULT WINAPI
PngEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1986 UINT cCount
, IWICColorContext
**ppIColorContext
)
1988 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1992 static HRESULT WINAPI
PngEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*palette
)
1994 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1997 TRACE("(%p,%p)\n", iface
, palette
);
1999 EnterCriticalSection(&This
->lock
);
2001 hr
= This
->stream
? WINCODEC_ERR_UNSUPPORTEDOPERATION
: WINCODEC_ERR_NOTINITIALIZED
;
2003 LeaveCriticalSection(&This
->lock
);
2008 static HRESULT WINAPI
PngEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
2010 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
2011 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
2014 static HRESULT WINAPI
PngEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
2016 TRACE("(%p,%p)\n", iface
, pIPreview
);
2017 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
2020 static HRESULT WINAPI
PngEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
2021 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
2023 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
2025 static const PROPBAG2 opts
[2] =
2027 { PROPBAG2_TYPE_DATA
, VT_BOOL
, 0, 0, (LPOLESTR
)wszPngInterlaceOption
},
2028 { PROPBAG2_TYPE_DATA
, VT_UI1
, 0, 0, (LPOLESTR
)wszPngFilterOption
},
2031 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
2033 EnterCriticalSection(&This
->lock
);
2035 if (This
->frame_count
!= 0)
2037 LeaveCriticalSection(&This
->lock
);
2038 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
2043 LeaveCriticalSection(&This
->lock
);
2044 return WINCODEC_ERR_NOTINITIALIZED
;
2047 if (ppIEncoderOptions
)
2049 hr
= CreatePropertyBag2(opts
, ARRAY_SIZE(opts
), ppIEncoderOptions
);
2052 LeaveCriticalSection(&This
->lock
);
2057 This
->frame_count
= 1;
2059 LeaveCriticalSection(&This
->lock
);
2061 IWICBitmapEncoder_AddRef(iface
);
2062 *ppIFrameEncode
= &This
->IWICBitmapFrameEncode_iface
;
2067 static HRESULT WINAPI
PngEncoder_Commit(IWICBitmapEncoder
*iface
)
2069 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
2070 TRACE("(%p)\n", iface
);
2072 EnterCriticalSection(&This
->lock
);
2074 if (!This
->frame_committed
|| This
->committed
)
2076 LeaveCriticalSection(&This
->lock
);
2077 return WINCODEC_ERR_WRONGSTATE
;
2080 This
->committed
= TRUE
;
2082 LeaveCriticalSection(&This
->lock
);
2087 static HRESULT WINAPI
PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
2088 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
2090 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
2094 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl
= {
2095 PngEncoder_QueryInterface
,
2098 PngEncoder_Initialize
,
2099 PngEncoder_GetContainerFormat
,
2100 PngEncoder_GetEncoderInfo
,
2101 PngEncoder_SetColorContexts
,
2102 PngEncoder_SetPalette
,
2103 PngEncoder_SetThumbnail
,
2104 PngEncoder_SetPreview
,
2105 PngEncoder_CreateNewFrame
,
2107 PngEncoder_GetMetadataQueryWriter
2110 HRESULT
PngEncoder_CreateInstance(REFIID iid
, void** ppv
)
2115 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
2121 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG
);
2125 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder
));
2126 if (!This
) return E_OUTOFMEMORY
;
2128 This
->IWICBitmapEncoder_iface
.lpVtbl
= &PngEncoder_Vtbl
;
2129 This
->IWICBitmapFrameEncode_iface
.lpVtbl
= &PngEncoder_FrameVtbl
;
2131 This
->png_ptr
= NULL
;
2132 This
->info_ptr
= NULL
;
2133 This
->stream
= NULL
;
2134 This
->frame_count
= 0;
2135 This
->frame_initialized
= FALSE
;
2136 This
->format
= NULL
;
2137 This
->info_written
= FALSE
;
2142 This
->lines_written
= 0;
2143 This
->frame_committed
= FALSE
;
2144 This
->committed
= FALSE
;
2147 InitializeCriticalSection(&This
->lock
);
2148 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngEncoder.lock");
2150 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
2151 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
2156 #else /* !HAVE_PNG_H */
2158 HRESULT
PngDecoder_CreateInstance(REFIID iid
, void** ppv
)
2160 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
2164 HRESULT
PngEncoder_CreateInstance(REFIID iid
, void** ppv
)
2166 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");