Release 1.6-rc2.
[wine/testsucceed.git] / dlls / windowscodecs / pngformat.c
blob17085808efe3dcfabc4e70a793c2a458a2306be9
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 HRESULT read_png_chunk(IStream *stream, BYTE *type, BYTE **data, ULONG *data_size)
46 BYTE header[8];
47 HRESULT hr;
48 ULONG bytesread;
50 hr = IStream_Read(stream, header, 8, &bytesread);
51 if (FAILED(hr) || bytesread < 8)
53 if (SUCCEEDED(hr))
54 hr = E_FAIL;
55 return hr;
58 *data_size = header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3];
60 memcpy(type, &header[4], 4);
62 if (data)
64 *data = HeapAlloc(GetProcessHeap(), 0, *data_size);
65 if (!*data)
66 return E_OUTOFMEMORY;
68 hr = IStream_Read(stream, *data, *data_size, &bytesread);
70 if (FAILED(hr) || bytesread < *data_size)
72 if (SUCCEEDED(hr))
73 hr = E_FAIL;
74 HeapFree(GetProcessHeap(), 0, *data);
75 *data = NULL;
76 return hr;
79 /* Windows ignores CRC of the chunk */
82 return S_OK;
85 static HRESULT LoadTextMetadata(IStream *stream, const GUID *preferred_vendor,
86 DWORD persist_options, MetadataItem **items, DWORD *item_count)
88 HRESULT hr;
89 BYTE type[4];
90 BYTE *data;
91 ULONG data_size;
92 ULONG name_len, value_len;
93 BYTE *name_end_ptr;
94 LPSTR name, value;
95 MetadataItem *result;
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);
107 return E_FAIL;
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;
137 *items = result;
138 *item_count = 1;
140 HeapFree(GetProcessHeap(), 0, data);
142 return S_OK;
145 static const MetadataHandlerVtbl TextReader_Vtbl = {
147 &CLSID_WICPngTextMetadataReader,
148 LoadTextMetadata
151 HRESULT PngTextReader_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
153 return MetadataReader_Create(&TextReader_Vtbl, pUnkOuter, iid, ppv);
156 #ifdef SONAME_LIBPNG
158 static void *libpng_handle;
159 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
160 MAKE_FUNCPTR(png_create_read_struct);
161 MAKE_FUNCPTR(png_create_info_struct);
162 MAKE_FUNCPTR(png_create_write_struct);
163 MAKE_FUNCPTR(png_destroy_read_struct);
164 MAKE_FUNCPTR(png_destroy_write_struct);
165 MAKE_FUNCPTR(png_error);
166 MAKE_FUNCPTR(png_get_bit_depth);
167 MAKE_FUNCPTR(png_get_color_type);
168 MAKE_FUNCPTR(png_get_error_ptr);
169 MAKE_FUNCPTR(png_get_iCCP);
170 MAKE_FUNCPTR(png_get_image_height);
171 MAKE_FUNCPTR(png_get_image_width);
172 MAKE_FUNCPTR(png_get_io_ptr);
173 MAKE_FUNCPTR(png_get_pHYs);
174 MAKE_FUNCPTR(png_get_PLTE);
175 MAKE_FUNCPTR(png_get_tRNS);
176 MAKE_FUNCPTR(png_set_bgr);
177 MAKE_FUNCPTR(png_set_crc_action);
178 MAKE_FUNCPTR(png_set_error_fn);
179 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
180 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
181 #else
182 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8);
183 #endif
184 MAKE_FUNCPTR(png_set_filler);
185 MAKE_FUNCPTR(png_set_gray_to_rgb);
186 MAKE_FUNCPTR(png_set_IHDR);
187 MAKE_FUNCPTR(png_set_pHYs);
188 MAKE_FUNCPTR(png_set_read_fn);
189 MAKE_FUNCPTR(png_set_strip_16);
190 MAKE_FUNCPTR(png_set_tRNS_to_alpha);
191 MAKE_FUNCPTR(png_set_write_fn);
192 MAKE_FUNCPTR(png_read_end);
193 MAKE_FUNCPTR(png_read_image);
194 MAKE_FUNCPTR(png_read_info);
195 MAKE_FUNCPTR(png_write_end);
196 MAKE_FUNCPTR(png_write_info);
197 MAKE_FUNCPTR(png_write_rows);
198 #undef MAKE_FUNCPTR
200 static void *load_libpng(void)
202 if((libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
204 #define LOAD_FUNCPTR(f) \
205 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
206 libpng_handle = NULL; \
207 return NULL; \
209 LOAD_FUNCPTR(png_create_read_struct);
210 LOAD_FUNCPTR(png_create_info_struct);
211 LOAD_FUNCPTR(png_create_write_struct);
212 LOAD_FUNCPTR(png_destroy_read_struct);
213 LOAD_FUNCPTR(png_destroy_write_struct);
214 LOAD_FUNCPTR(png_error);
215 LOAD_FUNCPTR(png_get_bit_depth);
216 LOAD_FUNCPTR(png_get_color_type);
217 LOAD_FUNCPTR(png_get_error_ptr);
218 LOAD_FUNCPTR(png_get_iCCP);
219 LOAD_FUNCPTR(png_get_image_height);
220 LOAD_FUNCPTR(png_get_image_width);
221 LOAD_FUNCPTR(png_get_io_ptr);
222 LOAD_FUNCPTR(png_get_pHYs);
223 LOAD_FUNCPTR(png_get_PLTE);
224 LOAD_FUNCPTR(png_get_tRNS);
225 LOAD_FUNCPTR(png_set_bgr);
226 LOAD_FUNCPTR(png_set_crc_action);
227 LOAD_FUNCPTR(png_set_error_fn);
228 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
229 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
230 #else
231 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8);
232 #endif
233 LOAD_FUNCPTR(png_set_filler);
234 LOAD_FUNCPTR(png_set_gray_to_rgb);
235 LOAD_FUNCPTR(png_set_IHDR);
236 LOAD_FUNCPTR(png_set_pHYs);
237 LOAD_FUNCPTR(png_set_read_fn);
238 LOAD_FUNCPTR(png_set_strip_16);
239 LOAD_FUNCPTR(png_set_tRNS_to_alpha);
240 LOAD_FUNCPTR(png_set_write_fn);
241 LOAD_FUNCPTR(png_read_end);
242 LOAD_FUNCPTR(png_read_image);
243 LOAD_FUNCPTR(png_read_info);
244 LOAD_FUNCPTR(png_write_end);
245 LOAD_FUNCPTR(png_write_info);
246 LOAD_FUNCPTR(png_write_rows);
248 #undef LOAD_FUNCPTR
250 return libpng_handle;
253 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
255 jmp_buf *pjmpbuf;
257 /* This uses setjmp/longjmp just like the default. We can't use the
258 * default because there's no way to access the jmp buffer in the png_struct
259 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
260 WARN("PNG error: %s\n", debugstr_a(error_message));
261 pjmpbuf = ppng_get_error_ptr(png_ptr);
262 longjmp(*pjmpbuf, 1);
265 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
267 WARN("PNG warning: %s\n", debugstr_a(warning_message));
270 typedef struct {
271 IWICBitmapDecoder IWICBitmapDecoder_iface;
272 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
273 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
274 LONG ref;
275 png_structp png_ptr;
276 png_infop info_ptr;
277 png_infop end_info;
278 BOOL initialized;
279 int bpp;
280 int width, height;
281 UINT stride;
282 const WICPixelFormatGUID *format;
283 BYTE *image_bits;
284 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
285 } PngDecoder;
287 static inline PngDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
289 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapDecoder_iface);
292 static inline PngDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
294 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapFrameDecode_iface);
297 static inline PngDecoder *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
299 return CONTAINING_RECORD(iface, PngDecoder, IWICMetadataBlockReader_iface);
302 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
304 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
305 void **ppv)
307 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
308 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
310 if (!ppv) return E_INVALIDARG;
312 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
314 *ppv = &This->IWICBitmapDecoder_iface;
316 else
318 *ppv = NULL;
319 return E_NOINTERFACE;
322 IUnknown_AddRef((IUnknown*)*ppv);
323 return S_OK;
326 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
328 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
329 ULONG ref = InterlockedIncrement(&This->ref);
331 TRACE("(%p) refcount=%u\n", iface, ref);
333 return ref;
336 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
338 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
339 ULONG ref = InterlockedDecrement(&This->ref);
341 TRACE("(%p) refcount=%u\n", iface, ref);
343 if (ref == 0)
345 if (This->png_ptr)
346 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
347 This->lock.DebugInfo->Spare[0] = 0;
348 DeleteCriticalSection(&This->lock);
349 HeapFree(GetProcessHeap(), 0, This->image_bits);
350 HeapFree(GetProcessHeap(), 0, This);
353 return ref;
356 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
357 DWORD *capability)
359 HRESULT hr;
361 TRACE("(%p,%p,%p)\n", iface, stream, capability);
363 if (!stream || !capability) return E_INVALIDARG;
365 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
366 if (hr != S_OK) return hr;
368 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
369 WICBitmapDecoderCapabilityCanDecodeSomeImages;
370 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
371 return S_OK;
374 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
376 IStream *stream = ppng_get_io_ptr(png_ptr);
377 HRESULT hr;
378 ULONG bytesread;
380 hr = IStream_Read(stream, data, length, &bytesread);
381 if (FAILED(hr) || bytesread != length)
383 ppng_error(png_ptr, "failed reading data");
387 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
388 WICDecodeOptions cacheOptions)
390 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
391 LARGE_INTEGER seek;
392 HRESULT hr=S_OK;
393 png_bytep *row_pointers=NULL;
394 UINT image_size;
395 UINT i;
396 int color_type, bit_depth;
397 png_bytep trans;
398 int num_trans;
399 png_uint_32 transparency;
400 png_color_16p trans_values;
401 jmp_buf jmpbuf;
403 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
405 EnterCriticalSection(&This->lock);
407 /* initialize libpng */
408 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
409 if (!This->png_ptr)
411 hr = E_FAIL;
412 goto end;
415 This->info_ptr = ppng_create_info_struct(This->png_ptr);
416 if (!This->info_ptr)
418 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
419 This->png_ptr = NULL;
420 hr = E_FAIL;
421 goto end;
424 This->end_info = ppng_create_info_struct(This->png_ptr);
425 if (!This->info_ptr)
427 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
428 This->png_ptr = NULL;
429 hr = E_FAIL;
430 goto end;
433 /* set up setjmp/longjmp error handling */
434 if (setjmp(jmpbuf))
436 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
437 HeapFree(GetProcessHeap(), 0, row_pointers);
438 This->png_ptr = NULL;
439 hr = E_FAIL;
440 goto end;
442 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
443 ppng_set_crc_action(This->png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
445 /* seek to the start of the stream */
446 seek.QuadPart = 0;
447 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
448 if (FAILED(hr)) goto end;
450 /* set up custom i/o handling */
451 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
453 /* read the header */
454 ppng_read_info(This->png_ptr, This->info_ptr);
456 /* choose a pixel format */
457 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
458 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
460 /* check for color-keyed alpha */
461 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
463 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
465 /* expand to RGBA */
466 if (color_type == PNG_COLOR_TYPE_GRAY)
468 if (bit_depth < 8)
470 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
471 ppng_set_expand_gray_1_2_4_to_8(This->png_ptr);
472 #else
473 ppng_set_gray_1_2_4_to_8(This->png_ptr);
474 #endif
475 bit_depth = 8;
477 ppng_set_gray_to_rgb(This->png_ptr);
479 ppng_set_tRNS_to_alpha(This->png_ptr);
480 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
483 switch (color_type)
485 case PNG_COLOR_TYPE_GRAY:
486 This->bpp = bit_depth;
487 switch (bit_depth)
489 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
490 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
491 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
492 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
493 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
494 default:
495 ERR("invalid grayscale bit depth: %i\n", bit_depth);
496 hr = E_FAIL;
497 goto end;
499 break;
500 case PNG_COLOR_TYPE_GRAY_ALPHA:
501 /* WIC does not support grayscale alpha formats so use RGBA */
502 ppng_set_gray_to_rgb(This->png_ptr);
503 /* fall through */
504 case PNG_COLOR_TYPE_RGB_ALPHA:
505 This->bpp = bit_depth * 4;
506 switch (bit_depth)
508 case 8:
509 ppng_set_bgr(This->png_ptr);
510 This->format = &GUID_WICPixelFormat32bppBGRA;
511 break;
512 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
513 default:
514 ERR("invalid RGBA bit depth: %i\n", bit_depth);
515 hr = E_FAIL;
516 goto end;
518 break;
519 case PNG_COLOR_TYPE_PALETTE:
520 This->bpp = bit_depth;
521 switch (bit_depth)
523 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
524 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
525 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
526 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
527 default:
528 ERR("invalid indexed color bit depth: %i\n", bit_depth);
529 hr = E_FAIL;
530 goto end;
532 break;
533 case PNG_COLOR_TYPE_RGB:
534 This->bpp = bit_depth * 3;
535 switch (bit_depth)
537 case 8:
538 ppng_set_bgr(This->png_ptr);
539 This->format = &GUID_WICPixelFormat24bppBGR;
540 break;
541 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
542 default:
543 ERR("invalid RGB color bit depth: %i\n", bit_depth);
544 hr = E_FAIL;
545 goto end;
547 break;
548 default:
549 ERR("invalid color type %i\n", color_type);
550 hr = E_FAIL;
551 goto end;
554 /* read the image data */
555 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
556 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
557 This->stride = This->width * This->bpp;
558 image_size = This->stride * This->height;
560 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
561 if (!This->image_bits)
563 hr = E_OUTOFMEMORY;
564 goto end;
567 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
568 if (!row_pointers)
570 hr = E_OUTOFMEMORY;
571 goto end;
574 for (i=0; i<This->height; i++)
575 row_pointers[i] = This->image_bits + i * This->stride;
577 ppng_read_image(This->png_ptr, row_pointers);
579 HeapFree(GetProcessHeap(), 0, row_pointers);
580 row_pointers = NULL;
582 ppng_read_end(This->png_ptr, This->end_info);
584 This->initialized = TRUE;
586 end:
588 LeaveCriticalSection(&This->lock);
590 return hr;
593 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
594 GUID *pguidContainerFormat)
596 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
597 return S_OK;
600 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
601 IWICBitmapDecoderInfo **ppIDecoderInfo)
603 HRESULT hr;
604 IWICComponentInfo *compinfo;
606 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
608 hr = CreateComponentInfo(&CLSID_WICPngDecoder, &compinfo);
609 if (FAILED(hr)) return hr;
611 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
612 (void**)ppIDecoderInfo);
614 IWICComponentInfo_Release(compinfo);
616 return hr;
619 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
620 IWICPalette *pIPalette)
622 FIXME("(%p,%p): stub\n", iface, pIPalette);
623 return E_NOTIMPL;
626 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
627 IWICMetadataQueryReader **ppIMetadataQueryReader)
629 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
630 return E_NOTIMPL;
633 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
634 IWICBitmapSource **ppIBitmapSource)
636 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
638 if (!ppIBitmapSource) return E_INVALIDARG;
640 *ppIBitmapSource = NULL;
641 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
644 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
645 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
647 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
648 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
651 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
652 IWICBitmapSource **ppIThumbnail)
654 TRACE("(%p,%p)\n", iface, ppIThumbnail);
656 if (!ppIThumbnail) return E_INVALIDARG;
658 *ppIThumbnail = NULL;
659 return WINCODEC_ERR_CODECNOTHUMBNAIL;
662 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
663 UINT *pCount)
665 if (!pCount) return E_INVALIDARG;
667 *pCount = 1;
668 return S_OK;
671 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
672 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
674 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
675 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
677 if (!This->initialized) return WINCODEC_ERR_FRAMEMISSING;
679 if (index != 0) return E_INVALIDARG;
681 IWICBitmapDecoder_AddRef(iface);
683 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
685 return S_OK;
688 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
689 PngDecoder_QueryInterface,
690 PngDecoder_AddRef,
691 PngDecoder_Release,
692 PngDecoder_QueryCapability,
693 PngDecoder_Initialize,
694 PngDecoder_GetContainerFormat,
695 PngDecoder_GetDecoderInfo,
696 PngDecoder_CopyPalette,
697 PngDecoder_GetMetadataQueryReader,
698 PngDecoder_GetPreview,
699 PngDecoder_GetColorContexts,
700 PngDecoder_GetThumbnail,
701 PngDecoder_GetFrameCount,
702 PngDecoder_GetFrame
705 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
706 void **ppv)
708 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
709 if (!ppv) return E_INVALIDARG;
711 if (IsEqualIID(&IID_IUnknown, iid) ||
712 IsEqualIID(&IID_IWICBitmapSource, iid) ||
713 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
715 *ppv = &This->IWICBitmapFrameDecode_iface;
717 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
719 *ppv = &This->IWICMetadataBlockReader_iface;
721 else
723 *ppv = NULL;
724 return E_NOINTERFACE;
727 IUnknown_AddRef((IUnknown*)*ppv);
728 return S_OK;
731 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
733 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
734 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
737 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
739 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
740 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
743 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
744 UINT *puiWidth, UINT *puiHeight)
746 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
747 *puiWidth = This->width;
748 *puiHeight = This->height;
749 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
750 return S_OK;
753 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
754 WICPixelFormatGUID *pPixelFormat)
756 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
757 TRACE("(%p,%p)\n", iface, pPixelFormat);
759 memcpy(pPixelFormat, This->format, sizeof(GUID));
761 return S_OK;
764 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
765 double *pDpiX, double *pDpiY)
767 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
768 png_uint_32 ret, xres, yres;
769 int unit_type;
771 EnterCriticalSection(&This->lock);
773 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
775 if (ret && unit_type == PNG_RESOLUTION_METER)
777 *pDpiX = xres * 0.0254;
778 *pDpiY = yres * 0.0254;
780 else
782 WARN("no pHYs block present\n");
783 *pDpiX = *pDpiY = 96.0;
786 LeaveCriticalSection(&This->lock);
788 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
790 return S_OK;
793 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
794 IWICPalette *pIPalette)
796 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
797 png_uint_32 ret;
798 png_colorp png_palette;
799 int num_palette;
800 WICColor palette[256];
801 png_bytep trans_alpha;
802 int num_trans;
803 png_color_16p trans_values;
804 int i;
805 HRESULT hr=S_OK;
807 TRACE("(%p,%p)\n", iface, pIPalette);
809 EnterCriticalSection(&This->lock);
811 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
812 if (!ret)
814 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
815 goto end;
818 if (num_palette > 256)
820 ERR("palette has %i colors?!\n", num_palette);
821 hr = E_FAIL;
822 goto end;
825 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans_alpha, &num_trans, &trans_values);
826 if (!ret) num_trans = 0;
828 for (i=0; i<num_palette; i++)
830 BYTE alpha = (i < num_trans) ? trans_alpha[i] : 0xff;
831 palette[i] = (alpha << 24 |
832 png_palette[i].red << 16|
833 png_palette[i].green << 8|
834 png_palette[i].blue);
837 end:
839 LeaveCriticalSection(&This->lock);
841 if (SUCCEEDED(hr))
842 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
844 return hr;
847 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
848 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
850 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
851 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
853 return copy_pixels(This->bpp, This->image_bits,
854 This->width, This->height, This->stride,
855 prc, cbStride, cbBufferSize, pbBuffer);
858 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
859 IWICMetadataQueryReader **ppIMetadataQueryReader)
861 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
862 return E_NOTIMPL;
865 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
866 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
868 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
869 png_charp name, profile;
870 png_uint_32 len;
871 int compression_type;
872 HRESULT hr;
874 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
876 if (!pcActualCount) return E_INVALIDARG;
878 EnterCriticalSection(&This->lock);
880 if (ppng_get_iCCP(This->png_ptr, This->info_ptr, &name, &compression_type, &profile, &len))
882 if (cCount && ppIColorContexts)
884 hr = IWICColorContext_InitializeFromMemory(*ppIColorContexts, (const BYTE *)profile, len);
885 if (FAILED(hr))
887 LeaveCriticalSection(&This->lock);
888 return hr;
891 *pcActualCount = 1;
893 else
894 *pcActualCount = 0;
896 LeaveCriticalSection(&This->lock);
898 return S_OK;
901 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
902 IWICBitmapSource **ppIThumbnail)
904 TRACE("(%p,%p)\n", iface, ppIThumbnail);
906 if (!ppIThumbnail) return E_INVALIDARG;
908 *ppIThumbnail = NULL;
909 return WINCODEC_ERR_CODECNOTHUMBNAIL;
912 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
913 PngDecoder_Frame_QueryInterface,
914 PngDecoder_Frame_AddRef,
915 PngDecoder_Frame_Release,
916 PngDecoder_Frame_GetSize,
917 PngDecoder_Frame_GetPixelFormat,
918 PngDecoder_Frame_GetResolution,
919 PngDecoder_Frame_CopyPalette,
920 PngDecoder_Frame_CopyPixels,
921 PngDecoder_Frame_GetMetadataQueryReader,
922 PngDecoder_Frame_GetColorContexts,
923 PngDecoder_Frame_GetThumbnail
926 static HRESULT WINAPI PngDecoder_Block_QueryInterface(IWICMetadataBlockReader *iface, REFIID iid,
927 void **ppv)
929 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
930 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
933 static ULONG WINAPI PngDecoder_Block_AddRef(IWICMetadataBlockReader *iface)
935 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
936 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
939 static ULONG WINAPI PngDecoder_Block_Release(IWICMetadataBlockReader *iface)
941 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
942 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
945 static HRESULT WINAPI PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
946 GUID *pguidContainerFormat)
948 if (!pguidContainerFormat) return E_INVALIDARG;
949 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
950 return S_OK;
953 static HRESULT WINAPI PngDecoder_Block_GetCount(IWICMetadataBlockReader *iface,
954 UINT *pcCount)
956 FIXME("%p,%p: stub\n", iface, pcCount);
957 return E_NOTIMPL;
960 static HRESULT WINAPI PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
961 UINT nIndex, IWICMetadataReader **ppIMetadataReader)
963 FIXME("%p,%d,%p\n", iface, nIndex, ppIMetadataReader);
964 return E_NOTIMPL;
967 static HRESULT WINAPI PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader *iface,
968 IEnumUnknown **ppIEnumMetadata)
970 FIXME("%p,%p\n", iface, ppIEnumMetadata);
971 return E_NOTIMPL;
974 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl = {
975 PngDecoder_Block_QueryInterface,
976 PngDecoder_Block_AddRef,
977 PngDecoder_Block_Release,
978 PngDecoder_Block_GetContainerFormat,
979 PngDecoder_Block_GetCount,
980 PngDecoder_Block_GetReaderByIndex,
981 PngDecoder_Block_GetEnumerator,
984 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
986 PngDecoder *This;
987 HRESULT ret;
989 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
991 *ppv = NULL;
993 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
995 if (!libpng_handle && !load_libpng())
997 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
998 return E_FAIL;
1001 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
1002 if (!This) return E_OUTOFMEMORY;
1004 This->IWICBitmapDecoder_iface.lpVtbl = &PngDecoder_Vtbl;
1005 This->IWICBitmapFrameDecode_iface.lpVtbl = &PngDecoder_FrameVtbl;
1006 This->IWICMetadataBlockReader_iface.lpVtbl = &PngDecoder_BlockVtbl;
1007 This->ref = 1;
1008 This->png_ptr = NULL;
1009 This->info_ptr = NULL;
1010 This->end_info = NULL;
1011 This->initialized = FALSE;
1012 This->image_bits = NULL;
1013 InitializeCriticalSection(&This->lock);
1014 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngDecoder.lock");
1016 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1017 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1019 return ret;
1022 struct png_pixelformat {
1023 const WICPixelFormatGUID *guid;
1024 UINT bpp;
1025 int bit_depth;
1026 int color_type;
1027 BOOL remove_filler;
1028 BOOL swap_rgb;
1031 static const struct png_pixelformat formats[] = {
1032 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
1033 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
1034 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
1035 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
1036 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
1037 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
1038 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
1039 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
1040 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
1041 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
1042 {NULL},
1045 typedef struct PngEncoder {
1046 IWICBitmapEncoder IWICBitmapEncoder_iface;
1047 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1048 LONG ref;
1049 IStream *stream;
1050 png_structp png_ptr;
1051 png_infop info_ptr;
1052 UINT frame_count;
1053 BOOL frame_initialized;
1054 const struct png_pixelformat *format;
1055 BOOL info_written;
1056 UINT width, height;
1057 double xres, yres;
1058 UINT lines_written;
1059 BOOL frame_committed;
1060 BOOL committed;
1061 CRITICAL_SECTION lock;
1062 } PngEncoder;
1064 static inline PngEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1066 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapEncoder_iface);
1069 static inline PngEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1071 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapFrameEncode_iface);
1074 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1075 void **ppv)
1077 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1078 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1080 if (!ppv) return E_INVALIDARG;
1082 if (IsEqualIID(&IID_IUnknown, iid) ||
1083 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1085 *ppv = &This->IWICBitmapFrameEncode_iface;
1087 else
1089 *ppv = NULL;
1090 return E_NOINTERFACE;
1093 IUnknown_AddRef((IUnknown*)*ppv);
1094 return S_OK;
1097 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1099 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1100 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
1103 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
1105 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1106 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1109 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1110 IPropertyBag2 *pIEncoderOptions)
1112 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1113 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1115 EnterCriticalSection(&This->lock);
1117 if (This->frame_initialized)
1119 LeaveCriticalSection(&This->lock);
1120 return WINCODEC_ERR_WRONGSTATE;
1123 This->frame_initialized = TRUE;
1125 LeaveCriticalSection(&This->lock);
1127 return S_OK;
1130 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1131 UINT uiWidth, UINT uiHeight)
1133 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1134 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1136 EnterCriticalSection(&This->lock);
1138 if (!This->frame_initialized || This->info_written)
1140 LeaveCriticalSection(&This->lock);
1141 return WINCODEC_ERR_WRONGSTATE;
1144 This->width = uiWidth;
1145 This->height = uiHeight;
1147 LeaveCriticalSection(&This->lock);
1149 return S_OK;
1152 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1153 double dpiX, double dpiY)
1155 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1156 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1158 EnterCriticalSection(&This->lock);
1160 if (!This->frame_initialized || This->info_written)
1162 LeaveCriticalSection(&This->lock);
1163 return WINCODEC_ERR_WRONGSTATE;
1166 This->xres = dpiX;
1167 This->yres = dpiY;
1169 LeaveCriticalSection(&This->lock);
1171 return S_OK;
1174 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1175 WICPixelFormatGUID *pPixelFormat)
1177 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1178 int i;
1179 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1181 EnterCriticalSection(&This->lock);
1183 if (!This->frame_initialized || This->info_written)
1185 LeaveCriticalSection(&This->lock);
1186 return WINCODEC_ERR_WRONGSTATE;
1189 for (i=0; formats[i].guid; i++)
1191 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1192 break;
1195 if (!formats[i].guid) i = 0;
1197 This->format = &formats[i];
1198 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1200 LeaveCriticalSection(&This->lock);
1202 return S_OK;
1205 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1206 UINT cCount, IWICColorContext **ppIColorContext)
1208 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1209 return E_NOTIMPL;
1212 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1213 IWICPalette *pIPalette)
1215 FIXME("(%p,%p): stub\n", iface, pIPalette);
1216 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1219 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1220 IWICBitmapSource *pIThumbnail)
1222 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1223 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1226 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1227 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1229 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1230 png_byte **row_pointers=NULL;
1231 UINT i;
1232 jmp_buf jmpbuf;
1233 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1235 EnterCriticalSection(&This->lock);
1237 if (!This->frame_initialized || !This->width || !This->height || !This->format)
1239 LeaveCriticalSection(&This->lock);
1240 return WINCODEC_ERR_WRONGSTATE;
1243 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1245 LeaveCriticalSection(&This->lock);
1246 return E_INVALIDARG;
1249 /* set up setjmp/longjmp error handling */
1250 if (setjmp(jmpbuf))
1252 LeaveCriticalSection(&This->lock);
1253 HeapFree(GetProcessHeap(), 0, row_pointers);
1254 return E_FAIL;
1256 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1258 if (!This->info_written)
1260 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
1261 This->format->bit_depth, This->format->color_type, PNG_INTERLACE_NONE,
1262 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1264 if (This->xres != 0.0 && This->yres != 0.0)
1266 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
1267 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
1270 ppng_write_info(This->png_ptr, This->info_ptr);
1272 if (This->format->remove_filler)
1273 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1275 if (This->format->swap_rgb)
1276 ppng_set_bgr(This->png_ptr);
1278 This->info_written = TRUE;
1281 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1282 if (!row_pointers)
1284 LeaveCriticalSection(&This->lock);
1285 return E_OUTOFMEMORY;
1288 for (i=0; i<lineCount; i++)
1289 row_pointers[i] = pbPixels + cbStride * i;
1291 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1292 This->lines_written += lineCount;
1294 LeaveCriticalSection(&This->lock);
1296 HeapFree(GetProcessHeap(), 0, row_pointers);
1298 return S_OK;
1301 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1302 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1304 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1305 HRESULT hr;
1306 WICRect rc;
1307 WICPixelFormatGUID guid;
1308 UINT stride;
1309 BYTE *pixeldata;
1310 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1312 if (!This->frame_initialized || !This->width || !This->height)
1313 return WINCODEC_ERR_WRONGSTATE;
1315 if (!This->format)
1317 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1318 if (FAILED(hr)) return hr;
1319 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1320 if (FAILED(hr)) return hr;
1323 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1324 if (FAILED(hr)) return hr;
1325 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1327 /* FIXME: should use WICConvertBitmapSource to convert */
1328 ERR("format %s unsupported\n", debugstr_guid(&guid));
1329 return E_FAIL;
1332 if (This->xres == 0.0 || This->yres == 0.0)
1334 double xres, yres;
1335 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1336 if (FAILED(hr)) return hr;
1337 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1338 if (FAILED(hr)) return hr;
1341 if (!prc)
1343 UINT width, height;
1344 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1345 if (FAILED(hr)) return hr;
1346 rc.X = 0;
1347 rc.Y = 0;
1348 rc.Width = width;
1349 rc.Height = height;
1350 prc = &rc;
1353 if (prc->Width != This->width) return E_INVALIDARG;
1355 stride = (This->format->bpp * This->width + 7)/8;
1357 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1358 if (!pixeldata) return E_OUTOFMEMORY;
1360 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1361 stride*prc->Height, pixeldata);
1363 if (SUCCEEDED(hr))
1365 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1366 stride*prc->Height, pixeldata);
1369 HeapFree(GetProcessHeap(), 0, pixeldata);
1371 return hr;
1374 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1376 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1377 jmp_buf jmpbuf;
1378 TRACE("(%p)\n", iface);
1380 EnterCriticalSection(&This->lock);
1382 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1384 LeaveCriticalSection(&This->lock);
1385 return WINCODEC_ERR_WRONGSTATE;
1388 /* set up setjmp/longjmp error handling */
1389 if (setjmp(jmpbuf))
1391 LeaveCriticalSection(&This->lock);
1392 return E_FAIL;
1394 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1396 ppng_write_end(This->png_ptr, This->info_ptr);
1398 This->frame_committed = TRUE;
1400 LeaveCriticalSection(&This->lock);
1402 return S_OK;
1405 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1406 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1408 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1409 return E_NOTIMPL;
1412 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1413 PngFrameEncode_QueryInterface,
1414 PngFrameEncode_AddRef,
1415 PngFrameEncode_Release,
1416 PngFrameEncode_Initialize,
1417 PngFrameEncode_SetSize,
1418 PngFrameEncode_SetResolution,
1419 PngFrameEncode_SetPixelFormat,
1420 PngFrameEncode_SetColorContexts,
1421 PngFrameEncode_SetPalette,
1422 PngFrameEncode_SetThumbnail,
1423 PngFrameEncode_WritePixels,
1424 PngFrameEncode_WriteSource,
1425 PngFrameEncode_Commit,
1426 PngFrameEncode_GetMetadataQueryWriter
1429 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1430 void **ppv)
1432 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1433 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1435 if (!ppv) return E_INVALIDARG;
1437 if (IsEqualIID(&IID_IUnknown, iid) ||
1438 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1440 *ppv = &This->IWICBitmapEncoder_iface;
1442 else
1444 *ppv = NULL;
1445 return E_NOINTERFACE;
1448 IUnknown_AddRef((IUnknown*)*ppv);
1449 return S_OK;
1452 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1454 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1455 ULONG ref = InterlockedIncrement(&This->ref);
1457 TRACE("(%p) refcount=%u\n", iface, ref);
1459 return ref;
1462 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1464 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1465 ULONG ref = InterlockedDecrement(&This->ref);
1467 TRACE("(%p) refcount=%u\n", iface, ref);
1469 if (ref == 0)
1471 This->lock.DebugInfo->Spare[0] = 0;
1472 DeleteCriticalSection(&This->lock);
1473 if (This->png_ptr)
1474 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1475 if (This->stream)
1476 IStream_Release(This->stream);
1477 HeapFree(GetProcessHeap(), 0, This);
1480 return ref;
1483 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1485 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1486 HRESULT hr;
1487 ULONG byteswritten;
1489 hr = IStream_Write(This->stream, data, length, &byteswritten);
1490 if (FAILED(hr) || byteswritten != length)
1492 ppng_error(png_ptr, "failed writing data");
1496 static void user_flush(png_structp png_ptr)
1500 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1501 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1503 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1504 jmp_buf jmpbuf;
1506 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1508 EnterCriticalSection(&This->lock);
1510 if (This->png_ptr)
1512 LeaveCriticalSection(&This->lock);
1513 return WINCODEC_ERR_WRONGSTATE;
1516 /* initialize libpng */
1517 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1518 if (!This->png_ptr)
1520 LeaveCriticalSection(&This->lock);
1521 return E_FAIL;
1524 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1525 if (!This->info_ptr)
1527 ppng_destroy_write_struct(&This->png_ptr, NULL);
1528 This->png_ptr = NULL;
1529 LeaveCriticalSection(&This->lock);
1530 return E_FAIL;
1533 IStream_AddRef(pIStream);
1534 This->stream = pIStream;
1536 /* set up setjmp/longjmp error handling */
1537 if (setjmp(jmpbuf))
1539 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1540 This->png_ptr = NULL;
1541 IStream_Release(This->stream);
1542 This->stream = NULL;
1543 LeaveCriticalSection(&This->lock);
1544 return E_FAIL;
1546 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1548 /* set up custom i/o handling */
1549 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1551 LeaveCriticalSection(&This->lock);
1553 return S_OK;
1556 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1557 GUID *pguidContainerFormat)
1559 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1560 return E_NOTIMPL;
1563 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1564 IWICBitmapEncoderInfo **ppIEncoderInfo)
1566 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1567 return E_NOTIMPL;
1570 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1571 UINT cCount, IWICColorContext **ppIColorContext)
1573 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1574 return E_NOTIMPL;
1577 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1579 TRACE("(%p,%p)\n", iface, pIPalette);
1580 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1583 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1585 TRACE("(%p,%p)\n", iface, pIThumbnail);
1586 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1589 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1591 TRACE("(%p,%p)\n", iface, pIPreview);
1592 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1595 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1596 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1598 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1599 HRESULT hr;
1600 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1602 EnterCriticalSection(&This->lock);
1604 if (This->frame_count != 0)
1606 LeaveCriticalSection(&This->lock);
1607 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1610 if (!This->stream)
1612 LeaveCriticalSection(&This->lock);
1613 return WINCODEC_ERR_NOTINITIALIZED;
1616 hr = CreatePropertyBag2(NULL, 0, ppIEncoderOptions);
1617 if (FAILED(hr))
1619 LeaveCriticalSection(&This->lock);
1620 return hr;
1623 This->frame_count = 1;
1625 LeaveCriticalSection(&This->lock);
1627 IWICBitmapEncoder_AddRef(iface);
1628 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1630 return S_OK;
1633 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1635 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1636 TRACE("(%p)\n", iface);
1638 EnterCriticalSection(&This->lock);
1640 if (!This->frame_committed || This->committed)
1642 LeaveCriticalSection(&This->lock);
1643 return WINCODEC_ERR_WRONGSTATE;
1646 This->committed = TRUE;
1648 LeaveCriticalSection(&This->lock);
1650 return S_OK;
1653 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1654 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1656 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1657 return E_NOTIMPL;
1660 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
1661 PngEncoder_QueryInterface,
1662 PngEncoder_AddRef,
1663 PngEncoder_Release,
1664 PngEncoder_Initialize,
1665 PngEncoder_GetContainerFormat,
1666 PngEncoder_GetEncoderInfo,
1667 PngEncoder_SetColorContexts,
1668 PngEncoder_SetPalette,
1669 PngEncoder_SetThumbnail,
1670 PngEncoder_SetPreview,
1671 PngEncoder_CreateNewFrame,
1672 PngEncoder_Commit,
1673 PngEncoder_GetMetadataQueryWriter
1676 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1678 PngEncoder *This;
1679 HRESULT ret;
1681 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1683 *ppv = NULL;
1685 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1687 if (!libpng_handle && !load_libpng())
1689 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1690 return E_FAIL;
1693 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1694 if (!This) return E_OUTOFMEMORY;
1696 This->IWICBitmapEncoder_iface.lpVtbl = &PngEncoder_Vtbl;
1697 This->IWICBitmapFrameEncode_iface.lpVtbl = &PngEncoder_FrameVtbl;
1698 This->ref = 1;
1699 This->png_ptr = NULL;
1700 This->info_ptr = NULL;
1701 This->stream = NULL;
1702 This->frame_count = 0;
1703 This->frame_initialized = FALSE;
1704 This->format = NULL;
1705 This->info_written = FALSE;
1706 This->width = 0;
1707 This->height = 0;
1708 This->xres = 0.0;
1709 This->yres = 0.0;
1710 This->lines_written = 0;
1711 This->frame_committed = FALSE;
1712 This->committed = FALSE;
1713 InitializeCriticalSection(&This->lock);
1714 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngEncoder.lock");
1716 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
1717 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1719 return ret;
1722 #else /* !HAVE_PNG_H */
1724 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1726 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1727 return E_FAIL;
1730 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1732 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");
1733 return E_FAIL;
1736 #endif