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