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
19 #include "wincodecs_private.h"
25 static HRESULT
read_png_chunk(IStream
*stream
, BYTE
*type
, BYTE
**data
, ULONG
*data_size
)
31 hr
= IStream_Read(stream
, header
, 8, &bytesread
);
32 if (FAILED(hr
) || bytesread
< 8)
39 *data_size
= header
[0] << 24 | header
[1] << 16 | header
[2] << 8 | header
[3];
41 memcpy(type
, &header
[4], 4);
45 *data
= HeapAlloc(GetProcessHeap(), 0, *data_size
);
49 hr
= IStream_Read(stream
, *data
, *data_size
, &bytesread
);
51 if (FAILED(hr
) || bytesread
< *data_size
)
55 HeapFree(GetProcessHeap(), 0, *data
);
60 /* Windows ignores CRC of the chunk */
66 static HRESULT
LoadTextMetadata(IStream
*stream
, const GUID
*preferred_vendor
,
67 DWORD persist_options
, MetadataItem
**items
, DWORD
*item_count
)
73 ULONG name_len
, value_len
;
78 hr
= read_png_chunk(stream
, type
, &data
, &data_size
);
79 if (FAILED(hr
)) return hr
;
81 name_end_ptr
= memchr(data
, 0, data_size
);
83 name_len
= name_end_ptr
- data
;
85 if (!name_end_ptr
|| name_len
> 79)
87 HeapFree(GetProcessHeap(), 0, data
);
91 value_len
= data_size
- name_len
- 1;
93 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem
));
94 name
= HeapAlloc(GetProcessHeap(), 0, name_len
+ 1);
95 value
= HeapAlloc(GetProcessHeap(), 0, value_len
+ 1);
96 if (!result
|| !name
|| !value
)
98 HeapFree(GetProcessHeap(), 0, data
);
99 HeapFree(GetProcessHeap(), 0, result
);
100 HeapFree(GetProcessHeap(), 0, name
);
101 HeapFree(GetProcessHeap(), 0, value
);
102 return E_OUTOFMEMORY
;
105 PropVariantInit(&result
[0].schema
);
106 PropVariantInit(&result
[0].id
);
107 PropVariantInit(&result
[0].value
);
109 memcpy(name
, data
, name_len
+ 1);
110 memcpy(value
, name_end_ptr
+ 1, value_len
);
111 value
[value_len
] = 0;
113 result
[0].id
.vt
= VT_LPSTR
;
114 result
[0].id
.u
.pszVal
= name
;
115 result
[0].value
.vt
= VT_LPSTR
;
116 result
[0].value
.u
.pszVal
= value
;
121 HeapFree(GetProcessHeap(), 0, data
);
126 static const MetadataHandlerVtbl TextReader_Vtbl
= {
128 &CLSID_WICPngTextMetadataReader
,
132 HRESULT
PngTextReader_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
134 return MetadataReader_Create(&TextReader_Vtbl
, pUnkOuter
, iid
, ppv
);
139 static void *libpng_handle
;
140 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
141 MAKE_FUNCPTR(png_create_read_struct
);
142 MAKE_FUNCPTR(png_create_info_struct
);
143 MAKE_FUNCPTR(png_create_write_struct
);
144 MAKE_FUNCPTR(png_destroy_read_struct
);
145 MAKE_FUNCPTR(png_destroy_write_struct
);
146 MAKE_FUNCPTR(png_error
);
147 MAKE_FUNCPTR(png_get_bit_depth
);
148 MAKE_FUNCPTR(png_get_color_type
);
149 MAKE_FUNCPTR(png_get_error_ptr
);
150 MAKE_FUNCPTR(png_get_iCCP
);
151 MAKE_FUNCPTR(png_get_image_height
);
152 MAKE_FUNCPTR(png_get_image_width
);
153 MAKE_FUNCPTR(png_get_io_ptr
);
154 MAKE_FUNCPTR(png_get_pHYs
);
155 MAKE_FUNCPTR(png_get_PLTE
);
156 MAKE_FUNCPTR(png_get_tRNS
);
157 MAKE_FUNCPTR(png_set_bgr
);
158 MAKE_FUNCPTR(png_set_crc_action
);
159 MAKE_FUNCPTR(png_set_error_fn
);
160 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
161 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8
);
163 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8
);
165 MAKE_FUNCPTR(png_set_filler
);
166 MAKE_FUNCPTR(png_set_gray_to_rgb
);
167 MAKE_FUNCPTR(png_set_IHDR
);
168 MAKE_FUNCPTR(png_set_pHYs
);
169 MAKE_FUNCPTR(png_set_read_fn
);
170 MAKE_FUNCPTR(png_set_strip_16
);
171 MAKE_FUNCPTR(png_set_tRNS_to_alpha
);
172 MAKE_FUNCPTR(png_set_write_fn
);
173 MAKE_FUNCPTR(png_read_end
);
174 MAKE_FUNCPTR(png_read_image
);
175 MAKE_FUNCPTR(png_read_info
);
176 MAKE_FUNCPTR(png_write_end
);
177 MAKE_FUNCPTR(png_write_info
);
178 MAKE_FUNCPTR(png_write_rows
);
181 static void *load_libpng(void)
183 if((libpng_handle
= wine_dlopen(SONAME_LIBPNG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
185 #define LOAD_FUNCPTR(f) \
186 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
187 libpng_handle = NULL; \
190 LOAD_FUNCPTR(png_create_read_struct
);
191 LOAD_FUNCPTR(png_create_info_struct
);
192 LOAD_FUNCPTR(png_create_write_struct
);
193 LOAD_FUNCPTR(png_destroy_read_struct
);
194 LOAD_FUNCPTR(png_destroy_write_struct
);
195 LOAD_FUNCPTR(png_error
);
196 LOAD_FUNCPTR(png_get_bit_depth
);
197 LOAD_FUNCPTR(png_get_color_type
);
198 LOAD_FUNCPTR(png_get_error_ptr
);
199 LOAD_FUNCPTR(png_get_iCCP
);
200 LOAD_FUNCPTR(png_get_image_height
);
201 LOAD_FUNCPTR(png_get_image_width
);
202 LOAD_FUNCPTR(png_get_io_ptr
);
203 LOAD_FUNCPTR(png_get_pHYs
);
204 LOAD_FUNCPTR(png_get_PLTE
);
205 LOAD_FUNCPTR(png_get_tRNS
);
206 LOAD_FUNCPTR(png_set_bgr
);
207 LOAD_FUNCPTR(png_set_crc_action
);
208 LOAD_FUNCPTR(png_set_error_fn
);
209 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
210 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8
);
212 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8
);
214 LOAD_FUNCPTR(png_set_filler
);
215 LOAD_FUNCPTR(png_set_gray_to_rgb
);
216 LOAD_FUNCPTR(png_set_IHDR
);
217 LOAD_FUNCPTR(png_set_pHYs
);
218 LOAD_FUNCPTR(png_set_read_fn
);
219 LOAD_FUNCPTR(png_set_strip_16
);
220 LOAD_FUNCPTR(png_set_tRNS_to_alpha
);
221 LOAD_FUNCPTR(png_set_write_fn
);
222 LOAD_FUNCPTR(png_read_end
);
223 LOAD_FUNCPTR(png_read_image
);
224 LOAD_FUNCPTR(png_read_info
);
225 LOAD_FUNCPTR(png_write_end
);
226 LOAD_FUNCPTR(png_write_info
);
227 LOAD_FUNCPTR(png_write_rows
);
231 return libpng_handle
;
234 static void user_error_fn(png_structp png_ptr
, png_const_charp error_message
)
238 /* This uses setjmp/longjmp just like the default. We can't use the
239 * default because there's no way to access the jmp buffer in the png_struct
240 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
241 WARN("PNG error: %s\n", debugstr_a(error_message
));
242 pjmpbuf
= ppng_get_error_ptr(png_ptr
);
243 longjmp(*pjmpbuf
, 1);
246 static void user_warning_fn(png_structp png_ptr
, png_const_charp warning_message
)
248 WARN("PNG warning: %s\n", debugstr_a(warning_message
));
252 IWICBitmapDecoder IWICBitmapDecoder_iface
;
253 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
254 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
263 const WICPixelFormatGUID
*format
;
265 CRITICAL_SECTION lock
; /* must be held when png structures are accessed or initialized is set */
268 static inline PngDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
270 return CONTAINING_RECORD(iface
, PngDecoder
, IWICBitmapDecoder_iface
);
273 static inline PngDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
275 return CONTAINING_RECORD(iface
, PngDecoder
, IWICBitmapFrameDecode_iface
);
278 static inline PngDecoder
*impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
280 return CONTAINING_RECORD(iface
, PngDecoder
, IWICMetadataBlockReader_iface
);
283 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
;
285 static HRESULT WINAPI
PngDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
288 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
289 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
291 if (!ppv
) return E_INVALIDARG
;
293 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
295 *ppv
= &This
->IWICBitmapDecoder_iface
;
300 return E_NOINTERFACE
;
303 IUnknown_AddRef((IUnknown
*)*ppv
);
307 static ULONG WINAPI
PngDecoder_AddRef(IWICBitmapDecoder
*iface
)
309 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
310 ULONG ref
= InterlockedIncrement(&This
->ref
);
312 TRACE("(%p) refcount=%u\n", iface
, ref
);
317 static ULONG WINAPI
PngDecoder_Release(IWICBitmapDecoder
*iface
)
319 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
320 ULONG ref
= InterlockedDecrement(&This
->ref
);
322 TRACE("(%p) refcount=%u\n", iface
, ref
);
327 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
328 This
->lock
.DebugInfo
->Spare
[0] = 0;
329 DeleteCriticalSection(&This
->lock
);
330 HeapFree(GetProcessHeap(), 0, This
->image_bits
);
331 HeapFree(GetProcessHeap(), 0, This
);
337 static HRESULT WINAPI
PngDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
342 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
344 if (!stream
|| !capability
) return E_INVALIDARG
;
346 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
347 if (hr
!= S_OK
) return hr
;
349 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
350 WICBitmapDecoderCapabilityCanDecodeSomeImages
;
351 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
355 static void user_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
357 IStream
*stream
= ppng_get_io_ptr(png_ptr
);
361 hr
= IStream_Read(stream
, data
, length
, &bytesread
);
362 if (FAILED(hr
) || bytesread
!= length
)
364 ppng_error(png_ptr
, "failed reading data");
368 static HRESULT WINAPI
PngDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
369 WICDecodeOptions cacheOptions
)
371 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
374 png_bytep
*row_pointers
=NULL
;
377 int color_type
, bit_depth
;
380 png_uint_32 transparency
;
381 png_color_16p trans_values
;
384 TRACE("(%p,%p,%x)\n", iface
, pIStream
, cacheOptions
);
386 EnterCriticalSection(&This
->lock
);
388 /* initialize libpng */
389 This
->png_ptr
= ppng_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
396 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
399 ppng_destroy_read_struct(&This
->png_ptr
, NULL
, NULL
);
400 This
->png_ptr
= NULL
;
405 This
->end_info
= ppng_create_info_struct(This
->png_ptr
);
408 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, NULL
);
409 This
->png_ptr
= NULL
;
414 /* set up setjmp/longjmp error handling */
417 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
418 HeapFree(GetProcessHeap(), 0, row_pointers
);
419 This
->png_ptr
= NULL
;
423 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
424 ppng_set_crc_action(This
->png_ptr
, PNG_CRC_QUIET_USE
, PNG_CRC_QUIET_USE
);
426 /* seek to the start of the stream */
428 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
429 if (FAILED(hr
)) goto end
;
431 /* set up custom i/o handling */
432 ppng_set_read_fn(This
->png_ptr
, pIStream
, user_read_data
);
434 /* read the header */
435 ppng_read_info(This
->png_ptr
, This
->info_ptr
);
437 /* choose a pixel format */
438 color_type
= ppng_get_color_type(This
->png_ptr
, This
->info_ptr
);
439 bit_depth
= ppng_get_bit_depth(This
->png_ptr
, This
->info_ptr
);
441 /* check for color-keyed alpha */
442 transparency
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
444 if (transparency
&& color_type
!= PNG_COLOR_TYPE_PALETTE
)
447 if (color_type
== PNG_COLOR_TYPE_GRAY
)
451 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
452 ppng_set_expand_gray_1_2_4_to_8(This
->png_ptr
);
454 ppng_set_gray_1_2_4_to_8(This
->png_ptr
);
458 ppng_set_gray_to_rgb(This
->png_ptr
);
460 ppng_set_tRNS_to_alpha(This
->png_ptr
);
461 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
466 case PNG_COLOR_TYPE_GRAY
:
467 This
->bpp
= bit_depth
;
470 case 1: This
->format
= &GUID_WICPixelFormatBlackWhite
; break;
471 case 2: This
->format
= &GUID_WICPixelFormat2bppGray
; break;
472 case 4: This
->format
= &GUID_WICPixelFormat4bppGray
; break;
473 case 8: This
->format
= &GUID_WICPixelFormat8bppGray
; break;
474 case 16: This
->format
= &GUID_WICPixelFormat16bppGray
; break;
476 ERR("invalid grayscale bit depth: %i\n", bit_depth
);
481 case PNG_COLOR_TYPE_GRAY_ALPHA
:
482 /* WIC does not support grayscale alpha formats so use RGBA */
483 ppng_set_gray_to_rgb(This
->png_ptr
);
485 case PNG_COLOR_TYPE_RGB_ALPHA
:
486 This
->bpp
= bit_depth
* 4;
490 ppng_set_bgr(This
->png_ptr
);
491 This
->format
= &GUID_WICPixelFormat32bppBGRA
;
493 case 16: This
->format
= &GUID_WICPixelFormat64bppRGBA
; break;
495 ERR("invalid RGBA bit depth: %i\n", bit_depth
);
500 case PNG_COLOR_TYPE_PALETTE
:
501 This
->bpp
= bit_depth
;
504 case 1: This
->format
= &GUID_WICPixelFormat1bppIndexed
; break;
505 case 2: This
->format
= &GUID_WICPixelFormat2bppIndexed
; break;
506 case 4: This
->format
= &GUID_WICPixelFormat4bppIndexed
; break;
507 case 8: This
->format
= &GUID_WICPixelFormat8bppIndexed
; break;
509 ERR("invalid indexed color bit depth: %i\n", bit_depth
);
514 case PNG_COLOR_TYPE_RGB
:
515 This
->bpp
= bit_depth
* 3;
519 ppng_set_bgr(This
->png_ptr
);
520 This
->format
= &GUID_WICPixelFormat24bppBGR
;
522 case 16: This
->format
= &GUID_WICPixelFormat48bppRGB
; break;
524 ERR("invalid RGB color bit depth: %i\n", bit_depth
);
530 ERR("invalid color type %i\n", color_type
);
535 /* read the image data */
536 This
->width
= ppng_get_image_width(This
->png_ptr
, This
->info_ptr
);
537 This
->height
= ppng_get_image_height(This
->png_ptr
, This
->info_ptr
);
538 This
->stride
= This
->width
* This
->bpp
;
539 image_size
= This
->stride
* This
->height
;
541 This
->image_bits
= HeapAlloc(GetProcessHeap(), 0, image_size
);
542 if (!This
->image_bits
)
548 row_pointers
= HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep
)*This
->height
);
555 for (i
=0; i
<This
->height
; i
++)
556 row_pointers
[i
] = This
->image_bits
+ i
* This
->stride
;
558 ppng_read_image(This
->png_ptr
, row_pointers
);
560 HeapFree(GetProcessHeap(), 0, row_pointers
);
563 ppng_read_end(This
->png_ptr
, This
->end_info
);
565 This
->initialized
= TRUE
;
569 LeaveCriticalSection(&This
->lock
);
574 static HRESULT WINAPI
PngDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
575 GUID
*pguidContainerFormat
)
577 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
581 static HRESULT WINAPI
PngDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
582 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
585 IWICComponentInfo
*compinfo
;
587 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
589 hr
= CreateComponentInfo(&CLSID_WICPngDecoder
, &compinfo
);
590 if (FAILED(hr
)) return hr
;
592 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
593 (void**)ppIDecoderInfo
);
595 IWICComponentInfo_Release(compinfo
);
600 static HRESULT WINAPI
PngDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
601 IWICPalette
*pIPalette
)
603 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
607 static HRESULT WINAPI
PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
608 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
610 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
614 static HRESULT WINAPI
PngDecoder_GetPreview(IWICBitmapDecoder
*iface
,
615 IWICBitmapSource
**ppIBitmapSource
)
617 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
619 if (!ppIBitmapSource
) return E_INVALIDARG
;
621 *ppIBitmapSource
= NULL
;
622 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
625 static HRESULT WINAPI
PngDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
626 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
628 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
629 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
632 static HRESULT WINAPI
PngDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
633 IWICBitmapSource
**ppIThumbnail
)
635 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
637 if (!ppIThumbnail
) return E_INVALIDARG
;
639 *ppIThumbnail
= NULL
;
640 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
643 static HRESULT WINAPI
PngDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
646 if (!pCount
) return E_INVALIDARG
;
652 static HRESULT WINAPI
PngDecoder_GetFrame(IWICBitmapDecoder
*iface
,
653 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
655 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
656 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
658 if (!This
->initialized
) return WINCODEC_ERR_FRAMEMISSING
;
660 if (index
!= 0) return E_INVALIDARG
;
662 IWICBitmapDecoder_AddRef(iface
);
664 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
669 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl
= {
670 PngDecoder_QueryInterface
,
673 PngDecoder_QueryCapability
,
674 PngDecoder_Initialize
,
675 PngDecoder_GetContainerFormat
,
676 PngDecoder_GetDecoderInfo
,
677 PngDecoder_CopyPalette
,
678 PngDecoder_GetMetadataQueryReader
,
679 PngDecoder_GetPreview
,
680 PngDecoder_GetColorContexts
,
681 PngDecoder_GetThumbnail
,
682 PngDecoder_GetFrameCount
,
686 static HRESULT WINAPI
PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
689 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
690 if (!ppv
) return E_INVALIDARG
;
692 if (IsEqualIID(&IID_IUnknown
, iid
) ||
693 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
694 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
696 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
698 else if (IsEqualIID(&IID_IWICMetadataBlockReader
, iid
))
700 *ppv
= &This
->IWICMetadataBlockReader_iface
;
705 return E_NOINTERFACE
;
708 IUnknown_AddRef((IUnknown
*)*ppv
);
712 static ULONG WINAPI
PngDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
714 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
715 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
718 static ULONG WINAPI
PngDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
720 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
721 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
724 static HRESULT WINAPI
PngDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
725 UINT
*puiWidth
, UINT
*puiHeight
)
727 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
728 *puiWidth
= This
->width
;
729 *puiHeight
= This
->height
;
730 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
734 static HRESULT WINAPI
PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
735 WICPixelFormatGUID
*pPixelFormat
)
737 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
738 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
740 memcpy(pPixelFormat
, This
->format
, sizeof(GUID
));
745 static HRESULT WINAPI
PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
746 double *pDpiX
, double *pDpiY
)
748 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
749 png_uint_32 ret
, xres
, yres
;
752 EnterCriticalSection(&This
->lock
);
754 ret
= ppng_get_pHYs(This
->png_ptr
, This
->info_ptr
, &xres
, &yres
, &unit_type
);
756 if (ret
&& unit_type
== PNG_RESOLUTION_METER
)
758 *pDpiX
= xres
* 0.0254;
759 *pDpiY
= yres
* 0.0254;
763 WARN("no pHYs block present\n");
764 *pDpiX
= *pDpiY
= 96.0;
767 LeaveCriticalSection(&This
->lock
);
769 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
774 static HRESULT WINAPI
PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
775 IWICPalette
*pIPalette
)
777 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
779 png_colorp png_palette
;
781 WICColor palette
[256];
782 png_bytep trans_alpha
;
784 png_color_16p trans_values
;
788 TRACE("(%p,%p)\n", iface
, pIPalette
);
790 EnterCriticalSection(&This
->lock
);
792 ret
= ppng_get_PLTE(This
->png_ptr
, This
->info_ptr
, &png_palette
, &num_palette
);
795 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
799 if (num_palette
> 256)
801 ERR("palette has %i colors?!\n", num_palette
);
806 ret
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans_alpha
, &num_trans
, &trans_values
);
807 if (!ret
) num_trans
= 0;
809 for (i
=0; i
<num_palette
; i
++)
811 BYTE alpha
= (i
< num_trans
) ? trans_alpha
[i
] : 0xff;
812 palette
[i
] = (alpha
<< 24 |
813 png_palette
[i
].red
<< 16|
814 png_palette
[i
].green
<< 8|
815 png_palette
[i
].blue
);
820 LeaveCriticalSection(&This
->lock
);
823 hr
= IWICPalette_InitializeCustom(pIPalette
, palette
, num_palette
);
828 static HRESULT WINAPI
PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
829 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
831 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
832 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
834 return copy_pixels(This
->bpp
, This
->image_bits
,
835 This
->width
, This
->height
, This
->stride
,
836 prc
, cbStride
, cbBufferSize
, pbBuffer
);
839 static HRESULT WINAPI
PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
840 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
842 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
846 static HRESULT WINAPI
PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
847 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
849 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
853 int compression_type
;
856 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
858 if (!pcActualCount
) return E_INVALIDARG
;
860 EnterCriticalSection(&This
->lock
);
862 if (ppng_get_iCCP(This
->png_ptr
, This
->info_ptr
, &name
, &compression_type
, (void *)&profile
, &len
))
864 if (cCount
&& ppIColorContexts
)
866 hr
= IWICColorContext_InitializeFromMemory(*ppIColorContexts
, profile
, len
);
869 LeaveCriticalSection(&This
->lock
);
878 LeaveCriticalSection(&This
->lock
);
883 static HRESULT WINAPI
PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
884 IWICBitmapSource
**ppIThumbnail
)
886 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
888 if (!ppIThumbnail
) return E_INVALIDARG
;
890 *ppIThumbnail
= NULL
;
891 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
894 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
= {
895 PngDecoder_Frame_QueryInterface
,
896 PngDecoder_Frame_AddRef
,
897 PngDecoder_Frame_Release
,
898 PngDecoder_Frame_GetSize
,
899 PngDecoder_Frame_GetPixelFormat
,
900 PngDecoder_Frame_GetResolution
,
901 PngDecoder_Frame_CopyPalette
,
902 PngDecoder_Frame_CopyPixels
,
903 PngDecoder_Frame_GetMetadataQueryReader
,
904 PngDecoder_Frame_GetColorContexts
,
905 PngDecoder_Frame_GetThumbnail
908 static HRESULT WINAPI
PngDecoder_Block_QueryInterface(IWICMetadataBlockReader
*iface
, REFIID iid
,
911 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
912 return IWICBitmapFrameDecode_QueryInterface(&This
->IWICBitmapFrameDecode_iface
, iid
, ppv
);
915 static ULONG WINAPI
PngDecoder_Block_AddRef(IWICMetadataBlockReader
*iface
)
917 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
918 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
921 static ULONG WINAPI
PngDecoder_Block_Release(IWICMetadataBlockReader
*iface
)
923 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
924 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
927 static HRESULT WINAPI
PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
928 GUID
*pguidContainerFormat
)
930 if (!pguidContainerFormat
) return E_INVALIDARG
;
931 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
935 static HRESULT WINAPI
PngDecoder_Block_GetCount(IWICMetadataBlockReader
*iface
,
938 FIXME("%p,%p: stub\n", iface
, pcCount
);
942 static HRESULT WINAPI
PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
943 UINT nIndex
, IWICMetadataReader
**ppIMetadataReader
)
945 FIXME("%p,%d,%p\n", iface
, nIndex
, ppIMetadataReader
);
949 static HRESULT WINAPI
PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
950 IEnumUnknown
**ppIEnumMetadata
)
952 FIXME("%p,%p\n", iface
, ppIEnumMetadata
);
956 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl
= {
957 PngDecoder_Block_QueryInterface
,
958 PngDecoder_Block_AddRef
,
959 PngDecoder_Block_Release
,
960 PngDecoder_Block_GetContainerFormat
,
961 PngDecoder_Block_GetCount
,
962 PngDecoder_Block_GetReaderByIndex
,
963 PngDecoder_Block_GetEnumerator
,
966 HRESULT
PngDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
971 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
975 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
977 if (!libpng_handle
&& !load_libpng())
979 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG
);
983 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder
));
984 if (!This
) return E_OUTOFMEMORY
;
986 This
->IWICBitmapDecoder_iface
.lpVtbl
= &PngDecoder_Vtbl
;
987 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &PngDecoder_FrameVtbl
;
988 This
->IWICMetadataBlockReader_iface
.lpVtbl
= &PngDecoder_BlockVtbl
;
990 This
->png_ptr
= NULL
;
991 This
->info_ptr
= NULL
;
992 This
->end_info
= NULL
;
993 This
->initialized
= FALSE
;
994 This
->image_bits
= NULL
;
995 InitializeCriticalSection(&This
->lock
);
996 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngDecoder.lock");
998 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
999 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1004 struct png_pixelformat
{
1005 const WICPixelFormatGUID
*guid
;
1013 static const struct png_pixelformat formats
[] = {
1014 {&GUID_WICPixelFormat24bppBGR
, 24, 8, PNG_COLOR_TYPE_RGB
, 0, 1},
1015 {&GUID_WICPixelFormatBlackWhite
, 1, 1, PNG_COLOR_TYPE_GRAY
, 0, 0},
1016 {&GUID_WICPixelFormat2bppGray
, 2, 2, PNG_COLOR_TYPE_GRAY
, 0, 0},
1017 {&GUID_WICPixelFormat4bppGray
, 4, 4, PNG_COLOR_TYPE_GRAY
, 0, 0},
1018 {&GUID_WICPixelFormat8bppGray
, 8, 8, PNG_COLOR_TYPE_GRAY
, 0, 0},
1019 {&GUID_WICPixelFormat16bppGray
, 16, 16, PNG_COLOR_TYPE_GRAY
, 0, 0},
1020 {&GUID_WICPixelFormat32bppBGR
, 32, 8, PNG_COLOR_TYPE_RGB
, 1, 1},
1021 {&GUID_WICPixelFormat32bppBGRA
, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 1},
1022 {&GUID_WICPixelFormat48bppRGB
, 48, 16, PNG_COLOR_TYPE_RGB
, 0, 0},
1023 {&GUID_WICPixelFormat64bppRGBA
, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 0},
1027 typedef struct PngEncoder
{
1028 IWICBitmapEncoder IWICBitmapEncoder_iface
;
1029 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
1032 png_structp png_ptr
;
1035 BOOL frame_initialized
;
1036 const struct png_pixelformat
*format
;
1041 BOOL frame_committed
;
1043 CRITICAL_SECTION lock
;
1046 static inline PngEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
1048 return CONTAINING_RECORD(iface
, PngEncoder
, IWICBitmapEncoder_iface
);
1051 static inline PngEncoder
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
1053 return CONTAINING_RECORD(iface
, PngEncoder
, IWICBitmapFrameEncode_iface
);
1056 static HRESULT WINAPI
PngFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
1059 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1060 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1062 if (!ppv
) return E_INVALIDARG
;
1064 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1065 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
1067 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
1072 return E_NOINTERFACE
;
1075 IUnknown_AddRef((IUnknown
*)*ppv
);
1079 static ULONG WINAPI
PngFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
1081 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1082 return IWICBitmapEncoder_AddRef(&This
->IWICBitmapEncoder_iface
);
1085 static ULONG WINAPI
PngFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
1087 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1088 return IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1091 static HRESULT WINAPI
PngFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
1092 IPropertyBag2
*pIEncoderOptions
)
1094 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1095 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
1097 EnterCriticalSection(&This
->lock
);
1099 if (This
->frame_initialized
)
1101 LeaveCriticalSection(&This
->lock
);
1102 return WINCODEC_ERR_WRONGSTATE
;
1105 This
->frame_initialized
= TRUE
;
1107 LeaveCriticalSection(&This
->lock
);
1112 static HRESULT WINAPI
PngFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
1113 UINT uiWidth
, UINT uiHeight
)
1115 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1116 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
1118 EnterCriticalSection(&This
->lock
);
1120 if (!This
->frame_initialized
|| This
->info_written
)
1122 LeaveCriticalSection(&This
->lock
);
1123 return WINCODEC_ERR_WRONGSTATE
;
1126 This
->width
= uiWidth
;
1127 This
->height
= uiHeight
;
1129 LeaveCriticalSection(&This
->lock
);
1134 static HRESULT WINAPI
PngFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
1135 double dpiX
, double dpiY
)
1137 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1138 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
1140 EnterCriticalSection(&This
->lock
);
1142 if (!This
->frame_initialized
|| This
->info_written
)
1144 LeaveCriticalSection(&This
->lock
);
1145 return WINCODEC_ERR_WRONGSTATE
;
1151 LeaveCriticalSection(&This
->lock
);
1156 static HRESULT WINAPI
PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
1157 WICPixelFormatGUID
*pPixelFormat
)
1159 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1161 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
1163 EnterCriticalSection(&This
->lock
);
1165 if (!This
->frame_initialized
|| This
->info_written
)
1167 LeaveCriticalSection(&This
->lock
);
1168 return WINCODEC_ERR_WRONGSTATE
;
1171 for (i
=0; formats
[i
].guid
; i
++)
1173 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
1177 if (!formats
[i
].guid
) i
= 0;
1179 This
->format
= &formats
[i
];
1180 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
1182 LeaveCriticalSection(&This
->lock
);
1187 static HRESULT WINAPI
PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
1188 UINT cCount
, IWICColorContext
**ppIColorContext
)
1190 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1194 static HRESULT WINAPI
PngFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
1195 IWICPalette
*pIPalette
)
1197 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
1198 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1201 static HRESULT WINAPI
PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
1202 IWICBitmapSource
*pIThumbnail
)
1204 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
1205 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1208 static HRESULT WINAPI
PngFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
1209 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
1211 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1212 png_byte
**row_pointers
=NULL
;
1215 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
1217 EnterCriticalSection(&This
->lock
);
1219 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
1221 LeaveCriticalSection(&This
->lock
);
1222 return WINCODEC_ERR_WRONGSTATE
;
1225 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
1227 LeaveCriticalSection(&This
->lock
);
1228 return E_INVALIDARG
;
1231 /* set up setjmp/longjmp error handling */
1234 LeaveCriticalSection(&This
->lock
);
1235 HeapFree(GetProcessHeap(), 0, row_pointers
);
1238 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1240 if (!This
->info_written
)
1242 ppng_set_IHDR(This
->png_ptr
, This
->info_ptr
, This
->width
, This
->height
,
1243 This
->format
->bit_depth
, This
->format
->color_type
, PNG_INTERLACE_NONE
,
1244 PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
1246 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
1248 ppng_set_pHYs(This
->png_ptr
, This
->info_ptr
, (This
->xres
+0.0127) / 0.0254,
1249 (This
->yres
+0.0127) / 0.0254, PNG_RESOLUTION_METER
);
1252 ppng_write_info(This
->png_ptr
, This
->info_ptr
);
1254 if (This
->format
->remove_filler
)
1255 ppng_set_filler(This
->png_ptr
, 0, PNG_FILLER_AFTER
);
1257 if (This
->format
->swap_rgb
)
1258 ppng_set_bgr(This
->png_ptr
);
1260 This
->info_written
= TRUE
;
1263 row_pointers
= HeapAlloc(GetProcessHeap(), 0, lineCount
* sizeof(png_byte
*));
1266 LeaveCriticalSection(&This
->lock
);
1267 return E_OUTOFMEMORY
;
1270 for (i
=0; i
<lineCount
; i
++)
1271 row_pointers
[i
] = pbPixels
+ cbStride
* i
;
1273 ppng_write_rows(This
->png_ptr
, row_pointers
, lineCount
);
1274 This
->lines_written
+= lineCount
;
1276 LeaveCriticalSection(&This
->lock
);
1278 HeapFree(GetProcessHeap(), 0, row_pointers
);
1283 static HRESULT WINAPI
PngFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
1284 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
1286 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1289 WICPixelFormatGUID guid
;
1292 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
1294 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
)
1295 return WINCODEC_ERR_WRONGSTATE
;
1299 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
1300 if (FAILED(hr
)) return hr
;
1301 hr
= IWICBitmapFrameEncode_SetPixelFormat(iface
, &guid
);
1302 if (FAILED(hr
)) return hr
;
1305 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
1306 if (FAILED(hr
)) return hr
;
1307 if (memcmp(&guid
, This
->format
->guid
, sizeof(GUID
)) != 0)
1309 /* FIXME: should use WICConvertBitmapSource to convert */
1310 ERR("format %s unsupported\n", debugstr_guid(&guid
));
1314 if (This
->xres
== 0.0 || This
->yres
== 0.0)
1317 hr
= IWICBitmapSource_GetResolution(pIBitmapSource
, &xres
, &yres
);
1318 if (FAILED(hr
)) return hr
;
1319 hr
= IWICBitmapFrameEncode_SetResolution(iface
, xres
, yres
);
1320 if (FAILED(hr
)) return hr
;
1326 hr
= IWICBitmapSource_GetSize(pIBitmapSource
, &width
, &height
);
1327 if (FAILED(hr
)) return hr
;
1335 if (prc
->Width
!= This
->width
) return E_INVALIDARG
;
1337 stride
= (This
->format
->bpp
* This
->width
+ 7)/8;
1339 pixeldata
= HeapAlloc(GetProcessHeap(), 0, stride
* prc
->Height
);
1340 if (!pixeldata
) return E_OUTOFMEMORY
;
1342 hr
= IWICBitmapSource_CopyPixels(pIBitmapSource
, prc
, stride
,
1343 stride
*prc
->Height
, pixeldata
);
1347 hr
= IWICBitmapFrameEncode_WritePixels(iface
, prc
->Height
, stride
,
1348 stride
*prc
->Height
, pixeldata
);
1351 HeapFree(GetProcessHeap(), 0, pixeldata
);
1356 static HRESULT WINAPI
PngFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
1358 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1360 TRACE("(%p)\n", iface
);
1362 EnterCriticalSection(&This
->lock
);
1364 if (!This
->info_written
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
1366 LeaveCriticalSection(&This
->lock
);
1367 return WINCODEC_ERR_WRONGSTATE
;
1370 /* set up setjmp/longjmp error handling */
1373 LeaveCriticalSection(&This
->lock
);
1376 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1378 ppng_write_end(This
->png_ptr
, This
->info_ptr
);
1380 This
->frame_committed
= TRUE
;
1382 LeaveCriticalSection(&This
->lock
);
1387 static HRESULT WINAPI
PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1388 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1390 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1394 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl
= {
1395 PngFrameEncode_QueryInterface
,
1396 PngFrameEncode_AddRef
,
1397 PngFrameEncode_Release
,
1398 PngFrameEncode_Initialize
,
1399 PngFrameEncode_SetSize
,
1400 PngFrameEncode_SetResolution
,
1401 PngFrameEncode_SetPixelFormat
,
1402 PngFrameEncode_SetColorContexts
,
1403 PngFrameEncode_SetPalette
,
1404 PngFrameEncode_SetThumbnail
,
1405 PngFrameEncode_WritePixels
,
1406 PngFrameEncode_WriteSource
,
1407 PngFrameEncode_Commit
,
1408 PngFrameEncode_GetMetadataQueryWriter
1411 static HRESULT WINAPI
PngEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1414 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1415 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1417 if (!ppv
) return E_INVALIDARG
;
1419 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1420 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1422 *ppv
= &This
->IWICBitmapEncoder_iface
;
1427 return E_NOINTERFACE
;
1430 IUnknown_AddRef((IUnknown
*)*ppv
);
1434 static ULONG WINAPI
PngEncoder_AddRef(IWICBitmapEncoder
*iface
)
1436 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1437 ULONG ref
= InterlockedIncrement(&This
->ref
);
1439 TRACE("(%p) refcount=%u\n", iface
, ref
);
1444 static ULONG WINAPI
PngEncoder_Release(IWICBitmapEncoder
*iface
)
1446 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1447 ULONG ref
= InterlockedDecrement(&This
->ref
);
1449 TRACE("(%p) refcount=%u\n", iface
, ref
);
1453 This
->lock
.DebugInfo
->Spare
[0] = 0;
1454 DeleteCriticalSection(&This
->lock
);
1456 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1458 IStream_Release(This
->stream
);
1459 HeapFree(GetProcessHeap(), 0, This
);
1465 static void user_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
1467 PngEncoder
*This
= ppng_get_io_ptr(png_ptr
);
1471 hr
= IStream_Write(This
->stream
, data
, length
, &byteswritten
);
1472 if (FAILED(hr
) || byteswritten
!= length
)
1474 ppng_error(png_ptr
, "failed writing data");
1478 static void user_flush(png_structp png_ptr
)
1482 static HRESULT WINAPI
PngEncoder_Initialize(IWICBitmapEncoder
*iface
,
1483 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1485 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1488 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1490 EnterCriticalSection(&This
->lock
);
1494 LeaveCriticalSection(&This
->lock
);
1495 return WINCODEC_ERR_WRONGSTATE
;
1498 /* initialize libpng */
1499 This
->png_ptr
= ppng_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
1502 LeaveCriticalSection(&This
->lock
);
1506 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
1507 if (!This
->info_ptr
)
1509 ppng_destroy_write_struct(&This
->png_ptr
, NULL
);
1510 This
->png_ptr
= NULL
;
1511 LeaveCriticalSection(&This
->lock
);
1515 IStream_AddRef(pIStream
);
1516 This
->stream
= pIStream
;
1518 /* set up setjmp/longjmp error handling */
1521 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1522 This
->png_ptr
= NULL
;
1523 IStream_Release(This
->stream
);
1524 This
->stream
= NULL
;
1525 LeaveCriticalSection(&This
->lock
);
1528 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1530 /* set up custom i/o handling */
1531 ppng_set_write_fn(This
->png_ptr
, This
, user_write_data
, user_flush
);
1533 LeaveCriticalSection(&This
->lock
);
1538 static HRESULT WINAPI
PngEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1539 GUID
*pguidContainerFormat
)
1541 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1545 static HRESULT WINAPI
PngEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1546 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1548 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1552 static HRESULT WINAPI
PngEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1553 UINT cCount
, IWICColorContext
**ppIColorContext
)
1555 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1559 static HRESULT WINAPI
PngEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1561 TRACE("(%p,%p)\n", iface
, pIPalette
);
1562 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1565 static HRESULT WINAPI
PngEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1567 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1568 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1571 static HRESULT WINAPI
PngEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1573 TRACE("(%p,%p)\n", iface
, pIPreview
);
1574 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1577 static HRESULT WINAPI
PngEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1578 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1580 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1582 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1584 EnterCriticalSection(&This
->lock
);
1586 if (This
->frame_count
!= 0)
1588 LeaveCriticalSection(&This
->lock
);
1589 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1594 LeaveCriticalSection(&This
->lock
);
1595 return WINCODEC_ERR_NOTINITIALIZED
;
1598 hr
= CreatePropertyBag2(NULL
, 0, ppIEncoderOptions
);
1601 LeaveCriticalSection(&This
->lock
);
1605 This
->frame_count
= 1;
1607 LeaveCriticalSection(&This
->lock
);
1609 IWICBitmapEncoder_AddRef(iface
);
1610 *ppIFrameEncode
= &This
->IWICBitmapFrameEncode_iface
;
1615 static HRESULT WINAPI
PngEncoder_Commit(IWICBitmapEncoder
*iface
)
1617 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1618 TRACE("(%p)\n", iface
);
1620 EnterCriticalSection(&This
->lock
);
1622 if (!This
->frame_committed
|| This
->committed
)
1624 LeaveCriticalSection(&This
->lock
);
1625 return WINCODEC_ERR_WRONGSTATE
;
1628 This
->committed
= TRUE
;
1630 LeaveCriticalSection(&This
->lock
);
1635 static HRESULT WINAPI
PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
1636 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1638 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
1642 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl
= {
1643 PngEncoder_QueryInterface
,
1646 PngEncoder_Initialize
,
1647 PngEncoder_GetContainerFormat
,
1648 PngEncoder_GetEncoderInfo
,
1649 PngEncoder_SetColorContexts
,
1650 PngEncoder_SetPalette
,
1651 PngEncoder_SetThumbnail
,
1652 PngEncoder_SetPreview
,
1653 PngEncoder_CreateNewFrame
,
1655 PngEncoder_GetMetadataQueryWriter
1658 HRESULT
PngEncoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1663 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
1667 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
1669 if (!libpng_handle
&& !load_libpng())
1671 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG
);
1675 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder
));
1676 if (!This
) return E_OUTOFMEMORY
;
1678 This
->IWICBitmapEncoder_iface
.lpVtbl
= &PngEncoder_Vtbl
;
1679 This
->IWICBitmapFrameEncode_iface
.lpVtbl
= &PngEncoder_FrameVtbl
;
1681 This
->png_ptr
= NULL
;
1682 This
->info_ptr
= NULL
;
1683 This
->stream
= NULL
;
1684 This
->frame_count
= 0;
1685 This
->frame_initialized
= FALSE
;
1686 This
->format
= NULL
;
1687 This
->info_written
= FALSE
;
1692 This
->lines_written
= 0;
1693 This
->frame_committed
= FALSE
;
1694 This
->committed
= FALSE
;
1695 InitializeCriticalSection(&This
->lock
);
1696 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngEncoder.lock");
1698 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
1699 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1704 #else /* !HAVE_PNG_H */
1706 HRESULT
PngDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1708 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1712 HRESULT
PngEncoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1714 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");