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
;
167 static inline PngDecoder
*impl_from_frame(IWICBitmapFrameDecode
*iface
)
169 return CONTAINING_RECORD(iface
, PngDecoder
, lpFrameVtbl
);
172 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
;
174 static HRESULT WINAPI
PngDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
177 PngDecoder
*This
= (PngDecoder
*)iface
;
178 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
180 if (!ppv
) return E_INVALIDARG
;
182 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
189 return E_NOINTERFACE
;
192 IUnknown_AddRef((IUnknown
*)*ppv
);
196 static ULONG WINAPI
PngDecoder_AddRef(IWICBitmapDecoder
*iface
)
198 PngDecoder
*This
= (PngDecoder
*)iface
;
199 ULONG ref
= InterlockedIncrement(&This
->ref
);
201 TRACE("(%p) refcount=%u\n", iface
, ref
);
206 static ULONG WINAPI
PngDecoder_Release(IWICBitmapDecoder
*iface
)
208 PngDecoder
*This
= (PngDecoder
*)iface
;
209 ULONG ref
= InterlockedDecrement(&This
->ref
);
211 TRACE("(%p) refcount=%u\n", iface
, ref
);
216 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
217 HeapFree(GetProcessHeap(), 0, This
->image_bits
);
218 HeapFree(GetProcessHeap(), 0, This
);
224 static HRESULT WINAPI
PngDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
225 DWORD
*pdwCapability
)
227 FIXME("(%p,%p,%p): stub\n", iface
, pIStream
, pdwCapability
);
231 static void user_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
233 IStream
*stream
= ppng_get_io_ptr(png_ptr
);
237 hr
= IStream_Read(stream
, data
, length
, &bytesread
);
238 if (FAILED(hr
) || bytesread
!= length
)
240 ppng_error(png_ptr
, "failed reading data");
244 static HRESULT WINAPI
PngDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
245 WICDecodeOptions cacheOptions
)
247 PngDecoder
*This
= (PngDecoder
*)iface
;
250 png_bytep
*row_pointers
=NULL
;
253 int color_type
, bit_depth
;
256 png_uint_32 transparency
;
257 png_color_16p trans_values
;
260 TRACE("(%p,%p,%x)\n", iface
, pIStream
, cacheOptions
);
262 /* initialize libpng */
263 This
->png_ptr
= ppng_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
264 if (!This
->png_ptr
) return E_FAIL
;
266 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
269 ppng_destroy_read_struct(&This
->png_ptr
, NULL
, NULL
);
270 This
->png_ptr
= NULL
;
274 This
->end_info
= ppng_create_info_struct(This
->png_ptr
);
277 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, NULL
);
278 This
->png_ptr
= NULL
;
282 /* set up setjmp/longjmp error handling */
285 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
286 HeapFree(GetProcessHeap(), 0, row_pointers
);
287 This
->png_ptr
= NULL
;
290 ppng_set_error_fn(This
->png_ptr
, &jmpbuf
, user_error_fn
, user_warning_fn
);
292 /* seek to the start of the stream */
294 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
295 if (FAILED(hr
)) return hr
;
297 /* set up custom i/o handling */
298 ppng_set_read_fn(This
->png_ptr
, pIStream
, user_read_data
);
300 /* read the header */
301 ppng_read_info(This
->png_ptr
, This
->info_ptr
);
303 /* choose a pixel format */
304 color_type
= ppng_get_color_type(This
->png_ptr
, This
->info_ptr
);
305 bit_depth
= ppng_get_bit_depth(This
->png_ptr
, This
->info_ptr
);
307 /* check for color-keyed alpha */
308 transparency
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
310 if (transparency
&& color_type
!= PNG_COLOR_TYPE_PALETTE
)
313 if (color_type
== PNG_COLOR_TYPE_GRAY
)
317 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
318 ppng_set_expand_gray_1_2_4_to_8(This
->png_ptr
);
320 ppng_set_gray_1_2_4_to_8(This
->png_ptr
);
324 ppng_set_gray_to_rgb(This
->png_ptr
);
326 ppng_set_tRNS_to_alpha(This
->png_ptr
);
327 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
332 case PNG_COLOR_TYPE_GRAY
:
333 This
->bpp
= bit_depth
;
336 case 1: This
->format
= &GUID_WICPixelFormatBlackWhite
; break;
337 case 2: This
->format
= &GUID_WICPixelFormat2bppGray
; break;
338 case 4: This
->format
= &GUID_WICPixelFormat4bppGray
; break;
339 case 8: This
->format
= &GUID_WICPixelFormat8bppGray
; break;
340 case 16: This
->format
= &GUID_WICPixelFormat16bppGray
; break;
342 ERR("invalid grayscale bit depth: %i\n", bit_depth
);
346 case PNG_COLOR_TYPE_GRAY_ALPHA
:
347 /* WIC does not support grayscale alpha formats so use RGBA */
348 ppng_set_gray_to_rgb(This
->png_ptr
);
349 case PNG_COLOR_TYPE_RGB_ALPHA
:
350 This
->bpp
= bit_depth
* 4;
354 ppng_set_bgr(This
->png_ptr
);
355 This
->format
= &GUID_WICPixelFormat32bppBGRA
;
357 case 16: This
->format
= &GUID_WICPixelFormat64bppRGBA
; break;
359 ERR("invalid RGBA bit depth: %i\n", bit_depth
);
363 case PNG_COLOR_TYPE_PALETTE
:
364 This
->bpp
= bit_depth
;
367 case 1: This
->format
= &GUID_WICPixelFormat1bppIndexed
; break;
368 case 2: This
->format
= &GUID_WICPixelFormat2bppIndexed
; break;
369 case 4: This
->format
= &GUID_WICPixelFormat4bppIndexed
; break;
370 case 8: This
->format
= &GUID_WICPixelFormat8bppIndexed
; break;
372 ERR("invalid indexed color bit depth: %i\n", bit_depth
);
376 case PNG_COLOR_TYPE_RGB
:
377 This
->bpp
= bit_depth
* 3;
381 ppng_set_bgr(This
->png_ptr
);
382 This
->format
= &GUID_WICPixelFormat24bppBGR
;
384 case 16: This
->format
= &GUID_WICPixelFormat48bppRGB
; break;
386 ERR("invalid RGB color bit depth: %i\n", bit_depth
);
391 ERR("invalid color type %i\n", color_type
);
395 /* read the image data */
396 This
->width
= ppng_get_image_width(This
->png_ptr
, This
->info_ptr
);
397 This
->height
= ppng_get_image_height(This
->png_ptr
, This
->info_ptr
);
398 This
->stride
= This
->width
* This
->bpp
;
399 image_size
= This
->stride
* This
->height
;
401 This
->image_bits
= HeapAlloc(GetProcessHeap(), 0, image_size
);
402 if (!This
->image_bits
) return E_OUTOFMEMORY
;
404 row_pointers
= HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep
)*This
->height
);
405 if (!row_pointers
) return E_OUTOFMEMORY
;
407 for (i
=0; i
<This
->height
; i
++)
408 row_pointers
[i
] = This
->image_bits
+ i
* This
->stride
;
410 ppng_read_image(This
->png_ptr
, row_pointers
);
412 HeapFree(GetProcessHeap(), 0, row_pointers
);
415 ppng_read_end(This
->png_ptr
, This
->end_info
);
417 This
->initialized
= TRUE
;
422 static HRESULT WINAPI
PngDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
423 GUID
*pguidContainerFormat
)
425 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
429 static HRESULT WINAPI
PngDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
430 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
432 FIXME("(%p,%p): stub\n", iface
, ppIDecoderInfo
);
436 static HRESULT WINAPI
PngDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
437 IWICPalette
*pIPalette
)
439 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
443 static HRESULT WINAPI
PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
444 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
446 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
450 static HRESULT WINAPI
PngDecoder_GetPreview(IWICBitmapDecoder
*iface
,
451 IWICBitmapSource
**ppIBitmapSource
)
453 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
457 static HRESULT WINAPI
PngDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
458 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
460 FIXME("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
464 static HRESULT WINAPI
PngDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
465 IWICBitmapSource
**ppIThumbnail
)
467 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
468 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
471 static HRESULT WINAPI
PngDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
478 static HRESULT WINAPI
PngDecoder_GetFrame(IWICBitmapDecoder
*iface
,
479 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
481 PngDecoder
*This
= (PngDecoder
*)iface
;
482 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
484 if (!This
->initialized
) return WINCODEC_ERR_NOTINITIALIZED
;
486 if (index
!= 0) return E_INVALIDARG
;
488 IWICBitmapDecoder_AddRef(iface
);
490 *ppIBitmapFrame
= (void*)(&This
->lpFrameVtbl
);
495 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl
= {
496 PngDecoder_QueryInterface
,
499 PngDecoder_QueryCapability
,
500 PngDecoder_Initialize
,
501 PngDecoder_GetContainerFormat
,
502 PngDecoder_GetDecoderInfo
,
503 PngDecoder_CopyPalette
,
504 PngDecoder_GetMetadataQueryReader
,
505 PngDecoder_GetPreview
,
506 PngDecoder_GetColorContexts
,
507 PngDecoder_GetThumbnail
,
508 PngDecoder_GetFrameCount
,
512 static HRESULT WINAPI
PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
515 if (!ppv
) return E_INVALIDARG
;
517 if (IsEqualIID(&IID_IUnknown
, iid
) ||
518 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
519 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
526 return E_NOINTERFACE
;
529 IUnknown_AddRef((IUnknown
*)*ppv
);
533 static ULONG WINAPI
PngDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
535 PngDecoder
*This
= impl_from_frame(iface
);
536 return IUnknown_AddRef((IUnknown
*)This
);
539 static ULONG WINAPI
PngDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
541 PngDecoder
*This
= impl_from_frame(iface
);
542 return IUnknown_Release((IUnknown
*)This
);
545 static HRESULT WINAPI
PngDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
546 UINT
*puiWidth
, UINT
*puiHeight
)
548 PngDecoder
*This
= impl_from_frame(iface
);
549 *puiWidth
= This
->width
;
550 *puiHeight
= This
->height
;
551 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
555 static HRESULT WINAPI
PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
556 WICPixelFormatGUID
*pPixelFormat
)
558 PngDecoder
*This
= impl_from_frame(iface
);
559 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
561 memcpy(pPixelFormat
, This
->format
, sizeof(GUID
));
566 static HRESULT WINAPI
PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
567 double *pDpiX
, double *pDpiY
)
569 PngDecoder
*This
= impl_from_frame(iface
);
570 png_uint_32 ret
, xres
, yres
;
573 ret
= ppng_get_pHYs(This
->png_ptr
, This
->info_ptr
, &xres
, &yres
, &unit_type
);
575 if (ret
&& unit_type
== PNG_RESOLUTION_METER
)
577 *pDpiX
= xres
* 0.0254;
578 *pDpiY
= yres
* 0.0254;
582 WARN("no pHYs block present\n");
583 *pDpiX
= *pDpiY
= 96.0;
586 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
591 static HRESULT WINAPI
PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
592 IWICPalette
*pIPalette
)
594 PngDecoder
*This
= impl_from_frame(iface
);
596 png_colorp png_palette
;
598 WICColor palette
[256];
601 png_color_16p trans_values
;
604 TRACE("(%p,%p)\n", iface
, pIPalette
);
606 ret
= ppng_get_PLTE(This
->png_ptr
, This
->info_ptr
, &png_palette
, &num_palette
);
607 if (!ret
) return WINCODEC_ERR_PALETTEUNAVAILABLE
;
609 if (num_palette
> 256)
611 ERR("palette has %i colors?!\n", num_palette
);
615 for (i
=0; i
<num_palette
; i
++)
617 palette
[i
] = (0xff000000|
618 png_palette
[i
].red
<< 16|
619 png_palette
[i
].green
<< 8|
620 png_palette
[i
].blue
);
623 ret
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
626 for (i
=0; i
<num_trans
; i
++)
628 palette
[trans
[i
]] = 0x00000000;
632 return IWICPalette_InitializeCustom(pIPalette
, palette
, num_palette
);
635 static HRESULT WINAPI
PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
636 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
638 PngDecoder
*This
= impl_from_frame(iface
);
639 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
641 return copy_pixels(This
->bpp
, This
->image_bits
,
642 This
->width
, This
->height
, This
->stride
,
643 prc
, cbStride
, cbBufferSize
, pbBuffer
);
646 static HRESULT WINAPI
PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
647 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
649 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
653 static HRESULT WINAPI
PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
654 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
656 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
660 static HRESULT WINAPI
PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
661 IWICBitmapSource
**ppIThumbnail
)
663 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
667 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
= {
668 PngDecoder_Frame_QueryInterface
,
669 PngDecoder_Frame_AddRef
,
670 PngDecoder_Frame_Release
,
671 PngDecoder_Frame_GetSize
,
672 PngDecoder_Frame_GetPixelFormat
,
673 PngDecoder_Frame_GetResolution
,
674 PngDecoder_Frame_CopyPalette
,
675 PngDecoder_Frame_CopyPixels
,
676 PngDecoder_Frame_GetMetadataQueryReader
,
677 PngDecoder_Frame_GetColorContexts
,
678 PngDecoder_Frame_GetThumbnail
681 HRESULT
PngDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
686 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
690 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
692 if (!libpng_handle
&& !load_libpng())
694 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG
);
698 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder
));
699 if (!This
) return E_OUTOFMEMORY
;
701 This
->lpVtbl
= &PngDecoder_Vtbl
;
702 This
->lpFrameVtbl
= &PngDecoder_FrameVtbl
;
704 This
->png_ptr
= NULL
;
705 This
->info_ptr
= NULL
;
706 This
->end_info
= NULL
;
707 This
->initialized
= FALSE
;
708 This
->image_bits
= NULL
;
710 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
711 IUnknown_Release((IUnknown
*)This
);
716 struct png_pixelformat
{
717 const WICPixelFormatGUID
*guid
;
725 static const struct png_pixelformat formats
[] = {
726 {&GUID_WICPixelFormat24bppBGR
, 24, 8, PNG_COLOR_TYPE_RGB
, 0, 1},
727 {&GUID_WICPixelFormatBlackWhite
, 1, 1, PNG_COLOR_TYPE_GRAY
, 0, 0},
728 {&GUID_WICPixelFormat2bppGray
, 2, 2, PNG_COLOR_TYPE_GRAY
, 0, 0},
729 {&GUID_WICPixelFormat4bppGray
, 4, 4, PNG_COLOR_TYPE_GRAY
, 0, 0},
730 {&GUID_WICPixelFormat8bppGray
, 8, 8, PNG_COLOR_TYPE_GRAY
, 0, 0},
731 {&GUID_WICPixelFormat16bppGray
, 16, 16, PNG_COLOR_TYPE_GRAY
, 0, 0},
732 {&GUID_WICPixelFormat32bppBGR
, 32, 8, PNG_COLOR_TYPE_RGB
, 1, 1},
733 {&GUID_WICPixelFormat32bppBGRA
, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 1},
734 {&GUID_WICPixelFormat48bppRGB
, 48, 16, PNG_COLOR_TYPE_RGB
, 0, 0},
735 {&GUID_WICPixelFormat64bppRGBA
, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 0},
739 typedef struct PngEncoder
{
740 const IWICBitmapEncoderVtbl
*lpVtbl
;
741 const IWICBitmapFrameEncodeVtbl
*lpFrameVtbl
;
747 BOOL frame_initialized
;
748 const struct png_pixelformat
*format
;
753 BOOL frame_committed
;
757 static inline PngEncoder
*encoder_from_frame(IWICBitmapFrameEncode
*iface
)
759 return CONTAINING_RECORD(iface
, PngEncoder
, lpFrameVtbl
);
762 static HRESULT WINAPI
PngFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
765 PngEncoder
*This
= encoder_from_frame(iface
);
766 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
768 if (!ppv
) return E_INVALIDARG
;
770 if (IsEqualIID(&IID_IUnknown
, iid
) ||
771 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
773 *ppv
= &This
->lpFrameVtbl
;
778 return E_NOINTERFACE
;
781 IUnknown_AddRef((IUnknown
*)*ppv
);
785 static ULONG WINAPI
PngFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
787 PngEncoder
*This
= encoder_from_frame(iface
);
788 return IUnknown_AddRef((IUnknown
*)This
);
791 static ULONG WINAPI
PngFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
793 PngEncoder
*This
= encoder_from_frame(iface
);
794 return IUnknown_Release((IUnknown
*)This
);
797 static HRESULT WINAPI
PngFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
798 IPropertyBag2
*pIEncoderOptions
)
800 PngEncoder
*This
= encoder_from_frame(iface
);
801 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
803 if (This
->frame_initialized
) return WINCODEC_ERR_WRONGSTATE
;
805 This
->frame_initialized
= TRUE
;
810 static HRESULT WINAPI
PngFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
811 UINT uiWidth
, UINT uiHeight
)
813 PngEncoder
*This
= encoder_from_frame(iface
);
814 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
816 if (!This
->frame_initialized
|| This
->info_written
) return WINCODEC_ERR_WRONGSTATE
;
818 This
->width
= uiWidth
;
819 This
->height
= uiHeight
;
824 static HRESULT WINAPI
PngFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
825 double dpiX
, double dpiY
)
827 PngEncoder
*This
= encoder_from_frame(iface
);
828 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
830 if (!This
->frame_initialized
|| This
->info_written
) return WINCODEC_ERR_WRONGSTATE
;
838 static HRESULT WINAPI
PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
839 WICPixelFormatGUID
*pPixelFormat
)
841 PngEncoder
*This
= encoder_from_frame(iface
);
843 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
845 if (!This
->frame_initialized
|| This
->info_written
) return WINCODEC_ERR_WRONGSTATE
;
847 for (i
=0; formats
[i
].guid
; i
++)
849 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
853 if (!formats
[i
].guid
) i
= 0;
855 This
->format
= &formats
[i
];
856 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
861 static HRESULT WINAPI
PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
862 UINT cCount
, IWICColorContext
**ppIColorContext
)
864 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
868 static HRESULT WINAPI
PngFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
869 IWICPalette
*pIPalette
)
871 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
872 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
875 static HRESULT WINAPI
PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
876 IWICBitmapSource
*pIThumbnail
)
878 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
879 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
882 static HRESULT WINAPI
PngFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
883 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
885 PngEncoder
*This
= encoder_from_frame(iface
);
886 png_byte
**row_pointers
=NULL
;
889 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
891 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
892 return WINCODEC_ERR_WRONGSTATE
;
894 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
897 /* set up setjmp/longjmp error handling */
900 HeapFree(GetProcessHeap(), 0, row_pointers
);
903 ppng_set_error_fn(This
->png_ptr
, &jmpbuf
, user_error_fn
, user_warning_fn
);
905 if (!This
->info_written
)
907 ppng_set_IHDR(This
->png_ptr
, This
->info_ptr
, This
->width
, This
->height
,
908 This
->format
->bit_depth
, This
->format
->color_type
, PNG_INTERLACE_NONE
,
909 PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
911 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
913 ppng_set_pHYs(This
->png_ptr
, This
->info_ptr
, (This
->xres
+0.0127) / 0.0254,
914 (This
->yres
+0.0127) / 0.0254, PNG_RESOLUTION_METER
);
917 ppng_write_info(This
->png_ptr
, This
->info_ptr
);
919 if (This
->format
->remove_filler
)
920 ppng_set_filler(This
->png_ptr
, 0, PNG_FILLER_AFTER
);
922 if (This
->format
->swap_rgb
)
923 ppng_set_bgr(This
->png_ptr
);
925 This
->info_written
= TRUE
;
928 row_pointers
= HeapAlloc(GetProcessHeap(), 0, lineCount
* sizeof(png_byte
*));
930 return E_OUTOFMEMORY
;
932 for (i
=0; i
<lineCount
; i
++)
933 row_pointers
[i
] = pbPixels
+ cbStride
* i
;
935 ppng_write_rows(This
->png_ptr
, row_pointers
, lineCount
);
936 This
->lines_written
+= lineCount
;
938 HeapFree(GetProcessHeap(), 0, row_pointers
);
943 static HRESULT WINAPI
PngFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
944 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
946 PngEncoder
*This
= encoder_from_frame(iface
);
949 WICPixelFormatGUID guid
;
952 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
954 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
)
955 return WINCODEC_ERR_WRONGSTATE
;
959 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
960 if (FAILED(hr
)) return hr
;
961 hr
= IWICBitmapFrameEncode_SetPixelFormat(iface
, &guid
);
962 if (FAILED(hr
)) return hr
;
965 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
966 if (FAILED(hr
)) return hr
;
967 if (memcmp(&guid
, This
->format
->guid
, sizeof(GUID
)) != 0)
969 /* FIXME: should use WICConvertBitmapSource to convert */
970 ERR("format %s unsupported\n", debugstr_guid(&guid
));
974 if (This
->xres
== 0.0 || This
->yres
== 0.0)
977 hr
= IWICBitmapSource_GetResolution(pIBitmapSource
, &xres
, &yres
);
978 if (FAILED(hr
)) return hr
;
979 hr
= IWICBitmapFrameEncode_SetResolution(iface
, xres
, yres
);
980 if (FAILED(hr
)) return hr
;
986 hr
= IWICBitmapSource_GetSize(pIBitmapSource
, &width
, &height
);
987 if (FAILED(hr
)) return hr
;
995 if (prc
->Width
!= This
->width
) return E_INVALIDARG
;
997 stride
= (This
->format
->bpp
* This
->width
+ 7)/8;
999 pixeldata
= HeapAlloc(GetProcessHeap(), 0, stride
* prc
->Height
);
1000 if (!pixeldata
) return E_OUTOFMEMORY
;
1002 hr
= IWICBitmapSource_CopyPixels(pIBitmapSource
, prc
, stride
,
1003 stride
*prc
->Height
, pixeldata
);
1007 hr
= IWICBitmapFrameEncode_WritePixels(iface
, prc
->Height
, stride
,
1008 stride
*prc
->Height
, pixeldata
);
1011 HeapFree(GetProcessHeap(), 0, pixeldata
);
1016 static HRESULT WINAPI
PngFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
1018 PngEncoder
*This
= encoder_from_frame(iface
);
1020 TRACE("(%p)\n", iface
);
1022 if (!This
->info_written
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
1023 return WINCODEC_ERR_WRONGSTATE
;
1025 /* set up setjmp/longjmp error handling */
1030 ppng_set_error_fn(This
->png_ptr
, &jmpbuf
, user_error_fn
, user_warning_fn
);
1032 ppng_write_end(This
->png_ptr
, This
->info_ptr
);
1034 This
->frame_committed
= TRUE
;
1039 static HRESULT WINAPI
PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1040 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1042 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1046 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl
= {
1047 PngFrameEncode_QueryInterface
,
1048 PngFrameEncode_AddRef
,
1049 PngFrameEncode_Release
,
1050 PngFrameEncode_Initialize
,
1051 PngFrameEncode_SetSize
,
1052 PngFrameEncode_SetResolution
,
1053 PngFrameEncode_SetPixelFormat
,
1054 PngFrameEncode_SetColorContexts
,
1055 PngFrameEncode_SetPalette
,
1056 PngFrameEncode_SetThumbnail
,
1057 PngFrameEncode_WritePixels
,
1058 PngFrameEncode_WriteSource
,
1059 PngFrameEncode_Commit
,
1060 PngFrameEncode_GetMetadataQueryWriter
1063 static HRESULT WINAPI
PngEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1066 PngEncoder
*This
= (PngEncoder
*)iface
;
1067 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1069 if (!ppv
) return E_INVALIDARG
;
1071 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1072 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1079 return E_NOINTERFACE
;
1082 IUnknown_AddRef((IUnknown
*)*ppv
);
1086 static ULONG WINAPI
PngEncoder_AddRef(IWICBitmapEncoder
*iface
)
1088 PngEncoder
*This
= (PngEncoder
*)iface
;
1089 ULONG ref
= InterlockedIncrement(&This
->ref
);
1091 TRACE("(%p) refcount=%u\n", iface
, ref
);
1096 static ULONG WINAPI
PngEncoder_Release(IWICBitmapEncoder
*iface
)
1098 PngEncoder
*This
= (PngEncoder
*)iface
;
1099 ULONG ref
= InterlockedDecrement(&This
->ref
);
1101 TRACE("(%p) refcount=%u\n", iface
, ref
);
1106 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1108 IStream_Release(This
->stream
);
1109 HeapFree(GetProcessHeap(), 0, This
);
1115 static void user_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
1117 PngEncoder
*This
= ppng_get_io_ptr(png_ptr
);
1121 hr
= IStream_Write(This
->stream
, data
, length
, &byteswritten
);
1122 if (FAILED(hr
) || byteswritten
!= length
)
1124 ppng_error(png_ptr
, "failed writing data");
1128 static void user_flush(png_structp png_ptr
)
1132 static HRESULT WINAPI
PngEncoder_Initialize(IWICBitmapEncoder
*iface
,
1133 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1135 PngEncoder
*This
= (PngEncoder
*)iface
;
1138 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1141 return WINCODEC_ERR_WRONGSTATE
;
1143 /* initialize libpng */
1144 This
->png_ptr
= ppng_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
1148 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
1149 if (!This
->info_ptr
)
1151 ppng_destroy_write_struct(&This
->png_ptr
, NULL
);
1152 This
->png_ptr
= NULL
;
1156 IStream_AddRef(pIStream
);
1157 This
->stream
= pIStream
;
1159 /* set up setjmp/longjmp error handling */
1162 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1163 This
->png_ptr
= NULL
;
1164 IStream_Release(This
->stream
);
1165 This
->stream
= NULL
;
1168 ppng_set_error_fn(This
->png_ptr
, &jmpbuf
, user_error_fn
, user_warning_fn
);
1170 /* set up custom i/o handling */
1171 ppng_set_write_fn(This
->png_ptr
, This
, user_write_data
, user_flush
);
1176 static HRESULT WINAPI
PngEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1177 GUID
*pguidContainerFormat
)
1179 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1183 static HRESULT WINAPI
PngEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1184 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1186 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1190 static HRESULT WINAPI
PngEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1191 UINT cCount
, IWICColorContext
**ppIColorContext
)
1193 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1197 static HRESULT WINAPI
PngEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1199 TRACE("(%p,%p)\n", iface
, pIPalette
);
1200 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1203 static HRESULT WINAPI
PngEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1205 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1206 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1209 static HRESULT WINAPI
PngEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1211 TRACE("(%p,%p)\n", iface
, pIPreview
);
1212 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1215 static HRESULT WINAPI
PngEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1216 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1218 PngEncoder
*This
= (PngEncoder
*)iface
;
1220 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1222 if (This
->frame_count
!= 0)
1223 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1226 return WINCODEC_ERR_NOTINITIALIZED
;
1228 hr
= CreatePropertyBag2(ppIEncoderOptions
);
1229 if (FAILED(hr
)) return hr
;
1231 This
->frame_count
= 1;
1233 IWICBitmapEncoder_AddRef(iface
);
1234 *ppIFrameEncode
= (IWICBitmapFrameEncode
*)&This
->lpFrameVtbl
;
1239 static HRESULT WINAPI
PngEncoder_Commit(IWICBitmapEncoder
*iface
)
1241 PngEncoder
*This
= (PngEncoder
*)iface
;
1242 TRACE("(%p)\n", iface
);
1244 if (!This
->frame_committed
|| This
->committed
)
1245 return WINCODEC_ERR_WRONGSTATE
;
1247 This
->committed
= TRUE
;
1252 static HRESULT WINAPI
PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
1253 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1255 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
1259 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl
= {
1260 PngEncoder_QueryInterface
,
1263 PngEncoder_Initialize
,
1264 PngEncoder_GetContainerFormat
,
1265 PngEncoder_GetEncoderInfo
,
1266 PngEncoder_SetColorContexts
,
1267 PngEncoder_SetPalette
,
1268 PngEncoder_SetThumbnail
,
1269 PngEncoder_SetPreview
,
1270 PngEncoder_CreateNewFrame
,
1272 PngEncoder_GetMetadataQueryWriter
1275 HRESULT
PngEncoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1280 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
1284 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
1286 if (!libpng_handle
&& !load_libpng())
1288 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG
);
1292 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder
));
1293 if (!This
) return E_OUTOFMEMORY
;
1295 This
->lpVtbl
= &PngEncoder_Vtbl
;
1296 This
->lpFrameVtbl
= &PngEncoder_FrameVtbl
;
1298 This
->png_ptr
= NULL
;
1299 This
->info_ptr
= NULL
;
1300 This
->stream
= NULL
;
1301 This
->frame_count
= 0;
1302 This
->frame_initialized
= FALSE
;
1303 This
->format
= NULL
;
1304 This
->info_written
= FALSE
;
1309 This
->lines_written
= 0;
1310 This
->frame_committed
= FALSE
;
1311 This
->committed
= FALSE
;
1313 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
1314 IUnknown_Release((IUnknown
*)This
);
1319 #else /* !HAVE_PNG_H */
1321 HRESULT
PngDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1323 ERR("Trying to load PNG picture, but PNG supported not compiled in.\n");
1327 HRESULT
PngEncoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1329 ERR("Trying to save PNG picture, but PNG supported not compiled in.\n");