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"
35 #include "wincodecs_private.h"
37 #include "wine/debug.h"
38 #include "wine/library.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
44 static void *libpng_handle
;
45 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
46 MAKE_FUNCPTR(png_create_read_struct
);
47 MAKE_FUNCPTR(png_create_info_struct
);
48 MAKE_FUNCPTR(png_create_write_struct
);
49 MAKE_FUNCPTR(png_destroy_read_struct
);
50 MAKE_FUNCPTR(png_destroy_write_struct
);
51 MAKE_FUNCPTR(png_error
);
52 MAKE_FUNCPTR(png_get_bit_depth
);
53 MAKE_FUNCPTR(png_get_color_type
);
54 MAKE_FUNCPTR(png_get_error_ptr
);
55 MAKE_FUNCPTR(png_get_image_height
);
56 MAKE_FUNCPTR(png_get_image_width
);
57 MAKE_FUNCPTR(png_get_io_ptr
);
58 MAKE_FUNCPTR(png_get_pHYs
);
59 MAKE_FUNCPTR(png_get_PLTE
);
60 MAKE_FUNCPTR(png_get_tRNS
);
61 MAKE_FUNCPTR(png_set_bgr
);
62 MAKE_FUNCPTR(png_set_error_fn
);
63 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
64 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8
);
66 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8
);
68 MAKE_FUNCPTR(png_set_filler
);
69 MAKE_FUNCPTR(png_set_gray_to_rgb
);
70 MAKE_FUNCPTR(png_set_IHDR
);
71 MAKE_FUNCPTR(png_set_pHYs
);
72 MAKE_FUNCPTR(png_set_read_fn
);
73 MAKE_FUNCPTR(png_set_strip_16
);
74 MAKE_FUNCPTR(png_set_tRNS_to_alpha
);
75 MAKE_FUNCPTR(png_set_write_fn
);
76 MAKE_FUNCPTR(png_read_end
);
77 MAKE_FUNCPTR(png_read_image
);
78 MAKE_FUNCPTR(png_read_info
);
79 MAKE_FUNCPTR(png_write_end
);
80 MAKE_FUNCPTR(png_write_info
);
81 MAKE_FUNCPTR(png_write_rows
);
84 static void *load_libpng(void)
86 if((libpng_handle
= wine_dlopen(SONAME_LIBPNG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
88 #define LOAD_FUNCPTR(f) \
89 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
90 libpng_handle = NULL; \
93 LOAD_FUNCPTR(png_create_read_struct
);
94 LOAD_FUNCPTR(png_create_info_struct
);
95 LOAD_FUNCPTR(png_create_write_struct
);
96 LOAD_FUNCPTR(png_destroy_read_struct
);
97 LOAD_FUNCPTR(png_destroy_write_struct
);
98 LOAD_FUNCPTR(png_error
);
99 LOAD_FUNCPTR(png_get_bit_depth
);
100 LOAD_FUNCPTR(png_get_color_type
);
101 LOAD_FUNCPTR(png_get_error_ptr
);
102 LOAD_FUNCPTR(png_get_image_height
);
103 LOAD_FUNCPTR(png_get_image_width
);
104 LOAD_FUNCPTR(png_get_io_ptr
);
105 LOAD_FUNCPTR(png_get_pHYs
);
106 LOAD_FUNCPTR(png_get_PLTE
);
107 LOAD_FUNCPTR(png_get_tRNS
);
108 LOAD_FUNCPTR(png_set_bgr
);
109 LOAD_FUNCPTR(png_set_error_fn
);
110 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
111 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8
);
113 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8
);
115 LOAD_FUNCPTR(png_set_filler
);
116 LOAD_FUNCPTR(png_set_gray_to_rgb
);
117 LOAD_FUNCPTR(png_set_IHDR
);
118 LOAD_FUNCPTR(png_set_pHYs
);
119 LOAD_FUNCPTR(png_set_read_fn
);
120 LOAD_FUNCPTR(png_set_strip_16
);
121 LOAD_FUNCPTR(png_set_tRNS_to_alpha
);
122 LOAD_FUNCPTR(png_set_write_fn
);
123 LOAD_FUNCPTR(png_read_end
);
124 LOAD_FUNCPTR(png_read_image
);
125 LOAD_FUNCPTR(png_read_info
);
126 LOAD_FUNCPTR(png_write_end
);
127 LOAD_FUNCPTR(png_write_info
);
128 LOAD_FUNCPTR(png_write_rows
);
132 return libpng_handle
;
135 static void user_error_fn(png_structp png_ptr
, png_const_charp error_message
)
139 /* This uses setjmp/longjmp just like the default. We can't use the
140 * default because there's no way to access the jmp buffer in the png_struct
141 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
142 WARN("PNG error: %s\n", debugstr_a(error_message
));
143 pjmpbuf
= ppng_get_error_ptr(png_ptr
);
144 longjmp(*pjmpbuf
, 1);
147 static void user_warning_fn(png_structp png_ptr
, png_const_charp warning_message
)
149 WARN("PNG warning: %s\n", debugstr_a(warning_message
));
153 const IWICBitmapDecoderVtbl
*lpVtbl
;
154 const IWICBitmapFrameDecodeVtbl
*lpFrameVtbl
;
163 const WICPixelFormatGUID
*format
;
165 CRITICAL_SECTION lock
; /* must be held when png structures are accessed or initialized is set */
168 static inline PngDecoder
*impl_from_frame(IWICBitmapFrameDecode
*iface
)
170 return CONTAINING_RECORD(iface
, PngDecoder
, lpFrameVtbl
);
173 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
;
175 static HRESULT WINAPI
PngDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
178 PngDecoder
*This
= (PngDecoder
*)iface
;
179 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
181 if (!ppv
) return E_INVALIDARG
;
183 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
190 return E_NOINTERFACE
;
193 IUnknown_AddRef((IUnknown
*)*ppv
);
197 static ULONG WINAPI
PngDecoder_AddRef(IWICBitmapDecoder
*iface
)
199 PngDecoder
*This
= (PngDecoder
*)iface
;
200 ULONG ref
= InterlockedIncrement(&This
->ref
);
202 TRACE("(%p) refcount=%u\n", iface
, ref
);
207 static ULONG WINAPI
PngDecoder_Release(IWICBitmapDecoder
*iface
)
209 PngDecoder
*This
= (PngDecoder
*)iface
;
210 ULONG ref
= InterlockedDecrement(&This
->ref
);
212 TRACE("(%p) refcount=%u\n", iface
, ref
);
217 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
218 This
->lock
.DebugInfo
->Spare
[0] = 0;
219 DeleteCriticalSection(&This
->lock
);
220 HeapFree(GetProcessHeap(), 0, This
->image_bits
);
221 HeapFree(GetProcessHeap(), 0, This
);
227 static HRESULT WINAPI
PngDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
228 DWORD
*pdwCapability
)
230 FIXME("(%p,%p,%p): stub\n", iface
, pIStream
, pdwCapability
);
234 static void user_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
236 IStream
*stream
= ppng_get_io_ptr(png_ptr
);
240 hr
= IStream_Read(stream
, data
, length
, &bytesread
);
241 if (FAILED(hr
) || bytesread
!= length
)
243 ppng_error(png_ptr
, "failed reading data");
247 static HRESULT WINAPI
PngDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
248 WICDecodeOptions cacheOptions
)
250 PngDecoder
*This
= (PngDecoder
*)iface
;
253 png_bytep
*row_pointers
=NULL
;
256 int color_type
, bit_depth
;
259 png_uint_32 transparency
;
260 png_color_16p trans_values
;
263 TRACE("(%p,%p,%x)\n", iface
, pIStream
, cacheOptions
);
265 EnterCriticalSection(&This
->lock
);
267 /* initialize libpng */
268 This
->png_ptr
= ppng_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
275 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
278 ppng_destroy_read_struct(&This
->png_ptr
, NULL
, NULL
);
279 This
->png_ptr
= NULL
;
284 This
->end_info
= ppng_create_info_struct(This
->png_ptr
);
287 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, NULL
);
288 This
->png_ptr
= NULL
;
293 /* set up setjmp/longjmp error handling */
296 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
297 HeapFree(GetProcessHeap(), 0, row_pointers
);
298 This
->png_ptr
= NULL
;
302 ppng_set_error_fn(This
->png_ptr
, &jmpbuf
, user_error_fn
, user_warning_fn
);
304 /* seek to the start of the stream */
306 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
307 if (FAILED(hr
)) goto end
;
309 /* set up custom i/o handling */
310 ppng_set_read_fn(This
->png_ptr
, pIStream
, user_read_data
);
312 /* read the header */
313 ppng_read_info(This
->png_ptr
, This
->info_ptr
);
315 /* choose a pixel format */
316 color_type
= ppng_get_color_type(This
->png_ptr
, This
->info_ptr
);
317 bit_depth
= ppng_get_bit_depth(This
->png_ptr
, This
->info_ptr
);
319 /* check for color-keyed alpha */
320 transparency
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
322 if (transparency
&& color_type
!= PNG_COLOR_TYPE_PALETTE
)
325 if (color_type
== PNG_COLOR_TYPE_GRAY
)
329 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
330 ppng_set_expand_gray_1_2_4_to_8(This
->png_ptr
);
332 ppng_set_gray_1_2_4_to_8(This
->png_ptr
);
336 ppng_set_gray_to_rgb(This
->png_ptr
);
338 ppng_set_tRNS_to_alpha(This
->png_ptr
);
339 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
344 case PNG_COLOR_TYPE_GRAY
:
345 This
->bpp
= bit_depth
;
348 case 1: This
->format
= &GUID_WICPixelFormatBlackWhite
; break;
349 case 2: This
->format
= &GUID_WICPixelFormat2bppGray
; break;
350 case 4: This
->format
= &GUID_WICPixelFormat4bppGray
; break;
351 case 8: This
->format
= &GUID_WICPixelFormat8bppGray
; break;
352 case 16: This
->format
= &GUID_WICPixelFormat16bppGray
; break;
354 ERR("invalid grayscale bit depth: %i\n", bit_depth
);
359 case PNG_COLOR_TYPE_GRAY_ALPHA
:
360 /* WIC does not support grayscale alpha formats so use RGBA */
361 ppng_set_gray_to_rgb(This
->png_ptr
);
362 case PNG_COLOR_TYPE_RGB_ALPHA
:
363 This
->bpp
= bit_depth
* 4;
367 ppng_set_bgr(This
->png_ptr
);
368 This
->format
= &GUID_WICPixelFormat32bppBGRA
;
370 case 16: This
->format
= &GUID_WICPixelFormat64bppRGBA
; break;
372 ERR("invalid RGBA bit depth: %i\n", bit_depth
);
377 case PNG_COLOR_TYPE_PALETTE
:
378 This
->bpp
= bit_depth
;
381 case 1: This
->format
= &GUID_WICPixelFormat1bppIndexed
; break;
382 case 2: This
->format
= &GUID_WICPixelFormat2bppIndexed
; break;
383 case 4: This
->format
= &GUID_WICPixelFormat4bppIndexed
; break;
384 case 8: This
->format
= &GUID_WICPixelFormat8bppIndexed
; break;
386 ERR("invalid indexed color bit depth: %i\n", bit_depth
);
391 case PNG_COLOR_TYPE_RGB
:
392 This
->bpp
= bit_depth
* 3;
396 ppng_set_bgr(This
->png_ptr
);
397 This
->format
= &GUID_WICPixelFormat24bppBGR
;
399 case 16: This
->format
= &GUID_WICPixelFormat48bppRGB
; break;
401 ERR("invalid RGB color bit depth: %i\n", bit_depth
);
407 ERR("invalid color type %i\n", color_type
);
412 /* read the image data */
413 This
->width
= ppng_get_image_width(This
->png_ptr
, This
->info_ptr
);
414 This
->height
= ppng_get_image_height(This
->png_ptr
, This
->info_ptr
);
415 This
->stride
= This
->width
* This
->bpp
;
416 image_size
= This
->stride
* This
->height
;
418 This
->image_bits
= HeapAlloc(GetProcessHeap(), 0, image_size
);
419 if (!This
->image_bits
)
425 row_pointers
= HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep
)*This
->height
);
432 for (i
=0; i
<This
->height
; i
++)
433 row_pointers
[i
] = This
->image_bits
+ i
* This
->stride
;
435 ppng_read_image(This
->png_ptr
, row_pointers
);
437 HeapFree(GetProcessHeap(), 0, row_pointers
);
440 ppng_read_end(This
->png_ptr
, This
->end_info
);
442 This
->initialized
= TRUE
;
446 LeaveCriticalSection(&This
->lock
);
451 static HRESULT WINAPI
PngDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
452 GUID
*pguidContainerFormat
)
454 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
458 static HRESULT WINAPI
PngDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
459 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
461 FIXME("(%p,%p): stub\n", iface
, ppIDecoderInfo
);
465 static HRESULT WINAPI
PngDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
466 IWICPalette
*pIPalette
)
468 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
472 static HRESULT WINAPI
PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
473 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
475 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
479 static HRESULT WINAPI
PngDecoder_GetPreview(IWICBitmapDecoder
*iface
,
480 IWICBitmapSource
**ppIBitmapSource
)
482 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
486 static HRESULT WINAPI
PngDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
487 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
489 FIXME("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
493 static HRESULT WINAPI
PngDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
494 IWICBitmapSource
**ppIThumbnail
)
496 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
497 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
500 static HRESULT WINAPI
PngDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
507 static HRESULT WINAPI
PngDecoder_GetFrame(IWICBitmapDecoder
*iface
,
508 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
510 PngDecoder
*This
= (PngDecoder
*)iface
;
511 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
513 if (!This
->initialized
) return WINCODEC_ERR_NOTINITIALIZED
;
515 if (index
!= 0) return E_INVALIDARG
;
517 IWICBitmapDecoder_AddRef(iface
);
519 *ppIBitmapFrame
= (void*)(&This
->lpFrameVtbl
);
524 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl
= {
525 PngDecoder_QueryInterface
,
528 PngDecoder_QueryCapability
,
529 PngDecoder_Initialize
,
530 PngDecoder_GetContainerFormat
,
531 PngDecoder_GetDecoderInfo
,
532 PngDecoder_CopyPalette
,
533 PngDecoder_GetMetadataQueryReader
,
534 PngDecoder_GetPreview
,
535 PngDecoder_GetColorContexts
,
536 PngDecoder_GetThumbnail
,
537 PngDecoder_GetFrameCount
,
541 static HRESULT WINAPI
PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
544 if (!ppv
) return E_INVALIDARG
;
546 if (IsEqualIID(&IID_IUnknown
, iid
) ||
547 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
548 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
555 return E_NOINTERFACE
;
558 IUnknown_AddRef((IUnknown
*)*ppv
);
562 static ULONG WINAPI
PngDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
564 PngDecoder
*This
= impl_from_frame(iface
);
565 return IUnknown_AddRef((IUnknown
*)This
);
568 static ULONG WINAPI
PngDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
570 PngDecoder
*This
= impl_from_frame(iface
);
571 return IUnknown_Release((IUnknown
*)This
);
574 static HRESULT WINAPI
PngDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
575 UINT
*puiWidth
, UINT
*puiHeight
)
577 PngDecoder
*This
= impl_from_frame(iface
);
578 *puiWidth
= This
->width
;
579 *puiHeight
= This
->height
;
580 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
584 static HRESULT WINAPI
PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
585 WICPixelFormatGUID
*pPixelFormat
)
587 PngDecoder
*This
= impl_from_frame(iface
);
588 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
590 memcpy(pPixelFormat
, This
->format
, sizeof(GUID
));
595 static HRESULT WINAPI
PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
596 double *pDpiX
, double *pDpiY
)
598 PngDecoder
*This
= impl_from_frame(iface
);
599 png_uint_32 ret
, xres
, yres
;
602 EnterCriticalSection(&This
->lock
);
604 ret
= ppng_get_pHYs(This
->png_ptr
, This
->info_ptr
, &xres
, &yres
, &unit_type
);
606 if (ret
&& unit_type
== PNG_RESOLUTION_METER
)
608 *pDpiX
= xres
* 0.0254;
609 *pDpiY
= yres
* 0.0254;
613 WARN("no pHYs block present\n");
614 *pDpiX
= *pDpiY
= 96.0;
617 LeaveCriticalSection(&This
->lock
);
619 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
624 static HRESULT WINAPI
PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
625 IWICPalette
*pIPalette
)
627 PngDecoder
*This
= impl_from_frame(iface
);
629 png_colorp png_palette
;
631 WICColor palette
[256];
634 png_color_16p trans_values
;
638 TRACE("(%p,%p)\n", iface
, pIPalette
);
640 EnterCriticalSection(&This
->lock
);
642 ret
= ppng_get_PLTE(This
->png_ptr
, This
->info_ptr
, &png_palette
, &num_palette
);
645 hr
= WINCODEC_ERR_PALETTEUNAVAILABLE
;
649 if (num_palette
> 256)
651 ERR("palette has %i colors?!\n", num_palette
);
656 for (i
=0; i
<num_palette
; i
++)
658 palette
[i
] = (0xff000000|
659 png_palette
[i
].red
<< 16|
660 png_palette
[i
].green
<< 8|
661 png_palette
[i
].blue
);
664 ret
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
667 for (i
=0; i
<num_trans
; i
++)
669 palette
[trans
[i
]] = 0x00000000;
675 LeaveCriticalSection(&This
->lock
);
678 hr
= IWICPalette_InitializeCustom(pIPalette
, palette
, num_palette
);
683 static HRESULT WINAPI
PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
684 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
686 PngDecoder
*This
= impl_from_frame(iface
);
687 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
689 return copy_pixels(This
->bpp
, This
->image_bits
,
690 This
->width
, This
->height
, This
->stride
,
691 prc
, cbStride
, cbBufferSize
, pbBuffer
);
694 static HRESULT WINAPI
PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
695 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
697 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
701 static HRESULT WINAPI
PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
702 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
704 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
708 static HRESULT WINAPI
PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
709 IWICBitmapSource
**ppIThumbnail
)
711 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
715 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
= {
716 PngDecoder_Frame_QueryInterface
,
717 PngDecoder_Frame_AddRef
,
718 PngDecoder_Frame_Release
,
719 PngDecoder_Frame_GetSize
,
720 PngDecoder_Frame_GetPixelFormat
,
721 PngDecoder_Frame_GetResolution
,
722 PngDecoder_Frame_CopyPalette
,
723 PngDecoder_Frame_CopyPixels
,
724 PngDecoder_Frame_GetMetadataQueryReader
,
725 PngDecoder_Frame_GetColorContexts
,
726 PngDecoder_Frame_GetThumbnail
729 HRESULT
PngDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
734 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
738 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
740 if (!libpng_handle
&& !load_libpng())
742 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG
);
746 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder
));
747 if (!This
) return E_OUTOFMEMORY
;
749 This
->lpVtbl
= &PngDecoder_Vtbl
;
750 This
->lpFrameVtbl
= &PngDecoder_FrameVtbl
;
752 This
->png_ptr
= NULL
;
753 This
->info_ptr
= NULL
;
754 This
->end_info
= NULL
;
755 This
->initialized
= FALSE
;
756 This
->image_bits
= NULL
;
757 InitializeCriticalSection(&This
->lock
);
758 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PngDecoder.lock");
760 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
761 IUnknown_Release((IUnknown
*)This
);
766 struct png_pixelformat
{
767 const WICPixelFormatGUID
*guid
;
775 static const struct png_pixelformat formats
[] = {
776 {&GUID_WICPixelFormat24bppBGR
, 24, 8, PNG_COLOR_TYPE_RGB
, 0, 1},
777 {&GUID_WICPixelFormatBlackWhite
, 1, 1, PNG_COLOR_TYPE_GRAY
, 0, 0},
778 {&GUID_WICPixelFormat2bppGray
, 2, 2, PNG_COLOR_TYPE_GRAY
, 0, 0},
779 {&GUID_WICPixelFormat4bppGray
, 4, 4, PNG_COLOR_TYPE_GRAY
, 0, 0},
780 {&GUID_WICPixelFormat8bppGray
, 8, 8, PNG_COLOR_TYPE_GRAY
, 0, 0},
781 {&GUID_WICPixelFormat16bppGray
, 16, 16, PNG_COLOR_TYPE_GRAY
, 0, 0},
782 {&GUID_WICPixelFormat32bppBGR
, 32, 8, PNG_COLOR_TYPE_RGB
, 1, 1},
783 {&GUID_WICPixelFormat32bppBGRA
, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 1},
784 {&GUID_WICPixelFormat48bppRGB
, 48, 16, PNG_COLOR_TYPE_RGB
, 0, 0},
785 {&GUID_WICPixelFormat64bppRGBA
, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 0},
789 typedef struct PngEncoder
{
790 const IWICBitmapEncoderVtbl
*lpVtbl
;
791 const IWICBitmapFrameEncodeVtbl
*lpFrameVtbl
;
797 BOOL frame_initialized
;
798 const struct png_pixelformat
*format
;
803 BOOL frame_committed
;
807 static inline PngEncoder
*encoder_from_frame(IWICBitmapFrameEncode
*iface
)
809 return CONTAINING_RECORD(iface
, PngEncoder
, lpFrameVtbl
);
812 static HRESULT WINAPI
PngFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
815 PngEncoder
*This
= encoder_from_frame(iface
);
816 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
818 if (!ppv
) return E_INVALIDARG
;
820 if (IsEqualIID(&IID_IUnknown
, iid
) ||
821 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
823 *ppv
= &This
->lpFrameVtbl
;
828 return E_NOINTERFACE
;
831 IUnknown_AddRef((IUnknown
*)*ppv
);
835 static ULONG WINAPI
PngFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
837 PngEncoder
*This
= encoder_from_frame(iface
);
838 return IUnknown_AddRef((IUnknown
*)This
);
841 static ULONG WINAPI
PngFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
843 PngEncoder
*This
= encoder_from_frame(iface
);
844 return IUnknown_Release((IUnknown
*)This
);
847 static HRESULT WINAPI
PngFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
848 IPropertyBag2
*pIEncoderOptions
)
850 PngEncoder
*This
= encoder_from_frame(iface
);
851 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
853 if (This
->frame_initialized
) return WINCODEC_ERR_WRONGSTATE
;
855 This
->frame_initialized
= TRUE
;
860 static HRESULT WINAPI
PngFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
861 UINT uiWidth
, UINT uiHeight
)
863 PngEncoder
*This
= encoder_from_frame(iface
);
864 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
866 if (!This
->frame_initialized
|| This
->info_written
) return WINCODEC_ERR_WRONGSTATE
;
868 This
->width
= uiWidth
;
869 This
->height
= uiHeight
;
874 static HRESULT WINAPI
PngFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
875 double dpiX
, double dpiY
)
877 PngEncoder
*This
= encoder_from_frame(iface
);
878 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
880 if (!This
->frame_initialized
|| This
->info_written
) return WINCODEC_ERR_WRONGSTATE
;
888 static HRESULT WINAPI
PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
889 WICPixelFormatGUID
*pPixelFormat
)
891 PngEncoder
*This
= encoder_from_frame(iface
);
893 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
895 if (!This
->frame_initialized
|| This
->info_written
) return WINCODEC_ERR_WRONGSTATE
;
897 for (i
=0; formats
[i
].guid
; i
++)
899 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
903 if (!formats
[i
].guid
) i
= 0;
905 This
->format
= &formats
[i
];
906 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
911 static HRESULT WINAPI
PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
912 UINT cCount
, IWICColorContext
**ppIColorContext
)
914 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
918 static HRESULT WINAPI
PngFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
919 IWICPalette
*pIPalette
)
921 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
922 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
925 static HRESULT WINAPI
PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
926 IWICBitmapSource
*pIThumbnail
)
928 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
929 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
932 static HRESULT WINAPI
PngFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
933 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
935 PngEncoder
*This
= encoder_from_frame(iface
);
936 png_byte
**row_pointers
=NULL
;
939 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
941 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
942 return WINCODEC_ERR_WRONGSTATE
;
944 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
947 /* set up setjmp/longjmp error handling */
950 HeapFree(GetProcessHeap(), 0, row_pointers
);
953 ppng_set_error_fn(This
->png_ptr
, &jmpbuf
, user_error_fn
, user_warning_fn
);
955 if (!This
->info_written
)
957 ppng_set_IHDR(This
->png_ptr
, This
->info_ptr
, This
->width
, This
->height
,
958 This
->format
->bit_depth
, This
->format
->color_type
, PNG_INTERLACE_NONE
,
959 PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
961 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
963 ppng_set_pHYs(This
->png_ptr
, This
->info_ptr
, (This
->xres
+0.0127) / 0.0254,
964 (This
->yres
+0.0127) / 0.0254, PNG_RESOLUTION_METER
);
967 ppng_write_info(This
->png_ptr
, This
->info_ptr
);
969 if (This
->format
->remove_filler
)
970 ppng_set_filler(This
->png_ptr
, 0, PNG_FILLER_AFTER
);
972 if (This
->format
->swap_rgb
)
973 ppng_set_bgr(This
->png_ptr
);
975 This
->info_written
= TRUE
;
978 row_pointers
= HeapAlloc(GetProcessHeap(), 0, lineCount
* sizeof(png_byte
*));
980 return E_OUTOFMEMORY
;
982 for (i
=0; i
<lineCount
; i
++)
983 row_pointers
[i
] = pbPixels
+ cbStride
* i
;
985 ppng_write_rows(This
->png_ptr
, row_pointers
, lineCount
);
986 This
->lines_written
+= lineCount
;
988 HeapFree(GetProcessHeap(), 0, row_pointers
);
993 static HRESULT WINAPI
PngFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
994 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
996 PngEncoder
*This
= encoder_from_frame(iface
);
999 WICPixelFormatGUID guid
;
1002 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
1004 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
)
1005 return WINCODEC_ERR_WRONGSTATE
;
1009 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
1010 if (FAILED(hr
)) return hr
;
1011 hr
= IWICBitmapFrameEncode_SetPixelFormat(iface
, &guid
);
1012 if (FAILED(hr
)) return hr
;
1015 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
1016 if (FAILED(hr
)) return hr
;
1017 if (memcmp(&guid
, This
->format
->guid
, sizeof(GUID
)) != 0)
1019 /* FIXME: should use WICConvertBitmapSource to convert */
1020 ERR("format %s unsupported\n", debugstr_guid(&guid
));
1024 if (This
->xres
== 0.0 || This
->yres
== 0.0)
1027 hr
= IWICBitmapSource_GetResolution(pIBitmapSource
, &xres
, &yres
);
1028 if (FAILED(hr
)) return hr
;
1029 hr
= IWICBitmapFrameEncode_SetResolution(iface
, xres
, yres
);
1030 if (FAILED(hr
)) return hr
;
1036 hr
= IWICBitmapSource_GetSize(pIBitmapSource
, &width
, &height
);
1037 if (FAILED(hr
)) return hr
;
1045 if (prc
->Width
!= This
->width
) return E_INVALIDARG
;
1047 stride
= (This
->format
->bpp
* This
->width
+ 7)/8;
1049 pixeldata
= HeapAlloc(GetProcessHeap(), 0, stride
* prc
->Height
);
1050 if (!pixeldata
) return E_OUTOFMEMORY
;
1052 hr
= IWICBitmapSource_CopyPixels(pIBitmapSource
, prc
, stride
,
1053 stride
*prc
->Height
, pixeldata
);
1057 hr
= IWICBitmapFrameEncode_WritePixels(iface
, prc
->Height
, stride
,
1058 stride
*prc
->Height
, pixeldata
);
1061 HeapFree(GetProcessHeap(), 0, pixeldata
);
1066 static HRESULT WINAPI
PngFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
1068 PngEncoder
*This
= encoder_from_frame(iface
);
1070 TRACE("(%p)\n", iface
);
1072 if (!This
->info_written
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
1073 return WINCODEC_ERR_WRONGSTATE
;
1075 /* set up setjmp/longjmp error handling */
1080 ppng_set_error_fn(This
->png_ptr
, &jmpbuf
, user_error_fn
, user_warning_fn
);
1082 ppng_write_end(This
->png_ptr
, This
->info_ptr
);
1084 This
->frame_committed
= TRUE
;
1089 static HRESULT WINAPI
PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1090 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1092 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1096 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl
= {
1097 PngFrameEncode_QueryInterface
,
1098 PngFrameEncode_AddRef
,
1099 PngFrameEncode_Release
,
1100 PngFrameEncode_Initialize
,
1101 PngFrameEncode_SetSize
,
1102 PngFrameEncode_SetResolution
,
1103 PngFrameEncode_SetPixelFormat
,
1104 PngFrameEncode_SetColorContexts
,
1105 PngFrameEncode_SetPalette
,
1106 PngFrameEncode_SetThumbnail
,
1107 PngFrameEncode_WritePixels
,
1108 PngFrameEncode_WriteSource
,
1109 PngFrameEncode_Commit
,
1110 PngFrameEncode_GetMetadataQueryWriter
1113 static HRESULT WINAPI
PngEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1116 PngEncoder
*This
= (PngEncoder
*)iface
;
1117 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1119 if (!ppv
) return E_INVALIDARG
;
1121 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1122 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1129 return E_NOINTERFACE
;
1132 IUnknown_AddRef((IUnknown
*)*ppv
);
1136 static ULONG WINAPI
PngEncoder_AddRef(IWICBitmapEncoder
*iface
)
1138 PngEncoder
*This
= (PngEncoder
*)iface
;
1139 ULONG ref
= InterlockedIncrement(&This
->ref
);
1141 TRACE("(%p) refcount=%u\n", iface
, ref
);
1146 static ULONG WINAPI
PngEncoder_Release(IWICBitmapEncoder
*iface
)
1148 PngEncoder
*This
= (PngEncoder
*)iface
;
1149 ULONG ref
= InterlockedDecrement(&This
->ref
);
1151 TRACE("(%p) refcount=%u\n", iface
, ref
);
1156 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1158 IStream_Release(This
->stream
);
1159 HeapFree(GetProcessHeap(), 0, This
);
1165 static void user_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
1167 PngEncoder
*This
= ppng_get_io_ptr(png_ptr
);
1171 hr
= IStream_Write(This
->stream
, data
, length
, &byteswritten
);
1172 if (FAILED(hr
) || byteswritten
!= length
)
1174 ppng_error(png_ptr
, "failed writing data");
1178 static void user_flush(png_structp png_ptr
)
1182 static HRESULT WINAPI
PngEncoder_Initialize(IWICBitmapEncoder
*iface
,
1183 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1185 PngEncoder
*This
= (PngEncoder
*)iface
;
1188 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1191 return WINCODEC_ERR_WRONGSTATE
;
1193 /* initialize libpng */
1194 This
->png_ptr
= ppng_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
1198 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
1199 if (!This
->info_ptr
)
1201 ppng_destroy_write_struct(&This
->png_ptr
, NULL
);
1202 This
->png_ptr
= NULL
;
1206 IStream_AddRef(pIStream
);
1207 This
->stream
= pIStream
;
1209 /* set up setjmp/longjmp error handling */
1212 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1213 This
->png_ptr
= NULL
;
1214 IStream_Release(This
->stream
);
1215 This
->stream
= NULL
;
1218 ppng_set_error_fn(This
->png_ptr
, &jmpbuf
, user_error_fn
, user_warning_fn
);
1220 /* set up custom i/o handling */
1221 ppng_set_write_fn(This
->png_ptr
, This
, user_write_data
, user_flush
);
1226 static HRESULT WINAPI
PngEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1227 GUID
*pguidContainerFormat
)
1229 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1233 static HRESULT WINAPI
PngEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1234 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1236 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1240 static HRESULT WINAPI
PngEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1241 UINT cCount
, IWICColorContext
**ppIColorContext
)
1243 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1247 static HRESULT WINAPI
PngEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1249 TRACE("(%p,%p)\n", iface
, pIPalette
);
1250 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1253 static HRESULT WINAPI
PngEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1255 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1256 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1259 static HRESULT WINAPI
PngEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1261 TRACE("(%p,%p)\n", iface
, pIPreview
);
1262 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1265 static HRESULT WINAPI
PngEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1266 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1268 PngEncoder
*This
= (PngEncoder
*)iface
;
1270 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1272 if (This
->frame_count
!= 0)
1273 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1276 return WINCODEC_ERR_NOTINITIALIZED
;
1278 hr
= CreatePropertyBag2(ppIEncoderOptions
);
1279 if (FAILED(hr
)) return hr
;
1281 This
->frame_count
= 1;
1283 IWICBitmapEncoder_AddRef(iface
);
1284 *ppIFrameEncode
= (IWICBitmapFrameEncode
*)&This
->lpFrameVtbl
;
1289 static HRESULT WINAPI
PngEncoder_Commit(IWICBitmapEncoder
*iface
)
1291 PngEncoder
*This
= (PngEncoder
*)iface
;
1292 TRACE("(%p)\n", iface
);
1294 if (!This
->frame_committed
|| This
->committed
)
1295 return WINCODEC_ERR_WRONGSTATE
;
1297 This
->committed
= TRUE
;
1302 static HRESULT WINAPI
PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
1303 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1305 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
1309 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl
= {
1310 PngEncoder_QueryInterface
,
1313 PngEncoder_Initialize
,
1314 PngEncoder_GetContainerFormat
,
1315 PngEncoder_GetEncoderInfo
,
1316 PngEncoder_SetColorContexts
,
1317 PngEncoder_SetPalette
,
1318 PngEncoder_SetThumbnail
,
1319 PngEncoder_SetPreview
,
1320 PngEncoder_CreateNewFrame
,
1322 PngEncoder_GetMetadataQueryWriter
1325 HRESULT
PngEncoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1330 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
1334 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
1336 if (!libpng_handle
&& !load_libpng())
1338 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG
);
1342 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder
));
1343 if (!This
) return E_OUTOFMEMORY
;
1345 This
->lpVtbl
= &PngEncoder_Vtbl
;
1346 This
->lpFrameVtbl
= &PngEncoder_FrameVtbl
;
1348 This
->png_ptr
= NULL
;
1349 This
->info_ptr
= NULL
;
1350 This
->stream
= NULL
;
1351 This
->frame_count
= 0;
1352 This
->frame_initialized
= FALSE
;
1353 This
->format
= NULL
;
1354 This
->info_written
= FALSE
;
1359 This
->lines_written
= 0;
1360 This
->frame_committed
= FALSE
;
1361 This
->committed
= FALSE
;
1363 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
1364 IUnknown_Release((IUnknown
*)This
);
1369 #else /* !HAVE_PNG_H */
1371 HRESULT
PngDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1373 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1377 HRESULT
PngEncoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1379 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");