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"
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 LINKAGEMODE _Optlink
65 static void LINKAGEMODE
_PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
67 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
70 static void LINKAGEMODE
_PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
72 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
76 // so that the libpng doesn't send anything on stderr
78 LINKAGEMODE
png_silent_error(png_structp png_ptr
, png_const_charp
WXUNUSED(message
))
80 #ifdef USE_FAR_KEYWORD
83 png_memcpy(jmpbuf
,png_ptr
->jmpbuf
,sizeof(jmp_buf));
87 longjmp(png_ptr
->jmpbuf
, 1);
92 LINKAGEMODE
png_silent_warning(png_structp
WXUNUSED(png_ptr
), png_const_charp
WXUNUSED(message
))
96 // temporarily disable the warning C4611 (interaction between '_setjmp' and
97 // C++ object destruction is non-portable) - I don't see any dtors here
99 #pragma warning(disable:4611)
102 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
104 // VZ: as this function uses setjmp() the only fool proof error handling
105 // method is to use goto (setjmp is not really C++ dtors friendly...)
107 unsigned char **lines
;
109 png_infop info_ptr
= (png_infop
) NULL
;
113 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
115 (png_error_ptr
) NULL
,
116 (png_error_ptr
) NULL
);
120 // the file example.c explain how to guess if the stream is a png image
121 if (!verbose
) png_set_error_fn(png_ptr
, (png_voidp
)NULL
, png_silent_error
, png_silent_warning
);
123 info_ptr
= png_create_info_struct( png_ptr
);
127 if (setjmp(png_ptr
->jmpbuf
))
130 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
133 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
135 png_uint_32 width
,height
;
136 int bit_depth
,color_type
,interlace_type
;
138 png_read_info( png_ptr
, info_ptr
);
139 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
141 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
142 png_set_expand( png_ptr
);
144 png_set_strip_16( png_ptr
);
145 png_set_packing( png_ptr
);
146 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
147 png_set_expand( png_ptr
);
148 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
150 image
->Create( (int)width
, (int)height
);
155 lines
= (unsigned char **)malloc( (size_t)(height
* sizeof(unsigned char *)) );
159 for (i
= 0; i
< height
; i
++)
161 if ((lines
[i
] = (unsigned char *)malloc( (size_t)(width
* (sizeof(unsigned char) * 4)))) == NULL
)
163 for ( unsigned int n
= 0; n
< i
; n
++ )
169 // loaded successfully!
172 png_read_image( png_ptr
, lines
);
173 png_read_end( png_ptr
, info_ptr
);
174 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
175 unsigned char *ptr
= image
->GetData();
176 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
177 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
179 for (unsigned int y
= 0; y
< height
; y
++)
181 unsigned char *ptr2
= lines
[y
];
182 for (unsigned int x
= 0; x
< width
; x
++)
184 unsigned char r
= *ptr2
++;
185 unsigned char a
= *ptr2
++;
204 for (unsigned int y
= 0; y
< height
; y
++)
206 unsigned char *ptr2
= lines
[y
];
207 for (unsigned int x
= 0; x
< width
; x
++)
209 unsigned char r
= *ptr2
++;
210 unsigned char g
= *ptr2
++;
211 unsigned char b
= *ptr2
++;
212 unsigned char a
= *ptr2
++;
222 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
231 for ( unsigned int j
= 0; j
< height
; j
++ )
237 image
->SetMaskColour( 255, 0, 255 );
241 image
->SetMask( FALSE
);
248 lines
= NULL
; // called from before it was set
251 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
267 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
271 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
276 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
279 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
285 if (!verbose
) png_set_error_fn(png_ptr
, (png_voidp
)NULL
, png_silent_error
, png_silent_warning
);
287 png_infop info_ptr
= png_create_info_struct(png_ptr
);
288 if (info_ptr
== NULL
)
290 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
294 if (setjmp(png_ptr
->jmpbuf
))
296 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
300 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
302 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
303 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
304 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
311 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
312 png_write_info( png_ptr
, info_ptr
);
313 png_set_shift( png_ptr
, &sig_bit
);
314 png_set_packing( png_ptr
);
316 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
319 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
323 for (int y
= 0; y
< image
->GetHeight(); y
++)
325 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
326 for (int x
= 0; x
< image
->GetWidth(); x
++)
328 data
[(x
<< 2) + 0] = *ptr
++;
329 data
[(x
<< 2) + 1] = *ptr
++;
330 data
[(x
<< 2) + 2] = *ptr
++;
331 if (( !image
->HasMask() ) || \
332 (data
[(x
<< 2) + 0] != image
->GetMaskRed()) || \
333 (data
[(x
<< 2) + 1] != image
->GetMaskGreen()) || \
334 (data
[(x
<< 2) + 2] != image
->GetMaskBlue()))
336 data
[(x
<< 2) + 3] = 255;
340 data
[(x
<< 2) + 3] = 0;
343 png_bytep row_ptr
= data
;
344 png_write_rows( png_ptr
, &row_ptr
, 1 );
348 png_write_end( png_ptr
, info_ptr
);
349 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
355 #pragma warning(default:4611)
358 bool wxPNGHandler::DoCanRead( wxInputStream
& stream
)
360 unsigned char hdr
[4];
362 stream
.Read(&hdr
, 4);
363 stream
.SeekI(-4, wxFromCurrent
);
364 return (hdr
[0] == 0x89 && hdr
[1] == 'P' && hdr
[2] == 'N' && hdr
[3] == 'G');
367 #endif // wxUSE_STREAMS
369 #endif // wxUSE_LIBPNG