Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc2 / src / modules / mod_png / trgt_png.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_png.cpp
3 **      \brief png_trgt 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 /* === H E A D E R S ======================================================= */
27
28 #define SYNFIG_TARGET
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 "trgt_png.h"
38 #include <png.h>
39 #include <ETL/stringf>
40 #include <cstdio>
41 #include <algorithm>
42 #include <functional>
43 #include <ETL/misc>
44
45 #endif
46
47 /* === M A C R O S ========================================================= */
48
49 using namespace synfig;
50 using namespace std;
51 using namespace etl;
52
53 /* === G L O B A L S ======================================================= */
54
55 SYNFIG_TARGET_INIT(png_trgt);
56 SYNFIG_TARGET_SET_NAME(png_trgt,"png");
57 SYNFIG_TARGET_SET_EXT(png_trgt,"png");
58 SYNFIG_TARGET_SET_VERSION(png_trgt,"0.1");
59 SYNFIG_TARGET_SET_CVS_ID(png_trgt,"$Id$");
60
61 /* === M E T H O D S ======================================================= */
62
63 void
64 png_trgt::png_out_error(png_struct *png_data,const char *msg)
65 {
66         png_trgt *me=(png_trgt*)png_data->error_ptr;
67         synfig::error(strprintf("png_trgt: error: %s",msg));
68         me->ready=false;
69 }
70
71 void
72 png_trgt::png_out_warning(png_struct *png_data,const char *msg)
73 {
74         png_trgt *me=(png_trgt*)png_data->error_ptr;
75         synfig::warning(strprintf("png_trgt: warning: %s",msg));
76         me->ready=false;
77 }
78
79
80 //Target *png_trgt::New(const char *filename){  return new png_trgt(filename);}
81
82 png_trgt::png_trgt(const char *Filename)
83 {
84         file=NULL;
85         filename=Filename;
86         buffer=NULL;
87         ready=false;
88         color_buffer=0;
89 }
90
91 png_trgt::~png_trgt()
92 {
93         if(file)
94                 fclose(file);
95         file=NULL;
96         delete [] buffer;
97         delete [] color_buffer;
98 }
99
100 bool
101 png_trgt::set_rend_desc(RendDesc *given_desc)
102 {
103         //given_desc->set_pixel_format(PixelFormat((int)PF_RGB|(int)PF_A));
104         desc=*given_desc;
105         imagecount=desc.get_frame_start();
106         if(desc.get_frame_end()-desc.get_frame_start()>0)
107                 multi_image=true;
108         else
109                 multi_image=false;
110         return true;
111 }
112
113 void
114 png_trgt::end_frame()
115 {
116         if(ready && file)
117         {
118                 png_write_end(png_ptr,info_ptr);
119                 png_destroy_write_struct(&png_ptr, &info_ptr);
120         }
121
122         if(file && file!=stdout)
123                 fclose(file);
124         file=NULL;
125         imagecount++;
126         ready=false;
127 }
128
129 bool
130 png_trgt::start_frame(synfig::ProgressCallback *callback)
131 {
132         int w=desc.get_w(),h=desc.get_h();
133
134         if(file && file!=stdout)
135                 fclose(file);
136         if(filename=="-")
137         {
138                 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
139                 file=stdout;
140         }
141         else if(multi_image)
142         {
143                 String
144                         newfilename(filename),
145                         ext(find(filename.begin(),filename.end(),'.'),filename.end());
146                 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
147
148                 newfilename+=etl::strprintf("%04d",imagecount)+ext;
149                 file=fopen(newfilename.c_str(),"wb");
150                 if(callback)callback->task(newfilename);
151         }
152         else
153         {
154                 file=fopen(filename.c_str(),"wb");
155                 if(callback)callback->task(filename);
156         }
157
158         if(!file)
159                 return false;
160
161         delete [] buffer;
162         buffer=new unsigned char[4*w];
163
164         delete [] color_buffer;
165         color_buffer=new Color[w];
166
167         png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)this,png_out_error, png_out_warning);
168         if (!png_ptr)
169         {
170                 synfig::error("Unable to setup PNG struct");
171                 fclose(file);
172                 return false;
173         }
174
175         info_ptr= png_create_info_struct(png_ptr);
176         if (!info_ptr)
177         {
178                 synfig::error("Unable to setup PNG info struct");
179                 fclose(file);
180                 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
181                 return false;
182         }
183
184         if (setjmp(png_jmpbuf(png_ptr)))
185         {
186                 synfig::error("Unable to setup longjump");
187                 png_destroy_write_struct(&png_ptr, &info_ptr);
188                 fclose(file);
189                 return false;
190         }
191         png_init_io(png_ptr,file);
192         png_set_filter(png_ptr,0,PNG_FILTER_NONE);
193
194         setjmp(png_jmpbuf(png_ptr));
195         png_set_IHDR(png_ptr,info_ptr,w,h,8,PNG_COLOR_TYPE_RGBA,PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
196
197         // Write the gamma
198         //png_set_gAMA(png_ptr, info_ptr,1.0/gamma().get_gamma());
199         png_set_gAMA(png_ptr, info_ptr,gamma().get_gamma());
200
201         // Write the physical size
202         png_set_pHYs(png_ptr,info_ptr,round_to_int(desc.get_x_res()),round_to_int(desc.get_y_res()),PNG_RESOLUTION_METER);
203
204         // Output any text info along with the file
205         png_text comments[]=
206         {
207                 { PNG_TEXT_COMPRESSION_NONE, "Title", const_cast<char *>(get_canvas()->get_name().c_str()),
208                   strlen(get_canvas()->get_name().c_str()) },
209                 { PNG_TEXT_COMPRESSION_NONE, "Description", const_cast<char *>(get_canvas()->get_description().c_str()),
210                   strlen(get_canvas()->get_description().c_str()) },
211 //              { PNG_TEXT_COMPRESSION_NONE, "Copyright", "(c) 2004 Voria Studios, LLC",
212 //                strlen("(c) 2004 Voria Studios, LLC") },
213                 { PNG_TEXT_COMPRESSION_NONE, "Software", "SYNFIG",
214                   strlen("SYNFIG") },
215         };
216         png_set_text(png_ptr,info_ptr,comments,sizeof(comments)/sizeof(png_text));
217
218         png_write_info_before_PLTE(png_ptr, info_ptr);
219         png_write_info(png_ptr, info_ptr);
220         ready=true;
221         return true;
222 }
223
224 Color *
225 png_trgt::start_scanline(int /*scanline*/)
226 {
227         return color_buffer;
228 }
229
230 bool
231 png_trgt::end_scanline()
232 {
233         if(!file || !ready)
234                 return false;
235
236         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB|PF_A, gamma());
237
238         setjmp(png_jmpbuf(png_ptr));
239         png_write_row(png_ptr,buffer);
240
241         return true;
242 }