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_progressive_ptr);
60 MAKE_FUNCPTR(png_get_PLTE);
61 MAKE_FUNCPTR(png_get_tRNS);
62 MAKE_FUNCPTR(png_process_data);
63 MAKE_FUNCPTR(png_progressive_combine_row);
64 MAKE_FUNCPTR(png_set_bgr);
65 MAKE_FUNCPTR(png_set_error_fn);
66 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
67 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
69 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8);
71 MAKE_FUNCPTR(png_set_filler);
72 MAKE_FUNCPTR(png_set_gray_to_rgb);
73 MAKE_FUNCPTR(png_set_IHDR);
74 MAKE_FUNCPTR(png_set_pHYs);
75 MAKE_FUNCPTR(png_set_progressive_read_fn);
76 MAKE_FUNCPTR(png_set_read_fn);
77 MAKE_FUNCPTR(png_set_strip_16);
78 MAKE_FUNCPTR(png_set_tRNS_to_alpha);
79 MAKE_FUNCPTR(png_set_write_fn);
80 MAKE_FUNCPTR(png_read_end);
81 MAKE_FUNCPTR(png_read_image);
82 MAKE_FUNCPTR(png_read_info);
83 MAKE_FUNCPTR(png_read_update_info);
84 MAKE_FUNCPTR(png_write_end);
85 MAKE_FUNCPTR(png_write_info);
86 MAKE_FUNCPTR(png_write_rows);
89 static void *load_libpng(void)
91 if((libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
93 #define LOAD_FUNCPTR(f) \
94 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
95 libpng_handle = NULL; \
98 LOAD_FUNCPTR(png_create_read_struct);
99 LOAD_FUNCPTR(png_create_info_struct);
100 LOAD_FUNCPTR(png_create_write_struct);
101 LOAD_FUNCPTR(png_destroy_read_struct);
102 LOAD_FUNCPTR(png_destroy_write_struct);
103 LOAD_FUNCPTR(png_error);
104 LOAD_FUNCPTR(png_get_bit_depth);
105 LOAD_FUNCPTR(png_get_color_type);
106 LOAD_FUNCPTR(png_get_error_ptr);
107 LOAD_FUNCPTR(png_get_image_height);
108 LOAD_FUNCPTR(png_get_image_width);
109 LOAD_FUNCPTR(png_get_io_ptr);
110 LOAD_FUNCPTR(png_get_pHYs);
111 LOAD_FUNCPTR(png_get_progressive_ptr);
112 LOAD_FUNCPTR(png_get_PLTE);
113 LOAD_FUNCPTR(png_get_tRNS);
114 LOAD_FUNCPTR(png_process_data);
115 LOAD_FUNCPTR(png_progressive_combine_row);
116 LOAD_FUNCPTR(png_set_bgr);
117 LOAD_FUNCPTR(png_set_error_fn);
118 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
119 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
121 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8);
123 LOAD_FUNCPTR(png_set_filler);
124 LOAD_FUNCPTR(png_set_gray_to_rgb);
125 LOAD_FUNCPTR(png_set_IHDR);
126 LOAD_FUNCPTR(png_set_pHYs);
127 LOAD_FUNCPTR(png_set_progressive_read_fn);
128 LOAD_FUNCPTR(png_set_read_fn);
129 LOAD_FUNCPTR(png_set_strip_16);
130 LOAD_FUNCPTR(png_set_tRNS_to_alpha);
131 LOAD_FUNCPTR(png_set_write_fn);
132 LOAD_FUNCPTR(png_read_end);
133 LOAD_FUNCPTR(png_read_image);
134 LOAD_FUNCPTR(png_read_info);
135 LOAD_FUNCPTR(png_read_update_info);
136 LOAD_FUNCPTR(png_write_end);
137 LOAD_FUNCPTR(png_write_info);
138 LOAD_FUNCPTR(png_write_rows);
142 return libpng_handle;
145 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
149 /* This uses setjmp/longjmp just like the default. We can't use the
150 * default because there's no way to access the jmp buffer in the png_struct
151 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
152 WARN("PNG error: %s\n", debugstr_a(error_message));
153 pjmpbuf = ppng_get_error_ptr(png_ptr);
154 longjmp(*pjmpbuf, 1);
157 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
159 WARN("PNG warning: %s\n", debugstr_a(warning_message));
163 IWICBitmapDecoder IWICBitmapDecoder_iface;
164 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
172 const WICPixelFormatGUID *format;
174 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
177 static inline PngDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
179 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapDecoder_iface);
182 static inline PngDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
184 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapFrameDecode_iface);
187 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
189 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
192 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
193 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
195 if (!ppv) return E_INVALIDARG;
197 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
204 return E_NOINTERFACE;
207 IUnknown_AddRef((IUnknown*)*ppv);
211 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
213 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
214 ULONG ref = InterlockedIncrement(&This->ref);
216 TRACE("(%p) refcount=%u\n", iface, ref);
221 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
223 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
224 ULONG ref = InterlockedDecrement(&This->ref);
226 TRACE("(%p) refcount=%u\n", iface, ref);
231 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
232 This->lock.DebugInfo->Spare[0] = 0;
233 DeleteCriticalSection(&This->lock);
234 HeapFree(GetProcessHeap(), 0, This->image_bits);
235 HeapFree(GetProcessHeap(), 0, This);
241 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
242 DWORD *pdwCapability)
244 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
248 static void info_callback(png_structp png_ptr, png_infop info)
250 int color_type, bit_depth;
253 png_uint_32 transparency;
254 png_color_16p trans_values;
259 This = (PngDecoder*) ppng_get_progressive_ptr(png_ptr);
261 /* choose a pixel format */
262 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
263 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
265 /* check for color-keyed alpha */
266 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
268 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
271 if (color_type == PNG_COLOR_TYPE_GRAY)
275 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
276 ppng_set_expand_gray_1_2_4_to_8(This->png_ptr);
278 ppng_set_gray_1_2_4_to_8(This->png_ptr);
282 ppng_set_gray_to_rgb(This->png_ptr);
284 ppng_set_tRNS_to_alpha(This->png_ptr);
285 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
290 case PNG_COLOR_TYPE_GRAY:
291 This->bpp = bit_depth;
294 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
295 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
296 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
297 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
298 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
300 ERR("invalid grayscale bit depth: %i\n", bit_depth);
305 case PNG_COLOR_TYPE_GRAY_ALPHA:
306 /* WIC does not support grayscale alpha formats so use RGBA */
307 ppng_set_gray_to_rgb(This->png_ptr);
308 case PNG_COLOR_TYPE_RGB_ALPHA:
309 This->bpp = bit_depth * 4;
313 ppng_set_bgr(This->png_ptr);
314 This->format = &GUID_WICPixelFormat32bppBGRA;
316 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
318 ERR("invalid RGBA bit depth: %i\n", bit_depth);
323 case PNG_COLOR_TYPE_PALETTE:
324 This->bpp = bit_depth;
327 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
328 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
329 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
330 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
332 ERR("invalid indexed color bit depth: %i\n", bit_depth);
337 case PNG_COLOR_TYPE_RGB:
338 This->bpp = bit_depth * 3;
342 ppng_set_bgr(This->png_ptr);
343 This->format = &GUID_WICPixelFormat24bppBGR;
345 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
347 ERR("invalid RGB color bit depth: %i\n", bit_depth);
353 ERR("invalid color type %i\n", color_type);
358 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
359 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
360 This->stride = This->width * This->bpp;
361 image_size = This->stride * This->height;
363 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
364 if (!This->image_bits)
370 ppng_read_update_info(This->png_ptr, This->info_ptr);
371 This->initialized = TRUE;
375 ERR("PNG info parsing failed, hr=0x%08X\n", hr);
378 static void row_callback(png_structp png_ptr, png_bytep new_row,
379 png_uint_32 row_num, int pass)
384 This = (PngDecoder*) ppng_get_progressive_ptr(png_ptr);
385 if (!This->initialized)
388 old_row = ((png_bytep)This->image_bits) + row_num*This->stride;
389 ppng_progressive_combine_row(png_ptr, old_row, new_row);
392 static void end_callback(png_structp png_ptr, png_infop info)
396 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
397 WICDecodeOptions cacheOptions)
399 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
402 png_bytep *row_pointers=NULL;
407 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
409 EnterCriticalSection(&This->lock);
411 /* initialize libpng */
412 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
419 This->info_ptr = ppng_create_info_struct(This->png_ptr);
422 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
423 This->png_ptr = NULL;
428 /* set up setjmp/longjmp error handling */
431 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
432 HeapFree(GetProcessHeap(), 0, row_pointers);
433 This->png_ptr = NULL;
437 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
439 /* seek to the start of the stream */
441 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
442 if (FAILED(hr)) goto end;
444 /* set up progressive loading */
445 ppng_set_progressive_read_fn(This->png_ptr, (void*)This,
446 info_callback, row_callback, end_callback);
450 hr = IStream_Read(pIStream, buffer, sizeof(buffer), &bytesRead);
452 ppng_process_data(This->png_ptr, This->info_ptr, buffer, bytesRead);
453 } while (bytesRead == sizeof(buffer));
457 ERR("reading PNG file failed, error 0x%08X\n", hr);
458 if (!This->initialized)
463 LeaveCriticalSection(&This->lock);
468 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
469 GUID *pguidContainerFormat)
471 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
475 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
476 IWICBitmapDecoderInfo **ppIDecoderInfo)
478 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
482 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
483 IWICPalette *pIPalette)
485 FIXME("(%p,%p): stub\n", iface, pIPalette);
489 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
490 IWICMetadataQueryReader **ppIMetadataQueryReader)
492 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
496 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
497 IWICBitmapSource **ppIBitmapSource)
499 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
503 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
504 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
506 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
510 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
511 IWICBitmapSource **ppIThumbnail)
513 TRACE("(%p,%p)\n", iface, ppIThumbnail);
514 return WINCODEC_ERR_CODECNOTHUMBNAIL;
517 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
524 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
525 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
527 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
528 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
530 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
532 if (index != 0) return E_INVALIDARG;
534 IWICBitmapDecoder_AddRef(iface);
536 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
541 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
542 PngDecoder_QueryInterface,
545 PngDecoder_QueryCapability,
546 PngDecoder_Initialize,
547 PngDecoder_GetContainerFormat,
548 PngDecoder_GetDecoderInfo,
549 PngDecoder_CopyPalette,
550 PngDecoder_GetMetadataQueryReader,
551 PngDecoder_GetPreview,
552 PngDecoder_GetColorContexts,
553 PngDecoder_GetThumbnail,
554 PngDecoder_GetFrameCount,
558 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
561 if (!ppv) return E_INVALIDARG;
563 if (IsEqualIID(&IID_IUnknown, iid) ||
564 IsEqualIID(&IID_IWICBitmapSource, iid) ||
565 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
572 return E_NOINTERFACE;
575 IUnknown_AddRef((IUnknown*)*ppv);
579 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
581 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
582 return IUnknown_AddRef((IUnknown*)This);
585 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
587 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
588 return IUnknown_Release((IUnknown*)This);
591 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
592 UINT *puiWidth, UINT *puiHeight)
594 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
595 *puiWidth = This->width;
596 *puiHeight = This->height;
597 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
601 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
602 WICPixelFormatGUID *pPixelFormat)
604 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
605 TRACE("(%p,%p)\n", iface, pPixelFormat);
607 memcpy(pPixelFormat, This->format, sizeof(GUID));
612 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
613 double *pDpiX, double *pDpiY)
615 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
616 png_uint_32 ret, xres, yres;
619 EnterCriticalSection(&This->lock);
621 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
623 if (ret && unit_type == PNG_RESOLUTION_METER)
625 *pDpiX = xres * 0.0254;
626 *pDpiY = yres * 0.0254;
630 WARN("no pHYs block present\n");
631 *pDpiX = *pDpiY = 96.0;
634 LeaveCriticalSection(&This->lock);
636 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
641 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
642 IWICPalette *pIPalette)
644 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
646 png_colorp png_palette;
648 WICColor palette[256];
651 png_color_16p trans_values;
655 TRACE("(%p,%p)\n", iface, pIPalette);
657 EnterCriticalSection(&This->lock);
659 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
662 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
666 if (num_palette > 256)
668 ERR("palette has %i colors?!\n", num_palette);
673 for (i=0; i<num_palette; i++)
675 palette[i] = (0xff000000|
676 png_palette[i].red << 16|
677 png_palette[i].green << 8|
678 png_palette[i].blue);
681 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
684 for (i=0; i<num_trans; i++)
686 palette[trans[i]] = 0x00000000;
692 LeaveCriticalSection(&This->lock);
695 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
700 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
701 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
703 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
704 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
706 return copy_pixels(This->bpp, This->image_bits,
707 This->width, This->height, This->stride,
708 prc, cbStride, cbBufferSize, pbBuffer);
711 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
712 IWICMetadataQueryReader **ppIMetadataQueryReader)
714 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
718 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
719 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
721 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
725 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
726 IWICBitmapSource **ppIThumbnail)
728 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
732 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
733 PngDecoder_Frame_QueryInterface,
734 PngDecoder_Frame_AddRef,
735 PngDecoder_Frame_Release,
736 PngDecoder_Frame_GetSize,
737 PngDecoder_Frame_GetPixelFormat,
738 PngDecoder_Frame_GetResolution,
739 PngDecoder_Frame_CopyPalette,
740 PngDecoder_Frame_CopyPixels,
741 PngDecoder_Frame_GetMetadataQueryReader,
742 PngDecoder_Frame_GetColorContexts,
743 PngDecoder_Frame_GetThumbnail
746 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
751 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
755 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
757 if (!libpng_handle && !load_libpng())
759 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
763 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
764 if (!This) return E_OUTOFMEMORY;
766 This->IWICBitmapDecoder_iface.lpVtbl = &PngDecoder_Vtbl;
767 This->IWICBitmapFrameDecode_iface.lpVtbl = &PngDecoder_FrameVtbl;
769 This->png_ptr = NULL;
770 This->info_ptr = NULL;
771 This->initialized = FALSE;
772 This->image_bits = NULL;
773 InitializeCriticalSection(&This->lock);
774 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngDecoder.lock");
776 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
777 IUnknown_Release((IUnknown*)This);
782 struct png_pixelformat {
783 const WICPixelFormatGUID *guid;
791 static const struct png_pixelformat formats[] = {
792 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
793 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
794 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
795 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
796 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
797 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
798 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
799 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
800 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
801 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
805 typedef struct PngEncoder {
806 const IWICBitmapEncoderVtbl *lpVtbl;
807 const IWICBitmapFrameEncodeVtbl *lpFrameVtbl;
813 BOOL frame_initialized;
814 const struct png_pixelformat *format;
819 BOOL frame_committed;
821 CRITICAL_SECTION lock;
824 static inline PngEncoder *encoder_from_frame(IWICBitmapFrameEncode *iface)
826 return CONTAINING_RECORD(iface, PngEncoder, lpFrameVtbl);
829 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
832 PngEncoder *This = encoder_from_frame(iface);
833 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
835 if (!ppv) return E_INVALIDARG;
837 if (IsEqualIID(&IID_IUnknown, iid) ||
838 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
840 *ppv = &This->lpFrameVtbl;
845 return E_NOINTERFACE;
848 IUnknown_AddRef((IUnknown*)*ppv);
852 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
854 PngEncoder *This = encoder_from_frame(iface);
855 return IUnknown_AddRef((IUnknown*)This);
858 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
860 PngEncoder *This = encoder_from_frame(iface);
861 return IUnknown_Release((IUnknown*)This);
864 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
865 IPropertyBag2 *pIEncoderOptions)
867 PngEncoder *This = encoder_from_frame(iface);
868 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
870 EnterCriticalSection(&This->lock);
872 if (This->frame_initialized)
874 LeaveCriticalSection(&This->lock);
875 return WINCODEC_ERR_WRONGSTATE;
878 This->frame_initialized = TRUE;
880 LeaveCriticalSection(&This->lock);
885 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
886 UINT uiWidth, UINT uiHeight)
888 PngEncoder *This = encoder_from_frame(iface);
889 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
891 EnterCriticalSection(&This->lock);
893 if (!This->frame_initialized || This->info_written)
895 LeaveCriticalSection(&This->lock);
896 return WINCODEC_ERR_WRONGSTATE;
899 This->width = uiWidth;
900 This->height = uiHeight;
902 LeaveCriticalSection(&This->lock);
907 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
908 double dpiX, double dpiY)
910 PngEncoder *This = encoder_from_frame(iface);
911 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
913 EnterCriticalSection(&This->lock);
915 if (!This->frame_initialized || This->info_written)
917 LeaveCriticalSection(&This->lock);
918 return WINCODEC_ERR_WRONGSTATE;
924 LeaveCriticalSection(&This->lock);
929 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
930 WICPixelFormatGUID *pPixelFormat)
932 PngEncoder *This = encoder_from_frame(iface);
934 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
936 EnterCriticalSection(&This->lock);
938 if (!This->frame_initialized || This->info_written)
940 LeaveCriticalSection(&This->lock);
941 return WINCODEC_ERR_WRONGSTATE;
944 for (i=0; formats[i].guid; i++)
946 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
950 if (!formats[i].guid) i = 0;
952 This->format = &formats[i];
953 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
955 LeaveCriticalSection(&This->lock);
960 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
961 UINT cCount, IWICColorContext **ppIColorContext)
963 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
967 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
968 IWICPalette *pIPalette)
970 FIXME("(%p,%p): stub\n", iface, pIPalette);
971 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
974 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
975 IWICBitmapSource *pIThumbnail)
977 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
978 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
981 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
982 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
984 PngEncoder *This = encoder_from_frame(iface);
985 png_byte **row_pointers=NULL;
988 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
990 EnterCriticalSection(&This->lock);
992 if (!This->frame_initialized || !This->width || !This->height || !This->format)
994 LeaveCriticalSection(&This->lock);
995 return WINCODEC_ERR_WRONGSTATE;
998 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1000 LeaveCriticalSection(&This->lock);
1001 return E_INVALIDARG;
1004 /* set up setjmp/longjmp error handling */
1007 LeaveCriticalSection(&This->lock);
1008 HeapFree(GetProcessHeap(), 0, row_pointers);
1011 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
1013 if (!This->info_written)
1015 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
1016 This->format->bit_depth, This->format->color_type, PNG_INTERLACE_NONE,
1017 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1019 if (This->xres != 0.0 && This->yres != 0.0)
1021 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
1022 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
1025 ppng_write_info(This->png_ptr, This->info_ptr);
1027 if (This->format->remove_filler)
1028 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1030 if (This->format->swap_rgb)
1031 ppng_set_bgr(This->png_ptr);
1033 This->info_written = TRUE;
1036 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1039 LeaveCriticalSection(&This->lock);
1040 return E_OUTOFMEMORY;
1043 for (i=0; i<lineCount; i++)
1044 row_pointers[i] = pbPixels + cbStride * i;
1046 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1047 This->lines_written += lineCount;
1049 LeaveCriticalSection(&This->lock);
1051 HeapFree(GetProcessHeap(), 0, row_pointers);
1056 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1057 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1059 PngEncoder *This = encoder_from_frame(iface);
1062 WICPixelFormatGUID guid;
1065 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1067 if (!This->frame_initialized || !This->width || !This->height)
1068 return WINCODEC_ERR_WRONGSTATE;
1072 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1073 if (FAILED(hr)) return hr;
1074 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1075 if (FAILED(hr)) return hr;
1078 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1079 if (FAILED(hr)) return hr;
1080 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1082 /* FIXME: should use WICConvertBitmapSource to convert */
1083 ERR("format %s unsupported\n", debugstr_guid(&guid));
1087 if (This->xres == 0.0 || This->yres == 0.0)
1090 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1091 if (FAILED(hr)) return hr;
1092 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1093 if (FAILED(hr)) return hr;
1099 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1100 if (FAILED(hr)) return hr;
1108 if (prc->Width != This->width) return E_INVALIDARG;
1110 stride = (This->format->bpp * This->width + 7)/8;
1112 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1113 if (!pixeldata) return E_OUTOFMEMORY;
1115 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1116 stride*prc->Height, pixeldata);
1120 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1121 stride*prc->Height, pixeldata);
1124 HeapFree(GetProcessHeap(), 0, pixeldata);
1129 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1131 PngEncoder *This = encoder_from_frame(iface);
1133 TRACE("(%p)\n", iface);
1135 EnterCriticalSection(&This->lock);
1137 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1139 LeaveCriticalSection(&This->lock);
1140 return WINCODEC_ERR_WRONGSTATE;
1143 /* set up setjmp/longjmp error handling */
1146 LeaveCriticalSection(&This->lock);
1149 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
1151 ppng_write_end(This->png_ptr, This->info_ptr);
1153 This->frame_committed = TRUE;
1155 LeaveCriticalSection(&This->lock);
1160 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1161 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1163 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1167 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1168 PngFrameEncode_QueryInterface,
1169 PngFrameEncode_AddRef,
1170 PngFrameEncode_Release,
1171 PngFrameEncode_Initialize,
1172 PngFrameEncode_SetSize,
1173 PngFrameEncode_SetResolution,
1174 PngFrameEncode_SetPixelFormat,
1175 PngFrameEncode_SetColorContexts,
1176 PngFrameEncode_SetPalette,
1177 PngFrameEncode_SetThumbnail,
1178 PngFrameEncode_WritePixels,
1179 PngFrameEncode_WriteSource,
1180 PngFrameEncode_Commit,
1181 PngFrameEncode_GetMetadataQueryWriter
1184 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1187 PngEncoder *This = (PngEncoder*)iface;
1188 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1190 if (!ppv) return E_INVALIDARG;
1192 if (IsEqualIID(&IID_IUnknown, iid) ||
1193 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1200 return E_NOINTERFACE;
1203 IUnknown_AddRef((IUnknown*)*ppv);
1207 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1209 PngEncoder *This = (PngEncoder*)iface;
1210 ULONG ref = InterlockedIncrement(&This->ref);
1212 TRACE("(%p) refcount=%u\n", iface, ref);
1217 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1219 PngEncoder *This = (PngEncoder*)iface;
1220 ULONG ref = InterlockedDecrement(&This->ref);
1222 TRACE("(%p) refcount=%u\n", iface, ref);
1226 This->lock.DebugInfo->Spare[0] = 0;
1227 DeleteCriticalSection(&This->lock);
1229 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1231 IStream_Release(This->stream);
1232 HeapFree(GetProcessHeap(), 0, This);
1238 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1240 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1244 hr = IStream_Write(This->stream, data, length, &byteswritten);
1245 if (FAILED(hr) || byteswritten != length)
1247 ppng_error(png_ptr, "failed writing data");
1251 static void user_flush(png_structp png_ptr)
1255 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1256 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1258 PngEncoder *This = (PngEncoder*)iface;
1261 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1263 EnterCriticalSection(&This->lock);
1267 LeaveCriticalSection(&This->lock);
1268 return WINCODEC_ERR_WRONGSTATE;
1271 /* initialize libpng */
1272 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1275 LeaveCriticalSection(&This->lock);
1279 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1280 if (!This->info_ptr)
1282 ppng_destroy_write_struct(&This->png_ptr, NULL);
1283 This->png_ptr = NULL;
1284 LeaveCriticalSection(&This->lock);
1288 IStream_AddRef(pIStream);
1289 This->stream = pIStream;
1291 /* set up setjmp/longjmp error handling */
1294 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1295 This->png_ptr = NULL;
1296 IStream_Release(This->stream);
1297 This->stream = NULL;
1298 LeaveCriticalSection(&This->lock);
1301 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
1303 /* set up custom i/o handling */
1304 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1306 LeaveCriticalSection(&This->lock);
1311 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1312 GUID *pguidContainerFormat)
1314 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1318 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1319 IWICBitmapEncoderInfo **ppIEncoderInfo)
1321 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1325 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1326 UINT cCount, IWICColorContext **ppIColorContext)
1328 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1332 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1334 TRACE("(%p,%p)\n", iface, pIPalette);
1335 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1338 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1340 TRACE("(%p,%p)\n", iface, pIThumbnail);
1341 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1344 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1346 TRACE("(%p,%p)\n", iface, pIPreview);
1347 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1350 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1351 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1353 PngEncoder *This = (PngEncoder*)iface;
1355 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1357 EnterCriticalSection(&This->lock);
1359 if (This->frame_count != 0)
1361 LeaveCriticalSection(&This->lock);
1362 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1367 LeaveCriticalSection(&This->lock);
1368 return WINCODEC_ERR_NOTINITIALIZED;
1371 hr = CreatePropertyBag2(ppIEncoderOptions);
1374 LeaveCriticalSection(&This->lock);
1378 This->frame_count = 1;
1380 LeaveCriticalSection(&This->lock);
1382 IWICBitmapEncoder_AddRef(iface);
1383 *ppIFrameEncode = (IWICBitmapFrameEncode*)&This->lpFrameVtbl;
1388 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1390 PngEncoder *This = (PngEncoder*)iface;
1391 TRACE("(%p)\n", iface);
1393 EnterCriticalSection(&This->lock);
1395 if (!This->frame_committed || This->committed)
1397 LeaveCriticalSection(&This->lock);
1398 return WINCODEC_ERR_WRONGSTATE;
1401 This->committed = TRUE;
1403 LeaveCriticalSection(&This->lock);
1408 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1409 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1411 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1415 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
1416 PngEncoder_QueryInterface,
1419 PngEncoder_Initialize,
1420 PngEncoder_GetContainerFormat,
1421 PngEncoder_GetEncoderInfo,
1422 PngEncoder_SetColorContexts,
1423 PngEncoder_SetPalette,
1424 PngEncoder_SetThumbnail,
1425 PngEncoder_SetPreview,
1426 PngEncoder_CreateNewFrame,
1428 PngEncoder_GetMetadataQueryWriter
1431 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1436 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1440 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1442 if (!libpng_handle && !load_libpng())
1444 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1448 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1449 if (!This) return E_OUTOFMEMORY;
1451 This->lpVtbl = &PngEncoder_Vtbl;
1452 This->lpFrameVtbl = &PngEncoder_FrameVtbl;
1454 This->png_ptr = NULL;
1455 This->info_ptr = NULL;
1456 This->stream = NULL;
1457 This->frame_count = 0;
1458 This->frame_initialized = FALSE;
1459 This->format = NULL;
1460 This->info_written = FALSE;
1465 This->lines_written = 0;
1466 This->frame_committed = FALSE;
1467 This->committed = FALSE;
1468 InitializeCriticalSection(&This->lock);
1469 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngEncoder.lock");
1471 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1472 IUnknown_Release((IUnknown*)This);
1477 #else /* !HAVE_PNG_H */
1479 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1481 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1485 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1487 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");