Octane v1.01.20 - The Open Compression Toolkit for C++ http://octane.sourceforge.net/
Homepage | Main | Modules | Class Hierarchy | Compound List | File List | Compound Members | Related Pages

coders/coder_huffman/huffmannodes.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 //---------------------------------------------------------------------------
00012 
00013 //---------------------------------------------------------------------------
00014 // application includes
00015 #include "huffmannodes.hpp"
00016 // system includes
00017 using namespace std;
00018 //---------------------------------------------------------------------------
00019 
00020 
00021 
00022 //---------------------------------------------------------------------------
00023 // The traverse_buildbitcodes member function is recursively compute bitcodes for each leaf; it will code entire tree if called on the root.
00024 void HuffNode::traverse_buildbitcodes(bitset<DEF_HUFFMANPQ_MAXFUNCBITS> &current_bitset, int bitsetlength)
00025 {
00026         // only the derived versions of this virtual function should ever be called.
00027         cout << " error -> HuffNode::traverse_buildbitcodes should not be called." << endl;
00028 }
00029 
00030 void HuffNode_Middle::traverse_buildbitcodes(bitset<DEF_HUFFMANPQ_MAXFUNCBITS> &current_bitset, int bitsetlength)
00031 {
00032         // traverse_buildbitcodes left tree
00033         if (bitsetlength>DEF_HUFFMANPQ_MAXIMUM_CODEBITDEPTH)
00034                 {
00035                 // we allow for the possibility that some initially built symbols will have a bitdepth which is too long for the dictionary
00036                 //  such symbols must be removed prior during pruning (note we record above the real bitdepth, but here only copy the limit)
00037                 // we comment this out here (the warning will still be shown when listing symbolset)
00038                 bitsetlength=DEF_HUFFMANPQ_MAXIMUM_CODEBITDEPTH;
00039                 }
00040         current_bitset[bitsetlength]=false;
00041         child0->traverse_buildbitcodes(current_bitset,bitsetlength+1);
00042         // traverse_buildbitcodes right tree
00043         current_bitset[bitsetlength]=true;
00044         child1->traverse_buildbitcodes(current_bitset,bitsetlength+1);
00045 }
00046 
00047 void HuffNode_Leaf::traverse_buildbitcodes(bitset<DEF_HUFFMANPQ_MAXFUNCBITS> &current_bitset, int bitsetlength)
00048 {
00049         // set bits used to code this symbol (the DEF_HUFFMANPQ_MAXFUNCBITS is just an insanely large number of bits which doesn't affect memory usage)
00050         int count, length;
00051         // first store the bitset in the leaf
00052         code_bitsetlength=bitsetlength;
00053         length=bitsetlength;
00054         if (length>DEF_HUFFMANPQ_MAXIMUM_CODEBITDEPTH)
00055                 {
00056                 // we allow for the possibility that some initially built symbols will have a bitdepth which is too long for the dictionary
00057                 //  such symbols must be removed prior during pruning (note we record above the real bitdepth, but here only copy the limit)
00058                 // we comment this out here (the warning will still be shown when listing symbolset)
00059                 length=DEF_HUFFMANPQ_MAXIMUM_CODEBITDEPTH;
00060                 }
00061         // copy the bitset
00062         for (count=0;count<length;++count)
00063                 code_bitset[count]=current_bitset[count];
00064 }
00065 //---------------------------------------------------------------------------
00066 
00067 
00068 //---------------------------------------------------------------------------
00069 string HuffNode_Leaf::get_bitcode_string()
00070 {
00071         // return the bitcode in nice string, for debugging
00072         string bitset_string;
00073         int count;
00074 
00075         // don't try to read more than we actually stored
00076         int length=code_bitsetlength;
00077         if (length>DEF_HUFFMANPQ_MAXIMUM_CODEBITDEPTH)
00078                 length=DEF_HUFFMANPQ_MAXIMUM_CODEBITDEPTH;
00079 
00080         for (count=0;count<length;++count)
00081                 {
00082                 if (code_bitset[count]==false)
00083                         bitset_string+="0";
00084                 else
00085                         bitset_string+="1";
00086                 }
00087 
00088         // add a note if its truncated (and thus unusable)
00089         if (code_bitsetlength>length)
00090                 {
00091                 char dummystr[80];
00092                 sprintf(dummystr," (only %d of %d bits stored; symbol unuseable).",length,code_bitsetlength);
00093                 bitset_string+=dummystr;
00094                 }
00095 
00096         // return bits as string
00097         return bitset_string;
00098 }
00099 //---------------------------------------------------------------------------
00100 
00101 
00102 //---------------------------------------------------------------------------
00103 bool HuffNode_Leaf::AddBitsToBitWriter(bitwriter &bw)
00104 {
00105         // push the bits from this code into the bitset
00106         // protect against too many bits
00107         int length=code_bitsetlength;
00108         if (length>DEF_HUFFMANPQ_MAXIMUM_CODEBITDEPTH)
00109                 {
00110                 // this is an error - it means we get a symbol whose bitcode we didn't store fully
00111                 return false;
00112                 }
00113 
00114         // add bits
00115         for (int count=0;count<length;++count)
00116                 bw.put_bit(code_bitset[count]);
00117                 
00118         // return success
00119         return true;
00120 }
00121 //---------------------------------------------------------------------------
 
Generated on 20 May 2004 by doxygen 1.3.3