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
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 ======================================================= */
37 #include "trgt_jpeg.h"
39 #include <ETL/stringf>
45 /* === M A C R O S ========================================================= */
47 using namespace synfig;
51 /* === G L O B A L S ======================================================= */
53 SYNFIG_TARGET_INIT(jpeg_trgt);
54 SYNFIG_TARGET_SET_NAME(jpeg_trgt,"jpeg");
55 SYNFIG_TARGET_SET_EXT(jpeg_trgt,"jpg");
56 SYNFIG_TARGET_SET_VERSION(jpeg_trgt,"0.1");
57 SYNFIG_TARGET_SET_CVS_ID(jpeg_trgt,"$Id$");
59 /* === M E T H O D S ======================================================= */
61 jpeg_trgt::jpeg_trgt(const char *Filename)
72 jpeg_trgt::~jpeg_trgt()
76 jpeg_finish_compress(&cinfo);
77 jpeg_destroy_compress(&cinfo);
84 delete [] color_buffer;
88 jpeg_trgt::set_rend_desc(RendDesc *given_desc)
91 imagecount=desc.get_frame_start();
92 if(desc.get_frame_end()-desc.get_frame_start()>0)
100 jpeg_trgt::start_frame(synfig::ProgressCallback *callback)
102 int w=desc.get_w(),h=desc.get_h();
104 if(file && file!=stdout)
108 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
113 String newfilename(filename_sans_extension(filename) +
114 etl::strprintf(".%04d",imagecount) +
115 filename_extension(filename));
116 file=fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE);
117 if(callback)callback->task(newfilename);
121 file=fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE);
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);