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());
144 newfilename(filename),
145 ext(find(filename.begin(),filename.end(),'.'),filename.end());
146 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
148 newfilename+=etl::strprintf("%04d",imagecount)+ext;
149 file=fopen(newfilename.c_str(),"wb");
150 if(callback)callback->task(newfilename);
154 file=fopen(filename.c_str(),"wb");
155 if(callback)callback->task(filename);
162 buffer=new unsigned char[4*w];
164 delete [] color_buffer;
165 color_buffer=new Color[w];
167 png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)this,png_out_error, png_out_warning);
170 synfig::error("Unable to setup PNG struct");
175 info_ptr= png_create_info_struct(png_ptr);
178 synfig::error("Unable to setup PNG info struct");
180 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
184 if (setjmp(png_jmpbuf(png_ptr)))
186 synfig::error("Unable to setup longjump");
187 png_destroy_write_struct(&png_ptr, &info_ptr);
191 png_init_io(png_ptr,file);
192 png_set_filter(png_ptr,0,PNG_FILTER_NONE);
194 setjmp(png_jmpbuf(png_ptr));
195 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);
198 //png_set_gAMA(png_ptr, info_ptr,1.0/gamma().get_gamma());
199 png_set_gAMA(png_ptr, info_ptr,gamma().get_gamma());
201 // Write the physical size
202 png_set_pHYs(png_ptr,info_ptr,round_to_int(desc.get_x_res()),round_to_int(desc.get_y_res()),PNG_RESOLUTION_METER);
204 // Output any text info along with the file
207 { PNG_TEXT_COMPRESSION_NONE, "Title", const_cast<char *>(get_canvas()->get_name().c_str()),
208 strlen(get_canvas()->get_name().c_str()) },
209 { PNG_TEXT_COMPRESSION_NONE, "Description", const_cast<char *>(get_canvas()->get_description().c_str()),
210 strlen(get_canvas()->get_description().c_str()) },
211 // { PNG_TEXT_COMPRESSION_NONE, "Copyright", "(c) 2004 Voria Studios, LLC",
212 // strlen("(c) 2004 Voria Studios, LLC") },
213 { PNG_TEXT_COMPRESSION_NONE, "Software", "SYNFIG",
216 png_set_text(png_ptr,info_ptr,comments,sizeof(comments)/sizeof(png_text));
218 png_write_info_before_PLTE(png_ptr, info_ptr);
219 png_write_info(png_ptr, info_ptr);
225 png_trgt::start_scanline(int /*scanline*/)
231 png_trgt::end_scanline()
236 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB|PF_A, gamma());
238 setjmp(png_jmpbuf(png_ptr));
239 png_write_row(png_ptr,buffer);