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
66 void PNGLINKAGEMODE
_PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
68 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
71 void PNGLINKAGEMODE
_PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
73 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
77 // so that the libpng doesn't send anything on stderr
79 PNGLINKAGEMODE
png_silent_error(png_structp png_ptr
, png_const_charp
WXUNUSED(message
))
81 #ifdef USE_FAR_KEYWORD
84 png_memcpy(jmpbuf
,png_ptr
->jmpbuf
,sizeof(jmp_buf));
88 longjmp(png_ptr
->jmpbuf
, 1);
93 PNGLINKAGEMODE
png_silent_warning(png_structp
WXUNUSED(png_ptr
), png_const_charp
WXUNUSED(message
))
99 // temporarily disable the warning C4611 (interaction between '_setjmp' and
100 // C++ object destruction is non-portable) - I don't see any dtors here
102 #pragma warning(disable:4611)
105 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
107 // VZ: as this function uses setjmp() the only fool proof error handling
108 // method is to use goto (setjmp is not really C++ dtors friendly...)
110 unsigned char **lines
;
112 png_infop info_ptr
= (png_infop
) NULL
;
116 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
118 (png_error_ptr
) NULL
,
119 (png_error_ptr
) NULL
);
123 // the file example.c explain how to guess if the stream is a png image
124 if (!verbose
) png_set_error_fn(png_ptr
, (png_voidp
)NULL
, png_silent_error
, png_silent_warning
);
126 info_ptr
= png_create_info_struct( png_ptr
);
130 if (setjmp(png_ptr
->jmpbuf
))
133 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
136 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
138 png_uint_32 width
,height
;
139 int bit_depth
,color_type
,interlace_type
;
141 png_read_info( png_ptr
, info_ptr
);
142 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
144 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
145 png_set_expand( png_ptr
);
147 png_set_strip_16( png_ptr
);
148 png_set_packing( png_ptr
);
149 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
150 png_set_expand( png_ptr
);
151 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
153 image
->Create( (int)width
, (int)height
);
158 lines
= (unsigned char **)malloc( (size_t)(height
* sizeof(unsigned char *)) );
162 for (i
= 0; i
< height
; i
++)
164 if ((lines
[i
] = (unsigned char *)malloc( (size_t)(width
* (sizeof(unsigned char) * 4)))) == NULL
)
166 for ( unsigned int n
= 0; n
< i
; n
++ )
172 // loaded successfully!
175 png_read_image( png_ptr
, lines
);
176 png_read_end( png_ptr
, info_ptr
);
177 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
178 unsigned char *ptr
= image
->GetData();
179 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
180 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
182 for (unsigned int y
= 0; y
< height
; y
++)
184 unsigned char *ptr2
= lines
[y
];
185 for (unsigned int x
= 0; x
< width
; x
++)
187 unsigned char r
= *ptr2
++;
188 unsigned char a
= *ptr2
++;
207 for (unsigned int y
= 0; y
< height
; y
++)
209 unsigned char *ptr2
= lines
[y
];
210 for (unsigned int x
= 0; x
< width
; x
++)
212 unsigned char r
= *ptr2
++;
213 unsigned char g
= *ptr2
++;
214 unsigned char b
= *ptr2
++;
215 unsigned char a
= *ptr2
++;
225 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
234 for ( unsigned int j
= 0; j
< height
; j
++ )
240 image
->SetMaskColour( 255, 0, 255 );
244 image
->SetMask( FALSE
);
251 lines
= NULL
; // called from before it was set
254 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
270 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
274 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
279 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
282 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
288 if (!verbose
) png_set_error_fn(png_ptr
, (png_voidp
)NULL
, png_silent_error
, png_silent_warning
);
290 png_infop info_ptr
= png_create_info_struct(png_ptr
);
291 if (info_ptr
== NULL
)
293 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
297 if (setjmp(png_ptr
->jmpbuf
))
299 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
303 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
305 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
306 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
307 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
314 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
315 png_write_info( png_ptr
, info_ptr
);
316 png_set_shift( png_ptr
, &sig_bit
);
317 png_set_packing( png_ptr
);
319 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
322 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
326 for (int y
= 0; y
< image
->GetHeight(); y
++)
328 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
329 for (int x
= 0; x
< image
->GetWidth(); x
++)
331 data
[(x
<< 2) + 0] = *ptr
++;
332 data
[(x
<< 2) + 1] = *ptr
++;
333 data
[(x
<< 2) + 2] = *ptr
++;
334 if (( !image
->HasMask() ) || \
335 (data
[(x
<< 2) + 0] != image
->GetMaskRed()) || \
336 (data
[(x
<< 2) + 1] != image
->GetMaskGreen()) || \
337 (data
[(x
<< 2) + 2] != image
->GetMaskBlue()))
339 data
[(x
<< 2) + 3] = 255;
343 data
[(x
<< 2) + 3] = 0;
346 png_bytep row_ptr
= data
;
347 png_write_rows( png_ptr
, &row_ptr
, 1 );
351 png_write_end( png_ptr
, info_ptr
);
352 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
358 #pragma warning(default:4611)
361 bool wxPNGHandler::DoCanRead( wxInputStream
& stream
)
363 unsigned char hdr
[4];
366 stream
.SeekI(-4, wxFromCurrent
);
367 return (hdr
[0] == 0x89 && hdr
[1] == 'P' && hdr
[2] == 'N' && hdr
[3] == 'G');
370 #endif // wxUSE_STREAMS
372 #endif // wxUSE_LIBPNG