windowscodecs: Fixed incorrect memset (Coverity).
[wine/multimedia.git] / dlls / windowscodecs / pngformat.c
bloba8ecb73937ded1a90b72d8ce43136eb6cd2c1359
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdarg.h>
24 #ifdef HAVE_PNG_H
25 #include <png.h>
26 #endif
28 #define NONAMELESSUNION
29 #define COBJMACROS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "objbase.h"
34 #include "wincodec.h"
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 const WCHAR wszPngInterlaceOption[] = {'I','n','t','e','r','l','a','c','e','O','p','t','i','o','n',0};
46 static HRESULT read_png_chunk(IStream *stream, BYTE *type, BYTE **data, ULONG *data_size)
48 BYTE header[8];
49 HRESULT hr;
50 ULONG bytesread;
52 hr = IStream_Read(stream, header, 8, &bytesread);
53 if (FAILED(hr) || bytesread < 8)
55 if (SUCCEEDED(hr))
56 hr = E_FAIL;
57 return hr;
60 *data_size = header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3];
62 memcpy(type, &header[4], 4);
64 if (data)
66 *data = HeapAlloc(GetProcessHeap(), 0, *data_size);
67 if (!*data)
68 return E_OUTOFMEMORY;
70 hr = IStream_Read(stream, *data, *data_size, &bytesread);
72 if (FAILED(hr) || bytesread < *data_size)
74 if (SUCCEEDED(hr))
75 hr = E_FAIL;
76 HeapFree(GetProcessHeap(), 0, *data);
77 *data = NULL;
78 return hr;
81 /* Windows ignores CRC of the chunk */
84 return S_OK;
87 static HRESULT LoadTextMetadata(IStream *stream, const GUID *preferred_vendor,
88 DWORD persist_options, MetadataItem **items, DWORD *item_count)
90 HRESULT hr;
91 BYTE type[4];
92 BYTE *data;
93 ULONG data_size;
94 ULONG name_len, value_len;
95 BYTE *name_end_ptr;
96 LPSTR name, value;
97 MetadataItem *result;
99 hr = read_png_chunk(stream, type, &data, &data_size);
100 if (FAILED(hr)) return hr;
102 name_end_ptr = memchr(data, 0, data_size);
104 name_len = name_end_ptr - data;
106 if (!name_end_ptr || name_len > 79)
108 HeapFree(GetProcessHeap(), 0, data);
109 return E_FAIL;
112 value_len = data_size - name_len - 1;
114 result = HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem));
115 name = HeapAlloc(GetProcessHeap(), 0, name_len + 1);
116 value = HeapAlloc(GetProcessHeap(), 0, value_len + 1);
117 if (!result || !name || !value)
119 HeapFree(GetProcessHeap(), 0, data);
120 HeapFree(GetProcessHeap(), 0, result);
121 HeapFree(GetProcessHeap(), 0, name);
122 HeapFree(GetProcessHeap(), 0, value);
123 return E_OUTOFMEMORY;
126 PropVariantInit(&result[0].schema);
127 PropVariantInit(&result[0].id);
128 PropVariantInit(&result[0].value);
130 memcpy(name, data, name_len + 1);
131 memcpy(value, name_end_ptr + 1, value_len);
132 value[value_len] = 0;
134 result[0].id.vt = VT_LPSTR;
135 result[0].id.u.pszVal = name;
136 result[0].value.vt = VT_LPSTR;
137 result[0].value.u.pszVal = value;
139 *items = result;
140 *item_count = 1;
142 HeapFree(GetProcessHeap(), 0, data);
144 return S_OK;
147 static const MetadataHandlerVtbl TextReader_Vtbl = {
149 &CLSID_WICPngTextMetadataReader,
150 LoadTextMetadata
153 HRESULT PngTextReader_CreateInstance(REFIID iid, void** ppv)
155 return MetadataReader_Create(&TextReader_Vtbl, iid, ppv);
158 #ifdef SONAME_LIBPNG
160 static void *libpng_handle;
161 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
162 MAKE_FUNCPTR(png_create_read_struct);
163 MAKE_FUNCPTR(png_create_info_struct);
164 MAKE_FUNCPTR(png_create_write_struct);
165 MAKE_FUNCPTR(png_destroy_read_struct);
166 MAKE_FUNCPTR(png_destroy_write_struct);
167 MAKE_FUNCPTR(png_error);
168 MAKE_FUNCPTR(png_get_bit_depth);
169 MAKE_FUNCPTR(png_get_color_type);
170 MAKE_FUNCPTR(png_get_error_ptr);
171 MAKE_FUNCPTR(png_get_iCCP);
172 MAKE_FUNCPTR(png_get_image_height);
173 MAKE_FUNCPTR(png_get_image_width);
174 MAKE_FUNCPTR(png_get_io_ptr);
175 MAKE_FUNCPTR(png_get_pHYs);
176 MAKE_FUNCPTR(png_get_PLTE);
177 MAKE_FUNCPTR(png_get_tRNS);
178 MAKE_FUNCPTR(png_set_bgr);
179 MAKE_FUNCPTR(png_set_crc_action);
180 MAKE_FUNCPTR(png_set_error_fn);
181 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
182 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
183 #else
184 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8);
185 #endif
186 MAKE_FUNCPTR(png_set_filler);
187 MAKE_FUNCPTR(png_set_gray_to_rgb);
188 MAKE_FUNCPTR(png_set_interlace_handling);
189 MAKE_FUNCPTR(png_set_IHDR);
190 MAKE_FUNCPTR(png_set_pHYs);
191 MAKE_FUNCPTR(png_set_read_fn);
192 MAKE_FUNCPTR(png_set_strip_16);
193 MAKE_FUNCPTR(png_set_tRNS_to_alpha);
194 MAKE_FUNCPTR(png_set_write_fn);
195 MAKE_FUNCPTR(png_read_end);
196 MAKE_FUNCPTR(png_read_image);
197 MAKE_FUNCPTR(png_read_info);
198 MAKE_FUNCPTR(png_write_end);
199 MAKE_FUNCPTR(png_write_info);
200 MAKE_FUNCPTR(png_write_rows);
201 #undef MAKE_FUNCPTR
203 static void *load_libpng(void)
205 if((libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
207 #define LOAD_FUNCPTR(f) \
208 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
209 libpng_handle = NULL; \
210 return NULL; \
212 LOAD_FUNCPTR(png_create_read_struct);
213 LOAD_FUNCPTR(png_create_info_struct);
214 LOAD_FUNCPTR(png_create_write_struct);
215 LOAD_FUNCPTR(png_destroy_read_struct);
216 LOAD_FUNCPTR(png_destroy_write_struct);
217 LOAD_FUNCPTR(png_error);
218 LOAD_FUNCPTR(png_get_bit_depth);
219 LOAD_FUNCPTR(png_get_color_type);
220 LOAD_FUNCPTR(png_get_error_ptr);
221 LOAD_FUNCPTR(png_get_iCCP);
222 LOAD_FUNCPTR(png_get_image_height);
223 LOAD_FUNCPTR(png_get_image_width);
224 LOAD_FUNCPTR(png_get_io_ptr);
225 LOAD_FUNCPTR(png_get_pHYs);
226 LOAD_FUNCPTR(png_get_PLTE);
227 LOAD_FUNCPTR(png_get_tRNS);
228 LOAD_FUNCPTR(png_set_bgr);
229 LOAD_FUNCPTR(png_set_crc_action);
230 LOAD_FUNCPTR(png_set_error_fn);
231 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
232 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
233 #else
234 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8);
235 #endif
236 LOAD_FUNCPTR(png_set_filler);
237 LOAD_FUNCPTR(png_set_gray_to_rgb);
238 LOAD_FUNCPTR(png_set_interlace_handling);
239 LOAD_FUNCPTR(png_set_IHDR);
240 LOAD_FUNCPTR(png_set_pHYs);
241 LOAD_FUNCPTR(png_set_read_fn);
242 LOAD_FUNCPTR(png_set_strip_16);
243 LOAD_FUNCPTR(png_set_tRNS_to_alpha);
244 LOAD_FUNCPTR(png_set_write_fn);
245 LOAD_FUNCPTR(png_read_end);
246 LOAD_FUNCPTR(png_read_image);
247 LOAD_FUNCPTR(png_read_info);
248 LOAD_FUNCPTR(png_write_end);
249 LOAD_FUNCPTR(png_write_info);
250 LOAD_FUNCPTR(png_write_rows);
252 #undef LOAD_FUNCPTR
254 return libpng_handle;
257 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
259 jmp_buf *pjmpbuf;
261 /* This uses setjmp/longjmp just like the default. We can't use the
262 * default because there's no way to access the jmp buffer in the png_struct
263 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
264 WARN("PNG error: %s\n", debugstr_a(error_message));
265 pjmpbuf = ppng_get_error_ptr(png_ptr);
266 longjmp(*pjmpbuf, 1);
269 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
271 WARN("PNG warning: %s\n", debugstr_a(warning_message));
274 typedef struct {
275 IWICBitmapDecoder IWICBitmapDecoder_iface;
276 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
277 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
278 LONG ref;
279 png_structp png_ptr;
280 png_infop info_ptr;
281 png_infop end_info;
282 BOOL initialized;
283 int bpp;
284 int width, height;
285 UINT stride;
286 const WICPixelFormatGUID *format;
287 BYTE *image_bits;
288 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
289 } PngDecoder;
291 static inline PngDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
293 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapDecoder_iface);
296 static inline PngDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
298 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapFrameDecode_iface);
301 static inline PngDecoder *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
303 return CONTAINING_RECORD(iface, PngDecoder, IWICMetadataBlockReader_iface);
306 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
308 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
309 void **ppv)
311 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
312 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
314 if (!ppv) return E_INVALIDARG;
316 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
318 *ppv = &This->IWICBitmapDecoder_iface;
320 else
322 *ppv = NULL;
323 return E_NOINTERFACE;
326 IUnknown_AddRef((IUnknown*)*ppv);
327 return S_OK;
330 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
332 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
333 ULONG ref = InterlockedIncrement(&This->ref);
335 TRACE("(%p) refcount=%u\n", iface, ref);
337 return ref;
340 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
342 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
343 ULONG ref = InterlockedDecrement(&This->ref);
345 TRACE("(%p) refcount=%u\n", iface, ref);
347 if (ref == 0)
349 if (This->png_ptr)
350 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
351 This->lock.DebugInfo->Spare[0] = 0;
352 DeleteCriticalSection(&This->lock);
353 HeapFree(GetProcessHeap(), 0, This->image_bits);
354 HeapFree(GetProcessHeap(), 0, This);
357 return ref;
360 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
361 DWORD *capability)
363 HRESULT hr;
365 TRACE("(%p,%p,%p)\n", iface, stream, capability);
367 if (!stream || !capability) return E_INVALIDARG;
369 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
370 if (hr != S_OK) return hr;
372 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
373 WICBitmapDecoderCapabilityCanDecodeSomeImages;
374 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
375 return S_OK;
378 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
380 IStream *stream = ppng_get_io_ptr(png_ptr);
381 HRESULT hr;
382 ULONG bytesread;
384 hr = IStream_Read(stream, data, length, &bytesread);
385 if (FAILED(hr) || bytesread != length)
387 ppng_error(png_ptr, "failed reading data");
391 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
392 WICDecodeOptions cacheOptions)
394 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
395 LARGE_INTEGER seek;
396 HRESULT hr=S_OK;
397 png_bytep *row_pointers=NULL;
398 UINT image_size;
399 UINT i;
400 int color_type, bit_depth;
401 png_bytep trans;
402 int num_trans;
403 png_uint_32 transparency;
404 png_color_16p trans_values;
405 jmp_buf jmpbuf;
407 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
409 EnterCriticalSection(&This->lock);
411 /* initialize libpng */
412 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
413 if (!This->png_ptr)
415 hr = E_FAIL;
416 goto end;
419 This->info_ptr = ppng_create_info_struct(This->png_ptr);
420 if (!This->info_ptr)
422 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
423 This->png_ptr = NULL;
424 hr = E_FAIL;
425 goto end;
428 This->end_info = ppng_create_info_struct(This->png_ptr);
429 if (!This->info_ptr)
431 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
432 This->png_ptr = NULL;
433 hr = E_FAIL;
434 goto end;
437 /* set up setjmp/longjmp error handling */
438 if (setjmp(jmpbuf))
440 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
441 HeapFree(GetProcessHeap(), 0, row_pointers);
442 This->png_ptr = NULL;
443 hr = E_FAIL;
444 goto end;
446 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
447 ppng_set_crc_action(This->png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
449 /* seek to the start of the stream */
450 seek.QuadPart = 0;
451 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
452 if (FAILED(hr)) goto end;
454 /* set up custom i/o handling */
455 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
457 /* read the header */
458 ppng_read_info(This->png_ptr, This->info_ptr);
460 /* choose a pixel format */
461 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
462 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
464 /* check for color-keyed alpha */
465 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
467 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
469 /* expand to RGBA */
470 if (color_type == PNG_COLOR_TYPE_GRAY)
472 if (bit_depth < 8)
474 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
475 ppng_set_expand_gray_1_2_4_to_8(This->png_ptr);
476 #else
477 ppng_set_gray_1_2_4_to_8(This->png_ptr);
478 #endif
479 bit_depth = 8;
481 ppng_set_gray_to_rgb(This->png_ptr);
483 ppng_set_tRNS_to_alpha(This->png_ptr);
484 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
487 switch (color_type)
489 case PNG_COLOR_TYPE_GRAY:
490 This->bpp = bit_depth;
491 switch (bit_depth)
493 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
494 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
495 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
496 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
497 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
498 default:
499 ERR("invalid grayscale bit depth: %i\n", bit_depth);
500 hr = E_FAIL;
501 goto end;
503 break;
504 case PNG_COLOR_TYPE_GRAY_ALPHA:
505 /* WIC does not support grayscale alpha formats so use RGBA */
506 ppng_set_gray_to_rgb(This->png_ptr);
507 /* fall through */
508 case PNG_COLOR_TYPE_RGB_ALPHA:
509 This->bpp = bit_depth * 4;
510 switch (bit_depth)
512 case 8:
513 ppng_set_bgr(This->png_ptr);
514 This->format = &GUID_WICPixelFormat32bppBGRA;
515 break;
516 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
517 default:
518 ERR("invalid RGBA bit depth: %i\n", bit_depth);
519 hr = E_FAIL;
520 goto end;
522 break;
523 case PNG_COLOR_TYPE_PALETTE:
524 This->bpp = bit_depth;
525 switch (bit_depth)
527 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
528 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
529 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
530 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
531 default:
532 ERR("invalid indexed color bit depth: %i\n", bit_depth);
533 hr = E_FAIL;
534 goto end;
536 break;
537 case PNG_COLOR_TYPE_RGB:
538 This->bpp = bit_depth * 3;
539 switch (bit_depth)
541 case 8:
542 ppng_set_bgr(This->png_ptr);
543 This->format = &GUID_WICPixelFormat24bppBGR;
544 break;
545 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
546 default:
547 ERR("invalid RGB color bit depth: %i\n", bit_depth);
548 hr = E_FAIL;
549 goto end;
551 break;
552 default:
553 ERR("invalid color type %i\n", color_type);
554 hr = E_FAIL;
555 goto end;
558 /* read the image data */
559 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
560 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
561 This->stride = This->width * This->bpp;
562 image_size = This->stride * This->height;
564 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
565 if (!This->image_bits)
567 hr = E_OUTOFMEMORY;
568 goto end;
571 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
572 if (!row_pointers)
574 hr = E_OUTOFMEMORY;
575 goto end;
578 for (i=0; i<This->height; i++)
579 row_pointers[i] = This->image_bits + i * This->stride;
581 ppng_read_image(This->png_ptr, row_pointers);
583 HeapFree(GetProcessHeap(), 0, row_pointers);
584 row_pointers = NULL;
586 ppng_read_end(This->png_ptr, This->end_info);
588 This->initialized = TRUE;
590 end:
592 LeaveCriticalSection(&This->lock);
594 return hr;
597 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
598 GUID *pguidContainerFormat)
600 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
601 return S_OK;
604 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
605 IWICBitmapDecoderInfo **ppIDecoderInfo)
607 HRESULT hr;
608 IWICComponentInfo *compinfo;
610 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
612 hr = CreateComponentInfo(&CLSID_WICPngDecoder, &compinfo);
613 if (FAILED(hr)) return hr;
615 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
616 (void**)ppIDecoderInfo);
618 IWICComponentInfo_Release(compinfo);
620 return hr;
623 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
624 IWICPalette *pIPalette)
626 FIXME("(%p,%p): stub\n", iface, pIPalette);
627 return E_NOTIMPL;
630 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
631 IWICMetadataQueryReader **ppIMetadataQueryReader)
633 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
634 return E_NOTIMPL;
637 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
638 IWICBitmapSource **ppIBitmapSource)
640 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
642 if (!ppIBitmapSource) return E_INVALIDARG;
644 *ppIBitmapSource = NULL;
645 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
648 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
649 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
651 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
652 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
655 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
656 IWICBitmapSource **ppIThumbnail)
658 TRACE("(%p,%p)\n", iface, ppIThumbnail);
660 if (!ppIThumbnail) return E_INVALIDARG;
662 *ppIThumbnail = NULL;
663 return WINCODEC_ERR_CODECNOTHUMBNAIL;
666 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
667 UINT *pCount)
669 if (!pCount) return E_INVALIDARG;
671 *pCount = 1;
672 return S_OK;
675 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
676 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
678 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
679 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
681 if (!This->initialized) return WINCODEC_ERR_FRAMEMISSING;
683 if (index != 0) return E_INVALIDARG;
685 IWICBitmapDecoder_AddRef(iface);
687 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
689 return S_OK;
692 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
693 PngDecoder_QueryInterface,
694 PngDecoder_AddRef,
695 PngDecoder_Release,
696 PngDecoder_QueryCapability,
697 PngDecoder_Initialize,
698 PngDecoder_GetContainerFormat,
699 PngDecoder_GetDecoderInfo,
700 PngDecoder_CopyPalette,
701 PngDecoder_GetMetadataQueryReader,
702 PngDecoder_GetPreview,
703 PngDecoder_GetColorContexts,
704 PngDecoder_GetThumbnail,
705 PngDecoder_GetFrameCount,
706 PngDecoder_GetFrame
709 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
710 void **ppv)
712 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
713 if (!ppv) return E_INVALIDARG;
715 if (IsEqualIID(&IID_IUnknown, iid) ||
716 IsEqualIID(&IID_IWICBitmapSource, iid) ||
717 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
719 *ppv = &This->IWICBitmapFrameDecode_iface;
721 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
723 *ppv = &This->IWICMetadataBlockReader_iface;
725 else
727 *ppv = NULL;
728 return E_NOINTERFACE;
731 IUnknown_AddRef((IUnknown*)*ppv);
732 return S_OK;
735 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
737 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
738 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
741 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
743 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
744 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
747 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
748 UINT *puiWidth, UINT *puiHeight)
750 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
751 *puiWidth = This->width;
752 *puiHeight = This->height;
753 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
754 return S_OK;
757 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
758 WICPixelFormatGUID *pPixelFormat)
760 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
761 TRACE("(%p,%p)\n", iface, pPixelFormat);
763 memcpy(pPixelFormat, This->format, sizeof(GUID));
765 return S_OK;
768 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
769 double *pDpiX, double *pDpiY)
771 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
772 png_uint_32 ret, xres, yres;
773 int unit_type;
775 EnterCriticalSection(&This->lock);
777 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
779 if (ret && unit_type == PNG_RESOLUTION_METER)
781 *pDpiX = xres * 0.0254;
782 *pDpiY = yres * 0.0254;
784 else
786 WARN("no pHYs block present\n");
787 *pDpiX = *pDpiY = 96.0;
790 LeaveCriticalSection(&This->lock);
792 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
794 return S_OK;
797 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
798 IWICPalette *pIPalette)
800 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
801 png_uint_32 ret;
802 png_colorp png_palette;
803 int num_palette;
804 WICColor palette[256];
805 png_bytep trans_alpha;
806 int num_trans;
807 png_color_16p trans_values;
808 int i;
809 HRESULT hr=S_OK;
811 TRACE("(%p,%p)\n", iface, pIPalette);
813 EnterCriticalSection(&This->lock);
815 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
816 if (!ret)
818 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
819 goto end;
822 if (num_palette > 256)
824 ERR("palette has %i colors?!\n", num_palette);
825 hr = E_FAIL;
826 goto end;
829 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans_alpha, &num_trans, &trans_values);
830 if (!ret) num_trans = 0;
832 for (i=0; i<num_palette; i++)
834 BYTE alpha = (i < num_trans) ? trans_alpha[i] : 0xff;
835 palette[i] = (alpha << 24 |
836 png_palette[i].red << 16|
837 png_palette[i].green << 8|
838 png_palette[i].blue);
841 end:
843 LeaveCriticalSection(&This->lock);
845 if (SUCCEEDED(hr))
846 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
848 return hr;
851 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
852 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
854 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
855 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
857 return copy_pixels(This->bpp, This->image_bits,
858 This->width, This->height, This->stride,
859 prc, cbStride, cbBufferSize, pbBuffer);
862 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
863 IWICMetadataQueryReader **ppIMetadataQueryReader)
865 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
866 return E_NOTIMPL;
869 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
870 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
872 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
873 png_charp name;
874 BYTE *profile;
875 png_uint_32 len;
876 int compression_type;
877 HRESULT hr;
879 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
881 if (!pcActualCount) return E_INVALIDARG;
883 EnterCriticalSection(&This->lock);
885 if (ppng_get_iCCP(This->png_ptr, This->info_ptr, &name, &compression_type, (void *)&profile, &len))
887 if (cCount && ppIColorContexts)
889 hr = IWICColorContext_InitializeFromMemory(*ppIColorContexts, profile, len);
890 if (FAILED(hr))
892 LeaveCriticalSection(&This->lock);
893 return hr;
896 *pcActualCount = 1;
898 else
899 *pcActualCount = 0;
901 LeaveCriticalSection(&This->lock);
903 return S_OK;
906 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
907 IWICBitmapSource **ppIThumbnail)
909 TRACE("(%p,%p)\n", iface, ppIThumbnail);
911 if (!ppIThumbnail) return E_INVALIDARG;
913 *ppIThumbnail = NULL;
914 return WINCODEC_ERR_CODECNOTHUMBNAIL;
917 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
918 PngDecoder_Frame_QueryInterface,
919 PngDecoder_Frame_AddRef,
920 PngDecoder_Frame_Release,
921 PngDecoder_Frame_GetSize,
922 PngDecoder_Frame_GetPixelFormat,
923 PngDecoder_Frame_GetResolution,
924 PngDecoder_Frame_CopyPalette,
925 PngDecoder_Frame_CopyPixels,
926 PngDecoder_Frame_GetMetadataQueryReader,
927 PngDecoder_Frame_GetColorContexts,
928 PngDecoder_Frame_GetThumbnail
931 static HRESULT WINAPI PngDecoder_Block_QueryInterface(IWICMetadataBlockReader *iface, REFIID iid,
932 void **ppv)
934 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
935 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
938 static ULONG WINAPI PngDecoder_Block_AddRef(IWICMetadataBlockReader *iface)
940 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
941 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
944 static ULONG WINAPI PngDecoder_Block_Release(IWICMetadataBlockReader *iface)
946 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
947 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
950 static HRESULT WINAPI PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
951 GUID *pguidContainerFormat)
953 if (!pguidContainerFormat) return E_INVALIDARG;
954 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
955 return S_OK;
958 static HRESULT WINAPI PngDecoder_Block_GetCount(IWICMetadataBlockReader *iface,
959 UINT *pcCount)
961 static int once;
962 TRACE("%p,%p\n", iface, pcCount);
963 if (!once++) FIXME("stub\n");
964 return E_NOTIMPL;
967 static HRESULT WINAPI PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
968 UINT nIndex, IWICMetadataReader **ppIMetadataReader)
970 FIXME("%p,%d,%p\n", iface, nIndex, ppIMetadataReader);
971 return E_NOTIMPL;
974 static HRESULT WINAPI PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader *iface,
975 IEnumUnknown **ppIEnumMetadata)
977 FIXME("%p,%p\n", iface, ppIEnumMetadata);
978 return E_NOTIMPL;
981 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl = {
982 PngDecoder_Block_QueryInterface,
983 PngDecoder_Block_AddRef,
984 PngDecoder_Block_Release,
985 PngDecoder_Block_GetContainerFormat,
986 PngDecoder_Block_GetCount,
987 PngDecoder_Block_GetReaderByIndex,
988 PngDecoder_Block_GetEnumerator,
991 HRESULT PngDecoder_CreateInstance(REFIID iid, void** ppv)
993 PngDecoder *This;
994 HRESULT ret;
996 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
998 *ppv = NULL;
1000 if (!libpng_handle && !load_libpng())
1002 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
1003 return E_FAIL;
1006 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
1007 if (!This) return E_OUTOFMEMORY;
1009 This->IWICBitmapDecoder_iface.lpVtbl = &PngDecoder_Vtbl;
1010 This->IWICBitmapFrameDecode_iface.lpVtbl = &PngDecoder_FrameVtbl;
1011 This->IWICMetadataBlockReader_iface.lpVtbl = &PngDecoder_BlockVtbl;
1012 This->ref = 1;
1013 This->png_ptr = NULL;
1014 This->info_ptr = NULL;
1015 This->end_info = NULL;
1016 This->initialized = FALSE;
1017 This->image_bits = NULL;
1018 InitializeCriticalSection(&This->lock);
1019 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngDecoder.lock");
1021 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1022 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1024 return ret;
1027 struct png_pixelformat {
1028 const WICPixelFormatGUID *guid;
1029 UINT bpp;
1030 int bit_depth;
1031 int color_type;
1032 BOOL remove_filler;
1033 BOOL swap_rgb;
1036 static const struct png_pixelformat formats[] = {
1037 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
1038 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
1039 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
1040 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
1041 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
1042 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
1043 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
1044 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
1045 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
1046 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
1047 {NULL},
1050 typedef struct PngEncoder {
1051 IWICBitmapEncoder IWICBitmapEncoder_iface;
1052 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1053 LONG ref;
1054 IStream *stream;
1055 png_structp png_ptr;
1056 png_infop info_ptr;
1057 UINT frame_count;
1058 BOOL frame_initialized;
1059 const struct png_pixelformat *format;
1060 BOOL info_written;
1061 UINT width, height;
1062 double xres, yres;
1063 UINT lines_written;
1064 BOOL frame_committed;
1065 BOOL committed;
1066 CRITICAL_SECTION lock;
1067 BOOL interlace;
1068 BYTE *data;
1069 UINT stride;
1070 UINT passes;
1071 } PngEncoder;
1073 static inline PngEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1075 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapEncoder_iface);
1078 static inline PngEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1080 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapFrameEncode_iface);
1083 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1084 void **ppv)
1086 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1087 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1089 if (!ppv) return E_INVALIDARG;
1091 if (IsEqualIID(&IID_IUnknown, iid) ||
1092 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1094 *ppv = &This->IWICBitmapFrameEncode_iface;
1096 else
1098 *ppv = NULL;
1099 return E_NOINTERFACE;
1102 IUnknown_AddRef((IUnknown*)*ppv);
1103 return S_OK;
1106 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1108 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1109 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
1112 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
1114 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1115 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1118 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1119 IPropertyBag2 *pIEncoderOptions)
1121 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1122 BOOL interlace;
1123 PROPBAG2 opts[1]= {{0}};
1124 VARIANT opt_values[1];
1125 HRESULT opt_hres[1];
1126 HRESULT hr;
1128 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1130 opts[0].pstrName = (LPOLESTR)wszPngInterlaceOption;
1131 opts[0].vt = VT_BOOL;
1133 if (pIEncoderOptions)
1135 hr = IPropertyBag2_Read(pIEncoderOptions, 1, opts, NULL, opt_values, opt_hres);
1137 if (FAILED(hr))
1138 return hr;
1140 else
1141 memset(opt_values, 0, sizeof(opt_values));
1143 if (V_VT(&opt_values[0]) == VT_EMPTY)
1144 interlace = FALSE;
1145 else
1146 interlace = (V_BOOL(&opt_values[0]) != 0);
1148 EnterCriticalSection(&This->lock);
1150 if (This->frame_initialized)
1152 LeaveCriticalSection(&This->lock);
1153 return WINCODEC_ERR_WRONGSTATE;
1156 This->interlace = interlace;
1158 This->frame_initialized = TRUE;
1160 LeaveCriticalSection(&This->lock);
1162 return S_OK;
1165 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1166 UINT uiWidth, UINT uiHeight)
1168 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1169 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1171 EnterCriticalSection(&This->lock);
1173 if (!This->frame_initialized || This->info_written)
1175 LeaveCriticalSection(&This->lock);
1176 return WINCODEC_ERR_WRONGSTATE;
1179 This->width = uiWidth;
1180 This->height = uiHeight;
1182 LeaveCriticalSection(&This->lock);
1184 return S_OK;
1187 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1188 double dpiX, double dpiY)
1190 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1191 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1193 EnterCriticalSection(&This->lock);
1195 if (!This->frame_initialized || This->info_written)
1197 LeaveCriticalSection(&This->lock);
1198 return WINCODEC_ERR_WRONGSTATE;
1201 This->xres = dpiX;
1202 This->yres = dpiY;
1204 LeaveCriticalSection(&This->lock);
1206 return S_OK;
1209 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1210 WICPixelFormatGUID *pPixelFormat)
1212 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1213 int i;
1214 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1216 EnterCriticalSection(&This->lock);
1218 if (!This->frame_initialized || This->info_written)
1220 LeaveCriticalSection(&This->lock);
1221 return WINCODEC_ERR_WRONGSTATE;
1224 for (i=0; formats[i].guid; i++)
1226 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1227 break;
1230 if (!formats[i].guid) i = 0;
1232 This->format = &formats[i];
1233 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1235 LeaveCriticalSection(&This->lock);
1237 return S_OK;
1240 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1241 UINT cCount, IWICColorContext **ppIColorContext)
1243 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1244 return E_NOTIMPL;
1247 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1248 IWICPalette *pIPalette)
1250 FIXME("(%p,%p): stub\n", iface, pIPalette);
1251 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1254 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1255 IWICBitmapSource *pIThumbnail)
1257 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1258 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1261 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1262 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1264 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1265 png_byte **row_pointers=NULL;
1266 UINT i;
1267 jmp_buf jmpbuf;
1268 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1270 EnterCriticalSection(&This->lock);
1272 if (!This->frame_initialized || !This->width || !This->height || !This->format)
1274 LeaveCriticalSection(&This->lock);
1275 return WINCODEC_ERR_WRONGSTATE;
1278 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1280 LeaveCriticalSection(&This->lock);
1281 return E_INVALIDARG;
1284 /* set up setjmp/longjmp error handling */
1285 if (setjmp(jmpbuf))
1287 LeaveCriticalSection(&This->lock);
1288 HeapFree(GetProcessHeap(), 0, row_pointers);
1289 return E_FAIL;
1291 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1293 if (!This->info_written)
1295 if (This->interlace)
1297 /* libpng requires us to write all data multiple times in this case. */
1298 This->stride = (This->format->bpp * This->width + 7)/8;
1299 This->data = HeapAlloc(GetProcessHeap(), 0, This->height * This->stride);
1300 if (!This->data)
1302 LeaveCriticalSection(&This->lock);
1303 return E_OUTOFMEMORY;
1307 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
1308 This->format->bit_depth, This->format->color_type,
1309 This->interlace ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE,
1310 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1312 if (This->xres != 0.0 && This->yres != 0.0)
1314 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
1315 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
1318 ppng_write_info(This->png_ptr, This->info_ptr);
1320 if (This->format->remove_filler)
1321 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1323 if (This->format->swap_rgb)
1324 ppng_set_bgr(This->png_ptr);
1326 if (This->interlace)
1327 This->passes = ppng_set_interlace_handling(This->png_ptr);
1329 This->info_written = TRUE;
1332 if (This->interlace)
1334 /* Just store the data so we can write it in multiple passes in Commit. */
1335 for (i=0; i<lineCount; i++)
1336 memcpy(This->data + This->stride * (This->lines_written + i),
1337 pbPixels + cbStride * i,
1338 This->stride);
1340 This->lines_written += lineCount;
1342 LeaveCriticalSection(&This->lock);
1343 return S_OK;
1346 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1347 if (!row_pointers)
1349 LeaveCriticalSection(&This->lock);
1350 return E_OUTOFMEMORY;
1353 for (i=0; i<lineCount; i++)
1354 row_pointers[i] = pbPixels + cbStride * i;
1356 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1357 This->lines_written += lineCount;
1359 LeaveCriticalSection(&This->lock);
1361 HeapFree(GetProcessHeap(), 0, row_pointers);
1363 return S_OK;
1366 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1367 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1369 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1370 HRESULT hr;
1371 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1373 if (!This->frame_initialized)
1374 return WINCODEC_ERR_WRONGSTATE;
1376 hr = configure_write_source(iface, pIBitmapSource, prc,
1377 This->format ? This->format->guid : NULL, This->width, This->height,
1378 This->xres, This->yres);
1380 if (SUCCEEDED(hr))
1382 hr = write_source(iface, pIBitmapSource, prc,
1383 This->format->guid, This->format->bpp, This->width, This->height);
1386 return hr;
1389 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1391 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1392 png_byte **row_pointers=NULL;
1393 jmp_buf jmpbuf;
1394 TRACE("(%p)\n", iface);
1396 EnterCriticalSection(&This->lock);
1398 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1400 LeaveCriticalSection(&This->lock);
1401 return WINCODEC_ERR_WRONGSTATE;
1404 /* set up setjmp/longjmp error handling */
1405 if (setjmp(jmpbuf))
1407 LeaveCriticalSection(&This->lock);
1408 HeapFree(GetProcessHeap(), 0, row_pointers);
1409 return E_FAIL;
1411 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1413 if (This->interlace)
1415 int i;
1417 row_pointers = HeapAlloc(GetProcessHeap(), 0, This->height * sizeof(png_byte*));
1418 if (!row_pointers)
1420 LeaveCriticalSection(&This->lock);
1421 return E_OUTOFMEMORY;
1424 for (i=0; i<This->height; i++)
1425 row_pointers[i] = This->data + This->stride * i;
1427 for (i=0; i<This->passes; i++)
1428 ppng_write_rows(This->png_ptr, row_pointers, This->height);
1431 ppng_write_end(This->png_ptr, This->info_ptr);
1433 This->frame_committed = TRUE;
1435 HeapFree(GetProcessHeap(), 0, row_pointers);
1437 LeaveCriticalSection(&This->lock);
1439 return S_OK;
1442 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1443 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1445 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1446 return E_NOTIMPL;
1449 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1450 PngFrameEncode_QueryInterface,
1451 PngFrameEncode_AddRef,
1452 PngFrameEncode_Release,
1453 PngFrameEncode_Initialize,
1454 PngFrameEncode_SetSize,
1455 PngFrameEncode_SetResolution,
1456 PngFrameEncode_SetPixelFormat,
1457 PngFrameEncode_SetColorContexts,
1458 PngFrameEncode_SetPalette,
1459 PngFrameEncode_SetThumbnail,
1460 PngFrameEncode_WritePixels,
1461 PngFrameEncode_WriteSource,
1462 PngFrameEncode_Commit,
1463 PngFrameEncode_GetMetadataQueryWriter
1466 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1467 void **ppv)
1469 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1470 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1472 if (!ppv) return E_INVALIDARG;
1474 if (IsEqualIID(&IID_IUnknown, iid) ||
1475 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1477 *ppv = &This->IWICBitmapEncoder_iface;
1479 else
1481 *ppv = NULL;
1482 return E_NOINTERFACE;
1485 IUnknown_AddRef((IUnknown*)*ppv);
1486 return S_OK;
1489 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1491 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1492 ULONG ref = InterlockedIncrement(&This->ref);
1494 TRACE("(%p) refcount=%u\n", iface, ref);
1496 return ref;
1499 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1501 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1502 ULONG ref = InterlockedDecrement(&This->ref);
1504 TRACE("(%p) refcount=%u\n", iface, ref);
1506 if (ref == 0)
1508 This->lock.DebugInfo->Spare[0] = 0;
1509 DeleteCriticalSection(&This->lock);
1510 if (This->png_ptr)
1511 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1512 if (This->stream)
1513 IStream_Release(This->stream);
1514 HeapFree(GetProcessHeap(), 0, This->data);
1515 HeapFree(GetProcessHeap(), 0, This);
1518 return ref;
1521 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1523 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1524 HRESULT hr;
1525 ULONG byteswritten;
1527 hr = IStream_Write(This->stream, data, length, &byteswritten);
1528 if (FAILED(hr) || byteswritten != length)
1530 ppng_error(png_ptr, "failed writing data");
1534 static void user_flush(png_structp png_ptr)
1538 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1539 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1541 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1542 jmp_buf jmpbuf;
1544 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1546 EnterCriticalSection(&This->lock);
1548 if (This->png_ptr)
1550 LeaveCriticalSection(&This->lock);
1551 return WINCODEC_ERR_WRONGSTATE;
1554 /* initialize libpng */
1555 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1556 if (!This->png_ptr)
1558 LeaveCriticalSection(&This->lock);
1559 return E_FAIL;
1562 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1563 if (!This->info_ptr)
1565 ppng_destroy_write_struct(&This->png_ptr, NULL);
1566 This->png_ptr = NULL;
1567 LeaveCriticalSection(&This->lock);
1568 return E_FAIL;
1571 IStream_AddRef(pIStream);
1572 This->stream = pIStream;
1574 /* set up setjmp/longjmp error handling */
1575 if (setjmp(jmpbuf))
1577 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1578 This->png_ptr = NULL;
1579 IStream_Release(This->stream);
1580 This->stream = NULL;
1581 LeaveCriticalSection(&This->lock);
1582 return E_FAIL;
1584 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1586 /* set up custom i/o handling */
1587 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1589 LeaveCriticalSection(&This->lock);
1591 return S_OK;
1594 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1595 GUID *pguidContainerFormat)
1597 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1598 return E_NOTIMPL;
1601 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1602 IWICBitmapEncoderInfo **ppIEncoderInfo)
1604 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1605 return E_NOTIMPL;
1608 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1609 UINT cCount, IWICColorContext **ppIColorContext)
1611 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1612 return E_NOTIMPL;
1615 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1617 TRACE("(%p,%p)\n", iface, pIPalette);
1618 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1621 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1623 TRACE("(%p,%p)\n", iface, pIThumbnail);
1624 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1627 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1629 TRACE("(%p,%p)\n", iface, pIPreview);
1630 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1633 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1634 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1636 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1637 HRESULT hr;
1638 PROPBAG2 opts[1]= {{0}};
1640 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1642 EnterCriticalSection(&This->lock);
1644 if (This->frame_count != 0)
1646 LeaveCriticalSection(&This->lock);
1647 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1650 if (!This->stream)
1652 LeaveCriticalSection(&This->lock);
1653 return WINCODEC_ERR_NOTINITIALIZED;
1656 opts[0].pstrName = (LPOLESTR)wszPngInterlaceOption;
1657 opts[0].vt = VT_BOOL;
1658 opts[0].dwType = PROPBAG2_TYPE_DATA;
1660 hr = CreatePropertyBag2(opts, 1, ppIEncoderOptions);
1661 if (FAILED(hr))
1663 LeaveCriticalSection(&This->lock);
1664 return hr;
1667 This->frame_count = 1;
1669 LeaveCriticalSection(&This->lock);
1671 IWICBitmapEncoder_AddRef(iface);
1672 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1674 return S_OK;
1677 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1679 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1680 TRACE("(%p)\n", iface);
1682 EnterCriticalSection(&This->lock);
1684 if (!This->frame_committed || This->committed)
1686 LeaveCriticalSection(&This->lock);
1687 return WINCODEC_ERR_WRONGSTATE;
1690 This->committed = TRUE;
1692 LeaveCriticalSection(&This->lock);
1694 return S_OK;
1697 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1698 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1700 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1701 return E_NOTIMPL;
1704 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
1705 PngEncoder_QueryInterface,
1706 PngEncoder_AddRef,
1707 PngEncoder_Release,
1708 PngEncoder_Initialize,
1709 PngEncoder_GetContainerFormat,
1710 PngEncoder_GetEncoderInfo,
1711 PngEncoder_SetColorContexts,
1712 PngEncoder_SetPalette,
1713 PngEncoder_SetThumbnail,
1714 PngEncoder_SetPreview,
1715 PngEncoder_CreateNewFrame,
1716 PngEncoder_Commit,
1717 PngEncoder_GetMetadataQueryWriter
1720 HRESULT PngEncoder_CreateInstance(REFIID iid, void** ppv)
1722 PngEncoder *This;
1723 HRESULT ret;
1725 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1727 *ppv = NULL;
1729 if (!libpng_handle && !load_libpng())
1731 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1732 return E_FAIL;
1735 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1736 if (!This) return E_OUTOFMEMORY;
1738 This->IWICBitmapEncoder_iface.lpVtbl = &PngEncoder_Vtbl;
1739 This->IWICBitmapFrameEncode_iface.lpVtbl = &PngEncoder_FrameVtbl;
1740 This->ref = 1;
1741 This->png_ptr = NULL;
1742 This->info_ptr = NULL;
1743 This->stream = NULL;
1744 This->frame_count = 0;
1745 This->frame_initialized = FALSE;
1746 This->format = NULL;
1747 This->info_written = FALSE;
1748 This->width = 0;
1749 This->height = 0;
1750 This->xres = 0.0;
1751 This->yres = 0.0;
1752 This->lines_written = 0;
1753 This->frame_committed = FALSE;
1754 This->committed = FALSE;
1755 This->data = NULL;
1756 InitializeCriticalSection(&This->lock);
1757 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngEncoder.lock");
1759 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
1760 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1762 return ret;
1765 #else /* !HAVE_PNG_H */
1767 HRESULT PngDecoder_CreateInstance(REFIID iid, void** ppv)
1769 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1770 return E_FAIL;
1773 HRESULT PngEncoder_CreateInstance(REFIID iid, void** ppv)
1775 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");
1776 return E_FAIL;
1779 #endif