Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / modules / mod_png / trgt_png.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** png_trgt Target Module
4 ** $Id: trgt_png.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
5 **
6 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
7 **
8 **      This package is free software; you can redistribute it and/or
9 **      modify it under the terms of the GNU General Public License as
10 **      published by the Free Software Foundation; either version 2 of
11 **      the License, or (at your option) any later version.
12 **
13 **      This package is distributed in the hope that it will be useful,
14 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **      General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #define SYNFIG_TARGET
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "trgt_png.h"
34 #include <png.h>
35 #include <ETL/stringf>
36 #include <cstdio>
37 #include <algorithm>
38 #include <functional>
39 #include <ETL/misc>
40
41 #endif
42
43 /* === M A C R O S ========================================================= */
44
45 using namespace synfig;
46 using namespace std;
47 using namespace etl;
48
49 /* === G L O B A L S ======================================================= */
50
51 SYNFIG_TARGET_INIT(png_trgt);
52 SYNFIG_TARGET_SET_NAME(png_trgt,"png");
53 SYNFIG_TARGET_SET_EXT(png_trgt,"png");
54 SYNFIG_TARGET_SET_VERSION(png_trgt,"0.1");
55 SYNFIG_TARGET_SET_CVS_ID(png_trgt,"$Id: trgt_png.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
56
57 /* === M E T H O D S ======================================================= */
58
59 void
60 png_trgt::png_out_error(png_struct *png_data,const char *msg)
61 {
62         png_trgt *me=(png_trgt*)png_data->error_ptr;
63         synfig::error(strprintf("png_trgt: error: %s",msg));
64         me->ready=false;
65 }
66
67 void
68 png_trgt::png_out_warning(png_struct *png_data,const char *msg)
69 {
70         png_trgt *me=(png_trgt*)png_data->error_ptr;
71         synfig::warning(strprintf("png_trgt: warning: %s",msg));
72         me->ready=false;
73 }
74
75
76 //Target *png_trgt::New(const char *filename){  return new png_trgt(filename);}
77
78 png_trgt::png_trgt(const char *Filename)
79 {
80         file=NULL;
81         filename=Filename;
82         buffer=NULL;
83         ready=false;
84         color_buffer=0; 
85 }
86
87 png_trgt::~png_trgt()
88 {
89         if(file)
90                 fclose(file);
91         file=NULL;
92         delete [] buffer;
93         delete [] color_buffer;
94 }
95
96 bool
97 png_trgt::set_rend_desc(RendDesc *given_desc)
98 {
99         //given_desc->set_pixel_format(PixelFormat((int)PF_RGB|(int)PF_A));
100         desc=*given_desc;
101         imagecount=desc.get_frame_start();
102         if(desc.get_frame_end()-desc.get_frame_start()>0)
103                 multi_image=true;
104         else
105                 multi_image=false;
106         return true;
107 }
108
109 void
110 png_trgt::end_frame()
111 {
112         if(ready && file)
113         {
114                 png_write_end(png_ptr,info_ptr);
115                 png_destroy_write_struct(&png_ptr, &info_ptr);
116         }
117
118         if(file && file!=stdout)
119                 fclose(file);
120         file=NULL;
121         imagecount++;
122         ready=false;
123 }
124
125 bool
126 png_trgt::start_frame(synfig::ProgressCallback *callback)
127 {
128         int w=desc.get_w(),h=desc.get_h();
129         
130         if(file && file!=stdout)
131                 fclose(file);
132         if(filename=="-")
133         {
134                 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
135                 file=stdout;
136         }
137         else if(multi_image)
138         {
139                 String
140                         newfilename(filename),
141                         ext(find(filename.begin(),filename.end(),'.'),filename.end());
142                 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
143                 
144                 newfilename+=etl::strprintf("%04d",imagecount)+ext;
145                 file=fopen(newfilename.c_str(),"wb");
146                 if(callback)callback->task(newfilename);
147         }
148         else
149         {
150                 file=fopen(filename.c_str(),"wb");
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         // Output any text info along with the file
201         png_text comments[]=
202         {
203                 { PNG_TEXT_COMPRESSION_NONE, "Title", const_cast<char *>(get_canvas()->get_name().c_str()) },
204                 { PNG_TEXT_COMPRESSION_NONE, "Description", const_cast<char *>(get_canvas()->get_description().c_str()) },
205                 { PNG_TEXT_COMPRESSION_NONE, "Copyright", "(c) 2004 Voria Studios, LLC" },
206                 { PNG_TEXT_COMPRESSION_NONE, "Software", "SYNFIG" },
207         };
208         png_set_text(png_ptr,info_ptr,comments,sizeof(comments)/sizeof(png_text));
209         
210         png_write_info_before_PLTE(png_ptr, info_ptr);
211         png_write_info(png_ptr, info_ptr);
212         ready=true;
213         return true;
214 }
215
216 Color *
217 png_trgt::start_scanline(int scanline)
218 {
219         return color_buffer;
220 }
221
222 bool
223 png_trgt::end_scanline()
224 {
225         if(!file || !ready)
226                 return false;
227
228         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB|PF_A, gamma());
229
230         setjmp(png_jmpbuf(png_ptr));
231         png_write_row(png_ptr,buffer);
232
233         return true;
234 }