Claw 1.7.3
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@gamned.org
24*/
30#include <claw/xbm.hpp>
31#include <iomanip>
32
33/*----------------------------------------------------------------------------*/
38 : name("noname"), hot(NULL)
39{
40
41} // xbm::writer::options::options()
42
43/*----------------------------------------------------------------------------*/
50( const std::string& n, const claw::math::coordinate_2d<int>* h )
51 : name(n), hot(h)
52{
53
54} // xbm::writer::options::options()
55
56
57
58
59/*----------------------------------------------------------------------------*/
65 : m_image( img )
66{
67
68} // xbm::writer::writer()
69
70/*----------------------------------------------------------------------------*/
78( const image& img, std::ostream& f, const options& opt )
79 : m_image( img )
80{
81 save(f, opt);
82} // xbm::writer::writer()
83
84/*----------------------------------------------------------------------------*/
90void
91claw::graphic::xbm::writer::save( std::ostream& f, const options& opt ) const
92{
93 CLAW_PRECOND( !!f );
94
95 f << "#define " << opt.name << "_width " << m_image.width() << "\n";
96 f << "#define " << opt.name << "_height " << m_image.height() << "\n";
97
98 if ( opt.hot != NULL )
99 {
100 f << "#define " << opt.name << "_x_hot " << opt.hot->x << "\n";
101 f << "#define " << opt.name << "_y_hot " << opt.hot->y << "\n";
102 }
103
104 f << "static unsigned char " << opt.name << "_bits[] = {\n ";
105
106 save_bits(f);
107} // xbm::writer::save()
108
109/*----------------------------------------------------------------------------*/
114void claw::graphic::xbm::writer::save_bits( std::ostream& f ) const
115{
116 const unsigned int max_per_line = (80 - 1) / 6;
117 const unsigned int nb_pxl = m_image.width() * m_image.height();
118
119 unsigned int pxl_count = 0;
120 unsigned int per_line = 0;
121
122 for (unsigned int y=0; y!=m_image.height(); ++y)
123 {
124 unsigned int x=0;
125
126 while ( x!=m_image.width() )
127 {
128 unsigned int v(0);
129 unsigned int bits;
130
131 for ( bits=0; (x!=m_image.width()) && (bits != 8);
132 ++bits, ++x, ++pxl_count )
133 {
134 v >>= 1;
135 if ( m_image[y][x].luminosity() <= 127 )
136 v |= 0x80;
137 }
138
139 v >>= 8 - bits;
140
141 ++per_line;
142
143 f << " 0x" << std::setw(2) << std::setfill('0') << std::hex << v;
144
145 if ( pxl_count != nb_pxl )
146 {
147 f << ",";
148
149 if ( per_line == max_per_line )
150 {
151 f << "\n ";
152 per_line = 0;
153 }
154 }
155 }
156 }
157
158 f << "};" << std::endl;
159} // xbm::writer::save()
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition assert.hpp:98
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.
writer(const image &img)
Constructor.
Coordinates in a two dimensional space.
value_type x
X-coordinate.
value_type y
Y-coordinate.
Parameters of the writing algorithm.
Definition xbm.hpp:101
const claw::math::coordinate_2d< int > * hot
The position of the hot spot in the image.
Definition xbm.hpp:113
std::string name
The name of the image structure in the file.
Definition xbm.hpp:110
A class for xbm pictures.