1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
/**
* \file xbm_reader.cpp
* \brief Implementation of the claw::graphic::xbm::reader class.
* \author Julien Jorge
*/
#include <claw/xbm.hpp>
#include <claw/exception.hpp>
#include <claw/assert.hpp>
#include <claw/string_algorithm.hpp>
/*----------------------------------------------------------------------------*/
/**
* \brief Constructor.
* \param img The image in which the data will be stored.
*/
claw::graphic::xbm::reader::reader( image& img )
: m_image( img ), m_hot(NULL)
{
} // xbm::reader::reader()
/*----------------------------------------------------------------------------*/
/**
* \brief Constructor.
* \param img The image in which the data will be stored.
* \param f The file from which we read the data.
* \post img contains the data from \a f.
*/
claw::graphic::xbm::reader::reader( image& img, std::istream& f )
: m_image( img ), m_hot(NULL)
{
load(f);
} // xbm::reader::reader()
/*----------------------------------------------------------------------------*/
/**
* \brief Constructor.
* \param img The image in which the data will be stored.
* \param f The file from which we read the data.
* \post img contains the data from \a f.
*/
claw::graphic::xbm::reader::reader( xbm& img, std::istream& f )
: 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()
/*----------------------------------------------------------------------------*/
/**
* \brief Destructor.
*/
claw::graphic::xbm::reader::~reader()
{
if (m_hot != NULL)
{
delete m_hot;
m_hot = NULL;
}
} // xbm::reader::~reader()
/*----------------------------------------------------------------------------*/
/**
* \brief Load an image from a xbm file.
* \param f XBM file.
*/
void claw::graphic::xbm::reader::load( std::istream& f )
{
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()
/*----------------------------------------------------------------------------*/
/**
* \brief Load an image from a xbm file.
* \param f XBM file.
*/
void claw::graphic::xbm::reader::read_from_file( std::istream& f )
{
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()
/*----------------------------------------------------------------------------*/
/**
* \brief Read the size of the image.
* \param f The stream in which we read the field.
*/
void claw::graphic::xbm::reader::read_size( std::istream& f )
{
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()
/*----------------------------------------------------------------------------*/
/**
* \brief Read the width or height of the image.
* \param line The line in which we read the field.
*/
unsigned int
claw::graphic::xbm::reader::read_dim( const std::string& line ) const
{
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()
/*----------------------------------------------------------------------------*/
/**
* \brief Read the number of bits per entry.
* \param f The stream in which we read the field.
*/
unsigned int
claw::graphic::xbm::reader::read_bits_per_entry( std::istream& f ) const
{
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()
/*----------------------------------------------------------------------------*/
/**
* \brief Read the name of the image.
* \param f The stream in which we read the field.
*/
void claw::graphic::xbm::reader::read_name( std::istream& f )
{
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()
/*----------------------------------------------------------------------------*/
/**
* \brief Read the pixels of the image.
* \param f The stream in which we search the next line.
* \param bpe Number of bits per entry.
*/
void claw::graphic::xbm::reader::read_pixels
( std::istream& f, unsigned int bpe ) const
{
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()
/*----------------------------------------------------------------------------*/
/**
* \brief Read the next not commented line, and remove any comments from it.
* \param f The stream in which we search the next line.
* \param line The line in which we read the field.
* \param endchar The character used to end the line.
*/
void claw::graphic::xbm::reader::read_line
( std::istream& f, std::string& line, char endchar ) const
{
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()
/*----------------------------------------------------------------------------*/
/**
* \brief Remove the comments from a line.
* \param f If the line contains the begining of a multi line comment, we search
* the next line from this stream.
* \param line The line in which we read the field.
* \param endchar The character used to end the line.
*/
void claw::graphic::xbm::reader::remove_comments
( std::istream& f, std::string& line, char endchar ) const
{
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()
|