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