kernel32: Update Italian translation.
[wine/testsucceed.git] / dlls / windowscodecs / pngformat.c
bloba3c7730a2677407f86a36b072cd1d2eadf03481d
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 COBJMACROS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "objbase.h"
33 #include "wincodec.h"
35 #include "wincodecs_private.h"
37 #include "wine/debug.h"
38 #include "wine/library.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
42 #ifdef SONAME_LIBPNG
44 static void *libpng_handle;
45 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
46 MAKE_FUNCPTR(png_create_read_struct);
47 MAKE_FUNCPTR(png_create_info_struct);
48 MAKE_FUNCPTR(png_create_write_struct);
49 MAKE_FUNCPTR(png_destroy_read_struct);
50 MAKE_FUNCPTR(png_destroy_write_struct);
51 MAKE_FUNCPTR(png_error);
52 MAKE_FUNCPTR(png_get_bit_depth);
53 MAKE_FUNCPTR(png_get_color_type);
54 MAKE_FUNCPTR(png_get_error_ptr);
55 MAKE_FUNCPTR(png_get_image_height);
56 MAKE_FUNCPTR(png_get_image_width);
57 MAKE_FUNCPTR(png_get_io_ptr);
58 MAKE_FUNCPTR(png_get_pHYs);
59 MAKE_FUNCPTR(png_get_PLTE);
60 MAKE_FUNCPTR(png_get_tRNS);
61 MAKE_FUNCPTR(png_set_bgr);
62 MAKE_FUNCPTR(png_set_error_fn);
63 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
64 MAKE_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
65 #else
66 MAKE_FUNCPTR(png_set_gray_1_2_4_to_8);
67 #endif
68 MAKE_FUNCPTR(png_set_filler);
69 MAKE_FUNCPTR(png_set_gray_to_rgb);
70 MAKE_FUNCPTR(png_set_IHDR);
71 MAKE_FUNCPTR(png_set_pHYs);
72 MAKE_FUNCPTR(png_set_read_fn);
73 MAKE_FUNCPTR(png_set_strip_16);
74 MAKE_FUNCPTR(png_set_tRNS_to_alpha);
75 MAKE_FUNCPTR(png_set_write_fn);
76 MAKE_FUNCPTR(png_read_end);
77 MAKE_FUNCPTR(png_read_image);
78 MAKE_FUNCPTR(png_read_info);
79 MAKE_FUNCPTR(png_write_end);
80 MAKE_FUNCPTR(png_write_info);
81 MAKE_FUNCPTR(png_write_rows);
82 #undef MAKE_FUNCPTR
84 static void *load_libpng(void)
86 if((libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
88 #define LOAD_FUNCPTR(f) \
89 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
90 libpng_handle = NULL; \
91 return NULL; \
93 LOAD_FUNCPTR(png_create_read_struct);
94 LOAD_FUNCPTR(png_create_info_struct);
95 LOAD_FUNCPTR(png_create_write_struct);
96 LOAD_FUNCPTR(png_destroy_read_struct);
97 LOAD_FUNCPTR(png_destroy_write_struct);
98 LOAD_FUNCPTR(png_error);
99 LOAD_FUNCPTR(png_get_bit_depth);
100 LOAD_FUNCPTR(png_get_color_type);
101 LOAD_FUNCPTR(png_get_error_ptr);
102 LOAD_FUNCPTR(png_get_image_height);
103 LOAD_FUNCPTR(png_get_image_width);
104 LOAD_FUNCPTR(png_get_io_ptr);
105 LOAD_FUNCPTR(png_get_pHYs);
106 LOAD_FUNCPTR(png_get_PLTE);
107 LOAD_FUNCPTR(png_get_tRNS);
108 LOAD_FUNCPTR(png_set_bgr);
109 LOAD_FUNCPTR(png_set_error_fn);
110 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
111 LOAD_FUNCPTR(png_set_expand_gray_1_2_4_to_8);
112 #else
113 LOAD_FUNCPTR(png_set_gray_1_2_4_to_8);
114 #endif
115 LOAD_FUNCPTR(png_set_filler);
116 LOAD_FUNCPTR(png_set_gray_to_rgb);
117 LOAD_FUNCPTR(png_set_IHDR);
118 LOAD_FUNCPTR(png_set_pHYs);
119 LOAD_FUNCPTR(png_set_read_fn);
120 LOAD_FUNCPTR(png_set_strip_16);
121 LOAD_FUNCPTR(png_set_tRNS_to_alpha);
122 LOAD_FUNCPTR(png_set_write_fn);
123 LOAD_FUNCPTR(png_read_end);
124 LOAD_FUNCPTR(png_read_image);
125 LOAD_FUNCPTR(png_read_info);
126 LOAD_FUNCPTR(png_write_end);
127 LOAD_FUNCPTR(png_write_info);
128 LOAD_FUNCPTR(png_write_rows);
130 #undef LOAD_FUNCPTR
132 return libpng_handle;
135 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
137 jmp_buf *pjmpbuf;
139 /* This uses setjmp/longjmp just like the default. We can't use the
140 * default because there's no way to access the jmp buffer in the png_struct
141 * that works in 1.2 and 1.4 and allows us to dynamically load libpng. */
142 WARN("PNG error: %s\n", debugstr_a(error_message));
143 pjmpbuf = ppng_get_error_ptr(png_ptr);
144 longjmp(*pjmpbuf, 1);
147 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
149 WARN("PNG warning: %s\n", debugstr_a(warning_message));
152 typedef struct {
153 const IWICBitmapDecoderVtbl *lpVtbl;
154 const IWICBitmapFrameDecodeVtbl *lpFrameVtbl;
155 LONG ref;
156 png_structp png_ptr;
157 png_infop info_ptr;
158 png_infop end_info;
159 BOOL initialized;
160 int bpp;
161 int width, height;
162 UINT stride;
163 const WICPixelFormatGUID *format;
164 BYTE *image_bits;
165 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
166 } PngDecoder;
168 static inline PngDecoder *impl_from_frame(IWICBitmapFrameDecode *iface)
170 return CONTAINING_RECORD(iface, PngDecoder, lpFrameVtbl);
173 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
175 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
176 void **ppv)
178 PngDecoder *This = (PngDecoder*)iface;
179 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
181 if (!ppv) return E_INVALIDARG;
183 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
185 *ppv = This;
187 else
189 *ppv = NULL;
190 return E_NOINTERFACE;
193 IUnknown_AddRef((IUnknown*)*ppv);
194 return S_OK;
197 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
199 PngDecoder *This = (PngDecoder*)iface;
200 ULONG ref = InterlockedIncrement(&This->ref);
202 TRACE("(%p) refcount=%u\n", iface, ref);
204 return ref;
207 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
209 PngDecoder *This = (PngDecoder*)iface;
210 ULONG ref = InterlockedDecrement(&This->ref);
212 TRACE("(%p) refcount=%u\n", iface, ref);
214 if (ref == 0)
216 if (This->png_ptr)
217 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
218 This->lock.DebugInfo->Spare[0] = 0;
219 DeleteCriticalSection(&This->lock);
220 HeapFree(GetProcessHeap(), 0, This->image_bits);
221 HeapFree(GetProcessHeap(), 0, This);
224 return ref;
227 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
228 DWORD *pdwCapability)
230 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
231 return E_NOTIMPL;
234 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
236 IStream *stream = ppng_get_io_ptr(png_ptr);
237 HRESULT hr;
238 ULONG bytesread;
240 hr = IStream_Read(stream, data, length, &bytesread);
241 if (FAILED(hr) || bytesread != length)
243 ppng_error(png_ptr, "failed reading data");
247 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
248 WICDecodeOptions cacheOptions)
250 PngDecoder *This = (PngDecoder*)iface;
251 LARGE_INTEGER seek;
252 HRESULT hr=S_OK;
253 png_bytep *row_pointers=NULL;
254 UINT image_size;
255 UINT i;
256 int color_type, bit_depth;
257 png_bytep trans;
258 int num_trans;
259 png_uint_32 transparency;
260 png_color_16p trans_values;
261 jmp_buf jmpbuf;
263 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
265 EnterCriticalSection(&This->lock);
267 /* initialize libpng */
268 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
269 if (!This->png_ptr)
271 hr = E_FAIL;
272 goto end;
275 This->info_ptr = ppng_create_info_struct(This->png_ptr);
276 if (!This->info_ptr)
278 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
279 This->png_ptr = NULL;
280 hr = E_FAIL;
281 goto end;
284 This->end_info = ppng_create_info_struct(This->png_ptr);
285 if (!This->info_ptr)
287 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
288 This->png_ptr = NULL;
289 hr = E_FAIL;
290 goto end;
293 /* set up setjmp/longjmp error handling */
294 if (setjmp(jmpbuf))
296 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
297 HeapFree(GetProcessHeap(), 0, row_pointers);
298 This->png_ptr = NULL;
299 hr = E_FAIL;
300 goto end;
302 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
304 /* seek to the start of the stream */
305 seek.QuadPart = 0;
306 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
307 if (FAILED(hr)) goto end;
309 /* set up custom i/o handling */
310 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
312 /* read the header */
313 ppng_read_info(This->png_ptr, This->info_ptr);
315 /* choose a pixel format */
316 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
317 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
319 /* check for color-keyed alpha */
320 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
322 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
324 /* expand to RGBA */
325 if (color_type == PNG_COLOR_TYPE_GRAY)
327 if (bit_depth < 8)
329 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
330 ppng_set_expand_gray_1_2_4_to_8(This->png_ptr);
331 #else
332 ppng_set_gray_1_2_4_to_8(This->png_ptr);
333 #endif
334 bit_depth = 8;
336 ppng_set_gray_to_rgb(This->png_ptr);
338 ppng_set_tRNS_to_alpha(This->png_ptr);
339 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
342 switch (color_type)
344 case PNG_COLOR_TYPE_GRAY:
345 This->bpp = bit_depth;
346 switch (bit_depth)
348 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
349 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
350 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
351 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
352 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
353 default:
354 ERR("invalid grayscale bit depth: %i\n", bit_depth);
355 hr = E_FAIL;
356 goto end;
358 break;
359 case PNG_COLOR_TYPE_GRAY_ALPHA:
360 /* WIC does not support grayscale alpha formats so use RGBA */
361 ppng_set_gray_to_rgb(This->png_ptr);
362 case PNG_COLOR_TYPE_RGB_ALPHA:
363 This->bpp = bit_depth * 4;
364 switch (bit_depth)
366 case 8:
367 ppng_set_bgr(This->png_ptr);
368 This->format = &GUID_WICPixelFormat32bppBGRA;
369 break;
370 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
371 default:
372 ERR("invalid RGBA bit depth: %i\n", bit_depth);
373 hr = E_FAIL;
374 goto end;
376 break;
377 case PNG_COLOR_TYPE_PALETTE:
378 This->bpp = bit_depth;
379 switch (bit_depth)
381 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
382 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
383 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
384 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
385 default:
386 ERR("invalid indexed color bit depth: %i\n", bit_depth);
387 hr = E_FAIL;
388 goto end;
390 break;
391 case PNG_COLOR_TYPE_RGB:
392 This->bpp = bit_depth * 3;
393 switch (bit_depth)
395 case 8:
396 ppng_set_bgr(This->png_ptr);
397 This->format = &GUID_WICPixelFormat24bppBGR;
398 break;
399 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
400 default:
401 ERR("invalid RGB color bit depth: %i\n", bit_depth);
402 hr = E_FAIL;
403 goto end;
405 break;
406 default:
407 ERR("invalid color type %i\n", color_type);
408 hr = E_FAIL;
409 goto end;
412 /* read the image data */
413 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
414 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
415 This->stride = This->width * This->bpp;
416 image_size = This->stride * This->height;
418 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
419 if (!This->image_bits)
421 hr = E_OUTOFMEMORY;
422 goto end;
425 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
426 if (!row_pointers)
428 hr = E_OUTOFMEMORY;
429 goto end;
432 for (i=0; i<This->height; i++)
433 row_pointers[i] = This->image_bits + i * This->stride;
435 ppng_read_image(This->png_ptr, row_pointers);
437 HeapFree(GetProcessHeap(), 0, row_pointers);
438 row_pointers = NULL;
440 ppng_read_end(This->png_ptr, This->end_info);
442 This->initialized = TRUE;
444 end:
446 LeaveCriticalSection(&This->lock);
448 return hr;
451 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
452 GUID *pguidContainerFormat)
454 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
455 return S_OK;
458 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
459 IWICBitmapDecoderInfo **ppIDecoderInfo)
461 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
462 return E_NOTIMPL;
465 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
466 IWICPalette *pIPalette)
468 FIXME("(%p,%p): stub\n", iface, pIPalette);
469 return E_NOTIMPL;
472 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
473 IWICMetadataQueryReader **ppIMetadataQueryReader)
475 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
476 return E_NOTIMPL;
479 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
480 IWICBitmapSource **ppIBitmapSource)
482 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
483 return E_NOTIMPL;
486 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
487 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
489 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
490 return E_NOTIMPL;
493 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
494 IWICBitmapSource **ppIThumbnail)
496 TRACE("(%p,%p)\n", iface, ppIThumbnail);
497 return WINCODEC_ERR_CODECNOTHUMBNAIL;
500 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
501 UINT *pCount)
503 *pCount = 1;
504 return S_OK;
507 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
508 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
510 PngDecoder *This = (PngDecoder*)iface;
511 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
513 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
515 if (index != 0) return E_INVALIDARG;
517 IWICBitmapDecoder_AddRef(iface);
519 *ppIBitmapFrame = (void*)(&This->lpFrameVtbl);
521 return S_OK;
524 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
525 PngDecoder_QueryInterface,
526 PngDecoder_AddRef,
527 PngDecoder_Release,
528 PngDecoder_QueryCapability,
529 PngDecoder_Initialize,
530 PngDecoder_GetContainerFormat,
531 PngDecoder_GetDecoderInfo,
532 PngDecoder_CopyPalette,
533 PngDecoder_GetMetadataQueryReader,
534 PngDecoder_GetPreview,
535 PngDecoder_GetColorContexts,
536 PngDecoder_GetThumbnail,
537 PngDecoder_GetFrameCount,
538 PngDecoder_GetFrame
541 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
542 void **ppv)
544 if (!ppv) return E_INVALIDARG;
546 if (IsEqualIID(&IID_IUnknown, iid) ||
547 IsEqualIID(&IID_IWICBitmapSource, iid) ||
548 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
550 *ppv = iface;
552 else
554 *ppv = NULL;
555 return E_NOINTERFACE;
558 IUnknown_AddRef((IUnknown*)*ppv);
559 return S_OK;
562 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
564 PngDecoder *This = impl_from_frame(iface);
565 return IUnknown_AddRef((IUnknown*)This);
568 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
570 PngDecoder *This = impl_from_frame(iface);
571 return IUnknown_Release((IUnknown*)This);
574 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
575 UINT *puiWidth, UINT *puiHeight)
577 PngDecoder *This = impl_from_frame(iface);
578 *puiWidth = This->width;
579 *puiHeight = This->height;
580 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
581 return S_OK;
584 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
585 WICPixelFormatGUID *pPixelFormat)
587 PngDecoder *This = impl_from_frame(iface);
588 TRACE("(%p,%p)\n", iface, pPixelFormat);
590 memcpy(pPixelFormat, This->format, sizeof(GUID));
592 return S_OK;
595 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
596 double *pDpiX, double *pDpiY)
598 PngDecoder *This = impl_from_frame(iface);
599 png_uint_32 ret, xres, yres;
600 int unit_type;
602 EnterCriticalSection(&This->lock);
604 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
606 if (ret && unit_type == PNG_RESOLUTION_METER)
608 *pDpiX = xres * 0.0254;
609 *pDpiY = yres * 0.0254;
611 else
613 WARN("no pHYs block present\n");
614 *pDpiX = *pDpiY = 96.0;
617 LeaveCriticalSection(&This->lock);
619 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
621 return S_OK;
624 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
625 IWICPalette *pIPalette)
627 PngDecoder *This = impl_from_frame(iface);
628 png_uint_32 ret;
629 png_colorp png_palette;
630 int num_palette;
631 WICColor palette[256];
632 png_bytep trans;
633 int num_trans;
634 png_color_16p trans_values;
635 int i;
636 HRESULT hr=S_OK;
638 TRACE("(%p,%p)\n", iface, pIPalette);
640 EnterCriticalSection(&This->lock);
642 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
643 if (!ret)
645 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
646 goto end;
649 if (num_palette > 256)
651 ERR("palette has %i colors?!\n", num_palette);
652 hr = E_FAIL;
653 goto end;
656 for (i=0; i<num_palette; i++)
658 palette[i] = (0xff000000|
659 png_palette[i].red << 16|
660 png_palette[i].green << 8|
661 png_palette[i].blue);
664 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
665 if (ret)
667 for (i=0; i<num_trans; i++)
669 palette[trans[i]] = 0x00000000;
673 end:
675 LeaveCriticalSection(&This->lock);
677 if (SUCCEEDED(hr))
678 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
680 return hr;
683 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
684 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
686 PngDecoder *This = impl_from_frame(iface);
687 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
689 return copy_pixels(This->bpp, This->image_bits,
690 This->width, This->height, This->stride,
691 prc, cbStride, cbBufferSize, pbBuffer);
694 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
695 IWICMetadataQueryReader **ppIMetadataQueryReader)
697 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
698 return E_NOTIMPL;
701 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
702 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
704 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
705 return E_NOTIMPL;
708 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
709 IWICBitmapSource **ppIThumbnail)
711 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
712 return E_NOTIMPL;
715 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
716 PngDecoder_Frame_QueryInterface,
717 PngDecoder_Frame_AddRef,
718 PngDecoder_Frame_Release,
719 PngDecoder_Frame_GetSize,
720 PngDecoder_Frame_GetPixelFormat,
721 PngDecoder_Frame_GetResolution,
722 PngDecoder_Frame_CopyPalette,
723 PngDecoder_Frame_CopyPixels,
724 PngDecoder_Frame_GetMetadataQueryReader,
725 PngDecoder_Frame_GetColorContexts,
726 PngDecoder_Frame_GetThumbnail
729 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
731 PngDecoder *This;
732 HRESULT ret;
734 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
736 *ppv = NULL;
738 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
740 if (!libpng_handle && !load_libpng())
742 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
743 return E_FAIL;
746 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
747 if (!This) return E_OUTOFMEMORY;
749 This->lpVtbl = &PngDecoder_Vtbl;
750 This->lpFrameVtbl = &PngDecoder_FrameVtbl;
751 This->ref = 1;
752 This->png_ptr = NULL;
753 This->info_ptr = NULL;
754 This->end_info = NULL;
755 This->initialized = FALSE;
756 This->image_bits = NULL;
757 InitializeCriticalSection(&This->lock);
758 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngDecoder.lock");
760 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
761 IUnknown_Release((IUnknown*)This);
763 return ret;
766 struct png_pixelformat {
767 const WICPixelFormatGUID *guid;
768 UINT bpp;
769 int bit_depth;
770 int color_type;
771 BOOL remove_filler;
772 BOOL swap_rgb;
775 static const struct png_pixelformat formats[] = {
776 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
777 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
778 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
779 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
780 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
781 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
782 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
783 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
784 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
785 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
786 {NULL},
789 typedef struct PngEncoder {
790 const IWICBitmapEncoderVtbl *lpVtbl;
791 const IWICBitmapFrameEncodeVtbl *lpFrameVtbl;
792 LONG ref;
793 IStream *stream;
794 png_structp png_ptr;
795 png_infop info_ptr;
796 UINT frame_count;
797 BOOL frame_initialized;
798 const struct png_pixelformat *format;
799 BOOL info_written;
800 UINT width, height;
801 double xres, yres;
802 UINT lines_written;
803 BOOL frame_committed;
804 BOOL committed;
805 CRITICAL_SECTION lock;
806 } PngEncoder;
808 static inline PngEncoder *encoder_from_frame(IWICBitmapFrameEncode *iface)
810 return CONTAINING_RECORD(iface, PngEncoder, lpFrameVtbl);
813 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
814 void **ppv)
816 PngEncoder *This = encoder_from_frame(iface);
817 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
819 if (!ppv) return E_INVALIDARG;
821 if (IsEqualIID(&IID_IUnknown, iid) ||
822 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
824 *ppv = &This->lpFrameVtbl;
826 else
828 *ppv = NULL;
829 return E_NOINTERFACE;
832 IUnknown_AddRef((IUnknown*)*ppv);
833 return S_OK;
836 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
838 PngEncoder *This = encoder_from_frame(iface);
839 return IUnknown_AddRef((IUnknown*)This);
842 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
844 PngEncoder *This = encoder_from_frame(iface);
845 return IUnknown_Release((IUnknown*)This);
848 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
849 IPropertyBag2 *pIEncoderOptions)
851 PngEncoder *This = encoder_from_frame(iface);
852 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
854 EnterCriticalSection(&This->lock);
856 if (This->frame_initialized)
858 LeaveCriticalSection(&This->lock);
859 return WINCODEC_ERR_WRONGSTATE;
862 This->frame_initialized = TRUE;
864 LeaveCriticalSection(&This->lock);
866 return S_OK;
869 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
870 UINT uiWidth, UINT uiHeight)
872 PngEncoder *This = encoder_from_frame(iface);
873 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
875 EnterCriticalSection(&This->lock);
877 if (!This->frame_initialized || This->info_written)
879 LeaveCriticalSection(&This->lock);
880 return WINCODEC_ERR_WRONGSTATE;
883 This->width = uiWidth;
884 This->height = uiHeight;
886 LeaveCriticalSection(&This->lock);
888 return S_OK;
891 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
892 double dpiX, double dpiY)
894 PngEncoder *This = encoder_from_frame(iface);
895 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
897 EnterCriticalSection(&This->lock);
899 if (!This->frame_initialized || This->info_written)
901 LeaveCriticalSection(&This->lock);
902 return WINCODEC_ERR_WRONGSTATE;
905 This->xres = dpiX;
906 This->yres = dpiY;
908 LeaveCriticalSection(&This->lock);
910 return S_OK;
913 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
914 WICPixelFormatGUID *pPixelFormat)
916 PngEncoder *This = encoder_from_frame(iface);
917 int i;
918 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
920 EnterCriticalSection(&This->lock);
922 if (!This->frame_initialized || This->info_written)
924 LeaveCriticalSection(&This->lock);
925 return WINCODEC_ERR_WRONGSTATE;
928 for (i=0; formats[i].guid; i++)
930 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
931 break;
934 if (!formats[i].guid) i = 0;
936 This->format = &formats[i];
937 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
939 LeaveCriticalSection(&This->lock);
941 return S_OK;
944 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
945 UINT cCount, IWICColorContext **ppIColorContext)
947 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
948 return E_NOTIMPL;
951 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
952 IWICPalette *pIPalette)
954 FIXME("(%p,%p): stub\n", iface, pIPalette);
955 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
958 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
959 IWICBitmapSource *pIThumbnail)
961 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
962 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
965 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
966 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
968 PngEncoder *This = encoder_from_frame(iface);
969 png_byte **row_pointers=NULL;
970 UINT i;
971 jmp_buf jmpbuf;
972 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
974 EnterCriticalSection(&This->lock);
976 if (!This->frame_initialized || !This->width || !This->height || !This->format)
978 LeaveCriticalSection(&This->lock);
979 return WINCODEC_ERR_WRONGSTATE;
982 if (lineCount == 0 || lineCount + This->lines_written > This->height)
984 LeaveCriticalSection(&This->lock);
985 return E_INVALIDARG;
988 /* set up setjmp/longjmp error handling */
989 if (setjmp(jmpbuf))
991 LeaveCriticalSection(&This->lock);
992 HeapFree(GetProcessHeap(), 0, row_pointers);
993 return E_FAIL;
995 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
997 if (!This->info_written)
999 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
1000 This->format->bit_depth, This->format->color_type, PNG_INTERLACE_NONE,
1001 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1003 if (This->xres != 0.0 && This->yres != 0.0)
1005 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
1006 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
1009 ppng_write_info(This->png_ptr, This->info_ptr);
1011 if (This->format->remove_filler)
1012 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1014 if (This->format->swap_rgb)
1015 ppng_set_bgr(This->png_ptr);
1017 This->info_written = TRUE;
1020 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1021 if (!row_pointers)
1023 LeaveCriticalSection(&This->lock);
1024 return E_OUTOFMEMORY;
1027 for (i=0; i<lineCount; i++)
1028 row_pointers[i] = pbPixels + cbStride * i;
1030 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1031 This->lines_written += lineCount;
1033 LeaveCriticalSection(&This->lock);
1035 HeapFree(GetProcessHeap(), 0, row_pointers);
1037 return S_OK;
1040 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1041 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1043 PngEncoder *This = encoder_from_frame(iface);
1044 HRESULT hr;
1045 WICRect rc;
1046 WICPixelFormatGUID guid;
1047 UINT stride;
1048 BYTE *pixeldata;
1049 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1051 if (!This->frame_initialized || !This->width || !This->height)
1052 return WINCODEC_ERR_WRONGSTATE;
1054 if (!This->format)
1056 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1057 if (FAILED(hr)) return hr;
1058 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1059 if (FAILED(hr)) return hr;
1062 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1063 if (FAILED(hr)) return hr;
1064 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1066 /* FIXME: should use WICConvertBitmapSource to convert */
1067 ERR("format %s unsupported\n", debugstr_guid(&guid));
1068 return E_FAIL;
1071 if (This->xres == 0.0 || This->yres == 0.0)
1073 double xres, yres;
1074 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1075 if (FAILED(hr)) return hr;
1076 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1077 if (FAILED(hr)) return hr;
1080 if (!prc)
1082 UINT width, height;
1083 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1084 if (FAILED(hr)) return hr;
1085 rc.X = 0;
1086 rc.Y = 0;
1087 rc.Width = width;
1088 rc.Height = height;
1089 prc = &rc;
1092 if (prc->Width != This->width) return E_INVALIDARG;
1094 stride = (This->format->bpp * This->width + 7)/8;
1096 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1097 if (!pixeldata) return E_OUTOFMEMORY;
1099 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1100 stride*prc->Height, pixeldata);
1102 if (SUCCEEDED(hr))
1104 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1105 stride*prc->Height, pixeldata);
1108 HeapFree(GetProcessHeap(), 0, pixeldata);
1110 return hr;
1113 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1115 PngEncoder *This = encoder_from_frame(iface);
1116 jmp_buf jmpbuf;
1117 TRACE("(%p)\n", iface);
1119 EnterCriticalSection(&This->lock);
1121 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1123 LeaveCriticalSection(&This->lock);
1124 return WINCODEC_ERR_WRONGSTATE;
1127 /* set up setjmp/longjmp error handling */
1128 if (setjmp(jmpbuf))
1130 LeaveCriticalSection(&This->lock);
1131 return E_FAIL;
1133 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
1135 ppng_write_end(This->png_ptr, This->info_ptr);
1137 This->frame_committed = TRUE;
1139 LeaveCriticalSection(&This->lock);
1141 return S_OK;
1144 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1145 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1147 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1148 return E_NOTIMPL;
1151 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1152 PngFrameEncode_QueryInterface,
1153 PngFrameEncode_AddRef,
1154 PngFrameEncode_Release,
1155 PngFrameEncode_Initialize,
1156 PngFrameEncode_SetSize,
1157 PngFrameEncode_SetResolution,
1158 PngFrameEncode_SetPixelFormat,
1159 PngFrameEncode_SetColorContexts,
1160 PngFrameEncode_SetPalette,
1161 PngFrameEncode_SetThumbnail,
1162 PngFrameEncode_WritePixels,
1163 PngFrameEncode_WriteSource,
1164 PngFrameEncode_Commit,
1165 PngFrameEncode_GetMetadataQueryWriter
1168 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1169 void **ppv)
1171 PngEncoder *This = (PngEncoder*)iface;
1172 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1174 if (!ppv) return E_INVALIDARG;
1176 if (IsEqualIID(&IID_IUnknown, iid) ||
1177 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1179 *ppv = This;
1181 else
1183 *ppv = NULL;
1184 return E_NOINTERFACE;
1187 IUnknown_AddRef((IUnknown*)*ppv);
1188 return S_OK;
1191 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1193 PngEncoder *This = (PngEncoder*)iface;
1194 ULONG ref = InterlockedIncrement(&This->ref);
1196 TRACE("(%p) refcount=%u\n", iface, ref);
1198 return ref;
1201 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1203 PngEncoder *This = (PngEncoder*)iface;
1204 ULONG ref = InterlockedDecrement(&This->ref);
1206 TRACE("(%p) refcount=%u\n", iface, ref);
1208 if (ref == 0)
1210 This->lock.DebugInfo->Spare[0] = 0;
1211 DeleteCriticalSection(&This->lock);
1212 if (This->png_ptr)
1213 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1214 if (This->stream)
1215 IStream_Release(This->stream);
1216 HeapFree(GetProcessHeap(), 0, This);
1219 return ref;
1222 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1224 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1225 HRESULT hr;
1226 ULONG byteswritten;
1228 hr = IStream_Write(This->stream, data, length, &byteswritten);
1229 if (FAILED(hr) || byteswritten != length)
1231 ppng_error(png_ptr, "failed writing data");
1235 static void user_flush(png_structp png_ptr)
1239 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1240 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1242 PngEncoder *This = (PngEncoder*)iface;
1243 jmp_buf jmpbuf;
1245 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1247 EnterCriticalSection(&This->lock);
1249 if (This->png_ptr)
1251 LeaveCriticalSection(&This->lock);
1252 return WINCODEC_ERR_WRONGSTATE;
1255 /* initialize libpng */
1256 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1257 if (!This->png_ptr)
1259 LeaveCriticalSection(&This->lock);
1260 return E_FAIL;
1263 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1264 if (!This->info_ptr)
1266 ppng_destroy_write_struct(&This->png_ptr, NULL);
1267 This->png_ptr = NULL;
1268 LeaveCriticalSection(&This->lock);
1269 return E_FAIL;
1272 IStream_AddRef(pIStream);
1273 This->stream = pIStream;
1275 /* set up setjmp/longjmp error handling */
1276 if (setjmp(jmpbuf))
1278 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1279 This->png_ptr = NULL;
1280 IStream_Release(This->stream);
1281 This->stream = NULL;
1282 LeaveCriticalSection(&This->lock);
1283 return E_FAIL;
1285 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
1287 /* set up custom i/o handling */
1288 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1290 LeaveCriticalSection(&This->lock);
1292 return S_OK;
1295 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1296 GUID *pguidContainerFormat)
1298 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1299 return E_NOTIMPL;
1302 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1303 IWICBitmapEncoderInfo **ppIEncoderInfo)
1305 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1306 return E_NOTIMPL;
1309 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1310 UINT cCount, IWICColorContext **ppIColorContext)
1312 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1313 return E_NOTIMPL;
1316 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1318 TRACE("(%p,%p)\n", iface, pIPalette);
1319 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1322 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1324 TRACE("(%p,%p)\n", iface, pIThumbnail);
1325 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1328 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1330 TRACE("(%p,%p)\n", iface, pIPreview);
1331 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1334 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1335 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1337 PngEncoder *This = (PngEncoder*)iface;
1338 HRESULT hr;
1339 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1341 EnterCriticalSection(&This->lock);
1343 if (This->frame_count != 0)
1345 LeaveCriticalSection(&This->lock);
1346 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1349 if (!This->stream)
1351 LeaveCriticalSection(&This->lock);
1352 return WINCODEC_ERR_NOTINITIALIZED;
1355 hr = CreatePropertyBag2(ppIEncoderOptions);
1356 if (FAILED(hr))
1358 LeaveCriticalSection(&This->lock);
1359 return hr;
1362 This->frame_count = 1;
1364 LeaveCriticalSection(&This->lock);
1366 IWICBitmapEncoder_AddRef(iface);
1367 *ppIFrameEncode = (IWICBitmapFrameEncode*)&This->lpFrameVtbl;
1369 return S_OK;
1372 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1374 PngEncoder *This = (PngEncoder*)iface;
1375 TRACE("(%p)\n", iface);
1377 EnterCriticalSection(&This->lock);
1379 if (!This->frame_committed || This->committed)
1381 LeaveCriticalSection(&This->lock);
1382 return WINCODEC_ERR_WRONGSTATE;
1385 This->committed = TRUE;
1387 LeaveCriticalSection(&This->lock);
1389 return S_OK;
1392 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1393 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1395 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1396 return E_NOTIMPL;
1399 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
1400 PngEncoder_QueryInterface,
1401 PngEncoder_AddRef,
1402 PngEncoder_Release,
1403 PngEncoder_Initialize,
1404 PngEncoder_GetContainerFormat,
1405 PngEncoder_GetEncoderInfo,
1406 PngEncoder_SetColorContexts,
1407 PngEncoder_SetPalette,
1408 PngEncoder_SetThumbnail,
1409 PngEncoder_SetPreview,
1410 PngEncoder_CreateNewFrame,
1411 PngEncoder_Commit,
1412 PngEncoder_GetMetadataQueryWriter
1415 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1417 PngEncoder *This;
1418 HRESULT ret;
1420 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1422 *ppv = NULL;
1424 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1426 if (!libpng_handle && !load_libpng())
1428 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1429 return E_FAIL;
1432 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1433 if (!This) return E_OUTOFMEMORY;
1435 This->lpVtbl = &PngEncoder_Vtbl;
1436 This->lpFrameVtbl = &PngEncoder_FrameVtbl;
1437 This->ref = 1;
1438 This->png_ptr = NULL;
1439 This->info_ptr = NULL;
1440 This->stream = NULL;
1441 This->frame_count = 0;
1442 This->frame_initialized = FALSE;
1443 This->format = NULL;
1444 This->info_written = FALSE;
1445 This->width = 0;
1446 This->height = 0;
1447 This->xres = 0.0;
1448 This->yres = 0.0;
1449 This->lines_written = 0;
1450 This->frame_committed = FALSE;
1451 This->committed = FALSE;
1452 InitializeCriticalSection(&This->lock);
1453 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PngEncoder.lock");
1455 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1456 IUnknown_Release((IUnknown*)This);
1458 return ret;
1461 #else /* !HAVE_PNG_H */
1463 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1465 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1466 return E_FAIL;
1469 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1471 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");
1472 return E_FAIL;
1475 #endif