1 /* === S Y N F I G ========================================================= */
3 ** \brief png_trgt Target Module
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
37 #include <ETL/stringf>
45 /* === M A C R O S ========================================================= */
47 using namespace synfig;
51 /* === G L O B A L S ======================================================= */
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$");
59 /* === M E T H O D S ======================================================= */
62 png_trgt::png_out_error(png_struct *png_data,const char *msg)
64 png_trgt *me=(png_trgt*)png_data->error_ptr;
65 synfig::error(strprintf("png_trgt: error: %s",msg));
70 png_trgt::png_out_warning(png_struct *png_data,const char *msg)
72 png_trgt *me=(png_trgt*)png_data->error_ptr;
73 synfig::warning(strprintf("png_trgt: warning: %s",msg));
78 //Target *png_trgt::New(const char *filename){ return new png_trgt(filename);}
80 png_trgt::png_trgt(const char *Filename,
81 const synfig::TargetParam& /* params */)
96 delete [] color_buffer;
100 png_trgt::set_rend_desc(RendDesc *given_desc)
102 //given_desc->set_pixel_format(PixelFormat((int)PF_RGB|(int)PF_A));
104 imagecount=desc.get_frame_start();
105 if(desc.get_frame_end()-desc.get_frame_start()>0)
113 png_trgt::end_frame()
117 png_write_end(png_ptr,info_ptr);
118 png_destroy_write_struct(&png_ptr, &info_ptr);
121 if(file && file!=stdout)
129 png_trgt::start_frame(synfig::ProgressCallback *callback)
131 int w=desc.get_w(),h=desc.get_h();
133 if(file && file!=stdout)
137 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
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);
150 file=fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE);
151 if(callback)callback->task(filename);
158 buffer=new unsigned char[4*w];
160 delete [] color_buffer;
161 color_buffer=new Color[w];
163 png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)this,png_out_error, png_out_warning);
166 synfig::error("Unable to setup PNG struct");
171 info_ptr= png_create_info_struct(png_ptr);
174 synfig::error("Unable to setup PNG info struct");
176 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
180 if (setjmp(png_jmpbuf(png_ptr)))
182 synfig::error("Unable to setup longjump");
183 png_destroy_write_struct(&png_ptr, &info_ptr);
187 png_init_io(png_ptr,file);
188 png_set_filter(png_ptr,0,PNG_FILTER_NONE);
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);
194 //png_set_gAMA(png_ptr, info_ptr,1.0/gamma().get_gamma());
195 png_set_gAMA(png_ptr, info_ptr,gamma().get_gamma());
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);
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";
207 // Output any text info along with the file
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) },
217 png_set_text(png_ptr,info_ptr,comments,sizeof(comments)/sizeof(png_text));
219 png_write_info_before_PLTE(png_ptr, info_ptr);
220 png_write_info(png_ptr, info_ptr);
226 png_trgt::start_scanline(int /*scanline*/)
232 png_trgt::end_scanline()
237 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB|PF_A, gamma());
239 setjmp(png_jmpbuf(png_ptr));
240 png_write_row(png_ptr,buffer);