Removed a bunch more DEBUGPOINT()s.
[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                 for(y=0;y<surface_buffer.get_h();y++)
227                         for(x=0;x<surface_buffer.get_w();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<surface_buffer.get_h();y++)
251                         for(x=0;x<surface_buffer.get_w();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<surface_buffer.get_h();y++)
275                         for(x=0;x<surface_buffer.get_w();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<surface_buffer.get_h();y++)
290                         for(x=0;x<surface_buffer.get_w();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                 synfig::warning("png_mptr: Paletted PNGs aren't yet fully supported.");
305                 for(y=0;y<surface_buffer.get_h();y++)
306                         for(x=0;x<surface_buffer.get_w();x++)
307                         {
308                                 float r=gamma().r_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].red);
309                                 float g=gamma().g_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].green);
310                                 float b=gamma().b_U8_to_F32((unsigned char)png_ptr->palette[row_pointers[y][x]].blue);
311                                 surface_buffer[y][x]=Color(
312                                         r,
313                                         g,
314                                         b,
315                                         1.0
316                                 );
317                         }
318                 break;
319         default:
320                 synfig::error("png_mptr: error: Unsupported color type");
321         //! \todo THROW SOMETHING
322                 throw String("error on importer construction, *WRITEME*6");
323                 return;
324         }
325
326         png_read_end(png_ptr, end_info);
327         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
328         fclose(file);
329
330         delete [] row_pointers;
331         delete [] data;
332 }
333
334 png_mptr::~png_mptr()
335 {
336 }
337
338 bool
339 png_mptr::get_frame(synfig::Surface &surface,Time, synfig::ProgressCallback */*cb*/)
340 {
341         surface.mirror(surface_buffer);
342 //      surface=surface_buffer;
343         return true;
344 }