public Dictionary <HFCode, T> GetReadingTable() { if (readingTable == null) { readingTable = new Dictionary <HFCode, T>(); root.left.AddToReadingTable(HFCode.GetStartingCode(false), readingTable); root.right.AddToReadingTable(HFCode.GetStartingCode(true), readingTable); } return(readingTable); }
public Dictionary <T, HFCode> GetWritingTable() { if (writingTable == null) { writingTable = new Dictionary <T, HFCode>(); root.left.AddToWritingTable(HFCode.GetStartingCode(false), writingTable); root.right.AddToWritingTable(HFCode.GetStartingCode(true), writingTable); } return(writingTable); }
public void Read(byte[] cBytes, byte[] mBytes, byte[] yBytes, FileStream fStream) { //Reading Dictionary Length byte[] dictionaryHeader = new byte[2]; fStream.Read(dictionaryHeader, 0, 2); short dictionaryLength = BitConverter.ToInt16(dictionaryHeader, 0); Dictionary <HFCode, byte> readTable = new Dictionary <HFCode, byte>(); //Reading Values and Code Lengths byte[] valueAndCodeLength = new byte[3 * dictionaryLength]; fStream.Read(valueAndCodeLength, 0, 3 * dictionaryLength); //Reading Values and Codes BitStream bitStream = new BitStream(fStream); for (int i = 0; i < dictionaryLength; ++i) { byte value = valueAndCodeLength[3 * i]; ushort codeLength = BitConverter.ToUInt16(valueAndCodeLength, 3 * i + 1); BitArray bits = bitStream.ReadBits(codeLength); HFCode code = HFCode.FromBits(bits); readTable.Add(code, value); } //Reconstruct tree HuffmanTree <byte> hfTree = HuffmanTree <byte> .ReconstructFromTable(readTable); //hfTree.PrintTree(); //Read values to bytes int bufferSize = 128; BitArray buffer = bitStream.ReadBits(bufferSize); int bufferIndex = 0; for (int i = 0; i < cBytes.Length; ++i) { HuffmanTreeNode curNode = hfTree.root; while (!(curNode is HuffmanLeafNode <byte>)) { if (bufferIndex == buffer.Length) { buffer = bitStream.ReadBits(buffer.Length); bufferIndex = 0; } curNode = (buffer[bufferIndex]) ? curNode.right : curNode.left; bufferIndex++; } cBytes[i] = ((HuffmanLeafNode <byte>)curNode).info; } for (int i = 0; i < mBytes.Length; ++i) { HuffmanTreeNode curNode = hfTree.root; while (!(curNode is HuffmanLeafNode <byte>)) { if (bufferIndex == buffer.Length) { buffer = bitStream.ReadBits(buffer.Length); bufferIndex = 0; } curNode = (buffer[bufferIndex]) ? curNode.right : curNode.left; bufferIndex++; } mBytes[i] = ((HuffmanLeafNode <byte>)curNode).info; } for (int i = 0; i < yBytes.Length; ++i) { HuffmanTreeNode curNode = hfTree.root; while (!(curNode is HuffmanLeafNode <byte>)) { if (bufferIndex == buffer.Length) { buffer = bitStream.ReadBits(buffer.Length); bufferIndex = 0; } curNode = (buffer[bufferIndex]) ? curNode.right : curNode.left; bufferIndex++; } yBytes[i] = ((HuffmanLeafNode <byte>)curNode).info; } fStream.Close(); }
public void Read(byte[] cBytes, byte[] mBytes, byte[] yBytes, FileStream fStream) { //Reading Dictionary Length //C byte[] dictionaryHeader = new byte[2]; fStream.Read(dictionaryHeader, 0, 2); short cDictLength = BitConverter.ToInt16(dictionaryHeader, 0); Dictionary <HFCode, byte> cReadTable = new Dictionary <HFCode, byte>(); //Reading Values and Code Lengths byte[] cValueAndCodeLength = new byte[3 * cDictLength]; fStream.Read(cValueAndCodeLength, 0, 3 * cDictLength); //M dictionaryHeader = new byte[2]; fStream.Read(dictionaryHeader, 0, 2); short mDictLength = BitConverter.ToInt16(dictionaryHeader, 0); Dictionary <HFCode, byte> mReadTable = new Dictionary <HFCode, byte>(); //Reading Values and Code Lengths byte[] mValueAndCodeLength = new byte[3 * mDictLength]; fStream.Read(mValueAndCodeLength, 0, 3 * mDictLength); //M dictionaryHeader = new byte[2]; fStream.Read(dictionaryHeader, 0, 2); short yDictLength = BitConverter.ToInt16(dictionaryHeader, 0); Dictionary <HFCode, byte> yReadTable = new Dictionary <HFCode, byte>(); //Reading Values and Code Lengths byte[] yValueAndCodeLength = new byte[3 * yDictLength]; fStream.Read(yValueAndCodeLength, 0, 3 * yDictLength); //Reading Values and Codes BitStream bitStream = new BitStream(fStream); //C for (int i = 0; i < cDictLength; ++i) { byte value = cValueAndCodeLength[3 * i]; ushort codeLength = BitConverter.ToUInt16(cValueAndCodeLength, 3 * i + 1); BitArray bits = bitStream.ReadBits(codeLength); HFCode code = HFCode.FromBits(bits); cReadTable.Add(code, value); } //M for (int i = 0; i < mDictLength; ++i) { byte value = mValueAndCodeLength[3 * i]; ushort codeLength = BitConverter.ToUInt16(mValueAndCodeLength, 3 * i + 1); BitArray bits = bitStream.ReadBits(codeLength); HFCode code = HFCode.FromBits(bits); mReadTable.Add(code, value); } //Y for (int i = 0; i < yDictLength; ++i) { byte value = yValueAndCodeLength[3 * i]; ushort codeLength = BitConverter.ToUInt16(yValueAndCodeLength, 3 * i + 1); BitArray bits = bitStream.ReadBits(codeLength); HFCode code = HFCode.FromBits(bits); yReadTable.Add(code, value); //Console.WriteLine($"{value}:{code.length}:{code.GetStringCode()}"); } //Reconstruct tree //C HuffmanTree <byte> cTree = HuffmanTree <byte> .ReconstructFromTable(cReadTable); HuffmanTree <byte> mTree = HuffmanTree <byte> .ReconstructFromTable(mReadTable); HuffmanTree <byte> yTree = HuffmanTree <byte> .ReconstructFromTable(yReadTable); //Read values to bytes int bufferSize = 128; BitArray buffer = bitStream.ReadBits(bufferSize); int bufferIndex = 0; for (int i = 0; i < cBytes.Length; ++i) { HuffmanTreeNode curNode = cTree.root; while (!(curNode is HuffmanLeafNode <byte>)) { if (bufferIndex == buffer.Length) { buffer = bitStream.ReadBits(buffer.Length); bufferIndex = 0; } curNode = (buffer[bufferIndex]) ? curNode.right : curNode.left; bufferIndex++; } cBytes[i] = ((HuffmanLeafNode <byte>)curNode).info; } for (int i = 0; i < mBytes.Length; ++i) { HuffmanTreeNode curNode = mTree.root; while (!(curNode is HuffmanLeafNode <byte>)) { if (bufferIndex == buffer.Length) { buffer = bitStream.ReadBits(buffer.Length); bufferIndex = 0; } curNode = (buffer[bufferIndex]) ? curNode.right : curNode.left; bufferIndex++; } mBytes[i] = ((HuffmanLeafNode <byte>)curNode).info; } for (int i = 0; i < yBytes.Length; ++i) { HuffmanTreeNode curNode = yTree.root; while (!(curNode is HuffmanLeafNode <byte>)) { if (bufferIndex == buffer.Length) { buffer = bitStream.ReadBits(buffer.Length); bufferIndex = 0; } curNode = (buffer[bufferIndex]) ? curNode.right : curNode.left; bufferIndex++; } yBytes[i] = ((HuffmanLeafNode <byte>)curNode).info; } fStream.Close(); }
public override void AddToWritingTable <T>(HFCode inCode, Dictionary <T, HFCode> table) { T val = (T)(object)info; table.Add(val, inCode); }
public void PrintTree() { root.left.PrintNode(HFCode.GetStartingCode(false)); root.right.PrintNode(HFCode.GetStartingCode(true)); }
public override void AddToReadingTable <T>(HFCode inCode, Dictionary <HFCode, T> table) { table.Add(inCode, (T)(object)info); }
public override void PrintNode(HFCode code) { Console.WriteLine($"{info}:{code.length}:{code.GetStringCode()}:{frequency.ToString()}"); //base.PrintNode(inCode); }
public virtual void AddToWritingTable <T>(HFCode inCode, Dictionary <T, HFCode> table) { left.AddToWritingTable(inCode.GetLeft(), table); right.AddToWritingTable(inCode.GetRight(), table); }
public virtual void AddToReadingTable <T>(HFCode inCode, Dictionary <HFCode, T> table) { left.AddToReadingTable(inCode.GetLeft(), table); right.AddToReadingTable(inCode.GetRight(), table); }
public virtual void PrintNode(HFCode code) { left.PrintNode(code.GetLeft()); right.PrintNode(code.GetRight()); }