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_image_height
);
55 MAKE_FUNCPTR(png_get_image_width
);
56 MAKE_FUNCPTR(png_get_io_ptr
);
57 MAKE_FUNCPTR(png_get_pHYs
);
58 MAKE_FUNCPTR(png_get_PLTE
);
59 MAKE_FUNCPTR(png_get_tRNS
);
60 MAKE_FUNCPTR(png_set_bgr
);
61 MAKE_FUNCPTR(png_set_filler
);
62 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8
);
63 MAKE_FUNCPTR(png_set_gray_to_rgb
);
64 MAKE_FUNCPTR(png_set_IHDR
);
65 MAKE_FUNCPTR(png_set_pHYs
);
66 MAKE_FUNCPTR(png_set_read_fn
);
67 MAKE_FUNCPTR(png_set_strip_16
);
68 MAKE_FUNCPTR(png_set_tRNS_to_alpha
);
69 MAKE_FUNCPTR(png_set_write_fn
);
70 MAKE_FUNCPTR(png_read_end
);
71 MAKE_FUNCPTR(png_read_image
);
72 MAKE_FUNCPTR(png_read_info
);
73 MAKE_FUNCPTR(png_write_end
);
74 MAKE_FUNCPTR(png_write_info
);
75 MAKE_FUNCPTR(png_write_rows
);
78 static void *load_libpng(void)
80 if((libpng_handle
= wine_dlopen(SONAME_LIBPNG
, RTLD_NOW
, NULL
, 0)) != NULL
) {
82 #define LOAD_FUNCPTR(f) \
83 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
84 libpng_handle = NULL; \
87 LOAD_FUNCPTR(png_create_read_struct
);
88 LOAD_FUNCPTR(png_create_info_struct
);
89 LOAD_FUNCPTR(png_create_write_struct
);
90 LOAD_FUNCPTR(png_destroy_read_struct
);
91 LOAD_FUNCPTR(png_destroy_write_struct
);
92 LOAD_FUNCPTR(png_error
);
93 LOAD_FUNCPTR(png_get_bit_depth
);
94 LOAD_FUNCPTR(png_get_color_type
);
95 LOAD_FUNCPTR(png_get_image_height
);
96 LOAD_FUNCPTR(png_get_image_width
);
97 LOAD_FUNCPTR(png_get_io_ptr
);
98 LOAD_FUNCPTR(png_get_pHYs
);
99 LOAD_FUNCPTR(png_get_PLTE
);
100 LOAD_FUNCPTR(png_get_tRNS
);
101 LOAD_FUNCPTR(png_set_bgr
);
102 LOAD_FUNCPTR(png_set_filler
);
103 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8
);
104 LOAD_FUNCPTR(png_set_gray_to_rgb
);
105 LOAD_FUNCPTR(png_set_IHDR
);
106 LOAD_FUNCPTR(png_set_pHYs
);
107 LOAD_FUNCPTR(png_set_read_fn
);
108 LOAD_FUNCPTR(png_set_strip_16
);
109 LOAD_FUNCPTR(png_set_tRNS_to_alpha
);
110 LOAD_FUNCPTR(png_set_write_fn
);
111 LOAD_FUNCPTR(png_read_end
);
112 LOAD_FUNCPTR(png_read_image
);
113 LOAD_FUNCPTR(png_read_info
);
114 LOAD_FUNCPTR(png_write_end
);
115 LOAD_FUNCPTR(png_write_info
);
116 LOAD_FUNCPTR(png_write_rows
);
120 return libpng_handle
;
124 const IWICBitmapDecoderVtbl
*lpVtbl
;
125 const IWICBitmapFrameDecodeVtbl
*lpFrameVtbl
;
134 const WICPixelFormatGUID
*format
;
138 static inline PngDecoder
*impl_from_frame(IWICBitmapFrameDecode
*iface
)
140 return CONTAINING_RECORD(iface
, PngDecoder
, lpFrameVtbl
);
143 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
;
145 static HRESULT WINAPI
PngDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
148 PngDecoder
*This
= (PngDecoder
*)iface
;
149 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
151 if (!ppv
) return E_INVALIDARG
;
153 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
160 return E_NOINTERFACE
;
163 IUnknown_AddRef((IUnknown
*)*ppv
);
167 static ULONG WINAPI
PngDecoder_AddRef(IWICBitmapDecoder
*iface
)
169 PngDecoder
*This
= (PngDecoder
*)iface
;
170 ULONG ref
= InterlockedIncrement(&This
->ref
);
172 TRACE("(%p) refcount=%u\n", iface
, ref
);
177 static ULONG WINAPI
PngDecoder_Release(IWICBitmapDecoder
*iface
)
179 PngDecoder
*This
= (PngDecoder
*)iface
;
180 ULONG ref
= InterlockedDecrement(&This
->ref
);
182 TRACE("(%p) refcount=%u\n", iface
, ref
);
187 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
188 HeapFree(GetProcessHeap(), 0, This
->image_bits
);
189 HeapFree(GetProcessHeap(), 0, This
);
195 static HRESULT WINAPI
PngDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
196 DWORD
*pdwCapability
)
198 FIXME("(%p,%p,%p): stub\n", iface
, pIStream
, pdwCapability
);
202 static void user_read_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
204 IStream
*stream
= ppng_get_io_ptr(png_ptr
);
208 hr
= IStream_Read(stream
, data
, length
, &bytesread
);
209 if (FAILED(hr
) || bytesread
!= length
)
211 ppng_error(png_ptr
, "failed reading data");
215 static HRESULT WINAPI
PngDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
216 WICDecodeOptions cacheOptions
)
218 PngDecoder
*This
= (PngDecoder
*)iface
;
221 png_bytep
*row_pointers
=NULL
;
224 int color_type
, bit_depth
;
227 png_uint_32 transparency
;
228 png_color_16p trans_values
;
229 TRACE("(%p,%p,%x)\n", iface
, pIStream
, cacheOptions
);
231 /* initialize libpng */
232 This
->png_ptr
= ppng_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
233 if (!This
->png_ptr
) return E_FAIL
;
235 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
238 ppng_destroy_read_struct(&This
->png_ptr
, NULL
, NULL
);
239 This
->png_ptr
= NULL
;
243 This
->end_info
= ppng_create_info_struct(This
->png_ptr
);
246 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, NULL
);
247 This
->png_ptr
= NULL
;
251 /* set up setjmp/longjmp error handling */
252 if (setjmp(png_jmpbuf(This
->png_ptr
)))
254 ppng_destroy_read_struct(&This
->png_ptr
, &This
->info_ptr
, &This
->end_info
);
255 HeapFree(GetProcessHeap(), 0, row_pointers
);
256 This
->png_ptr
= NULL
;
260 /* seek to the start of the stream */
262 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
263 if (FAILED(hr
)) return hr
;
265 /* set up custom i/o handling */
266 ppng_set_read_fn(This
->png_ptr
, pIStream
, user_read_data
);
268 /* read the header */
269 ppng_read_info(This
->png_ptr
, This
->info_ptr
);
271 /* choose a pixel format */
272 color_type
= ppng_get_color_type(This
->png_ptr
, This
->info_ptr
);
273 bit_depth
= ppng_get_bit_depth(This
->png_ptr
, This
->info_ptr
);
275 /* check for color-keyed alpha */
276 transparency
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
278 if (transparency
&& color_type
!= PNG_COLOR_TYPE_PALETTE
)
281 if (color_type
== PNG_COLOR_TYPE_GRAY
)
285 ppng_set_gray_1_2_4_to_8(This
->png_ptr
);
288 ppng_set_gray_to_rgb(This
->png_ptr
);
290 ppng_set_tRNS_to_alpha(This
->png_ptr
);
291 color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
296 case PNG_COLOR_TYPE_GRAY
:
297 This
->bpp
= bit_depth
;
300 case 1: This
->format
= &GUID_WICPixelFormatBlackWhite
; break;
301 case 2: This
->format
= &GUID_WICPixelFormat2bppGray
; break;
302 case 4: This
->format
= &GUID_WICPixelFormat4bppGray
; break;
303 case 8: This
->format
= &GUID_WICPixelFormat8bppGray
; break;
304 case 16: This
->format
= &GUID_WICPixelFormat16bppGray
; break;
306 ERR("invalid grayscale bit depth: %i\n", bit_depth
);
310 case PNG_COLOR_TYPE_GRAY_ALPHA
:
311 /* WIC does not support grayscale alpha formats so use RGBA */
312 ppng_set_gray_to_rgb(This
->png_ptr
);
313 case PNG_COLOR_TYPE_RGB_ALPHA
:
314 This
->bpp
= bit_depth
* 4;
318 ppng_set_bgr(This
->png_ptr
);
319 This
->format
= &GUID_WICPixelFormat32bppBGRA
;
321 case 16: This
->format
= &GUID_WICPixelFormat64bppRGBA
; break;
323 ERR("invalid RGBA bit depth: %i\n", bit_depth
);
327 case PNG_COLOR_TYPE_PALETTE
:
328 This
->bpp
= bit_depth
;
331 case 1: This
->format
= &GUID_WICPixelFormat1bppIndexed
; break;
332 case 2: This
->format
= &GUID_WICPixelFormat2bppIndexed
; break;
333 case 4: This
->format
= &GUID_WICPixelFormat4bppIndexed
; break;
334 case 8: This
->format
= &GUID_WICPixelFormat8bppIndexed
; break;
336 ERR("invalid indexed color bit depth: %i\n", bit_depth
);
340 case PNG_COLOR_TYPE_RGB
:
341 This
->bpp
= bit_depth
* 3;
345 ppng_set_bgr(This
->png_ptr
);
346 This
->format
= &GUID_WICPixelFormat24bppBGR
;
348 case 16: This
->format
= &GUID_WICPixelFormat48bppRGB
; break;
350 ERR("invalid RGB color bit depth: %i\n", bit_depth
);
355 ERR("invalid color type %i\n", color_type
);
359 /* read the image data */
360 This
->width
= ppng_get_image_width(This
->png_ptr
, This
->info_ptr
);
361 This
->height
= ppng_get_image_height(This
->png_ptr
, This
->info_ptr
);
362 This
->stride
= This
->width
* This
->bpp
;
363 image_size
= This
->stride
* This
->height
;
365 This
->image_bits
= HeapAlloc(GetProcessHeap(), 0, image_size
);
366 if (!This
->image_bits
) return E_OUTOFMEMORY
;
368 row_pointers
= HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep
)*This
->height
);
369 if (!row_pointers
) return E_OUTOFMEMORY
;
371 for (i
=0; i
<This
->height
; i
++)
372 row_pointers
[i
] = This
->image_bits
+ i
* This
->stride
;
374 ppng_read_image(This
->png_ptr
, row_pointers
);
376 HeapFree(GetProcessHeap(), 0, row_pointers
);
379 ppng_read_end(This
->png_ptr
, This
->end_info
);
381 This
->initialized
= TRUE
;
386 static HRESULT WINAPI
PngDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
387 GUID
*pguidContainerFormat
)
389 memcpy(pguidContainerFormat
, &GUID_ContainerFormatPng
, sizeof(GUID
));
393 static HRESULT WINAPI
PngDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
394 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
396 FIXME("(%p,%p): stub\n", iface
, ppIDecoderInfo
);
400 static HRESULT WINAPI
PngDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
401 IWICPalette
*pIPalette
)
403 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
407 static HRESULT WINAPI
PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
408 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
410 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
414 static HRESULT WINAPI
PngDecoder_GetPreview(IWICBitmapDecoder
*iface
,
415 IWICBitmapSource
**ppIBitmapSource
)
417 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
421 static HRESULT WINAPI
PngDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
422 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
424 FIXME("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
428 static HRESULT WINAPI
PngDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
429 IWICBitmapSource
**ppIThumbnail
)
431 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
432 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
435 static HRESULT WINAPI
PngDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
442 static HRESULT WINAPI
PngDecoder_GetFrame(IWICBitmapDecoder
*iface
,
443 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
445 PngDecoder
*This
= (PngDecoder
*)iface
;
446 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
448 if (!This
->initialized
) return WINCODEC_ERR_NOTINITIALIZED
;
450 if (index
!= 0) return E_INVALIDARG
;
452 IWICBitmapDecoder_AddRef(iface
);
454 *ppIBitmapFrame
= (void*)(&This
->lpFrameVtbl
);
459 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl
= {
460 PngDecoder_QueryInterface
,
463 PngDecoder_QueryCapability
,
464 PngDecoder_Initialize
,
465 PngDecoder_GetContainerFormat
,
466 PngDecoder_GetDecoderInfo
,
467 PngDecoder_CopyPalette
,
468 PngDecoder_GetMetadataQueryReader
,
469 PngDecoder_GetPreview
,
470 PngDecoder_GetColorContexts
,
471 PngDecoder_GetThumbnail
,
472 PngDecoder_GetFrameCount
,
476 static HRESULT WINAPI
PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
479 if (!ppv
) return E_INVALIDARG
;
481 if (IsEqualIID(&IID_IUnknown
, iid
) ||
482 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
483 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
490 return E_NOINTERFACE
;
493 IUnknown_AddRef((IUnknown
*)*ppv
);
497 static ULONG WINAPI
PngDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
499 PngDecoder
*This
= impl_from_frame(iface
);
500 return IUnknown_AddRef((IUnknown
*)This
);
503 static ULONG WINAPI
PngDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
505 PngDecoder
*This
= impl_from_frame(iface
);
506 return IUnknown_Release((IUnknown
*)This
);
509 static HRESULT WINAPI
PngDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
510 UINT
*puiWidth
, UINT
*puiHeight
)
512 PngDecoder
*This
= impl_from_frame(iface
);
513 *puiWidth
= This
->width
;
514 *puiHeight
= This
->height
;
515 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
519 static HRESULT WINAPI
PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
520 WICPixelFormatGUID
*pPixelFormat
)
522 PngDecoder
*This
= impl_from_frame(iface
);
523 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
525 memcpy(pPixelFormat
, This
->format
, sizeof(GUID
));
530 static HRESULT WINAPI
PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
531 double *pDpiX
, double *pDpiY
)
533 PngDecoder
*This
= impl_from_frame(iface
);
534 png_uint_32 ret
, xres
, yres
;
537 ret
= ppng_get_pHYs(This
->png_ptr
, This
->info_ptr
, &xres
, &yres
, &unit_type
);
539 if (ret
&& unit_type
== PNG_RESOLUTION_METER
)
541 *pDpiX
= xres
* 0.0254;
542 *pDpiY
= yres
* 0.0254;
546 WARN("no pHYs block present\n");
547 *pDpiX
= *pDpiY
= 96.0;
550 TRACE("(%p)->(%0.2f,%0.2f)\n", iface
, *pDpiX
, *pDpiY
);
555 static HRESULT WINAPI
PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
556 IWICPalette
*pIPalette
)
558 PngDecoder
*This
= impl_from_frame(iface
);
560 png_colorp png_palette
;
562 WICColor palette
[256];
565 png_color_16p trans_values
;
568 TRACE("(%p,%p)\n", iface
, pIPalette
);
570 ret
= ppng_get_PLTE(This
->png_ptr
, This
->info_ptr
, &png_palette
, &num_palette
);
571 if (!ret
) return WINCODEC_ERR_PALETTEUNAVAILABLE
;
573 if (num_palette
> 256)
575 ERR("palette has %i colors?!\n", num_palette
);
579 for (i
=0; i
<num_palette
; i
++)
581 palette
[i
] = (0xff000000|
582 png_palette
[i
].red
<< 16|
583 png_palette
[i
].green
<< 8|
584 png_palette
[i
].blue
);
587 ret
= ppng_get_tRNS(This
->png_ptr
, This
->info_ptr
, &trans
, &num_trans
, &trans_values
);
590 for (i
=0; i
<num_trans
; i
++)
592 palette
[trans
[i
]] = 0x00000000;
596 return IWICPalette_InitializeCustom(pIPalette
, palette
, num_palette
);
599 static HRESULT WINAPI
PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
600 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
602 PngDecoder
*This
= impl_from_frame(iface
);
603 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
605 return copy_pixels(This
->bpp
, This
->image_bits
,
606 This
->width
, This
->height
, This
->stride
,
607 prc
, cbStride
, cbBufferSize
, pbBuffer
);
610 static HRESULT WINAPI
PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
611 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
613 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
617 static HRESULT WINAPI
PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
618 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
620 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
624 static HRESULT WINAPI
PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
625 IWICBitmapSource
**ppIThumbnail
)
627 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
631 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl
= {
632 PngDecoder_Frame_QueryInterface
,
633 PngDecoder_Frame_AddRef
,
634 PngDecoder_Frame_Release
,
635 PngDecoder_Frame_GetSize
,
636 PngDecoder_Frame_GetPixelFormat
,
637 PngDecoder_Frame_GetResolution
,
638 PngDecoder_Frame_CopyPalette
,
639 PngDecoder_Frame_CopyPixels
,
640 PngDecoder_Frame_GetMetadataQueryReader
,
641 PngDecoder_Frame_GetColorContexts
,
642 PngDecoder_Frame_GetThumbnail
645 HRESULT
PngDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
650 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
654 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
656 if (!libpng_handle
&& !load_libpng())
658 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG
);
662 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder
));
663 if (!This
) return E_OUTOFMEMORY
;
665 This
->lpVtbl
= &PngDecoder_Vtbl
;
666 This
->lpFrameVtbl
= &PngDecoder_FrameVtbl
;
668 This
->png_ptr
= NULL
;
669 This
->info_ptr
= NULL
;
670 This
->end_info
= NULL
;
671 This
->initialized
= FALSE
;
672 This
->image_bits
= NULL
;
674 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
675 IUnknown_Release((IUnknown
*)This
);
680 struct png_pixelformat
{
681 const WICPixelFormatGUID
*guid
;
689 static const struct png_pixelformat formats
[] = {
690 {&GUID_WICPixelFormat24bppBGR
, 24, 8, PNG_COLOR_TYPE_RGB
, 0, 1},
691 {&GUID_WICPixelFormatBlackWhite
, 1, 1, PNG_COLOR_TYPE_GRAY
, 0, 0},
692 {&GUID_WICPixelFormat2bppGray
, 2, 2, PNG_COLOR_TYPE_GRAY
, 0, 0},
693 {&GUID_WICPixelFormat4bppGray
, 4, 4, PNG_COLOR_TYPE_GRAY
, 0, 0},
694 {&GUID_WICPixelFormat8bppGray
, 8, 8, PNG_COLOR_TYPE_GRAY
, 0, 0},
695 {&GUID_WICPixelFormat16bppGray
, 16, 16, PNG_COLOR_TYPE_GRAY
, 0, 0},
696 {&GUID_WICPixelFormat32bppBGR
, 32, 8, PNG_COLOR_TYPE_RGB
, 1, 1},
697 {&GUID_WICPixelFormat32bppBGRA
, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 1},
698 {&GUID_WICPixelFormat48bppRGB
, 48, 16, PNG_COLOR_TYPE_RGB
, 0, 0},
699 {&GUID_WICPixelFormat64bppRGBA
, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA
, 0, 0},
703 typedef struct PngEncoder
{
704 const IWICBitmapEncoderVtbl
*lpVtbl
;
705 const IWICBitmapFrameEncodeVtbl
*lpFrameVtbl
;
711 BOOL frame_initialized
;
712 const struct png_pixelformat
*format
;
717 BOOL frame_committed
;
721 static inline PngEncoder
*encoder_from_frame(IWICBitmapFrameEncode
*iface
)
723 return CONTAINING_RECORD(iface
, PngEncoder
, lpFrameVtbl
);
726 static HRESULT WINAPI
PngFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
729 PngEncoder
*This
= encoder_from_frame(iface
);
730 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
732 if (!ppv
) return E_INVALIDARG
;
734 if (IsEqualIID(&IID_IUnknown
, iid
) ||
735 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
737 *ppv
= &This
->lpFrameVtbl
;
742 return E_NOINTERFACE
;
745 IUnknown_AddRef((IUnknown
*)*ppv
);
749 static ULONG WINAPI
PngFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
751 PngEncoder
*This
= encoder_from_frame(iface
);
752 return IUnknown_AddRef((IUnknown
*)This
);
755 static ULONG WINAPI
PngFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
757 PngEncoder
*This
= encoder_from_frame(iface
);
758 return IUnknown_Release((IUnknown
*)This
);
761 static HRESULT WINAPI
PngFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
762 IPropertyBag2
*pIEncoderOptions
)
764 PngEncoder
*This
= encoder_from_frame(iface
);
765 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
767 if (This
->frame_initialized
) return WINCODEC_ERR_WRONGSTATE
;
769 This
->frame_initialized
= TRUE
;
774 static HRESULT WINAPI
PngFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
775 UINT uiWidth
, UINT uiHeight
)
777 PngEncoder
*This
= encoder_from_frame(iface
);
778 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
780 if (!This
->frame_initialized
|| This
->info_written
) return WINCODEC_ERR_WRONGSTATE
;
782 This
->width
= uiWidth
;
783 This
->height
= uiHeight
;
788 static HRESULT WINAPI
PngFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
789 double dpiX
, double dpiY
)
791 PngEncoder
*This
= encoder_from_frame(iface
);
792 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
794 if (!This
->frame_initialized
|| This
->info_written
) return WINCODEC_ERR_WRONGSTATE
;
802 static HRESULT WINAPI
PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
803 WICPixelFormatGUID
*pPixelFormat
)
805 PngEncoder
*This
= encoder_from_frame(iface
);
807 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
809 if (!This
->frame_initialized
|| This
->info_written
) return WINCODEC_ERR_WRONGSTATE
;
811 for (i
=0; formats
[i
].guid
; i
++)
813 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
817 if (!formats
[i
].guid
) i
= 0;
819 This
->format
= &formats
[i
];
820 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
825 static HRESULT WINAPI
PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
826 UINT cCount
, IWICColorContext
**ppIColorContext
)
828 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
832 static HRESULT WINAPI
PngFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
833 IWICPalette
*pIPalette
)
835 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
836 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
839 static HRESULT WINAPI
PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
840 IWICBitmapSource
*pIThumbnail
)
842 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
843 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
846 static HRESULT WINAPI
PngFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
847 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
849 PngEncoder
*This
= encoder_from_frame(iface
);
850 png_byte
**row_pointers
=NULL
;
852 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
854 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
855 return WINCODEC_ERR_WRONGSTATE
;
857 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
860 /* set up setjmp/longjmp error handling */
861 if (setjmp(png_jmpbuf(This
->png_ptr
)))
863 HeapFree(GetProcessHeap(), 0, row_pointers
);
867 if (!This
->info_written
)
869 ppng_set_IHDR(This
->png_ptr
, This
->info_ptr
, This
->width
, This
->height
,
870 This
->format
->bit_depth
, This
->format
->color_type
, PNG_INTERLACE_NONE
,
871 PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
873 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
875 ppng_set_pHYs(This
->png_ptr
, This
->info_ptr
, (This
->xres
+0.0127) / 0.0254,
876 (This
->yres
+0.0127) / 0.0254, PNG_RESOLUTION_METER
);
879 ppng_write_info(This
->png_ptr
, This
->info_ptr
);
881 if (This
->format
->remove_filler
)
882 ppng_set_filler(This
->png_ptr
, 0, PNG_FILLER_AFTER
);
884 if (This
->format
->swap_rgb
)
885 ppng_set_bgr(This
->png_ptr
);
887 This
->info_written
= TRUE
;
890 row_pointers
= HeapAlloc(GetProcessHeap(), 0, lineCount
* sizeof(png_byte
*));
892 return E_OUTOFMEMORY
;
894 for (i
=0; i
<lineCount
; i
++)
895 row_pointers
[i
] = pbPixels
+ cbStride
* i
;
897 ppng_write_rows(This
->png_ptr
, row_pointers
, lineCount
);
898 This
->lines_written
+= lineCount
;
900 HeapFree(GetProcessHeap(), 0, row_pointers
);
905 static HRESULT WINAPI
PngFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
906 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
908 PngEncoder
*This
= encoder_from_frame(iface
);
911 WICPixelFormatGUID guid
;
914 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
916 if (!This
->frame_initialized
|| !This
->width
|| !This
->height
)
917 return WINCODEC_ERR_WRONGSTATE
;
921 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
922 if (FAILED(hr
)) return hr
;
923 hr
= IWICBitmapFrameEncode_SetPixelFormat(iface
, &guid
);
924 if (FAILED(hr
)) return hr
;
927 hr
= IWICBitmapSource_GetPixelFormat(pIBitmapSource
, &guid
);
928 if (FAILED(hr
)) return hr
;
929 if (memcmp(&guid
, This
->format
->guid
, sizeof(GUID
)) != 0)
931 /* FIXME: should use WICConvertBitmapSource to convert */
932 ERR("format %s unsupported\n", debugstr_guid(&guid
));
936 if (This
->xres
== 0.0 || This
->yres
== 0.0)
939 hr
= IWICBitmapSource_GetResolution(pIBitmapSource
, &xres
, &yres
);
940 if (FAILED(hr
)) return hr
;
941 hr
= IWICBitmapFrameEncode_SetResolution(iface
, xres
, yres
);
942 if (FAILED(hr
)) return hr
;
948 hr
= IWICBitmapSource_GetSize(pIBitmapSource
, &width
, &height
);
949 if (FAILED(hr
)) return hr
;
957 if (prc
->Width
!= This
->width
) return E_INVALIDARG
;
959 stride
= (This
->format
->bpp
* This
->width
+ 7)/8;
961 pixeldata
= HeapAlloc(GetProcessHeap(), 0, stride
* prc
->Height
);
962 if (!pixeldata
) return E_OUTOFMEMORY
;
964 hr
= IWICBitmapSource_CopyPixels(pIBitmapSource
, prc
, stride
,
965 stride
*prc
->Height
, pixeldata
);
969 hr
= IWICBitmapFrameEncode_WritePixels(iface
, prc
->Height
, stride
,
970 stride
*prc
->Height
, pixeldata
);
973 HeapFree(GetProcessHeap(), 0, pixeldata
);
978 static HRESULT WINAPI
PngFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
980 PngEncoder
*This
= encoder_from_frame(iface
);
981 TRACE("(%p)\n", iface
);
983 if (!This
->info_written
|| This
->lines_written
!= This
->height
|| This
->frame_committed
)
984 return WINCODEC_ERR_WRONGSTATE
;
986 /* set up setjmp/longjmp error handling */
987 if (setjmp(png_jmpbuf(This
->png_ptr
)))
992 ppng_write_end(This
->png_ptr
, This
->info_ptr
);
994 This
->frame_committed
= TRUE
;
999 static HRESULT WINAPI
PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1000 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1002 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1006 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl
= {
1007 PngFrameEncode_QueryInterface
,
1008 PngFrameEncode_AddRef
,
1009 PngFrameEncode_Release
,
1010 PngFrameEncode_Initialize
,
1011 PngFrameEncode_SetSize
,
1012 PngFrameEncode_SetResolution
,
1013 PngFrameEncode_SetPixelFormat
,
1014 PngFrameEncode_SetColorContexts
,
1015 PngFrameEncode_SetPalette
,
1016 PngFrameEncode_SetThumbnail
,
1017 PngFrameEncode_WritePixels
,
1018 PngFrameEncode_WriteSource
,
1019 PngFrameEncode_Commit
,
1020 PngFrameEncode_GetMetadataQueryWriter
1023 static HRESULT WINAPI
PngEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1026 PngEncoder
*This
= (PngEncoder
*)iface
;
1027 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1029 if (!ppv
) return E_INVALIDARG
;
1031 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1032 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1039 return E_NOINTERFACE
;
1042 IUnknown_AddRef((IUnknown
*)*ppv
);
1046 static ULONG WINAPI
PngEncoder_AddRef(IWICBitmapEncoder
*iface
)
1048 PngEncoder
*This
= (PngEncoder
*)iface
;
1049 ULONG ref
= InterlockedIncrement(&This
->ref
);
1051 TRACE("(%p) refcount=%u\n", iface
, ref
);
1056 static ULONG WINAPI
PngEncoder_Release(IWICBitmapEncoder
*iface
)
1058 PngEncoder
*This
= (PngEncoder
*)iface
;
1059 ULONG ref
= InterlockedDecrement(&This
->ref
);
1061 TRACE("(%p) refcount=%u\n", iface
, ref
);
1066 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1068 IStream_Release(This
->stream
);
1069 HeapFree(GetProcessHeap(), 0, This
);
1075 static void user_write_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
1077 PngEncoder
*This
= ppng_get_io_ptr(png_ptr
);
1081 hr
= IStream_Write(This
->stream
, data
, length
, &byteswritten
);
1082 if (FAILED(hr
) || byteswritten
!= length
)
1084 ppng_error(png_ptr
, "failed writing data");
1088 static void user_flush(png_structp png_ptr
)
1092 static HRESULT WINAPI
PngEncoder_Initialize(IWICBitmapEncoder
*iface
,
1093 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1095 PngEncoder
*This
= (PngEncoder
*)iface
;
1097 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1100 return WINCODEC_ERR_WRONGSTATE
;
1102 /* initialize libpng */
1103 This
->png_ptr
= ppng_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
1107 This
->info_ptr
= ppng_create_info_struct(This
->png_ptr
);
1108 if (!This
->info_ptr
)
1110 ppng_destroy_write_struct(&This
->png_ptr
, NULL
);
1111 This
->png_ptr
= NULL
;
1115 IStream_AddRef(pIStream
);
1116 This
->stream
= pIStream
;
1118 /* set up setjmp/longjmp error handling */
1119 if (setjmp(png_jmpbuf(This
->png_ptr
)))
1121 ppng_destroy_write_struct(&This
->png_ptr
, &This
->info_ptr
);
1122 This
->png_ptr
= NULL
;
1123 IStream_Release(This
->stream
);
1124 This
->stream
= NULL
;
1128 /* set up custom i/o handling */
1129 ppng_set_write_fn(This
->png_ptr
, This
, user_write_data
, user_flush
);
1134 static HRESULT WINAPI
PngEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1135 GUID
*pguidContainerFormat
)
1137 FIXME("(%p,%s): stub\n", iface
, debugstr_guid(pguidContainerFormat
));
1141 static HRESULT WINAPI
PngEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1142 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1144 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1148 static HRESULT WINAPI
PngEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1149 UINT cCount
, IWICColorContext
**ppIColorContext
)
1151 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1155 static HRESULT WINAPI
PngEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1157 TRACE("(%p,%p)\n", iface
, pIPalette
);
1158 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1161 static HRESULT WINAPI
PngEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1163 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1164 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1167 static HRESULT WINAPI
PngEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1169 TRACE("(%p,%p)\n", iface
, pIPreview
);
1170 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1173 static HRESULT WINAPI
PngEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1174 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1176 PngEncoder
*This
= (PngEncoder
*)iface
;
1178 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1180 if (This
->frame_count
!= 0)
1181 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1184 return WINCODEC_ERR_NOTINITIALIZED
;
1186 hr
= CreatePropertyBag2(ppIEncoderOptions
);
1187 if (FAILED(hr
)) return hr
;
1189 This
->frame_count
= 1;
1191 IWICBitmapEncoder_AddRef(iface
);
1192 *ppIFrameEncode
= (IWICBitmapFrameEncode
*)&This
->lpFrameVtbl
;
1197 static HRESULT WINAPI
PngEncoder_Commit(IWICBitmapEncoder
*iface
)
1199 PngEncoder
*This
= (PngEncoder
*)iface
;
1200 TRACE("(%p)\n", iface
);
1202 if (!This
->frame_committed
|| This
->committed
)
1203 return WINCODEC_ERR_WRONGSTATE
;
1205 This
->committed
= TRUE
;
1210 static HRESULT WINAPI
PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
1211 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1213 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
1217 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl
= {
1218 PngEncoder_QueryInterface
,
1221 PngEncoder_Initialize
,
1222 PngEncoder_GetContainerFormat
,
1223 PngEncoder_GetEncoderInfo
,
1224 PngEncoder_SetColorContexts
,
1225 PngEncoder_SetPalette
,
1226 PngEncoder_SetThumbnail
,
1227 PngEncoder_SetPreview
,
1228 PngEncoder_CreateNewFrame
,
1230 PngEncoder_GetMetadataQueryWriter
1233 HRESULT
PngEncoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1238 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
1242 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
1244 if (!libpng_handle
&& !load_libpng())
1246 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG
);
1250 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder
));
1251 if (!This
) return E_OUTOFMEMORY
;
1253 This
->lpVtbl
= &PngEncoder_Vtbl
;
1254 This
->lpFrameVtbl
= &PngEncoder_FrameVtbl
;
1256 This
->png_ptr
= NULL
;
1257 This
->info_ptr
= NULL
;
1258 This
->stream
= NULL
;
1259 This
->frame_count
= 0;
1260 This
->frame_initialized
= FALSE
;
1261 This
->format
= NULL
;
1262 This
->info_written
= FALSE
;
1267 This
->lines_written
= 0;
1268 This
->frame_committed
= FALSE
;
1269 This
->committed
= FALSE
;
1271 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
1272 IUnknown_Release((IUnknown
*)This
);
1277 #else /* !HAVE_PNG_H */
1279 HRESULT
PngDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1281 ERR("Trying to load PNG picture, but PNG supported not compiled in.\n");
1285 HRESULT
PngEncoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1287 ERR("Trying to save PNG picture, but PNG supported not compiled in.\n");