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

claw::graphic::png::writer Class Reference

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

#include <png.hpp>

List of all members.

Classes

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

Public Member Functions

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

Private Member Functions

void set_options (png_structp png_ptr, png_infop info_ptr, const options &opt) const
 Set the parameters of the PNG saving structures.
void save_image (png_structp png_ptr, png_infop info_ptr) const
 Save the image in a configured PNG file.
void copy_pixel_line (png_bytep data, unsigned int y) const
 Copy the pixels from the image to an array of bytes.
void create_write_structures (png_structp &png_ptr, png_infop &info_ptr) const
 Initialize the png write structures.

Private Attributes

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

Static Private Attributes

static const unsigned int s_rgba_pixel_size = 4
 Size, in bytes, of a red/green/blue/alpha pixel in a png file.

Detailed Description

This class write an image in a png file.

Author:
Julien Jorge

Definition at line 113 of file png.hpp.


Constructor & Destructor Documentation

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

Constructor.

Parameters:
imgThe image in which the data will be stored.

Definition at line 136 of file png_writer.cpp.

  : m_image( img )
{

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

Constructor.

Parameters:
imgThe image to save.
fThe file in which we write the data.
optSaving options.

Definition at line 150 of file png_writer.cpp.

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

  : m_image( img )
{
  save(f, opt);
} // png::writer::writer()

Member Function Documentation

void claw::graphic::png::writer::copy_pixel_line ( png_bytep  data,
unsigned int  y 
) const [private]

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

Parameters:
data(out) The pixels for the PNG image.
yIndex 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 268 of file png_writer.cpp.

References CLAW_PRECOND.

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

  // four bytes for each pixel in the line
  for (unsigned int x=0; x!=m_image.width(); ++x, data+=s_rgba_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;
      data[3] = m_image[y][x].components.alpha;
    }
} // png::writer::copy_pixel_line()
void claw::graphic::png::writer::create_write_structures ( png_structp &  png_ptr,
png_infop &  info_ptr 
) const [private]

Initialize the png write structures.

Parameters:
png_ptrPNG file description.
info_ptrPNG file informations.

Definition at line 290 of file png_writer.cpp.

References CLAW_EXCEPTION.

{
  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

  if (png_ptr)
    {
      info_ptr = png_create_info_struct(png_ptr);

      if (!info_ptr)
        png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
    }

  if (!png_ptr || !info_ptr)
    throw CLAW_EXCEPTION("Can't create PNG write structures.");
} // png::writer::create_write_structures()
void claw::graphic::png::writer::save ( std::ostream &  f,
const options opt = options() 
) const

Save the image in a PNG file.

Parameters:
fPNG file.
optSaving options.

Definition at line 163 of file png_writer.cpp.

References claw__graphic__png__target_manager__flush(), claw__graphic__png__target_manager__write(), CLAW_EXCEPTION, and CLAW_PRECOND.

{
  CLAW_PRECOND( !!f );

  target_manager outfile(f);
  png_structp png_ptr;
  png_infop info_ptr;

  create_write_structures(png_ptr, info_ptr);

  if (setjmp(png_jmpbuf(png_ptr)))
    {
      /* If we get here, we had a problem reading the file */
      /* Free all of the memory associated with the png_ptr and info_ptr */
      png_destroy_write_struct(&png_ptr, &info_ptr);
      throw CLAW_EXCEPTION("Invalid PNG file.");
    }
      
  png_set_write_fn( png_ptr, (void *)&outfile,
                    claw__graphic__png__target_manager__write,
                    claw__graphic__png__target_manager__flush );

  set_options( png_ptr, info_ptr, opt );
  save_image( png_ptr, info_ptr );
      
  png_destroy_write_struct(&png_ptr, &info_ptr);
} // png::writer::save()
void claw::graphic::png::writer::save_image ( png_structp  png_ptr,
png_infop  info_ptr 
) const [private]

Save the image in a configured PNG file.

Parameters:
png_ptrPNG file description.
info_ptrPNG file informations.

Definition at line 220 of file png_writer.cpp.

References CLAW_PRECOND.

{
  CLAW_PRECOND( png_ptr );
  CLAW_PRECOND( info_ptr );

  const unsigned int row_length = s_rgba_pixel_size * m_image.width();
  png_bytepp data =
    (png_bytepp)png_malloc( png_ptr, sizeof(png_bytep) * m_image.height() );
  unsigned int i=0;

  try
    {
      for (i=0; i!=m_image.height(); ++i)
        {
          data[i] = (png_bytep)png_malloc( png_ptr, row_length );

          if (!data[i])
            throw std::bad_alloc();

          copy_pixel_line( data[i], i );
        }

      png_set_rows(png_ptr, info_ptr, data);
      png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
    }
  catch(...)
    {
      for(unsigned int j=0; j!=i; ++j)
        png_free(png_ptr, data[j]);

      png_free(png_ptr, data);
      throw;
    }

  for(i=0; i!=m_image.height(); ++i)
    png_free(png_ptr, data[i]);

  png_free(png_ptr, data);
} // png::writer::save_image()
void claw::graphic::png::writer::set_options ( png_structp  png_ptr,
png_infop  info_ptr,
const options opt 
) const [private]

Set the parameters of the PNG saving structures.

Parameters:
png_ptrPNG file description.
info_ptrPNG file informations.
optCompression options.

Definition at line 199 of file png_writer.cpp.

References CLAW_PRECOND, claw::graphic::png::writer::options::compression, and claw::graphic::png::writer::options::interlace.

{
  CLAW_PRECOND( png_ptr );
  CLAW_PRECOND( info_ptr );

  png_set_compression_level( png_ptr, opt.compression );

  png_set_IHDR( png_ptr, info_ptr, m_image.width(), m_image.height(),
                sizeof(pixel_type::component_type) * 8, /* 8 bits per byte */
                PNG_COLOR_TYPE_RGB_ALPHA,
                opt.interlace, PNG_COMPRESSION_TYPE_DEFAULT,
                PNG_FILTER_TYPE_DEFAULT );
} // png::writer::set_options()

Member Data Documentation

The image from which we thake the data to save.

Definition at line 197 of file png.hpp.

const unsigned int claw::graphic::png::writer::s_rgba_pixel_size = 4 [static, private]

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

Definition at line 201 of file png.hpp.


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