claw  1.9.0
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@stuff-o-matic.com
24 */
30 #ifndef __CLAW_BITMAP_HPP__
31 #define __CLAW_BITMAP_HPP__
32 
34 #include <claw/graphic/image.hpp>
35 
37 #include <claw/rle_decoder.hpp>
38 
39 #include <iostream>
40 #include <vector>
41 
42 namespace claw
43 {
44  namespace graphic
45  {
50  class bitmap : public image
51  {
52  private:
58  class file_structure
59  {
60  public:
62  typedef color_palette<rgba_pixel_8> color_palette_type;
63 
65  enum compression
66  {
67  BMP_COMPRESSION_RGB = 0,
68  BMP_COMPRESSION_RLE8 = 1,
69  BMP_COMPRESSION_RLE4 = 2,
70  BMP_COMPRESSION_BITFIELDS = 3
71  };
72 
73 #pragma pack(push, 2)
74 
78  struct header
79  {
81  char id[2];
82 
84  unsigned int file_size;
85 
87  unsigned int nop;
88 
90  unsigned int data_offset;
91 
93  unsigned int header_size;
94 
96  unsigned int width;
97 
99  unsigned int height;
100 
102  unsigned short layers;
103 
105  unsigned short bpp;
106 
108  unsigned int compression;
109 
111  unsigned int image_size;
112 
114  unsigned int ppm_x;
115 
117  unsigned int ppm_y;
118 
120  unsigned int colors_count;
121 
123  unsigned int importants_colors;
124  };
125 #pragma pack(pop)
126 
127  }; // class file_structure
128 
129  public:
135  class reader : private file_structure
136  {
137  private:
141 
152  template <bool Coded4bits>
153  class rle_bitmap_output_buffer
154  {
155  public:
156  rle_bitmap_output_buffer(const color_palette_type& palette,
157  image& image);
158 
159  void fill(unsigned int n, unsigned char pattern);
160  void copy(unsigned int n, file_input_buffer& buffer);
161 
162  void next_line();
163  void delta_move(unsigned char x, unsigned char y);
164 
165  private:
167  const color_palette_type& m_palette;
168 
170  image& m_image;
171 
173  unsigned int m_x;
174 
176  unsigned int m_y;
177 
178  }; // class rle_bitmap_output_buffer
179 
196  template <typename OutputBuffer>
197  class rle_bitmap_decoder
198  : public rle_decoder<char, file_input_buffer, OutputBuffer>
199  {
200  public:
202  typedef OutputBuffer output_buffer_type;
203 
204  private:
205  virtual void read_mode(file_input_buffer& input,
206  output_buffer_type& output);
207  }; // class rle_bitmap_decoder
208 
210  typedef rle_bitmap_decoder<rle_bitmap_output_buffer<true> >
211  rle4_decoder;
212 
214  typedef rle_bitmap_decoder<rle_bitmap_output_buffer<false> >
215  rle8_decoder;
216 
220  class pixel1_to_pixel32
221  {
222  public:
223  void operator()(scanline& dest, const char* src,
224  const color_palette_type& palette) const;
225  }; // class pixel1_to_pixel32
226 
230  class pixel4_to_pixel32
231  {
232  public:
233  void operator()(scanline& dest, const char* src,
234  const color_palette_type& palette) const;
235  }; // class pixel4_to_pixel32
236 
240  class pixel8_to_pixel32
241  {
242  public:
243  void operator()(scanline& dest, const char* src,
244  const color_palette_type& palette) const;
245  }; // class pixel8_to_pixel32
246 
250  class pixel24_to_pixel32
251  {
252  public:
253  void operator()(scanline& dest, const char* src,
254  const color_palette_type& palette) const;
255  }; // class pixel24_to_pixel32
256 
257  public:
258  reader(image& img);
259  reader(image& img, std::istream& f);
260 
261  void load(std::istream& f);
262 
263  private:
264  void load_palette(const header& h, std::istream& f,
265  color_palette_type& palette) const;
266 
267  void load_1bpp(const header& h, std::istream& f);
268  void load_4bpp(const header& h, std::istream& f);
269  void load_8bpp(const header& h, std::istream& f);
270  void load_24bpp(const header& h, std::istream& f);
271 
272  void load_4bpp_rle(const header& h, std::istream& f,
273  const color_palette_type& palette);
274  void load_4bpp_rgb(const header& h, std::istream& f,
275  const color_palette_type& palette);
276  void load_8bpp_rle(const header& h, std::istream& f,
277  const color_palette_type& palette);
278  void load_8bpp_rgb(const header& h, std::istream& f,
279  const color_palette_type& palette);
280 
281  template <typename Convert>
282  void load_rgb_data(std::istream& f, unsigned int buffer_size,
283  const color_palette_type& palette,
284  const Convert& pixel_convert);
285 
286  private:
288  image& m_image;
289 
290  }; // class reader
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  }
326 }
327 
328 #include <claw/graphic/bitmap_reader.tpp>
329 
330 #endif // __CLAW_BITMAP_HPP__
One line in the image.
Definition: image.hpp:61
A class for bitmap images.
Definition: bitmap.hpp:50
This class read data from a bitmap file and store it in an image.
Definition: bitmap.hpp:135
unsigned int width
Image&#39;s width.
Definition: bitmap.hpp:96
unsigned short bpp
Bits per pixel.
Definition: bitmap.hpp:105
reader(image &img)
Constructor.
unsigned short layers
Number of layers.
Definition: bitmap.hpp:102
This class write an image in a bitmap file.
Definition: bitmap.hpp:296
A palette of color, for palettized images.
unsigned int image_size
Image&#39;s size (bytes).
Definition: bitmap.hpp:111
void fill(const math::rectangle< int > r, const pixel_type &c)
Fill an area of the image with a given color.
Definition: image.cpp:286
void save(std::ostream &f) const
Save the bitmap in a file.
A class to help decoding run-length encoded (RLE) streams.
Definition: rle_decoder.hpp:54
void save(std::ostream &f) const
Save the bitmap in a file.
Definition: bitmap.cpp:63
void load(std::istream &f)
Load the image data from a stream.
A palette of colors, for palettized images.
unsigned int header_size
Header&#39;s size.
Definition: bitmap.hpp:93
A class to help decoding run-length encoded (RLE) streams.
This class is made to help reading istreams with a buffer.
unsigned int colors_count
Number of colors.
Definition: bitmap.hpp:120
unsigned int file_size
File&#39;s size.
Definition: bitmap.hpp:84
unsigned int data_offset
Begininf of the datas.
Definition: bitmap.hpp:90
unsigned int importants_colors
Number of important colors.
Definition: bitmap.hpp:123
bitmap(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: bitmap.cpp:38
unsigned int ppm_y
Vertical resolution (pixels per meter).
Definition: bitmap.hpp:117
unsigned int height
Image&#39;s height.
Definition: bitmap.hpp:99
writer(const image &img)
Constructor.
A class to deal with images.
Definition: image.hpp:50
This is the main namespace.
Definition: application.hpp:49
unsigned int ppm_x
Horizontal resolution (pixels per meter).
Definition: bitmap.hpp:114
unsigned int compression
Compression algorithm.
Definition: bitmap.hpp:108
A class to deal with images.