Claw
1.7.3
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2011 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien.jorge@gamned.org 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 } // xbm::writer::options::options() 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 } // xbm::writer::options::options() 00055 00056 00057 00058 00059 /*----------------------------------------------------------------------------*/ 00064 claw::graphic::xbm::writer::writer( const image& img ) 00065 : m_image( img ) 00066 { 00067 00068 } // xbm::writer::writer() 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 } // xbm::writer::writer() 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 } // xbm::writer::save() 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 } // xbm::writer::save()