gif.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_GIF_HPP__
#define __CLAW_GIF_HPP__
#include <claw/color_palette.hpp>
#include <claw/functional.hpp>
#include <claw/image.hpp>
#include <claw/iterator.hpp>
#include <claw/lzw_decoder.hpp>
#include <claw/types.hpp>
#include <list>
namespace claw
{
namespace graphic
{
class gif:
public image
{
public:
class frame:
public image
{
public:
typedef image super;
public:
frame();
frame( std::size_t w, std::size_t h );
void set_delay(unsigned int d);
unsigned int get_delay() const;
private:
unsigned int m_delay;
}; // class frame
private:
typedef std::list<frame*> frame_list;
typedef image super;
public:
typedef wrapped_iterator
< frame,
frame_list::iterator,
claw::dereference<frame> >::iterator_type frame_iterator;
typedef wrapped_iterator
< const frame,
frame_list::const_iterator,
claw::const_dereference<frame> >::iterator_type const_frame_iterator;
private:
#pragma pack(push, 1)
struct header
{
u_int_8 signature[3];
u_int_8 version[3];
}; // struct header
struct screen_descriptor
{
public:
bool has_global_color_table() const;
unsigned int color_palette_size() const;
public:
u_int_16 screen_width;
u_int_16 screen_height;
u_int_8 packed;
u_int_8 background_color;
u_int_8 aspect_ratio;
}; // struct screen_descriptor
struct image_descriptor
{
public:
static const u_int_8 block_id = 0x2C;
public:
bool has_color_table() const;
bool is_interlaced() const;
unsigned int color_palette_size() const;
public:
u_int_16 left;
u_int_16 top;
u_int_16 width;
u_int_16 height;
u_int_8 packed;
}; // struct image_descriptor
struct extension
{
static const u_int_8 block_id = 0x21;
// no field
}; // struct extension
struct trailer
{
static const u_int_8 block_id = 0x3B;
// no field
}; // trailer
struct graphic_control_extension
{
public:
static const u_int_8 block_label = 0xF9;
enum disposal_method
{
dispose_none,
dispose_do_not_dispose,
dispose_background,
dispose_previous
}; // enum disposal_method
public:
disposal_method get_disposal_method() const;
bool has_transparent_color() const;
public:
u_int_8 block_size;
u_int_8 packed;
u_int_16 delay;
u_int_8 transparent_color;
u_int_8 terminator;
}; // struct graphic_control_extension
struct comment_extension
{
public:
static const u_int_8 block_label = 0xFE;
public:
// this block is ignored.
}; // struct comment_extension
struct plain_text_extension
{
public:
static const u_int_8 block_label = 0x01;
public:
// this block is ignored.
}; // struct plain_text_extension
struct application_extension
{
public:
static const u_int_8 block_label = 0xFF;
public:
// this block is ignored.
}; // struct application_extension
#pragma pack(pop)
public:
/*----------------------------------------------------------------------*/
class reader
{
private:
typedef color_palette<rgb_pixel> palette_type;
struct reader_info
{
screen_descriptor sd;
palette_type* palette;
int transparent_color_index;
std::vector<graphic_control_extension::disposal_method>
disposal_method;
}; // struct reader_info
class input_buffer
{
public:
input_buffer( std::istream& is, u_int_8 code_size );
bool end_of_data() const;
bool end_of_information() const;
unsigned int symbols_count() const;
unsigned int get_next();
void reset();
void new_code( unsigned int code );
private:
void fill_buffer();
private:
unsigned int m_val;
std::istream& m_input;
char m_buffer[257];
std::size_t m_pending;
unsigned char m_pending_bits;
std::size_t m_pending_end;
u_int_8 m_next_data_length;
const unsigned int m_initial_code_size;
unsigned int m_code_size;
unsigned int m_code_limit;
}; // class input_buffer
class output_buffer
{
public:
output_buffer
( const palette_type& p, const image_descriptor& id,
int transparent_color_index, image& output );
void write( unsigned int code );
private:
const palette_type& m_palette;
const image_descriptor& m_id;
const int m_transparent_color_index;
image& m_output;
std::size_t m_x;
std::size_t m_y;
int m_interlace_pass;
int m_interlace_step;
}; // class output_buffer
typedef claw::lzw_decoder<input_buffer, output_buffer> gif_lzw_decoder;
public:
reader( image& img );
reader( image& img, std::istream& f );
reader( frame_list& frames, std::istream& f );
reader( image& img, frame_list& frames, std::istream& f );
~reader();
void load( std::istream& f );
private:
void clear();
void inside_load( std::istream& f );
void make_frames( const reader_info& info );
void fill_background( image& img, const reader_info& info ) const;
void check_if_gif( std::istream& f ) const;
void read_screen_descriptor( std::istream& f, reader_info& info );
void read_palette( std::istream& f, palette_type& p ) const;
void read_data( std::istream& f, reader_info& info );
void read_frame( std::istream& f, reader_info& info );
void read_frame_with_gce( std::istream& f, reader_info& info );
void skip_extension( std::istream& f ) const;
void read_frame_data
( std::istream& f, const reader_info& info, frame& the_frame ) const;
void decode_data
( std::istream& f, const palette_type& palette,
const image_descriptor& id, int transparent_color_index,
frame& the_frame ) const;
private:
image* m_image;
frame_list m_frame;
}; // class reader
public:
gif();
gif( const gif& that );
gif( std::istream& f );
~gif();
gif& operator=( const gif& that );
void swap( gif& that );
frame_iterator frame_begin();
frame_iterator frame_end();
const_frame_iterator frame_begin() const;
const_frame_iterator frame_end() const;
private:
frame_list m_frame;
}; // class gif
} // namespace graphic
} // namespace claw
namespace std
{
void swap( claw::graphic::gif& a, claw::graphic::gif& b );
} // namespace std
#endif // __CLAW_GIF_HPP__
claw::const_dereference
Function object that dereferences a constant pointer.
Definition functional.hpp:351
claw::dereference
Function object that dereferences a pointer.
Definition functional.hpp:327
claw::graphic::color_palette
A palette of colors, for palettized images.
Definition color_palette.hpp:45
claw::graphic::gif::frame
One frame in the animation.
Definition gif.hpp:57
claw::graphic::gif::frame::get_delay
unsigned int get_delay() const
Get the time duration of this frame.
Definition gif_frame.cpp:68
claw::graphic::gif::frame::super
image super
The type of the parent class.
Definition gif.hpp:60
claw::graphic::gif::frame::frame
frame()
Constructor.
Definition gif_frame.cpp:36
claw::graphic::gif::frame::set_delay
void set_delay(unsigned int d)
Set the time duration of this frame.
Definition gif_frame.cpp:59
claw::graphic::gif::reader::reader
reader(image &img)
Constructor.
Definition gif_reader.cpp:272
claw::graphic::gif::reader::~reader
~reader()
Destructor.
Definition gif_reader.cpp:331
claw::graphic::gif::reader::load
void load(std::istream &f)
Load the image data from a stream.
Definition gif_reader.cpp:341
claw::graphic::gif
A class for gif pictures.
Definition gif.hpp:52
claw::graphic::gif::frame_end
frame_iterator frame_end()
Get an iterator on the end of the frame sequence.
Definition gif.cpp:203
claw::graphic::gif::~gif
~gif()
Destructor.
Definition gif.cpp:160
claw::graphic::gif::swap
void swap(gif &that)
Swap the content of two gifs.
Definition gif.cpp:184
claw::graphic::gif::operator=
gif & operator=(const gif &that)
Assignment.
Definition gif.cpp:172
claw::graphic::gif::frame_iterator
wrapped_iterator< frame, frame_list::iterator, claw::dereference< frame > >::iterator_type frame_iterator
Iterator on the content of the gif.
Definition gif.hpp:88
claw::graphic::gif::const_frame_iterator
wrapped_iterator< constframe, frame_list::const_iterator, claw::const_dereference< frame > >::iterator_type const_frame_iterator
Iterator on the content of the gif.
Definition gif.hpp:94
claw::graphic::gif::gif
gif()
Constructor.
Definition gif.cpp:127
claw::graphic::gif::frame_begin
frame_iterator frame_begin()
Get an iterator on the beginning of the frame sequence.
Definition gif.cpp:194
claw::graphic::image::height
unsigned int height() const
Gets image's height.
Definition image.cpp:159
claw::graphic::image::width
unsigned int width() const
Gets image's width.
Definition image.cpp:147
claw::graphic::image::image
image()
Constructor. Creates an image without datas.
Definition image.cpp:106
claw::lzw_decoder
A class to help decoding a stream encoded with Lempel-Ziv-Welch (LZW) compression algorithm.
Definition lzw_decoder.hpp:62
claw::wrapped_iterator
This class defines an iterator resulting of the appliance of a function to an effective iterator.
Definition iterator.hpp:362
color_palette.hpp
A palette of color, for palettized images.
functional.hpp
Some function object classes.
image.hpp
A class to deal with images.
iterator.hpp
Some special kind of iterators. As an example: iterator on the keys of a map.
lzw_decoder.hpp
A class to help decoding a stream encoded with Lempel-Ziv-Welch (LZW) compression algorithm.
claw::graphic
Everything about image structures and processing.
Definition bitmap.hpp:43
claw
This is the main namespace.
Definition algorithm.hpp:34
claw::u_int_16
unsigned_integer_of_size< 16 >::type u_int_16
An unsigned integer on 16 bits.
Definition types.hpp:138
claw::u_int_8
unsigned_integer_of_size< 8 >::type u_int_8
An unsigned integer on 8 bits.
Definition types.hpp:135
types.hpp
Some classes for the raw manipulation of the base types.