Added #include "wx/setup.h" (or "wx/defs.h") before checking for
[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 /*
11 We don't put pragma implement in this file because it is already present in
12 src/common/image.cpp
13 */
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/defs.h"
24 #endif
25
26 #if wxUSE_STREAMS && wxUSE_LIBPNG
27
28 #include "wx/image.h"
29 #include "wx/bitmap.h"
30 #include "wx/debug.h"
31 #include "wx/log.h"
32 #include "wx/app.h"
33 #include "png.h"
34 #include "wx/filefn.h"
35 #include "wx/wfstream.h"
36 #include "wx/intl.h"
37 #include "wx/module.h"
38
39 // For memcpy
40 #include <string.h>
41
42 #ifdef __SALFORDC__
43 #ifdef FAR
44 #undef FAR
45 #endif
46 #endif
47
48 #ifdef __WXMSW__
49 #include <windows.h>
50 #endif
51
52 //-----------------------------------------------------------------------------
53 // wxPNGHandler
54 //-----------------------------------------------------------------------------
55
56 #if !USE_SHARED_LIBRARIES
57 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler)
58 #endif
59
60 #if defined(__VISAGECPP__)
61 #define LINKAGEMODE _Optlink
62 #else
63 #define LINKAGEMODE
64 #endif
65
66 static void LINKAGEMODE _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length )
67 {
68 ((wxInputStream*) png_get_io_ptr( png_ptr )) -> Read(data, length);
69 }
70
71 static void LINKAGEMODE _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length )
72 {
73 ((wxOutputStream*) png_get_io_ptr( png_ptr )) -> Write(data, length);
74 }
75
76 // from pngerror.c
77 // so that the libpng doesn't send anything on stderr
78 void
79 LINKAGEMODE png_silent_error(png_structp png_ptr, png_const_charp WXUNUSED(message))
80 {
81 #ifdef USE_FAR_KEYWORD
82 {
83 jmp_buf jmpbuf;
84 png_memcpy(jmpbuf,png_ptr->jmpbuf,sizeof(jmp_buf));
85 longjmp(jmpbuf, 1);
86 }
87 #else
88 longjmp(png_ptr->jmpbuf, 1);
89 #endif
90 }
91
92 void
93 LINKAGEMODE png_silent_warning(png_structp WXUNUSED(png_ptr), png_const_charp WXUNUSED(message))
94 {
95 }
96
97 bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose)
98 {
99 // VZ: as this function uses setjmp() the only fool proof error handling
100 // method is to use goto (setjmp is not really C++ dtors friendly...)
101
102 unsigned char **lines;
103 unsigned int i;
104 png_infop info_ptr = (png_infop) NULL;
105
106 image->Destroy();
107
108 png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
109 (voidp) NULL,
110 (png_error_ptr) NULL,
111 (png_error_ptr) NULL );
112 if (!png_ptr)
113 goto error_nolines;
114
115 // the file example.c explain how to guess if the stream is a png image
116 if (!verbose) png_set_error_fn(png_ptr, (png_voidp)NULL, png_silent_error, png_silent_warning);
117
118 info_ptr = png_create_info_struct( png_ptr );
119 if (!info_ptr)
120 goto error_nolines;
121
122 if (setjmp(png_ptr->jmpbuf))
123 goto error_nolines;
124
125 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
126 goto error_nolines;
127
128 png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
129
130 png_uint_32 width,height;
131 int bit_depth,color_type,interlace_type;
132
133 png_read_info( png_ptr, info_ptr );
134 png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL );
135
136 if (color_type == PNG_COLOR_TYPE_PALETTE)
137 png_set_expand( png_ptr );
138
139 png_set_strip_16( png_ptr );
140 png_set_packing( png_ptr );
141 if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
142 png_set_expand( png_ptr );
143 png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
144
145 image->Create( width, height );
146
147 if (!image->Ok())
148 goto error_nolines;
149
150 lines = (unsigned char **)malloc( height * sizeof(unsigned char *) );
151 if (lines == NULL)
152 goto error_nolines;
153
154 for (i = 0; i < height; i++)
155 {
156 if ((lines[i] = (unsigned char *)malloc(width * (sizeof(unsigned char) * 4))) == NULL)
157 {
158 for ( unsigned int n = 0; n < i; n++ )
159 free( lines[n] );
160 goto error;
161 }
162 }
163
164 // loaded successfully!
165 {
166 int transp = 0;
167 png_read_image( png_ptr, lines );
168 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
169 unsigned char *ptr = image->GetData();
170 if ((color_type == PNG_COLOR_TYPE_GRAY) ||
171 (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
172 {
173 for (unsigned int y = 0; y < height; y++)
174 {
175 unsigned char *ptr2 = lines[y];
176 for (unsigned int x = 0; x < width; x++)
177 {
178 unsigned char r = *ptr2++;
179 unsigned char a = *ptr2++;
180 if (a < 128)
181 {
182 *ptr++ = 255;
183 *ptr++ = 0;
184 *ptr++ = 255;
185 transp = 1;
186 }
187 else
188 {
189 *ptr++ = r;
190 *ptr++ = r;
191 *ptr++ = r;
192 }
193 }
194 }
195 }
196 else
197 {
198 for (unsigned int y = 0; y < height; y++)
199 {
200 unsigned char *ptr2 = lines[y];
201 for (unsigned int x = 0; x < width; x++)
202 {
203 unsigned char r = *ptr2++;
204 unsigned char g = *ptr2++;
205 unsigned char b = *ptr2++;
206 unsigned char a = *ptr2++;
207 if (a < 128)
208 {
209 *ptr++ = 255;
210 *ptr++ = 0;
211 *ptr++ = 255;
212 transp = 1;
213 }
214 else
215 {
216 if ((r == 255) && (g == 0) && (b == 255)) r = 254;
217 *ptr++ = r;
218 *ptr++ = g;
219 *ptr++ = b;
220 }
221 }
222 }
223 }
224
225 for ( unsigned int j = 0; j < height; j++ )
226 free( lines[j] );
227 free( lines );
228
229 if (transp)
230 {
231 image->SetMaskColour( 255, 0, 255 );
232 }
233 else
234 {
235 image->SetMask( FALSE );
236 }
237 }
238
239 return TRUE;
240
241 error_nolines:
242 lines = NULL; // called from before it was set
243 error:
244 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
245
246 if ( image->Ok() )
247 {
248 image->Destroy();
249 }
250
251 if ( lines )
252 {
253 free( lines );
254 }
255
256 if ( png_ptr )
257 {
258 if ( info_ptr )
259 {
260 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
261 free(info_ptr);
262 }
263 else
264 png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
265 }
266 return FALSE;
267 }
268
269
270 bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
271 {
272 {
273 png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
274 if (!png_ptr)
275 {
276 return FALSE;
277 }
278
279 if (!verbose) png_set_error_fn(png_ptr, (png_voidp)NULL, png_silent_error, png_silent_warning);
280
281 png_infop info_ptr = png_create_info_struct(png_ptr);
282 if (info_ptr == NULL)
283 {
284 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
285 return FALSE;
286 }
287
288 if (setjmp(png_ptr->jmpbuf))
289 {
290 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
291 return FALSE;
292 }
293
294 png_set_write_fn( png_ptr, &stream, _PNG_stream_writer, NULL);
295
296 png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8,
297 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
298 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
299
300 png_color_8 sig_bit;
301 sig_bit.red = 8;
302 sig_bit.green = 8;
303 sig_bit.blue = 8;
304 sig_bit.alpha = 8;
305 png_set_sBIT( png_ptr, info_ptr, &sig_bit );
306 png_write_info( png_ptr, info_ptr );
307 png_set_shift( png_ptr, &sig_bit );
308 png_set_packing( png_ptr );
309
310 unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
311 if (!data)
312 {
313 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
314 return FALSE;
315 }
316
317 for (int y = 0; y < image->GetHeight(); y++)
318 {
319 unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
320 for (int x = 0; x < image->GetWidth(); x++)
321 {
322 data[(x << 2) + 0] = *ptr++;
323 data[(x << 2) + 1] = *ptr++;
324 data[(x << 2) + 2] = *ptr++;
325 if ((data[(x << 2) + 0] == image->GetMaskRed()) &&
326 (data[(x << 2) + 1] == image->GetMaskGreen()) &&
327 (data[(x << 2) + 2] == image->GetMaskBlue()))
328 {
329 data[(x << 2) + 3] = 0;
330 }
331 else
332 {
333 data[(x << 2) + 3] = 255;
334 }
335 }
336 png_bytep row_ptr = data;
337 png_write_rows( png_ptr, &row_ptr, 1 );
338 }
339
340 free(data);
341 png_write_end( png_ptr, info_ptr );
342 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
343 }
344 return TRUE;
345 }
346
347 bool wxPNGHandler::CanRead( wxInputStream& stream )
348 {
349 unsigned char hdr[4];
350
351 stream.Read(&hdr, 4);
352 stream.SeekI(-4, wxFromCurrent);
353 return (hdr[0] == 0x89 && hdr[1] == 'P' && hdr[2] == 'N' && hdr[3] == 'G');
354 }
355
356 #endif // wxUSE_STREAMS && wxUSE_LIBPNG
357