]>
Commit | Line | Data |
---|---|---|
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_STREAMS | |
58 | ||
59 | #ifndef PNGLINKAGEMODE | |
60 | #ifdef __WATCOMC__ | |
61 | // we need an explicit cdecl for Watcom, at least according to | |
62 | // | |
63 | // http://sf.net/tracker/index.php?func=detail&aid=651492&group_id=9863&atid=109863 | |
64 | // | |
65 | // more testing is needed for this however, please remove this comment | |
66 | // if you can confirm that my fix works with Watcom 11 | |
67 | #define PNGLINKAGEMODE cdecl | |
68 | #else | |
69 | #define PNGLINKAGEMODE LINKAGEMODE | |
70 | #endif | |
71 | #endif | |
72 | ||
73 | ||
74 | // VS: wxPNGInfoStruct declared below is a hack that needs some explanation. | |
75 | // First, let me describe what's the problem: libpng uses jmp_buf in | |
76 | // its png_struct structure. Unfortunately, this structure is | |
77 | // compiler-specific and may vary in size, so if you use libpng compiled | |
78 | // as DLL with another compiler than the main executable, it may not work | |
79 | // (this is for example the case with wxMGL port and SciTech MGL library | |
80 | // that provides custom runtime-loadable libpng implementation with jmpbuf | |
81 | // disabled altogether). Luckily, it is still possible to use setjmp() & | |
82 | // longjmp() as long as the structure is not part of png_struct. | |
83 | // | |
84 | // Sadly, there's no clean way to attach user-defined data to png_struct. | |
85 | // There is only one customizable place, png_struct.io_ptr, which is meant | |
86 | // only for I/O routines and is set with png_set_read_fn or | |
87 | // png_set_write_fn. The hacky part is that we use io_ptr to store | |
88 | // a pointer to wxPNGInfoStruct that holds I/O structures _and_ jmp_buf. | |
89 | ||
90 | struct wxPNGInfoStruct | |
91 | { | |
92 | jmp_buf jmpbuf; | |
93 | bool verbose; | |
94 | ||
95 | union | |
96 | { | |
97 | wxInputStream *in; | |
98 | wxOutputStream *out; | |
99 | } stream; | |
100 | }; | |
101 | ||
102 | #define WX_PNG_INFO(png_ptr) ((wxPNGInfoStruct*)png_get_io_ptr(png_ptr)) | |
103 | ||
104 | ||
105 | extern "C" | |
106 | { | |
107 | ||
108 | void PNGLINKAGEMODE _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length ) | |
109 | { | |
110 | WX_PNG_INFO(png_ptr)->stream.in->Read(data, length); | |
111 | } | |
112 | ||
113 | void PNGLINKAGEMODE _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length ) | |
114 | { | |
115 | WX_PNG_INFO(png_ptr)->stream.out->Write(data, length); | |
116 | } | |
117 | ||
118 | // from pngerror.c | |
119 | // so that the libpng doesn't send anything on stderr | |
120 | void | |
121 | PNGLINKAGEMODE wx_png_error(png_structp png_ptr, png_const_charp message) | |
122 | { | |
123 | wxPNGInfoStruct *info = WX_PNG_INFO(png_ptr); | |
124 | if (info->verbose) | |
125 | wxLogError( wxString::FromAscii(message) ); | |
126 | ||
127 | #ifdef USE_FAR_KEYWORD | |
128 | { | |
129 | jmp_buf jmpbuf; | |
130 | png_memcpy(jmpbuf,info->jmpbuf,sizeof(jmp_buf)); | |
131 | longjmp(jmpbuf, 1); | |
132 | } | |
133 | #else | |
134 | longjmp(info->jmpbuf, 1); | |
135 | #endif | |
136 | } | |
137 | ||
138 | void | |
139 | PNGLINKAGEMODE wx_png_warning(png_structp png_ptr, png_const_charp message) | |
140 | { | |
141 | wxPNGInfoStruct *info = WX_PNG_INFO(png_ptr); | |
142 | if (info->verbose) | |
143 | wxLogWarning( wxString::FromAscii(message) ); | |
144 | } | |
145 | ||
146 | } // extern "C" | |
147 | ||
148 | // temporarily disable the warning C4611 (interaction between '_setjmp' and | |
149 | // C++ object destruction is non-portable) - I don't see any dtors here | |
150 | #ifdef __VISUALC__ | |
151 | #pragma warning(disable:4611) | |
152 | #endif /* VC++ */ | |
153 | ||
154 | bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) ) | |
155 | { | |
156 | // VZ: as this function uses setjmp() the only fool proof error handling | |
157 | // method is to use goto (setjmp is not really C++ dtors friendly...) | |
158 | ||
159 | unsigned char **lines; | |
160 | unsigned int i; | |
161 | png_infop info_ptr = (png_infop) NULL; | |
162 | wxPNGInfoStruct wxinfo; | |
163 | ||
164 | wxinfo.verbose = verbose; | |
165 | wxinfo.stream.in = &stream; | |
166 | ||
167 | image->Destroy(); | |
168 | ||
169 | png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, | |
170 | (voidp) NULL, | |
171 | (png_error_ptr) NULL, | |
172 | (png_error_ptr) NULL ); | |
173 | if (!png_ptr) | |
174 | goto error_nolines; | |
175 | ||
176 | png_set_error_fn(png_ptr, (png_voidp)NULL, wx_png_error, wx_png_warning); | |
177 | ||
178 | // NB: please see the comment near wxPNGInfoStruct declaration for | |
179 | // explanation why this line is mandatory | |
180 | png_set_read_fn( png_ptr, &wxinfo, _PNG_stream_reader); | |
181 | ||
182 | info_ptr = png_create_info_struct( png_ptr ); | |
183 | if (!info_ptr) | |
184 | goto error_nolines; | |
185 | ||
186 | if (setjmp(wxinfo.jmpbuf)) | |
187 | goto error_nolines; | |
188 | ||
189 | if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | |
190 | goto error_nolines; | |
191 | ||
192 | png_uint_32 width,height; | |
193 | int bit_depth,color_type,interlace_type; | |
194 | ||
195 | png_read_info( png_ptr, info_ptr ); | |
196 | png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL ); | |
197 | ||
198 | if (color_type == PNG_COLOR_TYPE_PALETTE) | |
199 | png_set_expand( png_ptr ); | |
200 | ||
201 | // Fix for Bug [ 439207 ] Monochrome PNG images come up black | |
202 | if (bit_depth < 8) | |
203 | png_set_expand( png_ptr ); | |
204 | ||
205 | png_set_strip_16( png_ptr ); | |
206 | png_set_packing( png_ptr ); | |
207 | if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS)) | |
208 | png_set_expand( png_ptr ); | |
209 | png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER ); | |
210 | ||
211 | image->Create( (int)width, (int)height ); | |
212 | ||
213 | if (!image->Ok()) | |
214 | goto error_nolines; | |
215 | ||
216 | lines = (unsigned char **)malloc( (size_t)(height * sizeof(unsigned char *)) ); | |
217 | if (lines == NULL) | |
218 | goto error_nolines; | |
219 | ||
220 | for (i = 0; i < height; i++) | |
221 | { | |
222 | if ((lines[i] = (unsigned char *)malloc( (size_t)(width * (sizeof(unsigned char) * 4)))) == NULL) | |
223 | { | |
224 | for ( unsigned int n = 0; n < i; n++ ) | |
225 | free( lines[n] ); | |
226 | goto error; | |
227 | } | |
228 | } | |
229 | ||
230 | // loaded successfully! | |
231 | { | |
232 | int transp = 0; | |
233 | png_read_image( png_ptr, lines ); | |
234 | png_read_end( png_ptr, info_ptr ); | |
235 | png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL ); | |
236 | unsigned char *ptr = image->GetData(); | |
237 | if ((color_type == PNG_COLOR_TYPE_GRAY) || | |
238 | (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) | |
239 | { | |
240 | for (unsigned int y = 0; y < height; y++) | |
241 | { | |
242 | unsigned char *ptr2 = lines[y]; | |
243 | for (unsigned int x = 0; x < width; x++) | |
244 | { | |
245 | unsigned char r = *ptr2++; | |
246 | unsigned char a = *ptr2++; | |
247 | if (a < 128) | |
248 | { | |
249 | *ptr++ = 255; | |
250 | *ptr++ = 0; | |
251 | *ptr++ = 255; | |
252 | transp = 1; | |
253 | } | |
254 | else | |
255 | { | |
256 | *ptr++ = r; | |
257 | *ptr++ = r; | |
258 | *ptr++ = r; | |
259 | } | |
260 | } | |
261 | } | |
262 | } | |
263 | else | |
264 | { | |
265 | for (unsigned int y = 0; y < height; y++) | |
266 | { | |
267 | unsigned char *ptr2 = lines[y]; | |
268 | for (unsigned int x = 0; x < width; x++) | |
269 | { | |
270 | unsigned char r = *ptr2++; | |
271 | unsigned char g = *ptr2++; | |
272 | unsigned char b = *ptr2++; | |
273 | unsigned char a = *ptr2++; | |
274 | if (a < 128) | |
275 | { | |
276 | *ptr++ = 255; | |
277 | *ptr++ = 0; | |
278 | *ptr++ = 255; | |
279 | transp = 1; | |
280 | } | |
281 | else | |
282 | { | |
283 | if ((r == 255) && (g == 0) && (b == 255)) r = 254; | |
284 | *ptr++ = r; | |
285 | *ptr++ = g; | |
286 | *ptr++ = b; | |
287 | } | |
288 | } | |
289 | } | |
290 | } | |
291 | ||
292 | for ( unsigned int j = 0; j < height; j++ ) | |
293 | free( lines[j] ); | |
294 | free( lines ); | |
295 | ||
296 | if (transp) | |
297 | { | |
298 | image->SetMaskColour( 255, 0, 255 ); | |
299 | } | |
300 | else | |
301 | { | |
302 | image->SetMask( FALSE ); | |
303 | } | |
304 | } | |
305 | ||
306 | return TRUE; | |
307 | ||
308 | error_nolines: | |
309 | lines = NULL; // called from before it was set | |
310 | error: | |
311 | if (verbose) | |
312 | wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory.")); | |
313 | ||
314 | if ( image->Ok() ) | |
315 | { | |
316 | image->Destroy(); | |
317 | } | |
318 | ||
319 | if ( lines ) | |
320 | { | |
321 | free( lines ); | |
322 | } | |
323 | ||
324 | if ( png_ptr ) | |
325 | { | |
326 | if ( info_ptr ) | |
327 | { | |
328 | png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL ); | |
329 | free(info_ptr); | |
330 | } | |
331 | else | |
332 | png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL ); | |
333 | } | |
334 | return FALSE; | |
335 | } | |
336 | ||
337 | bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) | |
338 | { | |
339 | wxPNGInfoStruct wxinfo; | |
340 | ||
341 | wxinfo.verbose = verbose; | |
342 | wxinfo.stream.out = &stream; | |
343 | ||
344 | png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | |
345 | if (!png_ptr) | |
346 | { | |
347 | if (verbose) | |
348 | wxLogError(_("Couldn't save PNG image.")); | |
349 | return FALSE; | |
350 | } | |
351 | ||
352 | png_set_error_fn(png_ptr, (png_voidp)NULL, wx_png_error, wx_png_warning); | |
353 | ||
354 | png_infop info_ptr = png_create_info_struct(png_ptr); | |
355 | if (info_ptr == NULL) | |
356 | { | |
357 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); | |
358 | if (verbose) | |
359 | wxLogError(_("Couldn't save PNG image.")); | |
360 | return FALSE; | |
361 | } | |
362 | ||
363 | if (setjmp(wxinfo.jmpbuf)) | |
364 | { | |
365 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); | |
366 | if (verbose) | |
367 | wxLogError(_("Couldn't save PNG image.")); | |
368 | return FALSE; | |
369 | } | |
370 | ||
371 | // NB: please see the comment near wxPNGInfoStruct declaration for | |
372 | // explanation why this line is mandatory | |
373 | png_set_write_fn( png_ptr, &wxinfo, _PNG_stream_writer, NULL); | |
374 | ||
375 | png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8, | |
376 | PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, | |
377 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); | |
378 | ||
379 | png_color_8 sig_bit; | |
380 | sig_bit.red = 8; | |
381 | sig_bit.green = 8; | |
382 | sig_bit.blue = 8; | |
383 | sig_bit.alpha = 8; | |
384 | png_set_sBIT( png_ptr, info_ptr, &sig_bit ); | |
385 | png_write_info( png_ptr, info_ptr ); | |
386 | png_set_shift( png_ptr, &sig_bit ); | |
387 | png_set_packing( png_ptr ); | |
388 | ||
389 | unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 ); | |
390 | if (!data) | |
391 | { | |
392 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); | |
393 | return FALSE; | |
394 | } | |
395 | ||
396 | for (int y = 0; y < image->GetHeight(); y++) | |
397 | { | |
398 | unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3); | |
399 | for (int x = 0; x < image->GetWidth(); x++) | |
400 | { | |
401 | data[(x << 2) + 0] = *ptr++; | |
402 | data[(x << 2) + 1] = *ptr++; | |
403 | data[(x << 2) + 2] = *ptr++; | |
404 | if (( !image->HasMask() ) || \ | |
405 | (data[(x << 2) + 0] != image->GetMaskRed()) || \ | |
406 | (data[(x << 2) + 1] != image->GetMaskGreen()) || \ | |
407 | (data[(x << 2) + 2] != image->GetMaskBlue())) | |
408 | { | |
409 | data[(x << 2) + 3] = 255; | |
410 | } | |
411 | else | |
412 | { | |
413 | data[(x << 2) + 3] = 0; | |
414 | } | |
415 | } | |
416 | png_bytep row_ptr = data; | |
417 | png_write_rows( png_ptr, &row_ptr, 1 ); | |
418 | } | |
419 | ||
420 | free(data); | |
421 | png_write_end( png_ptr, info_ptr ); | |
422 | png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr ); | |
423 | ||
424 | return TRUE; | |
425 | } | |
426 | ||
427 | #ifdef __VISUALC__ | |
428 | #pragma warning(default:4611) | |
429 | #endif /* VC++ */ | |
430 | ||
431 | bool wxPNGHandler::DoCanRead( wxInputStream& stream ) | |
432 | { | |
433 | unsigned char hdr[4]; | |
434 | ||
435 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) | |
436 | return FALSE; | |
437 | ||
438 | return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0; | |
439 | } | |
440 | ||
441 | #endif // wxUSE_STREAMS | |
442 | ||
443 | #endif // wxUSE_LIBPNG |