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 #pragma implementation "image.h"
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
27 #include "wx/bitmap.h"
32 #include "../png/png.h"
34 #include "wx/filefn.h"
35 #include "wx/wfstream.h"
37 #include "wx/module.h"
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
58 #if !USE_SHARED_LIBRARIES
59 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
64 static void _PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
66 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
69 static void _PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
71 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
74 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
76 // VZ: as this function uses setjmp() the only fool proof error handling
77 // method is to use goto (setjmp is not really C++ dtors friendly...)
79 unsigned char **lines
= (unsigned char **) NULL
;
81 png_infop info_ptr
= (png_infop
) NULL
;
85 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
88 (png_error_ptr
) NULL
);
92 info_ptr
= png_create_info_struct( png_ptr
);
96 if (setjmp(png_ptr
->jmpbuf
))
99 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
102 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
104 png_uint_32 width
,height
;
105 int bit_depth
,color_type
,interlace_type
;
107 png_read_info( png_ptr
, info_ptr
);
108 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
110 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
111 png_set_expand( png_ptr
);
113 png_set_strip_16( png_ptr
);
114 png_set_packing( png_ptr
);
115 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
116 png_set_expand( png_ptr
);
117 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
119 image
->Create( width
, height
);
124 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
128 for (i
= 0; i
< height
; i
++)
130 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
132 for ( unsigned int n
= 0; n
< i
; n
++ )
138 // loaded successfully!
141 png_read_image( png_ptr
, lines
);
142 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
143 unsigned char *ptr
= image
->GetData();
144 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
145 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
147 for (unsigned int y
= 0; y
< height
; y
++)
149 unsigned char *ptr2
= lines
[y
];
150 for (unsigned int x
= 0; x
< width
; x
++)
152 unsigned char r
= *ptr2
++;
153 unsigned char a
= *ptr2
++;
172 for (unsigned int y
= 0; y
< height
; y
++)
174 unsigned char *ptr2
= lines
[y
];
175 for (unsigned int x
= 0; x
< width
; x
++)
177 unsigned char r
= *ptr2
++;
178 unsigned char g
= *ptr2
++;
179 unsigned char b
= *ptr2
++;
180 unsigned char a
= *ptr2
++;
190 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
199 for ( unsigned int j
= 0; j
< height
; j
++ )
205 image
->SetMaskColour( 255, 0, 255 );
209 image
->SetMask( FALSE
);
216 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
232 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
236 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
242 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
245 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
251 png_infop info_ptr
= png_create_info_struct(png_ptr
);
252 if (info_ptr
== NULL
)
254 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
258 if (setjmp(png_ptr
->jmpbuf
))
260 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
264 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
266 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
267 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
268 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
275 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
276 png_write_info( png_ptr
, info_ptr
);
277 png_set_shift( png_ptr
, &sig_bit
);
278 png_set_packing( png_ptr
);
280 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
283 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
287 for (int y
= 0; y
< image
->GetHeight(); y
++)
289 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
290 for (int x
= 0; x
< image
->GetWidth(); x
++)
292 data
[(x
<< 2) + 0] = *ptr
++;
293 data
[(x
<< 2) + 1] = *ptr
++;
294 data
[(x
<< 2) + 2] = *ptr
++;
295 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
296 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
297 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
299 data
[(x
<< 2) + 3] = 0;
303 data
[(x
<< 2) + 3] = 255;
306 png_bytep row_ptr
= data
;
307 png_write_rows( png_ptr
, &row_ptr
, 1 );
311 png_write_end( png_ptr
, info_ptr
);
312 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
316 #endif // wxUSE_STREAMS