bitmap.hpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#ifndef __CLAW_BITMAP_HPP__
#define __CLAW_BITMAP_HPP__
#include <iostream>
#include <vector>
#include <claw/image.hpp>
#include <claw/rle_decoder.hpp>
#include <claw/color_palette.hpp>
#include <claw/buffered_istream.hpp>
namespace claw
{
namespace graphic
{
class bitmap : public image
{
private:
class file_structure
{
public:
typedef color_palette<rgba_pixel_8> color_palette_type;
enum compression
{
BMP_COMPRESSION_RGB = 0,
BMP_COMPRESSION_RLE8 = 1,
BMP_COMPRESSION_RLE4 = 2,
BMP_COMPRESSION_BITFIELDS = 3
};
# pragma pack (push,2)
struct header
{
char id[2];
unsigned int file_size;
unsigned int nop;
unsigned int data_offset;
unsigned int header_size;
unsigned int width;
unsigned int height;
unsigned short layers;
unsigned short bpp;
unsigned int compression;
unsigned int image_size;
unsigned int ppm_x;
unsigned int ppm_y;
unsigned int colors_count;
unsigned int importants_colors;
};
# pragma pack (pop)
}; // class file_structure
public:
/*----------------------------------------------------------------------*/
class reader : private file_structure
{
private:
typedef buffered_istream<std::istream> file_input_buffer;
template< bool Coded4bits >
class rle_bitmap_output_buffer
{
public:
rle_bitmap_output_buffer( const color_palette_type& palette,
image& image );
void fill( unsigned int n, unsigned char pattern );
void copy( unsigned int n, file_input_buffer& buffer );
void next_line();
void delta_move(unsigned char x, unsigned char y);
private:
const color_palette_type& m_palette;
image& m_image;
unsigned int m_x;
unsigned int m_y;
}; // class rle_bitmap_output_buffer
template< typename OutputBuffer >
class rle_bitmap_decoder
: public rle_decoder< char, file_input_buffer, OutputBuffer >
{
public:
typedef OutputBuffer output_buffer_type;
private:
virtual void read_mode( file_input_buffer& input,
output_buffer_type& output );
}; // class rle_bitmap_decoder
typedef
rle_bitmap_decoder< rle_bitmap_output_buffer<true> > rle4_decoder;
typedef rle_bitmap_decoder< rle_bitmap_output_buffer<false> >
rle8_decoder;
class pixel1_to_pixel32
{
public:
void operator()( scanline& dest, const char* src,
const color_palette_type& palette ) const;
}; // class pixel1_to_pixel32
class pixel4_to_pixel32
{
public:
void operator()( scanline& dest, const char* src,
const color_palette_type& palette ) const;
}; // class pixel4_to_pixel32
class pixel8_to_pixel32
{
public:
void operator()( scanline& dest, const char* src,
const color_palette_type& palette ) const;
}; // class pixel8_to_pixel32
class pixel24_to_pixel32
{
public:
void operator()( scanline& dest, const char* src,
const color_palette_type& palette ) const;
}; // class pixel24_to_pixel32
public:
reader( image& img );
reader( image& img, std::istream& f );
void load( std::istream& f );
private:
void load_palette( const header& h, std::istream& f,
color_palette_type& palette ) const;
void load_1bpp( const header& h, std::istream& f );
void load_4bpp( const header& h, std::istream& f );
void load_8bpp( const header& h, std::istream& f );
void load_24bpp( const header& h, std::istream& f );
void load_4bpp_rle( const header& h, std::istream& f,
const color_palette_type& palette );
void load_4bpp_rgb( const header& h, std::istream& f,
const color_palette_type& palette );
void load_8bpp_rle( const header& h, std::istream& f,
const color_palette_type& palette );
void load_8bpp_rgb( const header& h, std::istream& f,
const color_palette_type& palette );
template<typename Convert>
void load_rgb_data( std::istream& f, unsigned int buffer_size,
const color_palette_type& palette,
const Convert& pixel_convert );
private:
image& m_image;
}; // class reader
/*----------------------------------------------------------------------*/
class writer : private file_structure
{
public:
writer( const image& img );
writer( const image& img, std::ostream& f );
void save( std::ostream& f ) const;
private:
void save_data( std::ostream& f ) const;
void pixel32_to_pixel24( char* dest, const scanline& src ) const;
void init_header( header& h ) const;
private:
const image& m_image;
}; // class writer
public:
bitmap( unsigned int w, unsigned int h );
bitmap( const image& that );
bitmap( std::istream& f );
void save( std::ostream& f ) const;
}; // class bitmap
} // namespace graphic
} // namespace claw
#include <claw/impl/bitmap_reader.tpp>
#endif // __CLAW_BITMAP_HPP__
claw::graphic::image::scanline
One line in the image.
Definition: image.hpp:59
claw::graphic::bitmap
A class for bitmap images.
Definition: bitmap.hpp:48
claw::graphic::bitmap::reader
This class read data from a bitmap file and store it in an image.
Definition: bitmap.hpp:134
claw::graphic::bitmap::file_structure::header::width
unsigned int width
Image&#39;s width.
Definition: bitmap.hpp:94
claw::graphic::bitmap::file_structure::header::bpp
unsigned short bpp
Bits per pixel.
Definition: bitmap.hpp:103
claw::graphic::bitmap::file_structure::header::layers
unsigned short layers
Number of layers.
Definition: bitmap.hpp:100
claw::graphic::bitmap::writer
This class write an image in a bitmap file.
Definition: bitmap.hpp:296
color_palette.hpp
A palette of color, for palettized images.
claw::graphic::bitmap::file_structure::header::image_size
unsigned int image_size
Image&#39;s size (bytes).
Definition: bitmap.hpp:109
claw::graphic::image::fill
void fill(const math::rectangle< int > r, const pixel_type &c)
Fill an area of the image with a given color.
Definition: image.cpp:314
claw::buffered_istream< std::istream >
claw::graphic::image::load
void load(std::istream &f)
Read the image from a stream.
Definition: image.cpp:381
claw::rle_decoder
A class to help decoding run-length encoded (RLE) streams.
Definition: rle_decoder.hpp:54
claw::graphic::bitmap::save
void save(std::ostream &f) const
Save the bitmap in a file.
Definition: bitmap.cpp:71
claw::graphic::color_palette
A palette of colors, for palettized images.
Definition: color_palette.hpp:44
claw::graphic::bitmap::file_structure::header::header_size
unsigned int header_size
Header&#39;s size.
Definition: bitmap.hpp:91
claw::graphic::bitmap::file_structure::header
Header of a bitmap file.
Definition: bitmap.hpp:76
rle_decoder.hpp
A class to help decoding run-length encoded (RLE) streams.
buffered_istream.hpp
This class is made to help reading istreams with a buffer.
claw::graphic::bitmap::file_structure::header::nop
unsigned int nop
not used.
Definition: bitmap.hpp:85
claw::graphic::bitmap::file_structure::header::colors_count
unsigned int colors_count
Number of colors.
Definition: bitmap.hpp:118
claw::graphic::bitmap::file_structure::header::file_size
unsigned int file_size
File&#39;s size.
Definition: bitmap.hpp:82
claw::graphic::bitmap::file_structure::header::data_offset
unsigned int data_offset
Begininf of the datas.
Definition: bitmap.hpp:88
claw::graphic::bitmap::file_structure::header::importants_colors
unsigned int importants_colors
Number of important colors.
Definition: bitmap.hpp:121
claw::graphic::bitmap::bitmap
bitmap(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: bitmap.cpp:39
claw::graphic::bitmap::file_structure::header::ppm_y
unsigned int ppm_y
Vertical resolution (pixels per meter).
Definition: bitmap.hpp:115
claw::graphic::bitmap::file_structure::header::height
unsigned int height
Image&#39;s height.
Definition: bitmap.hpp:97
claw::graphic::image
A class to deal with images.
Definition: image.hpp:49
claw
This is the main namespace.
Definition: algorithm.hpp:33
claw::graphic::bitmap::file_structure::header::ppm_x
unsigned int ppm_x
Horizontal resolution (pixels per meter).
Definition: bitmap.hpp:112
claw::graphic::bitmap::file_structure::header::compression
unsigned int compression
Compression algorithm.
Definition: bitmap.hpp:106
image.hpp
A class to deal with images.