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