Claw  1.7.3
lzw_decoder.tpp
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@gamned.org
24 */
30 #include <list>
31 
32 /*----------------------------------------------------------------------------*/
38 template<typename InputBuffer, typename OutputBuffer>
41 {
42  const unsigned int symbols_count = input.symbols_count();
43 
44  table_type table;
45  unsigned int table_size = 0;
46 
47  unsigned int prefix = input.get_next();
48 
49  if ( !input.end_of_data() )
50  {
51  while ( !input.end_of_data() )
52  {
53  unsigned int suffix = input.get_next();
54 
55  if (!input.end_of_data() )
56  {
57  unsigned int new_suffix;
58 
59  if ( suffix < table_size + symbols_count )
60  new_suffix = get_first_symbol(table, suffix, symbols_count);
61  else
62  new_suffix = get_first_symbol(table, prefix, symbols_count);
63 
64  table.push_back( word_type(prefix, new_suffix) );
65  ++table_size;
66  input.new_code(table_size + symbols_count);
67 
68  decompose( table, prefix, symbols_count, output );
69  prefix = suffix;
70  }
71  }
72 
73  decompose( table, prefix, symbols_count, output );
74  }
75 } // lzw_decoder::decode()
76 
77 /*----------------------------------------------------------------------------*/
84 template<typename InputBuffer, typename OutputBuffer>
86 ( const table_type& table, const unsigned int code,
87  const unsigned int symbols_count ) const
88 {
89  unsigned int result = code;
90 
91  while ( result >= symbols_count )
92  result = table[result - symbols_count].first;
93 
94  return result;
95 } // lzw_decoder::get_first_symbol()
96 
97 /*----------------------------------------------------------------------------*/
105 template<typename InputBuffer, typename OutputBuffer>
107 ( const table_type& table, unsigned int code,
108  const unsigned int symbols_count, output_buffer_type& output ) const
109 {
110  std::list<unsigned int> result;
111 
112  while ( code >= symbols_count )
113  {
114  result.push_front( table[code - symbols_count].second );
115  code = table[code - symbols_count].first;
116  }
117 
118  result.push_front(code);
119 
120  std::list<unsigned int>::const_iterator it;
121 
122  for (it=result.begin(); it!=result.end(); ++it)
123  output.write( *it );
124 
125 } // lzw_decoder::decompose()