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