]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpng.cpp
ambiguous operator[] access
[wxWidgets.git] / src / common / imagpng.cpp
CommitLineData
e9c4b1a2 1/////////////////////////////////////////////////////////////////////////////
27bb2b7c 2// Name: src/common/imagepng.cpp
e9c4b1a2
JS
3// Purpose: wxImage PNG handler
4// Author: Robert Roebling
5// RCS-ID: $Id$
6// Copyright: (c) Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
27bb2b7c
VZ
10// ============================================================================
11// declarations
12// ============================================================================
13
14f355c2 14#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
8f493002
VS
15#pragma implementation "imagpng.h"
16#endif
e9c4b1a2 17
27bb2b7c
VZ
18// ----------------------------------------------------------------------------
19// headers
20// ----------------------------------------------------------------------------
21
e9c4b1a2
JS
22// For compilers that support precompilation, includes "wx.h".
23#include "wx/wxprec.h"
24
25#ifdef __BORLANDC__
ce4169a4
RR
26 #pragma hdrstop
27#endif
28
29#ifndef WX_PRECOMP
30 #include "wx/defs.h"
e9c4b1a2
JS
31#endif
32
c96ea657 33#if wxUSE_IMAGE && wxUSE_LIBPNG
ce4169a4 34
8f493002 35#include "wx/imagpng.h"
e9c4b1a2
JS
36#include "wx/bitmap.h"
37#include "wx/debug.h"
38#include "wx/log.h"
39#include "wx/app.h"
ce4169a4 40#include "png.h"
e9c4b1a2
JS
41#include "wx/filefn.h"
42#include "wx/wfstream.h"
43#include "wx/intl.h"
44#include "wx/module.h"
45
46// For memcpy
47#include <string.h>
48
49#ifdef __SALFORDC__
50#ifdef FAR
51#undef FAR
52#endif
53#endif
54
27bb2b7c
VZ
55// ----------------------------------------------------------------------------
56// constants
57// ----------------------------------------------------------------------------
58
59// image can not have any transparent pixels at all, have only 100% opaque
60// and/or 100% transparent pixels in which case a simple mask is enough to
61// store this information in wxImage or have a real alpha channel in which case
62// we need to have it in wxImage as well
63enum Transparency
64{
65 Transparency_None,
66 Transparency_Mask,
67 Transparency_Alpha
68};
69
70// ----------------------------------------------------------------------------
71// local functions
72// ----------------------------------------------------------------------------
73
74// return the kind of transparency needed for this image assuming that it does
75// have transparent pixels, i.e. either Transparency_Alpha or Transparency_Mask
76static Transparency
36308e0e 77CheckTransparency(unsigned char **lines,
f7155274
VZ
78 png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h,
79 size_t numColBytes);
27bb2b7c
VZ
80
81// init the alpha channel for the image and fill it with 1s up to (x, y)
82static unsigned char *InitAlpha(wxImage *image, png_uint_32 x, png_uint_32 y);
83
84// find a free colour for the mask in the PNG data array
85static void
86FindMaskColour(unsigned char **lines, png_uint_32 width, png_uint_32 height,
87 unsigned char& rMask, unsigned char& gMask, unsigned char& bMask);
e9c4b1a2 88
c9fcf581
VZ
89// is the pixel with this value of alpha a fully opaque one?
90static inline
91bool IsOpaque(unsigned char a)
92{
93 return a == 0xff;
94}
95
96// is the pixel with this value of alpha a fully transparent one?
97static inline
98bool IsTransparent(unsigned char a)
99{
100 return !a;
101}
102
27bb2b7c
VZ
103// ============================================================================
104// wxPNGHandler implementation
105// ============================================================================
e9c4b1a2 106
e9c4b1a2 107IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler)
e9c4b1a2 108
e30285ab 109#if wxUSE_STREAMS
9ab6ee85 110
0729bd19 111#ifndef PNGLINKAGEMODE
30e0e251
VZ
112 #ifdef __WATCOMC__
113 // we need an explicit cdecl for Watcom, at least according to
114 //
115 // http://sf.net/tracker/index.php?func=detail&aid=651492&group_id=9863&atid=109863
116 //
117 // more testing is needed for this however, please remove this comment
118 // if you can confirm that my fix works with Watcom 11
119 #define PNGLINKAGEMODE cdecl
120 #else
121 #define PNGLINKAGEMODE LINKAGEMODE
122 #endif
717b9bf2
DW
123#endif
124
bdffd806
VS
125
126// VS: wxPNGInfoStruct declared below is a hack that needs some explanation.
27bb2b7c
VZ
127// First, let me describe what's the problem: libpng uses jmp_buf in
128// its png_struct structure. Unfortunately, this structure is
129// compiler-specific and may vary in size, so if you use libpng compiled
bdffd806 130// as DLL with another compiler than the main executable, it may not work
27bb2b7c 131// (this is for example the case with wxMGL port and SciTech MGL library
bdffd806 132// that provides custom runtime-loadable libpng implementation with jmpbuf
27bb2b7c 133// disabled altogether). Luckily, it is still possible to use setjmp() &
bdffd806
VS
134// longjmp() as long as the structure is not part of png_struct.
135//
136// Sadly, there's no clean way to attach user-defined data to png_struct.
137// There is only one customizable place, png_struct.io_ptr, which is meant
27bb2b7c 138// only for I/O routines and is set with png_set_read_fn or
bdffd806
VS
139// png_set_write_fn. The hacky part is that we use io_ptr to store
140// a pointer to wxPNGInfoStruct that holds I/O structures _and_ jmp_buf.
141
142struct wxPNGInfoStruct
143{
144 jmp_buf jmpbuf;
145 bool verbose;
27bb2b7c 146
bdffd806
VS
147 union
148 {
149 wxInputStream *in;
150 wxOutputStream *out;
151 } stream;
152};
153
154#define WX_PNG_INFO(png_ptr) ((wxPNGInfoStruct*)png_get_io_ptr(png_ptr))
155
27bb2b7c
VZ
156// ----------------------------------------------------------------------------
157// helper functions
158// ----------------------------------------------------------------------------
bdffd806 159
90350682
VZ
160extern "C"
161{
162
163void PNGLINKAGEMODE _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length )
e9c4b1a2 164{
bdffd806 165 WX_PNG_INFO(png_ptr)->stream.in->Read(data, length);
e9c4b1a2
JS
166}
167
90350682 168void PNGLINKAGEMODE _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length )
e9c4b1a2 169{
bdffd806 170 WX_PNG_INFO(png_ptr)->stream.out->Write(data, length);
e9c4b1a2
JS
171}
172
deb2fec0
SB
173// from pngerror.c
174// so that the libpng doesn't send anything on stderr
175void
bdffd806 176PNGLINKAGEMODE wx_png_error(png_structp png_ptr, png_const_charp message)
deb2fec0 177{
bdffd806 178 wxPNGInfoStruct *info = WX_PNG_INFO(png_ptr);
2b5f62a0
VZ
179 if (info->verbose)
180 wxLogError( wxString::FromAscii(message) );
bdffd806 181
deb2fec0 182#ifdef USE_FAR_KEYWORD
bdffd806
VS
183 {
184 jmp_buf jmpbuf;
185 png_memcpy(jmpbuf,info->jmpbuf,sizeof(jmp_buf));
186 longjmp(jmpbuf, 1);
187 }
deb2fec0 188#else
bdffd806 189 longjmp(info->jmpbuf, 1);
deb2fec0
SB
190#endif
191}
192
193void
bdffd806 194PNGLINKAGEMODE wx_png_warning(png_structp png_ptr, png_const_charp message)
deb2fec0 195{
bdffd806 196 wxPNGInfoStruct *info = WX_PNG_INFO(png_ptr);
2b5f62a0
VZ
197 if (info->verbose)
198 wxLogWarning( wxString::FromAscii(message) );
deb2fec0
SB
199}
200
90350682
VZ
201} // extern "C"
202
27bb2b7c
VZ
203// ----------------------------------------------------------------------------
204// LoadFile() helpers
205// ----------------------------------------------------------------------------
206
d26d9754 207// determine the kind of transparency we need for this image: if the only alpha
c9fcf581
VZ
208// values it has are 0 (transparent) and 0xff (opaque) then we can simply
209// create a mask for it, we should be ok with a simple mask but otherwise we
210// need a full blown alpha channel in wxImage
d26d9754
VZ
211//
212// parameters:
36308e0e 213// lines raw PNG data
d26d9754
VZ
214// x, y starting position
215// w, h size of the image
216// numColBytes number of colour bytes (1 for grey scale, 3 for RGB)
217// (NB: alpha always follows the colour bytes)
27bb2b7c 218Transparency
36308e0e 219CheckTransparency(unsigned char **lines,
d26d9754
VZ
220 png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h,
221 size_t numColBytes)
27bb2b7c 222{
d26d9754
VZ
223 // suppose that a mask will suffice and check all the remaining alpha
224 // values to see if it does
36308e0e 225 for ( ; y < h; y++ )
27bb2b7c 226 {
e45cc235
RD
227 // each pixel is numColBytes+1 bytes, offset into the current line by
228 // the current x position
229 unsigned const char *ptr = lines[y] + (x * (numColBytes + 1));
36308e0e 230
c9fcf581 231 for ( png_uint_32 x2 = x; x2 < w; x2++ )
27bb2b7c 232 {
c9fcf581 233 // skip the grey or colour byte(s)
36308e0e 234 ptr += numColBytes;
27bb2b7c 235
36308e0e 236 unsigned char a2 = *ptr++;
c9fcf581
VZ
237
238 if ( !IsTransparent(a2) && !IsOpaque(a2) )
27bb2b7c 239 {
d26d9754
VZ
240 // not fully opaque nor fully transparent, hence need alpha
241 return Transparency_Alpha;
27bb2b7c 242 }
27bb2b7c 243 }
c9fcf581
VZ
244
245 // during the next loop iteration check all the pixels in the row
246 x = 0;
27bb2b7c
VZ
247 }
248
d26d9754
VZ
249 // mask will be enough
250 return Transparency_Mask;
27bb2b7c
VZ
251}
252
253unsigned char *InitAlpha(wxImage *image, png_uint_32 x, png_uint_32 y)
254{
255 // create alpha channel
256 image->SetAlpha();
257
258 unsigned char *alpha = image->GetAlpha();
259
260 // set alpha for the pixels we had so far
c9fcf581
VZ
261 png_uint_32 end = y * image->GetWidth() + x;
262 for ( png_uint_32 i = 0; i < end; i++ )
27bb2b7c 263 {
c9fcf581
VZ
264 // all the previous pixels were opaque
265 *alpha++ = 0xff;
27bb2b7c
VZ
266 }
267
268 return alpha;
269}
270
271void
272FindMaskColour(unsigned char **lines, png_uint_32 width, png_uint_32 height,
273 unsigned char& rMask, unsigned char& gMask, unsigned char& bMask)
274{
275 // choosing the colour for the mask is more
276 // difficult: we need to iterate over the entire
277 // image for this in order to choose an unused
278 // colour (this is not very efficient but what else
279 // can we do?)
280 wxImageHistogram h;
281 unsigned nentries = 0;
282 unsigned char r2, g2, b2;
283 for ( png_uint_32 y2 = 0; y2 < height; y2++ )
284 {
285 const unsigned char *p = lines[y2];
286 for ( png_uint_32 x2 = 0; x2 < width; x2++ )
287 {
288 r2 = *p++;
289 g2 = *p++;
290 b2 = *p++;
291
292 wxImageHistogramEntry&
293 entry = h[wxImageHistogram:: MakeKey(r2, g2, b2)];
294
295 if ( entry.value++ == 0 )
296 entry.index = nentries++;
297 }
298 }
299
300 if ( !h.FindFirstUnusedColour(&rMask, &gMask, &bMask) )
301 {
302 wxLogWarning(_("Too many colours in PNG, the image may be slightly blurred."));
303
304 // use a fixed mask colour and we'll fudge
305 // the real pixels with this colour (see
306 // below)
307 rMask = 0xfe;
308 gMask = 0;
309 bMask = 0xff;
310 }
311}
312
313// ----------------------------------------------------------------------------
314// reading PNGs
315// ----------------------------------------------------------------------------
316
317bool wxPNGHandler::DoCanRead( wxInputStream& stream )
318{
319 unsigned char hdr[4];
320
321 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
322 return FALSE;
323
324 return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0;
325}
326
327// convert data from RGB to wxImage format
328static
329void CopyDataFromPNG(wxImage *image,
330 unsigned char **lines,
331 png_uint_32 width,
332 png_uint_32 height,
333 int color_type)
334{
335 Transparency transparency = Transparency_None;
336
337 // only non NULL if transparency == Transparency_Alpha
338 unsigned char *alpha = NULL;
339
340 // RGB of the mask colour if transparency == Transparency_Mask
341 // (but init them anyhow to avoid compiler warnings)
342 unsigned char rMask = 0,
343 gMask = 0,
344 bMask = 0;
345
346 unsigned char *ptrDst = image->GetData();
347 if ( !(color_type & PNG_COLOR_MASK_COLOR) )
348 {
349 // grey image: GAGAGA... where G == grey component and A == alpha
350 for ( png_uint_32 y = 0; y < height; y++ )
351 {
352 const unsigned char *ptrSrc = lines[y];
353 for ( png_uint_32 x = 0; x < width; x++ )
354 {
355 unsigned char g = *ptrSrc++;
356 unsigned char a = *ptrSrc++;
357
358 // the first time we encounter a transparent pixel we must
359 // decide about what to do about them
c9fcf581 360 if ( !IsOpaque(a) && transparency == Transparency_None )
27bb2b7c
VZ
361 {
362 // we'll need at least the mask for this image and
363 // maybe even full alpha channel info: the former is
364 // only enough if we have alpha values of 0 and 0xff
365 // only, otherwisewe need the latter
d26d9754
VZ
366 transparency = CheckTransparency
367 (
36308e0e 368 lines,
d26d9754
VZ
369 x, y,
370 width, height,
371 1
372 );
27bb2b7c
VZ
373
374 if ( transparency == Transparency_Mask )
375 {
376 // let's choose this colour for the mask: this is
377 // not a problem here as all the other pixels are
378 // grey, i.e. R == G == B which is not the case for
379 // this one so no confusion is possible
380 rMask = 0xff;
381 gMask = 0;
382 bMask = 0xff;
383 }
384 else // transparency == Transparency_Alpha
385 {
386 alpha = InitAlpha(image, x, y);
387 }
388 }
389
390 switch ( transparency )
391 {
c9fcf581
VZ
392 case Transparency_Mask:
393 if ( IsTransparent(a) )
394 {
395 *ptrDst++ = rMask;
396 *ptrDst++ = bMask;
397 *ptrDst++ = gMask;
398 break;
399 }
400 // else: !transparent
401
402 // must be opaque then as otherwise we shouldn't be
403 // using the mask at all
404 wxASSERT_MSG( IsOpaque(a), _T("logic error") );
405
406 // fall through
407
27bb2b7c 408 case Transparency_Alpha:
c9fcf581
VZ
409 if ( alpha )
410 *alpha++ = a;
27bb2b7c
VZ
411 // fall through
412
413 case Transparency_None:
414 *ptrDst++ = g;
415 *ptrDst++ = g;
416 *ptrDst++ = g;
417 break;
27bb2b7c
VZ
418 }
419 }
420 }
421 }
422 else // colour image: RGBRGB...
423 {
424 for ( png_uint_32 y = 0; y < height; y++ )
425 {
426 const unsigned char *ptrSrc = lines[y];
427 for ( png_uint_32 x = 0; x < width; x++ )
428 {
429 unsigned char r = *ptrSrc++;
430 unsigned char g = *ptrSrc++;
431 unsigned char b = *ptrSrc++;
432 unsigned char a = *ptrSrc++;
433
434 // the logic here is the same as for the grey case except
435 // where noted
c9fcf581 436 if ( !IsOpaque(a) && transparency == Transparency_None )
27bb2b7c 437 {
d26d9754
VZ
438 transparency = CheckTransparency
439 (
36308e0e 440 lines,
d26d9754
VZ
441 x, y,
442 width, height,
443 3
444 );
27bb2b7c
VZ
445
446 if ( transparency == Transparency_Mask )
447 {
448 FindMaskColour(lines, width, height,
449 rMask, gMask, bMask);
450 }
451 else // transparency == Transparency_Alpha
452 {
453 alpha = InitAlpha(image, x, y);
454 }
455
456 }
457
458 switch ( transparency )
459 {
c9fcf581
VZ
460 case Transparency_Mask:
461 if ( IsTransparent(a) )
462 {
c9fcf581
VZ
463 *ptrDst++ = rMask;
464 *ptrDst++ = bMask;
465 *ptrDst++ = gMask;
466 break;
467 }
999836aa
VZ
468 else // !transparent
469 {
470 // must be opaque then as otherwise we shouldn't be
471 // using the mask at all
472 wxASSERT_MSG( IsOpaque(a), _T("logic error") );
473
474 // if we couldn't find a unique colour for the
475 // mask, we can have real pixels with the same
476 // value as the mask and it's better to slightly
477 // change their colour than to make them
478 // transparent
479 if ( r == rMask && g == gMask && b == bMask )
480 {
481 r++;
482 }
483 }
c9fcf581
VZ
484
485 // fall through
486
27bb2b7c 487 case Transparency_Alpha:
c9fcf581
VZ
488 if ( alpha )
489 *alpha++ = a;
27bb2b7c
VZ
490 // fall through
491
492 case Transparency_None:
493 *ptrDst++ = r;
494 *ptrDst++ = g;
495 *ptrDst++ = b;
496 break;
27bb2b7c
VZ
497 }
498 }
499 }
500 }
501
502 if ( transparency == Transparency_Mask )
503 {
504 image->SetMaskColour(rMask, gMask, bMask);
505 }
506}
507
3ca6a5f0
BP
508// temporarily disable the warning C4611 (interaction between '_setjmp' and
509// C++ object destruction is non-portable) - I don't see any dtors here
510#ifdef __VISUALC__
511 #pragma warning(disable:4611)
512#endif /* VC++ */
513
27bb2b7c
VZ
514bool
515wxPNGHandler::LoadFile(wxImage *image,
516 wxInputStream& stream,
517 bool verbose,
518 int WXUNUSED(index))
e9c4b1a2
JS
519{
520 // VZ: as this function uses setjmp() the only fool proof error handling
521 // method is to use goto (setjmp is not really C++ dtors friendly...)
717b9bf2 522
27bb2b7c
VZ
523 unsigned char **lines = NULL;
524 png_infop info_ptr = (png_infop) NULL;
bdffd806
VS
525 wxPNGInfoStruct wxinfo;
526
527 wxinfo.verbose = verbose;
528 wxinfo.stream.in = &stream;
717b9bf2 529
e9c4b1a2 530 image->Destroy();
717b9bf2 531
e9c4b1a2
JS
532 png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
533 (voidp) NULL,
534 (png_error_ptr) NULL,
535 (png_error_ptr) NULL );
536 if (!png_ptr)
27bb2b7c 537 goto error;
717b9bf2 538
bdffd806
VS
539 png_set_error_fn(png_ptr, (png_voidp)NULL, wx_png_error, wx_png_warning);
540
541 // NB: please see the comment near wxPNGInfoStruct declaration for
542 // explanation why this line is mandatory
543 png_set_read_fn( png_ptr, &wxinfo, _PNG_stream_reader);
deb2fec0 544
e9c4b1a2
JS
545 info_ptr = png_create_info_struct( png_ptr );
546 if (!info_ptr)
27bb2b7c 547 goto error;
717b9bf2 548
bdffd806 549 if (setjmp(wxinfo.jmpbuf))
27bb2b7c 550 goto error;
717b9bf2 551
27bb2b7c
VZ
552 png_uint_32 i, width, height;
553 int bit_depth, color_type, interlace_type;
717b9bf2 554
e9c4b1a2
JS
555 png_read_info( png_ptr, info_ptr );
556 png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL );
717b9bf2 557
e9c4b1a2
JS
558 if (color_type == PNG_COLOR_TYPE_PALETTE)
559 png_set_expand( png_ptr );
717b9bf2 560
2b5f62a0
VZ
561 // Fix for Bug [ 439207 ] Monochrome PNG images come up black
562 if (bit_depth < 8)
563 png_set_expand( png_ptr );
564
e9c4b1a2
JS
565 png_set_strip_16( png_ptr );
566 png_set_packing( png_ptr );
567 if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
568 png_set_expand( png_ptr );
569 png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
717b9bf2 570
479cd5de 571 image->Create( (int)width, (int)height );
717b9bf2 572
e9c4b1a2 573 if (!image->Ok())
27bb2b7c 574 goto error;
717b9bf2 575
479cd5de 576 lines = (unsigned char **)malloc( (size_t)(height * sizeof(unsigned char *)) );
27bb2b7c
VZ
577 if ( !lines )
578 goto error;
717b9bf2 579
e9c4b1a2
JS
580 for (i = 0; i < height; i++)
581 {
479cd5de 582 if ((lines[i] = (unsigned char *)malloc( (size_t)(width * (sizeof(unsigned char) * 4)))) == NULL)
e9c4b1a2
JS
583 {
584 for ( unsigned int n = 0; n < i; n++ )
585 free( lines[n] );
586 goto error;
587 }
588 }
717b9bf2 589
27bb2b7c
VZ
590 png_read_image( png_ptr, lines );
591 png_read_end( png_ptr, info_ptr );
592 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
717b9bf2 593
27bb2b7c
VZ
594 // loaded successfully, now init wxImage with this data
595 CopyDataFromPNG(image, lines, width, height, color_type);
717b9bf2 596
27bb2b7c
VZ
597 for ( i = 0; i < height; i++ )
598 free( lines[i] );
599 free( lines );
717b9bf2 600
e9c4b1a2 601 return TRUE;
95ee0ac8 602
27bb2b7c 603error:
58c837a4
RR
604 if (verbose)
605 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
717b9bf2 606
e9c4b1a2
JS
607 if ( image->Ok() )
608 {
609 image->Destroy();
610 }
717b9bf2 611
e9c4b1a2
JS
612 if ( lines )
613 {
614 free( lines );
615 }
717b9bf2 616
e9c4b1a2
JS
617 if ( png_ptr )
618 {
619 if ( info_ptr )
620 {
621 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
622 free(info_ptr);
623 }
624 else
625 png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
626 }
627 return FALSE;
628}
629
27bb2b7c
VZ
630// ----------------------------------------------------------------------------
631// writing PNGs
632// ----------------------------------------------------------------------------
633
deb2fec0 634bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2 635{
bdffd806 636 wxPNGInfoStruct wxinfo;
717b9bf2 637
bdffd806
VS
638 wxinfo.verbose = verbose;
639 wxinfo.stream.out = &stream;
deb2fec0 640
bdffd806
VS
641 png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
642 if (!png_ptr)
643 {
644 if (verbose)
645 wxLogError(_("Couldn't save PNG image."));
646 return FALSE;
647 }
717b9bf2 648
bdffd806 649 png_set_error_fn(png_ptr, (png_voidp)NULL, wx_png_error, wx_png_warning);
717b9bf2 650
bdffd806
VS
651 png_infop info_ptr = png_create_info_struct(png_ptr);
652 if (info_ptr == NULL)
653 {
654 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
655 if (verbose)
656 wxLogError(_("Couldn't save PNG image."));
657 return FALSE;
658 }
717b9bf2 659
bdffd806
VS
660 if (setjmp(wxinfo.jmpbuf))
661 {
662 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
663 if (verbose)
664 wxLogError(_("Couldn't save PNG image."));
665 return FALSE;
666 }
717b9bf2 667
bdffd806
VS
668 // NB: please see the comment near wxPNGInfoStruct declaration for
669 // explanation why this line is mandatory
670 png_set_write_fn( png_ptr, &wxinfo, _PNG_stream_writer, NULL);
671
672 png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8,
673 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
674 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
675
676 png_color_8 sig_bit;
677 sig_bit.red = 8;
678 sig_bit.green = 8;
679 sig_bit.blue = 8;
680 sig_bit.alpha = 8;
681 png_set_sBIT( png_ptr, info_ptr, &sig_bit );
682 png_write_info( png_ptr, info_ptr );
683 png_set_shift( png_ptr, &sig_bit );
684 png_set_packing( png_ptr );
717b9bf2 685
bdffd806
VS
686 unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
687 if (!data)
688 {
689 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
690 return FALSE;
691 }
717b9bf2 692
bdffd806
VS
693 for (int y = 0; y < image->GetHeight(); y++)
694 {
695 unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
696 for (int x = 0; x < image->GetWidth(); x++)
e9c4b1a2 697 {
bdffd806
VS
698 data[(x << 2) + 0] = *ptr++;
699 data[(x << 2) + 1] = *ptr++;
700 data[(x << 2) + 2] = *ptr++;
701 if (( !image->HasMask() ) || \
702 (data[(x << 2) + 0] != image->GetMaskRed()) || \
703 (data[(x << 2) + 1] != image->GetMaskGreen()) || \
704 (data[(x << 2) + 2] != image->GetMaskBlue()))
e9c4b1a2 705 {
bdffd806
VS
706 data[(x << 2) + 3] = 255;
707 }
708 else
709 {
710 data[(x << 2) + 3] = 0;
e9c4b1a2 711 }
e9c4b1a2 712 }
bdffd806
VS
713 png_bytep row_ptr = data;
714 png_write_rows( png_ptr, &row_ptr, 1 );
e9c4b1a2 715 }
bdffd806
VS
716
717 free(data);
718 png_write_end( png_ptr, info_ptr );
719 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
720
e9c4b1a2
JS
721 return TRUE;
722}
e9c4b1a2 723
3ca6a5f0
BP
724#ifdef __VISUALC__
725 #pragma warning(default:4611)
726#endif /* VC++ */
727
9ab6ee85 728#endif // wxUSE_STREAMS
e9c4b1a2 729
9ab6ee85 730#endif // wxUSE_LIBPNG