1 /* === S Y N F I G ========================================================= */
3 ** \brief ppm Target Module
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.
19 ** === N O T E S ===========================================================
21 ** ========================================================================= */
24 ** \todo Support 16 bit PNG files
25 ** \todo Support GAMMA correction
26 ** \todo Fix memory leaks
29 /* === H E A D E R S ======================================================= */
39 #include <synfig/importer.h>
40 #include <synfig/time.h>
41 #include <synfig/general.h>
49 /* === M A C R O S ========================================================= */
51 using namespace synfig;
55 #define PNG_CHECK_BYTES 8
57 /* === G L O B A L S ======================================================= */
59 SYNFIG_IMPORTER_INIT(png_mptr);
60 SYNFIG_IMPORTER_SET_NAME(png_mptr,"png");
61 SYNFIG_IMPORTER_SET_EXT(png_mptr,"png");
62 SYNFIG_IMPORTER_SET_VERSION(png_mptr,"0.1");
63 SYNFIG_IMPORTER_SET_CVS_ID(png_mptr,"$Id: mptr_png.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
65 /* === M E T H O D S ======================================================= */
68 png_mptr::png_out_error(png_struct *png_data,const char *msg)
70 //png_mptr *me=(png_mptr*)png_data->error_ptr;
71 synfig::error(strprintf("png_mptr: error: %s",msg));
76 png_mptr::png_out_warning(png_struct *png_data,const char *msg)
78 //png_mptr *me=(png_mptr*)png_data->error_ptr;
79 synfig::warning(strprintf("png_mptr: warning: %s",msg));
84 png_mptr::read_chunk_callback(png_struct *png_data, png_unknown_chunkp chunk)
86 /* The unknown chunk structure contains your
91 /* Note that libpng has already taken care of
94 /* put your code here. Return one of the
97 //return (-n); /* chunk had an error */
98 return (0); /* did not recognize */
99 //return (n); /* success */
102 png_mptr::png_mptr(const char *file_name)
106 /* Open the file pointer */
107 FILE *file = fopen(file_name, "rb");
110 //! \todo THROW SOMETHING
111 throw strprintf("Unable to physically open %s",file_name);
116 /* Make sure we are dealing with a PNG format file */
117 png_byte header[PNG_CHECK_BYTES];
118 fread(header, 1, PNG_CHECK_BYTES, file);
119 bool is_png = !png_sig_cmp(header, 0, PNG_CHECK_BYTES);
122 //! \todo THROW SOMETHING
123 throw strprintf("This (\"%s\") doesn't appear to be a PNG file",file_name);
128 png_structp png_ptr = png_create_read_struct
129 (PNG_LIBPNG_VER_STRING, (png_voidp)this,
130 &png_mptr::png_out_error, &png_mptr::png_out_warning);
133 //! \todo THROW SOMETHING
134 throw String("error on importer construction, *WRITEME*3");
138 png_infop info_ptr = png_create_info_struct(png_ptr);
141 png_destroy_read_struct(&png_ptr,
142 (png_infopp)NULL, (png_infopp)NULL);
143 //! \todo THROW SOMETHING
144 throw String("error on importer construction, *WRITEME*4");
148 png_infop end_info = png_create_info_struct(png_ptr);
151 png_destroy_read_struct(&png_ptr, &info_ptr,
153 //! \todo THROW SOMETHING
154 throw String("error on importer construction, *WRITEME*4");
160 png_init_io(png_ptr, file);
161 png_set_sig_bytes(png_ptr,PNG_CHECK_BYTES);
164 if (png_get_gAMA(png_ptr, info_ptr, &fgamma))
166 synfig::info("PNG: Image gamma is %f",fgamma);
167 png_set_gamma(png_ptr, gamma().get_gamma(), fgamma);
172 if (setjmp(png_jmpbuf(png_ptr)))
174 synfig::error("Unable to setup longjump");
175 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
177 //! \todo THROW SOMETHING
178 throw String("error on importer construction, *WRITEME*5");
183 png_set_read_user_chunk_fn(png_ptr, this, &png_mptr::read_chunk_callback);
186 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL);
188 int bit_depth,color_type,interlace_type, compression_type,filter_method;
189 png_uint_32 width,height;
191 png_get_IHDR(png_ptr, info_ptr, &width, &height,
192 &bit_depth, &color_type, &interlace_type,
193 &compression_type, &filter_method);
195 png_bytep *row_pointers=new png_bytep[height];
196 row_pointers = png_get_rows(png_ptr, info_ptr);
199 surface_buffer.set_wh(width,height);
203 case PNG_COLOR_TYPE_RGB:
205 for(y=0;y<surface_buffer.get_h();y++)
206 for(x=0;x<surface_buffer.get_w();x++)
208 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*3+0]);
209 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*3+1]);
210 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*3+2]);
211 surface_buffer[y][x]=Color(
218 surface_buffer[y][x]=Color(
219 (float)(unsigned char)row_pointers[y][x*3+0]*(1.0/255.0),
220 (float)(unsigned char)row_pointers[y][x*3+1]*(1.0/255.0),
221 (float)(unsigned char)row_pointers[y][x*3+2]*(1.0/255.0),
228 case PNG_COLOR_TYPE_RGB_ALPHA:
230 for(y=0;y<surface_buffer.get_h();y++)
231 for(x=0;x<surface_buffer.get_w();x++)
233 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*4+0]);
234 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*4+1]);
235 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*4+2]);
236 surface_buffer[y][x]=Color(
240 (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
243 surface_buffer[y][x]=Color(
244 (float)(unsigned char)row_pointers[y][x*4+0]*(1.0/255.0),
245 (float)(unsigned char)row_pointers[y][x*4+1]*(1.0/255.0),
246 (float)(unsigned char)row_pointers[y][x*4+2]*(1.0/255.0),
247 (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
253 case PNG_COLOR_TYPE_GRAY:
254 for(y=0;y<surface_buffer.get_h();y++)
255 for(x=0;x<surface_buffer.get_w();x++)
257 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x]);
258 //float gray=(float)(unsigned char)row_pointers[y][x]*(1.0/255.0);
259 surface_buffer[y][x]=Color(
268 case PNG_COLOR_TYPE_GRAY_ALPHA:
269 for(y=0;y<surface_buffer.get_h();y++)
270 for(x=0;x<surface_buffer.get_w();x++)
272 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*2]);
273 // float gray=(float)(unsigned char)row_pointers[y][x*2]*(1.0/255.0);
274 surface_buffer[y][x]=Color(
278 (float)(unsigned char)row_pointers[y][x*2+1]*(1.0/255.0)
283 case PNG_COLOR_TYPE_PALETTE:
284 synfig::warning("png_mptr: Paletted PNGs aren't yet fully supported.");
285 for(y=0;y<surface_buffer.get_h();y++)
286 for(x=0;x<surface_buffer.get_w();x++)
288 float r=gamma().r_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].red);
289 float g=gamma().g_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].green);
290 float b=gamma().b_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].blue);
291 surface_buffer[y][x]=Color(
300 synfig::error("png_mptr: error: Unsupported color type");
301 //! \todo THROW SOMETHING
302 throw String("error on importer construction, *WRITEME*6");
308 // \fixme These shouldn't be uncommented, but for some
309 // reason, they crash the program. I will have to look into this
310 // later. This is a memory leak, but it shouldn't be too bad.
313 png_read_end(png_ptr, end_info);
314 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
318 delete [] row_pointers;
321 png_mptr::~png_mptr()
327 png_mptr::get_frame(synfig::Surface &surface,Time, synfig::ProgressCallback *cb)
329 surface.mirror(surface_buffer);
330 // surface=surface_buffer;