Claw 1.7.3
lzw_decoder.tpp
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*/
25/**
26 * \file lzw_decoder.tpp
27 * \brief Implementation of the claw::lzw_decoder class.
28 * \author Julien Jorge
29 */
30#include <list>
31
32/*----------------------------------------------------------------------------*/
33/**
34 * \brief Decode a sequence of LZW compressed datas.
35 * \param input Where we read the compressed datas.
36 * \param output Where we write uncompressed datas.
37 */
38template<typename InputBuffer, typename OutputBuffer>
39void claw::lzw_decoder<InputBuffer, OutputBuffer>::decode
40( input_buffer_type& input, output_buffer_type& output )
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/*----------------------------------------------------------------------------*/
78/**
79 * \brief Get the first symbol of a string, represented by a code.
80 * \param table The table of codes.
81 * \param code The code of the string from which we want the first symbol.
82 * \param symbols_count The count of atomic codes.
83 */
84template<typename InputBuffer, typename OutputBuffer>
85unsigned int claw::lzw_decoder<InputBuffer, OutputBuffer>::get_first_symbol
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/*----------------------------------------------------------------------------*/
98/**
99 * \brief Write a string, represented by a code, in the ouput buffer.
100 * \param table The table of codes.
101 * \param code The code of the string to write.
102 * \param symbols_count The count of atomic codes.
103 * \param output Where we write the uncompressed datas.
104 */
105template<typename InputBuffer, typename OutputBuffer>
106void claw::lzw_decoder<InputBuffer, OutputBuffer>::decompose
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()