Public Member Functions | Private Member Functions | Private Attributes

claw::graphic::xbm::reader Class Reference

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

#include <xbm.hpp>

List of all members.

Public Member Functions

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

Private Member Functions

void read_from_file (std::istream &f)
 Load an image from a xbm file.
void read_size (std::istream &f)
 Read the size of the image.
unsigned int read_dim (const std::string &line) const
 Read the width or height of the image.
unsigned int read_bits_per_entry (std::istream &f) const
 Read the number of bits per entry.
void read_name (std::istream &f)
 Read the name of the image.
void read_pixels (std::istream &f, unsigned int bpe) const
 Read the pixels of the image.
void read_line (std::istream &f, std::string &line, char endchar) const
 Read the next not commented line, and remove any comments from it.
void remove_comments (std::istream &f, std::string &line, char endchar) const
 Remove the comments from a line.

Private Attributes

imagem_image
 The image in which we store the data we read.
std::string m_name
 The name of the xbm image.
claw::math::coordinate_2d< int > * m_hot
 The position of the hot spot in the image.

Detailed Description

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

Author:
Julien Jorge

Definition at line 53 of file xbm.hpp.


Constructor & Destructor Documentation

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

Constructor.

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

Definition at line 41 of file xbm_reader.cpp.

  : m_image( img ), m_hot(NULL)
{

} // xbm::reader::reader()
claw::graphic::xbm::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 54 of file xbm_reader.cpp.

References load().

  : m_image( img ), m_hot(NULL)
{
  load(f);
} // xbm::reader::reader()
claw::graphic::xbm::reader::reader ( xbm 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 67 of file xbm_reader.cpp.

References load(), m_hot, m_name, claw::graphic::xbm::set_hot(), and claw::graphic::xbm::set_name().

  : m_image( img ), m_hot(NULL)
{
  load(f);
  img.set_name( m_name );

  if (m_hot != NULL)
    img.set_hot( *m_hot );
} // xbm::reader::reader()
claw::graphic::xbm::reader::~reader (  )

Destructor.

Definition at line 81 of file xbm_reader.cpp.

References claw::graphic::xbm::m_hot.

{
  if (m_hot != NULL)
    {
      delete m_hot;
      m_hot = NULL;
    }
} // xbm::reader::~reader()

Member Function Documentation

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

Load an image from a xbm file.

Parameters:
fXBM file.

Definition at line 95 of file xbm_reader.cpp.

References CLAW_PRECOND, and claw::graphic::xbm::m_hot.

Referenced by reader().

{
  CLAW_PRECOND( !!f );

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

  if (m_hot != NULL)
    {
      delete m_hot;
      m_hot = NULL;
    }

  try
    {
      read_from_file(f);
    }
  catch(...)
    {
      if (m_hot != NULL)
        delete m_hot;

      f.clear();
      f.seekg( init_pos, std::ios_base::beg );
      throw;
    }
} // xbm::reader::load()
unsigned int claw::graphic::xbm::reader::read_bits_per_entry ( std::istream &  f ) const [private]

Read the number of bits per entry.

Parameters:
fThe stream in which we read the field.

Definition at line 235 of file xbm_reader.cpp.

{
  std::string line;
  unsigned int result(0);

  std::string token;

  if ( f >> token )
    if ( token == "static" )
      if ( f >> token )
        {
          if ( token == "unsigned" )
            f >> token;
          else if ( token == "signed" )
            f >> token;

          if ( token == "char" )
            result = sizeof(char) * 8;
          else if ( token == "short" )
            result = sizeof(short) * 8;
          else if ( token == "int" )
            result = sizeof(int) * 8;
          else if ( token == "long" )
            result = sizeof(long) * 8;
        }

  if ( result == 0 )
    throw claw::exception( "Not an XBM file." );

  return result;
} // xbm::reader::read_bits_per_entry()
unsigned int claw::graphic::xbm::reader::read_dim ( const std::string &  line ) const [private]

Read the width or height of the image.

Parameters:
lineThe line in which we read the field.

Definition at line 210 of file xbm_reader.cpp.

{
  unsigned int result;
  std::istringstream iss(line);
  std::string token;
  bool valid = false;

  if (iss >> token)
    if ( token == "#define" )
      if ( iss >> token )
        if ( iss >> result )
          valid = true;

  if ( !valid )
    throw claw::exception( "Not an XBM file." );

  return result;
} // xbm::reader::read_dim()
void claw::graphic::xbm::reader::read_from_file ( std::istream &  f ) [private]

Load an image from a xbm file.

Parameters:
fXBM file.

Definition at line 127 of file xbm_reader.cpp.

{
  std::string line;
  bool valid_format = false;
  unsigned int bpe;

  read_size(f);
  bpe = read_bits_per_entry(f);
  read_name(f);
  read_line( f, line, '{' );

  if ( !line.empty() )
    {
      read_pixels(f, bpe);
      read_line(f, line, ';');

      valid_format = true;
    }

  if ( !valid_format )
    throw claw::exception( "Not an XBM file." );
} // xbm::reader::read_from_file()
void claw::graphic::xbm::reader::read_line ( std::istream &  f,
std::string &  line,
char  endchar 
) const [private]

Read the next not commented line, and remove any comments from it.

Parameters:
fThe stream in which we search the next line.
lineThe line in which we read the field.
endcharThe character used to end the line.

Definition at line 352 of file xbm_reader.cpp.

References claw::text::getline(), and claw::text::trim().

{
  bool stop = false;

  line.clear();

  while ( !stop )
    if ( std::getline( f, line, endchar ) )
      {
        text::trim(line);


        remove_comments(f, line, endchar);
        stop = !line.empty();
      }
    else
      stop = true;
} // xbm::reader::read_line()
void claw::graphic::xbm::reader::read_name ( std::istream &  f ) [private]

Read the name of the image.

Parameters:
fThe stream in which we read the field.

Definition at line 272 of file xbm_reader.cpp.

References claw::graphic::image::begin(), claw::graphic::image::end(), and claw::graphic::xbm::m_name.

{
  bool valid = false;
  std::string line;

  read_line(f, line, '[');

  if ( !line.empty() )
    {
      std::string::size_type end = line.find_last_of('_');
                  
      if ( end != std::string::npos )
        {
          std::string::size_type begin = line.find_last_of(" \t", end);

          if ( begin == std::string::npos )
            begin = 0;

          m_name = line.substr(begin, end - begin);
          valid = true;
        }
    }

  if ( !valid )
    throw claw::exception( "Not an XBM file." );
} // xbm::reader::read_name()
void claw::graphic::xbm::reader::read_pixels ( std::istream &  f,
unsigned int  bpe 
) const [private]

Read the pixels of the image.

Parameters:
fThe stream in which we search the next line.
bpeNumber of bits per entry.

Definition at line 306 of file xbm_reader.cpp.

References claw::graphic::black_pixel, and claw::graphic::white_pixel.

{
  image::iterator first = m_image.begin();
  const image::iterator last = m_image.end();

  bool valid = true;

  unsigned int x = 0;

  while ( (first!=last) && valid )
    {
      std::string s_val;
      read_line( f, s_val, ',' );

      std::istringstream iss(s_val);
      long int val;

      if ( iss >> std::hex >> val )
        {
          for( unsigned int i=0;
               (i!=bpe) && (first!=last) && (x!=m_image.width());
               ++i, ++first, ++x, val >>= 1 )
            if ( val & 1 )
              *first = black_pixel;
            else
              *first = white_pixel;

          if ( x==m_image.width() )
            x = 0;
        }
      else
        valid = false;
    }

  if ( !valid )
    throw claw::exception( "Not an XBM file." );
} // xbm::reader::read_pixels()
void claw::graphic::xbm::reader::read_size ( std::istream &  f ) [private]

Read the size of the image.

Parameters:
fThe stream in which we read the field.

Definition at line 155 of file xbm_reader.cpp.

References claw::graphic::xbm::m_hot, claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.

{
  unsigned int w(0), h(0);
  bool valid = true;
  bool stop = false;
  std::string line;

  while ( valid && !stop )
    {
      std::ios::pos_type pos = f.tellg();

      read_line( f, line, '\n' );

      if ( !line.empty() )
        {
          if ( line.find("width") != std::string::npos )
            w = read_dim(line);
          else if ( line.find("height") != std::string::npos )
            h = read_dim(line);
          else if ( line.find("x_hot") != std::string::npos )
            {
              if ( m_hot == NULL )
                m_hot = new claw::math::coordinate_2d<int>;

              m_hot->x = read_dim(line);
            }
          else if ( line.find("y_hot") != std::string::npos )
            {
              if ( m_hot == NULL )
                m_hot = new claw::math::coordinate_2d<int>;

              m_hot->y = read_dim(line);
            }
          else if ( line.find("static") != std::string::npos )
            {
              stop = true;
              f.seekg( pos );
            }
        }
      else
        valid = false;
    }
  
  if ( valid )
    m_image.set_size(w, h);
  else
    throw claw::exception( "Not an XBM file." );
} // xbm::reader::read_size()
void claw::graphic::xbm::reader::remove_comments ( std::istream &  f,
std::string &  line,
char  endchar 
) const [private]

Remove the comments from a line.

Parameters:
fIf the line contains the begining of a multi line comment, we search the next line from this stream.
lineThe line in which we read the field.
endcharThe character used to end the line.

Definition at line 380 of file xbm_reader.cpp.

References claw::graphic::image::end(), claw::text::getline(), and claw::text::trim().

{
  std::string working(line);
  std::string::size_type beg = working.find( "/*" );

  if ( beg != std::string::npos )
    {
      line = working.substr(0, beg);

      std::string::size_type end = working.rfind( "*/" );
      bool stop = false;

      while ( (end == std::string::npos) && !stop )
        if ( std::getline(f, working, endchar) )
          end = working.find( "*/" );
        else
          stop = true;

      if ( !stop )
        {
          line += working.substr(end+2, line.length() - end - 2);
          text::trim(line);
        }

      if ( !line.empty() )
        remove_comments(f, line, endchar);
    }
} // xbm::reader::remove_comments()

Member Data Documentation

The position of the hot spot in the image.

Definition at line 85 of file xbm.hpp.

Referenced by reader().

The image in which we store the data we read.

Definition at line 79 of file xbm.hpp.

std::string claw::graphic::xbm::reader::m_name [private]

The name of the xbm image.

Definition at line 82 of file xbm.hpp.

Referenced by reader().


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