Remove spaces and tabs at end of lines.
[synfig.git] / synfig-core / trunk / src / modules / mod_png / mptr_png.cpp
blobffdc153a6e30d556c17ee94cf34f007f4cfb993e
1 /* === S Y N F I G ========================================================= */
2 /*! \file mptr_png.cpp
3 ** \brief ppm Target Module
4 **
5 ** $Id$
6 **
7 ** \legal
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.
20 ** \endlegal
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /*!
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 ======================================================= */
34 #ifdef USING_PCH
35 # include "pch.h"
36 #else
37 #ifdef HAVE_CONFIG_H
38 # include <config.h>
39 #endif
41 #include "mptr_png.h"
42 #include <synfig/importer.h>
43 #include <synfig/time.h>
44 #include <synfig/general.h>
47 #include <cstdio>
48 #include <algorithm>
49 #include <functional>
50 #endif
52 /* === M A C R O S ========================================================= */
54 using namespace synfig;
55 using namespace std;
56 using namespace etl;
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 ======================================================= */
70 void
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));
75 //me->ready=false;
78 void
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));
83 //me->ready=false;
86 int
87 png_mptr::read_chunk_callback(png_struct */*png_data*/, png_unknown_chunkp /*chunk*/)
89 /* The unknown chunk structure contains your
90 chunk data: */
91 //png_byte name[5];
92 //png_byte *data;
93 //png_size_t size;
94 /* Note that libpng has already taken care of
95 the CRC handling */
97 /* put your code here. Return one of the
98 following: */
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)
107 filename=file_name;
109 /* Open the file pointer */
110 FILE *file = fopen(file_name, "rb");
111 if (!file)
113 //! \todo THROW SOMETHING
114 throw strprintf("Unable to physically open %s",file_name);
115 return;
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);
123 if (!is_png)
125 //! \todo THROW SOMETHING
126 throw strprintf("This (\"%s\") doesn't appear to be a PNG file",file_name);
127 return;
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);
134 if (!png_ptr)
136 //! \todo THROW SOMETHING
137 throw String("error on importer construction, *WRITEME*3");
138 return;
141 png_infop info_ptr = png_create_info_struct(png_ptr);
142 if (!info_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");
148 return;
151 png_infop end_info = png_create_info_struct(png_ptr);
152 if (!end_info)
154 png_destroy_read_struct(&png_ptr, &info_ptr,
155 (png_infopp)NULL);
156 //! \todo THROW SOMETHING
157 throw String("error on importer construction, *WRITEME*4");
158 return;
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);
175 if (bit_depth == 16)
176 png_set_strip_16(png_ptr);
178 if (bit_depth < 8)
179 png_set_packing(png_ptr);
181 double fgamma;
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);
194 fclose(file);
195 //! \todo THROW SOMETHING
196 throw String("error on importer construction, *WRITEME*5");
197 return;
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);
220 png_uint_32 x, y;
221 surface_buffer.set_wh(width,height);
223 switch(color_type)
225 case PNG_COLOR_TYPE_RGB:
226 for(y=0;y<height;y++)
227 for(x=0;x<width;x++)
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),
247 break;
249 case PNG_COLOR_TYPE_RGB_ALPHA:
250 for(y=0;y<height;y++)
251 for(x=0;x<width;x++)
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)
271 break;
273 case PNG_COLOR_TYPE_GRAY:
274 for(y=0;y<height;y++)
275 for(x=0;x<width;x++)
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(
280 gray,
281 gray,
282 gray,
286 break;
288 case PNG_COLOR_TYPE_GRAY_ALPHA:
289 for(y=0;y<height;y++)
290 for(x=0;x<width;x++)
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(
295 gray,
296 gray,
297 gray,
298 (float)(unsigned char)row_pointers[y][x*2+1]*(1.0/255.0)
301 break;
303 case PNG_COLOR_TYPE_PALETTE:
304 for(y=0;y<height;y++)
305 for(x=0;x<width;x++)
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);
310 float a=1.0;
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(
320 break;
321 default:
322 synfig::error("png_mptr: error: Unsupported color type");
323 //! \todo THROW SOMETHING
324 throw String("error on importer construction, *WRITEME*6");
325 return;
328 png_read_end(png_ptr, end_info);
329 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
330 fclose(file);
332 delete [] row_pointers;
333 delete [] data;
335 trim = false;
337 if (getenv("SYNFIG_DISABLE_CROP_IMPORTED_IMAGES"))
338 return;
340 switch(color_type)
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++)
347 for(x=0;x<width;x++)
348 if (surface_buffer[y][x].get_a()) break;
349 if (x != width) break;
352 if (y != height)
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--)
361 for(x=0;x<width;x++)
362 if (surface_buffer[y][x].get_a()) break;
363 if (x != width) break;
365 max_y = std::min(y+BORDER,height-1);
367 for(x=0;x<width;x++)
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)
386 trim = true;
387 orig_width = width;
388 orig_height = height;
389 trimmed_x = min_x;
390 trimmed_y = min_y;
392 width=max_x-min_x+1;
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++)
397 for(x=0;x<width;x++)
398 tmp_buffer[y][x] = surface_buffer[y+min_y][x+min_x];
399 surface_buffer = tmp_buffer;
405 png_mptr::~png_mptr()
409 bool
410 png_mptr::get_frame(synfig::Surface &surface,Time, synfig::ProgressCallback */*cb*/)
412 assert(0); // shouldn't be called?
413 surface=surface_buffer;
414 return true;
417 bool
418 png_mptr::get_frame(synfig::Surface &surface,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))
425 width = orig_width;
426 height = orig_height;
427 top = trimmed_y;
428 left = trimmed_x;
430 return true;