95 std::istream::pos_type init_pos = f.tellg();
113 f.seekg(init_pos, std::ios_base::beg);
122void claw::graphic::xbm::reader::read_from_file(std::istream& f)
125 bool valid_format =
false;
129 bpe = read_bits_per_entry(f);
131 read_line(f, line,
'{');
136 read_line(f, line,
';');
149void claw::graphic::xbm::reader::read_size(std::istream& f)
151 unsigned int w(0), h(0);
156 while(valid && !stop)
158 std::ios::pos_type pos = f.tellg();
160 read_line(f, line,
'\n');
164 if(line.find(
"width") != std::string::npos)
166 else if(line.find(
"height") != std::string::npos)
168 else if(line.find(
"x_hot") != std::string::npos)
173 m_hot->
x = read_dim(line);
175 else if(line.find(
"y_hot") != std::string::npos)
180 m_hot->
y = read_dim(line);
182 else if(line.find(
"static") != std::string::npos)
193 m_image.set_size(w, h);
195 throw claw::exception(
"Not an XBM file.");
203claw::graphic::xbm::reader::read_dim(
const std::string& line)
const
206 std::istringstream iss(line);
211 if(token ==
"#define")
217 throw claw::exception(
"Not an XBM file.");
227claw::graphic::xbm::reader::read_bits_per_entry(std::istream& f)
const
230 unsigned int result(0);
235 if(token ==
"static")
238 if(token ==
"unsigned")
240 else if(token ==
"signed")
244 result =
sizeof(char) * 8;
245 else if(token ==
"short")
246 result =
sizeof(short) * 8;
247 else if(token ==
"int")
248 result =
sizeof(int) * 8;
249 else if(token ==
"long")
250 result =
sizeof(long) * 8;
254 throw claw::exception(
"Not an XBM file.");
263void claw::graphic::xbm::reader::read_name(std::istream& f)
268 read_line(f, line,
'[');
272 std::string::size_type
end = line.find_last_of(
'_');
274 if(
end != std::string::npos)
276 std::string::size_type
begin = line.find_last_of(
" \t",
end);
278 if(
begin == std::string::npos)
287 throw claw::exception(
"Not an XBM file.");
295void claw::graphic::xbm::reader::read_pixels(std::istream& f,
296 unsigned int bpe)
const
305 while((first != last) && valid)
308 read_line(f, s_val,
',');
310 std::istringstream iss(s_val);
313 if(iss >> std::hex >> val)
315 for(
unsigned int i = 0;
316 (i != bpe) && (first != last) && (x != m_image.width());
317 ++i, ++first, ++x, val >>= 1)
323 if(x == m_image.width())
331 throw claw::exception(
"Not an XBM file.");
340void claw::graphic::xbm::reader::read_line(std::istream& f, std::string& line,
348 if(std::getline(f, line, endchar))
352 remove_comments(f, line, endchar);
353 stop = !line.empty();
365void claw::graphic::xbm::reader::remove_comments(std::istream& f,
369 std::string working(line);
370 std::string::size_type beg = working.find(
"/*");
372 if(beg != std::string::npos)
374 line = working.substr(0, beg);
376 std::string::size_type
end = working.rfind(
"*/");
379 while((
end == std::string::npos) && !stop)
380 if(std::getline(f, working, endchar))
381 end = working.find(
"*/");
387 line += working.substr(
end + 2, line.length() -
end - 2);
392 remove_comments(f, line, endchar);
Some assert macros to strengthen you code.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
A simple class to use as exception with string message.
iterator end()
Get an iterator pointing just past the last pixel.
image()
Constructor. Creates an image without datas.
base_iterator< image, pixel_type > iterator
The type of the iterator on the pixels of the image.
iterator begin()
Get an iterator pointing on the first pixel.
reader(image &img)
Constructor.
void load(std::istream &f)
Load an image from a xbm file.
xbm(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
void set_hot(const claw::math::coordinate_2d< int > &hot)
Set the hot spot of the image.
void set_name(const std::string &name)
Set the name of the image.
Coordinates in a two dimensional space.
value_type x
X-coordinate.
value_type y
Y-coordinate.
A simple class to use as exception with string message.
rgba_pixel white_pixel
The white color.
rgba_pixel black_pixel
The black color.
Generic algorithms on strings.
A class for xbm pictures.