bitmap_reader.tpp

Go to the documentation of this file.
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-2008 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@yahoo.fr
00024 */
00030 #include <cassert>
00031 #include <algorithm>
00032 #include <claw/exception.hpp>
00033 
00034 /*----------------------------------------------------------------------------*/
00040 template< bool Coded4Bits >
00041 claw::graphic::bitmap::reader::
00042 rle_bitmap_output_buffer<Coded4Bits>::rle_bitmap_output_buffer
00043 ( const color_palette_type& palette, image& img )
00044   : m_palette(palette), m_image(img), m_x(0), m_y(m_image.height() - 1)
00045 {
00046 
00047 } // bitmap::reader::rle_bitmap_output_buffer::rle_bitmap_output_buffer()
00048 
00049 /*----------------------------------------------------------------------------*/
00053 template< bool Coded4Bits >
00054 void claw::graphic::bitmap::reader::
00055 rle_bitmap_output_buffer<Coded4Bits>::next_line()
00056 {
00057   assert( m_y > 0 );
00058 
00059   m_x = 0;
00060   --m_y;
00061 } // bitmap::reader::rle_bitmap_output_buffer::next_line()
00062 
00063 /*----------------------------------------------------------------------------*/
00069 template< bool Coded4Bits >
00070 void claw::graphic::bitmap::reader::
00071 rle_bitmap_output_buffer<Coded4Bits>::delta_move
00072 (unsigned char x, unsigned char y)
00073 {
00074   assert( m_x + x < m_image.width() );
00075   assert( m_y + y < m_image.height() );
00076 
00077   m_x += x;
00078   m_y += y;
00079 } // bitmap::reader::rle_bitmap_output_buffer::delta_move()
00080 
00081 
00082 //******************************************************************************
00083 
00084 
00085 /*----------------------------------------------------------------------------*/
00092 template< typename OutputBuffer >
00093 void claw::graphic::bitmap::reader::rle_bitmap_decoder<OutputBuffer>::read_mode
00094 ( file_input_buffer& input, output_buffer_type& output)
00095 {
00096   this->m_mode = this->stop;
00097   bool ok = true;
00098 
00099   if ( input.remaining() < 2)
00100     ok = input.read_more(2);
00101 
00102   if (ok)
00103     {
00104       unsigned char key, pattern;
00105       
00106       key = input.get_next();
00107       pattern = input.get_next();
00108       
00109       // compressed data, next byte is the pattern
00110       if (key > 0)
00111         {
00112           this->m_mode = this->compressed;
00113           this->m_count = key;
00114           this->m_pattern = pattern;
00115         }
00116       else switch( pattern )
00117         {
00118           // end of line
00119         case 0 : output.next_line(); read_mode(input, output); break;
00120           // end of file
00121         case 1 : this->m_mode = this->stop; break;
00122           // delta move
00123         case 2 :
00124           {
00125             if ( input.remaining() < 1 )
00126               ok = input.read_more(1);
00127 
00128             if (ok)
00129               {
00130                 unsigned char x, y;
00131                 x = pattern;
00132                 y = input.get_next();
00133                 output.delta_move(x, y);
00134                 read_mode(input, output);
00135                 break;
00136               }
00137           }
00138           // raw data
00139         default: this->m_mode = this->raw; this->m_count = pattern; break;
00140         }
00141     }
00142 } // bitmap::reader::rle_bitmap_decoder::read_mode()
00143 
00144 
00145 /*----------------------------------------------------------------------------*/
00159 template<typename Convert>
00160 void claw::graphic::bitmap::reader::load_rgb_data
00161 ( std::istream& f, unsigned int buffer_size, const color_palette_type& palette,
00162   const Convert& pixel_convert )
00163 {
00164   unsigned int line;
00165 
00166   // lines are 4-bytes aligned, so adjust buffer's size.
00167   if (buffer_size % 4 != 0) 
00168     buffer_size += 4 - buffer_size % 4;
00169 
00170   char* buffer = new char[buffer_size];
00171 
00172   for (line = m_image.height(); (line>0) && !f.eof(); )
00173     {
00174       --line;
00175       f.read(buffer, buffer_size);
00176       pixel_convert( m_image[line], buffer, palette );
00177     }
00178 
00179   delete[] buffer;
00180 
00181   if ( f.rdstate() != std::ios_base::goodbit )
00182     throw claw::bad_format("bitmap::reader::load_data");
00183 } // bitmap::reader::load_data()

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1