Claw  1.7.3
bitmap_reader.tpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #include <cassert>
31 #include <algorithm>
32 #include <claw/exception.hpp>
33 
34 /*----------------------------------------------------------------------------*/
40 template< bool Coded4Bits >
41 claw::graphic::bitmap::reader::
42 rle_bitmap_output_buffer<Coded4Bits>::rle_bitmap_output_buffer
43 ( const color_palette_type& palette, image& img )
44  : m_palette(palette), m_image(img), m_x(0), m_y(m_image.height() - 1)
45 {
46 
47 } // bitmap::reader::rle_bitmap_output_buffer::rle_bitmap_output_buffer()
48 
49 /*----------------------------------------------------------------------------*/
53 template< bool Coded4Bits >
54 void claw::graphic::bitmap::reader::
55 rle_bitmap_output_buffer<Coded4Bits>::next_line()
56 {
57  assert( m_y > 0 );
58 
59  m_x = 0;
60  --m_y;
61 } // bitmap::reader::rle_bitmap_output_buffer::next_line()
62 
63 /*----------------------------------------------------------------------------*/
69 template< bool Coded4Bits >
70 void claw::graphic::bitmap::reader::
71 rle_bitmap_output_buffer<Coded4Bits>::delta_move
72 (unsigned char x, unsigned char y)
73 {
74  assert( m_x + x < m_image.width() );
75  assert( m_y + y < m_image.height() );
76 
77  m_x += x;
78  m_y += y;
79 } // bitmap::reader::rle_bitmap_output_buffer::delta_move()
80 
81 
82 //******************************************************************************
83 
84 
85 /*----------------------------------------------------------------------------*/
92 template< typename OutputBuffer >
93 void claw::graphic::bitmap::reader::rle_bitmap_decoder<OutputBuffer>::read_mode
94 ( file_input_buffer& input, output_buffer_type& output)
95 {
96  this->m_mode = this->stop;
97  bool ok = true;
98 
99  if ( input.remaining() < 2)
100  ok = input.read_more(2);
101 
102  if (ok)
103  {
104  unsigned char key, pattern;
105 
106  key = input.get_next();
107  pattern = input.get_next();
108 
109  // compressed data, next byte is the pattern
110  if (key > 0)
111  {
112  this->m_mode = this->compressed;
113  this->m_count = key;
114  this->m_pattern = pattern;
115  }
116  else switch( pattern )
117  {
118  // end of line
119  case 0 : output.next_line(); read_mode(input, output); break;
120  // end of file
121  case 1 : this->m_mode = this->stop; break;
122  // delta move
123  case 2 :
124  {
125  if ( input.remaining() < 1 )
126  ok = input.read_more(1);
127 
128  if (ok)
129  {
130  unsigned char x, y;
131  x = pattern;
132  y = input.get_next();
133  output.delta_move(x, y);
134  read_mode(input, output);
135  break;
136  }
137  }
138  // raw data
139  default: this->m_mode = this->raw; this->m_count = pattern; break;
140  }
141  }
142 } // bitmap::reader::rle_bitmap_decoder::read_mode()
143 
144 
145 /*----------------------------------------------------------------------------*/
159 template<typename Convert>
160 void claw::graphic::bitmap::reader::load_rgb_data
161 ( std::istream& f, unsigned int buffer_size, const color_palette_type& palette,
162  const Convert& pixel_convert )
163 {
164  unsigned int line;
165 
166  // lines are 4-bytes aligned, so adjust buffer's size.
167  if (buffer_size % 4 != 0)
168  buffer_size += 4 - buffer_size % 4;
169 
170  char* buffer = new char[buffer_size];
171 
172  for (line = m_image.height(); (line>0) && !f.eof(); )
173  {
174  --line;
175  f.read(buffer, buffer_size);
176  pixel_convert( m_image[line], buffer, palette );
177  }
178 
179  delete[] buffer;
180 
181  if ( f.rdstate() != std::ios_base::goodbit )
182  throw claw::bad_format("bitmap::reader::load_data");
183 } // bitmap::reader::load_data()