File: gif.cpp

package info (click to toggle)
libclaw 1.7.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 13,287; sh: 227; makefile: 8
file content (238 lines) | stat: -rw-r--r-- 6,781 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
  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
*/
/**
 * \file gif.cpp
 * \brief Implementation of the claw::graphic::gif class.
 * \author Julien Jorge
 */
#include "claw/gif.hpp"

#include <algorithm>
#include <claw/functional.hpp>

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if the file contains a global palette.
 */
bool claw::graphic::gif::screen_descriptor::has_global_color_table() const
{
  return (packed & 0x80) != 0;
} // gif::screen_descriptor::has_global_color_table()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the size of the palette, if any.
 */
unsigned int claw::graphic::gif::screen_descriptor::color_palette_size() const
{
  if ( !has_global_color_table() )
    return 0;
  else
    return 1 << ((packed & 0x07) + 1);
} // gif::screen_descriptor::color_palette_size()




/*----------------------------------------------------------------------------*/
/**
 * \brief Get the disposal method of the frame.
 */
claw::graphic::gif::graphic_control_extension::disposal_method
claw::graphic::gif::graphic_control_extension::get_disposal_method() const
{
  switch( (packed & 0x1C) >> 2 )
    {
    case 0: return dispose_none;
    case 1: return dispose_do_not_dispose;
    case 2: return dispose_background;
    case 3: return dispose_previous;
    default:
      return dispose_previous;
    }
} // gif::graphic_control_extension::get_disposal_method()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if the palette contains a transparent color.
 */
bool
claw::graphic::gif::graphic_control_extension::has_transparent_color() const
{
  return (packed & 0x01) != 0;
} // gif::graphic_control_extension::has_transparent_color()




/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if the file contains a global palette.
 */
bool claw::graphic::gif::image_descriptor::has_color_table() const
{
  return (packed & 0x80) != 0;
} // gif::image_descriptor::has_color_table()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if the data is interlaced.
 */
bool claw::graphic::gif::image_descriptor::is_interlaced() const
{
  return (packed & 0x40) != 0;
} // gif::image_descriptor::is_interlaced()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the size of the palette, if any.
 */
unsigned int claw::graphic::gif::image_descriptor::color_palette_size() const
{
  if ( !has_color_table() )
    return 0;
  else
    return 1 << ((packed & 0x07) + 1);
} // gif::image_descriptor::color_palette_size()




/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 */
claw::graphic::gif::gif()
{

} // gif::gif() [copy constructor]

/*----------------------------------------------------------------------------*/
/**
 * \brief Copy constructor.
 * \param that Image to copy from.
 */
claw::graphic::gif::gif( const gif& that )
  : image(that)
{
  frame_list::const_iterator it;

  for (it=that.m_frame.begin(); it!=that.m_frame.end(); ++it)
    m_frame.push_back( new frame(**it) );
} // gif::gif() [copy constructor]

/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor. Load an image from a gif file.
 * \param f Gif file.
 */
claw::graphic::gif::gif( std::istream& f )
{
  reader(*this, m_frame, f);
} // gif::gif() [constructor, from file]

/*----------------------------------------------------------------------------*/
/**
 * \brief Destructor.
 */
claw::graphic::gif::~gif()
{
  std::for_each
    ( m_frame.begin(), m_frame.end(), claw::delete_function<frame*>() );
  m_frame.clear();
} // gif::~gif()

/*----------------------------------------------------------------------------*/
/**
 * \brief Assignment.
 * \param that The gif to copy from.
 */
claw::graphic::gif& claw::graphic::gif::operator=( const gif& that )
{
  gif tmp(that);
  std::swap(tmp, *this);
  return *this;
} // gif::operator=()

/*----------------------------------------------------------------------------*/
/**
 * \brief Swap the content of two gifs.
 * \param that The gif to swap with.
 */
void claw::graphic::gif::swap( gif& that )
{
  super::swap(that);
  std::swap(m_frame, that.m_frame);
} // gif::swap()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get an iterator on the beginning of the frame sequence.
 */
claw::graphic::gif::frame_iterator claw::graphic::gif::frame_begin()
{
  return frame_iterator(m_frame.begin());
} // gif::begin()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get an iterator on the end of the frame sequence.
 */
claw::graphic::gif::frame_iterator claw::graphic::gif::frame_end()
{
  return frame_iterator(m_frame.end());
} // gif::end()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get an iterator on the beginning of the frame sequence.
 */
claw::graphic::gif::const_frame_iterator claw::graphic::gif::frame_begin() const
{
  return const_frame_iterator(m_frame.begin());
} // gif::begin()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get an iterator on the beginning of the frame sequence.
 */
claw::graphic::gif::const_frame_iterator claw::graphic::gif::frame_end() const
{
  return const_frame_iterator(m_frame.end());
} // gif::end()




/*----------------------------------------------------------------------------*/
/**
 * \brief Swap the content of two gifs.
 * \param a The gif to swap with \a b.
 * \param b The gif to swap with \a a.
 */
void std::swap( claw::graphic::gif& a, claw::graphic::gif& b )
{
  a.swap(b);
} // swap()