Removed a DEBUGPOINT().
[synfig.git] / synfig-core / trunk / 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         png_read_info(png_ptr, info_ptr);
166
167         int bit_depth,color_type,interlace_type, compression_type,filter_method;
168         png_uint_32 width,height;
169
170         png_get_IHDR(png_ptr, info_ptr, &width, &height,
171                                  &bit_depth, &color_type, &interlace_type,
172                                  &compression_type, &filter_method);
173
174         if (bit_depth == 16)
175                 png_set_strip_16(png_ptr);
176
177         if (bit_depth < 8)
178                 png_set_packing(png_ptr);
179
180         double fgamma;
181         if (png_get_gAMA(png_ptr, info_ptr, &fgamma))
182         {
183                 synfig::info("PNG: Image gamma is %f",fgamma);
184                 png_set_gamma(png_ptr, gamma().get_gamma(), fgamma);
185         }
186
187
188         /*
189         if (setjmp(png_jmpbuf(png_ptr)))
190         {
191                 synfig::error("Unable to setup longjump");
192                 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
193                 fclose(file);
194         //! \todo THROW SOMETHING
195                 throw String("error on importer construction, *WRITEME*5");
196                 return;
197         }
198         */
199
200         png_set_read_user_chunk_fn(png_ptr, this, &png_mptr::read_chunk_callback);
201
202         // man libpng tells me:
203         //   You must use png_transforms and not call any
204         //   png_set_transform() functions when you use png_read_png().
205         // but we used png_set_gamma(), which may be why we were seeing a crash at the end
206         //   png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL);
207
208         png_read_update_info(png_ptr, info_ptr);
209         png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
210
211         // allocate buffer to read image data into
212         png_bytep *row_pointers=new png_bytep[height];
213         png_byte *data = new png_byte[rowbytes*height];
214         for (png_uint_32 i = 0; i < height; i++)
215                 row_pointers[i] = &(data[rowbytes*i]);
216
217         png_read_image(png_ptr, row_pointers);
218
219         int x;
220         int y;
221         surface_buffer.set_wh(width,height);
222
223         switch(color_type)
224         {
225         case PNG_COLOR_TYPE_RGB:
226                 DEBUGPOINT();
227                 for(y=0;y<surface_buffer.get_h();y++)
228                         for(x=0;x<surface_buffer.get_w();x++)
229                         {
230                                 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*3+0]);
231                                 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*3+1]);
232                                 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*3+2]);
233                                 surface_buffer[y][x]=Color(
234                                         r,
235                                         g,
236                                         b,
237                                         1.0
238                                 );
239 /*
240                                 surface_buffer[y][x]=Color(
241                                         (float)(unsigned char)row_pointers[y][x*3+0]*(1.0/255.0),
242                                         (float)(unsigned char)row_pointers[y][x*3+1]*(1.0/255.0),
243                                         (float)(unsigned char)row_pointers[y][x*3+2]*(1.0/255.0),
244                                         1.0
245                                 );
246 */
247                         }
248                 break;
249
250         case PNG_COLOR_TYPE_RGB_ALPHA:
251                 DEBUGPOINT();
252                 for(y=0;y<surface_buffer.get_h();y++)
253                         for(x=0;x<surface_buffer.get_w();x++)
254                         {
255                                 float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*4+0]);
256                                 float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*4+1]);
257                                 float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*4+2]);
258                                 surface_buffer[y][x]=Color(
259                                         r,
260                                         g,
261                                         b,
262                                         (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
263                                 );
264                                 /*
265                                 surface_buffer[y][x]=Color(
266                                         (float)(unsigned char)row_pointers[y][x*4+0]*(1.0/255.0),
267                                         (float)(unsigned char)row_pointers[y][x*4+1]*(1.0/255.0),
268                                         (float)(unsigned char)row_pointers[y][x*4+2]*(1.0/255.0),
269                                         (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
270                                 );
271                                 */
272                         }
273                 break;
274
275         case PNG_COLOR_TYPE_GRAY:
276                 for(y=0;y<surface_buffer.get_h();y++)
277                         for(x=0;x<surface_buffer.get_w();x++)
278                         {
279                                 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x]);
280                                 //float gray=(float)(unsigned char)row_pointers[y][x]*(1.0/255.0);
281                                 surface_buffer[y][x]=Color(
282                                         gray,
283                                         gray,
284                                         gray,
285                                         1.0
286                                 );
287                         }
288                 break;
289
290         case PNG_COLOR_TYPE_GRAY_ALPHA:
291                 for(y=0;y<surface_buffer.get_h();y++)
292                         for(x=0;x<surface_buffer.get_w();x++)
293                         {
294                                 float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*2]);
295 //                              float gray=(float)(unsigned char)row_pointers[y][x*2]*(1.0/255.0);
296                                 surface_buffer[y][x]=Color(
297                                         gray,
298                                         gray,
299                                         gray,
300                                         (float)(unsigned char)row_pointers[y][x*2+1]*(1.0/255.0)
301                                 );
302                         }
303                 break;
304
305         case PNG_COLOR_TYPE_PALETTE:
306                 synfig::warning("png_mptr: Paletted PNGs aren't yet fully supported.");
307                 for(y=0;y<surface_buffer.get_h();y++)
308                         for(x=0;x<surface_buffer.get_w();x++)
309                         {
310                                 float r=gamma().r_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].red);
311                                 float g=gamma().g_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].green);
312                                 float b=gamma().b_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].blue);
313                                 surface_buffer[y][x]=Color(
314                                         r,
315                                         g,
316                                         b,
317                                         1.0
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         DEBUGPOINT();
329
330         png_read_end(png_ptr, end_info);
331         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
332         fclose(file);
333
334         delete [] row_pointers;
335         delete [] data;
336 }
337
338 png_mptr::~png_mptr()
339 {
340         DEBUGPOINT();
341 }
342
343 bool
344 png_mptr::get_frame(synfig::Surface &surface,Time, synfig::ProgressCallback */*cb*/)
345 {
346         surface.mirror(surface_buffer);
347 //      surface=surface_buffer;
348         return true;
349 }