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