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