claw  1.9.0
xbm_writer.cpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@stuff-o-matic.com
24 */
30 #include <claw/graphic/xbm.hpp>
31 
32 #include <iomanip>
33 
38  : name("noname")
39  , hot(NULL)
40 {}
41 
48  const std::string& n, const claw::math::coordinate_2d<int>* h)
49  : name(n)
50  , hot(h)
51 {}
52 
58  : m_image(img)
59 {}
60 
67 claw::graphic::xbm::writer::writer(const image& img, std::ostream& f,
68  const options& opt)
69  : m_image(img)
70 {
71  save(f, opt);
72 }
73 
79 void claw::graphic::xbm::writer::save(std::ostream& f,
80  const options& opt) const
81 {
82  CLAW_PRECOND(!!f);
83 
84  f << "#define " << opt.name << "_width " << m_image.width() << "\n";
85  f << "#define " << opt.name << "_height " << m_image.height() << "\n";
86 
87  if(opt.hot != NULL)
88  {
89  f << "#define " << opt.name << "_x_hot " << opt.hot->x << "\n";
90  f << "#define " << opt.name << "_y_hot " << opt.hot->y << "\n";
91  }
92 
93  f << "static unsigned char " << opt.name << "_bits[] = {\n ";
94 
95  save_bits(f);
96 }
97 
102 void claw::graphic::xbm::writer::save_bits(std::ostream& f) const
103 {
104  const unsigned int max_per_line = (80 - 1) / 6;
105  const unsigned int nb_pxl = m_image.width() * m_image.height();
106 
107  unsigned int pxl_count = 0;
108  unsigned int per_line = 0;
109 
110  for(unsigned int y = 0; y != m_image.height(); ++y)
111  {
112  unsigned int x = 0;
113 
114  while(x != m_image.width())
115  {
116  unsigned int v(0);
117  unsigned int bits;
118 
119  for(bits = 0; (x != m_image.width()) && (bits != 8);
120  ++bits, ++x, ++pxl_count)
121  {
122  v >>= 1;
123  if(m_image[y][x].luminosity() <= 127)
124  v |= 0x80;
125  }
126 
127  v >>= 8 - bits;
128 
129  ++per_line;
130 
131  f << " 0x" << std::setw(2) << std::setfill('0') << std::hex << v;
132 
133  if(pxl_count != nb_pxl)
134  {
135  f << ",";
136 
137  if(per_line == max_per_line)
138  {
139  f << "\n ";
140  per_line = 0;
141  }
142  }
143  }
144  }
145 
146  f << "};" << std::endl;
147 }
Parameters of the writing algorithm.
Definition: xbm.hpp:98
options()
Default constructor.
Definition: xbm_writer.cpp:37
std::string name
The name of the image structure in the file.
Definition: xbm.hpp:108
value_type y
Y-coordinate.
const claw::math::coordinate_2d< int > * hot
The position of the hot spot in the image.
Definition: xbm.hpp:111
A class to deal with images.
Definition: image.hpp:50
void save(std::ostream &f, const options &opt=options()) const
Save the image in a XBM file.
Definition: xbm_writer.cpp:79
A class for xbm pictures.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition: assert.hpp:94
writer(const image &img)
Constructor.
Definition: xbm_writer.cpp:57
value_type x
X-coordinate.