/// <summary> /// Reads a sequence of bits that represents a Huffman-compressed message from /// standard input; expands them; and writes the results to standard output.</summary> /// public void Expand() { // read in Huffman trie from input stream Node root = readTrie(); // number of bytes to write int length = input.ReadInt(); // decode using the Huffman trie for (int i = 0; i < length; i++) { Node x = root; while (!x.IsLeaf) { bool bit = input.ReadBoolean(); if (bit) { x = x.right; } else { x = x.left; } } output.Write(x.ch, 8); } output.Close(); }
/// <summary> /// Reads a sequence of bits from standard input; compresses /// them using run-length coding with 8-bit run lengths; and writes the /// results to standard output.</summary> /// public void Compress() { int run = 0; bool old = false; while (!input.IsEmpty) { bool b = input.ReadBoolean(); if (b != old) { output.Write(run, LG_R); run = 1; old = !old; } else { if (run == R - 1) { output.Write(run, LG_R); run = 0; output.Write(run, LG_R); } run++; } } output.Write(run, LG_R); output.Close(); }
public static void MainTest(string[] args) { int bitsPerLine = 16; if (args.Length == 1) { bitsPerLine = int.Parse(args[0]); } BinaryInput input = new BinaryInput(); int count; for (count = 0; !input.IsEmpty; count++) { if (bitsPerLine == 0) { input.ReadBoolean(); continue; } else if (count != 0 && count % bitsPerLine == 0) { Console.WriteLine(); } if (input.ReadBoolean()) { Console.Write(1); } else { Console.Write(0); } } if (bitsPerLine != 0) { Console.WriteLine(); } Console.WriteLine(count + " bits"); }