CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
Public Types | Public Member Functions | Private Types | Private Member Functions

claw::lzw_decoder< InputBuffer, OutputBuffer > Class Template Reference

A class to help decoding a stream encoded with Lempel-Ziv-Welch (LZW) compression algorithm. More...

#include <lzw_decoder.hpp>

List of all members.

Public Types

typedef InputBuffer input_buffer_type
 The type of the input buffer.
typedef OutputBuffer output_buffer_type
 The type of the output buffer.

Public Member Functions

void decode (input_buffer_type &input, output_buffer_type &output)
 Decode a sequence of LZW compressed datas.

Private Types

typedef std::pair< unsigned
int, unsigned int > 
word_type
typedef std::vector< word_typetable_type

Private Member Functions

unsigned int get_first_symbol (const table_type &table, const unsigned int code, const unsigned int symbols_count) const
 Get the first symbol of a string, represented by a code.
void decompose (const table_type &table, unsigned int code, const unsigned int symbols_count, output_buffer_type &output) const
 Write a string, represented by a code, in the ouput buffer.

Detailed Description

template<typename InputBuffer, typename OutputBuffer>
class claw::lzw_decoder< InputBuffer, OutputBuffer >

A class to help decoding a stream encoded with Lempel-Ziv-Welch (LZW) compression algorithm.

Template parameters:

The InputBuffer type must have the following methods:

The OutputBuffer type must have the following methods:

Author:
Julien Jorge

Definition at line 61 of file lzw_decoder.hpp.


Member Typedef Documentation

template<typename InputBuffer , typename OutputBuffer >
typedef InputBuffer claw::lzw_decoder< InputBuffer, OutputBuffer >::input_buffer_type

The type of the input buffer.

Definition at line 65 of file lzw_decoder.hpp.

template<typename InputBuffer , typename OutputBuffer >
typedef OutputBuffer claw::lzw_decoder< InputBuffer, OutputBuffer >::output_buffer_type

The type of the output buffer.

Definition at line 68 of file lzw_decoder.hpp.

template<typename InputBuffer , typename OutputBuffer >
typedef std::vector<word_type> claw::lzw_decoder< InputBuffer, OutputBuffer >::table_type [private]

Definition at line 72 of file lzw_decoder.hpp.

template<typename InputBuffer , typename OutputBuffer >
typedef std::pair<unsigned int, unsigned int> claw::lzw_decoder< InputBuffer, OutputBuffer >::word_type [private]

Definition at line 71 of file lzw_decoder.hpp.


Member Function Documentation

template<typename InputBuffer , typename OutputBuffer >
void claw::lzw_decoder< InputBuffer, OutputBuffer >::decode ( input_buffer_type input,
output_buffer_type output 
)

Decode a sequence of LZW compressed datas.

Parameters:
inputWhere we read the compressed datas.
outputWhere we write uncompressed datas.

Definition at line 40 of file lzw_decoder.tpp.

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

{
  const unsigned int symbols_count = input.symbols_count();

  table_type table;
  unsigned int table_size = 0;

  unsigned int prefix = input.get_next();

  if ( !input.end_of_data() )
    {
      while ( !input.end_of_data() )
        {
          unsigned int suffix = input.get_next();

          if (!input.end_of_data() )
            {
              unsigned int new_suffix;

              if ( suffix < table_size + symbols_count )
                new_suffix = get_first_symbol(table, suffix, symbols_count);
              else
                new_suffix = get_first_symbol(table, prefix, symbols_count);

              table.push_back( word_type(prefix, new_suffix) );
              ++table_size;
              input.new_code(table_size + symbols_count);

              decompose( table, prefix, symbols_count, output );
              prefix = suffix;
            }
        }

      decompose( table, prefix, symbols_count, output );
    }
} // lzw_decoder::decode()
template<typename InputBuffer , typename OutputBuffer >
void claw::lzw_decoder< InputBuffer, OutputBuffer >::decompose ( const table_type table,
unsigned int  code,
const unsigned int  symbols_count,
output_buffer_type output 
) const [private]

Write a string, represented by a code, in the ouput buffer.

Parameters:
tableThe table of codes.
codeThe code of the string to write.
symbols_countThe count of atomic codes.
outputWhere we write the uncompressed datas.

Definition at line 107 of file lzw_decoder.tpp.

{
  std::list<unsigned int> result;

  while ( code >= symbols_count )
    {
      result.push_front( table[code - symbols_count].second );
      code = table[code - symbols_count].first;
    }

  result.push_front(code);

  std::list<unsigned int>::const_iterator it;

  for (it=result.begin(); it!=result.end(); ++it)
    output.write( *it );

} // lzw_decoder::decompose()
template<typename InputBuffer , typename OutputBuffer >
unsigned int claw::lzw_decoder< InputBuffer, OutputBuffer >::get_first_symbol ( const table_type table,
const unsigned int  code,
const unsigned int  symbols_count 
) const [private]

Get the first symbol of a string, represented by a code.

Parameters:
tableThe table of codes.
codeThe code of the string from which we want the first symbol.
symbols_countThe count of atomic codes.

Definition at line 86 of file lzw_decoder.tpp.

{
  unsigned int result = code;

  while ( result >= symbols_count )
    result = table[result - symbols_count].first;

  return result;
} // lzw_decoder::get_first_symbol()

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