public void Write(byte[] cBytes, byte[] mBytes, byte[] yBytes, FileStream fStream) { byte[] fullBytes = new byte[cBytes.Length + mBytes.Length + yBytes.Length]; System.Buffer.BlockCopy(cBytes, 0, fullBytes, 0, cBytes.Length); System.Buffer.BlockCopy(mBytes, 0, fullBytes, cBytes.Length, mBytes.Length); System.Buffer.BlockCopy(yBytes, 0, fullBytes, cBytes.Length + mBytes.Length, yBytes.Length); HuffmanCode <byte> fHComp = new HuffmanCode <byte>(fullBytes); Dictionary <byte, HFCode> table = fHComp.GetWritingTable(); //Writing Dictionary Length byte[] dictionaryHeader = new byte[2]; dictionaryHeader = BitConverter.GetBytes((short)table.Count); fStream.Write(dictionaryHeader, 0, 2); //Writing Value and Length foreach Dictionary entry byte[] dictionaryBitLength = new byte[2]; foreach (KeyValuePair <byte, HFCode> pair in table) { fStream.WriteByte(pair.Key); dictionaryBitLength = BitConverter.GetBytes((ushort)pair.Value.length); fStream.Write(dictionaryBitLength, 0, 2); } //Writing Code for each Dictionary entry BitStream bitStream = new BitStream(fStream); foreach (KeyValuePair <byte, HFCode> pair in table) { bitStream.WriteBits(pair.Value.GetBitCode()); } //Write Data long dataLength = 0; BitArray buffer; for (int i = 0; i < cBytes.Length; ++i) { buffer = table[cBytes[i]].GetBitCode(); dataLength += buffer.Length; bitStream.WriteBits(buffer); } for (int i = 0; i < mBytes.Length; ++i) { buffer = table[mBytes[i]].GetBitCode(); dataLength += buffer.Length; bitStream.WriteBits(buffer); } for (int i = 0; i < yBytes.Length; ++i) { buffer = table[yBytes[i]].GetBitCode(); dataLength += buffer.Length; bitStream.WriteBits(buffer); } Console.WriteLine($"Written bits: {dataLength}: bytes: {(dataLength / 8) + 1} : avgBytes: {((double)dataLength / (double)fullBytes.Length)}"); bitStream.Flush(); fStream.Close(); }
public void Write(byte[] cBytes, byte[] mBytes, byte[] yBytes, FileStream fStream) { HuffmanCode <byte> cFHComp = new HuffmanCode <byte>(cBytes); Dictionary <byte, HFCode> cTable = cFHComp.GetWritingTable(); HuffmanCode <byte> mFHComp = new HuffmanCode <byte>(mBytes); Dictionary <byte, HFCode> mTable = mFHComp.GetWritingTable(); HuffmanCode <byte> yFHComp = new HuffmanCode <byte>(yBytes); Dictionary <byte, HFCode> yTable = yFHComp.GetWritingTable(); //Writing C Dictionary Length byte[] dictionaryHeader = new byte[2]; dictionaryHeader = BitConverter.GetBytes((short)cTable.Count); fStream.Write(dictionaryHeader, 0, 2); //Writing Value and Length foreach Dictionary entry byte[] dictionaryBitLength = new byte[2]; foreach (KeyValuePair <byte, HFCode> pair in cTable) { fStream.WriteByte(pair.Key); dictionaryBitLength = BitConverter.GetBytes((ushort)pair.Value.length); fStream.Write(dictionaryBitLength, 0, 2); } //Writing M Dictionary Length dictionaryHeader = BitConverter.GetBytes((short)mTable.Count); fStream.Write(dictionaryHeader, 0, 2); //Writing Value and Length foreach Dictionary entry foreach (KeyValuePair <byte, HFCode> pair in mTable) { fStream.WriteByte(pair.Key); dictionaryBitLength = BitConverter.GetBytes((ushort)pair.Value.length); fStream.Write(dictionaryBitLength, 0, 2); } //Writing Y Dictionary Length dictionaryHeader = BitConverter.GetBytes((short)yTable.Count); fStream.Write(dictionaryHeader, 0, 2); //Writing Value and Length foreach Dictionary entry foreach (KeyValuePair <byte, HFCode> pair in yTable) { fStream.WriteByte(pair.Key); dictionaryBitLength = BitConverter.GetBytes((ushort)pair.Value.length); fStream.Write(dictionaryBitLength, 0, 2); } //Writing Code for each Dictionary entry BitStream bitStream = new BitStream(fStream); foreach (KeyValuePair <byte, HFCode> pair in cTable) { bitStream.WriteBits(pair.Value.GetBitCode()); } foreach (KeyValuePair <byte, HFCode> pair in mTable) { bitStream.WriteBits(pair.Value.GetBitCode()); } foreach (KeyValuePair <byte, HFCode> pair in yTable) { bitStream.WriteBits(pair.Value.GetBitCode()); } //Write Data long dataLength = 0; BitArray buffer; for (int i = 0; i < cBytes.Length; ++i) { buffer = cTable[cBytes[i]].GetBitCode(); dataLength += buffer.Length; bitStream.WriteBits(buffer); } for (int i = 0; i < mBytes.Length; ++i) { buffer = mTable[mBytes[i]].GetBitCode(); dataLength += buffer.Length; bitStream.WriteBits(buffer); } for (int i = 0; i < yBytes.Length; ++i) { buffer = yTable[yBytes[i]].GetBitCode(); dataLength += buffer.Length; bitStream.WriteBits(buffer); } Console.WriteLine($"Written bits: {dataLength}: bytes: {(dataLength / 8) + 1} : avgBits: {((double)dataLength / (cBytes.Length + mBytes.Length + yBytes.Length))}"); bitStream.Flush(); fStream.Close(); }