1 /* === S Y N F I G ========================================================= */
3 ** \brief ppm Target Module
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
27 ** \todo Support 16 bit PNG files
28 ** \todo Support GAMMA correction
29 ** \todo Fix memory leaks
32 /* === H E A D E R S ======================================================= */
42 #include <synfig/importer.h>
43 #include <synfig/time.h>
44 #include <synfig/general.h>
52 /* === M A C R O S ========================================================= */
54 using namespace synfig;
58 #define PNG_CHECK_BYTES 8
60 /* === G L O B A L S ======================================================= */
62 SYNFIG_IMPORTER_INIT(png_mptr);
63 SYNFIG_IMPORTER_SET_NAME(png_mptr,"png");
64 SYNFIG_IMPORTER_SET_EXT(png_mptr,"png");
65 SYNFIG_IMPORTER_SET_VERSION(png_mptr,"0.1");
66 SYNFIG_IMPORTER_SET_CVS_ID(png_mptr,"$Id$");
68 /* === M E T H O D S ======================================================= */
71 png_mptr::png_out_error(png_struct */*png_data*/,const char *msg)
73 //png_mptr *me=(png_mptr*)png_data->error_ptr;
74 synfig::error(strprintf("png_mptr: error: %s",msg));
79 png_mptr::png_out_warning(png_struct */*png_data*/,const char *msg)
81 //png_mptr *me=(png_mptr*)png_data->error_ptr;
82 synfig::warning(strprintf("png_mptr: warning: %s",msg));
87 png_mptr::read_chunk_callback(png_struct */*png_data*/, png_unknown_chunkp /*chunk*/)
89 /* The unknown chunk structure contains your
94 /* Note that libpng has already taken care of
97 /* put your code here. Return one of the
100 //return (-n); /* chunk had an error */
101 return (0); /* did not recognize */
102 //return (n); /* success */
105 png_mptr::png_mptr(const char *file_name)
109 /* Open the file pointer */
110 FILE *file = fopen(file_name, "rb");
113 //! \todo THROW SOMETHING
114 throw strprintf("Unable to physically open %s",file_name);
119 /* Make sure we are dealing with a PNG format file */
120 png_byte header[PNG_CHECK_BYTES];
121 fread(header, 1, PNG_CHECK_BYTES, file);
122 bool is_png = !png_sig_cmp(header, 0, PNG_CHECK_BYTES);
125 //! \todo THROW SOMETHING
126 throw strprintf("This (\"%s\") doesn't appear to be a PNG file",file_name);
131 png_structp png_ptr = png_create_read_struct
132 (PNG_LIBPNG_VER_STRING, (png_voidp)this,
133 &png_mptr::png_out_error, &png_mptr::png_out_warning);
136 //! \todo THROW SOMETHING
137 throw String("error on importer construction, *WRITEME*3");
141 png_infop info_ptr = png_create_info_struct(png_ptr);
144 png_destroy_read_struct(&png_ptr,
145 (png_infopp)NULL, (png_infopp)NULL);
146 //! \todo THROW SOMETHING
147 throw String("error on importer construction, *WRITEME*4");
151 png_infop end_info = png_create_info_struct(png_ptr);
154 png_destroy_read_struct(&png_ptr, &info_ptr,
156 //! \todo THROW SOMETHING
157 throw String("error on importer construction, *WRITEME*4");
163 png_init_io(png_ptr, file);
164 png_set_sig_bytes(png_ptr,PNG_CHECK_BYTES);
166 png_read_info(png_ptr, info_ptr);
168 int bit_depth,color_type,interlace_type, compression_type,filter_method;
169 png_uint_32 width,height;
171 png_get_IHDR(png_ptr, info_ptr, &width, &height,
172 &bit_depth, &color_type, &interlace_type,
173 &compression_type, &filter_method);
176 png_set_strip_16(png_ptr);
179 png_set_packing(png_ptr);
182 if (png_get_gAMA(png_ptr, info_ptr, &fgamma))
184 synfig::info("PNG: Image gamma is %f",fgamma);
185 png_set_gamma(png_ptr, gamma().get_gamma(), fgamma);
190 if (setjmp(png_jmpbuf(png_ptr)))
192 synfig::error("Unable to setup longjump");
193 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
195 //! \todo THROW SOMETHING
196 throw String("error on importer construction, *WRITEME*5");
201 png_set_read_user_chunk_fn(png_ptr, this, &png_mptr::read_chunk_callback);
203 // man libpng tells me:
204 // You must use png_transforms and not call any
205 // png_set_transform() functions when you use png_read_png().
206 // but we used png_set_gamma(), which may be why we were seeing a crash at the end
207 // png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL);
209 png_read_update_info(png_ptr, info_ptr);
210 png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
212 // allocate buffer to read image data into
213 png_bytep *row_pointers=new png_bytep[height];
214 png_byte *data = new png_byte[rowbytes*height];
215 for (png_uint_32 i = 0; i < height; i++)
216 row_pointers[i] = &(data[rowbytes*i]);
218 png_read_image(png_ptr, row_pointers);
221 surface_buffer.set_wh(width,height);
225 case PNG_COLOR_TYPE_RGB:
226 for(y=0;y<height;y++)
229 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*3+0]);
230 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*3+1]);
231 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*3+2]);
232 surface_buffer[y][x]=Color(
239 surface_buffer[y][x]=Color(
240 (float)(unsigned char)row_pointers[y][x*3+0]*(1.0/255.0),
241 (float)(unsigned char)row_pointers[y][x*3+1]*(1.0/255.0),
242 (float)(unsigned char)row_pointers[y][x*3+2]*(1.0/255.0),
249 case PNG_COLOR_TYPE_RGB_ALPHA:
250 for(y=0;y<height;y++)
253 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*4+0]);
254 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*4+1]);
255 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*4+2]);
256 surface_buffer[y][x]=Color(
260 (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
263 surface_buffer[y][x]=Color(
264 (float)(unsigned char)row_pointers[y][x*4+0]*(1.0/255.0),
265 (float)(unsigned char)row_pointers[y][x*4+1]*(1.0/255.0),
266 (float)(unsigned char)row_pointers[y][x*4+2]*(1.0/255.0),
267 (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
273 case PNG_COLOR_TYPE_GRAY:
274 for(y=0;y<height;y++)
277 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x]);
278 //float gray=(float)(unsigned char)row_pointers[y][x]*(1.0/255.0);
279 surface_buffer[y][x]=Color(
288 case PNG_COLOR_TYPE_GRAY_ALPHA:
289 for(y=0;y<height;y++)
292 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*2]);
293 // float gray=(float)(unsigned char)row_pointers[y][x*2]*(1.0/255.0);
294 surface_buffer[y][x]=Color(
298 (float)(unsigned char)row_pointers[y][x*2+1]*(1.0/255.0)
303 case PNG_COLOR_TYPE_PALETTE:
304 for(y=0;y<height;y++)
307 float r=gamma().r_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].red);
308 float g=gamma().g_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].green);
309 float b=gamma().b_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].blue);
311 if(info_ptr->valid & PNG_INFO_tRNS)
312 a = (float)(unsigned char)png_ptr->trans[row_pointers[y][x]]*(1.0/255.0);
313 surface_buffer[y][x]=Color(
322 synfig::error("png_mptr: error: Unsupported color type");
323 //! \todo THROW SOMETHING
324 throw String("error on importer construction, *WRITEME*6");
328 png_read_end(png_ptr, end_info);
329 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
332 delete [] row_pointers;
337 //if (getenv("SYNFIG_DISABLE_CROP_IMPORTED_IMAGES"))
342 case PNG_COLOR_TYPE_RGB_ALPHA:
343 case PNG_COLOR_TYPE_GRAY_ALPHA:
344 case PNG_COLOR_TYPE_PALETTE:
345 for(y=0;y<height;y++)
348 if (surface_buffer[y][x].get_a()) break;
349 if (x != width) break;
354 #define BORDER 1 // don't chop off all the transparent space - leave a border this many pixels wide for the interpolation
356 png_uint_32 min_x, min_y, max_x, max_y;
357 if (y>BORDER) min_y = y-BORDER; else min_y = 0;
359 for(y=height-1;y>0;y--)
362 if (surface_buffer[y][x].get_a()) break;
363 if (x != width) break;
365 max_y = std::min(y+BORDER,height-1);
369 for(y=0;y<height;y++)
370 if (surface_buffer[y][x].get_a()) break;
371 if (y != height) break;
373 if (x>BORDER) min_x = x-BORDER; else min_x = 0;
375 for(x=width-1;x>0;x--)
377 for(y=0;y<height;y++)
378 if (surface_buffer[y][x].get_a()) break;
379 if (y != height) break;
381 max_x = std::min(x+BORDER,width-1);
383 if (min_x != 0 || max_x != width-1 ||
384 min_y != 0 || max_y != height-1)
388 orig_height = height;
393 height=max_y-min_y+1;
394 synfig::Surface tmp_buffer;
395 tmp_buffer.set_wh(width,height);
396 for(y=0;y<height;y++)
398 tmp_buffer[y][x] = surface_buffer[y+min_y][x+min_x];
399 surface_buffer = tmp_buffer;
405 png_mptr::~png_mptr()
410 png_mptr::get_frame(synfig::Surface &surface, const synfig::RendDesc &/*renddesc*/, Time, synfig::ProgressCallback */*cb*/)
412 //assert(0); // shouldn't be called?
413 surface=surface_buffer;
418 png_mptr::get_frame(synfig::Surface &surface, const synfig::RendDesc &renddesc, Time,
419 bool &trimmed, unsigned int &width, unsigned int &height, unsigned int &top, unsigned int &left,
420 synfig::ProgressCallback */*cb*/)
422 surface=surface_buffer;
423 if ((trimmed = trim))
426 height = orig_height;