1 /*! ========================================================================
3 ** jpeg_trgt Target Module
4 ** $Id: trgt_jpeg.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $
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.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === H E A D E R S ======================================================= */
33 #include "trgt_jpeg.h"
35 #include <ETL/stringf>
41 /* === M A C R O S ========================================================= */
43 using namespace synfig;
47 /* === G L O B A L S ======================================================= */
49 SYNFIG_TARGET_INIT(jpeg_trgt);
50 SYNFIG_TARGET_SET_NAME(jpeg_trgt,"jpeg");
51 SYNFIG_TARGET_SET_EXT(jpeg_trgt,"jpg");
52 SYNFIG_TARGET_SET_VERSION(jpeg_trgt,"0.1");
53 SYNFIG_TARGET_SET_CVS_ID(jpeg_trgt,"$Id: trgt_jpeg.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $");
55 /* === M E T H O D S ======================================================= */
57 jpeg_trgt::jpeg_trgt(const char *Filename)
68 jpeg_trgt::~jpeg_trgt()
72 jpeg_finish_compress(&cinfo);
73 jpeg_destroy_compress(&cinfo);
80 delete [] color_buffer;
84 jpeg_trgt::set_rend_desc(RendDesc *given_desc)
87 imagecount=desc.get_frame_start();
88 if(desc.get_frame_end()-desc.get_frame_start()>0)
96 jpeg_trgt::start_frame(synfig::ProgressCallback *callback)
98 int w=desc.get_w(),h=desc.get_h();
100 if(file && file!=stdout)
104 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
110 newfilename(filename),
111 ext(find(filename.begin(),filename.end(),'.'),filename.end());
112 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
114 newfilename+=etl::strprintf("%04d",imagecount)+ext;
115 file=fopen(newfilename.c_str(),"wb");
116 if(callback)callback->task(newfilename);
120 file=fopen(filename.c_str(),"wb");
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);