1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage PNG handler
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "image.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
22 #include "wx/bitmap.h"
27 #include "../png/png.h"
29 #include "wx/filefn.h"
30 #include "wx/wfstream.h"
32 #include "wx/module.h"
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
53 #if !USE_SHARED_LIBRARIES
54 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler
,wxImageHandler
)
59 static void _PNG_stream_reader( png_structp png_ptr
, png_bytep data
, png_size_t length
)
61 ((wxInputStream
*) png_get_io_ptr( png_ptr
)) -> Read(data
, length
);
64 static void _PNG_stream_writer( png_structp png_ptr
, png_bytep data
, png_size_t length
)
66 ((wxOutputStream
*) png_get_io_ptr( png_ptr
)) -> Write(data
, length
);
69 bool wxPNGHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
)
71 // VZ: as this function uses setjmp() the only fool proof error handling
72 // method is to use goto (setjmp is not really C++ dtors friendly...)
74 unsigned char **lines
= (unsigned char **) NULL
;
76 png_infop info_ptr
= (png_infop
) NULL
;
80 png_structp png_ptr
= png_create_read_struct( PNG_LIBPNG_VER_STRING
,
83 (png_error_ptr
) NULL
);
87 info_ptr
= png_create_info_struct( png_ptr
);
91 if (setjmp(png_ptr
->jmpbuf
))
94 if (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
97 png_set_read_fn( png_ptr
, &stream
, _PNG_stream_reader
);
99 png_uint_32 width
,height
;
100 int bit_depth
,color_type
,interlace_type
;
102 png_read_info( png_ptr
, info_ptr
);
103 png_get_IHDR( png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
, &interlace_type
, (int*) NULL
, (int*) NULL
);
105 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
106 png_set_expand( png_ptr
);
108 png_set_strip_16( png_ptr
);
109 png_set_packing( png_ptr
);
110 if (png_get_valid( png_ptr
, info_ptr
, PNG_INFO_tRNS
))
111 png_set_expand( png_ptr
);
112 png_set_filler( png_ptr
, 0xff, PNG_FILLER_AFTER
);
114 image
->Create( width
, height
);
119 lines
= (unsigned char **)malloc( height
* sizeof(unsigned char *) );
123 for (i
= 0; i
< height
; i
++)
125 if ((lines
[i
] = (unsigned char *)malloc(width
* (sizeof(unsigned char) * 4))) == NULL
)
127 for ( unsigned int n
= 0; n
< i
; n
++ )
133 // loaded successfully!
136 png_read_image( png_ptr
, lines
);
137 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
138 unsigned char *ptr
= image
->GetData();
139 if ((color_type
== PNG_COLOR_TYPE_GRAY
) ||
140 (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
142 for (unsigned int y
= 0; y
< height
; y
++)
144 unsigned char *ptr2
= lines
[y
];
145 for (unsigned int x
= 0; x
< width
; x
++)
147 unsigned char r
= *ptr2
++;
148 unsigned char a
= *ptr2
++;
167 for (unsigned int y
= 0; y
< height
; y
++)
169 unsigned char *ptr2
= lines
[y
];
170 for (unsigned int x
= 0; x
< width
; x
++)
172 unsigned char r
= *ptr2
++;
173 unsigned char g
= *ptr2
++;
174 unsigned char b
= *ptr2
++;
175 unsigned char a
= *ptr2
++;
185 if ((r
== 255) && (g
== 0) && (b
== 255)) r
= 254;
194 for ( unsigned int j
= 0; j
< height
; j
++ )
200 image
->SetMaskColour( 255, 0, 255 );
204 image
->SetMask( FALSE
);
211 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
227 png_destroy_read_struct( &png_ptr
, &info_ptr
, (png_infopp
) NULL
);
231 png_destroy_read_struct( &png_ptr
, (png_infopp
) NULL
, (png_infopp
) NULL
);
237 bool wxPNGHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
)
240 png_structp png_ptr
= png_create_write_struct( PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
246 png_infop info_ptr
= png_create_info_struct(png_ptr
);
247 if (info_ptr
== NULL
)
249 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
253 if (setjmp(png_ptr
->jmpbuf
))
255 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
259 png_set_write_fn( png_ptr
, &stream
, _PNG_stream_writer
, NULL
);
261 png_set_IHDR( png_ptr
, info_ptr
, image
->GetWidth(), image
->GetHeight(), 8,
262 PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
,
263 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
270 png_set_sBIT( png_ptr
, info_ptr
, &sig_bit
);
271 png_write_info( png_ptr
, info_ptr
);
272 png_set_shift( png_ptr
, &sig_bit
);
273 png_set_packing( png_ptr
);
275 unsigned char *data
= (unsigned char *)malloc( image
->GetWidth()*4 );
278 png_destroy_write_struct( &png_ptr
, (png_infopp
)NULL
);
282 for (int y
= 0; y
< image
->GetHeight(); y
++)
284 unsigned char *ptr
= image
->GetData() + (y
* image
->GetWidth() * 3);
285 for (int x
= 0; x
< image
->GetWidth(); x
++)
287 data
[(x
<< 2) + 0] = *ptr
++;
288 data
[(x
<< 2) + 1] = *ptr
++;
289 data
[(x
<< 2) + 2] = *ptr
++;
290 if ((data
[(x
<< 2) + 0] == image
->GetMaskRed()) &&
291 (data
[(x
<< 2) + 1] == image
->GetMaskGreen()) &&
292 (data
[(x
<< 2) + 2] == image
->GetMaskBlue()))
294 data
[(x
<< 2) + 3] = 0;
298 data
[(x
<< 2) + 3] = 255;
301 png_bytep row_ptr
= data
;
302 png_write_rows( png_ptr
, &row_ptr
, 1 );
306 png_write_end( png_ptr
, info_ptr
);
307 png_destroy_write_struct( &png_ptr
, (png_infopp
)&info_ptr
);
311 #endif // wxUSE_STREAMS