1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagepng.cpp
3 // Purpose: wxImage PNG handler
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
25 #if wxUSE_IMAGE && wxUSE_LIBPNG
32 #include "wx/imagpng.h"
33 #include "wx/bitmap.h"
35 #include "wx/filefn.h"
36 #include "wx/wfstream.h"
38 #include "wx/module.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // image can not have any transparent pixels at all, have only 100% opaque
54 // and/or 100% transparent pixels in which case a simple mask is enough to
55 // store this information in wxImage or have a real alpha channel in which case
56 // we need to have it in wxImage as well
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // return the kind of transparency needed for this image assuming that it does
69 // have transparent pixels, i.e. either Transparency_Alpha or Transparency_Mask
71 CheckTransparency(unsigned char **lines
,
72 png_uint_32 x
, png_uint_32 y
, png_uint_32 w
, png_uint_32 h
,
75 // init the alpha channel for the image and fill it with 1s up to (x, y)
76 static unsigned char *InitAlpha(wxImage
*image
, png_uint_32 x
, png_uint_32 y
);
78 // find a free colour for the mask in the PNG data array
80 FindMaskColour(unsigned char **lines
, png_uint_32 width
, png_uint_32 height
,
81 unsigned char& rMask
, unsigned char& gMask
, unsigned char& bMask
);
83 // is the pixel with this value of alpha a fully opaque one?
85 bool IsOpaque(unsigned char a
)
90 // is the pixel with this value of alpha a fully transparent one?
92 bool IsTransparent(unsigned char a
)
97 // ============================================================================
98 // wxPNGHandler implementation
99 // ============================================================================
101 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
105 #ifndef PNGLINKAGEMODE
106 #if defined(__WATCOMC__) && ( defined(__WXMSW__) || defined(__WXMGL__) )
107 // we need an explicit cdecl for Watcom, at least according to
109 // http://sf.net/tracker/index.php?func=detail&aid=651492&group_id=9863&atid=109863
111 // more testing is needed for this however, please remove this comment
112 // if you can confirm that my fix works with Watcom 11
113 #define PNGLINKAGEMODE cdecl
115 #define PNGLINKAGEMODE LINKAGEMODE
120 // VS: wxPNGInfoStruct declared below is a hack that needs some explanation.
121 // First, let me describe what's the problem: libpng uses jmp_buf in
122 // its png_struct structure. Unfortunately, this structure is
123 // compiler-specific and may vary in size, so if you use libpng compiled
124 // as DLL with another compiler than the main executable, it may not work
125 // (this is for example the case with wxMGL port and SciTech MGL library
126 // that provides custom runtime-loadable libpng implementation with jmpbuf
127 // disabled altogether). Luckily, it is still possible to use setjmp() &
128 // longjmp() as long as the structure is not part of png_struct.
130 // Sadly, there's no clean way to attach user-defined data to png_struct.
131 // There is only one customizable place, png_struct.io_ptr, which is meant
132 // only for I/O routines and is set with png_set_read_fn or
133 // png_set_write_fn. The hacky part is that we use io_ptr to store
134 // a pointer to wxPNGInfoStruct that holds I/O structures _and_ jmp_buf.
136 struct wxPNGInfoStruct
148 #define WX_PNG_INFO(png_ptr) ((wxPNGInfoStruct*)png_get_io_ptr(png_ptr))
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
157 void PNGLINKAGEMODE
wx_PNG_stream_reader( png_structp png_ptr
, png_bytep data
,
160 WX_PNG_INFO(png_ptr
)->stream
.in
->Read(data
, length
);
163 void PNGLINKAGEMODE
wx_PNG_stream_writer( png_structp png_ptr
, png_bytep data
,
166 WX_PNG_INFO(png_ptr
)->stream
.out
->Write(data
, length
);
170 PNGLINKAGEMODE
wx_png_warning(png_structp png_ptr
, png_const_charp message
)
172 wxPNGInfoStruct
*info
= png_ptr
? WX_PNG_INFO(png_ptr
) : NULL
;
173 if ( !info
|| info
->verbose
)
174 wxLogWarning( wxString::FromAscii(message
) );
178 // so that the libpng doesn't send anything on stderr
180 PNGLINKAGEMODE
wx_png_error(png_structp png_ptr
, png_const_charp message
)
182 wx_png_warning(NULL
, message
);
184 // we're not using libpng built-in jump buffer (see comment before
185 // wxPNGInfoStruct above) so we have to return ourselves, otherwise libpng
187 longjmp(WX_PNG_INFO(png_ptr
)->jmpbuf
, 1);
192 // ----------------------------------------------------------------------------
193 // LoadFile() helpers
194 // ----------------------------------------------------------------------------
196 // determine the kind of transparency we need for this image: if the only alpha
197 // values it has are 0 (transparent) and 0xff (opaque) then we can simply
198 // create a mask for it, we should be ok with a simple mask but otherwise we
199 // need a full blown alpha channel in wxImage
202 // lines raw PNG data
203 // x, y starting position
204 // w, h size of the image
205 // numColBytes number of colour bytes (1 for grey scale, 3 for RGB)
206 // (NB: alpha always follows the colour bytes)
208 CheckTransparency(unsigned char **lines
,
209 png_uint_32 x
, png_uint_32 y
, png_uint_32 w
, png_uint_32 h
,
212 // suppose that a mask will suffice and check all the remaining alpha
213 // values to see if it does
216 // each pixel is numColBytes+1 bytes, offset into the current line by
217 // the current x position
218 unsigned const char *ptr
= lines
[y
] + (x
* (numColBytes
+ 1));
220 for ( png_uint_32 x2
= x
; x2
< w
; x2
++ )
222 // skip the grey or colour byte(s)
225 unsigned char a2
= *ptr
++;
227 if ( !IsTransparent(a2
) && !IsOpaque(a2
) )
229 // not fully opaque nor fully transparent, hence need alpha
230 return Transparency_Alpha
;
234 // during the next loop iteration check all the pixels in the row
238 // mask will be enough
239 return Transparency_Mask
;
242 unsigned char *InitAlpha(wxImage
*image
, png_uint_32 x
, png_uint_32 y
)
244 // create alpha channel
247 unsigned char *alpha
= image
->GetAlpha();
249 // set alpha for the pixels we had so far
250 png_uint_32 end
= y
* image
->GetWidth() + x
;
251 for ( png_uint_32 i
= 0; i
< end
; i
++ )
253 // all the previous pixels were opaque
261 FindMaskColour(unsigned char **lines
, png_uint_32 width
, png_uint_32 height
,
262 unsigned char& rMask
, unsigned char& gMask
, unsigned char& bMask
)
264 // choosing the colour for the mask is more
265 // difficult: we need to iterate over the entire
266 // image for this in order to choose an unused
267 // colour (this is not very efficient but what else
270 unsigned nentries
= 0;
271 unsigned char r2
, g2
, b2
;
272 for ( png_uint_32 y2
= 0; y2
< height
; y2
++ )
274 const unsigned char *p
= lines
[y2
];
275 for ( png_uint_32 x2
= 0; x2
< width
; x2
++ )
281 wxImageHistogramEntry
&
282 entry
= h
[wxImageHistogram:: MakeKey(r2
, g2
, b2
)];
284 if ( entry
.value
++ == 0 )
285 entry
.index
= nentries
++;
289 if ( !h
.FindFirstUnusedColour(&rMask
, &gMask
, &bMask
) )
291 wxLogWarning(_("Too many colours in PNG, the image may be slightly blurred."));
293 // use a fixed mask colour and we'll fudge
294 // the real pixels with this colour (see
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 bool wxPNGHandler::DoCanRead( wxInputStream
& stream
)
308 unsigned char hdr
[4];
310 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
313 return memcmp(hdr
, "\211PNG", WXSIZEOF(hdr
)) == 0;
316 // convert data from RGB to wxImage format
318 void CopyDataFromPNG(wxImage
*image
,
319 unsigned char **lines
,
324 Transparency transparency
= Transparency_None
;
326 // only non NULL if transparency == Transparency_Alpha
327 unsigned char *alpha
= NULL
;
329 // RGB of the mask colour if transparency == Transparency_Mask
330 // (but init them anyhow to avoid compiler warnings)
331 unsigned char rMask
= 0,
335 unsigned char *ptrDst
= image
->GetData();
336 if ( !(color_type
& PNG_COLOR_MASK_COLOR
) )
338 // grey image: GAGAGA... where G == grey component and A == alpha
339 for ( png_uint_32 y
= 0; y
< height
; y
++ )
341 const unsigned char *ptrSrc
= lines
[y
];
342 for ( png_uint_32 x
= 0; x
< width
; x
++ )
344 unsigned char g
= *ptrSrc
++;
345 unsigned char a
= *ptrSrc
++;
347 // the first time we encounter a transparent pixel we must
348 // decide about what to do about them
349 if ( !IsOpaque(a
) && transparency
== Transparency_None
)
351 // we'll need at least the mask for this image and
352 // maybe even full alpha channel info: the former is
353 // only enough if we have alpha values of 0 and 0xff
354 // only, otherwisewe need the latter
355 transparency
= CheckTransparency
363 if ( transparency
== Transparency_Mask
)
365 // let's choose this colour for the mask: this is
366 // not a problem here as all the other pixels are
367 // grey, i.e. R == G == B which is not the case for
368 // this one so no confusion is possible
373 else // transparency == Transparency_Alpha
375 alpha
= InitAlpha(image
, x
, y
);
379 switch ( transparency
)
381 case Transparency_Mask
:
382 if ( IsTransparent(a
) )
389 // else: !transparent
391 // must be opaque then as otherwise we shouldn't be
392 // using the mask at all
393 wxASSERT_MSG( IsOpaque(a
), _T("logic error") );
397 case Transparency_Alpha
:
402 case Transparency_None
:
411 else // colour image: RGBRGB...
413 for ( png_uint_32 y
= 0; y
< height
; y
++ )
415 const unsigned char *ptrSrc
= lines
[y
];
416 for ( png_uint_32 x
= 0; x
< width
; x
++ )
418 unsigned char r
= *ptrSrc
++;
419 unsigned char g
= *ptrSrc
++;
420 unsigned char b
= *ptrSrc
++;
421 unsigned char a
= *ptrSrc
++;
423 // the logic here is the same as for the grey case except
425 if ( !IsOpaque(a
) && transparency
== Transparency_None
)
427 transparency
= CheckTransparency
435 if ( transparency
== Transparency_Mask
)
437 FindMaskColour(lines
, width
, height
,
438 rMask
, gMask
, bMask
);
440 else // transparency == Transparency_Alpha
442 alpha
= InitAlpha(image
, x
, y
);
447 switch ( transparency
)
449 case Transparency_Mask
:
450 if ( IsTransparent(a
) )
459 // must be opaque then as otherwise we shouldn't be
460 // using the mask at all
461 wxASSERT_MSG( IsOpaque(a
), _T("logic error") );
463 // if we couldn't find a unique colour for the
464 // mask, we can have real pixels with the same
465 // value as the mask and it's better to slightly
466 // change their colour than to make them
468 if ( r
== rMask
&& g
== gMask
&& b
== bMask
)
476 case Transparency_Alpha
:
481 case Transparency_None
:
491 if ( transparency
== Transparency_Mask
)
493 image
->SetMaskColour(rMask
, gMask
, bMask
);
497 // temporarily disable the warning C4611 (interaction between '_setjmp' and
498 // C++ object destruction is non-portable) - I don't see any dtors here
500 #pragma warning(disable:4611)
504 wxPNGHandler::LoadFile(wxImage
*image
,
505 wxInputStream
& stream
,
509 // VZ: as this function uses setjmp() the only fool-proof error handling
510 // method is to use goto (setjmp is not really C++ dtors friendly...)
512 unsigned char **lines
= NULL
;
513 png_infop info_ptr
= (png_infop
) NULL
;
514 wxPNGInfoStruct wxinfo
;
516 png_uint_32 i
, width
, height
= 0;
517 int bit_depth
, color_type
, interlace_type
;
519 wxinfo
.verbose
= verbose
;
520 wxinfo
.stream
.in
= &stream
;
524 png_structp png_ptr
= png_create_read_struct
526 PNG_LIBPNG_VER_STRING
,
534 // NB: please see the comment near wxPNGInfoStruct declaration for
535 // explanation why this line is mandatory
536 png_set_read_fn( png_ptr
, &wxinfo
, wx_PNG_stream_reader
);
538 info_ptr
= png_create_info_struct( png_ptr
);
542 if (setjmp(wxinfo
.jmpbuf
))
545 png_read_info( png_ptr
, info_ptr
);
546 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
548 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
549 png_set_expand( png_ptr
);
551 // Fix for Bug [ 439207 ] Monochrome PNG images come up black
553 png_set_expand( png_ptr
);
555 png_set_strip_16( png_ptr
);
556 png_set_packing( png_ptr
);
557 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
558 png_set_expand( png_ptr
);
559 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
561 image
->Create((int)width
, (int)height
, (bool) false /* no need to init pixels */);
566 lines
= (unsigned char **)malloc( (size_t)(height
* sizeof(unsigned char *)) );
570 for (i
= 0; i
< height
; i
++)
572 if ((lines
[i
] = (unsigned char *)malloc( (size_t)(width
* (sizeof(unsigned char) * 4)))) == NULL
)
574 for ( unsigned int n
= 0; n
< i
; n
++ )
580 png_read_image( png_ptr
, lines
);
581 png_read_end( png_ptr
, info_ptr
);
582 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
584 // loaded successfully, now init wxImage with this data
585 CopyDataFromPNG(image
, lines
, width
, height
, color_type
);
587 for ( i
= 0; i
< height
; i
++ )
595 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
604 for ( unsigned int n
= 0; n
< height
; n
++ )
614 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
618 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
623 // ----------------------------------------------------------------------------
625 // ----------------------------------------------------------------------------
627 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
629 wxPNGInfoStruct wxinfo
;
631 wxinfo
.verbose
= verbose
;
632 wxinfo
.stream
.out
= &stream
;
634 png_structp png_ptr
= png_create_write_struct
636 PNG_LIBPNG_VER_STRING
,
644 wxLogError(_("Couldn't save PNG image."));
648 png_infop info_ptr
= png_create_info_struct(png_ptr
);
649 if (info_ptr
== NULL
)
651 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
653 wxLogError(_("Couldn't save PNG image."));
657 if (setjmp(wxinfo
.jmpbuf
))
659 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
661 wxLogError(_("Couldn't save PNG image."));
665 // NB: please see the comment near wxPNGInfoStruct declaration for
666 // explanation why this line is mandatory
667 png_set_write_fn( png_ptr
, &wxinfo
, wx_PNG_stream_writer
, NULL
);
669 const int iColorType
= image
->HasOption(wxIMAGE_OPTION_PNG_FORMAT
)
670 ? image
->GetOptionInt(wxIMAGE_OPTION_PNG_FORMAT
)
672 const int iBitDepth
= image
->HasOption(wxIMAGE_OPTION_PNG_BITDEPTH
)
673 ? image
->GetOptionInt(wxIMAGE_OPTION_PNG_BITDEPTH
)
676 wxASSERT_MSG( iBitDepth
== 8 || iBitDepth
== 16,
677 _T("PNG bit depth must be 8 or 16") );
679 bool bHasAlpha
= image
->HasAlpha();
680 bool bHasMask
= image
->HasMask();
681 bool bUseAlpha
= bHasAlpha
|| bHasMask
;
684 if ( iColorType
==wxPNG_TYPE_COLOUR
)
686 iPngColorType
= bUseAlpha
? PNG_COLOR_TYPE_RGB_ALPHA
687 : PNG_COLOR_TYPE_RGB
;
691 iPngColorType
= bUseAlpha
? PNG_COLOR_TYPE_GRAY_ALPHA
692 : PNG_COLOR_TYPE_GRAY
;
695 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(),
696 iBitDepth
, iPngColorType
,
697 PNG_INTERLACE_NONE
, PNG_COMPRESSION_TYPE_BASE
,
698 PNG_FILTER_TYPE_BASE
);
703 if ( iPngColorType
& PNG_COLOR_MASK_COLOR
)
707 sig_bit
.blue
= (png_byte
)iBitDepth
;
712 sig_bit
.gray
= (png_byte
)iBitDepth
;
716 if ( iPngColorType
& PNG_COLOR_MASK_ALPHA
)
718 sig_bit
.alpha
= (png_byte
)iBitDepth
;
722 if ( iBitDepth
== 16 )
725 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
726 png_write_info( png_ptr
, info_ptr
);
727 png_set_shift( png_ptr
, &sig_bit
);
728 png_set_packing( png_ptr
);
731 data
= (unsigned char *)malloc( image
->GetWidth() * iElements
);
734 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
739 pAlpha
= (unsigned char *)(bHasAlpha
? image
->GetAlpha() : NULL
);
740 int iHeight
= image
->GetHeight();
741 int iWidth
= image
->GetWidth();
743 unsigned char uchMaskRed
= 0, uchMaskGreen
= 0, uchMaskBlue
= 0;
747 uchMaskRed
= image
->GetMaskRed();
748 uchMaskGreen
= image
->GetMaskGreen();
749 uchMaskBlue
= image
->GetMaskBlue();
752 unsigned char *pColors
= image
->GetData();
754 for (int y
= 0; y
!= iHeight
; ++y
)
756 unsigned char *pData
= data
;
757 for (int x
= 0; x
!= iWidth
; x
++)
759 unsigned char uchRed
= *pColors
++;
760 unsigned char uchGreen
= *pColors
++;
761 unsigned char uchBlue
= *pColors
++;
763 switch ( iColorType
)
766 wxFAIL_MSG( _T("unknown wxPNG_TYPE_XXX") );
769 case wxPNG_TYPE_COLOUR
:
771 if ( iBitDepth
== 16 )
774 if ( iBitDepth
== 16 )
777 if ( iBitDepth
== 16 )
781 case wxPNG_TYPE_GREY
:
783 // where do these coefficients come from? maybe we
784 // should have image options for them as well?
786 (unsigned) (76.544*(unsigned)uchRed
+
787 150.272*(unsigned)uchGreen
+
788 36.864*(unsigned)uchBlue
);
790 *pData
++ = (unsigned char)((uiColor
>> 8) & 0xFF);
791 if ( iBitDepth
== 16 )
792 *pData
++ = (unsigned char)(uiColor
& 0xFF);
796 case wxPNG_TYPE_GREY_RED
:
798 if ( iBitDepth
== 16 )
805 unsigned char uchAlpha
= 255;
807 uchAlpha
= *pAlpha
++;
811 if ( (uchRed
== uchMaskRed
)
812 && (uchGreen
== uchMaskGreen
)
813 && (uchBlue
== uchMaskBlue
) )
818 if ( iBitDepth
== 16 )
823 png_bytep row_ptr
= data
;
824 png_write_rows( png_ptr
, &row_ptr
, 1 );
828 png_write_end( png_ptr
, info_ptr
);
829 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
835 #pragma warning(default:4611)
838 #endif // wxUSE_STREAMS
840 #endif // wxUSE_LIBPNG