added wxUSE_IMAGE; added write-only wxXPMHandler
[wxWidgets.git] / src / common / imagpng.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: imagepng.cpp
3 // Purpose: wxImage PNG handler
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "imagpng.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/defs.h"
23 #endif
24
25 #if wxUSE_IMAGE && wxUSE_LIBPNG
26
27 #include "wx/imagpng.h"
28 #include "wx/bitmap.h"
29 #include "wx/debug.h"
30 #include "wx/log.h"
31 #include "wx/app.h"
32 #include "png.h"
33 #include "wx/filefn.h"
34 #include "wx/wfstream.h"
35 #include "wx/intl.h"
36 #include "wx/module.h"
37
38 // For memcpy
39 #include <string.h>
40
41 #ifdef __SALFORDC__
42 #ifdef FAR
43 #undef FAR
44 #endif
45 #endif
46
47 #ifdef __WXMSW__
48 #include <windows.h>
49 #endif
50
51 //-----------------------------------------------------------------------------
52 // wxPNGHandler
53 //-----------------------------------------------------------------------------
54
55 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler)
56
57 #if wxUSE_LIBPNG
58
59 #if defined(__VISAGECPP__)
60 #define LINKAGEMODE _Optlink
61 #else
62 #define LINKAGEMODE
63 #endif
64
65 static void LINKAGEMODE _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length )
66 {
67 ((wxInputStream*) png_get_io_ptr( png_ptr )) -> Read(data, length);
68 }
69
70 static void LINKAGEMODE _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length )
71 {
72 ((wxOutputStream*) png_get_io_ptr( png_ptr )) -> Write(data, length);
73 }
74
75 // from pngerror.c
76 // so that the libpng doesn't send anything on stderr
77 void
78 LINKAGEMODE png_silent_error(png_structp png_ptr, png_const_charp WXUNUSED(message))
79 {
80 #ifdef USE_FAR_KEYWORD
81 {
82 jmp_buf jmpbuf;
83 png_memcpy(jmpbuf,png_ptr->jmpbuf,sizeof(jmp_buf));
84 longjmp(jmpbuf, 1);
85 }
86 #else
87 longjmp(png_ptr->jmpbuf, 1);
88 #endif
89 }
90
91 void
92 LINKAGEMODE png_silent_warning(png_structp WXUNUSED(png_ptr), png_const_charp WXUNUSED(message))
93 {
94 }
95
96 // temporarily disable the warning C4611 (interaction between '_setjmp' and
97 // C++ object destruction is non-portable) - I don't see any dtors here
98 #ifdef __VISUALC__
99 #pragma warning(disable:4611)
100 #endif /* VC++ */
101
102 bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
103 {
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...)
106
107 unsigned char **lines;
108 unsigned int i;
109 png_infop info_ptr = (png_infop) NULL;
110
111 image->Destroy();
112
113 png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
114 (voidp) NULL,
115 (png_error_ptr) NULL,
116 (png_error_ptr) NULL );
117 if (!png_ptr)
118 goto error_nolines;
119
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);
122
123 info_ptr = png_create_info_struct( png_ptr );
124 if (!info_ptr)
125 goto error_nolines;
126
127 if (setjmp(png_ptr->jmpbuf))
128 goto error_nolines;
129
130 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
131 goto error_nolines;
132
133 png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
134
135 png_uint_32 width,height;
136 int bit_depth,color_type,interlace_type;
137
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 );
140
141 if (color_type == PNG_COLOR_TYPE_PALETTE)
142 png_set_expand( png_ptr );
143
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 );
149
150 image->Create( (int)width, (int)height );
151
152 if (!image->Ok())
153 goto error_nolines;
154
155 lines = (unsigned char **)malloc( (size_t)(height * sizeof(unsigned char *)) );
156 if (lines == NULL)
157 goto error_nolines;
158
159 for (i = 0; i < height; i++)
160 {
161 if ((lines[i] = (unsigned char *)malloc( (size_t)(width * (sizeof(unsigned char) * 4)))) == NULL)
162 {
163 for ( unsigned int n = 0; n < i; n++ )
164 free( lines[n] );
165 goto error;
166 }
167 }
168
169 // loaded successfully!
170 {
171 int transp = 0;
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))
178 {
179 for (unsigned int y = 0; y < height; y++)
180 {
181 unsigned char *ptr2 = lines[y];
182 for (unsigned int x = 0; x < width; x++)
183 {
184 unsigned char r = *ptr2++;
185 unsigned char a = *ptr2++;
186 if (a < 128)
187 {
188 *ptr++ = 255;
189 *ptr++ = 0;
190 *ptr++ = 255;
191 transp = 1;
192 }
193 else
194 {
195 *ptr++ = r;
196 *ptr++ = r;
197 *ptr++ = r;
198 }
199 }
200 }
201 }
202 else
203 {
204 for (unsigned int y = 0; y < height; y++)
205 {
206 unsigned char *ptr2 = lines[y];
207 for (unsigned int x = 0; x < width; x++)
208 {
209 unsigned char r = *ptr2++;
210 unsigned char g = *ptr2++;
211 unsigned char b = *ptr2++;
212 unsigned char a = *ptr2++;
213 if (a < 128)
214 {
215 *ptr++ = 255;
216 *ptr++ = 0;
217 *ptr++ = 255;
218 transp = 1;
219 }
220 else
221 {
222 if ((r == 255) && (g == 0) && (b == 255)) r = 254;
223 *ptr++ = r;
224 *ptr++ = g;
225 *ptr++ = b;
226 }
227 }
228 }
229 }
230
231 for ( unsigned int j = 0; j < height; j++ )
232 free( lines[j] );
233 free( lines );
234
235 if (transp)
236 {
237 image->SetMaskColour( 255, 0, 255 );
238 }
239 else
240 {
241 image->SetMask( FALSE );
242 }
243 }
244
245 return TRUE;
246
247 error_nolines:
248 lines = NULL; // called from before it was set
249 error:
250 if (verbose)
251 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
252
253 if ( image->Ok() )
254 {
255 image->Destroy();
256 }
257
258 if ( lines )
259 {
260 free( lines );
261 }
262
263 if ( png_ptr )
264 {
265 if ( info_ptr )
266 {
267 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
268 free(info_ptr);
269 }
270 else
271 png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
272 }
273 return FALSE;
274 }
275
276 bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
277 {
278 {
279 png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
280 if (!png_ptr)
281 {
282 return FALSE;
283 }
284
285 if (!verbose) png_set_error_fn(png_ptr, (png_voidp)NULL, png_silent_error, png_silent_warning);
286
287 png_infop info_ptr = png_create_info_struct(png_ptr);
288 if (info_ptr == NULL)
289 {
290 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
291 return FALSE;
292 }
293
294 if (setjmp(png_ptr->jmpbuf))
295 {
296 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
297 return FALSE;
298 }
299
300 png_set_write_fn( png_ptr, &stream, _PNG_stream_writer, NULL);
301
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);
305
306 png_color_8 sig_bit;
307 sig_bit.red = 8;
308 sig_bit.green = 8;
309 sig_bit.blue = 8;
310 sig_bit.alpha = 8;
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 );
315
316 unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
317 if (!data)
318 {
319 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
320 return FALSE;
321 }
322
323 for (int y = 0; y < image->GetHeight(); y++)
324 {
325 unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
326 for (int x = 0; x < image->GetWidth(); x++)
327 {
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()))
335 {
336 data[(x << 2) + 3] = 255;
337 }
338 else
339 {
340 data[(x << 2) + 3] = 0;
341 }
342 }
343 png_bytep row_ptr = data;
344 png_write_rows( png_ptr, &row_ptr, 1 );
345 }
346
347 free(data);
348 png_write_end( png_ptr, info_ptr );
349 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
350 }
351 return TRUE;
352 }
353
354 #ifdef __VISUALC__
355 #pragma warning(default:4611)
356 #endif /* VC++ */
357
358 bool wxPNGHandler::DoCanRead( wxInputStream& stream )
359 {
360 unsigned char hdr[4];
361
362 stream.Read(&hdr, 4);
363 stream.SeekI(-4, wxFromCurrent);
364 return (hdr[0] == 0x89 && hdr[1] == 'P' && hdr[2] == 'N' && hdr[3] == 'G');
365 }
366
367 #endif // wxUSE_STREAMS
368
369 #endif // wxUSE_LIBPNG