91 m_buffer =
new JOCTET[m_buffer_size];
92 pub.next_output_byte = m_buffer;
93 pub.free_in_buffer = m_buffer_size;
109 m_output.write((
char*)m_buffer, m_buffer_size);
111 pub.next_output_byte = m_buffer;
112 pub.free_in_buffer = m_buffer_size;
120 m_output.write((
char*)m_buffer, m_buffer_size -
pub.free_in_buffer);
144const unsigned int claw::graphic::jpeg::writer::s_rgb_pixel_size = 3;
178 jpeg_compress_struct cinfo;
181 cinfo.err = jpeg_std_error(&jerr.
pub);
187 create_compress_info(cinfo, outfile);
191 set_options(cinfo, opt);
193 jpeg_destroy_compress(&cinfo);
197 jpeg_abort_compress(&cinfo);
198 jpeg_destroy_compress(&cinfo);
208void claw::graphic::jpeg::writer::set_options(jpeg_compress_struct& cinfo,
209 const options& opt)
const
211 cinfo.image_width = m_image.width();
212 cinfo.image_height = m_image.height();
213 cinfo.input_components = s_rgb_pixel_size;
214 cinfo.in_color_space = JCS_RGB;
216 jpeg_set_defaults(&cinfo);
218 if(opt.quality > 100)
219 jpeg_set_quality(&cinfo, 100, TRUE);
221 jpeg_set_quality(&cinfo, opt.quality, TRUE);
224 jpeg_simple_progression(&cinfo);
231void claw::graphic::jpeg::writer::save_image(jpeg_compress_struct& cinfo)
const
233 JSAMPLE* data =
new JSAMPLE[m_image.width() * s_rgb_pixel_size];
236 jpeg_error_mgr* jerr_saved = cinfo.err;
238 cinfo.err = jpeg_std_error(&jerr.pub);
241 if(setjmp(jerr.setjmp_buffer))
244 jpeg_abort_compress(&cinfo);
248 jpeg_start_compress(&cinfo, TRUE);
250 while(cinfo.next_scanline < cinfo.image_height)
252 copy_pixel_line(data, cinfo.next_scanline);
253 jpeg_write_scanlines(&cinfo, &data, 1);
257 jpeg_finish_compress(&cinfo);
259 cinfo.err = jerr_saved;
268void claw::graphic::jpeg::writer::copy_pixel_line(JSAMPLE* data,
269 unsigned int y)
const
275 for(
unsigned int x = 0; x != m_image.width(); ++x, data += s_rgb_pixel_size)
277 data[0] = m_image[y][x].components.red;
278 data[1] = m_image[y][x].components.green;
279 data[2] = m_image[y][x].components.blue;
288void claw::graphic::jpeg::writer::create_compress_info(
289 jpeg_compress_struct& cinfo, destination_manager& outfile)
const
291 jpeg_create_compress(&cinfo);
293 cinfo.dest = &outfile.pub;
294 cinfo.client_data = &outfile;
296 outfile.pub.init_destination
298 outfile.pub.empty_output_buffer
300 outfile.pub.term_destination
Some assert macros to strengthen you code.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
image()
Constructor. Creates an image without datas.
void save(std::ostream &f, const options &opt=options()) const
Save an image in a jpeg file.
writer(const image &img)
Constructor.
A simple class to use as exception with string message.
#define CLAW_EXCEPTION(m)
Create an exception and add the name of the current function to the message.
A class for jpeg pictures.
Methods for the claw::graphic::jpeg::error_manager class.
claw__graphic__jpeg__destination_manager__empty_output_buffer(j_compress_ptr cinfo)
Write the content of the buffer in the file.
claw__graphic__jpeg__destination_manager__term_destination(j_compress_ptr cinfo)
Write the last pending bytes in the file.
claw__graphic__jpeg__destination_manager__init_destination(j_compress_ptr cinfo)
Initialize the output stream.
void jpeg__error_manager__error_exit(j_common_ptr cinfo)
Throw an exception when an error occurs in an internal jpeg processing.
Error handler that throw an exception instead of exiting the program.
std::string error_string
A comprehensive description of the error.
struct jpeg_error_mgr pub
"public" fields, needed by the jpeg library.
jmp_buf setjmp_buffer
For return to caller.
Destination manager that allow us to write in a std::ostream.
destination_manager(std::ostream &os)
Constructor.
void term()
Write the last pending bytes in the file.
struct jpeg_destination_mgr pub
"public" fields, needed by the jpeg library.
~destination_manager()
Destructor.
void flush()
Write the content of the buffer in the file.
Parameters of the writing algorithm.
bool progressive
Tell if we save a progressive jpeg.
options()
Default constructor.
unsigned char quality
Quality level to use in the saved stream.