mstask: Improve stubs for ITask::GetTaskFlags and ITask::GetFlags.
[wine.git] / dlls / windowscodecs / pngformat.c
blob26b5fd52bcc0a6c57dfba78879a74507f736e136
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2016 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include <stdarg.h>
25 #ifdef HAVE_PNG_H
26 #include <png.h>
27 #endif
29 #define NONAMELESSUNION
30 #define COBJMACROS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "objbase.h"
36 #include "wincodecs_private.h"
38 #include "wine/debug.h"
39 #include "wine/library.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
43 static inline ULONG read_ulong_be(BYTE* data)
45 return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
48 static HRESULT read_png_chunk(IStream *stream, BYTE *type, BYTE **data, ULONG *data_size)
50 BYTE header[8];
51 HRESULT hr;
52 ULONG bytesread;
54 hr = IStream_Read(stream, header, 8, &bytesread);
55 if (FAILED(hr) || bytesread < 8)
57 if (SUCCEEDED(hr))
58 hr = E_FAIL;
59 return hr;
62 *data_size = read_ulong_be(&header[0]);
64 memcpy(type, &header[4], 4);
66 if (data)
68 *data = HeapAlloc(GetProcessHeap(), 0, *data_size);
69 if (!*data)
70 return E_OUTOFMEMORY;
72 hr = IStream_Read(stream, *data, *data_size, &bytesread);
74 if (FAILED(hr) || bytesread < *data_size)
76 if (SUCCEEDED(hr))
77 hr = E_FAIL;
78 HeapFree(GetProcessHeap(), 0, *data);
79 *data = NULL;
80 return hr;
83 /* Windows ignores CRC of the chunk */
86 return S_OK;
89 static HRESULT LoadTextMetadata(IStream *stream, const GUID *preferred_vendor,
90 DWORD persist_options, MetadataItem **items, DWORD *item_count)
92 HRESULT hr;
93 BYTE type[4];
94 BYTE *data;
95 ULONG data_size;
96 ULONG name_len, value_len;
97 BYTE *name_end_ptr;
98 LPSTR name, value;
99 MetadataItem *result;
101 hr = read_png_chunk(stream, type, &data, &data_size);
102 if (FAILED(hr)) return hr;
104 name_end_ptr = memchr(data, 0, data_size);
106 name_len = name_end_ptr - data;
108 if (!name_end_ptr || name_len > 79)
110 HeapFree(GetProcessHeap(), 0, data);
111 return E_FAIL;
114 value_len = data_size - name_len - 1;
116 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
117 name = HeapAlloc(GetProcessHeap(), 0, name_len + 1);
118 value = HeapAlloc(GetProcessHeap(), 0, value_len + 1);
119 if (!result || !name || !value)
121 HeapFree(GetProcessHeap(), 0, data);
122 HeapFree(GetProcessHeap(), 0, result);
123 HeapFree(GetProcessHeap(), 0, name);
124 HeapFree(GetProcessHeap(), 0, value);
125 return E_OUTOFMEMORY;
128 PropVariantInit(&result[0].schema);
129 PropVariantInit(&result[0].id);
130 PropVariantInit(&result[0].value);
132 memcpy(name, data, name_len + 1);
133 memcpy(value, name_end_ptr + 1, value_len);
134 value[value_len] = 0;
136 result[0].id.vt = VT_LPSTR;
137 result[0].id.u.pszVal = name;
138 result[0].value.vt = VT_LPSTR;
139 result[0].value.u.pszVal = value;
141 *items = result;
142 *item_count = 1;
144 HeapFree(GetProcessHeap(), 0, data);
146 return S_OK;
149 static const MetadataHandlerVtbl TextReader_Vtbl = {
151 &CLSID_WICPngTextMetadataReader,
152 LoadTextMetadata
155 HRESULT PngTextReader_CreateInstance(REFIID iid, void** ppv)
157 return MetadataReader_Create(&TextReader_Vtbl, iid, ppv);
160 static HRESULT LoadGamaMetadata(IStream *stream, const GUID *preferred_vendor,
161 DWORD persist_options, MetadataItem **items, DWORD *item_count)
163 HRESULT hr;
164 BYTE type[4];
165 BYTE *data;
166 ULONG data_size;
167 ULONG gamma;
168 static const WCHAR ImageGamma[] = {'I','m','a','g','e','G','a','m','m','a',0};
169 LPWSTR name;
170 MetadataItem *result;
172 hr = read_png_chunk(stream, type, &data, &data_size);
173 if (FAILED(hr)) return hr;
175 if (data_size < 4)
177 HeapFree(GetProcessHeap(), 0, data);
178 return E_FAIL;
181 gamma = read_ulong_be(data);
183 HeapFree(GetProcessHeap(), 0, data);
185 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
186 name = HeapAlloc(GetProcessHeap(), 0, sizeof(ImageGamma));
187 if (!result || !name)
189 HeapFree(GetProcessHeap(), 0, result);
190 HeapFree(GetProcessHeap(), 0, name);
191 return E_OUTOFMEMORY;
194 PropVariantInit(&result[0].schema);
195 PropVariantInit(&result[0].id);
196 PropVariantInit(&result[0].value);
198 memcpy(name, ImageGamma, sizeof(ImageGamma));
200 result[0].id.vt = VT_LPWSTR;
201 result[0].id.u.pwszVal = name;
202 result[0].value.vt = VT_UI4;
203 result[0].value.u.ulVal = gamma;
205 *items = result;
206 *item_count = 1;
208 return S_OK;
211 static const MetadataHandlerVtbl GamaReader_Vtbl = {
213 &CLSID_WICPngGamaMetadataReader,
214 LoadGamaMetadata
217 HRESULT PngGamaReader_CreateInstance(REFIID iid, void** ppv)
219 return MetadataReader_Create(&GamaReader_Vtbl, iid, ppv);
222 static HRESULT LoadChrmMetadata(IStream *stream, const GUID *preferred_vendor,
223 DWORD persist_options, MetadataItem **items, DWORD *item_count)
225 HRESULT hr;
226 BYTE type[4];
227 BYTE *data;
228 ULONG data_size;
229 static const WCHAR names[8][12] = {
230 {'W','h','i','t','e','P','o','i','n','t','X',0},
231 {'W','h','i','t','e','P','o','i','n','t','Y',0},
232 {'R','e','d','X',0},
233 {'R','e','d','Y',0},
234 {'G','r','e','e','n','X',0},
235 {'G','r','e','e','n','Y',0},
236 {'B','l','u','e','X',0},
237 {'B','l','u','e','Y',0},
239 LPWSTR dyn_names[8] = {0};
240 MetadataItem *result;
241 int i;
243 hr = read_png_chunk(stream, type, &data, &data_size);
244 if (FAILED(hr)) return hr;
246 if (data_size < 32)
248 HeapFree(GetProcessHeap(), 0, data);
249 return E_FAIL;
252 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem)*8);
253 for (i=0; i<8; i++)
255 dyn_names[i] = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(lstrlenW(names[i])+1));
256 if (!dyn_names[i]) break;
258 if (!result || i < 8)
260 HeapFree(GetProcessHeap(), 0, result);
261 for (i=0; i<8; i++)
262 HeapFree(GetProcessHeap(), 0, dyn_names[i]);
263 HeapFree(GetProcessHeap(), 0, data);
264 return E_OUTOFMEMORY;
267 for (i=0; i<8; i++)
269 PropVariantInit(&result[i].schema);
271 PropVariantInit(&result[i].id);
272 result[i].id.vt = VT_LPWSTR;
273 result[i].id.u.pwszVal = dyn_names[i];
274 lstrcpyW(dyn_names[i], names[i]);
276 PropVariantInit(&result[i].value);
277 result[i].value.vt = VT_UI4;
278 result[i].value.u.ulVal = read_ulong_be(&data[i*4]);
281 *items = result;
282 *item_count = 8;
284 HeapFree(GetProcessHeap(), 0, data);
286 return S_OK;
289 static const MetadataHandlerVtbl ChrmReader_Vtbl = {
291 &CLSID_WICPngChrmMetadataReader,
292 LoadChrmMetadata
295 HRESULT PngChrmReader_CreateInstance(REFIID iid, void** ppv)
297 return MetadataReader_Create(&ChrmReader_Vtbl, iid, ppv);
300 #ifdef SONAME_LIBPNG
302 static void *libpng_handle;
303 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
304 MAKE_FUNCPTR(png_create_read_struct);
305 MAKE_FUNCPTR(png_create_info_struct);
306 MAKE_FUNCPTR(png_create_write_struct);
307 MAKE_FUNCPTR(png_destroy_read_struct);
308 MAKE_FUNCPTR(png_destroy_write_struct);
309 MAKE_FUNCPTR(png_error);
310 MAKE_FUNCPTR(png_get_bit_depth);
311 MAKE_FUNCPTR(png_get_color_type);
312 MAKE_FUNCPTR(png_get_error_ptr);
313 MAKE_FUNCPTR(png_get_iCCP);
314 MAKE_FUNCPTR(png_get_image_height);
315 MAKE_FUNCPTR(png_get_image_width);
316 MAKE_FUNCPTR(png_get_io_ptr);
317 MAKE_FUNCPTR(png_get_pHYs);
318 MAKE_FUNCPTR(png_get_PLTE);
319 MAKE_FUNCPTR(png_get_tRNS);
320 MAKE_FUNCPTR(png_set_bgr);
321 MAKE_FUNCPTR(png_set_crc_action);
322 MAKE_FUNCPTR(png_set_error_fn);
323 MAKE_FUNCPTR(png_set_filler);
324 MAKE_FUNCPTR(png_set_filter);
325 MAKE_FUNCPTR(png_set_gray_to_rgb);
326 MAKE_FUNCPTR(png_set_interlace_handling);
327 MAKE_FUNCPTR(png_set_IHDR);
328 MAKE_FUNCPTR(png_set_pHYs);
329 MAKE_FUNCPTR(png_set_PLTE);
330 MAKE_FUNCPTR(png_set_read_fn);
331 MAKE_FUNCPTR(png_set_strip_16);
332 MAKE_FUNCPTR(png_set_tRNS);
333 MAKE_FUNCPTR(png_set_tRNS_to_alpha);
334 MAKE_FUNCPTR(png_set_write_fn);
335 MAKE_FUNCPTR(png_read_end);
336 MAKE_FUNCPTR(png_read_image);
337 MAKE_FUNCPTR(png_read_info);
338 MAKE_FUNCPTR(png_write_end);
339 MAKE_FUNCPTR(png_write_info);
340 MAKE_FUNCPTR(png_write_rows);
341 #undef MAKE_FUNCPTR
343 static CRITICAL_SECTION init_png_cs;
344 static CRITICAL_SECTION_DEBUG init_png_cs_debug =
346 0, 0, &init_png_cs,
347 { &init_png_cs_debug.ProcessLocksList,
348 &init_png_cs_debug.ProcessLocksList },
349 0, 0, { (DWORD_PTR)(__FILE__ ": init_png_cs") }
351 static CRITICAL_SECTION init_png_cs = { &init_png_cs_debug, -1, 0, 0, 0, 0 };
353 static const WCHAR wszPngInterlaceOption[] = {'I','n','t','e','r','l','a','c','e','O','p','t','i','o','n',0};
354 static const WCHAR wszPngFilterOption[] = {'F','i','l','t','e','r','O','p','t','i','o','n',0};
356 static void *load_libpng(void)
358 void *result;
360 EnterCriticalSection(&init_png_cs);
362 if(!libpng_handle && (libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
364 #define LOAD_FUNCPTR(f) \
365 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
366 libpng_handle = NULL; \
367 LeaveCriticalSection(&init_png_cs); \
368 return NULL; \
370 LOAD_FUNCPTR(png_create_read_struct);
371 LOAD_FUNCPTR(png_create_info_struct);
372 LOAD_FUNCPTR(png_create_write_struct);
373 LOAD_FUNCPTR(png_destroy_read_struct);
374 LOAD_FUNCPTR(png_destroy_write_struct);
375 LOAD_FUNCPTR(png_error);
376 LOAD_FUNCPTR(png_get_bit_depth);
377 LOAD_FUNCPTR(png_get_color_type);
378 LOAD_FUNCPTR(png_get_error_ptr);
379 LOAD_FUNCPTR(png_get_iCCP);
380 LOAD_FUNCPTR(png_get_image_height);
381 LOAD_FUNCPTR(png_get_image_width);
382 LOAD_FUNCPTR(png_get_io_ptr);
383 LOAD_FUNCPTR(png_get_pHYs);
384 LOAD_FUNCPTR(png_get_PLTE);
385 LOAD_FUNCPTR(png_get_tRNS);
386 LOAD_FUNCPTR(png_set_bgr);
387 LOAD_FUNCPTR(png_set_crc_action);
388 LOAD_FUNCPTR(png_set_error_fn);
389 LOAD_FUNCPTR(png_set_filler);
390 LOAD_FUNCPTR(png_set_filter);
391 LOAD_FUNCPTR(png_set_gray_to_rgb);
392 LOAD_FUNCPTR(png_set_interlace_handling);
393 LOAD_FUNCPTR(png_set_IHDR);
394 LOAD_FUNCPTR(png_set_pHYs);
395 LOAD_FUNCPTR(png_set_PLTE);
396 LOAD_FUNCPTR(png_set_read_fn);
397 LOAD_FUNCPTR(png_set_strip_16);
398 LOAD_FUNCPTR(png_set_tRNS);
399 LOAD_FUNCPTR(png_set_tRNS_to_alpha);
400 LOAD_FUNCPTR(png_set_write_fn);
401 LOAD_FUNCPTR(png_read_end);
402 LOAD_FUNCPTR(png_read_image);
403 LOAD_FUNCPTR(png_read_info);
404 LOAD_FUNCPTR(png_write_end);
405 LOAD_FUNCPTR(png_write_info);
406 LOAD_FUNCPTR(png_write_rows);
408 #undef LOAD_FUNCPTR
411 result = libpng_handle;
413 LeaveCriticalSection(&init_png_cs);
415 return result;
418 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
420 jmp_buf *pjmpbuf;
422 /* This uses setjmp/longjmp just like the default. We can't use the
423 * default because there's no way to access the jmp buffer in the png_struct
424 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
425 WARN("PNG error: %s\n", debugstr_a(error_message));
426 pjmpbuf = ppng_get_error_ptr(png_ptr);
427 longjmp(*pjmpbuf, 1);
430 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
432 WARN("PNG warning: %s\n", debugstr_a(warning_message));
435 typedef struct {
436 ULARGE_INTEGER ofs, len;
437 IWICMetadataReader* reader;
438 } metadata_block_info;
440 typedef struct {
441 IWICBitmapDecoder IWICBitmapDecoder_iface;
442 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
443 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
444 LONG ref;
445 IStream *stream;
446 png_structp png_ptr;
447 png_infop info_ptr;
448 png_infop end_info;
449 BOOL initialized;
450 int bpp;
451 int width, height;
452 UINT stride;
453 const WICPixelFormatGUID *format;
454 BYTE *image_bits;
455 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
456 ULONG metadata_count;
457 metadata_block_info* metadata_blocks;
458 } PngDecoder;
460 static inline PngDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
462 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapDecoder_iface);
465 static inline PngDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
467 return CONTAINING_RECORD(iface, PngDecoder, IWICBitmapFrameDecode_iface);
470 static inline PngDecoder *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
472 return CONTAINING_RECORD(iface, PngDecoder, IWICMetadataBlockReader_iface);
475 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
477 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
478 void **ppv)
480 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
481 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
483 if (!ppv) return E_INVALIDARG;
485 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
487 *ppv = &This->IWICBitmapDecoder_iface;
489 else
491 *ppv = NULL;
492 return E_NOINTERFACE;
495 IUnknown_AddRef((IUnknown*)*ppv);
496 return S_OK;
499 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
501 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
502 ULONG ref = InterlockedIncrement(&This->ref);
504 TRACE("(%p) refcount=%u\n", iface, ref);
506 return ref;
509 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
511 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
512 ULONG ref = InterlockedDecrement(&This->ref);
513 ULONG i;
515 TRACE("(%p) refcount=%u\n", iface, ref);
517 if (ref == 0)
519 if (This->stream)
520 IStream_Release(This->stream);
521 if (This->png_ptr)
522 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
523 This->lock.DebugInfo->Spare[0] = 0;
524 DeleteCriticalSection(&This->lock);
525 HeapFree(GetProcessHeap(), 0, This->image_bits);
526 for (i=0; i<This->metadata_count; i++)
528 if (This->metadata_blocks[i].reader)
529 IWICMetadataReader_Release(This->metadata_blocks[i].reader);
531 HeapFree(GetProcessHeap(), 0, This->metadata_blocks);
532 HeapFree(GetProcessHeap(), 0, This);
535 return ref;
538 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
539 DWORD *capability)
541 HRESULT hr;
543 TRACE("(%p,%p,%p)\n", iface, stream, capability);
545 if (!stream || !capability) return E_INVALIDARG;
547 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
548 if (hr != S_OK) return hr;
550 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
551 WICBitmapDecoderCapabilityCanDecodeSomeImages;
552 /* FIXME: WICBitmapDecoderCapabilityCanEnumerateMetadata */
553 return S_OK;
556 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
558 IStream *stream = ppng_get_io_ptr(png_ptr);
559 HRESULT hr;
560 ULONG bytesread;
562 hr = IStream_Read(stream, data, length, &bytesread);
563 if (FAILED(hr) || bytesread != length)
565 ppng_error(png_ptr, "failed reading data");
569 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
570 WICDecodeOptions cacheOptions)
572 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
573 LARGE_INTEGER seek;
574 HRESULT hr=S_OK;
575 png_bytep *row_pointers=NULL;
576 UINT image_size;
577 UINT i;
578 int color_type, bit_depth;
579 png_bytep trans;
580 int num_trans;
581 png_uint_32 transparency;
582 png_color_16p trans_values;
583 jmp_buf jmpbuf;
584 BYTE chunk_type[4];
585 ULONG chunk_size;
586 ULARGE_INTEGER chunk_start;
587 ULONG metadata_blocks_size = 0;
589 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
591 EnterCriticalSection(&This->lock);
593 /* initialize libpng */
594 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
595 if (!This->png_ptr)
597 hr = E_FAIL;
598 goto end;
601 This->info_ptr = ppng_create_info_struct(This->png_ptr);
602 if (!This->info_ptr)
604 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
605 This->png_ptr = NULL;
606 hr = E_FAIL;
607 goto end;
610 This->end_info = ppng_create_info_struct(This->png_ptr);
611 if (!This->end_info)
613 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
614 This->png_ptr = NULL;
615 hr = E_FAIL;
616 goto end;
619 /* set up setjmp/longjmp error handling */
620 if (setjmp(jmpbuf))
622 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
623 HeapFree(GetProcessHeap(), 0, row_pointers);
624 This->png_ptr = NULL;
625 hr = E_FAIL;
626 goto end;
628 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
629 ppng_set_crc_action(This->png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
631 /* seek to the start of the stream */
632 seek.QuadPart = 0;
633 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
634 if (FAILED(hr)) goto end;
636 /* set up custom i/o handling */
637 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
639 /* read the header */
640 ppng_read_info(This->png_ptr, This->info_ptr);
642 /* choose a pixel format */
643 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
644 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
646 /* check for color-keyed alpha */
647 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
649 if (transparency && (color_type == PNG_COLOR_TYPE_RGB ||
650 (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 16)))
652 /* expand to RGBA */
653 if (color_type == PNG_COLOR_TYPE_GRAY)
654 ppng_set_gray_to_rgb(This->png_ptr);
655 ppng_set_tRNS_to_alpha(This->png_ptr);
656 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
659 switch (color_type)
661 case PNG_COLOR_TYPE_GRAY_ALPHA:
662 /* WIC does not support grayscale alpha formats so use RGBA */
663 ppng_set_gray_to_rgb(This->png_ptr);
664 /* fall through */
665 case PNG_COLOR_TYPE_RGB_ALPHA:
666 This->bpp = bit_depth * 4;
667 switch (bit_depth)
669 case 8:
670 ppng_set_bgr(This->png_ptr);
671 This->format = &GUID_WICPixelFormat32bppBGRA;
672 break;
673 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
674 default:
675 ERR("invalid RGBA bit depth: %i\n", bit_depth);
676 hr = E_FAIL;
677 goto end;
679 break;
680 case PNG_COLOR_TYPE_GRAY:
681 This->bpp = bit_depth;
682 if (!transparency)
684 switch (bit_depth)
686 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
687 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
688 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
689 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
690 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
691 default:
692 ERR("invalid grayscale bit depth: %i\n", bit_depth);
693 hr = E_FAIL;
694 goto end;
696 break;
698 /* else fall through */
699 case PNG_COLOR_TYPE_PALETTE:
700 This->bpp = bit_depth;
701 switch (bit_depth)
703 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
704 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
705 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
706 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
707 default:
708 ERR("invalid indexed color bit depth: %i\n", bit_depth);
709 hr = E_FAIL;
710 goto end;
712 break;
713 case PNG_COLOR_TYPE_RGB:
714 This->bpp = bit_depth * 3;
715 switch (bit_depth)
717 case 8:
718 ppng_set_bgr(This->png_ptr);
719 This->format = &GUID_WICPixelFormat24bppBGR;
720 break;
721 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
722 default:
723 ERR("invalid RGB color bit depth: %i\n", bit_depth);
724 hr = E_FAIL;
725 goto end;
727 break;
728 default:
729 ERR("invalid color type %i\n", color_type);
730 hr = E_FAIL;
731 goto end;
734 /* read the image data */
735 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
736 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
737 This->stride = (This->width * This->bpp + 7) / 8;
738 image_size = This->stride * This->height;
740 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
741 if (!This->image_bits)
743 hr = E_OUTOFMEMORY;
744 goto end;
747 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
748 if (!row_pointers)
750 hr = E_OUTOFMEMORY;
751 goto end;
754 for (i=0; i<This->height; i++)
755 row_pointers[i] = This->image_bits + i * This->stride;
757 ppng_read_image(This->png_ptr, row_pointers);
759 HeapFree(GetProcessHeap(), 0, row_pointers);
760 row_pointers = NULL;
762 ppng_read_end(This->png_ptr, This->end_info);
764 /* Find the metadata chunks in the file. */
765 seek.QuadPart = 8;
769 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, &chunk_start);
770 if (FAILED(hr)) goto end;
772 hr = read_png_chunk(pIStream, chunk_type, NULL, &chunk_size);
773 if (FAILED(hr)) goto end;
775 if (chunk_type[0] >= 'a' && chunk_type[0] <= 'z' &&
776 memcmp(chunk_type, "tRNS", 4) && memcmp(chunk_type, "pHYs", 4))
778 /* This chunk is considered metadata. */
779 if (This->metadata_count == metadata_blocks_size)
781 metadata_block_info* new_metadata_blocks;
782 ULONG new_metadata_blocks_size;
784 new_metadata_blocks_size = 4 + metadata_blocks_size * 2;
785 new_metadata_blocks = HeapAlloc(GetProcessHeap(), 0,
786 new_metadata_blocks_size * sizeof(*new_metadata_blocks));
788 if (!new_metadata_blocks)
790 hr = E_OUTOFMEMORY;
791 goto end;
794 memcpy(new_metadata_blocks, This->metadata_blocks,
795 This->metadata_count * sizeof(*new_metadata_blocks));
797 HeapFree(GetProcessHeap(), 0, This->metadata_blocks);
798 This->metadata_blocks = new_metadata_blocks;
799 metadata_blocks_size = new_metadata_blocks_size;
802 This->metadata_blocks[This->metadata_count].ofs = chunk_start;
803 This->metadata_blocks[This->metadata_count].len.QuadPart = chunk_size + 12;
804 This->metadata_blocks[This->metadata_count].reader = NULL;
805 This->metadata_count++;
808 seek.QuadPart = chunk_start.QuadPart + chunk_size + 12; /* skip data and CRC */
809 } while (memcmp(chunk_type, "IEND", 4));
811 This->stream = pIStream;
812 IStream_AddRef(This->stream);
814 This->initialized = TRUE;
816 end:
817 LeaveCriticalSection(&This->lock);
819 return hr;
822 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
823 GUID *pguidContainerFormat)
825 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
826 return S_OK;
829 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
830 IWICBitmapDecoderInfo **ppIDecoderInfo)
832 HRESULT hr;
833 IWICComponentInfo *compinfo;
835 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
837 hr = CreateComponentInfo(&CLSID_WICPngDecoder, &compinfo);
838 if (FAILED(hr)) return hr;
840 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
841 (void**)ppIDecoderInfo);
843 IWICComponentInfo_Release(compinfo);
845 return hr;
848 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
849 IWICPalette *palette)
851 TRACE("(%p,%p)\n", iface, palette);
852 return WINCODEC_ERR_PALETTEUNAVAILABLE;
855 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
856 IWICMetadataQueryReader **ppIMetadataQueryReader)
858 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
859 return E_NOTIMPL;
862 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
863 IWICBitmapSource **ppIBitmapSource)
865 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
867 if (!ppIBitmapSource) return E_INVALIDARG;
869 *ppIBitmapSource = NULL;
870 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
873 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
874 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
876 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
877 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
880 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
881 IWICBitmapSource **ppIThumbnail)
883 TRACE("(%p,%p)\n", iface, ppIThumbnail);
885 if (!ppIThumbnail) return E_INVALIDARG;
887 *ppIThumbnail = NULL;
888 return WINCODEC_ERR_CODECNOTHUMBNAIL;
891 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
892 UINT *pCount)
894 if (!pCount) return E_INVALIDARG;
896 *pCount = 1;
897 return S_OK;
900 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
901 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
903 PngDecoder *This = impl_from_IWICBitmapDecoder(iface);
904 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
906 if (!This->initialized) return WINCODEC_ERR_FRAMEMISSING;
908 if (index != 0) return E_INVALIDARG;
910 IWICBitmapDecoder_AddRef(iface);
912 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
914 return S_OK;
917 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
918 PngDecoder_QueryInterface,
919 PngDecoder_AddRef,
920 PngDecoder_Release,
921 PngDecoder_QueryCapability,
922 PngDecoder_Initialize,
923 PngDecoder_GetContainerFormat,
924 PngDecoder_GetDecoderInfo,
925 PngDecoder_CopyPalette,
926 PngDecoder_GetMetadataQueryReader,
927 PngDecoder_GetPreview,
928 PngDecoder_GetColorContexts,
929 PngDecoder_GetThumbnail,
930 PngDecoder_GetFrameCount,
931 PngDecoder_GetFrame
934 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
935 void **ppv)
937 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
938 if (!ppv) return E_INVALIDARG;
940 if (IsEqualIID(&IID_IUnknown, iid) ||
941 IsEqualIID(&IID_IWICBitmapSource, iid) ||
942 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
944 *ppv = &This->IWICBitmapFrameDecode_iface;
946 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
948 *ppv = &This->IWICMetadataBlockReader_iface;
950 else
952 *ppv = NULL;
953 return E_NOINTERFACE;
956 IUnknown_AddRef((IUnknown*)*ppv);
957 return S_OK;
960 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
962 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
963 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
966 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
968 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
969 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
972 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
973 UINT *puiWidth, UINT *puiHeight)
975 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
976 *puiWidth = This->width;
977 *puiHeight = This->height;
978 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
979 return S_OK;
982 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
983 WICPixelFormatGUID *pPixelFormat)
985 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
986 TRACE("(%p,%p)\n", iface, pPixelFormat);
988 memcpy(pPixelFormat, This->format, sizeof(GUID));
990 return S_OK;
993 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
994 double *pDpiX, double *pDpiY)
996 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
997 png_uint_32 ret, xres, yres;
998 int unit_type;
1000 EnterCriticalSection(&This->lock);
1002 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
1004 if (ret && unit_type == PNG_RESOLUTION_METER)
1006 *pDpiX = xres * 0.0254;
1007 *pDpiY = yres * 0.0254;
1009 else
1011 WARN("no pHYs block present\n");
1012 *pDpiX = *pDpiY = 96.0;
1015 LeaveCriticalSection(&This->lock);
1017 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
1019 return S_OK;
1022 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
1023 IWICPalette *pIPalette)
1025 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
1026 png_uint_32 ret, color_type, bit_depth;
1027 png_colorp png_palette;
1028 int num_palette;
1029 WICColor palette[256];
1030 png_bytep trans_alpha;
1031 int num_trans;
1032 png_color_16p trans_values;
1033 int i;
1034 HRESULT hr=S_OK;
1036 TRACE("(%p,%p)\n", iface, pIPalette);
1038 EnterCriticalSection(&This->lock);
1040 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
1041 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
1043 if (color_type == PNG_COLOR_TYPE_PALETTE)
1045 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
1046 if (!ret)
1048 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
1049 goto end;
1052 if (num_palette > 256)
1054 ERR("palette has %i colors?!\n", num_palette);
1055 hr = E_FAIL;
1056 goto end;
1059 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans_alpha, &num_trans, &trans_values);
1060 if (!ret) num_trans = 0;
1062 for (i=0; i<num_palette; i++)
1064 BYTE alpha = (i < num_trans) ? trans_alpha[i] : 0xff;
1065 palette[i] = (alpha << 24 |
1066 png_palette[i].red << 16|
1067 png_palette[i].green << 8|
1068 png_palette[i].blue);
1071 else if (color_type == PNG_COLOR_TYPE_GRAY) {
1072 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans_alpha, &num_trans, &trans_values);
1074 if (!ret)
1076 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
1077 goto end;
1080 num_palette = 1 << bit_depth;
1082 for (i=0; i<num_palette; i++)
1084 BYTE alpha = (i == trans_values[0].gray) ? 0 : 0xff;
1085 BYTE val = i * 255 / (num_palette - 1);
1086 palette[i] = (alpha << 24 | val << 16 | val << 8 | val);
1089 else
1091 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
1094 end:
1096 LeaveCriticalSection(&This->lock);
1098 if (SUCCEEDED(hr))
1099 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
1101 return hr;
1104 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
1105 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
1107 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
1108 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
1110 return copy_pixels(This->bpp, This->image_bits,
1111 This->width, This->height, This->stride,
1112 prc, cbStride, cbBufferSize, pbBuffer);
1115 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1116 IWICMetadataQueryReader **ppIMetadataQueryReader)
1118 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
1120 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
1122 if (!ppIMetadataQueryReader)
1123 return E_INVALIDARG;
1125 return MetadataQueryReader_CreateInstance(&This->IWICMetadataBlockReader_iface, NULL, ppIMetadataQueryReader);
1128 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
1129 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1131 PngDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
1132 png_charp name;
1133 BYTE *profile;
1134 png_uint_32 len;
1135 int compression_type;
1136 HRESULT hr;
1138 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
1140 if (!pcActualCount) return E_INVALIDARG;
1142 EnterCriticalSection(&This->lock);
1144 if (ppng_get_iCCP(This->png_ptr, This->info_ptr, &name, &compression_type, (void *)&profile, &len))
1146 if (cCount && ppIColorContexts)
1148 hr = IWICColorContext_InitializeFromMemory(*ppIColorContexts, profile, len);
1149 if (FAILED(hr))
1151 LeaveCriticalSection(&This->lock);
1152 return hr;
1155 *pcActualCount = 1;
1157 else
1158 *pcActualCount = 0;
1160 LeaveCriticalSection(&This->lock);
1162 return S_OK;
1165 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
1166 IWICBitmapSource **ppIThumbnail)
1168 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1170 if (!ppIThumbnail) return E_INVALIDARG;
1172 *ppIThumbnail = NULL;
1173 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1176 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
1177 PngDecoder_Frame_QueryInterface,
1178 PngDecoder_Frame_AddRef,
1179 PngDecoder_Frame_Release,
1180 PngDecoder_Frame_GetSize,
1181 PngDecoder_Frame_GetPixelFormat,
1182 PngDecoder_Frame_GetResolution,
1183 PngDecoder_Frame_CopyPalette,
1184 PngDecoder_Frame_CopyPixels,
1185 PngDecoder_Frame_GetMetadataQueryReader,
1186 PngDecoder_Frame_GetColorContexts,
1187 PngDecoder_Frame_GetThumbnail
1190 static HRESULT WINAPI PngDecoder_Block_QueryInterface(IWICMetadataBlockReader *iface, REFIID iid,
1191 void **ppv)
1193 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1194 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
1197 static ULONG WINAPI PngDecoder_Block_AddRef(IWICMetadataBlockReader *iface)
1199 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1200 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
1203 static ULONG WINAPI PngDecoder_Block_Release(IWICMetadataBlockReader *iface)
1205 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1206 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1209 static HRESULT WINAPI PngDecoder_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1210 GUID *pguidContainerFormat)
1212 if (!pguidContainerFormat) return E_INVALIDARG;
1213 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
1214 return S_OK;
1217 static HRESULT WINAPI PngDecoder_Block_GetCount(IWICMetadataBlockReader *iface,
1218 UINT *pcCount)
1220 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1222 TRACE("%p,%p\n", iface, pcCount);
1224 if (!pcCount) return E_INVALIDARG;
1226 *pcCount = This->metadata_count;
1228 return S_OK;
1231 static HRESULT WINAPI PngDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1232 UINT nIndex, IWICMetadataReader **ppIMetadataReader)
1234 PngDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1235 HRESULT hr;
1236 IWICComponentFactory* factory;
1237 IWICStream* stream;
1239 TRACE("%p,%d,%p\n", iface, nIndex, ppIMetadataReader);
1241 if (nIndex >= This->metadata_count || !ppIMetadataReader)
1242 return E_INVALIDARG;
1244 if (!This->metadata_blocks[nIndex].reader)
1246 hr = StreamImpl_Create(&stream);
1248 if (SUCCEEDED(hr))
1250 hr = IWICStream_InitializeFromIStreamRegion(stream, This->stream,
1251 This->metadata_blocks[nIndex].ofs, This->metadata_blocks[nIndex].len);
1253 if (SUCCEEDED(hr))
1254 hr = ComponentFactory_CreateInstance(&IID_IWICComponentFactory, (void**)&factory);
1256 if (SUCCEEDED(hr))
1258 hr = IWICComponentFactory_CreateMetadataReaderFromContainer(factory,
1259 &GUID_ContainerFormatPng, NULL, WICMetadataCreationAllowUnknown,
1260 (IStream*)stream, &This->metadata_blocks[nIndex].reader);
1262 IWICComponentFactory_Release(factory);
1265 IWICStream_Release(stream);
1268 if (FAILED(hr))
1270 *ppIMetadataReader = NULL;
1271 return hr;
1275 *ppIMetadataReader = This->metadata_blocks[nIndex].reader;
1276 IWICMetadataReader_AddRef(*ppIMetadataReader);
1278 return S_OK;
1281 static HRESULT WINAPI PngDecoder_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1282 IEnumUnknown **ppIEnumMetadata)
1284 FIXME("%p,%p\n", iface, ppIEnumMetadata);
1285 return E_NOTIMPL;
1288 static const IWICMetadataBlockReaderVtbl PngDecoder_BlockVtbl = {
1289 PngDecoder_Block_QueryInterface,
1290 PngDecoder_Block_AddRef,
1291 PngDecoder_Block_Release,
1292 PngDecoder_Block_GetContainerFormat,
1293 PngDecoder_Block_GetCount,
1294 PngDecoder_Block_GetReaderByIndex,
1295 PngDecoder_Block_GetEnumerator,
1298 HRESULT PngDecoder_CreateInstance(REFIID iid, void** ppv)
1300 PngDecoder *This;
1301 HRESULT ret;
1303 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1305 *ppv = NULL;
1307 if (!load_libpng())
1309 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
1310 return E_FAIL;
1313 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
1314 if (!This) return E_OUTOFMEMORY;
1316 This->IWICBitmapDecoder_iface.lpVtbl = &PngDecoder_Vtbl;
1317 This->IWICBitmapFrameDecode_iface.lpVtbl = &PngDecoder_FrameVtbl;
1318 This->IWICMetadataBlockReader_iface.lpVtbl = &PngDecoder_BlockVtbl;
1319 This->ref = 1;
1320 This->png_ptr = NULL;
1321 This->info_ptr = NULL;
1322 This->end_info = NULL;
1323 This->stream = NULL;
1324 This->initialized = FALSE;
1325 This->image_bits = NULL;
1326 InitializeCriticalSection(&This->lock);
1327 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngDecoder.lock");
1328 This->metadata_count = 0;
1329 This->metadata_blocks = NULL;
1331 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1332 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1334 return ret;
1337 struct png_pixelformat {
1338 const WICPixelFormatGUID *guid;
1339 UINT bpp;
1340 int bit_depth;
1341 int color_type;
1342 BOOL remove_filler;
1343 BOOL swap_rgb;
1346 static const struct png_pixelformat formats[] = {
1347 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
1348 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
1349 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
1350 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
1351 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
1352 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
1353 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
1354 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
1355 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
1356 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
1357 {&GUID_WICPixelFormat1bppIndexed, 1, 1, PNG_COLOR_TYPE_PALETTE, 0, 0},
1358 {&GUID_WICPixelFormat2bppIndexed, 2, 2, PNG_COLOR_TYPE_PALETTE, 0, 0},
1359 {&GUID_WICPixelFormat4bppIndexed, 4, 4, PNG_COLOR_TYPE_PALETTE, 0, 0},
1360 {&GUID_WICPixelFormat8bppIndexed, 8, 8, PNG_COLOR_TYPE_PALETTE, 0, 0},
1361 {NULL},
1364 typedef struct PngEncoder {
1365 IWICBitmapEncoder IWICBitmapEncoder_iface;
1366 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1367 LONG ref;
1368 IStream *stream;
1369 png_structp png_ptr;
1370 png_infop info_ptr;
1371 UINT frame_count;
1372 BOOL frame_initialized;
1373 const struct png_pixelformat *format;
1374 BOOL info_written;
1375 UINT width, height;
1376 double xres, yres;
1377 UINT lines_written;
1378 BOOL frame_committed;
1379 BOOL committed;
1380 CRITICAL_SECTION lock;
1381 BOOL interlace;
1382 WICPngFilterOption filter;
1383 BYTE *data;
1384 UINT stride;
1385 UINT passes;
1386 WICColor palette[256];
1387 UINT colors;
1388 } PngEncoder;
1390 static inline PngEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1392 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapEncoder_iface);
1395 static inline PngEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1397 return CONTAINING_RECORD(iface, PngEncoder, IWICBitmapFrameEncode_iface);
1400 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1401 void **ppv)
1403 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1404 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1406 if (!ppv) return E_INVALIDARG;
1408 if (IsEqualIID(&IID_IUnknown, iid) ||
1409 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1411 *ppv = &This->IWICBitmapFrameEncode_iface;
1413 else
1415 *ppv = NULL;
1416 return E_NOINTERFACE;
1419 IUnknown_AddRef((IUnknown*)*ppv);
1420 return S_OK;
1423 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1425 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1426 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
1429 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
1431 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1432 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
1435 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1436 IPropertyBag2 *pIEncoderOptions)
1438 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1439 WICPngFilterOption filter;
1440 BOOL interlace;
1441 PROPBAG2 opts[2]= {{0}};
1442 VARIANT opt_values[2];
1443 HRESULT opt_hres[2];
1444 HRESULT hr;
1446 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1448 opts[0].pstrName = (LPOLESTR)wszPngInterlaceOption;
1449 opts[0].vt = VT_BOOL;
1450 opts[1].pstrName = (LPOLESTR)wszPngFilterOption;
1451 opts[1].vt = VT_UI1;
1453 if (pIEncoderOptions)
1455 hr = IPropertyBag2_Read(pIEncoderOptions, sizeof(opts)/sizeof(opts[0]), opts, NULL, opt_values, opt_hres);
1457 if (FAILED(hr))
1458 return hr;
1460 if (V_VT(&opt_values[0]) == VT_EMPTY)
1461 interlace = FALSE;
1462 else
1463 interlace = (V_BOOL(&opt_values[0]) != 0);
1465 filter = V_UI1(&opt_values[1]);
1466 if (filter > WICPngFilterAdaptive)
1468 WARN("Unrecognized filter option value %u.\n", filter);
1469 filter = WICPngFilterUnspecified;
1472 else
1474 interlace = FALSE;
1475 filter = WICPngFilterUnspecified;
1478 EnterCriticalSection(&This->lock);
1480 if (This->frame_initialized)
1482 LeaveCriticalSection(&This->lock);
1483 return WINCODEC_ERR_WRONGSTATE;
1486 This->interlace = interlace;
1487 This->filter = filter;
1489 This->frame_initialized = TRUE;
1491 LeaveCriticalSection(&This->lock);
1493 return S_OK;
1496 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1497 UINT uiWidth, UINT uiHeight)
1499 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1500 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1502 EnterCriticalSection(&This->lock);
1504 if (!This->frame_initialized || This->info_written)
1506 LeaveCriticalSection(&This->lock);
1507 return WINCODEC_ERR_WRONGSTATE;
1510 This->width = uiWidth;
1511 This->height = uiHeight;
1513 LeaveCriticalSection(&This->lock);
1515 return S_OK;
1518 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1519 double dpiX, double dpiY)
1521 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1522 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1524 EnterCriticalSection(&This->lock);
1526 if (!This->frame_initialized || This->info_written)
1528 LeaveCriticalSection(&This->lock);
1529 return WINCODEC_ERR_WRONGSTATE;
1532 This->xres = dpiX;
1533 This->yres = dpiY;
1535 LeaveCriticalSection(&This->lock);
1537 return S_OK;
1540 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1541 WICPixelFormatGUID *pPixelFormat)
1543 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1544 int i;
1545 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1547 EnterCriticalSection(&This->lock);
1549 if (!This->frame_initialized || This->info_written)
1551 LeaveCriticalSection(&This->lock);
1552 return WINCODEC_ERR_WRONGSTATE;
1555 for (i=0; formats[i].guid; i++)
1557 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1558 break;
1561 if (!formats[i].guid) i = 0;
1563 This->format = &formats[i];
1564 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1566 LeaveCriticalSection(&This->lock);
1568 return S_OK;
1571 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1572 UINT cCount, IWICColorContext **ppIColorContext)
1574 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1575 return E_NOTIMPL;
1578 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1579 IWICPalette *palette)
1581 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1582 HRESULT hr;
1584 TRACE("(%p,%p)\n", iface, palette);
1586 if (!palette) return E_INVALIDARG;
1588 EnterCriticalSection(&This->lock);
1590 if (This->frame_initialized)
1591 hr = IWICPalette_GetColors(palette, 256, This->palette, &This->colors);
1592 else
1593 hr = WINCODEC_ERR_NOTINITIALIZED;
1595 LeaveCriticalSection(&This->lock);
1596 return hr;
1599 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1600 IWICBitmapSource *pIThumbnail)
1602 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1603 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1606 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1607 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1609 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1610 png_byte **row_pointers=NULL;
1611 UINT i;
1612 jmp_buf jmpbuf;
1613 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1615 EnterCriticalSection(&This->lock);
1617 if (!This->frame_initialized || !This->width || !This->height || !This->format)
1619 LeaveCriticalSection(&This->lock);
1620 return WINCODEC_ERR_WRONGSTATE;
1623 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1625 LeaveCriticalSection(&This->lock);
1626 return E_INVALIDARG;
1629 /* set up setjmp/longjmp error handling */
1630 if (setjmp(jmpbuf))
1632 LeaveCriticalSection(&This->lock);
1633 HeapFree(GetProcessHeap(), 0, row_pointers);
1634 return E_FAIL;
1636 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1638 if (!This->info_written)
1640 if (This->interlace)
1642 /* libpng requires us to write all data multiple times in this case. */
1643 This->stride = (This->format->bpp * This->width + 7)/8;
1644 This->data = HeapAlloc(GetProcessHeap(), 0, This->height * This->stride);
1645 if (!This->data)
1647 LeaveCriticalSection(&This->lock);
1648 return E_OUTOFMEMORY;
1652 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
1653 This->format->bit_depth, This->format->color_type,
1654 This->interlace ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE,
1655 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1657 if (This->xres != 0.0 && This->yres != 0.0)
1659 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
1660 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
1663 if (This->format->color_type == PNG_COLOR_TYPE_PALETTE && This->colors)
1665 png_color png_palette[256];
1666 png_byte trans[256];
1667 UINT i, num_trans = 0, colors;
1669 /* Newer libpng versions don't accept larger palettes than the declared
1670 * bit depth, so we need to generate the palette of the correct length.
1672 colors = min(This->colors, 1 << This->format->bit_depth);
1674 for (i = 0; i < colors; i++)
1676 png_palette[i].red = (This->palette[i] >> 16) & 0xff;
1677 png_palette[i].green = (This->palette[i] >> 8) & 0xff;
1678 png_palette[i].blue = This->palette[i] & 0xff;
1679 trans[i] = (This->palette[i] >> 24) & 0xff;
1680 if (trans[i] != 0xff)
1681 num_trans = i+1;
1684 ppng_set_PLTE(This->png_ptr, This->info_ptr, png_palette, colors);
1686 if (num_trans)
1687 ppng_set_tRNS(This->png_ptr, This->info_ptr, trans, num_trans, NULL);
1690 ppng_write_info(This->png_ptr, This->info_ptr);
1692 if (This->format->remove_filler)
1693 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1695 if (This->format->swap_rgb)
1696 ppng_set_bgr(This->png_ptr);
1698 if (This->interlace)
1699 This->passes = ppng_set_interlace_handling(This->png_ptr);
1701 if (This->filter != WICPngFilterUnspecified)
1703 static const int png_filter_map[] =
1705 /* WICPngFilterUnspecified */ PNG_NO_FILTERS,
1706 /* WICPngFilterNone */ PNG_FILTER_NONE,
1707 /* WICPngFilterSub */ PNG_FILTER_SUB,
1708 /* WICPngFilterUp */ PNG_FILTER_UP,
1709 /* WICPngFilterAverage */ PNG_FILTER_AVG,
1710 /* WICPngFilterPaeth */ PNG_FILTER_PAETH,
1711 /* WICPngFilterAdaptive */ PNG_ALL_FILTERS,
1714 ppng_set_filter(This->png_ptr, 0, png_filter_map[This->filter]);
1717 This->info_written = TRUE;
1720 if (This->interlace)
1722 /* Just store the data so we can write it in multiple passes in Commit. */
1723 for (i=0; i<lineCount; i++)
1724 memcpy(This->data + This->stride * (This->lines_written + i),
1725 pbPixels + cbStride * i,
1726 This->stride);
1728 This->lines_written += lineCount;
1730 LeaveCriticalSection(&This->lock);
1731 return S_OK;
1734 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1735 if (!row_pointers)
1737 LeaveCriticalSection(&This->lock);
1738 return E_OUTOFMEMORY;
1741 for (i=0; i<lineCount; i++)
1742 row_pointers[i] = pbPixels + cbStride * i;
1744 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1745 This->lines_written += lineCount;
1747 LeaveCriticalSection(&This->lock);
1749 HeapFree(GetProcessHeap(), 0, row_pointers);
1751 return S_OK;
1754 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1755 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1757 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1758 HRESULT hr;
1759 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1761 if (!This->frame_initialized)
1762 return WINCODEC_ERR_WRONGSTATE;
1764 hr = configure_write_source(iface, pIBitmapSource, prc,
1765 This->format ? This->format->guid : NULL, This->width, This->height,
1766 This->xres, This->yres);
1768 if (SUCCEEDED(hr))
1770 hr = write_source(iface, pIBitmapSource, prc,
1771 This->format->guid, This->format->bpp, This->width, This->height);
1774 return hr;
1777 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1779 PngEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1780 png_byte **row_pointers=NULL;
1781 jmp_buf jmpbuf;
1782 TRACE("(%p)\n", iface);
1784 EnterCriticalSection(&This->lock);
1786 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1788 LeaveCriticalSection(&This->lock);
1789 return WINCODEC_ERR_WRONGSTATE;
1792 /* set up setjmp/longjmp error handling */
1793 if (setjmp(jmpbuf))
1795 LeaveCriticalSection(&This->lock);
1796 HeapFree(GetProcessHeap(), 0, row_pointers);
1797 return E_FAIL;
1799 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1801 if (This->interlace)
1803 int i;
1805 row_pointers = HeapAlloc(GetProcessHeap(), 0, This->height * sizeof(png_byte*));
1806 if (!row_pointers)
1808 LeaveCriticalSection(&This->lock);
1809 return E_OUTOFMEMORY;
1812 for (i=0; i<This->height; i++)
1813 row_pointers[i] = This->data + This->stride * i;
1815 for (i=0; i<This->passes; i++)
1816 ppng_write_rows(This->png_ptr, row_pointers, This->height);
1819 ppng_write_end(This->png_ptr, This->info_ptr);
1821 This->frame_committed = TRUE;
1823 HeapFree(GetProcessHeap(), 0, row_pointers);
1825 LeaveCriticalSection(&This->lock);
1827 return S_OK;
1830 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1831 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1833 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1834 return E_NOTIMPL;
1837 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1838 PngFrameEncode_QueryInterface,
1839 PngFrameEncode_AddRef,
1840 PngFrameEncode_Release,
1841 PngFrameEncode_Initialize,
1842 PngFrameEncode_SetSize,
1843 PngFrameEncode_SetResolution,
1844 PngFrameEncode_SetPixelFormat,
1845 PngFrameEncode_SetColorContexts,
1846 PngFrameEncode_SetPalette,
1847 PngFrameEncode_SetThumbnail,
1848 PngFrameEncode_WritePixels,
1849 PngFrameEncode_WriteSource,
1850 PngFrameEncode_Commit,
1851 PngFrameEncode_GetMetadataQueryWriter
1854 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1855 void **ppv)
1857 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1858 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1860 if (!ppv) return E_INVALIDARG;
1862 if (IsEqualIID(&IID_IUnknown, iid) ||
1863 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1865 *ppv = &This->IWICBitmapEncoder_iface;
1867 else
1869 *ppv = NULL;
1870 return E_NOINTERFACE;
1873 IUnknown_AddRef((IUnknown*)*ppv);
1874 return S_OK;
1877 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1879 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1880 ULONG ref = InterlockedIncrement(&This->ref);
1882 TRACE("(%p) refcount=%u\n", iface, ref);
1884 return ref;
1887 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1889 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1890 ULONG ref = InterlockedDecrement(&This->ref);
1892 TRACE("(%p) refcount=%u\n", iface, ref);
1894 if (ref == 0)
1896 This->lock.DebugInfo->Spare[0] = 0;
1897 DeleteCriticalSection(&This->lock);
1898 if (This->png_ptr)
1899 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1900 if (This->stream)
1901 IStream_Release(This->stream);
1902 HeapFree(GetProcessHeap(), 0, This->data);
1903 HeapFree(GetProcessHeap(), 0, This);
1906 return ref;
1909 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1911 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1912 HRESULT hr;
1913 ULONG byteswritten;
1915 hr = IStream_Write(This->stream, data, length, &byteswritten);
1916 if (FAILED(hr) || byteswritten != length)
1918 ppng_error(png_ptr, "failed writing data");
1922 static void user_flush(png_structp png_ptr)
1926 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1927 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1929 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
1930 jmp_buf jmpbuf;
1932 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1934 EnterCriticalSection(&This->lock);
1936 if (This->png_ptr)
1938 LeaveCriticalSection(&This->lock);
1939 return WINCODEC_ERR_WRONGSTATE;
1942 /* initialize libpng */
1943 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1944 if (!This->png_ptr)
1946 LeaveCriticalSection(&This->lock);
1947 return E_FAIL;
1950 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1951 if (!This->info_ptr)
1953 ppng_destroy_write_struct(&This->png_ptr, NULL);
1954 This->png_ptr = NULL;
1955 LeaveCriticalSection(&This->lock);
1956 return E_FAIL;
1959 IStream_AddRef(pIStream);
1960 This->stream = pIStream;
1962 /* set up setjmp/longjmp error handling */
1963 if (setjmp(jmpbuf))
1965 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1966 This->png_ptr = NULL;
1967 IStream_Release(This->stream);
1968 This->stream = NULL;
1969 LeaveCriticalSection(&This->lock);
1970 return E_FAIL;
1972 ppng_set_error_fn(This->png_ptr, jmpbuf, user_error_fn, user_warning_fn);
1974 /* set up custom i/o handling */
1975 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1977 LeaveCriticalSection(&This->lock);
1979 return S_OK;
1982 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1983 GUID *pguidContainerFormat)
1985 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1986 return E_NOTIMPL;
1989 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1990 IWICBitmapEncoderInfo **ppIEncoderInfo)
1992 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1993 return E_NOTIMPL;
1996 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1997 UINT cCount, IWICColorContext **ppIColorContext)
1999 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
2000 return E_NOTIMPL;
2003 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *palette)
2005 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
2006 HRESULT hr;
2008 TRACE("(%p,%p)\n", iface, palette);
2010 EnterCriticalSection(&This->lock);
2012 hr = This->stream ? WINCODEC_ERR_UNSUPPORTEDOPERATION : WINCODEC_ERR_NOTINITIALIZED;
2014 LeaveCriticalSection(&This->lock);
2016 return hr;
2019 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
2021 TRACE("(%p,%p)\n", iface, pIThumbnail);
2022 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
2025 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
2027 TRACE("(%p,%p)\n", iface, pIPreview);
2028 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
2031 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
2032 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
2034 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
2035 HRESULT hr;
2036 static const PROPBAG2 opts[2] =
2038 { PROPBAG2_TYPE_DATA, VT_BOOL, 0, 0, (LPOLESTR)wszPngInterlaceOption },
2039 { PROPBAG2_TYPE_DATA, VT_UI1, 0, 0, (LPOLESTR)wszPngFilterOption },
2042 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
2044 EnterCriticalSection(&This->lock);
2046 if (This->frame_count != 0)
2048 LeaveCriticalSection(&This->lock);
2049 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
2052 if (!This->stream)
2054 LeaveCriticalSection(&This->lock);
2055 return WINCODEC_ERR_NOTINITIALIZED;
2058 if (ppIEncoderOptions)
2060 hr = CreatePropertyBag2(opts, sizeof(opts)/sizeof(opts[0]), ppIEncoderOptions);
2061 if (FAILED(hr))
2063 LeaveCriticalSection(&This->lock);
2064 return hr;
2068 This->frame_count = 1;
2070 LeaveCriticalSection(&This->lock);
2072 IWICBitmapEncoder_AddRef(iface);
2073 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
2075 return S_OK;
2078 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
2080 PngEncoder *This = impl_from_IWICBitmapEncoder(iface);
2081 TRACE("(%p)\n", iface);
2083 EnterCriticalSection(&This->lock);
2085 if (!This->frame_committed || This->committed)
2087 LeaveCriticalSection(&This->lock);
2088 return WINCODEC_ERR_WRONGSTATE;
2091 This->committed = TRUE;
2093 LeaveCriticalSection(&This->lock);
2095 return S_OK;
2098 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
2099 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
2101 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
2102 return E_NOTIMPL;
2105 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
2106 PngEncoder_QueryInterface,
2107 PngEncoder_AddRef,
2108 PngEncoder_Release,
2109 PngEncoder_Initialize,
2110 PngEncoder_GetContainerFormat,
2111 PngEncoder_GetEncoderInfo,
2112 PngEncoder_SetColorContexts,
2113 PngEncoder_SetPalette,
2114 PngEncoder_SetThumbnail,
2115 PngEncoder_SetPreview,
2116 PngEncoder_CreateNewFrame,
2117 PngEncoder_Commit,
2118 PngEncoder_GetMetadataQueryWriter
2121 HRESULT PngEncoder_CreateInstance(REFIID iid, void** ppv)
2123 PngEncoder *This;
2124 HRESULT ret;
2126 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
2128 *ppv = NULL;
2130 if (!load_libpng())
2132 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
2133 return E_FAIL;
2136 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
2137 if (!This) return E_OUTOFMEMORY;
2139 This->IWICBitmapEncoder_iface.lpVtbl = &PngEncoder_Vtbl;
2140 This->IWICBitmapFrameEncode_iface.lpVtbl = &PngEncoder_FrameVtbl;
2141 This->ref = 1;
2142 This->png_ptr = NULL;
2143 This->info_ptr = NULL;
2144 This->stream = NULL;
2145 This->frame_count = 0;
2146 This->frame_initialized = FALSE;
2147 This->format = NULL;
2148 This->info_written = FALSE;
2149 This->width = 0;
2150 This->height = 0;
2151 This->xres = 0.0;
2152 This->yres = 0.0;
2153 This->lines_written = 0;
2154 This->frame_committed = FALSE;
2155 This->committed = FALSE;
2156 This->data = NULL;
2157 This->colors = 0;
2158 InitializeCriticalSection(&This->lock);
2159 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngEncoder.lock");
2161 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
2162 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
2164 return ret;
2167 #else /* !HAVE_PNG_H */
2169 HRESULT PngDecoder_CreateInstance(REFIID iid, void** ppv)
2171 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
2172 return E_FAIL;
2175 HRESULT PngEncoder_CreateInstance(REFIID iid, void** ppv)
2177 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");
2178 return E_FAIL;
2181 #endif