claw  1.9.0
gif.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_GIF_HPP__
31 #define __CLAW_GIF_HPP__
32 
34 #include <claw/graphic/image.hpp>
35 
36 #include <claw/functional.hpp>
37 #include <claw/iterator.hpp>
38 #include <claw/lzw_decoder.hpp>
39 #include <claw/types.hpp>
40 
41 #include <list>
42 
43 namespace claw
44 {
45  namespace graphic
46  {
51  class gif : public image
52  {
53  public:
55  class frame : public image
56  {
57  public:
59  typedef image super;
60 
61  public:
62  frame();
63  frame(std::size_t w, std::size_t h);
64 
65  void set_delay(unsigned int d);
66  unsigned int get_delay() const;
67 
68  private:
71  unsigned int m_delay;
72 
73  }; // class frame
74 
75  private:
77  typedef std::list<frame*> frame_list;
78 
80  typedef image super;
81 
82  public:
84  typedef wrapped_iterator<frame, frame_list::iterator,
85  claw::dereference<frame> >::iterator_type
87 
89  typedef wrapped_iterator<const frame, frame_list::const_iterator,
90  claw::const_dereference<frame> >::iterator_type
92 
93  private:
94 #pragma pack(push, 1)
95 
97  struct header
98  {
100  u_int_8 signature[3];
101 
103  u_int_8 version[3];
104 
105  }; // struct header
106 
109  struct screen_descriptor
110  {
111  public:
112  bool has_global_color_table() const;
113  unsigned int color_palette_size() const;
114 
115  public:
117  u_int_16 screen_width;
118 
120  u_int_16 screen_height;
121 
123  u_int_8 packed;
124 
126  u_int_8 background_color;
127 
129  u_int_8 aspect_ratio;
130 
131  }; // struct screen_descriptor
132 
134  struct image_descriptor
135  {
136  public:
138  static const u_int_8 block_id = 0x2C;
139 
140  public:
141  bool has_color_table() const;
142  bool is_interlaced() const;
143  unsigned int color_palette_size() const;
144 
145  public:
147  u_int_16 left;
148 
150  u_int_16 top;
151 
153  u_int_16 width;
154 
157 
159  u_int_8 packed;
160 
161  }; // struct image_descriptor
162 
164  struct extension
165  {
167  static const u_int_8 block_id = 0x21;
168 
169  // no field
170  }; // struct extension
171 
173  struct trailer
174  {
176  static const u_int_8 block_id = 0x3B;
177 
178  // no field
179  }; // trailer
180 
182  struct graphic_control_extension
183  {
184  public:
186  static const u_int_8 block_label = 0xF9;
187 
190  enum disposal_method
191  {
194  dispose_none,
195 
197  dispose_do_not_dispose,
198 
201  dispose_background,
202 
206  dispose_previous
207 
208  }; // enum disposal_method
209 
210  public:
211  disposal_method get_disposal_method() const;
212  bool has_transparent_color() const;
213 
214  public:
216  u_int_8 block_size;
217 
219  u_int_8 packed;
220 
222  u_int_16 delay;
223 
225  u_int_8 transparent_color;
226 
228  u_int_8 terminator;
229 
230  }; // struct graphic_control_extension
231 
233  struct comment_extension
234  {
235  public:
237  static const u_int_8 block_label = 0xFE;
238 
239  public:
240  // this block is ignored.
241 
242  }; // struct comment_extension
243 
245  struct plain_text_extension
246  {
247  public:
249  static const u_int_8 block_label = 0x01;
250 
251  public:
252  // this block is ignored.
253 
254  }; // struct plain_text_extension
255 
257  struct application_extension
258  {
259  public:
261  static const u_int_8 block_label = 0xFF;
262 
263  public:
264  // this block is ignored.
265 
266  }; // struct application_extension
267 #pragma pack(pop)
268 
269  public:
276  class reader
277  {
278  private:
281 
283  struct reader_info
284  {
286  screen_descriptor sd;
287 
289  palette_type* palette;
290 
293  int transparent_color_index;
294 
296  std::vector<graphic_control_extension::disposal_method>
297  disposal_method;
298 
299  }; // struct reader_info
300 
302  class input_buffer
303  {
304  public:
305  input_buffer(std::istream& is, u_int_8 code_size);
306 
307  bool end_of_data() const;
308  bool end_of_information() const;
309  unsigned int symbols_count() const;
310  unsigned int get_next();
311 
312  void reset();
313  void new_code(unsigned int code);
314 
315  private:
316  void fill_buffer();
317 
318  private:
320  unsigned int m_val;
321 
323  std::istream& m_input;
324 
331  char m_buffer[257];
332 
334  std::size_t m_pending;
335 
337  unsigned char m_pending_bits;
338 
340  std::size_t m_pending_end;
341 
343  u_int_8 m_next_data_length;
344 
346  const unsigned int m_initial_code_size;
347 
349  unsigned int m_code_size;
350 
352  unsigned int m_code_limit;
353 
354  }; // class input_buffer
355 
357  class output_buffer
358  {
359  public:
360  output_buffer(const palette_type& p, const image_descriptor& id,
361  int transparent_color_index, image& output);
362 
363  void write(unsigned int code);
364 
365  private:
367  const palette_type& m_palette;
368 
370  const image_descriptor& m_id;
371 
373  const int m_transparent_color_index;
374 
376  image& m_output;
377 
379  std::size_t m_x;
380 
382  std::size_t m_y;
383 
385  int m_interlace_pass;
386 
388  int m_interlace_step;
389 
390  }; // class output_buffer
391 
394 
395  public:
396  reader(image& img);
397  reader(image& img, std::istream& f);
398  reader(frame_list& frames, std::istream& f);
399  reader(image& img, frame_list& frames, std::istream& f);
400  ~reader();
401 
402  void load(std::istream& f);
403 
404  private:
405  void clear();
406  void inside_load(std::istream& f);
407  void make_frames(const reader_info& info);
408  void fill_background(image& img, const reader_info& info) const;
409 
410  void check_if_gif(std::istream& f) const;
411  void read_screen_descriptor(std::istream& f, reader_info& info);
412 
413  void read_palette(std::istream& f, palette_type& p) const;
414  void read_data(std::istream& f, reader_info& info);
415  void read_frame(std::istream& f, reader_info& info);
416  void read_frame_with_gce(std::istream& f, reader_info& info);
417 
418  void skip_extension(std::istream& f) const;
419  void read_frame_data(std::istream& f, const reader_info& info,
420  frame& the_frame) const;
421 
422  void decode_data(std::istream& f, const palette_type& palette,
423  const image_descriptor& id,
424  int transparent_color_index, frame& the_frame) const;
425 
426  private:
428  image* m_image;
429 
431  frame_list m_frame;
432 
433  }; // class reader
434 
435  public:
436  gif();
437  gif(const gif& that);
438  gif(std::istream& f);
439  ~gif();
440 
441  gif& operator=(const gif& that);
442  void swap(gif& that);
443 
448 
449  private:
451  frame_list m_frame;
452 
453  }; // class gif
454  }
455 }
456 
457 namespace std
458 {
459  void swap(claw::graphic::gif& a, claw::graphic::gif& b);
460 }
461 
462 #endif // __CLAW_GIF_HPP__
This class reads data from a gif file. The image is resized to the size of the screen (as defined in ...
Definition: gif.hpp:276
gif & operator=(const gif &that)
Assignment.
Definition: gif.cpp:154
void load(std::istream &f)
Load the image data from a stream.
Definition: gif_reader.cpp:332
One frame in the animation.
Definition: gif.hpp:55
unsigned int get_delay() const
Get the time duration of this frame.
Definition: gif_frame.cpp:61
~gif()
Destructor.
Definition: gif.cpp:143
unsigned int height() const
Gets image&#39;s height.
Definition: image.cpp:141
image super
The type of the parent class.
Definition: gif.hpp:59
unsigned_integer_of_size< 8 >::type u_int_8
An unsigned integer on 8 bits.
Definition: types.hpp:132
Some classes for the raw manipulation of the base types.
A class to help decoding a stream encoded with Lempel-Ziv-Welch (LZW) compression algorithm...
Definition: lzw_decoder.hpp:62
A palette of color, for palettized images.
frame_iterator frame_begin()
Get an iterator on the beginning of the frame sequence.
Definition: gif.cpp:174
frame_iterator frame_end()
Get an iterator on the end of the frame sequence.
Definition: gif.cpp:182
frame()
Constructor.
Definition: gif_frame.cpp:35
A class to help decoding a stream encoded with Lempel-Ziv-Welch (LZW) compression algorithm...
This class defines an iterator resulting of the appliance of a function to an effective iterator...
Definition: iterator.hpp:449
Base class for wrapped iterators.
Definition: iterator.hpp:43
unsigned_integer_of_size< 16 >::type u_int_16
An unsigned integer on 16 bits.
Definition: types.hpp:135
A palette of colors, for palettized images.
Function object that dereferences a pointer.
Definition: functional.hpp:318
unsigned int width() const
Gets image&#39;s width.
Definition: image.cpp:130
A class for gif pictures.
Definition: gif.hpp:51
reader(image &img)
Constructor.
Definition: gif_reader.cpp:271
Function object that dereferences a constant pointer.
Definition: functional.hpp:341
gif()
Constructor.
Definition: gif.cpp:115
void set_delay(unsigned int d)
Set the time duration of this frame.
Definition: gif_frame.cpp:53
wrapped_iterator< const frame, frame_list::const_iterator, claw::const_dereference< frame > >::iterator_type const_frame_iterator
Iterator on the content of the gif.
Definition: gif.hpp:91
A class to deal with images.
Definition: image.hpp:50
Some special kind of iterators. As an example: iterator on the keys of a map.
void swap(gif &that)
Swap the content of two gifs.
Definition: gif.cpp:165
This is the main namespace.
Definition: application.hpp:49
wrapped_iterator< frame, frame_list::iterator, claw::dereference< frame > >::iterator_type frame_iterator
Iterator on the content of the gif.
Definition: gif.hpp:86
Some function object classes.
A class to deal with images.