]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpng.cpp
corrected generation of all event members (client data) and implemented DoGetBestSize
[wxWidgets.git] / src / common / imagpng.cpp
CommitLineData
e9c4b1a2
JS
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
8f493002
VS
10#ifdef __GNUG__
11#pragma implementation "imagpng.h"
12#endif
e9c4b1a2
JS
13
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
ce4169a4
RR
18 #pragma hdrstop
19#endif
20
21#ifndef WX_PRECOMP
22 #include "wx/defs.h"
e9c4b1a2
JS
23#endif
24
c96ea657 25#if wxUSE_IMAGE && wxUSE_LIBPNG
ce4169a4 26
8f493002 27#include "wx/imagpng.h"
e9c4b1a2
JS
28#include "wx/bitmap.h"
29#include "wx/debug.h"
30#include "wx/log.h"
31#include "wx/app.h"
ce4169a4 32#include "png.h"
e9c4b1a2
JS
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
e9c4b1a2 55IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler)
e9c4b1a2 56
9ab6ee85
GRG
57#if wxUSE_LIBPNG
58
0729bd19 59#ifndef PNGLINKAGEMODE
30e0e251
VZ
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
717b9bf2
DW
71#endif
72
bdffd806
VS
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
90struct 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
90350682
VZ
105extern "C"
106{
107
108void PNGLINKAGEMODE _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length )
e9c4b1a2 109{
bdffd806 110 WX_PNG_INFO(png_ptr)->stream.in->Read(data, length);
e9c4b1a2
JS
111}
112
90350682 113void PNGLINKAGEMODE _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length )
e9c4b1a2 114{
bdffd806 115 WX_PNG_INFO(png_ptr)->stream.out->Write(data, length);
e9c4b1a2
JS
116}
117
deb2fec0
SB
118// from pngerror.c
119// so that the libpng doesn't send anything on stderr
120void
bdffd806 121PNGLINKAGEMODE wx_png_error(png_structp png_ptr, png_const_charp message)
deb2fec0 122{
bdffd806 123 wxPNGInfoStruct *info = WX_PNG_INFO(png_ptr);
2b5f62a0
VZ
124 if (info->verbose)
125 wxLogError( wxString::FromAscii(message) );
bdffd806 126
deb2fec0 127#ifdef USE_FAR_KEYWORD
bdffd806
VS
128 {
129 jmp_buf jmpbuf;
130 png_memcpy(jmpbuf,info->jmpbuf,sizeof(jmp_buf));
131 longjmp(jmpbuf, 1);
132 }
deb2fec0 133#else
bdffd806 134 longjmp(info->jmpbuf, 1);
deb2fec0
SB
135#endif
136}
137
138void
bdffd806 139PNGLINKAGEMODE wx_png_warning(png_structp png_ptr, png_const_charp message)
deb2fec0 140{
bdffd806 141 wxPNGInfoStruct *info = WX_PNG_INFO(png_ptr);
2b5f62a0
VZ
142 if (info->verbose)
143 wxLogWarning( wxString::FromAscii(message) );
deb2fec0
SB
144}
145
90350682
VZ
146} // extern "C"
147
3ca6a5f0
BP
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
700ec454 154bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
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...)
717b9bf2 158
95ee0ac8 159 unsigned char **lines;
e9c4b1a2 160 unsigned int i;
bdffd806
VS
161 png_infop info_ptr = (png_infop) NULL;
162 wxPNGInfoStruct wxinfo;
163
164 wxinfo.verbose = verbose;
165 wxinfo.stream.in = &stream;
717b9bf2 166
e9c4b1a2 167 image->Destroy();
717b9bf2 168
e9c4b1a2
JS
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)
95ee0ac8 174 goto error_nolines;
717b9bf2 175
bdffd806
VS
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);
deb2fec0 181
e9c4b1a2
JS
182 info_ptr = png_create_info_struct( png_ptr );
183 if (!info_ptr)
95ee0ac8 184 goto error_nolines;
717b9bf2 185
bdffd806 186 if (setjmp(wxinfo.jmpbuf))
95ee0ac8 187 goto error_nolines;
717b9bf2 188
e9c4b1a2 189 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
95ee0ac8 190 goto error_nolines;
717b9bf2 191
e9c4b1a2
JS
192 png_uint_32 width,height;
193 int bit_depth,color_type,interlace_type;
717b9bf2 194
e9c4b1a2
JS
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 );
717b9bf2 197
e9c4b1a2
JS
198 if (color_type == PNG_COLOR_TYPE_PALETTE)
199 png_set_expand( png_ptr );
717b9bf2 200
2b5f62a0
VZ
201 // Fix for Bug [ 439207 ] Monochrome PNG images come up black
202 if (bit_depth < 8)
203 png_set_expand( png_ptr );
204
e9c4b1a2
JS
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 );
717b9bf2 210
479cd5de 211 image->Create( (int)width, (int)height );
717b9bf2 212
e9c4b1a2 213 if (!image->Ok())
95ee0ac8 214 goto error_nolines;
717b9bf2 215
479cd5de 216 lines = (unsigned char **)malloc( (size_t)(height * sizeof(unsigned char *)) );
e9c4b1a2 217 if (lines == NULL)
95ee0ac8 218 goto error_nolines;
717b9bf2 219
e9c4b1a2
JS
220 for (i = 0; i < height; i++)
221 {
479cd5de 222 if ((lines[i] = (unsigned char *)malloc( (size_t)(width * (sizeof(unsigned char) * 4)))) == NULL)
e9c4b1a2
JS
223 {
224 for ( unsigned int n = 0; n < i; n++ )
225 free( lines[n] );
226 goto error;
227 }
228 }
717b9bf2 229
e9c4b1a2
JS
230 // loaded successfully!
231 {
232 int transp = 0;
233 png_read_image( png_ptr, lines );
5ef37c79 234 png_read_end( png_ptr, info_ptr );
e9c4b1a2
JS
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 }
717b9bf2 291
e9c4b1a2
JS
292 for ( unsigned int j = 0; j < height; j++ )
293 free( lines[j] );
294 free( lines );
717b9bf2 295
e9c4b1a2
JS
296 if (transp)
297 {
298 image->SetMaskColour( 255, 0, 255 );
299 }
300 else
301 {
302 image->SetMask( FALSE );
303 }
304 }
717b9bf2 305
e9c4b1a2 306 return TRUE;
95ee0ac8
KB
307
308 error_nolines:
309 lines = NULL; // called from before it was set
310 error:
58c837a4
RR
311 if (verbose)
312 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
717b9bf2 313
e9c4b1a2
JS
314 if ( image->Ok() )
315 {
316 image->Destroy();
317 }
717b9bf2 318
e9c4b1a2
JS
319 if ( lines )
320 {
321 free( lines );
322 }
717b9bf2 323
e9c4b1a2
JS
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
deb2fec0 337bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2 338{
bdffd806 339 wxPNGInfoStruct wxinfo;
717b9bf2 340
bdffd806
VS
341 wxinfo.verbose = verbose;
342 wxinfo.stream.out = &stream;
deb2fec0 343
bdffd806
VS
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 }
717b9bf2 351
bdffd806 352 png_set_error_fn(png_ptr, (png_voidp)NULL, wx_png_error, wx_png_warning);
717b9bf2 353
bdffd806
VS
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 }
717b9bf2 362
bdffd806
VS
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 }
717b9bf2 370
bdffd806
VS
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 );
717b9bf2 388
bdffd806
VS
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 }
717b9bf2 395
bdffd806
VS
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++)
e9c4b1a2 400 {
bdffd806
VS
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()))
e9c4b1a2 408 {
bdffd806
VS
409 data[(x << 2) + 3] = 255;
410 }
411 else
412 {
413 data[(x << 2) + 3] = 0;
e9c4b1a2 414 }
e9c4b1a2 415 }
bdffd806
VS
416 png_bytep row_ptr = data;
417 png_write_rows( png_ptr, &row_ptr, 1 );
e9c4b1a2 418 }
bdffd806
VS
419
420 free(data);
421 png_write_end( png_ptr, info_ptr );
422 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
423
e9c4b1a2
JS
424 return TRUE;
425}
e9c4b1a2 426
3ca6a5f0
BP
427#ifdef __VISUALC__
428 #pragma warning(default:4611)
429#endif /* VC++ */
430
995612e2 431bool wxPNGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
432{
433 unsigned char hdr[4];
feeb8165 434
79fa2374
VZ
435 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
436 return FALSE;
437
79fa2374 438 return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0;
0828c087
VS
439}
440
9ab6ee85 441#endif // wxUSE_STREAMS
e9c4b1a2 442
9ab6ee85 443#endif // wxUSE_LIBPNG