1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage PNG handler
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 We don't put pragma implement in this file because it is already present in
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
29 #include "wx/bitmap.h"
34 #include "wx/filefn.h"
35 #include "wx/wfstream.h"
37 #include "wx/module.h"
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 #if !USE_SHARED_LIBRARIES
57 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
62 #if defined(__VISAGECPP__)
63 #define LINKAGEMODE _Optlink
68 static void LINKAGEMODE
_PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
70 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
73 static void LINKAGEMODE
_PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
75 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
79 // so that the libpng doesn't send anything on stderr
81 LINKAGEMODE
png_silent_error(png_structp png_ptr
, png_const_charp
WXUNUSED(message
))
83 #ifdef USE_FAR_KEYWORD
86 png_memcpy(jmpbuf
,png_ptr
->jmpbuf
,sizeof(jmp_buf));
90 longjmp(png_ptr
->jmpbuf
, 1);
95 LINKAGEMODE
png_silent_warning(png_structp
WXUNUSED(png_ptr
), png_const_charp
WXUNUSED(message
))
99 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
101 // VZ: as this function uses setjmp() the only fool proof error handling
102 // method is to use goto (setjmp is not really C++ dtors friendly...)
104 unsigned char **lines
;
106 png_infop info_ptr
= (png_infop
) NULL
;
110 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
112 (png_error_ptr
) NULL
,
113 (png_error_ptr
) NULL
);
117 // the file example.c explain how to guess if the stream is a png image
118 if (!verbose
) png_set_error_fn(png_ptr
, (png_voidp
)NULL
, png_silent_error
, png_silent_warning
);
120 info_ptr
= png_create_info_struct( png_ptr
);
124 if (setjmp(png_ptr
->jmpbuf
))
127 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
130 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
132 png_uint_32 width
,height
;
133 int bit_depth
,color_type
,interlace_type
;
135 png_read_info( png_ptr
, info_ptr
);
136 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
138 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
139 png_set_expand( png_ptr
);
141 png_set_strip_16( png_ptr
);
142 png_set_packing( png_ptr
);
143 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
144 png_set_expand( png_ptr
);
145 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
147 image
->Create( width
, height
);
152 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
156 for (i
= 0; i
< height
; i
++)
158 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
160 for ( unsigned int n
= 0; n
< i
; n
++ )
166 // loaded successfully!
169 png_read_image( png_ptr
, lines
);
170 png_read_end( png_ptr
, info_ptr
);
171 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
172 unsigned char *ptr
= image
->GetData();
173 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
174 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
176 for (unsigned int y
= 0; y
< height
; y
++)
178 unsigned char *ptr2
= lines
[y
];
179 for (unsigned int x
= 0; x
< width
; x
++)
181 unsigned char r
= *ptr2
++;
182 unsigned char a
= *ptr2
++;
201 for (unsigned int y
= 0; y
< height
; y
++)
203 unsigned char *ptr2
= lines
[y
];
204 for (unsigned int x
= 0; x
< width
; x
++)
206 unsigned char r
= *ptr2
++;
207 unsigned char g
= *ptr2
++;
208 unsigned char b
= *ptr2
++;
209 unsigned char a
= *ptr2
++;
219 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
228 for ( unsigned int j
= 0; j
< height
; j
++ )
234 image
->SetMaskColour( 255, 0, 255 );
238 image
->SetMask( FALSE
);
245 lines
= NULL
; // called from before it was set
248 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
264 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
268 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
274 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
277 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
283 if (!verbose
) png_set_error_fn(png_ptr
, (png_voidp
)NULL
, png_silent_error
, png_silent_warning
);
285 png_infop info_ptr
= png_create_info_struct(png_ptr
);
286 if (info_ptr
== NULL
)
288 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
292 if (setjmp(png_ptr
->jmpbuf
))
294 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
298 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
300 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
301 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
302 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
309 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
310 png_write_info( png_ptr
, info_ptr
);
311 png_set_shift( png_ptr
, &sig_bit
);
312 png_set_packing( png_ptr
);
314 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
317 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
321 for (int y
= 0; y
< image
->GetHeight(); y
++)
323 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
324 for (int x
= 0; x
< image
->GetWidth(); x
++)
326 data
[(x
<< 2) + 0] = *ptr
++;
327 data
[(x
<< 2) + 1] = *ptr
++;
328 data
[(x
<< 2) + 2] = *ptr
++;
329 if (( !image
->HasMask() ) || \
330 (data
[(x
<< 2) + 0] != image
->GetMaskRed()) || \
331 (data
[(x
<< 2) + 1] != image
->GetMaskGreen()) || \
332 (data
[(x
<< 2) + 2] != image
->GetMaskBlue()))
334 data
[(x
<< 2) + 3] = 255;
338 data
[(x
<< 2) + 3] = 0;
341 png_bytep row_ptr
= data
;
342 png_write_rows( png_ptr
, &row_ptr
, 1 );
346 png_write_end( png_ptr
, info_ptr
);
347 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
352 bool wxPNGHandler::DoCanRead( wxInputStream
& stream
)
354 unsigned char hdr
[4];
356 stream
.Read(&hdr
, 4);
357 stream
.SeekI(-4, wxFromCurrent
);
358 return (hdr
[0] == 0x89 && hdr
[1] == 'P' && hdr
[2] == 'N' && hdr
[3] == 'G');
361 #endif // wxUSE_STREAMS
363 #endif // wxUSE_LIBPNG