xbm_writer.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include <claw/xbm.hpp>
00031 #include <iomanip>
00032
00033
00037 claw::graphic::xbm::writer::options::options()
00038 : name("noname"), hot(NULL)
00039 {
00040
00041 }
00042
00043
00049 claw::graphic::xbm::writer::options::options
00050 ( const std::string& n, const claw::math::coordinate_2d<int>* h )
00051 : name(n), hot(h)
00052 {
00053
00054 }
00055
00056
00057
00058
00059
00064 claw::graphic::xbm::writer::writer( const image& img )
00065 : m_image( img )
00066 {
00067
00068 }
00069
00070
00077 claw::graphic::xbm::writer::writer
00078 ( const image& img, std::ostream& f, const options& opt )
00079 : m_image( img )
00080 {
00081 save(f, opt);
00082 }
00083
00084
00090 void
00091 claw::graphic::xbm::writer::save( std::ostream& f, const options& opt ) const
00092 {
00093 CLAW_PRECOND( !!f );
00094
00095 f << "#define " << opt.name << "_width " << m_image.width() << "\n";
00096 f << "#define " << opt.name << "_height " << m_image.height() << "\n";
00097
00098 if ( opt.hot != NULL )
00099 {
00100 f << "#define " << opt.name << "_x_hot " << opt.hot->x << "\n";
00101 f << "#define " << opt.name << "_y_hot " << opt.hot->y << "\n";
00102 }
00103
00104 f << "static unsigned char " << opt.name << "_bits[] = {\n ";
00105
00106 save_bits(f);
00107 }
00108
00109
00114 void claw::graphic::xbm::writer::save_bits( std::ostream& f ) const
00115 {
00116 const unsigned int max_per_line = (80 - 1) / 6;
00117 const unsigned int nb_pxl = m_image.width() * m_image.height();
00118
00119 unsigned int pxl_count = 0;
00120 unsigned int per_line = 0;
00121
00122 for (unsigned int y=0; y!=m_image.height(); ++y)
00123 {
00124 unsigned int x=0;
00125
00126 while ( x!=m_image.width() )
00127 {
00128 unsigned int v(0);
00129 unsigned int bits;
00130
00131 for ( bits=0; (x!=m_image.width()) && (bits != 8);
00132 ++bits, ++x, ++pxl_count )
00133 {
00134 v >>= 1;
00135 if ( m_image[y][x].luminosity() <= 127 )
00136 v |= 0x80;
00137 }
00138
00139 v >>= 8 - bits;
00140
00141 ++per_line;
00142
00143 f << " 0x" << std::setw(2) << std::setfill('0') << std::hex << v;
00144
00145 if ( pxl_count != nb_pxl )
00146 {
00147 f << ",";
00148
00149 if ( per_line == max_per_line )
00150 {
00151 f << "\n ";
00152 per_line = 0;
00153 }
00154 }
00155 }
00156 }
00157
00158 f << "};" << std::endl;
00159 }