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