Release synfig_0_61_06
[synfig.git] / synfig-core / tags / synfig_0_61_06 / 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 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #define SYNFIG_TARGET
28
29 #ifdef USING_PCH
30 #       include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 #       include <config.h>
34 #endif
35
36 #include "trgt_png.h"
37 #include <png.h>
38 #include <ETL/stringf>
39 #include <cstdio>
40 #include <algorithm>
41 #include <functional>
42 #include <ETL/misc>
43
44 #endif
45
46 /* === M A C R O S ========================================================= */
47
48 using namespace synfig;
49 using namespace std;
50 using namespace etl;
51
52 /* === G L O B A L S ======================================================= */
53
54 SYNFIG_TARGET_INIT(png_trgt);
55 SYNFIG_TARGET_SET_NAME(png_trgt,"png");
56 SYNFIG_TARGET_SET_EXT(png_trgt,"png");
57 SYNFIG_TARGET_SET_VERSION(png_trgt,"0.1");
58 SYNFIG_TARGET_SET_CVS_ID(png_trgt,"$Id$");
59
60 /* === M E T H O D S ======================================================= */
61
62 void
63 png_trgt::png_out_error(png_struct *png_data,const char *msg)
64 {
65         png_trgt *me=(png_trgt*)png_data->error_ptr;
66         synfig::error(strprintf("png_trgt: error: %s",msg));
67         me->ready=false;
68 }
69
70 void
71 png_trgt::png_out_warning(png_struct *png_data,const char *msg)
72 {
73         png_trgt *me=(png_trgt*)png_data->error_ptr;
74         synfig::warning(strprintf("png_trgt: warning: %s",msg));
75         me->ready=false;
76 }
77
78
79 //Target *png_trgt::New(const char *filename){  return new png_trgt(filename);}
80
81 png_trgt::png_trgt(const char *Filename)
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
143                         newfilename(filename),
144                         ext(find(filename.begin(),filename.end(),'.'),filename.end());
145                 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
146
147                 newfilename+=etl::strprintf("%04d",imagecount)+ext;
148                 file=fopen(newfilename.c_str(),"wb");
149                 if(callback)callback->task(newfilename);
150         }
151         else
152         {
153                 file=fopen(filename.c_str(),"wb");
154                 if(callback)callback->task(filename);
155         }
156
157         if(!file)
158                 return false;
159
160         delete [] buffer;
161         buffer=new unsigned char[4*w];
162
163         delete [] color_buffer;
164         color_buffer=new Color[w];
165
166         png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)this,png_out_error, png_out_warning);
167         if (!png_ptr)
168         {
169                 synfig::error("Unable to setup PNG struct");
170                 fclose(file);
171                 return false;
172         }
173
174         info_ptr= png_create_info_struct(png_ptr);
175         if (!info_ptr)
176         {
177                 synfig::error("Unable to setup PNG info struct");
178                 fclose(file);
179                 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
180                 return false;
181         }
182
183         if (setjmp(png_jmpbuf(png_ptr)))
184         {
185                 synfig::error("Unable to setup longjump");
186                 png_destroy_write_struct(&png_ptr, &info_ptr);
187                 fclose(file);
188                 return false;
189         }
190         png_init_io(png_ptr,file);
191         png_set_filter(png_ptr,0,PNG_FILTER_NONE);
192
193         setjmp(png_jmpbuf(png_ptr));
194         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);
195
196         // Write the gamma
197         //png_set_gAMA(png_ptr, info_ptr,1.0/gamma().get_gamma());
198         png_set_gAMA(png_ptr, info_ptr,gamma().get_gamma());
199
200         // Write the physical size
201         png_set_pHYs(png_ptr,info_ptr,round_to_int(desc.get_x_res()),round_to_int(desc.get_y_res()),PNG_RESOLUTION_METER);
202
203         // Output any text info along with the file
204         png_text comments[]=
205         {
206                 { PNG_TEXT_COMPRESSION_NONE, "Title", const_cast<char *>(get_canvas()->get_name().c_str()),
207                   strlen(get_canvas()->get_name().c_str()) },
208                 { PNG_TEXT_COMPRESSION_NONE, "Description", const_cast<char *>(get_canvas()->get_description().c_str()),
209                   strlen(get_canvas()->get_description().c_str()) },
210 //              { PNG_TEXT_COMPRESSION_NONE, "Copyright", "(c) 2004 Voria Studios, LLC",
211 //                strlen("(c) 2004 Voria Studios, LLC") },
212                 { PNG_TEXT_COMPRESSION_NONE, "Software", "SYNFIG",
213                   strlen("SYNFIG") },
214         };
215         png_set_text(png_ptr,info_ptr,comments,sizeof(comments)/sizeof(png_text));
216
217         png_write_info_before_PLTE(png_ptr, info_ptr);
218         png_write_info(png_ptr, info_ptr);
219         ready=true;
220         return true;
221 }
222
223 Color *
224 png_trgt::start_scanline(int scanline)
225 {
226         return color_buffer;
227 }
228
229 bool
230 png_trgt::end_scanline()
231 {
232         if(!file || !ready)
233                 return false;
234
235         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB|PF_A, gamma());
236
237         setjmp(png_jmpbuf(png_ptr));
238         png_write_row(png_ptr,buffer);
239
240         return true;
241 }