]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagpng.cpp
Changed a few true -> TRUE
[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_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 wxUSE_STREAMS
61
62 static void _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length )
63 {
64 ((wxInputStream*) png_get_io_ptr( png_ptr )) -> Read(data, length);
65 }
66
67 static void _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length )
68 {
69 ((wxOutputStream*) png_get_io_ptr( png_ptr )) -> Write(data, length);
70 }
71
72 bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
73 {
74 // VZ: as this function uses setjmp() the only fool proof error handling
75 // method is to use goto (setjmp is not really C++ dtors friendly...)
76
77 unsigned char **lines;
78 unsigned int i;
79 png_infop info_ptr = (png_infop) NULL;
80
81 image->Destroy();
82
83 png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
84 (voidp) NULL,
85 (png_error_ptr) NULL,
86 (png_error_ptr) NULL );
87 if (!png_ptr)
88 goto error_nolines;
89
90 info_ptr = png_create_info_struct( png_ptr );
91 if (!info_ptr)
92 goto error_nolines;
93
94 if (setjmp(png_ptr->jmpbuf))
95 goto error_nolines;
96
97 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
98 goto error_nolines;
99
100 png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
101
102 png_uint_32 width,height;
103 int bit_depth,color_type,interlace_type;
104
105 png_read_info( png_ptr, info_ptr );
106 png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL );
107
108 if (color_type == PNG_COLOR_TYPE_PALETTE)
109 png_set_expand( png_ptr );
110
111 png_set_strip_16( png_ptr );
112 png_set_packing( png_ptr );
113 if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
114 png_set_expand( png_ptr );
115 png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
116
117 image->Create( width, height );
118
119 if (!image->Ok())
120 goto error_nolines;
121
122 lines = (unsigned char **)malloc( height * sizeof(unsigned char *) );
123 if (lines == NULL)
124 goto error_nolines;
125
126 for (i = 0; i < height; i++)
127 {
128 if ((lines[i] = (unsigned char *)malloc(width * (sizeof(unsigned char) * 4))) == NULL)
129 {
130 for ( unsigned int n = 0; n < i; n++ )
131 free( lines[n] );
132 goto error;
133 }
134 }
135
136 // loaded successfully!
137 {
138 int transp = 0;
139 png_read_image( png_ptr, lines );
140 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
141 unsigned char *ptr = image->GetData();
142 if ((color_type == PNG_COLOR_TYPE_GRAY) ||
143 (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
144 {
145 for (unsigned int y = 0; y < height; y++)
146 {
147 unsigned char *ptr2 = lines[y];
148 for (unsigned int x = 0; x < width; x++)
149 {
150 unsigned char r = *ptr2++;
151 unsigned char a = *ptr2++;
152 if (a < 128)
153 {
154 *ptr++ = 255;
155 *ptr++ = 0;
156 *ptr++ = 255;
157 transp = 1;
158 }
159 else
160 {
161 *ptr++ = r;
162 *ptr++ = r;
163 *ptr++ = r;
164 }
165 }
166 }
167 }
168 else
169 {
170 for (unsigned int y = 0; y < height; y++)
171 {
172 unsigned char *ptr2 = lines[y];
173 for (unsigned int x = 0; x < width; x++)
174 {
175 unsigned char r = *ptr2++;
176 unsigned char g = *ptr2++;
177 unsigned char b = *ptr2++;
178 unsigned char a = *ptr2++;
179 if (a < 128)
180 {
181 *ptr++ = 255;
182 *ptr++ = 0;
183 *ptr++ = 255;
184 transp = 1;
185 }
186 else
187 {
188 if ((r == 255) && (g == 0) && (b == 255)) r = 254;
189 *ptr++ = r;
190 *ptr++ = g;
191 *ptr++ = b;
192 }
193 }
194 }
195 }
196
197 for ( unsigned int j = 0; j < height; j++ )
198 free( lines[j] );
199 free( lines );
200
201 if (transp)
202 {
203 image->SetMaskColour( 255, 0, 255 );
204 }
205 else
206 {
207 image->SetMask( FALSE );
208 }
209 }
210
211 return TRUE;
212
213 error_nolines:
214 lines = NULL; // called from before it was set
215 error:
216 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
217
218 if ( image->Ok() )
219 {
220 image->Destroy();
221 }
222
223 if ( lines )
224 {
225 free( lines );
226 }
227
228 if ( png_ptr )
229 {
230 if ( info_ptr )
231 {
232 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
233 free(info_ptr);
234 }
235 else
236 png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
237 }
238 return FALSE;
239 }
240
241
242 bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
243 {
244 {
245 png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
246 if (!png_ptr)
247 {
248 return FALSE;
249 }
250
251 png_infop info_ptr = png_create_info_struct(png_ptr);
252 if (info_ptr == NULL)
253 {
254 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
255 return FALSE;
256 }
257
258 if (setjmp(png_ptr->jmpbuf))
259 {
260 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
261 return FALSE;
262 }
263
264 png_set_write_fn( png_ptr, &stream, _PNG_stream_writer, NULL);
265
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);
269
270 png_color_8 sig_bit;
271 sig_bit.red = 8;
272 sig_bit.green = 8;
273 sig_bit.blue = 8;
274 sig_bit.alpha = 8;
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 );
279
280 unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
281 if (!data)
282 {
283 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
284 return FALSE;
285 }
286
287 for (int y = 0; y < image->GetHeight(); y++)
288 {
289 unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
290 for (int x = 0; x < image->GetWidth(); x++)
291 {
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()))
298 {
299 data[(x << 2) + 3] = 0;
300 }
301 else
302 {
303 data[(x << 2) + 3] = 255;
304 }
305 }
306 png_bytep row_ptr = data;
307 png_write_rows( png_ptr, &row_ptr, 1 );
308 }
309
310 free(data);
311 png_write_end( png_ptr, info_ptr );
312 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
313 }
314 return TRUE;
315 }
316
317 #endif
318 // wxUSE_STREAMS
319
320 #endif
321 // wxUSE_LIBPNG
322