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:
img The image in which the data will be stored.

Definition at line 136 of file png_writer.cpp.

00137   : m_image( img )
00138 {
00139 
00140 } // png::writer::writer()

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

Constructor.

Parameters:
img The image to save.
f The file in which we write the data.
opt Saving options.

Definition at line 150 of file png_writer.cpp.

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

00151   : m_image( img )
00152 {
00153   save(f, opt);
00154 } // 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.
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 268 of file png_writer.cpp.

References CLAW_PRECOND.

00269 {
00270   CLAW_PRECOND( data );
00271   CLAW_PRECOND( y < m_image.height() );
00272 
00273   // four bytes for each pixel in the line
00274   for (unsigned int x=0; x!=m_image.width(); ++x, data+=s_rgba_pixel_size)
00275     {
00276       data[0] = m_image[y][x].components.red;
00277       data[1] = m_image[y][x].components.green;
00278       data[2] = m_image[y][x].components.blue;
00279       data[3] = m_image[y][x].components.alpha;
00280     }
00281 } // 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_ptr PNG file description.
info_ptr PNG file informations.

Definition at line 290 of file png_writer.cpp.

References CLAW_EXCEPTION.

Referenced by save().

00291 {
00292   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00293 
00294   if (png_ptr)
00295     {
00296       info_ptr = png_create_info_struct(png_ptr);
00297 
00298       if (!info_ptr)
00299         png_destroy_write_struct(&png_ptr, png_infopp_NULL);
00300     }
00301 
00302   if (!png_ptr || !info_ptr)
00303     throw CLAW_EXCEPTION("Can't create PNG write structures.");
00304 } // 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:
f PNG file.
opt Saving 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, CLAW_PRECOND, create_write_structures(), save_image(), and set_options().

00164 {
00165   CLAW_PRECOND( !!f );
00166 
00167   target_manager outfile(f);
00168   png_structp png_ptr;
00169   png_infop info_ptr;
00170 
00171   create_write_structures(png_ptr, info_ptr);
00172 
00173   if (setjmp(png_jmpbuf(png_ptr)))
00174     {
00175       /* If we get here, we had a problem reading the file */
00176       /* Free all of the memory associated with the png_ptr and info_ptr */
00177       png_destroy_write_struct(&png_ptr, &info_ptr);
00178       throw CLAW_EXCEPTION("Invalid PNG file.");
00179     }
00180       
00181   png_set_write_fn( png_ptr, (void *)&outfile,
00182                     claw__graphic__png__target_manager__write,
00183                     claw__graphic__png__target_manager__flush );
00184 
00185   set_options( png_ptr, info_ptr, opt );
00186   save_image( png_ptr, info_ptr );
00187       
00188   png_destroy_write_struct(&png_ptr, &info_ptr);
00189 } // 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_ptr PNG file description.
info_ptr PNG file informations.

Definition at line 220 of file png_writer.cpp.

References CLAW_PRECOND.

Referenced by save().

00221 {
00222   CLAW_PRECOND( png_ptr );
00223   CLAW_PRECOND( info_ptr );
00224 
00225   const unsigned int row_length = s_rgba_pixel_size * m_image.width();
00226   png_bytepp data =
00227     (png_bytepp)png_malloc( png_ptr, sizeof(png_bytep) * m_image.height() );
00228   unsigned int i=0;
00229 
00230   try
00231     {
00232       for (i=0; i!=m_image.height(); ++i)
00233         {
00234           data[i] = (png_bytep)png_malloc( png_ptr, row_length );
00235 
00236           if (!data[i])
00237             throw std::bad_alloc();
00238 
00239           copy_pixel_line( data[i], i );
00240         }
00241 
00242       png_set_rows(png_ptr, info_ptr, data);
00243       png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
00244     }
00245   catch(...)
00246     {
00247       for(unsigned int j=0; j!=i; ++j)
00248         png_free(png_ptr, data[j]);
00249 
00250       png_free(png_ptr, data);
00251       throw;
00252     }
00253 
00254   for(i=0; i!=m_image.height(); ++i)
00255     png_free(png_ptr, data[i]);
00256 
00257   png_free(png_ptr, data);
00258 } // 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_ptr PNG file description.
info_ptr PNG file informations.
opt Compression 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.

Referenced by save().

00200 {
00201   CLAW_PRECOND( png_ptr );
00202   CLAW_PRECOND( info_ptr );
00203 
00204   png_set_compression_level( png_ptr, opt.compression );
00205 
00206   png_set_IHDR( png_ptr, info_ptr, m_image.width(), m_image.height(),
00207                 sizeof(pixel_type::component_type) * 8, /* 8 bits per byte */
00208                 PNG_COLOR_TYPE_RGB_ALPHA,
00209                 opt.interlace, PNG_COMPRESSION_TYPE_DEFAULT,
00210                 PNG_FILTER_TYPE_DEFAULT );
00211 } // 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:

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