]> git.saurik.com Git - wxWidgets.git/blame - src/common/imagpng.cpp
Changed BidEndianOrdered to BigEndianOrdered in datstrm.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
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
ce4169a4
RR
26#if wxUSE_LIBPNG
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
e9c4b1a2 60#if wxUSE_STREAMS
ce4169a4 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
78bool wxPNGHandler::LoadFile( wxImage *image, wxInputStream& stream )
79{
80 // VZ: as this function uses setjmp() the only fool proof error handling
81 // method is to use goto (setjmp is not really C++ dtors friendly...)
717b9bf2 82
95ee0ac8 83 unsigned char **lines;
e9c4b1a2
JS
84 unsigned int i;
85 png_infop info_ptr = (png_infop) NULL;
717b9bf2 86
e9c4b1a2 87 image->Destroy();
717b9bf2 88
e9c4b1a2
JS
89 png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
90 (voidp) NULL,
91 (png_error_ptr) NULL,
92 (png_error_ptr) NULL );
93 if (!png_ptr)
95ee0ac8 94 goto error_nolines;
717b9bf2 95
e9c4b1a2
JS
96 info_ptr = png_create_info_struct( png_ptr );
97 if (!info_ptr)
95ee0ac8 98 goto error_nolines;
717b9bf2 99
e9c4b1a2 100 if (setjmp(png_ptr->jmpbuf))
95ee0ac8 101 goto error_nolines;
717b9bf2 102
e9c4b1a2 103 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
95ee0ac8 104 goto error_nolines;
717b9bf2 105
e9c4b1a2 106 png_set_read_fn( png_ptr, &stream, _PNG_stream_reader);
717b9bf2 107
e9c4b1a2
JS
108 png_uint_32 width,height;
109 int bit_depth,color_type,interlace_type;
717b9bf2 110
e9c4b1a2
JS
111 png_read_info( png_ptr, info_ptr );
112 png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL );
717b9bf2 113
e9c4b1a2
JS
114 if (color_type == PNG_COLOR_TYPE_PALETTE)
115 png_set_expand( png_ptr );
717b9bf2 116
e9c4b1a2
JS
117 png_set_strip_16( png_ptr );
118 png_set_packing( png_ptr );
119 if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS))
120 png_set_expand( png_ptr );
121 png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER );
717b9bf2 122
e9c4b1a2 123 image->Create( width, height );
717b9bf2 124
e9c4b1a2 125 if (!image->Ok())
95ee0ac8 126 goto error_nolines;
717b9bf2 127
e9c4b1a2
JS
128 lines = (unsigned char **)malloc( height * sizeof(unsigned char *) );
129 if (lines == NULL)
95ee0ac8 130 goto error_nolines;
717b9bf2 131
e9c4b1a2
JS
132 for (i = 0; i < height; i++)
133 {
134 if ((lines[i] = (unsigned char *)malloc(width * (sizeof(unsigned char) * 4))) == NULL)
135 {
136 for ( unsigned int n = 0; n < i; n++ )
137 free( lines[n] );
138 goto error;
139 }
140 }
717b9bf2 141
e9c4b1a2
JS
142 // loaded successfully!
143 {
144 int transp = 0;
145 png_read_image( png_ptr, lines );
146 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
147 unsigned char *ptr = image->GetData();
148 if ((color_type == PNG_COLOR_TYPE_GRAY) ||
149 (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
150 {
151 for (unsigned int y = 0; y < height; y++)
152 {
153 unsigned char *ptr2 = lines[y];
154 for (unsigned int x = 0; x < width; x++)
155 {
156 unsigned char r = *ptr2++;
157 unsigned char a = *ptr2++;
158 if (a < 128)
159 {
160 *ptr++ = 255;
161 *ptr++ = 0;
162 *ptr++ = 255;
163 transp = 1;
164 }
165 else
166 {
167 *ptr++ = r;
168 *ptr++ = r;
169 *ptr++ = r;
170 }
171 }
172 }
173 }
174 else
175 {
176 for (unsigned int y = 0; y < height; y++)
177 {
178 unsigned char *ptr2 = lines[y];
179 for (unsigned int x = 0; x < width; x++)
180 {
181 unsigned char r = *ptr2++;
182 unsigned char g = *ptr2++;
183 unsigned char b = *ptr2++;
184 unsigned char a = *ptr2++;
185 if (a < 128)
186 {
187 *ptr++ = 255;
188 *ptr++ = 0;
189 *ptr++ = 255;
190 transp = 1;
191 }
192 else
193 {
194 if ((r == 255) && (g == 0) && (b == 255)) r = 254;
195 *ptr++ = r;
196 *ptr++ = g;
197 *ptr++ = b;
198 }
199 }
200 }
201 }
717b9bf2 202
e9c4b1a2
JS
203 for ( unsigned int j = 0; j < height; j++ )
204 free( lines[j] );
205 free( lines );
717b9bf2 206
e9c4b1a2
JS
207 if (transp)
208 {
209 image->SetMaskColour( 255, 0, 255 );
210 }
211 else
212 {
213 image->SetMask( FALSE );
214 }
215 }
717b9bf2 216
e9c4b1a2 217 return TRUE;
95ee0ac8
KB
218
219 error_nolines:
220 lines = NULL; // called from before it was set
221 error:
e9c4b1a2 222 wxLogError(_("Couldn't load a PNG image - probably file is corrupted."));
717b9bf2 223
e9c4b1a2
JS
224 if ( image->Ok() )
225 {
226 image->Destroy();
227 }
717b9bf2 228
e9c4b1a2
JS
229 if ( lines )
230 {
231 free( lines );
232 }
717b9bf2 233
e9c4b1a2
JS
234 if ( png_ptr )
235 {
236 if ( info_ptr )
237 {
238 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
239 free(info_ptr);
240 }
241 else
242 png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
243 }
244 return FALSE;
245}
246
247
248bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream )
249{
250 {
251 png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
252 if (!png_ptr)
253 {
254 return FALSE;
255 }
717b9bf2 256
e9c4b1a2
JS
257 png_infop info_ptr = png_create_info_struct(png_ptr);
258 if (info_ptr == NULL)
259 {
260 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
261 return FALSE;
262 }
717b9bf2 263
e9c4b1a2
JS
264 if (setjmp(png_ptr->jmpbuf))
265 {
266 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
267 return FALSE;
268 }
717b9bf2 269
e9c4b1a2 270 png_set_write_fn( png_ptr, &stream, _PNG_stream_writer, NULL);
717b9bf2 271
e9c4b1a2
JS
272 png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8,
273 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
274 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
717b9bf2 275
e9c4b1a2
JS
276 png_color_8 sig_bit;
277 sig_bit.red = 8;
278 sig_bit.green = 8;
279 sig_bit.blue = 8;
280 sig_bit.alpha = 8;
281 png_set_sBIT( png_ptr, info_ptr, &sig_bit );
282 png_write_info( png_ptr, info_ptr );
283 png_set_shift( png_ptr, &sig_bit );
284 png_set_packing( png_ptr );
717b9bf2 285
e9c4b1a2
JS
286 unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 );
287 if (!data)
288 {
289 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
290 return FALSE;
291 }
717b9bf2 292
e9c4b1a2
JS
293 for (int y = 0; y < image->GetHeight(); y++)
294 {
295 unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3);
296 for (int x = 0; x < image->GetWidth(); x++)
297 {
298 data[(x << 2) + 0] = *ptr++;
299 data[(x << 2) + 1] = *ptr++;
300 data[(x << 2) + 2] = *ptr++;
301 if ((data[(x << 2) + 0] == image->GetMaskRed()) &&
302 (data[(x << 2) + 1] == image->GetMaskGreen()) &&
303 (data[(x << 2) + 2] == image->GetMaskBlue()))
304 {
305 data[(x << 2) + 3] = 0;
306 }
307 else
308 {
309 data[(x << 2) + 3] = 255;
310 }
311 }
312 png_bytep row_ptr = data;
313 png_write_rows( png_ptr, &row_ptr, 1 );
314 }
717b9bf2 315
e9c4b1a2
JS
316 free(data);
317 png_write_end( png_ptr, info_ptr );
318 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
319 }
320 return TRUE;
321}
e9c4b1a2 322
717b9bf2 323#endif
ce4169a4 324 // wxUSE_STREAMS
e9c4b1a2 325
717b9bf2 326#endif
ce4169a4 327 // wxUSE_LIBPNG
e9c4b1a2 328