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 #if defined(__VISAGECPP__)
60 #define PNGLINKAGEMODE _Optlink
61 #elif defined(__WATCOMC__)
62 #define PNGLINKAGEMODE _cdecl
64 #define PNGLINKAGEMODE
67 static void PNGLINKAGEMODE
_PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
69 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
72 static void PNGLINKAGEMODE
_PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
74 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
78 // so that the libpng doesn't send anything on stderr
80 PNGLINKAGEMODE
png_silent_error(png_structp png_ptr
, png_const_charp
WXUNUSED(message
))
82 #ifdef USE_FAR_KEYWORD
85 png_memcpy(jmpbuf
,png_ptr
->jmpbuf
,sizeof(jmp_buf));
89 longjmp(png_ptr
->jmpbuf
, 1);
94 PNGLINKAGEMODE
png_silent_warning(png_structp
WXUNUSED(png_ptr
), png_const_charp
WXUNUSED(message
))
98 // temporarily disable the warning C4611 (interaction between '_setjmp' and
99 // C++ object destruction is non-portable) - I don't see any dtors here
101 #pragma warning(disable:4611)
104 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
106 // VZ: as this function uses setjmp() the only fool proof error handling
107 // method is to use goto (setjmp is not really C++ dtors friendly...)
109 unsigned char **lines
;
111 png_infop info_ptr
= (png_infop
) NULL
;
115 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
117 (png_error_ptr
) NULL
,
118 (png_error_ptr
) NULL
);
122 // the file example.c explain how to guess if the stream is a png image
123 if (!verbose
) png_set_error_fn(png_ptr
, (png_voidp
)NULL
, png_silent_error
, png_silent_warning
);
125 info_ptr
= png_create_info_struct( png_ptr
);
129 if (setjmp(png_ptr
->jmpbuf
))
132 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
135 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
137 png_uint_32 width
,height
;
138 int bit_depth
,color_type
,interlace_type
;
140 png_read_info( png_ptr
, info_ptr
);
141 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
143 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
144 png_set_expand( png_ptr
);
146 png_set_strip_16( png_ptr
);
147 png_set_packing( png_ptr
);
148 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
149 png_set_expand( png_ptr
);
150 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
152 image
->Create( (int)width
, (int)height
);
157 lines
= (unsigned char **)malloc( (size_t)(height
* sizeof(unsigned char *)) );
161 for (i
= 0; i
< height
; i
++)
163 if ((lines
[i
] = (unsigned char *)malloc( (size_t)(width
* (sizeof(unsigned char) * 4)))) == NULL
)
165 for ( unsigned int n
= 0; n
< i
; n
++ )
171 // loaded successfully!
174 png_read_image( png_ptr
, lines
);
175 png_read_end( png_ptr
, info_ptr
);
176 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
177 unsigned char *ptr
= image
->GetData();
178 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
179 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
181 for (unsigned int y
= 0; y
< height
; y
++)
183 unsigned char *ptr2
= lines
[y
];
184 for (unsigned int x
= 0; x
< width
; x
++)
186 unsigned char r
= *ptr2
++;
187 unsigned char a
= *ptr2
++;
206 for (unsigned int y
= 0; y
< height
; y
++)
208 unsigned char *ptr2
= lines
[y
];
209 for (unsigned int x
= 0; x
< width
; x
++)
211 unsigned char r
= *ptr2
++;
212 unsigned char g
= *ptr2
++;
213 unsigned char b
= *ptr2
++;
214 unsigned char a
= *ptr2
++;
224 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
233 for ( unsigned int j
= 0; j
< height
; j
++ )
239 image
->SetMaskColour( 255, 0, 255 );
243 image
->SetMask( FALSE
);
250 lines
= NULL
; // called from before it was set
253 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
269 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
273 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
278 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
281 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
287 if (!verbose
) png_set_error_fn(png_ptr
, (png_voidp
)NULL
, png_silent_error
, png_silent_warning
);
289 png_infop info_ptr
= png_create_info_struct(png_ptr
);
290 if (info_ptr
== NULL
)
292 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
296 if (setjmp(png_ptr
->jmpbuf
))
298 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
302 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
304 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
305 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
306 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
313 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
314 png_write_info( png_ptr
, info_ptr
);
315 png_set_shift( png_ptr
, &sig_bit
);
316 png_set_packing( png_ptr
);
318 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
321 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
325 for (int y
= 0; y
< image
->GetHeight(); y
++)
327 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
328 for (int x
= 0; x
< image
->GetWidth(); x
++)
330 data
[(x
<< 2) + 0] = *ptr
++;
331 data
[(x
<< 2) + 1] = *ptr
++;
332 data
[(x
<< 2) + 2] = *ptr
++;
333 if (( !image
->HasMask() ) || \
334 (data
[(x
<< 2) + 0] != image
->GetMaskRed()) || \
335 (data
[(x
<< 2) + 1] != image
->GetMaskGreen()) || \
336 (data
[(x
<< 2) + 2] != image
->GetMaskBlue()))
338 data
[(x
<< 2) + 3] = 255;
342 data
[(x
<< 2) + 3] = 0;
345 png_bytep row_ptr
= data
;
346 png_write_rows( png_ptr
, &row_ptr
, 1 );
350 png_write_end( png_ptr
, info_ptr
);
351 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
357 #pragma warning(default:4611)
360 bool wxPNGHandler::DoCanRead( wxInputStream
& stream
)
362 unsigned char hdr
[4];
365 stream
.SeekI(-4, wxFromCurrent
);
366 return (hdr
[0] == 0x89 && hdr
[1] == 'P' && hdr
[2] == 'N' && hdr
[3] == 'G');
369 #endif // wxUSE_STREAMS
371 #endif // wxUSE_LIBPNG