sync with trunk (r46275)
[reactos.git] / dll / win32 / windowscodecs / pngformat.c
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <stdarg.h>
23
24 #ifdef HAVE_PNG_H
25 #include <png.h>
26 #endif
27
28 #define COBJMACROS
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "objbase.h"
33 #include "wincodec.h"
34
35 #include "wincodecs_private.h"
36
37 #include "wine/debug.h"
38 #include "wine/library.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
41
42 #ifdef SONAME_LIBPNG
43
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
83
84 static void *load_libpng(void)
85 {
86 if((libpng_handle = wine_dlopen(SONAME_LIBPNG, RTLD_NOW, NULL, 0)) != NULL) {
87
88 #define LOAD_FUNCPTR(f) \
89 if((p##f = wine_dlsym(libpng_handle, #f, NULL, 0)) == NULL) { \
90 libpng_handle = NULL; \
91 return NULL; \
92 }
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);
129
130 #undef LOAD_FUNCPTR
131 }
132 return libpng_handle;
133 }
134
135 static void user_error_fn(png_structp png_ptr, png_const_charp error_message)
136 {
137 jmp_buf *pjmpbuf;
138
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);
145 }
146
147 static void user_warning_fn(png_structp png_ptr, png_const_charp warning_message)
148 {
149 WARN("PNG warning: %s\n", debugstr_a(warning_message));
150 }
151
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 } PngDecoder;
166
167 static inline PngDecoder *impl_from_frame(IWICBitmapFrameDecode *iface)
168 {
169 return CONTAINING_RECORD(iface, PngDecoder, lpFrameVtbl);
170 }
171
172 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
173
174 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
175 void **ppv)
176 {
177 PngDecoder *This = (PngDecoder*)iface;
178 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
179
180 if (!ppv) return E_INVALIDARG;
181
182 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
183 {
184 *ppv = This;
185 }
186 else
187 {
188 *ppv = NULL;
189 return E_NOINTERFACE;
190 }
191
192 IUnknown_AddRef((IUnknown*)*ppv);
193 return S_OK;
194 }
195
196 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
197 {
198 PngDecoder *This = (PngDecoder*)iface;
199 ULONG ref = InterlockedIncrement(&This->ref);
200
201 TRACE("(%p) refcount=%u\n", iface, ref);
202
203 return ref;
204 }
205
206 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
207 {
208 PngDecoder *This = (PngDecoder*)iface;
209 ULONG ref = InterlockedDecrement(&This->ref);
210
211 TRACE("(%p) refcount=%u\n", iface, ref);
212
213 if (ref == 0)
214 {
215 if (This->png_ptr)
216 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
217 HeapFree(GetProcessHeap(), 0, This->image_bits);
218 HeapFree(GetProcessHeap(), 0, This);
219 }
220
221 return ref;
222 }
223
224 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
225 DWORD *pdwCapability)
226 {
227 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
228 return E_NOTIMPL;
229 }
230
231 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
232 {
233 IStream *stream = ppng_get_io_ptr(png_ptr);
234 HRESULT hr;
235 ULONG bytesread;
236
237 hr = IStream_Read(stream, data, length, &bytesread);
238 if (FAILED(hr) || bytesread != length)
239 {
240 ppng_error(png_ptr, "failed reading data");
241 }
242 }
243
244 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
245 WICDecodeOptions cacheOptions)
246 {
247 PngDecoder *This = (PngDecoder*)iface;
248 LARGE_INTEGER seek;
249 HRESULT hr;
250 png_bytep *row_pointers=NULL;
251 UINT image_size;
252 UINT i;
253 int color_type, bit_depth;
254 png_bytep trans;
255 int num_trans;
256 png_uint_32 transparency;
257 png_color_16p trans_values;
258 jmp_buf jmpbuf;
259
260 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
261
262 /* initialize libpng */
263 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
264 if (!This->png_ptr) return E_FAIL;
265
266 This->info_ptr = ppng_create_info_struct(This->png_ptr);
267 if (!This->info_ptr)
268 {
269 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
270 This->png_ptr = NULL;
271 return E_FAIL;
272 }
273
274 This->end_info = ppng_create_info_struct(This->png_ptr);
275 if (!This->info_ptr)
276 {
277 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
278 This->png_ptr = NULL;
279 return E_FAIL;
280 }
281
282 /* set up setjmp/longjmp error handling */
283 if (setjmp(jmpbuf))
284 {
285 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, &This->end_info);
286 HeapFree(GetProcessHeap(), 0, row_pointers);
287 This->png_ptr = NULL;
288 return E_FAIL;
289 }
290 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
291
292 /* seek to the start of the stream */
293 seek.QuadPart = 0;
294 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
295 if (FAILED(hr)) return hr;
296
297 /* set up custom i/o handling */
298 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
299
300 /* read the header */
301 ppng_read_info(This->png_ptr, This->info_ptr);
302
303 /* choose a pixel format */
304 color_type = ppng_get_color_type(This->png_ptr, This->info_ptr);
305 bit_depth = ppng_get_bit_depth(This->png_ptr, This->info_ptr);
306
307 /* check for color-keyed alpha */
308 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
309
310 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
311 {
312 /* expand to RGBA */
313 if (color_type == PNG_COLOR_TYPE_GRAY)
314 {
315 if (bit_depth < 8)
316 {
317 #if HAVE_PNG_SET_EXPAND_GRAY_1_2_4_TO_8
318 ppng_set_expand_gray_1_2_4_to_8(This->png_ptr);
319 #else
320 ppng_set_gray_1_2_4_to_8(This->png_ptr);
321 #endif
322 bit_depth = 8;
323 }
324 ppng_set_gray_to_rgb(This->png_ptr);
325 }
326 ppng_set_tRNS_to_alpha(This->png_ptr);
327 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
328 }
329
330 switch (color_type)
331 {
332 case PNG_COLOR_TYPE_GRAY:
333 This->bpp = bit_depth;
334 switch (bit_depth)
335 {
336 case 1: This->format = &GUID_WICPixelFormatBlackWhite; break;
337 case 2: This->format = &GUID_WICPixelFormat2bppGray; break;
338 case 4: This->format = &GUID_WICPixelFormat4bppGray; break;
339 case 8: This->format = &GUID_WICPixelFormat8bppGray; break;
340 case 16: This->format = &GUID_WICPixelFormat16bppGray; break;
341 default:
342 ERR("invalid grayscale bit depth: %i\n", bit_depth);
343 return E_FAIL;
344 }
345 break;
346 case PNG_COLOR_TYPE_GRAY_ALPHA:
347 /* WIC does not support grayscale alpha formats so use RGBA */
348 ppng_set_gray_to_rgb(This->png_ptr);
349 case PNG_COLOR_TYPE_RGB_ALPHA:
350 This->bpp = bit_depth * 4;
351 switch (bit_depth)
352 {
353 case 8:
354 ppng_set_bgr(This->png_ptr);
355 This->format = &GUID_WICPixelFormat32bppBGRA;
356 break;
357 case 16: This->format = &GUID_WICPixelFormat64bppRGBA; break;
358 default:
359 ERR("invalid RGBA bit depth: %i\n", bit_depth);
360 return E_FAIL;
361 }
362 break;
363 case PNG_COLOR_TYPE_PALETTE:
364 This->bpp = bit_depth;
365 switch (bit_depth)
366 {
367 case 1: This->format = &GUID_WICPixelFormat1bppIndexed; break;
368 case 2: This->format = &GUID_WICPixelFormat2bppIndexed; break;
369 case 4: This->format = &GUID_WICPixelFormat4bppIndexed; break;
370 case 8: This->format = &GUID_WICPixelFormat8bppIndexed; break;
371 default:
372 ERR("invalid indexed color bit depth: %i\n", bit_depth);
373 return E_FAIL;
374 }
375 break;
376 case PNG_COLOR_TYPE_RGB:
377 This->bpp = bit_depth * 3;
378 switch (bit_depth)
379 {
380 case 8:
381 ppng_set_bgr(This->png_ptr);
382 This->format = &GUID_WICPixelFormat24bppBGR;
383 break;
384 case 16: This->format = &GUID_WICPixelFormat48bppRGB; break;
385 default:
386 ERR("invalid RGB color bit depth: %i\n", bit_depth);
387 return E_FAIL;
388 }
389 break;
390 default:
391 ERR("invalid color type %i\n", color_type);
392 return E_FAIL;
393 }
394
395 /* read the image data */
396 This->width = ppng_get_image_width(This->png_ptr, This->info_ptr);
397 This->height = ppng_get_image_height(This->png_ptr, This->info_ptr);
398 This->stride = This->width * This->bpp;
399 image_size = This->stride * This->height;
400
401 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
402 if (!This->image_bits) return E_OUTOFMEMORY;
403
404 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
405 if (!row_pointers) return E_OUTOFMEMORY;
406
407 for (i=0; i<This->height; i++)
408 row_pointers[i] = This->image_bits + i * This->stride;
409
410 ppng_read_image(This->png_ptr, row_pointers);
411
412 HeapFree(GetProcessHeap(), 0, row_pointers);
413 row_pointers = NULL;
414
415 ppng_read_end(This->png_ptr, This->end_info);
416
417 This->initialized = TRUE;
418
419 return S_OK;
420 }
421
422 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
423 GUID *pguidContainerFormat)
424 {
425 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
426 return S_OK;
427 }
428
429 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
430 IWICBitmapDecoderInfo **ppIDecoderInfo)
431 {
432 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
433 return E_NOTIMPL;
434 }
435
436 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
437 IWICPalette *pIPalette)
438 {
439 FIXME("(%p,%p): stub\n", iface, pIPalette);
440 return E_NOTIMPL;
441 }
442
443 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
444 IWICMetadataQueryReader **ppIMetadataQueryReader)
445 {
446 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
447 return E_NOTIMPL;
448 }
449
450 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
451 IWICBitmapSource **ppIBitmapSource)
452 {
453 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
454 return E_NOTIMPL;
455 }
456
457 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
458 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
459 {
460 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
461 return E_NOTIMPL;
462 }
463
464 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
465 IWICBitmapSource **ppIThumbnail)
466 {
467 TRACE("(%p,%p)\n", iface, ppIThumbnail);
468 return WINCODEC_ERR_CODECNOTHUMBNAIL;
469 }
470
471 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
472 UINT *pCount)
473 {
474 *pCount = 1;
475 return S_OK;
476 }
477
478 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
479 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
480 {
481 PngDecoder *This = (PngDecoder*)iface;
482 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
483
484 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
485
486 if (index != 0) return E_INVALIDARG;
487
488 IWICBitmapDecoder_AddRef(iface);
489
490 *ppIBitmapFrame = (void*)(&This->lpFrameVtbl);
491
492 return S_OK;
493 }
494
495 static const IWICBitmapDecoderVtbl PngDecoder_Vtbl = {
496 PngDecoder_QueryInterface,
497 PngDecoder_AddRef,
498 PngDecoder_Release,
499 PngDecoder_QueryCapability,
500 PngDecoder_Initialize,
501 PngDecoder_GetContainerFormat,
502 PngDecoder_GetDecoderInfo,
503 PngDecoder_CopyPalette,
504 PngDecoder_GetMetadataQueryReader,
505 PngDecoder_GetPreview,
506 PngDecoder_GetColorContexts,
507 PngDecoder_GetThumbnail,
508 PngDecoder_GetFrameCount,
509 PngDecoder_GetFrame
510 };
511
512 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
513 void **ppv)
514 {
515 if (!ppv) return E_INVALIDARG;
516
517 if (IsEqualIID(&IID_IUnknown, iid) ||
518 IsEqualIID(&IID_IWICBitmapSource, iid) ||
519 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
520 {
521 *ppv = iface;
522 }
523 else
524 {
525 *ppv = NULL;
526 return E_NOINTERFACE;
527 }
528
529 IUnknown_AddRef((IUnknown*)*ppv);
530 return S_OK;
531 }
532
533 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
534 {
535 PngDecoder *This = impl_from_frame(iface);
536 return IUnknown_AddRef((IUnknown*)This);
537 }
538
539 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
540 {
541 PngDecoder *This = impl_from_frame(iface);
542 return IUnknown_Release((IUnknown*)This);
543 }
544
545 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
546 UINT *puiWidth, UINT *puiHeight)
547 {
548 PngDecoder *This = impl_from_frame(iface);
549 *puiWidth = This->width;
550 *puiHeight = This->height;
551 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
552 return S_OK;
553 }
554
555 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
556 WICPixelFormatGUID *pPixelFormat)
557 {
558 PngDecoder *This = impl_from_frame(iface);
559 TRACE("(%p,%p)\n", iface, pPixelFormat);
560
561 memcpy(pPixelFormat, This->format, sizeof(GUID));
562
563 return S_OK;
564 }
565
566 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
567 double *pDpiX, double *pDpiY)
568 {
569 PngDecoder *This = impl_from_frame(iface);
570 png_uint_32 ret, xres, yres;
571 int unit_type;
572
573 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
574
575 if (ret && unit_type == PNG_RESOLUTION_METER)
576 {
577 *pDpiX = xres * 0.0254;
578 *pDpiY = yres * 0.0254;
579 }
580 else
581 {
582 WARN("no pHYs block present\n");
583 *pDpiX = *pDpiY = 96.0;
584 }
585
586 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
587
588 return S_OK;
589 }
590
591 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
592 IWICPalette *pIPalette)
593 {
594 PngDecoder *This = impl_from_frame(iface);
595 png_uint_32 ret;
596 png_colorp png_palette;
597 int num_palette;
598 WICColor palette[256];
599 png_bytep trans;
600 int num_trans;
601 png_color_16p trans_values;
602 int i;
603
604 TRACE("(%p,%p)\n", iface, pIPalette);
605
606 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
607 if (!ret) return WINCODEC_ERR_PALETTEUNAVAILABLE;
608
609 if (num_palette > 256)
610 {
611 ERR("palette has %i colors?!\n", num_palette);
612 return E_FAIL;
613 }
614
615 for (i=0; i<num_palette; i++)
616 {
617 palette[i] = (0xff000000|
618 png_palette[i].red << 16|
619 png_palette[i].green << 8|
620 png_palette[i].blue);
621 }
622
623 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
624 if (ret)
625 {
626 for (i=0; i<num_trans; i++)
627 {
628 palette[trans[i]] = 0x00000000;
629 }
630 }
631
632 return IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
633 }
634
635 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
636 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
637 {
638 PngDecoder *This = impl_from_frame(iface);
639 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
640
641 return copy_pixels(This->bpp, This->image_bits,
642 This->width, This->height, This->stride,
643 prc, cbStride, cbBufferSize, pbBuffer);
644 }
645
646 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
647 IWICMetadataQueryReader **ppIMetadataQueryReader)
648 {
649 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
650 return E_NOTIMPL;
651 }
652
653 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
654 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
655 {
656 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
657 return E_NOTIMPL;
658 }
659
660 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
661 IWICBitmapSource **ppIThumbnail)
662 {
663 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
664 return E_NOTIMPL;
665 }
666
667 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl = {
668 PngDecoder_Frame_QueryInterface,
669 PngDecoder_Frame_AddRef,
670 PngDecoder_Frame_Release,
671 PngDecoder_Frame_GetSize,
672 PngDecoder_Frame_GetPixelFormat,
673 PngDecoder_Frame_GetResolution,
674 PngDecoder_Frame_CopyPalette,
675 PngDecoder_Frame_CopyPixels,
676 PngDecoder_Frame_GetMetadataQueryReader,
677 PngDecoder_Frame_GetColorContexts,
678 PngDecoder_Frame_GetThumbnail
679 };
680
681 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
682 {
683 PngDecoder *This;
684 HRESULT ret;
685
686 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
687
688 *ppv = NULL;
689
690 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
691
692 if (!libpng_handle && !load_libpng())
693 {
694 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
695 return E_FAIL;
696 }
697
698 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
699 if (!This) return E_OUTOFMEMORY;
700
701 This->lpVtbl = &PngDecoder_Vtbl;
702 This->lpFrameVtbl = &PngDecoder_FrameVtbl;
703 This->ref = 1;
704 This->png_ptr = NULL;
705 This->info_ptr = NULL;
706 This->end_info = NULL;
707 This->initialized = FALSE;
708 This->image_bits = NULL;
709
710 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
711 IUnknown_Release((IUnknown*)This);
712
713 return ret;
714 }
715
716 struct png_pixelformat {
717 const WICPixelFormatGUID *guid;
718 UINT bpp;
719 int bit_depth;
720 int color_type;
721 BOOL remove_filler;
722 BOOL swap_rgb;
723 };
724
725 static const struct png_pixelformat formats[] = {
726 {&GUID_WICPixelFormat24bppBGR, 24, 8, PNG_COLOR_TYPE_RGB, 0, 1},
727 {&GUID_WICPixelFormatBlackWhite, 1, 1, PNG_COLOR_TYPE_GRAY, 0, 0},
728 {&GUID_WICPixelFormat2bppGray, 2, 2, PNG_COLOR_TYPE_GRAY, 0, 0},
729 {&GUID_WICPixelFormat4bppGray, 4, 4, PNG_COLOR_TYPE_GRAY, 0, 0},
730 {&GUID_WICPixelFormat8bppGray, 8, 8, PNG_COLOR_TYPE_GRAY, 0, 0},
731 {&GUID_WICPixelFormat16bppGray, 16, 16, PNG_COLOR_TYPE_GRAY, 0, 0},
732 {&GUID_WICPixelFormat32bppBGR, 32, 8, PNG_COLOR_TYPE_RGB, 1, 1},
733 {&GUID_WICPixelFormat32bppBGRA, 32, 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 1},
734 {&GUID_WICPixelFormat48bppRGB, 48, 16, PNG_COLOR_TYPE_RGB, 0, 0},
735 {&GUID_WICPixelFormat64bppRGBA, 64, 16, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0},
736 {NULL},
737 };
738
739 typedef struct PngEncoder {
740 const IWICBitmapEncoderVtbl *lpVtbl;
741 const IWICBitmapFrameEncodeVtbl *lpFrameVtbl;
742 LONG ref;
743 IStream *stream;
744 png_structp png_ptr;
745 png_infop info_ptr;
746 UINT frame_count;
747 BOOL frame_initialized;
748 const struct png_pixelformat *format;
749 BOOL info_written;
750 UINT width, height;
751 double xres, yres;
752 UINT lines_written;
753 BOOL frame_committed;
754 BOOL committed;
755 } PngEncoder;
756
757 static inline PngEncoder *encoder_from_frame(IWICBitmapFrameEncode *iface)
758 {
759 return CONTAINING_RECORD(iface, PngEncoder, lpFrameVtbl);
760 }
761
762 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
763 void **ppv)
764 {
765 PngEncoder *This = encoder_from_frame(iface);
766 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
767
768 if (!ppv) return E_INVALIDARG;
769
770 if (IsEqualIID(&IID_IUnknown, iid) ||
771 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
772 {
773 *ppv = &This->lpFrameVtbl;
774 }
775 else
776 {
777 *ppv = NULL;
778 return E_NOINTERFACE;
779 }
780
781 IUnknown_AddRef((IUnknown*)*ppv);
782 return S_OK;
783 }
784
785 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
786 {
787 PngEncoder *This = encoder_from_frame(iface);
788 return IUnknown_AddRef((IUnknown*)This);
789 }
790
791 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
792 {
793 PngEncoder *This = encoder_from_frame(iface);
794 return IUnknown_Release((IUnknown*)This);
795 }
796
797 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
798 IPropertyBag2 *pIEncoderOptions)
799 {
800 PngEncoder *This = encoder_from_frame(iface);
801 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
802
803 if (This->frame_initialized) return WINCODEC_ERR_WRONGSTATE;
804
805 This->frame_initialized = TRUE;
806
807 return S_OK;
808 }
809
810 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
811 UINT uiWidth, UINT uiHeight)
812 {
813 PngEncoder *This = encoder_from_frame(iface);
814 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
815
816 if (!This->frame_initialized || This->info_written) return WINCODEC_ERR_WRONGSTATE;
817
818 This->width = uiWidth;
819 This->height = uiHeight;
820
821 return S_OK;
822 }
823
824 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
825 double dpiX, double dpiY)
826 {
827 PngEncoder *This = encoder_from_frame(iface);
828 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
829
830 if (!This->frame_initialized || This->info_written) return WINCODEC_ERR_WRONGSTATE;
831
832 This->xres = dpiX;
833 This->yres = dpiY;
834
835 return S_OK;
836 }
837
838 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
839 WICPixelFormatGUID *pPixelFormat)
840 {
841 PngEncoder *This = encoder_from_frame(iface);
842 int i;
843 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
844
845 if (!This->frame_initialized || This->info_written) return WINCODEC_ERR_WRONGSTATE;
846
847 for (i=0; formats[i].guid; i++)
848 {
849 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
850 break;
851 }
852
853 if (!formats[i].guid) i = 0;
854
855 This->format = &formats[i];
856 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
857
858 return S_OK;
859 }
860
861 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
862 UINT cCount, IWICColorContext **ppIColorContext)
863 {
864 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
865 return E_NOTIMPL;
866 }
867
868 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
869 IWICPalette *pIPalette)
870 {
871 FIXME("(%p,%p): stub\n", iface, pIPalette);
872 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
873 }
874
875 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
876 IWICBitmapSource *pIThumbnail)
877 {
878 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
879 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
880 }
881
882 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
883 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
884 {
885 PngEncoder *This = encoder_from_frame(iface);
886 png_byte **row_pointers=NULL;
887 UINT i;
888 jmp_buf jmpbuf;
889 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
890
891 if (!This->frame_initialized || !This->width || !This->height || !This->format)
892 return WINCODEC_ERR_WRONGSTATE;
893
894 if (lineCount == 0 || lineCount + This->lines_written > This->height)
895 return E_INVALIDARG;
896
897 /* set up setjmp/longjmp error handling */
898 if (setjmp(jmpbuf))
899 {
900 HeapFree(GetProcessHeap(), 0, row_pointers);
901 return E_FAIL;
902 }
903 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
904
905 if (!This->info_written)
906 {
907 ppng_set_IHDR(This->png_ptr, This->info_ptr, This->width, This->height,
908 This->format->bit_depth, This->format->color_type, PNG_INTERLACE_NONE,
909 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
910
911 if (This->xres != 0.0 && This->yres != 0.0)
912 {
913 ppng_set_pHYs(This->png_ptr, This->info_ptr, (This->xres+0.0127) / 0.0254,
914 (This->yres+0.0127) / 0.0254, PNG_RESOLUTION_METER);
915 }
916
917 ppng_write_info(This->png_ptr, This->info_ptr);
918
919 if (This->format->remove_filler)
920 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
921
922 if (This->format->swap_rgb)
923 ppng_set_bgr(This->png_ptr);
924
925 This->info_written = TRUE;
926 }
927
928 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
929 if (!row_pointers)
930 return E_OUTOFMEMORY;
931
932 for (i=0; i<lineCount; i++)
933 row_pointers[i] = pbPixels + cbStride * i;
934
935 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
936 This->lines_written += lineCount;
937
938 HeapFree(GetProcessHeap(), 0, row_pointers);
939
940 return S_OK;
941 }
942
943 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
944 IWICBitmapSource *pIBitmapSource, WICRect *prc)
945 {
946 PngEncoder *This = encoder_from_frame(iface);
947 HRESULT hr;
948 WICRect rc;
949 WICPixelFormatGUID guid;
950 UINT stride;
951 BYTE *pixeldata;
952 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
953
954 if (!This->frame_initialized || !This->width || !This->height)
955 return WINCODEC_ERR_WRONGSTATE;
956
957 if (!This->format)
958 {
959 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
960 if (FAILED(hr)) return hr;
961 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
962 if (FAILED(hr)) return hr;
963 }
964
965 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
966 if (FAILED(hr)) return hr;
967 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
968 {
969 /* FIXME: should use WICConvertBitmapSource to convert */
970 ERR("format %s unsupported\n", debugstr_guid(&guid));
971 return E_FAIL;
972 }
973
974 if (This->xres == 0.0 || This->yres == 0.0)
975 {
976 double xres, yres;
977 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
978 if (FAILED(hr)) return hr;
979 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
980 if (FAILED(hr)) return hr;
981 }
982
983 if (!prc)
984 {
985 UINT width, height;
986 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
987 if (FAILED(hr)) return hr;
988 rc.X = 0;
989 rc.Y = 0;
990 rc.Width = width;
991 rc.Height = height;
992 prc = &rc;
993 }
994
995 if (prc->Width != This->width) return E_INVALIDARG;
996
997 stride = (This->format->bpp * This->width + 7)/8;
998
999 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1000 if (!pixeldata) return E_OUTOFMEMORY;
1001
1002 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1003 stride*prc->Height, pixeldata);
1004
1005 if (SUCCEEDED(hr))
1006 {
1007 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1008 stride*prc->Height, pixeldata);
1009 }
1010
1011 HeapFree(GetProcessHeap(), 0, pixeldata);
1012
1013 return hr;
1014 }
1015
1016 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1017 {
1018 PngEncoder *This = encoder_from_frame(iface);
1019 jmp_buf jmpbuf;
1020 TRACE("(%p)\n", iface);
1021
1022 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1023 return WINCODEC_ERR_WRONGSTATE;
1024
1025 /* set up setjmp/longjmp error handling */
1026 if (setjmp(jmpbuf))
1027 {
1028 return E_FAIL;
1029 }
1030 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
1031
1032 ppng_write_end(This->png_ptr, This->info_ptr);
1033
1034 This->frame_committed = TRUE;
1035
1036 return S_OK;
1037 }
1038
1039 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1040 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1041 {
1042 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1043 return E_NOTIMPL;
1044 }
1045
1046 static const IWICBitmapFrameEncodeVtbl PngEncoder_FrameVtbl = {
1047 PngFrameEncode_QueryInterface,
1048 PngFrameEncode_AddRef,
1049 PngFrameEncode_Release,
1050 PngFrameEncode_Initialize,
1051 PngFrameEncode_SetSize,
1052 PngFrameEncode_SetResolution,
1053 PngFrameEncode_SetPixelFormat,
1054 PngFrameEncode_SetColorContexts,
1055 PngFrameEncode_SetPalette,
1056 PngFrameEncode_SetThumbnail,
1057 PngFrameEncode_WritePixels,
1058 PngFrameEncode_WriteSource,
1059 PngFrameEncode_Commit,
1060 PngFrameEncode_GetMetadataQueryWriter
1061 };
1062
1063 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1064 void **ppv)
1065 {
1066 PngEncoder *This = (PngEncoder*)iface;
1067 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1068
1069 if (!ppv) return E_INVALIDARG;
1070
1071 if (IsEqualIID(&IID_IUnknown, iid) ||
1072 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1073 {
1074 *ppv = This;
1075 }
1076 else
1077 {
1078 *ppv = NULL;
1079 return E_NOINTERFACE;
1080 }
1081
1082 IUnknown_AddRef((IUnknown*)*ppv);
1083 return S_OK;
1084 }
1085
1086 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1087 {
1088 PngEncoder *This = (PngEncoder*)iface;
1089 ULONG ref = InterlockedIncrement(&This->ref);
1090
1091 TRACE("(%p) refcount=%u\n", iface, ref);
1092
1093 return ref;
1094 }
1095
1096 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1097 {
1098 PngEncoder *This = (PngEncoder*)iface;
1099 ULONG ref = InterlockedDecrement(&This->ref);
1100
1101 TRACE("(%p) refcount=%u\n", iface, ref);
1102
1103 if (ref == 0)
1104 {
1105 if (This->png_ptr)
1106 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1107 if (This->stream)
1108 IStream_Release(This->stream);
1109 HeapFree(GetProcessHeap(), 0, This);
1110 }
1111
1112 return ref;
1113 }
1114
1115 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1116 {
1117 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1118 HRESULT hr;
1119 ULONG byteswritten;
1120
1121 hr = IStream_Write(This->stream, data, length, &byteswritten);
1122 if (FAILED(hr) || byteswritten != length)
1123 {
1124 ppng_error(png_ptr, "failed writing data");
1125 }
1126 }
1127
1128 static void user_flush(png_structp png_ptr)
1129 {
1130 }
1131
1132 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1133 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1134 {
1135 PngEncoder *This = (PngEncoder*)iface;
1136 jmp_buf jmpbuf;
1137
1138 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1139
1140 if (This->png_ptr)
1141 return WINCODEC_ERR_WRONGSTATE;
1142
1143 /* initialize libpng */
1144 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1145 if (!This->png_ptr)
1146 return E_FAIL;
1147
1148 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1149 if (!This->info_ptr)
1150 {
1151 ppng_destroy_write_struct(&This->png_ptr, NULL);
1152 This->png_ptr = NULL;
1153 return E_FAIL;
1154 }
1155
1156 IStream_AddRef(pIStream);
1157 This->stream = pIStream;
1158
1159 /* set up setjmp/longjmp error handling */
1160 if (setjmp(jmpbuf))
1161 {
1162 ppng_destroy_write_struct(&This->png_ptr, &This->info_ptr);
1163 This->png_ptr = NULL;
1164 IStream_Release(This->stream);
1165 This->stream = NULL;
1166 return E_FAIL;
1167 }
1168 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
1169
1170 /* set up custom i/o handling */
1171 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1172
1173 return S_OK;
1174 }
1175
1176 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1177 GUID *pguidContainerFormat)
1178 {
1179 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1180 return E_NOTIMPL;
1181 }
1182
1183 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1184 IWICBitmapEncoderInfo **ppIEncoderInfo)
1185 {
1186 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1187 return E_NOTIMPL;
1188 }
1189
1190 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1191 UINT cCount, IWICColorContext **ppIColorContext)
1192 {
1193 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1194 return E_NOTIMPL;
1195 }
1196
1197 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1198 {
1199 TRACE("(%p,%p)\n", iface, pIPalette);
1200 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1201 }
1202
1203 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1204 {
1205 TRACE("(%p,%p)\n", iface, pIThumbnail);
1206 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1207 }
1208
1209 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1210 {
1211 TRACE("(%p,%p)\n", iface, pIPreview);
1212 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1213 }
1214
1215 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1216 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1217 {
1218 PngEncoder *This = (PngEncoder*)iface;
1219 HRESULT hr;
1220 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1221
1222 if (This->frame_count != 0)
1223 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1224
1225 if (!This->stream)
1226 return WINCODEC_ERR_NOTINITIALIZED;
1227
1228 hr = CreatePropertyBag2(ppIEncoderOptions);
1229 if (FAILED(hr)) return hr;
1230
1231 This->frame_count = 1;
1232
1233 IWICBitmapEncoder_AddRef(iface);
1234 *ppIFrameEncode = (IWICBitmapFrameEncode*)&This->lpFrameVtbl;
1235
1236 return S_OK;
1237 }
1238
1239 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1240 {
1241 PngEncoder *This = (PngEncoder*)iface;
1242 TRACE("(%p)\n", iface);
1243
1244 if (!This->frame_committed || This->committed)
1245 return WINCODEC_ERR_WRONGSTATE;
1246
1247 This->committed = TRUE;
1248
1249 return S_OK;
1250 }
1251
1252 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1253 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1254 {
1255 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1256 return E_NOTIMPL;
1257 }
1258
1259 static const IWICBitmapEncoderVtbl PngEncoder_Vtbl = {
1260 PngEncoder_QueryInterface,
1261 PngEncoder_AddRef,
1262 PngEncoder_Release,
1263 PngEncoder_Initialize,
1264 PngEncoder_GetContainerFormat,
1265 PngEncoder_GetEncoderInfo,
1266 PngEncoder_SetColorContexts,
1267 PngEncoder_SetPalette,
1268 PngEncoder_SetThumbnail,
1269 PngEncoder_SetPreview,
1270 PngEncoder_CreateNewFrame,
1271 PngEncoder_Commit,
1272 PngEncoder_GetMetadataQueryWriter
1273 };
1274
1275 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1276 {
1277 PngEncoder *This;
1278 HRESULT ret;
1279
1280 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1281
1282 *ppv = NULL;
1283
1284 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1285
1286 if (!libpng_handle && !load_libpng())
1287 {
1288 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1289 return E_FAIL;
1290 }
1291
1292 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1293 if (!This) return E_OUTOFMEMORY;
1294
1295 This->lpVtbl = &PngEncoder_Vtbl;
1296 This->lpFrameVtbl = &PngEncoder_FrameVtbl;
1297 This->ref = 1;
1298 This->png_ptr = NULL;
1299 This->info_ptr = NULL;
1300 This->stream = NULL;
1301 This->frame_count = 0;
1302 This->frame_initialized = FALSE;
1303 This->format = NULL;
1304 This->info_written = FALSE;
1305 This->width = 0;
1306 This->height = 0;
1307 This->xres = 0.0;
1308 This->yres = 0.0;
1309 This->lines_written = 0;
1310 This->frame_committed = FALSE;
1311 This->committed = FALSE;
1312
1313 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1314 IUnknown_Release((IUnknown*)This);
1315
1316 return ret;
1317 }
1318
1319 #else /* !HAVE_PNG_H */
1320
1321 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1322 {
1323 ERR("Trying to load PNG picture, but PNG supported not compiled in.\n");
1324 return E_FAIL;
1325 }
1326
1327 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1328 {
1329 ERR("Trying to save PNG picture, but PNG supported not compiled in.\n");
1330 return E_FAIL;
1331 }
1332
1333 #endif