claw::graphic::png::reader Class Reference

This class read data from a png file and store it in an image. More...

#include <png.hpp>

List of all members.

Classes

struct  source_manager
 Source manager that allow us to read from a std::istream. More...

Public Member Functions

 reader (image &img)
 Constructor.
 reader (image &img, std::istream &f)
 Constructor.
void load (std::istream &f)
 Load an image from a png file.

Private Member Functions

void read_from_file (std::istream &f)
 Load an image from a png file.
void check_if_png (png_structp png_ptr, std::istream &f) const
 Check that the stream contains a PNG file.
void read_image (png_structp png_ptr, png_infop info_ptr)
 Read the image data of the PNG.
void read_sequential_image (png_structp png_ptr, png_infop info_ptr)
 Read the image data of a non interlaced PNG.
void read_interlaced_image (png_structp png_ptr, png_infop info_ptr, unsigned int passes)
 Read the image data of an interlaced PNG.
void copy_pixel_line (png_bytep data, unsigned int y)
 Copy the pixels taken from the PNG to the image.
void create_read_structures (png_structp &png_ptr, png_infop &info_ptr) const
 Initialize the png read structures.

Private Attributes

imagem_image
 The image in which we store the data we read.

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 read data from a png file and store it in an image.

Author:
Julien Jorge

Definition at line 55 of file png.hpp.


Constructor & Destructor Documentation

claw::graphic::png::reader::reader ( image img  ) 

Constructor.

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

Definition at line 90 of file png_reader.cpp.

00091   : m_image( img )
00092 {
00093 
00094 } // png::reader::reader()

claw::graphic::png::reader::reader ( image img,
std::istream &  f 
)

Constructor.

Parameters:
img The image in which the data will be stored.
f The file from which we read the data.
Postcondition:
img contains the data from f.

Definition at line 103 of file png_reader.cpp.

References load().

00104   : m_image( img )
00105 {
00106   load(f);
00107 } // png::reader::reader()


Member Function Documentation

void claw::graphic::png::reader::check_if_png ( png_structp  png_ptr,
std::istream &  f 
) const [private]

Check that the stream contains a PNG file.

Parameters:
png_ptr PNG file description.
f The stream to read from.

Definition at line 183 of file png_reader.cpp.

References CLAW_EXCEPTION, and CLAW_PRECOND.

Referenced by read_from_file().

00184 {
00185   CLAW_PRECOND( !!f );
00186 
00187   const unsigned int bytes_to_check = 8;
00188   png_byte buffer[bytes_to_check];
00189 
00190   /* Read in some of the signature bytes */
00191   f.read( (char*)buffer, bytes_to_check * sizeof(png_byte) );
00192 
00193   if ( (png_sig_cmp(buffer, (png_size_t)0, bytes_to_check) != 0) || !f )
00194     throw CLAW_EXCEPTION( "Not a PNG file." );
00195 
00196   png_set_sig_bytes(png_ptr, bytes_to_check);
00197 } // png::reader::check_if_png()

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

Copy the pixels taken from the PNG to the image.

Parameters:
data the pixels from the PNG image.
y Index of the line of the image in which we copy the pixels.

Definition at line 312 of file png_reader.cpp.

References CLAW_PRECOND, claw::graphic::image::height(), m_image, s_rgba_pixel_size, and claw::graphic::image::width().

00313 {
00314   CLAW_PRECOND( data );
00315   CLAW_PRECOND( y < m_image.height() );
00316 
00317   // four bytes for each pixel in the line
00318   for (unsigned int x=0; x!=m_image.width(); ++x, data+=s_rgba_pixel_size)
00319     {
00320       m_image[y][x].components.red   = data[0];
00321       m_image[y][x].components.green = data[1];
00322       m_image[y][x].components.blue  = data[2];
00323       m_image[y][x].components.alpha = data[3];
00324     }
00325 } // png::reader::copy_pixel_line()

void claw::graphic::png::reader::create_read_structures ( png_structp &  png_ptr,
png_infop &  info_ptr 
) const [private]

Initialize the png read structures.

Parameters:
png_ptr PNG file description.
info_ptr PNG file informations.

Definition at line 334 of file png_reader.cpp.

References CLAW_EXCEPTION.

Referenced by read_from_file().

00335 {
00336   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00337 
00338   if (png_ptr)
00339     {
00340       info_ptr = png_create_info_struct(png_ptr);
00341 
00342       if (!info_ptr)
00343         png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
00344     }
00345 
00346   if (!png_ptr || !info_ptr)
00347     throw CLAW_EXCEPTION("Can't create PNG read structures.");
00348 } // png::reader::create_read_structures()

void claw::graphic::png::reader::load ( std::istream &  f  ) 

Load an image from a png file.

Parameters:
f PNG file.

Definition at line 114 of file png_reader.cpp.

References CLAW_PRECOND, and read_from_file().

Referenced by reader().

00115 {
00116   CLAW_PRECOND( !!f );
00117 
00118   std::istream::pos_type init_pos = f.tellg();
00119 
00120   try
00121     {
00122       read_from_file(f);
00123     }
00124   catch(...)
00125     {
00126       f.clear();
00127       f.seekg( init_pos, std::ios_base::beg );
00128       throw;
00129     }
00130 } // png::reader::load()

void claw::graphic::png::reader::read_from_file ( std::istream &  f  )  [private]

Load an image from a png file.

Parameters:
f PNG file.

Definition at line 137 of file png_reader.cpp.

References check_if_png(), claw__graphic__png__source_manager__read(), CLAW_EXCEPTION, create_read_structures(), and read_image().

Referenced by load().

00138 {
00139   source_manager infile(f);
00140   png_structp png_ptr;
00141   png_infop info_ptr;
00142 
00143   create_read_structures(png_ptr, info_ptr);
00144 
00145   if (setjmp(png_jmpbuf(png_ptr)))
00146     {
00147       /* If we get here, we had a problem reading the file */
00148       /* Free all of the memory associated with the png_ptr and info_ptr */
00149       png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
00150       throw CLAW_EXCEPTION("Invalid PNG file.");
00151     }
00152       
00153   check_if_png( png_ptr, f );
00154 
00155   png_set_read_fn( png_ptr, (void *)&infile,
00156                    claw__graphic__png__source_manager__read );
00157 
00158   png_set_strip_16(png_ptr);
00159   png_set_gray_1_2_4_to_8(png_ptr);
00160   png_set_packing(png_ptr);
00161 
00162   // transform palette index into RGB value
00163   png_set_palette_to_rgb(png_ptr);
00164 
00165   // add an alpha value if none
00166   png_set_filler( png_ptr,
00167                   std::numeric_limits<rgba_pixel_8::component_type>::max(),
00168                   PNG_FILLER_AFTER );
00169 
00170   png_read_info(png_ptr, info_ptr);
00171   read_image( png_ptr, info_ptr );
00172       
00173   png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
00174 } // png::reader::read_from_file()

void claw::graphic::png::reader::read_image ( png_structp  png_ptr,
png_infop  info_ptr 
) [private]

Read the image data of the PNG.

Parameters:
png_ptr PNG file description.
info_ptr PNG file informations.

Definition at line 206 of file png_reader.cpp.

References CLAW_PRECOND.

Referenced by read_from_file().

00207 {
00208   CLAW_PRECOND( png_ptr );
00209   CLAW_PRECOND( info_ptr );
00210 
00211   m_image.set_size( png_get_image_width(png_ptr, info_ptr),
00212                     png_get_image_height(png_ptr, info_ptr) );
00213 
00214   if ( png_get_interlace_type(png_ptr, info_ptr) == PNG_INTERLACE_NONE )
00215     read_sequential_image(png_ptr, info_ptr);
00216   else
00217     read_interlaced_image( png_ptr, info_ptr,
00218                            png_set_interlace_handling(png_ptr) );
00219 } // png::reader::read_image()

void claw::graphic::png::reader::read_interlaced_image ( png_structp  png_ptr,
png_infop  info_ptr,
unsigned int  passes 
) [private]

Read the image data of an interlaced PNG.

Parameters:
png_ptr PNG file description.
info_ptr PNG file informations.
passes Number of passes (for interlaced images).

Definition at line 261 of file png_reader.cpp.

References CLAW_PRECOND.

00262 {
00263   CLAW_PRECOND( passes > 1 );
00264   CLAW_PRECOND( png_ptr );
00265   CLAW_PRECOND( info_ptr );
00266 
00267   const unsigned int row_length = s_rgba_pixel_size * m_image.width();
00268   png_bytepp data =
00269     (png_bytepp)png_malloc( png_ptr, sizeof(png_bytep) * m_image.height() );
00270   unsigned int i=0;
00271 
00272   try
00273     {
00274       for (i=0; i!=m_image.height(); ++i)
00275         {
00276           data[i] = (png_bytep)png_malloc( png_ptr, row_length );
00277 
00278           if (!data[i])
00279             throw std::bad_alloc();
00280 
00281           copy_pixel_line( data[i], i );
00282         }
00283 
00284       for (unsigned int p=0; p!=passes; ++p)
00285         png_read_rows( png_ptr, data, NULL, m_image.height() );
00286 
00287       for (unsigned int y=0; y!=m_image.height(); ++y)
00288         copy_pixel_line( data[y], y );
00289     }
00290   catch(...)
00291     {
00292       for(unsigned int j=0; j!=i; ++j)
00293         png_free(png_ptr, data[j]);
00294 
00295       png_free(png_ptr, data);
00296       throw;
00297     }
00298 
00299   for(i=0; i!=m_image.height(); ++i)
00300     png_free(png_ptr, data[i]);
00301 
00302   png_free(png_ptr, data);
00303 } // png::reader::read_interlaced_image()

void claw::graphic::png::reader::read_sequential_image ( png_structp  png_ptr,
png_infop  info_ptr 
) [private]

Read the image data of a non interlaced PNG.

Parameters:
png_ptr PNG file description.
info_ptr PNG file informations.

Definition at line 228 of file png_reader.cpp.

References CLAW_PRECOND.

00229 {
00230   CLAW_PRECOND( png_ptr );
00231   CLAW_PRECOND( info_ptr );
00232 
00233   png_bytep data =
00234     (png_bytep)png_malloc( png_ptr, s_rgba_pixel_size * m_image.width() );
00235 
00236   try
00237     {
00238       for (unsigned int y=0; y!=m_image.height(); ++y)
00239         {
00240           png_read_row(png_ptr, data, NULL);
00241           copy_pixel_line( data, y );
00242         }
00243     }
00244   catch(...)
00245     {
00246       png_free(png_ptr, data);
00247       throw;
00248     }
00249 
00250   png_free(png_ptr, data);
00251 } // png::reader::read_sequential_image()


Member Data Documentation

The image in which we store the data we read.

Definition at line 100 of file png.hpp.

Referenced by copy_pixel_line().

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

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

Definition at line 104 of file png.hpp.

Referenced by copy_pixel_line().


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