Claw  1.7.3
bitmap.hpp
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 #ifndef __CLAW_BITMAP_HPP__
31 #define __CLAW_BITMAP_HPP__
32 
33 #include <iostream>
34 #include <vector>
35 #include <claw/image.hpp>
36 #include <claw/rle_decoder.hpp>
37 #include <claw/color_palette.hpp>
39 
40 namespace claw
41 {
42  namespace graphic
43  {
48  class bitmap : public image
49  {
50  private:
56  class file_structure
57  {
58  public:
60  typedef color_palette<rgba_pixel_8> color_palette_type;
61 
63  enum compression
64  {
65  BMP_COMPRESSION_RGB = 0,
66  BMP_COMPRESSION_RLE8 = 1,
67  BMP_COMPRESSION_RLE4 = 2,
68  BMP_COMPRESSION_BITFIELDS = 3
69  };
70 
71 # pragma pack (push,2)
72 
76  struct header
77  {
79  char id[2];
80 
82  unsigned int file_size;
83 
85  unsigned int nop;
86 
88  unsigned int data_offset;
89 
91  unsigned int header_size;
92 
94  unsigned int width;
95 
97  unsigned int height;
98 
100  unsigned short layers;
101 
103  unsigned short bpp;
104 
106  unsigned int compression;
107 
109  unsigned int image_size;
110 
112  unsigned int ppm_x;
113 
115  unsigned int ppm_y;
116 
118  unsigned int colors_count;
119 
121  unsigned int importants_colors;
122  };
123 # pragma pack (pop)
124 
125  }; // class file_structure
126 
127  public:
128  /*----------------------------------------------------------------------*/
134  class reader : private file_structure
135  {
136  private:
140 
151  template< bool Coded4bits >
152  class rle_bitmap_output_buffer
153  {
154  public:
155  rle_bitmap_output_buffer( const color_palette_type& palette,
156  image& image );
157 
158  void fill( unsigned int n, unsigned char pattern );
159  void copy( unsigned int n, file_input_buffer& buffer );
160 
161  void next_line();
162  void delta_move(unsigned char x, unsigned char y);
163 
164  private:
166  const color_palette_type& m_palette;
167 
169  image& m_image;
170 
172  unsigned int m_x;
173 
175  unsigned int m_y;
176 
177  }; // class rle_bitmap_output_buffer
178 
195  template< typename OutputBuffer >
196  class rle_bitmap_decoder
197  : public rle_decoder< char, file_input_buffer, OutputBuffer >
198  {
199  public:
201  typedef OutputBuffer output_buffer_type;
202 
203  private:
204  virtual void read_mode( file_input_buffer& input,
205  output_buffer_type& output );
206  }; // class rle_bitmap_decoder
207 
209  typedef
210  rle_bitmap_decoder< rle_bitmap_output_buffer<true> > rle4_decoder;
211 
213  typedef rle_bitmap_decoder< rle_bitmap_output_buffer<false> >
214  rle8_decoder;
215 
219  class pixel1_to_pixel32
220  {
221  public:
222  void operator()( scanline& dest, const char* src,
223  const color_palette_type& palette ) const;
224  }; // class pixel1_to_pixel32
225 
229  class pixel4_to_pixel32
230  {
231  public:
232  void operator()( scanline& dest, const char* src,
233  const color_palette_type& palette ) const;
234  }; // class pixel4_to_pixel32
235 
239  class pixel8_to_pixel32
240  {
241  public:
242  void operator()( scanline& dest, const char* src,
243  const color_palette_type& palette ) const;
244  }; // class pixel8_to_pixel32
245 
249  class pixel24_to_pixel32
250  {
251  public:
252  void operator()( scanline& dest, const char* src,
253  const color_palette_type& palette ) const;
254  }; // class pixel24_to_pixel32
255 
256  public:
257  reader( image& img );
258  reader( image& img, std::istream& f );
259 
260  void load( std::istream& f );
261 
262  private:
263  void load_palette( const header& h, std::istream& f,
264  color_palette_type& palette ) const;
265 
266  void load_1bpp( const header& h, std::istream& f );
267  void load_4bpp( const header& h, std::istream& f );
268  void load_8bpp( const header& h, std::istream& f );
269  void load_24bpp( const header& h, std::istream& f );
270 
271  void load_4bpp_rle( const header& h, std::istream& f,
272  const color_palette_type& palette );
273  void load_4bpp_rgb( const header& h, std::istream& f,
274  const color_palette_type& palette );
275  void load_8bpp_rle( const header& h, std::istream& f,
276  const color_palette_type& palette );
277  void load_8bpp_rgb( const header& h, std::istream& f,
278  const color_palette_type& palette );
279 
280  template<typename Convert>
281  void load_rgb_data( std::istream& f, unsigned int buffer_size,
282  const color_palette_type& palette,
283  const Convert& pixel_convert );
284 
285  private:
287  image& m_image;
288 
289  }; // class reader
290 
291  /*----------------------------------------------------------------------*/
296  class writer : private file_structure
297  {
298  public:
299  writer( const image& img );
300  writer( const image& img, std::ostream& f );
301 
302  void save( std::ostream& f ) const;
303 
304  private:
305  void save_data( std::ostream& f ) const;
306 
307  void pixel32_to_pixel24( char* dest, const scanline& src ) const;
308 
309  void init_header( header& h ) const;
310 
311  private:
313  const image& m_image;
314 
315  }; // class writer
316 
317  public:
318  bitmap( unsigned int w, unsigned int h );
319  bitmap( const image& that );
320  bitmap( std::istream& f );
321 
322  void save( std::ostream& f ) const;
323  }; // class bitmap
324 
325  } // namespace graphic
326 } // namespace claw
327 
329 
330 #endif // __CLAW_BITMAP_HPP__