public uint Decode(double p0) { checked // check for int overflow { var rangeDiff = hi - lo; uint mid = Convert.ToUInt32(lo + p0 * (rangeDiff)); uint decompressedBit; if (previousBytes <= mid) { hi = mid; decompressedBit = 0; } else { lo = mid + 1; decompressedBit = 1; } while ((lo & msb) == (hi & msb)) { lo = lo << 1; // shift one written bit hi = (hi << 1) | 1; // shift one written bit and set LSB to 1 previousBytes = (previousBytes << 1) | _sourceFile.Read(); } return(decompressedBit); } }
public ArithmeticDecoder(BitFile sourceFile) { _sourceFile = sourceFile; for (int i = 0; i < 32; i++) // initialize with first 4 bytes { previousBytes = (previousBytes << 1) | sourceFile.Read(); } }
private void Compress(BitFile sourceFile, BitFile targetFile) { EncodeHeader(targetFile, sourceFile.Length); var predictor = new Predictor(); var encoder = new ArithmeticEncoder(targetFile); while (!sourceFile.IsEof) { var bit = sourceFile.Read(); double p0 = predictor.Predict(); // todo: remove casting encoder.Encode((int)bit, p0); predictor.Update((int)bit); } encoder.EncodeRemainingBits(); }