xoreos  0.0.5
huffman.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "src/common/huffman.h"
28 #include "src/common/util.h"
29 #include "src/common/error.h"
30 #include "src/common/bitstream.h"
31 
32 namespace Common {
33 
34 Huffman::Symbol::Symbol(uint32 c, uint32 s) : code(c), symbol(s) {
35 }
36 
37 
39  init(table.maxLength, table.codeCount, table.codes, table.lengths, table.symbols);
40 }
41 
42 Huffman::Huffman(uint8 maxLength, size_t codeCount, const uint32 *codes,
43  const uint8 *lengths, const uint32 *symbols) {
44 
45  init(maxLength, codeCount, codes, lengths, symbols);
46 }
47 
48 void Huffman::init(uint8 maxLength, size_t codeCount, const uint32 *codes,
49  const uint8 *lengths, const uint32 *symbols) {
50 
51  assert(codeCount > 0);
52 
53  assert(codes);
54  assert(lengths);
55 
56  if (maxLength == 0)
57  for (size_t i = 0; i < codeCount; i++)
58  maxLength = MAX(maxLength, lengths[i]);
59 
60  assert(maxLength <= 32);
61 
62  _codes.resize(maxLength);
63  _symbols.resize(codeCount);
64 
65  for (size_t i = 0; i < codeCount; i++) {
66  // The symbol. If none were specified, just assume it's identical to the code index
67  uint32 symbol = symbols ? symbols[i] : i;
68 
69  // Put the code and symbol into the correct list
70  _codes[lengths[i] - 1].push_back(Symbol(codes[i], symbol));
71 
72  // And put the pointer to the symbol/code struct into the symbol list.
73  _symbols[i] = &_codes[lengths[i] - 1].back();
74  }
75 }
76 
78 }
79 
80 void Huffman::setSymbols(const uint32 *symbols) {
81  for (size_t i = 0; i < _symbols.size(); i++)
82  _symbols[i]->symbol = symbols ? *symbols++ : i;
83 }
84 
86  uint32 code = 0;
87 
88  for (size_t i = 0; i < _codes.size(); i++) {
89  bits.addBit(code, i);
90 
91  for (CodeList::const_iterator cCode = _codes[i].begin(); cCode != _codes[i].end(); ++cCode)
92  if (code == cCode->code)
93  return cCode->symbol;
94  }
95 
96  throw Exception("Unknown Huffman code");
97 }
98 
99 } // End of namespace Common
void setSymbols(const uint32 *symbols=0)
Modify the codes&#39; symbols.
Definition: huffman.cpp:80
Symbol(uint32 c, uint32 s)
Definition: huffman.cpp:34
Definition: 2dafile.h:39
uint8_t uint8
Definition: types.h:200
const uint8 * lengths
The lengths of the individual codes.
Definition: huffman.h:42
uint32 getSymbol(BitStream &bits) const
Return the next symbol in the bitstream.
Definition: huffman.cpp:85
Basic exceptions to throw.
Utility templates and functions.
uint8 maxLength
Maximal code length. If 0, it&#39;s searched for.
Definition: huffman.h:38
Huffman(uint8 maxLength, size_t codeCount, const uint32 *codes, const uint8 *lengths, const uint32 *symbols=0)
Construct a Huffman decoder.
Definition: huffman.cpp:42
StackException Exception
Definition: error.h:59
SymbolList _symbols
Sorted list of pointers to the symbols.
Definition: huffman.h:84
A bit stream.
const uint32 * symbols
The symbols, 0 if identical to the codes.
Definition: huffman.h:43
virtual void addBit(uint32 &x, size_t n)=0
Add a bit to the n-bit value x, making it an (n+1)-bit value.
Decompressing Huffman codes.
uint32_t uint32
Definition: types.h:204
CodeLists _codes
Lists of codes and their symbols, sorted by code length.
Definition: huffman.h:81
size_t codeCount
Number of codes.
Definition: huffman.h:39
const uint32 * codes
The actual codes.
Definition: huffman.h:41
T MAX(T a, T b)
Definition: util.h:71
void init(uint8 maxLength, size_t codeCount, const uint32 *codes, const uint8 *lengths, const uint32 *symbols)
Definition: huffman.cpp:48
A bit stream.
Definition: bitstream.h:40