Avoid some warnings for unused parameter.
[synfig.git] / synfig-core / src / modules / mod_png / mptr_png.cpp
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
10 **
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.
15 **
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
21 **
22 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /*!
27 **  \todo Support 16 bit PNG files
28 **      \todo Support GAMMA correction
29 **      \todo Fix memory leaks
30 */
31
32 /* === H E A D E R S ======================================================= */
33
34 #ifdef USING_PCH
35 #       include "pch.h"
36 #else
37 #ifdef HAVE_CONFIG_H
38 #       include <config.h>
39 #endif
40
41 #include "mptr_png.h"
42 #include <synfig/importer.h>
43 #include <synfig/time.h>
44 #include <synfig/general.h>
45
46
47 #include <cstdio>
48 #include <algorithm>
49 #include <functional>
50 #endif
51
52 /* === M A C R O S ========================================================= */
53
54 using namespace synfig;
55 using namespace std;
56 using namespace etl;
57
58 #define PNG_CHECK_BYTES         8
59
60 /* === G L O B A L S ======================================================= */
61
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$");
67
68 /* === M E T H O D S ======================================================= */
69
70 void
71 png_mptr::png_out_error(png_struct */*png_data*/,const char *msg)
72 {
73         //png_mptr *me=(png_mptr*)png_data->error_ptr;
74         synfig::error(strprintf("png_mptr: error: %s",msg));
75         //me->ready=false;
76 }
77
78 void
79 png_mptr::png_out_warning(png_struct */*png_data*/,const char *msg)
80 {
81         //png_mptr *me=(png_mptr*)png_data->error_ptr;
82         synfig::warning(strprintf("png_mptr: warning: %s",msg));
83         //me->ready=false;
84 }
85
86 int
87 png_mptr::read_chunk_callback(png_struct */*png_data*/, png_unknown_chunkp /*chunk*/)
88 {
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 */
96
97         /* put your code here.  Return one of the
98           following: */
99
100         //return (-n); /* chunk had an error */
101         return (0); /* did not recognize */
102         //return (n); /* success */
103 }
104
105 png_mptr::png_mptr(const char *file_name)
106 {
107         filename=file_name;
108
109         /* Open the file pointer */
110     FILE *file = fopen(file_name, "rb");
111     if (!file)
112     {
113         //! \todo THROW SOMETHING
114                 throw strprintf("Unable to physically open %s",file_name);
115                 return;
116     }
117
118
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)
124     {
125         //! \todo THROW SOMETHING
126                 throw strprintf("This (\"%s\") doesn't appear to be a PNG file",file_name);
127                 return;
128     }
129
130
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)
135     {
136         //! \todo THROW SOMETHING
137                 throw String("error on importer construction, *WRITEME*3");
138                 return;
139     }
140
141     png_infop info_ptr = png_create_info_struct(png_ptr);
142     if (!info_ptr)
143     {
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;
149     }
150
151     png_infop end_info = png_create_info_struct(png_ptr);
152     if (!end_info)
153     {
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;
159     }
160
161
162
163         png_init_io(png_ptr, file);
164         png_set_sig_bytes(png_ptr,PNG_CHECK_BYTES);
165
166         png_read_info(png_ptr, info_ptr);
167
168         int bit_depth,color_type,interlace_type, compression_type,filter_method;
169         png_uint_32 width,height;
170
171         png_get_IHDR(png_ptr, info_ptr, &width, &height,
172                                  &bit_depth, &color_type, &interlace_type,
173                                  &compression_type, &filter_method);
174
175         if (bit_depth == 16)
176                 png_set_strip_16(png_ptr);
177
178         if (bit_depth < 8)
179                 png_set_packing(png_ptr);
180
181         double fgamma;
182         if (png_get_gAMA(png_ptr, info_ptr, &fgamma))
183         {
184                 synfig::info("PNG: Image gamma is %f",fgamma);
185                 png_set_gamma(png_ptr, gamma().get_gamma(), fgamma);
186         }
187
188
189         /*
190         if (setjmp(png_jmpbuf(png_ptr)))
191         {
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;
198         }
199         */
200
201         png_set_read_user_chunk_fn(png_ptr, this, &png_mptr::read_chunk_callback);
202
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);
208
209         png_read_update_info(png_ptr, info_ptr);
210         png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
211
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]);
217
218         png_read_image(png_ptr, row_pointers);
219
220         png_uint_32 x, y;
221         surface_buffer.set_wh(width,height);
222
223         switch(color_type)
224         {
225         case PNG_COLOR_TYPE_RGB:
226                 for(y=0;y<height;y++)
227                         for(x=0;x<width;x++)
228                         {
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(
233                                         r,
234                                         g,
235                                         b,
236                                         1.0
237                                 );
238 /*
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),
243                                         1.0
244                                 );
245 */
246                         }
247                 break;
248
249         case PNG_COLOR_TYPE_RGB_ALPHA:
250                 for(y=0;y<height;y++)
251                         for(x=0;x<width;x++)
252                         {
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(
257                                         r,
258                                         g,
259                                         b,
260                                         (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
261                                 );
262                                 /*
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)
268                                 );
269                                 */
270                         }
271                 break;
272
273         case PNG_COLOR_TYPE_GRAY:
274                 for(y=0;y<height;y++)
275                         for(x=0;x<width;x++)
276                         {
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,
283                                         1.0
284                                 );
285                         }
286                 break;
287
288         case PNG_COLOR_TYPE_GRAY_ALPHA:
289                 for(y=0;y<height;y++)
290                         for(x=0;x<width;x++)
291                         {
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)
299                                 );
300                         }
301                 break;
302
303         case PNG_COLOR_TYPE_PALETTE:
304                 for(y=0;y<height;y++)
305                         for(x=0;x<width;x++)
306                         {
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(
314                                         r,
315                                         g,
316                                         b,
317                                         a
318                                 );
319                         }
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;
326         }
327
328         png_read_end(png_ptr, end_info);
329         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
330         fclose(file);
331
332         delete [] row_pointers;
333         delete [] data;
334
335         trim = false;
336
337         //if (getenv("SYNFIG_DISABLE_CROP_IMPORTED_IMAGES"))
338                 return;
339
340         switch(color_type)
341         {
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++)
346                 {
347                         for(x=0;x<width;x++)
348                                 if (surface_buffer[y][x].get_a()) break;
349                         if (x != width) break;
350                 }
351
352                 if (y != height)
353                 {
354 #define BORDER 1                                // don't chop off all the transparent space - leave a border this many pixels wide for the interpolation
355
356                         png_uint_32 min_x, min_y, max_x, max_y;
357                         if (y>BORDER) min_y = y-BORDER; else min_y = 0;
358
359                         for(y=height-1;y>0;y--)
360                         {
361                                 for(x=0;x<width;x++)
362                                         if (surface_buffer[y][x].get_a()) break;
363                                 if (x != width) break;
364                         }
365                         max_y = std::min(y+BORDER,height-1);
366
367                         for(x=0;x<width;x++)
368                         {
369                                 for(y=0;y<height;y++)
370                                         if (surface_buffer[y][x].get_a()) break;
371                                 if (y != height) break;
372                         }
373                         if (x>BORDER) min_x = x-BORDER; else min_x = 0;
374
375                         for(x=width-1;x>0;x--)
376                         {
377                                 for(y=0;y<height;y++)
378                                         if (surface_buffer[y][x].get_a()) break;
379                                 if (y != height) break;
380                         }
381                         max_x = std::min(x+BORDER,width-1);
382
383                         if (min_x != 0 || max_x != width-1 ||
384                                 min_y != 0 || max_y != height-1)
385                         {
386                                 trim = true;
387                                 orig_width = width;
388                                 orig_height = height;
389                                 trimmed_x = min_x;
390                                 trimmed_y = min_y;
391
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;
400                         }
401                 }
402         }
403 }
404
405 png_mptr::~png_mptr()
406 {
407 }
408
409 bool
410 png_mptr::get_frame(synfig::Surface &surface, const synfig::RendDesc &/*renddesc*/, Time, synfig::ProgressCallback */*cb*/)
411 {
412         //assert(0);                                    // shouldn't be called?
413         surface=surface_buffer;
414         return true;
415 }
416
417 bool
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*/)
421 {
422         surface=surface_buffer;
423         if ((trimmed = trim))
424         {
425                 width = orig_width;
426                 height = orig_height;
427                 top = trimmed_y;
428                 left = trimmed_x;
429         }
430         return true;
431 }