1 /* === S Y N F I G ========================================================= */
3 ** \brief png_trgt Target Module
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
38 #include <ETL/stringf>
46 /* === M A C R O S ========================================================= */
48 using namespace synfig;
52 /* === G L O B A L S ======================================================= */
54 SYNFIG_TARGET_INIT(png_trgt);
55 SYNFIG_TARGET_SET_NAME(png_trgt,"png");
56 SYNFIG_TARGET_SET_EXT(png_trgt,"png");
57 SYNFIG_TARGET_SET_VERSION(png_trgt,"0.1");
58 SYNFIG_TARGET_SET_CVS_ID(png_trgt,"$Id$");
60 /* === M E T H O D S ======================================================= */
63 png_trgt::png_out_error(png_struct *png_data,const char *msg)
65 png_trgt *me=(png_trgt*)png_data->error_ptr;
66 synfig::error(strprintf("png_trgt: error: %s",msg));
71 png_trgt::png_out_warning(png_struct *png_data,const char *msg)
73 png_trgt *me=(png_trgt*)png_data->error_ptr;
74 synfig::warning(strprintf("png_trgt: warning: %s",msg));
79 //Target *png_trgt::New(const char *filename){ return new png_trgt(filename);}
81 png_trgt::png_trgt(const char *Filename)
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());
143 newfilename(filename),
144 ext(find(filename.begin(),filename.end(),'.'),filename.end());
145 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
147 newfilename+=etl::strprintf("%04d",imagecount)+ext;
148 file=fopen(newfilename.c_str(),"wb");
149 if(callback)callback->task(newfilename);
153 file=fopen(filename.c_str(),"wb");
154 if(callback)callback->task(filename);
161 buffer=new unsigned char[4*w];
163 delete [] color_buffer;
164 color_buffer=new Color[w];
166 png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)this,png_out_error, png_out_warning);
169 synfig::error("Unable to setup PNG struct");
174 info_ptr= png_create_info_struct(png_ptr);
177 synfig::error("Unable to setup PNG info struct");
179 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
183 if (setjmp(png_jmpbuf(png_ptr)))
185 synfig::error("Unable to setup longjump");
186 png_destroy_write_struct(&png_ptr, &info_ptr);
190 png_init_io(png_ptr,file);
191 png_set_filter(png_ptr,0,PNG_FILTER_NONE);
193 setjmp(png_jmpbuf(png_ptr));
194 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);
197 //png_set_gAMA(png_ptr, info_ptr,1.0/gamma().get_gamma());
198 png_set_gAMA(png_ptr, info_ptr,gamma().get_gamma());
200 // Write the physical size
201 png_set_pHYs(png_ptr,info_ptr,round_to_int(desc.get_x_res()),round_to_int(desc.get_y_res()),PNG_RESOLUTION_METER);
203 // Output any text info along with the file
206 { PNG_TEXT_COMPRESSION_NONE, "Title", const_cast<char *>(get_canvas()->get_name().c_str()) },
207 { PNG_TEXT_COMPRESSION_NONE, "Description", const_cast<char *>(get_canvas()->get_description().c_str()) },
208 // { PNG_TEXT_COMPRESSION_NONE, "Copyright", "(c) 2004 Voria Studios, LLC" },
209 { PNG_TEXT_COMPRESSION_NONE, "Software", "SYNFIG" },
211 png_set_text(png_ptr,info_ptr,comments,sizeof(comments)/sizeof(png_text));
213 png_write_info_before_PLTE(png_ptr, info_ptr);
214 png_write_info(png_ptr, info_ptr);
220 png_trgt::start_scanline(int scanline)
226 png_trgt::end_scanline()
231 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB|PF_A, gamma());
233 setjmp(png_jmpbuf(png_ptr));
234 png_write_row(png_ptr,buffer);