Merge branch 'nikitakit_master'
[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 #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                                    const synfig::TargetParam& /* params */)
84 {
85         file=NULL;
86         filename=Filename;
87         buffer=NULL;
88         ready=false;
89         color_buffer=0;
90 }
91
92 png_trgt::~png_trgt()
93 {
94         if(file)
95                 fclose(file);
96         file=NULL;
97         delete [] buffer;
98         delete [] color_buffer;
99 }
100
101 bool
102 png_trgt::set_rend_desc(RendDesc *given_desc)
103 {
104         //given_desc->set_pixel_format(PixelFormat((int)PF_RGB|(int)PF_A));
105         desc=*given_desc;
106         imagecount=desc.get_frame_start();
107         if(desc.get_frame_end()-desc.get_frame_start()>0)
108                 multi_image=true;
109         else
110                 multi_image=false;
111         return true;
112 }
113
114 void
115 png_trgt::end_frame()
116 {
117         if(ready && file)
118         {
119                 png_write_end(png_ptr,info_ptr);
120                 png_destroy_write_struct(&png_ptr, &info_ptr);
121         }
122
123         if(file && file!=stdout)
124                 fclose(file);
125         file=NULL;
126         imagecount++;
127         ready=false;
128 }
129
130 bool
131 png_trgt::start_frame(synfig::ProgressCallback *callback)
132 {
133         int w=desc.get_w(),h=desc.get_h();
134
135         if(file && file!=stdout)
136                 fclose(file);
137         if(filename=="-")
138         {
139                 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
140                 file=stdout;
141         }
142         else if(multi_image)
143         {
144                 String newfilename(filename_sans_extension(filename) +
145                                                    etl::strprintf(".%04d",imagecount) +
146                                                    filename_extension(filename));
147                 file=fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE);
148                 if(callback)callback->task(newfilename);
149         }
150         else
151         {
152                 file=fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE);
153                 if(callback)callback->task(filename);
154         }
155
156         if(!file)
157                 return false;
158
159         delete [] buffer;
160         buffer=new unsigned char[4*w];
161
162         delete [] color_buffer;
163         color_buffer=new Color[w];
164
165         png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)this,png_out_error, png_out_warning);
166         if (!png_ptr)
167         {
168                 synfig::error("Unable to setup PNG struct");
169                 fclose(file);
170                 return false;
171         }
172
173         info_ptr= png_create_info_struct(png_ptr);
174         if (!info_ptr)
175         {
176                 synfig::error("Unable to setup PNG info struct");
177                 fclose(file);
178                 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
179                 return false;
180         }
181
182         if (setjmp(png_jmpbuf(png_ptr)))
183         {
184                 synfig::error("Unable to setup longjump");
185                 png_destroy_write_struct(&png_ptr, &info_ptr);
186                 fclose(file);
187                 return false;
188         }
189         png_init_io(png_ptr,file);
190         png_set_filter(png_ptr,0,PNG_FILTER_NONE);
191
192         setjmp(png_jmpbuf(png_ptr));
193         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);
194
195         // Write the gamma
196         //png_set_gAMA(png_ptr, info_ptr,1.0/gamma().get_gamma());
197         png_set_gAMA(png_ptr, info_ptr,gamma().get_gamma());
198
199         // Write the physical size
200         png_set_pHYs(png_ptr,info_ptr,round_to_int(desc.get_x_res()),round_to_int(desc.get_y_res()),PNG_RESOLUTION_METER);
201
202         char title      [] = "Title";
203         char description[] = "Description";
204         char software   [] = "Software";
205         char synfig     [] = "SYNFIG";
206 //      char copyright  [] = "Copyright";
207 //      char voria      [] = "(c) 2004 Voria Studios, LLC";
208
209         // Output any text info along with the file
210         png_text comments[]=
211         {
212                 { PNG_TEXT_COMPRESSION_NONE, title, const_cast<char *>(get_canvas()->get_name().c_str()),
213                   strlen(get_canvas()->get_name().c_str()) },
214                 { PNG_TEXT_COMPRESSION_NONE, description, const_cast<char *>(get_canvas()->get_description().c_str()),
215                   strlen(get_canvas()->get_description().c_str()) },
216 //              { PNG_TEXT_COMPRESSION_NONE, copyright, voria, strlen(voria) },
217                 { PNG_TEXT_COMPRESSION_NONE, software, synfig, strlen(synfig) },
218         };
219         png_set_text(png_ptr,info_ptr,comments,sizeof(comments)/sizeof(png_text));
220
221         png_write_info_before_PLTE(png_ptr, info_ptr);
222         png_write_info(png_ptr, info_ptr);
223         ready=true;
224         return true;
225 }
226
227 Color *
228 png_trgt::start_scanline(int /*scanline*/)
229 {
230         return color_buffer;
231 }
232
233 bool
234 png_trgt::end_scanline()
235 {
236         if(!file || !ready)
237                 return false;
238
239         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB|PF_A, gamma());
240
241         setjmp(png_jmpbuf(png_ptr));
242         png_write_row(png_ptr,buffer);
243
244         return true;
245 }