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 HRESULT
read_png_chunk(IStream
*stream
, BYTE
*type
, BYTE
**data
, ULONG
*data_size
)
50 hr
= IStream_Read(stream
, header
, 8, &bytesread
);
51 if (FAILED(hr
) || bytesread
< 8)
58 *data_size
= header
[0] << 24 | header
[1] << 16 | header
[2] << 8 | header
[3];
60 memcpy(type
, &header
[4], 4);
64 *data
= HeapAlloc(GetProcessHeap(), 0, *data_size
);
68 hr
= IStream_Read(stream
, *data
, *data_size
, &bytesread
);
70 if (FAILED(hr
) || bytesread
< *data_size
)
74 HeapFree(GetProcessHeap(), 0, *data
);
79 /* Windows ignores CRC of the chunk */
85 static HRESULT
LoadTextMetadata(IStream
*stream
, const GUID
*preferred_vendor
,
86 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
92 ULONG name_len
, value_len
;
97 hr
= read_png_chunk(stream
, type
, &data
, &data_size
);
98 if (FAILED(hr
)) return hr
;
100 name_end_ptr
= memchr(data
, 0, data_size
);
102 name_len
= name_end_ptr
- data
;
104 if (!name_end_ptr
|| name_len
> 79)
106 HeapFree(GetProcessHeap(), 0, data
);
110 value_len
= data_size
- name_len
- 1;
112 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem
));
113 name
= HeapAlloc(GetProcessHeap(), 0, name_len
+ 1);
114 value
= HeapAlloc(GetProcessHeap(), 0, value_len
+ 1);
115 if (!result
|| !name
|| !value
)
117 HeapFree(GetProcessHeap(), 0, data
);
118 HeapFree(GetProcessHeap(), 0, result
);
119 HeapFree(GetProcessHeap(), 0, name
);
120 HeapFree(GetProcessHeap(), 0, value
);
121 return E_OUTOFMEMORY
;
124 PropVariantInit(&result
[0].schema
);
125 PropVariantInit(&result
[0].id
);
126 PropVariantInit(&result
[0].value
);
128 memcpy(name
, data
, name_len
+ 1);
129 memcpy(value
, name_end_ptr
+ 1, value_len
);
130 value
[value_len
] = 0;
132 result
[0].id
.vt
= VT_LPSTR
;
133 result
[0].id
.u
.pszVal
= name
;
134 result
[0].value
.vt
= VT_LPSTR
;
135 result
[0].value
.u
.pszVal
= value
;
140 HeapFree(GetProcessHeap(), 0, data
);
145 static const MetadataHandlerVtbl TextReader_Vtbl
= {
147 &CLSID_WICPngTextMetadataReader
,
151 HRESULT
PngTextReader_CreateInstance(REFIID iid
, void** ppv
)
153 return MetadataReader_Create(&TextReader_Vtbl
, iid
, ppv
);
158 static void *libpng_handle
;
159 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
160 MAKE_FUNCPTR(png_create_read_struct
);
161 MAKE_FUNCPTR(png_create_info_struct
);
162 MAKE_FUNCPTR(png_create_write_struct
);
163 MAKE_FUNCPTR(png_destroy_read_struct
);
164 MAKE_FUNCPTR(png_destroy_write_struct
);
165 MAKE_FUNCPTR(png_error
);
166 MAKE_FUNCPTR(png_get_bit_depth
);
167 MAKE_FUNCPTR(png_get_color_type
);
168 MAKE_FUNCPTR(png_get_error_ptr
);
169 MAKE_FUNCPTR(png_get_iCCP
);
170 MAKE_FUNCPTR(png_get_image_height
);
171 MAKE_FUNCPTR(png_get_image_width
);
172 MAKE_FUNCPTR(png_get_io_ptr
);
173 MAKE_FUNCPTR(png_get_pHYs
);
174 MAKE_FUNCPTR(png_get_PLTE
);
175 MAKE_FUNCPTR(png_get_tRNS
);
176 MAKE_FUNCPTR(png_set_bgr
);
177 MAKE_FUNCPTR(png_set_crc_action
);
178 MAKE_FUNCPTR(png_set_error_fn
);
179 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
180 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8
);
182 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8
);
184 MAKE_FUNCPTR(png_set_filler
);
185 MAKE_FUNCPTR(png_set_gray_to_rgb
);
186 MAKE_FUNCPTR(png_set_IHDR
);
187 MAKE_FUNCPTR(png_set_pHYs
);
188 MAKE_FUNCPTR(png_set_read_fn
);
189 MAKE_FUNCPTR(png_set_strip_16
);
190 MAKE_FUNCPTR(png_set_tRNS_to_alpha
);
191 MAKE_FUNCPTR(png_set_write_fn
);
192 MAKE_FUNCPTR(png_read_end
);
193 MAKE_FUNCPTR(png_read_image
);
194 MAKE_FUNCPTR(png_read_info
);
195 MAKE_FUNCPTR(png_write_end
);
196 MAKE_FUNCPTR(png_write_info
);
197 MAKE_FUNCPTR(png_write_rows
);
200 static void *load_libpng(void)
202 if((libpng_handle
= wine_dlopen(SONAME_LIBPNG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
204 #define LOAD_FUNCPTR(f) \
205 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
206 libpng_handle = NULL; \
209 LOAD_FUNCPTR(png_create_read_struct
);
210 LOAD_FUNCPTR(png_create_info_struct
);
211 LOAD_FUNCPTR(png_create_write_struct
);
212 LOAD_FUNCPTR(png_destroy_read_struct
);
213 LOAD_FUNCPTR(png_destroy_write_struct
);
214 LOAD_FUNCPTR(png_error
);
215 LOAD_FUNCPTR(png_get_bit_depth
);
216 LOAD_FUNCPTR(png_get_color_type
);
217 LOAD_FUNCPTR(png_get_error_ptr
);
218 LOAD_FUNCPTR(png_get_iCCP
);
219 LOAD_FUNCPTR(png_get_image_height
);
220 LOAD_FUNCPTR(png_get_image_width
);
221 LOAD_FUNCPTR(png_get_io_ptr
);
222 LOAD_FUNCPTR(png_get_pHYs
);
223 LOAD_FUNCPTR(png_get_PLTE
);
224 LOAD_FUNCPTR(png_get_tRNS
);
225 LOAD_FUNCPTR(png_set_bgr
);
226 LOAD_FUNCPTR(png_set_crc_action
);
227 LOAD_FUNCPTR(png_set_error_fn
);
228 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
229 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8
);
231 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8
);
233 LOAD_FUNCPTR(png_set_filler
);
234 LOAD_FUNCPTR(png_set_gray_to_rgb
);
235 LOAD_FUNCPTR(png_set_IHDR
);
236 LOAD_FUNCPTR(png_set_pHYs
);
237 LOAD_FUNCPTR(png_set_read_fn
);
238 LOAD_FUNCPTR(png_set_strip_16
);
239 LOAD_FUNCPTR(png_set_tRNS_to_alpha
);
240 LOAD_FUNCPTR(png_set_write_fn
);
241 LOAD_FUNCPTR(png_read_end
);
242 LOAD_FUNCPTR(png_read_image
);
243 LOAD_FUNCPTR(png_read_info
);
244 LOAD_FUNCPTR(png_write_end
);
245 LOAD_FUNCPTR(png_write_info
);
246 LOAD_FUNCPTR(png_write_rows
);
250 return libpng_handle
;
253 static void user_error_fn(png_structp png_ptr
, png_const_charp error_message
)
257 /* This uses setjmp/longjmp just like the default. We can't use the
258 * default because there's no way to access the jmp buffer in the png_struct
259 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
260 WARN("PNG error: %s\n", debugstr_a(error_message
));
261 pjmpbuf
= ppng_get_error_ptr(png_ptr
);
262 longjmp(*pjmpbuf
, 1);
265 static void user_warning_fn(png_structp png_ptr
, png_const_charp warning_message
)
267 WARN("PNG warning: %s\n", debugstr_a(warning_message
));
271 IWICBitmapDecoder IWICBitmapDecoder_iface
;
272 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
273 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
282 const WICPixelFormatGUID
*format
;
284 CRITICAL_SECTION lock
; /* must be held when png structures are accessed or initialized is set */
287 static inline PngDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
289 return CONTAINING_RECORD(iface
, PngDecoder
, IWICBitmapDecoder_iface
);
292 static inline PngDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
294 return CONTAINING_RECORD(iface
, PngDecoder
, IWICBitmapFrameDecode_iface
);
297 static inline PngDecoder
*impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
299 return CONTAINING_RECORD(iface
, PngDecoder
, IWICMetadataBlockReader_iface
);
302 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
;
304 static HRESULT WINAPI
PngDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
307 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
308 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
310 if (!ppv
) return E_INVALIDARG
;
312 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
314 *ppv
= &This
->IWICBitmapDecoder_iface
;
319 return E_NOINTERFACE
;
322 IUnknown_AddRef((IUnknown
*)*ppv
);
326 static ULONG WINAPI
PngDecoder_AddRef(IWICBitmapDecoder
*iface
)
328 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
329 ULONG ref
= InterlockedIncrement(&This
->ref
);
331 TRACE("(%p) refcount=%u\n", iface
, ref
);
336 static ULONG WINAPI
PngDecoder_Release(IWICBitmapDecoder
*iface
)
338 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
339 ULONG ref
= InterlockedDecrement(&This
->ref
);
341 TRACE("(%p) refcount=%u\n", iface
, ref
);
346 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
347 This
->lock
.DebugInfo
->Spare
[0] = 0;
348 DeleteCriticalSection(&This
->lock
);
349 HeapFree(GetProcessHeap(), 0, This
->image_bits
);
350 HeapFree(GetProcessHeap(), 0, This
);
356 static HRESULT WINAPI
PngDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
361 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
363 if (!stream
|| !capability
) return E_INVALIDARG
;
365 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
366 if (hr
!= S_OK
) return hr
;
368 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
369 WICBitmapDecoderCapabilityCanDecodeSomeImages
;
370 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
374 static void user_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
376 IStream
*stream
= ppng_get_io_ptr(png_ptr
);
380 hr
= IStream_Read(stream
, data
, length
, &bytesread
);
381 if (FAILED(hr
) || bytesread
!= length
)
383 ppng_error(png_ptr
, "failed reading data");
387 static HRESULT WINAPI
PngDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
388 WICDecodeOptions cacheOptions
)
390 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
393 png_bytep
*row_pointers
=NULL
;
396 int color_type
, bit_depth
;
399 png_uint_32 transparency
;
400 png_color_16p trans_values
;
403 TRACE("(%p,%p,%x)\n", iface
, pIStream
, cacheOptions
);
405 EnterCriticalSection(&This
->lock
);
407 /* initialize libpng */
408 This
->png_ptr
= ppng_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
415 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
418 ppng_destroy_read_struct(&This
->png_ptr
, NULL
, NULL
);
419 This
->png_ptr
= NULL
;
424 This
->end_info
= ppng_create_info_struct(This
->png_ptr
);
427 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, NULL
);
428 This
->png_ptr
= NULL
;
433 /* set up setjmp/longjmp error handling */
436 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
437 HeapFree(GetProcessHeap(), 0, row_pointers
);
438 This
->png_ptr
= NULL
;
442 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
443 ppng_set_crc_action(This
->png_ptr
, PNG_CRC_QUIET_USE
, PNG_CRC_QUIET_USE
);
445 /* seek to the start of the stream */
447 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
448 if (FAILED(hr
)) goto end
;
450 /* set up custom i/o handling */
451 ppng_set_read_fn(This
->png_ptr
, pIStream
, user_read_data
);
453 /* read the header */
454 ppng_read_info(This
->png_ptr
, This
->info_ptr
);
456 /* choose a pixel format */
457 color_type
= ppng_get_color_type(This
->png_ptr
, This
->info_ptr
);
458 bit_depth
= ppng_get_bit_depth(This
->png_ptr
, This
->info_ptr
);
460 /* check for color-keyed alpha */
461 transparency
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
463 if (transparency
&& color_type
!= PNG_COLOR_TYPE_PALETTE
)
466 if (color_type
== PNG_COLOR_TYPE_GRAY
)
470 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
471 ppng_set_expand_gray_1_2_4_to_8(This
->png_ptr
);
473 ppng_set_gray_1_2_4_to_8(This
->png_ptr
);
477 ppng_set_gray_to_rgb(This
->png_ptr
);
479 ppng_set_tRNS_to_alpha(This
->png_ptr
);
480 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
485 case PNG_COLOR_TYPE_GRAY
:
486 This
->bpp
= bit_depth
;
489 case 1: This
->format
= &GUID_WICPixelFormatBlackWhite
; break;
490 case 2: This
->format
= &GUID_WICPixelFormat2bppGray
; break;
491 case 4: This
->format
= &GUID_WICPixelFormat4bppGray
; break;
492 case 8: This
->format
= &GUID_WICPixelFormat8bppGray
; break;
493 case 16: This
->format
= &GUID_WICPixelFormat16bppGray
; break;
495 ERR("invalid grayscale bit depth: %i\n", bit_depth
);
500 case PNG_COLOR_TYPE_GRAY_ALPHA
:
501 /* WIC does not support grayscale alpha formats so use RGBA */
502 ppng_set_gray_to_rgb(This
->png_ptr
);
504 case PNG_COLOR_TYPE_RGB_ALPHA
:
505 This
->bpp
= bit_depth
* 4;
509 ppng_set_bgr(This
->png_ptr
);
510 This
->format
= &GUID_WICPixelFormat32bppBGRA
;
512 case 16: This
->format
= &GUID_WICPixelFormat64bppRGBA
; break;
514 ERR("invalid RGBA bit depth: %i\n", bit_depth
);
519 case PNG_COLOR_TYPE_PALETTE
:
520 This
->bpp
= bit_depth
;
523 case 1: This
->format
= &GUID_WICPixelFormat1bppIndexed
; break;
524 case 2: This
->format
= &GUID_WICPixelFormat2bppIndexed
; break;
525 case 4: This
->format
= &GUID_WICPixelFormat4bppIndexed
; break;
526 case 8: This
->format
= &GUID_WICPixelFormat8bppIndexed
; break;
528 ERR("invalid indexed color bit depth: %i\n", bit_depth
);
533 case PNG_COLOR_TYPE_RGB
:
534 This
->bpp
= bit_depth
* 3;
538 ppng_set_bgr(This
->png_ptr
);
539 This
->format
= &GUID_WICPixelFormat24bppBGR
;
541 case 16: This
->format
= &GUID_WICPixelFormat48bppRGB
; break;
543 ERR("invalid RGB color bit depth: %i\n", bit_depth
);
549 ERR("invalid color type %i\n", color_type
);
554 /* read the image data */
555 This
->width
= ppng_get_image_width(This
->png_ptr
, This
->info_ptr
);
556 This
->height
= ppng_get_image_height(This
->png_ptr
, This
->info_ptr
);
557 This
->stride
= This
->width
* This
->bpp
;
558 image_size
= This
->stride
* This
->height
;
560 This
->image_bits
= HeapAlloc(GetProcessHeap(), 0, image_size
);
561 if (!This
->image_bits
)
567 row_pointers
= HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep
)*This
->height
);
574 for (i
=0; i
<This
->height
; i
++)
575 row_pointers
[i
] = This
->image_bits
+ i
* This
->stride
;
577 ppng_read_image(This
->png_ptr
, row_pointers
);
579 HeapFree(GetProcessHeap(), 0, row_pointers
);
582 ppng_read_end(This
->png_ptr
, This
->end_info
);
584 This
->initialized
= TRUE
;
588 LeaveCriticalSection(&This
->lock
);
593 static HRESULT WINAPI
PngDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
594 GUID
*pguidContainerFormat
)
596 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
600 static HRESULT WINAPI
PngDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
601 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
604 IWICComponentInfo
*compinfo
;
606 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
608 hr
= CreateComponentInfo(&CLSID_WICPngDecoder
, &compinfo
);
609 if (FAILED(hr
)) return hr
;
611 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
612 (void**)ppIDecoderInfo
);
614 IWICComponentInfo_Release(compinfo
);
619 static HRESULT WINAPI
PngDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
620 IWICPalette
*pIPalette
)
622 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
626 static HRESULT WINAPI
PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
627 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
629 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
633 static HRESULT WINAPI
PngDecoder_GetPreview(IWICBitmapDecoder
*iface
,
634 IWICBitmapSource
**ppIBitmapSource
)
636 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
638 if (!ppIBitmapSource
) return E_INVALIDARG
;
640 *ppIBitmapSource
= NULL
;
641 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
644 static HRESULT WINAPI
PngDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
645 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
647 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
648 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
651 static HRESULT WINAPI
PngDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
652 IWICBitmapSource
**ppIThumbnail
)
654 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
656 if (!ppIThumbnail
) return E_INVALIDARG
;
658 *ppIThumbnail
= NULL
;
659 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
662 static HRESULT WINAPI
PngDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
665 if (!pCount
) return E_INVALIDARG
;
671 static HRESULT WINAPI
PngDecoder_GetFrame(IWICBitmapDecoder
*iface
,
672 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
674 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
675 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
677 if (!This
->initialized
) return WINCODEC_ERR_FRAMEMISSING
;
679 if (index
!= 0) return E_INVALIDARG
;
681 IWICBitmapDecoder_AddRef(iface
);
683 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
688 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl
= {
689 PngDecoder_QueryInterface
,
692 PngDecoder_QueryCapability
,
693 PngDecoder_Initialize
,
694 PngDecoder_GetContainerFormat
,
695 PngDecoder_GetDecoderInfo
,
696 PngDecoder_CopyPalette
,
697 PngDecoder_GetMetadataQueryReader
,
698 PngDecoder_GetPreview
,
699 PngDecoder_GetColorContexts
,
700 PngDecoder_GetThumbnail
,
701 PngDecoder_GetFrameCount
,
705 static HRESULT WINAPI
PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
708 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
709 if (!ppv
) return E_INVALIDARG
;
711 if (IsEqualIID(&IID_IUnknown
, iid
) ||
712 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
713 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
715 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
717 else if (IsEqualIID(&IID_IWICMetadataBlockReader
, iid
))
719 *ppv
= &This
->IWICMetadataBlockReader_iface
;
724 return E_NOINTERFACE
;
727 IUnknown_AddRef((IUnknown
*)*ppv
);
731 static ULONG WINAPI
PngDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
733 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
734 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
737 static ULONG WINAPI
PngDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
739 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
740 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
743 static HRESULT WINAPI
PngDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
744 UINT
*puiWidth
, UINT
*puiHeight
)
746 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
747 *puiWidth
= This
->width
;
748 *puiHeight
= This
->height
;
749 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
753 static HRESULT WINAPI
PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
754 WICPixelFormatGUID
*pPixelFormat
)
756 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
757 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
759 memcpy(pPixelFormat
, This
->format
, sizeof(GUID
));
764 static HRESULT WINAPI
PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
765 double *pDpiX
, double *pDpiY
)
767 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
768 png_uint_32 ret
, xres
, yres
;
771 EnterCriticalSection(&This
->lock
);
773 ret
= ppng_get_pHYs(This
->png_ptr
, This
->info_ptr
, &xres
, &yres
, &unit_type
);
775 if (ret
&& unit_type
== PNG_RESOLUTION_METER
)
777 *pDpiX
= xres
* 0.0254;
778 *pDpiY
= yres
* 0.0254;
782 WARN("no pHYs block present\n");
783 *pDpiX
= *pDpiY
= 96.0;
786 LeaveCriticalSection(&This
->lock
);
788 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
793 static HRESULT WINAPI
PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
794 IWICPalette
*pIPalette
)
796 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
798 png_colorp png_palette
;
800 WICColor palette
[256];
801 png_bytep trans_alpha
;
803 png_color_16p trans_values
;
807 TRACE("(%p,%p)\n", iface
, pIPalette
);
809 EnterCriticalSection(&This
->lock
);
811 ret
= ppng_get_PLTE(This
->png_ptr
, This
->info_ptr
, &png_palette
, &num_palette
);
814 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
818 if (num_palette
> 256)
820 ERR("palette has %i colors?!\n", num_palette
);
825 ret
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans_alpha
, &num_trans
, &trans_values
);
826 if (!ret
) num_trans
= 0;
828 for (i
=0; i
<num_palette
; i
++)
830 BYTE alpha
= (i
< num_trans
) ? trans_alpha
[i
] : 0xff;
831 palette
[i
] = (alpha
<< 24 |
832 png_palette
[i
].red
<< 16|
833 png_palette
[i
].green
<< 8|
834 png_palette
[i
].blue
);
839 LeaveCriticalSection(&This
->lock
);
842 hr
= IWICPalette_InitializeCustom(pIPalette
, palette
, num_palette
);
847 static HRESULT WINAPI
PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
848 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
850 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
851 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
853 return copy_pixels(This
->bpp
, This
->image_bits
,
854 This
->width
, This
->height
, This
->stride
,
855 prc
, cbStride
, cbBufferSize
, pbBuffer
);
858 static HRESULT WINAPI
PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
859 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
861 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
865 static HRESULT WINAPI
PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
866 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
868 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
872 int compression_type
;
875 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
877 if (!pcActualCount
) return E_INVALIDARG
;
879 EnterCriticalSection(&This
->lock
);
881 if (ppng_get_iCCP(This
->png_ptr
, This
->info_ptr
, &name
, &compression_type
, (void *)&profile
, &len
))
883 if (cCount
&& ppIColorContexts
)
885 hr
= IWICColorContext_InitializeFromMemory(*ppIColorContexts
, profile
, len
);
888 LeaveCriticalSection(&This
->lock
);
897 LeaveCriticalSection(&This
->lock
);
902 static HRESULT WINAPI
PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
903 IWICBitmapSource
**ppIThumbnail
)
905 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
907 if (!ppIThumbnail
) return E_INVALIDARG
;
909 *ppIThumbnail
= NULL
;
910 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
913 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
= {
914 PngDecoder_Frame_QueryInterface
,
915 PngDecoder_Frame_AddRef
,
916 PngDecoder_Frame_Release
,
917 PngDecoder_Frame_GetSize
,
918 PngDecoder_Frame_GetPixelFormat
,
919 PngDecoder_Frame_GetResolution
,
920 PngDecoder_Frame_CopyPalette
,
921 PngDecoder_Frame_CopyPixels
,
922 PngDecoder_Frame_GetMetadataQueryReader
,
923 PngDecoder_Frame_GetColorContexts
,
924 PngDecoder_Frame_GetThumbnail
927 static HRESULT WINAPI
PngDecoder_Block_QueryInterface(IWICMetadataBlockReader
*iface
, REFIID iid
,
930 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
931 return IWICBitmapFrameDecode_QueryInterface(&This
->IWICBitmapFrameDecode_iface
, iid
, ppv
);
934 static ULONG WINAPI
PngDecoder_Block_AddRef(IWICMetadataBlockReader
*iface
)
936 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
937 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
940 static ULONG WINAPI
PngDecoder_Block_Release(IWICMetadataBlockReader
*iface
)
942 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
943 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
946 static HRESULT WINAPI
PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
947 GUID
*pguidContainerFormat
)
949 if (!pguidContainerFormat
) return E_INVALIDARG
;
950 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
954 static HRESULT WINAPI
PngDecoder_Block_GetCount(IWICMetadataBlockReader
*iface
,
958 TRACE("%p,%p\n", iface
, pcCount
);
959 if (!once
++) FIXME("stub\n");
963 static HRESULT WINAPI
PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
964 UINT nIndex
, IWICMetadataReader
**ppIMetadataReader
)
966 FIXME("%p,%d,%p\n", iface
, nIndex
, ppIMetadataReader
);
970 static HRESULT WINAPI
PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
971 IEnumUnknown
**ppIEnumMetadata
)
973 FIXME("%p,%p\n", iface
, ppIEnumMetadata
);
977 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl
= {
978 PngDecoder_Block_QueryInterface
,
979 PngDecoder_Block_AddRef
,
980 PngDecoder_Block_Release
,
981 PngDecoder_Block_GetContainerFormat
,
982 PngDecoder_Block_GetCount
,
983 PngDecoder_Block_GetReaderByIndex
,
984 PngDecoder_Block_GetEnumerator
,
987 HRESULT
PngDecoder_CreateInstance(REFIID iid
, void** ppv
)
992 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
996 if (!libpng_handle
&& !load_libpng())
998 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG
);
1002 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder
));
1003 if (!This
) return E_OUTOFMEMORY
;
1005 This
->IWICBitmapDecoder_iface
.lpVtbl
= &PngDecoder_Vtbl
;
1006 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &PngDecoder_FrameVtbl
;
1007 This
->IWICMetadataBlockReader_iface
.lpVtbl
= &PngDecoder_BlockVtbl
;
1009 This
->png_ptr
= NULL
;
1010 This
->info_ptr
= NULL
;
1011 This
->end_info
= NULL
;
1012 This
->initialized
= FALSE
;
1013 This
->image_bits
= NULL
;
1014 InitializeCriticalSection(&This
->lock
);
1015 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngDecoder.lock");
1017 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1018 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1023 struct png_pixelformat
{
1024 const WICPixelFormatGUID
*guid
;
1032 static const struct png_pixelformat formats
[] = {
1033 {&GUID_WICPixelFormat24bppBGR
, 24, 8, PNG_COLOR_TYPE_RGB
, 0, 1},
1034 {&GUID_WICPixelFormatBlackWhite
, 1, 1, PNG_COLOR_TYPE_GRAY
, 0, 0},
1035 {&GUID_WICPixelFormat2bppGray
, 2, 2, PNG_COLOR_TYPE_GRAY
, 0, 0},
1036 {&GUID_WICPixelFormat4bppGray
, 4, 4, PNG_COLOR_TYPE_GRAY
, 0, 0},
1037 {&GUID_WICPixelFormat8bppGray
, 8, 8, PNG_COLOR_TYPE_GRAY
, 0, 0},
1038 {&GUID_WICPixelFormat16bppGray
, 16, 16, PNG_COLOR_TYPE_GRAY
, 0, 0},
1039 {&GUID_WICPixelFormat32bppBGR
, 32, 8, PNG_COLOR_TYPE_RGB
, 1, 1},
1040 {&GUID_WICPixelFormat32bppBGRA
, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 1},
1041 {&GUID_WICPixelFormat48bppRGB
, 48, 16, PNG_COLOR_TYPE_RGB
, 0, 0},
1042 {&GUID_WICPixelFormat64bppRGBA
, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 0},
1046 typedef struct PngEncoder
{
1047 IWICBitmapEncoder IWICBitmapEncoder_iface
;
1048 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
1051 png_structp png_ptr
;
1054 BOOL frame_initialized
;
1055 const struct png_pixelformat
*format
;
1060 BOOL frame_committed
;
1062 CRITICAL_SECTION lock
;
1065 static inline PngEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
1067 return CONTAINING_RECORD(iface
, PngEncoder
, IWICBitmapEncoder_iface
);
1070 static inline PngEncoder
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
1072 return CONTAINING_RECORD(iface
, PngEncoder
, IWICBitmapFrameEncode_iface
);
1075 static HRESULT WINAPI
PngFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
1078 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1079 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1081 if (!ppv
) return E_INVALIDARG
;
1083 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1084 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
1086 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
1091 return E_NOINTERFACE
;
1094 IUnknown_AddRef((IUnknown
*)*ppv
);
1098 static ULONG WINAPI
PngFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
1100 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1101 return IWICBitmapEncoder_AddRef(&This
->IWICBitmapEncoder_iface
);
1104 static ULONG WINAPI
PngFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
1106 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1107 return IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1110 static HRESULT WINAPI
PngFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
1111 IPropertyBag2
*pIEncoderOptions
)
1113 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1114 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
1116 EnterCriticalSection(&This
->lock
);
1118 if (This
->frame_initialized
)
1120 LeaveCriticalSection(&This
->lock
);
1121 return WINCODEC_ERR_WRONGSTATE
;
1124 This
->frame_initialized
= TRUE
;
1126 LeaveCriticalSection(&This
->lock
);
1131 static HRESULT WINAPI
PngFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
1132 UINT uiWidth
, UINT uiHeight
)
1134 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1135 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
1137 EnterCriticalSection(&This
->lock
);
1139 if (!This
->frame_initialized
|| This
->info_written
)
1141 LeaveCriticalSection(&This
->lock
);
1142 return WINCODEC_ERR_WRONGSTATE
;
1145 This
->width
= uiWidth
;
1146 This
->height
= uiHeight
;
1148 LeaveCriticalSection(&This
->lock
);
1153 static HRESULT WINAPI
PngFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
1154 double dpiX
, double dpiY
)
1156 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1157 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
1159 EnterCriticalSection(&This
->lock
);
1161 if (!This
->frame_initialized
|| This
->info_written
)
1163 LeaveCriticalSection(&This
->lock
);
1164 return WINCODEC_ERR_WRONGSTATE
;
1170 LeaveCriticalSection(&This
->lock
);
1175 static HRESULT WINAPI
PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
1176 WICPixelFormatGUID
*pPixelFormat
)
1178 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1180 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
1182 EnterCriticalSection(&This
->lock
);
1184 if (!This
->frame_initialized
|| This
->info_written
)
1186 LeaveCriticalSection(&This
->lock
);
1187 return WINCODEC_ERR_WRONGSTATE
;
1190 for (i
=0; formats
[i
].guid
; i
++)
1192 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
1196 if (!formats
[i
].guid
) i
= 0;
1198 This
->format
= &formats
[i
];
1199 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
1201 LeaveCriticalSection(&This
->lock
);
1206 static HRESULT WINAPI
PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
1207 UINT cCount
, IWICColorContext
**ppIColorContext
)
1209 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1213 static HRESULT WINAPI
PngFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
1214 IWICPalette
*pIPalette
)
1216 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
1217 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1220 static HRESULT WINAPI
PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
1221 IWICBitmapSource
*pIThumbnail
)
1223 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
1224 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1227 static HRESULT WINAPI
PngFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
1228 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
1230 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1231 png_byte
**row_pointers
=NULL
;
1234 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
1236 EnterCriticalSection(&This
->lock
);
1238 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
1240 LeaveCriticalSection(&This
->lock
);
1241 return WINCODEC_ERR_WRONGSTATE
;
1244 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
1246 LeaveCriticalSection(&This
->lock
);
1247 return E_INVALIDARG
;
1250 /* set up setjmp/longjmp error handling */
1253 LeaveCriticalSection(&This
->lock
);
1254 HeapFree(GetProcessHeap(), 0, row_pointers
);
1257 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1259 if (!This
->info_written
)
1261 ppng_set_IHDR(This
->png_ptr
, This
->info_ptr
, This
->width
, This
->height
,
1262 This
->format
->bit_depth
, This
->format
->color_type
, PNG_INTERLACE_NONE
,
1263 PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
1265 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
1267 ppng_set_pHYs(This
->png_ptr
, This
->info_ptr
, (This
->xres
+0.0127) / 0.0254,
1268 (This
->yres
+0.0127) / 0.0254, PNG_RESOLUTION_METER
);
1271 ppng_write_info(This
->png_ptr
, This
->info_ptr
);
1273 if (This
->format
->remove_filler
)
1274 ppng_set_filler(This
->png_ptr
, 0, PNG_FILLER_AFTER
);
1276 if (This
->format
->swap_rgb
)
1277 ppng_set_bgr(This
->png_ptr
);
1279 This
->info_written
= TRUE
;
1282 row_pointers
= HeapAlloc(GetProcessHeap(), 0, lineCount
* sizeof(png_byte
*));
1285 LeaveCriticalSection(&This
->lock
);
1286 return E_OUTOFMEMORY
;
1289 for (i
=0; i
<lineCount
; i
++)
1290 row_pointers
[i
] = pbPixels
+ cbStride
* i
;
1292 ppng_write_rows(This
->png_ptr
, row_pointers
, lineCount
);
1293 This
->lines_written
+= lineCount
;
1295 LeaveCriticalSection(&This
->lock
);
1297 HeapFree(GetProcessHeap(), 0, row_pointers
);
1302 static HRESULT WINAPI
PngFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
1303 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
1305 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1308 WICPixelFormatGUID guid
;
1311 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
1313 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
)
1314 return WINCODEC_ERR_WRONGSTATE
;
1318 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
1319 if (FAILED(hr
)) return hr
;
1320 hr
= IWICBitmapFrameEncode_SetPixelFormat(iface
, &guid
);
1321 if (FAILED(hr
)) return hr
;
1324 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
1325 if (FAILED(hr
)) return hr
;
1326 if (memcmp(&guid
, This
->format
->guid
, sizeof(GUID
)) != 0)
1328 /* FIXME: should use WICConvertBitmapSource to convert */
1329 ERR("format %s unsupported\n", debugstr_guid(&guid
));
1333 if (This
->xres
== 0.0 || This
->yres
== 0.0)
1336 hr
= IWICBitmapSource_GetResolution(pIBitmapSource
, &xres
, &yres
);
1337 if (FAILED(hr
)) return hr
;
1338 hr
= IWICBitmapFrameEncode_SetResolution(iface
, xres
, yres
);
1339 if (FAILED(hr
)) return hr
;
1345 hr
= IWICBitmapSource_GetSize(pIBitmapSource
, &width
, &height
);
1346 if (FAILED(hr
)) return hr
;
1354 if (prc
->Width
!= This
->width
) return E_INVALIDARG
;
1356 stride
= (This
->format
->bpp
* This
->width
+ 7)/8;
1358 pixeldata
= HeapAlloc(GetProcessHeap(), 0, stride
* prc
->Height
);
1359 if (!pixeldata
) return E_OUTOFMEMORY
;
1361 hr
= IWICBitmapSource_CopyPixels(pIBitmapSource
, prc
, stride
,
1362 stride
*prc
->Height
, pixeldata
);
1366 hr
= IWICBitmapFrameEncode_WritePixels(iface
, prc
->Height
, stride
,
1367 stride
*prc
->Height
, pixeldata
);
1370 HeapFree(GetProcessHeap(), 0, pixeldata
);
1375 static HRESULT WINAPI
PngFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
1377 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1379 TRACE("(%p)\n", iface
);
1381 EnterCriticalSection(&This
->lock
);
1383 if (!This
->info_written
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
1385 LeaveCriticalSection(&This
->lock
);
1386 return WINCODEC_ERR_WRONGSTATE
;
1389 /* set up setjmp/longjmp error handling */
1392 LeaveCriticalSection(&This
->lock
);
1395 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1397 ppng_write_end(This
->png_ptr
, This
->info_ptr
);
1399 This
->frame_committed
= TRUE
;
1401 LeaveCriticalSection(&This
->lock
);
1406 static HRESULT WINAPI
PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1407 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1409 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1413 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl
= {
1414 PngFrameEncode_QueryInterface
,
1415 PngFrameEncode_AddRef
,
1416 PngFrameEncode_Release
,
1417 PngFrameEncode_Initialize
,
1418 PngFrameEncode_SetSize
,
1419 PngFrameEncode_SetResolution
,
1420 PngFrameEncode_SetPixelFormat
,
1421 PngFrameEncode_SetColorContexts
,
1422 PngFrameEncode_SetPalette
,
1423 PngFrameEncode_SetThumbnail
,
1424 PngFrameEncode_WritePixels
,
1425 PngFrameEncode_WriteSource
,
1426 PngFrameEncode_Commit
,
1427 PngFrameEncode_GetMetadataQueryWriter
1430 static HRESULT WINAPI
PngEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1433 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1434 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1436 if (!ppv
) return E_INVALIDARG
;
1438 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1439 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1441 *ppv
= &This
->IWICBitmapEncoder_iface
;
1446 return E_NOINTERFACE
;
1449 IUnknown_AddRef((IUnknown
*)*ppv
);
1453 static ULONG WINAPI
PngEncoder_AddRef(IWICBitmapEncoder
*iface
)
1455 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1456 ULONG ref
= InterlockedIncrement(&This
->ref
);
1458 TRACE("(%p) refcount=%u\n", iface
, ref
);
1463 static ULONG WINAPI
PngEncoder_Release(IWICBitmapEncoder
*iface
)
1465 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1466 ULONG ref
= InterlockedDecrement(&This
->ref
);
1468 TRACE("(%p) refcount=%u\n", iface
, ref
);
1472 This
->lock
.DebugInfo
->Spare
[0] = 0;
1473 DeleteCriticalSection(&This
->lock
);
1475 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1477 IStream_Release(This
->stream
);
1478 HeapFree(GetProcessHeap(), 0, This
);
1484 static void user_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
1486 PngEncoder
*This
= ppng_get_io_ptr(png_ptr
);
1490 hr
= IStream_Write(This
->stream
, data
, length
, &byteswritten
);
1491 if (FAILED(hr
) || byteswritten
!= length
)
1493 ppng_error(png_ptr
, "failed writing data");
1497 static void user_flush(png_structp png_ptr
)
1501 static HRESULT WINAPI
PngEncoder_Initialize(IWICBitmapEncoder
*iface
,
1502 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1504 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1507 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1509 EnterCriticalSection(&This
->lock
);
1513 LeaveCriticalSection(&This
->lock
);
1514 return WINCODEC_ERR_WRONGSTATE
;
1517 /* initialize libpng */
1518 This
->png_ptr
= ppng_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
1521 LeaveCriticalSection(&This
->lock
);
1525 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
1526 if (!This
->info_ptr
)
1528 ppng_destroy_write_struct(&This
->png_ptr
, NULL
);
1529 This
->png_ptr
= NULL
;
1530 LeaveCriticalSection(&This
->lock
);
1534 IStream_AddRef(pIStream
);
1535 This
->stream
= pIStream
;
1537 /* set up setjmp/longjmp error handling */
1540 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1541 This
->png_ptr
= NULL
;
1542 IStream_Release(This
->stream
);
1543 This
->stream
= NULL
;
1544 LeaveCriticalSection(&This
->lock
);
1547 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1549 /* set up custom i/o handling */
1550 ppng_set_write_fn(This
->png_ptr
, This
, user_write_data
, user_flush
);
1552 LeaveCriticalSection(&This
->lock
);
1557 static HRESULT WINAPI
PngEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1558 GUID
*pguidContainerFormat
)
1560 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1564 static HRESULT WINAPI
PngEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1565 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1567 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1571 static HRESULT WINAPI
PngEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1572 UINT cCount
, IWICColorContext
**ppIColorContext
)
1574 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1578 static HRESULT WINAPI
PngEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1580 TRACE("(%p,%p)\n", iface
, pIPalette
);
1581 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1584 static HRESULT WINAPI
PngEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1586 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1587 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1590 static HRESULT WINAPI
PngEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1592 TRACE("(%p,%p)\n", iface
, pIPreview
);
1593 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1596 static HRESULT WINAPI
PngEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1597 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1599 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1601 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1603 EnterCriticalSection(&This
->lock
);
1605 if (This
->frame_count
!= 0)
1607 LeaveCriticalSection(&This
->lock
);
1608 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1613 LeaveCriticalSection(&This
->lock
);
1614 return WINCODEC_ERR_NOTINITIALIZED
;
1617 hr
= CreatePropertyBag2(NULL
, 0, ppIEncoderOptions
);
1620 LeaveCriticalSection(&This
->lock
);
1624 This
->frame_count
= 1;
1626 LeaveCriticalSection(&This
->lock
);
1628 IWICBitmapEncoder_AddRef(iface
);
1629 *ppIFrameEncode
= &This
->IWICBitmapFrameEncode_iface
;
1634 static HRESULT WINAPI
PngEncoder_Commit(IWICBitmapEncoder
*iface
)
1636 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1637 TRACE("(%p)\n", iface
);
1639 EnterCriticalSection(&This
->lock
);
1641 if (!This
->frame_committed
|| This
->committed
)
1643 LeaveCriticalSection(&This
->lock
);
1644 return WINCODEC_ERR_WRONGSTATE
;
1647 This
->committed
= TRUE
;
1649 LeaveCriticalSection(&This
->lock
);
1654 static HRESULT WINAPI
PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
1655 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1657 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
1661 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl
= {
1662 PngEncoder_QueryInterface
,
1665 PngEncoder_Initialize
,
1666 PngEncoder_GetContainerFormat
,
1667 PngEncoder_GetEncoderInfo
,
1668 PngEncoder_SetColorContexts
,
1669 PngEncoder_SetPalette
,
1670 PngEncoder_SetThumbnail
,
1671 PngEncoder_SetPreview
,
1672 PngEncoder_CreateNewFrame
,
1674 PngEncoder_GetMetadataQueryWriter
1677 HRESULT
PngEncoder_CreateInstance(REFIID iid
, void** ppv
)
1682 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1686 if (!libpng_handle
&& !load_libpng())
1688 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG
);
1692 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder
));
1693 if (!This
) return E_OUTOFMEMORY
;
1695 This
->IWICBitmapEncoder_iface
.lpVtbl
= &PngEncoder_Vtbl
;
1696 This
->IWICBitmapFrameEncode_iface
.lpVtbl
= &PngEncoder_FrameVtbl
;
1698 This
->png_ptr
= NULL
;
1699 This
->info_ptr
= NULL
;
1700 This
->stream
= NULL
;
1701 This
->frame_count
= 0;
1702 This
->frame_initialized
= FALSE
;
1703 This
->format
= NULL
;
1704 This
->info_written
= FALSE
;
1709 This
->lines_written
= 0;
1710 This
->frame_committed
= FALSE
;
1711 This
->committed
= FALSE
;
1712 InitializeCriticalSection(&This
->lock
);
1713 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngEncoder.lock");
1715 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
1716 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1721 #else /* !HAVE_PNG_H */
1723 HRESULT
PngDecoder_CreateInstance(REFIID iid
, void** ppv
)
1725 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1729 HRESULT
PngEncoder_CreateInstance(REFIID iid
, void** ppv
)
1731 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");