1 /* === S Y N F I G ========================================================= */
2 /*! \file trgt_jpeg.cpp
3 ** \brief jpeg_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 ======================================================= */
34 #include "trgt_jpeg.h"
36 #include <ETL/stringf>
42 /* === M A C R O S ========================================================= */
44 using namespace synfig;
48 /* === G L O B A L S ======================================================= */
50 SYNFIG_TARGET_INIT(jpeg_trgt);
51 SYNFIG_TARGET_SET_NAME(jpeg_trgt,"jpeg");
52 SYNFIG_TARGET_SET_EXT(jpeg_trgt,"jpg");
53 SYNFIG_TARGET_SET_VERSION(jpeg_trgt,"0.1");
54 SYNFIG_TARGET_SET_CVS_ID(jpeg_trgt,"$Id: trgt_jpeg.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $");
56 /* === M E T H O D S ======================================================= */
58 jpeg_trgt::jpeg_trgt(const char *Filename)
69 jpeg_trgt::~jpeg_trgt()
73 jpeg_finish_compress(&cinfo);
74 jpeg_destroy_compress(&cinfo);
81 delete [] color_buffer;
85 jpeg_trgt::set_rend_desc(RendDesc *given_desc)
88 imagecount=desc.get_frame_start();
89 if(desc.get_frame_end()-desc.get_frame_start()>0)
97 jpeg_trgt::start_frame(synfig::ProgressCallback *callback)
99 int w=desc.get_w(),h=desc.get_h();
101 if(file && file!=stdout)
105 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
111 newfilename(filename),
112 ext(find(filename.begin(),filename.end(),'.'),filename.end());
113 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
115 newfilename+=etl::strprintf("%04d",imagecount)+ext;
116 file=fopen(newfilename.c_str(),"wb");
117 if(callback)callback->task(newfilename);
121 file=fopen(filename.c_str(),"wb");
122 if(callback)callback->task(filename);
129 buffer=new unsigned char[3*w];
131 delete [] color_buffer;
132 color_buffer=new Color[w];
135 cinfo.err = jpeg_std_error(&jerr);
136 jpeg_create_compress(&cinfo);
137 jpeg_stdio_dest(&cinfo, file);
139 cinfo.image_width = w; /* image width and height, in pixels */
140 cinfo.image_height = h;
141 cinfo.input_components = 3; /* # of color components per pixel */
142 cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
143 /* Now use the library's routine to set default compression parameters.
144 * (You must set at least cinfo.in_color_space before calling this,
145 * since the defaults depend on the source color space.)
147 jpeg_set_defaults(&cinfo);
148 /* Now you can set any non-default parameters you wish to.
149 * Here we just illustrate the use of quality (quantization table) scaling:
151 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
153 /* Step 4: Start compressor */
155 /* TRUE ensures that we will write a complete interchange-JPEG file.
156 * Pass TRUE unless you are very sure of what you're doing.
158 jpeg_start_compress(&cinfo, TRUE);
165 jpeg_trgt::end_frame()
169 jpeg_finish_compress(&cinfo);
170 jpeg_destroy_compress(&cinfo);
174 if(file && file!=stdout)
181 jpeg_trgt::start_scanline(int scanline)
187 jpeg_trgt::end_scanline()
192 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB,gamma());
193 JSAMPROW *row_pointer(&buffer);
194 jpeg_write_scanlines(&cinfo, row_pointer, 1);