public void Encode(int bit, double p0) { checked // check for int overflow { uint mid = Convert.ToUInt32(lo + p0 * (hi - lo)); if (bit == 1) { lo = mid + 1; } else { hi = mid; } while ((lo & msb) == (hi & msb)) { var writeBit = (lo & msb) >> shift; // & msb is probably useless here _targetFile.Write(writeBit); lo = lo << 1; // shift one written bit hi = (hi << 1) | 1; // shift one written bit and set LSB to 1 } } }
private void EncodeHeader(BitFile targetFile, long size) { string header = $"PAQ1\r\n{size}\0"; var headerBytes = Encoding.ASCII.GetBytes(header); foreach (var headerByte in headerBytes) { targetFile.Write(headerByte); } }
private void Decompress(BitFile sourceFile, BitFile targetFile) { long originalSize = DecodeHeader(sourceFile); var predictor = new Predictor(); var decoder = new ArithmeticDecoder(sourceFile); while (targetFile.Length < originalSize) { double p0 = predictor.Predict(); var bit = decoder.Decode(p0); targetFile.Write(bit); predictor.Update((int)bit); } }