1 /*! ========================================================================
4 ** $Id: mptr_png.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
6 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
23 ** \todo Support 16 bit PNG files
24 ** \todo Support GAMMA correction
25 ** \todo Fix memory leaks
28 /* === H E A D E R S ======================================================= */
38 #include <synfig/importer.h>
39 #include <synfig/time.h>
40 #include <synfig/general.h>
48 /* === M A C R O S ========================================================= */
50 using namespace synfig;
54 #define PNG_CHECK_BYTES 8
56 /* === G L O B A L S ======================================================= */
58 SYNFIG_IMPORTER_INIT(png_mptr);
59 SYNFIG_IMPORTER_SET_NAME(png_mptr,"png_mptr");
60 SYNFIG_IMPORTER_SET_EXT(png_mptr,"png");
61 SYNFIG_IMPORTER_SET_VERSION(png_mptr,"0.1");
62 SYNFIG_IMPORTER_SET_CVS_ID(png_mptr,"$Id: mptr_png.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
64 /* === M E T H O D S ======================================================= */
67 png_mptr::png_out_error(png_struct *png_data,const char *msg)
69 //png_mptr *me=(png_mptr*)png_data->error_ptr;
70 synfig::error(strprintf("png_mptr: error: %s",msg));
75 png_mptr::png_out_warning(png_struct *png_data,const char *msg)
77 //png_mptr *me=(png_mptr*)png_data->error_ptr;
78 synfig::warning(strprintf("png_mptr: warning: %s",msg));
83 png_mptr::read_chunk_callback(png_struct *png_data, png_unknown_chunkp chunk)
85 /* The unknown chunk structure contains your
90 /* Note that libpng has already taken care of
93 /* put your code here. Return one of the
96 //return (-n); /* chunk had an error */
97 return (0); /* did not recognize */
98 //return (n); /* success */
101 png_mptr::png_mptr(const char *file_name)
105 /* Open the file pointer */
106 FILE *file = fopen(file_name, "rb");
109 //! \todo THROW SOMETHING
110 throw strprintf("Unable to physically open %s",file_name);
115 /* Make sure we are dealing with a PNG format file */
116 png_byte header[PNG_CHECK_BYTES];
117 fread(header, 1, PNG_CHECK_BYTES, file);
118 bool is_png = !png_sig_cmp(header, 0, PNG_CHECK_BYTES);
121 //! \todo THROW SOMETHING
122 throw strprintf("This (\"%s\") doesn't appear to be a PNG file",file_name);
127 png_structp png_ptr = png_create_read_struct
128 (PNG_LIBPNG_VER_STRING, (png_voidp)this,
129 &png_mptr::png_out_error, &png_mptr::png_out_warning);
132 //! \todo THROW SOMETHING
133 throw String("error on importer construction, *WRITEME*3");
137 png_infop info_ptr = png_create_info_struct(png_ptr);
140 png_destroy_read_struct(&png_ptr,
141 (png_infopp)NULL, (png_infopp)NULL);
142 //! \todo THROW SOMETHING
143 throw String("error on importer construction, *WRITEME*4");
147 png_infop end_info = png_create_info_struct(png_ptr);
150 png_destroy_read_struct(&png_ptr, &info_ptr,
152 //! \todo THROW SOMETHING
153 throw String("error on importer construction, *WRITEME*4");
159 png_init_io(png_ptr, file);
160 png_set_sig_bytes(png_ptr,PNG_CHECK_BYTES);
163 if (png_get_gAMA(png_ptr, info_ptr, &fgamma))
165 synfig::info("PNG: Image gamma is %f",fgamma);
166 png_set_gamma(png_ptr, gamma().get_gamma(), fgamma);
171 if (setjmp(png_jmpbuf(png_ptr)))
173 synfig::error("Unable to setup longjump");
174 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
176 //! \todo THROW SOMETHING
177 throw String("error on importer construction, *WRITEME*5");
182 png_set_read_user_chunk_fn(png_ptr, this, &png_mptr::read_chunk_callback);
185 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL);
187 int bit_depth,color_type,interlace_type, compression_type,filter_method;
188 png_uint_32 width,height;
190 png_get_IHDR(png_ptr, info_ptr, &width, &height,
191 &bit_depth, &color_type, &interlace_type,
192 &compression_type, &filter_method);
194 png_bytep *row_pointers=new png_bytep[height];
195 row_pointers = png_get_rows(png_ptr, info_ptr);
198 surface_buffer.set_wh(width,height);
202 case PNG_COLOR_TYPE_RGB:
204 for(y=0;y<surface_buffer.get_h();y++)
205 for(x=0;x<surface_buffer.get_w();x++)
207 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*3+0]);
208 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*3+1]);
209 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*3+2]);
210 surface_buffer[y][x]=Color(
217 surface_buffer[y][x]=Color(
218 (float)(unsigned char)row_pointers[y][x*3+0]*(1.0/255.0),
219 (float)(unsigned char)row_pointers[y][x*3+1]*(1.0/255.0),
220 (float)(unsigned char)row_pointers[y][x*3+2]*(1.0/255.0),
227 case PNG_COLOR_TYPE_RGB_ALPHA:
229 for(y=0;y<surface_buffer.get_h();y++)
230 for(x=0;x<surface_buffer.get_w();x++)
232 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*4+0]);
233 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*4+1]);
234 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*4+2]);
235 surface_buffer[y][x]=Color(
239 (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
242 surface_buffer[y][x]=Color(
243 (float)(unsigned char)row_pointers[y][x*4+0]*(1.0/255.0),
244 (float)(unsigned char)row_pointers[y][x*4+1]*(1.0/255.0),
245 (float)(unsigned char)row_pointers[y][x*4+2]*(1.0/255.0),
246 (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
252 case PNG_COLOR_TYPE_GRAY:
253 for(y=0;y<surface_buffer.get_h();y++)
254 for(x=0;x<surface_buffer.get_w();x++)
256 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x]);
257 //float gray=(float)(unsigned char)row_pointers[y][x]*(1.0/255.0);
258 surface_buffer[y][x]=Color(
267 case PNG_COLOR_TYPE_GRAY_ALPHA:
268 for(y=0;y<surface_buffer.get_h();y++)
269 for(x=0;x<surface_buffer.get_w();x++)
271 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*2]);
272 // float gray=(float)(unsigned char)row_pointers[y][x*2]*(1.0/255.0);
273 surface_buffer[y][x]=Color(
277 (float)(unsigned char)row_pointers[y][x*2+1]*(1.0/255.0)
282 case PNG_COLOR_TYPE_PALETTE:
283 synfig::warning("png_mptr: Paletted PNGs aren't yet fully supported.");
284 for(y=0;y<surface_buffer.get_h();y++)
285 for(x=0;x<surface_buffer.get_w();x++)
287 float r=gamma().r_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].red);
288 float g=gamma().g_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].green);
289 float b=gamma().b_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].blue);
290 surface_buffer[y][x]=Color(
299 synfig::error("png_mptr: error: Unsupported color type");
300 //! \todo THROW SOMETHING
301 throw String("error on importer construction, *WRITEME*6");
307 // \fixme These shouldn't be uncommented, but for some
308 // reason, they crash the program. I will have to look into this
309 // later. This is a memory leak, but it shouldn't be too bad.
312 png_read_end(png_ptr, end_info);
313 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
317 delete [] row_pointers;
320 png_mptr::~png_mptr()
326 png_mptr::get_frame(synfig::Surface &surface,Time, synfig::ProgressCallback *cb)
328 surface.mirror(surface_buffer);
329 // surface=surface_buffer;