1 /* === S Y N F I G ========================================================= */
2 /*! \file trgt_jpeg.cpp
3 ** \brief jpeg_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 ======================================================= */
36 #include "trgt_jpeg.h"
38 #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(jpeg_trgt);
53 SYNFIG_TARGET_SET_NAME(jpeg_trgt,"jpeg");
54 SYNFIG_TARGET_SET_EXT(jpeg_trgt,"jpg");
55 SYNFIG_TARGET_SET_VERSION(jpeg_trgt,"0.1");
56 SYNFIG_TARGET_SET_CVS_ID(jpeg_trgt,"$Id$");
58 /* === M E T H O D S ======================================================= */
60 jpeg_trgt::jpeg_trgt(const char *Filename)
71 jpeg_trgt::~jpeg_trgt()
75 jpeg_finish_compress(&cinfo);
76 jpeg_destroy_compress(&cinfo);
83 delete [] color_buffer;
87 jpeg_trgt::set_rend_desc(RendDesc *given_desc)
90 imagecount=desc.get_frame_start();
91 if(desc.get_frame_end()-desc.get_frame_start()>0)
99 jpeg_trgt::start_frame(synfig::ProgressCallback *callback)
101 int w=desc.get_w(),h=desc.get_h();
103 if(file && file!=stdout)
107 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
112 String newfilename(filename_sans_extension(filename) +
113 etl::strprintf(".%04d",imagecount) +
114 filename_extension(filename));
115 file=fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE);
116 if(callback)callback->task(newfilename);
120 file=fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE);
121 if(callback)callback->task(filename);
128 buffer=new unsigned char[3*w];
130 delete [] color_buffer;
131 color_buffer=new Color[w];
134 cinfo.err = jpeg_std_error(&jerr);
135 jpeg_create_compress(&cinfo);
136 jpeg_stdio_dest(&cinfo, file);
138 cinfo.image_width = w; /* image width and height, in pixels */
139 cinfo.image_height = h;
140 cinfo.input_components = 3; /* # of color components per pixel */
141 cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
142 /* Now use the library's routine to set default compression parameters.
143 * (You must set at least cinfo.in_color_space before calling this,
144 * since the defaults depend on the source color space.)
146 jpeg_set_defaults(&cinfo);
147 /* Now you can set any non-default parameters you wish to.
148 * Here we just illustrate the use of quality (quantization table) scaling:
150 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
152 /* Step 4: Start compressor */
154 /* TRUE ensures that we will write a complete interchange-JPEG file.
155 * Pass TRUE unless you are very sure of what you're doing.
157 jpeg_start_compress(&cinfo, TRUE);
164 jpeg_trgt::end_frame()
168 jpeg_finish_compress(&cinfo);
169 jpeg_destroy_compress(&cinfo);
173 if(file && file!=stdout)
180 jpeg_trgt::start_scanline(int /*scanline*/)
186 jpeg_trgt::end_scanline()
191 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB,gamma());
192 JSAMPROW *row_pointer(&buffer);
193 jpeg_write_scanlines(&cinfo, row_pointer, 1);