private string Decode(BitArray bytes) { //First convert the compressed output to a bit array again again and skip trailing bits. //var bytes = Encoding.ASCII.GetBytes(input); _tree.BuildTree(); var boolAr = bytes.Cast <bool>().Take(_tree.BitCountForTree).ToArray(); var binary = new BitArray(boolAr); return(_tree.Decode(binary)); }
private byte[] Encode(string input) { _tree.BuildTree(input); //Build the huffman tree BitArray encoded = _tree.Encode(input); //Encode the tree //First show the generated binary output //Console.WriteLine(string.Join(string.Empty, encoded.Cast<bool>().Select(bit => bit ? "1" : "0"))); //Next, convert the binary output to the new characterized output string. byte[] bytes = new byte[(encoded.Length / 8) + 1]; encoded.CopyTo(bytes, 0); //string inText = Encoding.ASCII.GetString(bytes); //Console.WriteLine(inText); //Write the compressed output to the textbox. return(bytes); }
//Huffman Coding public string DoCompression() { FrequencyTable frequency = new FrequencyTable(); Dictionary <string, int> frequencyTable = frequency.BuildFrequencyTable(encodedFile); HuffmanTree tree = new HuffmanTree(); HuffmanNodes fullTree = tree.BuildTree(frequencyTable, frequency); IDictionary <string, string> binaryValues = tree.AssignCode(); binaryValues[binaryValues.First().Key] = "0"; //Stops eliminating first value foreach (KeyValuePair <string, string> kvp in binaryValues) { Console.WriteLine((kvp.Key == "\n" ? "EOF" : kvp.Key.ToString()) + ":\t" + kvp.Value); } string final = handleHuffmanFile(binaryValues); return(final); }
private void BuildTees() { _redTree.BuildTree(); _greenTree.BuildTree(); _blueTree.BuildTree(); }