gdi32: Fix arguments for OSMesaMakeCurrent when using 16 bit formats.
[wine.git] / dlls / windowscodecs / pngformat.c
blobb3e6cc65481fbbc8b6298d2e1970d3bb0431460d
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 CRITICAL_SECTION init_png_cs;
204 static CRITICAL_SECTION_DEBUG init_png_cs_debug =
206 0, 0, &init_png_cs,
207 { &init_png_cs_debug.ProcessLocksList,
208 &init_png_cs_debug.ProcessLocksList },
209 0, 0, { (DWORD_PTR)(__FILE__ ": init_png_cs") }
211 static CRITICAL_SECTION init_png_cs = { &init_png_cs_debug, -1, 0, 0, 0, 0 };
213 static void *load_libpng(void)
215 void *result;
217 EnterCriticalSection(&init_png_cs);
219 if(!libpng_handle && (libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
221 #define LOAD_FUNCPTR(f) \
222 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
223 libpng_handle = NULL; \
224 LeaveCriticalSection(&init_png_cs); \
225 return NULL; \
227 LOAD_FUNCPTR(png_create_read_struct);
228 LOAD_FUNCPTR(png_create_info_struct);
229 LOAD_FUNCPTR(png_create_write_struct);
230 LOAD_FUNCPTR(png_destroy_read_struct);
231 LOAD_FUNCPTR(png_destroy_write_struct);
232 LOAD_FUNCPTR(png_error);
233 LOAD_FUNCPTR(png_get_bit_depth);
234 LOAD_FUNCPTR(png_get_color_type);
235 LOAD_FUNCPTR(png_get_error_ptr);
236 LOAD_FUNCPTR(png_get_iCCP);
237 LOAD_FUNCPTR(png_get_image_height);
238 LOAD_FUNCPTR(png_get_image_width);
239 LOAD_FUNCPTR(png_get_io_ptr);
240 LOAD_FUNCPTR(png_get_pHYs);
241 LOAD_FUNCPTR(png_get_PLTE);
242 LOAD_FUNCPTR(png_get_tRNS);
243 LOAD_FUNCPTR(png_set_bgr);
244 LOAD_FUNCPTR(png_set_crc_action);
245 LOAD_FUNCPTR(png_set_error_fn);
246 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
247 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
248 #else
249 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8);
250 #endif
251 LOAD_FUNCPTR(png_set_filler);
252 LOAD_FUNCPTR(png_set_gray_to_rgb);
253 LOAD_FUNCPTR(png_set_interlace_handling);
254 LOAD_FUNCPTR(png_set_IHDR);
255 LOAD_FUNCPTR(png_set_pHYs);
256 LOAD_FUNCPTR(png_set_read_fn);
257 LOAD_FUNCPTR(png_set_strip_16);
258 LOAD_FUNCPTR(png_set_tRNS_to_alpha);
259 LOAD_FUNCPTR(png_set_write_fn);
260 LOAD_FUNCPTR(png_read_end);
261 LOAD_FUNCPTR(png_read_image);
262 LOAD_FUNCPTR(png_read_info);
263 LOAD_FUNCPTR(png_write_end);
264 LOAD_FUNCPTR(png_write_info);
265 LOAD_FUNCPTR(png_write_rows);
267 #undef LOAD_FUNCPTR
270 result = libpng_handle;
272 LeaveCriticalSection(&init_png_cs);
274 return result;
277 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
279 jmp_buf *pjmpbuf;
281 /* This uses setjmp/longjmp just like the default. We can't use the
282 * default because there's no way to access the jmp buffer in the png_struct
283 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
284 WARN("PNG error: %s\n", debugstr_a(error_message));
285 pjmpbuf = ppng_get_error_ptr(png_ptr);
286 longjmp(*pjmpbuf, 1);
289 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
291 WARN("PNG warning: %s\n", debugstr_a(warning_message));
294 typedef struct {
295 IWICBitmapDecoder IWICBitmapDecoder_iface;
296 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
297 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
298 LONG ref;
299 png_structp png_ptr;
300 png_infop info_ptr;
301 png_infop end_info;
302 BOOL initialized;
303 int bpp;
304 int width, height;
305 UINT stride;
306 const WICPixelFormatGUID *format;
307 BYTE *image_bits;
308 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
309 } PngDecoder;
311 static inline PngDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
313 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapDecoder_iface);
316 static inline PngDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
318 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapFrameDecode_iface);
321 static inline PngDecoder *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
323 return CONTAINING_RECORD(iface, PngDecoder, IWICMetadataBlockReader_iface);
326 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
328 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
329 void **ppv)
331 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
332 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
334 if (!ppv) return E_INVALIDARG;
336 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
338 *ppv = &This->IWICBitmapDecoder_iface;
340 else
342 *ppv = NULL;
343 return E_NOINTERFACE;
346 IUnknown_AddRef((IUnknown*)*ppv);
347 return S_OK;
350 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
352 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
353 ULONG ref = InterlockedIncrement(&This->ref);
355 TRACE("(%p) refcount=%u\n", iface, ref);
357 return ref;
360 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
362 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
363 ULONG ref = InterlockedDecrement(&This->ref);
365 TRACE("(%p) refcount=%u\n", iface, ref);
367 if (ref == 0)
369 if (This->png_ptr)
370 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
371 This->lock.DebugInfo->Spare[0] = 0;
372 DeleteCriticalSection(&This->lock);
373 HeapFree(GetProcessHeap(), 0, This->image_bits);
374 HeapFree(GetProcessHeap(), 0, This);
377 return ref;
380 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
381 DWORD *capability)
383 HRESULT hr;
385 TRACE("(%p,%p,%p)\n", iface, stream, capability);
387 if (!stream || !capability) return E_INVALIDARG;
389 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
390 if (hr != S_OK) return hr;
392 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
393 WICBitmapDecoderCapabilityCanDecodeSomeImages;
394 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
395 return S_OK;
398 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
400 IStream *stream = ppng_get_io_ptr(png_ptr);
401 HRESULT hr;
402 ULONG bytesread;
404 hr = IStream_Read(stream, data, length, &bytesread);
405 if (FAILED(hr) || bytesread != length)
407 ppng_error(png_ptr, "failed reading data");
411 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
412 WICDecodeOptions cacheOptions)
414 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
415 LARGE_INTEGER seek;
416 HRESULT hr=S_OK;
417 png_bytep *row_pointers=NULL;
418 UINT image_size;
419 UINT i;
420 int color_type, bit_depth;
421 png_bytep trans;
422 int num_trans;
423 png_uint_32 transparency;
424 png_color_16p trans_values;
425 jmp_buf jmpbuf;
427 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
429 EnterCriticalSection(&This->lock);
431 /* initialize libpng */
432 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
433 if (!This->png_ptr)
435 hr = E_FAIL;
436 goto end;
439 This->info_ptr = ppng_create_info_struct(This->png_ptr);
440 if (!This->info_ptr)
442 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
443 This->png_ptr = NULL;
444 hr = E_FAIL;
445 goto end;
448 This->end_info = ppng_create_info_struct(This->png_ptr);
449 if (!This->info_ptr)
451 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
452 This->png_ptr = NULL;
453 hr = E_FAIL;
454 goto end;
457 /* set up setjmp/longjmp error handling */
458 if (setjmp(jmpbuf))
460 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
461 HeapFree(GetProcessHeap(), 0, row_pointers);
462 This->png_ptr = NULL;
463 hr = E_FAIL;
464 goto end;
466 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
467 ppng_set_crc_action(This->png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
469 /* seek to the start of the stream */
470 seek.QuadPart = 0;
471 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
472 if (FAILED(hr)) goto end;
474 /* set up custom i/o handling */
475 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
477 /* read the header */
478 ppng_read_info(This->png_ptr, This->info_ptr);
480 /* choose a pixel format */
481 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
482 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
484 /* check for color-keyed alpha */
485 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
487 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
489 /* expand to RGBA */
490 if (color_type == PNG_COLOR_TYPE_GRAY)
492 if (bit_depth < 8)
494 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
495 ppng_set_expand_gray_1_2_4_to_8(This->png_ptr);
496 #else
497 ppng_set_gray_1_2_4_to_8(This->png_ptr);
498 #endif
499 bit_depth = 8;
501 ppng_set_gray_to_rgb(This->png_ptr);
503 ppng_set_tRNS_to_alpha(This->png_ptr);
504 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
507 switch (color_type)
509 case PNG_COLOR_TYPE_GRAY:
510 This->bpp = bit_depth;
511 switch (bit_depth)
513 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
514 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
515 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
516 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
517 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
518 default:
519 ERR("invalid grayscale bit depth: %i\n", bit_depth);
520 hr = E_FAIL;
521 goto end;
523 break;
524 case PNG_COLOR_TYPE_GRAY_ALPHA:
525 /* WIC does not support grayscale alpha formats so use RGBA */
526 ppng_set_gray_to_rgb(This->png_ptr);
527 /* fall through */
528 case PNG_COLOR_TYPE_RGB_ALPHA:
529 This->bpp = bit_depth * 4;
530 switch (bit_depth)
532 case 8:
533 ppng_set_bgr(This->png_ptr);
534 This->format = &GUID_WICPixelFormat32bppBGRA;
535 break;
536 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
537 default:
538 ERR("invalid RGBA bit depth: %i\n", bit_depth);
539 hr = E_FAIL;
540 goto end;
542 break;
543 case PNG_COLOR_TYPE_PALETTE:
544 This->bpp = bit_depth;
545 switch (bit_depth)
547 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
548 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
549 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
550 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
551 default:
552 ERR("invalid indexed color bit depth: %i\n", bit_depth);
553 hr = E_FAIL;
554 goto end;
556 break;
557 case PNG_COLOR_TYPE_RGB:
558 This->bpp = bit_depth * 3;
559 switch (bit_depth)
561 case 8:
562 ppng_set_bgr(This->png_ptr);
563 This->format = &GUID_WICPixelFormat24bppBGR;
564 break;
565 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
566 default:
567 ERR("invalid RGB color bit depth: %i\n", bit_depth);
568 hr = E_FAIL;
569 goto end;
571 break;
572 default:
573 ERR("invalid color type %i\n", color_type);
574 hr = E_FAIL;
575 goto end;
578 /* read the image data */
579 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
580 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
581 This->stride = This->width * This->bpp;
582 image_size = This->stride * This->height;
584 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
585 if (!This->image_bits)
587 hr = E_OUTOFMEMORY;
588 goto end;
591 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
592 if (!row_pointers)
594 hr = E_OUTOFMEMORY;
595 goto end;
598 for (i=0; i<This->height; i++)
599 row_pointers[i] = This->image_bits + i * This->stride;
601 ppng_read_image(This->png_ptr, row_pointers);
603 HeapFree(GetProcessHeap(), 0, row_pointers);
604 row_pointers = NULL;
606 ppng_read_end(This->png_ptr, This->end_info);
608 This->initialized = TRUE;
610 end:
612 LeaveCriticalSection(&This->lock);
614 return hr;
617 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
618 GUID *pguidContainerFormat)
620 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
621 return S_OK;
624 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
625 IWICBitmapDecoderInfo **ppIDecoderInfo)
627 HRESULT hr;
628 IWICComponentInfo *compinfo;
630 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
632 hr = CreateComponentInfo(&CLSID_WICPngDecoder, &compinfo);
633 if (FAILED(hr)) return hr;
635 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
636 (void**)ppIDecoderInfo);
638 IWICComponentInfo_Release(compinfo);
640 return hr;
643 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
644 IWICPalette *pIPalette)
646 FIXME("(%p,%p): stub\n", iface, pIPalette);
647 return E_NOTIMPL;
650 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
651 IWICMetadataQueryReader **ppIMetadataQueryReader)
653 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
654 return E_NOTIMPL;
657 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
658 IWICBitmapSource **ppIBitmapSource)
660 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
662 if (!ppIBitmapSource) return E_INVALIDARG;
664 *ppIBitmapSource = NULL;
665 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
668 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
669 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
671 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
672 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
675 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
676 IWICBitmapSource **ppIThumbnail)
678 TRACE("(%p,%p)\n", iface, ppIThumbnail);
680 if (!ppIThumbnail) return E_INVALIDARG;
682 *ppIThumbnail = NULL;
683 return WINCODEC_ERR_CODECNOTHUMBNAIL;
686 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
687 UINT *pCount)
689 if (!pCount) return E_INVALIDARG;
691 *pCount = 1;
692 return S_OK;
695 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
696 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
698 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
699 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
701 if (!This->initialized) return WINCODEC_ERR_FRAMEMISSING;
703 if (index != 0) return E_INVALIDARG;
705 IWICBitmapDecoder_AddRef(iface);
707 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
709 return S_OK;
712 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
713 PngDecoder_QueryInterface,
714 PngDecoder_AddRef,
715 PngDecoder_Release,
716 PngDecoder_QueryCapability,
717 PngDecoder_Initialize,
718 PngDecoder_GetContainerFormat,
719 PngDecoder_GetDecoderInfo,
720 PngDecoder_CopyPalette,
721 PngDecoder_GetMetadataQueryReader,
722 PngDecoder_GetPreview,
723 PngDecoder_GetColorContexts,
724 PngDecoder_GetThumbnail,
725 PngDecoder_GetFrameCount,
726 PngDecoder_GetFrame
729 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
730 void **ppv)
732 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
733 if (!ppv) return E_INVALIDARG;
735 if (IsEqualIID(&IID_IUnknown, iid) ||
736 IsEqualIID(&IID_IWICBitmapSource, iid) ||
737 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
739 *ppv = &This->IWICBitmapFrameDecode_iface;
741 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
743 *ppv = &This->IWICMetadataBlockReader_iface;
745 else
747 *ppv = NULL;
748 return E_NOINTERFACE;
751 IUnknown_AddRef((IUnknown*)*ppv);
752 return S_OK;
755 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
757 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
758 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
761 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
763 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
764 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
767 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
768 UINT *puiWidth, UINT *puiHeight)
770 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
771 *puiWidth = This->width;
772 *puiHeight = This->height;
773 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
774 return S_OK;
777 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
778 WICPixelFormatGUID *pPixelFormat)
780 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
781 TRACE("(%p,%p)\n", iface, pPixelFormat);
783 memcpy(pPixelFormat, This->format, sizeof(GUID));
785 return S_OK;
788 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
789 double *pDpiX, double *pDpiY)
791 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
792 png_uint_32 ret, xres, yres;
793 int unit_type;
795 EnterCriticalSection(&This->lock);
797 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
799 if (ret && unit_type == PNG_RESOLUTION_METER)
801 *pDpiX = xres * 0.0254;
802 *pDpiY = yres * 0.0254;
804 else
806 WARN("no pHYs block present\n");
807 *pDpiX = *pDpiY = 96.0;
810 LeaveCriticalSection(&This->lock);
812 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
814 return S_OK;
817 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
818 IWICPalette *pIPalette)
820 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
821 png_uint_32 ret;
822 png_colorp png_palette;
823 int num_palette;
824 WICColor palette[256];
825 png_bytep trans_alpha;
826 int num_trans;
827 png_color_16p trans_values;
828 int i;
829 HRESULT hr=S_OK;
831 TRACE("(%p,%p)\n", iface, pIPalette);
833 EnterCriticalSection(&This->lock);
835 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
836 if (!ret)
838 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
839 goto end;
842 if (num_palette > 256)
844 ERR("palette has %i colors?!\n", num_palette);
845 hr = E_FAIL;
846 goto end;
849 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans_alpha, &num_trans, &trans_values);
850 if (!ret) num_trans = 0;
852 for (i=0; i<num_palette; i++)
854 BYTE alpha = (i < num_trans) ? trans_alpha[i] : 0xff;
855 palette[i] = (alpha << 24 |
856 png_palette[i].red << 16|
857 png_palette[i].green << 8|
858 png_palette[i].blue);
861 end:
863 LeaveCriticalSection(&This->lock);
865 if (SUCCEEDED(hr))
866 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
868 return hr;
871 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
872 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
874 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
875 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
877 return copy_pixels(This->bpp, This->image_bits,
878 This->width, This->height, This->stride,
879 prc, cbStride, cbBufferSize, pbBuffer);
882 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
883 IWICMetadataQueryReader **ppIMetadataQueryReader)
885 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
886 return E_NOTIMPL;
889 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
890 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
892 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
893 png_charp name;
894 BYTE *profile;
895 png_uint_32 len;
896 int compression_type;
897 HRESULT hr;
899 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
901 if (!pcActualCount) return E_INVALIDARG;
903 EnterCriticalSection(&This->lock);
905 if (ppng_get_iCCP(This->png_ptr, This->info_ptr, &name, &compression_type, (void *)&profile, &len))
907 if (cCount && ppIColorContexts)
909 hr = IWICColorContext_InitializeFromMemory(*ppIColorContexts, profile, len);
910 if (FAILED(hr))
912 LeaveCriticalSection(&This->lock);
913 return hr;
916 *pcActualCount = 1;
918 else
919 *pcActualCount = 0;
921 LeaveCriticalSection(&This->lock);
923 return S_OK;
926 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
927 IWICBitmapSource **ppIThumbnail)
929 TRACE("(%p,%p)\n", iface, ppIThumbnail);
931 if (!ppIThumbnail) return E_INVALIDARG;
933 *ppIThumbnail = NULL;
934 return WINCODEC_ERR_CODECNOTHUMBNAIL;
937 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
938 PngDecoder_Frame_QueryInterface,
939 PngDecoder_Frame_AddRef,
940 PngDecoder_Frame_Release,
941 PngDecoder_Frame_GetSize,
942 PngDecoder_Frame_GetPixelFormat,
943 PngDecoder_Frame_GetResolution,
944 PngDecoder_Frame_CopyPalette,
945 PngDecoder_Frame_CopyPixels,
946 PngDecoder_Frame_GetMetadataQueryReader,
947 PngDecoder_Frame_GetColorContexts,
948 PngDecoder_Frame_GetThumbnail
951 static HRESULT WINAPI PngDecoder_Block_QueryInterface(IWICMetadataBlockReader *iface, REFIID iid,
952 void **ppv)
954 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
955 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
958 static ULONG WINAPI PngDecoder_Block_AddRef(IWICMetadataBlockReader *iface)
960 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
961 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
964 static ULONG WINAPI PngDecoder_Block_Release(IWICMetadataBlockReader *iface)
966 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
967 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
970 static HRESULT WINAPI PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
971 GUID *pguidContainerFormat)
973 if (!pguidContainerFormat) return E_INVALIDARG;
974 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
975 return S_OK;
978 static HRESULT WINAPI PngDecoder_Block_GetCount(IWICMetadataBlockReader *iface,
979 UINT *pcCount)
981 static int once;
982 TRACE("%p,%p\n", iface, pcCount);
983 if (!once++) FIXME("stub\n");
984 return E_NOTIMPL;
987 static HRESULT WINAPI PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
988 UINT nIndex, IWICMetadataReader **ppIMetadataReader)
990 FIXME("%p,%d,%p\n", iface, nIndex, ppIMetadataReader);
991 return E_NOTIMPL;
994 static HRESULT WINAPI PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader *iface,
995 IEnumUnknown **ppIEnumMetadata)
997 FIXME("%p,%p\n", iface, ppIEnumMetadata);
998 return E_NOTIMPL;
1001 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl = {
1002 PngDecoder_Block_QueryInterface,
1003 PngDecoder_Block_AddRef,
1004 PngDecoder_Block_Release,
1005 PngDecoder_Block_GetContainerFormat,
1006 PngDecoder_Block_GetCount,
1007 PngDecoder_Block_GetReaderByIndex,
1008 PngDecoder_Block_GetEnumerator,
1011 HRESULT PngDecoder_CreateInstance(REFIID iid, void** ppv)
1013 PngDecoder *This;
1014 HRESULT ret;
1016 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1018 *ppv = NULL;
1020 if (!load_libpng())
1022 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
1023 return E_FAIL;
1026 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
1027 if (!This) return E_OUTOFMEMORY;
1029 This->IWICBitmapDecoder_iface.lpVtbl = &PngDecoder_Vtbl;
1030 This->IWICBitmapFrameDecode_iface.lpVtbl = &PngDecoder_FrameVtbl;
1031 This->IWICMetadataBlockReader_iface.lpVtbl = &PngDecoder_BlockVtbl;
1032 This->ref = 1;
1033 This->png_ptr = NULL;
1034 This->info_ptr = NULL;
1035 This->end_info = NULL;
1036 This->initialized = FALSE;
1037 This->image_bits = NULL;
1038 InitializeCriticalSection(&This->lock);
1039 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngDecoder.lock");
1041 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1042 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1044 return ret;
1047 struct png_pixelformat {
1048 const WICPixelFormatGUID *guid;
1049 UINT bpp;
1050 int bit_depth;
1051 int color_type;
1052 BOOL remove_filler;
1053 BOOL swap_rgb;
1056 static const struct png_pixelformat formats[] = {
1057 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
1058 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
1059 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
1060 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
1061 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
1062 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
1063 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
1064 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
1065 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
1066 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
1067 {NULL},
1070 typedef struct PngEncoder {
1071 IWICBitmapEncoder IWICBitmapEncoder_iface;
1072 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1073 LONG ref;
1074 IStream *stream;
1075 png_structp png_ptr;
1076 png_infop info_ptr;
1077 UINT frame_count;
1078 BOOL frame_initialized;
1079 const struct png_pixelformat *format;
1080 BOOL info_written;
1081 UINT width, height;
1082 double xres, yres;
1083 UINT lines_written;
1084 BOOL frame_committed;
1085 BOOL committed;
1086 CRITICAL_SECTION lock;
1087 BOOL interlace;
1088 BYTE *data;
1089 UINT stride;
1090 UINT passes;
1091 } PngEncoder;
1093 static inline PngEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1095 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapEncoder_iface);
1098 static inline PngEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1100 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapFrameEncode_iface);
1103 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1104 void **ppv)
1106 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1107 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1109 if (!ppv) return E_INVALIDARG;
1111 if (IsEqualIID(&IID_IUnknown, iid) ||
1112 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1114 *ppv = &This->IWICBitmapFrameEncode_iface;
1116 else
1118 *ppv = NULL;
1119 return E_NOINTERFACE;
1122 IUnknown_AddRef((IUnknown*)*ppv);
1123 return S_OK;
1126 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1128 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1129 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
1132 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
1134 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1135 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1138 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1139 IPropertyBag2 *pIEncoderOptions)
1141 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1142 BOOL interlace;
1143 PROPBAG2 opts[1]= {{0}};
1144 VARIANT opt_values[1];
1145 HRESULT opt_hres[1];
1146 HRESULT hr;
1148 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1150 opts[0].pstrName = (LPOLESTR)wszPngInterlaceOption;
1151 opts[0].vt = VT_BOOL;
1153 if (pIEncoderOptions)
1155 hr = IPropertyBag2_Read(pIEncoderOptions, 1, opts, NULL, opt_values, opt_hres);
1157 if (FAILED(hr))
1158 return hr;
1160 else
1161 memset(opt_values, 0, sizeof(opt_values));
1163 if (V_VT(&opt_values[0]) == VT_EMPTY)
1164 interlace = FALSE;
1165 else
1166 interlace = (V_BOOL(&opt_values[0]) != 0);
1168 EnterCriticalSection(&This->lock);
1170 if (This->frame_initialized)
1172 LeaveCriticalSection(&This->lock);
1173 return WINCODEC_ERR_WRONGSTATE;
1176 This->interlace = interlace;
1178 This->frame_initialized = TRUE;
1180 LeaveCriticalSection(&This->lock);
1182 return S_OK;
1185 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1186 UINT uiWidth, UINT uiHeight)
1188 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1189 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1191 EnterCriticalSection(&This->lock);
1193 if (!This->frame_initialized || This->info_written)
1195 LeaveCriticalSection(&This->lock);
1196 return WINCODEC_ERR_WRONGSTATE;
1199 This->width = uiWidth;
1200 This->height = uiHeight;
1202 LeaveCriticalSection(&This->lock);
1204 return S_OK;
1207 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1208 double dpiX, double dpiY)
1210 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1211 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1213 EnterCriticalSection(&This->lock);
1215 if (!This->frame_initialized || This->info_written)
1217 LeaveCriticalSection(&This->lock);
1218 return WINCODEC_ERR_WRONGSTATE;
1221 This->xres = dpiX;
1222 This->yres = dpiY;
1224 LeaveCriticalSection(&This->lock);
1226 return S_OK;
1229 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1230 WICPixelFormatGUID *pPixelFormat)
1232 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1233 int i;
1234 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1236 EnterCriticalSection(&This->lock);
1238 if (!This->frame_initialized || This->info_written)
1240 LeaveCriticalSection(&This->lock);
1241 return WINCODEC_ERR_WRONGSTATE;
1244 for (i=0; formats[i].guid; i++)
1246 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1247 break;
1250 if (!formats[i].guid) i = 0;
1252 This->format = &formats[i];
1253 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1255 LeaveCriticalSection(&This->lock);
1257 return S_OK;
1260 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1261 UINT cCount, IWICColorContext **ppIColorContext)
1263 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1264 return E_NOTIMPL;
1267 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1268 IWICPalette *pIPalette)
1270 FIXME("(%p,%p): stub\n", iface, pIPalette);
1271 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1274 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1275 IWICBitmapSource *pIThumbnail)
1277 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1278 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1281 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1282 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1284 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1285 png_byte **row_pointers=NULL;
1286 UINT i;
1287 jmp_buf jmpbuf;
1288 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1290 EnterCriticalSection(&This->lock);
1292 if (!This->frame_initialized || !This->width || !This->height || !This->format)
1294 LeaveCriticalSection(&This->lock);
1295 return WINCODEC_ERR_WRONGSTATE;
1298 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1300 LeaveCriticalSection(&This->lock);
1301 return E_INVALIDARG;
1304 /* set up setjmp/longjmp error handling */
1305 if (setjmp(jmpbuf))
1307 LeaveCriticalSection(&This->lock);
1308 HeapFree(GetProcessHeap(), 0, row_pointers);
1309 return E_FAIL;
1311 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1313 if (!This->info_written)
1315 if (This->interlace)
1317 /* libpng requires us to write all data multiple times in this case. */
1318 This->stride = (This->format->bpp * This->width + 7)/8;
1319 This->data = HeapAlloc(GetProcessHeap(), 0, This->height * This->stride);
1320 if (!This->data)
1322 LeaveCriticalSection(&This->lock);
1323 return E_OUTOFMEMORY;
1327 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
1328 This->format->bit_depth, This->format->color_type,
1329 This->interlace ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE,
1330 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1332 if (This->xres != 0.0 && This->yres != 0.0)
1334 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
1335 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
1338 ppng_write_info(This->png_ptr, This->info_ptr);
1340 if (This->format->remove_filler)
1341 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1343 if (This->format->swap_rgb)
1344 ppng_set_bgr(This->png_ptr);
1346 if (This->interlace)
1347 This->passes = ppng_set_interlace_handling(This->png_ptr);
1349 This->info_written = TRUE;
1352 if (This->interlace)
1354 /* Just store the data so we can write it in multiple passes in Commit. */
1355 for (i=0; i<lineCount; i++)
1356 memcpy(This->data + This->stride * (This->lines_written + i),
1357 pbPixels + cbStride * i,
1358 This->stride);
1360 This->lines_written += lineCount;
1362 LeaveCriticalSection(&This->lock);
1363 return S_OK;
1366 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1367 if (!row_pointers)
1369 LeaveCriticalSection(&This->lock);
1370 return E_OUTOFMEMORY;
1373 for (i=0; i<lineCount; i++)
1374 row_pointers[i] = pbPixels + cbStride * i;
1376 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1377 This->lines_written += lineCount;
1379 LeaveCriticalSection(&This->lock);
1381 HeapFree(GetProcessHeap(), 0, row_pointers);
1383 return S_OK;
1386 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1387 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1389 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1390 HRESULT hr;
1391 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1393 if (!This->frame_initialized)
1394 return WINCODEC_ERR_WRONGSTATE;
1396 hr = configure_write_source(iface, pIBitmapSource, prc,
1397 This->format ? This->format->guid : NULL, This->width, This->height,
1398 This->xres, This->yres);
1400 if (SUCCEEDED(hr))
1402 hr = write_source(iface, pIBitmapSource, prc,
1403 This->format->guid, This->format->bpp, This->width, This->height);
1406 return hr;
1409 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1411 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1412 png_byte **row_pointers=NULL;
1413 jmp_buf jmpbuf;
1414 TRACE("(%p)\n", iface);
1416 EnterCriticalSection(&This->lock);
1418 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1420 LeaveCriticalSection(&This->lock);
1421 return WINCODEC_ERR_WRONGSTATE;
1424 /* set up setjmp/longjmp error handling */
1425 if (setjmp(jmpbuf))
1427 LeaveCriticalSection(&This->lock);
1428 HeapFree(GetProcessHeap(), 0, row_pointers);
1429 return E_FAIL;
1431 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1433 if (This->interlace)
1435 int i;
1437 row_pointers = HeapAlloc(GetProcessHeap(), 0, This->height * sizeof(png_byte*));
1438 if (!row_pointers)
1440 LeaveCriticalSection(&This->lock);
1441 return E_OUTOFMEMORY;
1444 for (i=0; i<This->height; i++)
1445 row_pointers[i] = This->data + This->stride * i;
1447 for (i=0; i<This->passes; i++)
1448 ppng_write_rows(This->png_ptr, row_pointers, This->height);
1451 ppng_write_end(This->png_ptr, This->info_ptr);
1453 This->frame_committed = TRUE;
1455 HeapFree(GetProcessHeap(), 0, row_pointers);
1457 LeaveCriticalSection(&This->lock);
1459 return S_OK;
1462 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1463 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1465 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1466 return E_NOTIMPL;
1469 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1470 PngFrameEncode_QueryInterface,
1471 PngFrameEncode_AddRef,
1472 PngFrameEncode_Release,
1473 PngFrameEncode_Initialize,
1474 PngFrameEncode_SetSize,
1475 PngFrameEncode_SetResolution,
1476 PngFrameEncode_SetPixelFormat,
1477 PngFrameEncode_SetColorContexts,
1478 PngFrameEncode_SetPalette,
1479 PngFrameEncode_SetThumbnail,
1480 PngFrameEncode_WritePixels,
1481 PngFrameEncode_WriteSource,
1482 PngFrameEncode_Commit,
1483 PngFrameEncode_GetMetadataQueryWriter
1486 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1487 void **ppv)
1489 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1490 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1492 if (!ppv) return E_INVALIDARG;
1494 if (IsEqualIID(&IID_IUnknown, iid) ||
1495 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1497 *ppv = &This->IWICBitmapEncoder_iface;
1499 else
1501 *ppv = NULL;
1502 return E_NOINTERFACE;
1505 IUnknown_AddRef((IUnknown*)*ppv);
1506 return S_OK;
1509 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1511 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1512 ULONG ref = InterlockedIncrement(&This->ref);
1514 TRACE("(%p) refcount=%u\n", iface, ref);
1516 return ref;
1519 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1521 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1522 ULONG ref = InterlockedDecrement(&This->ref);
1524 TRACE("(%p) refcount=%u\n", iface, ref);
1526 if (ref == 0)
1528 This->lock.DebugInfo->Spare[0] = 0;
1529 DeleteCriticalSection(&This->lock);
1530 if (This->png_ptr)
1531 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1532 if (This->stream)
1533 IStream_Release(This->stream);
1534 HeapFree(GetProcessHeap(), 0, This->data);
1535 HeapFree(GetProcessHeap(), 0, This);
1538 return ref;
1541 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1543 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1544 HRESULT hr;
1545 ULONG byteswritten;
1547 hr = IStream_Write(This->stream, data, length, &byteswritten);
1548 if (FAILED(hr) || byteswritten != length)
1550 ppng_error(png_ptr, "failed writing data");
1554 static void user_flush(png_structp png_ptr)
1558 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1559 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1561 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1562 jmp_buf jmpbuf;
1564 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1566 EnterCriticalSection(&This->lock);
1568 if (This->png_ptr)
1570 LeaveCriticalSection(&This->lock);
1571 return WINCODEC_ERR_WRONGSTATE;
1574 /* initialize libpng */
1575 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1576 if (!This->png_ptr)
1578 LeaveCriticalSection(&This->lock);
1579 return E_FAIL;
1582 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1583 if (!This->info_ptr)
1585 ppng_destroy_write_struct(&This->png_ptr, NULL);
1586 This->png_ptr = NULL;
1587 LeaveCriticalSection(&This->lock);
1588 return E_FAIL;
1591 IStream_AddRef(pIStream);
1592 This->stream = pIStream;
1594 /* set up setjmp/longjmp error handling */
1595 if (setjmp(jmpbuf))
1597 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1598 This->png_ptr = NULL;
1599 IStream_Release(This->stream);
1600 This->stream = NULL;
1601 LeaveCriticalSection(&This->lock);
1602 return E_FAIL;
1604 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1606 /* set up custom i/o handling */
1607 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1609 LeaveCriticalSection(&This->lock);
1611 return S_OK;
1614 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1615 GUID *pguidContainerFormat)
1617 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1618 return E_NOTIMPL;
1621 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1622 IWICBitmapEncoderInfo **ppIEncoderInfo)
1624 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1625 return E_NOTIMPL;
1628 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1629 UINT cCount, IWICColorContext **ppIColorContext)
1631 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1632 return E_NOTIMPL;
1635 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1637 TRACE("(%p,%p)\n", iface, pIPalette);
1638 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1641 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1643 TRACE("(%p,%p)\n", iface, pIThumbnail);
1644 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1647 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1649 TRACE("(%p,%p)\n", iface, pIPreview);
1650 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1653 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1654 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1656 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1657 HRESULT hr;
1658 PROPBAG2 opts[1]= {{0}};
1660 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1662 EnterCriticalSection(&This->lock);
1664 if (This->frame_count != 0)
1666 LeaveCriticalSection(&This->lock);
1667 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1670 if (!This->stream)
1672 LeaveCriticalSection(&This->lock);
1673 return WINCODEC_ERR_NOTINITIALIZED;
1676 opts[0].pstrName = (LPOLESTR)wszPngInterlaceOption;
1677 opts[0].vt = VT_BOOL;
1678 opts[0].dwType = PROPBAG2_TYPE_DATA;
1680 hr = CreatePropertyBag2(opts, 1, ppIEncoderOptions);
1681 if (FAILED(hr))
1683 LeaveCriticalSection(&This->lock);
1684 return hr;
1687 This->frame_count = 1;
1689 LeaveCriticalSection(&This->lock);
1691 IWICBitmapEncoder_AddRef(iface);
1692 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1694 return S_OK;
1697 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1699 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1700 TRACE("(%p)\n", iface);
1702 EnterCriticalSection(&This->lock);
1704 if (!This->frame_committed || This->committed)
1706 LeaveCriticalSection(&This->lock);
1707 return WINCODEC_ERR_WRONGSTATE;
1710 This->committed = TRUE;
1712 LeaveCriticalSection(&This->lock);
1714 return S_OK;
1717 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1718 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1720 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1721 return E_NOTIMPL;
1724 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
1725 PngEncoder_QueryInterface,
1726 PngEncoder_AddRef,
1727 PngEncoder_Release,
1728 PngEncoder_Initialize,
1729 PngEncoder_GetContainerFormat,
1730 PngEncoder_GetEncoderInfo,
1731 PngEncoder_SetColorContexts,
1732 PngEncoder_SetPalette,
1733 PngEncoder_SetThumbnail,
1734 PngEncoder_SetPreview,
1735 PngEncoder_CreateNewFrame,
1736 PngEncoder_Commit,
1737 PngEncoder_GetMetadataQueryWriter
1740 HRESULT PngEncoder_CreateInstance(REFIID iid, void** ppv)
1742 PngEncoder *This;
1743 HRESULT ret;
1745 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1747 *ppv = NULL;
1749 if (!load_libpng())
1751 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1752 return E_FAIL;
1755 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1756 if (!This) return E_OUTOFMEMORY;
1758 This->IWICBitmapEncoder_iface.lpVtbl = &PngEncoder_Vtbl;
1759 This->IWICBitmapFrameEncode_iface.lpVtbl = &PngEncoder_FrameVtbl;
1760 This->ref = 1;
1761 This->png_ptr = NULL;
1762 This->info_ptr = NULL;
1763 This->stream = NULL;
1764 This->frame_count = 0;
1765 This->frame_initialized = FALSE;
1766 This->format = NULL;
1767 This->info_written = FALSE;
1768 This->width = 0;
1769 This->height = 0;
1770 This->xres = 0.0;
1771 This->yres = 0.0;
1772 This->lines_written = 0;
1773 This->frame_committed = FALSE;
1774 This->committed = FALSE;
1775 This->data = NULL;
1776 InitializeCriticalSection(&This->lock);
1777 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngEncoder.lock");
1779 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
1780 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1782 return ret;
1785 #else /* !HAVE_PNG_H */
1787 HRESULT PngDecoder_CreateInstance(REFIID iid, void** ppv)
1789 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1790 return E_FAIL;
1793 HRESULT PngEncoder_CreateInstance(REFIID iid, void** ppv)
1795 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");
1796 return E_FAIL;
1799 #endif