This class read data from a xbm file and store it in an image. More...
#include <xbm.hpp>
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 | |
| image & | m_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. | |
This class read data from a xbm file and store it in an image.
Definition at line 53 of file xbm.hpp.
| claw::graphic::xbm::reader::reader | ( | image & | img | ) |
Constructor.
| img | The image in which the data will be stored. |
Definition at line 41 of file xbm_reader.cpp.
| claw::graphic::xbm::reader::reader | ( | image & | img, | |
| std::istream & | f | |||
| ) |
| claw::graphic::xbm::reader::reader | ( | xbm & | img, | |
| std::istream & | f | |||
| ) |
Constructor.
| img | The image in which the data will be stored. | |
| f | The file from which we read the data. |
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().
| claw::graphic::xbm::reader::~reader | ( | ) |
| void claw::graphic::xbm::reader::load | ( | std::istream & | f | ) |
Load an image from a xbm file.
| f | XBM file. |
Definition at line 95 of file xbm_reader.cpp.
References CLAW_PRECOND, m_hot, and read_from_file().
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.
| f | The stream in which we read the field. |
Definition at line 235 of file xbm_reader.cpp.
References CLAW_EXCEPTION.
Referenced by read_from_file().
{
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 a 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.
| line | The line in which we read the field. |
Definition at line 210 of file xbm_reader.cpp.
References CLAW_EXCEPTION.
Referenced by read_size().
{
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 a 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.
| f | XBM file. |
Definition at line 127 of file xbm_reader.cpp.
References CLAW_EXCEPTION, read_bits_per_entry(), read_line(), read_name(), read_pixels(), and read_size().
Referenced by load().
{
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 a 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.
| f | The stream in which we search the next line. | |
| line | The line in which we read the field. | |
| endchar | The character used to end the line. |
Definition at line 352 of file xbm_reader.cpp.
References claw::text::getline(), and claw::text::trim().
Referenced by read_from_file(), read_name(), and read_size().
{
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.
| f | The stream in which we read the field. |
Definition at line 272 of file xbm_reader.cpp.
References claw::graphic::image::begin(), CLAW_EXCEPTION, claw::graphic::image::end(), m_name, and read_line().
Referenced by read_from_file().
{
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 a 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.
| f | The stream in which we search the next line. | |
| bpe | Number of bits per entry. |
Definition at line 306 of file xbm_reader.cpp.
References claw::graphic::black_pixel, CLAW_EXCEPTION, and claw::graphic::white_pixel.
Referenced by read_from_file().
{
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 a XBM file." );
} // xbm::reader::read_pixels()
| void claw::graphic::xbm::reader::read_size | ( | std::istream & | f | ) | [private] |
Read the size of the image.
| f | The stream in which we read the field. |
Definition at line 155 of file xbm_reader.cpp.
References CLAW_EXCEPTION, m_hot, m_image, read_dim(), read_line(), claw::graphic::image::set_size(), claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.
Referenced by read_from_file().
{
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 a 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.
| f | If the line contains the begining of a multi line comment, we search the next line from this stream. | |
| line | The line in which we read the field. | |
| endchar | The 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()
claw::math::coordinate_2d<int>* claw::graphic::xbm::reader::m_hot [private] |
image& claw::graphic::xbm::reader::m_image [private] |
The image in which we store the data we read.
Definition at line 79 of file xbm.hpp.
Referenced by read_size().
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 read_name(), and reader().
1.7.1