1 /* === S Y N F I G ========================================================= */
3 ** \brief png_trgt Target Module
6 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
19 ** === N O T E S ===========================================================
21 ** ========================================================================= */
23 /* === H E A D E R S ======================================================= */
36 #include <ETL/stringf>
44 /* === M A C R O S ========================================================= */
46 using namespace synfig;
50 /* === G L O B A L S ======================================================= */
52 SYNFIG_TARGET_INIT(png_trgt);
53 SYNFIG_TARGET_SET_NAME(png_trgt,"png");
54 SYNFIG_TARGET_SET_EXT(png_trgt,"png");
55 SYNFIG_TARGET_SET_VERSION(png_trgt,"0.1");
56 SYNFIG_TARGET_SET_CVS_ID(png_trgt,"$Id: trgt_png.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
58 /* === M E T H O D S ======================================================= */
61 png_trgt::png_out_error(png_struct *png_data,const char *msg)
63 png_trgt *me=(png_trgt*)png_data->error_ptr;
64 synfig::error(strprintf("png_trgt: error: %s",msg));
69 png_trgt::png_out_warning(png_struct *png_data,const char *msg)
71 png_trgt *me=(png_trgt*)png_data->error_ptr;
72 synfig::warning(strprintf("png_trgt: warning: %s",msg));
77 //Target *png_trgt::New(const char *filename){ return new png_trgt(filename);}
79 png_trgt::png_trgt(const char *Filename)
94 delete [] color_buffer;
98 png_trgt::set_rend_desc(RendDesc *given_desc)
100 //given_desc->set_pixel_format(PixelFormat((int)PF_RGB|(int)PF_A));
102 imagecount=desc.get_frame_start();
103 if(desc.get_frame_end()-desc.get_frame_start()>0)
111 png_trgt::end_frame()
115 png_write_end(png_ptr,info_ptr);
116 png_destroy_write_struct(&png_ptr, &info_ptr);
119 if(file && file!=stdout)
127 png_trgt::start_frame(synfig::ProgressCallback *callback)
129 int w=desc.get_w(),h=desc.get_h();
131 if(file && file!=stdout)
135 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
141 newfilename(filename),
142 ext(find(filename.begin(),filename.end(),'.'),filename.end());
143 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
145 newfilename+=etl::strprintf("%04d",imagecount)+ext;
146 file=fopen(newfilename.c_str(),"wb");
147 if(callback)callback->task(newfilename);
151 file=fopen(filename.c_str(),"wb");
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 // Output any text info along with the file
204 { PNG_TEXT_COMPRESSION_NONE, "Title", const_cast<char *>(get_canvas()->get_name().c_str()) },
205 { PNG_TEXT_COMPRESSION_NONE, "Description", const_cast<char *>(get_canvas()->get_description().c_str()) },
206 // { PNG_TEXT_COMPRESSION_NONE, "Copyright", "(c) 2004 Voria Studios, LLC" },
207 { PNG_TEXT_COMPRESSION_NONE, "Software", "SYNFIG" },
209 png_set_text(png_ptr,info_ptr,comments,sizeof(comments)/sizeof(png_text));
211 png_write_info_before_PLTE(png_ptr, info_ptr);
212 png_write_info(png_ptr, info_ptr);
218 png_trgt::start_scanline(int scanline)
224 png_trgt::end_scanline()
229 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB|PF_A, gamma());
231 setjmp(png_jmpbuf(png_ptr));
232 png_write_row(png_ptr,buffer);