1 /* === S Y N F I G ========================================================= */
3 ** \brief ppm Target Module
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
26 ** \todo Support 16 bit PNG files
27 ** \todo Support GAMMA correction
28 ** \todo Fix memory leaks
31 /* === H E A D E R S ======================================================= */
41 #include <synfig/importer.h>
42 #include <synfig/time.h>
43 #include <synfig/general.h>
51 /* === M A C R O S ========================================================= */
53 using namespace synfig;
57 #define PNG_CHECK_BYTES 8
59 /* === G L O B A L S ======================================================= */
61 SYNFIG_IMPORTER_INIT(png_mptr);
62 SYNFIG_IMPORTER_SET_NAME(png_mptr,"png");
63 SYNFIG_IMPORTER_SET_EXT(png_mptr,"png");
64 SYNFIG_IMPORTER_SET_VERSION(png_mptr,"0.1");
65 SYNFIG_IMPORTER_SET_CVS_ID(png_mptr,"$Id$");
67 /* === M E T H O D S ======================================================= */
70 png_mptr::png_out_error(png_struct *png_data,const char *msg)
72 //png_mptr *me=(png_mptr*)png_data->error_ptr;
73 synfig::error(strprintf("png_mptr: error: %s",msg));
78 png_mptr::png_out_warning(png_struct *png_data,const char *msg)
80 //png_mptr *me=(png_mptr*)png_data->error_ptr;
81 synfig::warning(strprintf("png_mptr: warning: %s",msg));
86 png_mptr::read_chunk_callback(png_struct *png_data, png_unknown_chunkp chunk)
88 /* The unknown chunk structure contains your
93 /* Note that libpng has already taken care of
96 /* put your code here. Return one of the
99 //return (-n); /* chunk had an error */
100 return (0); /* did not recognize */
101 //return (n); /* success */
104 png_mptr::png_mptr(const char *file_name)
108 /* Open the file pointer */
109 FILE *file = fopen(file_name, "rb");
112 //! \todo THROW SOMETHING
113 throw strprintf("Unable to physically open %s",file_name);
118 /* Make sure we are dealing with a PNG format file */
119 png_byte header[PNG_CHECK_BYTES];
120 fread(header, 1, PNG_CHECK_BYTES, file);
121 bool is_png = !png_sig_cmp(header, 0, PNG_CHECK_BYTES);
124 //! \todo THROW SOMETHING
125 throw strprintf("This (\"%s\") doesn't appear to be a PNG file",file_name);
130 png_structp png_ptr = png_create_read_struct
131 (PNG_LIBPNG_VER_STRING, (png_voidp)this,
132 &png_mptr::png_out_error, &png_mptr::png_out_warning);
135 //! \todo THROW SOMETHING
136 throw String("error on importer construction, *WRITEME*3");
140 png_infop info_ptr = png_create_info_struct(png_ptr);
143 png_destroy_read_struct(&png_ptr,
144 (png_infopp)NULL, (png_infopp)NULL);
145 //! \todo THROW SOMETHING
146 throw String("error on importer construction, *WRITEME*4");
150 png_infop end_info = png_create_info_struct(png_ptr);
153 png_destroy_read_struct(&png_ptr, &info_ptr,
155 //! \todo THROW SOMETHING
156 throw String("error on importer construction, *WRITEME*4");
162 png_init_io(png_ptr, file);
163 png_set_sig_bytes(png_ptr,PNG_CHECK_BYTES);
166 if (png_get_gAMA(png_ptr, info_ptr, &fgamma))
168 synfig::info("PNG: Image gamma is %f",fgamma);
169 png_set_gamma(png_ptr, gamma().get_gamma(), fgamma);
174 if (setjmp(png_jmpbuf(png_ptr)))
176 synfig::error("Unable to setup longjump");
177 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
179 //! \todo THROW SOMETHING
180 throw String("error on importer construction, *WRITEME*5");
185 png_set_read_user_chunk_fn(png_ptr, this, &png_mptr::read_chunk_callback);
188 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL);
190 int bit_depth,color_type,interlace_type, compression_type,filter_method;
191 png_uint_32 width,height;
193 png_get_IHDR(png_ptr, info_ptr, &width, &height,
194 &bit_depth, &color_type, &interlace_type,
195 &compression_type, &filter_method);
197 png_bytep *row_pointers=new png_bytep[height];
198 row_pointers = png_get_rows(png_ptr, info_ptr);
201 surface_buffer.set_wh(width,height);
205 case PNG_COLOR_TYPE_RGB:
207 for(y=0;y<surface_buffer.get_h();y++)
208 for(x=0;x<surface_buffer.get_w();x++)
210 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*3+0]);
211 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*3+1]);
212 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*3+2]);
213 surface_buffer[y][x]=Color(
220 surface_buffer[y][x]=Color(
221 (float)(unsigned char)row_pointers[y][x*3+0]*(1.0/255.0),
222 (float)(unsigned char)row_pointers[y][x*3+1]*(1.0/255.0),
223 (float)(unsigned char)row_pointers[y][x*3+2]*(1.0/255.0),
230 case PNG_COLOR_TYPE_RGB_ALPHA:
232 for(y=0;y<surface_buffer.get_h();y++)
233 for(x=0;x<surface_buffer.get_w();x++)
235 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*4+0]);
236 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*4+1]);
237 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*4+2]);
238 surface_buffer[y][x]=Color(
242 (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
245 surface_buffer[y][x]=Color(
246 (float)(unsigned char)row_pointers[y][x*4+0]*(1.0/255.0),
247 (float)(unsigned char)row_pointers[y][x*4+1]*(1.0/255.0),
248 (float)(unsigned char)row_pointers[y][x*4+2]*(1.0/255.0),
249 (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
255 case PNG_COLOR_TYPE_GRAY:
256 for(y=0;y<surface_buffer.get_h();y++)
257 for(x=0;x<surface_buffer.get_w();x++)
259 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x]);
260 //float gray=(float)(unsigned char)row_pointers[y][x]*(1.0/255.0);
261 surface_buffer[y][x]=Color(
270 case PNG_COLOR_TYPE_GRAY_ALPHA:
271 for(y=0;y<surface_buffer.get_h();y++)
272 for(x=0;x<surface_buffer.get_w();x++)
274 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*2]);
275 // float gray=(float)(unsigned char)row_pointers[y][x*2]*(1.0/255.0);
276 surface_buffer[y][x]=Color(
280 (float)(unsigned char)row_pointers[y][x*2+1]*(1.0/255.0)
285 case PNG_COLOR_TYPE_PALETTE:
286 synfig::warning("png_mptr: Paletted PNGs aren't yet fully supported.");
287 for(y=0;y<surface_buffer.get_h();y++)
288 for(x=0;x<surface_buffer.get_w();x++)
290 float r=gamma().r_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].red);
291 float g=gamma().g_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].green);
292 float b=gamma().b_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].blue);
293 surface_buffer[y][x]=Color(
302 synfig::error("png_mptr: error: Unsupported color type");
303 //! \todo THROW SOMETHING
304 throw String("error on importer construction, *WRITEME*6");
310 // \fixme These shouldn't be uncommented, but for some
311 // reason, they crash the program. I will have to look into this
312 // later. This is a memory leak, but it shouldn't be too bad.
315 png_read_end(png_ptr, end_info);
316 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
320 delete [] row_pointers;
323 png_mptr::~png_mptr()
329 png_mptr::get_frame(synfig::Surface &surface,Time, synfig::ProgressCallback *cb)
331 surface.mirror(surface_buffer);
332 // surface=surface_buffer;