1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage PNG handler
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "imagpng.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
25 #if wxUSE_IMAGE && wxUSE_LIBPNG
27 #include "wx/imagpng.h"
28 #include "wx/bitmap.h"
33 #include "wx/filefn.h"
34 #include "wx/wfstream.h"
36 #include "wx/module.h"
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
59 #ifndef PNGLINKAGEMODE
61 // we need an explicit cdecl for Watcom, at least according to
63 // http://sf.net/tracker/index.php?func=detail&aid=651492&group_id=9863&atid=109863
65 // more testing is needed for this however, please remove this comment
66 // if you can confirm that my fix works with Watcom 11
67 #define PNGLINKAGEMODE cdecl
69 #define PNGLINKAGEMODE LINKAGEMODE
74 // VS: wxPNGInfoStruct declared below is a hack that needs some explanation.
75 // First, let me describe what's the problem: libpng uses jmp_buf in
76 // its png_struct structure. Unfortunately, this structure is
77 // compiler-specific and may vary in size, so if you use libpng compiled
78 // as DLL with another compiler than the main executable, it may not work
79 // (this is for example the case with wxMGL port and SciTech MGL library
80 // that provides custom runtime-loadable libpng implementation with jmpbuf
81 // disabled altogether). Luckily, it is still possible to use setjmp() &
82 // longjmp() as long as the structure is not part of png_struct.
84 // Sadly, there's no clean way to attach user-defined data to png_struct.
85 // There is only one customizable place, png_struct.io_ptr, which is meant
86 // only for I/O routines and is set with png_set_read_fn or
87 // png_set_write_fn. The hacky part is that we use io_ptr to store
88 // a pointer to wxPNGInfoStruct that holds I/O structures _and_ jmp_buf.
90 struct wxPNGInfoStruct
102 #define WX_PNG_INFO(png_ptr) ((wxPNGInfoStruct*)png_get_io_ptr(png_ptr))
108 void PNGLINKAGEMODE
_PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
110 WX_PNG_INFO(png_ptr
)->stream
.in
->Read(data
, length
);
113 void PNGLINKAGEMODE
_PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
115 WX_PNG_INFO(png_ptr
)->stream
.out
->Write(data
, length
);
119 // so that the libpng doesn't send anything on stderr
121 PNGLINKAGEMODE
wx_png_error(png_structp png_ptr
, png_const_charp message
)
123 wxPNGInfoStruct
*info
= WX_PNG_INFO(png_ptr
);
125 wxLogError( wxString::FromAscii(message
) );
127 #ifdef USE_FAR_KEYWORD
130 png_memcpy(jmpbuf
,info
->jmpbuf
,sizeof(jmp_buf));
134 longjmp(info
->jmpbuf
, 1);
139 PNGLINKAGEMODE
wx_png_warning(png_structp png_ptr
, png_const_charp message
)
141 wxPNGInfoStruct
*info
= WX_PNG_INFO(png_ptr
);
143 wxLogWarning( wxString::FromAscii(message
) );
148 // temporarily disable the warning C4611 (interaction between '_setjmp' and
149 // C++ object destruction is non-portable) - I don't see any dtors here
151 #pragma warning(disable:4611)
154 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
156 // VZ: as this function uses setjmp() the only fool proof error handling
157 // method is to use goto (setjmp is not really C++ dtors friendly...)
159 unsigned char **lines
;
161 png_infop info_ptr
= (png_infop
) NULL
;
162 wxPNGInfoStruct wxinfo
;
164 wxinfo
.verbose
= verbose
;
165 wxinfo
.stream
.in
= &stream
;
169 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
171 (png_error_ptr
) NULL
,
172 (png_error_ptr
) NULL
);
176 png_set_error_fn(png_ptr
, (png_voidp
)NULL
, wx_png_error
, wx_png_warning
);
178 // NB: please see the comment near wxPNGInfoStruct declaration for
179 // explanation why this line is mandatory
180 png_set_read_fn( png_ptr
, &wxinfo
, _PNG_stream_reader
);
182 info_ptr
= png_create_info_struct( png_ptr
);
186 if (setjmp(wxinfo
.jmpbuf
))
189 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
192 png_uint_32 width
,height
;
193 int bit_depth
,color_type
,interlace_type
;
195 png_read_info( png_ptr
, info_ptr
);
196 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
198 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
199 png_set_expand( png_ptr
);
201 // Fix for Bug [ 439207 ] Monochrome PNG images come up black
203 png_set_expand( png_ptr
);
205 png_set_strip_16( png_ptr
);
206 png_set_packing( png_ptr
);
207 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
208 png_set_expand( png_ptr
);
209 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
211 image
->Create( (int)width
, (int)height
);
216 lines
= (unsigned char **)malloc( (size_t)(height
* sizeof(unsigned char *)) );
220 for (i
= 0; i
< height
; i
++)
222 if ((lines
[i
] = (unsigned char *)malloc( (size_t)(width
* (sizeof(unsigned char) * 4)))) == NULL
)
224 for ( unsigned int n
= 0; n
< i
; n
++ )
230 // loaded successfully!
233 png_read_image( png_ptr
, lines
);
234 png_read_end( png_ptr
, info_ptr
);
235 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
236 unsigned char *ptr
= image
->GetData();
237 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
238 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
240 for (unsigned int y
= 0; y
< height
; y
++)
242 unsigned char *ptr2
= lines
[y
];
243 for (unsigned int x
= 0; x
< width
; x
++)
245 unsigned char r
= *ptr2
++;
246 unsigned char a
= *ptr2
++;
265 for (unsigned int y
= 0; y
< height
; y
++)
267 unsigned char *ptr2
= lines
[y
];
268 for (unsigned int x
= 0; x
< width
; x
++)
270 unsigned char r
= *ptr2
++;
271 unsigned char g
= *ptr2
++;
272 unsigned char b
= *ptr2
++;
273 unsigned char a
= *ptr2
++;
283 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
292 for ( unsigned int j
= 0; j
< height
; j
++ )
298 image
->SetMaskColour( 255, 0, 255 );
302 image
->SetMask( FALSE
);
309 lines
= NULL
; // called from before it was set
312 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
328 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
332 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
337 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
339 wxPNGInfoStruct wxinfo
;
341 wxinfo
.verbose
= verbose
;
342 wxinfo
.stream
.out
= &stream
;
344 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
348 wxLogError(_("Couldn't save PNG image."));
352 png_set_error_fn(png_ptr
, (png_voidp
)NULL
, wx_png_error
, wx_png_warning
);
354 png_infop info_ptr
= png_create_info_struct(png_ptr
);
355 if (info_ptr
== NULL
)
357 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
359 wxLogError(_("Couldn't save PNG image."));
363 if (setjmp(wxinfo
.jmpbuf
))
365 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
367 wxLogError(_("Couldn't save PNG image."));
371 // NB: please see the comment near wxPNGInfoStruct declaration for
372 // explanation why this line is mandatory
373 png_set_write_fn( png_ptr
, &wxinfo
, _PNG_stream_writer
, NULL
);
375 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
376 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
377 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
384 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
385 png_write_info( png_ptr
, info_ptr
);
386 png_set_shift( png_ptr
, &sig_bit
);
387 png_set_packing( png_ptr
);
389 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
392 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
396 for (int y
= 0; y
< image
->GetHeight(); y
++)
398 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
399 for (int x
= 0; x
< image
->GetWidth(); x
++)
401 data
[(x
<< 2) + 0] = *ptr
++;
402 data
[(x
<< 2) + 1] = *ptr
++;
403 data
[(x
<< 2) + 2] = *ptr
++;
404 if (( !image
->HasMask() ) || \
405 (data
[(x
<< 2) + 0] != image
->GetMaskRed()) || \
406 (data
[(x
<< 2) + 1] != image
->GetMaskGreen()) || \
407 (data
[(x
<< 2) + 2] != image
->GetMaskBlue()))
409 data
[(x
<< 2) + 3] = 255;
413 data
[(x
<< 2) + 3] = 0;
416 png_bytep row_ptr
= data
;
417 png_write_rows( png_ptr
, &row_ptr
, 1 );
421 png_write_end( png_ptr
, info_ptr
);
422 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
428 #pragma warning(default:4611)
431 bool wxPNGHandler::DoCanRead( wxInputStream
& stream
)
433 unsigned char hdr
[4];
435 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
438 return memcmp(hdr
, "\211PNG", WXSIZEOF(hdr
)) == 0;
441 #endif // wxUSE_STREAMS
443 #endif // wxUSE_LIBPNG