xbm_writer.cpp
Go to the documentation of this file.
/*
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
*/
#include <claw/xbm.hpp>
#include <iomanip>
/*----------------------------------------------------------------------------*/
claw::graphic::xbm::writer::options::options()
: name("noname"), hot(NULL)
{
} // xbm::writer::options::options()
/*----------------------------------------------------------------------------*/
claw::graphic::xbm::writer::options::options
( const std::string& n, const claw::math::coordinate_2d<int>* h )
: name(n), hot(h)
{
} // xbm::writer::options::options()
/*----------------------------------------------------------------------------*/
claw::graphic::xbm::writer::writer( const image& img )
: m_image( img )
{
} // xbm::writer::writer()
/*----------------------------------------------------------------------------*/
claw::graphic::xbm::writer::writer
( const image& img, std::ostream& f, const options& opt )
: m_image( img )
{
save(f, opt);
} // xbm::writer::writer()
/*----------------------------------------------------------------------------*/
void
claw::graphic::xbm::writer::save( std::ostream& f, const options& opt ) const
{
CLAW_PRECOND( !!f );
f << "#define " << opt.name << "_width " << m_image.width() << "\n";
f << "#define " << opt.name << "_height " << m_image.height() << "\n";
if ( opt.hot != NULL )
{
f << "#define " << opt.name << "_x_hot " << opt.hot->x << "\n";
f << "#define " << opt.name << "_y_hot " << opt.hot->y << "\n";
}
f << "static unsigned char " << opt.name << "_bits[] = {\n ";
save_bits(f);
} // xbm::writer::save()
/*----------------------------------------------------------------------------*/
void claw::graphic::xbm::writer::save_bits( std::ostream& f ) const
{
const unsigned int max_per_line = (80 - 1) / 6;
const unsigned int nb_pxl = m_image.width() * m_image.height();
unsigned int pxl_count = 0;
unsigned int per_line = 0;
for (unsigned int y=0; y!=m_image.height(); ++y)
{
unsigned int x=0;
while ( x!=m_image.width() )
{
unsigned int v(0);
unsigned int bits;
for ( bits=0; (x!=m_image.width()) && (bits != 8);
++bits, ++x, ++pxl_count )
{
v >>= 1;
if ( m_image[y][x].luminosity() <= 127 )
v |= 0x80;
}
v >>= 8 - bits;
++per_line;
f << " 0x" << std::setw(2) << std::setfill('0') << std::hex << v;
if ( pxl_count != nb_pxl )
{
f << ",";
if ( per_line == max_per_line )
{
f << "\n ";
per_line = 0;
}
}
}
}
f << "};" << std::endl;
} // xbm::writer::save()
claw::graphic::xbm::writer::options
Parameters of the writing algorithm.
Definition: xbm.hpp:100
claw::graphic::image::height
unsigned int height() const
Gets image&#39;s height.
Definition: image.cpp:159
claw::graphic::xbm::writer::options::options
options()
Default constructor.
Definition: xbm_writer.cpp:37
claw::math::coordinate_2d< int >
claw::graphic::xbm::writer::options::name
std::string name
The name of the image structure in the file.
Definition: xbm.hpp:110
claw::math::coordinate_2d::y
value_type y
Y-coordinate.
Definition: coordinate_2d.hpp:85
claw::graphic::image::width
unsigned int width() const
Gets image&#39;s width.
Definition: image.cpp:147
claw::graphic::xbm::writer::options::hot
const claw::math::coordinate_2d< int > * hot
The position of the hot spot in the image.
Definition: xbm.hpp:113
claw::graphic::image
A class to deal with images.
Definition: image.hpp:49
claw::graphic::xbm::writer::save
void save(std::ostream &f, const options &opt=options()) const
Save the image in a XBM file.
Definition: xbm_writer.cpp:91
xbm.hpp
A class for xbm pictures.
CLAW_PRECOND
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition: assert.hpp:98
claw::graphic::xbm::writer::writer
writer(const image &img)
Constructor.
Definition: xbm_writer.cpp:64
claw::math::coordinate_2d::x
value_type x
X-coordinate.
Definition: coordinate_2d.hpp:82