2
CLAW - a C++ Library Absolutely Wonderful
4
CLAW is a free library without any particular aim but being useful to
7
Copyright (C) 2005-2008 Julien Jorge
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.
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.
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
23
contact: julien_jorge@yahoo.fr
27
* \brief A class for bitmap pictures.
28
* \author Julien Jorge
30
#ifndef __CLAW_BITMAP_HPP__
31
#define __CLAW_BITMAP_HPP__
35
#include <claw/image.hpp>
36
#include <claw/rle_decoder.hpp>
37
#include <claw/color_palette.hpp>
38
#include <claw/buffered_istream.hpp>
45
* \brief A class for bitmap images.
46
* \author Julien Jorge
48
class bitmap : public image
52
* \brief Tool class used for defining the structures of the datas stored
54
* \author Julien Jorge
59
/** \brief The type of the color palette for low color image files. */
60
typedef color_palette<pixel32> color_palette_type;
62
/** \brief Compression mode. */
65
BMP_COMPRESSION_RGB = 0,
66
BMP_COMPRESSION_RLE8 = 1,
67
BMP_COMPRESSION_RLE4 = 2,
68
BMP_COMPRESSION_BITFIELDS = 3
71
# pragma pack (push,2)
74
* \brief Header of a bitmap file.
78
/** \brief File identifier (must be 'BM'). */
81
/** \brief File's size. */
82
unsigned int file_size;
84
/** \brief not used. */
87
/** \brief Begininf of the datas. */
88
unsigned int data_offset;
90
/** \brief Header's size. */
91
unsigned int header_size;
93
/** \brief Image's width. */
96
/** \brief Image's height. */
99
/** \brief Number of layers. */
100
unsigned short layers;
102
/** \brief Bits per pixel. */
105
/** \brief Compression algorithm. */
106
unsigned int compression;
108
/** \brief Image's size (bytes). */
109
unsigned int image_size;
111
/** \brief Horizontal resolution (pixels per meter). */
114
/** \brief Vertical resolution (pixels per meter). */
117
/** \brief Number of colors. */
118
unsigned int colors_count;
120
/** \brief Number of important colors. */
121
unsigned int importants_colors;
125
}; // class file_structure
128
/*----------------------------------------------------------------------*/
130
* \brief This class read data from a bitmap file and store it in an
132
* \author Julien Jorge
134
class reader : private file_structure
137
/** \brief The type of the input buffer associated with the file when
138
decoding RLE files. */
139
typedef buffered_istream<std::istream> file_input_buffer;
142
* \brief The output buffer for the RLE decoder.
144
* \b Template \b parameters
145
* - Coded4bits, true is the RLE patterns are coded in four bits ;
146
* otherwise the patterns are supposed to be on eight
149
* \author Julien Jorge
151
template< bool Coded4bits >
152
class rle_bitmap_output_buffer
155
rle_bitmap_output_buffer( const color_palette_type& palette,
158
void fill( unsigned int n, unsigned char pattern );
159
void copy( unsigned int n, file_input_buffer& buffer );
162
void delta_move(unsigned char x, unsigned char y);
165
/** \brief Color palette. */
166
const color_palette_type& m_palette;
168
/** \brief The image to fill. */
171
/** \brief Current column index in the bitmap. */
174
/** \brief Current row index in the bitmap. */
177
}; // class rle_bitmap_output_buffer
180
* \brief RLE decoder for bitmap RLE format
182
* \b Template \b parameters :
183
* - \a OutputBuffer The type of the output buffer.
185
* The \a OutputBuffer type must match the type requirements of the
186
* template parameter OutputBuffer of the rle_decoder class, plus two
188
* - next_line(), set the output position at the begining of the next
190
* - delta_move( unsigned char x, unsigned char y ), move the output
191
* position \a x pixels right and \a y pixels down.
193
* \author Julien Jorge
195
template< typename OutputBuffer >
196
class rle_bitmap_decoder
197
: public rle_decoder< char, file_input_buffer, OutputBuffer >
200
/** \brief Type of the output buffer. */
201
typedef OutputBuffer output_buffer_type;
204
virtual void read_mode( file_input_buffer& input,
205
output_buffer_type& output );
206
}; // class rle_bitmap_decoder
208
/** \brief RLE decoder for 4 bpp bitmap images. */
210
rle_bitmap_decoder< rle_bitmap_output_buffer<true> > rle4_decoder;
212
/** \brief RLE decoder for 8 bpp bitmap images. */
213
typedef rle_bitmap_decoder< rle_bitmap_output_buffer<false> >
217
* \brief Functor converting a 1bpp buffer to a 32bpp buffer.
219
class pixel1_to_pixel32
222
void operator()( scanline& dest, const char* src,
223
const color_palette_type& palette ) const;
224
}; // class pixel1_to_pixel32
227
* \brief Functor converting a 4bpp buffer to a 32bpp buffer.
229
class pixel4_to_pixel32
232
void operator()( scanline& dest, const char* src,
233
const color_palette_type& palette ) const;
234
}; // class pixel4_to_pixel32
237
* \brief Functor converting a 8bpp buffer to a 32bpp buffer.
239
class pixel8_to_pixel32
242
void operator()( scanline& dest, const char* src,
243
const color_palette_type& palette ) const;
244
}; // class pixel8_to_pixel32
247
* \brief Functor converting a 24bpp buffer to a 32bpp buffer.
249
class pixel24_to_pixel32
252
void operator()( scanline& dest, const char* src,
253
const color_palette_type& palette ) const;
254
}; // class pixel24_to_pixel32
257
reader( image& img );
258
reader( image& img, std::istream& f );
260
void load( std::istream& f );
263
void load_palette( const header& h, std::istream& f,
264
color_palette_type& palette ) const;
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 );
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 );
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 );
286
/** \brief The image in which we store the data we read. */
291
/*----------------------------------------------------------------------*/
293
* \brief This class write an image in a bitmap file.
294
* \author Julien Jorge
296
class writer : private file_structure
299
writer( const image& img );
300
writer( const image& img, std::ostream& f );
302
void save( std::ostream& f ) const;
305
void save_data( std::ostream& f ) const;
307
void pixel32_to_pixel24( char* dest, const scanline& src ) const;
309
void init_header( header& h ) const;
312
/** \brief The image from which we read the data. */
313
const image& m_image;
318
bitmap( unsigned int w, unsigned int h );
319
bitmap( const image& that );
320
bitmap( std::istream& f );
322
void save( std::ostream& f ) const;
325
} // namespace graphic
328
#include <claw/impl/bitmap_reader.tpp>
330
#endif // __CLAW_BITMAP_HPP__