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