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());
113 newfilename(filename),
114 ext(find(filename.begin(),filename.end(),'.'),filename.end());
115 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
117 newfilename+=etl::strprintf("%04d",imagecount)+ext;
118 file=fopen(newfilename.c_str(),"wb");
119 if(callback)callback->task(newfilename);
123 file=fopen(filename.c_str(),"wb");
124 if(callback)callback->task(filename);
131 buffer=new unsigned char[3*w];
133 delete [] color_buffer;
134 color_buffer=new Color[w];
137 cinfo.err = jpeg_std_error(&jerr);
138 jpeg_create_compress(&cinfo);
139 jpeg_stdio_dest(&cinfo, file);
141 cinfo.image_width = w; /* image width and height, in pixels */
142 cinfo.image_height = h;
143 cinfo.input_components = 3; /* # of color components per pixel */
144 cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
145 /* Now use the library's routine to set default compression parameters.
146 * (You must set at least cinfo.in_color_space before calling this,
147 * since the defaults depend on the source color space.)
149 jpeg_set_defaults(&cinfo);
150 /* Now you can set any non-default parameters you wish to.
151 * Here we just illustrate the use of quality (quantization table) scaling:
153 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
155 /* Step 4: Start compressor */
157 /* TRUE ensures that we will write a complete interchange-JPEG file.
158 * Pass TRUE unless you are very sure of what you're doing.
160 jpeg_start_compress(&cinfo, TRUE);
167 jpeg_trgt::end_frame()
171 jpeg_finish_compress(&cinfo);
172 jpeg_destroy_compress(&cinfo);
176 if(file && file!=stdout)
183 jpeg_trgt::start_scanline(int scanline)
189 jpeg_trgt::end_scanline()
194 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB,gamma());
195 JSAMPROW *row_pointer(&buffer);
196 jpeg_write_scanlines(&cinfo, row_pointer, 1);