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