]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpng.cpp
Watcom build system for wxMGL
[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
VS
59#ifndef PNGLINKAGEMODE
60 #define PNGLINKAGEMODE LINKAGEMODE
717b9bf2
DW
61#endif
62
90350682
VZ
63extern "C"
64{
65
66void PNGLINKAGEMODE _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length )
e9c4b1a2
JS
67{
68 ((wxInputStream*) png_get_io_ptr( png_ptr )) -> Read(data, length);
69}
70
90350682 71void PNGLINKAGEMODE _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length )
e9c4b1a2
JS
72{
73 ((wxOutputStream*) png_get_io_ptr( png_ptr )) -> Write(data, length);
74}
75
deb2fec0
SB
76// from pngerror.c
77// so that the libpng doesn't send anything on stderr
78void
33ac7e6f 79PNGLINKAGEMODE png_silent_error(png_structp png_ptr, png_const_charp WXUNUSED(message))
deb2fec0
SB
80{
81#ifdef USE_FAR_KEYWORD
82 {
83 jmp_buf jmpbuf;
84 png_memcpy(jmpbuf,png_ptr->jmpbuf,sizeof(jmp_buf));
85 longjmp(jmpbuf, 1);
86 }
87#else
88 longjmp(png_ptr->jmpbuf, 1);
89#endif
90}
91
92void
33ac7e6f 93PNGLINKAGEMODE png_silent_warning(png_structp WXUNUSED(png_ptr), png_const_charp WXUNUSED(message))
deb2fec0
SB
94{
95}
96
90350682
VZ
97} // extern "C"
98
3ca6a5f0
BP
99// temporarily disable the warning C4611 (interaction between '_setjmp' and
100// C++ object destruction is non-portable) - I don't see any dtors here
101#ifdef __VISUALC__
102 #pragma warning(disable:4611)
103#endif /* VC++ */
104
700ec454 105bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
e9c4b1a2
JS
106{
107 // VZ: as this function uses setjmp() the only fool proof error handling
108 // method is to use goto (setjmp is not really C++ dtors friendly...)
717b9bf2 109
95ee0ac8 110 unsigned char **lines;
e9c4b1a2
JS
111 unsigned int i;
112 png_infop info_ptr = (png_infop) NULL;
717b9bf2 113
e9c4b1a2 114 image->Destroy();
717b9bf2 115
e9c4b1a2
JS
116 png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
117 (voidp) NULL,
118 (png_error_ptr) NULL,
119 (png_error_ptr) NULL );
120 if (!png_ptr)
95ee0ac8 121 goto error_nolines;
717b9bf2 122
deb2fec0
SB
123 // the file example.c explain how to guess if the stream is a png image
124 if (!verbose) png_set_error_fn(png_ptr, (png_voidp)NULL, png_silent_error, png_silent_warning);
125
e9c4b1a2
JS
126 info_ptr = png_create_info_struct( png_ptr );
127 if (!info_ptr)
95ee0ac8 128 goto error_nolines;
717b9bf2 129
e9c4b1a2 130 if (setjmp(png_ptr->jmpbuf))
95ee0ac8 131 goto error_nolines;
717b9bf2 132
e9c4b1a2 133 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
95ee0ac8 134 goto error_nolines;
717b9bf2 135
e9c4b1a2 136 png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
717b9bf2 137
e9c4b1a2
JS
138 png_uint_32 width,height;
139 int bit_depth,color_type,interlace_type;
717b9bf2 140
e9c4b1a2
JS
141 png_read_info( png_ptr, info_ptr );
142 png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL );
717b9bf2 143
e9c4b1a2
JS
144 if (color_type == PNG_COLOR_TYPE_PALETTE)
145 png_set_expand( png_ptr );
717b9bf2 146
e9c4b1a2
JS
147 png_set_strip_16( png_ptr );
148 png_set_packing( png_ptr );
149 if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
150 png_set_expand( png_ptr );
151 png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
717b9bf2 152
479cd5de 153 image->Create( (int)width, (int)height );
717b9bf2 154
e9c4b1a2 155 if (!image->Ok())
95ee0ac8 156 goto error_nolines;
717b9bf2 157
479cd5de 158 lines = (unsigned char **)malloc( (size_t)(height * sizeof(unsigned char *)) );
e9c4b1a2 159 if (lines == NULL)
95ee0ac8 160 goto error_nolines;
717b9bf2 161
e9c4b1a2
JS
162 for (i = 0; i < height; i++)
163 {
479cd5de 164 if ((lines[i] = (unsigned char *)malloc( (size_t)(width * (sizeof(unsigned char) * 4)))) == NULL)
e9c4b1a2
JS
165 {
166 for ( unsigned int n = 0; n < i; n++ )
167 free( lines[n] );
168 goto error;
169 }
170 }
717b9bf2 171
e9c4b1a2
JS
172 // loaded successfully!
173 {
174 int transp = 0;
175 png_read_image( png_ptr, lines );
5ef37c79 176 png_read_end( png_ptr, info_ptr );
e9c4b1a2
JS
177 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
178 unsigned char *ptr = image->GetData();
179 if ((color_type == PNG_COLOR_TYPE_GRAY) ||
180 (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
181 {
182 for (unsigned int y = 0; y < height; y++)
183 {
184 unsigned char *ptr2 = lines[y];
185 for (unsigned int x = 0; x < width; x++)
186 {
187 unsigned char r = *ptr2++;
188 unsigned char a = *ptr2++;
189 if (a < 128)
190 {
191 *ptr++ = 255;
192 *ptr++ = 0;
193 *ptr++ = 255;
194 transp = 1;
195 }
196 else
197 {
198 *ptr++ = r;
199 *ptr++ = r;
200 *ptr++ = r;
201 }
202 }
203 }
204 }
205 else
206 {
207 for (unsigned int y = 0; y < height; y++)
208 {
209 unsigned char *ptr2 = lines[y];
210 for (unsigned int x = 0; x < width; x++)
211 {
212 unsigned char r = *ptr2++;
213 unsigned char g = *ptr2++;
214 unsigned char b = *ptr2++;
215 unsigned char a = *ptr2++;
216 if (a < 128)
217 {
218 *ptr++ = 255;
219 *ptr++ = 0;
220 *ptr++ = 255;
221 transp = 1;
222 }
223 else
224 {
225 if ((r == 255) && (g == 0) && (b == 255)) r = 254;
226 *ptr++ = r;
227 *ptr++ = g;
228 *ptr++ = b;
229 }
230 }
231 }
232 }
717b9bf2 233
e9c4b1a2
JS
234 for ( unsigned int j = 0; j < height; j++ )
235 free( lines[j] );
236 free( lines );
717b9bf2 237
e9c4b1a2
JS
238 if (transp)
239 {
240 image->SetMaskColour( 255, 0, 255 );
241 }
242 else
243 {
244 image->SetMask( FALSE );
245 }
246 }
717b9bf2 247
e9c4b1a2 248 return TRUE;
95ee0ac8
KB
249
250 error_nolines:
251 lines = NULL; // called from before it was set
252 error:
58c837a4
RR
253 if (verbose)
254 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
717b9bf2 255
e9c4b1a2
JS
256 if ( image->Ok() )
257 {
258 image->Destroy();
259 }
717b9bf2 260
e9c4b1a2
JS
261 if ( lines )
262 {
263 free( lines );
264 }
717b9bf2 265
e9c4b1a2
JS
266 if ( png_ptr )
267 {
268 if ( info_ptr )
269 {
270 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
271 free(info_ptr);
272 }
273 else
274 png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
275 }
276 return FALSE;
277}
278
deb2fec0 279bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
e9c4b1a2
JS
280{
281 {
282 png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
283 if (!png_ptr)
284 {
285 return FALSE;
286 }
717b9bf2 287
995612e2 288 if (!verbose) png_set_error_fn(png_ptr, (png_voidp)NULL, png_silent_error, png_silent_warning);
deb2fec0 289
e9c4b1a2
JS
290 png_infop info_ptr = png_create_info_struct(png_ptr);
291 if (info_ptr == NULL)
292 {
293 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
294 return FALSE;
295 }
717b9bf2 296
e9c4b1a2
JS
297 if (setjmp(png_ptr->jmpbuf))
298 {
299 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
300 return FALSE;
301 }
717b9bf2 302
e9c4b1a2 303 png_set_write_fn( png_ptr, &stream, _PNG_stream_writer, NULL);
717b9bf2 304
e9c4b1a2
JS
305 png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8,
306 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
307 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
717b9bf2 308
e9c4b1a2
JS
309 png_color_8 sig_bit;
310 sig_bit.red = 8;
311 sig_bit.green = 8;
312 sig_bit.blue = 8;
313 sig_bit.alpha = 8;
314 png_set_sBIT( png_ptr, info_ptr, &sig_bit );
315 png_write_info( png_ptr, info_ptr );
316 png_set_shift( png_ptr, &sig_bit );
317 png_set_packing( png_ptr );
717b9bf2 318
e9c4b1a2
JS
319 unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
320 if (!data)
321 {
322 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
323 return FALSE;
324 }
717b9bf2 325
e9c4b1a2
JS
326 for (int y = 0; y < image->GetHeight(); y++)
327 {
328 unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
329 for (int x = 0; x < image->GetWidth(); x++)
330 {
331 data[(x << 2) + 0] = *ptr++;
332 data[(x << 2) + 1] = *ptr++;
333 data[(x << 2) + 2] = *ptr++;
995612e2 334 if (( !image->HasMask() ) || \
a3daed02
UA
335 (data[(x << 2) + 0] != image->GetMaskRed()) || \
336 (data[(x << 2) + 1] != image->GetMaskGreen()) || \
337 (data[(x << 2) + 2] != image->GetMaskBlue()))
e9c4b1a2 338 {
a3daed02 339 data[(x << 2) + 3] = 255;
e9c4b1a2
JS
340 }
341 else
342 {
a3daed02 343 data[(x << 2) + 3] = 0;
e9c4b1a2
JS
344 }
345 }
346 png_bytep row_ptr = data;
347 png_write_rows( png_ptr, &row_ptr, 1 );
348 }
717b9bf2 349
e9c4b1a2
JS
350 free(data);
351 png_write_end( png_ptr, info_ptr );
352 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
353 }
354 return TRUE;
355}
e9c4b1a2 356
3ca6a5f0
BP
357#ifdef __VISUALC__
358 #pragma warning(default:4611)
359#endif /* VC++ */
360
995612e2 361bool wxPNGHandler::DoCanRead( wxInputStream& stream )
0828c087
VS
362{
363 unsigned char hdr[4];
feeb8165 364
33ac7e6f 365 stream.Read(hdr, 4);
0828c087
VS
366 stream.SeekI(-4, wxFromCurrent);
367 return (hdr[0] == 0x89 && hdr[1] == 'P' && hdr[2] == 'N' && hdr[3] == 'G');
368}
369
9ab6ee85 370#endif // wxUSE_STREAMS
e9c4b1a2 371
9ab6ee85 372#endif // wxUSE_LIBPNG