claw::graphic::jpeg::writer Class Reference

This class write an image in a jpeg file. More...

#include <jpeg.hpp>

List of all members.

Classes

struct  destination_manager
 Destination manager that allow us to write in a std::ostream. More...
struct  options
 Parameters of the writing algorithm. More...

Public Member Functions

 writer (const image &img)
 Constructor.
 writer (const image &img, std::ostream &f, const options &opt=options())
 Constructor. Save an image in a jpeg file.
void save (std::ostream &f, const options &opt=options()) const
 Save an image in a jpeg file.

Private Member Functions

void set_options (jpeg_compress_struct &cinfo, const options &opt) const
 Set the parameters of the JPEG saving structures.
void save_image (jpeg_compress_struct &cinfo) const
 Save the image in the configured stream.
void copy_pixel_line (JSAMPLE *data, unsigned int y) const
 Copy the pixels from the image to an array of bytes.
void create_compress_info (jpeg_compress_struct &cinfo, destination_manager &outfile) const
 Initialize the jpeg compression structure.

Private Attributes

const imagem_image
 The image from which we thake the data to save.

Static Private Attributes

static const unsigned int s_rgb_pixel_size = 3
 Size, in bytes, of a red/green/blue pixel in a jpeg file.

Detailed Description

This class write an image in a jpeg file.

Author:
Julien Jorge

Definition at line 167 of file jpeg.hpp.


Constructor & Destructor Documentation

claw::graphic::jpeg::writer::writer ( const image img  ) 

Constructor.

Parameters:
img The image in which the data will be stored.

Definition at line 168 of file jpeg_writer.cpp.

00169   : m_image( img )
00170 {
00171 
00172 } // jpeg::writer::writer()

claw::graphic::jpeg::writer::writer ( const image img,
std::ostream &  f,
const options opt = options() 
)

Constructor. Save an image in a jpeg file.

Parameters:
img The image from which we read the data.
f The file in which we write the data.
opt Options about the saved file.

Definition at line 182 of file jpeg_writer.cpp.

References claw::graphic::jpeg::save().

00183   : m_image( img )
00184 {
00185   save(f, opt);
00186 } // jpeg::writer::writer()


Member Function Documentation

void claw::graphic::jpeg::writer::copy_pixel_line ( JSAMPLE *  data,
unsigned int  y 
) const [private]

Copy the pixels from the image to an array of bytes.

Parameters:
data (out) The pixels for the JPEG image.
y Index of the line of the image from which we read the pixels.
Precondition:
The memory pointed by data is long enough to store the pixels.

Definition at line 295 of file jpeg_writer.cpp.

References CLAW_PRECOND.

Referenced by save_image().

00296 {
00297   CLAW_PRECOND( data );
00298   CLAW_PRECOND( y < m_image.height() );
00299 
00300   // three bytes for each pixel in the line
00301   for (unsigned int x=0; x!=m_image.width(); ++x, data+=s_rgb_pixel_size)
00302     {
00303       data[0] = m_image[y][x].components.red;
00304       data[1] = m_image[y][x].components.green;
00305       data[2] = m_image[y][x].components.blue;
00306     }
00307 } // jpeg::writer::copy_pixel_line()

void claw::graphic::jpeg::writer::create_compress_info ( jpeg_compress_struct &  cinfo,
destination_manager outfile 
) const [private]

Initialize the jpeg compression structure.

Parameters:
cinfo The structure to initialize.
outfile The destination manager.

Definition at line 316 of file jpeg_writer.cpp.

References claw__graphic__jpeg__destination_manager__empty_output_buffer(), claw__graphic__jpeg__destination_manager__init_destination(), claw__graphic__jpeg__destination_manager__term_destination(), and claw::graphic::jpeg::writer::destination_manager::pub.

Referenced by save().

00317 {
00318   jpeg_create_compress(&cinfo);
00319 
00320   cinfo.dest = &outfile.pub;
00321   cinfo.client_data = &outfile;
00322 
00323   outfile.pub.init_destination =
00324     claw__graphic__jpeg__destination_manager__init_destination;
00325   outfile.pub.empty_output_buffer =
00326     claw__graphic__jpeg__destination_manager__empty_output_buffer;
00327   outfile.pub.term_destination =
00328     claw__graphic__jpeg__destination_manager__term_destination;
00329 } // jpeg::writer::create_compress_info()

void claw::graphic::jpeg::writer::save ( std::ostream &  f,
const options opt = options() 
) const

Save an image in a jpeg file.

Parameters:
f Jpeg file.
opt Options about the saved file.

Definition at line 195 of file jpeg_writer.cpp.

References CLAW_EXCEPTION, CLAW_PRECOND, create_compress_info(), claw::graphic::jpeg::error_manager::error_string, claw::graphic::jpeg__error_manager__error_exit(), claw::graphic::jpeg::error_manager::pub, save_image(), set_options(), and claw::graphic::jpeg::error_manager::setjmp_buffer.

00196 {
00197   CLAW_PRECOND( !!f );
00198 
00199   destination_manager outfile(f);
00200   jpeg_compress_struct cinfo;
00201   error_manager jerr;
00202 
00203   cinfo.err = jpeg_std_error(&jerr.pub);
00204   jerr.pub.error_exit = jpeg__error_manager__error_exit;
00205   
00206   if ( setjmp(jerr.setjmp_buffer) )
00207     throw CLAW_EXCEPTION(jerr.error_string);
00208   
00209   create_compress_info( cinfo, outfile );
00210   
00211   try
00212     {
00213       set_options( cinfo, opt );
00214       save_image( cinfo );
00215       jpeg_destroy_compress(&cinfo);
00216     }
00217   catch(...)
00218     {
00219       jpeg_abort_compress(&cinfo);
00220       jpeg_destroy_compress(&cinfo);
00221       throw;
00222     }
00223 } // jpeg::writer::save()

void claw::graphic::jpeg::writer::save_image ( jpeg_compress_struct &  cinfo  )  const [private]

Save the image in the configured stream.

Parameters:
cinfo The structure to initialize.

Definition at line 256 of file jpeg_writer.cpp.

References CLAW_EXCEPTION, copy_pixel_line(), claw::graphic::jpeg::error_manager::error_string, claw::graphic::jpeg__error_manager__error_exit(), m_image, claw::graphic::jpeg::error_manager::pub, s_rgb_pixel_size, claw::graphic::jpeg::error_manager::setjmp_buffer, and claw::graphic::image::width().

Referenced by save().

00257 {
00258   JSAMPLE* data = new JSAMPLE[ m_image.width() * s_rgb_pixel_size ];
00259 
00260   error_manager jerr;
00261   jpeg_error_mgr* jerr_saved = cinfo.err;
00262 
00263   cinfo.err = jpeg_std_error(&jerr.pub);
00264   jerr.pub.error_exit = jpeg__error_manager__error_exit;
00265 
00266   if ( setjmp(jerr.setjmp_buffer) )
00267     {
00268       delete[] data;
00269       jpeg_abort_compress(&cinfo);
00270       throw CLAW_EXCEPTION(jerr.error_string);
00271     }
00272 
00273   jpeg_start_compress( &cinfo, TRUE );
00274 
00275   while (cinfo.next_scanline < cinfo.image_height)
00276     {
00277       copy_pixel_line( data, cinfo.next_scanline );
00278       jpeg_write_scanlines( &cinfo, &data, 1 );
00279     }
00280 
00281   delete[] data;
00282   jpeg_finish_compress(&cinfo);
00283 
00284   cinfo.err = jerr_saved;
00285 } // jpeg::writer::load()

void claw::graphic::jpeg::writer::set_options ( jpeg_compress_struct &  cinfo,
const options opt 
) const [private]

Set the parameters of the JPEG saving structures.

Parameters:
cinfo JPEG file description.
opt Options about the saved file.

Definition at line 232 of file jpeg_writer.cpp.

References claw::graphic::jpeg::writer::options::progressive, and claw::graphic::jpeg::writer::options::quality.

Referenced by save().

00233 {
00234   cinfo.image_width = m_image.width();       /* image width, in pixels */
00235   cinfo.image_height = m_image.height();     /* image height, in pixels */
00236   cinfo.input_components = s_rgb_pixel_size; /* # of components per pixel */
00237   cinfo.in_color_space = JCS_RGB;            /* colorspace of input image */
00238 
00239   jpeg_set_defaults(&cinfo);
00240 
00241   if (opt.quality > 100)
00242     jpeg_set_quality(&cinfo, 100, TRUE);
00243   else
00244     jpeg_set_quality(&cinfo, opt.quality, TRUE);
00245 
00246   if (opt.progressive)
00247     jpeg_simple_progression(&cinfo);
00248 } // jpeg::writer::set_options()


Member Data Documentation

The image from which we thake the data to save.

Definition at line 239 of file jpeg.hpp.

Referenced by save_image().

const unsigned int claw::graphic::jpeg::writer::s_rgb_pixel_size = 3 [static, private]

Size, in bytes, of a red/green/blue pixel in a jpeg file.

Definition at line 243 of file jpeg.hpp.

Referenced by save_image().


The documentation for this class was generated from the following files:

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1