Public Member Functions | Private Member Functions | Private Attributes

claw::graphic::gif::reader::input_buffer Class Reference

The buffer passed to the LZW decoder to decode the input. More...

List of all members.

Public Member Functions

 input_buffer (std::istream &is, u_int_8 code_size)
 Constructor.
bool end_of_data () const
 Tell if we reached the end of the data and we must restart the decompression procedure.
bool end_of_information () const
 Tell if we reached the end of the compressed stream.
unsigned int symbols_count () const
 Get The count of symbols in the alphabet.
unsigned int get_next ()
 Get the next code in the input.
void reset ()
 Reset the code size and the code limit.
void new_code (unsigned int code)
 Inform the input buffer that a new code has been added in the dictionary.

Private Member Functions

void fill_buffer ()
 Fill the input buffer.

Private Attributes

unsigned int m_val
 The last value read from the input.
std::istream & m_input
 The input data.
char m_buffer [257]
 Encoded data read from the stream.
std::size_t m_pending
 The position of the next byte to read in m_buffer.
unsigned char m_pending_bits
 The number of available bits in m_buffer[m_pending].
std::size_t m_pending_end
 The end of the data in m_buffer;.
u_int_8 m_next_data_length
 The length of the next data block.
const unsigned int m_initial_code_size
 The initial size of the codes.
unsigned int m_code_size
 The current size of the codes.
unsigned int m_code_limit
 The maximum code allowed with the current code size.

Detailed Description

The buffer passed to the LZW decoder to decode the input.

Definition at line 304 of file gif.hpp.


Constructor & Destructor Documentation

claw::graphic::gif::reader::input_buffer::input_buffer ( std::istream &  is,
u_int_8  code_size 
)

Constructor.

Parameters:
isThe stream from which we read the data.
code_sizeThe initial size of the codes.

Definition at line 46 of file gif_reader.cpp.

  : m_val(0), m_input(is), m_pending(0), m_pending_bits(0), m_pending_end(0),
    m_initial_code_size(code_size), m_code_size(m_initial_code_size+1),
    m_code_limit(1 << m_code_size)
{
  m_input.read
    ( reinterpret_cast<char*>(&m_next_data_length),
      sizeof(m_next_data_length) );
} // gif::reader::input_buffer::input_buffer()

Member Function Documentation

bool claw::graphic::gif::reader::input_buffer::end_of_data (  ) const

Tell if we reached the end of the data and we must restart the decompression procedure.

Definition at line 61 of file gif_reader.cpp.

References end_of_information(), m_initial_code_size, and m_val.

{
  return (m_val == (unsigned int)(1 << m_initial_code_size))
    || end_of_information();
} // gif::reader::input_buffer::end_of_data()
bool claw::graphic::gif::reader::input_buffer::end_of_information (  ) const

Tell if we reached the end of the compressed stream.

Definition at line 71 of file gif_reader.cpp.

Referenced by claw::graphic::gif::reader::decode_data(), and end_of_data().

{
  return !m_input || (m_val == (unsigned int)(1 << m_initial_code_size)+1)
    || ( (m_next_data_length == 0)             // no more data in the stream
         && (m_pending == m_pending_end)       // no more data in the buffer
         && (m_pending_bits < m_code_size) );
} // gif::reader::input_buffer::end_of_information()
void claw::graphic::gif::reader::input_buffer::fill_buffer (  ) [private]

Fill the input buffer.

Definition at line 177 of file gif_reader.cpp.

{
  // move available data at the begining of the buffer
  std::copy( m_buffer + m_pending, m_buffer + m_pending_end, m_buffer );
  m_pending_end = m_pending_end - m_pending;
  m_pending = 0;

  if (m_next_data_length != 0)
    {
      assert( m_pending_end + m_next_data_length <= sizeof(m_buffer) );

      m_input.read( m_buffer + m_pending_end, m_next_data_length );
      m_pending_end += m_next_data_length;

      if ( (m_pending_bits == 0) && (m_pending != m_pending_end) )
        m_pending_bits = CHAR_BIT;

      m_input.read
        ( reinterpret_cast<char*>(&m_next_data_length),
          sizeof(m_next_data_length) );
    }
} // gif::reader::input_buffer::fill_buffer()
unsigned int claw::graphic::gif::reader::input_buffer::get_next (  )

Get the next code in the input.

Definition at line 92 of file gif_reader.cpp.

{
  if ( m_pending == m_pending_end )
    fill_buffer();
  else if ( m_pending_bits + (m_pending_end - m_pending - 1) * CHAR_BIT
            < m_code_size )
    fill_buffer();

  m_val = 0;

  std::size_t n(m_code_size);
  unsigned int cur_size = 0;
  char* buf = reinterpret_cast<char*>(&m_val);

  while ( (n != 0) && m_input )
    {
      while( (m_pending_bits != 0) && (n!=0) && m_input )
        {
          unsigned int bits = std::min((std::size_t)m_pending_bits, n);

          if ( CHAR_BIT - cur_size < bits )
            bits = CHAR_BIT - cur_size;

          unsigned int mask = (1 << bits) - 1;

          *buf |= (m_buffer[m_pending] & mask) << cur_size;
          cur_size += bits;
          m_pending_bits -= bits;
          m_buffer[m_pending] >>= bits;
          n -= bits;

          if ( cur_size == CHAR_BIT )
            {
              ++buf;
              cur_size = 0;
            }
        }

      if ( m_pending_bits == 0 )
        {
          ++m_pending;

          if ( (m_pending == m_pending_end) && (n!=0) )
            fill_buffer();
          
          if ( m_pending == m_pending_end )
            n = 0;
          else
            m_pending_bits = CHAR_BIT;
        }
    }

  return m_val;
} // gif::reader::input_buffer::get_next()
void claw::graphic::gif::reader::input_buffer::new_code ( unsigned int  code )

Inform the input buffer that a new code has been added in the dictionary.

Parameters:
codeThe new code.

Definition at line 164 of file gif_reader.cpp.

{
  if ( (code == m_code_limit) && (m_code_size != 12) )
    {
      ++m_code_size;
      m_code_limit = 1 << m_code_size;
    }
} // gif::reader::input_buffer::new_code()
void claw::graphic::gif::reader::input_buffer::reset (  )

Reset the code size and the code limit.

Definition at line 151 of file gif_reader.cpp.

Referenced by claw::graphic::gif::reader::decode_data().

{
  m_val = 0;
  m_code_size = m_initial_code_size+1;
  m_code_limit = 1 << m_code_size;
} // gif::reader::input_buffer::reset()
unsigned int claw::graphic::gif::reader::input_buffer::symbols_count (  ) const

Get The count of symbols in the alphabet.

Definition at line 83 of file gif_reader.cpp.

{
  return (1 << m_initial_code_size) + 2;
} // gif::reader::input_buffer::symbols_count()

Member Data Documentation

Encoded data read from the stream.

A data block is 255 bytes long, maximum. But we may have to keep up to 12 bits from the previous block. So we need two more bytes.

Definition at line 333 of file gif.hpp.

The maximum code allowed with the current code size.

Definition at line 354 of file gif.hpp.

The current size of the codes.

Definition at line 351 of file gif.hpp.

The initial size of the codes.

Definition at line 348 of file gif.hpp.

Referenced by end_of_data().

The input data.

Definition at line 325 of file gif.hpp.

The length of the next data block.

Definition at line 345 of file gif.hpp.

The position of the next byte to read in m_buffer.

Definition at line 336 of file gif.hpp.

The number of available bits in m_buffer[m_pending].

Definition at line 339 of file gif.hpp.

The end of the data in m_buffer;.

Definition at line 342 of file gif.hpp.

The last value read from the input.

Definition at line 322 of file gif.hpp.

Referenced by end_of_data().


The documentation for this class was generated from the following files: