public byte[] EncodeAllBytes(HuffmanTree huffmanTree, byte[] data) { var bitString = new BitString(); // Calculate filler bits var fillerBits = CreateFillerUnevenByte(huffmanTree); bitString.Append(fillerBits); // Append the encoded tree for (var i = 0; i < huffmanTree.EncodedTreeList.Count; ++i) { bitString.Append(huffmanTree.EncodedTreeList[i]); } // Encode all the bytes for (var i = 0; i < data.Length; i++) { Progress = i; bitString.Append(huffmanTree.CodeDictionary[data[i]]); } Progress = data.Length; return(bitString.ToArray()); }
public void Append_ByteArray_ShouldAppendData_WhenLastBlockNotFull() { var bitString = new BitString("100101"); bitString.Append(new byte[] { 0xA5 }); Assert.AreEqual(14, bitString.Length); Assert.AreEqual("10010110100101", bitString.ToBinString()); }
public void Append_ByteArray_ShouldDoNothing_WhenNullData() { var bitString = new BitString("11001"); byte[] data = null; bitString.Append(data); Assert.AreEqual(5, bitString.Length); Assert.AreEqual("11001", bitString.ToBinString()); }
protected virtual void Absorb(byte[] bytes, int length) { State.Clear(); var message = new BitString(bytes, length); var rate = State.Rate; message.Append(Suffix()); message.Append(GetPadding(rate, message.Length)); var n = message.Length / rate; var zeroes = new BitString(Capacity); BitString chunk; for (var i = 0; i < n; i++) { chunk = message.Substring(rate * i, rate); chunk.Append(zeroes); State.BitString.Xor(chunk); Function(); } }
/// <summary> /// This method encodes a single symbol by narrowing the interval. /// </summary> /// <param name="count"> The count of the symbol. </param> /// <param name="cumulativeCount"> The count of the symbol, and all of the symbols below it. </param> /// <param name="totalCount"> The total count of all the symbols. </param> public void Encode(int count, int cumulativeCount, int totalCount) { // Calculate cumulative count of previous symbol var prevCount = cumulativeCount - count; // Narrow the interval _interval.Narrow(prevCount, cumulativeCount, totalCount); // Expand the interval as long as possible. ExpansionType et; while ((et = _interval.Expand()) != ExpansionType.None) { // If middle expansion, increment follow bits, to account for intervals around the middle if (et == ExpansionType.Middle) { ++_followBits; } else { // Encode 0 or 1 depending on left or right expansion UnevenByte toEncode; if (et == ExpansionType.Left) { toEncode = UnevenByte.One; } else // ExpansionType is Zero { toEncode = UnevenByte.Zero; } _bitString.Append(toEncode); // If there are any follow bits, encode the complement of the bit follow bit times for (; _followBits > 0; --_followBits) { _bitString.Append(!toEncode); // Encode follow bits as the complement } } } }
protected virtual byte[] Squeeze(int outputLength) { var rate = State.Rate; var q = new BitString(); while (true) { q.Append(State.BitString.Truncate(rate)); if (q.Length >= outputLength) { return((q.Length == outputLength) ? q.Bytes : q.Truncate(outputLength).Bytes); } Function(); } }
public DataFile Compress(DataFile input) { var slidingWindow = new SlidingWindow(input.GetAllBytes()); var lzByteConverter = new LZByteConverter(); var bitString = new BitString(); while (!slidingWindow.AtEnd()) { // Encode the next byte var eb = slidingWindow.Slide(); // Convert to UnevenByte var ub = lzByteConverter.ToUnevenByte(eb); // Add to bitString bitString.Append(ub); } return(new DataFile(bitString.ToArray())); }