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_image_height);
170 MAKE_FUNCPTR(png_get_image_width);
171 MAKE_FUNCPTR(png_get_io_ptr);
172 MAKE_FUNCPTR(png_get_pHYs);
173 MAKE_FUNCPTR(png_get_PLTE);
174 MAKE_FUNCPTR(png_get_tRNS);
175 MAKE_FUNCPTR(png_set_bgr);
176 MAKE_FUNCPTR(png_set_error_fn);
177 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
178 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
180 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8);
182 MAKE_FUNCPTR(png_set_filler);
183 MAKE_FUNCPTR(png_set_gray_to_rgb);
184 MAKE_FUNCPTR(png_set_IHDR);
185 MAKE_FUNCPTR(png_set_pHYs);
186 MAKE_FUNCPTR(png_set_read_fn);
187 MAKE_FUNCPTR(png_set_strip_16);
188 MAKE_FUNCPTR(png_set_tRNS_to_alpha);
189 MAKE_FUNCPTR(png_set_write_fn);
190 MAKE_FUNCPTR(png_read_end);
191 MAKE_FUNCPTR(png_read_image);
192 MAKE_FUNCPTR(png_read_info);
193 MAKE_FUNCPTR(png_write_end);
194 MAKE_FUNCPTR(png_write_info);
195 MAKE_FUNCPTR(png_write_rows);
198 static void *load_libpng(void)
200 if((libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
202 #define LOAD_FUNCPTR(f) \
203 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
204 libpng_handle = NULL; \
207 LOAD_FUNCPTR(png_create_read_struct);
208 LOAD_FUNCPTR(png_create_info_struct);
209 LOAD_FUNCPTR(png_create_write_struct);
210 LOAD_FUNCPTR(png_destroy_read_struct);
211 LOAD_FUNCPTR(png_destroy_write_struct);
212 LOAD_FUNCPTR(png_error);
213 LOAD_FUNCPTR(png_get_bit_depth);
214 LOAD_FUNCPTR(png_get_color_type);
215 LOAD_FUNCPTR(png_get_error_ptr);
216 LOAD_FUNCPTR(png_get_image_height);
217 LOAD_FUNCPTR(png_get_image_width);
218 LOAD_FUNCPTR(png_get_io_ptr);
219 LOAD_FUNCPTR(png_get_pHYs);
220 LOAD_FUNCPTR(png_get_PLTE);
221 LOAD_FUNCPTR(png_get_tRNS);
222 LOAD_FUNCPTR(png_set_bgr);
223 LOAD_FUNCPTR(png_set_error_fn);
224 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
225 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
227 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8);
229 LOAD_FUNCPTR(png_set_filler);
230 LOAD_FUNCPTR(png_set_gray_to_rgb);
231 LOAD_FUNCPTR(png_set_IHDR);
232 LOAD_FUNCPTR(png_set_pHYs);
233 LOAD_FUNCPTR(png_set_read_fn);
234 LOAD_FUNCPTR(png_set_strip_16);
235 LOAD_FUNCPTR(png_set_tRNS_to_alpha);
236 LOAD_FUNCPTR(png_set_write_fn);
237 LOAD_FUNCPTR(png_read_end);
238 LOAD_FUNCPTR(png_read_image);
239 LOAD_FUNCPTR(png_read_info);
240 LOAD_FUNCPTR(png_write_end);
241 LOAD_FUNCPTR(png_write_info);
242 LOAD_FUNCPTR(png_write_rows);
246 return libpng_handle;
249 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
253 /* This uses setjmp/longjmp just like the default. We can't use the
254 * default because there's no way to access the jmp buffer in the png_struct
255 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
256 WARN("PNG error: %s\n", debugstr_a(error_message));
257 pjmpbuf = ppng_get_error_ptr(png_ptr);
258 longjmp(*pjmpbuf, 1);
261 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
263 WARN("PNG warning: %s\n", debugstr_a(warning_message));
267 IWICBitmapDecoder IWICBitmapDecoder_iface;
268 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
269 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
278 const WICPixelFormatGUID *format;
280 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
283 static inline PngDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
285 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapDecoder_iface);
288 static inline PngDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
290 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapFrameDecode_iface);
293 static inline PngDecoder *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
295 return CONTAINING_RECORD(iface, PngDecoder, IWICMetadataBlockReader_iface);
298 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
300 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
303 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
304 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
306 if (!ppv) return E_INVALIDARG;
308 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
310 *ppv = &This->IWICBitmapDecoder_iface;
315 return E_NOINTERFACE;
318 IUnknown_AddRef((IUnknown*)*ppv);
322 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
324 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
325 ULONG ref = InterlockedIncrement(&This->ref);
327 TRACE("(%p) refcount=%u\n", iface, ref);
332 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
334 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
335 ULONG ref = InterlockedDecrement(&This->ref);
337 TRACE("(%p) refcount=%u\n", iface, ref);
342 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
343 This->lock.DebugInfo->Spare[0] = 0;
344 DeleteCriticalSection(&This->lock);
345 HeapFree(GetProcessHeap(), 0, This->image_bits);
346 HeapFree(GetProcessHeap(), 0, This);
352 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
353 DWORD *pdwCapability)
355 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
359 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
361 IStream *stream = ppng_get_io_ptr(png_ptr);
365 hr = IStream_Read(stream, data, length, &bytesread);
366 if (FAILED(hr) || bytesread != length)
368 ppng_error(png_ptr, "failed reading data");
372 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
373 WICDecodeOptions cacheOptions)
375 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
378 png_bytep *row_pointers=NULL;
381 int color_type, bit_depth;
384 png_uint_32 transparency;
385 png_color_16p trans_values;
388 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
390 EnterCriticalSection(&This->lock);
392 /* initialize libpng */
393 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
400 This->info_ptr = ppng_create_info_struct(This->png_ptr);
403 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
404 This->png_ptr = NULL;
409 This->end_info = ppng_create_info_struct(This->png_ptr);
412 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
413 This->png_ptr = NULL;
418 /* set up setjmp/longjmp error handling */
421 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
422 HeapFree(GetProcessHeap(), 0, row_pointers);
423 This->png_ptr = NULL;
427 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
429 /* seek to the start of the stream */
431 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
432 if (FAILED(hr)) goto end;
434 /* set up custom i/o handling */
435 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
437 /* read the header */
438 ppng_read_info(This->png_ptr, This->info_ptr);
440 /* choose a pixel format */
441 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
442 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
444 /* check for color-keyed alpha */
445 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
447 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
450 if (color_type == PNG_COLOR_TYPE_GRAY)
454 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
455 ppng_set_expand_gray_1_2_4_to_8(This->png_ptr);
457 ppng_set_gray_1_2_4_to_8(This->png_ptr);
461 ppng_set_gray_to_rgb(This->png_ptr);
463 ppng_set_tRNS_to_alpha(This->png_ptr);
464 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
469 case PNG_COLOR_TYPE_GRAY:
470 This->bpp = bit_depth;
473 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
474 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
475 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
476 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
477 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
479 ERR("invalid grayscale bit depth: %i\n", bit_depth);
484 case PNG_COLOR_TYPE_GRAY_ALPHA:
485 /* WIC does not support grayscale alpha formats so use RGBA */
486 ppng_set_gray_to_rgb(This->png_ptr);
488 case PNG_COLOR_TYPE_RGB_ALPHA:
489 This->bpp = bit_depth * 4;
493 ppng_set_bgr(This->png_ptr);
494 This->format = &GUID_WICPixelFormat32bppBGRA;
496 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
498 ERR("invalid RGBA bit depth: %i\n", bit_depth);
503 case PNG_COLOR_TYPE_PALETTE:
504 This->bpp = bit_depth;
507 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
508 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
509 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
510 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
512 ERR("invalid indexed color bit depth: %i\n", bit_depth);
517 case PNG_COLOR_TYPE_RGB:
518 This->bpp = bit_depth * 3;
522 ppng_set_bgr(This->png_ptr);
523 This->format = &GUID_WICPixelFormat24bppBGR;
525 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
527 ERR("invalid RGB color bit depth: %i\n", bit_depth);
533 ERR("invalid color type %i\n", color_type);
538 /* read the image data */
539 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
540 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
541 This->stride = This->width * This->bpp;
542 image_size = This->stride * This->height;
544 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
545 if (!This->image_bits)
551 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
558 for (i=0; i<This->height; i++)
559 row_pointers[i] = This->image_bits + i * This->stride;
561 ppng_read_image(This->png_ptr, row_pointers);
563 HeapFree(GetProcessHeap(), 0, row_pointers);
566 ppng_read_end(This->png_ptr, This->end_info);
568 This->initialized = TRUE;
572 LeaveCriticalSection(&This->lock);
577 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
578 GUID *pguidContainerFormat)
580 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
584 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
585 IWICBitmapDecoderInfo **ppIDecoderInfo)
588 IWICComponentInfo *compinfo;
590 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
592 hr = CreateComponentInfo(&CLSID_WICPngDecoder, &compinfo);
593 if (FAILED(hr)) return hr;
595 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
596 (void**)ppIDecoderInfo);
598 IWICComponentInfo_Release(compinfo);
603 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
604 IWICPalette *pIPalette)
606 FIXME("(%p,%p): stub\n", iface, pIPalette);
610 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
611 IWICMetadataQueryReader **ppIMetadataQueryReader)
613 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
617 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
618 IWICBitmapSource **ppIBitmapSource)
620 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
624 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
625 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
627 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
628 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
631 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
632 IWICBitmapSource **ppIThumbnail)
634 TRACE("(%p,%p)\n", iface, ppIThumbnail);
635 return WINCODEC_ERR_CODECNOTHUMBNAIL;
638 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
645 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
646 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
648 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
649 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
651 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
653 if (index != 0) return E_INVALIDARG;
655 IWICBitmapDecoder_AddRef(iface);
657 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
662 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
663 PngDecoder_QueryInterface,
666 PngDecoder_QueryCapability,
667 PngDecoder_Initialize,
668 PngDecoder_GetContainerFormat,
669 PngDecoder_GetDecoderInfo,
670 PngDecoder_CopyPalette,
671 PngDecoder_GetMetadataQueryReader,
672 PngDecoder_GetPreview,
673 PngDecoder_GetColorContexts,
674 PngDecoder_GetThumbnail,
675 PngDecoder_GetFrameCount,
679 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
682 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
683 if (!ppv) return E_INVALIDARG;
685 if (IsEqualIID(&IID_IUnknown, iid) ||
686 IsEqualIID(&IID_IWICBitmapSource, iid) ||
687 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
689 *ppv = &This->IWICBitmapFrameDecode_iface;
691 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
693 *ppv = &This->IWICMetadataBlockReader_iface;
698 return E_NOINTERFACE;
701 IUnknown_AddRef((IUnknown*)*ppv);
705 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
707 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
708 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
711 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
713 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
714 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
717 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
718 UINT *puiWidth, UINT *puiHeight)
720 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
721 *puiWidth = This->width;
722 *puiHeight = This->height;
723 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
727 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
728 WICPixelFormatGUID *pPixelFormat)
730 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
731 TRACE("(%p,%p)\n", iface, pPixelFormat);
733 memcpy(pPixelFormat, This->format, sizeof(GUID));
738 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
739 double *pDpiX, double *pDpiY)
741 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
742 png_uint_32 ret, xres, yres;
745 EnterCriticalSection(&This->lock);
747 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
749 if (ret && unit_type == PNG_RESOLUTION_METER)
751 *pDpiX = xres * 0.0254;
752 *pDpiY = yres * 0.0254;
756 WARN("no pHYs block present\n");
757 *pDpiX = *pDpiY = 96.0;
760 LeaveCriticalSection(&This->lock);
762 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
767 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
768 IWICPalette *pIPalette)
770 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
772 png_colorp png_palette;
774 WICColor palette[256];
777 png_color_16p trans_values;
781 TRACE("(%p,%p)\n", iface, pIPalette);
783 EnterCriticalSection(&This->lock);
785 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
788 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
792 if (num_palette > 256)
794 ERR("palette has %i colors?!\n", num_palette);
799 for (i=0; i<num_palette; i++)
801 palette[i] = (0xff000000|
802 png_palette[i].red << 16|
803 png_palette[i].green << 8|
804 png_palette[i].blue);
807 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
810 for (i=0; i<num_trans; i++)
812 palette[trans[i]] = 0x00000000;
818 LeaveCriticalSection(&This->lock);
821 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
826 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
827 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
829 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
830 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
832 return copy_pixels(This->bpp, This->image_bits,
833 This->width, This->height, This->stride,
834 prc, cbStride, cbBufferSize, pbBuffer);
837 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
838 IWICMetadataQueryReader **ppIMetadataQueryReader)
840 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
844 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
845 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
847 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
848 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
851 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
852 IWICBitmapSource **ppIThumbnail)
854 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
858 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
859 PngDecoder_Frame_QueryInterface,
860 PngDecoder_Frame_AddRef,
861 PngDecoder_Frame_Release,
862 PngDecoder_Frame_GetSize,
863 PngDecoder_Frame_GetPixelFormat,
864 PngDecoder_Frame_GetResolution,
865 PngDecoder_Frame_CopyPalette,
866 PngDecoder_Frame_CopyPixels,
867 PngDecoder_Frame_GetMetadataQueryReader,
868 PngDecoder_Frame_GetColorContexts,
869 PngDecoder_Frame_GetThumbnail
872 static HRESULT WINAPI PngDecoder_Block_QueryInterface(IWICMetadataBlockReader *iface, REFIID iid,
875 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
876 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
879 static ULONG WINAPI PngDecoder_Block_AddRef(IWICMetadataBlockReader *iface)
881 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
882 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
885 static ULONG WINAPI PngDecoder_Block_Release(IWICMetadataBlockReader *iface)
887 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
888 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
891 static HRESULT WINAPI PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
892 GUID *pguidContainerFormat)
894 if (!pguidContainerFormat) return E_INVALIDARG;
895 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
899 static HRESULT WINAPI PngDecoder_Block_GetCount(IWICMetadataBlockReader *iface,
902 FIXME("%p,%p: stub\n", iface, pcCount);
906 static HRESULT WINAPI PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
907 UINT nIndex, IWICMetadataReader **ppIMetadataReader)
909 FIXME("%p,%d,%p\n", iface, nIndex, ppIMetadataReader);
913 static HRESULT WINAPI PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader *iface,
914 IEnumUnknown **ppIEnumMetadata)
916 FIXME("%p,%p\n", iface, ppIEnumMetadata);
920 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl = {
921 PngDecoder_Block_QueryInterface,
922 PngDecoder_Block_AddRef,
923 PngDecoder_Block_Release,
924 PngDecoder_Block_GetContainerFormat,
925 PngDecoder_Block_GetCount,
926 PngDecoder_Block_GetReaderByIndex,
927 PngDecoder_Block_GetEnumerator,
930 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
935 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
939 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
941 if (!libpng_handle && !load_libpng())
943 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
947 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
948 if (!This) return E_OUTOFMEMORY;
950 This->IWICBitmapDecoder_iface.lpVtbl = &PngDecoder_Vtbl;
951 This->IWICBitmapFrameDecode_iface.lpVtbl = &PngDecoder_FrameVtbl;
952 This->IWICMetadataBlockReader_iface.lpVtbl = &PngDecoder_BlockVtbl;
954 This->png_ptr = NULL;
955 This->info_ptr = NULL;
956 This->end_info = NULL;
957 This->initialized = FALSE;
958 This->image_bits = NULL;
959 InitializeCriticalSection(&This->lock);
960 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngDecoder.lock");
962 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
963 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
968 struct png_pixelformat {
969 const WICPixelFormatGUID *guid;
977 static const struct png_pixelformat formats[] = {
978 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
979 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
980 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
981 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
982 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
983 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
984 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
985 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
986 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
987 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
991 typedef struct PngEncoder {
992 IWICBitmapEncoder IWICBitmapEncoder_iface;
993 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
999 BOOL frame_initialized;
1000 const struct png_pixelformat *format;
1005 BOOL frame_committed;
1007 CRITICAL_SECTION lock;
1010 static inline PngEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1012 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapEncoder_iface);
1015 static inline PngEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1017 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapFrameEncode_iface);
1020 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1023 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1024 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1026 if (!ppv) return E_INVALIDARG;
1028 if (IsEqualIID(&IID_IUnknown, iid) ||
1029 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1031 *ppv = &This->IWICBitmapFrameEncode_iface;
1036 return E_NOINTERFACE;
1039 IUnknown_AddRef((IUnknown*)*ppv);
1043 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1045 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1046 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
1049 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
1051 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1052 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1055 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1056 IPropertyBag2 *pIEncoderOptions)
1058 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1059 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1061 EnterCriticalSection(&This->lock);
1063 if (This->frame_initialized)
1065 LeaveCriticalSection(&This->lock);
1066 return WINCODEC_ERR_WRONGSTATE;
1069 This->frame_initialized = TRUE;
1071 LeaveCriticalSection(&This->lock);
1076 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1077 UINT uiWidth, UINT uiHeight)
1079 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1080 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1082 EnterCriticalSection(&This->lock);
1084 if (!This->frame_initialized || This->info_written)
1086 LeaveCriticalSection(&This->lock);
1087 return WINCODEC_ERR_WRONGSTATE;
1090 This->width = uiWidth;
1091 This->height = uiHeight;
1093 LeaveCriticalSection(&This->lock);
1098 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1099 double dpiX, double dpiY)
1101 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1102 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1104 EnterCriticalSection(&This->lock);
1106 if (!This->frame_initialized || This->info_written)
1108 LeaveCriticalSection(&This->lock);
1109 return WINCODEC_ERR_WRONGSTATE;
1115 LeaveCriticalSection(&This->lock);
1120 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1121 WICPixelFormatGUID *pPixelFormat)
1123 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1125 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1127 EnterCriticalSection(&This->lock);
1129 if (!This->frame_initialized || This->info_written)
1131 LeaveCriticalSection(&This->lock);
1132 return WINCODEC_ERR_WRONGSTATE;
1135 for (i=0; formats[i].guid; i++)
1137 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1141 if (!formats[i].guid) i = 0;
1143 This->format = &formats[i];
1144 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1146 LeaveCriticalSection(&This->lock);
1151 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1152 UINT cCount, IWICColorContext **ppIColorContext)
1154 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1158 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1159 IWICPalette *pIPalette)
1161 FIXME("(%p,%p): stub\n", iface, pIPalette);
1162 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1165 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1166 IWICBitmapSource *pIThumbnail)
1168 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1169 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1172 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1173 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1175 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1176 png_byte **row_pointers=NULL;
1179 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1181 EnterCriticalSection(&This->lock);
1183 if (!This->frame_initialized || !This->width || !This->height || !This->format)
1185 LeaveCriticalSection(&This->lock);
1186 return WINCODEC_ERR_WRONGSTATE;
1189 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1191 LeaveCriticalSection(&This->lock);
1192 return E_INVALIDARG;
1195 /* set up setjmp/longjmp error handling */
1198 LeaveCriticalSection(&This->lock);
1199 HeapFree(GetProcessHeap(), 0, row_pointers);
1202 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1204 if (!This->info_written)
1206 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
1207 This->format->bit_depth, This->format->color_type, PNG_INTERLACE_NONE,
1208 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1210 if (This->xres != 0.0 && This->yres != 0.0)
1212 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
1213 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
1216 ppng_write_info(This->png_ptr, This->info_ptr);
1218 if (This->format->remove_filler)
1219 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1221 if (This->format->swap_rgb)
1222 ppng_set_bgr(This->png_ptr);
1224 This->info_written = TRUE;
1227 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1230 LeaveCriticalSection(&This->lock);
1231 return E_OUTOFMEMORY;
1234 for (i=0; i<lineCount; i++)
1235 row_pointers[i] = pbPixels + cbStride * i;
1237 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1238 This->lines_written += lineCount;
1240 LeaveCriticalSection(&This->lock);
1242 HeapFree(GetProcessHeap(), 0, row_pointers);
1247 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1248 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1250 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1253 WICPixelFormatGUID guid;
1256 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1258 if (!This->frame_initialized || !This->width || !This->height)
1259 return WINCODEC_ERR_WRONGSTATE;
1263 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1264 if (FAILED(hr)) return hr;
1265 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1266 if (FAILED(hr)) return hr;
1269 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1270 if (FAILED(hr)) return hr;
1271 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1273 /* FIXME: should use WICConvertBitmapSource to convert */
1274 ERR("format %s unsupported\n", debugstr_guid(&guid));
1278 if (This->xres == 0.0 || This->yres == 0.0)
1281 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1282 if (FAILED(hr)) return hr;
1283 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1284 if (FAILED(hr)) return hr;
1290 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1291 if (FAILED(hr)) return hr;
1299 if (prc->Width != This->width) return E_INVALIDARG;
1301 stride = (This->format->bpp * This->width + 7)/8;
1303 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1304 if (!pixeldata) return E_OUTOFMEMORY;
1306 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1307 stride*prc->Height, pixeldata);
1311 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1312 stride*prc->Height, pixeldata);
1315 HeapFree(GetProcessHeap(), 0, pixeldata);
1320 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1322 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1324 TRACE("(%p)\n", iface);
1326 EnterCriticalSection(&This->lock);
1328 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1330 LeaveCriticalSection(&This->lock);
1331 return WINCODEC_ERR_WRONGSTATE;
1334 /* set up setjmp/longjmp error handling */
1337 LeaveCriticalSection(&This->lock);
1340 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1342 ppng_write_end(This->png_ptr, This->info_ptr);
1344 This->frame_committed = TRUE;
1346 LeaveCriticalSection(&This->lock);
1351 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1352 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1354 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1358 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1359 PngFrameEncode_QueryInterface,
1360 PngFrameEncode_AddRef,
1361 PngFrameEncode_Release,
1362 PngFrameEncode_Initialize,
1363 PngFrameEncode_SetSize,
1364 PngFrameEncode_SetResolution,
1365 PngFrameEncode_SetPixelFormat,
1366 PngFrameEncode_SetColorContexts,
1367 PngFrameEncode_SetPalette,
1368 PngFrameEncode_SetThumbnail,
1369 PngFrameEncode_WritePixels,
1370 PngFrameEncode_WriteSource,
1371 PngFrameEncode_Commit,
1372 PngFrameEncode_GetMetadataQueryWriter
1375 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1378 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1379 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1381 if (!ppv) return E_INVALIDARG;
1383 if (IsEqualIID(&IID_IUnknown, iid) ||
1384 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1386 *ppv = &This->IWICBitmapEncoder_iface;
1391 return E_NOINTERFACE;
1394 IUnknown_AddRef((IUnknown*)*ppv);
1398 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1400 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1401 ULONG ref = InterlockedIncrement(&This->ref);
1403 TRACE("(%p) refcount=%u\n", iface, ref);
1408 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1410 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1411 ULONG ref = InterlockedDecrement(&This->ref);
1413 TRACE("(%p) refcount=%u\n", iface, ref);
1417 This->lock.DebugInfo->Spare[0] = 0;
1418 DeleteCriticalSection(&This->lock);
1420 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1422 IStream_Release(This->stream);
1423 HeapFree(GetProcessHeap(), 0, This);
1429 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1431 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1435 hr = IStream_Write(This->stream, data, length, &byteswritten);
1436 if (FAILED(hr) || byteswritten != length)
1438 ppng_error(png_ptr, "failed writing data");
1442 static void user_flush(png_structp png_ptr)
1446 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1447 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1449 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1452 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1454 EnterCriticalSection(&This->lock);
1458 LeaveCriticalSection(&This->lock);
1459 return WINCODEC_ERR_WRONGSTATE;
1462 /* initialize libpng */
1463 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1466 LeaveCriticalSection(&This->lock);
1470 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1471 if (!This->info_ptr)
1473 ppng_destroy_write_struct(&This->png_ptr, NULL);
1474 This->png_ptr = NULL;
1475 LeaveCriticalSection(&This->lock);
1479 IStream_AddRef(pIStream);
1480 This->stream = pIStream;
1482 /* set up setjmp/longjmp error handling */
1485 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1486 This->png_ptr = NULL;
1487 IStream_Release(This->stream);
1488 This->stream = NULL;
1489 LeaveCriticalSection(&This->lock);
1492 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1494 /* set up custom i/o handling */
1495 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1497 LeaveCriticalSection(&This->lock);
1502 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1503 GUID *pguidContainerFormat)
1505 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1509 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1510 IWICBitmapEncoderInfo **ppIEncoderInfo)
1512 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1516 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1517 UINT cCount, IWICColorContext **ppIColorContext)
1519 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1523 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1525 TRACE("(%p,%p)\n", iface, pIPalette);
1526 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1529 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1531 TRACE("(%p,%p)\n", iface, pIThumbnail);
1532 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1535 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1537 TRACE("(%p,%p)\n", iface, pIPreview);
1538 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1541 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1542 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1544 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1546 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1548 EnterCriticalSection(&This->lock);
1550 if (This->frame_count != 0)
1552 LeaveCriticalSection(&This->lock);
1553 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1558 LeaveCriticalSection(&This->lock);
1559 return WINCODEC_ERR_NOTINITIALIZED;
1562 hr = CreatePropertyBag2(ppIEncoderOptions);
1565 LeaveCriticalSection(&This->lock);
1569 This->frame_count = 1;
1571 LeaveCriticalSection(&This->lock);
1573 IWICBitmapEncoder_AddRef(iface);
1574 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1579 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1581 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1582 TRACE("(%p)\n", iface);
1584 EnterCriticalSection(&This->lock);
1586 if (!This->frame_committed || This->committed)
1588 LeaveCriticalSection(&This->lock);
1589 return WINCODEC_ERR_WRONGSTATE;
1592 This->committed = TRUE;
1594 LeaveCriticalSection(&This->lock);
1599 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1600 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1602 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1606 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
1607 PngEncoder_QueryInterface,
1610 PngEncoder_Initialize,
1611 PngEncoder_GetContainerFormat,
1612 PngEncoder_GetEncoderInfo,
1613 PngEncoder_SetColorContexts,
1614 PngEncoder_SetPalette,
1615 PngEncoder_SetThumbnail,
1616 PngEncoder_SetPreview,
1617 PngEncoder_CreateNewFrame,
1619 PngEncoder_GetMetadataQueryWriter
1622 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1627 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1631 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1633 if (!libpng_handle && !load_libpng())
1635 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1639 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1640 if (!This) return E_OUTOFMEMORY;
1642 This->IWICBitmapEncoder_iface.lpVtbl = &PngEncoder_Vtbl;
1643 This->IWICBitmapFrameEncode_iface.lpVtbl = &PngEncoder_FrameVtbl;
1645 This->png_ptr = NULL;
1646 This->info_ptr = NULL;
1647 This->stream = NULL;
1648 This->frame_count = 0;
1649 This->frame_initialized = FALSE;
1650 This->format = NULL;
1651 This->info_written = FALSE;
1656 This->lines_written = 0;
1657 This->frame_committed = FALSE;
1658 This->committed = FALSE;
1659 InitializeCriticalSection(&This->lock);
1660 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngEncoder.lock");
1662 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
1663 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1668 #else /* !HAVE_PNG_H */
1670 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1672 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1676 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1678 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");