2 * Copyright 2009 Vincent Povirk for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
28 #define NONAMELESSUNION
35 #include "wincodecsdk.h"
37 #include "wincodecs_private.h"
39 #include "wine/debug.h"
40 #include "wine/library.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
44 static const WCHAR wszPngInterlaceOption
[] = {'I','n','t','e','r','l','a','c','e','O','p','t','i','o','n',0};
46 static HRESULT
read_png_chunk(IStream
*stream
, BYTE
*type
, BYTE
**data
, ULONG
*data_size
)
52 hr
= IStream_Read(stream
, header
, 8, &bytesread
);
53 if (FAILED(hr
) || bytesread
< 8)
60 *data_size
= header
[0] << 24 | header
[1] << 16 | header
[2] << 8 | header
[3];
62 memcpy(type
, &header
[4], 4);
66 *data
= HeapAlloc(GetProcessHeap(), 0, *data_size
);
70 hr
= IStream_Read(stream
, *data
, *data_size
, &bytesread
);
72 if (FAILED(hr
) || bytesread
< *data_size
)
76 HeapFree(GetProcessHeap(), 0, *data
);
81 /* Windows ignores CRC of the chunk */
87 static HRESULT
LoadTextMetadata(IStream
*stream
, const GUID
*preferred_vendor
,
88 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
94 ULONG name_len
, value_len
;
99 hr
= read_png_chunk(stream
, type
, &data
, &data_size
);
100 if (FAILED(hr
)) return hr
;
102 name_end_ptr
= memchr(data
, 0, data_size
);
104 name_len
= name_end_ptr
- data
;
106 if (!name_end_ptr
|| name_len
> 79)
108 HeapFree(GetProcessHeap(), 0, data
);
112 value_len
= data_size
- name_len
- 1;
114 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem
));
115 name
= HeapAlloc(GetProcessHeap(), 0, name_len
+ 1);
116 value
= HeapAlloc(GetProcessHeap(), 0, value_len
+ 1);
117 if (!result
|| !name
|| !value
)
119 HeapFree(GetProcessHeap(), 0, data
);
120 HeapFree(GetProcessHeap(), 0, result
);
121 HeapFree(GetProcessHeap(), 0, name
);
122 HeapFree(GetProcessHeap(), 0, value
);
123 return E_OUTOFMEMORY
;
126 PropVariantInit(&result
[0].schema
);
127 PropVariantInit(&result
[0].id
);
128 PropVariantInit(&result
[0].value
);
130 memcpy(name
, data
, name_len
+ 1);
131 memcpy(value
, name_end_ptr
+ 1, value_len
);
132 value
[value_len
] = 0;
134 result
[0].id
.vt
= VT_LPSTR
;
135 result
[0].id
.u
.pszVal
= name
;
136 result
[0].value
.vt
= VT_LPSTR
;
137 result
[0].value
.u
.pszVal
= value
;
142 HeapFree(GetProcessHeap(), 0, data
);
147 static const MetadataHandlerVtbl TextReader_Vtbl
= {
149 &CLSID_WICPngTextMetadataReader
,
153 HRESULT
PngTextReader_CreateInstance(REFIID iid
, void** ppv
)
155 return MetadataReader_Create(&TextReader_Vtbl
, iid
, ppv
);
160 static void *libpng_handle
;
161 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
162 MAKE_FUNCPTR(png_create_read_struct
);
163 MAKE_FUNCPTR(png_create_info_struct
);
164 MAKE_FUNCPTR(png_create_write_struct
);
165 MAKE_FUNCPTR(png_destroy_read_struct
);
166 MAKE_FUNCPTR(png_destroy_write_struct
);
167 MAKE_FUNCPTR(png_error
);
168 MAKE_FUNCPTR(png_get_bit_depth
);
169 MAKE_FUNCPTR(png_get_color_type
);
170 MAKE_FUNCPTR(png_get_error_ptr
);
171 MAKE_FUNCPTR(png_get_iCCP
);
172 MAKE_FUNCPTR(png_get_image_height
);
173 MAKE_FUNCPTR(png_get_image_width
);
174 MAKE_FUNCPTR(png_get_io_ptr
);
175 MAKE_FUNCPTR(png_get_pHYs
);
176 MAKE_FUNCPTR(png_get_PLTE
);
177 MAKE_FUNCPTR(png_get_tRNS
);
178 MAKE_FUNCPTR(png_set_bgr
);
179 MAKE_FUNCPTR(png_set_crc_action
);
180 MAKE_FUNCPTR(png_set_error_fn
);
181 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
182 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8
);
184 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8
);
186 MAKE_FUNCPTR(png_set_filler
);
187 MAKE_FUNCPTR(png_set_gray_to_rgb
);
188 MAKE_FUNCPTR(png_set_IHDR
);
189 MAKE_FUNCPTR(png_set_pHYs
);
190 MAKE_FUNCPTR(png_set_read_fn
);
191 MAKE_FUNCPTR(png_set_strip_16
);
192 MAKE_FUNCPTR(png_set_tRNS_to_alpha
);
193 MAKE_FUNCPTR(png_set_write_fn
);
194 MAKE_FUNCPTR(png_read_end
);
195 MAKE_FUNCPTR(png_read_image
);
196 MAKE_FUNCPTR(png_read_info
);
197 MAKE_FUNCPTR(png_write_end
);
198 MAKE_FUNCPTR(png_write_info
);
199 MAKE_FUNCPTR(png_write_rows
);
202 static void *load_libpng(void)
204 if((libpng_handle
= wine_dlopen(SONAME_LIBPNG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
206 #define LOAD_FUNCPTR(f) \
207 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
208 libpng_handle = NULL; \
211 LOAD_FUNCPTR(png_create_read_struct
);
212 LOAD_FUNCPTR(png_create_info_struct
);
213 LOAD_FUNCPTR(png_create_write_struct
);
214 LOAD_FUNCPTR(png_destroy_read_struct
);
215 LOAD_FUNCPTR(png_destroy_write_struct
);
216 LOAD_FUNCPTR(png_error
);
217 LOAD_FUNCPTR(png_get_bit_depth
);
218 LOAD_FUNCPTR(png_get_color_type
);
219 LOAD_FUNCPTR(png_get_error_ptr
);
220 LOAD_FUNCPTR(png_get_iCCP
);
221 LOAD_FUNCPTR(png_get_image_height
);
222 LOAD_FUNCPTR(png_get_image_width
);
223 LOAD_FUNCPTR(png_get_io_ptr
);
224 LOAD_FUNCPTR(png_get_pHYs
);
225 LOAD_FUNCPTR(png_get_PLTE
);
226 LOAD_FUNCPTR(png_get_tRNS
);
227 LOAD_FUNCPTR(png_set_bgr
);
228 LOAD_FUNCPTR(png_set_crc_action
);
229 LOAD_FUNCPTR(png_set_error_fn
);
230 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
231 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8
);
233 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8
);
235 LOAD_FUNCPTR(png_set_filler
);
236 LOAD_FUNCPTR(png_set_gray_to_rgb
);
237 LOAD_FUNCPTR(png_set_IHDR
);
238 LOAD_FUNCPTR(png_set_pHYs
);
239 LOAD_FUNCPTR(png_set_read_fn
);
240 LOAD_FUNCPTR(png_set_strip_16
);
241 LOAD_FUNCPTR(png_set_tRNS_to_alpha
);
242 LOAD_FUNCPTR(png_set_write_fn
);
243 LOAD_FUNCPTR(png_read_end
);
244 LOAD_FUNCPTR(png_read_image
);
245 LOAD_FUNCPTR(png_read_info
);
246 LOAD_FUNCPTR(png_write_end
);
247 LOAD_FUNCPTR(png_write_info
);
248 LOAD_FUNCPTR(png_write_rows
);
252 return libpng_handle
;
255 static void user_error_fn(png_structp png_ptr
, png_const_charp error_message
)
259 /* This uses setjmp/longjmp just like the default. We can't use the
260 * default because there's no way to access the jmp buffer in the png_struct
261 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
262 WARN("PNG error: %s\n", debugstr_a(error_message
));
263 pjmpbuf
= ppng_get_error_ptr(png_ptr
);
264 longjmp(*pjmpbuf
, 1);
267 static void user_warning_fn(png_structp png_ptr
, png_const_charp warning_message
)
269 WARN("PNG warning: %s\n", debugstr_a(warning_message
));
273 IWICBitmapDecoder IWICBitmapDecoder_iface
;
274 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
275 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
284 const WICPixelFormatGUID
*format
;
286 CRITICAL_SECTION lock
; /* must be held when png structures are accessed or initialized is set */
289 static inline PngDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
291 return CONTAINING_RECORD(iface
, PngDecoder
, IWICBitmapDecoder_iface
);
294 static inline PngDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
296 return CONTAINING_RECORD(iface
, PngDecoder
, IWICBitmapFrameDecode_iface
);
299 static inline PngDecoder
*impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
301 return CONTAINING_RECORD(iface
, PngDecoder
, IWICMetadataBlockReader_iface
);
304 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
;
306 static HRESULT WINAPI
PngDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
309 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
310 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
312 if (!ppv
) return E_INVALIDARG
;
314 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
316 *ppv
= &This
->IWICBitmapDecoder_iface
;
321 return E_NOINTERFACE
;
324 IUnknown_AddRef((IUnknown
*)*ppv
);
328 static ULONG WINAPI
PngDecoder_AddRef(IWICBitmapDecoder
*iface
)
330 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
331 ULONG ref
= InterlockedIncrement(&This
->ref
);
333 TRACE("(%p) refcount=%u\n", iface
, ref
);
338 static ULONG WINAPI
PngDecoder_Release(IWICBitmapDecoder
*iface
)
340 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
341 ULONG ref
= InterlockedDecrement(&This
->ref
);
343 TRACE("(%p) refcount=%u\n", iface
, ref
);
348 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
349 This
->lock
.DebugInfo
->Spare
[0] = 0;
350 DeleteCriticalSection(&This
->lock
);
351 HeapFree(GetProcessHeap(), 0, This
->image_bits
);
352 HeapFree(GetProcessHeap(), 0, This
);
358 static HRESULT WINAPI
PngDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
363 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
365 if (!stream
|| !capability
) return E_INVALIDARG
;
367 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
368 if (hr
!= S_OK
) return hr
;
370 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
371 WICBitmapDecoderCapabilityCanDecodeSomeImages
;
372 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
376 static void user_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
378 IStream
*stream
= ppng_get_io_ptr(png_ptr
);
382 hr
= IStream_Read(stream
, data
, length
, &bytesread
);
383 if (FAILED(hr
) || bytesread
!= length
)
385 ppng_error(png_ptr
, "failed reading data");
389 static HRESULT WINAPI
PngDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
390 WICDecodeOptions cacheOptions
)
392 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
395 png_bytep
*row_pointers
=NULL
;
398 int color_type
, bit_depth
;
401 png_uint_32 transparency
;
402 png_color_16p trans_values
;
405 TRACE("(%p,%p,%x)\n", iface
, pIStream
, cacheOptions
);
407 EnterCriticalSection(&This
->lock
);
409 /* initialize libpng */
410 This
->png_ptr
= ppng_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
417 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
420 ppng_destroy_read_struct(&This
->png_ptr
, NULL
, NULL
);
421 This
->png_ptr
= NULL
;
426 This
->end_info
= ppng_create_info_struct(This
->png_ptr
);
429 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, NULL
);
430 This
->png_ptr
= NULL
;
435 /* set up setjmp/longjmp error handling */
438 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
439 HeapFree(GetProcessHeap(), 0, row_pointers
);
440 This
->png_ptr
= NULL
;
444 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
445 ppng_set_crc_action(This
->png_ptr
, PNG_CRC_QUIET_USE
, PNG_CRC_QUIET_USE
);
447 /* seek to the start of the stream */
449 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
450 if (FAILED(hr
)) goto end
;
452 /* set up custom i/o handling */
453 ppng_set_read_fn(This
->png_ptr
, pIStream
, user_read_data
);
455 /* read the header */
456 ppng_read_info(This
->png_ptr
, This
->info_ptr
);
458 /* choose a pixel format */
459 color_type
= ppng_get_color_type(This
->png_ptr
, This
->info_ptr
);
460 bit_depth
= ppng_get_bit_depth(This
->png_ptr
, This
->info_ptr
);
462 /* check for color-keyed alpha */
463 transparency
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
465 if (transparency
&& color_type
!= PNG_COLOR_TYPE_PALETTE
)
468 if (color_type
== PNG_COLOR_TYPE_GRAY
)
472 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
473 ppng_set_expand_gray_1_2_4_to_8(This
->png_ptr
);
475 ppng_set_gray_1_2_4_to_8(This
->png_ptr
);
479 ppng_set_gray_to_rgb(This
->png_ptr
);
481 ppng_set_tRNS_to_alpha(This
->png_ptr
);
482 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
487 case PNG_COLOR_TYPE_GRAY
:
488 This
->bpp
= bit_depth
;
491 case 1: This
->format
= &GUID_WICPixelFormatBlackWhite
; break;
492 case 2: This
->format
= &GUID_WICPixelFormat2bppGray
; break;
493 case 4: This
->format
= &GUID_WICPixelFormat4bppGray
; break;
494 case 8: This
->format
= &GUID_WICPixelFormat8bppGray
; break;
495 case 16: This
->format
= &GUID_WICPixelFormat16bppGray
; break;
497 ERR("invalid grayscale bit depth: %i\n", bit_depth
);
502 case PNG_COLOR_TYPE_GRAY_ALPHA
:
503 /* WIC does not support grayscale alpha formats so use RGBA */
504 ppng_set_gray_to_rgb(This
->png_ptr
);
506 case PNG_COLOR_TYPE_RGB_ALPHA
:
507 This
->bpp
= bit_depth
* 4;
511 ppng_set_bgr(This
->png_ptr
);
512 This
->format
= &GUID_WICPixelFormat32bppBGRA
;
514 case 16: This
->format
= &GUID_WICPixelFormat64bppRGBA
; break;
516 ERR("invalid RGBA bit depth: %i\n", bit_depth
);
521 case PNG_COLOR_TYPE_PALETTE
:
522 This
->bpp
= bit_depth
;
525 case 1: This
->format
= &GUID_WICPixelFormat1bppIndexed
; break;
526 case 2: This
->format
= &GUID_WICPixelFormat2bppIndexed
; break;
527 case 4: This
->format
= &GUID_WICPixelFormat4bppIndexed
; break;
528 case 8: This
->format
= &GUID_WICPixelFormat8bppIndexed
; break;
530 ERR("invalid indexed color bit depth: %i\n", bit_depth
);
535 case PNG_COLOR_TYPE_RGB
:
536 This
->bpp
= bit_depth
* 3;
540 ppng_set_bgr(This
->png_ptr
);
541 This
->format
= &GUID_WICPixelFormat24bppBGR
;
543 case 16: This
->format
= &GUID_WICPixelFormat48bppRGB
; break;
545 ERR("invalid RGB color bit depth: %i\n", bit_depth
);
551 ERR("invalid color type %i\n", color_type
);
556 /* read the image data */
557 This
->width
= ppng_get_image_width(This
->png_ptr
, This
->info_ptr
);
558 This
->height
= ppng_get_image_height(This
->png_ptr
, This
->info_ptr
);
559 This
->stride
= This
->width
* This
->bpp
;
560 image_size
= This
->stride
* This
->height
;
562 This
->image_bits
= HeapAlloc(GetProcessHeap(), 0, image_size
);
563 if (!This
->image_bits
)
569 row_pointers
= HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep
)*This
->height
);
576 for (i
=0; i
<This
->height
; i
++)
577 row_pointers
[i
] = This
->image_bits
+ i
* This
->stride
;
579 ppng_read_image(This
->png_ptr
, row_pointers
);
581 HeapFree(GetProcessHeap(), 0, row_pointers
);
584 ppng_read_end(This
->png_ptr
, This
->end_info
);
586 This
->initialized
= TRUE
;
590 LeaveCriticalSection(&This
->lock
);
595 static HRESULT WINAPI
PngDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
596 GUID
*pguidContainerFormat
)
598 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
602 static HRESULT WINAPI
PngDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
603 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
606 IWICComponentInfo
*compinfo
;
608 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
610 hr
= CreateComponentInfo(&CLSID_WICPngDecoder
, &compinfo
);
611 if (FAILED(hr
)) return hr
;
613 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
614 (void**)ppIDecoderInfo
);
616 IWICComponentInfo_Release(compinfo
);
621 static HRESULT WINAPI
PngDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
622 IWICPalette
*pIPalette
)
624 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
628 static HRESULT WINAPI
PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
629 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
631 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
635 static HRESULT WINAPI
PngDecoder_GetPreview(IWICBitmapDecoder
*iface
,
636 IWICBitmapSource
**ppIBitmapSource
)
638 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
640 if (!ppIBitmapSource
) return E_INVALIDARG
;
642 *ppIBitmapSource
= NULL
;
643 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
646 static HRESULT WINAPI
PngDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
647 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
649 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
650 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
653 static HRESULT WINAPI
PngDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
654 IWICBitmapSource
**ppIThumbnail
)
656 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
658 if (!ppIThumbnail
) return E_INVALIDARG
;
660 *ppIThumbnail
= NULL
;
661 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
664 static HRESULT WINAPI
PngDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
667 if (!pCount
) return E_INVALIDARG
;
673 static HRESULT WINAPI
PngDecoder_GetFrame(IWICBitmapDecoder
*iface
,
674 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
676 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
677 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
679 if (!This
->initialized
) return WINCODEC_ERR_FRAMEMISSING
;
681 if (index
!= 0) return E_INVALIDARG
;
683 IWICBitmapDecoder_AddRef(iface
);
685 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
690 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl
= {
691 PngDecoder_QueryInterface
,
694 PngDecoder_QueryCapability
,
695 PngDecoder_Initialize
,
696 PngDecoder_GetContainerFormat
,
697 PngDecoder_GetDecoderInfo
,
698 PngDecoder_CopyPalette
,
699 PngDecoder_GetMetadataQueryReader
,
700 PngDecoder_GetPreview
,
701 PngDecoder_GetColorContexts
,
702 PngDecoder_GetThumbnail
,
703 PngDecoder_GetFrameCount
,
707 static HRESULT WINAPI
PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
710 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
711 if (!ppv
) return E_INVALIDARG
;
713 if (IsEqualIID(&IID_IUnknown
, iid
) ||
714 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
715 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
717 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
719 else if (IsEqualIID(&IID_IWICMetadataBlockReader
, iid
))
721 *ppv
= &This
->IWICMetadataBlockReader_iface
;
726 return E_NOINTERFACE
;
729 IUnknown_AddRef((IUnknown
*)*ppv
);
733 static ULONG WINAPI
PngDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
735 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
736 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
739 static ULONG WINAPI
PngDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
741 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
742 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
745 static HRESULT WINAPI
PngDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
746 UINT
*puiWidth
, UINT
*puiHeight
)
748 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
749 *puiWidth
= This
->width
;
750 *puiHeight
= This
->height
;
751 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
755 static HRESULT WINAPI
PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
756 WICPixelFormatGUID
*pPixelFormat
)
758 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
759 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
761 memcpy(pPixelFormat
, This
->format
, sizeof(GUID
));
766 static HRESULT WINAPI
PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
767 double *pDpiX
, double *pDpiY
)
769 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
770 png_uint_32 ret
, xres
, yres
;
773 EnterCriticalSection(&This
->lock
);
775 ret
= ppng_get_pHYs(This
->png_ptr
, This
->info_ptr
, &xres
, &yres
, &unit_type
);
777 if (ret
&& unit_type
== PNG_RESOLUTION_METER
)
779 *pDpiX
= xres
* 0.0254;
780 *pDpiY
= yres
* 0.0254;
784 WARN("no pHYs block present\n");
785 *pDpiX
= *pDpiY
= 96.0;
788 LeaveCriticalSection(&This
->lock
);
790 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
795 static HRESULT WINAPI
PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
796 IWICPalette
*pIPalette
)
798 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
800 png_colorp png_palette
;
802 WICColor palette
[256];
803 png_bytep trans_alpha
;
805 png_color_16p trans_values
;
809 TRACE("(%p,%p)\n", iface
, pIPalette
);
811 EnterCriticalSection(&This
->lock
);
813 ret
= ppng_get_PLTE(This
->png_ptr
, This
->info_ptr
, &png_palette
, &num_palette
);
816 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
820 if (num_palette
> 256)
822 ERR("palette has %i colors?!\n", num_palette
);
827 ret
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans_alpha
, &num_trans
, &trans_values
);
828 if (!ret
) num_trans
= 0;
830 for (i
=0; i
<num_palette
; i
++)
832 BYTE alpha
= (i
< num_trans
) ? trans_alpha
[i
] : 0xff;
833 palette
[i
] = (alpha
<< 24 |
834 png_palette
[i
].red
<< 16|
835 png_palette
[i
].green
<< 8|
836 png_palette
[i
].blue
);
841 LeaveCriticalSection(&This
->lock
);
844 hr
= IWICPalette_InitializeCustom(pIPalette
, palette
, num_palette
);
849 static HRESULT WINAPI
PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
850 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
852 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
853 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
855 return copy_pixels(This
->bpp
, This
->image_bits
,
856 This
->width
, This
->height
, This
->stride
,
857 prc
, cbStride
, cbBufferSize
, pbBuffer
);
860 static HRESULT WINAPI
PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
861 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
863 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
867 static HRESULT WINAPI
PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
868 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
870 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
874 int compression_type
;
877 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
879 if (!pcActualCount
) return E_INVALIDARG
;
881 EnterCriticalSection(&This
->lock
);
883 if (ppng_get_iCCP(This
->png_ptr
, This
->info_ptr
, &name
, &compression_type
, (void *)&profile
, &len
))
885 if (cCount
&& ppIColorContexts
)
887 hr
= IWICColorContext_InitializeFromMemory(*ppIColorContexts
, profile
, len
);
890 LeaveCriticalSection(&This
->lock
);
899 LeaveCriticalSection(&This
->lock
);
904 static HRESULT WINAPI
PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
905 IWICBitmapSource
**ppIThumbnail
)
907 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
909 if (!ppIThumbnail
) return E_INVALIDARG
;
911 *ppIThumbnail
= NULL
;
912 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
915 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
= {
916 PngDecoder_Frame_QueryInterface
,
917 PngDecoder_Frame_AddRef
,
918 PngDecoder_Frame_Release
,
919 PngDecoder_Frame_GetSize
,
920 PngDecoder_Frame_GetPixelFormat
,
921 PngDecoder_Frame_GetResolution
,
922 PngDecoder_Frame_CopyPalette
,
923 PngDecoder_Frame_CopyPixels
,
924 PngDecoder_Frame_GetMetadataQueryReader
,
925 PngDecoder_Frame_GetColorContexts
,
926 PngDecoder_Frame_GetThumbnail
929 static HRESULT WINAPI
PngDecoder_Block_QueryInterface(IWICMetadataBlockReader
*iface
, REFIID iid
,
932 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
933 return IWICBitmapFrameDecode_QueryInterface(&This
->IWICBitmapFrameDecode_iface
, iid
, ppv
);
936 static ULONG WINAPI
PngDecoder_Block_AddRef(IWICMetadataBlockReader
*iface
)
938 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
939 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
942 static ULONG WINAPI
PngDecoder_Block_Release(IWICMetadataBlockReader
*iface
)
944 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
945 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
948 static HRESULT WINAPI
PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
949 GUID
*pguidContainerFormat
)
951 if (!pguidContainerFormat
) return E_INVALIDARG
;
952 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
956 static HRESULT WINAPI
PngDecoder_Block_GetCount(IWICMetadataBlockReader
*iface
,
960 TRACE("%p,%p\n", iface
, pcCount
);
961 if (!once
++) FIXME("stub\n");
965 static HRESULT WINAPI
PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
966 UINT nIndex
, IWICMetadataReader
**ppIMetadataReader
)
968 FIXME("%p,%d,%p\n", iface
, nIndex
, ppIMetadataReader
);
972 static HRESULT WINAPI
PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
973 IEnumUnknown
**ppIEnumMetadata
)
975 FIXME("%p,%p\n", iface
, ppIEnumMetadata
);
979 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl
= {
980 PngDecoder_Block_QueryInterface
,
981 PngDecoder_Block_AddRef
,
982 PngDecoder_Block_Release
,
983 PngDecoder_Block_GetContainerFormat
,
984 PngDecoder_Block_GetCount
,
985 PngDecoder_Block_GetReaderByIndex
,
986 PngDecoder_Block_GetEnumerator
,
989 HRESULT
PngDecoder_CreateInstance(REFIID iid
, void** ppv
)
994 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
998 if (!libpng_handle
&& !load_libpng())
1000 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG
);
1004 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder
));
1005 if (!This
) return E_OUTOFMEMORY
;
1007 This
->IWICBitmapDecoder_iface
.lpVtbl
= &PngDecoder_Vtbl
;
1008 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &PngDecoder_FrameVtbl
;
1009 This
->IWICMetadataBlockReader_iface
.lpVtbl
= &PngDecoder_BlockVtbl
;
1011 This
->png_ptr
= NULL
;
1012 This
->info_ptr
= NULL
;
1013 This
->end_info
= NULL
;
1014 This
->initialized
= FALSE
;
1015 This
->image_bits
= NULL
;
1016 InitializeCriticalSection(&This
->lock
);
1017 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngDecoder.lock");
1019 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1020 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1025 struct png_pixelformat
{
1026 const WICPixelFormatGUID
*guid
;
1034 static const struct png_pixelformat formats
[] = {
1035 {&GUID_WICPixelFormat24bppBGR
, 24, 8, PNG_COLOR_TYPE_RGB
, 0, 1},
1036 {&GUID_WICPixelFormatBlackWhite
, 1, 1, PNG_COLOR_TYPE_GRAY
, 0, 0},
1037 {&GUID_WICPixelFormat2bppGray
, 2, 2, PNG_COLOR_TYPE_GRAY
, 0, 0},
1038 {&GUID_WICPixelFormat4bppGray
, 4, 4, PNG_COLOR_TYPE_GRAY
, 0, 0},
1039 {&GUID_WICPixelFormat8bppGray
, 8, 8, PNG_COLOR_TYPE_GRAY
, 0, 0},
1040 {&GUID_WICPixelFormat16bppGray
, 16, 16, PNG_COLOR_TYPE_GRAY
, 0, 0},
1041 {&GUID_WICPixelFormat32bppBGR
, 32, 8, PNG_COLOR_TYPE_RGB
, 1, 1},
1042 {&GUID_WICPixelFormat32bppBGRA
, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 1},
1043 {&GUID_WICPixelFormat48bppRGB
, 48, 16, PNG_COLOR_TYPE_RGB
, 0, 0},
1044 {&GUID_WICPixelFormat64bppRGBA
, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 0},
1048 typedef struct PngEncoder
{
1049 IWICBitmapEncoder IWICBitmapEncoder_iface
;
1050 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
1053 png_structp png_ptr
;
1056 BOOL frame_initialized
;
1057 const struct png_pixelformat
*format
;
1062 BOOL frame_committed
;
1064 CRITICAL_SECTION lock
;
1068 static inline PngEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
1070 return CONTAINING_RECORD(iface
, PngEncoder
, IWICBitmapEncoder_iface
);
1073 static inline PngEncoder
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
1075 return CONTAINING_RECORD(iface
, PngEncoder
, IWICBitmapFrameEncode_iface
);
1078 static HRESULT WINAPI
PngFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
1081 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1082 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1084 if (!ppv
) return E_INVALIDARG
;
1086 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1087 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
1089 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
1094 return E_NOINTERFACE
;
1097 IUnknown_AddRef((IUnknown
*)*ppv
);
1101 static ULONG WINAPI
PngFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
1103 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1104 return IWICBitmapEncoder_AddRef(&This
->IWICBitmapEncoder_iface
);
1107 static ULONG WINAPI
PngFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
1109 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1110 return IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1113 static HRESULT WINAPI
PngFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
1114 IPropertyBag2
*pIEncoderOptions
)
1116 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1118 PROPBAG2 opts
[1]= {{0}};
1119 VARIANT opt_values
[1];
1120 HRESULT opt_hres
[1];
1123 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
1125 opts
[0].pstrName
= (LPOLESTR
)wszPngInterlaceOption
;
1126 opts
[0].vt
= VT_BOOL
;
1128 if (pIEncoderOptions
)
1130 hr
= IPropertyBag2_Read(pIEncoderOptions
, 1, opts
, NULL
, opt_values
, opt_hres
);
1136 memset(opt_values
, sizeof(opt_values
), 0);
1138 if (V_VT(&opt_values
[0]) == VT_EMPTY
)
1141 interlace
= (V_BOOL(&opt_values
[0]) != 0);
1143 EnterCriticalSection(&This
->lock
);
1145 if (This
->frame_initialized
)
1147 LeaveCriticalSection(&This
->lock
);
1148 return WINCODEC_ERR_WRONGSTATE
;
1151 This
->interlace
= interlace
;
1153 This
->frame_initialized
= TRUE
;
1155 LeaveCriticalSection(&This
->lock
);
1160 static HRESULT WINAPI
PngFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
1161 UINT uiWidth
, UINT uiHeight
)
1163 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1164 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
1166 EnterCriticalSection(&This
->lock
);
1168 if (!This
->frame_initialized
|| This
->info_written
)
1170 LeaveCriticalSection(&This
->lock
);
1171 return WINCODEC_ERR_WRONGSTATE
;
1174 This
->width
= uiWidth
;
1175 This
->height
= uiHeight
;
1177 LeaveCriticalSection(&This
->lock
);
1182 static HRESULT WINAPI
PngFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
1183 double dpiX
, double dpiY
)
1185 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1186 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
1188 EnterCriticalSection(&This
->lock
);
1190 if (!This
->frame_initialized
|| This
->info_written
)
1192 LeaveCriticalSection(&This
->lock
);
1193 return WINCODEC_ERR_WRONGSTATE
;
1199 LeaveCriticalSection(&This
->lock
);
1204 static HRESULT WINAPI
PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
1205 WICPixelFormatGUID
*pPixelFormat
)
1207 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1209 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
1211 EnterCriticalSection(&This
->lock
);
1213 if (!This
->frame_initialized
|| This
->info_written
)
1215 LeaveCriticalSection(&This
->lock
);
1216 return WINCODEC_ERR_WRONGSTATE
;
1219 for (i
=0; formats
[i
].guid
; i
++)
1221 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
1225 if (!formats
[i
].guid
) i
= 0;
1227 This
->format
= &formats
[i
];
1228 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
1230 LeaveCriticalSection(&This
->lock
);
1235 static HRESULT WINAPI
PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
1236 UINT cCount
, IWICColorContext
**ppIColorContext
)
1238 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1242 static HRESULT WINAPI
PngFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
1243 IWICPalette
*pIPalette
)
1245 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
1246 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1249 static HRESULT WINAPI
PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
1250 IWICBitmapSource
*pIThumbnail
)
1252 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
1253 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1256 static HRESULT WINAPI
PngFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
1257 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
1259 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1260 png_byte
**row_pointers
=NULL
;
1263 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
1265 EnterCriticalSection(&This
->lock
);
1267 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
1269 LeaveCriticalSection(&This
->lock
);
1270 return WINCODEC_ERR_WRONGSTATE
;
1273 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
1275 LeaveCriticalSection(&This
->lock
);
1276 return E_INVALIDARG
;
1279 /* set up setjmp/longjmp error handling */
1282 LeaveCriticalSection(&This
->lock
);
1283 HeapFree(GetProcessHeap(), 0, row_pointers
);
1286 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1288 if (!This
->info_written
)
1290 ppng_set_IHDR(This
->png_ptr
, This
->info_ptr
, This
->width
, This
->height
,
1291 This
->format
->bit_depth
, This
->format
->color_type
,
1292 This
->interlace
? PNG_INTERLACE_ADAM7
: PNG_INTERLACE_NONE
,
1293 PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
1295 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
1297 ppng_set_pHYs(This
->png_ptr
, This
->info_ptr
, (This
->xres
+0.0127) / 0.0254,
1298 (This
->yres
+0.0127) / 0.0254, PNG_RESOLUTION_METER
);
1301 ppng_write_info(This
->png_ptr
, This
->info_ptr
);
1303 if (This
->format
->remove_filler
)
1304 ppng_set_filler(This
->png_ptr
, 0, PNG_FILLER_AFTER
);
1306 if (This
->format
->swap_rgb
)
1307 ppng_set_bgr(This
->png_ptr
);
1309 This
->info_written
= TRUE
;
1312 row_pointers
= HeapAlloc(GetProcessHeap(), 0, lineCount
* sizeof(png_byte
*));
1315 LeaveCriticalSection(&This
->lock
);
1316 return E_OUTOFMEMORY
;
1319 for (i
=0; i
<lineCount
; i
++)
1320 row_pointers
[i
] = pbPixels
+ cbStride
* i
;
1322 ppng_write_rows(This
->png_ptr
, row_pointers
, lineCount
);
1323 This
->lines_written
+= lineCount
;
1325 LeaveCriticalSection(&This
->lock
);
1327 HeapFree(GetProcessHeap(), 0, row_pointers
);
1332 static HRESULT WINAPI
PngFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
1333 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
1335 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1337 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
1339 if (!This
->frame_initialized
)
1340 return WINCODEC_ERR_WRONGSTATE
;
1342 hr
= configure_write_source(iface
, pIBitmapSource
, prc
,
1343 This
->format
? This
->format
->guid
: NULL
, This
->width
, This
->height
,
1344 This
->xres
, This
->yres
);
1348 hr
= write_source(iface
, pIBitmapSource
, prc
,
1349 This
->format
->guid
, This
->format
->bpp
, This
->width
, This
->height
);
1355 static HRESULT WINAPI
PngFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
1357 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1359 TRACE("(%p)\n", iface
);
1361 EnterCriticalSection(&This
->lock
);
1363 if (!This
->info_written
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
1365 LeaveCriticalSection(&This
->lock
);
1366 return WINCODEC_ERR_WRONGSTATE
;
1369 /* set up setjmp/longjmp error handling */
1372 LeaveCriticalSection(&This
->lock
);
1375 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1377 ppng_write_end(This
->png_ptr
, This
->info_ptr
);
1379 This
->frame_committed
= TRUE
;
1381 LeaveCriticalSection(&This
->lock
);
1386 static HRESULT WINAPI
PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1387 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1389 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1393 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl
= {
1394 PngFrameEncode_QueryInterface
,
1395 PngFrameEncode_AddRef
,
1396 PngFrameEncode_Release
,
1397 PngFrameEncode_Initialize
,
1398 PngFrameEncode_SetSize
,
1399 PngFrameEncode_SetResolution
,
1400 PngFrameEncode_SetPixelFormat
,
1401 PngFrameEncode_SetColorContexts
,
1402 PngFrameEncode_SetPalette
,
1403 PngFrameEncode_SetThumbnail
,
1404 PngFrameEncode_WritePixels
,
1405 PngFrameEncode_WriteSource
,
1406 PngFrameEncode_Commit
,
1407 PngFrameEncode_GetMetadataQueryWriter
1410 static HRESULT WINAPI
PngEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1413 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1414 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1416 if (!ppv
) return E_INVALIDARG
;
1418 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1419 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1421 *ppv
= &This
->IWICBitmapEncoder_iface
;
1426 return E_NOINTERFACE
;
1429 IUnknown_AddRef((IUnknown
*)*ppv
);
1433 static ULONG WINAPI
PngEncoder_AddRef(IWICBitmapEncoder
*iface
)
1435 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1436 ULONG ref
= InterlockedIncrement(&This
->ref
);
1438 TRACE("(%p) refcount=%u\n", iface
, ref
);
1443 static ULONG WINAPI
PngEncoder_Release(IWICBitmapEncoder
*iface
)
1445 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1446 ULONG ref
= InterlockedDecrement(&This
->ref
);
1448 TRACE("(%p) refcount=%u\n", iface
, ref
);
1452 This
->lock
.DebugInfo
->Spare
[0] = 0;
1453 DeleteCriticalSection(&This
->lock
);
1455 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1457 IStream_Release(This
->stream
);
1458 HeapFree(GetProcessHeap(), 0, This
);
1464 static void user_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
1466 PngEncoder
*This
= ppng_get_io_ptr(png_ptr
);
1470 hr
= IStream_Write(This
->stream
, data
, length
, &byteswritten
);
1471 if (FAILED(hr
) || byteswritten
!= length
)
1473 ppng_error(png_ptr
, "failed writing data");
1477 static void user_flush(png_structp png_ptr
)
1481 static HRESULT WINAPI
PngEncoder_Initialize(IWICBitmapEncoder
*iface
,
1482 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1484 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1487 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1489 EnterCriticalSection(&This
->lock
);
1493 LeaveCriticalSection(&This
->lock
);
1494 return WINCODEC_ERR_WRONGSTATE
;
1497 /* initialize libpng */
1498 This
->png_ptr
= ppng_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
1501 LeaveCriticalSection(&This
->lock
);
1505 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
1506 if (!This
->info_ptr
)
1508 ppng_destroy_write_struct(&This
->png_ptr
, NULL
);
1509 This
->png_ptr
= NULL
;
1510 LeaveCriticalSection(&This
->lock
);
1514 IStream_AddRef(pIStream
);
1515 This
->stream
= pIStream
;
1517 /* set up setjmp/longjmp error handling */
1520 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1521 This
->png_ptr
= NULL
;
1522 IStream_Release(This
->stream
);
1523 This
->stream
= NULL
;
1524 LeaveCriticalSection(&This
->lock
);
1527 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1529 /* set up custom i/o handling */
1530 ppng_set_write_fn(This
->png_ptr
, This
, user_write_data
, user_flush
);
1532 LeaveCriticalSection(&This
->lock
);
1537 static HRESULT WINAPI
PngEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1538 GUID
*pguidContainerFormat
)
1540 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1544 static HRESULT WINAPI
PngEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1545 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1547 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1551 static HRESULT WINAPI
PngEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1552 UINT cCount
, IWICColorContext
**ppIColorContext
)
1554 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1558 static HRESULT WINAPI
PngEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1560 TRACE("(%p,%p)\n", iface
, pIPalette
);
1561 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1564 static HRESULT WINAPI
PngEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1566 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1567 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1570 static HRESULT WINAPI
PngEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1572 TRACE("(%p,%p)\n", iface
, pIPreview
);
1573 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1576 static HRESULT WINAPI
PngEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1577 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1579 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1581 PROPBAG2 opts
[1]= {{0}};
1583 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1585 EnterCriticalSection(&This
->lock
);
1587 if (This
->frame_count
!= 0)
1589 LeaveCriticalSection(&This
->lock
);
1590 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1595 LeaveCriticalSection(&This
->lock
);
1596 return WINCODEC_ERR_NOTINITIALIZED
;
1599 opts
[0].pstrName
= (LPOLESTR
)wszPngInterlaceOption
;
1600 opts
[0].vt
= VT_BOOL
;
1601 opts
[0].dwType
= PROPBAG2_TYPE_DATA
;
1603 hr
= CreatePropertyBag2(opts
, 1, ppIEncoderOptions
);
1606 LeaveCriticalSection(&This
->lock
);
1610 This
->frame_count
= 1;
1612 LeaveCriticalSection(&This
->lock
);
1614 IWICBitmapEncoder_AddRef(iface
);
1615 *ppIFrameEncode
= &This
->IWICBitmapFrameEncode_iface
;
1620 static HRESULT WINAPI
PngEncoder_Commit(IWICBitmapEncoder
*iface
)
1622 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1623 TRACE("(%p)\n", iface
);
1625 EnterCriticalSection(&This
->lock
);
1627 if (!This
->frame_committed
|| This
->committed
)
1629 LeaveCriticalSection(&This
->lock
);
1630 return WINCODEC_ERR_WRONGSTATE
;
1633 This
->committed
= TRUE
;
1635 LeaveCriticalSection(&This
->lock
);
1640 static HRESULT WINAPI
PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
1641 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1643 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
1647 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl
= {
1648 PngEncoder_QueryInterface
,
1651 PngEncoder_Initialize
,
1652 PngEncoder_GetContainerFormat
,
1653 PngEncoder_GetEncoderInfo
,
1654 PngEncoder_SetColorContexts
,
1655 PngEncoder_SetPalette
,
1656 PngEncoder_SetThumbnail
,
1657 PngEncoder_SetPreview
,
1658 PngEncoder_CreateNewFrame
,
1660 PngEncoder_GetMetadataQueryWriter
1663 HRESULT
PngEncoder_CreateInstance(REFIID iid
, void** ppv
)
1668 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1672 if (!libpng_handle
&& !load_libpng())
1674 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG
);
1678 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder
));
1679 if (!This
) return E_OUTOFMEMORY
;
1681 This
->IWICBitmapEncoder_iface
.lpVtbl
= &PngEncoder_Vtbl
;
1682 This
->IWICBitmapFrameEncode_iface
.lpVtbl
= &PngEncoder_FrameVtbl
;
1684 This
->png_ptr
= NULL
;
1685 This
->info_ptr
= NULL
;
1686 This
->stream
= NULL
;
1687 This
->frame_count
= 0;
1688 This
->frame_initialized
= FALSE
;
1689 This
->format
= NULL
;
1690 This
->info_written
= FALSE
;
1695 This
->lines_written
= 0;
1696 This
->frame_committed
= FALSE
;
1697 This
->committed
= FALSE
;
1698 InitializeCriticalSection(&This
->lock
);
1699 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngEncoder.lock");
1701 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
1702 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1707 #else /* !HAVE_PNG_H */
1709 HRESULT
PngDecoder_CreateInstance(REFIID iid
, void** ppv
)
1711 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1715 HRESULT
PngEncoder_CreateInstance(REFIID iid
, void** ppv
)
1717 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");