54 (j_compress_ptr cinfo)
92 : m_output(os), m_buffer_size(1024)
94 m_buffer =
new JOCTET[m_buffer_size];
95 pub.next_output_byte = m_buffer;
96 pub.free_in_buffer = m_buffer_size;
114 m_output.write((
char*)m_buffer, m_buffer_size);
116 pub.next_output_byte = m_buffer;
117 pub.free_in_buffer = m_buffer_size;
127 m_output.write((
char*)m_buffer, m_buffer_size - pub.free_in_buffer);
138 : quality(75), progressive(false)
151 (
unsigned char quality_,
bool progressive_ )
152 : quality(quality_), progressive(progressive_)
161 const unsigned int claw::graphic::jpeg::writer::s_rgb_pixel_size = 3;
200 jpeg_compress_struct cinfo;
203 cinfo.err = jpeg_std_error(&jerr.
pub);
209 create_compress_info( cinfo, outfile );
213 set_options( cinfo, opt );
215 jpeg_destroy_compress(&cinfo);
219 jpeg_abort_compress(&cinfo);
220 jpeg_destroy_compress(&cinfo);
231 void claw::graphic::jpeg::writer::set_options
232 ( jpeg_compress_struct& cinfo,
const options& opt )
const
234 cinfo.image_width = m_image.width();
235 cinfo.image_height = m_image.height();
236 cinfo.input_components = s_rgb_pixel_size;
237 cinfo.in_color_space = JCS_RGB;
239 jpeg_set_defaults(&cinfo);
241 if (opt.quality > 100)
242 jpeg_set_quality(&cinfo, 100, TRUE);
244 jpeg_set_quality(&cinfo, opt.quality, TRUE);
247 jpeg_simple_progression(&cinfo);
256 claw::graphic::jpeg::writer::save_image( jpeg_compress_struct& cinfo )
const
258 JSAMPLE* data =
new JSAMPLE[ m_image.width() * s_rgb_pixel_size ];
261 jpeg_error_mgr* jerr_saved = cinfo.err;
263 cinfo.err = jpeg_std_error(&jerr.pub);
266 if ( setjmp(jerr.setjmp_buffer) )
269 jpeg_abort_compress(&cinfo);
273 jpeg_start_compress( &cinfo, TRUE );
275 while (cinfo.next_scanline < cinfo.image_height)
277 copy_pixel_line( data, cinfo.next_scanline );
278 jpeg_write_scanlines( &cinfo, &data, 1 );
282 jpeg_finish_compress(&cinfo);
284 cinfo.err = jerr_saved;
294 void claw::graphic::jpeg::writer::copy_pixel_line
295 ( JSAMPLE* data,
unsigned int y )
const
301 for (
unsigned int x=0; x!=m_image.width(); ++x, data+=s_rgb_pixel_size)
303 data[0] = m_image[y][x].components.red;
304 data[1] = m_image[y][x].components.green;
305 data[2] = m_image[y][x].components.blue;
315 void claw::graphic::jpeg::writer::create_compress_info
316 ( jpeg_compress_struct& cinfo, destination_manager& outfile )
const
318 jpeg_create_compress(&cinfo);
320 cinfo.dest = &outfile.pub;
321 cinfo.client_data = &outfile;
323 outfile.pub.init_destination =
325 outfile.pub.empty_output_buffer =
327 outfile.pub.term_destination =
void term()
Write the last pending bytes in the file.
A class for jpeg pictures.
void save(std::ostream &os, const writer::options &opt=writer::options()) const
Save the image.
#define CLAW_EXCEPTION(m)
Create an exception and add the name of the current function to the message.
struct jpeg_error_mgr pub
"public" fields, needed by the jpeg library.
~destination_manager()
Destructor.
void flush()
Write the content of the buffer in the file.
std::string error_string
A comprehensive description of the error.
Destination manager that allow us to write in a std::ostream.
writer(const image &img)
Constructor.
claw__graphic__jpeg__destination_manager__term_destination(j_compress_ptr cinfo)
Write the last pending bytes in the file.
void jpeg__error_manager__error_exit(j_common_ptr cinfo)
Throw an exception when an error occurs in an internal jpeg processing.
jmp_buf setjmp_buffer
For return to caller.
options()
Default constructor.
claw__graphic__jpeg__destination_manager__init_destination(j_compress_ptr cinfo)
Initialize the output stream.
destination_manager(std::ostream &os)
Constructor.
Error handler that throw an exception instead of exiting the program.
Parameters of the writing algorithm.
A simple class to use as exception with string message.
claw__graphic__jpeg__destination_manager__empty_output_buffer(j_compress_ptr cinfo)
Write the content of the buffer in the file.
Some assert macros to strengthen you code.
void save(std::ostream &f, const options &opt=options()) const
Save an image in a jpeg file.
A class to deal with images.
Methods for the claw::graphic::jpeg::error_manager class.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.