Classes | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes

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.

  : m_image( img )
{

} // 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().

  : m_image( img )
{
  save(f, opt);
} // 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().

{
  CLAW_PRECOND( data );
  CLAW_PRECOND( y < m_image.height() );

  // three bytes for each pixel in the line
  for (unsigned int x=0; x!=m_image.width(); ++x, data+=s_rgb_pixel_size)
    {
      data[0] = m_image[y][x].components.red;
      data[1] = m_image[y][x].components.green;
      data[2] = m_image[y][x].components.blue;
    }
} // 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::writer::destination_manager::pub.

Referenced by save().

{
  jpeg_create_compress(&cinfo);

  cinfo.dest = &outfile.pub;
  cinfo.client_data = &outfile;

  outfile.pub.init_destination =
    claw__graphic__jpeg__destination_manager__init_destination;
  outfile.pub.empty_output_buffer =
    claw__graphic__jpeg__destination_manager__empty_output_buffer;
  outfile.pub.term_destination =
    claw__graphic__jpeg__destination_manager__term_destination;
} // 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::pub, save_image(), set_options(), and claw::graphic::jpeg::error_manager::setjmp_buffer.

{
  CLAW_PRECOND( !!f );

  destination_manager outfile(f);
  jpeg_compress_struct cinfo;
  error_manager jerr;

  cinfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = jpeg__error_manager__error_exit;
  
  if ( setjmp(jerr.setjmp_buffer) )
    throw CLAW_EXCEPTION(jerr.error_string);
  
  create_compress_info( cinfo, outfile );
  
  try
    {
      set_options( cinfo, opt );
      save_image( cinfo );
      jpeg_destroy_compress(&cinfo);
    }
  catch(...)
    {
      jpeg_abort_compress(&cinfo);
      jpeg_destroy_compress(&cinfo);
      throw;
    }
} // 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, 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().

{
  JSAMPLE* data = new JSAMPLE[ m_image.width() * s_rgb_pixel_size ];

  error_manager jerr;
  jpeg_error_mgr* jerr_saved = cinfo.err;

  cinfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = jpeg__error_manager__error_exit;

  if ( setjmp(jerr.setjmp_buffer) )
    {
      delete[] data;
      jpeg_abort_compress(&cinfo);
      throw CLAW_EXCEPTION(jerr.error_string);
    }

  jpeg_start_compress( &cinfo, TRUE );

  while (cinfo.next_scanline < cinfo.image_height)
    {
      copy_pixel_line( data, cinfo.next_scanline );
      jpeg_write_scanlines( &cinfo, &data, 1 );
    }

  delete[] data;
  jpeg_finish_compress(&cinfo);

  cinfo.err = jerr_saved;
} // 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().

{
  cinfo.image_width = m_image.width();       /* image width, in pixels */
  cinfo.image_height = m_image.height();     /* image height, in pixels */
  cinfo.input_components = s_rgb_pixel_size; /* # of components per pixel */
  cinfo.in_color_space = JCS_RGB;            /* colorspace of input image */

  jpeg_set_defaults(&cinfo);

  if (opt.quality > 100)
    jpeg_set_quality(&cinfo, 100, TRUE);
  else
    jpeg_set_quality(&cinfo, opt.quality, TRUE);

  if (opt.progressive)
    jpeg_simple_progression(&cinfo);
} // 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: