- Sync rsaenh, schannel, shlwapi, sti, urlmon, usp10, version, windowscodecs, wininet...
[reactos.git] / reactos / 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 CRITICAL_SECTION lock; /* must be held when png structures are accessed or initialized is set */
166 } PngDecoder;
167
168 static inline PngDecoder *impl_from_frame(IWICBitmapFrameDecode *iface)
169 {
170 return CONTAINING_RECORD(iface, PngDecoder, lpFrameVtbl);
171 }
172
173 static const IWICBitmapFrameDecodeVtbl PngDecoder_FrameVtbl;
174
175 static HRESULT WINAPI PngDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
176 void **ppv)
177 {
178 PngDecoder *This = (PngDecoder*)iface;
179 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
180
181 if (!ppv) return E_INVALIDARG;
182
183 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
184 {
185 *ppv = This;
186 }
187 else
188 {
189 *ppv = NULL;
190 return E_NOINTERFACE;
191 }
192
193 IUnknown_AddRef((IUnknown*)*ppv);
194 return S_OK;
195 }
196
197 static ULONG WINAPI PngDecoder_AddRef(IWICBitmapDecoder *iface)
198 {
199 PngDecoder *This = (PngDecoder*)iface;
200 ULONG ref = InterlockedIncrement(&This->ref);
201
202 TRACE("(%p) refcount=%u\n", iface, ref);
203
204 return ref;
205 }
206
207 static ULONG WINAPI PngDecoder_Release(IWICBitmapDecoder *iface)
208 {
209 PngDecoder *This = (PngDecoder*)iface;
210 ULONG ref = InterlockedDecrement(&This->ref);
211
212 TRACE("(%p) refcount=%u\n", iface, ref);
213
214 if (ref == 0)
215 {
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);
222 }
223
224 return ref;
225 }
226
227 static HRESULT WINAPI PngDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
228 DWORD *pdwCapability)
229 {
230 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
231 return E_NOTIMPL;
232 }
233
234 static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
235 {
236 IStream *stream = ppng_get_io_ptr(png_ptr);
237 HRESULT hr;
238 ULONG bytesread;
239
240 hr = IStream_Read(stream, data, length, &bytesread);
241 if (FAILED(hr) || bytesread != length)
242 {
243 ppng_error(png_ptr, "failed reading data");
244 }
245 }
246
247 static HRESULT WINAPI PngDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
248 WICDecodeOptions cacheOptions)
249 {
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;
262
263 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
264
265 EnterCriticalSection(&This->lock);
266
267 /* initialize libpng */
268 This->png_ptr = ppng_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
269 if (!This->png_ptr)
270 {
271 hr = E_FAIL;
272 goto end;
273 }
274
275 This->info_ptr = ppng_create_info_struct(This->png_ptr);
276 if (!This->info_ptr)
277 {
278 ppng_destroy_read_struct(&This->png_ptr, NULL, NULL);
279 This->png_ptr = NULL;
280 hr = E_FAIL;
281 goto end;
282 }
283
284 This->end_info = ppng_create_info_struct(This->png_ptr);
285 if (!This->info_ptr)
286 {
287 ppng_destroy_read_struct(&This->png_ptr, &This->info_ptr, NULL);
288 This->png_ptr = NULL;
289 hr = E_FAIL;
290 goto end;
291 }
292
293 /* set up setjmp/longjmp error handling */
294 if (setjmp(jmpbuf))
295 {
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;
301 }
302 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
303
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;
308
309 /* set up custom i/o handling */
310 ppng_set_read_fn(This->png_ptr, pIStream, user_read_data);
311
312 /* read the header */
313 ppng_read_info(This->png_ptr, This->info_ptr);
314
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);
318
319 /* check for color-keyed alpha */
320 transparency = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
321
322 if (transparency && color_type != PNG_COLOR_TYPE_PALETTE)
323 {
324 /* expand to RGBA */
325 if (color_type == PNG_COLOR_TYPE_GRAY)
326 {
327 if (bit_depth < 8)
328 {
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;
335 }
336 ppng_set_gray_to_rgb(This->png_ptr);
337 }
338 ppng_set_tRNS_to_alpha(This->png_ptr);
339 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
340 }
341
342 switch (color_type)
343 {
344 case PNG_COLOR_TYPE_GRAY:
345 This->bpp = bit_depth;
346 switch (bit_depth)
347 {
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;
357 }
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)
365 {
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;
375 }
376 break;
377 case PNG_COLOR_TYPE_PALETTE:
378 This->bpp = bit_depth;
379 switch (bit_depth)
380 {
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;
389 }
390 break;
391 case PNG_COLOR_TYPE_RGB:
392 This->bpp = bit_depth * 3;
393 switch (bit_depth)
394 {
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;
404 }
405 break;
406 default:
407 ERR("invalid color type %i\n", color_type);
408 hr = E_FAIL;
409 goto end;
410 }
411
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;
417
418 This->image_bits = HeapAlloc(GetProcessHeap(), 0, image_size);
419 if (!This->image_bits)
420 {
421 hr = E_OUTOFMEMORY;
422 goto end;
423 }
424
425 row_pointers = HeapAlloc(GetProcessHeap(), 0, sizeof(png_bytep)*This->height);
426 if (!row_pointers)
427 {
428 hr = E_OUTOFMEMORY;
429 goto end;
430 }
431
432 for (i=0; i<This->height; i++)
433 row_pointers[i] = This->image_bits + i * This->stride;
434
435 ppng_read_image(This->png_ptr, row_pointers);
436
437 HeapFree(GetProcessHeap(), 0, row_pointers);
438 row_pointers = NULL;
439
440 ppng_read_end(This->png_ptr, This->end_info);
441
442 This->initialized = TRUE;
443
444 end:
445
446 LeaveCriticalSection(&This->lock);
447
448 return hr;
449 }
450
451 static HRESULT WINAPI PngDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
452 GUID *pguidContainerFormat)
453 {
454 memcpy(pguidContainerFormat, &GUID_ContainerFormatPng, sizeof(GUID));
455 return S_OK;
456 }
457
458 static HRESULT WINAPI PngDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
459 IWICBitmapDecoderInfo **ppIDecoderInfo)
460 {
461 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
462 return E_NOTIMPL;
463 }
464
465 static HRESULT WINAPI PngDecoder_CopyPalette(IWICBitmapDecoder *iface,
466 IWICPalette *pIPalette)
467 {
468 FIXME("(%p,%p): stub\n", iface, pIPalette);
469 return E_NOTIMPL;
470 }
471
472 static HRESULT WINAPI PngDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
473 IWICMetadataQueryReader **ppIMetadataQueryReader)
474 {
475 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
476 return E_NOTIMPL;
477 }
478
479 static HRESULT WINAPI PngDecoder_GetPreview(IWICBitmapDecoder *iface,
480 IWICBitmapSource **ppIBitmapSource)
481 {
482 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
483 return E_NOTIMPL;
484 }
485
486 static HRESULT WINAPI PngDecoder_GetColorContexts(IWICBitmapDecoder *iface,
487 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
488 {
489 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
490 return E_NOTIMPL;
491 }
492
493 static HRESULT WINAPI PngDecoder_GetThumbnail(IWICBitmapDecoder *iface,
494 IWICBitmapSource **ppIThumbnail)
495 {
496 TRACE("(%p,%p)\n", iface, ppIThumbnail);
497 return WINCODEC_ERR_CODECNOTHUMBNAIL;
498 }
499
500 static HRESULT WINAPI PngDecoder_GetFrameCount(IWICBitmapDecoder *iface,
501 UINT *pCount)
502 {
503 *pCount = 1;
504 return S_OK;
505 }
506
507 static HRESULT WINAPI PngDecoder_GetFrame(IWICBitmapDecoder *iface,
508 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
509 {
510 PngDecoder *This = (PngDecoder*)iface;
511 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
512
513 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
514
515 if (index != 0) return E_INVALIDARG;
516
517 IWICBitmapDecoder_AddRef(iface);
518
519 *ppIBitmapFrame = (void*)(&This->lpFrameVtbl);
520
521 return S_OK;
522 }
523
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
539 };
540
541 static HRESULT WINAPI PngDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
542 void **ppv)
543 {
544 if (!ppv) return E_INVALIDARG;
545
546 if (IsEqualIID(&IID_IUnknown, iid) ||
547 IsEqualIID(&IID_IWICBitmapSource, iid) ||
548 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
549 {
550 *ppv = iface;
551 }
552 else
553 {
554 *ppv = NULL;
555 return E_NOINTERFACE;
556 }
557
558 IUnknown_AddRef((IUnknown*)*ppv);
559 return S_OK;
560 }
561
562 static ULONG WINAPI PngDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
563 {
564 PngDecoder *This = impl_from_frame(iface);
565 return IUnknown_AddRef((IUnknown*)This);
566 }
567
568 static ULONG WINAPI PngDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
569 {
570 PngDecoder *This = impl_from_frame(iface);
571 return IUnknown_Release((IUnknown*)This);
572 }
573
574 static HRESULT WINAPI PngDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
575 UINT *puiWidth, UINT *puiHeight)
576 {
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;
582 }
583
584 static HRESULT WINAPI PngDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
585 WICPixelFormatGUID *pPixelFormat)
586 {
587 PngDecoder *This = impl_from_frame(iface);
588 TRACE("(%p,%p)\n", iface, pPixelFormat);
589
590 memcpy(pPixelFormat, This->format, sizeof(GUID));
591
592 return S_OK;
593 }
594
595 static HRESULT WINAPI PngDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
596 double *pDpiX, double *pDpiY)
597 {
598 PngDecoder *This = impl_from_frame(iface);
599 png_uint_32 ret, xres, yres;
600 int unit_type;
601
602 EnterCriticalSection(&This->lock);
603
604 ret = ppng_get_pHYs(This->png_ptr, This->info_ptr, &xres, &yres, &unit_type);
605
606 if (ret && unit_type == PNG_RESOLUTION_METER)
607 {
608 *pDpiX = xres * 0.0254;
609 *pDpiY = yres * 0.0254;
610 }
611 else
612 {
613 WARN("no pHYs block present\n");
614 *pDpiX = *pDpiY = 96.0;
615 }
616
617 LeaveCriticalSection(&This->lock);
618
619 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
620
621 return S_OK;
622 }
623
624 static HRESULT WINAPI PngDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
625 IWICPalette *pIPalette)
626 {
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;
637
638 TRACE("(%p,%p)\n", iface, pIPalette);
639
640 EnterCriticalSection(&This->lock);
641
642 ret = ppng_get_PLTE(This->png_ptr, This->info_ptr, &png_palette, &num_palette);
643 if (!ret)
644 {
645 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
646 goto end;
647 }
648
649 if (num_palette > 256)
650 {
651 ERR("palette has %i colors?!\n", num_palette);
652 hr = E_FAIL;
653 goto end;
654 }
655
656 for (i=0; i<num_palette; i++)
657 {
658 palette[i] = (0xff000000|
659 png_palette[i].red << 16|
660 png_palette[i].green << 8|
661 png_palette[i].blue);
662 }
663
664 ret = ppng_get_tRNS(This->png_ptr, This->info_ptr, &trans, &num_trans, &trans_values);
665 if (ret)
666 {
667 for (i=0; i<num_trans; i++)
668 {
669 palette[trans[i]] = 0x00000000;
670 }
671 }
672
673 end:
674
675 LeaveCriticalSection(&This->lock);
676
677 if (SUCCEEDED(hr))
678 hr = IWICPalette_InitializeCustom(pIPalette, palette, num_palette);
679
680 return hr;
681 }
682
683 static HRESULT WINAPI PngDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
684 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
685 {
686 PngDecoder *This = impl_from_frame(iface);
687 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
688
689 return copy_pixels(This->bpp, This->image_bits,
690 This->width, This->height, This->stride,
691 prc, cbStride, cbBufferSize, pbBuffer);
692 }
693
694 static HRESULT WINAPI PngDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
695 IWICMetadataQueryReader **ppIMetadataQueryReader)
696 {
697 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
698 return E_NOTIMPL;
699 }
700
701 static HRESULT WINAPI PngDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
702 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
703 {
704 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
705 return E_NOTIMPL;
706 }
707
708 static HRESULT WINAPI PngDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
709 IWICBitmapSource **ppIThumbnail)
710 {
711 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
712 return E_NOTIMPL;
713 }
714
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
727 };
728
729 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
730 {
731 PngDecoder *This;
732 HRESULT ret;
733
734 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
735
736 *ppv = NULL;
737
738 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
739
740 if (!libpng_handle && !load_libpng())
741 {
742 ERR("Failed reading PNG because unable to find %s\n",SONAME_LIBPNG);
743 return E_FAIL;
744 }
745
746 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngDecoder));
747 if (!This) return E_OUTOFMEMORY;
748
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");
759
760 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
761 IUnknown_Release((IUnknown*)This);
762
763 return ret;
764 }
765
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;
773 };
774
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},
787 };
788
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;
807
808 static inline PngEncoder *encoder_from_frame(IWICBitmapFrameEncode *iface)
809 {
810 return CONTAINING_RECORD(iface, PngEncoder, lpFrameVtbl);
811 }
812
813 static HRESULT WINAPI PngFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
814 void **ppv)
815 {
816 PngEncoder *This = encoder_from_frame(iface);
817 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
818
819 if (!ppv) return E_INVALIDARG;
820
821 if (IsEqualIID(&IID_IUnknown, iid) ||
822 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
823 {
824 *ppv = &This->lpFrameVtbl;
825 }
826 else
827 {
828 *ppv = NULL;
829 return E_NOINTERFACE;
830 }
831
832 IUnknown_AddRef((IUnknown*)*ppv);
833 return S_OK;
834 }
835
836 static ULONG WINAPI PngFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
837 {
838 PngEncoder *This = encoder_from_frame(iface);
839 return IUnknown_AddRef((IUnknown*)This);
840 }
841
842 static ULONG WINAPI PngFrameEncode_Release(IWICBitmapFrameEncode *iface)
843 {
844 PngEncoder *This = encoder_from_frame(iface);
845 return IUnknown_Release((IUnknown*)This);
846 }
847
848 static HRESULT WINAPI PngFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
849 IPropertyBag2 *pIEncoderOptions)
850 {
851 PngEncoder *This = encoder_from_frame(iface);
852 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
853
854 EnterCriticalSection(&This->lock);
855
856 if (This->frame_initialized)
857 {
858 LeaveCriticalSection(&This->lock);
859 return WINCODEC_ERR_WRONGSTATE;
860 }
861
862 This->frame_initialized = TRUE;
863
864 LeaveCriticalSection(&This->lock);
865
866 return S_OK;
867 }
868
869 static HRESULT WINAPI PngFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
870 UINT uiWidth, UINT uiHeight)
871 {
872 PngEncoder *This = encoder_from_frame(iface);
873 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
874
875 EnterCriticalSection(&This->lock);
876
877 if (!This->frame_initialized || This->info_written)
878 {
879 LeaveCriticalSection(&This->lock);
880 return WINCODEC_ERR_WRONGSTATE;
881 }
882
883 This->width = uiWidth;
884 This->height = uiHeight;
885
886 LeaveCriticalSection(&This->lock);
887
888 return S_OK;
889 }
890
891 static HRESULT WINAPI PngFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
892 double dpiX, double dpiY)
893 {
894 PngEncoder *This = encoder_from_frame(iface);
895 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
896
897 EnterCriticalSection(&This->lock);
898
899 if (!This->frame_initialized || This->info_written)
900 {
901 LeaveCriticalSection(&This->lock);
902 return WINCODEC_ERR_WRONGSTATE;
903 }
904
905 This->xres = dpiX;
906 This->yres = dpiY;
907
908 LeaveCriticalSection(&This->lock);
909
910 return S_OK;
911 }
912
913 static HRESULT WINAPI PngFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
914 WICPixelFormatGUID *pPixelFormat)
915 {
916 PngEncoder *This = encoder_from_frame(iface);
917 int i;
918 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
919
920 EnterCriticalSection(&This->lock);
921
922 if (!This->frame_initialized || This->info_written)
923 {
924 LeaveCriticalSection(&This->lock);
925 return WINCODEC_ERR_WRONGSTATE;
926 }
927
928 for (i=0; formats[i].guid; i++)
929 {
930 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
931 break;
932 }
933
934 if (!formats[i].guid) i = 0;
935
936 This->format = &formats[i];
937 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
938
939 LeaveCriticalSection(&This->lock);
940
941 return S_OK;
942 }
943
944 static HRESULT WINAPI PngFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
945 UINT cCount, IWICColorContext **ppIColorContext)
946 {
947 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
948 return E_NOTIMPL;
949 }
950
951 static HRESULT WINAPI PngFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
952 IWICPalette *pIPalette)
953 {
954 FIXME("(%p,%p): stub\n", iface, pIPalette);
955 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
956 }
957
958 static HRESULT WINAPI PngFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
959 IWICBitmapSource *pIThumbnail)
960 {
961 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
962 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
963 }
964
965 static HRESULT WINAPI PngFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
966 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
967 {
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);
973
974 EnterCriticalSection(&This->lock);
975
976 if (!This->frame_initialized || !This->width || !This->height || !This->format)
977 {
978 LeaveCriticalSection(&This->lock);
979 return WINCODEC_ERR_WRONGSTATE;
980 }
981
982 if (lineCount == 0 || lineCount + This->lines_written > This->height)
983 {
984 LeaveCriticalSection(&This->lock);
985 return E_INVALIDARG;
986 }
987
988 /* set up setjmp/longjmp error handling */
989 if (setjmp(jmpbuf))
990 {
991 LeaveCriticalSection(&This->lock);
992 HeapFree(GetProcessHeap(), 0, row_pointers);
993 return E_FAIL;
994 }
995 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
996
997 if (!This->info_written)
998 {
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);
1002
1003 if (This->xres != 0.0 && This->yres != 0.0)
1004 {
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);
1007 }
1008
1009 ppng_write_info(This->png_ptr, This->info_ptr);
1010
1011 if (This->format->remove_filler)
1012 ppng_set_filler(This->png_ptr, 0, PNG_FILLER_AFTER);
1013
1014 if (This->format->swap_rgb)
1015 ppng_set_bgr(This->png_ptr);
1016
1017 This->info_written = TRUE;
1018 }
1019
1020 row_pointers = HeapAlloc(GetProcessHeap(), 0, lineCount * sizeof(png_byte*));
1021 if (!row_pointers)
1022 {
1023 LeaveCriticalSection(&This->lock);
1024 return E_OUTOFMEMORY;
1025 }
1026
1027 for (i=0; i<lineCount; i++)
1028 row_pointers[i] = pbPixels + cbStride * i;
1029
1030 ppng_write_rows(This->png_ptr, row_pointers, lineCount);
1031 This->lines_written += lineCount;
1032
1033 LeaveCriticalSection(&This->lock);
1034
1035 HeapFree(GetProcessHeap(), 0, row_pointers);
1036
1037 return S_OK;
1038 }
1039
1040 static HRESULT WINAPI PngFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1041 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1042 {
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);
1050
1051 if (!This->frame_initialized || !This->width || !This->height)
1052 return WINCODEC_ERR_WRONGSTATE;
1053
1054 if (!This->format)
1055 {
1056 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1057 if (FAILED(hr)) return hr;
1058 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1059 if (FAILED(hr)) return hr;
1060 }
1061
1062 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1063 if (FAILED(hr)) return hr;
1064 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1065 {
1066 /* FIXME: should use WICConvertBitmapSource to convert */
1067 ERR("format %s unsupported\n", debugstr_guid(&guid));
1068 return E_FAIL;
1069 }
1070
1071 if (This->xres == 0.0 || This->yres == 0.0)
1072 {
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;
1078 }
1079
1080 if (!prc)
1081 {
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;
1090 }
1091
1092 if (prc->Width != This->width) return E_INVALIDARG;
1093
1094 stride = (This->format->bpp * This->width + 7)/8;
1095
1096 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1097 if (!pixeldata) return E_OUTOFMEMORY;
1098
1099 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1100 stride*prc->Height, pixeldata);
1101
1102 if (SUCCEEDED(hr))
1103 {
1104 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1105 stride*prc->Height, pixeldata);
1106 }
1107
1108 HeapFree(GetProcessHeap(), 0, pixeldata);
1109
1110 return hr;
1111 }
1112
1113 static HRESULT WINAPI PngFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1114 {
1115 PngEncoder *This = encoder_from_frame(iface);
1116 jmp_buf jmpbuf;
1117 TRACE("(%p)\n", iface);
1118
1119 EnterCriticalSection(&This->lock);
1120
1121 if (!This->info_written || This->lines_written != This->height || This->frame_committed)
1122 {
1123 LeaveCriticalSection(&This->lock);
1124 return WINCODEC_ERR_WRONGSTATE;
1125 }
1126
1127 /* set up setjmp/longjmp error handling */
1128 if (setjmp(jmpbuf))
1129 {
1130 LeaveCriticalSection(&This->lock);
1131 return E_FAIL;
1132 }
1133 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
1134
1135 ppng_write_end(This->png_ptr, This->info_ptr);
1136
1137 This->frame_committed = TRUE;
1138
1139 LeaveCriticalSection(&This->lock);
1140
1141 return S_OK;
1142 }
1143
1144 static HRESULT WINAPI PngFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1145 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1146 {
1147 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1148 return E_NOTIMPL;
1149 }
1150
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
1166 };
1167
1168 static HRESULT WINAPI PngEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1169 void **ppv)
1170 {
1171 PngEncoder *This = (PngEncoder*)iface;
1172 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1173
1174 if (!ppv) return E_INVALIDARG;
1175
1176 if (IsEqualIID(&IID_IUnknown, iid) ||
1177 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1178 {
1179 *ppv = This;
1180 }
1181 else
1182 {
1183 *ppv = NULL;
1184 return E_NOINTERFACE;
1185 }
1186
1187 IUnknown_AddRef((IUnknown*)*ppv);
1188 return S_OK;
1189 }
1190
1191 static ULONG WINAPI PngEncoder_AddRef(IWICBitmapEncoder *iface)
1192 {
1193 PngEncoder *This = (PngEncoder*)iface;
1194 ULONG ref = InterlockedIncrement(&This->ref);
1195
1196 TRACE("(%p) refcount=%u\n", iface, ref);
1197
1198 return ref;
1199 }
1200
1201 static ULONG WINAPI PngEncoder_Release(IWICBitmapEncoder *iface)
1202 {
1203 PngEncoder *This = (PngEncoder*)iface;
1204 ULONG ref = InterlockedDecrement(&This->ref);
1205
1206 TRACE("(%p) refcount=%u\n", iface, ref);
1207
1208 if (ref == 0)
1209 {
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);
1217 }
1218
1219 return ref;
1220 }
1221
1222 static void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1223 {
1224 PngEncoder *This = ppng_get_io_ptr(png_ptr);
1225 HRESULT hr;
1226 ULONG byteswritten;
1227
1228 hr = IStream_Write(This->stream, data, length, &byteswritten);
1229 if (FAILED(hr) || byteswritten != length)
1230 {
1231 ppng_error(png_ptr, "failed writing data");
1232 }
1233 }
1234
1235 static void user_flush(png_structp png_ptr)
1236 {
1237 }
1238
1239 static HRESULT WINAPI PngEncoder_Initialize(IWICBitmapEncoder *iface,
1240 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1241 {
1242 PngEncoder *This = (PngEncoder*)iface;
1243 jmp_buf jmpbuf;
1244
1245 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1246
1247 EnterCriticalSection(&This->lock);
1248
1249 if (This->png_ptr)
1250 {
1251 LeaveCriticalSection(&This->lock);
1252 return WINCODEC_ERR_WRONGSTATE;
1253 }
1254
1255 /* initialize libpng */
1256 This->png_ptr = ppng_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1257 if (!This->png_ptr)
1258 {
1259 LeaveCriticalSection(&This->lock);
1260 return E_FAIL;
1261 }
1262
1263 This->info_ptr = ppng_create_info_struct(This->png_ptr);
1264 if (!This->info_ptr)
1265 {
1266 ppng_destroy_write_struct(&This->png_ptr, NULL);
1267 This->png_ptr = NULL;
1268 LeaveCriticalSection(&This->lock);
1269 return E_FAIL;
1270 }
1271
1272 IStream_AddRef(pIStream);
1273 This->stream = pIStream;
1274
1275 /* set up setjmp/longjmp error handling */
1276 if (setjmp(jmpbuf))
1277 {
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;
1284 }
1285 ppng_set_error_fn(This->png_ptr, &jmpbuf, user_error_fn, user_warning_fn);
1286
1287 /* set up custom i/o handling */
1288 ppng_set_write_fn(This->png_ptr, This, user_write_data, user_flush);
1289
1290 LeaveCriticalSection(&This->lock);
1291
1292 return S_OK;
1293 }
1294
1295 static HRESULT WINAPI PngEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1296 GUID *pguidContainerFormat)
1297 {
1298 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1299 return E_NOTIMPL;
1300 }
1301
1302 static HRESULT WINAPI PngEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1303 IWICBitmapEncoderInfo **ppIEncoderInfo)
1304 {
1305 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1306 return E_NOTIMPL;
1307 }
1308
1309 static HRESULT WINAPI PngEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1310 UINT cCount, IWICColorContext **ppIColorContext)
1311 {
1312 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1313 return E_NOTIMPL;
1314 }
1315
1316 static HRESULT WINAPI PngEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1317 {
1318 TRACE("(%p,%p)\n", iface, pIPalette);
1319 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1320 }
1321
1322 static HRESULT WINAPI PngEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1323 {
1324 TRACE("(%p,%p)\n", iface, pIThumbnail);
1325 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1326 }
1327
1328 static HRESULT WINAPI PngEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1329 {
1330 TRACE("(%p,%p)\n", iface, pIPreview);
1331 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1332 }
1333
1334 static HRESULT WINAPI PngEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1335 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1336 {
1337 PngEncoder *This = (PngEncoder*)iface;
1338 HRESULT hr;
1339 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1340
1341 EnterCriticalSection(&This->lock);
1342
1343 if (This->frame_count != 0)
1344 {
1345 LeaveCriticalSection(&This->lock);
1346 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1347 }
1348
1349 if (!This->stream)
1350 {
1351 LeaveCriticalSection(&This->lock);
1352 return WINCODEC_ERR_NOTINITIALIZED;
1353 }
1354
1355 hr = CreatePropertyBag2(ppIEncoderOptions);
1356 if (FAILED(hr))
1357 {
1358 LeaveCriticalSection(&This->lock);
1359 return hr;
1360 }
1361
1362 This->frame_count = 1;
1363
1364 LeaveCriticalSection(&This->lock);
1365
1366 IWICBitmapEncoder_AddRef(iface);
1367 *ppIFrameEncode = (IWICBitmapFrameEncode*)&This->lpFrameVtbl;
1368
1369 return S_OK;
1370 }
1371
1372 static HRESULT WINAPI PngEncoder_Commit(IWICBitmapEncoder *iface)
1373 {
1374 PngEncoder *This = (PngEncoder*)iface;
1375 TRACE("(%p)\n", iface);
1376
1377 EnterCriticalSection(&This->lock);
1378
1379 if (!This->frame_committed || This->committed)
1380 {
1381 LeaveCriticalSection(&This->lock);
1382 return WINCODEC_ERR_WRONGSTATE;
1383 }
1384
1385 This->committed = TRUE;
1386
1387 EnterCriticalSection(&This->lock);
1388
1389 return S_OK;
1390 }
1391
1392 static HRESULT WINAPI PngEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1393 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1394 {
1395 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1396 return E_NOTIMPL;
1397 }
1398
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
1413 };
1414
1415 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1416 {
1417 PngEncoder *This;
1418 HRESULT ret;
1419
1420 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1421
1422 *ppv = NULL;
1423
1424 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1425
1426 if (!libpng_handle && !load_libpng())
1427 {
1428 ERR("Failed writing PNG because unable to find %s\n",SONAME_LIBPNG);
1429 return E_FAIL;
1430 }
1431
1432 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PngEncoder));
1433 if (!This) return E_OUTOFMEMORY;
1434
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");
1454
1455 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1456 IUnknown_Release((IUnknown*)This);
1457
1458 return ret;
1459 }
1460
1461 #else /* !HAVE_PNG_H */
1462
1463 HRESULT PngDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1464 {
1465 ERR("Trying to load PNG picture, but PNG support is not compiled in.\n");
1466 return E_FAIL;
1467 }
1468
1469 HRESULT PngEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1470 {
1471 ERR("Trying to save PNG picture, but PNG support is not compiled in.\n");
1472 return E_FAIL;
1473 }
1474
1475 #endif