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