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 /* FIXME: Verify the CRC? */
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(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
153 return MetadataReader_Create(&TextReader_Vtbl
, pUnkOuter
, 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_error_fn
);
178 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
179 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8
);
181 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8
);
183 MAKE_FUNCPTR(png_set_filler
);
184 MAKE_FUNCPTR(png_set_gray_to_rgb
);
185 MAKE_FUNCPTR(png_set_IHDR
);
186 MAKE_FUNCPTR(png_set_pHYs
);
187 MAKE_FUNCPTR(png_set_read_fn
);
188 MAKE_FUNCPTR(png_set_strip_16
);
189 MAKE_FUNCPTR(png_set_tRNS_to_alpha
);
190 MAKE_FUNCPTR(png_set_write_fn
);
191 MAKE_FUNCPTR(png_read_end
);
192 MAKE_FUNCPTR(png_read_image
);
193 MAKE_FUNCPTR(png_read_info
);
194 MAKE_FUNCPTR(png_write_end
);
195 MAKE_FUNCPTR(png_write_info
);
196 MAKE_FUNCPTR(png_write_rows
);
199 static void *load_libpng(void)
201 if((libpng_handle
= wine_dlopen(SONAME_LIBPNG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
203 #define LOAD_FUNCPTR(f) \
204 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
205 libpng_handle = NULL; \
208 LOAD_FUNCPTR(png_create_read_struct
);
209 LOAD_FUNCPTR(png_create_info_struct
);
210 LOAD_FUNCPTR(png_create_write_struct
);
211 LOAD_FUNCPTR(png_destroy_read_struct
);
212 LOAD_FUNCPTR(png_destroy_write_struct
);
213 LOAD_FUNCPTR(png_error
);
214 LOAD_FUNCPTR(png_get_bit_depth
);
215 LOAD_FUNCPTR(png_get_color_type
);
216 LOAD_FUNCPTR(png_get_error_ptr
);
217 LOAD_FUNCPTR(png_get_iCCP
);
218 LOAD_FUNCPTR(png_get_image_height
);
219 LOAD_FUNCPTR(png_get_image_width
);
220 LOAD_FUNCPTR(png_get_io_ptr
);
221 LOAD_FUNCPTR(png_get_pHYs
);
222 LOAD_FUNCPTR(png_get_PLTE
);
223 LOAD_FUNCPTR(png_get_tRNS
);
224 LOAD_FUNCPTR(png_set_bgr
);
225 LOAD_FUNCPTR(png_set_error_fn
);
226 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
227 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8
);
229 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8
);
231 LOAD_FUNCPTR(png_set_filler
);
232 LOAD_FUNCPTR(png_set_gray_to_rgb
);
233 LOAD_FUNCPTR(png_set_IHDR
);
234 LOAD_FUNCPTR(png_set_pHYs
);
235 LOAD_FUNCPTR(png_set_read_fn
);
236 LOAD_FUNCPTR(png_set_strip_16
);
237 LOAD_FUNCPTR(png_set_tRNS_to_alpha
);
238 LOAD_FUNCPTR(png_set_write_fn
);
239 LOAD_FUNCPTR(png_read_end
);
240 LOAD_FUNCPTR(png_read_image
);
241 LOAD_FUNCPTR(png_read_info
);
242 LOAD_FUNCPTR(png_write_end
);
243 LOAD_FUNCPTR(png_write_info
);
244 LOAD_FUNCPTR(png_write_rows
);
248 return libpng_handle
;
251 static void user_error_fn(png_structp png_ptr
, png_const_charp error_message
)
255 /* This uses setjmp/longjmp just like the default. We can't use the
256 * default because there's no way to access the jmp buffer in the png_struct
257 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
258 WARN("PNG error: %s\n", debugstr_a(error_message
));
259 pjmpbuf
= ppng_get_error_ptr(png_ptr
);
260 longjmp(*pjmpbuf
, 1);
263 static void user_warning_fn(png_structp png_ptr
, png_const_charp warning_message
)
265 WARN("PNG warning: %s\n", debugstr_a(warning_message
));
269 IWICBitmapDecoder IWICBitmapDecoder_iface
;
270 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
271 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
280 const WICPixelFormatGUID
*format
;
282 CRITICAL_SECTION lock
; /* must be held when png structures are accessed or initialized is set */
285 static inline PngDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
287 return CONTAINING_RECORD(iface
, PngDecoder
, IWICBitmapDecoder_iface
);
290 static inline PngDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
292 return CONTAINING_RECORD(iface
, PngDecoder
, IWICBitmapFrameDecode_iface
);
295 static inline PngDecoder
*impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
297 return CONTAINING_RECORD(iface
, PngDecoder
, IWICMetadataBlockReader_iface
);
300 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
;
302 static HRESULT WINAPI
PngDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
305 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
306 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
308 if (!ppv
) return E_INVALIDARG
;
310 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
312 *ppv
= &This
->IWICBitmapDecoder_iface
;
317 return E_NOINTERFACE
;
320 IUnknown_AddRef((IUnknown
*)*ppv
);
324 static ULONG WINAPI
PngDecoder_AddRef(IWICBitmapDecoder
*iface
)
326 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
327 ULONG ref
= InterlockedIncrement(&This
->ref
);
329 TRACE("(%p) refcount=%u\n", iface
, ref
);
334 static ULONG WINAPI
PngDecoder_Release(IWICBitmapDecoder
*iface
)
336 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
337 ULONG ref
= InterlockedDecrement(&This
->ref
);
339 TRACE("(%p) refcount=%u\n", iface
, ref
);
344 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
345 This
->lock
.DebugInfo
->Spare
[0] = 0;
346 DeleteCriticalSection(&This
->lock
);
347 HeapFree(GetProcessHeap(), 0, This
->image_bits
);
348 HeapFree(GetProcessHeap(), 0, This
);
354 static HRESULT WINAPI
PngDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
359 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
361 if (!stream
|| !capability
) return E_INVALIDARG
;
363 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
364 if (hr
!= S_OK
) return hr
;
366 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
367 WICBitmapDecoderCapabilityCanDecodeSomeImages
;
368 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
372 static void user_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
374 IStream
*stream
= ppng_get_io_ptr(png_ptr
);
378 hr
= IStream_Read(stream
, data
, length
, &bytesread
);
379 if (FAILED(hr
) || bytesread
!= length
)
381 ppng_error(png_ptr
, "failed reading data");
385 static HRESULT WINAPI
PngDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
386 WICDecodeOptions cacheOptions
)
388 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
391 png_bytep
*row_pointers
=NULL
;
394 int color_type
, bit_depth
;
397 png_uint_32 transparency
;
398 png_color_16p trans_values
;
401 TRACE("(%p,%p,%x)\n", iface
, pIStream
, cacheOptions
);
403 EnterCriticalSection(&This
->lock
);
405 /* initialize libpng */
406 This
->png_ptr
= ppng_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
413 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
416 ppng_destroy_read_struct(&This
->png_ptr
, NULL
, NULL
);
417 This
->png_ptr
= NULL
;
422 This
->end_info
= ppng_create_info_struct(This
->png_ptr
);
425 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, NULL
);
426 This
->png_ptr
= NULL
;
431 /* set up setjmp/longjmp error handling */
434 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
435 HeapFree(GetProcessHeap(), 0, row_pointers
);
436 This
->png_ptr
= NULL
;
440 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
442 /* seek to the start of the stream */
444 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
445 if (FAILED(hr
)) goto end
;
447 /* set up custom i/o handling */
448 ppng_set_read_fn(This
->png_ptr
, pIStream
, user_read_data
);
450 /* read the header */
451 ppng_read_info(This
->png_ptr
, This
->info_ptr
);
453 /* choose a pixel format */
454 color_type
= ppng_get_color_type(This
->png_ptr
, This
->info_ptr
);
455 bit_depth
= ppng_get_bit_depth(This
->png_ptr
, This
->info_ptr
);
457 /* check for color-keyed alpha */
458 transparency
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
460 if (transparency
&& color_type
!= PNG_COLOR_TYPE_PALETTE
)
463 if (color_type
== PNG_COLOR_TYPE_GRAY
)
467 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
468 ppng_set_expand_gray_1_2_4_to_8(This
->png_ptr
);
470 ppng_set_gray_1_2_4_to_8(This
->png_ptr
);
474 ppng_set_gray_to_rgb(This
->png_ptr
);
476 ppng_set_tRNS_to_alpha(This
->png_ptr
);
477 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
482 case PNG_COLOR_TYPE_GRAY
:
483 This
->bpp
= bit_depth
;
486 case 1: This
->format
= &GUID_WICPixelFormatBlackWhite
; break;
487 case 2: This
->format
= &GUID_WICPixelFormat2bppGray
; break;
488 case 4: This
->format
= &GUID_WICPixelFormat4bppGray
; break;
489 case 8: This
->format
= &GUID_WICPixelFormat8bppGray
; break;
490 case 16: This
->format
= &GUID_WICPixelFormat16bppGray
; break;
492 ERR("invalid grayscale bit depth: %i\n", bit_depth
);
497 case PNG_COLOR_TYPE_GRAY_ALPHA
:
498 /* WIC does not support grayscale alpha formats so use RGBA */
499 ppng_set_gray_to_rgb(This
->png_ptr
);
501 case PNG_COLOR_TYPE_RGB_ALPHA
:
502 This
->bpp
= bit_depth
* 4;
506 ppng_set_bgr(This
->png_ptr
);
507 This
->format
= &GUID_WICPixelFormat32bppBGRA
;
509 case 16: This
->format
= &GUID_WICPixelFormat64bppRGBA
; break;
511 ERR("invalid RGBA bit depth: %i\n", bit_depth
);
516 case PNG_COLOR_TYPE_PALETTE
:
517 This
->bpp
= bit_depth
;
520 case 1: This
->format
= &GUID_WICPixelFormat1bppIndexed
; break;
521 case 2: This
->format
= &GUID_WICPixelFormat2bppIndexed
; break;
522 case 4: This
->format
= &GUID_WICPixelFormat4bppIndexed
; break;
523 case 8: This
->format
= &GUID_WICPixelFormat8bppIndexed
; break;
525 ERR("invalid indexed color bit depth: %i\n", bit_depth
);
530 case PNG_COLOR_TYPE_RGB
:
531 This
->bpp
= bit_depth
* 3;
535 ppng_set_bgr(This
->png_ptr
);
536 This
->format
= &GUID_WICPixelFormat24bppBGR
;
538 case 16: This
->format
= &GUID_WICPixelFormat48bppRGB
; break;
540 ERR("invalid RGB color bit depth: %i\n", bit_depth
);
546 ERR("invalid color type %i\n", color_type
);
551 /* read the image data */
552 This
->width
= ppng_get_image_width(This
->png_ptr
, This
->info_ptr
);
553 This
->height
= ppng_get_image_height(This
->png_ptr
, This
->info_ptr
);
554 This
->stride
= This
->width
* This
->bpp
;
555 image_size
= This
->stride
* This
->height
;
557 This
->image_bits
= HeapAlloc(GetProcessHeap(), 0, image_size
);
558 if (!This
->image_bits
)
564 row_pointers
= HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep
)*This
->height
);
571 for (i
=0; i
<This
->height
; i
++)
572 row_pointers
[i
] = This
->image_bits
+ i
* This
->stride
;
574 ppng_read_image(This
->png_ptr
, row_pointers
);
576 HeapFree(GetProcessHeap(), 0, row_pointers
);
579 ppng_read_end(This
->png_ptr
, This
->end_info
);
581 This
->initialized
= TRUE
;
585 LeaveCriticalSection(&This
->lock
);
590 static HRESULT WINAPI
PngDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
591 GUID
*pguidContainerFormat
)
593 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
597 static HRESULT WINAPI
PngDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
598 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
601 IWICComponentInfo
*compinfo
;
603 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
605 hr
= CreateComponentInfo(&CLSID_WICPngDecoder
, &compinfo
);
606 if (FAILED(hr
)) return hr
;
608 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
609 (void**)ppIDecoderInfo
);
611 IWICComponentInfo_Release(compinfo
);
616 static HRESULT WINAPI
PngDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
617 IWICPalette
*pIPalette
)
619 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
623 static HRESULT WINAPI
PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
624 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
626 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
630 static HRESULT WINAPI
PngDecoder_GetPreview(IWICBitmapDecoder
*iface
,
631 IWICBitmapSource
**ppIBitmapSource
)
633 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
637 static HRESULT WINAPI
PngDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
638 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
640 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
641 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
644 static HRESULT WINAPI
PngDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
645 IWICBitmapSource
**ppIThumbnail
)
647 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
648 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
651 static HRESULT WINAPI
PngDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
654 if (!pCount
) return E_INVALIDARG
;
660 static HRESULT WINAPI
PngDecoder_GetFrame(IWICBitmapDecoder
*iface
,
661 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
663 PngDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
664 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
666 if (!This
->initialized
) return WINCODEC_ERR_FRAMEMISSING
;
668 if (index
!= 0) return E_INVALIDARG
;
670 IWICBitmapDecoder_AddRef(iface
);
672 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
677 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl
= {
678 PngDecoder_QueryInterface
,
681 PngDecoder_QueryCapability
,
682 PngDecoder_Initialize
,
683 PngDecoder_GetContainerFormat
,
684 PngDecoder_GetDecoderInfo
,
685 PngDecoder_CopyPalette
,
686 PngDecoder_GetMetadataQueryReader
,
687 PngDecoder_GetPreview
,
688 PngDecoder_GetColorContexts
,
689 PngDecoder_GetThumbnail
,
690 PngDecoder_GetFrameCount
,
694 static HRESULT WINAPI
PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
697 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
698 if (!ppv
) return E_INVALIDARG
;
700 if (IsEqualIID(&IID_IUnknown
, iid
) ||
701 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
702 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
704 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
706 else if (IsEqualIID(&IID_IWICMetadataBlockReader
, iid
))
708 *ppv
= &This
->IWICMetadataBlockReader_iface
;
713 return E_NOINTERFACE
;
716 IUnknown_AddRef((IUnknown
*)*ppv
);
720 static ULONG WINAPI
PngDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
722 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
723 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
726 static ULONG WINAPI
PngDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
728 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
729 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
732 static HRESULT WINAPI
PngDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
733 UINT
*puiWidth
, UINT
*puiHeight
)
735 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
736 *puiWidth
= This
->width
;
737 *puiHeight
= This
->height
;
738 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
742 static HRESULT WINAPI
PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
743 WICPixelFormatGUID
*pPixelFormat
)
745 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
746 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
748 memcpy(pPixelFormat
, This
->format
, sizeof(GUID
));
753 static HRESULT WINAPI
PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
754 double *pDpiX
, double *pDpiY
)
756 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
757 png_uint_32 ret
, xres
, yres
;
760 EnterCriticalSection(&This
->lock
);
762 ret
= ppng_get_pHYs(This
->png_ptr
, This
->info_ptr
, &xres
, &yres
, &unit_type
);
764 if (ret
&& unit_type
== PNG_RESOLUTION_METER
)
766 *pDpiX
= xres
* 0.0254;
767 *pDpiY
= yres
* 0.0254;
771 WARN("no pHYs block present\n");
772 *pDpiX
= *pDpiY
= 96.0;
775 LeaveCriticalSection(&This
->lock
);
777 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
782 static HRESULT WINAPI
PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
783 IWICPalette
*pIPalette
)
785 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
787 png_colorp png_palette
;
789 WICColor palette
[256];
792 png_color_16p trans_values
;
796 TRACE("(%p,%p)\n", iface
, pIPalette
);
798 EnterCriticalSection(&This
->lock
);
800 ret
= ppng_get_PLTE(This
->png_ptr
, This
->info_ptr
, &png_palette
, &num_palette
);
803 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
807 if (num_palette
> 256)
809 ERR("palette has %i colors?!\n", num_palette
);
814 for (i
=0; i
<num_palette
; i
++)
816 palette
[i
] = (0xff000000|
817 png_palette
[i
].red
<< 16|
818 png_palette
[i
].green
<< 8|
819 png_palette
[i
].blue
);
822 ret
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
825 for (i
=0; i
<num_trans
; i
++)
827 palette
[trans
[i
]] = 0x00000000;
833 LeaveCriticalSection(&This
->lock
);
836 hr
= IWICPalette_InitializeCustom(pIPalette
, palette
, num_palette
);
841 static HRESULT WINAPI
PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
842 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
844 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
845 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
847 return copy_pixels(This
->bpp
, This
->image_bits
,
848 This
->width
, This
->height
, This
->stride
,
849 prc
, cbStride
, cbBufferSize
, pbBuffer
);
852 static HRESULT WINAPI
PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
853 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
855 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
859 static HRESULT WINAPI
PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
860 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
862 PngDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
863 png_charp name
, profile
;
865 int compression_type
;
868 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
870 if (!pcActualCount
) return E_INVALIDARG
;
872 EnterCriticalSection(&This
->lock
);
874 if (ppng_get_iCCP(This
->png_ptr
, This
->info_ptr
, &name
, &compression_type
, &profile
, &len
))
876 if (cCount
&& ppIColorContexts
)
878 hr
= IWICColorContext_InitializeFromMemory(*ppIColorContexts
, (const BYTE
*)profile
, len
);
881 LeaveCriticalSection(&This
->lock
);
890 LeaveCriticalSection(&This
->lock
);
895 static HRESULT WINAPI
PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
896 IWICBitmapSource
**ppIThumbnail
)
898 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
902 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
= {
903 PngDecoder_Frame_QueryInterface
,
904 PngDecoder_Frame_AddRef
,
905 PngDecoder_Frame_Release
,
906 PngDecoder_Frame_GetSize
,
907 PngDecoder_Frame_GetPixelFormat
,
908 PngDecoder_Frame_GetResolution
,
909 PngDecoder_Frame_CopyPalette
,
910 PngDecoder_Frame_CopyPixels
,
911 PngDecoder_Frame_GetMetadataQueryReader
,
912 PngDecoder_Frame_GetColorContexts
,
913 PngDecoder_Frame_GetThumbnail
916 static HRESULT WINAPI
PngDecoder_Block_QueryInterface(IWICMetadataBlockReader
*iface
, REFIID iid
,
919 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
920 return IWICBitmapFrameDecode_QueryInterface(&This
->IWICBitmapFrameDecode_iface
, iid
, ppv
);
923 static ULONG WINAPI
PngDecoder_Block_AddRef(IWICMetadataBlockReader
*iface
)
925 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
926 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
929 static ULONG WINAPI
PngDecoder_Block_Release(IWICMetadataBlockReader
*iface
)
931 PngDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
932 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
935 static HRESULT WINAPI
PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
936 GUID
*pguidContainerFormat
)
938 if (!pguidContainerFormat
) return E_INVALIDARG
;
939 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
943 static HRESULT WINAPI
PngDecoder_Block_GetCount(IWICMetadataBlockReader
*iface
,
946 FIXME("%p,%p: stub\n", iface
, pcCount
);
950 static HRESULT WINAPI
PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
951 UINT nIndex
, IWICMetadataReader
**ppIMetadataReader
)
953 FIXME("%p,%d,%p\n", iface
, nIndex
, ppIMetadataReader
);
957 static HRESULT WINAPI
PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
958 IEnumUnknown
**ppIEnumMetadata
)
960 FIXME("%p,%p\n", iface
, ppIEnumMetadata
);
964 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl
= {
965 PngDecoder_Block_QueryInterface
,
966 PngDecoder_Block_AddRef
,
967 PngDecoder_Block_Release
,
968 PngDecoder_Block_GetContainerFormat
,
969 PngDecoder_Block_GetCount
,
970 PngDecoder_Block_GetReaderByIndex
,
971 PngDecoder_Block_GetEnumerator
,
974 HRESULT
PngDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
979 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
983 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
985 if (!libpng_handle
&& !load_libpng())
987 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG
);
991 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder
));
992 if (!This
) return E_OUTOFMEMORY
;
994 This
->IWICBitmapDecoder_iface
.lpVtbl
= &PngDecoder_Vtbl
;
995 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &PngDecoder_FrameVtbl
;
996 This
->IWICMetadataBlockReader_iface
.lpVtbl
= &PngDecoder_BlockVtbl
;
998 This
->png_ptr
= NULL
;
999 This
->info_ptr
= NULL
;
1000 This
->end_info
= NULL
;
1001 This
->initialized
= FALSE
;
1002 This
->image_bits
= NULL
;
1003 InitializeCriticalSection(&This
->lock
);
1004 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngDecoder.lock");
1006 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1007 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1012 struct png_pixelformat
{
1013 const WICPixelFormatGUID
*guid
;
1021 static const struct png_pixelformat formats
[] = {
1022 {&GUID_WICPixelFormat24bppBGR
, 24, 8, PNG_COLOR_TYPE_RGB
, 0, 1},
1023 {&GUID_WICPixelFormatBlackWhite
, 1, 1, PNG_COLOR_TYPE_GRAY
, 0, 0},
1024 {&GUID_WICPixelFormat2bppGray
, 2, 2, PNG_COLOR_TYPE_GRAY
, 0, 0},
1025 {&GUID_WICPixelFormat4bppGray
, 4, 4, PNG_COLOR_TYPE_GRAY
, 0, 0},
1026 {&GUID_WICPixelFormat8bppGray
, 8, 8, PNG_COLOR_TYPE_GRAY
, 0, 0},
1027 {&GUID_WICPixelFormat16bppGray
, 16, 16, PNG_COLOR_TYPE_GRAY
, 0, 0},
1028 {&GUID_WICPixelFormat32bppBGR
, 32, 8, PNG_COLOR_TYPE_RGB
, 1, 1},
1029 {&GUID_WICPixelFormat32bppBGRA
, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 1},
1030 {&GUID_WICPixelFormat48bppRGB
, 48, 16, PNG_COLOR_TYPE_RGB
, 0, 0},
1031 {&GUID_WICPixelFormat64bppRGBA
, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 0},
1035 typedef struct PngEncoder
{
1036 IWICBitmapEncoder IWICBitmapEncoder_iface
;
1037 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
1040 png_structp png_ptr
;
1043 BOOL frame_initialized
;
1044 const struct png_pixelformat
*format
;
1049 BOOL frame_committed
;
1051 CRITICAL_SECTION lock
;
1054 static inline PngEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
1056 return CONTAINING_RECORD(iface
, PngEncoder
, IWICBitmapEncoder_iface
);
1059 static inline PngEncoder
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
1061 return CONTAINING_RECORD(iface
, PngEncoder
, IWICBitmapFrameEncode_iface
);
1064 static HRESULT WINAPI
PngFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
1067 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1068 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1070 if (!ppv
) return E_INVALIDARG
;
1072 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1073 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
1075 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
1080 return E_NOINTERFACE
;
1083 IUnknown_AddRef((IUnknown
*)*ppv
);
1087 static ULONG WINAPI
PngFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
1089 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1090 return IWICBitmapEncoder_AddRef(&This
->IWICBitmapEncoder_iface
);
1093 static ULONG WINAPI
PngFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
1095 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1096 return IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1099 static HRESULT WINAPI
PngFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
1100 IPropertyBag2
*pIEncoderOptions
)
1102 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1103 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
1105 EnterCriticalSection(&This
->lock
);
1107 if (This
->frame_initialized
)
1109 LeaveCriticalSection(&This
->lock
);
1110 return WINCODEC_ERR_WRONGSTATE
;
1113 This
->frame_initialized
= TRUE
;
1115 LeaveCriticalSection(&This
->lock
);
1120 static HRESULT WINAPI
PngFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
1121 UINT uiWidth
, UINT uiHeight
)
1123 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1124 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
1126 EnterCriticalSection(&This
->lock
);
1128 if (!This
->frame_initialized
|| This
->info_written
)
1130 LeaveCriticalSection(&This
->lock
);
1131 return WINCODEC_ERR_WRONGSTATE
;
1134 This
->width
= uiWidth
;
1135 This
->height
= uiHeight
;
1137 LeaveCriticalSection(&This
->lock
);
1142 static HRESULT WINAPI
PngFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
1143 double dpiX
, double dpiY
)
1145 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1146 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
1148 EnterCriticalSection(&This
->lock
);
1150 if (!This
->frame_initialized
|| This
->info_written
)
1152 LeaveCriticalSection(&This
->lock
);
1153 return WINCODEC_ERR_WRONGSTATE
;
1159 LeaveCriticalSection(&This
->lock
);
1164 static HRESULT WINAPI
PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
1165 WICPixelFormatGUID
*pPixelFormat
)
1167 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1169 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
1171 EnterCriticalSection(&This
->lock
);
1173 if (!This
->frame_initialized
|| This
->info_written
)
1175 LeaveCriticalSection(&This
->lock
);
1176 return WINCODEC_ERR_WRONGSTATE
;
1179 for (i
=0; formats
[i
].guid
; i
++)
1181 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
1185 if (!formats
[i
].guid
) i
= 0;
1187 This
->format
= &formats
[i
];
1188 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
1190 LeaveCriticalSection(&This
->lock
);
1195 static HRESULT WINAPI
PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
1196 UINT cCount
, IWICColorContext
**ppIColorContext
)
1198 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1202 static HRESULT WINAPI
PngFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
1203 IWICPalette
*pIPalette
)
1205 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
1206 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1209 static HRESULT WINAPI
PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
1210 IWICBitmapSource
*pIThumbnail
)
1212 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
1213 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1216 static HRESULT WINAPI
PngFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
1217 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
1219 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1220 png_byte
**row_pointers
=NULL
;
1223 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
1225 EnterCriticalSection(&This
->lock
);
1227 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
1229 LeaveCriticalSection(&This
->lock
);
1230 return WINCODEC_ERR_WRONGSTATE
;
1233 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
1235 LeaveCriticalSection(&This
->lock
);
1236 return E_INVALIDARG
;
1239 /* set up setjmp/longjmp error handling */
1242 LeaveCriticalSection(&This
->lock
);
1243 HeapFree(GetProcessHeap(), 0, row_pointers
);
1246 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1248 if (!This
->info_written
)
1250 ppng_set_IHDR(This
->png_ptr
, This
->info_ptr
, This
->width
, This
->height
,
1251 This
->format
->bit_depth
, This
->format
->color_type
, PNG_INTERLACE_NONE
,
1252 PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
1254 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
1256 ppng_set_pHYs(This
->png_ptr
, This
->info_ptr
, (This
->xres
+0.0127) / 0.0254,
1257 (This
->yres
+0.0127) / 0.0254, PNG_RESOLUTION_METER
);
1260 ppng_write_info(This
->png_ptr
, This
->info_ptr
);
1262 if (This
->format
->remove_filler
)
1263 ppng_set_filler(This
->png_ptr
, 0, PNG_FILLER_AFTER
);
1265 if (This
->format
->swap_rgb
)
1266 ppng_set_bgr(This
->png_ptr
);
1268 This
->info_written
= TRUE
;
1271 row_pointers
= HeapAlloc(GetProcessHeap(), 0, lineCount
* sizeof(png_byte
*));
1274 LeaveCriticalSection(&This
->lock
);
1275 return E_OUTOFMEMORY
;
1278 for (i
=0; i
<lineCount
; i
++)
1279 row_pointers
[i
] = pbPixels
+ cbStride
* i
;
1281 ppng_write_rows(This
->png_ptr
, row_pointers
, lineCount
);
1282 This
->lines_written
+= lineCount
;
1284 LeaveCriticalSection(&This
->lock
);
1286 HeapFree(GetProcessHeap(), 0, row_pointers
);
1291 static HRESULT WINAPI
PngFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
1292 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
1294 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1297 WICPixelFormatGUID guid
;
1300 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
1302 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
)
1303 return WINCODEC_ERR_WRONGSTATE
;
1307 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
1308 if (FAILED(hr
)) return hr
;
1309 hr
= IWICBitmapFrameEncode_SetPixelFormat(iface
, &guid
);
1310 if (FAILED(hr
)) return hr
;
1313 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
1314 if (FAILED(hr
)) return hr
;
1315 if (memcmp(&guid
, This
->format
->guid
, sizeof(GUID
)) != 0)
1317 /* FIXME: should use WICConvertBitmapSource to convert */
1318 ERR("format %s unsupported\n", debugstr_guid(&guid
));
1322 if (This
->xres
== 0.0 || This
->yres
== 0.0)
1325 hr
= IWICBitmapSource_GetResolution(pIBitmapSource
, &xres
, &yres
);
1326 if (FAILED(hr
)) return hr
;
1327 hr
= IWICBitmapFrameEncode_SetResolution(iface
, xres
, yres
);
1328 if (FAILED(hr
)) return hr
;
1334 hr
= IWICBitmapSource_GetSize(pIBitmapSource
, &width
, &height
);
1335 if (FAILED(hr
)) return hr
;
1343 if (prc
->Width
!= This
->width
) return E_INVALIDARG
;
1345 stride
= (This
->format
->bpp
* This
->width
+ 7)/8;
1347 pixeldata
= HeapAlloc(GetProcessHeap(), 0, stride
* prc
->Height
);
1348 if (!pixeldata
) return E_OUTOFMEMORY
;
1350 hr
= IWICBitmapSource_CopyPixels(pIBitmapSource
, prc
, stride
,
1351 stride
*prc
->Height
, pixeldata
);
1355 hr
= IWICBitmapFrameEncode_WritePixels(iface
, prc
->Height
, stride
,
1356 stride
*prc
->Height
, pixeldata
);
1359 HeapFree(GetProcessHeap(), 0, pixeldata
);
1364 static HRESULT WINAPI
PngFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
1366 PngEncoder
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1368 TRACE("(%p)\n", iface
);
1370 EnterCriticalSection(&This
->lock
);
1372 if (!This
->info_written
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
1374 LeaveCriticalSection(&This
->lock
);
1375 return WINCODEC_ERR_WRONGSTATE
;
1378 /* set up setjmp/longjmp error handling */
1381 LeaveCriticalSection(&This
->lock
);
1384 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1386 ppng_write_end(This
->png_ptr
, This
->info_ptr
);
1388 This
->frame_committed
= TRUE
;
1390 LeaveCriticalSection(&This
->lock
);
1395 static HRESULT WINAPI
PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1396 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1398 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1402 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl
= {
1403 PngFrameEncode_QueryInterface
,
1404 PngFrameEncode_AddRef
,
1405 PngFrameEncode_Release
,
1406 PngFrameEncode_Initialize
,
1407 PngFrameEncode_SetSize
,
1408 PngFrameEncode_SetResolution
,
1409 PngFrameEncode_SetPixelFormat
,
1410 PngFrameEncode_SetColorContexts
,
1411 PngFrameEncode_SetPalette
,
1412 PngFrameEncode_SetThumbnail
,
1413 PngFrameEncode_WritePixels
,
1414 PngFrameEncode_WriteSource
,
1415 PngFrameEncode_Commit
,
1416 PngFrameEncode_GetMetadataQueryWriter
1419 static HRESULT WINAPI
PngEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1422 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1423 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1425 if (!ppv
) return E_INVALIDARG
;
1427 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1428 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1430 *ppv
= &This
->IWICBitmapEncoder_iface
;
1435 return E_NOINTERFACE
;
1438 IUnknown_AddRef((IUnknown
*)*ppv
);
1442 static ULONG WINAPI
PngEncoder_AddRef(IWICBitmapEncoder
*iface
)
1444 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1445 ULONG ref
= InterlockedIncrement(&This
->ref
);
1447 TRACE("(%p) refcount=%u\n", iface
, ref
);
1452 static ULONG WINAPI
PngEncoder_Release(IWICBitmapEncoder
*iface
)
1454 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1455 ULONG ref
= InterlockedDecrement(&This
->ref
);
1457 TRACE("(%p) refcount=%u\n", iface
, ref
);
1461 This
->lock
.DebugInfo
->Spare
[0] = 0;
1462 DeleteCriticalSection(&This
->lock
);
1464 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1466 IStream_Release(This
->stream
);
1467 HeapFree(GetProcessHeap(), 0, This
);
1473 static void user_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
1475 PngEncoder
*This
= ppng_get_io_ptr(png_ptr
);
1479 hr
= IStream_Write(This
->stream
, data
, length
, &byteswritten
);
1480 if (FAILED(hr
) || byteswritten
!= length
)
1482 ppng_error(png_ptr
, "failed writing data");
1486 static void user_flush(png_structp png_ptr
)
1490 static HRESULT WINAPI
PngEncoder_Initialize(IWICBitmapEncoder
*iface
,
1491 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1493 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1496 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1498 EnterCriticalSection(&This
->lock
);
1502 LeaveCriticalSection(&This
->lock
);
1503 return WINCODEC_ERR_WRONGSTATE
;
1506 /* initialize libpng */
1507 This
->png_ptr
= ppng_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
1510 LeaveCriticalSection(&This
->lock
);
1514 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
1515 if (!This
->info_ptr
)
1517 ppng_destroy_write_struct(&This
->png_ptr
, NULL
);
1518 This
->png_ptr
= NULL
;
1519 LeaveCriticalSection(&This
->lock
);
1523 IStream_AddRef(pIStream
);
1524 This
->stream
= pIStream
;
1526 /* set up setjmp/longjmp error handling */
1529 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1530 This
->png_ptr
= NULL
;
1531 IStream_Release(This
->stream
);
1532 This
->stream
= NULL
;
1533 LeaveCriticalSection(&This
->lock
);
1536 ppng_set_error_fn(This
->png_ptr
, jmpbuf
, user_error_fn
, user_warning_fn
);
1538 /* set up custom i/o handling */
1539 ppng_set_write_fn(This
->png_ptr
, This
, user_write_data
, user_flush
);
1541 LeaveCriticalSection(&This
->lock
);
1546 static HRESULT WINAPI
PngEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1547 GUID
*pguidContainerFormat
)
1549 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1553 static HRESULT WINAPI
PngEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1554 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1556 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1560 static HRESULT WINAPI
PngEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1561 UINT cCount
, IWICColorContext
**ppIColorContext
)
1563 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1567 static HRESULT WINAPI
PngEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1569 TRACE("(%p,%p)\n", iface
, pIPalette
);
1570 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1573 static HRESULT WINAPI
PngEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1575 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1576 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1579 static HRESULT WINAPI
PngEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1581 TRACE("(%p,%p)\n", iface
, pIPreview
);
1582 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1585 static HRESULT WINAPI
PngEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1586 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1588 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1590 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1592 EnterCriticalSection(&This
->lock
);
1594 if (This
->frame_count
!= 0)
1596 LeaveCriticalSection(&This
->lock
);
1597 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1602 LeaveCriticalSection(&This
->lock
);
1603 return WINCODEC_ERR_NOTINITIALIZED
;
1606 hr
= CreatePropertyBag2(ppIEncoderOptions
);
1609 LeaveCriticalSection(&This
->lock
);
1613 This
->frame_count
= 1;
1615 LeaveCriticalSection(&This
->lock
);
1617 IWICBitmapEncoder_AddRef(iface
);
1618 *ppIFrameEncode
= &This
->IWICBitmapFrameEncode_iface
;
1623 static HRESULT WINAPI
PngEncoder_Commit(IWICBitmapEncoder
*iface
)
1625 PngEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1626 TRACE("(%p)\n", iface
);
1628 EnterCriticalSection(&This
->lock
);
1630 if (!This
->frame_committed
|| This
->committed
)
1632 LeaveCriticalSection(&This
->lock
);
1633 return WINCODEC_ERR_WRONGSTATE
;
1636 This
->committed
= TRUE
;
1638 LeaveCriticalSection(&This
->lock
);
1643 static HRESULT WINAPI
PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
1644 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1646 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
1650 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl
= {
1651 PngEncoder_QueryInterface
,
1654 PngEncoder_Initialize
,
1655 PngEncoder_GetContainerFormat
,
1656 PngEncoder_GetEncoderInfo
,
1657 PngEncoder_SetColorContexts
,
1658 PngEncoder_SetPalette
,
1659 PngEncoder_SetThumbnail
,
1660 PngEncoder_SetPreview
,
1661 PngEncoder_CreateNewFrame
,
1663 PngEncoder_GetMetadataQueryWriter
1666 HRESULT
PngEncoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1671 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
1675 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
1677 if (!libpng_handle
&& !load_libpng())
1679 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG
);
1683 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder
));
1684 if (!This
) return E_OUTOFMEMORY
;
1686 This
->IWICBitmapEncoder_iface
.lpVtbl
= &PngEncoder_Vtbl
;
1687 This
->IWICBitmapFrameEncode_iface
.lpVtbl
= &PngEncoder_FrameVtbl
;
1689 This
->png_ptr
= NULL
;
1690 This
->info_ptr
= NULL
;
1691 This
->stream
= NULL
;
1692 This
->frame_count
= 0;
1693 This
->frame_initialized
= FALSE
;
1694 This
->format
= NULL
;
1695 This
->info_written
= FALSE
;
1700 This
->lines_written
= 0;
1701 This
->frame_committed
= FALSE
;
1702 This
->committed
= FALSE
;
1703 InitializeCriticalSection(&This
->lock
);
1704 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngEncoder.lock");
1706 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
1707 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
1712 #else /* !HAVE_PNG_H */
1714 HRESULT
PngDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1716 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1720 HRESULT
PngEncoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1722 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");