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"
34 #include "wincodecsdk.h"
36 #include "wincodecs_private.h"
38 #include "wine/debug.h"
39 #include "wine/library.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
43 static HRESULT read_png_chunk(IStream *stream, BYTE *type, BYTE **data, ULONG *data_size)
49 hr = IStream_Read(stream, header, 8, &bytesread);
50 if (FAILED(hr) || bytesread < 8)
57 *data_size = header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3];
59 memcpy(type, &header[4], 4);
63 *data = HeapAlloc(GetProcessHeap(), 0, *data_size);
67 hr = IStream_Read(stream, *data, *data_size, &bytesread);
69 if (FAILED(hr) || bytesread < *data_size)
73 HeapFree(GetProcessHeap(), 0, *data);
78 /* FIXME: Verify the CRC? */
84 static HRESULT LoadTextMetadata(IStream *stream, const GUID *preferred_vendor,
85 DWORD persist_options, MetadataItem **items, DWORD *item_count)
91 ULONG name_len, value_len;
96 hr = read_png_chunk(stream, type, &data, &data_size);
97 if (FAILED(hr)) return hr;
99 name_end_ptr = memchr(data, 0, data_size);
101 name_len = name_end_ptr - data;
103 if (!name_end_ptr || name_len > 79)
105 HeapFree(GetProcessHeap(), 0, data);
109 value_len = data_size - name_len - 1;
111 result = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem));
112 name = HeapAlloc(GetProcessHeap(), 0, name_len + 1);
113 value = HeapAlloc(GetProcessHeap(), 0, value_len + 1);
114 if (!result || !name || !value)
116 HeapFree(GetProcessHeap(), 0, data);
117 HeapFree(GetProcessHeap(), 0, result);
118 HeapFree(GetProcessHeap(), 0, name);
119 HeapFree(GetProcessHeap(), 0, value);
120 return E_OUTOFMEMORY;
123 PropVariantInit(&result[0].schema);
124 PropVariantInit(&result[0].id);
125 PropVariantInit(&result[0].value);
127 memcpy(name, data, name_len + 1);
128 memcpy(value, name_end_ptr + 1, value_len);
129 value[value_len] = 0;
131 result[0].id.vt = VT_LPSTR;
132 result[0].id.pszVal = name;
133 result[0].value.vt = VT_LPSTR;
134 result[0].value.pszVal = value;
139 HeapFree(GetProcessHeap(), 0, data);
144 static const MetadataHandlerVtbl TextReader_Vtbl = {
146 &CLSID_WICPngTextMetadataReader,
150 HRESULT PngTextReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
152 return MetadataReader_Create(&TextReader_Vtbl, pUnkOuter, iid, ppv);
157 static void *libpng_handle;
158 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
159 MAKE_FUNCPTR(png_create_read_struct);
160 MAKE_FUNCPTR(png_create_info_struct);
161 MAKE_FUNCPTR(png_create_write_struct);
162 MAKE_FUNCPTR(png_destroy_read_struct);
163 MAKE_FUNCPTR(png_destroy_write_struct);
164 MAKE_FUNCPTR(png_error);
165 MAKE_FUNCPTR(png_get_bit_depth);
166 MAKE_FUNCPTR(png_get_color_type);
167 MAKE_FUNCPTR(png_get_error_ptr);
168 MAKE_FUNCPTR(png_get_image_height);
169 MAKE_FUNCPTR(png_get_image_width);
170 MAKE_FUNCPTR(png_get_io_ptr);
171 MAKE_FUNCPTR(png_get_pHYs);
172 MAKE_FUNCPTR(png_get_PLTE);
173 MAKE_FUNCPTR(png_get_tRNS);
174 MAKE_FUNCPTR(png_set_bgr);
175 MAKE_FUNCPTR(png_set_error_fn);
176 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
177 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
179 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8);
181 MAKE_FUNCPTR(png_set_filler);
182 MAKE_FUNCPTR(png_set_gray_to_rgb);
183 MAKE_FUNCPTR(png_set_IHDR);
184 MAKE_FUNCPTR(png_set_pHYs);
185 MAKE_FUNCPTR(png_set_read_fn);
186 MAKE_FUNCPTR(png_set_strip_16);
187 MAKE_FUNCPTR(png_set_tRNS_to_alpha);
188 MAKE_FUNCPTR(png_set_write_fn);
189 MAKE_FUNCPTR(png_read_end);
190 MAKE_FUNCPTR(png_read_image);
191 MAKE_FUNCPTR(png_read_info);
192 MAKE_FUNCPTR(png_write_end);
193 MAKE_FUNCPTR(png_write_info);
194 MAKE_FUNCPTR(png_write_rows);
197 static void *load_libpng(void)
199 if((libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
201 #define LOAD_FUNCPTR(f) \
202 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
203 libpng_handle = NULL; \
206 LOAD_FUNCPTR(png_create_read_struct);
207 LOAD_FUNCPTR(png_create_info_struct);
208 LOAD_FUNCPTR(png_create_write_struct);
209 LOAD_FUNCPTR(png_destroy_read_struct);
210 LOAD_FUNCPTR(png_destroy_write_struct);
211 LOAD_FUNCPTR(png_error);
212 LOAD_FUNCPTR(png_get_bit_depth);
213 LOAD_FUNCPTR(png_get_color_type);
214 LOAD_FUNCPTR(png_get_error_ptr);
215 LOAD_FUNCPTR(png_get_image_height);
216 LOAD_FUNCPTR(png_get_image_width);
217 LOAD_FUNCPTR(png_get_io_ptr);
218 LOAD_FUNCPTR(png_get_pHYs);
219 LOAD_FUNCPTR(png_get_PLTE);
220 LOAD_FUNCPTR(png_get_tRNS);
221 LOAD_FUNCPTR(png_set_bgr);
222 LOAD_FUNCPTR(png_set_error_fn);
223 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
224 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
226 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8);
228 LOAD_FUNCPTR(png_set_filler);
229 LOAD_FUNCPTR(png_set_gray_to_rgb);
230 LOAD_FUNCPTR(png_set_IHDR);
231 LOAD_FUNCPTR(png_set_pHYs);
232 LOAD_FUNCPTR(png_set_read_fn);
233 LOAD_FUNCPTR(png_set_strip_16);
234 LOAD_FUNCPTR(png_set_tRNS_to_alpha);
235 LOAD_FUNCPTR(png_set_write_fn);
236 LOAD_FUNCPTR(png_read_end);
237 LOAD_FUNCPTR(png_read_image);
238 LOAD_FUNCPTR(png_read_info);
239 LOAD_FUNCPTR(png_write_end);
240 LOAD_FUNCPTR(png_write_info);
241 LOAD_FUNCPTR(png_write_rows);
245 return libpng_handle;
248 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
252 /* This uses setjmp/longjmp just like the default. We can't use the
253 * default because there's no way to access the jmp buffer in the png_struct
254 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
255 WARN("PNG error: %s\n", debugstr_a(error_message));
256 pjmpbuf = ppng_get_error_ptr(png_ptr);
257 longjmp(*pjmpbuf, 1);
260 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
262 WARN("PNG warning: %s\n", debugstr_a(warning_message));
266 IWICBitmapDecoder IWICBitmapDecoder_iface;
267 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
268 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
277 const WICPixelFormatGUID *format;
279 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
282 static inline PngDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
284 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapDecoder_iface);
287 static inline PngDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
289 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapFrameDecode_iface);
292 static inline PngDecoder *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
294 return CONTAINING_RECORD(iface, PngDecoder, IWICMetadataBlockReader_iface);
297 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
299 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
302 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
303 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
305 if (!ppv) return E_INVALIDARG;
307 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
309 *ppv = &This->IWICBitmapDecoder_iface;
314 return E_NOINTERFACE;
317 IUnknown_AddRef((IUnknown*)*ppv);
321 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
323 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
324 ULONG ref = InterlockedIncrement(&This->ref);
326 TRACE("(%p) refcount=%u\n", iface, ref);
331 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
333 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
334 ULONG ref = InterlockedDecrement(&This->ref);
336 TRACE("(%p) refcount=%u\n", iface, ref);
341 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
342 This->lock.DebugInfo->Spare[0] = 0;
343 DeleteCriticalSection(&This->lock);
344 HeapFree(GetProcessHeap(), 0, This->image_bits);
345 HeapFree(GetProcessHeap(), 0, This);
351 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
352 DWORD *pdwCapability)
354 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
358 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
360 IStream *stream = ppng_get_io_ptr(png_ptr);
364 hr = IStream_Read(stream, data, length, &bytesread);
365 if (FAILED(hr) || bytesread != length)
367 ppng_error(png_ptr, "failed reading data");
371 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
372 WICDecodeOptions cacheOptions)
374 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
377 png_bytep *row_pointers=NULL;
380 int color_type, bit_depth;
383 png_uint_32 transparency;
384 png_color_16p trans_values;
387 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
389 EnterCriticalSection(&This->lock);
391 /* initialize libpng */
392 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
399 This->info_ptr = ppng_create_info_struct(This->png_ptr);
402 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
403 This->png_ptr = NULL;
408 This->end_info = ppng_create_info_struct(This->png_ptr);
411 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
412 This->png_ptr = NULL;
417 /* set up setjmp/longjmp error handling */
420 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
421 HeapFree(GetProcessHeap(), 0, row_pointers);
422 This->png_ptr = NULL;
426 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
428 /* seek to the start of the stream */
430 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
431 if (FAILED(hr)) goto end;
433 /* set up custom i/o handling */
434 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
436 /* read the header */
437 ppng_read_info(This->png_ptr, This->info_ptr);
439 /* choose a pixel format */
440 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
441 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
443 /* check for color-keyed alpha */
444 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
446 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
449 if (color_type == PNG_COLOR_TYPE_GRAY)
453 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
454 ppng_set_expand_gray_1_2_4_to_8(This->png_ptr);
456 ppng_set_gray_1_2_4_to_8(This->png_ptr);
460 ppng_set_gray_to_rgb(This->png_ptr);
462 ppng_set_tRNS_to_alpha(This->png_ptr);
463 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
468 case PNG_COLOR_TYPE_GRAY:
469 This->bpp = bit_depth;
472 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
473 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
474 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
475 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
476 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
478 ERR("invalid grayscale bit depth: %i\n", bit_depth);
483 case PNG_COLOR_TYPE_GRAY_ALPHA:
484 /* WIC does not support grayscale alpha formats so use RGBA */
485 ppng_set_gray_to_rgb(This->png_ptr);
487 case PNG_COLOR_TYPE_RGB_ALPHA:
488 This->bpp = bit_depth * 4;
492 ppng_set_bgr(This->png_ptr);
493 This->format = &GUID_WICPixelFormat32bppBGRA;
495 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
497 ERR("invalid RGBA bit depth: %i\n", bit_depth);
502 case PNG_COLOR_TYPE_PALETTE:
503 This->bpp = bit_depth;
506 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
507 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
508 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
509 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
511 ERR("invalid indexed color bit depth: %i\n", bit_depth);
516 case PNG_COLOR_TYPE_RGB:
517 This->bpp = bit_depth * 3;
521 ppng_set_bgr(This->png_ptr);
522 This->format = &GUID_WICPixelFormat24bppBGR;
524 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
526 ERR("invalid RGB color bit depth: %i\n", bit_depth);
532 ERR("invalid color type %i\n", color_type);
537 /* read the image data */
538 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
539 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
540 This->stride = This->width * This->bpp;
541 image_size = This->stride * This->height;
543 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
544 if (!This->image_bits)
550 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
557 for (i=0; i<This->height; i++)
558 row_pointers[i] = This->image_bits + i * This->stride;
560 ppng_read_image(This->png_ptr, row_pointers);
562 HeapFree(GetProcessHeap(), 0, row_pointers);
565 ppng_read_end(This->png_ptr, This->end_info);
567 This->initialized = TRUE;
571 LeaveCriticalSection(&This->lock);
576 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
577 GUID *pguidContainerFormat)
579 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
583 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
584 IWICBitmapDecoderInfo **ppIDecoderInfo)
587 IWICComponentInfo *compinfo;
589 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
591 hr = CreateComponentInfo(&CLSID_WICPngDecoder, &compinfo);
592 if (FAILED(hr)) return hr;
594 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
595 (void**)ppIDecoderInfo);
597 IWICComponentInfo_Release(compinfo);
602 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
603 IWICPalette *pIPalette)
605 FIXME("(%p,%p): stub\n", iface, pIPalette);
609 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
610 IWICMetadataQueryReader **ppIMetadataQueryReader)
612 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
616 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
617 IWICBitmapSource **ppIBitmapSource)
619 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
623 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
624 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
626 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
630 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
631 IWICBitmapSource **ppIThumbnail)
633 TRACE("(%p,%p)\n", iface, ppIThumbnail);
634 return WINCODEC_ERR_CODECNOTHUMBNAIL;
637 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
644 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
645 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
647 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
648 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
650 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
652 if (index != 0) return E_INVALIDARG;
654 IWICBitmapDecoder_AddRef(iface);
656 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
661 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
662 PngDecoder_QueryInterface,
665 PngDecoder_QueryCapability,
666 PngDecoder_Initialize,
667 PngDecoder_GetContainerFormat,
668 PngDecoder_GetDecoderInfo,
669 PngDecoder_CopyPalette,
670 PngDecoder_GetMetadataQueryReader,
671 PngDecoder_GetPreview,
672 PngDecoder_GetColorContexts,
673 PngDecoder_GetThumbnail,
674 PngDecoder_GetFrameCount,
678 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
681 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
682 if (!ppv) return E_INVALIDARG;
684 if (IsEqualIID(&IID_IUnknown, iid) ||
685 IsEqualIID(&IID_IWICBitmapSource, iid) ||
686 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
688 *ppv = &This->IWICBitmapFrameDecode_iface;
690 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
692 *ppv = &This->IWICMetadataBlockReader_iface;
697 return E_NOINTERFACE;
700 IUnknown_AddRef((IUnknown*)*ppv);
704 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
706 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
707 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
710 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
712 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
713 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
716 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
717 UINT *puiWidth, UINT *puiHeight)
719 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
720 *puiWidth = This->width;
721 *puiHeight = This->height;
722 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
726 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
727 WICPixelFormatGUID *pPixelFormat)
729 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
730 TRACE("(%p,%p)\n", iface, pPixelFormat);
732 memcpy(pPixelFormat, This->format, sizeof(GUID));
737 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
738 double *pDpiX, double *pDpiY)
740 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
741 png_uint_32 ret, xres, yres;
744 EnterCriticalSection(&This->lock);
746 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
748 if (ret && unit_type == PNG_RESOLUTION_METER)
750 *pDpiX = xres * 0.0254;
751 *pDpiY = yres * 0.0254;
755 WARN("no pHYs block present\n");
756 *pDpiX = *pDpiY = 96.0;
759 LeaveCriticalSection(&This->lock);
761 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
766 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
767 IWICPalette *pIPalette)
769 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
771 png_colorp png_palette;
773 WICColor palette[256];
776 png_color_16p trans_values;
780 TRACE("(%p,%p)\n", iface, pIPalette);
782 EnterCriticalSection(&This->lock);
784 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
787 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
791 if (num_palette > 256)
793 ERR("palette has %i colors?!\n", num_palette);
798 for (i=0; i<num_palette; i++)
800 palette[i] = (0xff000000|
801 png_palette[i].red << 16|
802 png_palette[i].green << 8|
803 png_palette[i].blue);
806 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
809 for (i=0; i<num_trans; i++)
811 palette[trans[i]] = 0x00000000;
817 LeaveCriticalSection(&This->lock);
820 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
825 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
826 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
828 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
829 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
831 return copy_pixels(This->bpp, This->image_bits,
832 This->width, This->height, This->stride,
833 prc, cbStride, cbBufferSize, pbBuffer);
836 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
837 IWICMetadataQueryReader **ppIMetadataQueryReader)
839 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
843 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
844 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
846 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
850 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
851 IWICBitmapSource **ppIThumbnail)
853 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
857 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
858 PngDecoder_Frame_QueryInterface,
859 PngDecoder_Frame_AddRef,
860 PngDecoder_Frame_Release,
861 PngDecoder_Frame_GetSize,
862 PngDecoder_Frame_GetPixelFormat,
863 PngDecoder_Frame_GetResolution,
864 PngDecoder_Frame_CopyPalette,
865 PngDecoder_Frame_CopyPixels,
866 PngDecoder_Frame_GetMetadataQueryReader,
867 PngDecoder_Frame_GetColorContexts,
868 PngDecoder_Frame_GetThumbnail
871 static HRESULT WINAPI PngDecoder_Block_QueryInterface(IWICMetadataBlockReader *iface, REFIID iid,
874 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
875 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
878 static ULONG WINAPI PngDecoder_Block_AddRef(IWICMetadataBlockReader *iface)
880 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
881 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
884 static ULONG WINAPI PngDecoder_Block_Release(IWICMetadataBlockReader *iface)
886 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
887 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
890 static HRESULT WINAPI PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
891 GUID *pguidContainerFormat)
893 if (!pguidContainerFormat) return E_INVALIDARG;
894 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
898 static HRESULT WINAPI PngDecoder_Block_GetCount(IWICMetadataBlockReader *iface,
901 FIXME("%p,%p: stub\n", iface, pcCount);
905 static HRESULT WINAPI PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
906 UINT nIndex, IWICMetadataReader **ppIMetadataReader)
908 FIXME("%p,%d,%p\n", iface, nIndex, ppIMetadataReader);
912 static HRESULT WINAPI PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader *iface,
913 IEnumUnknown **ppIEnumMetadata)
915 FIXME("%p,%p\n", iface, ppIEnumMetadata);
919 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl = {
920 PngDecoder_Block_QueryInterface,
921 PngDecoder_Block_AddRef,
922 PngDecoder_Block_Release,
923 PngDecoder_Block_GetContainerFormat,
924 PngDecoder_Block_GetCount,
925 PngDecoder_Block_GetReaderByIndex,
926 PngDecoder_Block_GetEnumerator,
929 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
934 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
938 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
940 if (!libpng_handle && !load_libpng())
942 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
946 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
947 if (!This) return E_OUTOFMEMORY;
949 This->IWICBitmapDecoder_iface.lpVtbl = &PngDecoder_Vtbl;
950 This->IWICBitmapFrameDecode_iface.lpVtbl = &PngDecoder_FrameVtbl;
951 This->IWICMetadataBlockReader_iface.lpVtbl = &PngDecoder_BlockVtbl;
953 This->png_ptr = NULL;
954 This->info_ptr = NULL;
955 This->end_info = NULL;
956 This->initialized = FALSE;
957 This->image_bits = NULL;
958 InitializeCriticalSection(&This->lock);
959 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngDecoder.lock");
961 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
962 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
967 struct png_pixelformat {
968 const WICPixelFormatGUID *guid;
976 static const struct png_pixelformat formats[] = {
977 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
978 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
979 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
980 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
981 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
982 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
983 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
984 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
985 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
986 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
990 typedef struct PngEncoder {
991 IWICBitmapEncoder IWICBitmapEncoder_iface;
992 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
998 BOOL frame_initialized;
999 const struct png_pixelformat *format;
1004 BOOL frame_committed;
1006 CRITICAL_SECTION lock;
1009 static inline PngEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1011 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapEncoder_iface);
1014 static inline PngEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1016 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapFrameEncode_iface);
1019 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1022 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1023 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1025 if (!ppv) return E_INVALIDARG;
1027 if (IsEqualIID(&IID_IUnknown, iid) ||
1028 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1030 *ppv = &This->IWICBitmapFrameEncode_iface;
1035 return E_NOINTERFACE;
1038 IUnknown_AddRef((IUnknown*)*ppv);
1042 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1044 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1045 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
1048 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
1050 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1051 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1054 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1055 IPropertyBag2 *pIEncoderOptions)
1057 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1058 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1060 EnterCriticalSection(&This->lock);
1062 if (This->frame_initialized)
1064 LeaveCriticalSection(&This->lock);
1065 return WINCODEC_ERR_WRONGSTATE;
1068 This->frame_initialized = TRUE;
1070 LeaveCriticalSection(&This->lock);
1075 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1076 UINT uiWidth, UINT uiHeight)
1078 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1079 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1081 EnterCriticalSection(&This->lock);
1083 if (!This->frame_initialized || This->info_written)
1085 LeaveCriticalSection(&This->lock);
1086 return WINCODEC_ERR_WRONGSTATE;
1089 This->width = uiWidth;
1090 This->height = uiHeight;
1092 LeaveCriticalSection(&This->lock);
1097 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1098 double dpiX, double dpiY)
1100 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1101 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1103 EnterCriticalSection(&This->lock);
1105 if (!This->frame_initialized || This->info_written)
1107 LeaveCriticalSection(&This->lock);
1108 return WINCODEC_ERR_WRONGSTATE;
1114 LeaveCriticalSection(&This->lock);
1119 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1120 WICPixelFormatGUID *pPixelFormat)
1122 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1124 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1126 EnterCriticalSection(&This->lock);
1128 if (!This->frame_initialized || This->info_written)
1130 LeaveCriticalSection(&This->lock);
1131 return WINCODEC_ERR_WRONGSTATE;
1134 for (i=0; formats[i].guid; i++)
1136 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1140 if (!formats[i].guid) i = 0;
1142 This->format = &formats[i];
1143 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1145 LeaveCriticalSection(&This->lock);
1150 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1151 UINT cCount, IWICColorContext **ppIColorContext)
1153 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1157 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1158 IWICPalette *pIPalette)
1160 FIXME("(%p,%p): stub\n", iface, pIPalette);
1161 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1164 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1165 IWICBitmapSource *pIThumbnail)
1167 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1168 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1171 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1172 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1174 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1175 png_byte **row_pointers=NULL;
1178 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1180 EnterCriticalSection(&This->lock);
1182 if (!This->frame_initialized || !This->width || !This->height || !This->format)
1184 LeaveCriticalSection(&This->lock);
1185 return WINCODEC_ERR_WRONGSTATE;
1188 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1190 LeaveCriticalSection(&This->lock);
1191 return E_INVALIDARG;
1194 /* set up setjmp/longjmp error handling */
1197 LeaveCriticalSection(&This->lock);
1198 HeapFree(GetProcessHeap(), 0, row_pointers);
1201 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1203 if (!This->info_written)
1205 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
1206 This->format->bit_depth, This->format->color_type, PNG_INTERLACE_NONE,
1207 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1209 if (This->xres != 0.0 && This->yres != 0.0)
1211 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
1212 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
1215 ppng_write_info(This->png_ptr, This->info_ptr);
1217 if (This->format->remove_filler)
1218 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1220 if (This->format->swap_rgb)
1221 ppng_set_bgr(This->png_ptr);
1223 This->info_written = TRUE;
1226 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1229 LeaveCriticalSection(&This->lock);
1230 return E_OUTOFMEMORY;
1233 for (i=0; i<lineCount; i++)
1234 row_pointers[i] = pbPixels + cbStride * i;
1236 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1237 This->lines_written += lineCount;
1239 LeaveCriticalSection(&This->lock);
1241 HeapFree(GetProcessHeap(), 0, row_pointers);
1246 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1247 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1249 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1252 WICPixelFormatGUID guid;
1255 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1257 if (!This->frame_initialized || !This->width || !This->height)
1258 return WINCODEC_ERR_WRONGSTATE;
1262 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1263 if (FAILED(hr)) return hr;
1264 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1265 if (FAILED(hr)) return hr;
1268 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1269 if (FAILED(hr)) return hr;
1270 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1272 /* FIXME: should use WICConvertBitmapSource to convert */
1273 ERR("format %s unsupported\n", debugstr_guid(&guid));
1277 if (This->xres == 0.0 || This->yres == 0.0)
1280 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1281 if (FAILED(hr)) return hr;
1282 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1283 if (FAILED(hr)) return hr;
1289 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1290 if (FAILED(hr)) return hr;
1298 if (prc->Width != This->width) return E_INVALIDARG;
1300 stride = (This->format->bpp * This->width + 7)/8;
1302 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1303 if (!pixeldata) return E_OUTOFMEMORY;
1305 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1306 stride*prc->Height, pixeldata);
1310 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1311 stride*prc->Height, pixeldata);
1314 HeapFree(GetProcessHeap(), 0, pixeldata);
1319 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1321 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1323 TRACE("(%p)\n", iface);
1325 EnterCriticalSection(&This->lock);
1327 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1329 LeaveCriticalSection(&This->lock);
1330 return WINCODEC_ERR_WRONGSTATE;
1333 /* set up setjmp/longjmp error handling */
1336 LeaveCriticalSection(&This->lock);
1339 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1341 ppng_write_end(This->png_ptr, This->info_ptr);
1343 This->frame_committed = TRUE;
1345 LeaveCriticalSection(&This->lock);
1350 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1351 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1353 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1357 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1358 PngFrameEncode_QueryInterface,
1359 PngFrameEncode_AddRef,
1360 PngFrameEncode_Release,
1361 PngFrameEncode_Initialize,
1362 PngFrameEncode_SetSize,
1363 PngFrameEncode_SetResolution,
1364 PngFrameEncode_SetPixelFormat,
1365 PngFrameEncode_SetColorContexts,
1366 PngFrameEncode_SetPalette,
1367 PngFrameEncode_SetThumbnail,
1368 PngFrameEncode_WritePixels,
1369 PngFrameEncode_WriteSource,
1370 PngFrameEncode_Commit,
1371 PngFrameEncode_GetMetadataQueryWriter
1374 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1377 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1378 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1380 if (!ppv) return E_INVALIDARG;
1382 if (IsEqualIID(&IID_IUnknown, iid) ||
1383 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1385 *ppv = &This->IWICBitmapEncoder_iface;
1390 return E_NOINTERFACE;
1393 IUnknown_AddRef((IUnknown*)*ppv);
1397 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1399 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1400 ULONG ref = InterlockedIncrement(&This->ref);
1402 TRACE("(%p) refcount=%u\n", iface, ref);
1407 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1409 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1410 ULONG ref = InterlockedDecrement(&This->ref);
1412 TRACE("(%p) refcount=%u\n", iface, ref);
1416 This->lock.DebugInfo->Spare[0] = 0;
1417 DeleteCriticalSection(&This->lock);
1419 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1421 IStream_Release(This->stream);
1422 HeapFree(GetProcessHeap(), 0, This);
1428 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1430 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1434 hr = IStream_Write(This->stream, data, length, &byteswritten);
1435 if (FAILED(hr) || byteswritten != length)
1437 ppng_error(png_ptr, "failed writing data");
1441 static void user_flush(png_structp png_ptr)
1445 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1446 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1448 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1451 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1453 EnterCriticalSection(&This->lock);
1457 LeaveCriticalSection(&This->lock);
1458 return WINCODEC_ERR_WRONGSTATE;
1461 /* initialize libpng */
1462 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1465 LeaveCriticalSection(&This->lock);
1469 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1470 if (!This->info_ptr)
1472 ppng_destroy_write_struct(&This->png_ptr, NULL);
1473 This->png_ptr = NULL;
1474 LeaveCriticalSection(&This->lock);
1478 IStream_AddRef(pIStream);
1479 This->stream = pIStream;
1481 /* set up setjmp/longjmp error handling */
1484 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1485 This->png_ptr = NULL;
1486 IStream_Release(This->stream);
1487 This->stream = NULL;
1488 LeaveCriticalSection(&This->lock);
1491 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1493 /* set up custom i/o handling */
1494 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1496 LeaveCriticalSection(&This->lock);
1501 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1502 GUID *pguidContainerFormat)
1504 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1508 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1509 IWICBitmapEncoderInfo **ppIEncoderInfo)
1511 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1515 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1516 UINT cCount, IWICColorContext **ppIColorContext)
1518 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1522 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1524 TRACE("(%p,%p)\n", iface, pIPalette);
1525 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1528 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1530 TRACE("(%p,%p)\n", iface, pIThumbnail);
1531 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1534 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1536 TRACE("(%p,%p)\n", iface, pIPreview);
1537 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1540 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1541 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1543 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1545 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1547 EnterCriticalSection(&This->lock);
1549 if (This->frame_count != 0)
1551 LeaveCriticalSection(&This->lock);
1552 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1557 LeaveCriticalSection(&This->lock);
1558 return WINCODEC_ERR_NOTINITIALIZED;
1561 hr = CreatePropertyBag2(ppIEncoderOptions);
1564 LeaveCriticalSection(&This->lock);
1568 This->frame_count = 1;
1570 LeaveCriticalSection(&This->lock);
1572 IWICBitmapEncoder_AddRef(iface);
1573 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1578 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1580 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1581 TRACE("(%p)\n", iface);
1583 EnterCriticalSection(&This->lock);
1585 if (!This->frame_committed || This->committed)
1587 LeaveCriticalSection(&This->lock);
1588 return WINCODEC_ERR_WRONGSTATE;
1591 This->committed = TRUE;
1593 LeaveCriticalSection(&This->lock);
1598 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1599 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1601 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1605 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
1606 PngEncoder_QueryInterface,
1609 PngEncoder_Initialize,
1610 PngEncoder_GetContainerFormat,
1611 PngEncoder_GetEncoderInfo,
1612 PngEncoder_SetColorContexts,
1613 PngEncoder_SetPalette,
1614 PngEncoder_SetThumbnail,
1615 PngEncoder_SetPreview,
1616 PngEncoder_CreateNewFrame,
1618 PngEncoder_GetMetadataQueryWriter
1621 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1626 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1630 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1632 if (!libpng_handle && !load_libpng())
1634 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1638 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1639 if (!This) return E_OUTOFMEMORY;
1641 This->IWICBitmapEncoder_iface.lpVtbl = &PngEncoder_Vtbl;
1642 This->IWICBitmapFrameEncode_iface.lpVtbl = &PngEncoder_FrameVtbl;
1644 This->png_ptr = NULL;
1645 This->info_ptr = NULL;
1646 This->stream = NULL;
1647 This->frame_count = 0;
1648 This->frame_initialized = FALSE;
1649 This->format = NULL;
1650 This->info_written = FALSE;
1655 This->lines_written = 0;
1656 This->frame_committed = FALSE;
1657 This->committed = FALSE;
1658 InitializeCriticalSection(&This->lock);
1659 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngEncoder.lock");
1661 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
1662 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1667 #else /* !HAVE_PNG_H */
1669 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1671 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1675 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1677 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");