Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc3 / 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 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /*!
26 **  \todo Support 16 bit PNG files
27 **      \todo Support GAMMA correction
28 **      \todo Fix memory leaks
29 */
30
31 /* === H E A D E R S ======================================================= */
32
33 #ifdef USING_PCH
34 #       include "pch.h"
35 #else
36 #ifdef HAVE_CONFIG_H
37 #       include <config.h>
38 #endif
39
40 #include "mptr_png.h"
41 #include <synfig/importer.h>
42 #include <synfig/time.h>
43 #include <synfig/general.h>
44
45
46 #include <cstdio>
47 #include <algorithm>
48 #include <functional>
49 #endif
50
51 /* === M A C R O S ========================================================= */
52
53 using namespace synfig;
54 using namespace std;
55 using namespace etl;
56
57 #define PNG_CHECK_BYTES         8
58
59 /* === G L O B A L S ======================================================= */
60
61 SYNFIG_IMPORTER_INIT(png_mptr);
62 SYNFIG_IMPORTER_SET_NAME(png_mptr,"png");
63 SYNFIG_IMPORTER_SET_EXT(png_mptr,"png");
64 SYNFIG_IMPORTER_SET_VERSION(png_mptr,"0.1");
65 SYNFIG_IMPORTER_SET_CVS_ID(png_mptr,"$Id$");
66
67 /* === M E T H O D S ======================================================= */
68
69 void
70 png_mptr::png_out_error(png_struct */*png_data*/,const char *msg)
71 {
72         //png_mptr *me=(png_mptr*)png_data->error_ptr;
73         synfig::error(strprintf("png_mptr: error: %s",msg));
74         //me->ready=false;
75 }
76
77 void
78 png_mptr::png_out_warning(png_struct */*png_data*/,const char *msg)
79 {
80         //png_mptr *me=(png_mptr*)png_data->error_ptr;
81         synfig::warning(strprintf("png_mptr: warning: %s",msg));
82         //me->ready=false;
83 }
84
85 int
86 png_mptr::read_chunk_callback(png_struct */*png_data*/, png_unknown_chunkp /*chunk*/)
87 {
88         /* The unknown chunk structure contains your
89           chunk data: */
90         //png_byte name[5];
91         //png_byte *data;
92         //png_size_t size;
93         /* Note that libpng has already taken care of
94           the CRC handling */
95
96         /* put your code here.  Return one of the
97           following: */
98
99         //return (-n); /* chunk had an error */
100         return (0); /* did not recognize */
101         //return (n); /* success */
102 }
103
104 png_mptr::png_mptr(const char *file_name)
105 {
106         filename=file_name;
107
108         /* Open the file pointer */
109     FILE *file = fopen(file_name, "rb");
110     if (!file)
111     {
112         //! \todo THROW SOMETHING
113                 throw strprintf("Unable to physically open %s",file_name);
114                 return;
115     }
116
117
118         /* Make sure we are dealing with a PNG format file */
119         png_byte header[PNG_CHECK_BYTES];
120         fread(header, 1, PNG_CHECK_BYTES, file);
121     bool is_png = !png_sig_cmp(header, 0, PNG_CHECK_BYTES);
122     if (!is_png)
123     {
124         //! \todo THROW SOMETHING
125                 throw strprintf("This (\"%s\") doesn't appear to be a PNG file",file_name);
126                 return;
127     }
128
129
130         png_structp png_ptr = png_create_read_struct
131        (PNG_LIBPNG_VER_STRING, (png_voidp)this,
132         &png_mptr::png_out_error, &png_mptr::png_out_warning);
133         if (!png_ptr)
134     {
135         //! \todo THROW SOMETHING
136                 throw String("error on importer construction, *WRITEME*3");
137                 return;
138     }
139
140     png_infop info_ptr = png_create_info_struct(png_ptr);
141     if (!info_ptr)
142     {
143         png_destroy_read_struct(&png_ptr,
144            (png_infopp)NULL, (png_infopp)NULL);
145         //! \todo THROW SOMETHING
146                 throw String("error on importer construction, *WRITEME*4");
147                 return;
148     }
149
150     png_infop end_info = png_create_info_struct(png_ptr);
151     if (!end_info)
152     {
153         png_destroy_read_struct(&png_ptr, &info_ptr,
154           (png_infopp)NULL);
155         //! \todo THROW SOMETHING
156                 throw String("error on importer construction, *WRITEME*4");
157                 return;
158     }
159
160
161
162         png_init_io(png_ptr, file);
163         png_set_sig_bytes(png_ptr,PNG_CHECK_BYTES);
164
165         double fgamma;
166         if (png_get_gAMA(png_ptr, info_ptr, &fgamma))
167         {
168                 synfig::info("PNG: Image gamma is %f",fgamma);
169                 png_set_gamma(png_ptr, gamma().get_gamma(), fgamma);
170         }
171
172
173         /*
174         if (setjmp(png_jmpbuf(png_ptr)))
175         {
176                 synfig::error("Unable to setup longjump");
177                 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
178                 fclose(file);
179         //! \todo THROW SOMETHING
180                 throw String("error on importer construction, *WRITEME*5");
181                 return;
182         }
183         */
184
185         png_set_read_user_chunk_fn(png_ptr, this, &png_mptr::read_chunk_callback);
186
187
188         png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL);
189
190         int bit_depth,color_type,interlace_type, compression_type,filter_method;
191         png_uint_32 width,height;
192
193     png_get_IHDR(png_ptr, info_ptr, &width, &height,
194        &bit_depth, &color_type, &interlace_type,
195        &compression_type, &filter_method);
196
197         png_bytep *row_pointers=new png_bytep[height];
198         row_pointers = png_get_rows(png_ptr, info_ptr);
199         int x;
200         int y;
201         surface_buffer.set_wh(width,height);
202
203         switch(color_type)
204         {
205         case PNG_COLOR_TYPE_RGB:
206                 DEBUGPOINT();
207                 for(y=0;y<surface_buffer.get_h();y++)
208                         for(x=0;x<surface_buffer.get_w();x++)
209                         {
210                                 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*3+0]);
211                                 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*3+1]);
212                                 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*3+2]);
213                                 surface_buffer[y][x]=Color(
214                                         r,
215                                         g,
216                                         b,
217                                         1.0
218                                 );
219 /*
220                                 surface_buffer[y][x]=Color(
221                                         (float)(unsigned char)row_pointers[y][x*3+0]*(1.0/255.0),
222                                         (float)(unsigned char)row_pointers[y][x*3+1]*(1.0/255.0),
223                                         (float)(unsigned char)row_pointers[y][x*3+2]*(1.0/255.0),
224                                         1.0
225                                 );
226 */
227                         }
228                 break;
229
230         case PNG_COLOR_TYPE_RGB_ALPHA:
231                 DEBUGPOINT();
232                 for(y=0;y<surface_buffer.get_h();y++)
233                         for(x=0;x<surface_buffer.get_w();x++)
234                         {
235                                 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*4+0]);
236                                 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*4+1]);
237                                 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*4+2]);
238                                 surface_buffer[y][x]=Color(
239                                         r,
240                                         g,
241                                         b,
242                                         (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
243                                 );
244                                 /*
245                                 surface_buffer[y][x]=Color(
246                                         (float)(unsigned char)row_pointers[y][x*4+0]*(1.0/255.0),
247                                         (float)(unsigned char)row_pointers[y][x*4+1]*(1.0/255.0),
248                                         (float)(unsigned char)row_pointers[y][x*4+2]*(1.0/255.0),
249                                         (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
250                                 );
251                                 */
252                         }
253                 break;
254
255         case PNG_COLOR_TYPE_GRAY:
256                 for(y=0;y<surface_buffer.get_h();y++)
257                         for(x=0;x<surface_buffer.get_w();x++)
258                         {
259                                 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x]);
260                                 //float gray=(float)(unsigned char)row_pointers[y][x]*(1.0/255.0);
261                                 surface_buffer[y][x]=Color(
262                                         gray,
263                                         gray,
264                                         gray,
265                                         1.0
266                                 );
267                         }
268                 break;
269
270         case PNG_COLOR_TYPE_GRAY_ALPHA:
271                 for(y=0;y<surface_buffer.get_h();y++)
272                         for(x=0;x<surface_buffer.get_w();x++)
273                         {
274                                 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*2]);
275 //                              float gray=(float)(unsigned char)row_pointers[y][x*2]*(1.0/255.0);
276                                 surface_buffer[y][x]=Color(
277                                         gray,
278                                         gray,
279                                         gray,
280                                         (float)(unsigned char)row_pointers[y][x*2+1]*(1.0/255.0)
281                                 );
282                         }
283                 break;
284
285         case PNG_COLOR_TYPE_PALETTE:
286                 synfig::warning("png_mptr: Paletted PNGs aren't yet fully supported.");
287                 for(y=0;y<surface_buffer.get_h();y++)
288                         for(x=0;x<surface_buffer.get_w();x++)
289                         {
290                                 float r=gamma().r_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].red);
291                                 float g=gamma().g_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].green);
292                                 float b=gamma().b_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].blue);
293                                 surface_buffer[y][x]=Color(
294                                         r,
295                                         g,
296                                         b,
297                                         1.0
298                                 );
299                         }
300                 break;
301         default:
302                 synfig::error("png_mptr: error: Unsupported color type");
303         //! \todo THROW SOMETHING
304                 throw String("error on importer construction, *WRITEME*6");
305                 return;
306         }
307
308         DEBUGPOINT();
309
310         // \fixme These shouldn't be uncommented, but for some
311         // reason, they crash the program. I will have to look into this
312         // later. This is a memory leak, but it shouldn't be too bad.
313
314         /*
315         png_read_end(png_ptr, end_info);
316         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
317         fclose(file);
318         */
319
320         delete [] row_pointers;
321 }
322
323 png_mptr::~png_mptr()
324 {
325         DEBUGPOINT();
326 }
327
328 bool
329 png_mptr::get_frame(synfig::Surface &surface,Time, synfig::ProgressCallback */*cb*/)
330 {
331         surface.mirror(surface_buffer);
332 //      surface=surface_buffer;
333         return true;
334 }