2 * Copyright 2009 Vincent Povirk for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
28 #define NONAMELESSUNION
35 #include "wincodecsdk.h"
37 #include "wincodecs_private.h"
39 #include "wine/debug.h"
40 #include "wine/library.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
44 static HRESULT read_png_chunk(IStream *stream, BYTE *type, BYTE **data, ULONG *data_size)
50 hr = IStream_Read(stream, header, 8, &bytesread);
51 if (FAILED(hr) || bytesread < 8)
58 *data_size = header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3];
60 memcpy(type, &header[4], 4);
64 *data = HeapAlloc(GetProcessHeap(), 0, *data_size);
68 hr = IStream_Read(stream, *data, *data_size, &bytesread);
70 if (FAILED(hr) || bytesread < *data_size)
74 HeapFree(GetProcessHeap(), 0, *data);
79 /* FIXME: Verify the CRC? */
85 static HRESULT LoadTextMetadata(IStream *stream, const GUID *preferred_vendor,
86 DWORD persist_options, MetadataItem **items, DWORD *item_count)
92 ULONG name_len, value_len;
97 hr = read_png_chunk(stream, type, &data, &data_size);
98 if (FAILED(hr)) return hr;
100 name_end_ptr = memchr(data, 0, data_size);
102 name_len = name_end_ptr - data;
104 if (!name_end_ptr || name_len > 79)
106 HeapFree(GetProcessHeap(), 0, data);
110 value_len = data_size - name_len - 1;
112 result = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem));
113 name = HeapAlloc(GetProcessHeap(), 0, name_len + 1);
114 value = HeapAlloc(GetProcessHeap(), 0, value_len + 1);
115 if (!result || !name || !value)
117 HeapFree(GetProcessHeap(), 0, data);
118 HeapFree(GetProcessHeap(), 0, result);
119 HeapFree(GetProcessHeap(), 0, name);
120 HeapFree(GetProcessHeap(), 0, value);
121 return E_OUTOFMEMORY;
124 PropVariantInit(&result[0].schema);
125 PropVariantInit(&result[0].id);
126 PropVariantInit(&result[0].value);
128 memcpy(name, data, name_len + 1);
129 memcpy(value, name_end_ptr + 1, value_len);
130 value[value_len] = 0;
132 result[0].id.vt = VT_LPSTR;
133 result[0].id.u.pszVal = name;
134 result[0].value.vt = VT_LPSTR;
135 result[0].value.u.pszVal = value;
140 HeapFree(GetProcessHeap(), 0, data);
145 static const MetadataHandlerVtbl TextReader_Vtbl = {
147 &CLSID_WICPngTextMetadataReader,
151 HRESULT PngTextReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
153 return MetadataReader_Create(&TextReader_Vtbl, pUnkOuter, iid, ppv);
158 static void *libpng_handle;
159 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
160 MAKE_FUNCPTR(png_create_read_struct);
161 MAKE_FUNCPTR(png_create_info_struct);
162 MAKE_FUNCPTR(png_create_write_struct);
163 MAKE_FUNCPTR(png_destroy_read_struct);
164 MAKE_FUNCPTR(png_destroy_write_struct);
165 MAKE_FUNCPTR(png_error);
166 MAKE_FUNCPTR(png_get_bit_depth);
167 MAKE_FUNCPTR(png_get_color_type);
168 MAKE_FUNCPTR(png_get_error_ptr);
169 MAKE_FUNCPTR(png_get_iCCP);
170 MAKE_FUNCPTR(png_get_image_height);
171 MAKE_FUNCPTR(png_get_image_width);
172 MAKE_FUNCPTR(png_get_io_ptr);
173 MAKE_FUNCPTR(png_get_pHYs);
174 MAKE_FUNCPTR(png_get_PLTE);
175 MAKE_FUNCPTR(png_get_tRNS);
176 MAKE_FUNCPTR(png_set_bgr);
177 MAKE_FUNCPTR(png_set_error_fn);
178 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
179 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
181 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8);
183 MAKE_FUNCPTR(png_set_filler);
184 MAKE_FUNCPTR(png_set_gray_to_rgb);
185 MAKE_FUNCPTR(png_set_IHDR);
186 MAKE_FUNCPTR(png_set_pHYs);
187 MAKE_FUNCPTR(png_set_read_fn);
188 MAKE_FUNCPTR(png_set_strip_16);
189 MAKE_FUNCPTR(png_set_tRNS_to_alpha);
190 MAKE_FUNCPTR(png_set_write_fn);
191 MAKE_FUNCPTR(png_read_end);
192 MAKE_FUNCPTR(png_read_image);
193 MAKE_FUNCPTR(png_read_info);
194 MAKE_FUNCPTR(png_write_end);
195 MAKE_FUNCPTR(png_write_info);
196 MAKE_FUNCPTR(png_write_rows);
199 static void *load_libpng(void)
201 if((libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
203 #define LOAD_FUNCPTR(f) \
204 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
205 libpng_handle = NULL; \
208 LOAD_FUNCPTR(png_create_read_struct);
209 LOAD_FUNCPTR(png_create_info_struct);
210 LOAD_FUNCPTR(png_create_write_struct);
211 LOAD_FUNCPTR(png_destroy_read_struct);
212 LOAD_FUNCPTR(png_destroy_write_struct);
213 LOAD_FUNCPTR(png_error);
214 LOAD_FUNCPTR(png_get_bit_depth);
215 LOAD_FUNCPTR(png_get_color_type);
216 LOAD_FUNCPTR(png_get_error_ptr);
217 LOAD_FUNCPTR(png_get_iCCP);
218 LOAD_FUNCPTR(png_get_image_height);
219 LOAD_FUNCPTR(png_get_image_width);
220 LOAD_FUNCPTR(png_get_io_ptr);
221 LOAD_FUNCPTR(png_get_pHYs);
222 LOAD_FUNCPTR(png_get_PLTE);
223 LOAD_FUNCPTR(png_get_tRNS);
224 LOAD_FUNCPTR(png_set_bgr);
225 LOAD_FUNCPTR(png_set_error_fn);
226 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
227 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
229 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8);
231 LOAD_FUNCPTR(png_set_filler);
232 LOAD_FUNCPTR(png_set_gray_to_rgb);
233 LOAD_FUNCPTR(png_set_IHDR);
234 LOAD_FUNCPTR(png_set_pHYs);
235 LOAD_FUNCPTR(png_set_read_fn);
236 LOAD_FUNCPTR(png_set_strip_16);
237 LOAD_FUNCPTR(png_set_tRNS_to_alpha);
238 LOAD_FUNCPTR(png_set_write_fn);
239 LOAD_FUNCPTR(png_read_end);
240 LOAD_FUNCPTR(png_read_image);
241 LOAD_FUNCPTR(png_read_info);
242 LOAD_FUNCPTR(png_write_end);
243 LOAD_FUNCPTR(png_write_info);
244 LOAD_FUNCPTR(png_write_rows);
248 return libpng_handle;
251 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
255 /* This uses setjmp/longjmp just like the default. We can't use the
256 * default because there's no way to access the jmp buffer in the png_struct
257 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
258 WARN("PNG error: %s\n", debugstr_a(error_message));
259 pjmpbuf = ppng_get_error_ptr(png_ptr);
260 longjmp(*pjmpbuf, 1);
263 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
265 WARN("PNG warning: %s\n", debugstr_a(warning_message));
269 IWICBitmapDecoder IWICBitmapDecoder_iface;
270 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
271 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
280 const WICPixelFormatGUID *format;
282 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
285 static inline PngDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
287 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapDecoder_iface);
290 static inline PngDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
292 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapFrameDecode_iface);
295 static inline PngDecoder *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
297 return CONTAINING_RECORD(iface, PngDecoder, IWICMetadataBlockReader_iface);
300 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
302 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
305 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
306 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
308 if (!ppv) return E_INVALIDARG;
310 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
312 *ppv = &This->IWICBitmapDecoder_iface;
317 return E_NOINTERFACE;
320 IUnknown_AddRef((IUnknown*)*ppv);
324 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
326 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
327 ULONG ref = InterlockedIncrement(&This->ref);
329 TRACE("(%p) refcount=%u\n", iface, ref);
334 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
336 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
337 ULONG ref = InterlockedDecrement(&This->ref);
339 TRACE("(%p) refcount=%u\n", iface, ref);
344 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
345 This->lock.DebugInfo->Spare[0] = 0;
346 DeleteCriticalSection(&This->lock);
347 HeapFree(GetProcessHeap(), 0, This->image_bits);
348 HeapFree(GetProcessHeap(), 0, This);
354 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
359 TRACE("(%p,%p,%p)\n", iface, stream, capability);
361 if (!stream || !capability) return E_INVALIDARG;
363 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
364 if (hr != S_OK) return hr;
366 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
367 WICBitmapDecoderCapabilityCanDecodeSomeImages;
368 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
372 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
374 IStream *stream = ppng_get_io_ptr(png_ptr);
378 hr = IStream_Read(stream, data, length, &bytesread);
379 if (FAILED(hr) || bytesread != length)
381 ppng_error(png_ptr, "failed reading data");
385 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
386 WICDecodeOptions cacheOptions)
388 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
391 png_bytep *row_pointers=NULL;
394 int color_type, bit_depth;
397 png_uint_32 transparency;
398 png_color_16p trans_values;
401 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
403 EnterCriticalSection(&This->lock);
405 /* initialize libpng */
406 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
413 This->info_ptr = ppng_create_info_struct(This->png_ptr);
416 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
417 This->png_ptr = NULL;
422 This->end_info = ppng_create_info_struct(This->png_ptr);
425 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
426 This->png_ptr = NULL;
431 /* set up setjmp/longjmp error handling */
434 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
435 HeapFree(GetProcessHeap(), 0, row_pointers);
436 This->png_ptr = NULL;
440 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
442 /* seek to the start of the stream */
444 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
445 if (FAILED(hr)) goto end;
447 /* set up custom i/o handling */
448 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
450 /* read the header */
451 ppng_read_info(This->png_ptr, This->info_ptr);
453 /* choose a pixel format */
454 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
455 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
457 /* check for color-keyed alpha */
458 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
460 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
463 if (color_type == PNG_COLOR_TYPE_GRAY)
467 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
468 ppng_set_expand_gray_1_2_4_to_8(This->png_ptr);
470 ppng_set_gray_1_2_4_to_8(This->png_ptr);
474 ppng_set_gray_to_rgb(This->png_ptr);
476 ppng_set_tRNS_to_alpha(This->png_ptr);
477 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
482 case PNG_COLOR_TYPE_GRAY:
483 This->bpp = bit_depth;
486 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
487 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
488 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
489 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
490 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
492 ERR("invalid grayscale bit depth: %i\n", bit_depth);
497 case PNG_COLOR_TYPE_GRAY_ALPHA:
498 /* WIC does not support grayscale alpha formats so use RGBA */
499 ppng_set_gray_to_rgb(This->png_ptr);
501 case PNG_COLOR_TYPE_RGB_ALPHA:
502 This->bpp = bit_depth * 4;
506 ppng_set_bgr(This->png_ptr);
507 This->format = &GUID_WICPixelFormat32bppBGRA;
509 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
511 ERR("invalid RGBA bit depth: %i\n", bit_depth);
516 case PNG_COLOR_TYPE_PALETTE:
517 This->bpp = bit_depth;
520 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
521 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
522 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
523 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
525 ERR("invalid indexed color bit depth: %i\n", bit_depth);
530 case PNG_COLOR_TYPE_RGB:
531 This->bpp = bit_depth * 3;
535 ppng_set_bgr(This->png_ptr);
536 This->format = &GUID_WICPixelFormat24bppBGR;
538 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
540 ERR("invalid RGB color bit depth: %i\n", bit_depth);
546 ERR("invalid color type %i\n", color_type);
551 /* read the image data */
552 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
553 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
554 This->stride = This->width * This->bpp;
555 image_size = This->stride * This->height;
557 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
558 if (!This->image_bits)
564 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
571 for (i=0; i<This->height; i++)
572 row_pointers[i] = This->image_bits + i * This->stride;
574 ppng_read_image(This->png_ptr, row_pointers);
576 HeapFree(GetProcessHeap(), 0, row_pointers);
579 ppng_read_end(This->png_ptr, This->end_info);
581 This->initialized = TRUE;
585 LeaveCriticalSection(&This->lock);
590 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
591 GUID *pguidContainerFormat)
593 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
597 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
598 IWICBitmapDecoderInfo **ppIDecoderInfo)
601 IWICComponentInfo *compinfo;
603 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
605 hr = CreateComponentInfo(&CLSID_WICPngDecoder, &compinfo);
606 if (FAILED(hr)) return hr;
608 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
609 (void**)ppIDecoderInfo);
611 IWICComponentInfo_Release(compinfo);
616 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
617 IWICPalette *pIPalette)
619 FIXME("(%p,%p): stub\n", iface, pIPalette);
623 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
624 IWICMetadataQueryReader **ppIMetadataQueryReader)
626 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
630 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
631 IWICBitmapSource **ppIBitmapSource)
633 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
635 if (!ppIBitmapSource) return E_INVALIDARG;
637 *ppIBitmapSource = NULL;
638 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
641 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
642 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
644 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
645 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
648 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
649 IWICBitmapSource **ppIThumbnail)
651 TRACE("(%p,%p)\n", iface, ppIThumbnail);
653 if (!ppIThumbnail) return E_INVALIDARG;
655 *ppIThumbnail = NULL;
656 return WINCODEC_ERR_CODECNOTHUMBNAIL;
659 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
662 if (!pCount) return E_INVALIDARG;
668 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
669 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
671 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
672 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
674 if (!This->initialized) return WINCODEC_ERR_FRAMEMISSING;
676 if (index != 0) return E_INVALIDARG;
678 IWICBitmapDecoder_AddRef(iface);
680 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
685 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
686 PngDecoder_QueryInterface,
689 PngDecoder_QueryCapability,
690 PngDecoder_Initialize,
691 PngDecoder_GetContainerFormat,
692 PngDecoder_GetDecoderInfo,
693 PngDecoder_CopyPalette,
694 PngDecoder_GetMetadataQueryReader,
695 PngDecoder_GetPreview,
696 PngDecoder_GetColorContexts,
697 PngDecoder_GetThumbnail,
698 PngDecoder_GetFrameCount,
702 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
705 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
706 if (!ppv) return E_INVALIDARG;
708 if (IsEqualIID(&IID_IUnknown, iid) ||
709 IsEqualIID(&IID_IWICBitmapSource, iid) ||
710 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
712 *ppv = &This->IWICBitmapFrameDecode_iface;
714 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
716 *ppv = &This->IWICMetadataBlockReader_iface;
721 return E_NOINTERFACE;
724 IUnknown_AddRef((IUnknown*)*ppv);
728 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
730 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
731 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
734 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
736 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
737 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
740 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
741 UINT *puiWidth, UINT *puiHeight)
743 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
744 *puiWidth = This->width;
745 *puiHeight = This->height;
746 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
750 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
751 WICPixelFormatGUID *pPixelFormat)
753 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
754 TRACE("(%p,%p)\n", iface, pPixelFormat);
756 memcpy(pPixelFormat, This->format, sizeof(GUID));
761 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
762 double *pDpiX, double *pDpiY)
764 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
765 png_uint_32 ret, xres, yres;
768 EnterCriticalSection(&This->lock);
770 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
772 if (ret && unit_type == PNG_RESOLUTION_METER)
774 *pDpiX = xres * 0.0254;
775 *pDpiY = yres * 0.0254;
779 WARN("no pHYs block present\n");
780 *pDpiX = *pDpiY = 96.0;
783 LeaveCriticalSection(&This->lock);
785 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
790 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
791 IWICPalette *pIPalette)
793 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
795 png_colorp png_palette;
797 WICColor palette[256];
800 png_color_16p trans_values;
804 TRACE("(%p,%p)\n", iface, pIPalette);
806 EnterCriticalSection(&This->lock);
808 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
811 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
815 if (num_palette > 256)
817 ERR("palette has %i colors?!\n", num_palette);
822 for (i=0; i<num_palette; i++)
824 palette[i] = (0xff000000|
825 png_palette[i].red << 16|
826 png_palette[i].green << 8|
827 png_palette[i].blue);
830 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
833 for (i=0; i<num_trans; i++)
835 palette[trans[i]] = 0x00000000;
841 LeaveCriticalSection(&This->lock);
844 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
849 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
850 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
852 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
853 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
855 return copy_pixels(This->bpp, This->image_bits,
856 This->width, This->height, This->stride,
857 prc, cbStride, cbBufferSize, pbBuffer);
860 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
861 IWICMetadataQueryReader **ppIMetadataQueryReader)
863 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
867 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
868 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
870 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
871 png_charp name, profile;
873 int compression_type;
876 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
878 if (!pcActualCount) return E_INVALIDARG;
880 EnterCriticalSection(&This->lock);
882 if (ppng_get_iCCP(This->png_ptr, This->info_ptr, &name, &compression_type, &profile, &len))
884 if (cCount && ppIColorContexts)
886 hr = IWICColorContext_InitializeFromMemory(*ppIColorContexts, (const BYTE *)profile, len);
889 LeaveCriticalSection(&This->lock);
898 LeaveCriticalSection(&This->lock);
903 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
904 IWICBitmapSource **ppIThumbnail)
906 TRACE("(%p,%p)\n", iface, ppIThumbnail);
908 if (!ppIThumbnail) return E_INVALIDARG;
910 *ppIThumbnail = NULL;
911 return WINCODEC_ERR_CODECNOTHUMBNAIL;
914 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
915 PngDecoder_Frame_QueryInterface,
916 PngDecoder_Frame_AddRef,
917 PngDecoder_Frame_Release,
918 PngDecoder_Frame_GetSize,
919 PngDecoder_Frame_GetPixelFormat,
920 PngDecoder_Frame_GetResolution,
921 PngDecoder_Frame_CopyPalette,
922 PngDecoder_Frame_CopyPixels,
923 PngDecoder_Frame_GetMetadataQueryReader,
924 PngDecoder_Frame_GetColorContexts,
925 PngDecoder_Frame_GetThumbnail
928 static HRESULT WINAPI PngDecoder_Block_QueryInterface(IWICMetadataBlockReader *iface, REFIID iid,
931 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
932 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
935 static ULONG WINAPI PngDecoder_Block_AddRef(IWICMetadataBlockReader *iface)
937 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
938 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
941 static ULONG WINAPI PngDecoder_Block_Release(IWICMetadataBlockReader *iface)
943 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
944 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
947 static HRESULT WINAPI PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
948 GUID *pguidContainerFormat)
950 if (!pguidContainerFormat) return E_INVALIDARG;
951 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
955 static HRESULT WINAPI PngDecoder_Block_GetCount(IWICMetadataBlockReader *iface,
958 FIXME("%p,%p: stub\n", iface, pcCount);
962 static HRESULT WINAPI PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
963 UINT nIndex, IWICMetadataReader **ppIMetadataReader)
965 FIXME("%p,%d,%p\n", iface, nIndex, ppIMetadataReader);
969 static HRESULT WINAPI PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader *iface,
970 IEnumUnknown **ppIEnumMetadata)
972 FIXME("%p,%p\n", iface, ppIEnumMetadata);
976 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl = {
977 PngDecoder_Block_QueryInterface,
978 PngDecoder_Block_AddRef,
979 PngDecoder_Block_Release,
980 PngDecoder_Block_GetContainerFormat,
981 PngDecoder_Block_GetCount,
982 PngDecoder_Block_GetReaderByIndex,
983 PngDecoder_Block_GetEnumerator,
986 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
991 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
995 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
997 if (!libpng_handle && !load_libpng())
999 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
1003 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
1004 if (!This) return E_OUTOFMEMORY;
1006 This->IWICBitmapDecoder_iface.lpVtbl = &PngDecoder_Vtbl;
1007 This->IWICBitmapFrameDecode_iface.lpVtbl = &PngDecoder_FrameVtbl;
1008 This->IWICMetadataBlockReader_iface.lpVtbl = &PngDecoder_BlockVtbl;
1010 This->png_ptr = NULL;
1011 This->info_ptr = NULL;
1012 This->end_info = NULL;
1013 This->initialized = FALSE;
1014 This->image_bits = NULL;
1015 InitializeCriticalSection(&This->lock);
1016 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngDecoder.lock");
1018 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1019 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1024 struct png_pixelformat {
1025 const WICPixelFormatGUID *guid;
1033 static const struct png_pixelformat formats[] = {
1034 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
1035 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
1036 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
1037 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
1038 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
1039 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
1040 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
1041 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
1042 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
1043 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
1047 typedef struct PngEncoder {
1048 IWICBitmapEncoder IWICBitmapEncoder_iface;
1049 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1052 png_structp png_ptr;
1055 BOOL frame_initialized;
1056 const struct png_pixelformat *format;
1061 BOOL frame_committed;
1063 CRITICAL_SECTION lock;
1066 static inline PngEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1068 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapEncoder_iface);
1071 static inline PngEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1073 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapFrameEncode_iface);
1076 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1079 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1080 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1082 if (!ppv) return E_INVALIDARG;
1084 if (IsEqualIID(&IID_IUnknown, iid) ||
1085 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1087 *ppv = &This->IWICBitmapFrameEncode_iface;
1092 return E_NOINTERFACE;
1095 IUnknown_AddRef((IUnknown*)*ppv);
1099 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1101 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1102 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
1105 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
1107 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1108 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1111 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1112 IPropertyBag2 *pIEncoderOptions)
1114 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1115 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1117 EnterCriticalSection(&This->lock);
1119 if (This->frame_initialized)
1121 LeaveCriticalSection(&This->lock);
1122 return WINCODEC_ERR_WRONGSTATE;
1125 This->frame_initialized = TRUE;
1127 LeaveCriticalSection(&This->lock);
1132 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1133 UINT uiWidth, UINT uiHeight)
1135 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1136 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1138 EnterCriticalSection(&This->lock);
1140 if (!This->frame_initialized || This->info_written)
1142 LeaveCriticalSection(&This->lock);
1143 return WINCODEC_ERR_WRONGSTATE;
1146 This->width = uiWidth;
1147 This->height = uiHeight;
1149 LeaveCriticalSection(&This->lock);
1154 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1155 double dpiX, double dpiY)
1157 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1158 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1160 EnterCriticalSection(&This->lock);
1162 if (!This->frame_initialized || This->info_written)
1164 LeaveCriticalSection(&This->lock);
1165 return WINCODEC_ERR_WRONGSTATE;
1171 LeaveCriticalSection(&This->lock);
1176 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1177 WICPixelFormatGUID *pPixelFormat)
1179 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1181 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1183 EnterCriticalSection(&This->lock);
1185 if (!This->frame_initialized || This->info_written)
1187 LeaveCriticalSection(&This->lock);
1188 return WINCODEC_ERR_WRONGSTATE;
1191 for (i=0; formats[i].guid; i++)
1193 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1197 if (!formats[i].guid) i = 0;
1199 This->format = &formats[i];
1200 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1202 LeaveCriticalSection(&This->lock);
1207 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1208 UINT cCount, IWICColorContext **ppIColorContext)
1210 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1214 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1215 IWICPalette *pIPalette)
1217 FIXME("(%p,%p): stub\n", iface, pIPalette);
1218 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1221 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1222 IWICBitmapSource *pIThumbnail)
1224 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1225 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1228 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1229 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1231 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1232 png_byte **row_pointers=NULL;
1235 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1237 EnterCriticalSection(&This->lock);
1239 if (!This->frame_initialized || !This->width || !This->height || !This->format)
1241 LeaveCriticalSection(&This->lock);
1242 return WINCODEC_ERR_WRONGSTATE;
1245 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1247 LeaveCriticalSection(&This->lock);
1248 return E_INVALIDARG;
1251 /* set up setjmp/longjmp error handling */
1254 LeaveCriticalSection(&This->lock);
1255 HeapFree(GetProcessHeap(), 0, row_pointers);
1258 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1260 if (!This->info_written)
1262 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
1263 This->format->bit_depth, This->format->color_type, PNG_INTERLACE_NONE,
1264 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1266 if (This->xres != 0.0 && This->yres != 0.0)
1268 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
1269 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
1272 ppng_write_info(This->png_ptr, This->info_ptr);
1274 if (This->format->remove_filler)
1275 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1277 if (This->format->swap_rgb)
1278 ppng_set_bgr(This->png_ptr);
1280 This->info_written = TRUE;
1283 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1286 LeaveCriticalSection(&This->lock);
1287 return E_OUTOFMEMORY;
1290 for (i=0; i<lineCount; i++)
1291 row_pointers[i] = pbPixels + cbStride * i;
1293 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1294 This->lines_written += lineCount;
1296 LeaveCriticalSection(&This->lock);
1298 HeapFree(GetProcessHeap(), 0, row_pointers);
1303 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1304 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1306 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1309 WICPixelFormatGUID guid;
1312 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1314 if (!This->frame_initialized || !This->width || !This->height)
1315 return WINCODEC_ERR_WRONGSTATE;
1319 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1320 if (FAILED(hr)) return hr;
1321 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1322 if (FAILED(hr)) return hr;
1325 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1326 if (FAILED(hr)) return hr;
1327 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1329 /* FIXME: should use WICConvertBitmapSource to convert */
1330 ERR("format %s unsupported\n", debugstr_guid(&guid));
1334 if (This->xres == 0.0 || This->yres == 0.0)
1337 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1338 if (FAILED(hr)) return hr;
1339 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1340 if (FAILED(hr)) return hr;
1346 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1347 if (FAILED(hr)) return hr;
1355 if (prc->Width != This->width) return E_INVALIDARG;
1357 stride = (This->format->bpp * This->width + 7)/8;
1359 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1360 if (!pixeldata) return E_OUTOFMEMORY;
1362 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1363 stride*prc->Height, pixeldata);
1367 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1368 stride*prc->Height, pixeldata);
1371 HeapFree(GetProcessHeap(), 0, pixeldata);
1376 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1378 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1380 TRACE("(%p)\n", iface);
1382 EnterCriticalSection(&This->lock);
1384 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1386 LeaveCriticalSection(&This->lock);
1387 return WINCODEC_ERR_WRONGSTATE;
1390 /* set up setjmp/longjmp error handling */
1393 LeaveCriticalSection(&This->lock);
1396 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1398 ppng_write_end(This->png_ptr, This->info_ptr);
1400 This->frame_committed = TRUE;
1402 LeaveCriticalSection(&This->lock);
1407 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1408 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1410 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1414 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1415 PngFrameEncode_QueryInterface,
1416 PngFrameEncode_AddRef,
1417 PngFrameEncode_Release,
1418 PngFrameEncode_Initialize,
1419 PngFrameEncode_SetSize,
1420 PngFrameEncode_SetResolution,
1421 PngFrameEncode_SetPixelFormat,
1422 PngFrameEncode_SetColorContexts,
1423 PngFrameEncode_SetPalette,
1424 PngFrameEncode_SetThumbnail,
1425 PngFrameEncode_WritePixels,
1426 PngFrameEncode_WriteSource,
1427 PngFrameEncode_Commit,
1428 PngFrameEncode_GetMetadataQueryWriter
1431 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1434 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1435 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1437 if (!ppv) return E_INVALIDARG;
1439 if (IsEqualIID(&IID_IUnknown, iid) ||
1440 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1442 *ppv = &This->IWICBitmapEncoder_iface;
1447 return E_NOINTERFACE;
1450 IUnknown_AddRef((IUnknown*)*ppv);
1454 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1456 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1457 ULONG ref = InterlockedIncrement(&This->ref);
1459 TRACE("(%p) refcount=%u\n", iface, ref);
1464 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1466 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1467 ULONG ref = InterlockedDecrement(&This->ref);
1469 TRACE("(%p) refcount=%u\n", iface, ref);
1473 This->lock.DebugInfo->Spare[0] = 0;
1474 DeleteCriticalSection(&This->lock);
1476 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1478 IStream_Release(This->stream);
1479 HeapFree(GetProcessHeap(), 0, This);
1485 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1487 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1491 hr = IStream_Write(This->stream, data, length, &byteswritten);
1492 if (FAILED(hr) || byteswritten != length)
1494 ppng_error(png_ptr, "failed writing data");
1498 static void user_flush(png_structp png_ptr)
1502 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1503 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1505 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1508 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1510 EnterCriticalSection(&This->lock);
1514 LeaveCriticalSection(&This->lock);
1515 return WINCODEC_ERR_WRONGSTATE;
1518 /* initialize libpng */
1519 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1522 LeaveCriticalSection(&This->lock);
1526 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1527 if (!This->info_ptr)
1529 ppng_destroy_write_struct(&This->png_ptr, NULL);
1530 This->png_ptr = NULL;
1531 LeaveCriticalSection(&This->lock);
1535 IStream_AddRef(pIStream);
1536 This->stream = pIStream;
1538 /* set up setjmp/longjmp error handling */
1541 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1542 This->png_ptr = NULL;
1543 IStream_Release(This->stream);
1544 This->stream = NULL;
1545 LeaveCriticalSection(&This->lock);
1548 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1550 /* set up custom i/o handling */
1551 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1553 LeaveCriticalSection(&This->lock);
1558 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1559 GUID *pguidContainerFormat)
1561 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1565 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1566 IWICBitmapEncoderInfo **ppIEncoderInfo)
1568 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1572 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1573 UINT cCount, IWICColorContext **ppIColorContext)
1575 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1579 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1581 TRACE("(%p,%p)\n", iface, pIPalette);
1582 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1585 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1587 TRACE("(%p,%p)\n", iface, pIThumbnail);
1588 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1591 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1593 TRACE("(%p,%p)\n", iface, pIPreview);
1594 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1597 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1598 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1600 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1602 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1604 EnterCriticalSection(&This->lock);
1606 if (This->frame_count != 0)
1608 LeaveCriticalSection(&This->lock);
1609 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1614 LeaveCriticalSection(&This->lock);
1615 return WINCODEC_ERR_NOTINITIALIZED;
1618 hr = CreatePropertyBag2(ppIEncoderOptions);
1621 LeaveCriticalSection(&This->lock);
1625 This->frame_count = 1;
1627 LeaveCriticalSection(&This->lock);
1629 IWICBitmapEncoder_AddRef(iface);
1630 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1635 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1637 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1638 TRACE("(%p)\n", iface);
1640 EnterCriticalSection(&This->lock);
1642 if (!This->frame_committed || This->committed)
1644 LeaveCriticalSection(&This->lock);
1645 return WINCODEC_ERR_WRONGSTATE;
1648 This->committed = TRUE;
1650 LeaveCriticalSection(&This->lock);
1655 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1656 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1658 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1662 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
1663 PngEncoder_QueryInterface,
1666 PngEncoder_Initialize,
1667 PngEncoder_GetContainerFormat,
1668 PngEncoder_GetEncoderInfo,
1669 PngEncoder_SetColorContexts,
1670 PngEncoder_SetPalette,
1671 PngEncoder_SetThumbnail,
1672 PngEncoder_SetPreview,
1673 PngEncoder_CreateNewFrame,
1675 PngEncoder_GetMetadataQueryWriter
1678 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1683 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1687 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1689 if (!libpng_handle && !load_libpng())
1691 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1695 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1696 if (!This) return E_OUTOFMEMORY;
1698 This->IWICBitmapEncoder_iface.lpVtbl = &PngEncoder_Vtbl;
1699 This->IWICBitmapFrameEncode_iface.lpVtbl = &PngEncoder_FrameVtbl;
1701 This->png_ptr = NULL;
1702 This->info_ptr = NULL;
1703 This->stream = NULL;
1704 This->frame_count = 0;
1705 This->frame_initialized = FALSE;
1706 This->format = NULL;
1707 This->info_written = FALSE;
1712 This->lines_written = 0;
1713 This->frame_committed = FALSE;
1714 This->committed = FALSE;
1715 InitializeCriticalSection(&This->lock);
1716 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngEncoder.lock");
1718 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
1719 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1724 #else /* !HAVE_PNG_H */
1726 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1728 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1732 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1734 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");