/// <summary> /// Renormalizes the working state by appending a bit from /// the ANS bitstream. If there still excists bits in the /// ANS bitstream. /// </summary> /// <param name="input">Input ANS bitstream.</param> /// <param name="state">Working state.</param> private void RenormalizeDecode(BitStreamIO.Reader input, ref int state) { while (state < Config.TotalFrequencyCounts) { state <<= 1; if (input.HasBits()) { state ^= input.GetBit(); } } }
/// <summary> /// Decodes metadata and bitstream into a list of bytes. /// </summary> /// <param name="input">Input ANS bitstream and metadata</param> public List <byte> DecodeANSBitStream(BitStreamIO.Reader input) { SymbolStreamIO.Writer output = new SymbolStreamIO.Writer(); int state = input.BitStream.EndState; for (int i = 0; i < (input.BitStream.UncompressedLength * 8); i++) { output.PutSymbol(DecodeSymbol(ref state)); RenormalizeDecode(input, ref state); } if (state != Config.TotalFrequencyCounts) { throw new ApplicationException("Decoding data corruption. Expected state " + Config.TotalFrequencyCounts.ToString() + ", Received " + state.ToString() + "."); } output.Reverse(); return(output.SymbolStream.Data); }
public void StreamDecodeFile(string inputFile, string outputFile) { StaticProbabilityANS.Decoder Decoder = new StaticProbabilityANS.Decoder(Alphabet, FrequencyCounts); BinaryReader InputFile = new BinaryReader(File.Open(inputFile, FileMode.Open)); int endState = InputFile.ReadInt32(); int EndBitOffset = InputFile.ReadInt32(); long UncompressedLength = InputFile.ReadInt64(); List <byte> input = new List <byte>(); for (int i = 16; i < InputFile.BaseStream.Length; i++) { input.Add(InputFile.ReadByte()); } BitStreamIO.Reader encodedChunk = new BitStreamIO.Reader(endState, EndBitOffset, input, UncompressedLength); List <byte> output = Decoder.DecodeANSBitStream(encodedChunk); BinaryWriter OutputFile = new BinaryWriter(File.Open(outputFile, FileMode.Create)); for (int i = 0; i < output.Count; i++) { OutputFile.Write(output[i]); } InputFile.Close(); OutputFile.Close(); }