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
60 #define PNGLINKAGEMODE LINKAGEMODE
64 // VS: wxPNGInfoStruct declared below is a hack that needs some explanation.
65 // First, let me describe what's the problem: libpng uses jmp_buf in
66 // its png_struct structure. Unfortunately, this structure is
67 // compiler-specific and may vary in size, so if you use libpng compiled
68 // as DLL with another compiler than the main executable, it may not work
69 // (this is for example the case with wxMGL port and SciTech MGL library
70 // that provides custom runtime-loadable libpng implementation with jmpbuf
71 // disabled altogether). Luckily, it is still possible to use setjmp() &
72 // longjmp() as long as the structure is not part of png_struct.
74 // Sadly, there's no clean way to attach user-defined data to png_struct.
75 // There is only one customizable place, png_struct.io_ptr, which is meant
76 // only for I/O routines and is set with png_set_read_fn or
77 // png_set_write_fn. The hacky part is that we use io_ptr to store
78 // a pointer to wxPNGInfoStruct that holds I/O structures _and_ jmp_buf.
80 struct wxPNGInfoStruct
92 #define WX_PNG_INFO(png_ptr) ((wxPNGInfoStruct*)png_get_io_ptr(png_ptr))
98 void PNGLINKAGEMODE
_PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
100 WX_PNG_INFO(png_ptr
)->stream
.in
->Read(data
, length
);
103 void PNGLINKAGEMODE
_PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
105 WX_PNG_INFO(png_ptr
)->stream
.out
->Write(data
, length
);
109 // so that the libpng doesn't send anything on stderr
111 PNGLINKAGEMODE
wx_png_error(png_structp png_ptr
, png_const_charp message
)
113 wxPNGInfoStruct
*info
= WX_PNG_INFO(png_ptr
);
115 wxLogError( wxString::FromAscii(message
) );
117 #ifdef USE_FAR_KEYWORD
120 png_memcpy(jmpbuf
,info
->jmpbuf
,sizeof(jmp_buf));
124 longjmp(info
->jmpbuf
, 1);
129 PNGLINKAGEMODE
wx_png_warning(png_structp png_ptr
, png_const_charp message
)
131 wxPNGInfoStruct
*info
= WX_PNG_INFO(png_ptr
);
133 wxLogWarning( wxString::FromAscii(message
) );
138 // temporarily disable the warning C4611 (interaction between '_setjmp' and
139 // C++ object destruction is non-portable) - I don't see any dtors here
141 #pragma warning(disable:4611)
144 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
146 // VZ: as this function uses setjmp() the only fool proof error handling
147 // method is to use goto (setjmp is not really C++ dtors friendly...)
149 unsigned char **lines
;
151 png_infop info_ptr
= (png_infop
) NULL
;
152 wxPNGInfoStruct wxinfo
;
154 wxinfo
.verbose
= verbose
;
155 wxinfo
.stream
.in
= &stream
;
159 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
161 (png_error_ptr
) NULL
,
162 (png_error_ptr
) NULL
);
166 png_set_error_fn(png_ptr
, (png_voidp
)NULL
, wx_png_error
, wx_png_warning
);
168 // NB: please see the comment near wxPNGInfoStruct declaration for
169 // explanation why this line is mandatory
170 png_set_read_fn( png_ptr
, &wxinfo
, _PNG_stream_reader
);
172 info_ptr
= png_create_info_struct( png_ptr
);
176 if (setjmp(wxinfo
.jmpbuf
))
179 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
182 png_uint_32 width
,height
;
183 int bit_depth
,color_type
,interlace_type
;
185 png_read_info( png_ptr
, info_ptr
);
186 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
188 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
189 png_set_expand( png_ptr
);
191 // Fix for Bug [ 439207 ] Monochrome PNG images come up black
193 png_set_expand( png_ptr
);
195 png_set_strip_16( png_ptr
);
196 png_set_packing( png_ptr
);
197 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
198 png_set_expand( png_ptr
);
199 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
201 image
->Create( (int)width
, (int)height
);
206 lines
= (unsigned char **)malloc( (size_t)(height
* sizeof(unsigned char *)) );
210 for (i
= 0; i
< height
; i
++)
212 if ((lines
[i
] = (unsigned char *)malloc( (size_t)(width
* (sizeof(unsigned char) * 4)))) == NULL
)
214 for ( unsigned int n
= 0; n
< i
; n
++ )
220 // loaded successfully!
223 png_read_image( png_ptr
, lines
);
224 png_read_end( png_ptr
, info_ptr
);
225 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
226 unsigned char *ptr
= image
->GetData();
227 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
228 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
230 for (unsigned int y
= 0; y
< height
; y
++)
232 unsigned char *ptr2
= lines
[y
];
233 for (unsigned int x
= 0; x
< width
; x
++)
235 unsigned char r
= *ptr2
++;
236 unsigned char a
= *ptr2
++;
255 for (unsigned int y
= 0; y
< height
; y
++)
257 unsigned char *ptr2
= lines
[y
];
258 for (unsigned int x
= 0; x
< width
; x
++)
260 unsigned char r
= *ptr2
++;
261 unsigned char g
= *ptr2
++;
262 unsigned char b
= *ptr2
++;
263 unsigned char a
= *ptr2
++;
273 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
282 for ( unsigned int j
= 0; j
< height
; j
++ )
288 image
->SetMaskColour( 255, 0, 255 );
292 image
->SetMask( FALSE
);
299 lines
= NULL
; // called from before it was set
302 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
318 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
322 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
327 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
329 wxPNGInfoStruct wxinfo
;
331 wxinfo
.verbose
= verbose
;
332 wxinfo
.stream
.out
= &stream
;
334 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
338 wxLogError(_("Couldn't save PNG image."));
342 png_set_error_fn(png_ptr
, (png_voidp
)NULL
, wx_png_error
, wx_png_warning
);
344 png_infop info_ptr
= png_create_info_struct(png_ptr
);
345 if (info_ptr
== NULL
)
347 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
349 wxLogError(_("Couldn't save PNG image."));
353 if (setjmp(wxinfo
.jmpbuf
))
355 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
357 wxLogError(_("Couldn't save PNG image."));
361 // NB: please see the comment near wxPNGInfoStruct declaration for
362 // explanation why this line is mandatory
363 png_set_write_fn( png_ptr
, &wxinfo
, _PNG_stream_writer
, NULL
);
365 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
366 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
367 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
374 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
375 png_write_info( png_ptr
, info_ptr
);
376 png_set_shift( png_ptr
, &sig_bit
);
377 png_set_packing( png_ptr
);
379 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
382 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
386 for (int y
= 0; y
< image
->GetHeight(); y
++)
388 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
389 for (int x
= 0; x
< image
->GetWidth(); x
++)
391 data
[(x
<< 2) + 0] = *ptr
++;
392 data
[(x
<< 2) + 1] = *ptr
++;
393 data
[(x
<< 2) + 2] = *ptr
++;
394 if (( !image
->HasMask() ) || \
395 (data
[(x
<< 2) + 0] != image
->GetMaskRed()) || \
396 (data
[(x
<< 2) + 1] != image
->GetMaskGreen()) || \
397 (data
[(x
<< 2) + 2] != image
->GetMaskBlue()))
399 data
[(x
<< 2) + 3] = 255;
403 data
[(x
<< 2) + 3] = 0;
406 png_bytep row_ptr
= data
;
407 png_write_rows( png_ptr
, &row_ptr
, 1 );
411 png_write_end( png_ptr
, info_ptr
);
412 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
418 #pragma warning(default:4611)
421 bool wxPNGHandler::DoCanRead( wxInputStream
& stream
)
423 unsigned char hdr
[4];
425 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
428 return memcmp(hdr
, "\211PNG", WXSIZEOF(hdr
)) == 0;
431 #endif // wxUSE_STREAMS
433 #endif // wxUSE_LIBPNG