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 Robert B. Quattlebaum Jr.
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
17 ** === N O T E S ===========================================================
19 ** ========================================================================= */
21 /* === H E A D E R S ======================================================= */
32 #include "trgt_jpeg.h"
34 #include <ETL/stringf>
40 /* === M A C R O S ========================================================= */
42 using namespace sinfg;
46 /* === G L O B A L S ======================================================= */
48 SINFG_TARGET_INIT(jpeg_trgt);
49 SINFG_TARGET_SET_NAME(jpeg_trgt,"jpeg");
50 SINFG_TARGET_SET_EXT(jpeg_trgt,"jpg");
51 SINFG_TARGET_SET_VERSION(jpeg_trgt,"0.1");
52 SINFG_TARGET_SET_CVS_ID(jpeg_trgt,"$Id: trgt_jpeg.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $");
54 /* === M E T H O D S ======================================================= */
56 jpeg_trgt::jpeg_trgt(const char *Filename)
67 jpeg_trgt::~jpeg_trgt()
71 jpeg_finish_compress(&cinfo);
72 jpeg_destroy_compress(&cinfo);
79 delete [] color_buffer;
83 jpeg_trgt::set_rend_desc(RendDesc *given_desc)
86 imagecount=desc.get_frame_start();
87 if(desc.get_frame_end()-desc.get_frame_start()>0)
95 jpeg_trgt::start_frame(sinfg::ProgressCallback *callback)
97 int w=desc.get_w(),h=desc.get_h();
99 if(file && file!=stdout)
103 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
109 newfilename(filename),
110 ext(find(filename.begin(),filename.end(),'.'),filename.end());
111 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
113 newfilename+=etl::strprintf("%04d",imagecount)+ext;
114 file=fopen(newfilename.c_str(),"wb");
115 if(callback)callback->task(newfilename);
119 file=fopen(filename.c_str(),"wb");
120 if(callback)callback->task(filename);
127 buffer=new unsigned char[3*w];
129 delete [] color_buffer;
130 color_buffer=new Color[w];
133 cinfo.err = jpeg_std_error(&jerr);
134 jpeg_create_compress(&cinfo);
135 jpeg_stdio_dest(&cinfo, file);
137 cinfo.image_width = w; /* image width and height, in pixels */
138 cinfo.image_height = h;
139 cinfo.input_components = 3; /* # of color components per pixel */
140 cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
141 /* Now use the library's routine to set default compression parameters.
142 * (You must set at least cinfo.in_color_space before calling this,
143 * since the defaults depend on the source color space.)
145 jpeg_set_defaults(&cinfo);
146 /* Now you can set any non-default parameters you wish to.
147 * Here we just illustrate the use of quality (quantization table) scaling:
149 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
151 /* Step 4: Start compressor */
153 /* TRUE ensures that we will write a complete interchange-JPEG file.
154 * Pass TRUE unless you are very sure of what you're doing.
156 jpeg_start_compress(&cinfo, TRUE);
163 jpeg_trgt::end_frame()
167 jpeg_finish_compress(&cinfo);
168 jpeg_destroy_compress(&cinfo);
172 if(file && file!=stdout)
179 jpeg_trgt::start_scanline(int scanline)
185 jpeg_trgt::end_scanline()
190 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB,gamma());
191 JSAMPROW *row_pointer(&buffer);
192 jpeg_write_scanlines(&cinfo, row_pointer, 1);