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
);
78 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
80 // VZ: as this function uses setjmp() the only fool proof error handling
81 // method is to use goto (setjmp is not really C++ dtors friendly...)
83 unsigned char **lines
;
85 png_infop info_ptr
= (png_infop
) NULL
;
89 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
92 (png_error_ptr
) NULL
);
96 info_ptr
= png_create_info_struct( png_ptr
);
100 if (setjmp(png_ptr
->jmpbuf
))
103 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
106 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
108 png_uint_32 width
,height
;
109 int bit_depth
,color_type
,interlace_type
;
111 png_read_info( png_ptr
, info_ptr
);
112 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
114 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
115 png_set_expand( png_ptr
);
117 png_set_strip_16( png_ptr
);
118 png_set_packing( png_ptr
);
119 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
120 png_set_expand( png_ptr
);
121 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
123 image
->Create( width
, height
);
128 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
132 for (i
= 0; i
< height
; i
++)
134 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
136 for ( unsigned int n
= 0; n
< i
; n
++ )
142 // loaded successfully!
145 png_read_image( png_ptr
, lines
);
146 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
147 unsigned char *ptr
= image
->GetData();
148 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
149 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
151 for (unsigned int y
= 0; y
< height
; y
++)
153 unsigned char *ptr2
= lines
[y
];
154 for (unsigned int x
= 0; x
< width
; x
++)
156 unsigned char r
= *ptr2
++;
157 unsigned char a
= *ptr2
++;
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 g
= *ptr2
++;
183 unsigned char b
= *ptr2
++;
184 unsigned char a
= *ptr2
++;
194 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
203 for ( unsigned int j
= 0; j
< height
; j
++ )
209 image
->SetMaskColour( 255, 0, 255 );
213 image
->SetMask( FALSE
);
220 lines
= NULL
; // called from before it was set
222 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
238 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
242 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
248 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
251 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
257 png_infop info_ptr
= png_create_info_struct(png_ptr
);
258 if (info_ptr
== NULL
)
260 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
264 if (setjmp(png_ptr
->jmpbuf
))
266 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
270 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
272 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
273 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
274 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
281 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
282 png_write_info( png_ptr
, info_ptr
);
283 png_set_shift( png_ptr
, &sig_bit
);
284 png_set_packing( png_ptr
);
286 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
289 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
293 for (int y
= 0; y
< image
->GetHeight(); y
++)
295 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
296 for (int x
= 0; x
< image
->GetWidth(); x
++)
298 data
[(x
<< 2) + 0] = *ptr
++;
299 data
[(x
<< 2) + 1] = *ptr
++;
300 data
[(x
<< 2) + 2] = *ptr
++;
301 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
302 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
303 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
305 data
[(x
<< 2) + 3] = 0;
309 data
[(x
<< 2) + 3] = 255;
312 png_bytep row_ptr
= data
;
313 png_write_rows( png_ptr
, &row_ptr
, 1 );
317 png_write_end( png_ptr
, info_ptr
);
318 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);