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