huffman.cpp

00001 /* -*- tab-width:4; c-basic-offset:4 -*- */
00002 /*
00003  * libopenraw - huffman.cpp
00004  *
00005  * Copyright (C) 2008 Rafael Avila de Espindola.
00006  *
00007  * This library is free software: you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation, either version 3 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library.  If not, see
00019  * <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 #include <string>
00023 #include <iostream>
00024 #include "huffman.h"
00025 
00026 namespace OpenRaw {
00027 namespace Internals {
00028 
00029 void HuffmanDecoder::printTable_(std::string prefix, unsigned int pos)  const
00030 {
00031     const HuffmanNode &cur = m_p[pos];
00032     if (cur.isLeaf) {
00033         std::cerr << prefix << " " << cur.data << "\n";
00034     } else {
00035         printTable_(prefix + "0", pos + 1);
00036         printTable_(prefix + "1", cur.data);
00037     }
00038 }
00039 
00040 HuffmanDecoder::HuffmanDecoder(const HuffmanNode* const p) : m_p(p)
00041 {
00042 }
00043 
00044 void HuffmanDecoder::printTable() const
00045 {
00046     printTable_("", 0);
00047 }
00048 
00049 unsigned int HuffmanDecoder::decode(BitIterator& i)
00050 {
00051     int cur = 0;
00052     while (!m_p[cur].isLeaf) {
00053         unsigned int bit = i.get(1);
00054         if (bit)
00055             cur = m_p[cur].data;
00056         else
00057             cur = cur + 1;
00058     }
00059     return m_p[cur].data;
00060 }
00061 
00062 }
00063 }

Generated by  doxygen 1.6.2