]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpng.cpp
Document wxTE_MULTILINE support in wxTextEntryDialog.
[wxWidgets.git] / src / common / imagpng.cpp
CommitLineData
e9c4b1a2 1/////////////////////////////////////////////////////////////////////////////
f172cb82 2// Name: src/common/imagpng.cpp
e9c4b1a2
JS
3// Purpose: wxImage PNG handler
4// Author: Robert Roebling
5// RCS-ID: $Id$
6// Copyright: (c) Robert Roebling
65571936 7// Licence: wxWindows licence
e9c4b1a2
JS
8/////////////////////////////////////////////////////////////////////////////
9
27bb2b7c
VZ
10// ============================================================================
11// declarations
12// ============================================================================
13
27bb2b7c
VZ
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
e9c4b1a2
JS
18// For compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
8898456d 22 #pragma hdrstop
ce4169a4
RR
23#endif
24
8898456d
WS
25#if wxUSE_IMAGE && wxUSE_LIBPNG
26
0bca0373 27#include "wx/imagpng.h"
ccec9093 28#include "wx/versioninfo.h"
0bca0373 29
ce4169a4 30#ifndef WX_PRECOMP
8898456d 31 #include "wx/log.h"
c3b10b44
PC
32 #include "wx/intl.h"
33 #include "wx/palette.h"
34 #include "wx/stream.h"
e9c4b1a2
JS
35#endif
36
a37e7424 37#include "png.h"
e9c4b1a2
JS
38
39// For memcpy
40#include <string.h>
41
27bb2b7c
VZ
42// ----------------------------------------------------------------------------
43// constants
44// ----------------------------------------------------------------------------
45
46// image can not have any transparent pixels at all, have only 100% opaque
47// and/or 100% transparent pixels in which case a simple mask is enough to
48// store this information in wxImage or have a real alpha channel in which case
49// we need to have it in wxImage as well
50enum Transparency
51{
52 Transparency_None,
53 Transparency_Mask,
54 Transparency_Alpha
55};
56
57// ----------------------------------------------------------------------------
58// local functions
59// ----------------------------------------------------------------------------
60
61// return the kind of transparency needed for this image assuming that it does
62// have transparent pixels, i.e. either Transparency_Alpha or Transparency_Mask
63static Transparency
36308e0e 64CheckTransparency(unsigned char **lines,
f7155274
VZ
65 png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h,
66 size_t numColBytes);
27bb2b7c
VZ
67
68// init the alpha channel for the image and fill it with 1s up to (x, y)
69static unsigned char *InitAlpha(wxImage *image, png_uint_32 x, png_uint_32 y);
70
71// find a free colour for the mask in the PNG data array
72static void
73FindMaskColour(unsigned char **lines, png_uint_32 width, png_uint_32 height,
74 unsigned char& rMask, unsigned char& gMask, unsigned char& bMask);
e9c4b1a2 75
c9fcf581
VZ
76// is the pixel with this value of alpha a fully opaque one?
77static inline
78bool IsOpaque(unsigned char a)
79{
80 return a == 0xff;
81}
82
83// is the pixel with this value of alpha a fully transparent one?
84static inline
85bool IsTransparent(unsigned char a)
86{
87 return !a;
88}
89
27bb2b7c
VZ
90// ============================================================================
91// wxPNGHandler implementation
92// ============================================================================
e9c4b1a2 93
e9c4b1a2 94IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler)
e9c4b1a2 95
e30285ab 96#if wxUSE_STREAMS
9ab6ee85 97
0729bd19 98#ifndef PNGLINKAGEMODE
c3b10b44
PC
99 #ifdef PNGAPI
100 #define PNGLINKAGEMODE PNGAPI
101 #elif defined(__WATCOMC__)
30e0e251
VZ
102 // we need an explicit cdecl for Watcom, at least according to
103 //
104 // http://sf.net/tracker/index.php?func=detail&aid=651492&group_id=9863&atid=109863
105 //
106 // more testing is needed for this however, please remove this comment
107 // if you can confirm that my fix works with Watcom 11
108 #define PNGLINKAGEMODE cdecl
109 #else
110 #define PNGLINKAGEMODE LINKAGEMODE
111 #endif
717b9bf2
DW
112#endif
113
bdffd806
VS
114
115// VS: wxPNGInfoStruct declared below is a hack that needs some explanation.
27bb2b7c
VZ
116// First, let me describe what's the problem: libpng uses jmp_buf in
117// its png_struct structure. Unfortunately, this structure is
118// compiler-specific and may vary in size, so if you use libpng compiled
bdffd806 119// as DLL with another compiler than the main executable, it may not work
27bb2b7c 120// (this is for example the case with wxMGL port and SciTech MGL library
bdffd806 121// that provides custom runtime-loadable libpng implementation with jmpbuf
27bb2b7c 122// disabled altogether). Luckily, it is still possible to use setjmp() &
bdffd806
VS
123// longjmp() as long as the structure is not part of png_struct.
124//
125// Sadly, there's no clean way to attach user-defined data to png_struct.
126// There is only one customizable place, png_struct.io_ptr, which is meant
27bb2b7c 127// only for I/O routines and is set with png_set_read_fn or
bdffd806
VS
128// png_set_write_fn. The hacky part is that we use io_ptr to store
129// a pointer to wxPNGInfoStruct that holds I/O structures _and_ jmp_buf.
130
131struct wxPNGInfoStruct
132{
133 jmp_buf jmpbuf;
134 bool verbose;
27bb2b7c 135
bdffd806
VS
136 union
137 {
138 wxInputStream *in;
139 wxOutputStream *out;
140 } stream;
141};
142
143#define WX_PNG_INFO(png_ptr) ((wxPNGInfoStruct*)png_get_io_ptr(png_ptr))
144
27bb2b7c
VZ
145// ----------------------------------------------------------------------------
146// helper functions
147// ----------------------------------------------------------------------------
bdffd806 148
90350682
VZ
149extern "C"
150{
151
7d7b3f69
FM
152static void PNGLINKAGEMODE wx_PNG_stream_reader( png_structp png_ptr, png_bytep data,
153 png_size_t length )
e9c4b1a2 154{
bdffd806 155 WX_PNG_INFO(png_ptr)->stream.in->Read(data, length);
e9c4b1a2
JS
156}
157
7d7b3f69
FM
158static void PNGLINKAGEMODE wx_PNG_stream_writer( png_structp png_ptr, png_bytep data,
159 png_size_t length )
e9c4b1a2 160{
bdffd806 161 WX_PNG_INFO(png_ptr)->stream.out->Write(data, length);
e9c4b1a2
JS
162}
163
7d7b3f69 164static void
bdffd806 165PNGLINKAGEMODE wx_png_warning(png_structp png_ptr, png_const_charp message)
deb2fec0 166{
d10c19d6
VZ
167 wxPNGInfoStruct *info = png_ptr ? WX_PNG_INFO(png_ptr) : NULL;
168 if ( !info || info->verbose )
af588446 169 {
2b5f62a0 170 wxLogWarning( wxString::FromAscii(message) );
af588446 171 }
deb2fec0
SB
172}
173
ce615190
DS
174// from pngerror.c
175// so that the libpng doesn't send anything on stderr
7d7b3f69 176static void
0e0589e8 177PNGLINKAGEMODE wx_png_error(png_structp png_ptr, png_const_charp message)
ce615190 178{
ce615190 179 wx_png_warning(NULL, message);
0e0589e8
VZ
180
181 // we're not using libpng built-in jump buffer (see comment before
182 // wxPNGInfoStruct above) so we have to return ourselves, otherwise libpng
183 // would just abort
184 longjmp(WX_PNG_INFO(png_ptr)->jmpbuf, 1);
ce615190
DS
185}
186
90350682
VZ
187} // extern "C"
188
27bb2b7c
VZ
189// ----------------------------------------------------------------------------
190// LoadFile() helpers
191// ----------------------------------------------------------------------------
192
d26d9754 193// determine the kind of transparency we need for this image: if the only alpha
c9fcf581
VZ
194// values it has are 0 (transparent) and 0xff (opaque) then we can simply
195// create a mask for it, we should be ok with a simple mask but otherwise we
196// need a full blown alpha channel in wxImage
d26d9754
VZ
197//
198// parameters:
36308e0e 199// lines raw PNG data
d26d9754
VZ
200// x, y starting position
201// w, h size of the image
202// numColBytes number of colour bytes (1 for grey scale, 3 for RGB)
203// (NB: alpha always follows the colour bytes)
27bb2b7c 204Transparency
36308e0e 205CheckTransparency(unsigned char **lines,
d26d9754
VZ
206 png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h,
207 size_t numColBytes)
27bb2b7c 208{
d26d9754
VZ
209 // suppose that a mask will suffice and check all the remaining alpha
210 // values to see if it does
36308e0e 211 for ( ; y < h; y++ )
27bb2b7c 212 {
e45cc235
RD
213 // each pixel is numColBytes+1 bytes, offset into the current line by
214 // the current x position
215 unsigned const char *ptr = lines[y] + (x * (numColBytes + 1));
36308e0e 216
c9fcf581 217 for ( png_uint_32 x2 = x; x2 < w; x2++ )
27bb2b7c 218 {
c9fcf581 219 // skip the grey or colour byte(s)
36308e0e 220 ptr += numColBytes;
27bb2b7c 221
36308e0e 222 unsigned char a2 = *ptr++;
c9fcf581
VZ
223
224 if ( !IsTransparent(a2) && !IsOpaque(a2) )
27bb2b7c 225 {
d26d9754
VZ
226 // not fully opaque nor fully transparent, hence need alpha
227 return Transparency_Alpha;
27bb2b7c 228 }
27bb2b7c 229 }
c9fcf581
VZ
230
231 // during the next loop iteration check all the pixels in the row
232 x = 0;
27bb2b7c
VZ
233 }
234
d26d9754
VZ
235 // mask will be enough
236 return Transparency_Mask;
27bb2b7c
VZ
237}
238
239unsigned char *InitAlpha(wxImage *image, png_uint_32 x, png_uint_32 y)
240{
241 // create alpha channel
242 image->SetAlpha();
243
244 unsigned char *alpha = image->GetAlpha();
245
246 // set alpha for the pixels we had so far
c9fcf581
VZ
247 png_uint_32 end = y * image->GetWidth() + x;
248 for ( png_uint_32 i = 0; i < end; i++ )
27bb2b7c 249 {
c9fcf581
VZ
250 // all the previous pixels were opaque
251 *alpha++ = 0xff;
27bb2b7c
VZ
252 }
253
254 return alpha;
255}
256
257void
258FindMaskColour(unsigned char **lines, png_uint_32 width, png_uint_32 height,
259 unsigned char& rMask, unsigned char& gMask, unsigned char& bMask)
260{
261 // choosing the colour for the mask is more
262 // difficult: we need to iterate over the entire
263 // image for this in order to choose an unused
264 // colour (this is not very efficient but what else
265 // can we do?)
266 wxImageHistogram h;
267 unsigned nentries = 0;
268 unsigned char r2, g2, b2;
269 for ( png_uint_32 y2 = 0; y2 < height; y2++ )
270 {
271 const unsigned char *p = lines[y2];
272 for ( png_uint_32 x2 = 0; x2 < width; x2++ )
273 {
274 r2 = *p++;
275 g2 = *p++;
276 b2 = *p++;
dcc36b34 277 ++p; // jump over alpha
27bb2b7c
VZ
278
279 wxImageHistogramEntry&
280 entry = h[wxImageHistogram:: MakeKey(r2, g2, b2)];
281
282 if ( entry.value++ == 0 )
283 entry.index = nentries++;
284 }
285 }
286
f773e9b0 287 if ( !h.FindFirstUnusedColour(&rMask, &gMask, &bMask) )
27bb2b7c
VZ
288 {
289 wxLogWarning(_("Too many colours in PNG, the image may be slightly blurred."));
290
291 // use a fixed mask colour and we'll fudge
292 // the real pixels with this colour (see
293 // below)
294 rMask = 0xfe;
295 gMask = 0;
296 bMask = 0xff;
297 }
298}
299
300// ----------------------------------------------------------------------------
301// reading PNGs
302// ----------------------------------------------------------------------------
303
304bool wxPNGHandler::DoCanRead( wxInputStream& stream )
305{
306 unsigned char hdr[4];
307
8faef7cc 308 if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) // it's ok to modify the stream position here
7beb59f3 309 return false;
27bb2b7c
VZ
310
311 return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0;
312}
313
314// convert data from RGB to wxImage format
315static
316void CopyDataFromPNG(wxImage *image,
317 unsigned char **lines,
318 png_uint_32 width,
319 png_uint_32 height,
320 int color_type)
321{
322 Transparency transparency = Transparency_None;
323
324 // only non NULL if transparency == Transparency_Alpha
325 unsigned char *alpha = NULL;
326
327 // RGB of the mask colour if transparency == Transparency_Mask
328 // (but init them anyhow to avoid compiler warnings)
329 unsigned char rMask = 0,
330 gMask = 0,
331 bMask = 0;
332
333 unsigned char *ptrDst = image->GetData();
334 if ( !(color_type & PNG_COLOR_MASK_COLOR) )
335 {
336 // grey image: GAGAGA... where G == grey component and A == alpha
337 for ( png_uint_32 y = 0; y < height; y++ )
338 {
339 const unsigned char *ptrSrc = lines[y];
340 for ( png_uint_32 x = 0; x < width; x++ )
341 {
342 unsigned char g = *ptrSrc++;
343 unsigned char a = *ptrSrc++;
344
345 // the first time we encounter a transparent pixel we must
346 // decide about what to do about them
c9fcf581 347 if ( !IsOpaque(a) && transparency == Transparency_None )
27bb2b7c
VZ
348 {
349 // we'll need at least the mask for this image and
350 // maybe even full alpha channel info: the former is
351 // only enough if we have alpha values of 0 and 0xff
352 // only, otherwisewe need the latter
d26d9754
VZ
353 transparency = CheckTransparency
354 (
36308e0e 355 lines,
d26d9754
VZ
356 x, y,
357 width, height,
358 1
359 );
27bb2b7c
VZ
360
361 if ( transparency == Transparency_Mask )
362 {
363 // let's choose this colour for the mask: this is
364 // not a problem here as all the other pixels are
365 // grey, i.e. R == G == B which is not the case for
366 // this one so no confusion is possible
367 rMask = 0xff;
368 gMask = 0;
369 bMask = 0xff;
370 }
371 else // transparency == Transparency_Alpha
372 {
373 alpha = InitAlpha(image, x, y);
374 }
375 }
376
377 switch ( transparency )
378 {
c9fcf581
VZ
379 case Transparency_Mask:
380 if ( IsTransparent(a) )
381 {
382 *ptrDst++ = rMask;
c9fcf581 383 *ptrDst++ = gMask;
dcc36b34 384 *ptrDst++ = bMask;
c9fcf581
VZ
385 break;
386 }
387 // else: !transparent
388
389 // must be opaque then as otherwise we shouldn't be
390 // using the mask at all
9a83f860 391 wxASSERT_MSG( IsOpaque(a), wxT("logic error") );
c9fcf581
VZ
392
393 // fall through
394
27bb2b7c 395 case Transparency_Alpha:
c9fcf581
VZ
396 if ( alpha )
397 *alpha++ = a;
27bb2b7c
VZ
398 // fall through
399
400 case Transparency_None:
401 *ptrDst++ = g;
402 *ptrDst++ = g;
403 *ptrDst++ = g;
404 break;
27bb2b7c
VZ
405 }
406 }
407 }
408 }
409 else // colour image: RGBRGB...
410 {
411 for ( png_uint_32 y = 0; y < height; y++ )
412 {
413 const unsigned char *ptrSrc = lines[y];
414 for ( png_uint_32 x = 0; x < width; x++ )
415 {
416 unsigned char r = *ptrSrc++;
417 unsigned char g = *ptrSrc++;
418 unsigned char b = *ptrSrc++;
419 unsigned char a = *ptrSrc++;
420
421 // the logic here is the same as for the grey case except
422 // where noted
c9fcf581 423 if ( !IsOpaque(a) && transparency == Transparency_None )
27bb2b7c 424 {
d26d9754
VZ
425 transparency = CheckTransparency
426 (
36308e0e 427 lines,
d26d9754
VZ
428 x, y,
429 width, height,
430 3
431 );
27bb2b7c
VZ
432
433 if ( transparency == Transparency_Mask )
434 {
f773e9b0
RR
435 FindMaskColour(lines, width, height,
436 rMask, gMask, bMask);
27bb2b7c
VZ
437 }
438 else // transparency == Transparency_Alpha
439 {
440 alpha = InitAlpha(image, x, y);
441 }
442
443 }
444
445 switch ( transparency )
446 {
c9fcf581
VZ
447 case Transparency_Mask:
448 if ( IsTransparent(a) )
449 {
c9fcf581 450 *ptrDst++ = rMask;
c9fcf581 451 *ptrDst++ = gMask;
dcc36b34 452 *ptrDst++ = bMask;
c9fcf581
VZ
453 break;
454 }
999836aa
VZ
455 else // !transparent
456 {
457 // must be opaque then as otherwise we shouldn't be
458 // using the mask at all
9a83f860 459 wxASSERT_MSG( IsOpaque(a), wxT("logic error") );
999836aa
VZ
460
461 // if we couldn't find a unique colour for the
462 // mask, we can have real pixels with the same
463 // value as the mask and it's better to slightly
464 // change their colour than to make them
465 // transparent
466 if ( r == rMask && g == gMask && b == bMask )
467 {
468 r++;
469 }
470 }
c9fcf581
VZ
471
472 // fall through
473
27bb2b7c 474 case Transparency_Alpha:
c9fcf581
VZ
475 if ( alpha )
476 *alpha++ = a;
27bb2b7c
VZ
477 // fall through
478
479 case Transparency_None:
480 *ptrDst++ = r;
481 *ptrDst++ = g;
482 *ptrDst++ = b;
483 break;
27bb2b7c
VZ
484 }
485 }
486 }
487 }
488
489 if ( transparency == Transparency_Mask )
490 {
491 image->SetMaskColour(rMask, gMask, bMask);
492 }
493}
494
3ca6a5f0
BP
495// temporarily disable the warning C4611 (interaction between '_setjmp' and
496// C++ object destruction is non-portable) - I don't see any dtors here
497#ifdef __VISUALC__
498 #pragma warning(disable:4611)
499#endif /* VC++ */
500
27bb2b7c
VZ
501bool
502wxPNGHandler::LoadFile(wxImage *image,
503 wxInputStream& stream,
504 bool verbose,
505 int WXUNUSED(index))
e9c4b1a2 506{
7884ab90 507 // VZ: as this function uses setjmp() the only fool-proof error handling
e9c4b1a2 508 // method is to use goto (setjmp is not really C++ dtors friendly...)
717b9bf2 509
27bb2b7c
VZ
510 unsigned char **lines = NULL;
511 png_infop info_ptr = (png_infop) NULL;
bdffd806
VS
512 wxPNGInfoStruct wxinfo;
513
7884ab90
DS
514 png_uint_32 i, width, height = 0;
515 int bit_depth, color_type, interlace_type;
516
bdffd806
VS
517 wxinfo.verbose = verbose;
518 wxinfo.stream.in = &stream;
717b9bf2 519
e9c4b1a2 520 image->Destroy();
717b9bf2 521
d10c19d6
VZ
522 png_structp png_ptr = png_create_read_struct
523 (
524 PNG_LIBPNG_VER_STRING,
c742231d 525 NULL,
d10c19d6
VZ
526 wx_png_error,
527 wx_png_warning
528 );
e9c4b1a2 529 if (!png_ptr)
27bb2b7c 530 goto error;
717b9bf2 531
bdffd806
VS
532 // NB: please see the comment near wxPNGInfoStruct declaration for
533 // explanation why this line is mandatory
e9b964cf 534 png_set_read_fn( png_ptr, &wxinfo, wx_PNG_stream_reader);
deb2fec0 535
e9c4b1a2
JS
536 info_ptr = png_create_info_struct( png_ptr );
537 if (!info_ptr)
27bb2b7c 538 goto error;
717b9bf2 539
bdffd806 540 if (setjmp(wxinfo.jmpbuf))
27bb2b7c 541 goto error;
717b9bf2 542
e9c4b1a2 543 png_read_info( png_ptr, info_ptr );
d3b9f782 544 png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL );
717b9bf2 545
e9c4b1a2
JS
546 if (color_type == PNG_COLOR_TYPE_PALETTE)
547 png_set_expand( png_ptr );
717b9bf2 548
2b5f62a0
VZ
549 // Fix for Bug [ 439207 ] Monochrome PNG images come up black
550 if (bit_depth < 8)
551 png_set_expand( png_ptr );
552
e9c4b1a2
JS
553 png_set_strip_16( png_ptr );
554 png_set_packing( png_ptr );
555 if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
556 png_set_expand( png_ptr );
557 png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
717b9bf2 558
154e1ca1 559 image->Create((int)width, (int)height, (bool) false /* no need to init pixels */);
717b9bf2 560
e9c4b1a2 561 if (!image->Ok())
27bb2b7c 562 goto error;
717b9bf2 563
5550f66b
VZ
564 // initialize all line pointers to NULL to ensure that they can be safely
565 // free()d if an error occurs before all of them could be allocated
566 lines = (unsigned char **)calloc(height, sizeof(unsigned char *));
27bb2b7c
VZ
567 if ( !lines )
568 goto error;
717b9bf2 569
e9c4b1a2
JS
570 for (i = 0; i < height; i++)
571 {
479cd5de 572 if ((lines[i] = (unsigned char *)malloc( (size_t)(width * (sizeof(unsigned char) * 4)))) == NULL)
e9c4b1a2 573 goto error;
e9c4b1a2 574 }
717b9bf2 575
27bb2b7c
VZ
576 png_read_image( png_ptr, lines );
577 png_read_end( png_ptr, info_ptr );
982a6623
RR
578
579#if wxUSE_PALETTE
580 if (color_type == PNG_COLOR_TYPE_PALETTE)
581 {
1de255d6
DS
582 png_colorp palette = NULL;
583 int numPalette = 0;
982a6623 584
1de255d6
DS
585 (void) png_get_PLTE(png_ptr, info_ptr, &palette, &numPalette);
586
587 unsigned char* r = new unsigned char[numPalette];
588 unsigned char* g = new unsigned char[numPalette];
589 unsigned char* b = new unsigned char[numPalette];
590
591 for (int j = 0; j < numPalette; j++)
982a6623 592 {
1de255d6
DS
593 r[j] = palette[j].red;
594 g[j] = palette[j].green;
595 b[j] = palette[j].blue;
982a6623
RR
596 }
597
1de255d6 598 image->SetPalette(wxPalette(numPalette, r, g, b));
982a6623
RR
599 delete[] r;
600 delete[] g;
601 delete[] b;
602 }
603#endif // wxUSE_PALETTE
604
27bb2b7c 605 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
717b9bf2 606
27bb2b7c
VZ
607 // loaded successfully, now init wxImage with this data
608 CopyDataFromPNG(image, lines, width, height, color_type);
717b9bf2 609
27bb2b7c
VZ
610 for ( i = 0; i < height; i++ )
611 free( lines[i] );
612 free( lines );
717b9bf2 613
7beb59f3 614 return true;
95ee0ac8 615
27bb2b7c 616error:
58c837a4 617 if (verbose)
af588446 618 {
58c837a4 619 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
af588446 620 }
717b9bf2 621
e9c4b1a2
JS
622 if ( image->Ok() )
623 {
624 image->Destroy();
625 }
717b9bf2 626
e9c4b1a2
JS
627 if ( lines )
628 {
0e0589e8
VZ
629 for ( unsigned int n = 0; n < height; n++ )
630 free( lines[n] );
631
e9c4b1a2
JS
632 free( lines );
633 }
717b9bf2 634
e9c4b1a2
JS
635 if ( png_ptr )
636 {
637 if ( info_ptr )
638 {
639 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
640 free(info_ptr);
641 }
642 else
643 png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
644 }
7beb59f3 645 return false;
e9c4b1a2
JS
646}
647
8ee313d2 648// ----------------------------------------------------------------------------
8529b0b9 649// SaveFile() palette helpers
8ee313d2
DS
650// ----------------------------------------------------------------------------
651
8529b0b9 652typedef wxLongToLongHashMap PaletteMap;
967956dd 653
8529b0b9 654static unsigned long PaletteMakeKey(const png_color_8& clr)
8ee313d2 655{
8529b0b9 656 return (wxImageHistogram::MakeKey(clr.red, clr.green, clr.blue) << 8) | clr.alpha;
8ee313d2
DS
657}
658
8529b0b9
DS
659static long PaletteFind(const PaletteMap& palette, const png_color_8& clr)
660{
661 unsigned long value = PaletteMakeKey(clr);
662 PaletteMap::const_iterator it = palette.find(value);
663
664 return (it != palette.end()) ? it->second : wxNOT_FOUND;
665}
666
667static long PaletteAdd(PaletteMap *palette, const png_color_8& clr)
668{
669 unsigned long value = PaletteMakeKey(clr);
670 PaletteMap::const_iterator it = palette->find(value);
671 size_t index;
672
673 if (it == palette->end())
674 {
675 index = palette->size();
676 (*palette)[value] = index;
677 }
678 else
679 {
680 index = it->second;
681 }
682
683 return index;
684}
967956dd 685
27bb2b7c
VZ
686// ----------------------------------------------------------------------------
687// writing PNGs
688// ----------------------------------------------------------------------------
689
deb2fec0 690bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2 691{
bdffd806 692 wxPNGInfoStruct wxinfo;
717b9bf2 693
bdffd806
VS
694 wxinfo.verbose = verbose;
695 wxinfo.stream.out = &stream;
deb2fec0 696
d10c19d6
VZ
697 png_structp png_ptr = png_create_write_struct
698 (
699 PNG_LIBPNG_VER_STRING,
700 NULL,
701 wx_png_error,
702 wx_png_warning
703 );
bdffd806
VS
704 if (!png_ptr)
705 {
706 if (verbose)
af588446 707 {
bdffd806 708 wxLogError(_("Couldn't save PNG image."));
af588446 709 }
7beb59f3 710 return false;
bdffd806 711 }
717b9bf2 712
bdffd806
VS
713 png_infop info_ptr = png_create_info_struct(png_ptr);
714 if (info_ptr == NULL)
715 {
716 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
717 if (verbose)
af588446 718 {
bdffd806 719 wxLogError(_("Couldn't save PNG image."));
af588446 720 }
7beb59f3 721 return false;
bdffd806 722 }
717b9bf2 723
bdffd806
VS
724 if (setjmp(wxinfo.jmpbuf))
725 {
726 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
727 if (verbose)
af588446 728 {
bdffd806 729 wxLogError(_("Couldn't save PNG image."));
af588446 730 }
7beb59f3 731 return false;
bdffd806 732 }
717b9bf2 733
bdffd806
VS
734 // NB: please see the comment near wxPNGInfoStruct declaration for
735 // explanation why this line is mandatory
e9b964cf 736 png_set_write_fn( png_ptr, &wxinfo, wx_PNG_stream_writer, NULL);
bdffd806 737
8529b0b9
DS
738 const int iHeight = image->GetHeight();
739 const int iWidth = image->GetWidth();
740
8ee313d2
DS
741 const bool bHasPngFormatOption
742 = image->HasOption(wxIMAGE_OPTION_PNG_FORMAT);
743
744 int iColorType = bHasPngFormatOption
a4efa721
VZ
745 ? image->GetOptionInt(wxIMAGE_OPTION_PNG_FORMAT)
746 : wxPNG_TYPE_COLOUR;
a4efa721 747
a4efa721
VZ
748 bool bHasAlpha = image->HasAlpha();
749 bool bHasMask = image->HasMask();
8ee313d2 750
8529b0b9 751 bool bUsePalette = iColorType == wxPNG_TYPE_PALETTE
8ee313d2 752#if wxUSE_PALETTE
8529b0b9
DS
753 || (!bHasPngFormatOption && image->HasPalette() )
754#endif
755 ;
756
757 png_color_8 mask;
758
759 if (bHasMask)
760 {
761 mask.red = image->GetMaskRed();
762 mask.green = image->GetMaskGreen();
763 mask.blue = image->GetMaskBlue();
764 mask.alpha = 0;
765 mask.gray = 0;
766 }
767
768 PaletteMap palette;
769
770 if (bUsePalette)
8ee313d2 771 {
8529b0b9
DS
772 png_color png_rgb [PNG_MAX_PALETTE_LENGTH];
773 png_byte png_trans[PNG_MAX_PALETTE_LENGTH];
774
775 const unsigned char *pColors = image->GetData();
776 const unsigned char* pAlpha = image->GetAlpha();
777
778 if (bHasMask && !pAlpha)
8ee313d2 779 {
8529b0b9
DS
780 // Mask must be first
781 PaletteAdd(&palette, mask);
8ee313d2 782 }
8529b0b9
DS
783
784 for (int y = 0; y < iHeight; y++)
8ee313d2 785 {
8529b0b9
DS
786 for (int x = 0; x < iWidth; x++)
787 {
788 png_color_8 rgba;
789
790 rgba.red = *pColors++;
791 rgba.green = *pColors++;
792 rgba.blue = *pColors++;
793 rgba.gray = 0;
794 rgba.alpha = (pAlpha && !bHasMask) ? *pAlpha++ : 0;
795
796 // save in our palette
797 long index = PaletteAdd(&palette, rgba);
798
799 if (index < PNG_MAX_PALETTE_LENGTH)
800 {
801 // save in libpng's palette
802 png_rgb[index].red = rgba.red;
803 png_rgb[index].green = rgba.green;
804 png_rgb[index].blue = rgba.blue;
805 png_trans[index] = rgba.alpha;
806 }
807 else
808 {
809 bUsePalette = false;
810 break;
811 }
812 }
813 }
814
815 if (bUsePalette)
816 {
817 png_set_PLTE(png_ptr, info_ptr, png_rgb, palette.size());
818
819 if (bHasMask && !pAlpha)
820 {
821 wxASSERT(PaletteFind(palette, mask) == 0);
822 png_trans[0] = 0;
823 png_set_tRNS(png_ptr, info_ptr, png_trans, 1, NULL);
824 }
825 else if (pAlpha && !bHasMask)
826 {
827 png_set_tRNS(png_ptr, info_ptr, png_trans, palette.size(), NULL);
828 }
8ee313d2
DS
829 }
830 }
13c5d825 831
b057ac07
DS
832 /*
833 If saving palettised was requested but it was decided we can't use a
834 palette then reset the colour type to RGB.
835 */
836 if (!bUsePalette && iColorType == wxPNG_TYPE_PALETTE)
13c5d825
DS
837 {
838 iColorType = wxPNG_TYPE_COLOUR;
839 }
8ee313d2
DS
840
841 bool bUseAlpha = !bUsePalette && (bHasAlpha || bHasMask);
842
a4efa721 843 int iPngColorType;
8ee313d2 844
8ee313d2
DS
845 if (bUsePalette)
846 {
847 iPngColorType = PNG_COLOR_TYPE_PALETTE;
848 iColorType = wxPNG_TYPE_PALETTE;
849 }
8529b0b9 850 else if ( iColorType==wxPNG_TYPE_COLOUR )
a4efa721
VZ
851 {
852 iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_RGB_ALPHA
853 : PNG_COLOR_TYPE_RGB;
854 }
855 else
856 {
857 iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_GRAY_ALPHA
858 : PNG_COLOR_TYPE_GRAY;
859 }
bdffd806 860
d19ce8c4
FM
861 if (image->HasOption(wxIMAGE_OPTION_PNG_FILTER))
862 png_set_filter( png_ptr, PNG_FILTER_TYPE_BASE, image->GetOptionInt(wxIMAGE_OPTION_PNG_FILTER) );
863
864 if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL))
865 png_set_compression_level( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL) );
866
867 if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL))
868 png_set_compression_mem_level( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL) );
869
870 if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY))
871 png_set_compression_strategy( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY) );
872
873 if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE))
874 png_set_compression_buffer_size( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE) );
875
8ee313d2
DS
876 int iBitDepth = !bUsePalette && image->HasOption(wxIMAGE_OPTION_PNG_BITDEPTH)
877 ? image->GetOptionInt(wxIMAGE_OPTION_PNG_BITDEPTH)
878 : 8;
879
a4efa721
VZ
880 png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(),
881 iBitDepth, iPngColorType,
882 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
883 PNG_FILTER_TYPE_BASE);
884
885 int iElements;
bdffd806 886 png_color_8 sig_bit;
a4efa721
VZ
887
888 if ( iPngColorType & PNG_COLOR_MASK_COLOR )
889 {
890 sig_bit.red =
891 sig_bit.green =
892 sig_bit.blue = (png_byte)iBitDepth;
893 iElements = 3;
894 }
895 else // grey
896 {
897 sig_bit.gray = (png_byte)iBitDepth;
898 iElements = 1;
899 }
900
8529b0b9 901 if ( bUseAlpha )
a4efa721
VZ
902 {
903 sig_bit.alpha = (png_byte)iBitDepth;
904 iElements++;
905 }
906
dc683654
VZ
907 if ( iBitDepth == 16 )
908 iElements *= 2;
909
361f4288
VZ
910 // save the image resolution if we have it
911 int resX, resY;
912 switch ( GetResolutionFromOptions(*image, &resX, &resY) )
913 {
914 case wxIMAGE_RESOLUTION_INCHES:
095cb950
PC
915 {
916 const double INCHES_IN_METER = 10000.0 / 254;
917 resX = int(resX * INCHES_IN_METER);
918 resY = int(resY * INCHES_IN_METER);
919 }
361f4288
VZ
920 break;
921
922 case wxIMAGE_RESOLUTION_CM:
923 resX *= 100;
924 resY *= 100;
925 break;
926
927 case wxIMAGE_RESOLUTION_NONE:
928 break;
929
930 default:
9a83f860 931 wxFAIL_MSG( wxT("unsupported image resolution units") );
361f4288
VZ
932 }
933
934 if ( resX && resY )
935 png_set_pHYs( png_ptr, info_ptr, resX, resY, PNG_RESOLUTION_METER );
936
bdffd806
VS
937 png_set_sBIT( png_ptr, info_ptr, &sig_bit );
938 png_write_info( png_ptr, info_ptr );
939 png_set_shift( png_ptr, &sig_bit );
940 png_set_packing( png_ptr );
717b9bf2 941
a4efa721
VZ
942 unsigned char *
943 data = (unsigned char *)malloc( image->GetWidth() * iElements );
944 if ( !data )
bdffd806
VS
945 {
946 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
7beb59f3 947 return false;
bdffd806 948 }
717b9bf2 949
8529b0b9
DS
950 const unsigned char *
951 pAlpha = (const unsigned char *)(bHasAlpha ? image->GetAlpha() : NULL);
a4efa721 952
8529b0b9 953 const unsigned char *pColors = image->GetData();
a4efa721
VZ
954
955 for (int y = 0; y != iHeight; ++y)
bdffd806 956 {
a4efa721
VZ
957 unsigned char *pData = data;
958 for (int x = 0; x != iWidth; x++)
e9c4b1a2 959 {
8529b0b9 960 png_color_8 clr;
8ee313d2
DS
961 clr.red = *pColors++;
962 clr.green = *pColors++;
963 clr.blue = *pColors++;
8529b0b9
DS
964 clr.gray = 0;
965 clr.alpha = (bUsePalette && pAlpha) ? *pAlpha++ : 0; // use with wxPNG_TYPE_PALETTE only
e1e36a3e 966
a4efa721 967 switch ( iColorType )
bdffd806 968 {
a4efa721 969 default:
9a83f860 970 wxFAIL_MSG( wxT("unknown wxPNG_TYPE_XXX") );
a4efa721
VZ
971 // fall through
972
973 case wxPNG_TYPE_COLOUR:
8ee313d2 974 *pData++ = clr.red;
dc683654 975 if ( iBitDepth == 16 )
a4efa721 976 *pData++ = 0;
8ee313d2 977 *pData++ = clr.green;
dc683654 978 if ( iBitDepth == 16 )
a4efa721 979 *pData++ = 0;
8ee313d2 980 *pData++ = clr.blue;
dc683654 981 if ( iBitDepth == 16 )
a4efa721
VZ
982 *pData++ = 0;
983 break;
984
985 case wxPNG_TYPE_GREY:
986 {
dc683654
VZ
987 // where do these coefficients come from? maybe we
988 // should have image options for them as well?
a4efa721 989 unsigned uiColor =
8ee313d2
DS
990 (unsigned) (76.544*(unsigned)clr.red +
991 150.272*(unsigned)clr.green +
992 36.864*(unsigned)clr.blue);
dc683654
VZ
993
994 *pData++ = (unsigned char)((uiColor >> 8) & 0xFF);
995 if ( iBitDepth == 16 )
a4efa721 996 *pData++ = (unsigned char)(uiColor & 0xFF);
a4efa721
VZ
997 }
998 break;
999
1000 case wxPNG_TYPE_GREY_RED:
8ee313d2 1001 *pData++ = clr.red;
dc683654 1002 if ( iBitDepth == 16 )
a4efa721
VZ
1003 *pData++ = 0;
1004 break;
8ee313d2
DS
1005
1006 case wxPNG_TYPE_PALETTE:
8529b0b9 1007 *pData++ = (unsigned char) PaletteFind(palette, clr);
8ee313d2 1008 break;
a4efa721
VZ
1009 }
1010
1011 if ( bUseAlpha )
1012 {
1013 unsigned char uchAlpha = 255;
1014 if ( bHasAlpha )
1015 uchAlpha = *pAlpha++;
1016
1017 if ( bHasMask )
e1e36a3e 1018 {
8ee313d2
DS
1019 if ( (clr.red == mask.red)
1020 && (clr.green == mask.green)
1021 && (clr.blue == mask.blue) )
a4efa721 1022 uchAlpha = 0;
e1e36a3e 1023 }
a4efa721
VZ
1024
1025 *pData++ = uchAlpha;
dc683654 1026 if ( iBitDepth == 16 )
a4efa721 1027 *pData++ = 0;
e9c4b1a2 1028 }
e9c4b1a2 1029 }
a4efa721 1030
bdffd806
VS
1031 png_bytep row_ptr = data;
1032 png_write_rows( png_ptr, &row_ptr, 1 );
e9c4b1a2 1033 }
bdffd806
VS
1034
1035 free(data);
1036 png_write_end( png_ptr, info_ptr );
1037 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
1038
7beb59f3 1039 return true;
e9c4b1a2 1040}
e9c4b1a2 1041
3ca6a5f0
BP
1042#ifdef __VISUALC__
1043 #pragma warning(default:4611)
1044#endif /* VC++ */
1045
9ab6ee85 1046#endif // wxUSE_STREAMS
e9c4b1a2 1047
ccec9093
VZ
1048/*static*/ wxVersionInfo wxPNGHandler::GetLibraryVersionInfo()
1049{
1050 // The version string seems to always have a leading space and a trailing
1051 // new line, get rid of them both.
1052 wxString str = png_get_header_version(NULL) + 1;
1053 str.Replace("\n", "");
1054
1055 return wxVersionInfo("libpng",
1056 PNG_LIBPNG_VER_MAJOR,
1057 PNG_LIBPNG_VER_MINOR,
1058 PNG_LIBPNG_VER_RELEASE,
1059 str);
1060}
1061
9ab6ee85 1062#endif // wxUSE_LIBPNG