wined3d: Use a shader for unconverted blits.
[wine.git] / dlls / windowscodecs / pngformat.c
blob2aac560840d78b5a7bb6be203773fbbbb59ae9bc
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 inline ULONG read_ulong_be(BYTE* data)
48 return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
51 static HRESULT read_png_chunk(IStream *stream, BYTE *type, BYTE **data, ULONG *data_size)
53 BYTE header[8];
54 HRESULT hr;
55 ULONG bytesread;
57 hr = IStream_Read(stream, header, 8, &bytesread);
58 if (FAILED(hr) || bytesread < 8)
60 if (SUCCEEDED(hr))
61 hr = E_FAIL;
62 return hr;
65 *data_size = read_ulong_be(&header[0]);
67 memcpy(type, &header[4], 4);
69 if (data)
71 *data = HeapAlloc(GetProcessHeap(), 0, *data_size);
72 if (!*data)
73 return E_OUTOFMEMORY;
75 hr = IStream_Read(stream, *data, *data_size, &bytesread);
77 if (FAILED(hr) || bytesread < *data_size)
79 if (SUCCEEDED(hr))
80 hr = E_FAIL;
81 HeapFree(GetProcessHeap(), 0, *data);
82 *data = NULL;
83 return hr;
86 /* Windows ignores CRC of the chunk */
89 return S_OK;
92 static HRESULT LoadTextMetadata(IStream *stream, const GUID *preferred_vendor,
93 DWORD persist_options, MetadataItem **items, DWORD *item_count)
95 HRESULT hr;
96 BYTE type[4];
97 BYTE *data;
98 ULONG data_size;
99 ULONG name_len, value_len;
100 BYTE *name_end_ptr;
101 LPSTR name, value;
102 MetadataItem *result;
104 hr = read_png_chunk(stream, type, &data, &data_size);
105 if (FAILED(hr)) return hr;
107 name_end_ptr = memchr(data, 0, data_size);
109 name_len = name_end_ptr - data;
111 if (!name_end_ptr || name_len > 79)
113 HeapFree(GetProcessHeap(), 0, data);
114 return E_FAIL;
117 value_len = data_size - name_len - 1;
119 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
120 name = HeapAlloc(GetProcessHeap(), 0, name_len + 1);
121 value = HeapAlloc(GetProcessHeap(), 0, value_len + 1);
122 if (!result || !name || !value)
124 HeapFree(GetProcessHeap(), 0, data);
125 HeapFree(GetProcessHeap(), 0, result);
126 HeapFree(GetProcessHeap(), 0, name);
127 HeapFree(GetProcessHeap(), 0, value);
128 return E_OUTOFMEMORY;
131 PropVariantInit(&result[0].schema);
132 PropVariantInit(&result[0].id);
133 PropVariantInit(&result[0].value);
135 memcpy(name, data, name_len + 1);
136 memcpy(value, name_end_ptr + 1, value_len);
137 value[value_len] = 0;
139 result[0].id.vt = VT_LPSTR;
140 result[0].id.u.pszVal = name;
141 result[0].value.vt = VT_LPSTR;
142 result[0].value.u.pszVal = value;
144 *items = result;
145 *item_count = 1;
147 HeapFree(GetProcessHeap(), 0, data);
149 return S_OK;
152 static const MetadataHandlerVtbl TextReader_Vtbl = {
154 &CLSID_WICPngTextMetadataReader,
155 LoadTextMetadata
158 HRESULT PngTextReader_CreateInstance(REFIID iid, void** ppv)
160 return MetadataReader_Create(&TextReader_Vtbl, iid, ppv);
163 static HRESULT LoadGamaMetadata(IStream *stream, const GUID *preferred_vendor,
164 DWORD persist_options, MetadataItem **items, DWORD *item_count)
166 HRESULT hr;
167 BYTE type[4];
168 BYTE *data;
169 ULONG data_size;
170 ULONG gamma;
171 static const WCHAR ImageGamma[] = {'I','m','a','g','e','G','a','m','m','a',0};
172 LPWSTR name;
173 MetadataItem *result;
175 hr = read_png_chunk(stream, type, &data, &data_size);
176 if (FAILED(hr)) return hr;
178 if (data_size < 4)
180 HeapFree(GetProcessHeap(), 0, data);
181 return E_FAIL;
184 gamma = read_ulong_be(data);
186 HeapFree(GetProcessHeap(), 0, data);
188 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
189 name = HeapAlloc(GetProcessHeap(), 0, sizeof(ImageGamma));
190 if (!result || !name)
192 HeapFree(GetProcessHeap(), 0, result);
193 HeapFree(GetProcessHeap(), 0, name);
194 return E_OUTOFMEMORY;
197 PropVariantInit(&result[0].schema);
198 PropVariantInit(&result[0].id);
199 PropVariantInit(&result[0].value);
201 memcpy(name, ImageGamma, sizeof(ImageGamma));
203 result[0].id.vt = VT_LPWSTR;
204 result[0].id.u.pwszVal = name;
205 result[0].value.vt = VT_UI4;
206 result[0].value.u.ulVal = gamma;
208 *items = result;
209 *item_count = 1;
211 return S_OK;
214 static const MetadataHandlerVtbl GamaReader_Vtbl = {
216 &CLSID_WICPngGamaMetadataReader,
217 LoadGamaMetadata
220 HRESULT PngGamaReader_CreateInstance(REFIID iid, void** ppv)
222 return MetadataReader_Create(&GamaReader_Vtbl, iid, ppv);
225 #ifdef SONAME_LIBPNG
227 static void *libpng_handle;
228 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
229 MAKE_FUNCPTR(png_create_read_struct);
230 MAKE_FUNCPTR(png_create_info_struct);
231 MAKE_FUNCPTR(png_create_write_struct);
232 MAKE_FUNCPTR(png_destroy_read_struct);
233 MAKE_FUNCPTR(png_destroy_write_struct);
234 MAKE_FUNCPTR(png_error);
235 MAKE_FUNCPTR(png_get_bit_depth);
236 MAKE_FUNCPTR(png_get_color_type);
237 MAKE_FUNCPTR(png_get_error_ptr);
238 MAKE_FUNCPTR(png_get_iCCP);
239 MAKE_FUNCPTR(png_get_image_height);
240 MAKE_FUNCPTR(png_get_image_width);
241 MAKE_FUNCPTR(png_get_io_ptr);
242 MAKE_FUNCPTR(png_get_pHYs);
243 MAKE_FUNCPTR(png_get_PLTE);
244 MAKE_FUNCPTR(png_get_tRNS);
245 MAKE_FUNCPTR(png_set_bgr);
246 MAKE_FUNCPTR(png_set_crc_action);
247 MAKE_FUNCPTR(png_set_error_fn);
248 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
249 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
250 #else
251 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8);
252 #endif
253 MAKE_FUNCPTR(png_set_filler);
254 MAKE_FUNCPTR(png_set_gray_to_rgb);
255 MAKE_FUNCPTR(png_set_interlace_handling);
256 MAKE_FUNCPTR(png_set_IHDR);
257 MAKE_FUNCPTR(png_set_pHYs);
258 MAKE_FUNCPTR(png_set_read_fn);
259 MAKE_FUNCPTR(png_set_strip_16);
260 MAKE_FUNCPTR(png_set_tRNS_to_alpha);
261 MAKE_FUNCPTR(png_set_write_fn);
262 MAKE_FUNCPTR(png_read_end);
263 MAKE_FUNCPTR(png_read_image);
264 MAKE_FUNCPTR(png_read_info);
265 MAKE_FUNCPTR(png_write_end);
266 MAKE_FUNCPTR(png_write_info);
267 MAKE_FUNCPTR(png_write_rows);
268 #undef MAKE_FUNCPTR
270 static CRITICAL_SECTION init_png_cs;
271 static CRITICAL_SECTION_DEBUG init_png_cs_debug =
273 0, 0, &init_png_cs,
274 { &init_png_cs_debug.ProcessLocksList,
275 &init_png_cs_debug.ProcessLocksList },
276 0, 0, { (DWORD_PTR)(__FILE__ ": init_png_cs") }
278 static CRITICAL_SECTION init_png_cs = { &init_png_cs_debug, -1, 0, 0, 0, 0 };
280 static void *load_libpng(void)
282 void *result;
284 EnterCriticalSection(&init_png_cs);
286 if(!libpng_handle && (libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
288 #define LOAD_FUNCPTR(f) \
289 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
290 libpng_handle = NULL; \
291 LeaveCriticalSection(&init_png_cs); \
292 return NULL; \
294 LOAD_FUNCPTR(png_create_read_struct);
295 LOAD_FUNCPTR(png_create_info_struct);
296 LOAD_FUNCPTR(png_create_write_struct);
297 LOAD_FUNCPTR(png_destroy_read_struct);
298 LOAD_FUNCPTR(png_destroy_write_struct);
299 LOAD_FUNCPTR(png_error);
300 LOAD_FUNCPTR(png_get_bit_depth);
301 LOAD_FUNCPTR(png_get_color_type);
302 LOAD_FUNCPTR(png_get_error_ptr);
303 LOAD_FUNCPTR(png_get_iCCP);
304 LOAD_FUNCPTR(png_get_image_height);
305 LOAD_FUNCPTR(png_get_image_width);
306 LOAD_FUNCPTR(png_get_io_ptr);
307 LOAD_FUNCPTR(png_get_pHYs);
308 LOAD_FUNCPTR(png_get_PLTE);
309 LOAD_FUNCPTR(png_get_tRNS);
310 LOAD_FUNCPTR(png_set_bgr);
311 LOAD_FUNCPTR(png_set_crc_action);
312 LOAD_FUNCPTR(png_set_error_fn);
313 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
314 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
315 #else
316 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8);
317 #endif
318 LOAD_FUNCPTR(png_set_filler);
319 LOAD_FUNCPTR(png_set_gray_to_rgb);
320 LOAD_FUNCPTR(png_set_interlace_handling);
321 LOAD_FUNCPTR(png_set_IHDR);
322 LOAD_FUNCPTR(png_set_pHYs);
323 LOAD_FUNCPTR(png_set_read_fn);
324 LOAD_FUNCPTR(png_set_strip_16);
325 LOAD_FUNCPTR(png_set_tRNS_to_alpha);
326 LOAD_FUNCPTR(png_set_write_fn);
327 LOAD_FUNCPTR(png_read_end);
328 LOAD_FUNCPTR(png_read_image);
329 LOAD_FUNCPTR(png_read_info);
330 LOAD_FUNCPTR(png_write_end);
331 LOAD_FUNCPTR(png_write_info);
332 LOAD_FUNCPTR(png_write_rows);
334 #undef LOAD_FUNCPTR
337 result = libpng_handle;
339 LeaveCriticalSection(&init_png_cs);
341 return result;
344 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
346 jmp_buf *pjmpbuf;
348 /* This uses setjmp/longjmp just like the default. We can't use the
349 * default because there's no way to access the jmp buffer in the png_struct
350 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
351 WARN("PNG error: %s\n", debugstr_a(error_message));
352 pjmpbuf = ppng_get_error_ptr(png_ptr);
353 longjmp(*pjmpbuf, 1);
356 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
358 WARN("PNG warning: %s\n", debugstr_a(warning_message));
361 typedef struct {
362 ULARGE_INTEGER ofs, len;
363 IWICMetadataReader* reader;
364 } metadata_block_info;
366 typedef struct {
367 IWICBitmapDecoder IWICBitmapDecoder_iface;
368 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
369 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
370 LONG ref;
371 IStream *stream;
372 png_structp png_ptr;
373 png_infop info_ptr;
374 png_infop end_info;
375 BOOL initialized;
376 int bpp;
377 int width, height;
378 UINT stride;
379 const WICPixelFormatGUID *format;
380 BYTE *image_bits;
381 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
382 ULONG metadata_count;
383 metadata_block_info* metadata_blocks;
384 } PngDecoder;
386 static inline PngDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
388 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapDecoder_iface);
391 static inline PngDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
393 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapFrameDecode_iface);
396 static inline PngDecoder *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
398 return CONTAINING_RECORD(iface, PngDecoder, IWICMetadataBlockReader_iface);
401 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
403 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
404 void **ppv)
406 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
407 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
409 if (!ppv) return E_INVALIDARG;
411 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
413 *ppv = &This->IWICBitmapDecoder_iface;
415 else
417 *ppv = NULL;
418 return E_NOINTERFACE;
421 IUnknown_AddRef((IUnknown*)*ppv);
422 return S_OK;
425 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
427 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
428 ULONG ref = InterlockedIncrement(&This->ref);
430 TRACE("(%p) refcount=%u\n", iface, ref);
432 return ref;
435 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
437 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
438 ULONG ref = InterlockedDecrement(&This->ref);
439 ULONG i;
441 TRACE("(%p) refcount=%u\n", iface, ref);
443 if (ref == 0)
445 IStream_Release(This->stream);
446 if (This->png_ptr)
447 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
448 This->lock.DebugInfo->Spare[0] = 0;
449 DeleteCriticalSection(&This->lock);
450 HeapFree(GetProcessHeap(), 0, This->image_bits);
451 for (i=0; i<This->metadata_count; i++)
453 if (This->metadata_blocks[i].reader)
454 IWICMetadataReader_Release(This->metadata_blocks[i].reader);
456 HeapFree(GetProcessHeap(), 0, This->metadata_blocks);
457 HeapFree(GetProcessHeap(), 0, This);
460 return ref;
463 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
464 DWORD *capability)
466 HRESULT hr;
468 TRACE("(%p,%p,%p)\n", iface, stream, capability);
470 if (!stream || !capability) return E_INVALIDARG;
472 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
473 if (hr != S_OK) return hr;
475 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
476 WICBitmapDecoderCapabilityCanDecodeSomeImages;
477 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
478 return S_OK;
481 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
483 IStream *stream = ppng_get_io_ptr(png_ptr);
484 HRESULT hr;
485 ULONG bytesread;
487 hr = IStream_Read(stream, data, length, &bytesread);
488 if (FAILED(hr) || bytesread != length)
490 ppng_error(png_ptr, "failed reading data");
494 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
495 WICDecodeOptions cacheOptions)
497 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
498 LARGE_INTEGER seek;
499 HRESULT hr=S_OK;
500 png_bytep *row_pointers=NULL;
501 UINT image_size;
502 UINT i;
503 int color_type, bit_depth;
504 png_bytep trans;
505 int num_trans;
506 png_uint_32 transparency;
507 png_color_16p trans_values;
508 jmp_buf jmpbuf;
509 BYTE chunk_type[4];
510 ULONG chunk_size;
511 ULARGE_INTEGER chunk_start;
512 ULONG metadata_blocks_size = 0;
514 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
516 EnterCriticalSection(&This->lock);
518 /* initialize libpng */
519 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
520 if (!This->png_ptr)
522 hr = E_FAIL;
523 goto end;
526 This->info_ptr = ppng_create_info_struct(This->png_ptr);
527 if (!This->info_ptr)
529 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
530 This->png_ptr = NULL;
531 hr = E_FAIL;
532 goto end;
535 This->end_info = ppng_create_info_struct(This->png_ptr);
536 if (!This->info_ptr)
538 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
539 This->png_ptr = NULL;
540 hr = E_FAIL;
541 goto end;
544 /* set up setjmp/longjmp error handling */
545 if (setjmp(jmpbuf))
547 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
548 HeapFree(GetProcessHeap(), 0, row_pointers);
549 This->png_ptr = NULL;
550 hr = E_FAIL;
551 goto end;
553 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
554 ppng_set_crc_action(This->png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
556 /* seek to the start of the stream */
557 seek.QuadPart = 0;
558 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
559 if (FAILED(hr)) goto end;
561 /* set up custom i/o handling */
562 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
564 /* read the header */
565 ppng_read_info(This->png_ptr, This->info_ptr);
567 /* choose a pixel format */
568 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
569 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
571 /* check for color-keyed alpha */
572 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
574 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
576 /* expand to RGBA */
577 if (color_type == PNG_COLOR_TYPE_GRAY)
579 if (bit_depth < 8)
581 #ifdef HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
582 ppng_set_expand_gray_1_2_4_to_8(This->png_ptr);
583 #else
584 ppng_set_gray_1_2_4_to_8(This->png_ptr);
585 #endif
586 bit_depth = 8;
588 ppng_set_gray_to_rgb(This->png_ptr);
590 ppng_set_tRNS_to_alpha(This->png_ptr);
591 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
594 switch (color_type)
596 case PNG_COLOR_TYPE_GRAY:
597 This->bpp = bit_depth;
598 switch (bit_depth)
600 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
601 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
602 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
603 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
604 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
605 default:
606 ERR("invalid grayscale bit depth: %i\n", bit_depth);
607 hr = E_FAIL;
608 goto end;
610 break;
611 case PNG_COLOR_TYPE_GRAY_ALPHA:
612 /* WIC does not support grayscale alpha formats so use RGBA */
613 ppng_set_gray_to_rgb(This->png_ptr);
614 /* fall through */
615 case PNG_COLOR_TYPE_RGB_ALPHA:
616 This->bpp = bit_depth * 4;
617 switch (bit_depth)
619 case 8:
620 ppng_set_bgr(This->png_ptr);
621 This->format = &GUID_WICPixelFormat32bppBGRA;
622 break;
623 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
624 default:
625 ERR("invalid RGBA bit depth: %i\n", bit_depth);
626 hr = E_FAIL;
627 goto end;
629 break;
630 case PNG_COLOR_TYPE_PALETTE:
631 This->bpp = bit_depth;
632 switch (bit_depth)
634 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
635 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
636 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
637 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
638 default:
639 ERR("invalid indexed color bit depth: %i\n", bit_depth);
640 hr = E_FAIL;
641 goto end;
643 break;
644 case PNG_COLOR_TYPE_RGB:
645 This->bpp = bit_depth * 3;
646 switch (bit_depth)
648 case 8:
649 ppng_set_bgr(This->png_ptr);
650 This->format = &GUID_WICPixelFormat24bppBGR;
651 break;
652 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
653 default:
654 ERR("invalid RGB color bit depth: %i\n", bit_depth);
655 hr = E_FAIL;
656 goto end;
658 break;
659 default:
660 ERR("invalid color type %i\n", color_type);
661 hr = E_FAIL;
662 goto end;
665 /* read the image data */
666 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
667 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
668 This->stride = This->width * This->bpp;
669 image_size = This->stride * This->height;
671 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
672 if (!This->image_bits)
674 hr = E_OUTOFMEMORY;
675 goto end;
678 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
679 if (!row_pointers)
681 hr = E_OUTOFMEMORY;
682 goto end;
685 for (i=0; i<This->height; i++)
686 row_pointers[i] = This->image_bits + i * This->stride;
688 ppng_read_image(This->png_ptr, row_pointers);
690 HeapFree(GetProcessHeap(), 0, row_pointers);
691 row_pointers = NULL;
693 ppng_read_end(This->png_ptr, This->end_info);
695 /* Find the metadata chunks in the file. */
696 seek.QuadPart = 8;
697 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, &chunk_start);
698 if (FAILED(hr)) goto end;
702 hr = read_png_chunk(pIStream, chunk_type, NULL, &chunk_size);
703 if (FAILED(hr)) goto end;
705 if (chunk_type[0] >= 'a' && chunk_type[0] <= 'z' &&
706 memcmp(chunk_type, "tRNS", 4) && memcmp(chunk_type, "pHYs", 4))
708 /* This chunk is considered metadata. */
709 if (This->metadata_count == metadata_blocks_size)
711 metadata_block_info* new_metadata_blocks;
712 ULONG new_metadata_blocks_size;
714 new_metadata_blocks_size = 4 + metadata_blocks_size * 2;
715 new_metadata_blocks = HeapAlloc(GetProcessHeap(), 0,
716 new_metadata_blocks_size * sizeof(*new_metadata_blocks));
718 if (!new_metadata_blocks)
720 hr = E_OUTOFMEMORY;
721 goto end;
724 memcpy(new_metadata_blocks, This->metadata_blocks,
725 This->metadata_count * sizeof(*new_metadata_blocks));
727 HeapFree(GetProcessHeap(), 0, This->metadata_blocks);
728 This->metadata_blocks = new_metadata_blocks;
729 metadata_blocks_size = new_metadata_blocks_size;
732 This->metadata_blocks[This->metadata_count].ofs = chunk_start;
733 This->metadata_blocks[This->metadata_count].len.QuadPart = chunk_size + 12;
734 This->metadata_blocks[This->metadata_count].reader = NULL;
735 This->metadata_count++;
738 seek.QuadPart = chunk_start.QuadPart + chunk_size + 12; /* skip data and CRC */
739 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, &chunk_start);
740 if (FAILED(hr)) goto end;
741 } while (memcmp(chunk_type, "IEND", 4));
743 This->stream = pIStream;
744 IStream_AddRef(This->stream);
746 This->initialized = TRUE;
748 end:
749 LeaveCriticalSection(&This->lock);
751 return hr;
754 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
755 GUID *pguidContainerFormat)
757 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
758 return S_OK;
761 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
762 IWICBitmapDecoderInfo **ppIDecoderInfo)
764 HRESULT hr;
765 IWICComponentInfo *compinfo;
767 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
769 hr = CreateComponentInfo(&CLSID_WICPngDecoder, &compinfo);
770 if (FAILED(hr)) return hr;
772 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
773 (void**)ppIDecoderInfo);
775 IWICComponentInfo_Release(compinfo);
777 return hr;
780 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
781 IWICPalette *pIPalette)
783 FIXME("(%p,%p): stub\n", iface, pIPalette);
784 return E_NOTIMPL;
787 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
788 IWICMetadataQueryReader **ppIMetadataQueryReader)
790 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
791 return E_NOTIMPL;
794 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
795 IWICBitmapSource **ppIBitmapSource)
797 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
799 if (!ppIBitmapSource) return E_INVALIDARG;
801 *ppIBitmapSource = NULL;
802 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
805 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
806 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
808 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
809 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
812 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
813 IWICBitmapSource **ppIThumbnail)
815 TRACE("(%p,%p)\n", iface, ppIThumbnail);
817 if (!ppIThumbnail) return E_INVALIDARG;
819 *ppIThumbnail = NULL;
820 return WINCODEC_ERR_CODECNOTHUMBNAIL;
823 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
824 UINT *pCount)
826 if (!pCount) return E_INVALIDARG;
828 *pCount = 1;
829 return S_OK;
832 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
833 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
835 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
836 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
838 if (!This->initialized) return WINCODEC_ERR_FRAMEMISSING;
840 if (index != 0) return E_INVALIDARG;
842 IWICBitmapDecoder_AddRef(iface);
844 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
846 return S_OK;
849 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
850 PngDecoder_QueryInterface,
851 PngDecoder_AddRef,
852 PngDecoder_Release,
853 PngDecoder_QueryCapability,
854 PngDecoder_Initialize,
855 PngDecoder_GetContainerFormat,
856 PngDecoder_GetDecoderInfo,
857 PngDecoder_CopyPalette,
858 PngDecoder_GetMetadataQueryReader,
859 PngDecoder_GetPreview,
860 PngDecoder_GetColorContexts,
861 PngDecoder_GetThumbnail,
862 PngDecoder_GetFrameCount,
863 PngDecoder_GetFrame
866 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
867 void **ppv)
869 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
870 if (!ppv) return E_INVALIDARG;
872 if (IsEqualIID(&IID_IUnknown, iid) ||
873 IsEqualIID(&IID_IWICBitmapSource, iid) ||
874 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
876 *ppv = &This->IWICBitmapFrameDecode_iface;
878 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
880 *ppv = &This->IWICMetadataBlockReader_iface;
882 else
884 *ppv = NULL;
885 return E_NOINTERFACE;
888 IUnknown_AddRef((IUnknown*)*ppv);
889 return S_OK;
892 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
894 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
895 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
898 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
900 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
901 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
904 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
905 UINT *puiWidth, UINT *puiHeight)
907 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
908 *puiWidth = This->width;
909 *puiHeight = This->height;
910 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
911 return S_OK;
914 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
915 WICPixelFormatGUID *pPixelFormat)
917 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
918 TRACE("(%p,%p)\n", iface, pPixelFormat);
920 memcpy(pPixelFormat, This->format, sizeof(GUID));
922 return S_OK;
925 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
926 double *pDpiX, double *pDpiY)
928 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
929 png_uint_32 ret, xres, yres;
930 int unit_type;
932 EnterCriticalSection(&This->lock);
934 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
936 if (ret && unit_type == PNG_RESOLUTION_METER)
938 *pDpiX = xres * 0.0254;
939 *pDpiY = yres * 0.0254;
941 else
943 WARN("no pHYs block present\n");
944 *pDpiX = *pDpiY = 96.0;
947 LeaveCriticalSection(&This->lock);
949 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
951 return S_OK;
954 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
955 IWICPalette *pIPalette)
957 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
958 png_uint_32 ret;
959 png_colorp png_palette;
960 int num_palette;
961 WICColor palette[256];
962 png_bytep trans_alpha;
963 int num_trans;
964 png_color_16p trans_values;
965 int i;
966 HRESULT hr=S_OK;
968 TRACE("(%p,%p)\n", iface, pIPalette);
970 EnterCriticalSection(&This->lock);
972 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
973 if (!ret)
975 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
976 goto end;
979 if (num_palette > 256)
981 ERR("palette has %i colors?!\n", num_palette);
982 hr = E_FAIL;
983 goto end;
986 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans_alpha, &num_trans, &trans_values);
987 if (!ret) num_trans = 0;
989 for (i=0; i<num_palette; i++)
991 BYTE alpha = (i < num_trans) ? trans_alpha[i] : 0xff;
992 palette[i] = (alpha << 24 |
993 png_palette[i].red << 16|
994 png_palette[i].green << 8|
995 png_palette[i].blue);
998 end:
1000 LeaveCriticalSection(&This->lock);
1002 if (SUCCEEDED(hr))
1003 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
1005 return hr;
1008 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
1009 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
1011 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
1012 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
1014 return copy_pixels(This->bpp, This->image_bits,
1015 This->width, This->height, This->stride,
1016 prc, cbStride, cbBufferSize, pbBuffer);
1019 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1020 IWICMetadataQueryReader **ppIMetadataQueryReader)
1022 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
1023 return E_NOTIMPL;
1026 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
1027 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1029 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
1030 png_charp name;
1031 BYTE *profile;
1032 png_uint_32 len;
1033 int compression_type;
1034 HRESULT hr;
1036 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
1038 if (!pcActualCount) return E_INVALIDARG;
1040 EnterCriticalSection(&This->lock);
1042 if (ppng_get_iCCP(This->png_ptr, This->info_ptr, &name, &compression_type, (void *)&profile, &len))
1044 if (cCount && ppIColorContexts)
1046 hr = IWICColorContext_InitializeFromMemory(*ppIColorContexts, profile, len);
1047 if (FAILED(hr))
1049 LeaveCriticalSection(&This->lock);
1050 return hr;
1053 *pcActualCount = 1;
1055 else
1056 *pcActualCount = 0;
1058 LeaveCriticalSection(&This->lock);
1060 return S_OK;
1063 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
1064 IWICBitmapSource **ppIThumbnail)
1066 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1068 if (!ppIThumbnail) return E_INVALIDARG;
1070 *ppIThumbnail = NULL;
1071 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1074 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
1075 PngDecoder_Frame_QueryInterface,
1076 PngDecoder_Frame_AddRef,
1077 PngDecoder_Frame_Release,
1078 PngDecoder_Frame_GetSize,
1079 PngDecoder_Frame_GetPixelFormat,
1080 PngDecoder_Frame_GetResolution,
1081 PngDecoder_Frame_CopyPalette,
1082 PngDecoder_Frame_CopyPixels,
1083 PngDecoder_Frame_GetMetadataQueryReader,
1084 PngDecoder_Frame_GetColorContexts,
1085 PngDecoder_Frame_GetThumbnail
1088 static HRESULT WINAPI PngDecoder_Block_QueryInterface(IWICMetadataBlockReader *iface, REFIID iid,
1089 void **ppv)
1091 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1092 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
1095 static ULONG WINAPI PngDecoder_Block_AddRef(IWICMetadataBlockReader *iface)
1097 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1098 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
1101 static ULONG WINAPI PngDecoder_Block_Release(IWICMetadataBlockReader *iface)
1103 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1104 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1107 static HRESULT WINAPI PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1108 GUID *pguidContainerFormat)
1110 if (!pguidContainerFormat) return E_INVALIDARG;
1111 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
1112 return S_OK;
1115 static HRESULT WINAPI PngDecoder_Block_GetCount(IWICMetadataBlockReader *iface,
1116 UINT *pcCount)
1118 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1120 TRACE("%p,%p\n", iface, pcCount);
1122 if (!pcCount) return E_INVALIDARG;
1124 *pcCount = This->metadata_count;
1126 return S_OK;
1129 static HRESULT WINAPI PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1130 UINT nIndex, IWICMetadataReader **ppIMetadataReader)
1132 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1133 HRESULT hr;
1134 IWICComponentFactory* factory;
1135 IWICStream* stream;
1137 TRACE("%p,%d,%p\n", iface, nIndex, ppIMetadataReader);
1139 if (nIndex >= This->metadata_count || !ppIMetadataReader)
1140 return E_INVALIDARG;
1142 if (!This->metadata_blocks[nIndex].reader)
1144 hr = StreamImpl_Create(&stream);
1146 if (SUCCEEDED(hr))
1148 hr = IWICStream_InitializeFromIStreamRegion(stream, This->stream,
1149 This->metadata_blocks[nIndex].ofs, This->metadata_blocks[nIndex].len);
1151 if (SUCCEEDED(hr))
1152 hr = ComponentFactory_CreateInstance(&IID_IWICComponentFactory, (void**)&factory);
1154 if (SUCCEEDED(hr))
1156 hr = IWICComponentFactory_CreateMetadataReaderFromContainer(factory,
1157 &GUID_ContainerFormatPng, NULL, WICMetadataCreationAllowUnknown,
1158 (IStream*)stream, &This->metadata_blocks[nIndex].reader);
1160 IWICComponentFactory_Release(factory);
1163 IWICStream_Release(stream);
1166 if (FAILED(hr))
1168 *ppIMetadataReader = NULL;
1169 return hr;
1173 *ppIMetadataReader = This->metadata_blocks[nIndex].reader;
1174 IWICMetadataReader_AddRef(*ppIMetadataReader);
1176 return S_OK;
1179 static HRESULT WINAPI PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1180 IEnumUnknown **ppIEnumMetadata)
1182 FIXME("%p,%p\n", iface, ppIEnumMetadata);
1183 return E_NOTIMPL;
1186 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl = {
1187 PngDecoder_Block_QueryInterface,
1188 PngDecoder_Block_AddRef,
1189 PngDecoder_Block_Release,
1190 PngDecoder_Block_GetContainerFormat,
1191 PngDecoder_Block_GetCount,
1192 PngDecoder_Block_GetReaderByIndex,
1193 PngDecoder_Block_GetEnumerator,
1196 HRESULT PngDecoder_CreateInstance(REFIID iid, void** ppv)
1198 PngDecoder *This;
1199 HRESULT ret;
1201 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1203 *ppv = NULL;
1205 if (!load_libpng())
1207 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
1208 return E_FAIL;
1211 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
1212 if (!This) return E_OUTOFMEMORY;
1214 This->IWICBitmapDecoder_iface.lpVtbl = &PngDecoder_Vtbl;
1215 This->IWICBitmapFrameDecode_iface.lpVtbl = &PngDecoder_FrameVtbl;
1216 This->IWICMetadataBlockReader_iface.lpVtbl = &PngDecoder_BlockVtbl;
1217 This->ref = 1;
1218 This->png_ptr = NULL;
1219 This->info_ptr = NULL;
1220 This->end_info = NULL;
1221 This->initialized = FALSE;
1222 This->image_bits = NULL;
1223 InitializeCriticalSection(&This->lock);
1224 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngDecoder.lock");
1225 This->metadata_count = 0;
1226 This->metadata_blocks = NULL;
1228 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1229 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1231 return ret;
1234 struct png_pixelformat {
1235 const WICPixelFormatGUID *guid;
1236 UINT bpp;
1237 int bit_depth;
1238 int color_type;
1239 BOOL remove_filler;
1240 BOOL swap_rgb;
1243 static const struct png_pixelformat formats[] = {
1244 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
1245 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
1246 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
1247 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
1248 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
1249 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
1250 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
1251 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
1252 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
1253 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
1254 {NULL},
1257 typedef struct PngEncoder {
1258 IWICBitmapEncoder IWICBitmapEncoder_iface;
1259 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1260 LONG ref;
1261 IStream *stream;
1262 png_structp png_ptr;
1263 png_infop info_ptr;
1264 UINT frame_count;
1265 BOOL frame_initialized;
1266 const struct png_pixelformat *format;
1267 BOOL info_written;
1268 UINT width, height;
1269 double xres, yres;
1270 UINT lines_written;
1271 BOOL frame_committed;
1272 BOOL committed;
1273 CRITICAL_SECTION lock;
1274 BOOL interlace;
1275 BYTE *data;
1276 UINT stride;
1277 UINT passes;
1278 } PngEncoder;
1280 static inline PngEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1282 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapEncoder_iface);
1285 static inline PngEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1287 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapFrameEncode_iface);
1290 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1291 void **ppv)
1293 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1294 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1296 if (!ppv) return E_INVALIDARG;
1298 if (IsEqualIID(&IID_IUnknown, iid) ||
1299 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1301 *ppv = &This->IWICBitmapFrameEncode_iface;
1303 else
1305 *ppv = NULL;
1306 return E_NOINTERFACE;
1309 IUnknown_AddRef((IUnknown*)*ppv);
1310 return S_OK;
1313 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1315 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1316 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
1319 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
1321 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1322 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1325 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1326 IPropertyBag2 *pIEncoderOptions)
1328 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1329 BOOL interlace;
1330 PROPBAG2 opts[1]= {{0}};
1331 VARIANT opt_values[1];
1332 HRESULT opt_hres[1];
1333 HRESULT hr;
1335 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1337 opts[0].pstrName = (LPOLESTR)wszPngInterlaceOption;
1338 opts[0].vt = VT_BOOL;
1340 if (pIEncoderOptions)
1342 hr = IPropertyBag2_Read(pIEncoderOptions, 1, opts, NULL, opt_values, opt_hres);
1344 if (FAILED(hr))
1345 return hr;
1347 else
1348 memset(opt_values, 0, sizeof(opt_values));
1350 if (V_VT(&opt_values[0]) == VT_EMPTY)
1351 interlace = FALSE;
1352 else
1353 interlace = (V_BOOL(&opt_values[0]) != 0);
1355 EnterCriticalSection(&This->lock);
1357 if (This->frame_initialized)
1359 LeaveCriticalSection(&This->lock);
1360 return WINCODEC_ERR_WRONGSTATE;
1363 This->interlace = interlace;
1365 This->frame_initialized = TRUE;
1367 LeaveCriticalSection(&This->lock);
1369 return S_OK;
1372 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1373 UINT uiWidth, UINT uiHeight)
1375 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1376 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1378 EnterCriticalSection(&This->lock);
1380 if (!This->frame_initialized || This->info_written)
1382 LeaveCriticalSection(&This->lock);
1383 return WINCODEC_ERR_WRONGSTATE;
1386 This->width = uiWidth;
1387 This->height = uiHeight;
1389 LeaveCriticalSection(&This->lock);
1391 return S_OK;
1394 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1395 double dpiX, double dpiY)
1397 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1398 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1400 EnterCriticalSection(&This->lock);
1402 if (!This->frame_initialized || This->info_written)
1404 LeaveCriticalSection(&This->lock);
1405 return WINCODEC_ERR_WRONGSTATE;
1408 This->xres = dpiX;
1409 This->yres = dpiY;
1411 LeaveCriticalSection(&This->lock);
1413 return S_OK;
1416 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1417 WICPixelFormatGUID *pPixelFormat)
1419 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1420 int i;
1421 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1423 EnterCriticalSection(&This->lock);
1425 if (!This->frame_initialized || This->info_written)
1427 LeaveCriticalSection(&This->lock);
1428 return WINCODEC_ERR_WRONGSTATE;
1431 for (i=0; formats[i].guid; i++)
1433 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1434 break;
1437 if (!formats[i].guid) i = 0;
1439 This->format = &formats[i];
1440 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1442 LeaveCriticalSection(&This->lock);
1444 return S_OK;
1447 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1448 UINT cCount, IWICColorContext **ppIColorContext)
1450 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1451 return E_NOTIMPL;
1454 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1455 IWICPalette *pIPalette)
1457 FIXME("(%p,%p): stub\n", iface, pIPalette);
1458 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1461 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1462 IWICBitmapSource *pIThumbnail)
1464 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1465 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1468 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1469 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1471 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1472 png_byte **row_pointers=NULL;
1473 UINT i;
1474 jmp_buf jmpbuf;
1475 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1477 EnterCriticalSection(&This->lock);
1479 if (!This->frame_initialized || !This->width || !This->height || !This->format)
1481 LeaveCriticalSection(&This->lock);
1482 return WINCODEC_ERR_WRONGSTATE;
1485 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1487 LeaveCriticalSection(&This->lock);
1488 return E_INVALIDARG;
1491 /* set up setjmp/longjmp error handling */
1492 if (setjmp(jmpbuf))
1494 LeaveCriticalSection(&This->lock);
1495 HeapFree(GetProcessHeap(), 0, row_pointers);
1496 return E_FAIL;
1498 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1500 if (!This->info_written)
1502 if (This->interlace)
1504 /* libpng requires us to write all data multiple times in this case. */
1505 This->stride = (This->format->bpp * This->width + 7)/8;
1506 This->data = HeapAlloc(GetProcessHeap(), 0, This->height * This->stride);
1507 if (!This->data)
1509 LeaveCriticalSection(&This->lock);
1510 return E_OUTOFMEMORY;
1514 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
1515 This->format->bit_depth, This->format->color_type,
1516 This->interlace ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE,
1517 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1519 if (This->xres != 0.0 && This->yres != 0.0)
1521 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
1522 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
1525 ppng_write_info(This->png_ptr, This->info_ptr);
1527 if (This->format->remove_filler)
1528 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1530 if (This->format->swap_rgb)
1531 ppng_set_bgr(This->png_ptr);
1533 if (This->interlace)
1534 This->passes = ppng_set_interlace_handling(This->png_ptr);
1536 This->info_written = TRUE;
1539 if (This->interlace)
1541 /* Just store the data so we can write it in multiple passes in Commit. */
1542 for (i=0; i<lineCount; i++)
1543 memcpy(This->data + This->stride * (This->lines_written + i),
1544 pbPixels + cbStride * i,
1545 This->stride);
1547 This->lines_written += lineCount;
1549 LeaveCriticalSection(&This->lock);
1550 return S_OK;
1553 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1554 if (!row_pointers)
1556 LeaveCriticalSection(&This->lock);
1557 return E_OUTOFMEMORY;
1560 for (i=0; i<lineCount; i++)
1561 row_pointers[i] = pbPixels + cbStride * i;
1563 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1564 This->lines_written += lineCount;
1566 LeaveCriticalSection(&This->lock);
1568 HeapFree(GetProcessHeap(), 0, row_pointers);
1570 return S_OK;
1573 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1574 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1576 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1577 HRESULT hr;
1578 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1580 if (!This->frame_initialized)
1581 return WINCODEC_ERR_WRONGSTATE;
1583 hr = configure_write_source(iface, pIBitmapSource, prc,
1584 This->format ? This->format->guid : NULL, This->width, This->height,
1585 This->xres, This->yres);
1587 if (SUCCEEDED(hr))
1589 hr = write_source(iface, pIBitmapSource, prc,
1590 This->format->guid, This->format->bpp, This->width, This->height);
1593 return hr;
1596 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1598 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1599 png_byte **row_pointers=NULL;
1600 jmp_buf jmpbuf;
1601 TRACE("(%p)\n", iface);
1603 EnterCriticalSection(&This->lock);
1605 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1607 LeaveCriticalSection(&This->lock);
1608 return WINCODEC_ERR_WRONGSTATE;
1611 /* set up setjmp/longjmp error handling */
1612 if (setjmp(jmpbuf))
1614 LeaveCriticalSection(&This->lock);
1615 HeapFree(GetProcessHeap(), 0, row_pointers);
1616 return E_FAIL;
1618 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1620 if (This->interlace)
1622 int i;
1624 row_pointers = HeapAlloc(GetProcessHeap(), 0, This->height * sizeof(png_byte*));
1625 if (!row_pointers)
1627 LeaveCriticalSection(&This->lock);
1628 return E_OUTOFMEMORY;
1631 for (i=0; i<This->height; i++)
1632 row_pointers[i] = This->data + This->stride * i;
1634 for (i=0; i<This->passes; i++)
1635 ppng_write_rows(This->png_ptr, row_pointers, This->height);
1638 ppng_write_end(This->png_ptr, This->info_ptr);
1640 This->frame_committed = TRUE;
1642 HeapFree(GetProcessHeap(), 0, row_pointers);
1644 LeaveCriticalSection(&This->lock);
1646 return S_OK;
1649 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1650 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1652 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1653 return E_NOTIMPL;
1656 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1657 PngFrameEncode_QueryInterface,
1658 PngFrameEncode_AddRef,
1659 PngFrameEncode_Release,
1660 PngFrameEncode_Initialize,
1661 PngFrameEncode_SetSize,
1662 PngFrameEncode_SetResolution,
1663 PngFrameEncode_SetPixelFormat,
1664 PngFrameEncode_SetColorContexts,
1665 PngFrameEncode_SetPalette,
1666 PngFrameEncode_SetThumbnail,
1667 PngFrameEncode_WritePixels,
1668 PngFrameEncode_WriteSource,
1669 PngFrameEncode_Commit,
1670 PngFrameEncode_GetMetadataQueryWriter
1673 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1674 void **ppv)
1676 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1677 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1679 if (!ppv) return E_INVALIDARG;
1681 if (IsEqualIID(&IID_IUnknown, iid) ||
1682 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1684 *ppv = &This->IWICBitmapEncoder_iface;
1686 else
1688 *ppv = NULL;
1689 return E_NOINTERFACE;
1692 IUnknown_AddRef((IUnknown*)*ppv);
1693 return S_OK;
1696 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1698 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1699 ULONG ref = InterlockedIncrement(&This->ref);
1701 TRACE("(%p) refcount=%u\n", iface, ref);
1703 return ref;
1706 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1708 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1709 ULONG ref = InterlockedDecrement(&This->ref);
1711 TRACE("(%p) refcount=%u\n", iface, ref);
1713 if (ref == 0)
1715 This->lock.DebugInfo->Spare[0] = 0;
1716 DeleteCriticalSection(&This->lock);
1717 if (This->png_ptr)
1718 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1719 if (This->stream)
1720 IStream_Release(This->stream);
1721 HeapFree(GetProcessHeap(), 0, This->data);
1722 HeapFree(GetProcessHeap(), 0, This);
1725 return ref;
1728 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1730 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1731 HRESULT hr;
1732 ULONG byteswritten;
1734 hr = IStream_Write(This->stream, data, length, &byteswritten);
1735 if (FAILED(hr) || byteswritten != length)
1737 ppng_error(png_ptr, "failed writing data");
1741 static void user_flush(png_structp png_ptr)
1745 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1746 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1748 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1749 jmp_buf jmpbuf;
1751 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1753 EnterCriticalSection(&This->lock);
1755 if (This->png_ptr)
1757 LeaveCriticalSection(&This->lock);
1758 return WINCODEC_ERR_WRONGSTATE;
1761 /* initialize libpng */
1762 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1763 if (!This->png_ptr)
1765 LeaveCriticalSection(&This->lock);
1766 return E_FAIL;
1769 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1770 if (!This->info_ptr)
1772 ppng_destroy_write_struct(&This->png_ptr, NULL);
1773 This->png_ptr = NULL;
1774 LeaveCriticalSection(&This->lock);
1775 return E_FAIL;
1778 IStream_AddRef(pIStream);
1779 This->stream = pIStream;
1781 /* set up setjmp/longjmp error handling */
1782 if (setjmp(jmpbuf))
1784 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1785 This->png_ptr = NULL;
1786 IStream_Release(This->stream);
1787 This->stream = NULL;
1788 LeaveCriticalSection(&This->lock);
1789 return E_FAIL;
1791 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1793 /* set up custom i/o handling */
1794 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1796 LeaveCriticalSection(&This->lock);
1798 return S_OK;
1801 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1802 GUID *pguidContainerFormat)
1804 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1805 return E_NOTIMPL;
1808 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1809 IWICBitmapEncoderInfo **ppIEncoderInfo)
1811 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1812 return E_NOTIMPL;
1815 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1816 UINT cCount, IWICColorContext **ppIColorContext)
1818 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1819 return E_NOTIMPL;
1822 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1824 TRACE("(%p,%p)\n", iface, pIPalette);
1825 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1828 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1830 TRACE("(%p,%p)\n", iface, pIThumbnail);
1831 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1834 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1836 TRACE("(%p,%p)\n", iface, pIPreview);
1837 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1840 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1841 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1843 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1844 HRESULT hr;
1845 PROPBAG2 opts[1]= {{0}};
1847 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1849 EnterCriticalSection(&This->lock);
1851 if (This->frame_count != 0)
1853 LeaveCriticalSection(&This->lock);
1854 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1857 if (!This->stream)
1859 LeaveCriticalSection(&This->lock);
1860 return WINCODEC_ERR_NOTINITIALIZED;
1863 opts[0].pstrName = (LPOLESTR)wszPngInterlaceOption;
1864 opts[0].vt = VT_BOOL;
1865 opts[0].dwType = PROPBAG2_TYPE_DATA;
1867 hr = CreatePropertyBag2(opts, 1, ppIEncoderOptions);
1868 if (FAILED(hr))
1870 LeaveCriticalSection(&This->lock);
1871 return hr;
1874 This->frame_count = 1;
1876 LeaveCriticalSection(&This->lock);
1878 IWICBitmapEncoder_AddRef(iface);
1879 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1881 return S_OK;
1884 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1886 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1887 TRACE("(%p)\n", iface);
1889 EnterCriticalSection(&This->lock);
1891 if (!This->frame_committed || This->committed)
1893 LeaveCriticalSection(&This->lock);
1894 return WINCODEC_ERR_WRONGSTATE;
1897 This->committed = TRUE;
1899 LeaveCriticalSection(&This->lock);
1901 return S_OK;
1904 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1905 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1907 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1908 return E_NOTIMPL;
1911 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
1912 PngEncoder_QueryInterface,
1913 PngEncoder_AddRef,
1914 PngEncoder_Release,
1915 PngEncoder_Initialize,
1916 PngEncoder_GetContainerFormat,
1917 PngEncoder_GetEncoderInfo,
1918 PngEncoder_SetColorContexts,
1919 PngEncoder_SetPalette,
1920 PngEncoder_SetThumbnail,
1921 PngEncoder_SetPreview,
1922 PngEncoder_CreateNewFrame,
1923 PngEncoder_Commit,
1924 PngEncoder_GetMetadataQueryWriter
1927 HRESULT PngEncoder_CreateInstance(REFIID iid, void** ppv)
1929 PngEncoder *This;
1930 HRESULT ret;
1932 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1934 *ppv = NULL;
1936 if (!load_libpng())
1938 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1939 return E_FAIL;
1942 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1943 if (!This) return E_OUTOFMEMORY;
1945 This->IWICBitmapEncoder_iface.lpVtbl = &PngEncoder_Vtbl;
1946 This->IWICBitmapFrameEncode_iface.lpVtbl = &PngEncoder_FrameVtbl;
1947 This->ref = 1;
1948 This->png_ptr = NULL;
1949 This->info_ptr = NULL;
1950 This->stream = NULL;
1951 This->frame_count = 0;
1952 This->frame_initialized = FALSE;
1953 This->format = NULL;
1954 This->info_written = FALSE;
1955 This->width = 0;
1956 This->height = 0;
1957 This->xres = 0.0;
1958 This->yres = 0.0;
1959 This->lines_written = 0;
1960 This->frame_committed = FALSE;
1961 This->committed = FALSE;
1962 This->data = NULL;
1963 InitializeCriticalSection(&This->lock);
1964 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngEncoder.lock");
1966 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
1967 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1969 return ret;
1972 #else /* !HAVE_PNG_H */
1974 HRESULT PngDecoder_CreateInstance(REFIID iid, void** ppv)
1976 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1977 return E_FAIL;
1980 HRESULT PngEncoder_CreateInstance(REFIID iid, void** ppv)
1982 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");
1983 return E_FAIL;
1986 #endif