CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
Classes | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
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:
imgThe image in which the data will be stored.

Definition at line 90 of file png_reader.cpp.

  : m_image( img )
{

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

Constructor.

Parameters:
imgThe image in which the data will be stored.
fThe 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().

  : m_image( img )
{
  load(f);
} // 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_ptrPNG file description.
fThe stream to read from.

Definition at line 183 of file png_reader.cpp.

References CLAW_EXCEPTION, and CLAW_PRECOND.

{
  CLAW_PRECOND( !!f );

  const unsigned int bytes_to_check = 8;
  png_byte buffer[bytes_to_check];

  /* Read in some of the signature bytes */
  f.read( (char*)buffer, bytes_to_check * sizeof(png_byte) );

  if ( (png_sig_cmp(buffer, (png_size_t)0, bytes_to_check) != 0) || !f )
    throw CLAW_EXCEPTION( "Not a PNG file." );

  png_set_sig_bytes(png_ptr, bytes_to_check);
} // 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:
datathe pixels from the PNG image.
yIndex 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_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)
    {
      m_image[y][x].components.red   = data[0];
      m_image[y][x].components.green = data[1];
      m_image[y][x].components.blue  = data[2];
      m_image[y][x].components.alpha = data[3];
    }
} // 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_ptrPNG file description.
info_ptrPNG file informations.

Definition at line 334 of file png_reader.cpp.

References CLAW_EXCEPTION.

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

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

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

  if (!png_ptr || !info_ptr)
    throw CLAW_EXCEPTION("Can't create PNG read structures.");
} // png::reader::create_read_structures()
void claw::graphic::png::reader::load ( std::istream &  f)

Load an image from a png file.

Parameters:
fPNG file.

Definition at line 114 of file png_reader.cpp.

References CLAW_PRECOND.

Referenced by reader().

{
  CLAW_PRECOND( !!f );

  std::istream::pos_type init_pos = f.tellg();

  try
    {
      read_from_file(f);
    }
  catch(...)
    {
      f.clear();
      f.seekg( init_pos, std::ios_base::beg );
      throw;
    }
} // png::reader::load()
void claw::graphic::png::reader::read_from_file ( std::istream &  f) [private]

Load an image from a png file.

Parameters:
fPNG file.

Definition at line 137 of file png_reader.cpp.

References claw__graphic__png__source_manager__read(), and CLAW_EXCEPTION.

{
  source_manager infile(f);
  png_structp png_ptr;
  png_infop info_ptr;

  create_read_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_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
      throw CLAW_EXCEPTION("Invalid PNG file.");
    }
      
  check_if_png( png_ptr, f );

  png_set_read_fn( png_ptr, (void *)&infile,
                   claw__graphic__png__source_manager__read );

  png_set_strip_16(png_ptr);
  png_set_expand_gray_1_2_4_to_8(png_ptr);
  png_set_packing(png_ptr);

  // transform palette index into RGB value
  png_set_palette_to_rgb(png_ptr);

  // add an alpha value if none
  png_set_filler( png_ptr,
                  std::numeric_limits<rgba_pixel_8::component_type>::max(),
                  PNG_FILLER_AFTER );

  png_read_info(png_ptr, info_ptr);
  read_image( png_ptr, info_ptr );
      
  png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
} // 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_ptrPNG file description.
info_ptrPNG file informations.

Definition at line 206 of file png_reader.cpp.

References CLAW_PRECOND.

{
  CLAW_PRECOND( png_ptr );
  CLAW_PRECOND( info_ptr );

  m_image.set_size( png_get_image_width(png_ptr, info_ptr),
                    png_get_image_height(png_ptr, info_ptr) );

  if ( png_get_interlace_type(png_ptr, info_ptr) == PNG_INTERLACE_NONE )
    read_sequential_image(png_ptr, info_ptr);
  else
    read_interlaced_image( png_ptr, info_ptr,
                           png_set_interlace_handling(png_ptr) );
} // 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_ptrPNG file description.
info_ptrPNG file informations.
passesNumber of passes (for interlaced images).

Definition at line 261 of file png_reader.cpp.

References CLAW_PRECOND.

{
  CLAW_PRECOND( passes > 1 );
  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 );
        }

      for (unsigned int p=0; p!=passes; ++p)
        png_read_rows( png_ptr, data, NULL, m_image.height() );

      for (unsigned int y=0; y!=m_image.height(); ++y)
        copy_pixel_line( data[y], y );
    }
  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::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_ptrPNG file description.
info_ptrPNG file informations.

Definition at line 228 of file png_reader.cpp.

References CLAW_PRECOND.

{
  CLAW_PRECOND( png_ptr );
  CLAW_PRECOND( info_ptr );

  png_bytep data =
    (png_bytep)png_malloc( png_ptr, s_rgba_pixel_size * m_image.width() );

  try
    {
      for (unsigned int y=0; y!=m_image.height(); ++y)
        {
          png_read_row(png_ptr, data, NULL);
          copy_pixel_line( data, y );
        }
    }
  catch(...)
    {
      png_free(png_ptr, data);
      throw;
    }

  png_free(png_ptr, data);
} // 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.

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.


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