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 ======================================================= */
39 #include <ETL/stringf>
47 /* === M A C R O S ========================================================= */
49 using namespace synfig;
53 /* === G L O B A L S ======================================================= */
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$");
61 /* === M E T H O D S ======================================================= */
64 png_trgt::png_out_error(png_struct *png_data,const char *msg)
66 png_trgt *me=(png_trgt*)png_data->error_ptr;
67 synfig::error(strprintf("png_trgt: error: %s",msg));
72 png_trgt::png_out_warning(png_struct *png_data,const char *msg)
74 png_trgt *me=(png_trgt*)png_data->error_ptr;
75 synfig::warning(strprintf("png_trgt: warning: %s",msg));
80 //Target *png_trgt::New(const char *filename){ return new png_trgt(filename);}
82 png_trgt::png_trgt(const char *Filename)
97 delete [] color_buffer;
101 png_trgt::set_rend_desc(RendDesc *given_desc)
103 //given_desc->set_pixel_format(PixelFormat((int)PF_RGB|(int)PF_A));
105 imagecount=desc.get_frame_start();
106 if(desc.get_frame_end()-desc.get_frame_start()>0)
114 png_trgt::end_frame()
118 png_write_end(png_ptr,info_ptr);
119 png_destroy_write_struct(&png_ptr, &info_ptr);
122 if(file && file!=stdout)
130 png_trgt::start_frame(synfig::ProgressCallback *callback)
132 int w=desc.get_w(),h=desc.get_h();
134 if(file && file!=stdout)
138 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
143 String newfilename(filename_sans_extension(filename) +
144 etl::strprintf(".%04d",imagecount) +
145 filename_extension(filename));
146 file=fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE);
147 if(callback)callback->task(newfilename);
151 file=fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE);
152 if(callback)callback->task(filename);
159 buffer=new unsigned char[4*w];
161 delete [] color_buffer;
162 color_buffer=new Color[w];
164 png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)this,png_out_error, png_out_warning);
167 synfig::error("Unable to setup PNG struct");
172 info_ptr= png_create_info_struct(png_ptr);
175 synfig::error("Unable to setup PNG info struct");
177 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
181 if (setjmp(png_jmpbuf(png_ptr)))
183 synfig::error("Unable to setup longjump");
184 png_destroy_write_struct(&png_ptr, &info_ptr);
188 png_init_io(png_ptr,file);
189 png_set_filter(png_ptr,0,PNG_FILTER_NONE);
191 setjmp(png_jmpbuf(png_ptr));
192 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 //png_set_gAMA(png_ptr, info_ptr,1.0/gamma().get_gamma());
196 png_set_gAMA(png_ptr, info_ptr,gamma().get_gamma());
198 // Write the physical size
199 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 char title [] = "Title";
202 char description[] = "Description";
203 char software [] = "Software";
204 char synfig [] = "SYNFIG";
205 // char copyright [] = "Copyright";
206 // char voria [] = "(c) 2004 Voria Studios, LLC";
208 // Output any text info along with the file
211 { PNG_TEXT_COMPRESSION_NONE, title, const_cast<char *>(get_canvas()->get_name().c_str()),
212 strlen(get_canvas()->get_name().c_str()) },
213 { PNG_TEXT_COMPRESSION_NONE, description, const_cast<char *>(get_canvas()->get_description().c_str()),
214 strlen(get_canvas()->get_description().c_str()) },
215 // { PNG_TEXT_COMPRESSION_NONE, copyright, voria, strlen(voria) },
216 { PNG_TEXT_COMPRESSION_NONE, software, synfig, strlen(synfig) },
218 png_set_text(png_ptr,info_ptr,comments,sizeof(comments)/sizeof(png_text));
220 png_write_info_before_PLTE(png_ptr, info_ptr);
221 png_write_info(png_ptr, info_ptr);
227 png_trgt::start_scanline(int /*scanline*/)
233 png_trgt::end_scanline()
238 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB|PF_A, gamma());
240 setjmp(png_jmpbuf(png_ptr));
241 png_write_row(png_ptr,buffer);