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