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__
buffered_istream.hpp
This class is made to help reading istreams with a buffer.
claw::buffered_istream
This class is made to help reading istreams with a buffer.
Definition buffered_istream.hpp:43
claw::graphic::bitmap::reader::reader
reader(image &img)
Constructor.
Definition bitmap_reader.cpp:287
claw::graphic::bitmap::reader::load
void load(std::istream &f)
Load the image data from a stream.
Definition bitmap_reader.cpp:312
claw::graphic::bitmap::writer::writer
writer(const image &img)
Constructor.
Definition bitmap_writer.cpp:38
claw::graphic::bitmap::writer::save
void save(std::ostream &f) const
Save the bitmap in a file.
Definition bitmap_writer.cpp:61
claw::graphic::bitmap::save
void save(std::ostream &f) const
Save the bitmap in a file.
Definition bitmap.cpp:71
claw::graphic::bitmap::bitmap
bitmap(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition bitmap.cpp:39
claw::graphic::color_palette
A palette of colors, for palettized images.
Definition color_palette.hpp:45
claw::graphic::image::scanline
One line in the image.
Definition image.hpp:61
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::graphic::image::image
image()
Constructor. Creates an image without datas.
Definition image.cpp:106
claw::rle_decoder
A class to help decoding run-length encoded (RLE) streams.
Definition rle_decoder.hpp:55
claw::rle_decoder< char, file_input_buffer, OutputBuffer >::output_buffer_type
OutputBuffer output_buffer_type
Definition rle_decoder.hpp:64
color_palette.hpp
A palette of color, for palettized images.
image.hpp
A class to deal with images.
claw::graphic
Everything about image structures and processing.
Definition bitmap.hpp:43
claw::pattern
Here are the design patterns.
Definition basic_singleton.hpp:38
claw
This is the main namespace.
Definition algorithm.hpp:34
rle_decoder.hpp
A class to help decoding run-length encoded (RLE) streams.
claw::graphic::bitmap::file_structure::header
Header of a bitmap file.
Definition bitmap.hpp:77
claw::graphic::bitmap::file_structure::header::bpp
unsigned short bpp
Bits per pixel.
Definition bitmap.hpp:103
claw::graphic::bitmap::file_structure::header::width
unsigned int width
Image's width.
Definition bitmap.hpp:94
claw::graphic::bitmap::file_structure::header::image_size
unsigned int image_size
Image's size (bytes).
Definition bitmap.hpp:109
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::ppm_x
unsigned int ppm_x
Horizontal resolution (pixels per meter).
Definition bitmap.hpp:112
claw::graphic::bitmap::file_structure::header::importants_colors
unsigned int importants_colors
Number of important colors.
Definition bitmap.hpp:121
claw::graphic::bitmap::file_structure::header::nop
unsigned int nop
not used.
Definition bitmap.hpp:85
claw::graphic::bitmap::file_structure::header::file_size
unsigned int file_size
File's size.
Definition bitmap.hpp:82
claw::graphic::bitmap::file_structure::header::height
unsigned int height
Image's height.
Definition bitmap.hpp:97
claw::graphic::bitmap::file_structure::header::layers
unsigned short layers
Number of layers.
Definition bitmap.hpp:100
claw::graphic::bitmap::file_structure::header::header_size
unsigned int header_size
Header's size.
Definition bitmap.hpp:91
claw::graphic::bitmap::file_structure::header::compression
unsigned int compression
Compression algorithm.
Definition bitmap.hpp:106
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::colors_count
unsigned int colors_count
Number of colors.
Definition bitmap.hpp:118