bitmap_reader.tpp
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 <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 }
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 }
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 }
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
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
00119 case 0 : output.next_line(); read_mode(input, output); break;
00120
00121 case 1 : this->m_mode = this->stop; break;
00122
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
00139 default: this->m_mode = this->raw; this->m_count = pattern; break;
00140 }
00141 }
00142 }
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
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 }